Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 24 | |
| 25 | #include "ethernet-face.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 26 | #include "core/logger.hpp" |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 27 | #include "core/network-interface.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 28 | |
| 29 | #include <pcap/pcap.h> |
| 30 | |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 31 | #include <cstring> // for std::strncpy() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 32 | #include <arpa/inet.h> // for htons() and ntohs() |
| 33 | #include <net/ethernet.h> // for struct ether_header |
| 34 | #include <net/if.h> // for struct ifreq |
| 35 | #include <stdio.h> // for snprintf() |
| 36 | #include <sys/ioctl.h> // for ioctl() |
| 37 | #include <unistd.h> // for dup() |
| 38 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 39 | namespace nfd { |
| 40 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 41 | NFD_LOG_INIT("EthernetFace"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 42 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 43 | EthernetFace::EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket, |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 44 | const shared_ptr<NetworkInterfaceInfo>& interface, |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 45 | const ethernet::Address& address) |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 46 | : Face(FaceUri(address), FaceUri::fromDev(interface->name)) |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 47 | , m_socket(socket) |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 48 | , m_interfaceName(interface->name) |
| 49 | , m_srcAddress(interface->etherAddress) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 50 | , m_destAddress(address) |
| 51 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 52 | NFD_LOG_INFO("Creating ethernet face on " << m_interfaceName << ": " |
| 53 | << m_srcAddress << " <--> " << m_destAddress); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 54 | pcapInit(); |
| 55 | |
| 56 | int fd = pcap_get_selectable_fd(m_pcap); |
| 57 | if (fd < 0) |
| 58 | throw Error("pcap_get_selectable_fd() failed"); |
| 59 | |
| 60 | // need to duplicate the fd, otherwise both pcap_close() |
| 61 | // and stream_descriptor::close() will try to close the |
| 62 | // same fd and one of them will fail |
| 63 | m_socket->assign(::dup(fd)); |
| 64 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 65 | m_interfaceMtu = getInterfaceMtu(); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 66 | NFD_LOG_DEBUG("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 67 | << "] Interface MTU is: " << m_interfaceMtu); |
| 68 | |
| 69 | char filter[100]; |
| 70 | ::snprintf(filter, sizeof(filter), |
| 71 | "(ether proto 0x%x) && (ether dst %s) && (not ether src %s)", |
| 72 | ETHERTYPE_NDN, |
Davide Pesavento | edae353 | 2014-04-09 03:07:11 +0200 | [diff] [blame] | 73 | m_destAddress.toString().c_str(), |
| 74 | m_srcAddress.toString().c_str()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 75 | setPacketFilter(filter); |
| 76 | |
| 77 | m_socket->async_read_some(boost::asio::null_buffers(), |
| 78 | bind(&EthernetFace::handleRead, this, |
| 79 | boost::asio::placeholders::error, |
| 80 | boost::asio::placeholders::bytes_transferred)); |
| 81 | } |
| 82 | |
| 83 | EthernetFace::~EthernetFace() |
| 84 | { |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 85 | onFail.clear(); // no reason to call onFail anymore |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 86 | close(); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | EthernetFace::sendInterest(const Interest& interest) |
| 91 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 92 | onSendInterest(interest); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 93 | sendPacket(interest.wireEncode()); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | EthernetFace::sendData(const Data& data) |
| 98 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 99 | onSendData(data); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 100 | sendPacket(data.wireEncode()); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | EthernetFace::close() |
| 105 | { |
| 106 | if (m_pcap) |
| 107 | { |
| 108 | boost::system::error_code error; |
| 109 | m_socket->close(error); // ignore errors |
| 110 | pcap_close(m_pcap); |
| 111 | m_pcap = 0; |
Alexander Afanasyev | 251f3c1 | 2014-06-02 18:39:58 +0300 | [diff] [blame] | 112 | |
| 113 | onFail("Face closed"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | EthernetFace::pcapInit() |
| 119 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 120 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 121 | errbuf[0] = '\0'; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 122 | m_pcap = pcap_create(m_interfaceName.c_str(), errbuf); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 123 | if (!m_pcap) |
| 124 | throw Error("pcap_create(): " + std::string(errbuf)); |
| 125 | |
Davide Pesavento | f0ae442 | 2014-05-05 16:32:12 +0200 | [diff] [blame] | 126 | #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE |
| 127 | // Enable "immediate mode", effectively disabling any read buffering in the kernel. |
| 128 | // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including OS X) |
| 129 | // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3, |
| 130 | // even if the kernel supports it, thus preventing bug #1511. |
| 131 | pcap_set_immediate_mode(m_pcap, 1); |
| 132 | #endif |
| 133 | |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 134 | /// \todo Do not rely on promisc mode, see task #1278 |
| 135 | if (!m_destAddress.isBroadcast()) |
| 136 | pcap_set_promisc(m_pcap, 1); |
| 137 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 138 | if (pcap_activate(m_pcap) < 0) |
| 139 | throw Error("pcap_activate() failed"); |
| 140 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 141 | if (pcap_set_datalink(m_pcap, DLT_EN10MB) < 0) |
| 142 | throw Error("pcap_set_datalink(): " + std::string(pcap_geterr(m_pcap))); |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 143 | |
| 144 | if (pcap_setdirection(m_pcap, PCAP_D_IN) < 0) |
| 145 | // no need to throw on failure, BPF will filter unwanted packets anyway |
| 146 | NFD_LOG_WARN("pcap_setdirection(): " << pcap_geterr(m_pcap)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void |
| 150 | EthernetFace::setPacketFilter(const char* filterString) |
| 151 | { |
| 152 | bpf_program filter; |
| 153 | if (pcap_compile(m_pcap, &filter, filterString, 1, PCAP_NETMASK_UNKNOWN) < 0) |
| 154 | throw Error("pcap_compile(): " + std::string(pcap_geterr(m_pcap))); |
| 155 | |
| 156 | int ret = pcap_setfilter(m_pcap, &filter); |
| 157 | pcap_freecode(&filter); |
| 158 | if (ret < 0) |
| 159 | throw Error("pcap_setfilter(): " + std::string(pcap_geterr(m_pcap))); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | EthernetFace::sendPacket(const ndn::Block& block) |
| 164 | { |
| 165 | if (!m_pcap) |
| 166 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 167 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 168 | << "] Trying to send on closed face"); |
| 169 | onFail("Face closed"); |
| 170 | return; |
| 171 | } |
| 172 | |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 173 | /// \todo Fragmentation |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 174 | if (block.size() > m_interfaceMtu) |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 175 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 176 | NFD_LOG_ERROR("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 177 | << "] Fragmentation not implemented: dropping packet larger than MTU"); |
Davide Pesavento | 9eaeac7 | 2014-03-15 05:03:42 +0100 | [diff] [blame] | 178 | return; |
| 179 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 180 | |
| 181 | /// \todo Right now there is no reserve when packet is received, but |
| 182 | /// we should reserve some space at the beginning and at the end |
| 183 | ndn::EncodingBuffer buffer(block); |
| 184 | |
Davide Pesavento | e463100 | 2014-03-21 20:55:36 +0100 | [diff] [blame] | 185 | // pad with zeroes if the payload is too short |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 186 | if (block.size() < ethernet::MIN_DATA_LEN) |
| 187 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 188 | static const uint8_t padding[ethernet::MIN_DATA_LEN] = {0}; |
| 189 | buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // construct and prepend the ethernet header |
| 193 | static uint16_t ethertype = htons(ETHERTYPE_NDN); |
| 194 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 195 | buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 196 | buffer.prependByteArray(m_destAddress.data(), m_destAddress.size()); |
| 197 | |
| 198 | // send the packet |
| 199 | int sent = pcap_inject(m_pcap, buffer.buf(), buffer.size()); |
| 200 | if (sent < 0) |
| 201 | { |
| 202 | throw Error("pcap_inject(): " + std::string(pcap_geterr(m_pcap))); |
| 203 | } |
| 204 | else if (static_cast<size_t>(sent) < buffer.size()) |
| 205 | { |
| 206 | throw Error("Failed to send packet"); |
| 207 | } |
| 208 | |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 209 | NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 210 | << "] Successfully sent: " << buffer.size() << " bytes"); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | EthernetFace::handleRead(const boost::system::error_code& error, size_t) |
| 215 | { |
| 216 | if (error) |
| 217 | return processErrorCode(error); |
| 218 | |
| 219 | pcap_pkthdr* pktHeader; |
| 220 | const uint8_t* packet; |
| 221 | int ret = pcap_next_ex(m_pcap, &pktHeader, &packet); |
| 222 | if (ret < 0) |
| 223 | { |
| 224 | throw Error("pcap_next_ex(): " + std::string(pcap_geterr(m_pcap))); |
| 225 | } |
| 226 | else if (ret == 0) |
| 227 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 228 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 229 | << "] pcap_next_ex() timed out"); |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | size_t length = pktHeader->caplen; |
| 234 | if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN) |
| 235 | throw Error("Received packet is too short"); |
| 236 | |
| 237 | const ether_header* eh = reinterpret_cast<const ether_header*>(packet); |
| 238 | if (ntohs(eh->ether_type) != ETHERTYPE_NDN) |
| 239 | throw Error("Unrecognized ethertype"); |
| 240 | |
| 241 | packet += ethernet::HDR_LEN; |
| 242 | length -= ethernet::HDR_LEN; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 243 | NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 244 | << "] Received: " << length << " bytes"); |
| 245 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 246 | /// \todo Reserve space in front and at the back |
| 247 | /// of the underlying buffer |
| 248 | Block element; |
| 249 | bool isOk = Block::fromBuffer(packet, length, element); |
| 250 | if (isOk) |
| 251 | { |
| 252 | if (!decodeAndDispatchInput(element)) |
| 253 | { |
| 254 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 255 | << "] Received unrecognized block of type " << element.type()); |
| 256 | // ignore unknown packet |
| 257 | } |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 262 | << "] Received block is invalid or too large to process"); |
| 263 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | m_socket->async_read_some(boost::asio::null_buffers(), |
| 267 | bind(&EthernetFace::handleRead, this, |
| 268 | boost::asio::placeholders::error, |
| 269 | boost::asio::placeholders::bytes_transferred)); |
| 270 | } |
| 271 | |
| 272 | void |
| 273 | EthernetFace::processErrorCode(const boost::system::error_code& error) |
| 274 | { |
| 275 | if (error == boost::system::errc::operation_canceled) |
| 276 | // when socket is closed by someone |
| 277 | return; |
| 278 | |
| 279 | if (!m_pcap) |
| 280 | { |
| 281 | onFail("Face closed"); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | std::string msg; |
| 286 | if (error == boost::asio::error::eof) |
| 287 | { |
| 288 | msg = "Face closed"; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 289 | NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 290 | } |
| 291 | else |
| 292 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 293 | msg = "Receive operation failed, closing face: " + error.category().message(error.value()); |
| 294 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | close(); |
| 298 | onFail(msg); |
| 299 | } |
| 300 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 301 | size_t |
| 302 | EthernetFace::getInterfaceMtu() const |
| 303 | { |
| 304 | size_t mtu = ethernet::MAX_DATA_LEN; |
| 305 | |
| 306 | #ifdef SIOCGIFMTU |
Alexander Afanasyev | f4e89b4 | 2014-05-31 15:54:18 +0300 | [diff] [blame] | 307 | ifreq ifr = boost::initialized_value; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 308 | 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] | 309 | |
| 310 | if (::ioctl(m_socket->native_handle(), SIOCGIFMTU, &ifr) < 0) |
| 311 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 312 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 313 | << "] Failed to get interface MTU, assuming " << mtu); |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | mtu = std::min(mtu, static_cast<size_t>(ifr.ifr_mtu)); |
| 318 | } |
| 319 | #endif |
| 320 | |
| 321 | return mtu; |
| 322 | } |
| 323 | |
| 324 | } // namespace nfd |