blob: a4c4006009ad26a1006584f54f65a0181d5565e0 [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7003c602017-01-10 13:35:28 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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
Davide Pesavento35120ea2015-11-17 21:13:18 +010026#include "ethernet-transport.hpp"
Davide Pesavento43ff2a92017-05-18 19:50:57 -040027#include "ethernet-protocol.hpp"
Davide Pesaventof8b41eb2014-12-26 19:14:06 +010028#include "core/global-io.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010029
30#include <pcap/pcap.h>
31
Davide Pesavento43ff2a92017-05-18 19:50:57 -040032#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 Afanasyevd9479aa2014-06-26 13:52:02 -070037
Davide Pesavento44deacc2014-02-19 10:48:07 +010038namespace nfd {
Davide Pesavento35120ea2015-11-17 21:13:18 +010039namespace face {
Davide Pesavento44deacc2014-02-19 10:48:07 +010040
Davide Pesavento35120ea2015-11-17 21:13:18 +010041NFD_LOG_INIT("EthernetTransport");
Davide Pesavento44deacc2014-02-19 10:48:07 +010042
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040043EthernetTransport::EthernetTransport(const NetworkInterfaceInfo& localEndpoint,
44 const ethernet::Address& remoteEndpoint)
Davide Pesaventofe0580c2017-05-12 02:02:10 -040045 : m_socket(getGlobalIoService())
46 , m_pcap(localEndpoint.name)
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040047 , m_srcAddress(localEndpoint.etherAddress)
48 , m_destAddress(remoteEndpoint)
49 , m_interfaceName(localEndpoint.name)
Davide Pesavento1816d4b2017-07-02 12:20:48 -040050 , m_hasBeenUsedRecently(false)
Davide Pesavento9a090a02015-01-29 18:15:26 +010051#ifdef _DEBUG
52 , m_nDropped(0)
53#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +010054{
Davide Pesaventofe0580c2017-05-12 02:02:10 -040055 try {
56 m_pcap.activate(DLT_EN10MB);
57 m_socket.assign(m_pcap.getFd());
58 }
59 catch (const PcapHelper::Error& e) {
60 BOOST_THROW_EXCEPTION(Error(e.what()));
61 }
Davide Pesavento44deacc2014-02-19 10:48:07 +010062
Davide Pesavento35120ea2015-11-17 21:13:18 +010063 // do this after assigning m_socket because getInterfaceMtu uses it
64 this->setMtu(getInterfaceMtu());
Matteo Sammarco66df9742014-11-21 18:31:26 +010065
Davide Pesavento43ff2a92017-05-18 19:50:57 -040066 asyncRead();
Davide Pesavento44deacc2014-02-19 10:48:07 +010067}
68
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040069void
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040070EthernetTransport::doClose()
Davide Pesavento44deacc2014-02-19 10:48:07 +010071{
Davide Pesavento35120ea2015-11-17 21:13:18 +010072 NFD_LOG_FACE_TRACE(__func__);
Alexander Afanasyev251f3c12014-06-02 18:39:58 +030073
Davide Pesavento35120ea2015-11-17 21:13:18 +010074 if (m_socket.is_open()) {
75 // Cancel all outstanding operations and close the socket.
76 // Use the non-throwing variants and ignore errors, if any.
77 boost::system::error_code error;
78 m_socket.cancel(error);
79 m_socket.close(error);
80 }
Davide Pesaventofe0580c2017-05-12 02:02:10 -040081 m_pcap.close();
Davide Pesavento7726ae52014-11-23 21:01:05 +010082
Davide Pesavento35120ea2015-11-17 21:13:18 +010083 // Ensure that the Transport stays alive at least
84 // until all pending handlers are dispatched
85 getGlobalIoService().post([this] {
86 this->setState(TransportState::CLOSED);
87 });
Davide Pesavento44deacc2014-02-19 10:48:07 +010088}
89
90void
Davide Pesaventofe0580c2017-05-12 02:02:10 -040091EthernetTransport::doSend(Transport::Packet&& packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +010092{
Davide Pesaventofe0580c2017-05-12 02:02:10 -040093 NFD_LOG_FACE_TRACE(__func__);
Davide Pesavento44deacc2014-02-19 10:48:07 +010094
Davide Pesaventofe0580c2017-05-12 02:02:10 -040095 sendPacket(packet.packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +010096}
97
Davide Pesavento10783f22014-03-15 04:40:01 +010098void
Davide Pesavento35120ea2015-11-17 21:13:18 +010099EthernetTransport::sendPacket(const ndn::Block& block)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100100{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100101 ndn::EncodingBuffer buffer(block);
102
Davide Pesaventoe4631002014-03-21 20:55:36 +0100103 // pad with zeroes if the payload is too short
Davide Pesavento35120ea2015-11-17 21:13:18 +0100104 if (block.size() < ethernet::MIN_DATA_LEN) {
105 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {};
106 buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size());
107 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100108
109 // construct and prepend the ethernet header
Junxiao Shi3ffe66d2014-11-06 15:37:59 -0700110 static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100111 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
Davide Pesaventob60cc122014-03-19 19:26:02 +0100112 buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100113 buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
114
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400115 // send the frame
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400116 int sent = pcap_inject(m_pcap, buffer.buf(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100117 if (sent < 0)
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400118 handleError("Send operation failed: " + m_pcap.getLastError());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100119 else if (static_cast<size_t>(sent) < buffer.size())
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400120 handleError("Failed to send the full frame: size=" + to_string(buffer.size()) +
121 " sent=" + to_string(sent));
Davide Pesavento35120ea2015-11-17 21:13:18 +0100122 else
123 // print block size because we don't want to count the padding in buffer
124 NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100125}
126
127void
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400128EthernetTransport::asyncRead()
129{
130 m_socket.async_read_some(boost::asio::null_buffers(),
131 bind(&EthernetTransport::handleRead, this,
132 boost::asio::placeholders::error));
133}
134
135void
136EthernetTransport::handleRead(const boost::system::error_code& error)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100137{
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400138 if (error) {
139 // boost::asio::error::operation_aborted must be checked first: in that case, the Transport
140 // may already have been destructed, therefore it's unsafe to call getState() or do logging.
141 if (error != boost::asio::error::operation_aborted &&
142 getState() != TransportState::CLOSING &&
143 getState() != TransportState::FAILED &&
144 getState() != TransportState::CLOSED) {
145 handleError("Receive operation failed: " + error.message());
146 }
147 return;
148 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100149
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400150 const uint8_t* pkt;
151 size_t len;
152 std::string err;
153 std::tie(pkt, len, err) = m_pcap.readNextPacket();
Davide Pesavento35120ea2015-11-17 21:13:18 +0100154
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400155 if (pkt == nullptr) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400156 NFD_LOG_FACE_WARN("Read error: " << err);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400157 }
158 else {
159 const ether_header* eh;
160 std::tie(eh, err) = ethernet::checkFrameHeader(pkt, len, m_srcAddress,
161 m_destAddress.isMulticast() ? m_destAddress : m_srcAddress);
162 if (eh == nullptr) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400163 NFD_LOG_FACE_WARN(err);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400164 }
165 else {
166 ethernet::Address sender(eh->ether_shost);
167 pkt += ethernet::HDR_LEN;
168 len -= ethernet::HDR_LEN;
169 receivePayload(pkt, len, sender);
170 }
171 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100172
Davide Pesavento9a090a02015-01-29 18:15:26 +0100173#ifdef _DEBUG
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400174 size_t nDropped = m_pcap.getNDropped();
175 if (nDropped - m_nDropped > 0)
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400176 NFD_LOG_FACE_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)");
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400177 m_nDropped = nDropped;
Davide Pesavento9a090a02015-01-29 18:15:26 +0100178#endif
179
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400180 asyncRead();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100181}
182
183void
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400184EthernetTransport::receivePayload(const uint8_t* payload, size_t length,
185 const ethernet::Address& sender)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100186{
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400187 NFD_LOG_FACE_TRACE("Received: " << length << " bytes from " << sender);
188
Junxiao Shi78926c92015-02-28 22:56:06 -0700189 bool isOk = false;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100190 Block element;
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400191 std::tie(isOk, element) = Block::fromBuffer(payload, length);
Junxiao Shi083f7782015-02-21 12:06:26 -0700192 if (!isOk) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400193 NFD_LOG_FACE_WARN("Failed to parse incoming packet from " << sender);
194 // This packet won't extend the face lifetime
Junxiao Shi083f7782015-02-21 12:06:26 -0700195 return;
196 }
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400197 m_hasBeenUsedRecently = true;
Matteo Sammarco66df9742014-11-21 18:31:26 +0100198
Davide Pesavento35120ea2015-11-17 21:13:18 +0100199 Transport::Packet tp(std::move(element));
200 static_assert(sizeof(tp.remoteEndpoint) >= ethernet::ADDR_LEN,
201 "Transport::Packet::remoteEndpoint is too small");
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400202 if (m_destAddress.isMulticast()) {
203 std::memcpy(&tp.remoteEndpoint, sender.data(), sender.size());
204 }
Davide Pesavento35120ea2015-11-17 21:13:18 +0100205 this->receive(std::move(tp));
Davide Pesavento7726ae52014-11-23 21:01:05 +0100206}
207
208void
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400209EthernetTransport::handleError(const std::string& errorMessage)
Davide Pesavento7726ae52014-11-23 21:01:05 +0100210{
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400211 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
212 NFD_LOG_FACE_DEBUG("Permanent face ignores error: " << errorMessage);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100213 return;
Junxiao Shi7003c602017-01-10 13:35:28 +0000214 }
Davide Pesavento7726ae52014-11-23 21:01:05 +0100215
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400216 NFD_LOG_FACE_ERROR(errorMessage);
217 this->setState(TransportState::FAILED);
218 doClose();
Davide Pesavento44deacc2014-02-19 10:48:07 +0100219}
220
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400221int
Davide Pesavento35120ea2015-11-17 21:13:18 +0100222EthernetTransport::getInterfaceMtu()
Davide Pesavento44deacc2014-02-19 10:48:07 +0100223{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100224#ifdef SIOCGIFMTU
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100225#if defined(__APPLE__) || defined(__FreeBSD__)
226 // see bug #2328
227 using boost::asio::ip::udp;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100228 udp::socket sock(getGlobalIoService(), udp::v4());
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100229 int fd = sock.native_handle();
230#else
Davide Pesavento292e5e12015-03-13 02:08:33 +0100231 int fd = m_socket.native_handle();
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100232#endif
233
Davide Pesavento10783f22014-03-15 04:40:01 +0100234 ifreq ifr{};
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400235 std::strncpy(ifr.ifr_name, m_interfaceName.data(), sizeof(ifr.ifr_name) - 1);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100236
Davide Pesavento35120ea2015-11-17 21:13:18 +0100237 if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0) {
238 NFD_LOG_FACE_DEBUG("Interface MTU is " << ifr.ifr_mtu);
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400239 return ifr.ifr_mtu;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100240 }
Davide Pesavento8eb99572014-12-19 19:12:15 +0100241
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100242 NFD_LOG_FACE_WARN("Failed to get interface MTU: " << std::strerror(errno));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100243#endif
244
Davide Pesavento35120ea2015-11-17 21:13:18 +0100245 NFD_LOG_FACE_DEBUG("Assuming default MTU of " << ethernet::MAX_DATA_LEN);
Davide Pesavento8eb99572014-12-19 19:12:15 +0100246 return ethernet::MAX_DATA_LEN;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100247}
248
Davide Pesavento35120ea2015-11-17 21:13:18 +0100249} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100250} // namespace nfd