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