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