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