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