• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt