10 #include "interface.h"
11 #include "interface-ip.h"
27 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
28 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
29 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
30 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
31 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
32 [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
33 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
34 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
37 const struct config_param_list route_attr_list = {
38 .n_params = __ROUTE_MAX,
43 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
45 struct interface_ip_settings *ip;
46 struct blob_attr *tb[__ROUTE_MAX], *cur;
47 struct device_route *route;
48 int af = v6 ? AF_INET6 : AF_INET;
51 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
53 if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
57 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
60 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
64 ip = &iface->config_ip;
67 ip = &iface->proto_ip;
70 route = calloc(1, sizeof(*route));
74 route->mask = v6 ? 128 : 32;
75 if ((cur = tb[ROUTE_MASK]) != NULL) {
76 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
77 if (route->mask > (v6 ? 128 : 32))
81 if ((cur = tb[ROUTE_TARGET]) != NULL) {
82 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
83 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
88 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
89 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
90 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
95 if ((cur = tb[ROUTE_METRIC]) != NULL)
96 route->metric = blobmsg_get_u32(cur);
98 if ((cur = tb[ROUTE_MTU]) != NULL)
99 route->mtu = blobmsg_get_u32(cur);
101 if (!config && (cur = tb[ROUTE_DEVICE]) != NULL)
102 route->device = device_get(blobmsg_data(cur), true);
104 vlist_add(&ip->route, &route->node);
112 addr_cmp(const void *k1, const void *k2, void *ptr)
114 return memcmp(k1, k2, sizeof(struct device_addr) -
115 offsetof(struct device_addr, mask));
119 route_cmp(const void *k1, const void *k2, void *ptr)
121 return memcmp(k1, k2, sizeof(struct device_route) -
122 offsetof(struct device_route, mask));
126 interface_update_proto_addr(struct vlist_tree *tree,
127 struct vlist_node *node_new,
128 struct vlist_node *node_old)
130 struct interface_ip_settings *ip;
131 struct interface *iface;
133 struct device_addr *addr;
136 ip = container_of(tree, struct interface_ip_settings, addr);
138 dev = iface->l3_dev->dev;
140 if (node_old && node_new)
144 addr = container_of(node_old, struct device_addr, node);
145 if (!(addr->flags & DEVADDR_EXTERNAL) && addr->enabled && !keep)
146 system_del_address(dev, addr);
151 addr = container_of(node_new, struct device_addr, node);
152 if (!(addr->flags & DEVADDR_EXTERNAL) && !keep)
153 system_add_address(dev, addr);
154 addr->enabled = true;
159 interface_update_proto_route(struct vlist_tree *tree,
160 struct vlist_node *node_new,
161 struct vlist_node *node_old)
163 struct interface_ip_settings *ip;
164 struct interface *iface;
166 struct device_route *route_old, *route_new;
169 ip = container_of(tree, struct interface_ip_settings, route);
171 dev = iface->l3_dev->dev;
173 route_old = container_of(node_old, struct device_route, node);
174 route_new = container_of(node_new, struct device_route, node);
176 if (node_old && node_new)
177 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
180 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
181 system_del_route(dev, route_old);
186 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep)
187 system_add_route(dev, route_new);
188 route_new->enabled = true;
193 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
195 struct dns_server *s;
197 s = calloc(1, sizeof(*s));
199 if (inet_pton(s->af, str, &s->addr.in))
203 if (inet_pton(s->af, str, &s->addr.in))
210 D(INTERFACE, "Add IPv%c DNS server: %s\n",
211 s->af == AF_INET6 ? '6' : '4', str);
212 vlist_simple_add(&ip->dns_servers, &s->node);
216 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
218 struct blob_attr *cur;
221 blobmsg_for_each_attr(cur, list, rem) {
222 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
225 if (!blobmsg_check_attr(cur, NULL))
228 interface_add_dns_server(ip, blobmsg_data(cur));
233 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
235 struct dns_search_domain *s;
236 int len = strlen(str);
238 s = calloc(1, sizeof(*s) + len + 1);
242 D(INTERFACE, "Add DNS search domain: %s\n", str);
243 memcpy(s->name, str, len);
244 vlist_simple_add(&ip->dns_search, &s->node);
248 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
250 struct blob_attr *cur;
253 blobmsg_for_each_attr(cur, list, rem) {
254 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
257 if (!blobmsg_check_attr(cur, NULL))
260 interface_add_dns_search_domain(ip, blobmsg_data(cur));
265 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
267 struct dns_server *s;
268 struct dns_search_domain *d;
272 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
273 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
277 fprintf(f, "nameserver %s\n", str);
280 vlist_simple_for_each_element(&ip->dns_search, d, node) {
281 fprintf(f, "search %s\n", d->name);
286 interface_write_resolv_conf(void)
288 struct interface *iface;
289 char *path = alloca(strlen(resolv_conf) + 5);
292 sprintf(path, "%s.tmp", resolv_conf);
294 f = fopen(path, "w");
296 D(INTERFACE, "Failed to open %s for writing\n", path);
300 vlist_for_each_element(&interfaces, iface, node) {
301 if (iface->state != IFS_UP)
304 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
305 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
306 vlist_simple_empty(&iface->config_ip.dns_search) &&
307 vlist_simple_empty(&iface->config_ip.dns_servers))
310 fprintf(f, "# Interface %s\n", iface->name);
311 write_resolv_conf_entries(f, &iface->config_ip);
312 write_resolv_conf_entries(f, &iface->proto_ip);
315 if (rename(path, resolv_conf) < 0) {
316 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
321 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
323 struct device_addr *addr;
324 struct device_route *route;
327 ip->enabled = enabled;
328 dev = ip->iface->l3_dev->dev;
332 vlist_for_each_element(&ip->addr, addr, node) {
333 if (addr->enabled == enabled)
337 system_add_address(dev, addr);
339 system_del_address(dev, addr);
340 addr->enabled = enabled;
343 vlist_for_each_element(&ip->route, route, node) {
344 if (route->enabled == enabled)
348 system_add_route(dev, route);
350 system_del_route(dev, route);
351 route->enabled = enabled;
356 interface_ip_update_start(struct interface_ip_settings *ip)
358 vlist_simple_update(&ip->dns_servers);
359 vlist_simple_update(&ip->dns_search);
360 vlist_update(&ip->route);
361 vlist_update(&ip->addr);
365 interface_ip_update_complete(struct interface_ip_settings *ip)
367 vlist_simple_flush(&ip->dns_servers);
368 vlist_simple_flush(&ip->dns_search);
369 vlist_flush(&ip->route);
370 vlist_flush(&ip->addr);
374 interface_ip_flush(struct interface_ip_settings *ip)
376 vlist_simple_flush_all(&ip->dns_servers);
377 vlist_simple_flush_all(&ip->dns_search);
378 vlist_flush_all(&ip->route);
379 vlist_flush_all(&ip->addr);
383 interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
387 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
388 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
389 vlist_init(&ip->route, route_cmp, interface_update_proto_route,
390 struct device_route, node, mask);
391 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr,
392 struct device_addr, node, mask);