blob: 05d1f71a7242202a1f2de6a6866a05a3ac73216f [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * 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 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
26#include "ethernet-face.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
32#include <cstdio> // for std::snprintf()
33#include <cstring> // for std::strerror() and std::strncpy()
Davide Pesavento44deacc2014-02-19 10:48:07 +010034#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 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 {
60
Davide Pesavento1bdef282014-04-08 20:59:50 +020061NFD_LOG_INIT("EthernetFace");
Davide Pesavento44deacc2014-02-19 10:48:07 +010062
Matteo Sammarco66df9742014-11-21 18:31:26 +010063const time::nanoseconds EthernetFace::REASSEMBLER_LIFETIME = time::seconds(60);
64
Davide Pesavento292e5e12015-03-13 02:08:33 +010065EthernetFace::EthernetFace(boost::asio::posix::stream_descriptor socket,
Davide Pesaventob499a602014-11-18 22:36:56 +010066 const NetworkInterfaceInfo& interface,
Davide Pesavento44deacc2014-02-19 10:48:07 +010067 const ethernet::Address& address)
Davide Pesavento94279412015-02-27 01:29:32 +010068 : Face(FaceUri(address), FaceUri::fromDev(interface.name), false, true)
Davide Pesavento7726ae52014-11-23 21:01:05 +010069 , m_pcap(nullptr, pcap_close)
Davide Pesavento292e5e12015-03-13 02:08:33 +010070 , m_socket(std::move(socket))
Davide Pesavento10783f22014-03-15 04:40:01 +010071#if defined(__linux__)
72 , m_interfaceIndex(interface.index)
73#endif
Davide Pesaventob499a602014-11-18 22:36:56 +010074 , m_interfaceName(interface.name)
75 , m_srcAddress(interface.etherAddress)
Davide Pesavento44deacc2014-02-19 10:48:07 +010076 , m_destAddress(address)
Davide Pesavento9a090a02015-01-29 18:15:26 +010077#ifdef _DEBUG
78 , m_nDropped(0)
79#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +010080{
Davide Pesaventobe40fb12015-02-23 21:09:34 +010081 NFD_LOG_FACE_INFO("Creating face on " << m_interfaceName << "/" << m_srcAddress);
Davide Pesavento44deacc2014-02-19 10:48:07 +010082 pcapInit();
83
Davide Pesavento7726ae52014-11-23 21:01:05 +010084 int fd = pcap_get_selectable_fd(m_pcap.get());
Davide Pesavento44deacc2014-02-19 10:48:07 +010085 if (fd < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +010086 throw Error("pcap_get_selectable_fd failed");
Davide Pesavento44deacc2014-02-19 10:48:07 +010087
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 Pesavento292e5e12015-03-13 02:08:33 +010091 m_socket.assign(::dup(fd));
Davide Pesavento44deacc2014-02-19 10:48:07 +010092
Davide Pesavento44deacc2014-02-19 10:48:07 +010093 m_interfaceMtu = getInterfaceMtu();
Davide Pesaventobe40fb12015-02-23 21:09:34 +010094 NFD_LOG_FACE_DEBUG("Interface MTU is: " << m_interfaceMtu);
Davide Pesavento44deacc2014-02-19 10:48:07 +010095
Matteo Sammarco66df9742014-11-21 18:31:26 +010096 m_slicer.reset(new ndnlp::Slicer(m_interfaceMtu));
97
Davide Pesavento44deacc2014-02-19 10:48:07 +010098 char filter[100];
Davide Pesavento10783f22014-03-15 04:40:01 +010099 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 Pesavento44deacc2014-02-19 10:48:07 +0100104 setPacketFilter(filter);
105
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100106 if (!m_destAddress.isBroadcast() && !joinMulticastGroup())
107 {
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100108 NFD_LOG_FACE_WARN("Falling back to promiscuous mode");
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100109 pcap_set_promisc(m_pcap.get(), 1);
110 }
Davide Pesavento10783f22014-03-15 04:40:01 +0100111
Davide Pesavento292e5e12015-03-13 02:08:33 +0100112 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 Pesavento44deacc2014-02-19 10:48:07 +0100116}
117
118void
119EthernetFace::sendInterest(const Interest& interest)
120{
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100121 NFD_LOG_FACE_TRACE(__func__);
122
Junxiao Shic099ddb2014-12-25 20:53:20 -0700123 this->emitSignal(onSendInterest, interest);
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100124
Matteo Sammarco66df9742014-11-21 18:31:26 +0100125 ndnlp::PacketArray pa = m_slicer->slice(interest.wireEncode());
126 for (const auto& packet : *pa) {
127 sendPacket(packet);
128 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100129}
130
131void
132EthernetFace::sendData(const Data& data)
133{
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100134 NFD_LOG_FACE_TRACE(__func__);
135
Junxiao Shic099ddb2014-12-25 20:53:20 -0700136 this->emitSignal(onSendData, data);
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100137
Matteo Sammarco66df9742014-11-21 18:31:26 +0100138 ndnlp::PacketArray pa = m_slicer->slice(data.wireEncode());
139 for (const auto& packet : *pa) {
140 sendPacket(packet);
141 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100142}
143
144void
145EthernetFace::close()
146{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100147 if (!m_pcap)
148 return;
Alexander Afanasyev251f3c12014-06-02 18:39:58 +0300149
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100150 NFD_LOG_FACE_INFO("Closing face");
Davide Pesavento7726ae52014-11-23 21:01:05 +0100151
152 boost::system::error_code error;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100153 m_socket.cancel(error); // ignore errors
154 m_socket.close(error); // ignore errors
155 m_pcap.reset();
Davide Pesavento7726ae52014-11-23 21:01:05 +0100156
157 fail("Face closed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100158}
159
160void
161EthernetFace::pcapInit()
162{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100163 char errbuf[PCAP_ERRBUF_SIZE] = {};
164 m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100165 if (!m_pcap)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100166 throw Error("pcap_create: " + std::string(errbuf));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100167
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200168#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 Pesavento7726ae52014-11-23 21:01:05 +0100173 pcap_set_immediate_mode(m_pcap.get(), 1);
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200174#endif
175
Davide Pesavento7726ae52014-11-23 21:01:05 +0100176 if (pcap_activate(m_pcap.get()) < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100177 throw Error("pcap_activate failed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100178
Davide Pesavento7726ae52014-11-23 21:01:05 +0100179 if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100180 throw Error("pcap_set_datalink: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100181
Davide Pesavento7726ae52014-11-23 21:01:05 +0100182 if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0)
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100183 // no need to throw on failure, BPF will filter unwanted packets anyway
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100184 NFD_LOG_FACE_WARN("pcap_setdirection failed: " << pcap_geterr(m_pcap.get()));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100185}
186
187void
188EthernetFace::setPacketFilter(const char* filterString)
189{
190 bpf_program filter;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100191 if (pcap_compile(m_pcap.get(), &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100192 throw Error("pcap_compile: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100193
Davide Pesavento7726ae52014-11-23 21:01:05 +0100194 int ret = pcap_setfilter(m_pcap.get(), &filter);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100195 pcap_freecode(&filter);
196 if (ret < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100197 throw Error("pcap_setfilter: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100198}
199
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100200bool
Davide Pesavento10783f22014-03-15 04:40:01 +0100201EthernetFace::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 Pesavento292e5e12015-03-13 02:08:33 +0100210 if (::setsockopt(m_socket.native_handle(), SOL_PACKET,
Davide Pesavento10783f22014-03-15 04:40:01 +0100211 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100212 return true; // success
Davide Pesavento10783f22014-03-15 04:40:01 +0100213
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100214 NFD_LOG_FACE_WARN("setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno));
Davide Pesavento10783f22014-03-15 04:40:01 +0100215#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 Pesaventof8b41eb2014-12-26 19:14:06 +0100222 // 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 Pesavento10783f22014-03-15 04:40:01 +0100227 /*
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 Pesavento292e5e12015-03-13 02:08:33 +0100248 int fd = m_socket.native_handle();
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100249
Davide Pesavento10783f22014-03-15 04:40:01 +0100250 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 Pesaventof8b41eb2014-12-26 19:14:06 +0100257 if (::ioctl(fd, SIOCADDMULTI, &ifr) == 0)
258 return true; // success
Davide Pesavento10783f22014-03-15 04:40:01 +0100259
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100260 NFD_LOG_FACE_WARN("ioctl(SIOCADDMULTI) failed: " << std::strerror(errno));
Davide Pesavento10783f22014-03-15 04:40:01 +0100261#endif
262
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100263 return false;
Davide Pesavento10783f22014-03-15 04:40:01 +0100264}
265
266void
Davide Pesavento44deacc2014-02-19 10:48:07 +0100267EthernetFace::sendPacket(const ndn::Block& block)
268{
269 if (!m_pcap)
270 {
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100271 NFD_LOG_FACE_WARN("Trying to send on closed face");
Davide Pesavento7726ae52014-11-23 21:01:05 +0100272 return fail("Face closed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100273 }
274
Matteo Sammarco66df9742014-11-21 18:31:26 +0100275 BOOST_ASSERT(block.size() <= m_interfaceMtu);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100276
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 Pesaventoe4631002014-03-21 20:55:36 +0100281 // pad with zeroes if the payload is too short
Davide Pesavento44deacc2014-02-19 10:48:07 +0100282 if (block.size() < ethernet::MIN_DATA_LEN)
283 {
Davide Pesavento8eb99572014-12-19 19:12:15 +0100284 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {};
Davide Pesaventob60cc122014-03-19 19:26:02 +0100285 buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100286 }
287
288 // construct and prepend the ethernet header
Junxiao Shi3ffe66d2014-11-06 15:37:59 -0700289 static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100290 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
Davide Pesaventob60cc122014-03-19 19:26:02 +0100291 buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100292 buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
293
294 // send the packet
Davide Pesavento7726ae52014-11-23 21:01:05 +0100295 int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100296 if (sent < 0)
297 {
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100298 return fail("pcap_inject: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100299 }
300 else if (static_cast<size_t>(sent) < buffer.size())
301 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100302 return fail("Failed to inject frame");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100303 }
304
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100305 NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes");
Davide Pesavento4c1a0782014-08-14 16:13:20 +0200306 this->getMutableCounters().getNOutBytes() += block.size();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100307}
308
309void
310EthernetFace::handleRead(const boost::system::error_code& error, size_t)
311{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100312 if (!m_pcap)
313 return fail("Face closed");
314
Davide Pesavento44deacc2014-02-19 10:48:07 +0100315 if (error)
316 return processErrorCode(error);
317
Davide Pesavento7726ae52014-11-23 21:01:05 +0100318 pcap_pkthdr* header;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100319 const uint8_t* packet;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100320 int ret = pcap_next_ex(m_pcap.get(), &header, &packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100321 if (ret < 0)
322 {
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100323 return fail("pcap_next_ex: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100324 }
325 else if (ret == 0)
326 {
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100327 NFD_LOG_FACE_WARN("Read timeout");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100328 }
329 else
330 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100331 processIncomingPacket(header, packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100332 }
333
Davide Pesavento9a090a02015-01-29 18:15:26 +0100334#ifdef _DEBUG
335 pcap_stat ps{};
336 ret = pcap_stats(m_pcap.get(), &ps);
337 if (ret < 0)
338 {
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100339 NFD_LOG_FACE_DEBUG("pcap_stats failed: " << pcap_geterr(m_pcap.get()));
Davide Pesavento9a090a02015-01-29 18:15:26 +0100340 }
341 else if (ret == 0)
342 {
343 if (ps.ps_drop - m_nDropped > 0)
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100344 NFD_LOG_FACE_DEBUG("Detected " << ps.ps_drop - m_nDropped << " dropped packet(s)");
Davide Pesavento9a090a02015-01-29 18:15:26 +0100345 m_nDropped = ps.ps_drop;
346 }
347#endif
348
Davide Pesavento292e5e12015-03-13 02:08:33 +0100349 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 Pesavento44deacc2014-02-19 10:48:07 +0100353}
354
355void
Davide Pesavento7726ae52014-11-23 21:01:05 +0100356EthernetFace::processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100357{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100358 size_t length = header->caplen;
Junxiao Shi083f7782015-02-21 12:06:26 -0700359 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 Pesavento44deacc2014-02-19 10:48:07 +0100363
Davide Pesavento7726ae52014-11-23 21:01:05 +0100364 const ether_header* eh = reinterpret_cast<const ether_header*>(packet);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100365 const ethernet::Address sourceAddress(eh->ether_shost);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100366
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 Sammarco66df9742014-11-21 18:31:26 +0100370 BOOST_ASSERT_MSG(sourceAddress != m_srcAddress,
Davide Pesavento7726ae52014-11-23 21:01:05 +0100371 "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 Shi78926c92015-02-28 22:56:06 -0700379 bool isOk = false;
Junxiao Shi083f7782015-02-21 12:06:26 -0700380 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 Sammarco66df9742014-11-21 18:31:26 +0100387
Junxiao Shi083f7782015-02-21 12:06:26 -0700388 NFD_LOG_FACE_TRACE("Received: " << fragmentBlock.size() << " bytes from "
389 << sourceAddress.toString());
390 this->getMutableCounters().getNInBytes() += fragmentBlock.size();
Matteo Sammarco66df9742014-11-21 18:31:26 +0100391
392 Reassembler& reassembler = m_reassemblers[sourceAddress];
Junxiao Shi083f7782015-02-21 12:06:26 -0700393 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 Sammarco66df9742014-11-21 18:31:26 +0100404
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 Shi083f7782015-02-21 12:06:26 -0700411 ndnlp::NdnlpData fragment;
412 std::tie(isOk, fragment) = ndnlp::NdnlpData::fromBlock(fragmentBlock);
413 if (!isOk) {
Davide Pesavento292e5e12015-03-13 02:08:33 +0100414 NFD_LOG_FACE_WARN("Received invalid NDNLP fragment from " << sourceAddress.toString());
Junxiao Shi083f7782015-02-21 12:06:26 -0700415 return;
Matteo Sammarco66df9742014-11-21 18:31:26 +0100416 }
Junxiao Shi083f7782015-02-21 12:06:26 -0700417
418 reassembler.pms->receive(fragment);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100419}
420
421void
422EthernetFace::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 Pesavento44deacc2014-02-19 10:48:07 +0100428 std::string msg;
429 if (error == boost::asio::error::eof)
430 {
431 msg = "Face closed";
Davide Pesavento44deacc2014-02-19 10:48:07 +0100432 }
433 else
434 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100435 msg = "Receive operation failed: " + error.message();
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100436 NFD_LOG_FACE_WARN(msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100437 }
Junxiao Shi08d07a72014-06-09 23:17:57 -0700438 fail(msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100439}
440
Davide Pesavento44deacc2014-02-19 10:48:07 +0100441size_t
Davide Pesavento292e5e12015-03-13 02:08:33 +0100442EthernetFace::getInterfaceMtu()
Davide Pesavento44deacc2014-02-19 10:48:07 +0100443{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100444#ifdef SIOCGIFMTU
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100445#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 Pesavento292e5e12015-03-13 02:08:33 +0100451 int fd = m_socket.native_handle();
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100452#endif
453
Davide Pesavento10783f22014-03-15 04:40:01 +0100454 ifreq ifr{};
Davide Pesaventob60cc122014-03-19 19:26:02 +0100455 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100456
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100457 if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0)
Davide Pesavento8eb99572014-12-19 19:12:15 +0100458 return static_cast<size_t>(ifr.ifr_mtu);
459
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100460 NFD_LOG_FACE_WARN("Failed to get interface MTU: " << std::strerror(errno));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100461#endif
462
Davide Pesavento8eb99572014-12-19 19:12:15 +0100463 return ethernet::MAX_DATA_LEN;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100464}
465
466} // namespace nfd