bridge: implement config reload support, preserves hotplug interfaces and adds fast...
[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->mask = v6 ? 128 : 32;
68         if ((cur = tb[ROUTE_MASK]) != NULL) {
69                 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
70                 if (route->mask > (v6 ? 128 : 32))
71                         goto error;
72         }
73
74         if ((cur = tb[ROUTE_TARGET]) != NULL) {
75                 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
76                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
77                         goto error;
78                 }
79         }
80
81         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
82                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
83                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
84                         goto error;
85                 }
86         } else {
87                 route->flags |= DEVADDR_DEVICE;
88         }
89
90         if ((cur = tb[ROUTE_METRIC]) != NULL)
91                 route->metric = blobmsg_get_u32(cur);
92         else
93                 route->metric = -1;
94
95         if ((cur = tb[ROUTE_MTU]) != NULL)
96                 route->mtu = blobmsg_get_u32(cur);
97
98         vlist_add(&ip->route, &route->node, &route->mask);
99         return;
100
101 error:
102         free(route);
103 }
104
105 static int
106 addr_cmp(const void *k1, const void *k2, void *ptr)
107 {
108         return memcmp(k1, k2, sizeof(struct device_addr) -
109                       offsetof(struct device_addr, mask));
110 }
111
112 static int
113 route_cmp(const void *k1, const void *k2, void *ptr)
114 {
115         return memcmp(k1, k2, sizeof(struct device_route) -
116                       offsetof(struct device_route, mask));
117 }
118
119 static void
120 interface_update_proto_addr(struct vlist_tree *tree,
121                             struct vlist_node *node_new,
122                             struct vlist_node *node_old)
123 {
124         struct interface_ip_settings *ip;
125         struct interface *iface;
126         struct device *dev;
127         struct device_addr *a_new = NULL, *a_old = NULL;
128         bool keep = false;
129
130         ip = container_of(tree, struct interface_ip_settings, addr);
131         iface = ip->iface;
132         dev = iface->l3_dev->dev;
133
134         if (node_new) {
135                 a_new = container_of(node_new, struct device_addr, node);
136
137                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
138                     !a_new->broadcast) {
139
140                         uint32_t mask = ~0;
141                         uint32_t *a = (uint32_t *) &a_new->addr;
142
143                         mask >>= a_new->mask;
144                         a_new->broadcast = *a | mask;
145                 }
146         }
147
148         if (node_old)
149                 a_old = container_of(node_old, struct device_addr, node);
150
151         if (a_new && a_old) {
152                 keep = true;
153
154                 if (a_old->flags != a_new->flags)
155                         keep = false;
156
157                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
158                     a_new->broadcast != a_old->broadcast)
159                         keep = false;
160         }
161
162         if (node_old) {
163                 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep)
164                         system_del_address(dev, a_old);
165                 free(a_old);
166         }
167
168         if (node_new) {
169                 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep)
170                         system_add_address(dev, a_new);
171                 a_new->enabled = true;
172         }
173 }
174
175 static bool
176 enable_route(struct interface_ip_settings *ip, struct device_route *route)
177 {
178         if (ip->no_defaultroute && !route->mask)
179                 return false;
180
181         return true;
182 }
183
184 static void
185 interface_update_proto_route(struct vlist_tree *tree,
186                              struct vlist_node *node_new,
187                              struct vlist_node *node_old)
188 {
189         struct interface_ip_settings *ip;
190         struct interface *iface;
191         struct device *dev;
192         struct device_route *route_old, *route_new;
193         bool keep = false;
194
195         ip = container_of(tree, struct interface_ip_settings, route);
196         iface = ip->iface;
197         dev = iface->l3_dev->dev;
198
199         route_old = container_of(node_old, struct device_route, node);
200         route_new = container_of(node_new, struct device_route, node);
201
202         if (node_old && node_new)
203                 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
204
205         if (node_old) {
206                 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
207                         system_del_route(dev, route_old);
208                 free(route_old);
209         }
210
211         if (node_new) {
212                 bool _enabled = enable_route(ip, route_new);
213
214                 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
215                         system_add_route(dev, route_new);
216
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                         system_add_route(dev, route);
383                 else
384                         system_del_route(dev, route);
385                 route->enabled = _enabled;
386         }
387 }
388
389 void
390 interface_ip_update_start(struct interface_ip_settings *ip)
391 {
392         vlist_simple_update(&ip->dns_servers);
393         vlist_simple_update(&ip->dns_search);
394         vlist_update(&ip->route);
395         vlist_update(&ip->addr);
396 }
397
398 void
399 interface_ip_update_complete(struct interface_ip_settings *ip)
400 {
401         vlist_simple_flush(&ip->dns_servers);
402         vlist_simple_flush(&ip->dns_search);
403         vlist_flush(&ip->route);
404         vlist_flush(&ip->addr);
405 }
406
407 void
408 interface_ip_flush(struct interface_ip_settings *ip)
409 {
410         vlist_simple_flush_all(&ip->dns_servers);
411         vlist_simple_flush_all(&ip->dns_search);
412         vlist_flush_all(&ip->route);
413         vlist_flush_all(&ip->addr);
414 }
415
416 void
417 interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
418 {
419         ip->iface = iface;
420         ip->enabled = true;
421         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
422         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
423         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
424         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
425 }