IPv6: Use source-routing to allow multi-wan
[project/netifd.git] / proto.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2012 Steven Barth <steven@midlink.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
21
22 #include "netifd.h"
23 #include "system.h"
24 #include "interface.h"
25 #include "interface-ip.h"
26 #include "proto.h"
27
28 static struct avl_tree handlers;
29
30 enum {
31 OPT_IPADDR,
32 OPT_IP6ADDR,
33 OPT_NETMASK,
34 OPT_BROADCAST,
35 OPT_GATEWAY,
36 OPT_IP6GW,
37 OPT_IP6PREFIX,
38 __OPT_MAX,
39 };
40
41 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
42 [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
43 [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
44 [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
45 [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
46 [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
47 [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
48 [OPT_IP6PREFIX] = { .name = "ip6prefix", .type = BLOBMSG_TYPE_ARRAY },
49 };
50
51 static const union config_param_info proto_ip_attr_info[__OPT_MAX] = {
52 [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
53 [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
54 [OPT_IP6PREFIX] = { .type = BLOBMSG_TYPE_STRING },
55 };
56
57 const struct config_param_list proto_ip_attr = {
58 .n_params = __OPT_MAX,
59 .params = proto_ip_attributes,
60 .info = proto_ip_attr_info,
61 };
62
63 enum {
64 ADDR_IPADDR,
65 ADDR_MASK,
66 ADDR_BROADCAST,
67 ADDR_PTP,
68 ADDR_PREFERRED,
69 ADDR_VALID,
70 ADDR_OFFLINK,
71 __ADDR_MAX
72 };
73
74 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
75 [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
76 [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
77 [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
78 [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
79 [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 },
80 [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
81 [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL },
82 };
83
84 static struct device_addr *
85 alloc_device_addr(bool v6, bool ext)
86 {
87 struct device_addr *addr;
88
89 addr = calloc(1, sizeof(*addr));
90 addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
91 if (ext)
92 addr->flags |= DEVADDR_EXTERNAL;
93
94 return addr;
95 }
96
97 static bool
98 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
99 bool ext, uint32_t broadcast)
100 {
101 struct device_addr *addr;
102 int af = v6 ? AF_INET6 : AF_INET;
103
104 addr = alloc_device_addr(v6, ext);
105 if (!addr)
106 return false;
107
108 addr->mask = mask;
109 if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
110 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
111 free(addr);
112 return false;
113 }
114
115 if (broadcast)
116 addr->broadcast = broadcast;
117
118 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
119 return true;
120 }
121
122 static int
123 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
124 bool v6, int netmask, bool ext, uint32_t broadcast)
125 {
126 struct blob_attr *cur;
127 int n_addr = 0;
128 int rem;
129
130 blobmsg_for_each_attr(cur, attr, rem) {
131 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
132 return -1;
133
134 n_addr++;
135 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
136 broadcast))
137 return -1;
138 }
139
140 return n_addr;
141 }
142
143 static struct device_addr *
144 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
145 {
146 struct device_addr *addr;
147 struct blob_attr *tb[__ADDR_MAX];
148 struct blob_attr *cur;
149
150 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
151 return NULL;
152
153 addr = alloc_device_addr(v6, ext);
154 if (!addr)
155 return NULL;
156
157 blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
158
159 addr->mask = v6 ? 128 : 32;
160 if ((cur = tb[ADDR_MASK])) {
161 unsigned int new_mask;
162
163 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
164 if (new_mask > addr->mask)
165 goto error;
166
167 addr->mask = new_mask;
168 }
169
170 cur = tb[ADDR_IPADDR];
171 if (!cur)
172 goto error;
173
174 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
175 goto error;
176
177 if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
178 addr->flags |= DEVADDR_OFFLINK;
179
180 if (!v6) {
181 if ((cur = tb[ADDR_BROADCAST]) &&
182 !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
183 goto error;
184 if ((cur = tb[ADDR_PTP]) &&
185 !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
186 goto error;
187 } else {
188 time_t now = system_get_rtime();
189 if ((cur = tb[ADDR_PREFERRED])) {
190 uint32_t preferred = blobmsg_get_u32(cur);
191 if (preferred < UINT32_MAX)
192 addr->preferred_until = now + preferred;
193 }
194
195 if ((cur = tb[ADDR_VALID])) {
196 uint32_t valid = blobmsg_get_u32(cur);
197 if (valid < UINT32_MAX)
198 addr->valid_until = now + valid;
199
200 }
201
202 if (addr->valid_until) {
203 if (!addr->preferred_until)
204 addr->preferred_until = addr->valid_until;
205 else if (addr->preferred_until > addr->valid_until)
206 goto error;
207 }
208 }
209
210 return addr;
211
212 error:
213 free(addr);
214 return NULL;
215 }
216
217 static int
218 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
219 bool ext)
220 {
221 struct device_addr *addr;
222 struct blob_attr *cur;
223 int n_addr = 0;
224 int rem;
225
226 blobmsg_for_each_attr(cur, attr, rem) {
227 addr = parse_address_item(cur, v6, ext);
228 if (!addr)
229 return -1;
230
231 n_addr++;
232 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
233 }
234
235 return n_addr;
236 }
237
238 static bool
239 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
240 {
241 struct device_route *route;
242 const char *str = blobmsg_data(attr);
243 int af = v6 ? AF_INET6 : AF_INET;
244
245 route = calloc(1, sizeof(*route));
246 if (!inet_pton(af, str, &route->nexthop)) {
247 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
248 free(route);
249 return false;
250 }
251
252 route->mask = 0;
253 route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
254
255 if (v6) {
256 route->table = interface_ip_resolve_v6_rtable(iface->l3_dev.dev->ifindex);
257 route->flags |= DEVROUTE_SRCTABLE;
258 }
259
260 vlist_add(&iface->proto_ip.route, &route->node, route);
261
262 return true;
263 }
264
265 static bool
266 parse_prefix_option(struct interface *iface, const char *str, size_t len)
267 {
268 char buf[128] = {0}, *saveptr;
269 if (len > sizeof(buf))
270 return false;
271
272 memcpy(buf, str, len);
273 char *addrstr = strtok_r(buf, "/", &saveptr);
274 if (!addrstr)
275 return false;
276
277 char *lengthstr = strtok_r(NULL, ",", &saveptr);
278 if (!lengthstr)
279 return false;
280
281 char *prefstr = strtok_r(NULL, ",", &saveptr);
282 char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
283 char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
284
285 uint32_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
286 uint32_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
287
288 uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
289 if (length < 1 || length > 64)
290 return false;
291
292 struct in6_addr addr, excluded, *excludedp = NULL;
293 if (inet_pton(AF_INET6, addrstr, &addr) < 1)
294 return false;
295
296 for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) {
297 char *key = NULL, *val = NULL, *addsaveptr;
298 if (!(key = strtok_r(addstr, "=", &addsaveptr)) ||
299 !(val = strtok_r(NULL, ",", &addsaveptr)))
300 continue;
301
302 if (!strcmp(key, "excluded")) {
303 char *sep = strchr(val, '/');
304 if (!sep)
305 return false;
306
307 *sep = 0;
308 excl_length = atoi(sep + 1);
309
310 if (inet_pton(AF_INET6, val, &excluded) < 1)
311 return false;
312
313 excludedp = &excluded;
314 }
315
316 }
317
318
319
320
321 time_t now = system_get_rtime();
322 time_t preferred_until = 0;
323 if (prefstr && pref != 0xffffffffU)
324 preferred_until = pref + now;
325
326 time_t valid_until = 0;
327 if (validstr && valid != 0xffffffffU)
328 valid_until = valid + now;
329
330 interface_ip_add_device_prefix(iface, &addr, length,
331 valid_until, preferred_until,
332 excludedp, excl_length);
333 return true;
334 }
335
336 static int
337 parse_prefix_list(struct interface *iface, struct blob_attr *attr)
338 {
339 struct blob_attr *cur;
340 int n_addr = 0;
341 int rem;
342
343 blobmsg_for_each_attr(cur, attr, rem) {
344 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
345 return -1;
346
347 n_addr++;
348 if (!parse_prefix_option(iface, blobmsg_data(cur),
349 blobmsg_data_len(cur)))
350 return -1;
351 }
352
353 return n_addr;
354 }
355
356 int
357 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
358 {
359 struct blob_attr *tb[__OPT_MAX];
360 struct blob_attr *cur;
361 const char *error;
362 unsigned int netmask = 32;
363 int n_v4 = 0, n_v6 = 0;
364 struct in_addr bcast = {};
365
366 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
367
368 if ((cur = tb[OPT_NETMASK])) {
369 netmask = parse_netmask_string(blobmsg_data(cur), false);
370 if (netmask > 32) {
371 error = "INVALID_NETMASK";
372 goto error;
373 }
374 }
375
376 if ((cur = tb[OPT_BROADCAST])) {
377 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
378 error = "INVALID_BROADCAST";
379 goto error;
380 }
381 }
382
383 if ((cur = tb[OPT_IPADDR]))
384 n_v4 = parse_static_address_option(iface, cur, false,
385 netmask, false, bcast.s_addr);
386
387 if ((cur = tb[OPT_IP6ADDR]))
388 n_v6 = parse_static_address_option(iface, cur, true,
389 128, false, 0);
390
391 if ((cur = tb[OPT_IP6PREFIX]))
392 if (parse_prefix_list(iface, cur) < 0)
393 goto out;
394
395 if (n_v4 < 0 || n_v6 < 0)
396 goto out;
397
398 if ((cur = tb[OPT_GATEWAY])) {
399 if (n_v4 && !parse_gateway_option(iface, cur, false))
400 goto out;
401 }
402
403 if ((cur = tb[OPT_IP6GW])) {
404 if (n_v6 && !parse_gateway_option(iface, cur, true))
405 goto out;
406 }
407
408 return 0;
409
410 error:
411 interface_add_error(iface, "proto", error, NULL, 0);
412 out:
413 return -1;
414 }
415
416 int
417 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
418 {
419 struct blob_attr *tb[__OPT_MAX];
420 struct blob_attr *cur;
421 int n_v4 = 0, n_v6 = 0;
422
423 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
424
425 if ((cur = tb[OPT_IPADDR]))
426 n_v4 = parse_address_list(iface, cur, false, ext);
427
428 if ((cur = tb[OPT_IP6ADDR]))
429 n_v6 = parse_address_list(iface, cur, true, ext);
430
431 if ((cur = tb[OPT_IP6PREFIX]))
432 if (parse_prefix_list(iface, cur) < 0)
433 goto out;
434
435 if (n_v4 < 0 || n_v6 < 0)
436 goto out;
437
438 if ((cur = tb[OPT_GATEWAY])) {
439 if (n_v4 && !parse_gateway_option(iface, cur, false))
440 goto out;
441 }
442
443 if ((cur = tb[OPT_IP6GW])) {
444 if (n_v6 && !parse_gateway_option(iface, cur, true))
445 goto out;
446 }
447
448 return 0;
449
450 out:
451 return -1;
452 }
453
454 void add_proto_handler(struct proto_handler *p)
455 {
456 if (!handlers.comp)
457 avl_init(&handlers, avl_strcmp, false, NULL);
458
459 if (p->avl.key)
460 return;
461
462 p->avl.key = p->name;
463 avl_insert(&handlers, &p->avl);
464 }
465
466 static void
467 default_proto_free(struct interface_proto_state *proto)
468 {
469 free(proto);
470 }
471
472 static int
473 invalid_proto_handler(struct interface_proto_state *proto,
474 enum interface_proto_cmd cmd, bool force)
475 {
476 return -1;
477 }
478
479 static int
480 no_proto_handler(struct interface_proto_state *proto,
481 enum interface_proto_cmd cmd, bool force)
482 {
483 return 0;
484 }
485
486 static struct interface_proto_state *
487 default_proto_attach(const struct proto_handler *h,
488 struct interface *iface, struct blob_attr *attr)
489 {
490 struct interface_proto_state *proto;
491
492 proto = calloc(1, sizeof(*proto));
493 proto->free = default_proto_free;
494 proto->cb = no_proto_handler;
495
496 return proto;
497 }
498
499 static const struct proto_handler no_proto = {
500 .name = "none",
501 .flags = PROTO_FLAG_IMMEDIATE,
502 .attach = default_proto_attach,
503 };
504
505 static const struct proto_handler *
506 get_proto_handler(const char *name)
507 {
508 struct proto_handler *proto;
509
510 if (!strcmp(name, "none"))
511 return &no_proto;
512
513 if (!handlers.comp)
514 return NULL;
515
516 return avl_find_element(&handlers, name, proto, avl);
517 }
518
519 void
520 proto_dump_handlers(struct blob_buf *b)
521 {
522 struct proto_handler *p;
523 void *c;
524
525 avl_for_each_element(&handlers, p, avl) {
526 c = blobmsg_open_table(b, p->name);
527 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
528 blobmsg_close_table(b, c);
529 }
530 }
531
532 void
533 proto_init_interface(struct interface *iface, struct blob_attr *attr)
534 {
535 const struct proto_handler *proto = iface->proto_handler;
536 struct interface_proto_state *state = NULL;
537
538 if (!proto)
539 proto = &no_proto;
540
541 state = proto->attach(proto, iface, attr);
542 if (!state) {
543 state = no_proto.attach(&no_proto, iface, attr);
544 state->cb = invalid_proto_handler;
545 }
546
547 state->handler = proto;
548 interface_set_proto_state(iface, state);
549 }
550
551 void
552 proto_attach_interface(struct interface *iface, const char *proto_name)
553 {
554 const struct proto_handler *proto = &no_proto;
555
556 if (proto_name) {
557 proto = get_proto_handler(proto_name);
558 if (!proto) {
559 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
560 proto = &no_proto;
561 }
562 }
563
564 iface->proto_handler = proto;
565 }
566
567 int
568 interface_proto_event(struct interface_proto_state *proto,
569 enum interface_proto_cmd cmd, bool force)
570 {
571 enum interface_proto_event ev;
572 int ret;
573
574 ret = proto->cb(proto, cmd, force);
575 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
576 goto out;
577
578 switch(cmd) {
579 case PROTO_CMD_SETUP:
580 ev = IFPEV_UP;
581 break;
582 case PROTO_CMD_TEARDOWN:
583 ev = IFPEV_DOWN;
584 break;
585 default:
586 return -EINVAL;
587 }
588 proto->proto_event(proto, ev);
589
590 out:
591 return ret;
592 }