2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
22 #include "interface.h"
23 #include "interface-ip.h"
26 static struct avl_tree handlers;
40 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
41 [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
42 [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
43 [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
44 [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
45 [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
46 [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
47 [OPT_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
48 [OPT_DNS_SEARCH] = { .name = "dns_search", .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_DNS] = { .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,
70 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
71 [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
72 [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
73 [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
77 parse_netmask_string(const char *str, bool v6)
83 if (!strchr(str, '.')) {
84 ret = strtoul(str, &err, 0);
94 if (inet_aton(str, &addr) != 1)
97 return 32 - fls(~(ntohl(addr.s_addr)));
104 split_netmask(char *str, unsigned int *netmask, bool v6)
106 char *delim = strchr(str, '/');
111 *netmask = parse_netmask_string(delim, v6);
117 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
119 char *astr = alloca(strlen(str) + 1);
122 if (!split_netmask(astr, netmask, af == AF_INET6))
125 if (af == AF_INET6) {
133 return inet_pton(af, astr, addr);
136 static struct device_addr *
137 alloc_device_addr(bool v6, bool ext)
139 struct device_addr *addr;
141 addr = calloc(1, sizeof(*addr));
142 addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
144 addr->flags |= DEVADDR_EXTERNAL;
150 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
151 bool ext, uint32_t broadcast)
153 struct device_addr *addr;
154 int af = v6 ? AF_INET6 : AF_INET;
156 addr = alloc_device_addr(v6, ext);
161 if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
162 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
168 addr->broadcast = broadcast;
170 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
175 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
176 bool v6, int netmask, bool ext, uint32_t broadcast)
178 struct blob_attr *cur;
182 blobmsg_for_each_attr(cur, attr, rem) {
183 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
187 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
195 static struct device_addr *
196 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
198 struct device_addr *addr;
199 struct blob_attr *tb[__ADDR_MAX];
200 struct blob_attr *cur;
202 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
205 addr = alloc_device_addr(v6, ext);
209 blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
211 addr->mask = v6 ? 128 : 32;
212 if ((cur = tb[ADDR_MASK])) {
213 unsigned int new_mask;
215 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
216 if (new_mask > addr->mask)
219 addr->mask = new_mask;
222 cur = tb[ADDR_IPADDR];
226 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
229 if (!v6 && (cur = tb[ADDR_BROADCAST])) {
230 if (!inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
242 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
245 struct device_addr *addr;
246 struct blob_attr *cur;
250 blobmsg_for_each_attr(cur, attr, rem) {
251 addr = parse_address_item(cur, v6, ext);
256 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
263 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
265 struct device_route *route;
266 const char *str = blobmsg_data(attr);
267 int af = v6 ? AF_INET6 : AF_INET;
269 route = calloc(1, sizeof(*route));
270 if (!inet_pton(af, str, &route->nexthop)) {
271 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
277 route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
278 vlist_add(&iface->proto_ip.route, &route->node, &route->flags);
284 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
286 struct blob_attr *tb[__OPT_MAX];
287 struct blob_attr *cur;
289 unsigned int netmask = 32;
290 int n_v4 = 0, n_v6 = 0;
291 struct in_addr bcast = {};
293 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
295 if ((cur = tb[OPT_NETMASK])) {
296 netmask = parse_netmask_string(blobmsg_data(cur), false);
298 error = "INVALID_NETMASK";
303 if ((cur = tb[OPT_BROADCAST])) {
304 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
305 error = "INVALID_BROADCAST";
310 if ((cur = tb[OPT_IPADDR]))
311 n_v4 = parse_static_address_option(iface, cur, false,
312 netmask, false, bcast.s_addr);
314 if ((cur = tb[OPT_IP6ADDR]))
315 n_v6 = parse_static_address_option(iface, cur, true,
318 if (!n_v4 && !n_v6) {
319 error = "NO_ADDRESS";
323 if (n_v4 < 0 || n_v6 < 0)
326 if ((cur = tb[OPT_GATEWAY])) {
327 if (n_v4 && !parse_gateway_option(iface, cur, false))
331 if ((cur = tb[OPT_IP6GW])) {
332 if (n_v6 && !parse_gateway_option(iface, cur, true))
336 if ((cur = tb[OPT_DNS]))
337 interface_add_dns_server_list(&iface->proto_ip, cur);
339 if ((cur = tb[OPT_DNS_SEARCH]))
340 interface_add_dns_search_list(&iface->proto_ip, cur);
345 interface_add_error(iface, "proto", error, NULL, 0);
351 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
353 struct blob_attr *tb[__OPT_MAX];
354 struct blob_attr *cur;
356 int n_v4 = 0, n_v6 = 0;
358 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
360 if ((cur = tb[OPT_IPADDR]))
361 n_v4 = parse_address_list(iface, cur, false, ext);
363 if ((cur = tb[OPT_IP6ADDR]))
364 n_v6 = parse_address_list(iface, cur, true, ext);
366 if (!n_v4 && !n_v6) {
367 error = "NO_ADDRESS";
371 if (n_v4 < 0 || n_v6 < 0)
374 if ((cur = tb[OPT_GATEWAY])) {
375 if (n_v4 && !parse_gateway_option(iface, cur, false))
379 if ((cur = tb[OPT_IP6GW])) {
380 if (n_v6 && !parse_gateway_option(iface, cur, true))
384 if ((cur = tb[OPT_DNS]))
385 interface_add_dns_server_list(&iface->proto_ip, cur);
387 if ((cur = tb[OPT_DNS_SEARCH]))
388 interface_add_dns_search_list(&iface->proto_ip, cur);
393 interface_add_error(iface, "proto", error, NULL, 0);
398 void add_proto_handler(struct proto_handler *p)
401 avl_init(&handlers, avl_strcmp, false, NULL);
406 p->avl.key = p->name;
407 avl_insert(&handlers, &p->avl);
411 default_proto_free(struct interface_proto_state *proto)
417 invalid_proto_handler(struct interface_proto_state *proto,
418 enum interface_proto_cmd cmd, bool force)
424 no_proto_handler(struct interface_proto_state *proto,
425 enum interface_proto_cmd cmd, bool force)
430 static struct interface_proto_state *
431 default_proto_attach(const struct proto_handler *h,
432 struct interface *iface, struct blob_attr *attr)
434 struct interface_proto_state *proto;
436 proto = calloc(1, sizeof(*proto));
437 proto->free = default_proto_free;
438 proto->cb = no_proto_handler;
443 static const struct proto_handler no_proto = {
445 .flags = PROTO_FLAG_IMMEDIATE,
446 .attach = default_proto_attach,
449 static const struct proto_handler *
450 get_proto_handler(const char *name)
452 struct proto_handler *proto;
454 if (!strcmp(name, "none"))
460 return avl_find_element(&handlers, name, proto, avl);
464 proto_init_interface(struct interface *iface, struct blob_attr *attr)
466 const struct proto_handler *proto = iface->proto_handler;
467 struct interface_proto_state *state = NULL;
472 state = proto->attach(proto, iface, attr);
474 state = no_proto.attach(&no_proto, iface, attr);
475 state->cb = invalid_proto_handler;
478 state->handler = proto;
479 interface_set_proto_state(iface, state);
483 proto_attach_interface(struct interface *iface, const char *proto_name)
485 const struct proto_handler *proto = &no_proto;
488 proto = get_proto_handler(proto_name);
490 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
495 iface->proto_handler = proto;
499 interface_proto_event(struct interface_proto_state *proto,
500 enum interface_proto_cmd cmd, bool force)
502 enum interface_proto_event ev;
505 ret = proto->cb(proto, cmd, force);
506 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
510 case PROTO_CMD_SETUP:
513 case PROTO_CMD_TEARDOWN:
519 proto->proto_event(proto, ev);