31b36488b33d501c7d06ee683257ce2724808337
[project/netifd.git] / proto.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
20
21 #include "netifd.h"
22 #include "system.h"
23 #include "interface.h"
24 #include "interface-ip.h"
25 #include "proto.h"
26
27 static struct avl_tree handlers;
28
29 enum {
30         OPT_IPADDR,
31         OPT_IP6ADDR,
32         OPT_NETMASK,
33         OPT_BROADCAST,
34         OPT_GATEWAY,
35         OPT_IP6GW,
36         OPT_IP6PREFIX,
37         __OPT_MAX,
38 };
39
40 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
41         [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
42         [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
43         [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
44         [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
45         [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
46         [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
47         [OPT_IP6PREFIX] = { .name = "ip6prefix", .type = BLOBMSG_TYPE_ARRAY },
48 };
49
50 static const union config_param_info proto_ip_attr_info[__OPT_MAX] = {
51         [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
52         [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
53         [OPT_IP6PREFIX] = { .type = BLOBMSG_TYPE_STRING },
54 };
55
56 const struct config_param_list proto_ip_attr = {
57         .n_params = __OPT_MAX,
58         .params = proto_ip_attributes,
59         .info = proto_ip_attr_info,
60 };
61
62 enum {
63         ADDR_IPADDR,
64         ADDR_MASK,
65         ADDR_BROADCAST,
66         ADDR_PTP,
67         ADDR_PREFERRED,
68         ADDR_VALID,
69         ADDR_OFFLINK,
70         __ADDR_MAX
71 };
72
73 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
74         [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
75         [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
76         [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
77         [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
78         [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 },
79         [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
80         [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL },
81 };
82
83 static struct device_addr *
84 alloc_device_addr(bool v6, bool ext)
85 {
86         struct device_addr *addr;
87
88         addr = calloc(1, sizeof(*addr));
89         addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
90         if (ext)
91                 addr->flags |= DEVADDR_EXTERNAL;
92
93         return addr;
94 }
95
96 static bool
97 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
98            bool ext, uint32_t broadcast)
99 {
100         struct device_addr *addr;
101         int af = v6 ? AF_INET6 : AF_INET;
102
103         addr = alloc_device_addr(v6, ext);
104         if (!addr)
105                 return false;
106
107         addr->mask = mask;
108         if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
109                 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
110                 free(addr);
111                 return false;
112         }
113
114         if (broadcast)
115                 addr->broadcast = broadcast;
116
117         vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
118         return true;
119 }
120
121 static int
122 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
123                             bool v6, int netmask, bool ext, uint32_t broadcast)
124 {
125         struct blob_attr *cur;
126         int n_addr = 0;
127         int rem;
128
129         blobmsg_for_each_attr(cur, attr, rem) {
130                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
131                         return -1;
132
133                 n_addr++;
134                 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
135                                 broadcast))
136                         return -1;
137         }
138
139         return n_addr;
140 }
141
142 static struct device_addr *
143 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
144 {
145         struct device_addr *addr;
146         struct blob_attr *tb[__ADDR_MAX];
147         struct blob_attr *cur;
148
149         if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
150                 return NULL;
151
152         addr = alloc_device_addr(v6, ext);
153         if (!addr)
154                 return NULL;
155
156         blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
157
158         addr->mask = v6 ? 128 : 32;
159         if ((cur = tb[ADDR_MASK])) {
160                 unsigned int new_mask;
161
162                 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
163                 if (new_mask > addr->mask)
164                         goto error;
165
166                 addr->mask = new_mask;
167         }
168
169         cur = tb[ADDR_IPADDR];
170         if (!cur)
171                 goto error;
172
173         if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
174                 goto error;
175
176         if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
177                 addr->flags |= DEVADDR_OFFLINK;
178
179         if (!v6) {
180                 if ((cur = tb[ADDR_BROADCAST]) &&
181                     !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
182                         goto error;
183                 if ((cur = tb[ADDR_PTP]) &&
184                     !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
185                         goto error;
186         } else {
187                 time_t now = system_get_rtime();
188                 if ((cur = tb[ADDR_PREFERRED])) {
189                         uint32_t preferred = blobmsg_get_u32(cur);
190                         if (preferred < UINT32_MAX)
191                                 addr->preferred_until = now + preferred;
192                 }
193
194                 if ((cur = tb[ADDR_VALID])) {
195                         uint32_t valid = blobmsg_get_u32(cur);
196                         if (valid < UINT32_MAX)
197                                 addr->valid_until = now + valid;
198
199                 }
200
201                 if (addr->valid_until) {
202                         if (!addr->preferred_until)
203                                 addr->preferred_until = addr->valid_until;
204                         else if (addr->preferred_until > addr->valid_until)
205                                 goto error;
206                 }
207         }
208
209         return addr;
210
211 error:
212         free(addr);
213         return NULL;
214 }
215
216 static int
217 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
218                    bool ext)
219 {
220         struct device_addr *addr;
221         struct blob_attr *cur;
222         int n_addr = 0;
223         int rem;
224
225         blobmsg_for_each_attr(cur, attr, rem) {
226                 addr = parse_address_item(cur, v6, ext);
227                 if (!addr)
228                         return -1;
229
230                 n_addr++;
231                 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
232         }
233
234         return n_addr;
235 }
236
237 static bool
238 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
239 {
240         struct device_route *route;
241         const char *str = blobmsg_data(attr);
242         int af = v6 ? AF_INET6 : AF_INET;
243
244         route = calloc(1, sizeof(*route));
245         if (!inet_pton(af, str, &route->nexthop)) {
246                 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
247                 free(route);
248                 return false;
249         }
250
251         route->mask = 0;
252         route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
253         vlist_add(&iface->proto_ip.route, &route->node, &route->flags);
254
255         return true;
256 }
257
258 static bool
259 parse_prefix_option(struct interface *iface, const char *str, size_t len)
260 {
261         char buf[128] = {0}, *saveptr;
262         if (len > sizeof(buf))
263                 return false;
264
265         memcpy(buf, str, len);
266         char *addrstr = strtok_r(buf, "/", &saveptr);
267         if (!addrstr)
268                 return false;
269
270         char *lengthstr = strtok_r(NULL, ",", &saveptr);
271         if (!lengthstr)
272                 return false;
273
274         char *prefstr = strtok_r(NULL, ",", &saveptr);
275         char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
276         char *excludestr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
277
278         uint32_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
279         uint32_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
280
281         uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
282         if (length < 1 || length > 64)
283                 return false;
284
285         struct in6_addr addr, excluded, *excludedp = NULL;
286         if (inet_pton(AF_INET6, addrstr, &addr) < 1)
287                 return false;
288
289         if (excludestr) {
290                 char *sep = strchr(excludestr, '/');
291                 if (!sep)
292                         return false;
293
294                 *sep = 0;
295                 excl_length = atoi(sep + 1);
296
297                 if (inet_pton(AF_INET6, excludestr, &excluded) < 1)
298                         return false;
299
300                 excludedp = &excluded;
301         }
302
303         time_t now = system_get_rtime();
304         time_t preferred_until = 0;
305         if (prefstr && pref != 0xffffffffU)
306                 preferred_until = pref + now;
307
308         time_t valid_until = 0;
309         if (validstr && valid != 0xffffffffU)
310                 valid_until = valid + now;
311
312         interface_ip_add_device_prefix(iface, &addr, length,
313                         valid_until, preferred_until,
314                         excludedp, excl_length);
315         return true;
316 }
317
318 static int
319 parse_prefix_list(struct interface *iface, struct blob_attr *attr)
320 {
321         struct blob_attr *cur;
322         int n_addr = 0;
323         int rem;
324
325         blobmsg_for_each_attr(cur, attr, rem) {
326                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
327                         return -1;
328
329                 n_addr++;
330                 if (!parse_prefix_option(iface, blobmsg_data(cur),
331                                 blobmsg_data_len(cur)))
332                         return -1;
333         }
334
335         return n_addr;
336 }
337
338 int
339 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
340 {
341         struct blob_attr *tb[__OPT_MAX];
342         struct blob_attr *cur;
343         const char *error;
344         unsigned int netmask = 32;
345         int n_v4 = 0, n_v6 = 0;
346         struct in_addr bcast = {};
347
348         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
349
350         if ((cur = tb[OPT_NETMASK])) {
351                 netmask = parse_netmask_string(blobmsg_data(cur), false);
352                 if (netmask > 32) {
353                         error = "INVALID_NETMASK";
354                         goto error;
355                 }
356         }
357
358         if ((cur = tb[OPT_BROADCAST])) {
359                 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
360                         error = "INVALID_BROADCAST";
361                         goto error;
362                 }
363         }
364
365         if ((cur = tb[OPT_IPADDR]))
366                 n_v4 = parse_static_address_option(iface, cur, false,
367                         netmask, false, bcast.s_addr);
368
369         if ((cur = tb[OPT_IP6ADDR]))
370                 n_v6 = parse_static_address_option(iface, cur, true,
371                         128, false, 0);
372
373         if ((cur = tb[OPT_IP6PREFIX]))
374                 if (parse_prefix_list(iface, cur) < 0)
375                         goto out;
376
377         if (n_v4 < 0 || n_v6 < 0)
378                 goto out;
379
380         if ((cur = tb[OPT_GATEWAY])) {
381                 if (n_v4 && !parse_gateway_option(iface, cur, false))
382                         goto out;
383         }
384
385         if ((cur = tb[OPT_IP6GW])) {
386                 if (n_v6 && !parse_gateway_option(iface, cur, true))
387                         goto out;
388         }
389
390         return 0;
391
392 error:
393         interface_add_error(iface, "proto", error, NULL, 0);
394 out:
395         return -1;
396 }
397
398 int
399 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
400 {
401         struct blob_attr *tb[__OPT_MAX];
402         struct blob_attr *cur;
403         int n_v4 = 0, n_v6 = 0;
404
405         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
406
407         if ((cur = tb[OPT_IPADDR]))
408                 n_v4 = parse_address_list(iface, cur, false, ext);
409
410         if ((cur = tb[OPT_IP6ADDR]))
411                 n_v6 = parse_address_list(iface, cur, true, ext);
412
413         if ((cur = tb[OPT_IP6PREFIX]))
414                 if (parse_prefix_list(iface, cur) < 0)
415                         goto out;
416
417         if (n_v4 < 0 || n_v6 < 0)
418                 goto out;
419
420         if ((cur = tb[OPT_GATEWAY])) {
421                 if (n_v4 && !parse_gateway_option(iface, cur, false))
422                         goto out;
423         }
424
425         if ((cur = tb[OPT_IP6GW])) {
426                 if (n_v6 && !parse_gateway_option(iface, cur, true))
427                         goto out;
428         }
429
430         return 0;
431
432 out:
433         return -1;
434 }
435
436 void add_proto_handler(struct proto_handler *p)
437 {
438         if (!handlers.comp)
439                 avl_init(&handlers, avl_strcmp, false, NULL);
440
441         if (p->avl.key)
442                 return;
443
444         p->avl.key = p->name;
445         avl_insert(&handlers, &p->avl);
446 }
447
448 static void
449 default_proto_free(struct interface_proto_state *proto)
450 {
451         free(proto);
452 }
453
454 static int
455 invalid_proto_handler(struct interface_proto_state *proto,
456                       enum interface_proto_cmd cmd, bool force)
457 {
458         return -1;
459 }
460
461 static int
462 no_proto_handler(struct interface_proto_state *proto,
463                  enum interface_proto_cmd cmd, bool force)
464 {
465         return 0;
466 }
467
468 static struct interface_proto_state *
469 default_proto_attach(const struct proto_handler *h,
470                      struct interface *iface, struct blob_attr *attr)
471 {
472         struct interface_proto_state *proto;
473
474         proto = calloc(1, sizeof(*proto));
475         proto->free = default_proto_free;
476         proto->cb = no_proto_handler;
477
478         return proto;
479 }
480
481 static const struct proto_handler no_proto = {
482         .name = "none",
483         .flags = PROTO_FLAG_IMMEDIATE,
484         .attach = default_proto_attach,
485 };
486
487 static const struct proto_handler *
488 get_proto_handler(const char *name)
489 {
490         struct proto_handler *proto;
491
492         if (!strcmp(name, "none"))
493             return &no_proto;
494
495         if (!handlers.comp)
496                 return NULL;
497
498         return avl_find_element(&handlers, name, proto, avl);
499 }
500
501 void
502 proto_dump_handlers(struct blob_buf *b)
503 {
504         struct proto_handler *p;
505         void *c;
506
507         avl_for_each_element(&handlers, p, avl) {
508                 c = blobmsg_open_table(b, p->name);
509                 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
510                 blobmsg_close_table(b, c);
511         }
512 }
513
514 void
515 proto_init_interface(struct interface *iface, struct blob_attr *attr)
516 {
517         const struct proto_handler *proto = iface->proto_handler;
518         struct interface_proto_state *state = NULL;
519
520         if (!proto)
521                 proto = &no_proto;
522
523         state = proto->attach(proto, iface, attr);
524         if (!state) {
525                 state = no_proto.attach(&no_proto, iface, attr);
526                 state->cb = invalid_proto_handler;
527         }
528
529         state->handler = proto;
530         interface_set_proto_state(iface, state);
531 }
532
533 void
534 proto_attach_interface(struct interface *iface, const char *proto_name)
535 {
536         const struct proto_handler *proto = &no_proto;
537
538         if (proto_name) {
539                 proto = get_proto_handler(proto_name);
540                 if (!proto) {
541                         interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
542                         proto = &no_proto;
543                 }
544         }
545
546         iface->proto_handler = proto;
547 }
548
549 int
550 interface_proto_event(struct interface_proto_state *proto,
551                       enum interface_proto_cmd cmd, bool force)
552 {
553         enum interface_proto_event ev;
554         int ret;
555
556         ret = proto->cb(proto, cmd, force);
557         if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
558                 goto out;
559
560         switch(cmd) {
561         case PROTO_CMD_SETUP:
562                 ev = IFPEV_UP;
563                 break;
564         case PROTO_CMD_TEARDOWN:
565                 ev = IFPEV_DOWN;
566                 break;
567         default:
568                 return -EINVAL;
569         }
570         proto->proto_event(proto, ev);
571
572 out:
573         return ret;
574 }