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 | |
Davide Pesavento | e463100 | 2014-03-21 20:55:36 +0100 | [diff] [blame] | 155 | // pad with zeroes if the payload is too short |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 156 | if (block.size() < ethernet::MIN_DATA_LEN) |
| 157 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 158 | static const uint8_t padding[ethernet::MIN_DATA_LEN] = {0}; |
| 159 | buffer.appendByteArray(padding, ethernet::MIN_DATA_LEN - block.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // construct and prepend the ethernet header |
| 163 | static uint16_t ethertype = htons(ETHERTYPE_NDN); |
| 164 | buffer.prependByteArray(reinterpret_cast<const uint8_t*>(ðertype), ethernet::TYPE_LEN); |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 165 | buffer.prependByteArray(m_srcAddress.data(), m_srcAddress.size()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 166 | buffer.prependByteArray(m_destAddress.data(), m_destAddress.size()); |
| 167 | |
| 168 | // send the packet |
| 169 | int sent = pcap_inject(m_pcap, buffer.buf(), buffer.size()); |
| 170 | if (sent < 0) |
| 171 | { |
| 172 | throw Error("pcap_inject(): " + std::string(pcap_geterr(m_pcap))); |
| 173 | } |
| 174 | else if (static_cast<size_t>(sent) < buffer.size()) |
| 175 | { |
| 176 | throw Error("Failed to send packet"); |
| 177 | } |
| 178 | |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 179 | NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 180 | << "] Successfully sent: " << buffer.size() << " bytes"); |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | EthernetFace::handleRead(const boost::system::error_code& error, size_t) |
| 185 | { |
| 186 | if (error) |
| 187 | return processErrorCode(error); |
| 188 | |
| 189 | pcap_pkthdr* pktHeader; |
| 190 | const uint8_t* packet; |
| 191 | int ret = pcap_next_ex(m_pcap, &pktHeader, &packet); |
| 192 | if (ret < 0) |
| 193 | { |
| 194 | throw Error("pcap_next_ex(): " + std::string(pcap_geterr(m_pcap))); |
| 195 | } |
| 196 | else if (ret == 0) |
| 197 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 198 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 199 | << "] pcap_next_ex() timed out"); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | size_t length = pktHeader->caplen; |
| 204 | if (length < ethernet::HDR_LEN + ethernet::MIN_DATA_LEN) |
| 205 | throw Error("Received packet is too short"); |
| 206 | |
| 207 | const ether_header* eh = reinterpret_cast<const ether_header*>(packet); |
| 208 | if (ntohs(eh->ether_type) != ETHERTYPE_NDN) |
| 209 | throw Error("Unrecognized ethertype"); |
| 210 | |
| 211 | packet += ethernet::HDR_LEN; |
| 212 | length -= ethernet::HDR_LEN; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 213 | NFD_LOG_TRACE("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 214 | << "] Received: " << length << " bytes"); |
| 215 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 216 | /// \todo Reserve space in front and at the back |
| 217 | /// of the underlying buffer |
| 218 | Block element; |
| 219 | bool isOk = Block::fromBuffer(packet, length, element); |
| 220 | if (isOk) |
| 221 | { |
| 222 | if (!decodeAndDispatchInput(element)) |
| 223 | { |
| 224 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 225 | << "] Received unrecognized block of type " << element.type()); |
| 226 | // ignore unknown packet |
| 227 | } |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
| 232 | << "] Received block is invalid or too large to process"); |
| 233 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | m_socket->async_read_some(boost::asio::null_buffers(), |
| 237 | bind(&EthernetFace::handleRead, this, |
| 238 | boost::asio::placeholders::error, |
| 239 | boost::asio::placeholders::bytes_transferred)); |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | EthernetFace::processErrorCode(const boost::system::error_code& error) |
| 244 | { |
| 245 | if (error == boost::system::errc::operation_canceled) |
| 246 | // when socket is closed by someone |
| 247 | return; |
| 248 | |
| 249 | if (!m_pcap) |
| 250 | { |
| 251 | onFail("Face closed"); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | std::string msg; |
| 256 | if (error == boost::asio::error::eof) |
| 257 | { |
| 258 | msg = "Face closed"; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 259 | NFD_LOG_INFO("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 260 | } |
| 261 | else |
| 262 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 263 | msg = "Receive operation failed, closing face: " + error.category().message(error.value()); |
| 264 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName << "] " << msg); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | close(); |
| 268 | onFail(msg); |
| 269 | } |
| 270 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 271 | size_t |
| 272 | EthernetFace::getInterfaceMtu() const |
| 273 | { |
| 274 | size_t mtu = ethernet::MAX_DATA_LEN; |
| 275 | |
| 276 | #ifdef SIOCGIFMTU |
Davide Pesavento | e463100 | 2014-03-21 20:55:36 +0100 | [diff] [blame] | 277 | ifreq ifr = {}; |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 278 | 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] | 279 | |
| 280 | if (::ioctl(m_socket->native_handle(), SIOCGIFMTU, &ifr) < 0) |
| 281 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame] | 282 | NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 283 | << "] Failed to get interface MTU, assuming " << mtu); |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | mtu = std::min(mtu, static_cast<size_t>(ifr.ifr_mtu)); |
| 288 | } |
| 289 | #endif |
| 290 | |
| 291 | return mtu; |
| 292 | } |
| 293 | |
| 294 | } // namespace nfd |