move route interface assignment to the vlist update
[project/netifd.git] / interface-ip.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #include <arpa/inet.h>
7
8 #include "netifd.h"
9 #include "device.h"
10 #include "interface.h"
11 #include "interface-ip.h"
12 #include "proto.h"
13 #include "ubus.h"
14 #include "system.h"
15
16 enum {
17 ROUTE_INTERFACE,
18 ROUTE_TARGET,
19 ROUTE_MASK,
20 ROUTE_GATEWAY,
21 ROUTE_METRIC,
22 ROUTE_MTU,
23 __ROUTE_MAX
24 };
25
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 },
33 };
34
35 const struct config_param_list route_attr_list = {
36 .n_params = __ROUTE_MAX,
37 .params = route_attr,
38 };
39
40 void
41 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
42 {
43 struct interface_ip_settings *ip;
44 struct blob_attr *tb[__ROUTE_MAX], *cur;
45 struct device_route *route;
46 int af = v6 ? AF_INET6 : AF_INET;
47
48 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
49
50 if (!iface) {
51 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
52 return;
53
54 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
55 if (!iface)
56 return;
57
58 ip = &iface->config_ip;
59 } else {
60 ip = &iface->proto_ip;
61 }
62
63 route = calloc(1, sizeof(*route));
64 if (!route)
65 return;
66
67 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
68 route->mask = v6 ? 128 : 32;
69 if ((cur = tb[ROUTE_MASK]) != NULL) {
70 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
71 if (route->mask > (v6 ? 128 : 32))
72 goto error;
73 }
74
75 if ((cur = tb[ROUTE_TARGET]) != NULL) {
76 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
77 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
78 goto error;
79 }
80 }
81
82 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
83 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
84 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
85 goto error;
86 }
87 }
88
89 if ((cur = tb[ROUTE_METRIC]) != NULL) {
90 route->metric = blobmsg_get_u32(cur);
91 route->flags |= DEVROUTE_METRIC;
92 }
93
94 if ((cur = tb[ROUTE_MTU]) != NULL)
95 route->mtu = blobmsg_get_u32(cur);
96
97 vlist_add(&ip->route, &route->node, &route->flags);
98 return;
99
100 error:
101 free(route);
102 }
103
104 static int
105 addr_cmp(const void *k1, const void *k2, void *ptr)
106 {
107 return memcmp(k1, k2, sizeof(struct device_addr) -
108 offsetof(struct device_addr, flags));
109 }
110
111 static int
112 route_cmp(const void *k1, const void *k2, void *ptr)
113 {
114 return memcmp(k1, k2, sizeof(struct device_route) -
115 offsetof(struct device_route, flags));
116 }
117
118 static void
119 interface_update_proto_addr(struct vlist_tree *tree,
120 struct vlist_node *node_new,
121 struct vlist_node *node_old)
122 {
123 struct interface_ip_settings *ip;
124 struct interface *iface;
125 struct device *dev;
126 struct device_addr *a_new = NULL, *a_old = NULL;
127 bool keep = false;
128
129 ip = container_of(tree, struct interface_ip_settings, addr);
130 iface = ip->iface;
131 dev = iface->l3_dev.dev;
132
133 if (node_new) {
134 a_new = container_of(node_new, struct device_addr, node);
135
136 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
137 !a_new->broadcast) {
138
139 uint32_t mask = ~0;
140 uint32_t *a = (uint32_t *) &a_new->addr;
141
142 mask >>= a_new->mask;
143 a_new->broadcast = *a | mask;
144 }
145 }
146
147 if (node_old)
148 a_old = container_of(node_old, struct device_addr, node);
149
150 if (a_new && a_old) {
151 keep = true;
152
153 if (a_old->flags != a_new->flags)
154 keep = false;
155
156 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
157 a_new->broadcast != a_old->broadcast)
158 keep = false;
159 }
160
161 if (node_old) {
162 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep)
163 system_del_address(dev, a_old);
164 free(a_old);
165 }
166
167 if (node_new) {
168 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep)
169 system_add_address(dev, a_new);
170 a_new->enabled = true;
171 }
172 }
173
174 static bool
175 enable_route(struct interface_ip_settings *ip, struct device_route *route)
176 {
177 if (ip->no_defaultroute && !route->mask)
178 return false;
179
180 return ip->enabled;
181 }
182
183 static void
184 interface_update_proto_route(struct vlist_tree *tree,
185 struct vlist_node *node_new,
186 struct vlist_node *node_old)
187 {
188 struct interface_ip_settings *ip;
189 struct interface *iface;
190 struct device *dev;
191 struct device_route *route_old, *route_new;
192 bool keep = false;
193
194 ip = container_of(tree, struct interface_ip_settings, route);
195 iface = ip->iface;
196 dev = iface->l3_dev.dev;
197
198 route_old = container_of(node_old, struct device_route, node);
199 route_new = container_of(node_new, struct device_route, node);
200
201 if (node_old && node_new)
202 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
203
204 if (node_old) {
205 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
206 system_del_route(dev, route_old);
207 free(route_old);
208 }
209
210 if (node_new) {
211 bool _enabled = enable_route(ip, route_new);
212
213 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
214 system_add_route(dev, route_new);
215
216 route_new->iface = iface;
217 route_new->enabled = _enabled;
218 }
219 }
220
221 void
222 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
223 {
224 struct dns_server *s;
225
226 s = calloc(1, sizeof(*s));
227 s->af = AF_INET;
228 if (inet_pton(s->af, str, &s->addr.in))
229 goto add;
230
231 s->af = AF_INET6;
232 if (inet_pton(s->af, str, &s->addr.in))
233 goto add;
234
235 free(s);
236 return;
237
238 add:
239 D(INTERFACE, "Add IPv%c DNS server: %s\n",
240 s->af == AF_INET6 ? '6' : '4', str);
241 vlist_simple_add(&ip->dns_servers, &s->node);
242 }
243
244 void
245 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
246 {
247 struct blob_attr *cur;
248 int rem;
249
250 blobmsg_for_each_attr(cur, list, rem) {
251 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
252 continue;
253
254 if (!blobmsg_check_attr(cur, NULL))
255 continue;
256
257 interface_add_dns_server(ip, blobmsg_data(cur));
258 }
259 }
260
261 static void
262 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
263 {
264 struct dns_search_domain *s;
265 int len = strlen(str);
266
267 s = calloc(1, sizeof(*s) + len + 1);
268 if (!s)
269 return;
270
271 D(INTERFACE, "Add DNS search domain: %s\n", str);
272 memcpy(s->name, str, len);
273 vlist_simple_add(&ip->dns_search, &s->node);
274 }
275
276 void
277 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
278 {
279 struct blob_attr *cur;
280 int rem;
281
282 blobmsg_for_each_attr(cur, list, rem) {
283 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
284 continue;
285
286 if (!blobmsg_check_attr(cur, NULL))
287 continue;
288
289 interface_add_dns_search_domain(ip, blobmsg_data(cur));
290 }
291 }
292
293 static void
294 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
295 {
296 struct dns_server *s;
297 struct dns_search_domain *d;
298 const char *str;
299 char buf[32];
300
301 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
302 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
303 if (!str)
304 continue;
305
306 fprintf(f, "nameserver %s\n", str);
307 }
308
309 vlist_simple_for_each_element(&ip->dns_search, d, node) {
310 fprintf(f, "search %s\n", d->name);
311 }
312 }
313
314 void
315 interface_write_resolv_conf(void)
316 {
317 struct interface *iface;
318 char *path = alloca(strlen(resolv_conf) + 5);
319 FILE *f;
320
321 sprintf(path, "%s.tmp", resolv_conf);
322 unlink(path);
323 f = fopen(path, "w");
324 if (!f) {
325 D(INTERFACE, "Failed to open %s for writing\n", path);
326 return;
327 }
328
329 vlist_for_each_element(&interfaces, iface, node) {
330 if (iface->state != IFS_UP)
331 continue;
332
333 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
334 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
335 vlist_simple_empty(&iface->config_ip.dns_search) &&
336 vlist_simple_empty(&iface->config_ip.dns_servers))
337 continue;
338
339 fprintf(f, "# Interface %s\n", iface->name);
340 write_resolv_conf_entries(f, &iface->config_ip);
341 write_resolv_conf_entries(f, &iface->proto_ip);
342 }
343 fclose(f);
344 if (rename(path, resolv_conf) < 0) {
345 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
346 unlink(path);
347 }
348 }
349
350 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
351 {
352 struct device_addr *addr;
353 struct device_route *route;
354 struct device *dev;
355
356 ip->enabled = enabled;
357 dev = ip->iface->l3_dev.dev;
358 if (!dev)
359 return;
360
361 vlist_for_each_element(&ip->addr, addr, node) {
362 if (addr->enabled == enabled)
363 continue;
364
365 if (enabled)
366 system_add_address(dev, addr);
367 else
368 system_del_address(dev, addr);
369 addr->enabled = enabled;
370 }
371
372 vlist_for_each_element(&ip->route, route, node) {
373 bool _enabled = enabled;
374
375 if (!enable_route(ip, route))
376 _enabled = false;
377
378 if (route->enabled == _enabled)
379 continue;
380
381 if (_enabled) {
382 if (!(route->flags & DEVROUTE_METRIC))
383 route->metric = ip->iface->metric;
384
385 system_add_route(dev, route);
386 } else
387 system_del_route(dev, route);
388 route->enabled = _enabled;
389 }
390 }
391
392 void
393 interface_ip_update_start(struct interface_ip_settings *ip)
394 {
395 vlist_simple_update(&ip->dns_servers);
396 vlist_simple_update(&ip->dns_search);
397 vlist_update(&ip->route);
398 vlist_update(&ip->addr);
399 }
400
401 void
402 interface_ip_update_complete(struct interface_ip_settings *ip)
403 {
404 vlist_simple_flush(&ip->dns_servers);
405 vlist_simple_flush(&ip->dns_search);
406 vlist_flush(&ip->route);
407 vlist_flush(&ip->addr);
408 }
409
410 void
411 interface_ip_flush(struct interface_ip_settings *ip)
412 {
413 vlist_simple_flush_all(&ip->dns_servers);
414 vlist_simple_flush_all(&ip->dns_search);
415 vlist_flush_all(&ip->route);
416 vlist_flush_all(&ip->addr);
417 }
418
419 void
420 interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
421 {
422 ip->iface = iface;
423 ip->enabled = true;
424 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
425 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
426 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
427 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
428 }