]> git.decadent.org.uk Git - odhcp6c.git/blobdiff - src/odhcp6c.c
First hnet-based version.
[odhcp6c.git] / src / odhcp6c.c
index b517204829d20fc5e02f17f07e41dcb2457d95b1..64d363f46b9d21210d12c40fa72104a04848c5e0 100644 (file)
@@ -17,6 +17,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <unistd.h>
 #include <syslog.h>
 #include <signal.h>
 #include <sys/syscall.h>
 
 #include "odhcp6c.h"
+#include "ra.h"
 
 
 static void sighandler(int signal);
-static int sysctl_interface(const char *ifname, const char *option,
-               const char *data);
 static int usage(void);
 
 
@@ -40,12 +40,12 @@ static uint8_t *state_data[_STATE_MAX] = {NULL};
 static size_t state_len[_STATE_MAX] = {0};
 
 static volatile int do_signal = 0;
+static int urandom_fd = -1;
+static bool bound = false, allow_slaac_only = true, release = true;
 
 
 int main(_unused int argc, char* const argv[])
 {
-       openlog("odhcp6c", LOG_PERROR | LOG_PID, LOG_DAEMON);
-
        // Allocate ressources
        const char *pidfile = NULL;
        const char *script = "/usr/sbin/odhcp6c-update";
@@ -55,12 +55,13 @@ int main(_unused int argc, char* const argv[])
        uint16_t opttype;
        enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
 
-       bool help = false, daemonize = false, reset = false;
-       int c, request_pd = 0, timeout = 0;
-       while ((c = getopt(argc, argv, "RN:P:c:r:s:t:hdp:")) != -1) {
+       bool help = false, daemonize = false;
+       int logopt = LOG_PID;
+       int c, request_pd = 0;
+       while ((c = getopt(argc, argv, "SN:P:c:r:s:khedp:")) != -1) {
                switch (c) {
-               case 'R':
-                       reset = true;
+               case 'S':
+                       allow_slaac_only = false;
                        break;
 
                case 'N':
@@ -75,6 +76,7 @@ int main(_unused int argc, char* const argv[])
                        break;
 
                case 'P':
+                       allow_slaac_only = false;
                        request_pd = strtoul(optarg, NULL, 10);
                        if (request_pd == 0)
                                request_pd = -1;
@@ -86,7 +88,7 @@ int main(_unused int argc, char* const argv[])
                                buf[0] = 0;
                                buf[1] = DHCPV6_OPT_CLIENTID;
                                buf[2] = 0;
-                               buf[4] = l;
+                               buf[3] = l;
                                odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
                        } else {
                                help = true;
@@ -109,8 +111,12 @@ int main(_unused int argc, char* const argv[])
                        script = optarg;
                        break;
 
-               case 't':
-                       timeout = strtoul(optarg, NULL, 10);
+               case 'k':
+                       release = false;
+                       break;
+
+               case 'e':
+                       logopt |= LOG_PERROR;
                        break;
 
                case 'd':
@@ -127,32 +133,27 @@ int main(_unused int argc, char* const argv[])
                }
        }
 
+       openlog("odhcp6c", logopt, LOG_DAEMON);
        const char *ifname = argv[optind];
 
        if (help || !ifname)
                return usage();
 
-       if (init_dhcpv6(ifname, request_pd) || init_rtnetlink() ||
+       if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
+                       init_dhcpv6(ifname, request_pd) || ra_init(ifname) ||
                        script_init(script, ifname)) {
                syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
                return 3;
        }
 
+       signal(SIGIO, sighandler);
        signal(SIGHUP, sighandler);
        signal(SIGINT, sighandler);
-       signal(SIGALRM, sighandler);
        signal(SIGCHLD, sighandler);
        signal(SIGTERM, sighandler);
        signal(SIGUSR1, sighandler);
        signal(SIGUSR2, sighandler);
 
-       // Configure interface to accept RA
-       if (reset) {
-               sysctl_interface(ifname, "accept_ra", "2");
-               sysctl_interface(ifname, "disable_ipv6", "1");
-               sysctl_interface(ifname, "disable_ipv6", "0");
-       }
-
        if (daemonize) {
                openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
                if (daemon(0, 0)) {
@@ -177,16 +178,24 @@ int main(_unused int argc, char* const argv[])
                }
        }
 
+       script_call("started");
+
        while (do_signal != SIGTERM) { // Main logic
                odhcp6c_clear_state(STATE_SERVER_ID);
                odhcp6c_clear_state(STATE_SERVER_CAND);
                odhcp6c_clear_state(STATE_IA_PD);
-               odhcp6c_clear_state(STATE_IA_PD_LOST);
+               odhcp6c_clear_state(STATE_SNTP_IP);
+               odhcp6c_clear_state(STATE_SNTP_FQDN);
+               odhcp6c_clear_state(STATE_SIP_IP);
+               odhcp6c_clear_state(STATE_SIP_FQDN);
                dhcpv6_set_ia_na_mode(ia_na_mode);
+               bound = false;
+
+               syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
 
-               alarm(timeout);
                do_signal = 0;
                int res = dhcpv6_request(DHCPV6_MSG_SOLICIT);
+               odhcp6c_signal_process();
 
                if (res < 0) {
                        continue; // Might happen if we got a signal
@@ -195,6 +204,7 @@ int main(_unused int argc, char* const argv[])
                                do_signal = 0;
 
                                res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
+                               odhcp6c_signal_process();
                                if (do_signal == SIGUSR1)
                                        continue;
                                else if (res < 0)
@@ -202,14 +212,13 @@ int main(_unused int argc, char* const argv[])
                                else if (res > 0)
                                        script_call("informed");
 
-                               alarm(0);
+                               bound = true;
+                               syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
+
                                if (dhcpv6_poll_reconfigure() > 0)
                                        script_call("informed");
                        }
 
-                       if (do_signal == SIGALRM)
-                               script_call("timeout");
-
                        continue;
                }
 
@@ -217,13 +226,16 @@ int main(_unused int argc, char* const argv[])
                if (dhcpv6_request(DHCPV6_MSG_REQUEST) < 0)
                        continue;
 
+               odhcp6c_signal_process();
                script_call("bound");
-               alarm(0);
+               bound = true;
+               syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
 
                while (do_signal == 0 || do_signal == SIGUSR1) {
                        // Renew Cycle
                        // Wait for T1 to expire or until we get a reconfigure
                        int res = dhcpv6_poll_reconfigure();
+                       odhcp6c_signal_process();
                        if (res >= 0) {
                                if (res > 0)
                                        script_call("updated");
@@ -247,6 +259,7 @@ int main(_unused int argc, char* const argv[])
                                r = dhcpv6_request(DHCPV6_MSG_REQUEST);
                        else
                                r = dhcpv6_request(DHCPV6_MSG_RENEW);
+                       odhcp6c_signal_process();
                        if (r > 0) // Publish updates
                                script_call("updated");
                        if (r >= 0)
@@ -256,6 +269,7 @@ int main(_unused int argc, char* const argv[])
 
                        // If we have IAs, try rebind otherwise restart
                        res = dhcpv6_request(DHCPV6_MSG_REBIND);
+                       odhcp6c_signal_process();
 
                        odhcp6c_get_state(STATE_IA_PD, &ia_pd_new);
                        odhcp6c_get_state(STATE_IA_NA, &ia_na_new);
@@ -268,27 +282,22 @@ int main(_unused int argc, char* const argv[])
 
 
                size_t ia_pd_len, ia_na_len, server_id_len;
-               uint8_t *ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
+               odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
                odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
                odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
 
                // Add all prefixes to lost prefixes
-               odhcp6c_add_state(STATE_IA_PD_LOST, ia_pd, ia_pd_len);
-               odhcp6c_clear_state(STATE_IA_PD);
-
-               if (do_signal == SIGALRM)
-                       script_call("timeout");
-               else
-                       script_call("unbound");
+               bound = false;
+               script_call("unbound");
 
-               // Remove assigned addresses
-               if (ia_na_len > 0)
-                       dhcpv6_remove_addrs();
-
-               if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0))
+               if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
                        dhcpv6_request(DHCPV6_MSG_RELEASE);
+
+               odhcp6c_clear_state(STATE_IA_NA);
+               odhcp6c_clear_state(STATE_IA_PD);
        }
 
+       script_call("stopped");
        return 0;
 }
 
