blob: 9f1127c84c52099232e19ec9e2f6a56ad5c82f37 [file] [log] [blame]
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, Regents of the University of California,
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"
27#include "core/global-io.hpp"
28
29#include <pcap/pcap.h>
30
31#include <cerrno> // for errno
32#include <cstring> // for memcpy(), strerror(), strncpy()
33#include <net/if.h> // for struct ifreq
34#include <stdio.h> // for snprintf()
35#include <sys/ioctl.h> // for ioctl()
36
37#if defined(__linux__)
38#include <netpacket/packet.h> // for struct packet_mreq
39#include <sys/socket.h> // for setsockopt()
40#endif
41
42#ifdef SIOCADDMULTI
43#if defined(__APPLE__) || defined(__FreeBSD__)
44#include <net/if_dl.h> // for struct sockaddr_dl
45#endif
46#endif
47
48namespace nfd {
49namespace face {
50
51NFD_LOG_INIT("MulticastEthernetTransport");
52
53MulticastEthernetTransport::MulticastEthernetTransport(const NetworkInterfaceInfo& localEndpoint,
54 const ethernet::Address& mcastAddress,
55 ndn::nfd::LinkType linkType)
56 : EthernetTransport(localEndpoint, mcastAddress)
Davide Pesaventofe0580c2017-05-12 02:02:10 -040057#if defined(__linux__)
58 , m_interfaceIndex(localEndpoint.index)
59#endif
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040060{
61 this->setLocalUri(FaceUri::fromDev(m_interfaceName));
62 this->setRemoteUri(FaceUri(m_destAddress));
63 this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
64 this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
65 this->setLinkType(linkType);
66
67 NFD_LOG_FACE_INFO("Creating transport");
68
69 char filter[110];
70 // note #1: we cannot use std::snprintf because it's not available
71 // on some platforms (see #2299)
72 // note #2: "not vlan" must appear last in the filter expression, or the
73 // rest of the filter won't work as intended (see pcap-filter(7))
74 snprintf(filter, sizeof(filter),
75 "(ether proto 0x%x) && (ether dst %s) && (not ether src %s) && (not vlan)",
76 ethernet::ETHERTYPE_NDN,
Davide Pesaventofe0580c2017-05-12 02:02:10 -040077 m_destAddress.toString().data(),
78 m_srcAddress.toString().data());
79 m_pcap.setPacketFilter(filter);
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040080
81 if (!m_destAddress.isBroadcast() && !joinMulticastGroup()) {
82 NFD_LOG_FACE_WARN("Falling back to promiscuous mode");
Davide Pesaventofe0580c2017-05-12 02:02:10 -040083 pcap_set_promisc(m_pcap, 1);
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040084 }
85}
86
87bool
88MulticastEthernetTransport::joinMulticastGroup()
89{
90#if defined(__linux__)
91 packet_mreq mr{};
92 mr.mr_ifindex = m_interfaceIndex;
93 mr.mr_type = PACKET_MR_MULTICAST;
94 mr.mr_alen = m_destAddress.size();
95 std::memcpy(mr.mr_address, m_destAddress.data(), m_destAddress.size());
96
97 if (::setsockopt(m_socket.native_handle(), SOL_PACKET,
98 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == 0)
99 return true; // success
100
101 NFD_LOG_FACE_WARN("setsockopt(PACKET_ADD_MEMBERSHIP) failed: " << std::strerror(errno));
102#endif
103
104#if defined(SIOCADDMULTI)
105 ifreq ifr{};
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400106 std::strncpy(ifr.ifr_name, m_interfaceName.data(), sizeof(ifr.ifr_name) - 1);
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -0400107
108#if defined(__APPLE__) || defined(__FreeBSD__)
109 // see bug #2327
110 using boost::asio::ip::udp;
111 udp::socket sock(getGlobalIoService(), udp::v4());
112 int fd = sock.native_handle();
113
114 // Differences between Linux and the BSDs (including macOS):
115 // o BSD does not have ifr_hwaddr; use ifr_addr instead.
116 // o While macOS seems to accept both AF_LINK and AF_UNSPEC as the address
117 // family, FreeBSD explicitly requires AF_LINK, so we have to use AF_LINK
118 // and sockaddr_dl instead of the generic sockaddr structure.
119 // o BSD's sockaddr (and sockaddr_dl in particular) contains an additional
120 // field, sa_len (sdl_len), which must be set to the total length of the
121 // structure, including the length field itself.
122 // o We do not specify the interface name, thus sdl_nlen is left at 0 and
123 // LLADDR is effectively the same as sdl_data.
124
125 sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(&ifr.ifr_addr);
126 sdl->sdl_len = sizeof(ifr.ifr_addr);
127 sdl->sdl_family = AF_LINK;
128 sdl->sdl_alen = m_destAddress.size();
129 std::memcpy(LLADDR(sdl), m_destAddress.data(), m_destAddress.size());
130
131 static_assert(sizeof(ifr.ifr_addr) >= offsetof(sockaddr_dl, sdl_data) + ethernet::ADDR_LEN,
132 "ifr_addr in struct ifreq is too small on this platform");
133#else
134 int fd = m_socket.native_handle();
135
136 ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
137 std::memcpy(ifr.ifr_hwaddr.sa_data, m_destAddress.data(), m_destAddress.size());
138
139 static_assert(sizeof(ifr.ifr_hwaddr.sa_data) >= ethernet::ADDR_LEN,
140 "ifr_hwaddr in struct ifreq is too small on this platform");
141#endif
142
143 if (::ioctl(fd, SIOCADDMULTI, &ifr) == 0)
144 return true; // success
145
146 NFD_LOG_FACE_WARN("ioctl(SIOCADDMULTI) failed: " << std::strerror(errno));
147#endif
148
149 return false;
150}
151
152} // namespace face
153} // namespace nfd