83c63451c139c265b8c160a58814789dc8331706
[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         vlist_add(&iface->proto_ip.route, &route->node, route);
255
256         return true;
257 }
258
259 static bool
260 parse_prefix_option(struct interface *iface, const char *str, size_t len)
261 {
262         char buf[128] = {0}, *saveptr;
263         if (len > sizeof(buf))
264                 return false;
265
266         memcpy(buf, str, len);
267         char *addrstr = strtok_r(buf, "/", &saveptr);
268         if (!addrstr)
269                 return false;
270
271         char *lengthstr = strtok_r(NULL, ",", &saveptr);
272         if (!lengthstr)
273                 return false;
274
275         char *prefstr = strtok_r(NULL, ",", &saveptr);
276         char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
277         char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
278
279         uint32_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
280         uint32_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
281
282         uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
283         if (length < 1 || length > 64)
284                 return false;
285
286         struct in6_addr addr, excluded, *excludedp = NULL;
287         if (inet_pton(AF_INET6, addrstr, &addr) < 1)
288                 return false;
289
290         for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) {
291                 char *key = NULL, *val = NULL, *addsaveptr;
292                 if (!(key = strtok_r(addstr, "=", &addsaveptr)) ||
293                                 !(val = strtok_r(NULL, ",", &addsaveptr)))
294                         continue;
295
296                 if (!strcmp(key, "excluded")) {
297                         char *sep = strchr(val, '/');
298                         if (!sep)
299                                 return false;
300
301                         *sep = 0;
302                         excl_length = atoi(sep + 1);
303
304                         if (inet_pton(AF_INET6, val, &excluded) < 1)
305                                 return false;
306
307                         excludedp = &excluded;
308                 }
309
310         }
311
312
313
314
315         time_t now = system_get_rtime();
316         time_t preferred_until = 0;
317         if (prefstr && pref != 0xffffffffU)
318                 preferred_until = pref + now;
319
320         time_t valid_until = 0;
321         if (validstr && valid != 0xffffffffU)
322                 valid_until = valid + now;
323
324         interface_ip_add_device_prefix(iface, &addr, length,
325                         valid_until, preferred_until,
326                         excludedp, excl_length);
327         return true;
328 }
329
330 static int
331 parse_prefix_list(struct interface *iface, struct blob_attr *attr)
332 {
333         struct blob_attr *cur;
334         int n_addr = 0;
335         int rem;
336
337         blobmsg_for_each_attr(cur, attr, rem) {
338                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
339                         return -1;
340
341                 n_addr++;
342                 if (!parse_prefix_option(iface, blobmsg_data(cur),
343                                 blobmsg_data_len(cur)))
344                         return -1;
345         }
346
347         return n_addr;
348 }
349
350 int
351 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
352 {
353         struct blob_attr *tb[__OPT_MAX];
354         struct blob_attr *cur;
355         const char *error;
356         unsigned int netmask = 32;
357         int n_v4 = 0, n_v6 = 0;
358         struct in_addr bcast = {};
359
360         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
361
362         if ((cur = tb[OPT_NETMASK])) {
363                 netmask = parse_netmask_string(blobmsg_data(cur), false);
364                 if (netmask > 32) {
365                         error = "INVALID_NETMASK";
366                         goto error;
367                 }
368         }
369
370         if ((cur = tb[OPT_BROADCAST])) {
371                 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
372                         error = "INVALID_BROADCAST";
373                         goto error;
374                 }
375         }
376
377         if ((cur = tb[OPT_IPADDR]))
378                 n_v4 = parse_static_address_option(iface, cur, false,
379                         netmask, false, bcast.s_addr);
380
381         if ((cur = tb[OPT_IP6ADDR]))
382                 n_v6 = parse_static_address_option(iface, cur, true,
383                         128, false, 0);
384
385         if ((cur = tb[OPT_IP6PREFIX]))
386                 if (parse_prefix_list(iface, cur) < 0)
387                         goto out;
388
389         if (n_v4 < 0 || n_v6 < 0)
390                 goto out;
391
392         if ((cur = tb[OPT_GATEWAY])) {
393                 if (n_v4 && !parse_gateway_option(iface, cur, false))
394                         goto out;
395         }
396
397         if ((cur = tb[OPT_IP6GW])) {
398                 if (n_v6 && !parse_gateway_option(iface, cur, true))
399                         goto out;
400         }
401
402         return 0;
403
404 error:
405         interface_add_error(iface, "proto", error, NULL, 0);
406 out:
407         return -1;
408 }
409
410 int
411 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
412 {
413         struct blob_attr *tb[__OPT_MAX];
414         struct blob_attr *cur;
415         int n_v4 = 0, n_v6 = 0;
416
417         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
418
419         if ((cur = tb[OPT_IPADDR]))
420                 n_v4 = parse_address_list(iface, cur, false, ext);
421
422         if ((cur = tb[OPT_IP6ADDR]))
423                 n_v6 = parse_address_list(iface, cur, true, ext);
424
425         if ((cur = tb[OPT_IP6PREFIX]))
426                 if (parse_prefix_list(iface, cur) < 0)
427                         goto out;
428
429         if (n_v4 < 0 || n_v6 < 0)
430                 goto out;
431
432         if ((cur = tb[OPT_GATEWAY])) {
433                 if (n_v4 && !parse_gateway_option(iface, cur, false))
434                         goto out;
435         }
436
437         if ((cur = tb[OPT_IP6GW])) {
438                 if (n_v6 && !parse_gateway_option(iface, cur, true))
439                         goto out;
440         }
441
442         return 0;
443
444 out:
445         return -1;
446 }
447
448 void add_proto_handler(struct proto_handler *p)
449 {
450         if (!handlers.comp)
451                 avl_init(&handlers, avl_strcmp, false, NULL);
452
453         if (p->avl.key)
454                 return;
455
456         p->avl.key = p->name;
457         avl_insert(&handlers, &p->avl);
458 }
459
460 static void
461 default_proto_free(struct interface_proto_state *proto)
462 {
463         free(proto);
464 }
465
466 static int
467 invalid_proto_handler(struct interface_proto_state *proto,
468                       enum interface_proto_cmd cmd, bool force)
469 {
470         return -1;
471 }
472
473 static int
474 no_proto_handler(struct interface_proto_state *proto,
475                  enum interface_proto_cmd cmd, bool force)
476 {
477         return 0;
478 }
479
480 static struct interface_proto_state *
481 default_proto_attach(const struct proto_handler *h,
482                      struct interface *iface, struct blob_attr *attr)
483 {
484         struct interface_proto_state *proto;
485
486         proto = calloc(1, sizeof(*proto));
487         proto->free = default_proto_free;
488         proto->cb = no_proto_handler;
489
490         return proto;
491 }
492
493 static const struct proto_handler no_proto = {
494         .name = "none",
495         .flags = PROTO_FLAG_IMMEDIATE,
496         .attach = default_proto_attach,
497 };
498
499 static const struct proto_handler *
500 get_proto_handler(const char *name)
501 {
502         struct proto_handler *proto;
503
504         if (!strcmp(name, "none"))
505             return &no_proto;
506
507         if (!handlers.comp)
508                 return NULL;
509
510         return avl_find_element(&handlers, name, proto, avl);
511 }
512
513 void
514 proto_dump_handlers(struct blob_buf *b)
515 {
516         struct proto_handler *p;
517         void *c;
518
519         avl_for_each_element(&handlers, p, avl) {
520                 c = blobmsg_open_table(b, p->name);
521                 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
522                 blobmsg_close_table(b, c);
523         }
524 }
525
526 void
527 proto_init_interface(struct interface *iface, struct blob_attr *attr)
528 {
529         const struct proto_handler *proto = iface->proto_handler;
530         struct interface_proto_state *state = NULL;
531
532         if (!proto)
533                 proto = &no_proto;
534
535         state = proto->attach(proto, iface, attr);
536         if (!state) {
537                 state = no_proto.attach(&no_proto, iface, attr);
538                 state->cb = invalid_proto_handler;
539         }
540
541         state->handler = proto;
542         interface_set_proto_state(iface, state);
543 }
544
545 void
546 proto_attach_interface(struct interface *iface, const char *proto_name)
547 {
548         const struct proto_handler *proto = &no_proto;
549
550         if (proto_name) {
551                 proto = get_proto_handler(proto_name);
552                 if (!proto) {
553                         interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
554                         proto = &no_proto;
555                 }
556         }
557
558         iface->proto_handler = proto;
559 }
560
561 int
562 interface_proto_event(struct interface_proto_state *proto,
563                       enum interface_proto_cmd cmd, bool force)
564 {
565         enum interface_proto_event ev;
566         int ret;
567
568         ret = proto->cb(proto, cmd, force);
569         if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
570                 goto out;
571
572         switch(cmd) {
573         case PROTO_CMD_SETUP:
574                 ev = IFPEV_UP;
575                 break;
576         case PROTO_CMD_TEARDOWN:
577                 ev = IFPEV_DOWN;
578                 break;
579         default:
580                 return -EINVAL;
581         }
582         proto->proto_event(proto, ev);
583
584 out:
585         return ret;
586 }