@@ -298,15 +307,17 @@ static int usage(void)
        const char buf[] =
        "Usage: odhcp6c [options] <interface>\n"
        "\nFeature options:\n"
+       "       -S              Don't allow SLAAC-only (implied by -P)\n"
        "       -N <mode>       Mode for requesting addresses [try|force|none]\n"
        "       -P <length>     Request IPv6-Prefix (0 = auto)\n"
        "       -c <clientid>   Override client-ID (base-16 encoded)\n"
        "       -r <options>    Options to be requested (comma-separated)\n"
        "       -s <script>     Status update script (/usr/sbin/odhcp6c-update)\n"
-       "       -t <timeout>    Request timeout after which the script is called\n"
+       "       -k              Don't send a RELEASE when stopping\n"
        "\nInvocation options:\n"
        "       -p <pidfile>    Set pidfile (/var/run/6relayd.pid)\n"
        "       -d              Daemonize\n"
+       "       -e              Write logmessages to stderr\n"
        //"     -v              Increase logging verbosity\n"
        "       -h              Show this help\n\n";
        write(STDERR_FILENO, buf, sizeof(buf));
@@ -315,7 +326,7 @@ static int usage(void)
 
 
 // Don't want to pull-in librt and libpthread just for a monotonic clock...
-uint64_t adhc6c_get_milli_time(void)
+uint64_t odhcp6c_get_milli_time(void)
 {
        struct timespec t = {0, 0};
        syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
@@ -327,6 +338,8 @@ static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
 {
        if (len == 0)
                return state_data[state] + state_len[state];
+       else if (state_len[state] + len > 1024)
+               return NULL;
 
        uint8_t *n = realloc(state_data[state], state_len[state] + len);
        if (n || state_len[state] + len == 0) {
@@ -338,8 +351,18 @@ static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
 }
 
 
-bool odhcp6c_signal_is_pending(void)
+bool odhcp6c_signal_process(void)
 {
+       if (do_signal == SIGIO) {
+               do_signal = 0;
+               bool updated = ra_process();
+               updated |= ra_rtnl_process();
+               if (updated && (bound || allow_slaac_only)) {
+                       odhcp6c_expire();
+                       script_call("ra-updated");
+               }
+       }
+
        return do_signal != 0;
 }
 
@@ -370,38 +393,96 @@ size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
 }
 
 
-bool odhcp6c_commit_state(enum odhcp6c_state state, size_t old_len)
+void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
 {
-       size_t new_len = state_len[state] - old_len;
-       uint8_t *old_data = state_data[state], *new_data = old_data + old_len;
-       bool upd = new_len != old_len || memcmp(old_data, new_data, new_len);
+       *len = state_len[state];
+       return state_data[state];
+}
 
-       memmove(old_data, new_data, new_len);
-       odhcp6c_resize_state(state, -old_len);
 
-       return upd;
+struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
+{
+       size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + new->length / 8;
+       struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
+       struct odhcp6c_entry *x = NULL;
+
+       for (struct odhcp6c_entry *c = start; !x && c < &start[len/sizeof(*c)]; ++c)
+               if (!memcmp(c, new, cmplen))
+                       return c;
+
+       return NULL;
 }
 
 
-void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
+void odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
 {
-       *len = state_len[state];
-       return state_data[state];
+       size_t len;
+       struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
+       struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
+
+       if (x && x->valid > new->valid && new->valid < safe)
+               new->valid = safe;
+
+       if (new->valid > 0) {
+               if (x) {
+                       x->valid = new->valid;
+                       x->preferred = new->preferred;
+                        x->prefix_class = new->prefix_class;
+               } else {
+                       odhcp6c_add_state(state, new, sizeof(*new));
+               }
+       } else if (x) {
+               odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
+       }
 }
 
 
-static int sysctl_interface(const char *ifname, const char *option,
-               const char *data)
+void odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
 {
-       char pathbuf[64];
-       const char *sysctl_pattern = "/proc/sys/net/ipv6/conf/%s/%s";
-       snprintf(pathbuf, sizeof(pathbuf), sysctl_pattern, ifname, option);
+       odhcp6c_update_entry_safe(state, new, 0);
+}
 
-       int fd = open(pathbuf, O_WRONLY);
-       int written = write(fd, data, strlen(data));
-       close(fd);
 
-       return (written > 0) ? 0 : -1;
+static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
+{
+       size_t len;
+       struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
+       for (struct odhcp6c_entry *c = start; c < &start[len / sizeof(*c)]; ++c) {
+               if (c->preferred < elapsed)
+                       c->preferred = 0;
+               else if (c->preferred != UINT32_MAX)
+                       c->preferred -= elapsed;
+
+               if (c->valid < elapsed)
+                       c->valid = 0;
+               else if (c->valid != UINT32_MAX)
+                       c->valid -= elapsed;
+
+               if (!c->valid)
+                       odhcp6c_remove_state(state, (c - start) * sizeof(*c), sizeof(*c));
+       }
+}
+
+
+void odhcp6c_expire(void)
+{
+       static time_t last_update = 0;
+       time_t now = odhcp6c_get_milli_time() / 1000;
+
+       uint32_t elapsed = now - last_update;
+       last_update = now;
+
+       odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
+       odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
+       odhcp6c_expire_list(STATE_RA_DNS, elapsed);
+       odhcp6c_expire_list(STATE_IA_NA, elapsed);
+       odhcp6c_expire_list(STATE_IA_PD, elapsed);
+}
+
+
+void odhcp6c_random(void *buf, size_t len)
+{
+       read(urandom_fd, buf, len);
 }
 
 
@@ -413,8 +494,8 @@ static void sighandler(int signal)
                do_signal = SIGUSR1;
        else if (signal == SIGUSR2)
                do_signal = SIGUSR2;
-       else if (signal == SIGALRM)
-               do_signal = SIGALRM;
+       else if (signal == SIGIO)
+               do_signal = SIGIO;
        else
                do_signal = SIGTERM;
 }