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.
19 #include <arpa/inet.h>
23 #include "interface.h"
24 #include "interface-ip.h"
39 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
40 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
41 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
42 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
43 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
44 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
45 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
48 const struct config_param_list route_attr_list = {
49 .n_params = __ROUTE_MAX,
54 clear_if_addr(union if_addr *a, int mask)
56 int m_bytes = (mask + 7) / 8;
57 uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
58 uint8_t *p = (uint8_t *) a;
60 if (m_bytes < sizeof(a))
61 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
63 p[m_bytes - 1] &= ~m_clear;
67 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
69 union if_addr *p1, *p2;
71 p1 = alloca(sizeof(*a1));
72 p2 = alloca(sizeof(*a2));
74 memcpy(p1, a1, sizeof(*a1));
75 clear_if_addr(p1, mask);
76 memcpy(p2, a2, sizeof(*a2));
77 clear_if_addr(p2, mask);
79 return !memcmp(p1, p2, sizeof(*p1));
83 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
85 struct device_addr *addr;
87 vlist_for_each_element(&ip->addr, addr, node) {
91 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
94 if (!match_if_addr(&addr->addr, a, addr->mask))
104 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
105 bool v6, struct device_route **res)
107 struct device_route *route;
109 vlist_for_each_element(&ip->route, route, node) {
113 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
116 if (!match_if_addr(&route->addr, a, route->mask))
119 if (!*res || route->mask < (*res)->mask)
125 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
127 return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
128 __find_ip_addr_target(&iface->config_ip, a, v6);
132 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
133 bool v6, struct device_route **route)
135 __find_ip_route_target(&iface->proto_ip, a, v6, route);
136 __find_ip_route_target(&iface->config_ip, a, v6, route);
140 interface_ip_add_target_route(union if_addr *addr, bool v6)
142 struct interface *iface;
143 struct device_route *route, *r_next = NULL;
145 route = calloc(1, sizeof(*route));
149 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
150 route->mask = v6 ? 128 : 32;
151 memcpy(&route->addr, addr, v6 ? sizeof(addr->in6) : sizeof(addr->in));
153 vlist_for_each_element(&interfaces, iface, node) {
154 /* look for locally addressable target first */
155 if (interface_ip_find_addr_target(iface, addr, v6))
158 /* do not stop at the first route, let the lookup compare
159 * masks to find the best match */
160 interface_ip_find_route_target(iface, addr, v6, &r_next);
166 iface = r_next->iface;
167 memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
168 route->mtu = r_next->mtu;
169 route->metric = r_next->metric;
172 route->iface = iface;
173 vlist_add(&iface->host_routes, &route->node, &route->flags);
178 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
180 struct interface_ip_settings *ip;
181 struct blob_attr *tb[__ROUTE_MAX], *cur;
182 struct device_route *route;
183 int af = v6 ? AF_INET6 : AF_INET;
185 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
188 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
191 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
195 ip = &iface->config_ip;
197 ip = &iface->proto_ip;
200 route = calloc(1, sizeof(*route));
204 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
205 route->mask = v6 ? 128 : 32;
206 if ((cur = tb[ROUTE_MASK]) != NULL) {
207 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
208 if (route->mask > (v6 ? 128 : 32))
212 if ((cur = tb[ROUTE_TARGET]) != NULL) {
213 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
214 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
219 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
220 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
221 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
226 if ((cur = tb[ROUTE_METRIC]) != NULL) {
227 route->metric = blobmsg_get_u32(cur);
228 route->flags |= DEVROUTE_METRIC;
231 if ((cur = tb[ROUTE_MTU]) != NULL)
232 route->mtu = blobmsg_get_u32(cur);
234 vlist_add(&ip->route, &route->node, &route->flags);
242 addr_cmp(const void *k1, const void *k2, void *ptr)
244 return memcmp(k1, k2, sizeof(struct device_addr) -
245 offsetof(struct device_addr, flags));
249 route_cmp(const void *k1, const void *k2, void *ptr)
251 return memcmp(k1, k2, sizeof(struct device_route) -
252 offsetof(struct device_route, flags));
256 interface_update_proto_addr(struct vlist_tree *tree,
257 struct vlist_node *node_new,
258 struct vlist_node *node_old)
260 struct interface_ip_settings *ip;
261 struct interface *iface;
263 struct device_addr *a_new = NULL, *a_old = NULL;
265 struct device_route *route;
267 ip = container_of(tree, struct interface_ip_settings, addr);
269 dev = iface->l3_dev.dev;
272 a_new = container_of(node_new, struct device_addr, node);
274 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
278 uint32_t *a = (uint32_t *) &a_new->addr;
280 mask >>= a_new->mask;
281 a_new->broadcast = *a | mask;
286 a_old = container_of(node_old, struct device_addr, node);
288 if (a_new && a_old) {
291 if (a_old->flags != a_new->flags)
294 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
295 a_new->broadcast != a_old->broadcast)
300 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep)
301 system_del_address(dev, a_old);
306 a_new->enabled = true;
307 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) {
308 system_add_address(dev, a_new);
316 route = calloc(1, sizeof(*route));
317 route->iface = iface;
318 route->flags = a_new->flags | DEVADDR_KERNEL;
319 route->mask = a_new->mask;
320 memcpy(&route->addr, &a_new->addr, sizeof(route->addr));
321 clear_if_addr(&route->addr, route->mask);
323 system_del_route(dev, route);
325 route->flags &= ~DEVADDR_KERNEL;
326 route->metric = iface->metric;
327 vlist_add(&ip->route, &route->node, &route->flags);
331 enable_route(struct interface_ip_settings *ip, struct device_route *route)
333 if (ip->no_defaultroute && !route->mask)
340 interface_update_proto_route(struct vlist_tree *tree,
341 struct vlist_node *node_new,
342 struct vlist_node *node_old)
344 struct interface_ip_settings *ip;
345 struct interface *iface;
347 struct device_route *route_old, *route_new;
350 ip = container_of(tree, struct interface_ip_settings, route);
352 dev = iface->l3_dev.dev;
354 route_old = container_of(node_old, struct device_route, node);
355 route_new = container_of(node_new, struct device_route, node);
357 if (node_old && node_new)
358 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
361 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
362 system_del_route(dev, route_old);
367 bool _enabled = enable_route(ip, route_new);
369 if (!(route_new->flags & DEVROUTE_METRIC))
370 route_new->metric = iface->metric;
372 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
373 system_add_route(dev, route_new);
375 route_new->iface = iface;
376 route_new->enabled = _enabled;
381 interface_update_host_route(struct vlist_tree *tree,
382 struct vlist_node *node_new,
383 struct vlist_node *node_old)
385 struct interface *iface;
387 struct device_route *route_old, *route_new;
389 iface = container_of(tree, struct interface, host_routes);
390 dev = iface->l3_dev.dev;
392 route_old = container_of(node_old, struct device_route, node);
393 route_new = container_of(node_new, struct device_route, node);
396 system_del_route(dev, route_old);
401 system_add_route(dev, route_new);
405 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
407 struct dns_server *s;
409 s = calloc(1, sizeof(*s));
411 if (inet_pton(s->af, str, &s->addr.in))
415 if (inet_pton(s->af, str, &s->addr.in))
422 D(INTERFACE, "Add IPv%c DNS server: %s\n",
423 s->af == AF_INET6 ? '6' : '4', str);
424 vlist_simple_add(&ip->dns_servers, &s->node);
428 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
430 struct blob_attr *cur;
433 blobmsg_for_each_attr(cur, list, rem) {
434 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
437 if (!blobmsg_check_attr(cur, NULL))
440 interface_add_dns_server(ip, blobmsg_data(cur));
445 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
447 struct dns_search_domain *s;
448 int len = strlen(str);
450 s = calloc(1, sizeof(*s) + len + 1);
454 D(INTERFACE, "Add DNS search domain: %s\n", str);
455 memcpy(s->name, str, len);
456 vlist_simple_add(&ip->dns_search, &s->node);
460 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
462 struct blob_attr *cur;
465 blobmsg_for_each_attr(cur, list, rem) {
466 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
469 if (!blobmsg_check_attr(cur, NULL))
472 interface_add_dns_search_domain(ip, blobmsg_data(cur));
477 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
479 struct dns_server *s;
480 struct dns_search_domain *d;
484 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
485 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
489 fprintf(f, "nameserver %s\n", str);
492 vlist_simple_for_each_element(&ip->dns_search, d, node) {
493 fprintf(f, "search %s\n", d->name);
498 interface_write_resolv_conf(void)
500 struct interface *iface;
501 char *path = alloca(strlen(resolv_conf) + 5);
504 sprintf(path, "%s.tmp", resolv_conf);
506 f = fopen(path, "w");
508 D(INTERFACE, "Failed to open %s for writing\n", path);
512 vlist_for_each_element(&interfaces, iface, node) {
513 if (iface->state != IFS_UP)
516 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
517 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
518 vlist_simple_empty(&iface->config_ip.dns_search) &&
519 vlist_simple_empty(&iface->config_ip.dns_servers))
522 fprintf(f, "# Interface %s\n", iface->name);
523 write_resolv_conf_entries(f, &iface->config_ip);
524 if (!iface->proto_ip.no_dns)
525 write_resolv_conf_entries(f, &iface->proto_ip);
528 if (rename(path, resolv_conf) < 0) {
529 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
534 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
536 struct device_addr *addr;
537 struct device_route *route;
540 ip->enabled = enabled;
541 dev = ip->iface->l3_dev.dev;
545 vlist_for_each_element(&ip->addr, addr, node) {
546 if (addr->enabled == enabled)
550 system_add_address(dev, addr);
552 system_del_address(dev, addr);
553 addr->enabled = enabled;
556 vlist_for_each_element(&ip->route, route, node) {
557 bool _enabled = enabled;
559 if (!enable_route(ip, route))
562 if (route->enabled == _enabled)
566 if (!(route->flags & DEVROUTE_METRIC))
567 route->metric = ip->iface->metric;
569 system_add_route(dev, route);
571 system_del_route(dev, route);
572 route->enabled = _enabled;
577 interface_ip_update_start(struct interface_ip_settings *ip)
579 if (ip != &ip->iface->config_ip) {
580 vlist_simple_update(&ip->dns_servers);
581 vlist_simple_update(&ip->dns_search);
583 vlist_update(&ip->route);
584 vlist_update(&ip->addr);
588 interface_ip_update_complete(struct interface_ip_settings *ip)
590 vlist_simple_flush(&ip->dns_servers);
591 vlist_simple_flush(&ip->dns_search);
592 vlist_flush(&ip->route);
593 vlist_flush(&ip->addr);
597 interface_ip_flush(struct interface_ip_settings *ip)
599 if (ip == &ip->iface->proto_ip)
600 vlist_flush_all(&ip->iface->host_routes);
601 vlist_simple_flush_all(&ip->dns_servers);
602 vlist_simple_flush_all(&ip->dns_search);
603 vlist_flush_all(&ip->route);
604 vlist_flush_all(&ip->addr);
608 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
612 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
613 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
614 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
615 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
619 interface_ip_init(struct interface *iface)
621 __interface_ip_init(&iface->proto_ip, iface);
622 __interface_ip_init(&iface->config_ip, iface);
623 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);