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 | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 27 | #include "ethernet-protocol.hpp" |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 28 | #include "core/global-io.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 29 | |
| 30 | #include <pcap/pcap.h> |
| 31 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 32 | #include <cerrno> // for errno |
| 33 | #include <cstring> // for memcpy(), strerror(), strncpy() |
| 34 | #include <arpa/inet.h> // for htons() |
| 35 | #include <net/if.h> // for struct ifreq |
| 36 | #include <sys/ioctl.h> // for ioctl() |
Alexander Afanasyev | d9479aa | 2014-06-26 13:52:02 -0700 | [diff] [blame] | 37 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 38 | namespace nfd { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 39 | namespace face { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 40 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 41 | NFD_LOG_INIT("EthernetTransport"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 42 | |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 43 | EthernetTransport::EthernetTransport(const NetworkInterfaceInfo& localEndpoint, |
| 44 | const ethernet::Address& remoteEndpoint) |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 45 | : m_socket(getGlobalIoService()) |
| 46 | , m_pcap(localEndpoint.name) |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 47 | , m_srcAddress(localEndpoint.etherAddress) |
| 48 | , m_destAddress(remoteEndpoint) |
| 49 | , m_interfaceName(localEndpoint.name) |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 50 | #ifdef _DEBUG |
| 51 | , m_nDropped(0) |
| 52 | #endif |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 53 | { |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 54 | try { |
| 55 | m_pcap.activate(DLT_EN10MB); |
| 56 | m_socket.assign(m_pcap.getFd()); |
| 57 | } |
| 58 | catch (const PcapHelper::Error& e) { |
| 59 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 60 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 61 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 62 | // do this after assigning m_socket because getInterfaceMtu uses it |
| 63 | this->setMtu(getInterfaceMtu()); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 64 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 65 | asyncRead(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 68 | void |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 69 | EthernetTransport::doClose() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 70 | { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 71 | NFD_LOG_FACE_TRACE(__func__); |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 72 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 73 | if (m_socket.is_open()) { |
| 74 | // Cancel all outstanding operations and close the socket. |
| 75 | // Use the non-throwing variants and ignore errors, if any. |
| 76 | boost::system::error_code error; |
| 77 | m_socket.cancel(error); |
| 78 | m_socket.close(error); |
| 79 | } |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 80 | m_pcap.close(); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 81 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 82 | // Ensure that the Transport stays alive at least |
| 83 | // until all pending handlers are dispatched |
| 84 | getGlobalIoService().post([this] { |
| 85 | this->setState(TransportState::CLOSED); |
| 86 | }); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 90 | EthernetTransport::doSend(Transport::Packet&& packet) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 91 | { |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 92 | NFD_LOG_FACE_TRACE(__func__); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 93 | |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 94 | sendPacket(packet.packet); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 97 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 98 | EthernetTransport::sendPacket(const ndn::Block& block) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 99 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 100 | ndn::EncodingBuffer buffer(block); |
| 101 | |
Davide Pesavento | e463100 | 2014-03-21 20:55:36 +0100 | [diff] [blame] | 102 | // pad with zeroes if the payload is too short |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 103 | if (block.size() < ethernet::MIN_DATA_LEN) { |
| 104 | static const uint8_t padding[ethernet::MIN_DATA_LEN] = {}; |
| 105 | buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size()); |
| 106 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 107 | |
| 108 | // construct and prepend the ethernet header |
Junxiao Shi | 3ffe66d | 2014-11-06 15:37:59 -0700 | [diff] [blame] | 109 | static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 110 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 111 | buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 112 | buffer.prependByteArray(m_destAddress.data(), m_destAddress.size()); |
| 113 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 114 | // send the frame |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 115 | int sent = pcap_inject(m_pcap, buffer.buf(), buffer.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 116 | if (sent < 0) |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 117 | NFD_LOG_FACE_ERROR("pcap_inject failed: " << m_pcap.getLastError()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 118 | else if (static_cast<size_t>(sent) < buffer.size()) |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 119 | NFD_LOG_FACE_ERROR("Failed to send the full frame: size=" << buffer.size() << " sent=" << sent); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 120 | else |
| 121 | // print block size because we don't want to count the padding in buffer |
| 122 | NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 126 | EthernetTransport::asyncRead() |
| 127 | { |
| 128 | m_socket.async_read_some(boost::asio::null_buffers(), |
| 129 | bind(&EthernetTransport::handleRead, this, |
| 130 | boost::asio::placeholders::error)); |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | EthernetTransport::handleRead(const boost::system::error_code& error) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 135 | { |
| 136 | if (error) |
| 137 | return processErrorCode(error); |
| 138 | |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 139 | const uint8_t* pkt; |
| 140 | size_t len; |
| 141 | std::string err; |
| 142 | std::tie(pkt, len, err) = m_pcap.readNextPacket(); |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 143 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 144 | if (pkt == nullptr) { |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 145 | NFD_LOG_FACE_ERROR("Read error: " << err); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 146 | } |
| 147 | else { |
| 148 | const ether_header* eh; |
| 149 | std::tie(eh, err) = ethernet::checkFrameHeader(pkt, len, m_srcAddress, |
| 150 | m_destAddress.isMulticast() ? m_destAddress : m_srcAddress); |
| 151 | if (eh == nullptr) { |
| 152 | NFD_LOG_FACE_DEBUG(err); |
| 153 | } |
| 154 | else { |
| 155 | ethernet::Address sender(eh->ether_shost); |
| 156 | pkt += ethernet::HDR_LEN; |
| 157 | len -= ethernet::HDR_LEN; |
| 158 | receivePayload(pkt, len, sender); |
| 159 | } |
| 160 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 161 | |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 162 | #ifdef _DEBUG |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 163 | size_t nDropped = m_pcap.getNDropped(); |
| 164 | if (nDropped - m_nDropped > 0) |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 165 | NFD_LOG_FACE_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)"); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 166 | m_nDropped = nDropped; |
Davide Pesavento | 9a090a0 | 2015-01-29 18:15:26 +0100 | [diff] [blame] | 167 | #endif |
| 168 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 169 | asyncRead(); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 173 | EthernetTransport::receivePayload(const uint8_t* payload, size_t length, |
| 174 | const ethernet::Address& sender) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 175 | { |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 176 | bool isOk = false; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 177 | Block element; |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 178 | std::tie(isOk, element) = Block::fromBuffer(payload, length); |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 179 | if (!isOk) { |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 180 | NFD_LOG_FACE_WARN("Received invalid packet from " << sender); |
Junxiao Shi | 083f778 | 2015-02-21 12:06:26 -0700 | [diff] [blame] | 181 | return; |
| 182 | } |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 183 | |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 184 | NFD_LOG_FACE_TRACE("Received: " << element.size() << " bytes from " << sender); |
Matteo Sammarco | 66df974 | 2014-11-21 18:31:26 +0100 | [diff] [blame] | 185 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 186 | Transport::Packet tp(std::move(element)); |
| 187 | static_assert(sizeof(tp.remoteEndpoint) >= ethernet::ADDR_LEN, |
| 188 | "Transport::Packet::remoteEndpoint is too small"); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame^] | 189 | if (m_destAddress.isMulticast()) { |
| 190 | std::memcpy(&tp.remoteEndpoint, sender.data(), sender.size()); |
| 191 | } |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 192 | this->receive(std::move(tp)); |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 196 | EthernetTransport::processErrorCode(const boost::system::error_code& error) |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 197 | { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 198 | // boost::asio::error::operation_aborted must be checked first. In that situation, the Transport |
| 199 | // may already have been destructed, and it's unsafe to call getState() or do logging. |
| 200 | if (error == boost::asio::error::operation_aborted || |
| 201 | getState() == TransportState::CLOSING || |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 202 | getState() == TransportState::FAILED || |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 203 | getState() == TransportState::CLOSED) { |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 204 | // transport is shutting down, ignore any errors |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 205 | return; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 206 | } |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 207 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 208 | NFD_LOG_FACE_WARN("Receive operation failed: " << error.message()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 211 | int |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 212 | EthernetTransport::getInterfaceMtu() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 213 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 214 | #ifdef SIOCGIFMTU |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 215 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 216 | // see bug #2328 |
| 217 | using boost::asio::ip::udp; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 218 | udp::socket sock(getGlobalIoService(), udp::v4()); |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 219 | int fd = sock.native_handle(); |
| 220 | #else |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 221 | int fd = m_socket.native_handle(); |
Davide Pesavento | f8b41eb | 2014-12-26 19:14:06 +0100 | [diff] [blame] | 222 | #endif |
| 223 | |
Davide Pesavento | 10783f2 | 2014-03-15 04:40:01 +0100 | [diff] [blame] | 224 | ifreq ifr{}; |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 225 | std::strncpy(ifr.ifr_name, m_interfaceName.data(), sizeof(ifr.ifr_name) - 1); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 226 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 227 | if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0) { |
| 228 | NFD_LOG_FACE_DEBUG("Interface MTU is " << ifr.ifr_mtu); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 229 | return ifr.ifr_mtu; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 230 | } |
Davide Pesavento | 8eb9957 | 2014-12-19 19:12:15 +0100 | [diff] [blame] | 231 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 232 | NFD_LOG_FACE_WARN("Failed to get interface MTU: " << std::strerror(errno)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 233 | #endif |
| 234 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 235 | NFD_LOG_FACE_DEBUG("Assuming default MTU of " << ethernet::MAX_DATA_LEN); |
Davide Pesavento | 8eb9957 | 2014-12-19 19:12:15 +0100 | [diff] [blame] | 236 | return ethernet::MAX_DATA_LEN; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 239 | } // namespace face |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 240 | } // namespace nfd |