2 * $Id: vip6-autod.c,v 1.2 2007/07/26 19:00:23 dhozac Exp $
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
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <sys/ioctl.h>
19 #include <arpa/inet.h>
25 #include <asm/types.h>
26 /* not defined for gcc -ansi */
27 typedef uint64_t __u64;
28 typedef int64_t __s64;
29 #include <netlink/netlink.h>
30 #include <netlink/route/addr.h>
32 typedef unsigned int nid_t;
33 typedef unsigned int xid_t;
36 #define HAS_ADDRESS 0x01
37 #define HAS_PREFIX 0x02
40 struct nid_list *next;
43 struct prefix_list *prev;
44 struct prefix_list *next;
57 struct nid_list *nids;
60 struct nl_handle *handle;
62 /* from linux/include/net/ipv6.h */
63 static inline int ipv6_prefix_equal(struct in6_addr *prefix,
64 struct in6_addr *addr, int prefixlen)
66 uint32_t *a1 = prefix->s6_addr32, *a2 = addr->s6_addr32;
69 /* check complete u32 in prefix */
71 if (pdw && memcmp(a1, a2, pdw << 2))
74 /* check incomplete u32 in prefix */
75 pbi = prefixlen & 0x1f;
76 if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
82 static int add_address_to_interface(int ifindex, struct in6_addr *address, int prefix)
85 struct rtnl_addr *rta;
88 nl = nl_addr_build(AF_INET6, address, sizeof(struct in6_addr));
89 rta = rtnl_addr_alloc();
91 rtnl_addr_set_family(rta, AF_INET6);
92 rtnl_addr_set_ifindex(rta, ifindex);
93 rtnl_addr_set_local(rta, nl);
94 rtnl_addr_set_prefixlen(rta, prefix);
96 if (rtnl_addr_add(handle, rta, NLM_F_REPLACE) != -1 || errno == EEXIST)
104 static int add_nid_to_list(struct prefix_list *i, nid_t nid)
107 n = calloc(1, sizeof(struct nid_list));
116 static void cleanup_prefix(struct prefix_list *i)
120 for (n = i->nids; n; n = n->next) {
121 struct rtnl_addr *rta;
125 memcpy(&a, &i->address.addr, sizeof(a));
126 rta = rtnl_addr_alloc();
127 nl = nl_addr_build(AF_INET6, &a, sizeof(a));
129 rtnl_addr_set_family(rta, AF_INET6);
130 rtnl_addr_set_ifindex(rta, i->ifindex);
131 rtnl_addr_set_local(rta, nl);
132 rtnl_addr_set_prefixlen(rta, i->address.prefix_len);
135 rtnl_addr_delete(handle, rta, 0);
142 static void do_slices_autoconf(struct prefix_list *head)
147 struct vc_net_nx addr;
148 struct prefix_list *i;
150 if ((dp = opendir("/proc/virtnet")) == NULL)
152 while ((de = readdir(dp)) != NULL) {
153 if (!isdigit(de->d_name[0]))
156 nid = strtoul(de->d_name, NULL, 10);
157 addr.type = vcNET_IPV6A;
159 if (vc_net_remove(nid, &addr) == -1) {
160 perror("vc_net_remove");
164 for (i = head->next; i;) {
166 if (i->mask & HAS_PREFIX && i->prefix.valid_until < time(NULL)) {
167 struct prefix_list *tmp;
170 inet_ntop(AF_INET6, &i->address.addr, buf, sizeof(buf));
171 printf("Address %s timed out.\n", buf);
174 i->next->prev = i->prev;
176 i->prev->next = i->next;
185 if (i->mask != (HAS_ADDRESS|HAS_PREFIX))
189 addr.mask[0] = i->prefix.prefix_len;
190 memcpy(addr.ip, &i->address.addr, sizeof(struct in6_addr));
191 addr.ip[2] = htonl((ntohl(addr.ip[2]) & 0xffffff00) | ((nid & 0x7f00) >> 7));
192 addr.ip[3] = htonl((ntohl(addr.ip[3]) & 0x00ffffff) | ((nid & 0xff) << 25));
193 if (vc_net_add(nid, &addr) == -1) {
194 perror("vc_net_add");
197 if (add_address_to_interface(i->ifindex, (struct in6_addr *) addr.ip, i->prefix.prefix_len) == -1) {
198 perror("add_address_to_interface");
201 if (add_nid_to_list(i, nid) == -1) {
202 perror("add_nid_to_list");
212 static int add_prefix(struct prefix_list *head, struct prefixmsg *msg,
213 struct in6_addr *prefix, struct prefix_cacheinfo *cache)
215 struct prefix_list *i = head;
216 if (!msg || !prefix || !cache)
218 /* XXX IF_PREFIX_AUTOCONF == 0x02 */
219 if (!(msg->prefix_flags & 0x02))
225 if (ipv6_prefix_equal(prefix, &i->prefix.addr, msg->prefix_len) ||
226 ipv6_prefix_equal(prefix, &i->address.addr, msg->prefix_len)) {
227 i->mask |= HAS_PREFIX;
228 i->ifindex = msg->prefix_ifindex;
229 memcpy(&i->prefix.addr, prefix, sizeof(*prefix));
230 i->prefix.prefix_len = msg->prefix_len;
231 i->prefix.valid_until = time(NULL) + cache->preferred_time;
236 i->next = calloc(1, sizeof(*i));
241 i->mask = HAS_PREFIX;
242 memcpy(&i->prefix.addr, prefix, sizeof(*prefix));
243 i->prefix.prefix_len = msg->prefix_len;
244 i->prefix.valid_until = time(NULL) + cache->preferred_time;
249 static inline int add_address(struct prefix_list *head, struct ifaddrmsg *msg,
250 struct in6_addr *address, struct ifa_cacheinfo *cache)
252 struct prefix_list *i = head;
253 if (!msg || !address || !cache)
256 if (address->s6_addr[11] != 0xFF || address->s6_addr[12] != 0xFE)
262 if (ipv6_prefix_equal(address, &i->prefix.addr, msg->ifa_prefixlen) ||
263 ipv6_prefix_equal(address, &i->address.addr, msg->ifa_prefixlen)) {
264 i->mask |= HAS_ADDRESS;
265 memcpy(&i->address.addr, address, sizeof(*address));
266 i->address.prefix_len = msg->ifa_prefixlen;
267 i->address.valid_until = time(NULL) + cache->ifa_prefered;
272 i->next = calloc(1, sizeof(*i));
277 i->mask = HAS_ADDRESS;
278 memcpy(&i->address.addr, address, sizeof(*address));
279 i->address.prefix_len = msg->ifa_prefixlen;
280 i->address.valid_until = time(NULL) + cache->ifa_prefered;
285 static struct nla_policy addr_policy[IFA_MAX+1] = {
286 [IFA_ADDRESS] = { .minlen = sizeof(struct in6_addr) },
287 [IFA_LABEL] = { .type = NLA_STRING,
288 .maxlen = IFNAMSIZ },
289 [IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) },
291 static struct nla_policy prefix_policy[PREFIX_MAX+1] = {
292 [PREFIX_ADDRESS] = { .minlen = sizeof(struct in6_addr) },
293 [PREFIX_CACHEINFO] = { .minlen = sizeof(struct prefix_cacheinfo) },
295 int handle_valid_msg(struct nl_msg *msg, void *arg)
297 struct nlmsghdr *nlh = nlmsg_hdr(msg);
300 struct sockaddr_nl *source = nlmsg_get_src(msg);
302 payload = nlmsg_data(nlh);
303 if (source->nl_groups == RTMGRP_IPV6_PREFIX) {
304 struct prefixmsg *prefixmsg;
305 struct in6_addr *prefix = NULL;
306 struct prefix_cacheinfo *cacheinfo = NULL;
307 struct nlattr *tb[PREFIX_MAX+1];
309 if (nlmsg_parse(nlh, sizeof(struct prefixmsg), tb, PREFIX_MAX, prefix_policy) < 0) {
310 printf("Failed to parse prefixmsg.\n");
314 prefixmsg = (struct prefixmsg *) payload;
315 if (tb[PREFIX_ADDRESS])
316 prefix = nl_data_get(nla_get_data(tb[PREFIX_ADDRESS]));
317 if (tb[PREFIX_CACHEINFO])
318 cacheinfo = nl_data_get(nla_get_data(tb[PREFIX_CACHEINFO]));
319 ret = add_prefix(arg, prefixmsg, prefix, cacheinfo);
321 else if (source->nl_groups == RTMGRP_IPV6_IFADDR) {
322 struct ifaddrmsg *ifaddrmsg;
323 struct in6_addr *address = NULL;
324 struct ifa_cacheinfo *cacheinfo = NULL;
325 struct nlattr *tb[IFA_MAX+1];
327 if (nlmsg_parse(nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX, addr_policy) < 0) {
328 printf("Failed to parse ifaddrmsg.\n");
332 ifaddrmsg = (struct ifaddrmsg *) payload;
334 address = nl_data_get(nla_get_data(tb[IFA_ADDRESS]));
335 if (tb[IFA_CACHEINFO])
336 cacheinfo = nl_data_get(nla_get_data(tb[IFA_CACHEINFO]));
337 ret = add_address(arg, ifaddrmsg, address, cacheinfo);
340 do_slices_autoconf(arg);
345 int handle_error_msg(struct sockaddr_nl *source, struct nlmsgerr *err,
348 perror("Got an error");
352 int handle_no_op(struct nl_msg *msg, void *arg)
357 /* only for access in the signal handler */
358 struct prefix_list head;
359 void signal_handler(int signal)
363 do_slices_autoconf(&head);
368 int main(int argc, char *argv[])
372 head.prev = head.next = NULL;
373 signal(SIGUSR1, signal_handler);
375 handle = nl_handle_alloc_nondefault(NL_CB_VERBOSE);
376 cbs = nl_handle_get_cb(handle);
377 nl_cb_set(cbs, NL_CB_VALID, NL_CB_CUSTOM, handle_valid_msg, &head);
378 nl_cb_set(cbs, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, handle_no_op, NULL);
379 nl_cb_err(cbs, NL_CB_CUSTOM, handle_error_msg, &head);
380 nl_disable_sequence_check(handle);
382 nl_join_groups(handle, RTMGRP_IPV6_PREFIX|RTMGRP_IPV6_IFADDR);
383 if (nl_connect(handle, NETLINK_ROUTE) == -1) {
384 perror("nl_connect");
388 while (nl_recvmsgs(handle, cbs) > 0);