X-Git-Url: http://git.openwrt.org/?p=project%2Fodhcp6c.git;a=blobdiff_plain;f=src%2Fodhcp6c.c;h=2fe41e5160158ed8501863e23a130a640b285034;hp=de452c04a7fef0de9488aef00fdafcac1119e779;hb=419fb6347233506012eecc59f8ae6a531a377a8d;hpb=63358b8f56aa4eb9976c09bf34e9e04f37d6a2a8 diff --git a/src/odhcp6c.c b/src/odhcp6c.c index de452c0..2fe41e5 100644 --- a/src/odhcp6c.c +++ b/src/odhcp6c.c @@ -1,5 +1,5 @@ /** - * Copyright (C) 2012-2013 Steven Barth + * Copyright (C) 2012-2014 Steven Barth * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License v2 as published by @@ -27,7 +27,6 @@ #include #include -#include #include #include #include @@ -35,9 +34,6 @@ #include "odhcp6c.h" #include "ra.h" -#ifdef EXT_BFD_PING -#include "bfd.h" -#endif #ifndef IN6_IS_ADDR_UNIQUELOCAL #define IN6_IS_ADDR_UNIQUELOCAL(a) \ @@ -51,12 +47,20 @@ static int usage(void); static uint8_t *state_data[_STATE_MAX] = {NULL}; static size_t state_len[_STATE_MAX] = {0}; -static volatile int do_signal = 0; +static volatile bool signal_io = false; +static volatile bool signal_usr1 = false; +static volatile bool signal_usr2 = false; +static volatile bool signal_term = false; + static int urandom_fd = -1, allow_slaac_only = 0; -static bool bound = false, release = true; +static bool bound = false, release = true, ra = false; static time_t last_update = 0; static char *ifname = NULL; +static unsigned int min_update_interval = DEFAULT_MIN_UPDATE_INTERVAL; +static unsigned int script_sync_delay = 10; +static unsigned int script_accu_delay = 1; + int main(_unused int argc, char* const argv[]) { // Allocate ressources @@ -66,19 +70,22 @@ int main(_unused int argc, char* const argv[]) uint8_t buf[134]; char *optpos; uint16_t opttype; + uint16_t optlen; enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY; - enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_TRY; + enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_NONE; + int ia_pd_iaid_index = 0; static struct in6_addr ifid = IN6ADDR_ANY_INIT; int sol_timeout = DHCPV6_SOL_MAX_RT; + int verbosity = 0; -#ifdef EXT_BFD_PING - int bfd_interval = 0, bfd_loss = 3; -#endif bool help = false, daemonize = false; int logopt = LOG_PID; - int c, request_pd = 0; - while ((c = getopt(argc, argv, "S::N:P:FB:c:i:r:s:kt:hedp:")) != -1) { + int c; + unsigned int client_options = DHCPV6_CLIENT_FQDN | DHCPV6_ACCEPT_RECONFIGURE; + unsigned int ra_options = RA_RDNSS_DEFAULT_LIFETIME; + + while ((c = getopt(argc, argv, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:Lhedp:fav")) != -1) { switch (c) { case 'S': allow_slaac_only = (optarg) ? atoi(optarg) : -1; @@ -97,13 +104,41 @@ int main(_unused int argc, char* const argv[]) } break; + case 'V': + l = script_unhexlify(buf, sizeof(buf), optarg); + if (!l) + help=true; + + odhcp6c_add_state(STATE_VENDORCLASS, buf, l); + + break; case 'P': + if (ia_pd_mode == IA_MODE_NONE) + ia_pd_mode = IA_MODE_TRY; + if (allow_slaac_only >= 0 && allow_slaac_only < 10) allow_slaac_only = 10; - request_pd = strtoul(optarg, NULL, 10); - if (request_pd == 0) - request_pd = -1; + char *iaid_begin; + int iaid_len = 0; + + int prefix_length = strtoul(optarg, &iaid_begin, 10); + + if (*iaid_begin != '\0' && *iaid_begin != ',' && *iaid_begin != ':') { + syslog(LOG_ERR, "invalid argument: '%s'", optarg); + return 1; + } + + struct odhcp6c_request_prefix prefix = { 0, prefix_length }; + + if (*iaid_begin == ',' && (iaid_len = strlen(iaid_begin)) > 1) + memcpy(&prefix.iaid, iaid_begin + 1, iaid_len > 4 ? 4 : iaid_len); + else if (*iaid_begin == ':') + prefix.iaid = htonl((uint32_t)strtoul(&iaid_begin[1], NULL, 16)); + else + prefix.iaid = htonl(++ia_pd_iaid_index); + + odhcp6c_add_state(STATE_IA_PD_INIT, &prefix, sizeof(prefix)); break; @@ -112,12 +147,6 @@ int main(_unused int argc, char* const argv[]) ia_pd_mode = IA_MODE_FORCE; break; -#ifdef EXT_BFD_PING - case 'B': - bfd_interval = atoi(optarg); - break; -#endif - case 'c': l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg); if (l > 0) { @@ -148,6 +177,16 @@ int main(_unused int argc, char* const argv[]) } break; + case 'R': + client_options |= DHCPV6_STRICT_OPTIONS; + break; + + case 'u': + optlen = htons(strlen(optarg)); + odhcp6c_add_state(STATE_USERCLASS, &optlen, 2); + odhcp6c_add_state(STATE_USERCLASS, optarg, strlen(optarg)); + break; + case 's': script = optarg; break; @@ -160,6 +199,14 @@ int main(_unused int argc, char* const argv[]) sol_timeout = atoi(optarg); break; + case 'm': + min_update_interval = atoi(optarg); + break; + + case 'L': + ra_options &= ~RA_RDNSS_DEFAULT_LIFETIME; + break; + case 'e': logopt |= LOG_PERROR; break; @@ -172,13 +219,31 @@ int main(_unused int argc, char* const argv[]) pidfile = optarg; break; + case 'f': + client_options &= ~DHCPV6_CLIENT_FQDN; + break; + + case 'a': + client_options &= ~DHCPV6_ACCEPT_RECONFIGURE; + break; + + case 'v': + ++verbosity; + break; + default: help = true; break; } } + if (allow_slaac_only > 0) + script_sync_delay = allow_slaac_only; + openlog("odhcp6c", logopt, LOG_DAEMON); + if (!verbosity) + setlogmask(LOG_UPTO(LOG_WARNING)); + ifname = argv[optind]; if (help || !ifname) @@ -187,14 +252,14 @@ int main(_unused int argc, char* const argv[]) signal(SIGIO, sighandler); signal(SIGHUP, sighandler); signal(SIGINT, sighandler); - signal(SIGCHLD, sighandler); signal(SIGTERM, sighandler); signal(SIGUSR1, sighandler); signal(SIGUSR2, sighandler); if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 || - init_dhcpv6(ifname, request_pd, sol_timeout) || - ra_init(ifname, &ifid) || script_init(script, ifname)) { + init_dhcpv6(ifname, client_options, sol_timeout) || + ra_init(ifname, &ifid, ra_options) || + script_init(script, ifname)) { syslog(LOG_ERR, "failed to initialize: %s", strerror(errno)); return 3; } @@ -207,39 +272,38 @@ int main(_unused int argc, char* const argv[]) return 4; } - char pidbuf[128]; if (!pidfile) { - snprintf(pidbuf, sizeof(pidbuf), - "/var/run/odhcp6c.%s.pid", ifname); - pidfile = pidbuf; + snprintf((char*)buf, sizeof(buf), "/var/run/odhcp6c.%s.pid", ifname); + pidfile = (char*)buf; } - int fd = open(pidfile, O_WRONLY | O_CREAT); - if (fd >= 0) { - char buf[8]; - int len = snprintf(buf, sizeof(buf), "%i\n", getpid()); - write(fd, buf, len); - close(fd); + FILE *fp = fopen(pidfile, "w"); + if (fp) { + fprintf(fp, "%i\n", getpid()); + fclose(fp); } } - script_call("started"); + script_call("started", 0, false); - while (do_signal != SIGTERM) { // Main logic + while (!signal_term) { // Main logic odhcp6c_clear_state(STATE_SERVER_ID); + odhcp6c_clear_state(STATE_SERVER_ADDR); odhcp6c_clear_state(STATE_IA_NA); odhcp6c_clear_state(STATE_IA_PD); odhcp6c_clear_state(STATE_SNTP_IP); - odhcp6c_clear_state(STATE_SNTP_FQDN); + odhcp6c_clear_state(STATE_NTP_IP); + odhcp6c_clear_state(STATE_NTP_FQDN); odhcp6c_clear_state(STATE_SIP_IP); odhcp6c_clear_state(STATE_SIP_FQDN); - dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode); bound = false; syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname); - do_signal = 0; - int mode = dhcpv6_request(DHCPV6_MSG_SOLICIT); + signal_usr1 = signal_usr2 = false; + int mode = dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode); + if (mode != DHCPV6_STATELESS) + mode = dhcpv6_request(DHCPV6_MSG_SOLICIT); odhcp6c_signal_process(); if (mode < 0) @@ -248,11 +312,11 @@ int main(_unused int argc, char* const argv[]) do { int res = dhcpv6_request(mode == DHCPV6_STATELESS ? DHCPV6_MSG_INFO_REQ : DHCPV6_MSG_REQUEST); + bool signalled = odhcp6c_signal_process(); - odhcp6c_signal_process(); if (res > 0) break; - else if (do_signal > 0) { + else if (signalled) { mode = -1; break; } @@ -268,9 +332,9 @@ int main(_unused int argc, char* const argv[]) bound = true; syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname); - while (do_signal == 0 || do_signal == SIGUSR1) { - do_signal = 0; - script_call("informed"); + while (!signal_usr2 && !signal_term) { + signal_usr1 = false; + script_call("informed", script_sync_delay, true); int res = dhcpv6_poll_reconfigure(); odhcp6c_signal_process(); @@ -278,15 +342,16 @@ int main(_unused int argc, char* const argv[]) if (res > 0) continue; - if (do_signal == SIGUSR1) { - do_signal = 0; // Acknowledged + if (signal_usr1) { + signal_usr1 = false; // Acknowledged continue; - } else if (do_signal > 0) + } + if (signal_usr2 || signal_term) break; res = dhcpv6_request(DHCPV6_MSG_INFO_REQ); odhcp6c_signal_process(); - if (do_signal == SIGUSR1) + if (signal_usr1) continue; else if (res < 0) break; @@ -294,28 +359,24 @@ int main(_unused int argc, char* const argv[]) break; case DHCPV6_STATEFUL: - script_call("bound"); bound = true; + script_call("bound", script_sync_delay, true); syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname); -#ifdef EXT_BFD_PING - if (bfd_interval > 0) - bfd_start(ifname, bfd_loss, bfd_interval); -#endif - while (do_signal == 0 || do_signal == SIGUSR1) { + while (!signal_usr2 && !signal_term) { // Renew Cycle // Wait for T1 to expire or until we get a reconfigure int res = dhcpv6_poll_reconfigure(); odhcp6c_signal_process(); if (res > 0) { - script_call("updated"); + script_call("updated", 0, false); continue; } // Handle signal, if necessary - if (do_signal == SIGUSR1) - do_signal = 0; // Acknowledged - else if (do_signal > 0) + if (signal_usr1) + signal_usr1 = false; // Acknowledged + if (signal_usr2 || signal_term) break; // Other signal type // Send renew as T1 expired @@ -323,22 +384,27 @@ int main(_unused int argc, char* const argv[]) odhcp6c_signal_process(); if (res > 0) { // Renew was succesfull // Publish updates - script_call("updated"); + script_call("updated", 0, false); continue; // Renew was successful } - + odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding + odhcp6c_clear_state(STATE_SERVER_ADDR); + + size_t ia_pd_len, ia_na_len; + odhcp6c_get_state(STATE_IA_PD, &ia_pd_len); + odhcp6c_get_state(STATE_IA_NA, &ia_na_len); + + if (ia_pd_len == 0 && ia_na_len == 0) + break; // If we have IAs, try rebind otherwise restart res = dhcpv6_request(DHCPV6_MSG_REBIND); odhcp6c_signal_process(); if (res > 0) - script_call("rebound"); + script_call("rebound", 0, true); else { -#ifdef EXT_BFD_PING - bfd_stop(); -#endif break; } } @@ -348,6 +414,8 @@ int main(_unused int argc, char* const argv[]) break; } + odhcp6c_expire(); + size_t ia_pd_len, ia_na_len, server_id_len; odhcp6c_get_state(STATE_IA_PD, &ia_pd_len); odhcp6c_get_state(STATE_IA_NA, &ia_na_len); @@ -355,7 +423,7 @@ int main(_unused int argc, char* const argv[]) // Add all prefixes to lost prefixes bound = false; - script_call("unbound"); + script_call("unbound", 0, true); if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release) dhcpv6_request(DHCPV6_MSG_RELEASE); @@ -364,7 +432,7 @@ int main(_unused int argc, char* const argv[]) odhcp6c_clear_state(STATE_IA_PD); } - script_call("stopped"); + script_call("stopped", 0, true); return 0; } @@ -378,22 +446,26 @@ static int usage(void) " -N Mode for requesting addresses [try|force|none]\n" " -P Request IPv6-Prefix (0 = auto)\n" " -F Force IPv6-Prefix\n" -#ifdef EXT_BFD_PING - " -B Enable BFD ping check\n" -#endif - " -c Override client-ID (base-16 encoded)\n" + " -V Set vendor-class option (base-16 encoded)\n" + " -u Set user-class option string\n" + " -c Override client-ID (base-16 encoded 16-bit type + value)\n" " -i Use a custom interface identifier for RA handling\n" " -r Options to be requested (comma-separated)\n" + " -R Do not request any options except those specified with -r\n" " -s