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/route/addr.h>
36 #define HAS_ADDRESS 0x01
37 #define HAS_PREFIX 0x02
41 struct nid_list *next;
57 struct nid_prefix_map {
59 struct nid_prefix_map *prev;
60 struct nid_prefix_map *next;
63 struct nid_prefix_map *prev;
64 struct nid_prefix_map *next;
66 struct prefix *prefix;
70 struct nl_handle *handle;
72 /* from linux/include/net/ipv6.h */
73 static inline int ipv6_prefix_equal(struct in6_addr *prefix,
74 struct in6_addr *addr, int prefixlen)
76 uint32_t *a1 = prefix->s6_addr32, *a2 = addr->s6_addr32;
79 /* check complete u32 in prefix */
81 if (pdw && memcmp(a1, a2, pdw << 2))
84 /* check incomplete u32 in prefix */
85 pbi = prefixlen & 0x1f;
86 if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
92 static int add_address_to_interface(int ifindex, struct in6_addr *address,
96 struct rtnl_addr *rta;
99 nl = nl_addr_build(AF_INET6, address, sizeof(struct in6_addr));
100 rta = rtnl_addr_alloc();
102 rtnl_addr_set_family(rta, AF_INET6);
103 rtnl_addr_set_ifindex(rta, ifindex);
104 rtnl_addr_set_local(rta, nl);
105 rtnl_addr_set_prefixlen(rta, prefix);
107 if (rtnl_addr_add(handle, rta, NLM_F_REPLACE) != -1 || errno == EEXIST)
115 static inline int remove_address_from_interface(struct nid_prefix_map *entry)
117 struct rtnl_addr *rta;
122 memcpy(&a, &entry->prefix->address.addr, sizeof(a));
123 if (entry->nid != 0) {
124 a.s6_addr[11] = (entry->nid & 0x7f80) >> 7;
125 a.s6_addr[12] = (entry->nid & 0x7f) << 1;
128 nl = nl_addr_build(AF_INET6, &a, sizeof(a));
131 rta = rtnl_addr_alloc();
135 rtnl_addr_set_family(rta, AF_INET6);
136 rtnl_addr_set_ifindex(rta, entry->prefix->ifindex);
137 rtnl_addr_set_local(rta, nl);
138 rtnl_addr_set_prefixlen(rta, entry->prefix->address.prefix_len);
140 ret = rtnl_addr_delete(handle, rta, 0);
148 static int add_to_map(struct nid_prefix_map *map, struct nid_prefix_map *new)
150 struct nid_prefix_map *i;
151 #define PUT_IT_IN_PLACE(node, member, om) \
152 /* find the correct location in the list */ \
153 for (i = map->node.next; i->node.next && i->member < \
154 new->member; i = i->node.next) \
156 if (i && i->member == new->member && i->om == new->om) \
158 /* first in the list */ \
159 if (!i || !i->node.prev) { \
160 new->node.prev = NULL; \
161 new->node.next = i; \
162 map->node.next = new; \
164 i->node.prev = new; \
166 /* last in the list */ \
167 else if (i->node.next == NULL) { \
168 new->node.prev = i; \
169 new->node.next = NULL; \
170 i->node.next = new; \
172 /* somewhere in the middle */ \
174 new->node.prev = i->node.prev; \
175 new->node.next = i; \
176 i->node.prev->node.next = new; \
177 i->node.prev = new; \
179 PUT_IT_IN_PLACE(p, prefix, nid)
180 PUT_IT_IN_PLACE(n, nid, prefix)
184 static inline void remove_from_map(struct nid_prefix_map *map,
185 struct nid_prefix_map *entry)
187 if (map->n.next == entry)
188 map->n.next = entry->n.next;
189 if (map->n.prev == entry)
190 map->n.prev = entry->n.prev;
191 if (map->p.next == entry)
192 map->p.next = entry->p.next;
193 if (map->p.prev == entry)
194 map->p.prev = entry->p.prev;
197 static inline void remove_from_map_and_free(struct nid_prefix_map *map,
198 struct nid_prefix_map *entry)
200 remove_from_map(map, entry);
204 static int add_nid_to_map(struct nid_prefix_map *map, struct prefix *prefix,
207 struct nid_prefix_map *new = calloc(1, sizeof(struct nid_prefix_map));
213 new->prefix = prefix;
215 ret = add_to_map(map, new);
223 static int add_prefix_to_map(struct nid_prefix_map *map, struct prefix *prefix)
225 return add_nid_to_map(map, prefix, 0);
228 static void cleanup_prefix(struct nid_prefix_map *map,
229 struct nid_prefix_map *first)
231 struct nid_prefix_map *i, *p = NULL;
233 for (i = first; i && first->prefix == i->prefix; i = i->p.next) {
235 remove_from_map_and_free(map, p);
238 remove_address_from_interface(i);
243 remove_from_map_and_free(map, p);
246 static inline int add_nid_to_list(struct nid_list **head, nid_t nid)
248 struct nid_list *i, *new;
250 for (i = *head; i && i->next && i->next->nid < nid; i = i->next)
252 /* check if this nid is first in the list */
253 if (i && i->nid == nid)
255 /* check if it's already in the list */
256 if (i && i->next && i->next->nid == nid)
260 new = calloc(1, sizeof(struct nid_list));
265 /* this is the lowest nid in the list */
270 /* in the middle/at the end */
275 /* there was no list */
282 static inline void free_nid_list(struct nid_list *head)
285 for (p = NULL; head; head = head->next) {
294 static inline void cleanup_nid(struct nid_prefix_map *map,
297 struct nid_prefix_map *i, *p = NULL;
298 for (i = map->n.next; i->nid < nid; i = i->n.next)
300 /* this nid doesn't have any entries in the map */
303 for (; i->nid == nid; i = i->n.next) {
305 remove_from_map_and_free(map, p);
306 remove_address_from_interface(i);
310 remove_from_map_and_free(map, p);
313 static inline void cleanup_nids(struct nid_prefix_map *map,
314 struct nid_list *previous,
315 struct nid_list *current)
317 struct nid_list *p, *pprev = NULL, *c;
318 for (p = previous, c = current; p; pprev = p, p = p->next) {
321 while (c->nid < p->nid)
323 if (c->nid == p->nid)
325 /* this context has disappeared */
326 cleanup_nid(map, p->nid);
332 static void do_slices_autoconf(struct nid_prefix_map *map)
336 struct vc_net_addr addr;
337 struct nid_prefix_map *i;
338 struct nid_list *current = NULL, *n;
339 static struct nid_list *previous = NULL;
341 if ((dp = opendir("/proc/virtnet")) == NULL)
343 while ((de = readdir(dp)) != NULL) {
346 if (!isdigit(de->d_name[0]))
349 nid = strtoul(de->d_name, NULL, 10);
350 addr.vna_type = VC_NXA_TYPE_IPV6 | VC_NXA_TYPE_ANY;
351 if (vc_net_remove(nid, &addr) == -1) {
352 syslog(LOG_ERR, "vc_net_remove(%u): %s", nid, strerror(errno));
356 add_nid_to_list(¤t, nid);
360 for (n = current; n; n = n->next) {
361 for (i = map->p.next; i && i->nid == 0;) {
363 if (i->prefix->mask & HAS_PREFIX && i->prefix->prefix.valid_until < time(NULL)) {
364 struct nid_prefix_map *tmp;
367 inet_ntop(AF_INET6, &i->prefix->address.addr, buf, sizeof(buf));
368 syslog(LOG_NOTICE, "Address %s timed out", buf);
372 cleanup_prefix(map, i);
377 if (i->prefix->mask != (HAS_ADDRESS|HAS_PREFIX))
380 addr.vna_type = VC_NXA_TYPE_IPV6 | VC_NXA_TYPE_ADDR;
381 memcpy(&addr.vna_v6_ip, &i->prefix->address.addr, sizeof(struct in6_addr));
382 addr.vna_prefix = i->prefix->prefix.prefix_len;
383 if (addr.vna_prefix == 64) {
384 addr.vna_v6_mask.s6_addr32[0] = addr.vna_v6_mask.s6_addr32[1] = 0xffffffff;
385 addr.vna_v6_mask.s6_addr32[2] = addr.vna_v6_mask.s6_addr32[3] = 0;
387 addr.vna_v6_ip.s6_addr[11] = (n->nid & 0x7f80) >> 7;
388 addr.vna_v6_ip.s6_addr[12] = (n->nid & 0x007f) << 1;
389 if (vc_net_add(n->nid, &addr) == -1) {
390 syslog(LOG_ERR, "vc_net_add(%u): %s", n->nid, strerror(errno));
393 if (add_address_to_interface(i->prefix->ifindex, &addr.vna_v6_ip, addr.vna_prefix) == -1) {
394 syslog(LOG_ERR, "add_address_to_interface: %s", strerror(errno));
397 if (add_nid_to_map(map, i->prefix, n->nid) == -1) {
398 syslog(LOG_ERR, "add_nid_to_map: %s", strerror(errno));
406 cleanup_nids(map, previous, current);
410 /* XXX These two functions are very similar */
411 static int add_prefix(struct nid_prefix_map *map, struct prefixmsg *msg,
412 struct in6_addr *prefix, struct prefix_cacheinfo *cache)
414 struct nid_prefix_map *i = map;
417 if (!msg || !prefix || !cache)
419 /* XXX IF_PREFIX_AUTOCONF == 0x02 */
420 if (!(msg->prefix_flags & 0x02))
424 if (i->p.next != NULL)
426 if (ipv6_prefix_equal(prefix, &i->prefix->prefix.addr, msg->prefix_len) ||
427 ipv6_prefix_equal(prefix, &i->prefix->address.addr, msg->prefix_len)) {
428 i->prefix->mask |= HAS_PREFIX;
429 i->prefix->ifindex = msg->prefix_ifindex;
430 memcpy(&i->prefix->prefix.addr, prefix, sizeof(*prefix));
431 i->prefix->prefix.prefix_len = msg->prefix_len;
432 i->prefix->prefix.valid_until = time(NULL) + cache->preferred_time;
435 } while (i->p.next && i->nid == 0);
437 /* not yet in the map */
438 new = calloc(1, sizeof(*new));
441 new->mask = HAS_PREFIX;
442 memcpy(&new->prefix.addr, prefix, sizeof(*prefix));
443 new->prefix.prefix_len = msg->prefix_len;
444 new->prefix.valid_until = time(NULL) + cache->preferred_time;
445 if (add_prefix_to_map(map, new) == -1)
451 static inline int add_address(struct nid_prefix_map *map, struct ifaddrmsg *msg,
452 struct in6_addr *address, struct ifa_cacheinfo *cache)
454 struct nid_prefix_map *i = map;
457 if (!msg || !address || !cache)
460 if (address->s6_addr[11] != 0xFF || address->s6_addr[12] != 0xFE)
464 if (i->p.next != NULL)
466 if (ipv6_prefix_equal(address, &i->prefix->prefix.addr, msg->ifa_prefixlen) ||
467 ipv6_prefix_equal(address, &i->prefix->address.addr, 128)) {
468 i->prefix->mask |= HAS_ADDRESS;
469 memcpy(&i->prefix->address.addr, address, sizeof(*address));
470 i->prefix->address.prefix_len = msg->ifa_prefixlen;
471 i->prefix->address.valid_until = time(NULL) + cache->ifa_prefered;
474 } while (i->p.next && i->nid == 0);
476 new = calloc(1, sizeof(*new));
479 new->mask = HAS_ADDRESS;
480 memcpy(&new->address.addr, address, sizeof(*address));
481 new->address.prefix_len = msg->ifa_prefixlen;
482 new->address.valid_until = time(NULL) + cache->ifa_prefered;
483 if (add_prefix_to_map(map, new) == -1)
489 static struct nla_policy addr_policy[IFA_MAX+1] = {
490 [IFA_ADDRESS] = { .minlen = sizeof(struct in6_addr) },
491 [IFA_LABEL] = { .type = NLA_STRING,
492 .maxlen = IFNAMSIZ },
493 [IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) },
495 static struct nla_policy prefix_policy[PREFIX_MAX+1] = {
496 [PREFIX_ADDRESS] = { .minlen = sizeof(struct in6_addr) },
497 [PREFIX_CACHEINFO] = { .minlen = sizeof(struct prefix_cacheinfo) },
499 int handle_valid_msg(struct nl_msg *msg, void *arg)
501 struct nlmsghdr *nlh = nlmsg_hdr(msg);
504 struct sockaddr_nl *source = nlmsg_get_src(msg);
506 payload = nlmsg_data(nlh);
507 if (source->nl_groups == RTMGRP_IPV6_PREFIX) {
508 struct prefixmsg *prefixmsg;
509 struct in6_addr *prefix = NULL;
510 struct prefix_cacheinfo *cacheinfo = NULL;
511 struct nlattr *tb[PREFIX_MAX+1];
513 if (nlmsg_parse(nlh, sizeof(struct prefixmsg), tb, PREFIX_MAX, prefix_policy) < 0) {
514 syslog(LOG_ERR, "Failed to parse prefixmsg");
518 prefixmsg = (struct prefixmsg *) payload;
519 if (tb[PREFIX_ADDRESS])
520 prefix = nl_data_get(nla_get_data(tb[PREFIX_ADDRESS]));
521 if (tb[PREFIX_CACHEINFO])
522 cacheinfo = nl_data_get(nla_get_data(tb[PREFIX_CACHEINFO]));
523 ret = add_prefix(arg, prefixmsg, prefix, cacheinfo);
525 else if (source->nl_groups == RTMGRP_IPV6_IFADDR) {
526 struct ifaddrmsg *ifaddrmsg;
527 struct in6_addr *address = NULL;
528 struct ifa_cacheinfo *cacheinfo = NULL;
529 struct nlattr *tb[IFA_MAX+1];
531 if (nlmsg_parse(nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX, addr_policy) < 0) {
532 syslog(LOG_ERR, "Failed to parse ifaddrmsg");
536 ifaddrmsg = (struct ifaddrmsg *) payload;
538 address = nl_data_get(nla_get_data(tb[IFA_ADDRESS]));
539 if (tb[IFA_CACHEINFO])
540 cacheinfo = nl_data_get(nla_get_data(tb[IFA_CACHEINFO]));
541 ret = add_address(arg, ifaddrmsg, address, cacheinfo);
544 do_slices_autoconf(arg);
549 int handle_error_msg(struct sockaddr_nl *source, struct nlmsgerr *err,
552 syslog(LOG_ERR, "%s", strerror(err->error));
556 int handle_no_op(struct nl_msg *msg, void *arg)
561 /* only for access in the signal handler */
562 struct nid_prefix_map map = {
572 void signal_handler(int signal)
576 do_slices_autoconf(&map);
581 static int write_pidfile(const char *filename)
584 fp = fopen(filename, "w");
587 fprintf(fp, "%d\n", getpid());
592 int main(int argc, char *argv[])
596 openlog("vip6-autod", LOG_PERROR, LOG_DAEMON);
598 handle = nl_handle_alloc_nondefault(NL_CB_VERBOSE);
599 cbs = nl_handle_get_cb(handle);
600 nl_cb_set(cbs, NL_CB_VALID, NL_CB_CUSTOM, handle_valid_msg, &map);
601 nl_cb_set(cbs, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, handle_no_op, NULL);
602 nl_cb_err(cbs, NL_CB_CUSTOM, handle_error_msg, &map);
603 nl_disable_sequence_check(handle);
605 nl_join_groups(handle, RTMGRP_IPV6_PREFIX|RTMGRP_IPV6_IFADDR);
606 if (nl_connect(handle, NETLINK_ROUTE) == -1) {
607 syslog(LOG_CRIT, "nl_connect: %s", strerror(errno));
611 if (daemon(0, 0) == -1)
614 write_pidfile(LOCALSTATEDIR "/run/vip6-autod.pid");
616 signal(SIGUSR1, signal_handler);
618 while (nl_recvmsgs(handle, cbs) > 0);