2 * $Id: vip6-autod.c,v 1.3 2007/07/26 23:08:00 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
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 /* not defined for gcc -ansi */
32 typedef uint64_t __u64;
33 typedef int64_t __s64;
34 #include <netlink/netlink.h>
35 #include <netlink/route/addr.h>
38 #include "pathconfig.h"
40 #define HAS_ADDRESS 0x01
41 #define HAS_PREFIX 0x02
44 struct nid_list *next;
47 struct prefix_list *prev;
48 struct prefix_list *next;
61 struct nid_list *nids;
64 struct nl_handle *handle;
66 /* from linux/include/net/ipv6.h */
67 static inline int ipv6_prefix_equal(struct in6_addr *prefix,
68 struct in6_addr *addr, int prefixlen)
70 uint32_t *a1 = prefix->s6_addr32, *a2 = addr->s6_addr32;
73 /* check complete u32 in prefix */
75 if (pdw && memcmp(a1, a2, pdw << 2))
78 /* check incomplete u32 in prefix */
79 pbi = prefixlen & 0x1f;
80 if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
86 static int add_address_to_interface(int ifindex, struct in6_addr *address, int prefix)
89 struct rtnl_addr *rta;
92 nl = nl_addr_build(AF_INET6, address, sizeof(struct in6_addr));
93 rta = rtnl_addr_alloc();
95 rtnl_addr_set_family(rta, AF_INET6);
96 rtnl_addr_set_ifindex(rta, ifindex);
97 rtnl_addr_set_local(rta, nl);
98 rtnl_addr_set_prefixlen(rta, prefix);
100 if (rtnl_addr_add(handle, rta, NLM_F_REPLACE) != -1 || errno == EEXIST)
108 static int add_nid_to_list(struct prefix_list *i, nid_t nid)
111 n = calloc(1, sizeof(struct nid_list));
120 static void cleanup_prefix(struct prefix_list *i)
124 for (n = i->nids; n; n = n->next) {
125 struct rtnl_addr *rta;
129 memcpy(&a, &i->address.addr, sizeof(a));
130 rta = rtnl_addr_alloc();
131 nl = nl_addr_build(AF_INET6, &a, sizeof(a));
133 rtnl_addr_set_family(rta, AF_INET6);
134 rtnl_addr_set_ifindex(rta, i->ifindex);
135 rtnl_addr_set_local(rta, nl);
136 rtnl_addr_set_prefixlen(rta, i->address.prefix_len);
139 rtnl_addr_delete(handle, rta, 0);
146 static void do_slices_autoconf(struct prefix_list *head)
151 struct vc_net_nx addr;
152 struct prefix_list *i;
154 if ((dp = opendir("/proc/virtnet")) == NULL)
156 while ((de = readdir(dp)) != NULL) {
157 if (!isdigit(de->d_name[0]))
160 nid = strtoul(de->d_name, NULL, 10);
161 addr.type = vcNET_IPV6A;
163 if (vc_net_remove(nid, &addr) == -1) {
164 syslog(LOG_ERR, "vc_net_remove: %s", strerror(errno));
168 for (i = head->next; i;) {
170 if (i->mask & HAS_PREFIX && i->prefix.valid_until < time(NULL)) {
171 struct prefix_list *tmp;
174 inet_ntop(AF_INET6, &i->address.addr, buf, sizeof(buf));
175 syslog(LOG_NOTICE, "Address %s timed out", buf);
178 i->next->prev = i->prev;
180 i->prev->next = i->next;
189 if (i->mask != (HAS_ADDRESS|HAS_PREFIX))
193 addr.mask[0] = i->prefix.prefix_len;
194 memcpy(addr.ip, &i->address.addr, sizeof(struct in6_addr));
195 addr.ip[2] = htonl((ntohl(addr.ip[2]) & 0xffffff00) | ((nid & 0x7f00) >> 7));
196 addr.ip[3] = htonl((ntohl(addr.ip[3]) & 0x00ffffff) | ((nid & 0xff) << 25));
197 if (vc_net_add(nid, &addr) == -1) {
198 syslog(LOG_ERR, "vc_net_add: %s", strerror(errno));
201 if (add_address_to_interface(i->ifindex, (struct in6_addr *) addr.ip, i->prefix.prefix_len) == -1) {
202 syslog(LOG_ERR, "add_address_to_interface: %s", strerror(errno));
205 if (add_nid_to_list(i, nid) == -1) {
206 syslog(LOG_ERR, "add_nid_to_list: %s", strerror(errno));
216 static int add_prefix(struct prefix_list *head, struct prefixmsg *msg,
217 struct in6_addr *prefix, struct prefix_cacheinfo *cache)
219 struct prefix_list *i = head;
220 if (!msg || !prefix || !cache)
222 /* XXX IF_PREFIX_AUTOCONF == 0x02 */
223 if (!(msg->prefix_flags & 0x02))
229 if (ipv6_prefix_equal(prefix, &i->prefix.addr, msg->prefix_len) ||
230 ipv6_prefix_equal(prefix, &i->address.addr, msg->prefix_len)) {
231 i->mask |= HAS_PREFIX;
232 i->ifindex = msg->prefix_ifindex;
233 memcpy(&i->prefix.addr, prefix, sizeof(*prefix));
234 i->prefix.prefix_len = msg->prefix_len;
235 i->prefix.valid_until = time(NULL) + cache->preferred_time;
240 i->next = calloc(1, sizeof(*i));
245 i->mask = HAS_PREFIX;
246 memcpy(&i->prefix.addr, prefix, sizeof(*prefix));
247 i->prefix.prefix_len = msg->prefix_len;
248 i->prefix.valid_until = time(NULL) + cache->preferred_time;
253 static inline int add_address(struct prefix_list *head, struct ifaddrmsg *msg,
254 struct in6_addr *address, struct ifa_cacheinfo *cache)
256 struct prefix_list *i = head;
257 if (!msg || !address || !cache)
260 if (address->s6_addr[11] != 0xFF || address->s6_addr[12] != 0xFE)
266 if (ipv6_prefix_equal(address, &i->prefix.addr, msg->ifa_prefixlen) ||
267 ipv6_prefix_equal(address, &i->address.addr, msg->ifa_prefixlen)) {
268 i->mask |= HAS_ADDRESS;
269 memcpy(&i->address.addr, address, sizeof(*address));
270 i->address.prefix_len = msg->ifa_prefixlen;
271 i->address.valid_until = time(NULL) + cache->ifa_prefered;
276 i->next = calloc(1, sizeof(*i));
281 i->mask = HAS_ADDRESS;
282 memcpy(&i->address.addr, address, sizeof(*address));
283 i->address.prefix_len = msg->ifa_prefixlen;
284 i->address.valid_until = time(NULL) + cache->ifa_prefered;
289 static struct nla_policy addr_policy[IFA_MAX+1] = {
290 [IFA_ADDRESS] = { .minlen = sizeof(struct in6_addr) },
291 [IFA_LABEL] = { .type = NLA_STRING,
292 .maxlen = IFNAMSIZ },
293 [IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) },
295 static struct nla_policy prefix_policy[PREFIX_MAX+1] = {
296 [PREFIX_ADDRESS] = { .minlen = sizeof(struct in6_addr) },
297 [PREFIX_CACHEINFO] = { .minlen = sizeof(struct prefix_cacheinfo) },
299 int handle_valid_msg(struct nl_msg *msg, void *arg)
301 struct nlmsghdr *nlh = nlmsg_hdr(msg);
304 struct sockaddr_nl *source = nlmsg_get_src(msg);
306 payload = nlmsg_data(nlh);
307 if (source->nl_groups == RTMGRP_IPV6_PREFIX) {
308 struct prefixmsg *prefixmsg;
309 struct in6_addr *prefix = NULL;
310 struct prefix_cacheinfo *cacheinfo = NULL;
311 struct nlattr *tb[PREFIX_MAX+1];
313 if (nlmsg_parse(nlh, sizeof(struct prefixmsg), tb, PREFIX_MAX, prefix_policy) < 0) {
314 syslog(LOG_ERR, "Failed to parse prefixmsg");
318 prefixmsg = (struct prefixmsg *) payload;
319 if (tb[PREFIX_ADDRESS])
320 prefix = nl_data_get(nla_get_data(tb[PREFIX_ADDRESS]));
321 if (tb[PREFIX_CACHEINFO])
322 cacheinfo = nl_data_get(nla_get_data(tb[PREFIX_CACHEINFO]));
323 ret = add_prefix(arg, prefixmsg, prefix, cacheinfo);
325 else if (source->nl_groups == RTMGRP_IPV6_IFADDR) {
326 struct ifaddrmsg *ifaddrmsg;
327 struct in6_addr *address = NULL;
328 struct ifa_cacheinfo *cacheinfo = NULL;
329 struct nlattr *tb[IFA_MAX+1];
331 if (nlmsg_parse(nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX, addr_policy) < 0) {
332 syslog(LOG_ERR, "Failed to parse ifaddrmsg");
336 ifaddrmsg = (struct ifaddrmsg *) payload;
338 address = nl_data_get(nla_get_data(tb[IFA_ADDRESS]));
339 if (tb[IFA_CACHEINFO])
340 cacheinfo = nl_data_get(nla_get_data(tb[IFA_CACHEINFO]));
341 ret = add_address(arg, ifaddrmsg, address, cacheinfo);
344 do_slices_autoconf(arg);
349 int handle_error_msg(struct sockaddr_nl *source, struct nlmsgerr *err,
352 syslog(LOG_ERR, "%s", strerror(err->error));
356 int handle_no_op(struct nl_msg *msg, void *arg)
361 /* only for access in the signal handler */
362 struct prefix_list head;
363 void signal_handler(int signal)
367 do_slices_autoconf(&head);
372 static int write_pidfile(const char *filename)
375 fp = fopen(filename, "w");
378 fprintf(fp, "%d\n", getpid());
383 int main(int argc, char *argv[])
386 head.prev = head.next = NULL;
388 openlog("vip6-autod", LOG_PERROR, LOG_DAEMON);
390 handle = nl_handle_alloc_nondefault(NL_CB_VERBOSE);
391 cbs = nl_handle_get_cb(handle);
392 nl_cb_set(cbs, NL_CB_VALID, NL_CB_CUSTOM, handle_valid_msg, &head);
393 nl_cb_set(cbs, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, handle_no_op, NULL);
394 nl_cb_err(cbs, NL_CB_CUSTOM, handle_error_msg, &head);
395 nl_disable_sequence_check(handle);
397 nl_join_groups(handle, RTMGRP_IPV6_PREFIX|RTMGRP_IPV6_IFADDR);
398 if (nl_connect(handle, NETLINK_ROUTE) == -1) {
399 syslog(LOG_CRIT, "nl_connect: %s", strerror(errno));
403 if (daemon(0, 0) == -1)
406 /* XXX .. here is a hack */
407 write_pidfile(DEFAULT_PKGSTATEDIR "/../vip6-autod.pid");
409 signal(SIGUSR1, signal_handler);
411 while (nl_recvmsgs(handle, cbs) > 0);