Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | c52cd5e | 2022-03-05 20:40:54 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "multicast-ethernet-transport.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 28 | |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 29 | #include <cerrno> // for errno |
Davide Pesavento | c52cd5e | 2022-03-05 20:40:54 -0500 | [diff] [blame] | 30 | #include <cstdio> // for snprintf() |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 31 | #include <cstring> // for memcpy(), strerror(), strncpy() |
| 32 | #include <net/if.h> // for struct ifreq |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 33 | #include <sys/ioctl.h> // for ioctl() |
| 34 | |
| 35 | #if defined(__linux__) |
| 36 | #include <netpacket/packet.h> // for struct packet_mreq |
| 37 | #include <sys/socket.h> // for setsockopt() |
| 38 | #endif |
| 39 | |
| 40 | #ifdef SIOCADDMULTI |
| 41 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 42 | #include <net/if_dl.h> // for struct sockaddr_dl |
| 43 | #endif |
| 44 | #endif |
| 45 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 46 | namespace nfd::face { |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 47 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 48 | NFD_LOG_INIT(MulticastEthernetTransport); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 49 | |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 50 | MulticastEthernetTransport::MulticastEthernetTransport(const ndn::net::NetworkInterface& localEndpoint, |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 51 | const ethernet::Address& mcastAddress, |
| 52 | ndn::nfd::LinkType linkType) |
| 53 | : EthernetTransport(localEndpoint, mcastAddress) |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 54 | #if defined(__linux__) |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 55 | , m_interfaceIndex(localEndpoint.getIndex()) |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 56 | #endif |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 57 | { |
| 58 | this->setLocalUri(FaceUri::fromDev(m_interfaceName)); |
| 59 | this->setRemoteUri(FaceUri(m_destAddress)); |
| 60 | this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 61 | this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 62 | this->setLinkType(linkType); |
Davide Pesavento | b8eebcb | 2017-08-08 15:03:17 -0400 | [diff] [blame] | 63 | this->setMtu(localEndpoint.getMtu()); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 64 | |
Davide Pesavento | a681a24 | 2019-03-29 23:48:27 -0400 | [diff] [blame] | 65 | NFD_LOG_FACE_DEBUG("Creating transport"); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 66 | |
| 67 | char filter[110]; |
Davide Pesavento | c52cd5e | 2022-03-05 20:40:54 -0500 | [diff] [blame] | 68 | // Note: "not vlan" must appear last in the filter expression, or the |
| 69 | // rest of the filter won't work as intended (see pcap-filter(7)) |
| 70 | std::snprintf(filter, sizeof(filter), |
| 71 | "(ether proto 0x%x) && (ether dst %s) && (not ether src %s) && (not vlan)", |
| 72 | ethernet::ETHERTYPE_NDN, |
| 73 | m_destAddress.toString().data(), |
| 74 | m_srcAddress.toString().data()); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 75 | m_pcap.setPacketFilter(filter); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 76 | |
Davide Pesavento | a94f367 | 2017-05-18 01:18:39 -0400 | [diff] [blame] | 77 | BOOST_ASSERT(m_destAddress.isMulticast()); |
Davide Pesavento | c52cd5e | 2022-03-05 20:40:54 -0500 | [diff] [blame] | 78 | if (!m_destAddress.isBroadcast()) { |
Davide Pesavento | a94f367 | 2017-05-18 01:18:39 -0400 | [diff] [blame] | 79 | joinMulticastGroup(); |
Davide Pesavento | c52cd5e | 2022-03-05 20:40:54 -0500 | [diff] [blame] | 80 | } |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Davide Pesavento | a94f367 | 2017-05-18 01:18:39 -0400 | [diff] [blame] | 83 | void |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 84 | MulticastEthernetTransport::joinMulticastGroup() |
| 85 | { |
| 86 | #if defined(__linux__) |
| 87 | packet_mreq mr{}; |
| 88 | mr.mr_ifindex = m_interfaceIndex; |
| 89 | mr.mr_type = PACKET_MR_MULTICAST; |
| 90 | mr.mr_alen = m_destAddress.size(); |
| 91 | std::memcpy(mr.mr_address, m_destAddress.data(), m_destAddress.size()); |
| 92 | |
| 93 | if (::setsockopt(m_socket.native_handle(), SOL_PACKET, |
| 94 | PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0) |
Davide Pesavento | a94f367 | 2017-05-18 01:18:39 -0400 | [diff] [blame] | 95 | return; // success |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 96 | |
| 97 | NFD_LOG_FACE_WARN("setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno)); |
| 98 | #endif |
| 99 | |
| 100 | #if defined(SIOCADDMULTI) |
| 101 | ifreq ifr{}; |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 102 | std::strncpy(ifr.ifr_name, m_interfaceName.data(), sizeof(ifr.ifr_name) - 1); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 103 | |
| 104 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 105 | // see bug #2327 |
| 106 | using boost::asio::ip::udp; |
| 107 | udp::socket sock(getGlobalIoService(), udp::v4()); |
| 108 | int fd = sock.native_handle(); |
| 109 | |
| 110 | // Differences between Linux and the BSDs (including macOS): |
| 111 | // o BSD does not have ifr_hwaddr; use ifr_addr instead. |
| 112 | // o While macOS seems to accept both AF_LINK and AF_UNSPEC as the address |
| 113 | // family, FreeBSD explicitly requires AF_LINK, so we have to use AF_LINK |
| 114 | // and sockaddr_dl instead of the generic sockaddr structure. |
| 115 | // o BSD's sockaddr (and sockaddr_dl in particular) contains an additional |
| 116 | // field, sa_len (sdl_len), which must be set to the total length of the |
| 117 | // structure, including the length field itself. |
| 118 | // o We do not specify the interface name, thus sdl_nlen is left at 0 and |
| 119 | // LLADDR is effectively the same as sdl_data. |
| 120 | |
| 121 | sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(&ifr.ifr_addr); |
| 122 | sdl->sdl_len = sizeof(ifr.ifr_addr); |
| 123 | sdl->sdl_family = AF_LINK; |
| 124 | sdl->sdl_alen = m_destAddress.size(); |
| 125 | std::memcpy(LLADDR(sdl), m_destAddress.data(), m_destAddress.size()); |
| 126 | |
| 127 | static_assert(sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) + ethernet::ADDR_LEN, |
| 128 | "ifr_addr in struct ifreq is too small on this platform"); |
| 129 | #else |
| 130 | int fd = m_socket.native_handle(); |
| 131 | |
| 132 | ifr.ifr_hwaddr.sa_family = AF_UNSPEC; |
| 133 | std::memcpy(ifr.ifr_hwaddr.sa_data, m_destAddress.data(), m_destAddress.size()); |
| 134 | |
| 135 | static_assert(sizeof(ifr.ifr_hwaddr.sa_data) >= ethernet::ADDR_LEN, |
| 136 | "ifr_hwaddr in struct ifreq is too small on this platform"); |
| 137 | #endif |
| 138 | |
| 139 | if (::ioctl(fd, SIOCADDMULTI, &ifr) == 0) |
Davide Pesavento | a94f367 | 2017-05-18 01:18:39 -0400 | [diff] [blame] | 140 | return; // success |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 141 | |
| 142 | NFD_LOG_FACE_WARN("ioctl(SIOCADDMULTI) failed: " << std::strerror(errno)); |
| 143 | #endif |
| 144 | |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 145 | NDN_THROW(Error("Failed to join multicast group")); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 148 | } // namespace nfd::face |