]> git.decadent.org.uk Git - odhcp6c.git/blob - src/odhcp6c.c
Wait 1 second between disable and enable calls
[odhcp6c.git] / src / odhcp6c.c
1 /**
2  * Copyright (C) 2012 Steven Barth <steven@midlink.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License v2 as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14
15 #include <time.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <syslog.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <stdbool.h>
25
26 #include <net/if.h>
27 #include <sys/wait.h>
28 #include <sys/syscall.h>
29
30 #include "odhcp6c.h"
31
32
33 static void sighandler(int signal);
34 static int sysctl_interface(const char *ifname, const char *option,
35                 const char *data);
36 static int usage(void);
37
38
39 static uint8_t *state_data[_STATE_MAX] = {NULL};
40 static size_t state_len[_STATE_MAX] = {0};
41
42 static volatile int do_signal = 0;
43
44
45 int main(_unused int argc, char* const argv[])
46 {
47         openlog("odhcp6c", LOG_PERROR | LOG_PID, LOG_DAEMON);
48
49         // Allocate ressources
50         const char *pidfile = NULL;
51         const char *script = "/usr/sbin/odhcp6c-update";
52         ssize_t l;
53         uint8_t buf[134];
54         char *optpos;
55         uint16_t opttype;
56         enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
57
58         bool help = false, daemonize = false, reset = false;
59         int c, request_pd = 0, timeout = 0;
60         while ((c = getopt(argc, argv, "RN:P:c:r:s:t:hdp:")) != -1) {
61                 switch (c) {
62                 case 'R':
63                         reset = true;
64                         break;
65
66                 case 'N':
67                         if (!strcmp(optarg, "force"))
68                                 ia_na_mode = IA_MODE_FORCE;
69                         else if (!strcmp(optarg, "none"))
70                                 ia_na_mode = IA_MODE_NONE;
71                         else if (!strcmp(optarg, "try"))
72                                 ia_na_mode = IA_MODE_TRY;
73                         else
74                                 help = true;
75                         break;
76
77                 case 'P':
78                         request_pd = strtoul(optarg, NULL, 10);
79                         if (request_pd == 0)
80                                 request_pd = -1;
81                         break;
82
83                 case 'c':
84                         l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
85                         if (l > 0) {
86                                 buf[0] = 0;
87                                 buf[1] = DHCPV6_OPT_CLIENTID;
88                                 buf[2] = 0;
89                                 buf[4] = l;
90                                 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
91                         } else {
92                                 help = true;
93                         }
94                         break;
95
96                 case 'r':
97                         optpos = optarg;
98                         while (optpos[0]) {
99                                 opttype = htons(strtoul(optarg, &optpos, 10));
100                                 if (optpos == optarg)
101                                         break;
102                                 else if (optpos[0])
103                                         optarg = &optpos[1];
104                                 odhcp6c_add_state(STATE_ORO, &opttype, 2);
105                         }
106                         break;
107
108                 case 's':
109                         script = optarg;
110                         break;
111
112                 case 't':
113                         timeout = strtoul(optarg, NULL, 10);
114                         break;
115
116                 case 'd':
117                         daemonize = true;
118                         break;
119
120                 case 'p':
121                         pidfile = optarg;
122                         break;
123
124                 default:
125                         help = true;
126                         break;
127                 }
128         }
129
130         const char *ifname = argv[optind];
131
132         if (help || !ifname)
133                 return usage();
134
135         if (init_dhcpv6(ifname, request_pd) || init_rtnetlink() ||
136                         script_init(script, ifname)) {
137                 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
138                 return 3;
139         }
140
141         signal(SIGHUP, sighandler);
142         signal(SIGINT, sighandler);
143         signal(SIGALRM, sighandler);
144         signal(SIGCHLD, sighandler);
145         signal(SIGTERM, sighandler);
146         signal(SIGUSR1, sighandler);
147         signal(SIGUSR2, sighandler);
148
149         // Configure interface to accept RA
150         if (reset) {
151                 sysctl_interface(ifname, "accept_ra", "2");
152                 sysctl_interface(ifname, "disable_ipv6", "1");
153                 struct timespec ts = {1, 0};
154                 nanosleep(&ts, NULL);
155                 sysctl_interface(ifname, "disable_ipv6", "0");
156         }
157
158         if (daemonize) {
159                 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
160                 if (daemon(0, 0)) {
161                         syslog(LOG_ERR, "Failed to daemonize: %s",
162                                         strerror(errno));
163                         return 4;
164                 }
165
166                 char pidbuf[128];
167                 if (!pidfile) {
168                         snprintf(pidbuf, sizeof(pidbuf),
169                                         "/var/run/odhcp6c.%s.pid", ifname);
170                         pidfile = pidbuf;
171                 }
172
173                 int fd = open(pidfile, O_WRONLY | O_CREAT);
174                 if (fd >= 0) {
175                         char buf[8];
176                         int len = snprintf(buf, sizeof(buf), "%i\n", getpid());
177                         write(fd, buf, len);
178                         close(fd);
179                 }
180         }
181
182         while (do_signal != SIGTERM) { // Main logic
183                 odhcp6c_clear_state(STATE_SERVER_ID);
184                 odhcp6c_clear_state(STATE_SERVER_CAND);
185                 odhcp6c_clear_state(STATE_IA_PD);
186                 odhcp6c_clear_state(STATE_IA_PD_LOST);
187                 dhcpv6_set_ia_na_mode(ia_na_mode);
188
189                 alarm(timeout);
190                 do_signal = 0;
191                 int res = dhcpv6_request(DHCPV6_MSG_SOLICIT);
192
193                 if (res < 0) {
194                         continue; // Might happen if we got a signal
195                 } else if (res == DHCPV6_STATELESS) { // Stateless mode
196                         while (do_signal == 0 || do_signal == SIGUSR1) {
197                                 do_signal = 0;
198
199                                 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
200                                 if (do_signal == SIGUSR1)
201                                         continue;
202                                 else if (res < 0)
203                                         break;
204                                 else if (res > 0)
205                                         script_call("informed");
206
207                                 alarm(0);
208                                 if (dhcpv6_poll_reconfigure() > 0)
209                                         script_call("informed");
210                         }
211
212                         if (do_signal == SIGALRM)
213                                 script_call("timeout");
214
215                         continue;
216                 }
217
218                 // Stateful mode
219                 if (dhcpv6_request(DHCPV6_MSG_REQUEST) < 0)
220                         continue;
221
222                 script_call("bound");
223                 alarm(0);
224
225                 while (do_signal == 0 || do_signal == SIGUSR1) {
226                         // Renew Cycle
227                         // Wait for T1 to expire or until we get a reconfigure
228                         int res = dhcpv6_poll_reconfigure();
229                         if (res >= 0) {
230                                 if (res > 0)
231                                         script_call("updated");
232
233                                 continue;
234                         }
235
236                         // Handle signal, if necessary
237                         if (do_signal == SIGUSR1)
238                                 do_signal = 0; // Acknowledged
239                         else if (do_signal > 0)
240                                 break; // Other signal type
241
242                         size_t ia_pd_len, ia_na_len, ia_pd_new, ia_na_new;
243                         odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
244                         odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
245
246                         // If we have any IAs, send renew, otherwise request
247                         int r;
248                         if (ia_pd_len == 0 && ia_na_len == 0)
249                                 r = dhcpv6_request(DHCPV6_MSG_REQUEST);
250                         else
251                                 r = dhcpv6_request(DHCPV6_MSG_RENEW);
252                         if (r > 0) // Publish updates
253                                 script_call("updated");
254                         if (r >= 0)
255                                 continue; // Renew was successful
256
257                         odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
258
259                         // If we have IAs, try rebind otherwise restart
260                         res = dhcpv6_request(DHCPV6_MSG_REBIND);
261
262                         odhcp6c_get_state(STATE_IA_PD, &ia_pd_new);
263                         odhcp6c_get_state(STATE_IA_NA, &ia_na_new);
264                         if (res < 0 || (ia_pd_new == 0 && ia_pd_len) ||
265                                         (ia_na_new == 0 && ia_na_len))
266                                 break; // We lost all our IAs, restart
267                         else if (res > 0)
268                                 script_call("rebound");
269                 }
270
271
272                 size_t ia_pd_len, ia_na_len, server_id_len;
273                 uint8_t *ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
274                 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
275                 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
276
277                 // Add all prefixes to lost prefixes
278                 odhcp6c_add_state(STATE_IA_PD_LOST, ia_pd, ia_pd_len);
279                 odhcp6c_clear_state(STATE_IA_PD);
280
281                 if (do_signal == SIGALRM)
282                         script_call("timeout");
283                 else
284                         script_call("unbound");
285
286                 // Remove assigned addresses
287                 if (ia_na_len > 0)
288                         dhcpv6_remove_addrs();
289
290                 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0))
291                         dhcpv6_request(DHCPV6_MSG_RELEASE);
292         }
293
294         return 0;
295 }
296
297
298 static int usage(void)
299 {
300         const char buf[] =
301         "Usage: odhcp6c [options] <interface>\n"
302         "\nFeature options:\n"
303         "       -N <mode>       Mode for requesting addresses [try|force|none]\n"
304         "       -P <length>     Request IPv6-Prefix (0 = auto)\n"
305         "       -c <clientid>   Override client-ID (base-16 encoded)\n"
306         "       -r <options>    Options to be requested (comma-separated)\n"
307         "       -s <script>     Status update script (/usr/sbin/odhcp6c-update)\n"
308         "       -t <timeout>    Request timeout after which the script is called\n"
309         "\nInvocation options:\n"
310         "       -p <pidfile>    Set pidfile (/var/run/6relayd.pid)\n"
311         "       -d              Daemonize\n"
312         //"     -v              Increase logging verbosity\n"
313         "       -h              Show this help\n\n";
314         write(STDERR_FILENO, buf, sizeof(buf));
315         return 1;
316 }
317
318
319 // Don't want to pull-in librt and libpthread just for a monotonic clock...
320 uint64_t adhc6c_get_milli_time(void)
321 {
322         struct timespec t = {0, 0};
323         syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
324         return t.tv_sec * 1000 + t.tv_nsec / 1000000;
325 }
326
327
328 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
329 {
330         if (len == 0)
331                 return state_data[state] + state_len[state];
332
333         uint8_t *n = realloc(state_data[state], state_len[state] + len);
334         if (n || state_len[state] + len == 0) {
335                 state_data[state] = n;
336                 n += state_len[state];
337                 state_len[state] += len;
338         }
339         return n;
340 }
341
342
343 bool odhcp6c_signal_is_pending(void)
344 {
345         return do_signal != 0;
346 }
347
348
349 void odhcp6c_clear_state(enum odhcp6c_state state)
350 {
351         state_len[state] = 0;
352 }
353
354
355 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
356 {
357         uint8_t *n = odhcp6c_resize_state(state, len);
358         if (n)
359                 memcpy(n, data, len);
360 }
361
362
363 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
364 {
365         uint8_t *data = state_data[state];
366         ssize_t len_after = state_len[state] - (offset + len);
367         if (len_after < 0)
368                 return state_len[state];
369
370         memmove(data + offset, data + offset + len, len_after);
371         return state_len[state] -= len;
372 }
373
374
375 bool odhcp6c_commit_state(enum odhcp6c_state state, size_t old_len)
376 {
377         size_t new_len = state_len[state] - old_len;
378         uint8_t *old_data = state_data[state], *new_data = old_data + old_len;
379         bool upd = new_len != old_len || memcmp(old_data, new_data, new_len);
380
381         memmove(old_data, new_data, new_len);
382         odhcp6c_resize_state(state, -old_len);
383
384         return upd;
385 }
386
387
388 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
389 {
390         *len = state_len[state];
391         return state_data[state];
392 }
393
394
395 static int sysctl_interface(const char *ifname, const char *option,
396                 const char *data)
397 {
398         char pathbuf[64];
399         const char *sysctl_pattern = "/proc/sys/net/ipv6/conf/%s/%s";
400         snprintf(pathbuf, sizeof(pathbuf), sysctl_pattern, ifname, option);
401
402         int fd = open(pathbuf, O_WRONLY);
403         int written = write(fd, data, strlen(data));
404         close(fd);
405
406         return (written > 0) ? 0 : -1;
407 }
408
409
410 static void sighandler(int signal)
411 {
412         if (signal == SIGCHLD)
413                 while (waitpid(-1, NULL, WNOHANG) > 0);
414         else if (signal == SIGUSR1)
415                 do_signal = SIGUSR1;
416         else if (signal == SIGUSR2)
417                 do_signal = SIGUSR2;
418         else if (signal == SIGALRM)
419                 do_signal = SIGALRM;
420         else
421                 do_signal = SIGTERM;
422 }