• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/netifd/interface-ip.c

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt