]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/icmp6-util.c
Merge pull request #3431 from poettering/network-fixes
[thirdparty/systemd.git] / src / libsystemd-network / icmp6-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2014 Intel Corporation. All rights reserved.
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <errno.h>
21 #include <netinet/icmp6.h>
22 #include <netinet/in.h>
23 #include <netinet/ip6.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <net/if.h>
30 #include <linux/if_packet.h>
31
32 #include "fd-util.h"
33 #include "icmp6-util.h"
34 #include "socket-util.h"
35
36 #define IN6ADDR_ALL_ROUTERS_MULTICAST_INIT \
37 { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } } }
39
40 #define IN6ADDR_ALL_NODES_MULTICAST_INIT \
41 { { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } } }
43
44 int 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,
49 };
50 _cleanup_close_ int s = -1;
51 char ifname[IF_NAMESIZE] = "";
52 static const int zero = 0, one = 1, hops = 255;
53 int r;
54
55 s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_ICMPV6);
56 if (s < 0)
57 return -errno;
58
59 ICMP6_FILTER_SETBLOCKALL(&filter);
60 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
61 r = setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter));
62 if (r < 0)
63 return -errno;
64
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, sizeof(index));
70 if (r < 0)
71 return -errno;
72
73 r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &zero, sizeof(zero));
74 if (r < 0)
75 return -errno;
76
77 r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, sizeof(hops));
78 if (r < 0)
79 return -errno;
80
81 r = setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
82 if (r < 0)
83 return -errno;
84
85 r = setsockopt(s, SOL_IPV6, IPV6_RECVHOPLIMIT, &one, sizeof(one));
86 if (r < 0)
87 return -errno;
88
89 r = setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
90 if (r < 0)
91 return -errno;
92
93 if (if_indextoname(index, ifname) == 0)
94 return -errno;
95
96 r = setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
97 if (r < 0)
98 return -errno;
99
100 r = s;
101 s = -1;
102 return r;
103 }
104
105 int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) {
106 struct sockaddr_in6 dst = {
107 .sin6_family = AF_INET6,
108 .sin6_addr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
109 };
110 struct {
111 struct nd_router_solicit rs;
112 struct nd_opt_hdr rs_opt;
113 struct ether_addr rs_opt_mac;
114 } _packed_ rs = {
115 .rs.nd_rs_type = ND_ROUTER_SOLICIT,
116 .rs_opt.nd_opt_type = ND_OPT_SOURCE_LINKADDR,
117 .rs_opt.nd_opt_len = 1,
118 };
119 struct iovec iov = {
120 .iov_base = &rs,
121 .iov_len = sizeof(rs),
122 };
123 struct msghdr msg = {
124 .msg_name = &dst,
125 .msg_namelen = sizeof(dst),
126 .msg_iov = &iov,
127 .msg_iovlen = 1,
128 };
129 int r;
130
131 assert(s >= 0);
132 assert(ether_addr);
133
134 rs.rs_opt_mac = *ether_addr;
135
136 r = sendmsg(s, &msg, 0);
137 if (r < 0)
138 return -errno;
139
140 return 0;
141 }