2 This file is part of systemd.
4 Copyright (C) 2014 Intel Corporation. All rights reserved.
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <linux/if_packet.h>
27 #include <netinet/ip6.h>
28 #include <netinet/icmp6.h>
29 #include <netinet/in.h>
31 #include "socket-util.h"
33 #include "dhcp6-internal.h"
35 #define IN6ADDR_ALL_ROUTERS_MULTICAST_INIT \
36 { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } } }
39 #define IN6ADDR_ALL_NODES_MULTICAST_INIT \
40 { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } } }
43 int dhcp_network_icmp6_bind_router_solicitation(int index)
45 struct icmp6_filter filter = { };
46 struct ipv6_mreq mreq = {
47 .ipv6mr_multiaddr = IN6ADDR_ALL_NODES_MULTICAST_INIT,
48 .ipv6mr_interface = index,
50 _cleanup_close_ int s = -1;
51 int r, zero = 0, hops = 255;
53 s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
58 ICMP6_FILTER_SETBLOCKALL(&filter);
59 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
60 r = setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filter,
65 /* RFC 3315, section 6.7, bullet point 2 may indicate that an
66 IPV6_PKTINFO socket option also applies for ICMPv6 multicast.
67 Empirical experiments indicates otherwise and therefore an
68 IPV6_MULTICAST_IF socket option is used here instead */
69 r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index,
74 r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &zero,
79 r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops,
84 r = setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq,
94 int dhcp_network_icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr)
96 struct sockaddr_in6 dst = {
97 .sin6_family = AF_INET6,
98 .sin6_addr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
101 struct nd_router_solicit rs;
102 struct nd_opt_hdr rs_opt;
103 struct ether_addr rs_opt_mac;
105 .rs.nd_rs_type = ND_ROUTER_SOLICIT,
107 struct iovec iov[1] = {
110 struct msghdr msg = {
112 .msg_namelen = sizeof(dst),
119 memcpy(&rs.rs_opt_mac, ether_addr, ETH_ALEN);
120 rs.rs_opt.nd_opt_type = ND_OPT_SOURCE_LINKADDR;
121 rs.rs_opt.nd_opt_len = 1;
122 iov[0].iov_len = sizeof(rs);
124 iov[0].iov_len = sizeof(rs.rs);
126 r = sendmsg(s, &msg, 0);