3 * Copyright (c) 2007 The Trustees of Princeton University
4 * Author: Daniel Hokka Zakrisson <daniel@hozac.com>
6 * Licensed under the terms of the GNU General Public License
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <arpa/inet.h>
30 #include <asm/types.h>
31 #include <netlink/netlink.h>
32 #include <netlink/handlers.h>
33 #include <netlink/route/addr.h>
37 #define HAS_ADDRESS 0x01
38 #define HAS_PREFIX 0x02
42 struct nid_list *next;
58 struct nid_prefix_map {
60 struct nid_prefix_map *prev;
61 struct nid_prefix_map *next;
64 struct nid_prefix_map *prev;
65 struct nid_prefix_map *next;
67 struct prefix *prefix;
71 struct nl_handle *handle;
73 /* from linux/include/net/ipv6.h */
74 static inline int ipv6_prefix_equal(struct in6_addr *prefix,
75 struct in6_addr *addr, int prefixlen)
77 uint32_t *a1 = prefix->s6_addr32, *a2 = addr->s6_addr32;
80 /* check complete u32 in prefix */
82 if (pdw && memcmp(a1, a2, pdw << 2))
85 /* check incomplete u32 in prefix */
86 pbi = prefixlen & 0x1f;
87 if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
93 static int add_address_to_interface(int ifindex, struct in6_addr *address,
97 struct rtnl_addr *rta;
100 nl = nl_addr_build(AF_INET6, address, sizeof(struct in6_addr));
101 rta = rtnl_addr_alloc();
103 rtnl_addr_set_family(rta, AF_INET6);
104 rtnl_addr_set_ifindex(rta, ifindex);
105 rtnl_addr_set_local(rta, nl);
106 rtnl_addr_set_prefixlen(rta, prefix);
108 if (rtnl_addr_add(handle, rta, NLM_F_REPLACE) != -1 || errno == EEXIST)
116 static inline int remove_address_from_interface(struct nid_prefix_map *entry)
118 struct rtnl_addr *rta;
123 memcpy(&a, &entry->prefix->address.addr, sizeof(a));
124 if (entry->nid != 0) {
125 a.s6_addr[11] = (entry->nid & 0x7f80) >> 7;
126 a.s6_addr[12] = (entry->nid & 0x7f) << 1;
129 nl = nl_addr_build(AF_INET6, &a, sizeof(a));
132 rta = rtnl_addr_alloc();
136 rtnl_addr_set_family(rta, AF_INET6);
137 rtnl_addr_set_ifindex(rta, entry->prefix->ifindex);
138 rtnl_addr_set_local(rta, nl);
139 rtnl_addr_set_prefixlen(rta, entry->prefix->address.prefix_len);
141 ret = rtnl_addr_delete(handle, rta, 0);
149 static int add_to_map(struct nid_prefix_map *map, struct nid_prefix_map *new)
151 struct nid_prefix_map *i;
152 #define PUT_IT_IN_PLACE(node, member, om) \
153 /* find the correct location in the list */ \
154 for (i = map->node.next; i->node.next && i->member < \
155 new->member; i = i->node.next) \
157 if (i && i->member == new->member && i->om == new->om) \
159 /* first in the list */ \
160 if (!i || !i->node.prev) { \
161 new->node.prev = NULL; \
162 new->node.next = i; \
163 map->node.next = new; \
165 i->node.prev = new; \
167 /* last in the list */ \
168 else if (i->node.next == NULL) { \
169 new->node.prev = i; \
170 new->node.next = NULL; \
171 i->node.next = new; \
173 /* somewhere in the middle */ \
175 new->node.prev = i->node.prev; \
176 new->node.next = i; \
177 i->node.prev->node.next = new; \
178 i->node.prev = new; \
180 PUT_IT_IN_PLACE(p, prefix, nid)
181 PUT_IT_IN_PLACE(n, nid, prefix)
185 static inline void remove_from_map(struct nid_prefix_map *map,
186 struct nid_prefix_map *entry)
188 if (map->n.next == entry)
189 map->n.next = entry->n.next;
190 if (map->n.prev == entry)
191 map->n.prev = entry->n.prev;
192 if (map->p.next == entry)
193 map->p.next = entry->p.next;
194 if (map->p.prev == entry)
195 map->p.prev = entry->p.prev;
198 static inline void remove_from_map_and_free(struct nid_prefix_map *map,
199 struct nid_prefix_map *entry)
201 remove_from_map(map, entry);
205 static int add_nid_to_map(struct nid_prefix_map *map, struct prefix *prefix,
208 struct nid_prefix_map *new = calloc(1, sizeof(struct nid_prefix_map));
214 new->prefix = prefix;
216 ret = add_to_map(map, new);
224 static int add_prefix_to_map(struct nid_prefix_map *map, struct prefix *prefix)
226 return add_nid_to_map(map, prefix, 0);
229 static void cleanup_prefix(struct nid_prefix_map *map,
230 struct nid_prefix_map *first)
232 struct nid_prefix_map *i, *p = NULL;
234 for (i = first; i && first->prefix == i->prefix; i = i->p.next) {
236 remove_from_map_and_free(map, p);
239 remove_address_from_interface(i);
244 remove_from_map_and_free(map, p);
247 static inline int add_nid_to_list(struct nid_list **head, nid_t nid)
249 struct nid_list *i, *new;
251 for (i = *head; i && i->next && i->next->nid < nid; i = i->next)
253 /* check if this nid is first in the list */
254 if (i && i->nid == nid)
256 /* check if it's already in the list */
257 if (i && i->next && i->next->nid == nid)
261 new = calloc(1, sizeof(struct nid_list));
266 /* this is the lowest nid in the list */
271 /* in the middle/at the end */
276 /* there was no list */
283 static inline void free_nid_list(struct nid_list *head)
286 for (p = NULL; head; head = head->next) {
295 static inline void cleanup_nid(struct nid_prefix_map *map,
298 struct nid_prefix_map *i, *p = NULL;
299 for (i = map->n.next; i->nid < nid; i = i->n.next)
301 /* this nid doesn't have any entries in the map */
304 for (; i->nid == nid; i = i->n.next) {
306 remove_from_map_and_free(map, p);
307 remove_address_from_interface(i);
311 remove_from_map_and_free(map, p);
314 static inline void cleanup_nids(struct nid_prefix_map *map,
315 struct nid_list *previous,
316 struct nid_list *current)
318 struct nid_list *p, *pprev = NULL, *c;
319 for (p = previous, c = current; p; pprev = p, p = p->next) {
322 while (c->nid < p->nid)
324 if (c->nid == p->nid)
326 /* this context has disappeared */
327 cleanup_nid(map, p->nid);
333 static void do_slices_autoconf(struct nid_prefix_map *map)
337 struct vc_net_addr addr;
338 struct nid_prefix_map *i;
339 struct nid_list *current = NULL, *n;
340 static struct nid_list *previous = NULL;
342 if ((dp = opendir("/proc/virtnet")) == NULL)
344 while ((de = readdir(dp)) != NULL) {
347 if (!isdigit(de->d_name[0]))
350 nid = strtoul(de->d_name, NULL, 10);
351 addr.vna_type = VC_NXA_TYPE_IPV6 | VC_NXA_TYPE_ANY;
352 if (vc_net_remove(nid, &addr) == -1) {
353 syslog(LOG_ERR, "vc_net_remove(%u): %s", nid, strerror(errno));
357 add_nid_to_list(¤t, nid);
361 for (n = current; n; n = n->next) {
362 for (i = map->p.next; i && i->nid == 0;) {
364 if (i->prefix->mask & HAS_PREFIX && i->prefix->prefix.valid_until < time(NULL)) {
365 struct nid_prefix_map *tmp;
368 inet_ntop(AF_INET6, &i->prefix->address.addr, buf, sizeof(buf));
369 syslog(LOG_NOTICE, "Address %s timed out", buf);
373 cleanup_prefix(map, i);
378 if (i->prefix->mask != (HAS_ADDRESS|HAS_PREFIX))
381 addr.vna_type = VC_NXA_TYPE_IPV6 | VC_NXA_TYPE_ADDR;
382 memcpy(&addr.vna_v6_ip, &i->prefix->address.addr, sizeof(struct in6_addr));
383 addr.vna_prefix = i->prefix->prefix.prefix_len;
384 if (addr.vna_prefix == 64) {
385 addr.vna_v6_mask.s6_addr32[0] = addr.vna_v6_mask.s6_addr32[1] = 0xffffffff;
386 addr.vna_v6_mask.s6_addr32[2] = addr.vna_v6_mask.s6_addr32[3] = 0;
388 addr.vna_v6_ip.s6_addr[11] = (n->nid & 0x7f80) >> 7;
389 addr.vna_v6_ip.s6_addr[12] = (n->nid & 0x007f) << 1;
390 if (vc_net_add(n->nid, &addr) == -1) {
391 syslog(LOG_ERR, "vc_net_add(%u): %s", n->nid, strerror(errno));
394 if (add_address_to_interface(i->prefix->ifindex, &addr.vna_v6_ip, addr.vna_prefix) == -1) {
395 syslog(LOG_ERR, "add_address_to_interface: %s", strerror(errno));
398 if (add_nid_to_map(map, i->prefix, n->nid) == -1) {
399 syslog(LOG_ERR, "add_nid_to_map: %s", strerror(errno));
407 cleanup_nids(map, previous, current);
411 /* XXX These two functions are very similar */
412 static int add_prefix(struct nid_prefix_map *map, struct prefixmsg *msg,
413 struct in6_addr *prefix, struct prefix_cacheinfo *cache)
415 struct nid_prefix_map *i = map;
418 if (!msg || !prefix || !cache)
420 /* XXX IF_PREFIX_AUTOCONF == 0x02 */
421 if (!(msg->prefix_flags & 0x02))
425 if (i->p.next != NULL)
427 if (ipv6_prefix_equal(prefix, &i->prefix->prefix.addr, msg->prefix_len) ||
428 ipv6_prefix_equal(prefix, &i->prefix->address.addr, msg->prefix_len)) {
429 i->prefix->mask |= HAS_PREFIX;
430 i->prefix->ifindex = msg->prefix_ifindex;
431 memcpy(&i->prefix->prefix.addr, prefix, sizeof(*prefix));
432 i->prefix->prefix.prefix_len = msg->prefix_len;
433 i->prefix->prefix.valid_until = time(NULL) + cache->preferred_time;
436 } while (i->p.next && i->nid == 0);
438 /* not yet in the map */
439 new = calloc(1, sizeof(*new));
442 new->mask = HAS_PREFIX;
443 memcpy(&new->prefix.addr, prefix, sizeof(*prefix));
444 new->prefix.prefix_len = msg->prefix_len;
445 new->prefix.valid_until = time(NULL) + cache->preferred_time;
446 if (add_prefix_to_map(map, new) == -1)
452 static inline int add_address(struct nid_prefix_map *map, struct ifaddrmsg *msg,
453 struct in6_addr *address, struct ifa_cacheinfo *cache)
455 struct nid_prefix_map *i = map;
458 if (!msg || !address || !cache)
461 if (address->s6_addr[11] != 0xFF || address->s6_addr[12] != 0xFE)
465 if (i->p.next != NULL)
467 if (ipv6_prefix_equal(address, &i->prefix->prefix.addr, msg->ifa_prefixlen) ||
468 ipv6_prefix_equal(address, &i->prefix->address.addr, 128)) {
469 i->prefix->mask |= HAS_ADDRESS;
470 memcpy(&i->prefix->address.addr, address, sizeof(*address));
471 i->prefix->address.prefix_len = msg->ifa_prefixlen;
472 i->prefix->address.valid_until = time(NULL) + cache->ifa_prefered;
475 } while (i->p.next && i->nid == 0);
477 new = calloc(1, sizeof(*new));
480 new->mask = HAS_ADDRESS;
481 memcpy(&new->address.addr, address, sizeof(*address));
482 new->address.prefix_len = msg->ifa_prefixlen;
483 new->address.valid_until = time(NULL) + cache->ifa_prefered;
484 if (add_prefix_to_map(map, new) == -1)
490 static struct nla_policy addr_policy[IFA_MAX+1] = {
491 [IFA_ADDRESS] = { .minlen = sizeof(struct in6_addr) },
492 [IFA_LABEL] = { .type = NLA_STRING,
493 .maxlen = IFNAMSIZ },
494 [IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) },
496 static struct nla_policy prefix_policy[PREFIX_MAX+1] = {
497 [PREFIX_ADDRESS] = { .minlen = sizeof(struct in6_addr) },
498 [PREFIX_CACHEINFO] = { .minlen = sizeof(struct prefix_cacheinfo) },
500 int handle_valid_msg(struct nl_msg *msg, void *arg)
502 struct nlmsghdr *nlh = nlmsg_hdr(msg);
505 struct sockaddr_nl *source = nlmsg_get_src(msg);
507 payload = nlmsg_data(nlh);
508 if (source->nl_groups == RTMGRP_IPV6_PREFIX) {
509 struct prefixmsg *prefixmsg;
510 struct in6_addr *prefix = NULL;
511 struct prefix_cacheinfo *cacheinfo = NULL;
512 struct nlattr *tb[PREFIX_MAX+1];
514 if (nlmsg_parse(nlh, sizeof(struct prefixmsg), tb, PREFIX_MAX, prefix_policy) < 0) {
515 syslog(LOG_ERR, "Failed to parse prefixmsg");
519 prefixmsg = (struct prefixmsg *) payload;
520 if (tb[PREFIX_ADDRESS])
521 prefix = nl_data_get(nla_get_data(tb[PREFIX_ADDRESS]));
522 if (tb[PREFIX_CACHEINFO])
523 cacheinfo = nl_data_get(nla_get_data(tb[PREFIX_CACHEINFO]));
524 ret = add_prefix(arg, prefixmsg, prefix, cacheinfo);
526 else if (source->nl_groups == RTMGRP_IPV6_IFADDR) {
527 struct ifaddrmsg *ifaddrmsg;
528 struct in6_addr *address = NULL;
529 struct ifa_cacheinfo *cacheinfo = NULL;
530 struct nlattr *tb[IFA_MAX+1];
532 if (nlmsg_parse(nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX, addr_policy) < 0) {
533 syslog(LOG_ERR, "Failed to parse ifaddrmsg");
537 ifaddrmsg = (struct ifaddrmsg *) payload;
539 address = nl_data_get(nla_get_data(tb[IFA_ADDRESS]));
540 if (tb[IFA_CACHEINFO])
541 cacheinfo = nl_data_get(nla_get_data(tb[IFA_CACHEINFO]));
542 ret = add_address(arg, ifaddrmsg, address, cacheinfo);
545 do_slices_autoconf(arg);
550 int handle_error_msg(struct sockaddr_nl *source, struct nlmsgerr *err,
553 syslog(LOG_ERR, "%s", strerror(err->error));
557 int handle_no_op(struct nl_msg *msg, void *arg)
562 /* only for access in the signal handler */
563 struct nid_prefix_map map = {
573 void signal_handler(int signal)
577 do_slices_autoconf(&map);
582 static int write_pidfile(const char *filename)
585 fp = fopen(filename, "w");
588 fprintf(fp, "%d\n", getpid());
593 int main(int argc, char *argv[])
597 openlog("vip6-autod", LOG_PERROR, LOG_DAEMON);
599 cbs = nl_cb_alloc(NL_CB_VERBOSE);
601 syslog(LOG_CRIT, "nl_cb_alloc: %s", strerror(errno));
604 nl_cb_set(cbs, NL_CB_VALID, NL_CB_CUSTOM, handle_valid_msg, &map);
605 nl_cb_set(cbs, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, handle_no_op, NULL);
606 nl_cb_err(cbs, NL_CB_CUSTOM, handle_error_msg, &map);
608 handle = nl_handle_alloc_cb(cbs);
609 nl_disable_sequence_check(handle);
611 nl_join_groups(handle, RTMGRP_IPV6_PREFIX|RTMGRP_IPV6_IFADDR);
612 if (nl_connect(handle, NETLINK_ROUTE) == -1) {
613 syslog(LOG_CRIT, "nl_connect: %s", strerror(errno));
617 if (daemon(0, 0) == -1)
620 write_pidfile(LOCALSTATEDIR "/run/vip6-autod.pid");
622 signal(SIGUSR1, signal_handler);
624 while (nl_recvmsgs(handle, cbs) > 0);