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