• 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 | DEVADDR_OFFLINK;
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                 if (system_add_address(l3_downlink, &addr))
964                         return;
965 
966                 if (!assignment->enabled) {
967                         if (iface->ip6table)
968                                 set_ip_source_policy(true, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
969                                                 addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
970 
971                         if (prefix->iface) {
972                                 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr,
973                                                 addr.mask, 0, iface, "unreachable", true);
974 
975                                 if (prefix->iface->ip6table)
976                                         set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr,
977                                                         addr.mask, prefix->iface->ip6table, iface, NULL, true);
978                         }
979                 }
980 
981                 clear_if_addr(&route.addr, route.mask);
982                 interface_set_route_info(iface, &route);
983 
984                 system_add_route(l3_downlink, &route);
985 
986                 if (uplink && uplink->l3_dev.dev && !(l3_downlink->settings.flags & DEV_OPT_MTU6)) {
987                         int mtu = system_update_ipv6_mtu(uplink->l3_dev.dev, 0);
988                         int mtu_old = system_update_ipv6_mtu(l3_downlink, 0);
989 
990                         if (mtu > 0 && mtu_old != mtu) {
991                                 if (system_update_ipv6_mtu(l3_downlink, mtu) < 0 && mtu < mtu_old)
992                                         netifd_log_message(L_WARNING, "Failed to set IPv6 mtu to %d "
993                                                         "on interface '%s'\n", mtu, iface->name);
994                         }
995                 }
996 
997                 assignment->enabled = true;
998         }
999 }
1000 
1001 static bool interface_prefix_assign(struct list_head *list,
1002                 struct device_prefix_assignment *assign)
1003 {
1004         int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
1005         struct device_prefix_assignment *c;
1006         list_for_each_entry(c, list, head) {
1007                 if (assign->assigned != -1) {
1008                         if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
1009                                 list_add_tail(&assign->head, &c->head);
1010                                 return true;
1011                         }
1012                 } else if (assign->assigned == -1) {
1013                         current = (current + asize) & (~asize);
1014                         if (current + asize < c->assigned) {
1015                                 assign->assigned = current;
1016                                 list_add_tail(&assign->head, &c->head);
1017                                 return true;
1018                         }
1019                 }
1020                 current = (c->assigned + (1 << (64 - c->length)));
1021         }
1022         return false;
1023 }
1024 
1025 /*
1026  * Sorting of assignment entries:
1027  * Primary on assignment length: smallest assignment first
1028  * Secondary on assignment weight: highest weight first
1029  * Finally alphabetical order of interface names
1030  */
1031 static int prefix_assignment_cmp(const void *k1, const void *k2, void *ptr)
1032 {
1033         const struct device_prefix_assignment *a1 = k1, *a2 = k2;
1034 
1035         if (a1->length != a2->length)
1036                 return a1->length - a2->length;
1037 
1038         if (a1->weight != a2->weight)
1039                 return a2->weight - a1->weight;
1040 
1041         return strcmp(a1->name, a2->name);
1042 }
1043 
1044 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
1045 {
1046         struct device_prefix_assignment *c;
1047         struct interface *iface;
1048 
1049         /* Delete all assignments */
1050         while (!list_empty(&prefix->assignments)) {
1051                 c = list_first_entry(&prefix->assignments,
1052                                 struct device_prefix_assignment, head);
1053                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1054                         interface_set_prefix_address(c, prefix, iface, false);
1055                 list_del(&c->head);
1056                 free(c);
1057         }
1058 
1059         if (!setup)
1060                 return;
1061 
1062         /* End-of-assignment sentinel */
1063         c = malloc(sizeof(*c) + 1);
1064         if (!c)
1065                 return;
1066 
1067         c->assigned = 1 << (64 - prefix->length);
1068         c->length = 64;
1069         c->name[0] = 0;
1070         c->addr = in6addr_any;
1071         list_add(&c->head, &prefix->assignments);
1072 
1073         /* Excluded prefix */
1074         if (prefix->excl_length > 0) {
1075                 const char name[] = "!excluded";
1076                 c = malloc(sizeof(*c) + sizeof(name));
1077                 if (c) {
1078                         c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
1079                                         ((1 << (64 - prefix->length)) - 1);
1080                         c->length = prefix->excl_length;
1081                         c->addr = in6addr_any;
1082                         memcpy(c->name, name, sizeof(name));
1083                         list_add(&c->head, &prefix->assignments);
1084                 }
1085         }
1086 
1087         bool assigned_any = false;
1088         struct {
1089                 struct avl_node node;
1090         } *entry, *n_entry;
1091         struct avl_tree assign_later;
1092 
1093         avl_init(&assign_later, prefix_assignment_cmp, false, NULL);
1094 
1095         vlist_for_each_element(&interfaces, iface, node) {
1096                 if (iface->assignment_length < 48 ||
1097                                 iface->assignment_length > 64)
1098                         continue;
1099 
1100                 /* Test whether there is a matching class */
1101                 if (!list_empty(&iface->assignment_classes)) {
1102                         bool found = false;
1103 
1104                         struct interface_assignment_class *c;
1105                         list_for_each_entry(c, &iface->assignment_classes, head) {
1106                                 if (!strcmp(c->name, prefix->pclass)) {
1107                                         found = true;
1108                                         break;
1109                                 }
1110                         }
1111 
1112                         if (!found)
1113                                 continue;
1114                 }
1115 
1116                 size_t namelen = strlen(iface->name) + 1;
1117                 c = malloc(sizeof(*c) + namelen);
1118                 if (!c)
1119                         continue;
1120 
1121                 c->length = iface->assignment_length;
1122                 c->assigned = iface->assignment_hint;
1123                 c->weight = iface->assignment_weight;
1124                 c->addr = in6addr_any;
1125                 c->enabled = false;
1126                 memcpy(c->name, iface->name, namelen);
1127 
1128                 /* First process all custom assignments, put all others in later-list */
1129                 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
1130                         if (c->assigned != -1) {
1131                                 c->assigned = -1;
1132                                 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
1133                                                 "of size %hhu for %s, trying other\n", c->length, c->name);
1134                         }
1135 
1136                         entry = calloc(1, sizeof(*entry));
1137                         if (!entry) {
1138                                 free(c);
1139                                 continue;
1140                         }
1141 
1142                         entry->node.key = c;
1143                         avl_insert(&assign_later, &entry->node);
1144                 }
1145 
1146                 if (c->assigned != -1)
1147                         assigned_any = true;
1148         }
1149 
1150         /* Then try to assign all other + failed custom assignments */
1151         avl_for_each_element_safe(&assign_later, entry, node, n_entry) {
1152                 bool assigned = false;
1153 
1154                 c = (struct device_prefix_assignment *)entry->node.key;
1155                 avl_delete(&assign_later, &entry->node);
1156 
1157                 do {
1158                         assigned = interface_prefix_assign(&prefix->assignments, c);
1159                 } while (!assigned && ++c->length <= 64);
1160 
1161                 if (!assigned) {
1162                         netifd_log_message(L_WARNING, "Failed to assign subprefix "
1163                                         "of size %hhu for %s\n", c->length, c->name);
1164                         free(c);
1165                 } else
1166                         assigned_any = true;
1167 
1168                 free(entry);
1169         }
1170 
1171         list_for_each_entry(c, &prefix->assignments, head)
1172                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1173                         interface_set_prefix_address(c, prefix, iface, true);
1174 
1175         if (!assigned_any)
1176                 netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them "
1177                                 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
1178 }
1179 
1180 
1181 void interface_refresh_assignments(bool hint)
1182 {
1183         static bool refresh = false;
1184         if (!hint && refresh) {
1185                 struct device_prefix *p;
1186                 list_for_each_entry(p, &prefixes, head)
1187                         interface_update_prefix_assignments(p, true);
1188         }
1189         refresh = hint;
1190 }
1191 
1192 void interface_update_prefix_delegation(struct interface_ip_settings *ip)
1193 {
1194         struct device_prefix *prefix;
1195 
1196         vlist_for_each_element(&ip->prefix, prefix, node) {
1197                 interface_update_prefix_assignments(prefix, !ip->no_delegation);
1198 
1199                 if (ip->no_delegation) {
1200                         if (prefix->head.next)
1201                                 list_del(&prefix->head);
1202                 } else
1203                         list_add(&prefix->head, &prefixes);
1204         }
1205 }
1206 
1207 static void
1208 interface_update_prefix(struct vlist_tree *tree,
1209                              struct vlist_node *node_new,
1210                              struct vlist_node *node_old)
1211 {
1212         struct device_prefix *prefix_old, *prefix_new;
1213         prefix_old = container_of(node_old, struct device_prefix, node);
1214         prefix_new = container_of(node_new, struct device_prefix, node);
1215 
1216         struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix);
1217         if (tree && (!node_new || !node_old))
1218                 ip->iface->updated |= IUF_PREFIX;
1219 
1220         struct device_route route;
1221         memset(&route, 0, sizeof(route));
1222         route.flags = DEVADDR_INET6;
1223         route.metric = INT32_MAX;
1224         route.mask = (node_new) ? prefix_new->length : prefix_old->length;
1225         route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
1226 
1227 
1228         struct device_prefix_assignment *c;
1229         struct interface *iface;
1230 
1231         if (node_old && node_new) {
1232                 /* Move assignments and refresh addresses to update valid times */
1233                 list_splice(&prefix_old->assignments, &prefix_new->assignments);
1234 
1235                 list_for_each_entry(c, &prefix_new->assignments, head)
1236                         if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1237                                 interface_set_prefix_address(c, prefix_new, iface, true);
1238 
1239                 if (prefix_new->preferred_until != prefix_old->preferred_until ||
1240                                 prefix_new->valid_until != prefix_old->valid_until)
1241                         ip->iface->updated |= IUF_PREFIX;
1242         } else if (node_new) {
1243                 /* Set null-route to avoid routing loops */
1244                 system_add_route(NULL, &route);
1245 
1246                 if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)
1247                         interface_update_prefix_assignments(prefix_new, true);
1248         } else if (node_old) {
1249                 /* Remove null-route */
1250                 interface_update_prefix_assignments(prefix_old, false);
1251                 system_del_route(NULL, &route);
1252         }
1253 
1254         if (node_old) {
1255                 if (prefix_old->head.next)
1256                         list_del(&prefix_old->head);
1257                 free(prefix_old);
1258         }
1259 
1260         if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation))
1261                 list_add(&prefix_new->head, &prefixes);
1262 
1263 }
1264 
1265 struct device_prefix*
1266 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
1267                 uint8_t length, time_t valid_until, time_t preferred_until,
1268                 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass)
1269 {
1270         if (!pclass)
1271                 pclass = (iface) ? iface->name : "local";
1272 
1273         struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1);
1274         if (!prefix)
1275                 return NULL;
1276 
1277         prefix->length = length;
1278         prefix->addr = *addr;
1279         prefix->preferred_until = preferred_until;
1280         prefix->valid_until = valid_until;
1281         prefix->iface = iface;
1282         INIT_LIST_HEAD(&prefix->assignments);
1283 
1284         if (excl_addr) {
1285                 prefix->excl_addr = *excl_addr;
1286                 prefix->excl_length = excl_length;
1287         }
1288 
1289         strcpy(prefix->pclass, pclass);
1290 
1291         if (iface)
1292                 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
1293         else
1294                 interface_update_prefix(NULL, &prefix->node, NULL);
1295 
1296         return prefix;
1297 }
1298 
1299 void
1300 interface_ip_set_ula_prefix(const char *prefix)
1301 {
1302         char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
1303         if (prefix)
1304                 strncpy(buf, prefix, sizeof(buf) - 1);
1305         char *prefixaddr = strtok_r(buf, "/", &saveptr);
1306 
1307         struct in6_addr addr;
1308         if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
1309                 if (ula_prefix) {
1310                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
1311                         ula_prefix = NULL;
1312                 }
1313                 return;
1314         }
1315 
1316         int length;
1317         char *prefixlen = strtok_r(NULL, ",", &saveptr);
1318         if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
1319                 return;
1320 
1321         if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
1322                         ula_prefix->length != length) {
1323                 if (ula_prefix)
1324                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
1325 
1326                 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
1327                                 0, 0, NULL, 0, NULL);
1328         }
1329 }
1330 
1331 static void
1332 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
1333 {
1334         struct dns_server *s;
1335 
1336         s = calloc(1, sizeof(*s));
1337         if (!s)
1338                 return;
1339 
1340         s->af = AF_INET;
1341         if (inet_pton(s->af, str, &s->addr.in))
1342                 goto add;
1343 
1344         s->af = AF_INET6;
1345         if (inet_pton(s->af, str, &s->addr.in))
1346                 goto add;
1347 
1348         free(s);
1349         return;
1350 
1351 add:
1352         D(INTERFACE, "Add IPv%c DNS server: %s\n",
1353           s->af == AF_INET6 ? '6' : '4', str);
1354         vlist_simple_add(&ip->dns_servers, &s->node);
1355 }
1356 
1357 void
1358 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
1359 {
1360         struct blob_attr *cur;
1361         int rem;
1362 
1363         blobmsg_for_each_attr(cur, list, rem) {
1364                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1365                         continue;
1366 
1367                 if (!blobmsg_check_attr(cur, false))
1368                         continue;
1369 
1370                 interface_add_dns_server(ip, blobmsg_data(cur));
1371         }
1372 }
1373 
1374 static void
1375 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
1376 {
1377         struct dns_search_domain *s;
1378         int len = strlen(str);
1379 
1380         s = calloc(1, sizeof(*s) + len + 1);
1381         if (!s)
1382                 return;
1383 
1384         D(INTERFACE, "Add DNS search domain: %s\n", str);
1385         memcpy(s->name, str, len);
1386         vlist_simple_add(&ip->dns_search, &s->node);
1387 }
1388 
1389 void
1390 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
1391 {
1392         struct blob_attr *cur;
1393         int rem;
1394 
1395         blobmsg_for_each_attr(cur, list, rem) {
1396                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1397                         continue;
1398 
1399                 if (!blobmsg_check_attr(cur, false))
1400                         continue;
1401 
1402                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
1403         }
1404 }
1405 
1406 static void
1407 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip, const char *dev)
1408 {
1409         struct dns_server *s;
1410         struct dns_search_domain *d;
1411         const char *str;
1412         char buf[INET6_ADDRSTRLEN];
1413 
1414         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
1415                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
1416                 if (!str)
1417                         continue;
1418 
1419                 if (s->af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&s->addr.in6))
1420                         fprintf(f, "nameserver %s%%%s\n", str, dev);
1421                 else
1422                         fprintf(f, "nameserver %s\n", str);
1423         }
1424 
1425         vlist_simple_for_each_element(&ip->dns_search, d, node) {
1426                 fprintf(f, "search %s\n", d->name);
1427         }
1428 }
1429 
1430 /* Sorting of interface resolver entries :               */
1431 /* Primary on interface dns_metric : lowest metric first */
1432 /* Secondary on interface metric : lowest metric first   */
1433 /* Finally alphabetical order of interface names         */
1434 static int resolv_conf_iface_cmp(const void *k1, const void *k2, void *ptr)
1435 {
1436         const struct interface *iface1 = k1, *iface2 = k2;
1437 
1438         if (iface1->dns_metric != iface2->dns_metric)
1439                 return iface1->dns_metric - iface2->dns_metric;
1440 
1441         if (iface1->metric != iface2->metric)
1442                 return iface1->metric - iface2->metric;
1443 
1444         return strcmp(iface1->name, iface2->name);
1445 }
1446 
1447 static void
1448 __interface_write_dns_entries(FILE *f, const char *jail)
1449 {
1450         struct interface *iface;
1451         struct {
1452                 struct avl_node node;
1453         } *entry, *n_entry;
1454         struct avl_tree resolv_conf_iface_entries;
1455 
1456         avl_init(&resolv_conf_iface_entries, resolv_conf_iface_cmp, false, NULL);
1457 
1458         vlist_for_each_element(&interfaces, iface, node) {
1459                 if (iface->state != IFS_UP)
1460                         continue;
1461 
1462                 if (jail && (!iface->jail || strcmp(jail, iface->jail)))
1463                         continue;
1464 
1465                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
1466                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
1467                     vlist_simple_empty(&iface->config_ip.dns_search) &&
1468                     vlist_simple_empty(&iface->config_ip.dns_servers))
1469                         continue;
1470 
1471                 entry = calloc(1, sizeof(*entry));
1472                 if (!entry)
1473                         continue;
1474 
1475                 entry->node.key = iface;
1476                 avl_insert(&resolv_conf_iface_entries, &entry->node);
1477         }
1478 
1479         avl_for_each_element(&resolv_conf_iface_entries, entry, node) {
1480                 iface = (struct interface *)entry->node.key;
1481                 struct device *dev = iface->l3_dev.dev;
1482 
1483                 fprintf(f, "# Interface %s\n", iface->name);
1484 
1485                 write_resolv_conf_entries(f, &iface->config_ip, dev->ifname);
1486 
1487                 if (!iface->proto_ip.no_dns)
1488                         write_resolv_conf_entries(f, &iface->proto_ip, dev->ifname);
1489         }
1490 
1491         avl_remove_all_elements(&resolv_conf_iface_entries, entry, node, n_entry)
1492                 free(entry);
1493 }
1494 
1495 void
1496 interface_write_resolv_conf(const char *jail)
1497 {
1498         size_t plen = (jail ? strlen(jail) + 1 : 0 ) + strlen(resolv_conf) + 1;
1499         char *path = alloca(plen);
1500         char *dpath = alloca(plen);
1501         char *tmppath = alloca(plen + 4);
1502         FILE *f;
1503         uint32_t crcold, crcnew;
1504 
1505         if (jail) {
1506                 sprintf(path, "/tmp/resolv.conf-%s.d/resolv.conf.auto", jail);
1507                 strcpy(dpath, path);
1508                 dpath = dirname(dpath);
1509                 mkdir(dpath, 0755);
1510         } else {
1511                 strcpy(path, resolv_conf);
1512         }
1513 
1514         sprintf(tmppath, "%s.tmp", path);
1515         unlink(tmppath);
1516         f = fopen(tmppath, "w+");
1517         if (!f) {
1518                 D(INTERFACE, "Failed to open %s for writing\n", path);
1519                 return;
1520         }
1521 
1522         __interface_write_dns_entries(f, jail);
1523 
1524         fflush(f);
1525         rewind(f);
1526         crcnew = crc32_file(f);
1527         fclose(f);
1528 
1529         crcold = crcnew + 1;
1530         f = fopen(path, "r");
1531         if (f) {
1532                 crcold = crc32_file(f);
1533                 fclose(f);
1534         }
1535 
1536         if (crcold == crcnew) {
1537                 unlink(tmppath);
1538         } else if (rename(tmppath, path) < 0) {
1539                 D(INTERFACE, "Failed to replace %s\n", path);
1540                 unlink(tmppath);
1541         }
1542 }
1543 
1544 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
1545 {
1546         struct device_addr *addr;
1547         struct device_route *route;
1548         struct device_neighbor *neighbor;
1549         struct device *dev;
1550         struct interface *iface;
1551 
1552         ip->enabled = enabled;
1553         iface = ip->iface;
1554         dev = iface->l3_dev.dev;
1555         if (!dev)
1556                 return;
1557 
1558         vlist_for_each_element(&ip->addr, addr, node) {
1559                 bool v6 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6) ? true : false;
1560 
1561                 if (addr->flags & DEVADDR_EXTERNAL)
1562                         continue;
1563 
1564                 if (addr->enabled == enabled)
1565                         continue;
1566 
1567                 if (enabled) {
1568                         system_add_address(dev, addr);
1569 
1570                         addr->policy_table = (v6) ? iface->ip6table : iface->ip4table;
1571                         if (iface->metric || addr->policy_table)
1572                                 interface_handle_subnet_route(iface, addr, true);
1573 
1574                         if (addr->policy_table)
1575                                 interface_add_addr_rules(addr, true);
1576                 } else {
1577                         interface_handle_subnet_route(iface, addr, false);
1578                         system_del_address(dev, addr);
1579 
1580                         if (addr->policy_table)
1581                                 interface_add_addr_rules(addr, false);
1582                 }
1583                 addr->enabled = enabled;
1584         }
1585 
1586         vlist_for_each_element(&ip->route, route, node) {
1587                 bool _enabled = enabled;
1588 
1589                 if (route->flags & DEVADDR_EXTERNAL)
1590                         continue;
1591 
1592                 if (!enable_route(ip, route))
1593                         _enabled = false;
1594                 if (route->enabled == _enabled)
1595                         continue;
1596 
1597                 if (_enabled) {
1598                         interface_set_route_info(ip->iface, route);
1599 
1600                         if (system_add_route(dev, route))
1601                                 route->failed = true;
1602                 } else
1603                         system_del_route(dev, route);
1604                 route->enabled = _enabled;
1605         }
1606 
1607         vlist_for_each_element(&ip->neighbor, neighbor, node) {
1608                 if (neighbor->enabled == enabled)
1609                         continue;
1610 
1611                 if (enabled) {
1612                         if(system_add_neighbor(dev, neighbor))
1613                                 neighbor->failed = true;
1614                 } else
1615                         system_del_neighbor(dev, neighbor);
1616 
1617                 neighbor->enabled = enabled;
1618         }
1619 
1620         struct device_prefix *c;
1621         struct device_prefix_assignment *a;
1622         list_for_each_entry(c, &prefixes, head)
1623                 list_for_each_entry(a, &c->assignments, head)
1624                         if (!strcmp(a->name, ip->iface->name))
1625                                 interface_set_prefix_address(a, c, ip->iface, enabled);
1626 
1627         if (ip->iface->policy_rules_set != enabled &&
1628             ip->iface->l3_dev.dev) {
1629                 set_ip_lo_policy(enabled, true, ip->iface);
1630                 set_ip_lo_policy(enabled, false, ip->iface);
1631 
1632                 set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex,
1633                         NULL, 0, 0, ip->iface, "failed_policy", true);
1634                 ip->iface->policy_rules_set = enabled;
1635         }
1636 }
1637 
1638 void
1639 interface_ip_update_start(struct interface_ip_settings *ip)
1640 {
1641         if (ip != &ip->iface->config_ip) {
1642                 vlist_simple_update(&ip->dns_servers);
1643                 vlist_simple_update(&ip->dns_search);
1644         }
1645         vlist_update(&ip->route);
1646         vlist_update(&ip->addr);
1647         vlist_update(&ip->prefix);
1648         vlist_update(&ip->neighbor);
1649 }
1650 
1651 void
1652 interface_ip_update_complete(struct interface_ip_settings *ip)
1653 {
1654         vlist_simple_flush(&ip->dns_servers);
1655         vlist_simple_flush(&ip->dns_search);
1656         vlist_flush(&ip->route);
1657         vlist_flush(&ip->addr);
1658         vlist_flush(&ip->prefix);
1659         vlist_flush(&ip->neighbor);
1660         interface_write_resolv_conf(ip->iface->jail);
1661 }
1662 
1663 void
1664 interface_ip_flush(struct interface_ip_settings *ip)
1665 {
1666         if (ip == &ip->iface->proto_ip)
1667                 vlist_flush_all(&ip->iface->host_routes);
1668         vlist_simple_flush_all(&ip->dns_servers);
1669         vlist_simple_flush_all(&ip->dns_search);
1670         vlist_flush_all(&ip->route);
1671         vlist_flush_all(&ip->addr);
1672         vlist_flush_all(&ip->neighbor);
1673         vlist_flush_all(&ip->prefix);
1674 }
1675 
1676 static void
1677 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
1678 {
1679         ip->iface = iface;
1680         ip->enabled = true;
1681         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
1682         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
1683         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
1684         vlist_init(&ip->neighbor, neighbor_cmp, interface_update_proto_neighbor);
1685         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
1686         vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
1687 }
1688 
1689 void
1690 interface_ip_init(struct interface *iface)
1691 {
1692         __interface_ip_init(&iface->proto_ip, iface);
1693         __interface_ip_init(&iface->config_ip, iface);
1694         vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
1695 }
1696 
1697 static void
1698 interface_ip_valid_until_handler(struct uloop_timeout *t)
1699 {
1700         time_t now = system_get_rtime();
1701         struct interface *iface;
1702         vlist_for_each_element(&interfaces, iface, node) {
1703                 if (iface->state != IFS_UP)
1704                         continue;
1705 
1706                 struct device_addr *addr, *addrp;
1707                 struct device_route *route, *routep;
1708                 struct device_prefix *pref, *prefp;
1709 
1710                 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
1711                         if (addr->valid_until && addr->valid_until < now)
1712                                 vlist_delete(&iface->proto_ip.addr, &addr->node);
1713 
1714                 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
1715                         if (route->valid_until && route->valid_until < now)
1716                                 vlist_delete(&iface->proto_ip.route, &route->node);
1717 
1718                 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
1719                         if (pref->valid_until && pref->valid_until < now)
1720                                 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1721 
1722         }
1723 
1724         uloop_timeout_set(t, 1000);
1725 }
1726 
1727 static void __init
1728 interface_ip_init_worker(void)
1729 {
1730         valid_until_timeout.cb = interface_ip_valid_until_handler;
1731         uloop_timeout_set(&valid_until_timeout, 1000);
1732 }
1733 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt