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 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 26 | #include "ethernet-transport.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 |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 32 | #include <cstring> // for memcpy(), strerror(), strncpy() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 33 | #include <arpa/inet.h> // for htons() and ntohs() |
| 34 | #include <net/ethernet.h> // for struct ether_header |
| 35 | #include <net/if.h> // for struct ifreq |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 36 | #include <stdio.h> // for snprintf() |
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 { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 60 | namespace face { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 61 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 62 | NFD_LOG_INIT("EthernetTransport"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 63 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 64 | EthernetTransport::EthernetTransport(const NetworkInterfaceInfo& interface, |
| 65 | const ethernet::Address& mcastAddress) |
| 66 | : m_pcap(nullptr, pcap_close) |
| 67 | , m_socket(getGlobalIoService()) |
| 68 | , m_srcAddress(interface.etherAddress) |
| 69 | , m_destAddress(mcastAddress) |
| 70 | , m_interfaceName(interface.name) |
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 | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 74 | #ifdef _DEBUG |
| 75 | , m_nDropped(0) |
| 76 | #endif |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 77 | { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 78 | this->setLocalUri(FaceUri::fromDev(interface.name)); |
| 79 | this->setRemoteUri(FaceUri(mcastAddress)); |
| 80 | this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 81 | this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 82 | this->setLinkType(ndn::nfd::LINK_TYPE_MULTI_ACCESS); |
| 83 | |
| 84 | NFD_LOG_FACE_INFO("Creating transport"); |
| 85 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 86 | pcapInit(); |
| 87 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 88 | int fd = pcap_get_selectable_fd(m_pcap.get()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 89 | if (fd < 0) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 90 | BOOST_THROW_EXCEPTION(Error("pcap_get_selectable_fd failed")); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 91 | |
| 92 | // need to duplicate the fd, otherwise both pcap_close() |
| 93 | // and stream_descriptor::close() will try to close the |
| 94 | // same fd and one of them will fail |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 95 | m_socket.assign(::dup(fd)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 96 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 97 | // do this after assigning m_socket because getInterfaceMtu uses it |
| 98 | this->setMtu(getInterfaceMtu()); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 99 | |
Davide Pesavento | ca25d72 | 2015-12-21 21:04:07 +0100 | [diff] [blame] | 100 | char filter[110]; |
| 101 | // note #1: we cannot use std::snprintf because it's not available |
| 102 | // on some platforms (see #2299) |
| 103 | // note #2: "not vlan" must appear last in the filter expression, or the |
| 104 | // rest of the filter won't work as intended (see pcap-filter(7)) |
Mathias Gibbens | 802c092 | 2015-04-14 13:47:22 -0700 | [diff] [blame] | 105 | snprintf(filter, sizeof(filter), |
Davide Pesavento | ca25d72 | 2015-12-21 21:04:07 +0100 | [diff] [blame] | 106 | "(ether proto 0x%x) && (ether dst %s) && (not ether src %s) && (not vlan)", |
Mathias Gibbens | 802c092 | 2015-04-14 13:47:22 -0700 | [diff] [blame] | 107 | ethernet::ETHERTYPE_NDN, |
| 108 | m_destAddress.toString().c_str(), |
| 109 | m_srcAddress.toString().c_str()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 110 | setPacketFilter(filter); |
| 111 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 112 | if (!m_destAddress.isBroadcast() && !joinMulticastGroup()) { |
| 113 | NFD_LOG_FACE_WARN("Falling back to promiscuous mode"); |
| 114 | pcap_set_promisc(m_pcap.get(), 1); |
| 115 | } |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 116 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 117 | m_socket.async_read_some(boost::asio::null_buffers(), |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 118 | bind(&EthernetTransport::handleRead, this, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 119 | boost::asio::placeholders::error, |
| 120 | boost::asio::placeholders::bytes_transferred)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 123 | void EthernetTransport::beforeChangePersistency(ndn::nfd::FacePersistency newPersistency) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 124 | { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 125 | if (newPersistency != ndn::nfd::FACE_PERSISTENCY_PERMANENT) { |
| 126 | BOOST_THROW_EXCEPTION( |
| 127 | std::invalid_argument("EthernetTransport supports only FACE_PERSISTENCY_PERMANENT")); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 128 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 131 | void EthernetTransport::doSend(Transport::Packet&& packet) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 132 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 133 | NFD_LOG_FACE_TRACE(__func__); |
| 134 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 135 | sendPacket(packet.packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 138 | void EthernetTransport::doClose() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 139 | { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 140 | NFD_LOG_FACE_TRACE(__func__); |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 141 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 142 | if (m_socket.is_open()) { |
| 143 | // Cancel all outstanding operations and close the socket. |
| 144 | // Use the non-throwing variants and ignore errors, if any. |
| 145 | boost::system::error_code error; |
| 146 | m_socket.cancel(error); |
| 147 | m_socket.close(error); |
| 148 | } |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 149 | m_pcap.reset(); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 150 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 151 | // Ensure that the Transport stays alive at least |
| 152 | // until all pending handlers are dispatched |
| 153 | getGlobalIoService().post([this] { |
| 154 | this->setState(TransportState::CLOSED); |
| 155 | }); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 159 | EthernetTransport::pcapInit() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 160 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 161 | char errbuf[PCAP_ERRBUF_SIZE] = {}; |
| 162 | m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 163 | if (!m_pcap) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 164 | BOOST_THROW_EXCEPTION(Error("pcap_create: " + std::string(errbuf))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 165 | |
Davide Pesavento | f0ae442 | 2014-05-05 16:32:12 +0200 | [diff] [blame] | 166 | #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE |
| 167 | // Enable "immediate mode", effectively disabling any read buffering in the kernel. |
| 168 | // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including OS X) |
| 169 | // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3, |
| 170 | // even if the kernel supports it, thus preventing bug #1511. |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 171 | pcap_set_immediate_mode(m_pcap.get(), 1); |
Davide Pesavento | f0ae442 | 2014-05-05 16:32:12 +0200 | [diff] [blame] | 172 | #endif |
| 173 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 174 | if (pcap_activate(m_pcap.get()) < 0) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 175 | BOOST_THROW_EXCEPTION(Error("pcap_activate failed")); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 176 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 177 | if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 178 | BOOST_THROW_EXCEPTION(Error("pcap_set_datalink: " + std::string(pcap_geterr(m_pcap.get())))); |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 179 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 180 | if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0) |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 181 | // no need to throw on failure, BPF will filter unwanted packets anyway |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 182 | NFD_LOG_FACE_WARN("pcap_setdirection failed: " << pcap_geterr(m_pcap.get())); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 186 | EthernetTransport::setPacketFilter(const char* filterString) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 187 | { |
| 188 | bpf_program filter; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 189 | if (pcap_compile(m_pcap.get(), &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 190 | BOOST_THROW_EXCEPTION(Error("pcap_compile: " + std::string(pcap_geterr(m_pcap.get())))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 191 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 192 | int ret = pcap_setfilter(m_pcap.get(), &filter); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 193 | pcap_freecode(&filter); |
| 194 | if (ret < 0) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 195 | BOOST_THROW_EXCEPTION(Error("pcap_setfilter: " + std::string(pcap_geterr(m_pcap.get())))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 198 | bool |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 199 | EthernetTransport::joinMulticastGroup() |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 200 | { |
| 201 | #if defined(__linux__) |
| 202 | packet_mreq mr{}; |
| 203 | mr.mr_ifindex = m_interfaceIndex; |
| 204 | mr.mr_type = PACKET_MR_MULTICAST; |
| 205 | mr.mr_alen = m_destAddress.size(); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 206 | std::memcpy(mr.mr_address, m_destAddress.data(), m_destAddress.size()); |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 207 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 208 | if (::setsockopt(m_socket.native_handle(), SOL_PACKET, |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 209 | PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0) |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 210 | return true; // success |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 211 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 212 | NFD_LOG_FACE_WARN("setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno)); |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 213 | #endif |
| 214 | |
| 215 | #if defined(SIOCADDMULTI) |
| 216 | ifreq ifr{}; |
| 217 | std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1); |
| 218 | |
| 219 | #if defined(__APPLE__) || defined(__FreeBSD__) |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 220 | // see bug #2327 |
| 221 | using boost::asio::ip::udp; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 222 | udp::socket sock(getGlobalIoService(), udp::v4()); |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 223 | int fd = sock.native_handle(); |
| 224 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 225 | /* |
| 226 | * Differences between Linux and the BSDs (including OS X): |
| 227 | * o BSD does not have ifr_hwaddr; use ifr_addr instead. |
| 228 | * o While OS X seems to accept both AF_LINK and AF_UNSPEC as the address |
| 229 | * family, FreeBSD explicitly requires AF_LINK, so we have to use AF_LINK |
| 230 | * and sockaddr_dl instead of the generic sockaddr structure. |
| 231 | * o BSD's sockaddr (and sockaddr_dl in particular) contains an additional |
| 232 | * field, sa_len (sdl_len), which must be set to the total length of the |
| 233 | * structure, including the length field itself. |
| 234 | * o We do not specify the interface name, thus sdl_nlen is left at 0 and |
| 235 | * LLADDR is effectively the same as sdl_data. |
| 236 | */ |
| 237 | sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(&ifr.ifr_addr); |
| 238 | sdl->sdl_len = sizeof(ifr.ifr_addr); |
| 239 | sdl->sdl_family = AF_LINK; |
| 240 | sdl->sdl_alen = m_destAddress.size(); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 241 | std::memcpy(LLADDR(sdl), m_destAddress.data(), m_destAddress.size()); |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 242 | |
| 243 | static_assert(sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) + ethernet::ADDR_LEN, |
| 244 | "ifr_addr in struct ifreq is too small on this platform"); |
| 245 | #else |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 246 | int fd = m_socket.native_handle(); |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 247 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 248 | ifr.ifr_hwaddr.sa_family = AF_UNSPEC; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 249 | std::memcpy(ifr.ifr_hwaddr.sa_data, m_destAddress.data(), m_destAddress.size()); |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 250 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 251 | static_assert(sizeof(ifr.ifr_hwaddr.sa_data) >= ethernet::ADDR_LEN, |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 252 | "ifr_hwaddr in struct ifreq is too small on this platform"); |
| 253 | #endif |
| 254 | |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 255 | if (::ioctl(fd, SIOCADDMULTI, &ifr) == 0) |
| 256 | return true; // success |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 257 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 258 | NFD_LOG_FACE_WARN("ioctl(SIOCADDMULTI) failed: " << std::strerror(errno)); |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 259 | #endif |
| 260 | |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 261 | return false; |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 265 | EthernetTransport::sendPacket(const ndn::Block& block) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 266 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 267 | /// \todo Right now there is no reserve when packet is received, but |
| 268 | /// we should reserve some space at the beginning and at the end |
| 269 | ndn::EncodingBuffer buffer(block); |
| 270 | |
Davide Pesavento | e463100 | 2014-03-21 20:55:36 +0100 | [diff] [blame] | 271 | // pad with zeroes if the payload is too short |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 272 | if (block.size() < ethernet::MIN_DATA_LEN) { |
| 273 | static const uint8_t padding[ethernet::MIN_DATA_LEN] = {}; |
| 274 | buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size()); |
| 275 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 276 | |
| 277 | // construct and prepend the ethernet header |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 278 | static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 279 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 280 | buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 281 | buffer.prependByteArray(m_destAddress.data(), m_destAddress.size()); |
| 282 | |
| 283 | // send the packet |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 284 | int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 285 | if (sent < 0) |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 286 | NFD_LOG_FACE_ERROR("pcap_inject failed: " << pcap_geterr(m_pcap.get())); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 287 | else if (static_cast<size_t>(sent) < buffer.size()) |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 288 | NFD_LOG_FACE_ERROR("Failed to send the full frame: bufsize=" << buffer.size() << " sent=" << sent); |
| 289 | else |
| 290 | // print block size because we don't want to count the padding in buffer |
| 291 | NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 295 | EthernetTransport::handleRead(const boost::system::error_code& error, size_t) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 296 | { |
| 297 | if (error) |
| 298 | return processErrorCode(error); |
| 299 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 300 | pcap_pkthdr* header; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 301 | const uint8_t* packet; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 302 | |
| 303 | // read the pcap header and packet data |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 304 | int ret = pcap_next_ex(m_pcap.get(), &header, &packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 305 | if (ret < 0) |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 306 | NFD_LOG_FACE_ERROR("pcap_next_ex failed: " << pcap_geterr(m_pcap.get())); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 307 | else if (ret == 0) |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 308 | NFD_LOG_FACE_WARN("Read timeout"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 309 | else |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 310 | processIncomingPacket(header, packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 311 | |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 312 | #ifdef _DEBUG |
| 313 | pcap_stat ps{}; |
| 314 | ret = pcap_stats(m_pcap.get(), &ps); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 315 | if (ret < 0) { |
| 316 | NFD_LOG_FACE_DEBUG("pcap_stats failed: " << pcap_geterr(m_pcap.get())); |
| 317 | } |
| 318 | else if (ret == 0) { |
| 319 | if (ps.ps_drop - m_nDropped > 0) |
| 320 | NFD_LOG_FACE_DEBUG("Detected " << ps.ps_drop - m_nDropped << " dropped packet(s)"); |
| 321 | m_nDropped = ps.ps_drop; |
| 322 | } |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 323 | #endif |
| 324 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 325 | m_socket.async_read_some(boost::asio::null_buffers(), |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 326 | bind(&EthernetTransport::handleRead, this, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 327 | boost::asio::placeholders::error, |
| 328 | boost::asio::placeholders::bytes_transferred)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 332 | EthernetTransport::processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 333 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 334 | size_t length = header->caplen; |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 335 | if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN) { |
| 336 | NFD_LOG_FACE_WARN("Received frame is too short (" << length << " bytes)"); |
| 337 | return; |
| 338 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 339 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 340 | const ether_header* eh = reinterpret_cast<const ether_header*>(packet); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 341 | const ethernet::Address sourceAddress(eh->ether_shost); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 342 | |
Davide Pesavento | ca25d72 | 2015-12-21 21:04:07 +0100 | [diff] [blame] | 343 | // in some cases VLAN-tagged frames may survive the BPF filter, |
| 344 | // make sure we do not process those frames (see #3348) |
| 345 | if (ntohs(eh->ether_type) != ethernet::ETHERTYPE_NDN) |
| 346 | return; |
| 347 | |
| 348 | // check that our BPF filter is working correctly |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 349 | BOOST_ASSERT_MSG(ethernet::Address(eh->ether_dhost) == m_destAddress, |
| 350 | "Received frame addressed to a different multicast group"); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 351 | BOOST_ASSERT_MSG(sourceAddress != m_srcAddress, |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 352 | "Received frame sent by this host"); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 353 | |
| 354 | packet += ethernet::HDR_LEN; |
| 355 | length -= ethernet::HDR_LEN; |
| 356 | |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 357 | bool isOk = false; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 358 | Block element; |
| 359 | std::tie(isOk, element) = Block::fromBuffer(packet, length); |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 360 | if (!isOk) { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 361 | NFD_LOG_FACE_WARN("Received invalid packet from " << sourceAddress.toString()); |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 362 | return; |
| 363 | } |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 364 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 365 | NFD_LOG_FACE_TRACE("Received: " << element.size() << " bytes from " << sourceAddress.toString()); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 366 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 367 | Transport::Packet tp(std::move(element)); |
| 368 | static_assert(sizeof(tp.remoteEndpoint) >= ethernet::ADDR_LEN, |
| 369 | "Transport::Packet::remoteEndpoint is too small"); |
| 370 | std::memcpy(&tp.remoteEndpoint, sourceAddress.data(), sourceAddress.size()); |
| 371 | this->receive(std::move(tp)); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 375 | EthernetTransport::processErrorCode(const boost::system::error_code& error) |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 376 | { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 377 | NFD_LOG_FACE_TRACE(__func__); |
| 378 | |
| 379 | if (getState() == TransportState::CLOSING || |
| 380 | getState() == TransportState::FAILED || |
| 381 | getState() == TransportState::CLOSED || |
| 382 | error == boost::asio::error::operation_aborted) |
| 383 | // transport is shutting down, ignore any errors |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 384 | return; |
| 385 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 386 | NFD_LOG_FACE_WARN("Receive operation failed: " << error.message()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 389 | size_t |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 390 | EthernetTransport::getInterfaceMtu() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 391 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 392 | #ifdef SIOCGIFMTU |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 393 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 394 | // see bug #2328 |
| 395 | using boost::asio::ip::udp; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 396 | udp::socket sock(getGlobalIoService(), udp::v4()); |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 397 | int fd = sock.native_handle(); |
| 398 | #else |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 399 | int fd = m_socket.native_handle(); |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 400 | #endif |
| 401 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 402 | ifreq ifr{}; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 403 | 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] | 404 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 405 | if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0) { |
| 406 | NFD_LOG_FACE_DEBUG("Interface MTU is " << ifr.ifr_mtu); |
Davide Pesavento | 8eb9957 | 2014-12-19 19:12:15 +0100 | [diff] [blame] | 407 | return static_cast<size_t>(ifr.ifr_mtu); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 408 | } |
Davide Pesavento | 8eb9957 | 2014-12-19 19:12:15 +0100 | [diff] [blame] | 409 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 410 | NFD_LOG_FACE_WARN("Failed to get interface MTU: " << std::strerror(errno)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 411 | #endif |
| 412 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 413 | NFD_LOG_FACE_DEBUG("Assuming default MTU of " << ethernet::MAX_DATA_LEN); |
Davide Pesavento | 8eb9957 | 2014-12-19 19:12:15 +0100 | [diff] [blame] | 414 | return ethernet::MAX_DATA_LEN; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 415 | } |
| 416 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 417 | } // namespace face |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 418 | } // namespace nfd |