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 | |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame^] | 63 | const time::nanoseconds EthernetFace::REASSEMBLER_LIFETIME = time::seconds(60); |
| 64 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 65 | EthernetFace::EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket, |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 66 | const NetworkInterfaceInfo& interface, |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 67 | const ethernet::Address& address) |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 68 | : Face(FaceUri(address), FaceUri::fromDev(interface.name)) |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 69 | , m_pcap(nullptr, pcap_close) |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 70 | , m_socket(socket) |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 71 | #if defined(__linux__) |
| 72 | , m_interfaceIndex(interface.index) |
| 73 | #endif |
Davide Pesavento | b499a60 | 2014-11-18 22:36:56 +0100 | [diff] [blame] | 74 | , m_interfaceName(interface.name) |
| 75 | , m_srcAddress(interface.etherAddress) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 76 | , m_destAddress(address) |
| 77 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 78 | NFD_LOG_INFO("Creating ethernet face on " << m_interfaceName << ": " |
| 79 | << m_srcAddress << " <--> " << m_destAddress); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 80 | pcapInit(); |
| 81 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 82 | int fd = pcap_get_selectable_fd(m_pcap.get()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 83 | if (fd < 0) |
| 84 | throw Error("pcap_get_selectable_fd() failed"); |
| 85 | |
| 86 | // need to duplicate the fd, otherwise both pcap_close() |
| 87 | // and stream_descriptor::close() will try to close the |
| 88 | // same fd and one of them will fail |
| 89 | m_socket->assign(::dup(fd)); |
| 90 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 91 | m_interfaceMtu = getInterfaceMtu(); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 92 | NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 93 | << "] Interface MTU is: " << m_interfaceMtu); |
| 94 | |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame^] | 95 | m_slicer.reset(new ndnlp::Slicer(m_interfaceMtu)); |
| 96 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 97 | char filter[100]; |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 98 | std::snprintf(filter, sizeof(filter), |
| 99 | "(ether proto 0x%x) && (ether dst %s) && (not ether src %s)", |
| 100 | ethernet::ETHERTYPE_NDN, |
| 101 | m_destAddress.toString().c_str(), |
| 102 | m_srcAddress.toString().c_str()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 103 | setPacketFilter(filter); |
| 104 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 105 | joinMulticastGroup(); |
| 106 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 107 | m_socket->async_read_some(boost::asio::null_buffers(), |
| 108 | bind(&EthernetFace::handleRead, this, |
| 109 | boost::asio::placeholders::error, |
| 110 | boost::asio::placeholders::bytes_transferred)); |
| 111 | } |
| 112 | |
| 113 | EthernetFace::~EthernetFace() |
| 114 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
| 118 | EthernetFace::sendInterest(const Interest& interest) |
| 119 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 120 | onSendInterest(interest); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame^] | 121 | ndnlp::PacketArray pa = m_slicer->slice(interest.wireEncode()); |
| 122 | for (const auto& packet : *pa) { |
| 123 | sendPacket(packet); |
| 124 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
| 128 | EthernetFace::sendData(const Data& data) |
| 129 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 130 | onSendData(data); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame^] | 131 | ndnlp::PacketArray pa = m_slicer->slice(data.wireEncode()); |
| 132 | for (const auto& packet : *pa) { |
| 133 | sendPacket(packet); |
| 134 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void |
| 138 | EthernetFace::close() |
| 139 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 140 | if (!m_pcap) |
| 141 | return; |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 142 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 143 | NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 144 | << "] Closing face"); |
| 145 | |
| 146 | boost::system::error_code error; |
| 147 | m_socket->cancel(error); // ignore errors |
| 148 | m_socket->close(error); // ignore errors |
| 149 | m_pcap.reset(nullptr); |
| 150 | |
| 151 | fail("Face closed"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void |
| 155 | EthernetFace::pcapInit() |
| 156 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 157 | char errbuf[PCAP_ERRBUF_SIZE] = {}; |
| 158 | m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 159 | if (!m_pcap) |
| 160 | throw Error("pcap_create(): " + std::string(errbuf)); |
| 161 | |
Davide Pesavento | f0ae442 | 2014-05-05 16:32:12 +0200 | [diff] [blame] | 162 | #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE |
| 163 | // Enable "immediate mode", effectively disabling any read buffering in the kernel. |
| 164 | // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including OS X) |
| 165 | // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3, |
| 166 | // even if the kernel supports it, thus preventing bug #1511. |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 167 | pcap_set_immediate_mode(m_pcap.get(), 1); |
Davide Pesavento | f0ae442 | 2014-05-05 16:32:12 +0200 | [diff] [blame] | 168 | #endif |
| 169 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 170 | if (pcap_activate(m_pcap.get()) < 0) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 171 | throw Error("pcap_activate() failed"); |
| 172 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 173 | if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0) |
| 174 | throw Error("pcap_set_datalink(): " + std::string(pcap_geterr(m_pcap.get()))); |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 175 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 176 | if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0) |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 177 | // no need to throw on failure, BPF will filter unwanted packets anyway |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 178 | NFD_LOG_WARN("pcap_setdirection(): " << pcap_geterr(m_pcap.get())); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void |
| 182 | EthernetFace::setPacketFilter(const char* filterString) |
| 183 | { |
| 184 | bpf_program filter; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 185 | if (pcap_compile(m_pcap.get(), &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0) |
| 186 | throw Error("pcap_compile(): " + std::string(pcap_geterr(m_pcap.get()))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 187 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 188 | int ret = pcap_setfilter(m_pcap.get(), &filter); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 189 | pcap_freecode(&filter); |
| 190 | if (ret < 0) |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 191 | throw Error("pcap_setfilter(): " + std::string(pcap_geterr(m_pcap.get()))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 195 | EthernetFace::joinMulticastGroup() |
| 196 | { |
| 197 | #if defined(__linux__) |
| 198 | packet_mreq mr{}; |
| 199 | mr.mr_ifindex = m_interfaceIndex; |
| 200 | mr.mr_type = PACKET_MR_MULTICAST; |
| 201 | mr.mr_alen = m_destAddress.size(); |
| 202 | std::copy(m_destAddress.begin(), m_destAddress.end(), mr.mr_address); |
| 203 | |
| 204 | if (::setsockopt(m_socket->native_handle(), SOL_PACKET, |
| 205 | PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0) |
| 206 | return; // success |
| 207 | |
| 208 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 209 | << "] setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno)); |
| 210 | #endif |
| 211 | |
| 212 | #if defined(SIOCADDMULTI) |
| 213 | ifreq ifr{}; |
| 214 | std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1); |
| 215 | |
| 216 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 217 | /* |
| 218 | * Differences between Linux and the BSDs (including OS X): |
| 219 | * o BSD does not have ifr_hwaddr; use ifr_addr instead. |
| 220 | * o While OS X seems to accept both AF_LINK and AF_UNSPEC as the address |
| 221 | * family, FreeBSD explicitly requires AF_LINK, so we have to use AF_LINK |
| 222 | * and sockaddr_dl instead of the generic sockaddr structure. |
| 223 | * o BSD's sockaddr (and sockaddr_dl in particular) contains an additional |
| 224 | * field, sa_len (sdl_len), which must be set to the total length of the |
| 225 | * structure, including the length field itself. |
| 226 | * o We do not specify the interface name, thus sdl_nlen is left at 0 and |
| 227 | * LLADDR is effectively the same as sdl_data. |
| 228 | */ |
| 229 | sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(&ifr.ifr_addr); |
| 230 | sdl->sdl_len = sizeof(ifr.ifr_addr); |
| 231 | sdl->sdl_family = AF_LINK; |
| 232 | sdl->sdl_alen = m_destAddress.size(); |
| 233 | std::copy(m_destAddress.begin(), m_destAddress.end(), LLADDR(sdl)); |
| 234 | |
| 235 | static_assert(sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) + ethernet::ADDR_LEN, |
| 236 | "ifr_addr in struct ifreq is too small on this platform"); |
| 237 | #else |
| 238 | ifr.ifr_hwaddr.sa_family = AF_UNSPEC; |
| 239 | std::copy(m_destAddress.begin(), m_destAddress.end(), ifr.ifr_hwaddr.sa_data); |
| 240 | |
| 241 | static_assert(sizeof(ifr.ifr_hwaddr) >= offsetof(sockaddr, sa_data) + ethernet::ADDR_LEN, |
| 242 | "ifr_hwaddr in struct ifreq is too small on this platform"); |
| 243 | #endif |
| 244 | |
| 245 | if (::ioctl(m_socket->native_handle(), SIOCADDMULTI, &ifr) >= 0) |
| 246 | return; // success |
| 247 | |
| 248 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 249 | << "] ioctl(SIOCADDMULTI) failed: " << std::strerror(errno)); |
| 250 | #endif |
| 251 | |
| 252 | if (!m_destAddress.isBroadcast()) |
| 253 | { |
| 254 | NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 255 | << "] Falling back to promiscuous mode"); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 256 | pcap_set_promisc(m_pcap.get(), 1); |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | void |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 261 | EthernetFace::sendPacket(const ndn::Block& block) |
| 262 | { |
| 263 | if (!m_pcap) |
| 264 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 265 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 266 | << "] Trying to send on closed face"); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 267 | return fail("Face closed"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame^] | 270 | BOOST_ASSERT(block.size() <= m_interfaceMtu); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 271 | |
| 272 | /// \todo Right now there is no reserve when packet is received, but |
| 273 | /// we should reserve some space at the beginning and at the end |
| 274 | ndn::EncodingBuffer buffer(block); |
| 275 | |
Davide Pesavento | e463100 | 2014-03-21 20:55:36 +0100 | [diff] [blame] | 276 | // pad with zeroes if the payload is too short |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 277 | if (block.size() < ethernet::MIN_DATA_LEN) |
| 278 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 279 | static const uint8_t padding[ethernet::MIN_DATA_LEN] = {0}; |
| 280 | buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // construct and prepend the ethernet header |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 284 | static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 285 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 286 | buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 287 | buffer.prependByteArray(m_destAddress.data(), m_destAddress.size()); |
| 288 | |
| 289 | // send the packet |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 290 | int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 291 | if (sent < 0) |
| 292 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 293 | return fail("pcap_inject(): " + std::string(pcap_geterr(m_pcap.get()))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 294 | } |
| 295 | else if (static_cast<size_t>(sent) < buffer.size()) |
| 296 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 297 | return fail("Failed to inject frame"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 300 | NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 4c1a078 | 2014-08-14 16:13:20 +0200 | [diff] [blame] | 301 | << "] Successfully sent: " << block.size() << " bytes"); |
| 302 | this->getMutableCounters().getNOutBytes() += block.size(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void |
| 306 | EthernetFace::handleRead(const boost::system::error_code& error, size_t) |
| 307 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 308 | if (!m_pcap) |
| 309 | return fail("Face closed"); |
| 310 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 311 | if (error) |
| 312 | return processErrorCode(error); |
| 313 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 314 | pcap_pkthdr* header; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 315 | const uint8_t* packet; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 316 | int ret = pcap_next_ex(m_pcap.get(), &header, &packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 317 | if (ret < 0) |
| 318 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 319 | return fail("pcap_next_ex(): " + std::string(pcap_geterr(m_pcap.get()))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 320 | } |
| 321 | else if (ret == 0) |
| 322 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 323 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] Read timeout"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 324 | } |
| 325 | else |
| 326 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 327 | processIncomingPacket(header, packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | m_socket->async_read_some(boost::asio::null_buffers(), |
| 331 | bind(&EthernetFace::handleRead, this, |
| 332 | boost::asio::placeholders::error, |
| 333 | boost::asio::placeholders::bytes_transferred)); |
| 334 | } |
| 335 | |
| 336 | void |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 337 | EthernetFace::processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 338 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 339 | size_t length = header->caplen; |
| 340 | if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 341 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 342 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 343 | << "] Received frame is too short (" << length << " bytes)"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 347 | const ether_header* eh = reinterpret_cast<const ether_header*>(packet); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame^] | 348 | const ethernet::Address sourceAddress(eh->ether_shost); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 349 | |
| 350 | // assert in case BPF fails to filter unwanted frames |
| 351 | BOOST_ASSERT_MSG(ethernet::Address(eh->ether_dhost) == m_destAddress, |
| 352 | "Received frame addressed to a different multicast group"); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame^] | 353 | BOOST_ASSERT_MSG(sourceAddress != m_srcAddress, |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 354 | "Received frame sent by this host"); |
| 355 | BOOST_ASSERT_MSG(ntohs(eh->ether_type) == ethernet::ETHERTYPE_NDN, |
| 356 | "Received frame with unrecognized ethertype"); |
| 357 | |
| 358 | packet += ethernet::HDR_LEN; |
| 359 | length -= ethernet::HDR_LEN; |
| 360 | |
| 361 | /// \todo Reserve space in front and at the back of the underlying buffer |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame^] | 362 | Block fragment; |
| 363 | bool isOk = Block::fromBuffer(packet, length, fragment); |
| 364 | if (!isOk) |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 365 | { |
| 366 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame^] | 367 | << "] Block received from " << sourceAddress.toString() |
| 368 | << " is invalid or too large to process"); |
| 369 | return; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 370 | } |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame^] | 371 | |
| 372 | NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 373 | << "] Received: " << fragment.size() << " bytes from " |
| 374 | << sourceAddress.toString()); |
| 375 | this->getMutableCounters().getNInBytes() += fragment.size(); |
| 376 | |
| 377 | Reassembler& reassembler = m_reassemblers[sourceAddress]; |
| 378 | if (!reassembler.pms) |
| 379 | { |
| 380 | // new sender, setup a PartialMessageStore for it |
| 381 | reassembler.pms.reset(new ndnlp::PartialMessageStore); |
| 382 | reassembler.pms->onReceive += |
| 383 | [this, sourceAddress] (const Block& block) { |
| 384 | NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 385 | << "] All fragments received from " << sourceAddress.toString()); |
| 386 | if (!decodeAndDispatchInput(block)) |
| 387 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 388 | << "] Received unrecognized TLV block of type " << block.type() |
| 389 | << " from " << sourceAddress.toString()); |
| 390 | }; |
| 391 | } |
| 392 | |
| 393 | scheduler::cancel(reassembler.expireEvent); |
| 394 | reassembler.expireEvent = scheduler::schedule(REASSEMBLER_LIFETIME, |
| 395 | [this, sourceAddress] { |
| 396 | BOOST_VERIFY(m_reassemblers.erase(sourceAddress) == 1); |
| 397 | }); |
| 398 | |
| 399 | try { |
| 400 | reassembler.pms->receiveNdnlpData(fragment); |
| 401 | } |
| 402 | catch (const ndnlp::ParseError& e) { |
| 403 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 404 | << "] Received invalid NDNLP fragment from " |
| 405 | << sourceAddress.toString() << " : " << e.what()); |
| 406 | } |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void |
| 410 | EthernetFace::processErrorCode(const boost::system::error_code& error) |
| 411 | { |
| 412 | if (error == boost::asio::error::operation_aborted) |
| 413 | // cancel() has been called on the socket |
| 414 | return; |
| 415 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 416 | std::string msg; |
| 417 | if (error == boost::asio::error::eof) |
| 418 | { |
| 419 | msg = "Face closed"; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 420 | NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 421 | } |
| 422 | else |
| 423 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 424 | msg = "Receive operation failed: " + error.message(); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 425 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 426 | } |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 427 | fail(msg); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 430 | size_t |
| 431 | EthernetFace::getInterfaceMtu() const |
| 432 | { |
| 433 | size_t mtu = ethernet::MAX_DATA_LEN; |
| 434 | |
| 435 | #ifdef SIOCGIFMTU |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 436 | ifreq ifr{}; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 437 | 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] | 438 | |
| 439 | if (::ioctl(m_socket->native_handle(), SIOCGIFMTU, &ifr) < 0) |
| 440 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 441 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 442 | << "] Failed to get interface MTU: " << std::strerror(errno)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 443 | } |
| 444 | else |
| 445 | { |
| 446 | mtu = std::min(mtu, static_cast<size_t>(ifr.ifr_mtu)); |
| 447 | } |
| 448 | #endif |
| 449 | |
| 450 | return mtu; |
| 451 | } |
| 452 | |
| 453 | } // namespace nfd |