10 #include "interface.h"
11 #include "interface-ip.h"
26 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
27 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
28 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
29 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
30 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
31 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
32 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
35 const struct config_param_list route_attr_list = {
36 .n_params = __ROUTE_MAX,
41 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
44 int m_bytes = (mask + 7) / 8;
45 uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
50 memcpy(p1, a1, m_bytes);
51 memcpy(p2, a2, m_bytes);
53 p1[m_bytes - 1] &= ~m_clear;
54 p2[m_bytes - 1] &= ~m_clear;
56 return !memcmp(p1, p2, m_bytes);
60 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
62 struct device_addr *addr;
64 vlist_for_each_element(&ip->addr, addr, node) {
68 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
71 if (!match_if_addr(&addr->addr, a, addr->mask))
81 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
82 bool v6, struct device_route **res)
84 struct device_route *route;
86 vlist_for_each_element(&ip->route, route, node) {
90 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
93 if (!match_if_addr(&route->addr, a, route->mask))
96 if (!*res || route->mask < (*res)->mask)
102 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
104 return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
105 __find_ip_addr_target(&iface->config_ip, a, v6);
109 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
110 bool v6, struct device_route **route)
112 __find_ip_route_target(&iface->proto_ip, a, v6, route);
113 __find_ip_route_target(&iface->config_ip, a, v6, route);
117 interface_ip_add_target_route(union if_addr *addr, bool v6)
119 struct interface *iface;
120 struct device_route *route, *r_next = NULL;
122 route = calloc(1, sizeof(*route));
126 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
127 route->mask = v6 ? 128 : 32;
128 memcpy(&route->addr, addr, v6 ? sizeof(addr->in6) : sizeof(addr->in));
130 vlist_for_each_element(&interfaces, iface, node) {
131 /* look for locally addressable target first */
132 if (interface_ip_find_addr_target(iface, addr, v6))
135 /* do not stop at the first route, let the lookup compare
136 * masks to find the best match */
137 interface_ip_find_route_target(iface, addr, v6, &r_next);
143 iface = r_next->iface;
144 memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
145 route->mtu = r_next->mtu;
146 route->metric = r_next->metric;
149 route->iface = iface;
150 vlist_add(&iface->host_routes, &route->node, &route->flags);
155 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
157 struct interface_ip_settings *ip;
158 struct blob_attr *tb[__ROUTE_MAX], *cur;
159 struct device_route *route;
160 int af = v6 ? AF_INET6 : AF_INET;
162 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
165 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
168 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
172 ip = &iface->config_ip;
174 ip = &iface->proto_ip;
177 route = calloc(1, sizeof(*route));
181 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
182 route->mask = v6 ? 128 : 32;
183 if ((cur = tb[ROUTE_MASK]) != NULL) {
184 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
185 if (route->mask > (v6 ? 128 : 32))
189 if ((cur = tb[ROUTE_TARGET]) != NULL) {
190 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
191 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
196 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
197 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
198 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
203 if ((cur = tb[ROUTE_METRIC]) != NULL) {
204 route->metric = blobmsg_get_u32(cur);
205 route->flags |= DEVROUTE_METRIC;
208 if ((cur = tb[ROUTE_MTU]) != NULL)
209 route->mtu = blobmsg_get_u32(cur);
211 vlist_add(&ip->route, &route->node, &route->flags);
219 addr_cmp(const void *k1, const void *k2, void *ptr)
221 return memcmp(k1, k2, sizeof(struct device_addr) -
222 offsetof(struct device_addr, flags));
226 route_cmp(const void *k1, const void *k2, void *ptr)
228 return memcmp(k1, k2, sizeof(struct device_route) -
229 offsetof(struct device_route, flags));
233 interface_update_proto_addr(struct vlist_tree *tree,
234 struct vlist_node *node_new,
235 struct vlist_node *node_old)
237 struct interface_ip_settings *ip;
238 struct interface *iface;
240 struct device_addr *a_new = NULL, *a_old = NULL;
243 ip = container_of(tree, struct interface_ip_settings, addr);
245 dev = iface->l3_dev.dev;
248 a_new = container_of(node_new, struct device_addr, node);
250 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
254 uint32_t *a = (uint32_t *) &a_new->addr;
256 mask >>= a_new->mask;
257 a_new->broadcast = *a | mask;
262 a_old = container_of(node_old, struct device_addr, node);
264 if (a_new && a_old) {
267 if (a_old->flags != a_new->flags)
270 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
271 a_new->broadcast != a_old->broadcast)
276 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep)
277 system_del_address(dev, a_old);
282 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep)
283 system_add_address(dev, a_new);
284 a_new->enabled = true;
289 enable_route(struct interface_ip_settings *ip, struct device_route *route)
291 if (ip->no_defaultroute && !route->mask)
298 interface_update_proto_route(struct vlist_tree *tree,
299 struct vlist_node *node_new,
300 struct vlist_node *node_old)
302 struct interface_ip_settings *ip;
303 struct interface *iface;
305 struct device_route *route_old, *route_new;
308 ip = container_of(tree, struct interface_ip_settings, route);
310 dev = iface->l3_dev.dev;
312 route_old = container_of(node_old, struct device_route, node);
313 route_new = container_of(node_new, struct device_route, node);
315 if (node_old && node_new)
316 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
319 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
320 system_del_route(dev, route_old);
325 bool _enabled = enable_route(ip, route_new);
327 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
328 system_add_route(dev, route_new);
330 route_new->iface = iface;
331 route_new->enabled = _enabled;
336 interface_update_host_route(struct vlist_tree *tree,
337 struct vlist_node *node_new,
338 struct vlist_node *node_old)
340 struct interface *iface;
342 struct device_route *route_old, *route_new;
344 iface = container_of(tree, struct interface, host_routes);
345 dev = iface->l3_dev.dev;
347 route_old = container_of(node_old, struct device_route, node);
348 route_new = container_of(node_new, struct device_route, node);
351 system_del_route(dev, route_old);
356 system_add_route(dev, route_new);
360 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
362 struct dns_server *s;
364 s = calloc(1, sizeof(*s));
366 if (inet_pton(s->af, str, &s->addr.in))
370 if (inet_pton(s->af, str, &s->addr.in))
377 D(INTERFACE, "Add IPv%c DNS server: %s\n",
378 s->af == AF_INET6 ? '6' : '4', str);
379 vlist_simple_add(&ip->dns_servers, &s->node);
383 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
385 struct blob_attr *cur;
388 blobmsg_for_each_attr(cur, list, rem) {
389 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
392 if (!blobmsg_check_attr(cur, NULL))
395 interface_add_dns_server(ip, blobmsg_data(cur));
400 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
402 struct dns_search_domain *s;
403 int len = strlen(str);
405 s = calloc(1, sizeof(*s) + len + 1);
409 D(INTERFACE, "Add DNS search domain: %s\n", str);
410 memcpy(s->name, str, len);
411 vlist_simple_add(&ip->dns_search, &s->node);
415 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
417 struct blob_attr *cur;
420 blobmsg_for_each_attr(cur, list, rem) {
421 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
424 if (!blobmsg_check_attr(cur, NULL))
427 interface_add_dns_search_domain(ip, blobmsg_data(cur));
432 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
434 struct dns_server *s;
435 struct dns_search_domain *d;
439 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
440 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
444 fprintf(f, "nameserver %s\n", str);
447 vlist_simple_for_each_element(&ip->dns_search, d, node) {
448 fprintf(f, "search %s\n", d->name);
453 interface_write_resolv_conf(void)
455 struct interface *iface;
456 char *path = alloca(strlen(resolv_conf) + 5);
459 sprintf(path, "%s.tmp", resolv_conf);
461 f = fopen(path, "w");
463 D(INTERFACE, "Failed to open %s for writing\n", path);
467 vlist_for_each_element(&interfaces, iface, node) {
468 if (iface->state != IFS_UP)
471 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
472 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
473 vlist_simple_empty(&iface->config_ip.dns_search) &&
474 vlist_simple_empty(&iface->config_ip.dns_servers))
477 fprintf(f, "# Interface %s\n", iface->name);
478 write_resolv_conf_entries(f, &iface->config_ip);
479 write_resolv_conf_entries(f, &iface->proto_ip);
482 if (rename(path, resolv_conf) < 0) {
483 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
488 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
490 struct device_addr *addr;
491 struct device_route *route;
494 ip->enabled = enabled;
495 dev = ip->iface->l3_dev.dev;
499 vlist_for_each_element(&ip->addr, addr, node) {
500 if (addr->enabled == enabled)
504 system_add_address(dev, addr);
506 system_del_address(dev, addr);
507 addr->enabled = enabled;
510 vlist_for_each_element(&ip->route, route, node) {
511 bool _enabled = enabled;
513 if (!enable_route(ip, route))
516 if (route->enabled == _enabled)
520 if (!(route->flags & DEVROUTE_METRIC))
521 route->metric = ip->iface->metric;
523 system_add_route(dev, route);
525 system_del_route(dev, route);
526 route->enabled = _enabled;
531 interface_ip_update_start(struct interface_ip_settings *ip)
533 vlist_simple_update(&ip->dns_servers);
534 vlist_simple_update(&ip->dns_search);
535 vlist_update(&ip->route);
536 vlist_update(&ip->addr);
540 interface_ip_update_complete(struct interface_ip_settings *ip)
542 vlist_simple_flush(&ip->dns_servers);
543 vlist_simple_flush(&ip->dns_search);
544 vlist_flush(&ip->route);
545 vlist_flush(&ip->addr);
549 interface_ip_flush(struct interface_ip_settings *ip)
551 if (ip == &ip->iface->proto_ip)
552 vlist_flush_all(&ip->iface->host_routes);
553 vlist_simple_flush_all(&ip->dns_servers);
554 vlist_simple_flush_all(&ip->dns_search);
555 vlist_flush_all(&ip->route);
556 vlist_flush_all(&ip->addr);
560 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
564 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
565 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
566 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
567 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
571 interface_ip_init(struct interface *iface)
573 __interface_ip_init(&iface->proto_ip, iface);
574 __interface_ip_init(&iface->config_ip, iface);
575 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);