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