1b60aa0d79fa0611b2ccfc74c3d1a6dd0657a26e
[util-vserver.git] / src / vip6-autod.c
1 /*
2  * $Id: vip6-autod.c,v 1.4 2007/07/30 14:59:11 dhozac Exp $
3  * Copyright (c) 2007 The Trustees of Princeton University
4  * Author: Daniel Hokka Zakrisson <daniel@hozac.com>
5  *
6  * Licensed under the terms of the GNU General Public License
7  * version 2 or later.
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #  include <config.h>
12 #endif
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stdint.h>
18 #include <string.h>
19 #include <time.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <arpa/inet.h>
24 #include <dirent.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <signal.h>
28 #include <syslog.h>
29
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>
36
37 #include <vserver.h>
38 #include "pathconfig.h"
39
40 #define HAS_ADDRESS     0x01
41 #define HAS_PREFIX      0x02
42 struct nid_list {
43         nid_t nid;
44         struct nid_list *next;
45 };
46 struct prefix_list {
47         struct prefix_list *prev;
48         struct prefix_list *next;
49         uint32_t mask;
50         int ifindex;
51         struct {
52                 struct in6_addr addr;
53                 int prefix_len;
54                 time_t valid_until;
55         } prefix;
56         struct {
57                 struct in6_addr addr;
58                 int prefix_len;
59                 time_t valid_until;
60         } address;
61         struct nid_list *nids;
62 };
63
64 struct nl_handle *handle;
65
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)
69 {
70         uint32_t *a1 = prefix->s6_addr32, *a2 = addr->s6_addr32;
71         unsigned pdw, pbi;
72
73         /* check complete u32 in prefix */
74         pdw = prefixlen >> 5;
75         if (pdw && memcmp(a1, a2, pdw << 2))
76                 return 0;
77
78         /* check incomplete u32 in prefix */
79         pbi = prefixlen & 0x1f;
80         if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
81                 return 0;
82
83         return 1;
84 }
85
86 static int add_address_to_interface(int ifindex, struct in6_addr *address, int prefix)
87 {
88         int err = -1;
89         struct rtnl_addr *rta;
90         struct nl_addr *nl;
91
92         nl = nl_addr_build(AF_INET6, address, sizeof(struct in6_addr));
93         rta = rtnl_addr_alloc();
94
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);
99
100         if (rtnl_addr_add(handle, rta, NLM_F_REPLACE) != -1 || errno == EEXIST)
101                 err = 0;
102
103         rtnl_addr_free(rta);
104         nl_addr_destroy(nl);
105         return err;
106 }
107
108 static int add_nid_to_list(struct nid_list **l, nid_t nid)
109 {
110         struct nid_list *n;
111         for (n = *l; n; n = n->next) {
112                 if (n->nid == nid)
113                         return 0;
114         }
115         n = calloc(1, sizeof(struct nid_list));
116         if (!n)
117                 return -1;
118         n->nid = nid;
119         n->next = *l;
120         *l = n;
121         return 1;
122 }
123
124 static void cleanup_prefix(struct prefix_list *i)
125 {
126         struct nid_list *n, *p = NULL;
127
128         for (n = i->nids; n; n = n->next) {
129                 struct rtnl_addr *rta;
130                 struct nl_addr *nl;
131                 struct in6_addr a;
132
133                 if (p)
134                         free(p);
135                 memcpy(&a, &i->address.addr, sizeof(a));
136                 rta = rtnl_addr_alloc();
137                 nl = nl_addr_build(AF_INET6, &a, sizeof(a));
138
139                 rtnl_addr_set_family(rta, AF_INET6);
140                 rtnl_addr_set_ifindex(rta, i->ifindex);
141                 rtnl_addr_set_local(rta, nl);
142                 rtnl_addr_set_prefixlen(rta, i->address.prefix_len);
143
144                 /* ignore errors */
145                 rtnl_addr_delete(handle, rta, 0);
146
147                 nl_addr_destroy(nl);
148                 rtnl_addr_free(rta);
149
150                 p = n;
151         }
152         if (p)
153                 free(p);
154 }
155
156 static void do_slices_autoconf(struct prefix_list *head)
157 {
158         DIR *dp;
159         struct dirent *de;
160         nid_t nid;
161         struct vc_net_nx addr;
162         struct prefix_list *i;
163         struct nid_list *current = NULL, *n;
164
165         if ((dp = opendir("/proc/virtnet")) == NULL)
166                 return;
167         while ((de = readdir(dp)) != NULL) {
168                 if (!isdigit(de->d_name[0]))
169                         continue;
170
171                 nid = strtoul(de->d_name, NULL, 10);
172                 addr.type = vcNET_IPV6A;
173                 addr.count = 0;
174                 if (vc_net_remove(nid, &addr) == -1) {
175                         syslog(LOG_ERR, "vc_net_remove(%u): %s", nid, strerror(errno));
176                         continue;
177                 }
178
179                 add_nid_to_list(&current, nid);
180
181                 for (i = head->next; i;) {
182                         /* expired */
183                         if (i->mask & HAS_PREFIX && i->prefix.valid_until < time(NULL)) {
184                                 struct prefix_list *tmp;
185                                 char buf[64];
186
187                                 inet_ntop(AF_INET6, &i->address.addr, buf, sizeof(buf));
188                                 syslog(LOG_NOTICE, "Address %s timed out", buf);
189
190                                 if (i->next)
191                                         i->next->prev = i->prev;
192                                 if (i->prev)
193                                         i->prev->next = i->next;
194                                 tmp = i->next;
195
196                                 cleanup_prefix(i);
197
198                                 free(i);
199                                 i = tmp;
200                                 continue;
201                         }
202                         if (i->mask != (HAS_ADDRESS|HAS_PREFIX))
203                                 goto next;
204
205                         addr.type = vcNET_IPV6;
206                         addr.count = 1;
207                         addr.mask[0] = i->prefix.prefix_len;
208                         memcpy(addr.ip, &i->address.addr, sizeof(struct in6_addr));
209                         addr.ip[2] = htonl((ntohl(addr.ip[2]) & 0xffffff00) | ((nid & 0x7f80) >> 7));
210                         addr.ip[3] = htonl((ntohl(addr.ip[3]) & 0x00ffffff) | ((nid & 0x7f) << 25));
211                         if (vc_net_add(nid, &addr) == -1) {
212                                 syslog(LOG_ERR, "vc_net_add(%u): %s", nid, strerror(errno));
213                                 goto next;
214                         }
215                         if (add_address_to_interface(i->ifindex, (struct in6_addr *) addr.ip, i->prefix.prefix_len) == -1) {
216                                 syslog(LOG_ERR, "add_address_to_interface: %s", strerror(errno));
217                                 goto next;
218                         }
219                         if (add_nid_to_list(&i->nids, nid) == -1) {
220                                 syslog(LOG_ERR, "add_nid_to_list: %s", strerror(errno));
221                                 goto next;
222                         }
223 next:
224                         i = i->next;
225                 }
226         }
227         closedir(dp);
228 }
229
230 static int add_prefix(struct prefix_list *head, struct prefixmsg *msg,
231                       struct in6_addr *prefix, struct prefix_cacheinfo *cache)
232 {
233         struct prefix_list *i = head;
234         if (!msg || !prefix || !cache)
235                 return -1;
236         /* XXX IF_PREFIX_AUTOCONF == 0x02 */
237         if (!(msg->prefix_flags & 0x02))
238                 return -1;
239
240         do {
241                 if (i->next != NULL)
242                         i = i->next;
243                 if (ipv6_prefix_equal(prefix, &i->prefix.addr, msg->prefix_len) ||
244                     ipv6_prefix_equal(prefix, &i->address.addr, msg->prefix_len)) {
245                         i->mask |= HAS_PREFIX;
246                         i->ifindex = msg->prefix_ifindex;
247                         memcpy(&i->prefix.addr, prefix, sizeof(*prefix));
248                         i->prefix.prefix_len = msg->prefix_len;
249                         i->prefix.valid_until = time(NULL) + cache->preferred_time;
250                         return 0;
251                 }
252         } while (i->next);
253
254         i->next = calloc(1, sizeof(*i));
255         if (!i->next)
256                 return -1;
257         i->next->prev = i;
258         i = i->next;
259         i->mask = HAS_PREFIX;
260         memcpy(&i->prefix.addr, prefix, sizeof(*prefix));
261         i->prefix.prefix_len = msg->prefix_len;
262         i->prefix.valid_until = time(NULL) + cache->preferred_time;
263
264         return 1;
265 }
266
267 static inline int add_address(struct prefix_list *head, struct ifaddrmsg *msg,
268                               struct in6_addr *address, struct ifa_cacheinfo *cache)
269 {
270         struct prefix_list *i = head;
271         if (!msg || !address || !cache)
272                 return -1;
273
274         if (address->s6_addr[11] != 0xFF || address->s6_addr[12] != 0xFE)
275                 return -1;
276
277         do {
278                 if (i->next != NULL)
279                         i = i->next;
280                 if (ipv6_prefix_equal(address, &i->prefix.addr, msg->ifa_prefixlen) ||
281                     ipv6_prefix_equal(address, &i->address.addr, msg->ifa_prefixlen)) {
282                         i->mask |= HAS_ADDRESS;
283                         memcpy(&i->address.addr, address, sizeof(*address));
284                         i->address.prefix_len = msg->ifa_prefixlen;
285                         i->address.valid_until = time(NULL) + cache->ifa_prefered;
286                         return 0;
287                 }
288         } while (i->next);
289
290         i->next = calloc(1, sizeof(*i));
291         if (!i->next)
292                 return -1;
293         i->next->prev = i;
294         i = i->next;
295         i->mask = HAS_ADDRESS;
296         memcpy(&i->address.addr, address, sizeof(*address));
297         i->address.prefix_len = msg->ifa_prefixlen;
298         i->address.valid_until = time(NULL) + cache->ifa_prefered;
299
300         return 1;
301 }
302
303 static struct nla_policy addr_policy[IFA_MAX+1] = {
304         [IFA_ADDRESS]   = { .minlen = sizeof(struct in6_addr) },
305         [IFA_LABEL]     = { .type = NLA_STRING,
306                             .maxlen = IFNAMSIZ },
307         [IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) },
308 };
309 static struct nla_policy prefix_policy[PREFIX_MAX+1] = {
310         [PREFIX_ADDRESS]   = { .minlen = sizeof(struct in6_addr) },
311         [PREFIX_CACHEINFO] = { .minlen = sizeof(struct prefix_cacheinfo) },
312 };
313 int handle_valid_msg(struct nl_msg *msg, void *arg)
314 {
315         struct nlmsghdr *nlh = nlmsg_hdr(msg);
316         int ret = -1;
317         char *payload;
318         struct sockaddr_nl *source = nlmsg_get_src(msg);
319
320         payload = nlmsg_data(nlh);
321         if (source->nl_groups == RTMGRP_IPV6_PREFIX) {
322                 struct prefixmsg *prefixmsg;
323                 struct in6_addr *prefix = NULL;
324                 struct prefix_cacheinfo *cacheinfo = NULL;
325                 struct nlattr *tb[PREFIX_MAX+1];
326
327                 if (nlmsg_parse(nlh, sizeof(struct prefixmsg), tb, PREFIX_MAX, prefix_policy) < 0) {
328                         syslog(LOG_ERR, "Failed to parse prefixmsg");
329                         return -1;
330                 }
331
332                 prefixmsg = (struct prefixmsg *) payload;
333                 if (tb[PREFIX_ADDRESS])
334                         prefix = nl_data_get(nla_get_data(tb[PREFIX_ADDRESS]));
335                 if (tb[PREFIX_CACHEINFO])
336                         cacheinfo = nl_data_get(nla_get_data(tb[PREFIX_CACHEINFO]));
337                 ret = add_prefix(arg, prefixmsg, prefix, cacheinfo);
338         }       
339         else if (source->nl_groups == RTMGRP_IPV6_IFADDR) {
340                 struct ifaddrmsg *ifaddrmsg;
341                 struct in6_addr *address = NULL;
342                 struct ifa_cacheinfo *cacheinfo = NULL;
343                 struct nlattr *tb[IFA_MAX+1];
344
345                 if (nlmsg_parse(nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX, addr_policy) < 0) {
346                         syslog(LOG_ERR, "Failed to parse ifaddrmsg");
347                         return -1;
348                 }
349
350                 ifaddrmsg = (struct ifaddrmsg *) payload;
351                 if (tb[IFA_ADDRESS])
352                         address = nl_data_get(nla_get_data(tb[IFA_ADDRESS]));
353                 if (tb[IFA_CACHEINFO])
354                         cacheinfo = nl_data_get(nla_get_data(tb[IFA_CACHEINFO]));
355                 ret = add_address(arg, ifaddrmsg, address, cacheinfo);
356         }
357         if (ret >= 0)
358                 do_slices_autoconf(arg);
359
360         return 0;
361 }
362
363 int handle_error_msg(struct sockaddr_nl *source, struct nlmsgerr *err,
364                      void *arg)
365 {
366         syslog(LOG_ERR, "%s", strerror(err->error));
367         return 0;
368 }
369
370 int handle_no_op(struct nl_msg *msg, void *arg)
371 {
372         return 0;
373 }
374
375 /* only for access in the signal handler */
376 struct prefix_list head;
377 void signal_handler(int signal)
378 {
379         switch (signal) {
380         case SIGUSR1:
381                 do_slices_autoconf(&head);
382                 break;
383         }
384 }
385
386 static int write_pidfile(const char *filename)
387 {
388         FILE *fp;
389         fp = fopen(filename, "w");
390         if (!fp)
391                 return -1;
392         fprintf(fp, "%d\n", getpid());
393         fclose(fp);
394         return 0;
395 }
396
397 int main(int argc, char *argv[])
398 {
399         struct nl_cb *cbs;
400         head.prev = head.next = NULL;
401
402         openlog("vip6-autod", LOG_PERROR, LOG_DAEMON);
403
404         handle = nl_handle_alloc_nondefault(NL_CB_VERBOSE);
405         cbs = nl_handle_get_cb(handle);
406         nl_cb_set(cbs, NL_CB_VALID, NL_CB_CUSTOM, handle_valid_msg, &head);
407         nl_cb_set(cbs, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, handle_no_op, NULL);
408         nl_cb_err(cbs, NL_CB_CUSTOM, handle_error_msg, &head);
409         nl_disable_sequence_check(handle);
410
411         nl_join_groups(handle, RTMGRP_IPV6_PREFIX|RTMGRP_IPV6_IFADDR);
412         if (nl_connect(handle, NETLINK_ROUTE) == -1) {
413                 syslog(LOG_CRIT, "nl_connect: %s", strerror(errno));
414                 exit(1);
415         }
416
417         if (daemon(0, 0) == -1)
418                 return -1;
419
420         /* XXX .. here is a hack */
421         write_pidfile(DEFAULT_PKGSTATEDIR "/../vip6-autod.pid");
422
423         signal(SIGUSR1, signal_handler);
424
425         while (nl_recvmsgs(handle, cbs) > 0);
426
427         nl_close(handle);
428         closelog();
429         return 0;
430 }