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.
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
24 #include "interface.h"
25 #include "interface-ip.h"
28 static struct avl_tree handlers;
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 },
51 static const union config_param_info proto_ip_attr_info[__OPT_MAX] = {
52 [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
53 [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
54 [OPT_IP6PREFIX] = { .type = BLOBMSG_TYPE_STRING },
57 const struct config_param_list proto_ip_attr = {
58 .n_params = __OPT_MAX,
59 .params = proto_ip_attributes,
60 .info = proto_ip_attr_info,
74 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
75 [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
76 [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
77 [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
78 [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
79 [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 },
80 [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
81 [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL },
84 static struct device_addr *
85 alloc_device_addr(bool v6, bool ext)
87 struct device_addr *addr;
89 addr = calloc(1, sizeof(*addr));
90 addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
92 addr->flags |= DEVADDR_EXTERNAL;
98 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
99 bool ext, uint32_t broadcast)
101 struct device_addr *addr;
102 int af = v6 ? AF_INET6 : AF_INET;
104 addr = alloc_device_addr(v6, ext);
109 if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
110 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
116 addr->broadcast = broadcast;
118 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
123 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
124 bool v6, int netmask, bool ext, uint32_t broadcast)
126 struct blob_attr *cur;
130 blobmsg_for_each_attr(cur, attr, rem) {
131 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
135 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
143 static struct device_addr *
144 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
146 struct device_addr *addr;
147 struct blob_attr *tb[__ADDR_MAX];
148 struct blob_attr *cur;
150 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
153 addr = alloc_device_addr(v6, ext);
157 blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
159 addr->mask = v6 ? 128 : 32;
160 if ((cur = tb[ADDR_MASK])) {
161 unsigned int new_mask;
163 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
164 if (new_mask > addr->mask)
167 addr->mask = new_mask;
170 cur = tb[ADDR_IPADDR];
174 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
177 if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
178 addr->flags |= DEVADDR_OFFLINK;
181 if ((cur = tb[ADDR_BROADCAST]) &&
182 !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
184 if ((cur = tb[ADDR_PTP]) &&
185 !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
188 time_t now = system_get_rtime();
189 if ((cur = tb[ADDR_PREFERRED])) {
190 uint32_t preferred = blobmsg_get_u32(cur);
191 if (preferred < UINT32_MAX)
192 addr->preferred_until = now + preferred;
195 if ((cur = tb[ADDR_VALID])) {
196 uint32_t valid = blobmsg_get_u32(cur);
197 if (valid < UINT32_MAX)
198 addr->valid_until = now + valid;
202 if (addr->valid_until) {
203 if (!addr->preferred_until)
204 addr->preferred_until = addr->valid_until;
205 else if (addr->preferred_until > addr->valid_until)
218 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
221 struct device_addr *addr;
222 struct blob_attr *cur;
226 blobmsg_for_each_attr(cur, attr, rem) {
227 addr = parse_address_item(cur, v6, ext);
232 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
239 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
241 struct device_route *route;
242 const char *str = blobmsg_data(attr);
243 int af = v6 ? AF_INET6 : AF_INET;
245 route = calloc(1, sizeof(*route));
246 if (!inet_pton(af, str, &route->nexthop)) {
247 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
253 route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
254 vlist_add(&iface->proto_ip.route, &route->node, route);
260 parse_prefix_option(struct interface *iface, const char *str, size_t len)
262 char buf[128] = {0}, *saveptr;
263 if (len > sizeof(buf))
266 memcpy(buf, str, len);
267 char *addrstr = strtok_r(buf, "/", &saveptr);
271 char *lengthstr = strtok_r(NULL, ",", &saveptr);
275 char *prefstr = strtok_r(NULL, ",", &saveptr);
276 char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
277 char *excludestr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
279 uint32_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
280 uint32_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
282 uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
283 if (length < 1 || length > 64)
286 struct in6_addr addr, excluded, *excludedp = NULL;
287 if (inet_pton(AF_INET6, addrstr, &addr) < 1)
291 char *sep = strchr(excludestr, '/');
296 excl_length = atoi(sep + 1);
298 if (inet_pton(AF_INET6, excludestr, &excluded) < 1)
301 excludedp = &excluded;
304 time_t now = system_get_rtime();
305 time_t preferred_until = 0;
306 if (prefstr && pref != 0xffffffffU)
307 preferred_until = pref + now;
309 time_t valid_until = 0;
310 if (validstr && valid != 0xffffffffU)
311 valid_until = valid + now;
313 interface_ip_add_device_prefix(iface, &addr, length,
314 valid_until, preferred_until,
315 excludedp, excl_length);
320 parse_prefix_list(struct interface *iface, struct blob_attr *attr)
322 struct blob_attr *cur;
326 blobmsg_for_each_attr(cur, attr, rem) {
327 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
331 if (!parse_prefix_option(iface, blobmsg_data(cur),
332 blobmsg_data_len(cur)))
340 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
342 struct blob_attr *tb[__OPT_MAX];
343 struct blob_attr *cur;
345 unsigned int netmask = 32;
346 int n_v4 = 0, n_v6 = 0;
347 struct in_addr bcast = {};
349 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
351 if ((cur = tb[OPT_NETMASK])) {
352 netmask = parse_netmask_string(blobmsg_data(cur), false);
354 error = "INVALID_NETMASK";
359 if ((cur = tb[OPT_BROADCAST])) {
360 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
361 error = "INVALID_BROADCAST";
366 if ((cur = tb[OPT_IPADDR]))
367 n_v4 = parse_static_address_option(iface, cur, false,
368 netmask, false, bcast.s_addr);
370 if ((cur = tb[OPT_IP6ADDR]))
371 n_v6 = parse_static_address_option(iface, cur, true,
374 if ((cur = tb[OPT_IP6PREFIX]))
375 if (parse_prefix_list(iface, cur) < 0)
378 if (n_v4 < 0 || n_v6 < 0)
381 if ((cur = tb[OPT_GATEWAY])) {
382 if (n_v4 && !parse_gateway_option(iface, cur, false))
386 if ((cur = tb[OPT_IP6GW])) {
387 if (n_v6 && !parse_gateway_option(iface, cur, true))
394 interface_add_error(iface, "proto", error, NULL, 0);
400 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
402 struct blob_attr *tb[__OPT_MAX];
403 struct blob_attr *cur;
404 int n_v4 = 0, n_v6 = 0;
406 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
408 if ((cur = tb[OPT_IPADDR]))
409 n_v4 = parse_address_list(iface, cur, false, ext);
411 if ((cur = tb[OPT_IP6ADDR]))
412 n_v6 = parse_address_list(iface, cur, true, ext);
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))
437 void add_proto_handler(struct proto_handler *p)
440 avl_init(&handlers, avl_strcmp, false, NULL);
445 p->avl.key = p->name;
446 avl_insert(&handlers, &p->avl);
450 default_proto_free(struct interface_proto_state *proto)
456 invalid_proto_handler(struct interface_proto_state *proto,
457 enum interface_proto_cmd cmd, bool force)
463 no_proto_handler(struct interface_proto_state *proto,
464 enum interface_proto_cmd cmd, bool force)
469 static struct interface_proto_state *
470 default_proto_attach(const struct proto_handler *h,
471 struct interface *iface, struct blob_attr *attr)
473 struct interface_proto_state *proto;
475 proto = calloc(1, sizeof(*proto));
476 proto->free = default_proto_free;
477 proto->cb = no_proto_handler;
482 static const struct proto_handler no_proto = {
484 .flags = PROTO_FLAG_IMMEDIATE,
485 .attach = default_proto_attach,
488 static const struct proto_handler *
489 get_proto_handler(const char *name)
491 struct proto_handler *proto;
493 if (!strcmp(name, "none"))
499 return avl_find_element(&handlers, name, proto, avl);
503 proto_dump_handlers(struct blob_buf *b)
505 struct proto_handler *p;
508 avl_for_each_element(&handlers, p, avl) {
509 c = blobmsg_open_table(b, p->name);
510 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
511 blobmsg_close_table(b, c);
516 proto_init_interface(struct interface *iface, struct blob_attr *attr)
518 const struct proto_handler *proto = iface->proto_handler;
519 struct interface_proto_state *state = NULL;
524 state = proto->attach(proto, iface, attr);
526 state = no_proto.attach(&no_proto, iface, attr);
527 state->cb = invalid_proto_handler;
530 state->handler = proto;
531 interface_set_proto_state(iface, state);
535 proto_attach_interface(struct interface *iface, const char *proto_name)
537 const struct proto_handler *proto = &no_proto;
540 proto = get_proto_handler(proto_name);
542 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
547 iface->proto_handler = proto;
551 interface_proto_event(struct interface_proto_state *proto,
552 enum interface_proto_cmd cmd, bool force)
554 enum interface_proto_event ev;
557 ret = proto->cb(proto, cmd, force);
558 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
562 case PROTO_CMD_SETUP:
565 case PROTO_CMD_TEARDOWN:
571 proto->proto_event(proto, ev);