• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/netifd/interface-ip.c

  1 /*
  2  * netifd - network interface daemon
  3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4  * Copyright (C) 2012 Steven Barth <steven@midlink.org>
  5  *
  6  * This program is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License version 2
  8  * as published by the Free Software Foundation
  9  *
 10  * This program is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU General Public License for more details.
 14  */
 15 #include <string.h>
 16 #include <stdlib.h>
 17 #include <stdio.h>
 18 #include <libgen.h>
 19 #include <sys/stat.h>
 20 
 21 #include <limits.h>
 22 #include <arpa/inet.h>
 23 #include <netinet/in.h>
 24 
 25 #ifdef linux
 26 #include <netinet/ether.h>
 27 #endif
 28 
 29 #include "netifd.h"
 30 #include "device.h"
 31 #include "interface.h"
 32 #include "interface-ip.h"
 33 #include "proto.h"
 34 #include "ubus.h"
 35 #include "system.h"
 36 
 37 enum {
 38         ROUTE_INTERFACE,
 39         ROUTE_TARGET,
 40         ROUTE_MASK,
 41         ROUTE_GATEWAY,
 42         ROUTE_METRIC,
 43         ROUTE_MTU,
 44         ROUTE_VALID,
 45         ROUTE_TABLE,
 46         ROUTE_SOURCE,
 47         ROUTE_ONLINK,
 48         ROUTE_TYPE,
 49         ROUTE_PROTO,
 50         __ROUTE_MAX
 51 };
 52 
 53 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
 54         [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
 55         [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
 56         [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
 57         [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
 58         [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
 59         [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
 60         [ROUTE_TABLE] = { .name = "table", .type = BLOBMSG_TYPE_STRING },
 61         [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
 62         [ROUTE_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_STRING },
 63         [ROUTE_ONLINK] = { .name = "onlink", .type = BLOBMSG_TYPE_BOOL },
 64         [ROUTE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
 65         [ROUTE_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
 66 };
 67 
 68 const struct uci_blob_param_list route_attr_list = {
 69         .n_params = __ROUTE_MAX,
 70         .params = route_attr,
 71 };
 72 
 73 enum {
 74         NEIGHBOR_INTERFACE,
 75         NEIGHBOR_ADDRESS,
 76         NEIGHBOR_MAC,
 77         NEIGHBOR_PROXY,
 78         NEIGHBOR_ROUTER,
 79         __NEIGHBOR_MAX
 80 };
 81 
 82 static const struct blobmsg_policy neighbor_attr[__NEIGHBOR_MAX]={
 83         [NEIGHBOR_INTERFACE]= { .name = "interface", .type = BLOBMSG_TYPE_STRING},
 84         [NEIGHBOR_ADDRESS]= { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING},
 85         [NEIGHBOR_MAC]= { .name = "mac", .type = BLOBMSG_TYPE_STRING},
 86         [NEIGHBOR_PROXY]= { .name = "proxy", .type = BLOBMSG_TYPE_BOOL},
 87         [NEIGHBOR_ROUTER]= {.name = "router", .type = BLOBMSG_TYPE_BOOL},
 88 };
 89 
 90 const struct uci_blob_param_list neighbor_attr_list = {
 91         .n_params = __NEIGHBOR_MAX,
 92         .params = neighbor_attr,
 93 };
 94 
 95 
 96 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
 97 static struct device_prefix *ula_prefix = NULL;
 98 static struct uloop_timeout valid_until_timeout;
 99 
100 
101 static void
102 clear_if_addr(union if_addr *a, int mask)
103 {
104         int m_bytes = (mask + 7) / 8;
105         uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
106         uint8_t *p = (uint8_t *) a;
107 
108         if (m_bytes < sizeof(*a))
109                 memset(p + m_bytes, 0, sizeof(*a) - m_bytes);
110 
111         p[m_bytes - 1] &= ~m_clear;
112 }
113 
114 static bool
115 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
116 {
117         union if_addr *p1, *p2;
118 
119         p1 = alloca(sizeof(*a1));
120         p2 = alloca(sizeof(*a2));
121 
122         memcpy(p1, a1, sizeof(*a1));
123         clear_if_addr(p1, mask);
124         memcpy(p2, a2, sizeof(*a2));
125         clear_if_addr(p2, mask);
126 
127         return !memcmp(p1, p2, sizeof(*p1));
128 }
129 
130 static int set_ip_source_policy(bool add, bool v6, unsigned int priority,
131                 const union if_addr *addr, uint8_t mask, unsigned int table,
132                 struct interface *in_iface, const char *action, bool src)
133 {
134         struct iprule rule = {
135                 .flags = IPRULE_PRIORITY,
136                 .priority = priority
137         };
138 
139         if (addr) {
140                 if (src) {
141                         rule.flags |= IPRULE_SRC;
142                         rule.src_addr = *addr;
143                         rule.src_mask = mask;
144                 } else {
145                         rule.flags |= IPRULE_DEST;
146                         rule.dest_addr = *addr;
147                         rule.dest_mask = mask;
148                 }
149         }
150 
151         if (table) {
152                 rule.flags |= IPRULE_LOOKUP;
153                 rule.lookup = table;
154 
155                 if (!rule.lookup)
156                         return 0;
157         } else if (action) {
158                 rule.flags |= IPRULE_ACTION;
159                 system_resolve_iprule_action(action, &rule.action);
160         }
161 
162         if (in_iface && in_iface->l3_dev.dev) {
163                 rule.flags |= IPRULE_IN;
164                 strcpy(rule.in_dev, in_iface->l3_dev.dev->ifname);
165         }
166 
167         rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
168 
169         return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
170 }
171 
172 static int set_ip_lo_policy(bool add, bool v6, struct interface *iface)
173 {
174         struct iprule rule = {
175                 .flags = IPRULE_IN | IPRULE_LOOKUP | IPRULE_PRIORITY,
176                 .priority = IPRULE_PRIORITY_NW + iface->l3_dev.dev->ifindex,
177                 .lookup = (v6) ? iface->ip6table : iface->ip4table,
178                 .in_dev = "lo"
179         };
180 
181         if (!rule.lookup)
182                 return 0;
183 
184         rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
185 
186         return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
187 }
188 
189 static bool
190 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
191 {
192         struct device_addr *addr;
193 
194         vlist_for_each_element(&ip->addr, addr, node) {
195                 if (!addr->enabled)
196                         continue;
197 
198                 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
199                         continue;
200 
201                 if (((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) &&
202                                 addr->point_to_point && a->in.s_addr == addr->point_to_point)
203                         return true;
204 
205                 /* Handle offlink addresses correctly */
206                 unsigned int mask = addr->mask;
207                 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6 &&
208                                 (addr->flags & DEVADDR_OFFLINK))
209                         mask = 128;
210 
211                 if (!match_if_addr(&addr->addr, a, mask))
212                         continue;
213 
214                 return true;
215         }
216 
217         return false;
218 }
219 
220 static void
221 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
222                        bool v6, struct device_route **res)
223 {
224         struct device_route *route;
225 
226         vlist_for_each_element(&ip->route, route, node) {
227                 if (!route->enabled)
228                         continue;
229 
230                 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
231                         continue;
232 
233                 if (!match_if_addr(&route->addr, a, route->mask))
234                         continue;
235 
236                 if (route->flags & DEVROUTE_TABLE)
237                         continue;
238 
239                 if (!*res || route->mask > (*res)->mask)
240                         *res = route;
241         }
242 }
243 
244 static bool
245 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
246 {
247         return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
248                __find_ip_addr_target(&iface->config_ip, a, v6);
249 }
250 
251 static void
252 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
253                                bool v6, struct device_route **route)
254 {
255         __find_ip_route_target(&iface->proto_ip, a, v6, route);
256         __find_ip_route_target(&iface->config_ip, a, v6, route);
257 }
258 
259 struct interface *
260 interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface)
261 {
262         struct device_route *route, *r_next = NULL;
263         bool defaultroute_target = false;
264         union if_addr addr_zero;
265         int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
266 
267         memset(&addr_zero, 0, sizeof(addr_zero));
268         if (memcmp(&addr_zero, addr, addrsize) == 0)
269                 defaultroute_target = true;
270 
271         if (iface) {
272                 /* look for locally addressable target first */
273                 if (interface_ip_find_addr_target(iface, addr, v6))
274                         return iface;
275 
276                 /* do not stop at the first route, let the lookup compare
277                  * masks to find the best match */
278                 interface_ip_find_route_target(iface, addr, v6, &r_next);
279         } else {
280                 vlist_for_each_element(&interfaces, iface, node) {
281                         /* look for locally addressable target first */
282                         if (interface_ip_find_addr_target(iface, addr, v6))
283                                 return iface;
284 
285                         /* do not stop at the first route, let the lookup compare
286                          * masks to find the best match */
287                         interface_ip_find_route_target(iface, addr, v6, &r_next);
288                 }
289         }
290 
291         if (!r_next)
292                 return NULL;
293 
294         iface = r_next->iface;
295         if (defaultroute_target)
296                 return iface;
297 
298         route = calloc(1, sizeof(*route));
299         if (!route)
300                 return NULL;
301 
302         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
303         route->mask = v6 ? 128 : 32;
304         memcpy(&route->addr, addr, addrsize);
305         memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
306         route->mtu = r_next->mtu;
307         route->metric = r_next->metric;
308         route->table = r_next->table;
309         route->iface = iface;
310         vlist_add(&iface->host_routes, &route->node, route);
311 
312         return iface;
313 }
314 
315 static void
316 interface_set_route_info(struct interface *iface, struct device_route *route)
317 {
318         bool v6 = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6);
319 
320         if (!iface)
321                 return;
322 
323         if (!(route->flags & DEVROUTE_METRIC))
324                 route->metric = iface->metric;
325 
326         if (!(route->flags & DEVROUTE_TABLE)) {
327                 route->table = (v6) ? iface->ip6table : iface->ip4table;
328                 if (route->table)
329                         route->flags |= DEVROUTE_SRCTABLE;
330         }
331 }
332 
333 void
334 interface_ip_add_neighbor(struct interface *iface, struct blob_attr *attr, bool v6)
335 {
336         struct interface_ip_settings *ip;
337         struct blob_attr *tb[__NEIGHBOR_MAX], *cur;
338         struct device_neighbor *neighbor;
339         int af = v6 ? AF_INET6: AF_INET;
340         struct ether_addr *ea;
341 
342         blobmsg_parse(neighbor_attr, __NEIGHBOR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
343 
344         if (!iface) {
345                 if ((cur = tb[NEIGHBOR_INTERFACE]) == NULL)
346                         return;
347 
348                 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
349 
350                 if (!iface)
351                         return;
352 
353                 ip = &iface->config_ip;
354         } else
355                 ip = &iface->proto_ip;
356 
357         neighbor = calloc(1,sizeof(*neighbor));
358         if (!neighbor)
359                 return;
360 
361         neighbor->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
362 
363         if ((cur = tb[NEIGHBOR_ADDRESS]) != NULL){
364                 if (!inet_pton(af, blobmsg_data(cur), &neighbor->addr))
365                         goto error;
366         } else
367                 goto error;
368 
369         if ((cur = tb[NEIGHBOR_MAC]) != NULL) {
370                 neighbor->flags |= DEVNEIGH_MAC;
371                 ea = ether_aton(blobmsg_data(cur));
372                 if (!ea)
373                         goto error;
374 
375                 memcpy(neighbor->macaddr, ea, 6);
376         }
377 
378         if ((cur = tb[NEIGHBOR_PROXY]) != NULL)
379                 neighbor->proxy = blobmsg_get_bool(cur);
380 
381         if ((cur = tb[NEIGHBOR_ROUTER]) != NULL)
382                 neighbor->router = blobmsg_get_bool(cur);
383 
384         vlist_add(&ip->neighbor, &neighbor->node, neighbor);
385         return;
386 
387 error:
388         free(neighbor);
389 }
390 
391 void
392 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
393 {
394         struct interface_ip_settings *ip;
395         struct blob_attr *tb[__ROUTE_MAX], *cur;
396         struct device_route *route;
397         int af = v6 ? AF_INET6 : AF_INET;
398 
399         blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
400 
401         if (!iface) {
402                 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
403                         return;
404 
405                 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
406                 if (!iface)
407                         return;
408 
409                 ip = &iface->config_ip;
410         } else {
411                 ip = &iface->proto_ip;
412         }
413 
414         route = calloc(1, sizeof(*route));
415         if (!route)
416                 return;
417 
418         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
419         route->mask = v6 ? 128 : 32;
420         if ((cur = tb[ROUTE_MASK]) != NULL) {
421                 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
422                 if (route->mask > (v6 ? 128 : 32))
423                         goto error;
424         }
425 
426         if ((cur = tb[ROUTE_TARGET]) != NULL) {
427                 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
428                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
429                         goto error;
430                 }
431         }
432 
433         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
434                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
435                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
436                         goto error;
437                 }
438         }
439 
440         if ((cur = tb[ROUTE_METRIC]) != NULL) {
441                 route->metric = blobmsg_get_u32(cur);
442                 route->flags |= DEVROUTE_METRIC;
443         }
444 
445         if ((cur = tb[ROUTE_MTU]) != NULL) {
446                 route->mtu = blobmsg_get_u32(cur);
447                 route->flags |= DEVROUTE_MTU;
448         }
449 
450         /* Use source-based routing */
451         if ((cur = tb[ROUTE_SOURCE]) != NULL) {
452                 char *saveptr, *source = alloca(blobmsg_data_len(cur));
453                 memcpy(source, blobmsg_data(cur), blobmsg_data_len(cur));
454 
455                 const char *addr = strtok_r(source, "/", &saveptr);
456                 const char *mask = strtok_r(NULL, "/", &saveptr);
457 
458                 if (!addr || inet_pton(af, addr, &route->source) < 1) {
459                         DPRINTF("Failed to parse route source: %s\n", addr ? addr : "NULL");
460                         goto error;
461                 }
462 
463                 route->sourcemask = (mask) ? atoi(mask) : ((af == AF_INET6) ? 128 : 32);
464         }
465 
466         if ((cur = tb[ROUTE_ONLINK]) != NULL && blobmsg_get_bool(cur))
467                 route->flags |= DEVROUTE_ONLINK;
468 
469         if ((cur = tb[ROUTE_TABLE]) != NULL) {
470                 if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) {
471                         DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
472                         goto error;
473                 }
474 
475                 /* only set the table flag if not using the main (default) table */
476                 if (system_is_default_rt_table(route->table))
477                         route->table = 0;
478 
479                 if (route->table)
480                         route->flags |= DEVROUTE_TABLE;
481         }
482 
483         if ((cur = tb[ROUTE_VALID]) != NULL) {
484                 int64_t valid = blobmsg_get_u32(cur);
485                 int64_t valid_until = valid + (int64_t)system_get_rtime();
486                 if (valid_until <= LONG_MAX && valid != 0xffffffffLL) /* Catch overflow */
487                         route->valid_until = valid_until;
488         }
489 
490         if ((cur = tb[ROUTE_TYPE]) != NULL) {
491                 if (!system_resolve_rt_type(blobmsg_data(cur), &route->type)) {
492                         DPRINTF("Failed to resolve routing type: %s\n", (char *) blobmsg_data(cur));
493                         goto error;
494                 }
495                 route->flags |= DEVROUTE_TYPE;
496         }
497 
498         if ((cur = tb[ROUTE_PROTO]) != NULL) {
499                 if (!system_resolve_rt_proto(blobmsg_data(cur), &route->proto)) {
500                         DPRINTF("Failed to resolve proto type: %s\n", (char *) blobmsg_data(cur));
501                         goto error;
502                 }
503                 route->flags |= DEVROUTE_PROTO;
504         }
505 
506         interface_set_route_info(iface, route);
507         vlist_add(&ip->route, &route->node, route);
508         return;
509 
510 error:
511         free(route);
512 }
513 
514 static int
515 addr_cmp(const void *k1, const void *k2, void *ptr)
516 {
517         return memcmp(k1, k2, sizeof(struct device_addr) -
518                       offsetof(struct device_addr, flags));
519 }
520 
521 static int
522 neighbor_cmp(const void *k1, const void *k2, void *ptr)
523 {
524         const struct device_neighbor *n1 = k1, *n2 = k2;
525 
526         return memcmp(&n1->addr, &n2->addr, sizeof(n2->addr));
527 }
528 
529 static int
530 route_cmp(const void *k1, const void *k2, void *ptr)
531 {
532         const struct device_route *r1 = k1, *r2 = k2;
533 
534         if (r1->mask != r2->mask)
535                 return r2->mask - r1->mask;
536 
537         if (r1->metric != r2->metric)
538                 return r1->metric - r2->metric;
539 
540         if (r1->flags != r2->flags)
541                 return r2->flags - r1->flags;
542 
543         if (r1->sourcemask != r2->sourcemask)
544                 return r1->sourcemask - r2->sourcemask;
545 
546         if (r1->table != r2->table)
547                 return r1->table - r2->table;
548 
549         int maskcmp = memcmp(&r1->source, &r2->source, sizeof(r1->source));
550         if (maskcmp)
551                 return maskcmp;
552 
553         return memcmp(&r1->addr, &r2->addr, sizeof(r1->addr));
554 }
555 
556 static int
557 prefix_cmp(const void *k1, const void *k2, void *ptr)
558 {
559         return memcmp(k1, k2, offsetof(struct device_prefix, pclass) -
560                         offsetof(struct device_prefix, addr));
561 }
562 
563 static void
564 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
565 {
566         struct device *dev = iface->l3_dev.dev;
567         struct device_route *r = &addr->subnet;
568 
569         if (addr->flags & DEVADDR_OFFLINK)
570                 return;
571 
572         if (!add) {
573                 if (!addr->subnet.iface)
574                         return;
575 
576                 system_del_route(dev, r);
577                 memset(r, 0, sizeof(*r));
578                 return;
579         }
580 
581         r->iface = iface;
582         r->flags = addr->flags;
583         r->mask = addr->mask;
584         memcpy(&r->addr, &addr->addr, sizeof(r->addr));
585         clear_if_addr(&r->addr, r->mask);
586 
587         if (!system_resolve_rt_proto("kernel", &r->proto))
588                 return;
589 
590         r->flags |= DEVROUTE_PROTO;
591         system_del_route(dev, r);
592 
593         r->flags &= ~DEVROUTE_PROTO;
594         interface_set_route_info(iface, r);
595 
596         system_add_route(dev, r);
597 }
598 
599 static void
600 interface_add_addr_rules(struct device_addr *addr, bool enabled)
601 {
602         bool v6 = (addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6;
603 
604         set_ip_source_policy(enabled, v6, IPRULE_PRIORITY_ADDR, &addr->addr,
605                              (v6) ? 128 : 32, addr->policy_table, NULL, NULL,
606                              true);
607         set_ip_source_policy(enabled, v6, IPRULE_PRIORITY_ADDR_MASK,
608                              &addr->addr, addr->mask, addr->policy_table, NULL,
609                              NULL, false);
610 }
611 
612 static void
613 interface_update_proto_addr(struct vlist_tree *tree,
614                             struct vlist_node *node_new,
615                             struct vlist_node *node_old)
616 {
617         struct interface_ip_settings *ip;
618         struct interface *iface;
619         struct device *dev;
620         struct device_addr *a_new = NULL, *a_old = NULL;
621         bool replace = false;
622         bool keep = false;
623         bool v6 = false;
624 
625         ip = container_of(tree, struct interface_ip_settings, addr);
626         iface = ip->iface;
627         dev = iface->l3_dev.dev;
628 
629         if (!node_new || !node_old)
630                 iface->updated |= IUF_ADDRESS;
631 
632         if (node_new) {
633                 a_new = container_of(node_new, struct device_addr, node);
634 
635                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
636                     !a_new->broadcast) {
637 
638                         /* /31 and /32 addressing need 255.255.255.255
639                          * as broadcast address. */
640                         if (a_new->mask >= 31) {
641                                 a_new->broadcast = (uint32_t) ~0;
642                         } else {
643                                 uint32_t mask = ~0;
644                                 uint32_t *a = (uint32_t *) &a_new->addr;
645 
646                                 mask >>= a_new->mask;
647                                 a_new->broadcast = *a | htonl(mask);
648                         }
649                 }
650         }
651 
652         if (node_old)
653                 a_old = container_of(node_old, struct device_addr, node);
654 
655         if (a_new && a_old) {
656                 keep = true;
657 
658                 if (a_old->flags != a_new->flags || a_old->failed)
659                         keep = false;
660 
661                 if (a_old->valid_until != a_new->valid_until ||
662                                 a_old->preferred_until != a_new->preferred_until)
663                         replace = true;
664 
665                 if (((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4) &&
666                      (a_new->broadcast != a_old->broadcast ||
667                       a_new->point_to_point != a_old->point_to_point))
668                         keep = false;
669         }
670 
671         if (node_old) {
672                 if (a_old->enabled && !keep) {
673                         /*
674                          * This is needed for source routing to work correctly. If a device
675                          * has two connections to a network using the same subnet, adding
676                          * only the network-rule will cause packets to be routed through the
677                          * first matching network (source IP matches both masks)
678                          */
679                         if (a_old->policy_table)
680                                 interface_add_addr_rules(a_old, false);
681 
682                         if (!(a_old->flags & DEVADDR_EXTERNAL)) {
683                                 interface_handle_subnet_route(iface, a_old, false);
684                                 system_del_address(dev, a_old);
685                         }
686                 }
687                 free(a_old->pclass);
688                 free(a_old);
689         }
690 
691         if (node_new) {
692                 a_new->enabled = true;
693 
694                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
695                                 v6 = true;
696 
697                 a_new->policy_table = (v6) ? iface->ip6table : iface->ip4table;
698 
699                 if (!keep || replace) {
700                         if (!(a_new->flags & DEVADDR_EXTERNAL)) {
701                                 if (system_add_address(dev, a_new))
702                                         a_new->failed = true;
703 
704                                 if (iface->metric || a_new->policy_table)
705                                         interface_handle_subnet_route(iface, a_new, true);
706                         }
707 
708                         if (!keep) {
709                                 if (a_new->policy_table)
710                                         interface_add_addr_rules(a_new, true);
711                         }
712                 }
713         }
714 }
715 
716 static bool
717 enable_route(struct interface_ip_settings *ip, struct device_route *route)
718 {
719         if (ip->no_defaultroute && !route->mask)
720                 return false;
721 
722         return ip->enabled;
723 }
724 
725 static void
726 interface_update_proto_neighbor(struct vlist_tree *tree,
727                                 struct vlist_node * node_new,
728                                 struct vlist_node *node_old)
729 {
730         struct device *dev;
731         struct device_neighbor *neighbor_old, *neighbor_new;
732         struct interface_ip_settings *ip;
733         bool keep = false;
734 
735         ip = container_of(tree, struct interface_ip_settings, neighbor);
736         dev = ip->iface->l3_dev.dev;
737 
738         neighbor_old = container_of(node_old, struct device_neighbor, node);
739         neighbor_new = container_of(node_new, struct device_neighbor, node);
740 
741         if (node_old && node_new) {
742                 keep = (!memcmp(neighbor_old->macaddr, neighbor_new->macaddr, sizeof(neighbor_old->macaddr)) &&
743                         (neighbor_old->proxy == neighbor_new->proxy) &&
744                         (neighbor_old->router == neighbor_new->router));
745         }
746 
747         if (node_old) {
748                 if (!keep && neighbor_old->enabled)
749                         system_del_neighbor(dev, neighbor_old);
750 
751                 free(neighbor_old);
752         }
753 
754         if (node_new) {
755                 if (!keep && ip->enabled)
756                         if (system_add_neighbor(dev, neighbor_new))
757                                 neighbor_new->failed = true;
758 
759                 neighbor_new->enabled = ip->enabled;
760         }
761 }
762 
763 static void
764 interface_update_proto_route(struct vlist_tree *tree,
765                              struct vlist_node *node_new,
766                              struct vlist_node *node_old)
767 {
768         struct interface_ip_settings *ip;
769         struct interface *iface;
770         struct device *dev;
771         struct device_route *route_old, *route_new;
772         bool keep = false;
773 
774         ip = container_of(tree, struct interface_ip_settings, route);
775         iface = ip->iface;
776         dev = iface->l3_dev.dev;
777 
778         if (!node_new || !node_old)
779                 iface->updated |= IUF_ROUTE;
780 
781         route_old = container_of(node_old, struct device_route, node);
782         route_new = container_of(node_new, struct device_route, node);
783 
784         if (node_old && node_new)
785                 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop)) &&
786                         (route_old->mtu == route_new->mtu) && (route_old->type == route_new->type) &&
787                         (route_old->proto == route_new->proto) && !route_old->failed;
788 
789         if (node_old) {
790                 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
791                         system_del_route(dev, route_old);
792 
793                 free(route_old);
794         }
795 
796         if (node_new) {
797                 bool _enabled = enable_route(ip, route_new);
798 
799                 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
800                         if (system_add_route(dev, route_new))
801                                 route_new->failed = true;
802 
803                 route_new->iface = iface;
804                 route_new->enabled = _enabled;
805         }
806 }
807 
808 static void
809 interface_update_host_route(struct vlist_tree *tree,
810                              struct vlist_node *node_new,
811                              struct vlist_node *node_old)
812 {
813         struct interface *iface;
814         struct device *dev;
815         struct device_route *route_old, *route_new;
816 
817         iface = container_of(tree, struct interface, host_routes);
818         dev = iface->l3_dev.dev;
819 
820         route_old = container_of(node_old, struct device_route, node);
821         route_new = container_of(node_new, struct device_route, node);
822 
823         if (node_old) {
824                 system_del_route(dev, route_old);
825                 free(route_old);
826         }
827 
828         if (node_new) {
829                 if (system_add_route(dev, route_new))
830                         route_new->failed = true;
831         }
832 }
833 
834 static void
835 random_ifaceid(struct in6_addr *addr)
836 {
837         static bool initialized = false;
838         struct timeval t;
839 
840         if (!initialized) {
841                 long int seed = 0;
842                 gettimeofday(&t, NULL);
843                 seed = t.tv_sec ^ t.tv_usec ^ getpid();
844                 srand48(seed);
845                 initialized = true;
846         }
847         addr->s6_addr32[2] = (uint32_t)mrand48();
848         addr->s6_addr32[3] = (uint32_t)mrand48();
849 }
850 
851 static bool
852 eui64_ifaceid(struct interface *iface, struct in6_addr *addr)
853 {
854         struct device_settings st;
855 
856         device_merge_settings(iface->l3_dev.dev, &st);
857 
858         if (!(st.flags & DEV_OPT_MACADDR))
859                 return false;
860 
861         /* get mac address */
862         uint8_t *ifaceid = addr->s6_addr + 8;
863         memcpy(ifaceid, st.macaddr, 3);
864         memcpy(ifaceid + 5, st.macaddr + 3, 3);
865         ifaceid[3] = 0xff;
866         ifaceid[4] = 0xfe;
867         ifaceid[0] ^= 0x02;
868 
869         return true;
870 }
871 
872 static bool
873 generate_ifaceid(struct interface *iface, struct in6_addr *addr)
874 {
875         bool ret = true;
876 
877         /* generate new iface id */
878         switch (iface->assignment_iface_id_selection) {
879         case IFID_FIXED:
880                 /* fixed */
881                 /* copy host part from assignment_fixed_iface_id */
882                 memcpy(addr->s6_addr + 8, iface->assignment_fixed_iface_id.s6_addr + 8, 8);
883                 break;
884         case IFID_RANDOM:
885                 /* randomize last 64 bits */
886                 random_ifaceid(addr);
887                 break;
888         case IFID_EUI64:
889                 /* eui64 */
890                 ret = eui64_ifaceid(iface, addr);
891                 break;
892         default:
893                 ret = false;
894                 break;
895         }
896         return ret;
897 }
898 
899 static void
900 interface_set_prefix_address(struct device_prefix_assignment *assignment,
901                 const struct device_prefix *prefix, struct interface *iface, bool add)
902 {
903         const struct interface *uplink = prefix->iface;
904         if (!iface->l3_dev.dev)
905                 return;
906 
907         struct device *l3_downlink = iface->l3_dev.dev;
908 
909         struct device_addr addr;
910         struct device_route route;
911         memset(&addr, 0, sizeof(addr));
912         memset(&route, 0, sizeof(route));
913 
914         addr.addr.in6 = assignment->addr;
915         addr.mask = assignment->length;
916         addr.flags = DEVADDR_INET6;
917         addr.preferred_until = prefix->preferred_until;
918         addr.valid_until = prefix->valid_until;
919 
920         route.flags = DEVADDR_INET6;
921         route.mask = addr.mask < 64 ? 64 : addr.mask;
922         route.addr = addr.addr;
923 
924         if (!add && assignment->enabled) {
925                 time_t now = system_get_rtime();
926 
927                 addr.preferred_until = now;
928                 if (!addr.valid_until || addr.valid_until - now > 7200)
929                         addr.valid_until = now + 7200;
930 
931                 if (iface->ip6table)
932                         set_ip_source_policy(false, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
933                                         addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
934 
935                 if (prefix->iface) {
936                         if (prefix->iface->ip6table)
937                                 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &addr.addr,
938                                                 addr.mask, prefix->iface->ip6table, iface, NULL, true);
939 
940                         set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT, &addr.addr,
941                                                         addr.mask, 0, iface, "unreachable", true);
942                 }
943 
944                 clear_if_addr(&route.addr, route.mask);
945                 interface_set_route_info(iface, &route);
946 
947                 system_del_route(l3_downlink, &route);
948                 system_add_address(l3_downlink, &addr);
949 
950                 assignment->addr = in6addr_any;
951                 assignment->enabled = false;
952         } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) {
953                 if (IN6_IS_ADDR_UNSPECIFIED(&addr.addr.in6)) {
954                         addr.addr.in6 = prefix->addr;
955                         addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
956                         if (!generate_ifaceid(iface, &addr.addr.in6))
957                                 return;
958 
959                         assignment->addr = addr.addr.in6;
960                         route.addr = addr.addr;
961                 }
962 
963                 addr.flags |= DEVADDR_OFFLINK;
964                 if (system_add_address(l3_downlink, &addr))
965                         return;
966 
967                 if (!assignment->enabled) {
968                         if (iface->ip6table)
969                                 set_ip_source_policy(true, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
970                                                 addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
971 
972                         if (prefix->iface) {
973                                 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr,
974                                                 addr.mask, 0, iface, "unreachable", true);
975 
976                                 if (prefix->iface->ip6table)
977                                         set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr,
978                                                         addr.mask, prefix->iface->ip6table, iface, NULL, true);
979                         }
980                 }
981 
982                 clear_if_addr(&route.addr, route.mask);
983                 interface_set_route_info(iface, &route);
984 
985                 system_add_route(l3_downlink, &route);
986 
987                 if (uplink && uplink->l3_dev.dev && !(l3_downlink->settings.flags & DEV_OPT_MTU6)) {
988                         int mtu = system_update_ipv6_mtu(uplink->l3_dev.dev, 0);
989                         int mtu_old = system_update_ipv6_mtu(l3_downlink, 0);
990 
991                         if (mtu > 0 && mtu_old != mtu) {
992                                 if (system_update_ipv6_mtu(l3_downlink, mtu) < 0 && mtu < mtu_old)
993                                         netifd_log_message(L_WARNING, "Failed to set IPv6 mtu to %d "
994                                                         "on interface '%s'\n", mtu, iface->name);
995                         }
996                 }
997 
998                 assignment->enabled = true;
999         }
1000 }
1001 
1002 static bool interface_prefix_assign(struct list_head *list,
1003                 struct device_prefix_assignment *assign)
1004 {
1005         int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
1006         struct device_prefix_assignment *c;
1007         list_for_each_entry(c, list, head) {
1008                 if (assign->assigned != -1) {
1009                         if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
1010                                 list_add_tail(&assign->head, &c->head);
1011                                 return true;
1012                         }
1013                 } else if (assign->assigned == -1) {
1014                         current = (current + asize) & (~asize);
1015                         if (current + asize < c->assigned) {
1016                                 assign->assigned = current;
1017                                 list_add_tail(&assign->head, &c->head);
1018                                 return true;
1019                         }
1020                 }
1021                 current = (c->assigned + (1 << (64 - c->length)));
1022         }
1023         return false;
1024 }
1025 
1026 /*
1027  * Sorting of assignment entries:
1028  * Primary on assignment length: smallest assignment first
1029  * Secondary on assignment weight: highest weight first
1030  * Finally alphabetical order of interface names
1031  */
1032 static int prefix_assignment_cmp(const void *k1, const void *k2, void *ptr)
1033 {
1034         const struct device_prefix_assignment *a1 = k1, *a2 = k2;
1035 
1036         if (a1->length != a2->length)
1037                 return a1->length - a2->length;
1038 
1039         if (a1->weight != a2->weight)
1040                 return a2->weight - a1->weight;
1041 
1042         return strcmp(a1->name, a2->name);
1043 }
1044 
1045 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
1046 {
1047         struct device_prefix_assignment *c;
1048         struct interface *iface;
1049 
1050         /* Delete all assignments */
1051         while (!list_empty(&prefix->assignments)) {
1052                 c = list_first_entry(&prefix->assignments,
1053                                 struct device_prefix_assignment, head);
1054                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1055                         interface_set_prefix_address(c, prefix, iface, false);
1056                 list_del(&c->head);
1057                 free(c);
1058         }
1059 
1060         if (!setup)
1061                 return;
1062 
1063         /* End-of-assignment sentinel */
1064         c = malloc(sizeof(*c) + 1);
1065         if (!c)
1066                 return;
1067 
1068         c->assigned = 1 << (64 - prefix->length);
1069         c->length = 64;
1070         c->name[0] = 0;
1071         c->addr = in6addr_any;
1072         list_add(&c->head, &prefix->assignments);
1073 
1074         /* Excluded prefix */
1075         if (prefix->excl_length > 0) {
1076                 const char name[] = "!excluded";
1077                 c = malloc(sizeof(*c) + sizeof(name));
1078                 if (c) {
1079                         c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
1080                                         ((1 << (64 - prefix->length)) - 1);
1081                         c->length = prefix->excl_length;
1082                         c->addr = in6addr_any;
1083                         memcpy(c->name, name, sizeof(name));
1084                         list_add(&c->head, &prefix->assignments);
1085                 }
1086         }
1087 
1088         bool assigned_any = false;
1089         struct {
1090                 struct avl_node node;
1091         } *entry, *n_entry;
1092         struct avl_tree assign_later;
1093 
1094         avl_init(&assign_later, prefix_assignment_cmp, false, NULL);
1095 
1096         vlist_for_each_element(&interfaces, iface, node) {
1097                 if (iface->assignment_length < 48 ||
1098                                 iface->assignment_length > 64)
1099                         continue;
1100 
1101                 /* Test whether there is a matching class */
1102                 if (!list_empty(&iface->assignment_classes)) {
1103                         bool found = false;
1104 
1105                         struct interface_assignment_class *c;
1106                         list_for_each_entry(c, &iface->assignment_classes, head) {
1107                                 if (!strcmp(c->name, prefix->pclass)) {
1108                                         found = true;
1109                                         break;
1110                                 }
1111                         }
1112 
1113                         if (!found)
1114                                 continue;
1115                 }
1116 
1117                 size_t namelen = strlen(iface->name) + 1;
1118                 c = malloc(sizeof(*c) + namelen);
1119                 if (!c)
1120                         continue;
1121 
1122                 c->length = iface->assignment_length;
1123                 c->assigned = iface->assignment_hint;
1124                 c->weight = iface->assignment_weight;
1125                 c->addr = in6addr_any;
1126                 c->enabled = false;
1127                 memcpy(c->name, iface->name, namelen);
1128 
1129                 /* First process all custom assignments, put all others in later-list */
1130                 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
1131                         if (c->assigned != -1) {
1132                                 c->assigned = -1;
1133                                 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
1134                                                 "of size %hhu for %s, trying other\n", c->length, c->name);
1135                         }
1136 
1137                         entry = calloc(1, sizeof(*entry));
1138                         if (!entry) {
1139                                 free(c);
1140                                 continue;
1141                         }
1142 
1143                         entry->node.key = c;
1144                         avl_insert(&assign_later, &entry->node);
1145                 }
1146 
1147                 if (c->assigned != -1)
1148                         assigned_any = true;
1149         }
1150 
1151         /* Then try to assign all other + failed custom assignments */
1152         avl_for_each_element_safe(&assign_later, entry, node, n_entry) {
1153                 bool assigned = false;
1154 
1155                 c = (struct device_prefix_assignment *)entry->node.key;
1156                 avl_delete(&assign_later, &entry->node);
1157 
1158                 do {
1159                         assigned = interface_prefix_assign(&prefix->assignments, c);
1160                 } while (!assigned && ++c->length <= 64);
1161 
1162                 if (!assigned) {
1163                         netifd_log_message(L_WARNING, "Failed to assign subprefix "
1164                                         "of size %hhu for %s\n", c->length, c->name);
1165                         free(c);
1166                 } else
1167                         assigned_any = true;
1168 
1169                 free(entry);
1170         }
1171 
1172         list_for_each_entry(c, &prefix->assignments, head)
1173                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1174                         interface_set_prefix_address(c, prefix, iface, true);
1175 
1176         if (!assigned_any)
1177                 netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them "
1178                                 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
1179 }
1180 
1181 
1182 void interface_refresh_assignments(bool hint)
1183 {
1184         static bool refresh = false;
1185         if (!hint && refresh) {
1186                 struct device_prefix *p;
1187                 list_for_each_entry(p, &prefixes, head)
1188                         interface_update_prefix_assignments(p, true);
1189         }
1190         refresh = hint;
1191 }
1192 
1193 void interface_update_prefix_delegation(struct interface_ip_settings *ip)
1194 {
1195         struct device_prefix *prefix;
1196 
1197         vlist_for_each_element(&ip->prefix, prefix, node) {
1198                 interface_update_prefix_assignments(prefix, !ip->no_delegation);
1199 
1200                 if (ip->no_delegation) {
1201                         if (prefix->head.next)
1202                                 list_del(&prefix->head);
1203                 } else
1204                         list_add(&prefix->head, &prefixes);
1205         }
1206 }
1207 
1208 static void
1209 interface_update_prefix(struct vlist_tree *tree,
1210                              struct vlist_node *node_new,
1211                              struct vlist_node *node_old)
1212 {
1213         struct device_prefix *prefix_old, *prefix_new;
1214         prefix_old = container_of(node_old, struct device_prefix, node);
1215         prefix_new = container_of(node_new, struct device_prefix, node);
1216 
1217         struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix);
1218         if (tree && (!node_new || !node_old))
1219                 ip->iface->updated |= IUF_PREFIX;
1220 
1221         struct device_route route;
1222         memset(&route, 0, sizeof(route));
1223         route.flags = DEVADDR_INET6;
1224         route.metric = INT32_MAX;
1225         route.mask = (node_new) ? prefix_new->length : prefix_old->length;
1226         route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
1227 
1228 
1229         struct device_prefix_assignment *c;
1230         struct interface *iface;
1231 
1232         if (node_old && node_new) {
1233                 /* Move assignments and refresh addresses to update valid times */
1234                 list_splice(&prefix_old->assignments, &prefix_new->assignments);
1235 
1236                 list_for_each_entry(c, &prefix_new->assignments, head)
1237                         if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1238                                 interface_set_prefix_address(c, prefix_new, iface, true);
1239 
1240                 if (prefix_new->preferred_until != prefix_old->preferred_until ||
1241                                 prefix_new->valid_until != prefix_old->valid_until)
1242                         ip->iface->updated |= IUF_PREFIX;
1243         } else if (node_new) {
1244                 /* Set null-route to avoid routing loops */
1245                 system_add_route(NULL, &route);
1246 
1247                 if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)
1248                         interface_update_prefix_assignments(prefix_new, true);
1249         } else if (node_old) {
1250                 /* Remove null-route */
1251                 interface_update_prefix_assignments(prefix_old, false);
1252                 system_del_route(NULL, &route);
1253         }
1254 
1255         if (node_old) {
1256                 if (prefix_old->head.next)
1257                         list_del(&prefix_old->head);
1258                 free(prefix_old);
1259         }
1260 
1261         if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation))
1262                 list_add(&prefix_new->head, &prefixes);
1263 
1264 }
1265 
1266 struct device_prefix*
1267 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
1268                 uint8_t length, time_t valid_until, time_t preferred_until,
1269                 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass)
1270 {
1271         if (!pclass)
1272                 pclass = (iface) ? iface->name : "local";
1273 
1274         struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1);
1275         if (!prefix)
1276                 return NULL;
1277 
1278         prefix->length = length;
1279         prefix->addr = *addr;
1280         prefix->preferred_until = preferred_until;
1281         prefix->valid_until = valid_until;
1282         prefix->iface = iface;
1283         INIT_LIST_HEAD(&prefix->assignments);
1284 
1285         if (excl_addr) {
1286                 prefix->excl_addr = *excl_addr;
1287                 prefix->excl_length = excl_length;
1288         }
1289 
1290         strcpy(prefix->pclass, pclass);
1291 
1292         if (iface)
1293                 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
1294         else
1295                 interface_update_prefix(NULL, &prefix->node, NULL);
1296 
1297         return prefix;
1298 }
1299 
1300 void
1301 interface_ip_set_ula_prefix(const char *prefix)
1302 {
1303         char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
1304         if (prefix)
1305                 strncpy(buf, prefix, sizeof(buf) - 1);
1306         char *prefixaddr = strtok_r(buf, "/", &saveptr);
1307 
1308         struct in6_addr addr;
1309         if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
1310                 if (ula_prefix) {
1311                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
1312                         ula_prefix = NULL;
1313                 }
1314                 return;
1315         }
1316 
1317         int length;
1318         char *prefixlen = strtok_r(NULL, ",", &saveptr);
1319         if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
1320                 return;
1321 
1322         if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
1323                         ula_prefix->length != length) {
1324                 if (ula_prefix)
1325                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
1326 
1327                 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
1328                                 0, 0, NULL, 0, NULL);
1329         }
1330 }
1331 
1332 static void
1333 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
1334 {
1335         struct dns_server *s;
1336 
1337         s = calloc(1, sizeof(*s));
1338         if (!s)
1339                 return;
1340 
1341         s->af = AF_INET;
1342         if (inet_pton(s->af, str, &s->addr.in))
1343                 goto add;
1344 
1345         s->af = AF_INET6;
1346         if (inet_pton(s->af, str, &s->addr.in))
1347                 goto add;
1348 
1349         free(s);
1350         return;
1351 
1352 add:
1353         D(INTERFACE, "Add IPv%c DNS server: %s\n",
1354           s->af == AF_INET6 ? '6' : '4', str);
1355         vlist_simple_add(&ip->dns_servers, &s->node);
1356 }
1357 
1358 void
1359 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
1360 {
1361         struct blob_attr *cur;
1362         int rem;
1363 
1364         blobmsg_for_each_attr(cur, list, rem) {
1365                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1366                         continue;
1367 
1368                 if (!blobmsg_check_attr(cur, false))
1369                         continue;
1370 
1371                 interface_add_dns_server(ip, blobmsg_data(cur));
1372         }
1373 }
1374 
1375 static void
1376 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
1377 {
1378         struct dns_search_domain *s;
1379         int len = strlen(str);
1380 
1381         s = calloc(1, sizeof(*s) + len + 1);
1382         if (!s)
1383                 return;
1384 
1385         D(INTERFACE, "Add DNS search domain: %s\n", str);
1386         memcpy(s->name, str, len);
1387         vlist_simple_add(&ip->dns_search, &s->node);
1388 }
1389 
1390 void
1391 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
1392 {
1393         struct blob_attr *cur;
1394         int rem;
1395 
1396         blobmsg_for_each_attr(cur, list, rem) {
1397                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1398                         continue;
1399 
1400                 if (!blobmsg_check_attr(cur, false))
1401                         continue;
1402 
1403                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
1404         }
1405 }
1406 
1407 static void
1408 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip, const char *dev)
1409 {
1410         struct dns_server *s;
1411         struct dns_search_domain *d;
1412         const char *str;
1413         char buf[INET6_ADDRSTRLEN];
1414 
1415         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
1416                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
1417                 if (!str)
1418                         continue;
1419 
1420                 if (s->af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&s->addr.in6))
1421                         fprintf(f, "nameserver %s%%%s\n", str, dev);
1422                 else
1423                         fprintf(f, "nameserver %s\n", str);
1424         }
1425 
1426         vlist_simple_for_each_element(&ip->dns_search, d, node) {
1427                 fprintf(f, "search %s\n", d->name);
1428         }
1429 }
1430 
1431 /* Sorting of interface resolver entries :               */
1432 /* Primary on interface dns_metric : lowest metric first */
1433 /* Secondary on interface metric : lowest metric first   */
1434 /* Finally alphabetical order of interface names         */
1435 static int resolv_conf_iface_cmp(const void *k1, const void *k2, void *ptr)
1436 {
1437         const struct interface *iface1 = k1, *iface2 = k2;
1438 
1439         if (iface1->dns_metric != iface2->dns_metric)
1440                 return iface1->dns_metric - iface2->dns_metric;
1441 
1442         if (iface1->metric != iface2->metric)
1443                 return iface1->metric - iface2->metric;
1444 
1445         return strcmp(iface1->name, iface2->name);
1446 }
1447 
1448 static void
1449 __interface_write_dns_entries(FILE *f, const char *jail)
1450 {
1451         struct interface *iface;
1452         struct {
1453                 struct avl_node node;
1454         } *entry, *n_entry;
1455         struct avl_tree resolv_conf_iface_entries;
1456 
1457         avl_init(&resolv_conf_iface_entries, resolv_conf_iface_cmp, false, NULL);
1458 
1459         vlist_for_each_element(&interfaces, iface, node) {
1460                 if (iface->state != IFS_UP)
1461                         continue;
1462 
1463                 if (jail && (!iface->jail || strcmp(jail, iface->jail)))
1464                         continue;
1465 
1466                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
1467                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
1468                     vlist_simple_empty(&iface->config_ip.dns_search) &&
1469                     vlist_simple_empty(&iface->config_ip.dns_servers))
1470                         continue;
1471 
1472                 entry = calloc(1, sizeof(*entry));
1473                 if (!entry)
1474                         continue;
1475 
1476                 entry->node.key = iface;
1477                 avl_insert(&resolv_conf_iface_entries, &entry->node);
1478         }
1479 
1480         avl_for_each_element(&resolv_conf_iface_entries, entry, node) {
1481                 iface = (struct interface *)entry->node.key;
1482                 struct device *dev = iface->l3_dev.dev;
1483 
1484                 fprintf(f, "# Interface %s\n", iface->name);
1485 
1486                 write_resolv_conf_entries(f, &iface->config_ip, dev->ifname);
1487 
1488                 if (!iface->proto_ip.no_dns)
1489                         write_resolv_conf_entries(f, &iface->proto_ip, dev->ifname);
1490         }
1491 
1492         avl_remove_all_elements(&resolv_conf_iface_entries, entry, node, n_entry)
1493                 free(entry);
1494 }
1495 
1496 void
1497 interface_write_resolv_conf(const char *jail)
1498 {
1499         size_t plen = (jail ? strlen(jail) + 1 : 0 ) + strlen(resolv_conf) + 1;
1500         char *path = alloca(plen);
1501         char *dpath = alloca(plen);
1502         char *tmppath = alloca(plen + 4);
1503         FILE *f;
1504         uint32_t crcold, crcnew;
1505 
1506         if (jail) {
1507                 sprintf(path, "/tmp/resolv.conf-%s.d/resolv.conf.auto", jail);
1508                 strcpy(dpath, path);
1509                 dpath = dirname(dpath);
1510                 mkdir(dpath, 0755);
1511         } else {
1512                 strcpy(path, resolv_conf);
1513         }
1514 
1515         sprintf(tmppath, "%s.tmp", path);
1516         unlink(tmppath);
1517         f = fopen(tmppath, "w+");
1518         if (!f) {
1519                 D(INTERFACE, "Failed to open %s for writing\n", path);
1520                 return;
1521         }
1522 
1523         __interface_write_dns_entries(f, jail);
1524 
1525         fflush(f);
1526         rewind(f);
1527         crcnew = crc32_file(f);
1528         fclose(f);
1529 
1530         crcold = crcnew + 1;
1531         f = fopen(path, "r");
1532         if (f) {
1533                 crcold = crc32_file(f);
1534                 fclose(f);
1535         }
1536 
1537         if (crcold == crcnew) {
1538                 unlink(tmppath);
1539         } else if (rename(tmppath, path) < 0) {
1540                 D(INTERFACE, "Failed to replace %s\n", path);
1541                 unlink(tmppath);
1542         }
1543 }
1544 
1545 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
1546 {
1547         struct device_addr *addr;
1548         struct device_route *route;
1549         struct device_neighbor *neighbor;
1550         struct device *dev;
1551         struct interface *iface;
1552 
1553         ip->enabled = enabled;
1554         iface = ip->iface;
1555         dev = iface->l3_dev.dev;
1556         if (!dev)
1557                 return;
1558 
1559         vlist_for_each_element(&ip->addr, addr, node) {
1560                 bool v6 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6) ? true : false;
1561 
1562                 if (addr->flags & DEVADDR_EXTERNAL)
1563                         continue;
1564 
1565                 if (addr->enabled == enabled)
1566                         continue;
1567 
1568                 if (enabled) {
1569                         system_add_address(dev, addr);
1570 
1571                         addr->policy_table = (v6) ? iface->ip6table : iface->ip4table;
1572                         if (iface->metric || addr->policy_table)
1573                                 interface_handle_subnet_route(iface, addr, true);
1574 
1575                         if (addr->policy_table)
1576                                 interface_add_addr_rules(addr, true);
1577                 } else {
1578                         interface_handle_subnet_route(iface, addr, false);
1579                         system_del_address(dev, addr);
1580 
1581                         if (addr->policy_table)
1582                                 interface_add_addr_rules(addr, false);
1583                 }
1584                 addr->enabled = enabled;
1585         }
1586 
1587         vlist_for_each_element(&ip->route, route, node) {
1588                 bool _enabled = enabled;
1589 
1590                 if (route->flags & DEVADDR_EXTERNAL)
1591                         continue;
1592 
1593                 if (!enable_route(ip, route))
1594                         _enabled = false;
1595                 if (route->enabled == _enabled)
1596                         continue;
1597 
1598                 if (_enabled) {
1599                         interface_set_route_info(ip->iface, route);
1600 
1601                         if (system_add_route(dev, route))
1602                                 route->failed = true;
1603                 } else
1604                         system_del_route(dev, route);
1605                 route->enabled = _enabled;
1606         }
1607 
1608         vlist_for_each_element(&ip->neighbor, neighbor, node) {
1609                 if (neighbor->enabled == enabled)
1610                         continue;
1611 
1612                 if (enabled) {
1613                         if(system_add_neighbor(dev, neighbor))
1614                                 neighbor->failed = true;
1615                 } else
1616                         system_del_neighbor(dev, neighbor);
1617 
1618                 neighbor->enabled = enabled;
1619         }
1620 
1621         struct device_prefix *c;
1622         struct device_prefix_assignment *a;
1623         list_for_each_entry(c, &prefixes, head)
1624                 list_for_each_entry(a, &c->assignments, head)
1625                         if (!strcmp(a->name, ip->iface->name))
1626                                 interface_set_prefix_address(a, c, ip->iface, enabled);
1627 
1628         if (ip->iface->policy_rules_set != enabled &&
1629             ip->iface->l3_dev.dev) {
1630                 set_ip_lo_policy(enabled, true, ip->iface);
1631                 set_ip_lo_policy(enabled, false, ip->iface);
1632 
1633                 set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex,
1634                         NULL, 0, 0, ip->iface, "failed_policy", true);
1635                 ip->iface->policy_rules_set = enabled;
1636         }
1637 }
1638 
1639 void
1640 interface_ip_update_start(struct interface_ip_settings *ip)
1641 {
1642         if (ip != &ip->iface->config_ip) {
1643                 vlist_simple_update(&ip->dns_servers);
1644                 vlist_simple_update(&ip->dns_search);
1645         }
1646         vlist_update(&ip->route);
1647         vlist_update(&ip->addr);
1648         vlist_update(&ip->prefix);
1649         vlist_update(&ip->neighbor);
1650 }
1651 
1652 void
1653 interface_ip_update_complete(struct interface_ip_settings *ip)
1654 {
1655         vlist_simple_flush(&ip->dns_servers);
1656         vlist_simple_flush(&ip->dns_search);
1657         vlist_flush(&ip->route);
1658         vlist_flush(&ip->addr);
1659         vlist_flush(&ip->prefix);
1660         vlist_flush(&ip->neighbor);
1661         interface_write_resolv_conf(ip->iface->jail);
1662 }
1663 
1664 void
1665 interface_ip_flush(struct interface_ip_settings *ip)
1666 {
1667         if (ip == &ip->iface->proto_ip)
1668                 vlist_flush_all(&ip->iface->host_routes);
1669         vlist_simple_flush_all(&ip->dns_servers);
1670         vlist_simple_flush_all(&ip->dns_search);
1671         vlist_flush_all(&ip->route);
1672         vlist_flush_all(&ip->addr);
1673         vlist_flush_all(&ip->neighbor);
1674         vlist_flush_all(&ip->prefix);
1675 }
1676 
1677 static void
1678 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
1679 {
1680         ip->iface = iface;
1681         ip->enabled = true;
1682         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
1683         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
1684         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
1685         vlist_init(&ip->neighbor, neighbor_cmp, interface_update_proto_neighbor);
1686         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
1687         vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
1688 }
1689 
1690 void
1691 interface_ip_init(struct interface *iface)
1692 {
1693         __interface_ip_init(&iface->proto_ip, iface);
1694         __interface_ip_init(&iface->config_ip, iface);
1695         vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
1696 }
1697 
1698 static void
1699 interface_ip_valid_until_handler(struct uloop_timeout *t)
1700 {
1701         time_t now = system_get_rtime();
1702         struct interface *iface;
1703         vlist_for_each_element(&interfaces, iface, node) {
1704                 if (iface->state != IFS_UP)
1705                         continue;
1706 
1707                 struct device_addr *addr, *addrp;
1708                 struct device_route *route, *routep;
1709                 struct device_prefix *pref, *prefp;
1710 
1711                 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
1712                         if (addr->valid_until && addr->valid_until < now)
1713                                 vlist_delete(&iface->proto_ip.addr, &addr->node);
1714 
1715                 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
1716                         if (route->valid_until && route->valid_until < now)
1717                                 vlist_delete(&iface->proto_ip.route, &route->node);
1718 
1719                 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
1720                         if (pref->valid_until && pref->valid_until < now)
1721                                 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1722 
1723         }
1724 
1725         uloop_timeout_set(t, 1000);
1726 }
1727 
1728 static void __init
1729 interface_ip_init_worker(void)
1730 {
1731         valid_until_timeout.cb = interface_ip_valid_until_handler;
1732         uloop_timeout_set(&valid_until_timeout, 1000);
1733 }
1734 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt