split alias support into a separate source file for better readability
[project/netifd.git] / interface-ip.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <unistd.h>
18
19 #include <arpa/inet.h>
20
21 #include "netifd.h"
22 #include "device.h"
23 #include "interface.h"
24 #include "interface-ip.h"
25 #include "proto.h"
26 #include "ubus.h"
27 #include "system.h"
28
29 enum {
30         ROUTE_INTERFACE,
31         ROUTE_TARGET,
32         ROUTE_MASK,
33         ROUTE_GATEWAY,
34         ROUTE_METRIC,
35         ROUTE_MTU,
36         __ROUTE_MAX
37 };
38
39 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
40         [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
41         [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
42         [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
43         [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
44         [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
45         [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
46 };
47
48 const struct config_param_list route_attr_list = {
49         .n_params = __ROUTE_MAX,
50         .params = route_attr,
51 };
52
53 static void
54 clear_if_addr(union if_addr *a, int mask)
55 {
56         int m_bytes = (mask + 7) / 8;
57         uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
58         uint8_t *p = (uint8_t *) a;
59
60         if (m_bytes < sizeof(a))
61                 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
62
63         p[m_bytes - 1] &= ~m_clear;
64 }
65
66 static bool
67 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
68 {
69         union if_addr *p1, *p2;
70
71         p1 = alloca(sizeof(*a1));
72         p2 = alloca(sizeof(*a2));
73
74         memcpy(p1, a1, sizeof(*a1));
75         clear_if_addr(p1, mask);
76         memcpy(p2, a2, sizeof(*a2));
77         clear_if_addr(p2, mask);
78
79         return !memcmp(p1, p2, sizeof(*p1));
80 }
81
82 static bool
83 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
84 {
85         struct device_addr *addr;
86
87         vlist_for_each_element(&ip->addr, addr, node) {
88                 if (!addr->enabled)
89                         continue;
90
91                 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
92                         continue;
93
94                 if (!match_if_addr(&addr->addr, a, addr->mask))
95                         continue;
96
97                 return true;
98         }
99
100         return false;
101 }
102
103 static void
104 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
105                        bool v6, struct device_route **res)
106 {
107         struct device_route *route;
108
109         vlist_for_each_element(&ip->route, route, node) {
110                 if (!route->enabled)
111                         continue;
112
113                 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
114                         continue;
115
116                 if (!match_if_addr(&route->addr, a, route->mask))
117                         continue;
118
119                 if (!*res || route->mask < (*res)->mask)
120                         *res = route;
121         }
122 }
123
124 static bool
125 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
126 {
127         return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
128                __find_ip_addr_target(&iface->config_ip, a, v6);
129 }
130
131 static void
132 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
133                                bool v6, struct device_route **route)
134 {
135         __find_ip_route_target(&iface->proto_ip, a, v6, route);
136         __find_ip_route_target(&iface->config_ip, a, v6, route);
137 }
138
139 struct interface *
140 interface_ip_add_target_route(union if_addr *addr, bool v6)
141 {
142         struct interface *iface;
143         struct device_route *route, *r_next = NULL;
144         bool defaultroute_target = false;
145         int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
146
147         route = calloc(1, sizeof(*route));
148         if (!route)
149                 return NULL;
150
151         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
152         route->mask = v6 ? 128 : 32;
153         if (memcmp(&route->addr, addr, addrsize) == 0)
154                 defaultroute_target = true;
155         else
156                 memcpy(&route->addr, addr, addrsize);
157
158         vlist_for_each_element(&interfaces, iface, node) {
159                 /* look for locally addressable target first */
160                 if (interface_ip_find_addr_target(iface, addr, v6))
161                         goto done;
162
163                 /* do not stop at the first route, let the lookup compare
164                  * masks to find the best match */
165                 interface_ip_find_route_target(iface, addr, v6, &r_next);
166         }
167
168         if (!r_next) {
169                 free(route);
170                 return NULL;
171         }
172
173         iface = r_next->iface;
174         memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
175         route->mtu = r_next->mtu;
176         route->metric = r_next->metric;
177
178 done:
179         route->iface = iface;
180         if (defaultroute_target)
181                 free(route);
182         else
183                 vlist_add(&iface->host_routes, &route->node, &route->flags);
184         return iface;
185 }
186
187 void
188 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
189 {
190         struct interface_ip_settings *ip;
191         struct blob_attr *tb[__ROUTE_MAX], *cur;
192         struct device_route *route;
193         int af = v6 ? AF_INET6 : AF_INET;
194
195         blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
196
197         if (!iface) {
198                 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
199                         return;
200
201                 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
202                 if (!iface)
203                         return;
204
205                 ip = &iface->config_ip;
206         } else {
207                 ip = &iface->proto_ip;
208         }
209
210         route = calloc(1, sizeof(*route));
211         if (!route)
212                 return;
213
214         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
215         route->mask = v6 ? 128 : 32;
216         if ((cur = tb[ROUTE_MASK]) != NULL) {
217                 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
218                 if (route->mask > (v6 ? 128 : 32))
219                         goto error;
220         }
221
222         if ((cur = tb[ROUTE_TARGET]) != NULL) {
223                 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
224                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
225                         goto error;
226                 }
227         }
228
229         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
230                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
231                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
232                         goto error;
233                 }
234         }
235
236         if ((cur = tb[ROUTE_METRIC]) != NULL) {
237                 route->metric = blobmsg_get_u32(cur);
238                 route->flags |= DEVROUTE_METRIC;
239         }
240
241         if ((cur = tb[ROUTE_MTU]) != NULL) {
242                 route->mtu = blobmsg_get_u32(cur);
243                 route->flags |= DEVROUTE_MTU;
244         }
245
246         vlist_add(&ip->route, &route->node, &route->flags);
247         return;
248
249 error:
250         free(route);
251 }
252
253 static int
254 addr_cmp(const void *k1, const void *k2, void *ptr)
255 {
256         return memcmp(k1, k2, sizeof(struct device_addr) -
257                       offsetof(struct device_addr, flags));
258 }
259
260 static int
261 route_cmp(const void *k1, const void *k2, void *ptr)
262 {
263         return memcmp(k1, k2, sizeof(struct device_route) -
264                       offsetof(struct device_route, flags));
265 }
266
267 static void
268 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
269 {
270         struct device *dev = iface->l3_dev.dev;
271         struct device_route route;
272
273         memset(&route, 0, sizeof(route));
274         route.iface = iface;
275         route.flags = addr->flags;
276         route.mask = addr->mask;
277         memcpy(&route.addr, &addr->addr, sizeof(route.addr));
278         clear_if_addr(&route.addr, route.mask);
279
280         if (add) {
281                 route.flags |= DEVADDR_KERNEL;
282                 system_del_route(dev, &route);
283
284                 route.flags &= ~DEVADDR_KERNEL;
285                 route.metric = iface->metric;
286                 system_add_route(dev, &route);
287         } else {
288                 system_del_route(dev, &route);
289         }
290 }
291
292 static void
293 interface_update_proto_addr(struct vlist_tree *tree,
294                             struct vlist_node *node_new,
295                             struct vlist_node *node_old)
296 {
297         struct interface_ip_settings *ip;
298         struct interface *iface;
299         struct device *dev;
300         struct device_addr *a_new = NULL, *a_old = NULL;
301         bool keep = false;
302
303         ip = container_of(tree, struct interface_ip_settings, addr);
304         iface = ip->iface;
305         dev = iface->l3_dev.dev;
306
307         if (node_new) {
308                 a_new = container_of(node_new, struct device_addr, node);
309
310                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
311                     !a_new->broadcast) {
312
313                         uint32_t mask = ~0;
314                         uint32_t *a = (uint32_t *) &a_new->addr;
315
316                         mask >>= a_new->mask;
317                         a_new->broadcast = *a | htonl(mask);
318                 }
319         }
320
321         if (node_old)
322                 a_old = container_of(node_old, struct device_addr, node);
323
324         if (a_new && a_old) {
325                 keep = true;
326
327                 if (a_old->flags != a_new->flags)
328                         keep = false;
329
330                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
331                     a_new->broadcast != a_old->broadcast)
332                         keep = false;
333         }
334
335         if (node_old) {
336                 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
337                         interface_handle_subnet_route(iface, a_old, false);
338                         system_del_address(dev, a_old);
339                 }
340                 free(a_old);
341         }
342
343         if (node_new) {
344                 a_new->enabled = true;
345                 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) {
346                         system_add_address(dev, a_new);
347                         if (iface->metric)
348                                 interface_handle_subnet_route(iface, a_new, true);
349                 }
350         }
351 }
352
353 static bool
354 enable_route(struct interface_ip_settings *ip, struct device_route *route)
355 {
356         if (ip->no_defaultroute && !route->mask)
357                 return false;
358
359         return ip->enabled;
360 }
361
362 static void
363 interface_update_proto_route(struct vlist_tree *tree,
364                              struct vlist_node *node_new,
365                              struct vlist_node *node_old)
366 {
367         struct interface_ip_settings *ip;
368         struct interface *iface;
369         struct device *dev;
370         struct device_route *route_old, *route_new;
371         bool keep = false;
372
373         ip = container_of(tree, struct interface_ip_settings, route);
374         iface = ip->iface;
375         dev = iface->l3_dev.dev;
376
377         route_old = container_of(node_old, struct device_route, node);
378         route_new = container_of(node_new, struct device_route, node);
379
380         if (node_old && node_new)
381                 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
382
383         if (node_old) {
384                 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
385                         system_del_route(dev, route_old);
386                 free(route_old);
387         }
388
389         if (node_new) {
390                 bool _enabled = enable_route(ip, route_new);
391
392                 if (!(route_new->flags & DEVROUTE_METRIC))
393                         route_new->metric = iface->metric;
394
395                 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
396                         system_add_route(dev, route_new);
397
398                 route_new->iface = iface;
399                 route_new->enabled = _enabled;
400         }
401 }
402
403 static void
404 interface_update_host_route(struct vlist_tree *tree,
405                              struct vlist_node *node_new,
406                              struct vlist_node *node_old)
407 {
408         struct interface *iface;
409         struct device *dev;
410         struct device_route *route_old, *route_new;
411
412         iface = container_of(tree, struct interface, host_routes);
413         dev = iface->l3_dev.dev;
414
415         route_old = container_of(node_old, struct device_route, node);
416         route_new = container_of(node_new, struct device_route, node);
417
418         if (node_old) {
419                 system_del_route(dev, route_old);
420                 free(route_old);
421         }
422
423         if (node_new)
424                 system_add_route(dev, route_new);
425 }
426
427 void
428 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
429 {
430         struct dns_server *s;
431
432         s = calloc(1, sizeof(*s));
433         s->af = AF_INET;
434         if (inet_pton(s->af, str, &s->addr.in))
435                 goto add;
436
437         s->af = AF_INET6;
438         if (inet_pton(s->af, str, &s->addr.in))
439                 goto add;
440
441         free(s);
442         return;
443
444 add:
445         D(INTERFACE, "Add IPv%c DNS server: %s\n",
446           s->af == AF_INET6 ? '6' : '4', str);
447         vlist_simple_add(&ip->dns_servers, &s->node);
448 }
449
450 void
451 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
452 {
453         struct blob_attr *cur;
454         int rem;
455
456         blobmsg_for_each_attr(cur, list, rem) {
457                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
458                         continue;
459
460                 if (!blobmsg_check_attr(cur, NULL))
461                         continue;
462
463                 interface_add_dns_server(ip, blobmsg_data(cur));
464         }
465 }
466
467 static void
468 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
469 {
470         struct dns_search_domain *s;
471         int len = strlen(str);
472
473         s = calloc(1, sizeof(*s) + len + 1);
474         if (!s)
475                 return;
476
477         D(INTERFACE, "Add DNS search domain: %s\n", str);
478         memcpy(s->name, str, len);
479         vlist_simple_add(&ip->dns_search, &s->node);
480 }
481
482 void
483 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
484 {
485         struct blob_attr *cur;
486         int rem;
487
488         blobmsg_for_each_attr(cur, list, rem) {
489                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
490                         continue;
491
492                 if (!blobmsg_check_attr(cur, NULL))
493                         continue;
494
495                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
496         }
497 }
498
499 static void
500 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
501 {
502         struct dns_server *s;
503         struct dns_search_domain *d;
504         const char *str;
505         char buf[32];
506
507         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
508                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
509                 if (!str)
510                         continue;
511
512                 fprintf(f, "nameserver %s\n", str);
513         }
514
515         vlist_simple_for_each_element(&ip->dns_search, d, node) {
516                 fprintf(f, "search %s\n", d->name);
517         }
518 }
519
520 void
521 interface_write_resolv_conf(void)
522 {
523         struct interface *iface;
524         char *path = alloca(strlen(resolv_conf) + 5);
525         FILE *f;
526
527         sprintf(path, "%s.tmp", resolv_conf);
528         unlink(path);
529         f = fopen(path, "w");
530         if (!f) {
531                 D(INTERFACE, "Failed to open %s for writing\n", path);
532                 return;
533         }
534
535         vlist_for_each_element(&interfaces, iface, node) {
536                 if (iface->state != IFS_UP)
537                         continue;
538
539                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
540                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
541                         vlist_simple_empty(&iface->config_ip.dns_search) &&
542                     vlist_simple_empty(&iface->config_ip.dns_servers))
543                         continue;
544
545                 fprintf(f, "# Interface %s\n", iface->name);
546                 write_resolv_conf_entries(f, &iface->config_ip);
547                 if (!iface->proto_ip.no_dns)
548                         write_resolv_conf_entries(f, &iface->proto_ip);
549         }
550         fclose(f);
551         if (rename(path, resolv_conf) < 0) {
552                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
553                 unlink(path);
554         }
555 }
556
557 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
558 {
559         struct device_addr *addr;
560         struct device_route *route;
561         struct device *dev;
562
563         ip->enabled = enabled;
564         dev = ip->iface->l3_dev.dev;
565         if (!dev)
566                 return;
567
568         vlist_for_each_element(&ip->addr, addr, node) {
569                 if (addr->enabled == enabled)
570                         continue;
571
572                 if (enabled)
573                         system_add_address(dev, addr);
574                 else
575                         system_del_address(dev, addr);
576                 addr->enabled = enabled;
577         }
578
579         vlist_for_each_element(&ip->route, route, node) {
580                 bool _enabled = enabled;
581
582                 if (!enable_route(ip, route))
583                         _enabled = false;
584
585                 if (route->enabled == _enabled)
586                         continue;
587
588                 if (_enabled) {
589                         if (!(route->flags & DEVROUTE_METRIC))
590                                 route->metric = ip->iface->metric;
591
592                         system_add_route(dev, route);
593                 } else
594                         system_del_route(dev, route);
595                 route->enabled = _enabled;
596         }
597 }
598
599 void
600 interface_ip_update_start(struct interface_ip_settings *ip)
601 {
602         if (ip != &ip->iface->config_ip) {
603                 vlist_simple_update(&ip->dns_servers);
604                 vlist_simple_update(&ip->dns_search);
605         }
606         vlist_update(&ip->route);
607         vlist_update(&ip->addr);
608 }
609
610 void
611 interface_ip_update_complete(struct interface_ip_settings *ip)
612 {
613         vlist_simple_flush(&ip->dns_servers);
614         vlist_simple_flush(&ip->dns_search);
615         vlist_flush(&ip->route);
616         vlist_flush(&ip->addr);
617 }
618
619 void
620 interface_ip_flush(struct interface_ip_settings *ip)
621 {
622         if (ip == &ip->iface->proto_ip)
623                 vlist_flush_all(&ip->iface->host_routes);
624         vlist_simple_flush_all(&ip->dns_servers);
625         vlist_simple_flush_all(&ip->dns_search);
626         vlist_flush_all(&ip->route);
627         vlist_flush_all(&ip->addr);
628 }
629
630 static void
631 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
632 {
633         ip->iface = iface;
634         ip->enabled = true;
635         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
636         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
637         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
638         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
639 }
640
641 void
642 interface_ip_init(struct interface *iface)
643 {
644         __interface_ip_init(&iface->proto_ip, iface);
645         __interface_ip_init(&iface->config_ip, iface);
646         vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
647 }