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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt