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