blob: 4994926d116cbd4e021f2778430ded21db0920b2 [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"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010029
30#include <pcap/pcap.h>
31
Davide Pesavento10783f22014-03-15 04:40:01 +010032#include <cerrno> // for errno
33#include <cstdio> // for std::snprintf()
34#include <cstring> // for std::strerror() and std::strncpy()
Davide Pesavento44deacc2014-02-19 10:48:07 +010035#include <arpa/inet.h> // for htons() and ntohs()
36#include <net/ethernet.h> // for struct ether_header
37#include <net/if.h> // for struct ifreq
Davide Pesavento44deacc2014-02-19 10:48:07 +010038#include <sys/ioctl.h> // for ioctl()
39#include <unistd.h> // for dup()
40
Davide Pesavento10783f22014-03-15 04:40:01 +010041#if defined(__linux__)
42#include <netpacket/packet.h> // for struct packet_mreq
43#include <sys/socket.h> // for setsockopt()
44#endif
45
46#ifdef SIOCADDMULTI
47#if defined(__APPLE__) || defined(__FreeBSD__)
48#include <net/if_dl.h> // for struct sockaddr_dl
49#endif
50#endif
51
Alexander Afanasyevd9479aa2014-06-26 13:52:02 -070052#if !defined(PCAP_NETMASK_UNKNOWN)
53/*
54 * Value to pass to pcap_compile() as the netmask if you don't know what
55 * the netmask is.
56 */
57#define PCAP_NETMASK_UNKNOWN 0xffffffff
58#endif
59
Davide Pesavento44deacc2014-02-19 10:48:07 +010060namespace nfd {
61
Davide Pesavento1bdef282014-04-08 20:59:50 +020062NFD_LOG_INIT("EthernetFace");
Davide Pesavento44deacc2014-02-19 10:48:07 +010063
Matteo Sammarco66df9742014-11-21 18:31:26 +010064const time::nanoseconds EthernetFace::REASSEMBLER_LIFETIME = time::seconds(60);
65
Davide Pesavento44deacc2014-02-19 10:48:07 +010066EthernetFace::EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket,
Davide Pesaventob499a602014-11-18 22:36:56 +010067 const NetworkInterfaceInfo& interface,
Davide Pesavento44deacc2014-02-19 10:48:07 +010068 const ethernet::Address& address)
Davide Pesaventob499a602014-11-18 22:36:56 +010069 : Face(FaceUri(address), FaceUri::fromDev(interface.name))
Davide Pesavento7726ae52014-11-23 21:01:05 +010070 , m_pcap(nullptr, pcap_close)
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +000071 , m_socket(socket)
Davide Pesavento10783f22014-03-15 04:40:01 +010072#if defined(__linux__)
73 , m_interfaceIndex(interface.index)
74#endif
Davide Pesaventob499a602014-11-18 22:36:56 +010075 , m_interfaceName(interface.name)
76 , m_srcAddress(interface.etherAddress)
Davide Pesavento44deacc2014-02-19 10:48:07 +010077 , m_destAddress(address)
Davide Pesavento9a090a02015-01-29 18:15:26 +010078#ifdef _DEBUG
79 , m_nDropped(0)
80#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +010081{
Davide Pesaventob60cc122014-03-19 19:26:02 +010082 NFD_LOG_INFO("Creating ethernet face on " << m_interfaceName << ": "
83 << m_srcAddress << " <--> " << m_destAddress);
Davide Pesavento44deacc2014-02-19 10:48:07 +010084 pcapInit();
85
Davide Pesavento7726ae52014-11-23 21:01:05 +010086 int fd = pcap_get_selectable_fd(m_pcap.get());
Davide Pesavento44deacc2014-02-19 10:48:07 +010087 if (fd < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +010088 throw Error("pcap_get_selectable_fd failed");
Davide Pesavento44deacc2014-02-19 10:48:07 +010089
90 // need to duplicate the fd, otherwise both pcap_close()
91 // and stream_descriptor::close() will try to close the
92 // same fd and one of them will fail
93 m_socket->assign(::dup(fd));
94
Davide Pesavento44deacc2014-02-19 10:48:07 +010095 m_interfaceMtu = getInterfaceMtu();
Davide Pesaventob60cc122014-03-19 19:26:02 +010096 NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName
Davide Pesavento44deacc2014-02-19 10:48:07 +010097 << "] Interface MTU is: " << m_interfaceMtu);
98
Matteo Sammarco66df9742014-11-21 18:31:26 +010099 m_slicer.reset(new ndnlp::Slicer(m_interfaceMtu));
100
Davide Pesavento44deacc2014-02-19 10:48:07 +0100101 char filter[100];
Davide Pesavento10783f22014-03-15 04:40:01 +0100102 std::snprintf(filter, sizeof(filter),
103 "(ether proto 0x%x) && (ether dst %s) && (not ether src %s)",
104 ethernet::ETHERTYPE_NDN,
105 m_destAddress.toString().c_str(),
106 m_srcAddress.toString().c_str());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100107 setPacketFilter(filter);
108
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100109 if (!m_destAddress.isBroadcast() && !joinMulticastGroup())
110 {
111 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
112 << "] Falling back to promiscuous mode");
113 pcap_set_promisc(m_pcap.get(), 1);
114 }
Davide Pesavento10783f22014-03-15 04:40:01 +0100115
Davide Pesavento44deacc2014-02-19 10:48:07 +0100116 m_socket->async_read_some(boost::asio::null_buffers(),
117 bind(&EthernetFace::handleRead, this,
118 boost::asio::placeholders::error,
119 boost::asio::placeholders::bytes_transferred));
120}
121
122EthernetFace::~EthernetFace()
123{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100124}
125
126void
127EthernetFace::sendInterest(const Interest& interest)
128{
Junxiao Shic099ddb2014-12-25 20:53:20 -0700129 this->emitSignal(onSendInterest, interest);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100130 ndnlp::PacketArray pa = m_slicer->slice(interest.wireEncode());
131 for (const auto& packet : *pa) {
132 sendPacket(packet);
133 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100134}
135
136void
137EthernetFace::sendData(const Data& data)
138{
Junxiao Shic099ddb2014-12-25 20:53:20 -0700139 this->emitSignal(onSendData, data);
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 Pesavento7726ae52014-11-23 21:01:05 +0100152 NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName
153 << "] Closing face");
154
155 boost::system::error_code error;
156 m_socket->cancel(error); // ignore errors
157 m_socket->close(error); // ignore errors
158 m_pcap.reset(nullptr);
159
160 fail("Face closed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100161}
162
163void
164EthernetFace::pcapInit()
165{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100166 char errbuf[PCAP_ERRBUF_SIZE] = {};
167 m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100168 if (!m_pcap)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100169 throw Error("pcap_create: " + std::string(errbuf));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100170
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200171#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
172 // Enable "immediate mode", effectively disabling any read buffering in the kernel.
173 // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including OS X)
174 // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3,
175 // even if the kernel supports it, thus preventing bug #1511.
Davide Pesavento7726ae52014-11-23 21:01:05 +0100176 pcap_set_immediate_mode(m_pcap.get(), 1);
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200177#endif
178
Davide Pesavento7726ae52014-11-23 21:01:05 +0100179 if (pcap_activate(m_pcap.get()) < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100180 throw Error("pcap_activate failed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100181
Davide Pesavento7726ae52014-11-23 21:01:05 +0100182 if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100183 throw Error("pcap_set_datalink: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100184
Davide Pesavento7726ae52014-11-23 21:01:05 +0100185 if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0)
Davide Pesavento9eaeac72014-03-15 05:03:42 +0100186 // no need to throw on failure, BPF will filter unwanted packets anyway
Davide Pesavento9a090a02015-01-29 18:15:26 +0100187 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
188 << "] pcap_setdirection failed: " << pcap_geterr(m_pcap.get()));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100189}
190
191void
192EthernetFace::setPacketFilter(const char* filterString)
193{
194 bpf_program filter;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100195 if (pcap_compile(m_pcap.get(), &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100196 throw Error("pcap_compile: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100197
Davide Pesavento7726ae52014-11-23 21:01:05 +0100198 int ret = pcap_setfilter(m_pcap.get(), &filter);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100199 pcap_freecode(&filter);
200 if (ret < 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100201 throw Error("pcap_setfilter: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100202}
203
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100204bool
Davide Pesavento10783f22014-03-15 04:40:01 +0100205EthernetFace::joinMulticastGroup()
206{
207#if defined(__linux__)
208 packet_mreq mr{};
209 mr.mr_ifindex = m_interfaceIndex;
210 mr.mr_type = PACKET_MR_MULTICAST;
211 mr.mr_alen = m_destAddress.size();
212 std::copy(m_destAddress.begin(), m_destAddress.end(), mr.mr_address);
213
214 if (::setsockopt(m_socket->native_handle(), SOL_PACKET,
215 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100216 return true; // success
Davide Pesavento10783f22014-03-15 04:40:01 +0100217
218 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
219 << "] setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno));
220#endif
221
222#if defined(SIOCADDMULTI)
223 ifreq ifr{};
224 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
225
226#if defined(__APPLE__) || defined(__FreeBSD__)
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100227 // see bug #2327
228 using boost::asio::ip::udp;
229 udp::socket sock(ref(getGlobalIoService()), udp::v4());
230 int fd = sock.native_handle();
231
Davide Pesavento10783f22014-03-15 04:40:01 +0100232 /*
233 * Differences between Linux and the BSDs (including OS X):
234 * o BSD does not have ifr_hwaddr; use ifr_addr instead.
235 * o While OS X seems to accept both AF_LINK and AF_UNSPEC as the address
236 * family, FreeBSD explicitly requires AF_LINK, so we have to use AF_LINK
237 * and sockaddr_dl instead of the generic sockaddr structure.
238 * o BSD's sockaddr (and sockaddr_dl in particular) contains an additional
239 * field, sa_len (sdl_len), which must be set to the total length of the
240 * structure, including the length field itself.
241 * o We do not specify the interface name, thus sdl_nlen is left at 0 and
242 * LLADDR is effectively the same as sdl_data.
243 */
244 sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(&ifr.ifr_addr);
245 sdl->sdl_len = sizeof(ifr.ifr_addr);
246 sdl->sdl_family = AF_LINK;
247 sdl->sdl_alen = m_destAddress.size();
248 std::copy(m_destAddress.begin(), m_destAddress.end(), LLADDR(sdl));
249
250 static_assert(sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) + ethernet::ADDR_LEN,
251 "ifr_addr in struct ifreq is too small on this platform");
252#else
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100253 int fd = m_socket->native_handle();
254
Davide Pesavento10783f22014-03-15 04:40:01 +0100255 ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
256 std::copy(m_destAddress.begin(), m_destAddress.end(), ifr.ifr_hwaddr.sa_data);
257
258 static_assert(sizeof(ifr.ifr_hwaddr) >= offsetof(sockaddr, sa_data) + ethernet::ADDR_LEN,
259 "ifr_hwaddr in struct ifreq is too small on this platform");
260#endif
261
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100262 if (::ioctl(fd, SIOCADDMULTI, &ifr) == 0)
263 return true; // success
Davide Pesavento10783f22014-03-15 04:40:01 +0100264
265 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
266 << "] ioctl(SIOCADDMULTI) failed: " << std::strerror(errno));
267#endif
268
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100269 return false;
Davide Pesavento10783f22014-03-15 04:40:01 +0100270}
271
272void
Davide Pesavento44deacc2014-02-19 10:48:07 +0100273EthernetFace::sendPacket(const ndn::Block& block)
274{
275 if (!m_pcap)
276 {
Davide Pesaventob60cc122014-03-19 19:26:02 +0100277 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
Davide Pesavento44deacc2014-02-19 10:48:07 +0100278 << "] Trying to send on closed face");
Davide Pesavento7726ae52014-11-23 21:01:05 +0100279 return fail("Face closed");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100280 }
281
Matteo Sammarco66df9742014-11-21 18:31:26 +0100282 BOOST_ASSERT(block.size() <= m_interfaceMtu);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100283
284 /// \todo Right now there is no reserve when packet is received, but
285 /// we should reserve some space at the beginning and at the end
286 ndn::EncodingBuffer buffer(block);
287
Davide Pesaventoe4631002014-03-21 20:55:36 +0100288 // pad with zeroes if the payload is too short
Davide Pesavento44deacc2014-02-19 10:48:07 +0100289 if (block.size() < ethernet::MIN_DATA_LEN)
290 {
Davide Pesavento8eb99572014-12-19 19:12:15 +0100291 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {};
Davide Pesaventob60cc122014-03-19 19:26:02 +0100292 buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100293 }
294
295 // construct and prepend the ethernet header
Junxiao Shi3ffe66d2014-11-06 15:37:59 -0700296 static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100297 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
Davide Pesaventob60cc122014-03-19 19:26:02 +0100298 buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100299 buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
300
301 // send the packet
Davide Pesavento7726ae52014-11-23 21:01:05 +0100302 int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100303 if (sent < 0)
304 {
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100305 return fail("pcap_inject: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100306 }
307 else if (static_cast<size_t>(sent) < buffer.size())
308 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100309 return fail("Failed to inject frame");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100310 }
311
Davide Pesaventob60cc122014-03-19 19:26:02 +0100312 NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
Davide Pesavento4c1a0782014-08-14 16:13:20 +0200313 << "] Successfully sent: " << block.size() << " bytes");
314 this->getMutableCounters().getNOutBytes() += block.size();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100315}
316
317void
318EthernetFace::handleRead(const boost::system::error_code& error, size_t)
319{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100320 if (!m_pcap)
321 return fail("Face closed");
322
Davide Pesavento44deacc2014-02-19 10:48:07 +0100323 if (error)
324 return processErrorCode(error);
325
Davide Pesavento7726ae52014-11-23 21:01:05 +0100326 pcap_pkthdr* header;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100327 const uint8_t* packet;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100328 int ret = pcap_next_ex(m_pcap.get(), &header, &packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100329 if (ret < 0)
330 {
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100331 return fail("pcap_next_ex: " + std::string(pcap_geterr(m_pcap.get())));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100332 }
333 else if (ret == 0)
334 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100335 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] Read timeout");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100336 }
337 else
338 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100339 processIncomingPacket(header, packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100340 }
341
Davide Pesavento9a090a02015-01-29 18:15:26 +0100342#ifdef _DEBUG
343 pcap_stat ps{};
344 ret = pcap_stats(m_pcap.get(), &ps);
345 if (ret < 0)
346 {
347 NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName
348 << "] pcap_stats failed: " << pcap_geterr(m_pcap.get()));
349 }
350 else if (ret == 0)
351 {
352 if (ps.ps_drop - m_nDropped > 0)
353 NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName
354 << "] Detected " << ps.ps_drop - m_nDropped << " dropped packet(s)");
355 m_nDropped = ps.ps_drop;
356 }
357#endif
358
Davide Pesavento44deacc2014-02-19 10:48:07 +0100359 m_socket->async_read_some(boost::asio::null_buffers(),
360 bind(&EthernetFace::handleRead, this,
361 boost::asio::placeholders::error,
362 boost::asio::placeholders::bytes_transferred));
363}
364
365void
Davide Pesavento7726ae52014-11-23 21:01:05 +0100366EthernetFace::processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100367{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100368 size_t length = header->caplen;
369 if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100370 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100371 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
372 << "] Received frame is too short (" << length << " bytes)");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100373 return;
374 }
375
Davide Pesavento7726ae52014-11-23 21:01:05 +0100376 const ether_header* eh = reinterpret_cast<const ether_header*>(packet);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100377 const ethernet::Address sourceAddress(eh->ether_shost);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100378
379 // assert in case BPF fails to filter unwanted frames
380 BOOST_ASSERT_MSG(ethernet::Address(eh->ether_dhost) == m_destAddress,
381 "Received frame addressed to a different multicast group");
Matteo Sammarco66df9742014-11-21 18:31:26 +0100382 BOOST_ASSERT_MSG(sourceAddress != m_srcAddress,
Davide Pesavento7726ae52014-11-23 21:01:05 +0100383 "Received frame sent by this host");
384 BOOST_ASSERT_MSG(ntohs(eh->ether_type) == ethernet::ETHERTYPE_NDN,
385 "Received frame with unrecognized ethertype");
386
387 packet += ethernet::HDR_LEN;
388 length -= ethernet::HDR_LEN;
389
390 /// \todo Reserve space in front and at the back of the underlying buffer
Matteo Sammarco66df9742014-11-21 18:31:26 +0100391 Block fragment;
392 bool isOk = Block::fromBuffer(packet, length, fragment);
393 if (!isOk)
Davide Pesavento7726ae52014-11-23 21:01:05 +0100394 {
395 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
Matteo Sammarco66df9742014-11-21 18:31:26 +0100396 << "] Block received from " << sourceAddress.toString()
397 << " is invalid or too large to process");
398 return;
Davide Pesavento7726ae52014-11-23 21:01:05 +0100399 }
Matteo Sammarco66df9742014-11-21 18:31:26 +0100400
401 NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
402 << "] Received: " << fragment.size() << " bytes from "
403 << sourceAddress.toString());
404 this->getMutableCounters().getNInBytes() += fragment.size();
405
406 Reassembler& reassembler = m_reassemblers[sourceAddress];
407 if (!reassembler.pms)
408 {
409 // new sender, setup a PartialMessageStore for it
410 reassembler.pms.reset(new ndnlp::PartialMessageStore);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700411 reassembler.pms->onReceive.connect(
Matteo Sammarco66df9742014-11-21 18:31:26 +0100412 [this, sourceAddress] (const Block& block) {
413 NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName
414 << "] All fragments received from " << sourceAddress.toString());
415 if (!decodeAndDispatchInput(block))
416 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
417 << "] Received unrecognized TLV block of type " << block.type()
418 << " from " << sourceAddress.toString());
Junxiao Shic099ddb2014-12-25 20:53:20 -0700419 });
Matteo Sammarco66df9742014-11-21 18:31:26 +0100420 }
421
422 scheduler::cancel(reassembler.expireEvent);
423 reassembler.expireEvent = scheduler::schedule(REASSEMBLER_LIFETIME,
424 [this, sourceAddress] {
425 BOOST_VERIFY(m_reassemblers.erase(sourceAddress) == 1);
426 });
427
428 try {
429 reassembler.pms->receiveNdnlpData(fragment);
430 }
431 catch (const ndnlp::ParseError& e) {
432 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
433 << "] Received invalid NDNLP fragment from "
434 << sourceAddress.toString() << " : " << e.what());
435 }
Davide Pesavento7726ae52014-11-23 21:01:05 +0100436}
437
438void
439EthernetFace::processErrorCode(const boost::system::error_code& error)
440{
441 if (error == boost::asio::error::operation_aborted)
442 // cancel() has been called on the socket
443 return;
444
Davide Pesavento44deacc2014-02-19 10:48:07 +0100445 std::string msg;
446 if (error == boost::asio::error::eof)
447 {
448 msg = "Face closed";
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100449 NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100450 }
451 else
452 {
Davide Pesavento7726ae52014-11-23 21:01:05 +0100453 msg = "Receive operation failed: " + error.message();
Davide Pesaventob60cc122014-03-19 19:26:02 +0100454 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100455 }
Junxiao Shi08d07a72014-06-09 23:17:57 -0700456 fail(msg);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100457}
458
Davide Pesavento44deacc2014-02-19 10:48:07 +0100459size_t
460EthernetFace::getInterfaceMtu() const
461{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100462#ifdef SIOCGIFMTU
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100463#if defined(__APPLE__) || defined(__FreeBSD__)
464 // see bug #2328
465 using boost::asio::ip::udp;
466 udp::socket sock(ref(getGlobalIoService()), udp::v4());
467 int fd = sock.native_handle();
468#else
469 int fd = m_socket->native_handle();
470#endif
471
Davide Pesavento10783f22014-03-15 04:40:01 +0100472 ifreq ifr{};
Davide Pesaventob60cc122014-03-19 19:26:02 +0100473 std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100474
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100475 if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0)
Davide Pesavento8eb99572014-12-19 19:12:15 +0100476 return static_cast<size_t>(ifr.ifr_mtu);
477
478 NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
479 << "] Failed to get interface MTU: " << std::strerror(errno));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100480#endif
481
Davide Pesavento8eb99572014-12-19 19:12:15 +0100482 return ethernet::MAX_DATA_LEN;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100483}
484
485} // namespace nfd