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