Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 24 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 25 | |
| 26 | #include "ethernet-face.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 28 | |
| 29 | #include <pcap/pcap.h> |
| 30 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 31 | #include <cerrno> // for errno |
| 32 | #include <cstdio> // for std::snprintf() |
| 33 | #include <cstring> // for std::strerror() and std::strncpy() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 34 | #include <arpa/inet.h> // for htons() and ntohs() |
| 35 | #include <net/ethernet.h> // for struct ether_header |
| 36 | #include <net/if.h> // for struct ifreq |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 37 | #include <sys/ioctl.h> // for ioctl() |
| 38 | #include <unistd.h> // for dup() |
| 39 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 40 | #if defined(__linux__) |
| 41 | #include <netpacket/packet.h> // for struct packet_mreq |
| 42 | #include <sys/socket.h> // for setsockopt() |
| 43 | #endif |
| 44 | |
| 45 | #ifdef SIOCADDMULTI |
| 46 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 47 | #include <net/if_dl.h> // for struct sockaddr_dl |
| 48 | #endif |
| 49 | #endif |
| 50 | |
Alexander Afanasyev | d9479aa | 2014-06-26 13:52:02 -0700 | [diff] [blame] | 51 | #if !defined(PCAP_NETMASK_UNKNOWN) |
| 52 | /* |
| 53 | * Value to pass to pcap_compile() as the netmask if you don't know what |
| 54 | * the netmask is. |
| 55 | */ |
| 56 | #define PCAP_NETMASK_UNKNOWN 0xffffffff |
| 57 | #endif |
| 58 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 59 | namespace nfd { |
| 60 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 61 | NFD_LOG_INIT("EthernetFace"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 62 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 63 | EthernetFace::EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket, |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 64 | const NetworkInterfaceInfo& interface, |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 65 | const ethernet::Address& address) |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 66 | : Face(FaceUri(address), FaceUri::fromDev(interface.name)) |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 67 | , m_pcap(nullptr, pcap_close) |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 68 | , m_socket(socket) |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 69 | #if defined(__linux__) |
| 70 | , m_interfaceIndex(interface.index) |
| 71 | #endif |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 72 | , m_interfaceName(interface.name) |
| 73 | , m_srcAddress(interface.etherAddress) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 74 | , m_destAddress(address) |
| 75 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 76 | NFD_LOG_INFO("Creating ethernet face on " << m_interfaceName << ": " |
| 77 | << m_srcAddress << " <--> " << m_destAddress); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 78 | pcapInit(); |
| 79 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 80 | int fd = pcap_get_selectable_fd(m_pcap.get()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 81 | if (fd < 0) |
| 82 | throw Error("pcap_get_selectable_fd() failed"); |
| 83 | |
| 84 | // need to duplicate the fd, otherwise both pcap_close() |
| 85 | // and stream_descriptor::close() will try to close the |
| 86 | // same fd and one of them will fail |
| 87 | m_socket->assign(::dup(fd)); |
| 88 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 89 | m_interfaceMtu = getInterfaceMtu(); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 90 | NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 91 | << "] Interface MTU is: " << m_interfaceMtu); |
| 92 | |
| 93 | char filter[100]; |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 94 | std::snprintf(filter, sizeof(filter), |
| 95 | "(ether proto 0x%x) && (ether dst %s) && (not ether src %s)", |
| 96 | ethernet::ETHERTYPE_NDN, |
| 97 | m_destAddress.toString().c_str(), |
| 98 | m_srcAddress.toString().c_str()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 99 | setPacketFilter(filter); |
| 100 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 101 | joinMulticastGroup(); |
| 102 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 103 | m_socket->async_read_some(boost::asio::null_buffers(), |
| 104 | bind(&EthernetFace::handleRead, this, |
| 105 | boost::asio::placeholders::error, |
| 106 | boost::asio::placeholders::bytes_transferred)); |
| 107 | } |
| 108 | |
| 109 | EthernetFace::~EthernetFace() |
| 110 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
| 114 | EthernetFace::sendInterest(const Interest& interest) |
| 115 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 116 | onSendInterest(interest); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 117 | sendPacket(interest.wireEncode()); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | EthernetFace::sendData(const Data& data) |
| 122 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 123 | onSendData(data); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 124 | sendPacket(data.wireEncode()); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | EthernetFace::close() |
| 129 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 130 | if (!m_pcap) |
| 131 | return; |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 132 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 133 | NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 134 | << "] Closing face"); |
| 135 | |
| 136 | boost::system::error_code error; |
| 137 | m_socket->cancel(error); // ignore errors |
| 138 | m_socket->close(error); // ignore errors |
| 139 | m_pcap.reset(nullptr); |
| 140 | |
| 141 | fail("Face closed"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
| 145 | EthernetFace::pcapInit() |
| 146 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 147 | char errbuf[PCAP_ERRBUF_SIZE] = {}; |
| 148 | m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 149 | if (!m_pcap) |
| 150 | throw Error("pcap_create(): " + std::string(errbuf)); |
| 151 | |
Davide Pesavento | f0ae442 | 2014-05-05 16:32:12 +0200 | [diff] [blame] | 152 | #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE |
| 153 | // Enable "immediate mode", effectively disabling any read buffering in the kernel. |
| 154 | // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including OS X) |
| 155 | // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3, |
| 156 | // even if the kernel supports it, thus preventing bug #1511. |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 157 | pcap_set_immediate_mode(m_pcap.get(), 1); |
Davide Pesavento | f0ae442 | 2014-05-05 16:32:12 +0200 | [diff] [blame] | 158 | #endif |
| 159 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 160 | if (pcap_activate(m_pcap.get()) < 0) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 161 | throw Error("pcap_activate() failed"); |
| 162 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 163 | if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0) |
| 164 | throw Error("pcap_set_datalink(): " + std::string(pcap_geterr(m_pcap.get()))); |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 165 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 166 | if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0) |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 167 | // no need to throw on failure, BPF will filter unwanted packets anyway |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 168 | NFD_LOG_WARN("pcap_setdirection(): " << pcap_geterr(m_pcap.get())); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void |
| 172 | EthernetFace::setPacketFilter(const char* filterString) |
| 173 | { |
| 174 | bpf_program filter; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 175 | if (pcap_compile(m_pcap.get(), &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0) |
| 176 | throw Error("pcap_compile(): " + std::string(pcap_geterr(m_pcap.get()))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 177 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 178 | int ret = pcap_setfilter(m_pcap.get(), &filter); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 179 | pcap_freecode(&filter); |
| 180 | if (ret < 0) |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 181 | throw Error("pcap_setfilter(): " + std::string(pcap_geterr(m_pcap.get()))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 185 | EthernetFace::joinMulticastGroup() |
| 186 | { |
| 187 | #if defined(__linux__) |
| 188 | packet_mreq mr{}; |
| 189 | mr.mr_ifindex = m_interfaceIndex; |
| 190 | mr.mr_type = PACKET_MR_MULTICAST; |
| 191 | mr.mr_alen = m_destAddress.size(); |
| 192 | std::copy(m_destAddress.begin(), m_destAddress.end(), mr.mr_address); |
| 193 | |
| 194 | if (::setsockopt(m_socket->native_handle(), SOL_PACKET, |
| 195 | PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0) |
| 196 | return; // success |
| 197 | |
| 198 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 199 | << "] setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno)); |
| 200 | #endif |
| 201 | |
| 202 | #if defined(SIOCADDMULTI) |
| 203 | ifreq ifr{}; |
| 204 | std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1); |
| 205 | |
| 206 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 207 | /* |
| 208 | * Differences between Linux and the BSDs (including OS X): |
| 209 | * o BSD does not have ifr_hwaddr; use ifr_addr instead. |
| 210 | * o While OS X seems to accept both AF_LINK and AF_UNSPEC as the address |
| 211 | * family, FreeBSD explicitly requires AF_LINK, so we have to use AF_LINK |
| 212 | * and sockaddr_dl instead of the generic sockaddr structure. |
| 213 | * o BSD's sockaddr (and sockaddr_dl in particular) contains an additional |
| 214 | * field, sa_len (sdl_len), which must be set to the total length of the |
| 215 | * structure, including the length field itself. |
| 216 | * o We do not specify the interface name, thus sdl_nlen is left at 0 and |
| 217 | * LLADDR is effectively the same as sdl_data. |
| 218 | */ |
| 219 | sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(&ifr.ifr_addr); |
| 220 | sdl->sdl_len = sizeof(ifr.ifr_addr); |
| 221 | sdl->sdl_family = AF_LINK; |
| 222 | sdl->sdl_alen = m_destAddress.size(); |
| 223 | std::copy(m_destAddress.begin(), m_destAddress.end(), LLADDR(sdl)); |
| 224 | |
| 225 | static_assert(sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) + ethernet::ADDR_LEN, |
| 226 | "ifr_addr in struct ifreq is too small on this platform"); |
| 227 | #else |
| 228 | ifr.ifr_hwaddr.sa_family = AF_UNSPEC; |
| 229 | std::copy(m_destAddress.begin(), m_destAddress.end(), ifr.ifr_hwaddr.sa_data); |
| 230 | |
| 231 | static_assert(sizeof(ifr.ifr_hwaddr) >= offsetof(sockaddr, sa_data) + ethernet::ADDR_LEN, |
| 232 | "ifr_hwaddr in struct ifreq is too small on this platform"); |
| 233 | #endif |
| 234 | |
| 235 | if (::ioctl(m_socket->native_handle(), SIOCADDMULTI, &ifr) >= 0) |
| 236 | return; // success |
| 237 | |
| 238 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 239 | << "] ioctl(SIOCADDMULTI) failed: " << std::strerror(errno)); |
| 240 | #endif |
| 241 | |
| 242 | if (!m_destAddress.isBroadcast()) |
| 243 | { |
| 244 | NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 245 | << "] Falling back to promiscuous mode"); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 246 | pcap_set_promisc(m_pcap.get(), 1); |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | void |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 251 | EthernetFace::sendPacket(const ndn::Block& block) |
| 252 | { |
| 253 | if (!m_pcap) |
| 254 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 255 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 256 | << "] Trying to send on closed face"); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 257 | return fail("Face closed"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 260 | /// \todo Fragmentation |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 261 | if (block.size() > m_interfaceMtu) |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 262 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 263 | NFD_LOG_ERROR("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 264 | << "] Fragmentation not implemented: dropping packet larger than MTU"); |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 265 | return; |
| 266 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 267 | |
| 268 | /// \todo Right now there is no reserve when packet is received, but |
| 269 | /// we should reserve some space at the beginning and at the end |
| 270 | ndn::EncodingBuffer buffer(block); |
| 271 | |
Davide Pesavento | e463100 | 2014-03-21 20:55:36 +0100 | [diff] [blame] | 272 | // pad with zeroes if the payload is too short |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 273 | if (block.size() < ethernet::MIN_DATA_LEN) |
| 274 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 275 | static const uint8_t padding[ethernet::MIN_DATA_LEN] = {0}; |
| 276 | buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // construct and prepend the ethernet header |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 280 | static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 281 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 282 | buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 283 | buffer.prependByteArray(m_destAddress.data(), m_destAddress.size()); |
| 284 | |
| 285 | // send the packet |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 286 | int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 287 | if (sent < 0) |
| 288 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 289 | return fail("pcap_inject(): " + std::string(pcap_geterr(m_pcap.get()))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 290 | } |
| 291 | else if (static_cast<size_t>(sent) < buffer.size()) |
| 292 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 293 | return fail("Failed to inject frame"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 296 | NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 4c1a078 | 2014-08-14 16:13:20 +0200 | [diff] [blame] | 297 | << "] Successfully sent: " << block.size() << " bytes"); |
| 298 | this->getMutableCounters().getNOutBytes() += block.size(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void |
| 302 | EthernetFace::handleRead(const boost::system::error_code& error, size_t) |
| 303 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 304 | if (!m_pcap) |
| 305 | return fail("Face closed"); |
| 306 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 307 | if (error) |
| 308 | return processErrorCode(error); |
| 309 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 310 | pcap_pkthdr* header; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 311 | const uint8_t* packet; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 312 | int ret = pcap_next_ex(m_pcap.get(), &header, &packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 313 | if (ret < 0) |
| 314 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 315 | return fail("pcap_next_ex(): " + std::string(pcap_geterr(m_pcap.get()))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 316 | } |
| 317 | else if (ret == 0) |
| 318 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 319 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] Read timeout"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 320 | } |
| 321 | else |
| 322 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 323 | processIncomingPacket(header, packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | m_socket->async_read_some(boost::asio::null_buffers(), |
| 327 | bind(&EthernetFace::handleRead, this, |
| 328 | boost::asio::placeholders::error, |
| 329 | boost::asio::placeholders::bytes_transferred)); |
| 330 | } |
| 331 | |
| 332 | void |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 333 | EthernetFace::processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 334 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 335 | size_t length = header->caplen; |
| 336 | if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 337 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 338 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 339 | << "] Received frame is too short (" << length << " bytes)"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 343 | const ether_header* eh = reinterpret_cast<const ether_header*>(packet); |
| 344 | |
| 345 | // assert in case BPF fails to filter unwanted frames |
| 346 | BOOST_ASSERT_MSG(ethernet::Address(eh->ether_dhost) == m_destAddress, |
| 347 | "Received frame addressed to a different multicast group"); |
| 348 | BOOST_ASSERT_MSG(ethernet::Address(eh->ether_shost) != m_srcAddress, |
| 349 | "Received frame sent by this host"); |
| 350 | BOOST_ASSERT_MSG(ntohs(eh->ether_type) == ethernet::ETHERTYPE_NDN, |
| 351 | "Received frame with unrecognized ethertype"); |
| 352 | |
| 353 | packet += ethernet::HDR_LEN; |
| 354 | length -= ethernet::HDR_LEN; |
| 355 | |
| 356 | /// \todo Reserve space in front and at the back of the underlying buffer |
| 357 | Block element; |
| 358 | bool isOk = Block::fromBuffer(packet, length, element); |
| 359 | if (isOk) |
| 360 | { |
| 361 | NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 362 | << "] Received: " << element.size() << " bytes"); |
| 363 | this->getMutableCounters().getNInBytes() += element.size(); |
| 364 | |
| 365 | if (!decodeAndDispatchInput(element)) |
| 366 | { |
| 367 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 368 | << "] Received unrecognized block of type " << element.type()); |
| 369 | } |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 374 | << "] Received block is invalid or too large to process"); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | void |
| 379 | EthernetFace::processErrorCode(const boost::system::error_code& error) |
| 380 | { |
| 381 | if (error == boost::asio::error::operation_aborted) |
| 382 | // cancel() has been called on the socket |
| 383 | return; |
| 384 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 385 | std::string msg; |
| 386 | if (error == boost::asio::error::eof) |
| 387 | { |
| 388 | msg = "Face closed"; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 389 | NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 390 | } |
| 391 | else |
| 392 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame^] | 393 | msg = "Receive operation failed: " + error.message(); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 394 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 395 | } |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 396 | fail(msg); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 399 | size_t |
| 400 | EthernetFace::getInterfaceMtu() const |
| 401 | { |
| 402 | size_t mtu = ethernet::MAX_DATA_LEN; |
| 403 | |
| 404 | #ifdef SIOCGIFMTU |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 405 | ifreq ifr{}; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 406 | std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 407 | |
| 408 | if (::ioctl(m_socket->native_handle(), SIOCGIFMTU, &ifr) < 0) |
| 409 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 410 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 411 | << "] Failed to get interface MTU: " << std::strerror(errno)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 412 | } |
| 413 | else |
| 414 | { |
| 415 | mtu = std::min(mtu, static_cast<size_t>(ifr.ifr_mtu)); |
| 416 | } |
| 417 | #endif |
| 418 | |
| 419 | return mtu; |
| 420 | } |
| 421 | |
| 422 | } // namespace nfd |