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