blob: f47f1b1a944c7008a5bb8f926369cfb05c1549bb [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7003c602017-01-10 13:35:28 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shi3ffe66d2014-11-06 15:37:59 -070024 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010025
Davide Pesavento35120ea2015-11-17 21:13:18 +010026#include "ethernet-transport.hpp"
Davide Pesaventof8b41eb2014-12-26 19:14:06 +010027#include "core/global-io.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010028
29#include <pcap/pcap.h>
30
Davide Pesavento10783f22014-03-15 04:40:01 +010031#include <cerrno> // for errno
Davide Pesavento35120ea2015-11-17 21:13:18 +010032#include <cstring> // for memcpy(), strerror(), strncpy()
Davide Pesavento44deacc2014-02-19 10:48:07 +010033#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 Pesavento35120ea2015-11-17 21:13:18 +010036#include <stdio.h> // for snprintf()
Davide Pesavento44deacc2014-02-19 10:48:07 +010037#include <sys/ioctl.h> // for ioctl()
38#include <unistd.h> // for dup()
39
Davide Pesavento10783f22014-03-15 04:40:01 +010040#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 Afanasyevd9479aa2014-06-26 13:52:02 -070051#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 Pesavento44deacc2014-02-19 10:48:07 +010059namespace nfd {
Davide Pesavento35120ea2015-11-17 21:13:18 +010060namespace face {
Davide Pesavento44deacc2014-02-19 10:48:07 +010061
Davide Pesavento35120ea2015-11-17 21:13:18 +010062NFD_LOG_INIT("EthernetTransport");
Davide Pesavento44deacc2014-02-19 10:48:07 +010063
Davide Pesavento35120ea2015-11-17 21:13:18 +010064EthernetTransport::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 Pesavento10783f22014-03-15 04:40:01 +010071#if defined(__linux__)
72 , m_interfaceIndex(interface.index)
73#endif
Davide Pesavento9a090a02015-01-29 18:15:26 +010074#ifdef _DEBUG
75 , m_nDropped(0)
76#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +010077{
Davide Pesavento35120ea2015-11-17 21:13:18 +010078 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 Pesavento44deacc2014-02-19 10:48:07 +010086 pcapInit();
87
Davide Pesavento7726ae52014-11-23 21:01:05 +010088 int fd = pcap_get_selectable_fd(m_pcap.get());
Davide Pesavento44deacc2014-02-19 10:48:07 +010089 if (fd < 0)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070090 BOOST_THROW_EXCEPTION(Error("pcap_get_selectable_fd failed"));
Davide Pesavento44deacc2014-02-19 10:48:07 +010091
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 Pesavento292e5e12015-03-13 02:08:33 +010095 m_socket.assign(::dup(fd));
Davide Pesavento44deacc2014-02-19 10:48:07 +010096
Davide Pesavento35120ea2015-11-17 21:13:18 +010097 // do this after assigning m_socket because getInterfaceMtu uses it
98 this->setMtu(getInterfaceMtu());
Matteo Sammarco66df9742014-11-21 18:31:26 +010099
Davide Pesaventoca25d722015-12-21 21:04:07 +0100100 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 Gibbens802c0922015-04-14 13:47:22 -0700105 snprintf(filter, sizeof(filter),
Davide Pesaventoca25d722015-12-21 21:04:07 +0100106 "(ether proto 0x%x) && (ether dst %s) && (not ether src %s) && (not vlan)",
Mathias Gibbens802c0922015-04-14 13:47:22 -0700107 ethernet::ETHERTYPE_NDN,
108 m_destAddress.toString().c_str(),
109 m_srcAddress.toString().c_str());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100110 setPacketFilter(filter);
111
Davide Pesavento35120ea2015-11-17 21:13:18 +0100112 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 Pesavento10783f22014-03-15 04:40:01 +0100116
Davide Pesavento292e5e12015-03-13 02:08:33 +0100117 m_socket.async_read_some(boost::asio::null_buffers(),
Davide Pesavento35120ea2015-11-17 21:13:18 +0100118 bind(&EthernetTransport::handleRead, this,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100119 boost::asio::placeholders::error,
120 boost::asio::placeholders::bytes_transferred));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100121}
122
Davide Pesavento35120ea2015-11-17 21:13:18 +0100123void EthernetTransport::doSend(Transport::Packet&& packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100124{
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100125 NFD_LOG_FACE_TRACE(__func__);
126
Davide Pesavento35120ea2015-11-17 21:13:18 +0100127 sendPacket(packet.packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100128}
129
Davide Pesavento35120ea2015-11-17 21:13:18 +0100130void EthernetTransport::doClose()
Davide Pesavento44deacc2014-02-19 10:48:07 +0100131{
Davide Pesavento35120ea2015-11-17 21:13:18 +0100132 NFD_LOG_FACE_TRACE(__func__);
Alexander Afanasyev251f3c12014-06-02 18:39:58 +0300133
Davide Pesavento35120ea2015-11-17 21:13:18 +0100134 if (m_socket.is_open()) {
135 // Cancel all outstanding operations and close the socket.
136 // Use the non-throwing variants and ignore errors, if any.
137 boost::system::error_code error;
138 m_socket.cancel(error);
139 m_socket.close(error);
140 }
Davide Pesavento292e5e12015-03-13 02:08:33 +0100141 m_pcap.reset();
Davide Pesavento7726ae52014-11-23 21:01:05 +0100142
Davide Pesavento35120ea2015-11-17 21:13:18 +0100143 // Ensure that the Transport stays alive at least
144 // until all pending handlers are dispatched
145 getGlobalIoService().post([this] {
146 this->setState(TransportState::CLOSED);
147 });
Davide Pesavento44deacc2014-02-19 10:48:07 +0100148}
149
150void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100151EthernetTransport::pcapInit()
Davide Pesavento44deacc2014-02-19 10:48:07 +0100152{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100153 char errbuf[PCAP_ERRBUF_SIZE] = {};
154 m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100155 if (!m_pcap)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700156 BOOST_THROW_EXCEPTION(Error("pcap_create: " + std::string(errbuf)));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100157
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200158#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
159 // Enable "immediate mode", effectively disabling any read buffering in the kernel.
160 // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including OS X)
161 // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3,
162 // even if the kernel supports it, thus preventing bug #1511.
Davide Pesavento7726ae52014-11-23 21:01:05 +0100163 pcap_set_immediate_mode(m_pcap.get(), 1);
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200164#endif
165
Davide Pesavento7726ae52014-11-23 21:01:05 +0100166 if (pcap_activate(m_pcap.get()) < 0)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700167 BOOST_THROW_EXCEPTION(Error("pcap_activate failed"));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100168
Davide Pesavento7726ae52014-11-23 21:01:05 +0100169 if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700170 BOOST_THROW_EXCEPTION(Error("pcap_set_datalink: " + std::string(pcap_geterr(m_pcap.get()))));
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100171
Davide Pesavento7726ae52014-11-23 21:01:05 +0100172 if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0)
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100173 // no need to throw on failure, BPF will filter unwanted packets anyway
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100174 NFD_LOG_FACE_WARN("pcap_setdirection failed: " << pcap_geterr(m_pcap.get()));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100175}
176
177void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100178EthernetTransport::setPacketFilter(const char* filterString)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100179{
180 bpf_program filter;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100181 if (pcap_compile(m_pcap.get(), &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700182 BOOST_THROW_EXCEPTION(Error("pcap_compile: " + std::string(pcap_geterr(m_pcap.get()))));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100183
Davide Pesavento7726ae52014-11-23 21:01:05 +0100184 int ret = pcap_setfilter(m_pcap.get(), &filter);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100185 pcap_freecode(&filter);
186 if (ret < 0)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700187 BOOST_THROW_EXCEPTION(Error("pcap_setfilter: " + std::string(pcap_geterr(m_pcap.get()))));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100188}
189
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100190bool
Davide Pesavento35120ea2015-11-17 21:13:18 +0100191EthernetTransport::joinMulticastGroup()
Davide Pesavento10783f22014-03-15 04:40:01 +0100192{
193#if defined(__linux__)
194 packet_mreq mr{};
195 mr.mr_ifindex = m_interfaceIndex;
196 mr.mr_type = PACKET_MR_MULTICAST;
197 mr.mr_alen = m_destAddress.size();
Davide Pesavento35120ea2015-11-17 21:13:18 +0100198 std::memcpy(mr.mr_address, m_destAddress.data(), m_destAddress.size());
Davide Pesavento10783f22014-03-15 04:40:01 +0100199
Davide Pesavento292e5e12015-03-13 02:08:33 +0100200 if (::setsockopt(m_socket.native_handle(), SOL_PACKET,
Davide Pesavento10783f22014-03-15 04:40:01 +0100201 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100202 return true; // success
Davide Pesavento10783f22014-03-15 04:40:01 +0100203
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100204 NFD_LOG_FACE_WARN("setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno));
Davide Pesavento10783f22014-03-15 04:40:01 +0100205#endif
206
207#if defined(SIOCADDMULTI)
208 ifreq ifr{};
209 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
210
211#if defined(__APPLE__) || defined(__FreeBSD__)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100212 // see bug #2327
213 using boost::asio::ip::udp;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100214 udp::socket sock(getGlobalIoService(), udp::v4());
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100215 int fd = sock.native_handle();
216
Davide Pesavento10783f22014-03-15 04:40:01 +0100217 /*
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();
Davide Pesavento35120ea2015-11-17 21:13:18 +0100233 std::memcpy(LLADDR(sdl), m_destAddress.data(), m_destAddress.size());
Davide Pesavento10783f22014-03-15 04:40:01 +0100234
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
Davide Pesavento292e5e12015-03-13 02:08:33 +0100238 int fd = m_socket.native_handle();
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100239
Davide Pesavento10783f22014-03-15 04:40:01 +0100240 ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100241 std::memcpy(ifr.ifr_hwaddr.sa_data, m_destAddress.data(), m_destAddress.size());
Davide Pesavento10783f22014-03-15 04:40:01 +0100242
Davide Pesavento35120ea2015-11-17 21:13:18 +0100243 static_assert(sizeof(ifr.ifr_hwaddr.sa_data) >= ethernet::ADDR_LEN,
Davide Pesavento10783f22014-03-15 04:40:01 +0100244 "ifr_hwaddr in struct ifreq is too small on this platform");
245#endif
246
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100247 if (::ioctl(fd, SIOCADDMULTI, &ifr) == 0)
248 return true; // success
Davide Pesavento10783f22014-03-15 04:40:01 +0100249
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100250 NFD_LOG_FACE_WARN("ioctl(SIOCADDMULTI) failed: " << std::strerror(errno));
Davide Pesavento10783f22014-03-15 04:40:01 +0100251#endif
252
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100253 return false;
Davide Pesavento10783f22014-03-15 04:40:01 +0100254}
255
256void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100257EthernetTransport::sendPacket(const ndn::Block& block)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100258{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100259 /// \todo Right now there is no reserve when packet is received, but
260 /// we should reserve some space at the beginning and at the end
261 ndn::EncodingBuffer buffer(block);
262
Davide Pesaventoe4631002014-03-21 20:55:36 +0100263 // pad with zeroes if the payload is too short
Davide Pesavento35120ea2015-11-17 21:13:18 +0100264 if (block.size() < ethernet::MIN_DATA_LEN) {
265 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {};
266 buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size());
267 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100268
269 // construct and prepend the ethernet header
Junxiao Shi3ffe66d2014-11-06 15:37:59 -0700270 static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100271 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
Davide Pesaventob60cc122014-03-19 19:26:02 +0100272 buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100273 buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
274
275 // send the packet
Davide Pesavento7726ae52014-11-23 21:01:05 +0100276 int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100277 if (sent < 0)
Davide Pesavento35120ea2015-11-17 21:13:18 +0100278 NFD_LOG_FACE_ERROR("pcap_inject failed: " << pcap_geterr(m_pcap.get()));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100279 else if (static_cast<size_t>(sent) < buffer.size())
Davide Pesavento35120ea2015-11-17 21:13:18 +0100280 NFD_LOG_FACE_ERROR("Failed to send the full frame: bufsize=" << buffer.size() << " sent=" << sent);
281 else
282 // print block size because we don't want to count the padding in buffer
283 NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100284}
285
286void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100287EthernetTransport::handleRead(const boost::system::error_code& error, size_t)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100288{
289 if (error)
290 return processErrorCode(error);
291
Davide Pesavento7726ae52014-11-23 21:01:05 +0100292 pcap_pkthdr* header;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100293 const uint8_t* packet;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100294
295 // read the pcap header and packet data
Davide Pesavento7726ae52014-11-23 21:01:05 +0100296 int ret = pcap_next_ex(m_pcap.get(), &header, &packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100297 if (ret < 0)
Davide Pesavento35120ea2015-11-17 21:13:18 +0100298 NFD_LOG_FACE_ERROR("pcap_next_ex failed: " << pcap_geterr(m_pcap.get()));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100299 else if (ret == 0)
Davide Pesavento35120ea2015-11-17 21:13:18 +0100300 NFD_LOG_FACE_WARN("Read timeout");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100301 else
Davide Pesavento35120ea2015-11-17 21:13:18 +0100302 processIncomingPacket(header, packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100303
Davide Pesavento9a090a02015-01-29 18:15:26 +0100304#ifdef _DEBUG
305 pcap_stat ps{};
306 ret = pcap_stats(m_pcap.get(), &ps);
Davide Pesavento35120ea2015-11-17 21:13:18 +0100307 if (ret < 0) {
308 NFD_LOG_FACE_DEBUG("pcap_stats failed: " << pcap_geterr(m_pcap.get()));
309 }
310 else if (ret == 0) {
311 if (ps.ps_drop - m_nDropped > 0)
312 NFD_LOG_FACE_DEBUG("Detected " << ps.ps_drop - m_nDropped << " dropped packet(s)");
313 m_nDropped = ps.ps_drop;
314 }
Davide Pesavento9a090a02015-01-29 18:15:26 +0100315#endif
316
Davide Pesavento292e5e12015-03-13 02:08:33 +0100317 m_socket.async_read_some(boost::asio::null_buffers(),
Davide Pesavento35120ea2015-11-17 21:13:18 +0100318 bind(&EthernetTransport::handleRead, this,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100319 boost::asio::placeholders::error,
320 boost::asio::placeholders::bytes_transferred));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100321}
322
323void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100324EthernetTransport::processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100325{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100326 size_t length = header->caplen;
Junxiao Shi083f7782015-02-21 12:06:26 -0700327 if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN) {
328 NFD_LOG_FACE_WARN("Received frame is too short (" << length << " bytes)");
329 return;
330 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100331
Davide Pesavento7726ae52014-11-23 21:01:05 +0100332 const ether_header* eh = reinterpret_cast<const ether_header*>(packet);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100333 const ethernet::Address sourceAddress(eh->ether_shost);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100334
Davide Pesaventoca25d722015-12-21 21:04:07 +0100335 // in some cases VLAN-tagged frames may survive the BPF filter,
336 // make sure we do not process those frames (see #3348)
337 if (ntohs(eh->ether_type) != ethernet::ETHERTYPE_NDN)
338 return;
339
340 // check that our BPF filter is working correctly
Davide Pesavento7726ae52014-11-23 21:01:05 +0100341 BOOST_ASSERT_MSG(ethernet::Address(eh->ether_dhost) == m_destAddress,
342 "Received frame addressed to a different multicast group");
Matteo Sammarco66df9742014-11-21 18:31:26 +0100343 BOOST_ASSERT_MSG(sourceAddress != m_srcAddress,
Davide Pesavento7726ae52014-11-23 21:01:05 +0100344 "Received frame sent by this host");
Davide Pesavento7726ae52014-11-23 21:01:05 +0100345
346 packet += ethernet::HDR_LEN;
347 length -= ethernet::HDR_LEN;
348
Junxiao Shi78926c92015-02-28 22:56:06 -0700349 bool isOk = false;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100350 Block element;
351 std::tie(isOk, element) = Block::fromBuffer(packet, length);
Junxiao Shi083f7782015-02-21 12:06:26 -0700352 if (!isOk) {
Davide Pesavento35120ea2015-11-17 21:13:18 +0100353 NFD_LOG_FACE_WARN("Received invalid packet from " << sourceAddress.toString());
Junxiao Shi083f7782015-02-21 12:06:26 -0700354 return;
355 }
Matteo Sammarco66df9742014-11-21 18:31:26 +0100356
Davide Pesavento35120ea2015-11-17 21:13:18 +0100357 NFD_LOG_FACE_TRACE("Received: " << element.size() << " bytes from " << sourceAddress.toString());
Matteo Sammarco66df9742014-11-21 18:31:26 +0100358
Davide Pesavento35120ea2015-11-17 21:13:18 +0100359 Transport::Packet tp(std::move(element));
360 static_assert(sizeof(tp.remoteEndpoint) >= ethernet::ADDR_LEN,
361 "Transport::Packet::remoteEndpoint is too small");
362 std::memcpy(&tp.remoteEndpoint, sourceAddress.data(), sourceAddress.size());
363 this->receive(std::move(tp));
Davide Pesavento7726ae52014-11-23 21:01:05 +0100364}
365
366void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100367EthernetTransport::processErrorCode(const boost::system::error_code& error)
Davide Pesavento7726ae52014-11-23 21:01:05 +0100368{
Junxiao Shi7003c602017-01-10 13:35:28 +0000369 // boost::asio::error::operation_aborted must be checked first. In that situation, the Transport
370 // may already have been destructed, and it's unsafe to call getState() or do logging.
371 if (error == boost::asio::error::operation_aborted ||
372 getState() == TransportState::CLOSING ||
Davide Pesavento35120ea2015-11-17 21:13:18 +0100373 getState() == TransportState::FAILED ||
Junxiao Shi7003c602017-01-10 13:35:28 +0000374 getState() == TransportState::CLOSED) {
Davide Pesavento35120ea2015-11-17 21:13:18 +0100375 // transport is shutting down, ignore any errors
Davide Pesavento7726ae52014-11-23 21:01:05 +0100376 return;
Junxiao Shi7003c602017-01-10 13:35:28 +0000377 }
Davide Pesavento7726ae52014-11-23 21:01:05 +0100378
Davide Pesavento35120ea2015-11-17 21:13:18 +0100379 NFD_LOG_FACE_WARN("Receive operation failed: " << error.message());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100380}
381
Davide Pesavento44deacc2014-02-19 10:48:07 +0100382size_t
Davide Pesavento35120ea2015-11-17 21:13:18 +0100383EthernetTransport::getInterfaceMtu()
Davide Pesavento44deacc2014-02-19 10:48:07 +0100384{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100385#ifdef SIOCGIFMTU
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100386#if defined(__APPLE__) || defined(__FreeBSD__)
387 // see bug #2328
388 using boost::asio::ip::udp;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100389 udp::socket sock(getGlobalIoService(), udp::v4());
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100390 int fd = sock.native_handle();
391#else
Davide Pesavento292e5e12015-03-13 02:08:33 +0100392 int fd = m_socket.native_handle();
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100393#endif
394
Davide Pesavento10783f22014-03-15 04:40:01 +0100395 ifreq ifr{};
Davide Pesaventob60cc122014-03-19 19:26:02 +0100396 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100397
Davide Pesavento35120ea2015-11-17 21:13:18 +0100398 if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0) {
399 NFD_LOG_FACE_DEBUG("Interface MTU is " << ifr.ifr_mtu);
Davide Pesavento8eb99572014-12-19 19:12:15 +0100400 return static_cast<size_t>(ifr.ifr_mtu);
Davide Pesavento35120ea2015-11-17 21:13:18 +0100401 }
Davide Pesavento8eb99572014-12-19 19:12:15 +0100402
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100403 NFD_LOG_FACE_WARN("Failed to get interface MTU: " << std::strerror(errno));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100404#endif
405
Davide Pesavento35120ea2015-11-17 21:13:18 +0100406 NFD_LOG_FACE_DEBUG("Assuming default MTU of " << ethernet::MAX_DATA_LEN);
Davide Pesavento8eb99572014-12-19 19:12:15 +0100407 return ethernet::MAX_DATA_LEN;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100408}
409
Davide Pesavento35120ea2015-11-17 21:13:18 +0100410} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100411} // namespace nfd