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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt