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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt