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