Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 24 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 25 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 26 | #include "ethernet-transport.hpp" |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 27 | #include "core/global-io.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 28 | |
| 29 | #include <pcap/pcap.h> |
| 30 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 31 | #include <cerrno> // for errno |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 32 | #include <cstring> // for memcpy(), strerror(), strncpy() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 33 | #include <arpa/inet.h> // for htons() and ntohs() |
| 34 | #include <net/ethernet.h> // for struct ether_header |
| 35 | #include <net/if.h> // for struct ifreq |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 36 | #include <sys/ioctl.h> // for ioctl() |
| 37 | #include <unistd.h> // for dup() |
| 38 | |
Alexander Afanasyev | d9479aa | 2014-06-26 13:52:02 -0700 | [diff] [blame] | 39 | #if !defined(PCAP_NETMASK_UNKNOWN) |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame^] | 40 | #define PCAP_NETMASK_UNKNOWN 0xffffffff |
Alexander Afanasyev | d9479aa | 2014-06-26 13:52:02 -0700 | [diff] [blame] | 41 | #endif |
| 42 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 43 | namespace nfd { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 44 | namespace face { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 45 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 46 | NFD_LOG_INIT("EthernetTransport"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 47 | |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame^] | 48 | EthernetTransport::EthernetTransport(const NetworkInterfaceInfo& localEndpoint, |
| 49 | const ethernet::Address& remoteEndpoint) |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 50 | : m_pcap(nullptr, pcap_close) |
| 51 | , m_socket(getGlobalIoService()) |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame^] | 52 | , m_srcAddress(localEndpoint.etherAddress) |
| 53 | , m_destAddress(remoteEndpoint) |
| 54 | , m_interfaceName(localEndpoint.name) |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 55 | #if defined(__linux__) |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame^] | 56 | , m_interfaceIndex(localEndpoint.index) |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 57 | #endif |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 58 | #ifdef _DEBUG |
| 59 | , m_nDropped(0) |
| 60 | #endif |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 61 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 62 | pcapInit(); |
| 63 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 64 | int fd = pcap_get_selectable_fd(m_pcap.get()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 65 | if (fd < 0) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 66 | BOOST_THROW_EXCEPTION(Error("pcap_get_selectable_fd failed")); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 67 | |
| 68 | // need to duplicate the fd, otherwise both pcap_close() |
| 69 | // and stream_descriptor::close() will try to close the |
| 70 | // same fd and one of them will fail |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 71 | m_socket.assign(::dup(fd)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 72 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 73 | // do this after assigning m_socket because getInterfaceMtu uses it |
| 74 | this->setMtu(getInterfaceMtu()); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 75 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 76 | m_socket.async_read_some(boost::asio::null_buffers(), |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 77 | bind(&EthernetTransport::handleRead, this, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 78 | boost::asio::placeholders::error, |
| 79 | boost::asio::placeholders::bytes_transferred)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame^] | 82 | void |
| 83 | EthernetTransport::doSend(Transport::Packet&& packet) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 84 | { |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 85 | NFD_LOG_FACE_TRACE(__func__); |
| 86 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 87 | sendPacket(packet.packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame^] | 90 | void |
| 91 | EthernetTransport::doClose() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 92 | { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 93 | NFD_LOG_FACE_TRACE(__func__); |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 94 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 95 | if (m_socket.is_open()) { |
| 96 | // Cancel all outstanding operations and close the socket. |
| 97 | // Use the non-throwing variants and ignore errors, if any. |
| 98 | boost::system::error_code error; |
| 99 | m_socket.cancel(error); |
| 100 | m_socket.close(error); |
| 101 | } |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 102 | m_pcap.reset(); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 103 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 104 | // Ensure that the Transport stays alive at least |
| 105 | // until all pending handlers are dispatched |
| 106 | getGlobalIoService().post([this] { |
| 107 | this->setState(TransportState::CLOSED); |
| 108 | }); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 112 | EthernetTransport::pcapInit() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 113 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 114 | char errbuf[PCAP_ERRBUF_SIZE] = {}; |
| 115 | m_pcap.reset(pcap_create(m_interfaceName.c_str(), errbuf)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 116 | if (!m_pcap) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 117 | BOOST_THROW_EXCEPTION(Error("pcap_create: " + std::string(errbuf))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 118 | |
Davide Pesavento | f0ae442 | 2014-05-05 16:32:12 +0200 | [diff] [blame] | 119 | #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE |
| 120 | // Enable "immediate mode", effectively disabling any read buffering in the kernel. |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame^] | 121 | // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including macOS) |
Davide Pesavento | f0ae442 | 2014-05-05 16:32:12 +0200 | [diff] [blame] | 122 | // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3, |
| 123 | // even if the kernel supports it, thus preventing bug #1511. |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 124 | pcap_set_immediate_mode(m_pcap.get(), 1); |
Davide Pesavento | f0ae442 | 2014-05-05 16:32:12 +0200 | [diff] [blame] | 125 | #endif |
| 126 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 127 | if (pcap_activate(m_pcap.get()) < 0) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 128 | BOOST_THROW_EXCEPTION(Error("pcap_activate failed")); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 129 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 130 | if (pcap_set_datalink(m_pcap.get(), DLT_EN10MB) < 0) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 131 | BOOST_THROW_EXCEPTION(Error("pcap_set_datalink: " + std::string(pcap_geterr(m_pcap.get())))); |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 132 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 133 | if (pcap_setdirection(m_pcap.get(), PCAP_D_IN) < 0) |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 134 | // no need to throw on failure, BPF will filter unwanted packets anyway |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 135 | NFD_LOG_FACE_WARN("pcap_setdirection failed: " << pcap_geterr(m_pcap.get())); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 139 | EthernetTransport::setPacketFilter(const char* filterString) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 140 | { |
| 141 | bpf_program filter; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 142 | if (pcap_compile(m_pcap.get(), &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 143 | BOOST_THROW_EXCEPTION(Error("pcap_compile: " + std::string(pcap_geterr(m_pcap.get())))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 144 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 145 | int ret = pcap_setfilter(m_pcap.get(), &filter); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 146 | pcap_freecode(&filter); |
| 147 | if (ret < 0) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 148 | BOOST_THROW_EXCEPTION(Error("pcap_setfilter: " + std::string(pcap_geterr(m_pcap.get())))); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 151 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 152 | EthernetTransport::sendPacket(const ndn::Block& block) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 153 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 154 | /// \todo Right now there is no reserve when packet is received, but |
| 155 | /// we should reserve some space at the beginning and at the end |
| 156 | ndn::EncodingBuffer buffer(block); |
| 157 | |
Davide Pesavento | e463100 | 2014-03-21 20:55:36 +0100 | [diff] [blame] | 158 | // pad with zeroes if the payload is too short |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 159 | if (block.size() < ethernet::MIN_DATA_LEN) { |
| 160 | static const uint8_t padding[ethernet::MIN_DATA_LEN] = {}; |
| 161 | buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size()); |
| 162 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 163 | |
| 164 | // construct and prepend the ethernet header |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 165 | static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 166 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 167 | buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 168 | buffer.prependByteArray(m_destAddress.data(), m_destAddress.size()); |
| 169 | |
| 170 | // send the packet |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 171 | int sent = pcap_inject(m_pcap.get(), buffer.buf(), buffer.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 172 | if (sent < 0) |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 173 | NFD_LOG_FACE_ERROR("pcap_inject failed: " << pcap_geterr(m_pcap.get())); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 174 | else if (static_cast<size_t>(sent) < buffer.size()) |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 175 | NFD_LOG_FACE_ERROR("Failed to send the full frame: bufsize=" << buffer.size() << " sent=" << sent); |
| 176 | else |
| 177 | // print block size because we don't want to count the padding in buffer |
| 178 | NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 182 | EthernetTransport::handleRead(const boost::system::error_code& error, size_t) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 183 | { |
| 184 | if (error) |
| 185 | return processErrorCode(error); |
| 186 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 187 | pcap_pkthdr* header; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 188 | const uint8_t* packet; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 189 | |
| 190 | // read the pcap header and packet data |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 191 | int ret = pcap_next_ex(m_pcap.get(), &header, &packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 192 | if (ret < 0) |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 193 | NFD_LOG_FACE_ERROR("pcap_next_ex failed: " << pcap_geterr(m_pcap.get())); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 194 | else if (ret == 0) |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 195 | NFD_LOG_FACE_WARN("Read timeout"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 196 | else |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 197 | processIncomingPacket(header, packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 198 | |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 199 | #ifdef _DEBUG |
| 200 | pcap_stat ps{}; |
| 201 | ret = pcap_stats(m_pcap.get(), &ps); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 202 | if (ret < 0) { |
| 203 | NFD_LOG_FACE_DEBUG("pcap_stats failed: " << pcap_geterr(m_pcap.get())); |
| 204 | } |
| 205 | else if (ret == 0) { |
| 206 | if (ps.ps_drop - m_nDropped > 0) |
| 207 | NFD_LOG_FACE_DEBUG("Detected " << ps.ps_drop - m_nDropped << " dropped packet(s)"); |
| 208 | m_nDropped = ps.ps_drop; |
| 209 | } |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 210 | #endif |
| 211 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 212 | m_socket.async_read_some(boost::asio::null_buffers(), |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 213 | bind(&EthernetTransport::handleRead, this, |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 214 | boost::asio::placeholders::error, |
| 215 | boost::asio::placeholders::bytes_transferred)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 219 | EthernetTransport::processIncomingPacket(const pcap_pkthdr* header, const uint8_t* packet) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 220 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 221 | size_t length = header->caplen; |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 222 | if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN) { |
| 223 | NFD_LOG_FACE_WARN("Received frame is too short (" << length << " bytes)"); |
| 224 | return; |
| 225 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 226 | |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 227 | const ether_header* eh = reinterpret_cast<const ether_header*>(packet); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 228 | const ethernet::Address sourceAddress(eh->ether_shost); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 229 | |
Davide Pesavento | ca25d72 | 2015-12-21 21:04:07 +0100 | [diff] [blame] | 230 | // in some cases VLAN-tagged frames may survive the BPF filter, |
| 231 | // make sure we do not process those frames (see #3348) |
| 232 | if (ntohs(eh->ether_type) != ethernet::ETHERTYPE_NDN) |
| 233 | return; |
| 234 | |
| 235 | // check that our BPF filter is working correctly |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 236 | BOOST_ASSERT_MSG(ethernet::Address(eh->ether_dhost) == m_destAddress, |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame^] | 237 | "Received frame addressed to another host or multicast group"); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 238 | BOOST_ASSERT_MSG(sourceAddress != m_srcAddress, |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 239 | "Received frame sent by this host"); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 240 | |
| 241 | packet += ethernet::HDR_LEN; |
| 242 | length -= ethernet::HDR_LEN; |
| 243 | |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 244 | bool isOk = false; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 245 | Block element; |
| 246 | std::tie(isOk, element) = Block::fromBuffer(packet, length); |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 247 | if (!isOk) { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 248 | NFD_LOG_FACE_WARN("Received invalid packet from " << sourceAddress.toString()); |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 249 | return; |
| 250 | } |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 251 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 252 | NFD_LOG_FACE_TRACE("Received: " << element.size() << " bytes from " << sourceAddress.toString()); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 253 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 254 | Transport::Packet tp(std::move(element)); |
| 255 | static_assert(sizeof(tp.remoteEndpoint) >= ethernet::ADDR_LEN, |
| 256 | "Transport::Packet::remoteEndpoint is too small"); |
| 257 | std::memcpy(&tp.remoteEndpoint, sourceAddress.data(), sourceAddress.size()); |
| 258 | this->receive(std::move(tp)); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 262 | EthernetTransport::processErrorCode(const boost::system::error_code& error) |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 263 | { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 264 | // boost::asio::error::operation_aborted must be checked first. In that situation, the Transport |
| 265 | // may already have been destructed, and it's unsafe to call getState() or do logging. |
| 266 | if (error == boost::asio::error::operation_aborted || |
| 267 | getState() == TransportState::CLOSING || |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 268 | getState() == TransportState::FAILED || |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 269 | getState() == TransportState::CLOSED) { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 270 | // transport is shutting down, ignore any errors |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 271 | return; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 272 | } |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 273 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 274 | NFD_LOG_FACE_WARN("Receive operation failed: " << error.message()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 277 | size_t |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 278 | EthernetTransport::getInterfaceMtu() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 279 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 280 | #ifdef SIOCGIFMTU |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 281 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 282 | // see bug #2328 |
| 283 | using boost::asio::ip::udp; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 284 | udp::socket sock(getGlobalIoService(), udp::v4()); |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 285 | int fd = sock.native_handle(); |
| 286 | #else |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 287 | int fd = m_socket.native_handle(); |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 288 | #endif |
| 289 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 290 | ifreq ifr{}; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 291 | std::strncpy(ifr.ifr_name, m_interfaceName.c_str(), sizeof(ifr.ifr_name) - 1); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 292 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 293 | if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0) { |
| 294 | NFD_LOG_FACE_DEBUG("Interface MTU is " << ifr.ifr_mtu); |
Davide Pesavento | 8eb9957 | 2014-12-19 19:12:15 +0100 | [diff] [blame] | 295 | return static_cast<size_t>(ifr.ifr_mtu); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 296 | } |
Davide Pesavento | 8eb9957 | 2014-12-19 19:12:15 +0100 | [diff] [blame] | 297 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 298 | NFD_LOG_FACE_WARN("Failed to get interface MTU: " << std::strerror(errno)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 299 | #endif |
| 300 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 301 | NFD_LOG_FACE_DEBUG("Assuming default MTU of " << ethernet::MAX_DATA_LEN); |
Davide Pesavento | 8eb9957 | 2014-12-19 19:12:15 +0100 | [diff] [blame] | 302 | return ethernet::MAX_DATA_LEN; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 303 | } |
| 304 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 305 | } // namespace face |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 306 | } // namespace nfd |