From: Hans Dedecker Date: Fri, 27 Jan 2017 09:39:52 +0000 (+0100) Subject: dhcpv6: server unicast option support X-Git-Url: http://git.openwrt.org/?p=project%2Fodhcp6c.git;a=commitdiff_plain;h=419fb6347233506012eecc59f8ae6a531a377a8d;hp=d420f49396c627ce1072b83170889baf0720bc8b dhcpv6: server unicast option support Add server unicast option support according rfc3315 Signed-off-by: Hans Dedecker --- diff --git a/src/dhcpv6.c b/src/dhcpv6.c index 2a9f8a2..6389052 100644 --- a/src/dhcpv6.c +++ b/src/dhcpv6.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -106,6 +107,8 @@ static int64_t t1 = 0, t2 = 0, t3 = 0; static int request_prefix = -1; static enum odhcp6c_ia_mode na_mode = IA_MODE_NONE, pd_mode = IA_MODE_NONE; static bool accept_reconfig = false; +// Server unicast address +static struct in6_addr server_addr = IN6ADDR_ANY_INIT; // Reconfigure key static uint8_t reconf_key[16]; @@ -180,6 +183,7 @@ int init_dhcpv6(const char *ifname, unsigned int options, int sol_timeout) htons(DHCPV6_OPT_SIP_SERVER_A), htons(DHCPV6_OPT_DNS_SERVERS), htons(DHCPV6_OPT_DNS_DOMAIN), + htons(DHCPV6_OPT_UNICAST), htons(DHCPV6_OPT_SNTP_SERVERS), htons(DHCPV6_OPT_NTP_SERVER), htons(DHCPV6_OPT_AFTR_NAME), @@ -501,7 +505,29 @@ static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs) struct msghdr msg = {.msg_name = &srv, .msg_namelen = sizeof(srv), .msg_iov = iov, .msg_iovlen = cnt}; - sendmsg(sock, &msg, 0); + switch (type) { + case DHCPV6_MSG_REQUEST: + case DHCPV6_MSG_RENEW: + case DHCPV6_MSG_RELEASE: + case DHCPV6_MSG_DECLINE: + if (!IN6_IS_ADDR_UNSPECIFIED(&server_addr) && + odhcp6c_addr_in_scope(&server_addr)) { + srv.sin6_addr = server_addr; + if (!IN6_IS_ADDR_LINKLOCAL(&server_addr)) + srv.sin6_scope_id = 0; + } + break; + default: + break; + } + + if (sendmsg(sock, &msg, 0) < 0) { + char in6_str[INET6_ADDRSTRLEN]; + + syslog(LOG_ERR, "Failed to send DHCPV6 message to %s (%s)", + inet_ntop(AF_INET6, (const void *)&srv.sin6_addr, + in6_str, sizeof(in6_str)), strerror(errno)); + } } @@ -799,7 +825,7 @@ static int dhcpv6_handle_advert(enum dhcpv6_msg orig, const int rc, uint16_t olen, otype; uint8_t *odata, pref = 0; struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}, - DHCPV6_SOL_MAX_RT, + IN6ADDR_ANY_INIT, DHCPV6_SOL_MAX_RT, DHCPV6_INF_MAX_RT, NULL, NULL, 0, 0}; bool have_na = false; int have_pd = 0; @@ -818,6 +844,8 @@ static int dhcpv6_handle_advert(enum dhcpv6_msg orig, const int rc, } else if (otype == DHCPV6_OPT_PREF && olen >= 1 && cand.preference >= 0) { cand.preference = pref = odata[0]; + } else if (otype == DHCPV6_OPT_UNICAST && olen == sizeof(cand.server_addr)) { + cand.server_addr = *(struct in6_addr *)odata; } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) { cand.wants_reconfigure = true; } else if (otype == DHCPV6_OPT_SOL_MAX_RT && olen == 4) { @@ -1008,6 +1036,9 @@ static int dhcpv6_handle_reply(enum dhcpv6_msg orig, _unused const int rc, dhcpv6_parse_ia(ia_hdr, odata + olen); passthru = false; + } else if (otype == DHCPV6_OPT_UNICAST && olen == sizeof(server_addr)) { + server_addr = *(struct in6_addr *)odata; + passthru = false; } else if (otype == DHCPV6_OPT_STATUS && olen >= 2) { uint8_t *mdata = (olen > 2) ? &odata[2] : NULL; uint16_t mlen = (olen > 2) ? olen - 2 : 0; @@ -1015,8 +1046,7 @@ static int dhcpv6_handle_reply(enum dhcpv6_msg orig, _unused const int rc, dhcpv6_handle_status_code(orig, code, mdata, mlen, &ret); passthru = false; - } - else if (otype == DHCPV6_OPT_DNS_SERVERS) { + } else if (otype == DHCPV6_OPT_DNS_SERVERS) { if (olen % 16 == 0) odhcp6c_add_state(STATE_DNS, odata, olen); } else if (otype == DHCPV6_OPT_DNS_DOMAIN) { @@ -1345,7 +1375,18 @@ static void dhcpv6_handle_status_code(const enum dhcpv6_msg orig, break; case DHCPV6_UseMulticast: - // TODO handle multicast status code + switch(orig) { + case DHCPV6_MSG_REQUEST: + case DHCPV6_MSG_RENEW: + case DHCPV6_MSG_RELEASE: + case DHCPV6_MSG_DECLINE: + // Message needs to be retransmitted according to RFC3315 chapter 18.1.8 + server_addr = in6addr_any; + *ret = 0; + break; + default: + break; + } break; case DHCPV6_NoAddrsAvail: diff --git a/src/odhcp6c.c b/src/odhcp6c.c index e97ad26..2fe41e5 100644 --- a/src/odhcp6c.c +++ b/src/odhcp6c.c @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -22,16 +23,23 @@ #include #include #include +#include #include #include #include #include +#include #include "odhcp6c.h" #include "ra.h" +#ifndef IN6_IS_ADDR_UNIQUELOCAL +#define IN6_IS_ADDR_UNIQUELOCAL(a) \ + ((((__const uint32_t *) (a))[0] & htonl (0xfe000000)) \ + == htonl (0xfc000000)) +#endif static void sighandler(int signal); static int usage(void); @@ -47,6 +55,7 @@ static volatile bool signal_term = false; static int urandom_fd = -1, allow_slaac_only = 0; 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; @@ -235,7 +244,7 @@ int main(_unused int argc, char* const argv[]) if (!verbosity) setlogmask(LOG_UPTO(LOG_WARNING)); - const char *ifname = argv[optind]; + ifname = argv[optind]; if (help || !ifname) return usage(); @@ -690,6 +699,57 @@ bool odhcp6c_is_bound(void) return bound; } +bool odhcp6c_addr_in_scope(const struct in6_addr *addr) +{ + FILE *fd = fopen("/proc/net/if_inet6", "r"); + int len; + char buf[256]; + + if (fd == NULL) + return false; + + while (fgets(buf, sizeof(buf), fd)) { + struct in6_addr inet6_addr; + uint32_t flags, dummy; + unsigned int i; + char name[8], addr_buf[32]; + + len = strlen(buf); + + if ((len <= 0) || buf[len - 1] != '\n') + return false; + + buf[--len] = '\0'; + + if (sscanf(buf, "%s %x %x %x %x %s", + addr_buf, &dummy, &dummy, &dummy, &flags, name) != 6) + return false; + + if (strcmp(name, ifname) || + (flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE | IFA_F_DEPRECATED))) + continue; + + for (i = 0; i < sizeof(addr_buf); i++) { + if (!isxdigit(addr_buf[i]) || isupper(addr_buf[i])) + return false; + } + + memset(&inet6_addr, 0, sizeof(inet6_addr)); + for (i = 0; i < (sizeof(addr_buf) / 2); i++) { + unsigned char byte; + static const char hex[] = "0123456789abcdef"; + byte = ((index(hex, addr_buf[i * 2]) - hex) << 4) | + (index(hex, addr_buf[i * 2 + 1]) - hex); + inet6_addr.s6_addr[i] = byte; + } + + if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr) == IN6_IS_ADDR_LINKLOCAL(addr)) && + (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr) == IN6_IS_ADDR_UNIQUELOCAL(addr))) + return true; + } + return false; +} + static void sighandler(int signal) { if (signal == SIGUSR1) diff --git a/src/odhcp6c.h b/src/odhcp6c.h index 2a10113..e55d3c1 100644 --- a/src/odhcp6c.h +++ b/src/odhcp6c.h @@ -227,6 +227,7 @@ struct dhcpv6_server_cand { int16_t preference; uint8_t duid_len; uint8_t duid[130]; + struct in6_addr server_addr; uint32_t sol_max_rt; uint32_t inf_max_rt; void *ia_na; @@ -343,6 +344,7 @@ bool odhcp6c_signal_process(void); uint64_t odhcp6c_get_milli_time(void); int odhcp6c_random(void *buf, size_t len); bool odhcp6c_is_bound(void); +bool odhcp6c_addr_in_scope(const struct in6_addr *addr); // State manipulation void odhcp6c_clear_state(enum odhcp6c_state state);