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