blob: 0773e85574f142c45d2ef7b4ec5d5bed67fdc691 [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 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 Pesavento35120ea2015-11-17 21:13:18 +010032#include <cstring> // for memcpy(), strerror(), strncpy()
Davide Pesavento44deacc2014-02-19 10:48:07 +010033#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 Pesavento44deacc2014-02-19 10:48:07 +010036#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 Pesavento9a090a02015-01-29 18:15:26 +010050#ifdef _DEBUG
51 , m_nDropped(0)
52#endif
Davide Pesavento44deacc2014-02-19 10:48:07 +010053{
Davide Pesaventofe0580c2017-05-12 02:02:10 -040054 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 Pesavento44deacc2014-02-19 10:48:07 +010061
Davide Pesavento35120ea2015-11-17 21:13:18 +010062 // do this after assigning m_socket because getInterfaceMtu uses it
63 this->setMtu(getInterfaceMtu());
Matteo Sammarco66df9742014-11-21 18:31:26 +010064
Davide Pesavento292e5e12015-03-13 02:08:33 +010065 m_socket.async_read_some(boost::asio::null_buffers(),
Davide Pesavento35120ea2015-11-17 21:13:18 +010066 bind(&EthernetTransport::handleRead, this,
Davide Pesavento292e5e12015-03-13 02:08:33 +010067 boost::asio::placeholders::error,
68 boost::asio::placeholders::bytes_transferred));
Davide Pesavento44deacc2014-02-19 10:48:07 +010069}
70
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040071void
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040072EthernetTransport::doClose()
Davide Pesavento44deacc2014-02-19 10:48:07 +010073{
Davide Pesavento35120ea2015-11-17 21:13:18 +010074 NFD_LOG_FACE_TRACE(__func__);
Alexander Afanasyev251f3c12014-06-02 18:39:58 +030075
Davide Pesavento35120ea2015-11-17 21:13:18 +010076 if (m_socket.is_open()) {
77 // Cancel all outstanding operations and close the socket.
78 // Use the non-throwing variants and ignore errors, if any.
79 boost::system::error_code error;
80 m_socket.cancel(error);
81 m_socket.close(error);
82 }
Davide Pesaventofe0580c2017-05-12 02:02:10 -040083 m_pcap.close();
Davide Pesavento7726ae52014-11-23 21:01:05 +010084
Davide Pesavento35120ea2015-11-17 21:13:18 +010085 // Ensure that the Transport stays alive at least
86 // until all pending handlers are dispatched
87 getGlobalIoService().post([this] {
88 this->setState(TransportState::CLOSED);
89 });
Davide Pesavento44deacc2014-02-19 10:48:07 +010090}
91
92void
Davide Pesaventofe0580c2017-05-12 02:02:10 -040093EthernetTransport::doSend(Transport::Packet&& packet)
Davide Pesavento44deacc2014-02-19 10:48:07 +010094{
Davide Pesaventofe0580c2017-05-12 02:02:10 -040095 NFD_LOG_FACE_TRACE(__func__);
Davide Pesavento44deacc2014-02-19 10:48:07 +010096
Davide Pesaventofe0580c2017-05-12 02:02:10 -040097 sendPacket(packet.packet);
Davide Pesavento44deacc2014-02-19 10:48:07 +010098}
99
Davide Pesavento10783f22014-03-15 04:40:01 +0100100void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100101EthernetTransport::sendPacket(const ndn::Block& block)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100102{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100103 ndn::EncodingBuffer buffer(block);
104
Davide Pesaventoe4631002014-03-21 20:55:36 +0100105 // pad with zeroes if the payload is too short
Davide Pesavento35120ea2015-11-17 21:13:18 +0100106 if (block.size() < ethernet::MIN_DATA_LEN) {
107 static const uint8_t padding[ethernet::MIN_DATA_LEN] = {};
108 buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size());
109 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100110
111 // construct and prepend the ethernet header
Junxiao Shi3ffe66d2014-11-06 15:37:59 -0700112 static uint16_t ethertype = htons(ethernet::ETHERTYPE_NDN);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100113 buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&ethertype), ethernet::TYPE_LEN);
Davide Pesaventob60cc122014-03-19 19:26:02 +0100114 buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100115 buffer.prependByteArray(m_destAddress.data(), m_destAddress.size());
116
117 // send the packet
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400118 int sent = pcap_inject(m_pcap, buffer.buf(), buffer.size());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100119 if (sent < 0)
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400120 NFD_LOG_FACE_ERROR("pcap_inject failed: " << m_pcap.getLastError());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100121 else if (static_cast<size_t>(sent) < buffer.size())
Davide Pesavento35120ea2015-11-17 21:13:18 +0100122 NFD_LOG_FACE_ERROR("Failed to send the full frame: bufsize=" << buffer.size() << " sent=" << sent);
123 else
124 // print block size because we don't want to count the padding in buffer
125 NFD_LOG_FACE_TRACE("Successfully sent: " << block.size() << " bytes");
Davide Pesavento44deacc2014-02-19 10:48:07 +0100126}
127
128void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100129EthernetTransport::handleRead(const boost::system::error_code& error, size_t)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100130{
131 if (error)
132 return processErrorCode(error);
133
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400134 const uint8_t* pkt;
135 size_t len;
136 std::string err;
137 std::tie(pkt, len, err) = m_pcap.readNextPacket();
Davide Pesavento35120ea2015-11-17 21:13:18 +0100138
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400139 if (pkt == nullptr)
140 NFD_LOG_FACE_ERROR("Read error: " << err);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100141 else
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400142 processIncomingPacket(pkt, len);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100143
Davide Pesavento9a090a02015-01-29 18:15:26 +0100144#ifdef _DEBUG
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400145 size_t nDropped = m_pcap.getNDropped();
146 if (nDropped - m_nDropped > 0)
147 NFD_LOG_FACE_DEBUG("Detected " << nDropped - m_nDropped << " dropped packet(s)");
148 m_nDropped = nDropped;
Davide Pesavento9a090a02015-01-29 18:15:26 +0100149#endif
150
Davide Pesavento292e5e12015-03-13 02:08:33 +0100151 m_socket.async_read_some(boost::asio::null_buffers(),
Davide Pesavento35120ea2015-11-17 21:13:18 +0100152 bind(&EthernetTransport::handleRead, this,
Davide Pesavento292e5e12015-03-13 02:08:33 +0100153 boost::asio::placeholders::error,
154 boost::asio::placeholders::bytes_transferred));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100155}
156
157void
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400158EthernetTransport::processIncomingPacket(const uint8_t* packet, size_t length)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100159{
Junxiao Shi083f7782015-02-21 12:06:26 -0700160 if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN) {
161 NFD_LOG_FACE_WARN("Received frame is too short (" << length << " bytes)");
162 return;
163 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100164
Davide Pesavento7726ae52014-11-23 21:01:05 +0100165 const ether_header* eh = reinterpret_cast<const ether_header*>(packet);
Matteo Sammarco66df9742014-11-21 18:31:26 +0100166 const ethernet::Address sourceAddress(eh->ether_shost);
Davide Pesavento7726ae52014-11-23 21:01:05 +0100167
Davide Pesaventoca25d722015-12-21 21:04:07 +0100168 // in some cases VLAN-tagged frames may survive the BPF filter,
169 // make sure we do not process those frames (see #3348)
170 if (ntohs(eh->ether_type) != ethernet::ETHERTYPE_NDN)
171 return;
172
173 // check that our BPF filter is working correctly
Davide Pesavento7726ae52014-11-23 21:01:05 +0100174 BOOST_ASSERT_MSG(ethernet::Address(eh->ether_dhost) == m_destAddress,
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -0400175 "Received frame addressed to another host or multicast group");
Matteo Sammarco66df9742014-11-21 18:31:26 +0100176 BOOST_ASSERT_MSG(sourceAddress != m_srcAddress,
Davide Pesavento7726ae52014-11-23 21:01:05 +0100177 "Received frame sent by this host");
Davide Pesavento7726ae52014-11-23 21:01:05 +0100178
179 packet += ethernet::HDR_LEN;
180 length -= ethernet::HDR_LEN;
181
Junxiao Shi78926c92015-02-28 22:56:06 -0700182 bool isOk = false;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100183 Block element;
184 std::tie(isOk, element) = Block::fromBuffer(packet, length);
Junxiao Shi083f7782015-02-21 12:06:26 -0700185 if (!isOk) {
Davide Pesavento35120ea2015-11-17 21:13:18 +0100186 NFD_LOG_FACE_WARN("Received invalid packet from " << sourceAddress.toString());
Junxiao Shi083f7782015-02-21 12:06:26 -0700187 return;
188 }
Matteo Sammarco66df9742014-11-21 18:31:26 +0100189
Davide Pesavento35120ea2015-11-17 21:13:18 +0100190 NFD_LOG_FACE_TRACE("Received: " << element.size() << " bytes from " << sourceAddress.toString());
Matteo Sammarco66df9742014-11-21 18:31:26 +0100191
Davide Pesavento35120ea2015-11-17 21:13:18 +0100192 Transport::Packet tp(std::move(element));
193 static_assert(sizeof(tp.remoteEndpoint) >= ethernet::ADDR_LEN,
194 "Transport::Packet::remoteEndpoint is too small");
195 std::memcpy(&tp.remoteEndpoint, sourceAddress.data(), sourceAddress.size());
196 this->receive(std::move(tp));
Davide Pesavento7726ae52014-11-23 21:01:05 +0100197}
198
199void
Davide Pesavento35120ea2015-11-17 21:13:18 +0100200EthernetTransport::processErrorCode(const boost::system::error_code& error)
Davide Pesavento7726ae52014-11-23 21:01:05 +0100201{
Junxiao Shi7003c602017-01-10 13:35:28 +0000202 // boost::asio::error::operation_aborted must be checked first. In that situation, the Transport
203 // may already have been destructed, and it's unsafe to call getState() or do logging.
204 if (error == boost::asio::error::operation_aborted ||
205 getState() == TransportState::CLOSING ||
Davide Pesavento35120ea2015-11-17 21:13:18 +0100206 getState() == TransportState::FAILED ||
Junxiao Shi7003c602017-01-10 13:35:28 +0000207 getState() == TransportState::CLOSED) {
Davide Pesavento35120ea2015-11-17 21:13:18 +0100208 // transport is shutting down, ignore any errors
Davide Pesavento7726ae52014-11-23 21:01:05 +0100209 return;
Junxiao Shi7003c602017-01-10 13:35:28 +0000210 }
Davide Pesavento7726ae52014-11-23 21:01:05 +0100211
Davide Pesavento35120ea2015-11-17 21:13:18 +0100212 NFD_LOG_FACE_WARN("Receive operation failed: " << error.message());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100213}
214
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400215int
Davide Pesavento35120ea2015-11-17 21:13:18 +0100216EthernetTransport::getInterfaceMtu()
Davide Pesavento44deacc2014-02-19 10:48:07 +0100217{
Davide Pesavento44deacc2014-02-19 10:48:07 +0100218#ifdef SIOCGIFMTU
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100219#if defined(__APPLE__) || defined(__FreeBSD__)
220 // see bug #2328
221 using boost::asio::ip::udp;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100222 udp::socket sock(getGlobalIoService(), udp::v4());
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100223 int fd = sock.native_handle();
224#else
Davide Pesavento292e5e12015-03-13 02:08:33 +0100225 int fd = m_socket.native_handle();
Davide Pesaventof8b41eb2014-12-26 19:14:06 +0100226#endif
227
Davide Pesavento10783f22014-03-15 04:40:01 +0100228 ifreq ifr{};
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400229 std::strncpy(ifr.ifr_name, m_interfaceName.data(), sizeof(ifr.ifr_name) - 1);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100230
Davide Pesavento35120ea2015-11-17 21:13:18 +0100231 if (::ioctl(fd, SIOCGIFMTU, &ifr) == 0) {
232 NFD_LOG_FACE_DEBUG("Interface MTU is " << ifr.ifr_mtu);
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400233 return ifr.ifr_mtu;
Davide Pesavento35120ea2015-11-17 21:13:18 +0100234 }
Davide Pesavento8eb99572014-12-19 19:12:15 +0100235
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100236 NFD_LOG_FACE_WARN("Failed to get interface MTU: " << std::strerror(errno));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100237#endif
238
Davide Pesavento35120ea2015-11-17 21:13:18 +0100239 NFD_LOG_FACE_DEBUG("Assuming default MTU of " << ethernet::MAX_DATA_LEN);
Davide Pesavento8eb99572014-12-19 19:12:15 +0100240 return ethernet::MAX_DATA_LEN;
Davide Pesavento44deacc2014-02-19 10:48:07 +0100241}
242
Davide Pesavento35120ea2015-11-17 21:13:18 +0100243} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100244} // namespace nfd