add copyright headers
[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 bool
54 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
55 {
56         uint8_t *p1, *p2;
57         int m_bytes = (mask + 7) / 8;
58         uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
59
60         p1 = alloca(m_bytes);
61         p2 = alloca(m_bytes);
62
63         memcpy(p1, a1, m_bytes);
64         memcpy(p2, a2, m_bytes);
65
66         p1[m_bytes - 1] &= ~m_clear;
67         p2[m_bytes - 1] &= ~m_clear;
68
69         return !memcmp(p1, p2, m_bytes);
70 }
71
72 static bool
73 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
74 {
75         struct device_addr *addr;
76
77         vlist_for_each_element(&ip->addr, addr, node) {
78                 if (!addr->enabled)
79                         continue;
80
81                 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
82                         continue;
83
84                 if (!match_if_addr(&addr->addr, a, addr->mask))
85                         continue;
86
87                 return true;
88         }
89
90         return false;
91 }
92
93 static void
94 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
95                        bool v6, struct device_route **res)
96 {
97         struct device_route *route;
98
99         vlist_for_each_element(&ip->route, route, node) {
100                 if (!route->enabled)
101                         continue;
102
103                 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
104                         continue;
105
106                 if (!match_if_addr(&route->addr, a, route->mask))
107                         continue;
108
109                 if (!*res || route->mask < (*res)->mask)
110                         *res = route;
111         }
112 }
113
114 static bool
115 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
116 {
117         return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
118                __find_ip_addr_target(&iface->config_ip, a, v6);
119 }
120
121 static void
122 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
123                                bool v6, struct device_route **route)
124 {
125         __find_ip_route_target(&iface->proto_ip, a, v6, route);
126         __find_ip_route_target(&iface->config_ip, a, v6, route);
127 }
128
129 struct interface *
130 interface_ip_add_target_route(union if_addr *addr, bool v6)
131 {
132         struct interface *iface;
133         struct device_route *route, *r_next = NULL;
134
135         route = calloc(1, sizeof(*route));
136         if (!route)
137                 return false;
138
139         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
140         route->mask = v6 ? 128 : 32;
141         memcpy(&route->addr, addr, v6 ? sizeof(addr->in6) : sizeof(addr->in));
142
143         vlist_for_each_element(&interfaces, iface, node) {
144                 /* look for locally addressable target first */
145                 if (interface_ip_find_addr_target(iface, addr, v6))
146                         goto done;
147
148                 /* do not stop at the first route, let the lookup compare
149                  * masks to find the best match */
150                 interface_ip_find_route_target(iface, addr, v6, &r_next);
151         }
152
153         if (!r_next)
154                 return NULL;
155
156         iface = r_next->iface;
157         memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
158         route->mtu = r_next->mtu;
159         route->metric = r_next->metric;
160
161 done:
162         route->iface = iface;
163         vlist_add(&iface->host_routes, &route->node, &route->flags);
164         return iface;
165 }
166
167 void
168 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
169 {
170         struct interface_ip_settings *ip;
171         struct blob_attr *tb[__ROUTE_MAX], *cur;
172         struct device_route *route;
173         int af = v6 ? AF_INET6 : AF_INET;
174
175         blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
176
177         if (!iface) {
178                 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
179                         return;
180
181                 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
182                 if (!iface)
183                         return;
184
185                 ip = &iface->config_ip;
186         } else {
187                 ip = &iface->proto_ip;
188         }
189
190         route = calloc(1, sizeof(*route));
191         if (!route)
192                 return;
193
194         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
195         route->mask = v6 ? 128 : 32;
196         if ((cur = tb[ROUTE_MASK]) != NULL) {
197                 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
198                 if (route->mask > (v6 ? 128 : 32))
199                         goto error;
200         }
201
202         if ((cur = tb[ROUTE_TARGET]) != NULL) {
203                 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
204                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
205                         goto error;
206                 }
207         }
208
209         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
210                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
211                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
212                         goto error;
213                 }
214         }
215
216         if ((cur = tb[ROUTE_METRIC]) != NULL) {
217                 route->metric = blobmsg_get_u32(cur);
218                 route->flags |= DEVROUTE_METRIC;
219         }
220
221         if ((cur = tb[ROUTE_MTU]) != NULL)
222                 route->mtu = blobmsg_get_u32(cur);
223
224         vlist_add(&ip->route, &route->node, &route->flags);
225         return;
226
227 error:
228         free(route);
229 }
230
231 static int
232 addr_cmp(const void *k1, const void *k2, void *ptr)
233 {
234         return memcmp(k1, k2, sizeof(struct device_addr) -
235                       offsetof(struct device_addr, flags));
236 }
237
238 static int
239 route_cmp(const void *k1, const void *k2, void *ptr)
240 {
241         return memcmp(k1, k2, sizeof(struct device_route) -
242                       offsetof(struct device_route, flags));
243 }
244
245 static void
246 interface_update_proto_addr(struct vlist_tree *tree,
247                             struct vlist_node *node_new,
248                             struct vlist_node *node_old)
249 {
250         struct interface_ip_settings *ip;
251         struct interface *iface;
252         struct device *dev;
253         struct device_addr *a_new = NULL, *a_old = NULL;
254         bool keep = false;
255
256         ip = container_of(tree, struct interface_ip_settings, addr);
257         iface = ip->iface;
258         dev = iface->l3_dev.dev;
259
260         if (node_new) {
261                 a_new = container_of(node_new, struct device_addr, node);
262
263                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
264                     !a_new->broadcast) {
265
266                         uint32_t mask = ~0;
267                         uint32_t *a = (uint32_t *) &a_new->addr;
268
269                         mask >>= a_new->mask;
270                         a_new->broadcast = *a | mask;
271                 }
272         }
273
274         if (node_old)
275                 a_old = container_of(node_old, struct device_addr, node);
276
277         if (a_new && a_old) {
278                 keep = true;
279
280                 if (a_old->flags != a_new->flags)
281                         keep = false;
282
283                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
284                     a_new->broadcast != a_old->broadcast)
285                         keep = false;
286         }
287
288         if (node_old) {
289                 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep)
290                         system_del_address(dev, a_old);
291                 free(a_old);
292         }
293
294         if (node_new) {
295                 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep)
296                         system_add_address(dev, a_new);
297                 a_new->enabled = true;
298         }
299 }
300
301 static bool
302 enable_route(struct interface_ip_settings *ip, struct device_route *route)
303 {
304         if (ip->no_defaultroute && !route->mask)
305                 return false;
306
307         return ip->enabled;
308 }
309
310 static void
311 interface_update_proto_route(struct vlist_tree *tree,
312                              struct vlist_node *node_new,
313                              struct vlist_node *node_old)
314 {
315         struct interface_ip_settings *ip;
316         struct interface *iface;
317         struct device *dev;
318         struct device_route *route_old, *route_new;
319         bool keep = false;
320
321         ip = container_of(tree, struct interface_ip_settings, route);
322         iface = ip->iface;
323         dev = iface->l3_dev.dev;
324
325         route_old = container_of(node_old, struct device_route, node);
326         route_new = container_of(node_new, struct device_route, node);
327
328         if (node_old && node_new)
329                 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
330
331         if (node_old) {
332                 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
333                         system_del_route(dev, route_old);
334                 free(route_old);
335         }
336
337         if (node_new) {
338                 bool _enabled = enable_route(ip, route_new);
339
340                 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
341                         system_add_route(dev, route_new);
342
343                 route_new->iface = iface;
344                 route_new->enabled = _enabled;
345         }
346 }
347
348 static void
349 interface_update_host_route(struct vlist_tree *tree,
350                              struct vlist_node *node_new,
351                              struct vlist_node *node_old)
352 {
353         struct interface *iface;
354         struct device *dev;
355         struct device_route *route_old, *route_new;
356
357         iface = container_of(tree, struct interface, host_routes);
358         dev = iface->l3_dev.dev;
359
360         route_old = container_of(node_old, struct device_route, node);
361         route_new = container_of(node_new, struct device_route, node);
362
363         if (node_old) {
364                 system_del_route(dev, route_old);
365                 free(route_old);
366         }
367
368         if (node_new)
369                 system_add_route(dev, route_new);
370 }
371
372 void
373 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
374 {
375         struct dns_server *s;
376
377         s = calloc(1, sizeof(*s));
378         s->af = AF_INET;
379         if (inet_pton(s->af, str, &s->addr.in))
380                 goto add;
381
382         s->af = AF_INET6;
383         if (inet_pton(s->af, str, &s->addr.in))
384                 goto add;
385
386         free(s);
387         return;
388
389 add:
390         D(INTERFACE, "Add IPv%c DNS server: %s\n",
391           s->af == AF_INET6 ? '6' : '4', str);
392         vlist_simple_add(&ip->dns_servers, &s->node);
393 }
394
395 void
396 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
397 {
398         struct blob_attr *cur;
399         int rem;
400
401         blobmsg_for_each_attr(cur, list, rem) {
402                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
403                         continue;
404
405                 if (!blobmsg_check_attr(cur, NULL))
406                         continue;
407
408                 interface_add_dns_server(ip, blobmsg_data(cur));
409         }
410 }
411
412 static void
413 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
414 {
415         struct dns_search_domain *s;
416         int len = strlen(str);
417
418         s = calloc(1, sizeof(*s) + len + 1);
419         if (!s)
420                 return;
421
422         D(INTERFACE, "Add DNS search domain: %s\n", str);
423         memcpy(s->name, str, len);
424         vlist_simple_add(&ip->dns_search, &s->node);
425 }
426
427 void
428 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
429 {
430         struct blob_attr *cur;
431         int rem;
432
433         blobmsg_for_each_attr(cur, list, rem) {
434                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
435                         continue;
436
437                 if (!blobmsg_check_attr(cur, NULL))
438                         continue;
439
440                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
441         }
442 }
443
444 static void
445 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
446 {
447         struct dns_server *s;
448         struct dns_search_domain *d;
449         const char *str;
450         char buf[32];
451
452         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
453                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
454                 if (!str)
455                         continue;
456
457                 fprintf(f, "nameserver %s\n", str);
458         }
459
460         vlist_simple_for_each_element(&ip->dns_search, d, node) {
461                 fprintf(f, "search %s\n", d->name);
462         }
463 }
464
465 void
466 interface_write_resolv_conf(void)
467 {
468         struct interface *iface;
469         char *path = alloca(strlen(resolv_conf) + 5);
470         FILE *f;
471
472         sprintf(path, "%s.tmp", resolv_conf);
473         unlink(path);
474         f = fopen(path, "w");
475         if (!f) {
476                 D(INTERFACE, "Failed to open %s for writing\n", path);
477                 return;
478         }
479
480         vlist_for_each_element(&interfaces, iface, node) {
481                 if (iface->state != IFS_UP)
482                         continue;
483
484                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
485                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
486                         vlist_simple_empty(&iface->config_ip.dns_search) &&
487                     vlist_simple_empty(&iface->config_ip.dns_servers))
488                         continue;
489
490                 fprintf(f, "# Interface %s\n", iface->name);
491                 write_resolv_conf_entries(f, &iface->config_ip);
492                 write_resolv_conf_entries(f, &iface->proto_ip);
493         }
494         fclose(f);
495         if (rename(path, resolv_conf) < 0) {
496                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
497                 unlink(path);
498         }
499 }
500
501 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
502 {
503         struct device_addr *addr;
504         struct device_route *route;
505         struct device *dev;
506
507         ip->enabled = enabled;
508         dev = ip->iface->l3_dev.dev;
509         if (!dev)
510                 return;
511
512         vlist_for_each_element(&ip->addr, addr, node) {
513                 if (addr->enabled == enabled)
514                         continue;
515
516                 if (enabled)
517                         system_add_address(dev, addr);
518                 else
519                         system_del_address(dev, addr);
520                 addr->enabled = enabled;
521         }
522
523         vlist_for_each_element(&ip->route, route, node) {
524                 bool _enabled = enabled;
525
526                 if (!enable_route(ip, route))
527                         _enabled = false;
528
529                 if (route->enabled == _enabled)
530                         continue;
531
532                 if (_enabled) {
533                         if (!(route->flags & DEVROUTE_METRIC))
534                                 route->metric = ip->iface->metric;
535
536                         system_add_route(dev, route);
537                 } else
538                         system_del_route(dev, route);
539                 route->enabled = _enabled;
540         }
541 }
542
543 void
544 interface_ip_update_start(struct interface_ip_settings *ip)
545 {
546         vlist_simple_update(&ip->dns_servers);
547         vlist_simple_update(&ip->dns_search);
548         vlist_update(&ip->route);
549         vlist_update(&ip->addr);
550 }
551
552 void
553 interface_ip_update_complete(struct interface_ip_settings *ip)
554 {
555         vlist_simple_flush(&ip->dns_servers);
556         vlist_simple_flush(&ip->dns_search);
557         vlist_flush(&ip->route);
558         vlist_flush(&ip->addr);
559 }
560
561 void
562 interface_ip_flush(struct interface_ip_settings *ip)
563 {
564         if (ip == &ip->iface->proto_ip)
565                 vlist_flush_all(&ip->iface->host_routes);
566         vlist_simple_flush_all(&ip->dns_servers);
567         vlist_simple_flush_all(&ip->dns_search);
568         vlist_flush_all(&ip->route);
569         vlist_flush_all(&ip->addr);
570 }
571
572 static void
573 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
574 {
575         ip->iface = iface;
576         ip->enabled = true;
577         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
578         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
579         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
580         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
581 }
582
583 void
584 interface_ip_init(struct interface *iface)
585 {
586         __interface_ip_init(&iface->proto_ip, iface);
587         __interface_ip_init(&iface->config_ip, iface);
588         vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
589 }