]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/sd-radv.c
networkd-link.c: Add Router Advertisement starting and stopping
[thirdparty/systemd.git] / src / libsystemd-network / sd-radv.c
CommitLineData
04473969
PF
1/***
2 This file is part of systemd.
3
4 Copyright (C) 2017 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 <netinet/icmp6.h>
21#include <netinet/in.h>
204f99d2 22#include <arpa/inet.h>
04473969
PF
23
24#include "sd-radv.h"
25
204f99d2 26#include "macro.h"
04473969
PF
27#include "alloc-util.h"
28#include "fd-util.h"
29#include "icmp6-util.h"
30#include "in-addr-util.h"
31#include "radv-internal.h"
32#include "socket-util.h"
33#include "string-util.h"
34#include "util.h"
35
204f99d2
PF
36_public_ int sd_radv_new(sd_radv **ret) {
37 _cleanup_(sd_radv_unrefp) sd_radv *ra = NULL;
38
39 assert_return(ret, -EINVAL);
40
41 ra = new0(sd_radv, 1);
42 if (!ra)
43 return -ENOMEM;
44
45 ra->n_ref = 1;
46
47 LIST_HEAD_INIT(ra->prefixes);
48
49 *ret = ra;
50 ra = NULL;
51
52 return 0;
53}
54
55_public_ int sd_radv_attach_event(sd_radv *ra, sd_event *event, int64_t priority) {
56 int r;
57
58 assert_return(ra, -EINVAL);
59 assert_return(!ra->event, -EBUSY);
60
61 if (event)
62 ra->event = sd_event_ref(event);
63 else {
64 r = sd_event_default(&ra->event);
65 if (r < 0)
66 return 0;
67 }
68
69 ra->event_priority = priority;
70
71 return 0;
72}
73
74_public_ int sd_radv_detach_event(sd_radv *ra) {
75
76 assert_return(ra, -EINVAL);
77
78 ra->event = sd_event_unref(ra->event);
79 return 0;
80}
81
82_public_ sd_event *sd_radv_get_event(sd_radv *ra) {
83 assert_return(ra, NULL);
84
85 return ra->event;
86}
87
88_public_ sd_radv *sd_radv_ref(sd_radv *ra) {
89 if (!ra)
90 return NULL;
91
92 assert(ra->n_ref > 0);
93 ra->n_ref++;
94
95 return ra;
96}
97
98_public_ sd_radv *sd_radv_unref(sd_radv *ra) {
99 if (!ra)
100 return NULL;
101
102 assert(ra->n_ref > 0);
103 ra->n_ref--;
104
105 if (ra->n_ref > 0)
106 return NULL;
107
108 while (ra->prefixes) {
109 sd_radv_prefix *p = ra->prefixes;
110
111 LIST_REMOVE(prefix, ra->prefixes, p);
112 sd_radv_prefix_unref(p);
113 }
114
115 sd_radv_detach_event(ra);
116 return mfree(ra);
117}
118
119_public_ int sd_radv_stop(sd_radv *ra) {
120 assert_return(ra, -EINVAL);
121
122 log_radv("Stopping IPv6 Router Advertisement daemon");
123
124 ra->state = SD_RADV_STATE_IDLE;
125
126 return 0;
127}
128
129_public_ int sd_radv_start(sd_radv *ra) {
130 assert_return(ra, -EINVAL);
131 assert_return(ra->event, -EINVAL);
132 assert_return(ra->ifindex > 0, -EINVAL);
133
134 if (ra->state != SD_RADV_STATE_IDLE)
135 return 0;
136
137 ra->state = SD_RADV_STATE_ADVERTISING;
138
139 log_radv("Started IPv6 Router Advertisement daemon");
140
141 return 0;
142}
143
144_public_ int sd_radv_set_ifindex(sd_radv *ra, int ifindex) {
145 assert_return(ra, -EINVAL);
146 assert_return(ifindex >= -1, -EINVAL);
147
148 if (ra->state != SD_RADV_STATE_IDLE)
149 return -EBUSY;
150
151 ra->ifindex = ifindex;
152
153 return 0;
154}
155
156_public_ int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr) {
157 assert_return(ra, -EINVAL);
158
159 if (ra->state != SD_RADV_STATE_IDLE)
160 return -EBUSY;
161
162 if (mac_addr)
163 ra->mac_addr = *mac_addr;
164 else
165 zero(ra->mac_addr);
166
167 return 0;
168}
169
170_public_ int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu) {
171 assert_return(ra, -EINVAL);
172 assert_return(mtu >= 1280, -EINVAL);
173
174 if (ra->state != SD_RADV_STATE_IDLE)
175 return -EBUSY;
176
177 ra->mtu = mtu;
178
179 return 0;
180}
181
182_public_ int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit) {
183 assert_return(ra, -EINVAL);
184
185 if (ra->state != SD_RADV_STATE_IDLE)
186 return -EBUSY;
187
188 ra->hop_limit = hop_limit;
189
190 return 0;
191}
192
193_public_ int sd_radv_set_router_lifetime(sd_radv *ra, uint32_t router_lifetime) {
194 assert_return(ra, -EINVAL);
195
196 if (ra->state != SD_RADV_STATE_IDLE)
197 return -EBUSY;
198
199 /* RFC 4191, Section 2.2, "...If the Router Lifetime is zero, the
200 preference value MUST be set to (00) by the sender..." */
201 if (router_lifetime == 0 &&
202 (ra->flags & (0x3 << 3)) != (SD_NDISC_PREFERENCE_MEDIUM << 3))
203 return -ETIME;
204
205 ra->lifetime = router_lifetime;
206
207 return 0;
208}
209
210_public_ int sd_radv_set_managed_information(sd_radv *ra, int managed) {
211 assert_return(ra, -EINVAL);
212
213 if (ra->state != SD_RADV_STATE_IDLE)
214 return -EBUSY;
215
216 SET_FLAG(ra->flags, ND_RA_FLAG_MANAGED, managed);
217
218 return 0;
219}
220
221_public_ int sd_radv_set_other_information(sd_radv *ra, int other) {
222 assert_return(ra, -EINVAL);
223
224 if (ra->state != SD_RADV_STATE_IDLE)
225 return -EBUSY;
226
227 SET_FLAG(ra->flags, ND_RA_FLAG_OTHER, other);
228
229 return 0;
230}
231
232_public_ int sd_radv_set_preference(sd_radv *ra, unsigned preference) {
233 int r = 0;
234
235 assert_return(ra, -EINVAL);
236 assert_return(IN_SET(preference,
237 SD_NDISC_PREFERENCE_LOW,
238 SD_NDISC_PREFERENCE_MEDIUM,
239 SD_NDISC_PREFERENCE_HIGH), -EINVAL);
240
241 ra->flags = (ra->flags & ~(0x3 << 3)) | (preference << 3);
242
243 return r;
244}
245
246_public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
247 sd_radv_prefix *cur;
248 _cleanup_free_ char *addr_p = NULL;
249
250 assert_return(ra, -EINVAL);
251
252 if (!p)
253 return -EINVAL;
254
255 LIST_FOREACH(prefix, cur, ra->prefixes) {
256 int r;
257
258 r = in_addr_prefix_intersect(AF_INET6,
259 (union in_addr_union*) &cur->opt.in6_addr,
260 cur->opt.prefixlen,
261 (union in_addr_union*) &p->opt.in6_addr,
262 p->opt.prefixlen);
263 if (r > 0) {
264 _cleanup_free_ char *addr_cur = NULL;
265
266 (void) in_addr_to_string(AF_INET6,
267 (union in_addr_union*) &cur->opt.in6_addr,
268 &addr_cur);
269 (void) in_addr_to_string(AF_INET6,
270 (union in_addr_union*) &p->opt.in6_addr,
271 &addr_p);
272
273 log_radv("IPv6 prefix %s/%u already configured, ignoring %s/%u",
274 addr_cur, cur->opt.prefixlen,
275 addr_p, p->opt.prefixlen);
276
277 return -EEXIST;
278 }
279 }
280
281 p = sd_radv_prefix_ref(p);
282
283 LIST_APPEND(prefix, ra->prefixes, p);
284
285 ra->n_prefixes++;
286
287 (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &p->opt.in6_addr, &addr_p);
288 log_radv("Added prefix %s/%d", addr_p, p->opt.prefixlen);
289
290 return 0;
291}
292
04473969
PF
293_public_ int sd_radv_prefix_new(sd_radv_prefix **ret) {
294 _cleanup_(sd_radv_prefix_unrefp) sd_radv_prefix *p = NULL;
295
296 assert_return(ret, -EINVAL);
297
298 p = new0(sd_radv_prefix, 1);
299 if (!p)
300 return -ENOMEM;
301
302 p->n_ref = 1;
303
304 p->opt.type = ND_OPT_PREFIX_INFORMATION;
305 p->opt.length = (sizeof(p->opt) - 1) /8 + 1;
306
307 p->opt.prefixlen = 64;
308
309 /* RFC 4861, Section 6.2.1 */
310 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_ONLINK, true);
311 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_AUTO, true);
312 p->opt.preferred_lifetime = htobe32(604800);
313 p->opt.valid_lifetime = htobe32(2592000);
314
204f99d2
PF
315 LIST_INIT(prefix, p);
316
04473969
PF
317 *ret = p;
318 p = NULL;
319
320 return 0;
321}
322
323_public_ sd_radv_prefix *sd_radv_prefix_ref(sd_radv_prefix *p) {
324 if (!p)
325 return NULL;
326
327 assert(p->n_ref > 0);
328 p->n_ref++;
329
330 return p;
331}
332
333_public_ sd_radv_prefix *sd_radv_prefix_unref(sd_radv_prefix *p) {
334 if (!p)
335 return NULL;
336
337 assert(p->n_ref > 0);
338 p->n_ref--;
339
340 if (p->n_ref > 0)
341 return NULL;
342
343 return mfree(p);
344}
345
346_public_ int sd_radv_prefix_set_prefix(sd_radv_prefix *p, struct in6_addr *in6_addr,
347 unsigned char prefixlen) {
348 assert_return(p, -EINVAL);
349 assert_return(in6_addr, -EINVAL);
350
351 if (prefixlen < 3 || prefixlen > 128)
352 return -EINVAL;
353
354 if (prefixlen > 64)
355 /* unusual but allowed, log it */
356 log_radv("Unusual prefix length %d greater than 64", prefixlen);
357
358 p->opt.in6_addr = *in6_addr;
359 p->opt.prefixlen = prefixlen;
360
361 return 0;
362}
363
364_public_ int sd_radv_prefix_set_onlink(sd_radv_prefix *p, int onlink) {
365 assert_return(p, -EINVAL);
366
367 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_ONLINK, onlink);
368
369 return 0;
370}
371
372_public_ int sd_radv_prefix_set_address_autoconfiguration(sd_radv_prefix *p,
373 int address_autoconfiguration) {
374 assert_return(p, -EINVAL);
375
376 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_AUTO, address_autoconfiguration);
377
378 return 0;
379}
380
381_public_ int sd_radv_prefix_set_valid_lifetime(sd_radv_prefix *p,
382 uint32_t valid_lifetime) {
383 assert_return(p, -EINVAL);
384
385 p->opt.valid_lifetime = htobe32(valid_lifetime);
386
387 return 0;
388}
389
390_public_ int sd_radv_prefix_set_preferred_lifetime(sd_radv_prefix *p,
391 uint32_t preferred_lifetime) {
392 assert_return(p, -EINVAL);
393
394 p->opt.preferred_lifetime = htobe32(preferred_lifetime);
395
396 return 0;
397}