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