]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/icmp6-util.c
sd-*.h: clean up exported (or to-be-exported) header files
[thirdparty/systemd.git] / src / libsystemd-network / icmp6-util.c
CommitLineData
940367a0
TG
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 <sys/types.h>
22#include <sys/socket.h>
23#include <string.h>
24#include <linux/if_packet.h>
25#include <stdio.h>
26#include <unistd.h>
27#include <netinet/ip6.h>
28#include <netinet/icmp6.h>
29#include <netinet/in.h>
30
31#include "socket-util.h"
32
33#include "icmp6-util.h"
34
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 } } }
38
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 } } }
42
43int icmp6_bind_router_solicitation(int index) {
44 struct icmp6_filter filter = { };
45 struct ipv6_mreq mreq = {
46 .ipv6mr_multiaddr = IN6ADDR_ALL_NODES_MULTICAST_INIT,
47 .ipv6mr_interface = index,
48 };
49 _cleanup_close_ int s = -1;
50 int r, zero = 0, hops = 255;
51
52 s = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
53 IPPROTO_ICMPV6);
54 if (s < 0)
55 return -errno;
56
57 ICMP6_FILTER_SETBLOCKALL(&filter);
58 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
59 r = setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filter,
60 sizeof(filter));
61 if (r < 0)
62 return -errno;
63
64 /* RFC 3315, section 6.7, bullet point 2 may indicate that an
65 IPV6_PKTINFO socket option also applies for ICMPv6 multicast.
66 Empirical experiments indicates otherwise and therefore an
67 IPV6_MULTICAST_IF socket option is used here instead */
68 r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index,
69 sizeof(index));
70 if (r < 0)
71 return -errno;
72
73 r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &zero,
74 sizeof(zero));
75 if (r < 0)
76 return -errno;
77
78 r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops,
79 sizeof(hops));
80 if (r < 0)
81 return -errno;
82
83 r = setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq,
84 sizeof(mreq));
85 if (r < 0)
86 return -errno;
87
88 r = s;
89 s = -1;
90 return r;
91}
92
93int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) {
94 struct sockaddr_in6 dst = {
95 .sin6_family = AF_INET6,
96 .sin6_addr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
97 };
98 struct {
99 struct nd_router_solicit rs;
100 struct nd_opt_hdr rs_opt;
101 struct ether_addr rs_opt_mac;
102 } _packed_ rs = {
103 .rs.nd_rs_type = ND_ROUTER_SOLICIT,
104 };
105 struct iovec iov[1] = {
106 { &rs, },
107 };
108 struct msghdr msg = {
109 .msg_name = &dst,
110 .msg_namelen = sizeof(dst),
111 .msg_iov = iov,
112 .msg_iovlen = 1,
113 };
114 int r;
115
116 if (ether_addr) {
117 memcpy(&rs.rs_opt_mac, ether_addr, ETH_ALEN);
118 rs.rs_opt.nd_opt_type = ND_OPT_SOURCE_LINKADDR;
119 rs.rs_opt.nd_opt_len = 1;
120 iov[0].iov_len = sizeof(rs);
121 } else
122 iov[0].iov_len = sizeof(rs.rs);
123
124 r = sendmsg(s, &msg, 0);
125 if (r < 0)
126 return -errno;
127
128 return 0;
129}