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 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 },
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,
75 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
76 [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
77 [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
78 [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
79 [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
80 [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 },
81 [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
82 [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL },
85 static struct device_addr *
86 alloc_device_addr(bool v6, bool ext)
88 struct device_addr *addr;
90 addr = calloc(1, sizeof(*addr));
91 addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
93 addr->flags |= DEVADDR_EXTERNAL;
99 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
100 bool ext, uint32_t broadcast)
102 struct device_addr *addr;
103 int af = v6 ? AF_INET6 : AF_INET;
105 addr = alloc_device_addr(v6, ext);
110 if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
111 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
117 addr->broadcast = broadcast;
119 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
124 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
125 bool v6, int netmask, bool ext, uint32_t broadcast)
127 struct blob_attr *cur;
131 blobmsg_for_each_attr(cur, attr, rem) {
132 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
136 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
144 static struct device_addr *
145 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
147 struct device_addr *addr;
148 struct blob_attr *tb[__ADDR_MAX];
149 struct blob_attr *cur;
151 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
154 addr = alloc_device_addr(v6, ext);
158 blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
160 addr->mask = v6 ? 128 : 32;
161 if ((cur = tb[ADDR_MASK])) {
162 unsigned int new_mask;
164 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
165 if (new_mask > addr->mask)
168 addr->mask = new_mask;
171 cur = tb[ADDR_IPADDR];
175 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
178 if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
179 addr->flags |= DEVADDR_OFFLINK;
182 if ((cur = tb[ADDR_BROADCAST]) &&
183 !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
185 if ((cur = tb[ADDR_PTP]) &&
186 !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
189 time_t now = system_get_rtime();
190 if ((cur = tb[ADDR_PREFERRED])) {
191 int64_t preferred = blobmsg_get_u32(cur);
192 int64_t preferred_until = preferred + (int64_t)now;
193 if (preferred_until <= LONG_MAX && preferred != 0xffffffffLL)
194 addr->preferred_until = preferred_until;
197 if ((cur = tb[ADDR_VALID])) {
198 int64_t valid = blobmsg_get_u32(cur);
199 int64_t valid_until = valid + (int64_t)now;
200 if (valid_until <= LONG_MAX && valid != 0xffffffffLL)
201 addr->valid_until = valid_until;
205 if (addr->valid_until) {
206 if (!addr->preferred_until)
207 addr->preferred_until = addr->valid_until;
208 else if (addr->preferred_until > addr->valid_until)
221 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
224 struct device_addr *addr;
225 struct blob_attr *cur;
229 blobmsg_for_each_attr(cur, attr, rem) {
230 addr = parse_address_item(cur, v6, ext);
235 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
242 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
244 struct device_route *route;
245 const char *str = blobmsg_data(attr);
246 int af = v6 ? AF_INET6 : AF_INET;
248 route = calloc(1, sizeof(*route));
249 if (!inet_pton(af, str, &route->nexthop)) {
250 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
256 route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
258 unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
260 route->table = table;
261 route->flags |= DEVROUTE_SRCTABLE;
264 vlist_add(&iface->proto_ip.route, &route->node, route);
270 parse_prefix_option(struct interface *iface, const char *str, size_t len)
272 char buf[128] = {0}, *saveptr;
273 if (len > sizeof(buf))
276 memcpy(buf, str, len);
277 char *addrstr = strtok_r(buf, "/", &saveptr);
281 char *lengthstr = strtok_r(NULL, ",", &saveptr);
285 char *prefstr = strtok_r(NULL, ",", &saveptr);
286 char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
287 char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
289 int64_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
290 int64_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
292 uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
293 if (length < 1 || length > 64)
296 struct in6_addr addr, excluded, *excludedp = NULL;
297 if (inet_pton(AF_INET6, addrstr, &addr) < 1)
300 for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) {
301 char *key = NULL, *val = NULL, *addsaveptr;
302 if (!(key = strtok_r(addstr, "=", &addsaveptr)) ||
303 !(val = strtok_r(NULL, ",", &addsaveptr)))
306 if (!strcmp(key, "excluded")) {
307 char *sep = strchr(val, '/');
312 excl_length = atoi(sep + 1);
314 if (inet_pton(AF_INET6, val, &excluded) < 1)
317 excludedp = &excluded;
325 int64_t now = system_get_rtime();
326 time_t preferred_until = 0;
327 if (prefstr && pref != 0xffffffffLL && pref + now <= LONG_MAX)
328 preferred_until = pref + now;
330 time_t valid_until = 0;
331 if (validstr && valid != 0xffffffffLL && valid + now <= LONG_MAX)
332 valid_until = valid + now;
334 interface_ip_add_device_prefix(iface, &addr, length,
335 valid_until, preferred_until,
336 excludedp, excl_length);
341 parse_prefix_list(struct interface *iface, struct blob_attr *attr)
343 struct blob_attr *cur;
347 blobmsg_for_each_attr(cur, attr, rem) {
348 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
352 if (!parse_prefix_option(iface, blobmsg_data(cur),
353 blobmsg_data_len(cur)))
361 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
363 struct blob_attr *tb[__OPT_MAX];
364 struct blob_attr *cur;
366 unsigned int netmask = 32;
367 int n_v4 = 0, n_v6 = 0;
368 struct in_addr bcast = {};
370 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
372 if ((cur = tb[OPT_NETMASK])) {
373 netmask = parse_netmask_string(blobmsg_data(cur), false);
375 error = "INVALID_NETMASK";
380 if ((cur = tb[OPT_BROADCAST])) {
381 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
382 error = "INVALID_BROADCAST";
387 if ((cur = tb[OPT_IPADDR]))
388 n_v4 = parse_static_address_option(iface, cur, false,
389 netmask, false, bcast.s_addr);
391 if ((cur = tb[OPT_IP6ADDR]))
392 n_v6 = parse_static_address_option(iface, cur, true,
395 if ((cur = tb[OPT_IP6PREFIX]))
396 if (parse_prefix_list(iface, cur) < 0)
399 if (n_v4 < 0 || n_v6 < 0)
402 if ((cur = tb[OPT_GATEWAY])) {
403 if (n_v4 && !parse_gateway_option(iface, cur, false))
407 if ((cur = tb[OPT_IP6GW])) {
408 if (n_v6 && !parse_gateway_option(iface, cur, true))
415 interface_add_error(iface, "proto", error, NULL, 0);
421 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
423 struct blob_attr *tb[__OPT_MAX];
424 struct blob_attr *cur;
425 int n_v4 = 0, n_v6 = 0;
427 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
429 if ((cur = tb[OPT_IPADDR]))
430 n_v4 = parse_address_list(iface, cur, false, ext);
432 if ((cur = tb[OPT_IP6ADDR]))
433 n_v6 = parse_address_list(iface, cur, true, ext);
435 if ((cur = tb[OPT_IP6PREFIX]))
436 if (parse_prefix_list(iface, cur) < 0)
439 if (n_v4 < 0 || n_v6 < 0)
442 if ((cur = tb[OPT_GATEWAY])) {
443 if (n_v4 && !parse_gateway_option(iface, cur, false))
447 if ((cur = tb[OPT_IP6GW])) {
448 if (n_v6 && !parse_gateway_option(iface, cur, true))
458 void add_proto_handler(struct proto_handler *p)
461 avl_init(&handlers, avl_strcmp, false, NULL);
466 p->avl.key = p->name;
467 avl_insert(&handlers, &p->avl);
471 default_proto_free(struct interface_proto_state *proto)
477 invalid_proto_handler(struct interface_proto_state *proto,
478 enum interface_proto_cmd cmd, bool force)
484 no_proto_handler(struct interface_proto_state *proto,
485 enum interface_proto_cmd cmd, bool force)
490 static struct interface_proto_state *
491 default_proto_attach(const struct proto_handler *h,
492 struct interface *iface, struct blob_attr *attr)
494 struct interface_proto_state *proto;
496 proto = calloc(1, sizeof(*proto));
497 proto->free = default_proto_free;
498 proto->cb = no_proto_handler;
503 static const struct proto_handler no_proto = {
505 .flags = PROTO_FLAG_IMMEDIATE,
506 .attach = default_proto_attach,
509 static const struct proto_handler *
510 get_proto_handler(const char *name)
512 struct proto_handler *proto;
514 if (!strcmp(name, "none"))
520 return avl_find_element(&handlers, name, proto, avl);
524 proto_dump_handlers(struct blob_buf *b)
526 struct proto_handler *p;
529 avl_for_each_element(&handlers, p, avl) {
530 c = blobmsg_open_table(b, p->name);
531 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
532 blobmsg_close_table(b, c);
537 proto_init_interface(struct interface *iface, struct blob_attr *attr)
539 const struct proto_handler *proto = iface->proto_handler;
540 struct interface_proto_state *state = NULL;
545 state = proto->attach(proto, iface, attr);
547 state = no_proto.attach(&no_proto, iface, attr);
548 state->cb = invalid_proto_handler;
551 state->handler = proto;
552 interface_set_proto_state(iface, state);
556 proto_attach_interface(struct interface *iface, const char *proto_name)
558 const struct proto_handler *proto = &no_proto;
561 proto = get_proto_handler(proto_name);
563 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
568 iface->proto_handler = proto;
572 interface_proto_event(struct interface_proto_state *proto,
573 enum interface_proto_cmd cmd, bool force)
575 enum interface_proto_event ev;
578 ret = proto->cb(proto, cmd, force);
579 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
583 case PROTO_CMD_SETUP:
586 case PROTO_CMD_TEARDOWN:
592 proto->proto_event(proto, ev);