349473185a8b8225d2516d2830e2fee51969974c
[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->enabled = _enabled;
217         }
218 }
219
220 void
221 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
222 {
223         struct dns_server *s;
224
225         s = calloc(1, sizeof(*s));
226         s->af = AF_INET;
227         if (inet_pton(s->af, str, &s->addr.in))
228                 goto add;
229
230         s->af = AF_INET6;
231         if (inet_pton(s->af, str, &s->addr.in))
232                 goto add;
233
234         free(s);
235         return;
236
237 add:
238         D(INTERFACE, "Add IPv%c DNS server: %s\n",
239           s->af == AF_INET6 ? '6' : '4', str);
240         vlist_simple_add(&ip->dns_servers, &s->node);
241 }
242
243 void
244 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
245 {
246         struct blob_attr *cur;
247         int rem;
248
249         blobmsg_for_each_attr(cur, list, rem) {
250                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
251                         continue;
252
253                 if (!blobmsg_check_attr(cur, NULL))
254                         continue;
255
256                 interface_add_dns_server(ip, blobmsg_data(cur));
257         }
258 }
259
260 static void
261 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
262 {
263         struct dns_search_domain *s;
264         int len = strlen(str);
265
266         s = calloc(1, sizeof(*s) + len + 1);
267         if (!s)
268                 return;
269
270         D(INTERFACE, "Add DNS search domain: %s\n", str);
271         memcpy(s->name, str, len);
272         vlist_simple_add(&ip->dns_search, &s->node);
273 }
274
275 void
276 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
277 {
278         struct blob_attr *cur;
279         int rem;
280
281         blobmsg_for_each_attr(cur, list, rem) {
282                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
283                         continue;
284
285                 if (!blobmsg_check_attr(cur, NULL))
286                         continue;
287
288                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
289         }
290 }
291
292 static void
293 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
294 {
295         struct dns_server *s;
296         struct dns_search_domain *d;
297         const char *str;
298         char buf[32];
299
300         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
301                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
302                 if (!str)
303                         continue;
304
305                 fprintf(f, "nameserver %s\n", str);
306         }
307
308         vlist_simple_for_each_element(&ip->dns_search, d, node) {
309                 fprintf(f, "search %s\n", d->name);
310         }
311 }
312
313 void
314 interface_write_resolv_conf(void)
315 {
316         struct interface *iface;
317         char *path = alloca(strlen(resolv_conf) + 5);
318         FILE *f;
319
320         sprintf(path, "%s.tmp", resolv_conf);
321         unlink(path);
322         f = fopen(path, "w");
323         if (!f) {
324                 D(INTERFACE, "Failed to open %s for writing\n", path);
325                 return;
326         }
327
328         vlist_for_each_element(&interfaces, iface, node) {
329                 if (iface->state != IFS_UP)
330                         continue;
331
332                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
333                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
334                         vlist_simple_empty(&iface->config_ip.dns_search) &&
335                     vlist_simple_empty(&iface->config_ip.dns_servers))
336                         continue;
337
338                 fprintf(f, "# Interface %s\n", iface->name);
339                 write_resolv_conf_entries(f, &iface->config_ip);
340                 write_resolv_conf_entries(f, &iface->proto_ip);
341         }
342         fclose(f);
343         if (rename(path, resolv_conf) < 0) {
344                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
345                 unlink(path);
346         }
347 }
348
349 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
350 {
351         struct device_addr *addr;
352         struct device_route *route;
353         struct device *dev;
354
355         ip->enabled = enabled;
356         dev = ip->iface->l3_dev.dev;
357         if (!dev)
358                 return;
359
360         vlist_for_each_element(&ip->addr, addr, node) {
361                 if (addr->enabled == enabled)
362                         continue;
363
364                 if (enabled)
365                         system_add_address(dev, addr);
366                 else
367                         system_del_address(dev, addr);
368                 addr->enabled = enabled;
369         }
370
371         vlist_for_each_element(&ip->route, route, node) {
372                 bool _enabled = enabled;
373
374                 if (!enable_route(ip, route))
375                         _enabled = false;
376
377                 if (route->enabled == _enabled)
378                         continue;
379
380                 if (_enabled) {
381                         if (!(route->flags & DEVROUTE_METRIC))
382                                 route->metric = ip->iface->metric;
383
384                         system_add_route(dev, route);
385                 } else
386                         system_del_route(dev, route);
387                 route->enabled = _enabled;
388         }
389 }
390
391 void
392 interface_ip_update_start(struct interface_ip_settings *ip)
393 {
394         vlist_simple_update(&ip->dns_servers);
395         vlist_simple_update(&ip->dns_search);
396         vlist_update(&ip->route);
397         vlist_update(&ip->addr);
398 }
399
400 void
401 interface_ip_update_complete(struct interface_ip_settings *ip)
402 {
403         vlist_simple_flush(&ip->dns_servers);
404         vlist_simple_flush(&ip->dns_search);
405         vlist_flush(&ip->route);
406         vlist_flush(&ip->addr);
407 }
408
409 void
410 interface_ip_flush(struct interface_ip_settings *ip)
411 {
412         vlist_simple_flush_all(&ip->dns_servers);
413         vlist_simple_flush_all(&ip->dns_search);
414         vlist_flush_all(&ip->route);
415         vlist_flush_all(&ip->addr);
416 }
417
418 void
419 interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
420 {
421         ip->iface = iface;
422         ip->enabled = true;
423         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
424         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
425         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
426         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
427 }