Make vip6-autod a daemon
[util-vserver.git] / src / vip6-autod.c
1 /*
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>
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 prefix_list *i, nid_t nid)
109 {
110         struct nid_list *n;
111         n = calloc(1, sizeof(struct nid_list));
112         if (!n)
113                 return -1;
114         n->nid = nid;
115         n->next = i->nids;
116         i->nids = n;
117         return 0;
118 }
119
120 static void cleanup_prefix(struct prefix_list *i)
121 {
122         struct nid_list *n;
123
124         for (n = i->nids; n; n = n->next) {
125                 struct rtnl_addr *rta;
126                 struct nl_addr *nl;
127                 struct in6_addr a;
128
129                 memcpy(&a, &i->address.addr, sizeof(a));
130                 rta = rtnl_addr_alloc();
131                 nl = nl_addr_build(AF_INET6, &a, sizeof(a));
132
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);
137
138                 /* ignore errors */
139                 rtnl_addr_delete(handle, rta, 0);
140
141                 nl_addr_destroy(nl);
142                 rtnl_addr_free(rta);
143         }
144 }
145
146 static void do_slices_autoconf(struct prefix_list *head)
147 {
148         DIR *dp;
149         struct dirent *de;
150         nid_t nid;
151         struct vc_net_nx addr;
152         struct prefix_list *i;
153
154         if ((dp = opendir("/proc/virtnet")) == NULL)
155                 return;
156         while ((de = readdir(dp)) != NULL) {
157                 if (!isdigit(de->d_name[0]))
158                         continue;
159
160                 nid = strtoul(de->d_name, NULL, 10);
161                 addr.type = vcNET_IPV6A;
162                 addr.count = 0;
163                 if (vc_net_remove(nid, &addr) == -1) {
164                         syslog(LOG_ERR, "vc_net_remove: %s", strerror(errno));
165                         continue;
166                 }
167
168                 for (i = head->next; i;) {
169                         /* expired */
170                         if (i->mask & HAS_PREFIX && i->prefix.valid_until < time(NULL)) {
171                                 struct prefix_list *tmp;
172                                 char buf[64];
173
174                                 inet_ntop(AF_INET6, &i->address.addr, buf, sizeof(buf));
175                                 syslog(LOG_NOTICE, "Address %s timed out", buf);
176
177                                 if (i->next)
178                                         i->next->prev = i->prev;
179                                 if (i->prev)
180                                         i->prev->next = i->next;
181                                 tmp = i->next;
182
183                                 cleanup_prefix(i);
184
185                                 free(i);
186                                 i = tmp;
187                                 continue;
188                         }
189                         if (i->mask != (HAS_ADDRESS|HAS_PREFIX))
190                                 goto next;
191
192                         addr.count = 1;
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));
199                                 exit(1);
200                         }
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));
203                                 exit(1);
204                         }
205                         if (add_nid_to_list(i, nid) == -1) {
206                                 syslog(LOG_ERR, "add_nid_to_list: %s", strerror(errno));
207                                 exit(1);
208                         }
209 next:
210                         i = i->next;
211                 }
212         }
213         closedir(dp);
214 }
215
216 static int add_prefix(struct prefix_list *head, struct prefixmsg *msg,
217                       struct in6_addr *prefix, struct prefix_cacheinfo *cache)
218 {
219         struct prefix_list *i = head;
220         if (!msg || !prefix || !cache)
221                 return -1;
222         /* XXX IF_PREFIX_AUTOCONF == 0x02 */
223         if (!(msg->prefix_flags & 0x02))
224                 return -1;
225
226         do {
227                 if (i->next != NULL)
228                         i = i->next;
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;
236                         return 0;
237                 }
238         } while (i->next);
239
240         i->next = calloc(1, sizeof(*i));
241         if (!i->next)
242                 return -1;
243         i->next->prev = i;
244         i = i->next;
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;
249
250         return 1;
251 }
252
253 static inline int add_address(struct prefix_list *head, struct ifaddrmsg *msg,
254                               struct in6_addr *address, struct ifa_cacheinfo *cache)
255 {
256         struct prefix_list *i = head;
257         if (!msg || !address || !cache)
258                 return -1;
259
260         if (address->s6_addr[11] != 0xFF || address->s6_addr[12] != 0xFE)
261                 return -1;
262
263         do {
264                 if (i->next != NULL)
265                         i = i->next;
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;
272                         return 0;
273                 }
274         } while (i->next);
275
276         i->next = calloc(1, sizeof(*i));
277         if (!i->next)
278                 return -1;
279         i->next->prev = i;
280         i = i->next;
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;
285
286         return 1;
287 }
288
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) },
294 };
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) },
298 };
299 int handle_valid_msg(struct nl_msg *msg, void *arg)
300 {
301         struct nlmsghdr *nlh = nlmsg_hdr(msg);
302         int ret = -1;
303         char *payload;
304         struct sockaddr_nl *source = nlmsg_get_src(msg);
305
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];
312
313                 if (nlmsg_parse(nlh, sizeof(struct prefixmsg), tb, PREFIX_MAX, prefix_policy) < 0) {
314                         syslog(LOG_ERR, "Failed to parse prefixmsg");
315                         return -1;
316                 }
317
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);
324         }       
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];
330
331                 if (nlmsg_parse(nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX, addr_policy) < 0) {
332                         syslog(LOG_ERR, "Failed to parse ifaddrmsg");
333                         return -1;
334                 }
335
336                 ifaddrmsg = (struct ifaddrmsg *) payload;
337                 if (tb[IFA_ADDRESS])
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);
342         }
343         if (ret >= 0)
344                 do_slices_autoconf(arg);
345
346         return 0;
347 }
348
349 int handle_error_msg(struct sockaddr_nl *source, struct nlmsgerr *err,
350                      void *arg)
351 {
352         syslog(LOG_ERR, "%s", strerror(err->error));
353         return 0;
354 }
355
356 int handle_no_op(struct nl_msg *msg, void *arg)
357 {
358         return 0;
359 }
360
361 /* only for access in the signal handler */
362 struct prefix_list head;
363 void signal_handler(int signal)
364 {
365         switch (signal) {
366         case SIGUSR1:
367                 do_slices_autoconf(&head);
368                 break;
369         }
370 }
371
372 static int write_pidfile(const char *filename)
373 {
374         FILE *fp;
375         fp = fopen(filename, "w");
376         if (!fp)
377                 return -1;
378         fprintf(fp, "%d\n", getpid());
379         fclose(fp);
380         return 0;
381 }
382
383 int main(int argc, char *argv[])
384 {
385         struct nl_cb *cbs;
386         head.prev = head.next = NULL;
387
388         openlog("vip6-autod", LOG_PERROR, LOG_DAEMON);
389
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);
396
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));
400                 exit(1);
401         }
402
403         if (daemon(0, 0) == -1)
404                 return -1;
405
406         /* XXX .. here is a hack */
407         write_pidfile(DEFAULT_PKGSTATEDIR "/../vip6-autod.pid");
408
409         signal(SIGUSR1, signal_handler);
410
411         while (nl_recvmsgs(handle, cbs) > 0);
412
413         nl_close(handle);
414         closelog();
415         return 0;
416 }