blob: f31a8f628950dc424cbfc4936feee73ca1449437 [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
Davide Pesavento10783f22014-03-15 04:40:01 +010032#include <cstring> // for std::strerror() and std::strncpy()
Mathias Gibbens802c0922015-04-14 13:47:22 -070033#include <stdio.h> // for snprintf()
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)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070086 BOOST_THROW_EXCEPTION(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];
Mathias Gibbens802c0922015-04-14 13:47:22 -070099 // std::snprintf not found in some environments
100 // http://redmine.named-data.net/issues/2299 for more information
101 snprintf(filter, sizeof(filter),
102 "(ether proto 0x%x) && (ether dst %s) && (not ether src %s)",
103 ethernet::ETHERTYPE_NDN,
104 m_destAddress.toString().c_str(),
105 m_srcAddress.toString().c_str());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100106 setPacketFilter(filter);
107
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100108 if (!m_destAddress.isBroadcast() && !joinMulticastGroup())
109 {
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100110 NFD_LOG_FACE_WARN("Falling back to promiscuous mode");
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100111 pcap_set_promisc(m_pcap.get(), 1);
112 }
Davide Pesavento10783f22014-03-15 04:40:01 +0100113
Davide Pesavento292e5e12015-03-13 02:08:33 +0100114 m_socket.async_read_some(boost::asio::null_buffers(),
115 bind(&EthernetFace::handleRead, this,
116 boost::asio::placeholders::error,
117 boost::asio::placeholders::bytes_transferred));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100118}
119
120void
121EthernetFace::sendInterest(const Interest& interest)
122{
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100123 NFD_LOG_FACE_TRACE(__func__);
124
Junxiao Shic099ddb2014-12-25 20:53:20 -0700125 this->emitSignal(onSendInterest, interest);
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100126
Matteo Sammarco66df9742014-11-21 18:31:26 +0100127 ndnlp::PacketArray pa = m_slicer->slice(interest.wireEncode());
128 for (const auto& packet : *pa) {
129 sendPacket(packet);
130 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100131}
132
133void
134EthernetFace::sendData(const Data& data)
135{
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100136 NFD_LOG_FACE_TRACE(__func__);
137
Junxiao Shic099ddb2014-12-25 20:53:20 -0700138 this->emitSignal(onSendData, data);
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100139
Matteo Sammarco66df9742014-11-21 18:31:26 +0100140 ndnlp::PacketArray pa = m_slicer->slice(data.wireEncode());
141 for (const auto& packet : *pa) {
142 sendPacket(packet);
143 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100144}
145
146void
147EthernetFace::close()
148{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100149 if (!m_pcap)
150 return;
Alexander Afanasyev251f3c12014-06-02 18:39:58 +0300151
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100152 NFD_LOG_FACE_INFO("Closing face");
Davide Pesavento7726ae52014-11-23 21:01:05 +0100153
154 boost::system::error_code error;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100155 m_socket.cancel(error); // ignore errors
156 m_socket.close(error); // ignore errors
157 m_pcap.reset();
Davide Pesavento7726ae52014-11-23 21:01:05 +0100158
159 fail("Face closed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100160}
161
162void
163EthernetFace::pcapInit()
164{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100165 char errbuf[PCAP_ERRBUF_SIZE] = {};
166 m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100167 if (!m_pcap)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700168 BOOST_THROW_EXCEPTION(Error("pcap_create: " + std::string(errbuf)));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100169
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200170#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
171 // Enable "immediate mode", effectively disabling any read buffering in the kernel.
172 // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including OS X)
173 // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3,
174 // even if the kernel supports it, thus preventing bug #1511.
Davide Pesavento7726ae52014-11-23 21:01:05 +0100175 pcap_set_immediate_mode(m_pcap.get(), 1);
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200176#endif
177
Davide Pesavento7726ae52014-11-23 21:01:05 +0100178 if (pcap_activate(m_pcap.get()) < 0)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700179 BOOST_THROW_EXCEPTION(Error("pcap_activate failed"));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100180
Davide Pesavento7726ae52014-11-23 21:01:05 +0100181 if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700182 BOOST_THROW_EXCEPTION(Error("pcap_set_datalink: " + std::string(pcap_geterr(m_pcap.get()))));
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100183
Davide Pesavento7726ae52014-11-23 21:01:05 +0100184 if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0)
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100185 // no need to throw on failure, BPF will filter unwanted packets anyway
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100186 NFD_LOG_FACE_WARN("pcap_setdirection failed: " << pcap_geterr(m_pcap.get()));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100187}
188
189void
190EthernetFace::setPacketFilter(const char* filterString)
191{
192 bpf_program filter;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100193 if (pcap_compile(m_pcap.get(), &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700194 BOOST_THROW_EXCEPTION(Error("pcap_compile: " + std::string(pcap_geterr(m_pcap.get()))));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100195
Davide Pesavento7726ae52014-11-23 21:01:05 +0100196 int ret = pcap_setfilter(m_pcap.get(), &filter);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100197 pcap_freecode(&filter);
198 if (ret < 0)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700199 BOOST_THROW_EXCEPTION(Error("pcap_setfilter: " + std::string(pcap_geterr(m_pcap.get()))));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100200}
201
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100202bool
Davide Pesavento10783f22014-03-15 04:40:01 +0100203EthernetFace::joinMulticastGroup()
204{
205#if defined(__linux__)
206 packet_mreq mr{};
207 mr.mr_ifindex = m_interfaceIndex;
208 mr.mr_type = PACKET_MR_MULTICAST;
209 mr.mr_alen = m_destAddress.size();
210 std::copy(m_destAddress.begin(), m_destAddress.end(), mr.mr_address);
211
Davide Pesavento292e5e12015-03-13 02:08:33 +0100212 if (::setsockopt(m_socket.native_handle(), SOL_PACKET,
Davide Pesavento10783f22014-03-15 04:40:01 +0100213 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100214 return true; // success
Davide Pesavento10783f22014-03-15 04:40:01 +0100215
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100216 NFD_LOG_FACE_WARN("setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno));
Davide Pesavento10783f22014-03-15 04:40:01 +0100217#endif
218
219#if defined(SIOCADDMULTI)
220 ifreq ifr{};
221 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
222
223#if defined(__APPLE__) || defined(__FreeBSD__)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100224 // see bug #2327
225 using boost::asio::ip::udp;
226 udp::socket sock(ref(getGlobalIoService()), udp::v4());
227 int fd = sock.native_handle();
228
Davide Pesavento10783f22014-03-15 04:40:01 +0100229 /*
230 * Differences between Linux and the BSDs (including OS X):
231 * o BSD does not have ifr_hwaddr; use ifr_addr instead.
232 * o While OS X seems to accept both AF_LINK and AF_UNSPEC as the address
233 * family, FreeBSD explicitly requires AF_LINK, so we have to use AF_LINK
234 * and sockaddr_dl instead of the generic sockaddr structure.
235 * o BSD's sockaddr (and sockaddr_dl in particular) contains an additional
236 * field, sa_len (sdl_len), which must be set to the total length of the
237 * structure, including the length field itself.
238 * o We do not specify the interface name, thus sdl_nlen is left at 0 and
239 * LLADDR is effectively the same as sdl_data.
240 */
241 sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(&ifr.ifr_addr);
242 sdl->sdl_len = sizeof(ifr.ifr_addr);
243 sdl->sdl_family = AF_LINK;
244 sdl->sdl_alen = m_destAddress.size();
245 std::copy(m_destAddress.begin(), m_destAddress.end(), LLADDR(sdl));
246
247 static_assert(sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) + ethernet::ADDR_LEN,
248 "ifr_addr in struct ifreq is too small on this platform");
249#else
Davide Pesavento292e5e12015-03-13 02:08:33 +0100250 int fd = m_socket.native_handle();
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100251
Davide Pesavento10783f22014-03-15 04:40:01 +0100252 ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
253 std::copy(m_destAddress.begin(), m_destAddress.end(), ifr.ifr_hwaddr.sa_data);
254
255 static_assert(sizeof(ifr.ifr_hwaddr) >= offsetof(sockaddr, sa_data) + ethernet::ADDR_LEN,
256 "ifr_hwaddr in struct ifreq is too small on this platform");
257#endif
258
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100259 if (::ioctl(fd, SIOCADDMULTI, &ifr) == 0)
260 return true; // success
Davide Pesavento10783f22014-03-15 04:40:01 +0100261
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100262 NFD_LOG_FACE_WARN("ioctl(SIOCADDMULTI) failed: " << std::strerror(errno));
Davide Pesavento10783f22014-03-15 04:40:01 +0100263#endif
264
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100265 return false;
Davide Pesavento10783f22014-03-15 04:40:01 +0100266}
267
268void
Davide Pesavento44deacc2014-02-19 10:48:07 +0100269EthernetFace::sendPacket(const ndn::Block& block)
270{
271 if (!m_pcap)
272 {
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100273 NFD_LOG_FACE_WARN("Trying to send on closed face");
Davide Pesavento7726ae52014-11-23 21:01:05 +0100274 return fail("Face closed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100275 }
276
Matteo Sammarco66df9742014-11-21 18:31:26 +0100277 BOOST_ASSERT(block.size() <= m_interfaceMtu);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100278
279 /// \todo Right now there is no reserve when packet is received, but
280 /// we should reserve some space at the beginning and at the end
281 ndn::EncodingBuffer buffer(block);
282
Davide Pesaventoe4631002014-03-21 20:55:36 +0100283 // pad with zeroes if the payload is too short
Davide Pesavento44deacc2014-02-19 10:48:07 +0100284 if (block.size() < ethernet::MIN_DATA_LEN)
285 {
Davide Pesavento8eb99572014-12-19 19:12:15 +0100286 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {};
Davide Pesaventob60cc122014-03-19 19:26:02 +0100287 buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100288 }
289
290 // construct and prepend the ethernet header
Junxiao Shi3ffe66d2014-11-06 15:37:59 -0700291 static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100292 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
Davide Pesaventob60cc122014-03-19 19:26:02 +0100293 buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100294 buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
295
296 // send the packet
Davide Pesavento7726ae52014-11-23 21:01:05 +0100297 int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100298 if (sent < 0)
299 {
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100300 return fail("pcap_inject: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100301 }
302 else if (static_cast<size_t>(sent) < buffer.size())
303 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100304 return fail("Failed to inject frame");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100305 }
306
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100307 NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes");
Davide Pesavento4c1a0782014-08-14 16:13:20 +0200308 this->getMutableCounters().getNOutBytes() += block.size();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100309}
310
311void
312EthernetFace::handleRead(const boost::system::error_code& error, size_t)
313{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100314 if (!m_pcap)
315 return fail("Face closed");
316
Davide Pesavento44deacc2014-02-19 10:48:07 +0100317 if (error)
318 return processErrorCode(error);
319
Davide Pesavento7726ae52014-11-23 21:01:05 +0100320 pcap_pkthdr* header;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100321 const uint8_t* packet;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100322 int ret = pcap_next_ex(m_pcap.get(), &header, &packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100323 if (ret < 0)
324 {
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100325 return fail("pcap_next_ex: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100326 }
327 else if (ret == 0)
328 {
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100329 NFD_LOG_FACE_WARN("Read timeout");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100330 }
331 else
332 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100333 processIncomingPacket(header, packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100334 }
335
Davide Pesavento9a090a02015-01-29 18:15:26 +0100336#ifdef _DEBUG
337 pcap_stat ps{};
338 ret = pcap_stats(m_pcap.get(), &ps);
339 if (ret < 0)
340 {
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100341 NFD_LOG_FACE_DEBUG("pcap_stats failed: " << pcap_geterr(m_pcap.get()));
Davide Pesavento9a090a02015-01-29 18:15:26 +0100342 }
343 else if (ret == 0)
344 {
345 if (ps.ps_drop - m_nDropped > 0)
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100346 NFD_LOG_FACE_DEBUG("Detected " << ps.ps_drop - m_nDropped << " dropped packet(s)");
Davide Pesavento9a090a02015-01-29 18:15:26 +0100347 m_nDropped = ps.ps_drop;
348 }
349#endif
350
Davide Pesavento292e5e12015-03-13 02:08:33 +0100351 m_socket.async_read_some(boost::asio::null_buffers(),
352 bind(&EthernetFace::handleRead, this,
353 boost::asio::placeholders::error,
354 boost::asio::placeholders::bytes_transferred));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100355}
356
357void
Davide Pesavento7726ae52014-11-23 21:01:05 +0100358EthernetFace::processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100359{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100360 size_t length = header->caplen;
Junxiao Shi083f7782015-02-21 12:06:26 -0700361 if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN) {
362 NFD_LOG_FACE_WARN("Received frame is too short (" << length << " bytes)");
363 return;
364 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100365
Davide Pesavento7726ae52014-11-23 21:01:05 +0100366 const ether_header* eh = reinterpret_cast<const ether_header*>(packet);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100367 const ethernet::Address sourceAddress(eh->ether_shost);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100368
369 // assert in case BPF fails to filter unwanted frames
370 BOOST_ASSERT_MSG(ethernet::Address(eh->ether_dhost) == m_destAddress,
371 "Received frame addressed to a different multicast group");
Matteo Sammarco66df9742014-11-21 18:31:26 +0100372 BOOST_ASSERT_MSG(sourceAddress != m_srcAddress,
Davide Pesavento7726ae52014-11-23 21:01:05 +0100373 "Received frame sent by this host");
374 BOOST_ASSERT_MSG(ntohs(eh->ether_type) == ethernet::ETHERTYPE_NDN,
375 "Received frame with unrecognized ethertype");
376
377 packet += ethernet::HDR_LEN;
378 length -= ethernet::HDR_LEN;
379
380 /// \todo Reserve space in front and at the back of the underlying buffer
Junxiao Shi78926c92015-02-28 22:56:06 -0700381 bool isOk = false;
Junxiao Shi083f7782015-02-21 12:06:26 -0700382 Block fragmentBlock;
383 std::tie(isOk, fragmentBlock) = Block::fromBuffer(packet, length);
384 if (!isOk) {
385 NFD_LOG_FACE_WARN("Block received from " << sourceAddress.toString()
386 << " is invalid or too large to process");
387 return;
388 }
Matteo Sammarco66df9742014-11-21 18:31:26 +0100389
Junxiao Shi083f7782015-02-21 12:06:26 -0700390 NFD_LOG_FACE_TRACE("Received: " << fragmentBlock.size() << " bytes from "
391 << sourceAddress.toString());
392 this->getMutableCounters().getNInBytes() += fragmentBlock.size();
Matteo Sammarco66df9742014-11-21 18:31:26 +0100393
394 Reassembler& reassembler = m_reassemblers[sourceAddress];
Junxiao Shi083f7782015-02-21 12:06:26 -0700395 if (!reassembler.pms) {
396 // new sender, setup a PartialMessageStore for it
397 reassembler.pms.reset(new ndnlp::PartialMessageStore);
398 reassembler.pms->onReceive.connect(
399 [this, sourceAddress] (const Block& block) {
400 NFD_LOG_FACE_TRACE("All fragments received from " << sourceAddress.toString());
401 if (!decodeAndDispatchInput(block))
402 NFD_LOG_FACE_WARN("Received unrecognized TLV block of type " << block.type()
403 << " from " << sourceAddress.toString());
404 });
405 }
Matteo Sammarco66df9742014-11-21 18:31:26 +0100406
407 scheduler::cancel(reassembler.expireEvent);
408 reassembler.expireEvent = scheduler::schedule(REASSEMBLER_LIFETIME,
409 [this, sourceAddress] {
410 BOOST_VERIFY(m_reassemblers.erase(sourceAddress) == 1);
411 });
412
Junxiao Shi083f7782015-02-21 12:06:26 -0700413 ndnlp::NdnlpData fragment;
414 std::tie(isOk, fragment) = ndnlp::NdnlpData::fromBlock(fragmentBlock);
415 if (!isOk) {
Davide Pesavento292e5e12015-03-13 02:08:33 +0100416 NFD_LOG_FACE_WARN("Received invalid NDNLP fragment from " << sourceAddress.toString());
Junxiao Shi083f7782015-02-21 12:06:26 -0700417 return;
Matteo Sammarco66df9742014-11-21 18:31:26 +0100418 }
Junxiao Shi083f7782015-02-21 12:06:26 -0700419
420 reassembler.pms->receive(fragment);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100421}
422
423void
424EthernetFace::processErrorCode(const boost::system::error_code& error)
425{
426 if (error == boost::asio::error::operation_aborted)
427 // cancel() has been called on the socket
428 return;
429
Davide Pesavento44deacc2014-02-19 10:48:07 +0100430 std::string msg;
431 if (error == boost::asio::error::eof)
432 {
433 msg = "Face closed";
Davide Pesavento44deacc2014-02-19 10:48:07 +0100434 }
435 else
436 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100437 msg = "Receive operation failed: " + error.message();
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100438 NFD_LOG_FACE_WARN(msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100439 }
Junxiao Shi08d07a72014-06-09 23:17:57 -0700440 fail(msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100441}
442
Davide Pesavento44deacc2014-02-19 10:48:07 +0100443size_t
Davide Pesavento292e5e12015-03-13 02:08:33 +0100444EthernetFace::getInterfaceMtu()
Davide Pesavento44deacc2014-02-19 10:48:07 +0100445{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100446#ifdef SIOCGIFMTU
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100447#if defined(__APPLE__) || defined(__FreeBSD__)
448 // see bug #2328
449 using boost::asio::ip::udp;
450 udp::socket sock(ref(getGlobalIoService()), udp::v4());
451 int fd = sock.native_handle();
452#else
Davide Pesavento292e5e12015-03-13 02:08:33 +0100453 int fd = m_socket.native_handle();
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100454#endif
455
Davide Pesavento10783f22014-03-15 04:40:01 +0100456 ifreq ifr{};
Davide Pesaventob60cc122014-03-19 19:26:02 +0100457 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100458
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100459 if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0)
Davide Pesavento8eb99572014-12-19 19:12:15 +0100460 return static_cast<size_t>(ifr.ifr_mtu);
461
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100462 NFD_LOG_FACE_WARN("Failed to get interface MTU: " << std::strerror(errno));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100463#endif
464
Davide Pesavento8eb99572014-12-19 19:12:15 +0100465 return ethernet::MAX_DATA_LEN;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100466}
467
468} // namespace nfd