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