2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2012 Steven Barth <steven@midlink.org>
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
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.
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
25 #include "interface.h"
26 #include "interface-ip.h"
29 static struct avl_tree handlers;
42 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
43 [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
44 [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
45 [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
46 [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
47 [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
48 [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
49 [OPT_IP6PREFIX] = { .name = "ip6prefix", .type = BLOBMSG_TYPE_ARRAY },
52 static const struct uci_blob_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 },
58 static const char * const proto_ip_validate[__OPT_MAX] = {
59 [OPT_IPADDR] = "ip4addr",
60 [OPT_IP6ADDR] = "ip6addr",
61 [OPT_NETMASK] = "netmask",
62 [OPT_BROADCAST] = "ipaddr",
63 [OPT_GATEWAY] = "ip4addr",
64 [OPT_IP6GW] = "ip6addr",
65 [OPT_IP6PREFIX] = "ip6addr",
68 const struct uci_blob_param_list proto_ip_attr = {
69 .n_params = __OPT_MAX,
70 .params = proto_ip_attributes,
71 .validate = proto_ip_validate,
72 .info = proto_ip_attr_info,
87 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
88 [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
89 [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
90 [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
91 [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
92 [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 },
93 [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
94 [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL },
95 [ADDR_CLASS] = { .name = "class", .type = BLOBMSG_TYPE_STRING },
98 static struct device_addr *
99 alloc_device_addr(bool v6, bool ext)
101 struct device_addr *addr;
103 addr = calloc(1, sizeof(*addr));
104 addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
106 addr->flags |= DEVADDR_EXTERNAL;
112 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
113 bool ext, uint32_t broadcast)
115 struct device_addr *addr;
116 int af = v6 ? AF_INET6 : AF_INET;
118 addr = alloc_device_addr(v6, ext);
123 if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
124 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
130 addr->broadcast = broadcast;
132 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
137 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
138 bool v6, int netmask, bool ext, uint32_t broadcast)
140 struct blob_attr *cur;
144 blobmsg_for_each_attr(cur, attr, rem) {
145 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
149 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
157 static struct device_addr *
158 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
160 struct device_addr *addr;
161 struct blob_attr *tb[__ADDR_MAX];
162 struct blob_attr *cur;
164 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
167 addr = alloc_device_addr(v6, ext);
171 blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
173 addr->mask = v6 ? 128 : 32;
174 if ((cur = tb[ADDR_MASK])) {
175 unsigned int new_mask;
177 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
178 if (new_mask > addr->mask)
181 addr->mask = new_mask;
184 cur = tb[ADDR_IPADDR];
188 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
191 if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
192 addr->flags |= DEVADDR_OFFLINK;
195 if ((cur = tb[ADDR_BROADCAST]) &&
196 !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
198 if ((cur = tb[ADDR_PTP]) &&
199 !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
202 time_t now = system_get_rtime();
203 if ((cur = tb[ADDR_PREFERRED])) {
204 int64_t preferred = blobmsg_get_u32(cur);
205 int64_t preferred_until = preferred + (int64_t)now;
206 if (preferred_until <= LONG_MAX && preferred != 0xffffffffLL)
207 addr->preferred_until = preferred_until;
210 if ((cur = tb[ADDR_VALID])) {
211 int64_t valid = blobmsg_get_u32(cur);
212 int64_t valid_until = valid + (int64_t)now;
213 if (valid_until <= LONG_MAX && valid != 0xffffffffLL)
214 addr->valid_until = valid_until;
218 if (addr->valid_until) {
219 if (!addr->preferred_until)
220 addr->preferred_until = addr->valid_until;
221 else if (addr->preferred_until > addr->valid_until)
225 if ((cur = tb[ADDR_CLASS]))
226 addr->pclass = strdup(blobmsg_get_string(cur));
237 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
240 struct device_addr *addr;
241 struct blob_attr *cur;
245 blobmsg_for_each_attr(cur, attr, rem) {
246 addr = parse_address_item(cur, v6, ext);
251 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
258 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
260 struct device_route *route;
261 const char *str = blobmsg_data(attr);
262 int af = v6 ? AF_INET6 : AF_INET;
264 route = calloc(1, sizeof(*route));
265 if (!inet_pton(af, str, &route->nexthop)) {
266 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
272 route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
274 unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
276 route->table = table;
277 route->flags |= DEVROUTE_SRCTABLE;
280 vlist_add(&iface->proto_ip.route, &route->node, route);
286 parse_prefix_option(struct interface *iface, const char *str, size_t len)
288 char buf[128] = {0}, *saveptr;
289 if (len > sizeof(buf))
292 memcpy(buf, str, len);
293 char *addrstr = strtok_r(buf, "/", &saveptr);
297 char *lengthstr = strtok_r(NULL, ",", &saveptr);
301 char *prefstr = strtok_r(NULL, ",", &saveptr);
302 char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
303 char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
304 const char *pclass = NULL;
306 int64_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
307 int64_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
309 uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
310 if (length < 1 || length > 64)
313 struct in6_addr addr, excluded, *excludedp = NULL;
314 if (inet_pton(AF_INET6, addrstr, &addr) < 1)
317 for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) {
318 char *key = NULL, *val = NULL, *addsaveptr;
319 if (!(key = strtok_r(addstr, "=", &addsaveptr)) ||
320 !(val = strtok_r(NULL, ",", &addsaveptr)))
323 if (!strcmp(key, "excluded")) {
324 char *sep = strchr(val, '/');
329 excl_length = atoi(sep + 1);
331 if (inet_pton(AF_INET6, val, &excluded) < 1)
334 excludedp = &excluded;
335 } else if (!strcmp(key, "class")) {
344 int64_t now = system_get_rtime();
345 time_t preferred_until = 0;
346 if (prefstr && pref != 0xffffffffLL && pref + now <= LONG_MAX)
347 preferred_until = pref + now;
349 time_t valid_until = 0;
350 if (validstr && valid != 0xffffffffLL && valid + now <= LONG_MAX)
351 valid_until = valid + now;
353 interface_ip_add_device_prefix(iface, &addr, length,
354 valid_until, preferred_until,
355 excludedp, excl_length, pclass);
360 parse_prefix_list(struct interface *iface, struct blob_attr *attr)
362 struct blob_attr *cur;
366 blobmsg_for_each_attr(cur, attr, rem) {
367 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
371 if (!parse_prefix_option(iface, blobmsg_data(cur),
372 blobmsg_data_len(cur)))
380 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
382 struct blob_attr *tb[__OPT_MAX];
383 struct blob_attr *cur;
385 unsigned int netmask = 32;
386 int n_v4 = 0, n_v6 = 0;
387 struct in_addr bcast = {};
389 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
391 if ((cur = tb[OPT_NETMASK])) {
392 netmask = parse_netmask_string(blobmsg_data(cur), false);
394 error = "INVALID_NETMASK";
399 if ((cur = tb[OPT_BROADCAST])) {
400 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
401 error = "INVALID_BROADCAST";
406 if ((cur = tb[OPT_IPADDR]))
407 n_v4 = parse_static_address_option(iface, cur, false,
408 netmask, false, bcast.s_addr);
410 if ((cur = tb[OPT_IP6ADDR]))
411 n_v6 = parse_static_address_option(iface, cur, true,
414 if ((cur = tb[OPT_IP6PREFIX]))
415 if (parse_prefix_list(iface, cur) < 0)
418 if (n_v4 < 0 || n_v6 < 0)
421 if ((cur = tb[OPT_GATEWAY])) {
422 if (n_v4 && !parse_gateway_option(iface, cur, false))
426 if ((cur = tb[OPT_IP6GW])) {
427 if (n_v6 && !parse_gateway_option(iface, cur, true))
434 interface_add_error(iface, "proto", error, NULL, 0);
440 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
442 struct blob_attr *tb[__OPT_MAX];
443 struct blob_attr *cur;
444 int n_v4 = 0, n_v6 = 0;
446 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
448 if ((cur = tb[OPT_IPADDR]))
449 n_v4 = parse_address_list(iface, cur, false, ext);
451 if ((cur = tb[OPT_IP6ADDR]))
452 n_v6 = parse_address_list(iface, cur, true, ext);
454 if ((cur = tb[OPT_IP6PREFIX]))
455 if (parse_prefix_list(iface, cur) < 0)
458 if (n_v4 < 0 || n_v6 < 0)
461 if ((cur = tb[OPT_GATEWAY])) {
462 if (n_v4 && !parse_gateway_option(iface, cur, false))
466 if ((cur = tb[OPT_IP6GW])) {
467 if (n_v6 && !parse_gateway_option(iface, cur, true))
477 void add_proto_handler(struct proto_handler *p)
480 avl_init(&handlers, avl_strcmp, false, NULL);
485 p->avl.key = p->name;
486 avl_insert(&handlers, &p->avl);
490 default_proto_free(struct interface_proto_state *proto)
496 invalid_proto_handler(struct interface_proto_state *proto,
497 enum interface_proto_cmd cmd, bool force)
503 no_proto_handler(struct interface_proto_state *proto,
504 enum interface_proto_cmd cmd, bool force)
509 static struct interface_proto_state *
510 default_proto_attach(const struct proto_handler *h,
511 struct interface *iface, struct blob_attr *attr)
513 struct interface_proto_state *proto;
515 proto = calloc(1, sizeof(*proto));
516 proto->free = default_proto_free;
517 proto->cb = no_proto_handler;
522 static const struct proto_handler no_proto = {
524 .flags = PROTO_FLAG_IMMEDIATE,
525 .attach = default_proto_attach,
528 static const struct proto_handler *
529 get_proto_handler(const char *name)
531 struct proto_handler *proto;
533 if (!strcmp(name, "none"))
539 return avl_find_element(&handlers, name, proto, avl);
543 proto_dump_handlers(struct blob_buf *b)
545 struct proto_handler *p;
548 avl_for_each_element(&handlers, p, avl) {
551 c = blobmsg_open_table(b, p->name);
552 if (p->config_params->validate) {
555 v = blobmsg_open_table(b, "validate");
556 for (i = 0; i < p->config_params->n_params; i++)
557 blobmsg_add_string(b, p->config_params->params[i].name, uci_get_validate_string(p->config_params, i));
558 blobmsg_close_table(b, v);
560 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
561 blobmsg_close_table(b, c);
566 proto_init_interface(struct interface *iface, struct blob_attr *attr)
568 const struct proto_handler *proto = iface->proto_handler;
569 struct interface_proto_state *state = NULL;
574 state = proto->attach(proto, iface, attr);
576 state = no_proto.attach(&no_proto, iface, attr);
577 state->cb = invalid_proto_handler;
580 state->handler = proto;
581 interface_set_proto_state(iface, state);
585 proto_attach_interface(struct interface *iface, const char *proto_name)
587 const struct proto_handler *proto = &no_proto;
590 proto = get_proto_handler(proto_name);
592 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
597 iface->proto_handler = proto;
601 interface_proto_event(struct interface_proto_state *proto,
602 enum interface_proto_cmd cmd, bool force)
604 enum interface_proto_event ev;
607 ret = proto->cb(proto, cmd, force);
608 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
612 case PROTO_CMD_SETUP:
615 case PROTO_CMD_TEARDOWN:
621 proto->proto_event(proto, ev);