2 * Copyright (C) 2012 Steven Barth <steven@midlink.org>
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.
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.
28 #include <sys/syscall.h>
33 static void sighandler(int signal
);
34 static int sysctl_interface(const char *ifname
, const char *option
,
36 static int usage(void);
39 static uint8_t *state_data
[_STATE_MAX
] = {NULL
};
40 static size_t state_len
[_STATE_MAX
] = {0};
42 static volatile int do_signal
= 0;
45 int main(_unused
int argc
, char* const argv
[])
47 openlog("odhcp6c", LOG_PERROR
| LOG_PID
, LOG_DAEMON
);
49 // Allocate ressources
50 const char *pidfile
= NULL
;
51 const char *script
= "/usr/sbin/odhcp6c-update";
56 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
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) {
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
;
78 request_pd
= strtoul(optarg
, NULL
, 10);
84 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
87 buf
[1] = DHCPV6_OPT_CLIENTID
;
90 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
99 opttype
= htons(strtoul(optarg
, &optpos
, 10));
100 if (optpos
== optarg
)
104 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
113 timeout
= strtoul(optarg
, NULL
, 10);
130 const char *ifname
= argv
[optind
];
135 if (init_dhcpv6(ifname
, request_pd
) || init_rtnetlink() ||
136 script_init(script
, ifname
)) {
137 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
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
);
149 // Configure interface to accept RA
151 sysctl_interface(ifname
, "disable_ipv6", "1");
152 sysctl_interface(ifname
, "accept_ra", "2");
153 sysctl_interface(ifname
, "disable_ipv6", "0");
157 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
159 syslog(LOG_ERR
, "Failed to daemonize: %s",
166 snprintf(pidbuf
, sizeof(pidbuf
),
167 "/var/run/odhcp6c.%s.pid", ifname
);
171 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
);
174 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
180 while (do_signal
!= SIGTERM
) { // Main logic
181 odhcp6c_clear_state(STATE_SERVER_ID
);
182 odhcp6c_clear_state(STATE_SERVER_CAND
);
183 odhcp6c_clear_state(STATE_IA_PD
);
184 odhcp6c_clear_state(STATE_IA_PD_LOST
);
185 dhcpv6_set_ia_na_mode(ia_na_mode
);
189 int res
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
192 continue; // Might happen if we got a signal
193 } else if (res
== DHCPV6_STATELESS
) { // Stateless mode
194 while (do_signal
== 0 || do_signal
== SIGUSR1
) {
197 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
198 if (do_signal
== SIGUSR1
)
203 script_call("informed");
206 if (dhcpv6_poll_reconfigure() > 0)
207 script_call("informed");
210 if (do_signal
== SIGALRM
)
211 script_call("timeout");
217 if (dhcpv6_request(DHCPV6_MSG_REQUEST
) < 0)
220 script_call("bound");
223 while (do_signal
== 0 || do_signal
== SIGUSR1
) {
225 // Wait for T1 to expire or until we get a reconfigure
226 int res
= dhcpv6_poll_reconfigure();
229 script_call("updated");
234 // Handle signal, if necessary
235 if (do_signal
== SIGUSR1
)
236 do_signal
= 0; // Acknowledged
237 else if (do_signal
> 0)
238 break; // Other signal type
240 size_t ia_pd_len
, ia_na_len
, ia_pd_new
, ia_na_new
;
241 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
242 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
244 // If we have any IAs, send renew, otherwise request
246 if (ia_pd_len
== 0 && ia_na_len
== 0)
247 r
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
249 r
= dhcpv6_request(DHCPV6_MSG_RENEW
);
250 if (r
> 0) // Publish updates
251 script_call("updated");
253 continue; // Renew was successful
255 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
257 // If we have IAs, try rebind otherwise restart
258 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
260 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_new
);
261 odhcp6c_get_state(STATE_IA_NA
, &ia_na_new
);
262 if (res
< 0 || (ia_pd_new
== 0 && ia_pd_len
) ||
263 (ia_na_new
== 0 && ia_na_len
))
264 break; // We lost all our IAs, restart
266 script_call("rebound");
270 size_t ia_pd_len
, ia_na_len
, server_id_len
;
271 uint8_t *ia_pd
= odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
272 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
273 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
275 // Add all prefixes to lost prefixes
276 odhcp6c_add_state(STATE_IA_PD_LOST
, ia_pd
, ia_pd_len
);
277 odhcp6c_clear_state(STATE_IA_PD
);
279 if (do_signal
== SIGALRM
)
280 script_call("timeout");
282 script_call("unbound");
284 // Remove assigned addresses
286 dhcpv6_remove_addrs();
288 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0))
289 dhcpv6_request(DHCPV6_MSG_RELEASE
);
296 static int usage(void)
299 "Usage: odhcp6c [options] <interface>\n"
300 "\nFeature options:\n"
301 " -N <mode> Mode for requesting addresses [try|force|none]\n"
302 " -P <length> Request IPv6-Prefix (0 = auto)\n"
303 " -c <clientid> Override client-ID (base-16 encoded)\n"
304 " -r <options> Options to be requested (comma-separated)\n"
305 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
306 " -t <timeout> Request timeout after which the script is called\n"
307 "\nInvocation options:\n"
308 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
310 //" -v Increase logging verbosity\n"
311 " -h Show this help\n\n";
312 write(STDERR_FILENO
, buf
, sizeof(buf
));
317 // Don't want to pull-in librt and libpthread just for a monotonic clock...
318 uint64_t adhc6c_get_milli_time(void)
320 struct timespec t
= {0, 0};
321 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
322 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
326 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
329 return state_data
[state
] + state_len
[state
];
331 syslog(LOG_WARNING
, "state_reisze: %i %i %i", (int)state
, (int)state_len
[state
], (int)len
);
332 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
333 if (n
|| state_len
[state
] + len
== 0) {
334 state_data
[state
] = n
;
335 n
+= state_len
[state
];
336 state_len
[state
] += len
;
342 bool odhcp6c_signal_is_pending(void)
344 return do_signal
!= 0;
348 void odhcp6c_clear_state(enum odhcp6c_state state
)
350 state_len
[state
] = 0;
354 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
356 uint8_t *n
= odhcp6c_resize_state(state
, len
);
358 memcpy(n
, data
, len
);
362 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
364 uint8_t *data
= state_data
[state
];
365 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
367 return state_len
[state
];
369 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
370 return state_len
[state
] -= len
;
374 bool odhcp6c_commit_state(enum odhcp6c_state state
, size_t old_len
)
376 size_t new_len
= state_len
[state
] - old_len
;
377 uint8_t *old_data
= state_data
[state
], *new_data
= old_data
+ old_len
;
378 bool upd
= new_len
!= old_len
|| memcmp(old_data
, new_data
, new_len
);
380 memmove(old_data
, new_data
, new_len
);
381 odhcp6c_resize_state(state
, -old_len
);
387 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
389 *len
= state_len
[state
];
390 return state_data
[state
];
394 static int sysctl_interface(const char *ifname
, const char *option
,
398 const char *sysctl_pattern
= "/proc/sys/net/ipv6/conf/%s/%s";
399 snprintf(pathbuf
, sizeof(pathbuf
), sysctl_pattern
, ifname
, option
);
401 int fd
= open(pathbuf
, O_WRONLY
);
402 int written
= write(fd
, data
, strlen(data
));
405 return (written
> 0) ? 0 : -1;
409 static void sighandler(int signal
)
411 if (signal
== SIGCHLD
)
412 while (waitpid(-1, NULL
, WNOHANG
) > 0);
413 else if (signal
== SIGUSR1
)
415 else if (signal
== SIGUSR2
)
417 else if (signal
== SIGALRM
)