]> git.decadent.org.uk Git - odhcp6c.git/blobdiff - src/dhcpv6.c
Detect a suitable MAC if the main interface doesn't have one (e.g. ppp)
[odhcp6c.git] / src / dhcpv6.c
index 9b664ed473a4430f0ab81e01bab637fba1fb05f5..a3d4223404688dc9234be1753b7e3ccc2f4b9919 100644 (file)
@@ -110,12 +110,36 @@ int init_dhcpv6(const char *ifname, int request_pd)
                uint8_t duid[14] = {0, DHCPV6_OPT_CLIENTID, 0, 10, 0,
                                DHCPV6_DUID_LLADDR, 0, 1};
                memcpy(&duid[8], ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
+
+               uint8_t zero[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
+               struct ifreq ifs[100], *ifp, *ifend;
+               struct ifconf ifc;
+               ifc.ifc_req = ifs;
+               ifc.ifc_len = sizeof(ifs);
+
+               if (!memcmp(&duid[8], zero, ETHER_ADDR_LEN) &&
+                               ioctl(sock, SIOCGIFCONF, &ifc) >= 0) {
+                       // If our interface doesn't have an address...
+                       ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
+                       for (ifp = ifc.ifc_req; ifp < ifend &&
+                                       !memcmp(&duid[8], zero, 6); ifp++) {
+                               memcpy(ifr.ifr_name, ifp->ifr_name,
+                                               sizeof(ifr.ifr_name));
+                               ioctl(sock, SIOCGIFHWADDR, &ifr);
+                               memcpy(&duid[8], ifr.ifr_hwaddr.sa_data,
+                                               ETHER_ADDR_LEN);
+                       }
+               }
+
                odhcp6c_add_state(STATE_CLIENT_ID, duid, sizeof(duid));
        }
 
        // Create ORO
        uint16_t oro[] = {htons(DHCPV6_OPT_DNS_SERVERS),
-                       htons(DHCPV6_OPT_DNS_DOMAIN)};
+                       htons(DHCPV6_OPT_DNS_DOMAIN),
+                       htons(DHCPV6_OPT_NTP_SERVER),
+                       htons(DHCPV6_OPT_SIP_SERVER_A),
+                       htons(DHCPV6_OPT_SIP_SERVER_D)};
        odhcp6c_add_state(STATE_ORO, oro, sizeof(oro));
 
 
@@ -313,7 +337,7 @@ int dhcpv6_request(enum dhcpv6_msg type)
 
        syslog(LOG_NOTICE, "Sending %s (timeout %us)", retx->name, timeout);
 
-       uint64_t start = adhc6c_get_milli_time(), round_start = start, elapsed;
+       uint64_t start = odhcp6c_get_milli_time(), round_start = start, elapsed;
 
        // Generate transaction ID
        uint8_t trid[3];
@@ -344,7 +368,7 @@ int dhcpv6_request(enum dhcpv6_msg type)
 
                // Receive rounds
                for (; len < 0 && round_start < round_end;
-                               round_start = adhc6c_get_milli_time()) {
+                               round_start = odhcp6c_get_milli_time()) {
                        // Check for pending signal
                        if (odhcp6c_signal_is_pending())
                                return -1;
@@ -365,7 +389,7 @@ int dhcpv6_request(enum dhcpv6_msg type)
                                uint8_t *opt = &buf[4];
                                uint8_t *opt_end = opt + len - 4;
 
-                               round_start = adhc6c_get_milli_time();
+                               round_start = odhcp6c_get_milli_time();
                                elapsed = round_start - start;
                                syslog(LOG_NOTICE, "Got a valid reply after "
                                                "%ums", (unsigned)elapsed);
@@ -476,21 +500,23 @@ static int dhcpv6_handle_advert(_unused enum dhcpv6_msg orig,
                if (otype == DHCPV6_OPT_SERVERID && olen <= 130) {
                        memcpy(cand.duid, odata, olen);
                        cand.duid_len = olen;
-               } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 &&
-                               !odata[0] && odata[1] == DHCPV6_NoAddrsAvail) {
+               } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
+                               && odata[1] == DHCPV6_NoAddrsAvail) {
                        if (na_mode == IA_MODE_FORCE) {
                                return -1;
                        } else {
                                cand.has_noaddravail = true;
                                cand.preference -= 1000;
                        }
+               } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
+                               && odata[1] == DHCPV6_NoPrefixAvail) {
+                       cand.preference -= 2000;
                } else if (otype == DHCPV6_OPT_PREF && olen >= 1 &&
                                cand.preference >= 0) {
                        cand.preference = odata[1];
                } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
                        cand.wants_reconfigure = true;
