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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt