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