-               }
-               else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
+               } else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
                        struct dhcpv6_ia_hdr *h = (void*)odata;
                        uint8_t *oend = odata + olen, *d;
                        dhcpv6_for_each_option(&h[1], oend, otype, olen, d) {
@@ -499,7 +525,7 @@ static int dhcpv6_handle_advert(_unused enum dhcpv6_msg orig,
                                else if (otype == DHCPV6_OPT_STATUS &&
                                                olen >= 2 && d[0] == 0 &&
                                                d[1] == DHCPV6_NoPrefixAvail)
-                                       return -1;
+                                       cand.preference -= 2000;
                        }
                }
        }
@@ -571,11 +597,16 @@ static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig,
 
        t1 = t2 = t3 = 86400;
 
-       size_t ia_na_len, dns_len, search_len;
+       size_t ia_na_len, dns_len, search_len, sntp_ip_len, sntp_dns_len;
+       size_t sip_ip_len, sip_fqdn_len;
        uint8_t *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
        uint8_t *ia_end;
        odhcp6c_get_state(STATE_DNS, &dns_len);
        odhcp6c_get_state(STATE_SEARCH, &search_len);
+       odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len);
+       odhcp6c_get_state(STATE_SNTP_FQDN, &sntp_dns_len);
+       odhcp6c_get_state(STATE_SIP_IP, &sip_ip_len);
+       odhcp6c_get_state(STATE_SIP_FQDN, &sip_fqdn_len);
 
        // Decrease valid and preferred lifetime of prefixes
        size_t ia_pd_len;
@@ -627,6 +658,10 @@ static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig,
                        if (l_t2 > 0 && t2 > l_t2)
                                t2 = l_t2;
 
+                       // Always report update in case we have IA_PDs so that
+                       // the state-script is called with updated times
+                       if (otype == DHCPV6_OPT_IA_PD && request_prefix)
+                               have_update = true;
 
                        time_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
 
@@ -640,9 +675,29 @@ static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig,
                                t3 = n;
 
                } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
-                       odhcp6c_add_state(STATE_DNS, odata, olen);
+                       if (olen == 16)
+                               odhcp6c_add_state(STATE_DNS, odata, olen);
                } else if (otype == DHCPV6_OPT_DNS_DOMAIN) {
                        odhcp6c_add_state(STATE_SEARCH, odata, olen);
+               } else if (otype == DHCPV6_OPT_NTP_SERVER) {
+                       uint16_t stype, slen;
+                       uint8_t *sdata;
+                       // Test status and bail if error
+                       dhcpv6_for_each_option(odata, odata + olen,
+                                       stype, slen, sdata) {
+                               if (slen == 16 && (stype == NTP_MC_ADDR ||
+                                               stype == NTP_SRV_ADDR))
+                                       odhcp6c_add_state(STATE_SNTP_IP,
+                                                       sdata, slen);
+                               else if (slen > 0 && stype == NTP_SRV_FQDN)
+                                       odhcp6c_add_state(STATE_SNTP_FQDN,
+                                                       sdata, slen);
+                       }
+               } else if (otype == DHCPV6_OPT_SIP_SERVER_A) {
+                       if (olen == 16)
+                               odhcp6c_add_state(STATE_SIP_IP, odata, olen);
+               } else if (otype == DHCPV6_OPT_SIP_SERVER_D) {
+                       odhcp6c_add_state(STATE_SIP_FQDN, odata, olen);
                } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) {
                        uint32_t refresh = ntohl(*((uint32_t*)odata));
                        if (refresh < (uint32_t)t1)
@@ -657,6 +712,12 @@ static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig,
        if (opt) {
                have_update |= odhcp6c_commit_state(STATE_DNS, dns_len);
                have_update |= odhcp6c_commit_state(STATE_SEARCH, search_len);
+               have_update |= odhcp6c_commit_state(STATE_SNTP_IP,
+                               sntp_ip_len);
+               have_update |= odhcp6c_commit_state(STATE_SNTP_FQDN,
+                               sntp_dns_len);
+               have_update |= odhcp6c_commit_state(STATE_SIP_IP, sip_ip_len);
+               have_update |= odhcp6c_commit_state(STATE_SIP_FQDN, sip_fqdn_len);
                size_t new_ia_pd_len, new_ia_na_len;
                odhcp6c_get_state(STATE_IA_PD, &new_ia_pd_len);
                odhcp6c_get_state(STATE_IA_NA, &new_ia_na_len);
@@ -732,10 +793,6 @@ static time_t dhcpv6_parse_ia(void *opt, void *end)
 
                        if (timeout > valid)
                                timeout = valid;
-
-                       if (prefix->valid == 0) // We probably lost that prefix
-                               odhcp6c_add_state(STATE_IA_PD_LOST,
-                                               prefix, olen);
                } else if (otype == DHCPV6_OPT_IA_ADDR) {
                        struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
                        if (olen + 4U < sizeof(*addr))