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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt