config: use correct defaults if interface is not in UCI
[project/odhcpd.git] / src / dhcpv6.c
index 7560a75..62f30e2 100644 (file)
@@ -22,6 +22,8 @@
 #include "odhcpd.h"
 #include "dhcpv6.h"
 
 #include "odhcpd.h"
 #include "dhcpv6.h"
 
+static const char *excluded_class = "HOMENET";
+
 
 static void relay_client_request(struct sockaddr_in6 *source,
                const void *data, size_t len, struct interface *iface);
 
 static void relay_client_request(struct sockaddr_in6 *source,
                const void *data, size_t len, struct interface *iface);
@@ -32,68 +34,66 @@ static void handle_dhcpv6(void *addr, void *data, size_t len,
 static void handle_client_request(void *addr, void *data, size_t len,
                struct interface *iface);
 
 static void handle_client_request(void *addr, void *data, size_t len,
                struct interface *iface);
 
-static struct odhcpd_event dhcpv6_event = {{.fd = -1}, handle_dhcpv6};
-
 
 
 // Create socket and register events
 int init_dhcpv6(void)
 {
 
 
 // Create socket and register events
 int init_dhcpv6(void)
 {
-       int sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
-       if (sock < 0) {
-               syslog(LOG_ERR, "Failed to create DHCPv6 server socket: %s",
-                               strerror(errno));
-               return -1;
-       }
-
-       // Basic IPv6 configuration
-       int val = 1;
-       setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
-       setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
-       setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val));
-
-       val = DHCPV6_HOP_COUNT_LIMIT;
-       setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
-
-       val = 0;
-       setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, sizeof(val));
+       dhcpv6_ia_init();
+       return 0;
+}
 
 
-       struct sockaddr_in6 bind_addr = {AF_INET6, htons(DHCPV6_SERVER_PORT),
-                               0, IN6ADDR_ANY_INIT, 0};
 
 
-       if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr))) {
-               syslog(LOG_ERR, "Failed to open DHCPv6 server socket: %s",
-                               strerror(errno));
-               return -1;
+int setup_dhcpv6_interface(struct interface *iface, bool enable)
+{
+       if (iface->dhcpv6_event.uloop.fd > 0) {
+               uloop_fd_delete(&iface->dhcpv6_event.uloop);
+               close(iface->dhcpv6_event.uloop.fd);
+               iface->dhcpv6_event.uloop.fd = -1;
        }
 
        }
 
-       dhcpv6_event.uloop.fd = sock;
-       odhcpd_register(&dhcpv6_event);
+       // Configure multicast settings
+       if (enable && iface->dhcpv6 && !iface->master) {
+               int sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
+               if (sock < 0) {
+                       syslog(LOG_ERR, "Failed to create DHCPv6 server socket: %s",
+                                       strerror(errno));
+                       return -1;
+               }
+
+               // Basic IPv6 configuration
+               setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, iface->ifname, strlen(iface->ifname));
 
 
-       dhcpv6_ia_init(dhcpv6_event.uloop.fd);
+               int val = 1;
+               setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
+               setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
+               setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val));
 
 
-       return 0;
-}
+               val = DHCPV6_HOP_COUNT_LIMIT;
+               setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
 
 
+               val = 0;
+               setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, sizeof(val));
 
 
-int setup_dhcpv6_interface(struct interface *iface, bool enable)
-{
-       // Configure multicast settings
-       struct ipv6_mreq relay = {ALL_DHCPV6_RELAYS, iface->ifindex};
-       struct ipv6_mreq server = {ALL_DHCPV6_SERVERS, iface->ifindex};
+               struct sockaddr_in6 bind_addr = {AF_INET6, htons(DHCPV6_SERVER_PORT),
+                                       0, IN6ADDR_ANY_INIT, 0};
 
 
-       setsockopt(dhcpv6_event.uloop.fd, IPPROTO_IPV6,
-                       IPV6_DROP_MEMBERSHIP, &relay, sizeof(relay));
-       setsockopt(dhcpv6_event.uloop.fd, IPPROTO_IPV6,
-                       IPV6_DROP_MEMBERSHIP, &server, sizeof(server));
+               if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr))) {
+                       syslog(LOG_ERR, "Failed to open DHCPv6 server socket: %s",
+                                       strerror(errno));
+                       return -1;
+               }
 
 
-       if (enable && iface->dhcpv6 && !iface->master) {
-               setsockopt(dhcpv6_event.uloop.fd, IPPROTO_IPV6,
-                               IPV6_ADD_MEMBERSHIP, &relay, sizeof(relay));
+               struct ipv6_mreq relay = {ALL_DHCPV6_RELAYS, iface->ifindex};
+               struct ipv6_mreq server = {ALL_DHCPV6_SERVERS, iface->ifindex};
+               setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &relay, sizeof(relay));
 
                if (iface->dhcpv6 == RELAYD_SERVER)
 
                if (iface->dhcpv6 == RELAYD_SERVER)
-                       setsockopt(dhcpv6_event.uloop.fd, IPPROTO_IPV6,
-                                       IPV6_ADD_MEMBERSHIP, &server, sizeof(server));
+                       setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &server, sizeof(server));
+
+               iface->dhcpv6_event.uloop.fd = sock;
+               iface->dhcpv6_event.handle_dgram = handle_dhcpv6;
+               odhcpd_register(&iface->dhcpv6_event);
        }
 
        return setup_dhcpv6_ia_interface(iface, enable);
        }
 
        return setup_dhcpv6_ia_interface(iface, enable);
@@ -101,7 +101,7 @@ int setup_dhcpv6_interface(struct interface *iface, bool enable)
 
 
 static void handle_nested_message(uint8_t *data, size_t len,
 
 
 static void handle_nested_message(uint8_t *data, size_t len,
-               uint8_t **opts, uint8_t **end, struct iovec iov[6])
+               uint8_t **opts, uint8_t **end, struct iovec iov[9])
 {
        struct dhcpv6_relay_header *hdr = (struct dhcpv6_relay_header*)data;
        if (iov[0].iov_base == NULL) {
 {
        struct dhcpv6_relay_header *hdr = (struct dhcpv6_relay_header*)data;
        if (iov[0].iov_base == NULL) {
@@ -124,8 +124,8 @@ static void handle_nested_message(uint8_t *data, size_t len,
        uint8_t *odata;
        dhcpv6_for_each_option(hdr->options, data + len, otype, olen, odata) {
                if (otype == DHCPV6_OPT_RELAY_MSG) {
        uint8_t *odata;
        dhcpv6_for_each_option(hdr->options, data + len, otype, olen, odata) {
                if (otype == DHCPV6_OPT_RELAY_MSG) {
-                       iov[7].iov_base = odata + olen;
-                       iov[7].iov_len = (((uint8_t*)iov[0].iov_base) + iov[0].iov_len)
+                       iov[8].iov_base = odata + olen;
+                       iov[8].iov_len = (((uint8_t*)iov[0].iov_base) + iov[0].iov_len)
                                        - (odata + olen);
                        handle_nested_message(odata, olen, opts, end, iov);
                        return;
                                        - (odata + olen);
                        handle_nested_message(odata, olen, opts, end, iov);
                        return;
@@ -175,6 +175,9 @@ static void handle_client_request(void *addr, void *data, size_t len,
                uint16_t duid_type;
                uint16_t hardware_type;
                uint8_t mac[6];
                uint16_t duid_type;
                uint16_t hardware_type;
                uint8_t mac[6];
+               uint16_t solmaxrt_type;
+               uint16_t solmaxrt_length;
+               uint32_t solmaxrt_value;
                uint16_t clientid_type;
                uint16_t clientid_length;
                uint8_t clientid_buf[130];
                uint16_t clientid_type;
                uint16_t clientid_length;
                uint8_t clientid_buf[130];
@@ -184,6 +187,9 @@ static void handle_client_request(void *addr, void *data, size_t len,
                .serverid_length = htons(10),
                .duid_type = htons(3),
                .hardware_type = htons(1),
                .serverid_length = htons(10),
                .duid_type = htons(3),
                .hardware_type = htons(1),
+               .solmaxrt_type = htons(DHCPV6_OPT_SOL_MAX_RT),
+               .solmaxrt_length = htons(4),
+               .solmaxrt_value = htonl(60),
                .clientid_type = htons(DHCPV6_OPT_CLIENTID),
                .clientid_buf = {0}
        };
                .clientid_type = htons(DHCPV6_OPT_CLIENTID),
                .clientid_buf = {0}
        };
@@ -238,6 +244,14 @@ static void handle_client_request(void *addr, void *data, size_t len,
        } search = {htons(DHCPV6_OPT_DNS_DOMAIN), htons(search_len)};
 
 
        } search = {htons(DHCPV6_OPT_DNS_DOMAIN), htons(search_len)};
 
 
+       struct dhcpv6_cer_id cerid = {
+#ifdef EXT_CER_ID
+               .type = htons(EXT_CER_ID),
+#endif
+               .len = htons(36),
+               .addr = iface->dhcpv6_pd_cer,
+       };
+
 
        uint8_t pdbuf[512];
        struct iovec iov[] = {{NULL, 0},
 
        uint8_t pdbuf[512];
        struct iovec iov[] = {{NULL, 0},
@@ -247,6 +261,7 @@ static void handle_client_request(void *addr, void *data, size_t len,
                        {&search, (search_len) ? sizeof(search) : 0},
                        {search_domain, search_len},
                        {pdbuf, 0},
                        {&search, (search_len) ? sizeof(search) : 0},
                        {search_domain, search_len},
                        {pdbuf, 0},
+                       {&cerid, 0},
                        {NULL, 0}};
 
        uint8_t *opts = (uint8_t*)&hdr[1], *opts_end = (uint8_t*)data + len;
                        {NULL, 0}};
 
        uint8_t *opts = (uint8_t*)&hdr[1], *opts_end = (uint8_t*)data + len;
@@ -277,21 +292,44 @@ static void handle_client_request(void *addr, void *data, size_t len,
                        if (olen != ntohs(dest.serverid_length) ||
                                        memcmp(odata, &dest.duid_type, olen))
                                return; // Not for us
                        if (olen != ntohs(dest.serverid_length) ||
                                        memcmp(odata, &dest.duid_type, olen))
                                return; // Not for us
+               } else if (otype == DHCPV6_OPT_USER_CLASS) {
+                       uint8_t *c = odata, *cend = &odata[olen];
+                       for (; &c[2] <= cend && &c[2 + (c[0] << 8) + c[1]] <= cend; c = &c[2 + (c[0] << 8) + c[1]]) {
+                               size_t elen = strlen(excluded_class);
+                               if (((((size_t)c[0]) << 8) | c[1]) == elen && !memcmp(&c[2], excluded_class, elen))
+                                       return; // Ignore from homenet
+                       }
+               } else if (otype == DHCPV6_OPT_IA_PD) {
+#ifdef EXT_CER_ID
+                       iov[7].iov_len = sizeof(cerid);
+
+                       if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)) {
+                               struct odhcpd_ipaddr addrs[32];
+                               ssize_t len = odhcpd_get_interface_addresses(0, addrs,
+                                               sizeof(addrs) / sizeof(*addrs));
+
+                               for (ssize_t i = 0; i < len; ++i)
+                                       if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)
+                                                       || memcmp(&addrs[i].addr, &cerid.addr, sizeof(cerid.addr)) < 0)
+                                               cerid.addr = addrs[i].addr;
+                       }
+#endif
                }
        }
 
        if (opts[-4] != DHCPV6_MSG_INFORMATION_REQUEST) {
                }
        }
 
        if (opts[-4] != DHCPV6_MSG_INFORMATION_REQUEST) {
-               iov[6].iov_len = dhcpv6_handle_ia(pdbuf, sizeof(pdbuf), iface, addr, &opts[-4], opts_end);
-               if (iov[6].iov_len == 0 && opts[-4] == DHCPV6_MSG_REBIND)
+               ssize_t ialen = dhcpv6_handle_ia(pdbuf, sizeof(pdbuf), iface, addr, &opts[-4], opts_end);
+               iov[6].iov_len = ialen;
+               if (ialen < 0 || (ialen == 0 && opts[-4] == DHCPV6_MSG_REBIND))
                        return;
        }
 
        if (iov[0].iov_len > 0) // Update length
                update_nested_message(data, len, iov[1].iov_len + iov[2].iov_len +
                                iov[3].iov_len + iov[4].iov_len + iov[5].iov_len +
                        return;
        }
 
        if (iov[0].iov_len > 0) // Update length
                update_nested_message(data, len, iov[1].iov_len + iov[2].iov_len +
                                iov[3].iov_len + iov[4].iov_len + iov[5].iov_len +
-                               iov[6].iov_len - (4 + opts_end - opts));
+                               iov[6].iov_len + iov[7].iov_len - (4 + opts_end - opts));
 
 
-       odhcpd_send(dhcpv6_event.uloop.fd, addr, iov, ARRAY_SIZE(iov), iface);
+       odhcpd_send(iface->dhcpv6_event.uloop.fd, addr, iov, ARRAY_SIZE(iov), iface);
 }
 
 
 }
 
 
@@ -395,7 +433,7 @@ static void relay_server_response(uint8_t *data, size_t len)
        }
 
        struct iovec iov = {payload_data, payload_len};
        }
 
        struct iovec iov = {payload_data, payload_len};
-       odhcpd_send(dhcpv6_event.uloop.fd, &target, &iov, 1, iface);
+       odhcpd_send(iface->dhcpv6_event.uloop.fd, &target, &iov, 1, iface);
 }
 
 
 }
 
 
@@ -452,5 +490,5 @@ static void relay_client_request(struct sockaddr_in6 *source,
        struct sockaddr_in6 dhcpv6_servers = {AF_INET6,
                        htons(DHCPV6_SERVER_PORT), 0, ALL_DHCPV6_SERVERS, 0};
        struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void*)data, len}};
        struct sockaddr_in6 dhcpv6_servers = {AF_INET6,
                        htons(DHCPV6_SERVER_PORT), 0, ALL_DHCPV6_SERVERS, 0};
        struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void*)data, len}};
-       odhcpd_send(dhcpv6_event.uloop.fd, &dhcpv6_servers, iov, 2, master);
+       odhcpd_send(iface->dhcpv6_event.uloop.fd, &dhcpv6_servers, iov, 2, master);
 }
 }