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