router: fix dns search list option
[project/odhcpd.git] / src / dhcpv6.c
index f2080c87e1fdb01b531be6e3cd19f71400bd1d4b..f71418d9e05fe5df9ed41cddd277bd56410d44db 100644 (file)
@@ -47,7 +47,7 @@ int dhcpv6_setup_interface(struct interface *iface, bool enable)
 {
        int ret = 0;
 
-       if (iface->dhcpv6_event.uloop.fd > 0) {
+       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;
@@ -150,7 +150,7 @@ int dhcpv6_setup_interface(struct interface *iface, bool enable)
        ret = dhcpv6_ia_setup_interface(iface, enable);
 
 out:
-       if (ret < 0 && iface->dhcpv6_event.uloop.fd > 0) {
+       if (ret < 0 && iface->dhcpv6_event.uloop.fd >= 0) {
                close(iface->dhcpv6_event.uloop.fd);
                iface->dhcpv6_event.uloop.fd = -1;
        }
@@ -244,7 +244,7 @@ static void handle_client_request(void *addr, void *data, size_t len,
        if (len < sizeof(*hdr))
                return;
 
-       syslog(LOG_NOTICE, "Got DHCPv6 request on %s", iface->name);
+       syslog(LOG_NOTICE, "Got a DHCPv6-request on %s", iface->name);
 
        /* Construct reply message */
        struct __attribute__((packed)) {
@@ -450,6 +450,8 @@ static void handle_client_request(void *addr, void *data, size_t len,
                                      iov[IOV_CERID].iov_len + iov[IOV_DHCPV6_RAW].iov_len -
                                      (4 + opts_end - opts));
 
+       syslog(LOG_NOTICE, "Sending a DHCPv6-%s on %s", iov[IOV_NESTED].iov_len ? "relay-reply" : "reply", iface->name);
+
        odhcpd_send(iface->dhcpv6_event.uloop.fd, addr, iov, ARRAY_SIZE(iov), iface);
 }
 
@@ -478,19 +480,17 @@ static void relay_server_response(uint8_t *data, size_t len)
        int32_t ifaceidx = 0;
        struct sockaddr_in6 target = {AF_INET6, htons(DHCPV6_CLIENT_PORT),
                0, IN6ADDR_ANY_INIT, 0};
-
-       syslog(LOG_NOTICE, "Got a DHCPv6-reply");
-
        int otype, olen;
        uint8_t *odata, *end = data + len;
-
        /* Relay DHCPv6 reply from server to client */
        struct dhcpv6_relay_header *h = (void*)data;
+
+       syslog(LOG_NOTICE, "Got a DHCPv6-relay-reply");
+
        if (len < sizeof(*h) || h->msg_type != DHCPV6_MSG_RELAY_REPL)
                return;
 
-       memcpy(&target.sin6_addr, &h->peer_address,
-                       sizeof(struct in6_addr));
+       memcpy(&target.sin6_addr, &h->peer_address, sizeof(struct in6_addr));
 
        /* Go through options and find what we need */
        dhcpv6_for_each_option(h->options, end, otype, olen, odata) {
@@ -540,7 +540,7 @@ static void relay_server_response(uint8_t *data, size_t len)
 
                if (rewrite_cnt == 0) {
                        if (odhcpd_get_interface_dns_addr(iface, &addr))
-                               return; // Unable to get interface address
+                               return; /* Unable to get interface address */
 
                        rewrite = &addr;
                        rewrite_cnt = 1;
@@ -554,6 +554,9 @@ static void relay_server_response(uint8_t *data, size_t len)
        }
 
        struct iovec iov = {payload_data, payload_len};
+
+       syslog(LOG_NOTICE, "Sending a DHCPv6-reply on %s", iface->name);
+
        odhcpd_send(iface->dhcpv6_event.uloop.fd, &target, &iov, 1, iface);
 }
 
@@ -582,19 +585,7 @@ static struct odhcpd_ipaddr *relay_link_address(struct interface *iface)
 static void relay_client_request(struct sockaddr_in6 *source,
                const void *data, size_t len, struct interface *iface)
 {
-       struct interface *master = odhcpd_get_master_interface();
        const struct dhcpv6_relay_header *h = data;
-       struct sockaddr_in6 s;
-
-       if (!master || master->dhcpv6 != MODE_RELAY ||
-                       h->msg_type == DHCPV6_MSG_RELAY_REPL ||
-                       h->msg_type == DHCPV6_MSG_RECONFIGURE ||
-                       h->msg_type == DHCPV6_MSG_REPLY ||
-                       h->msg_type == DHCPV6_MSG_ADVERTISE)
-               return; /* Invalid message types for client */
-
-       syslog(LOG_NOTICE, "Got a DHCPv6-request");
-
        /* Construct our forwarding envelope */
        struct dhcpv6_relay_forward_envelope hdr = {
                .msg_type = DHCPV6_MSG_RELAY_FORW,
@@ -604,39 +595,60 @@ static void relay_client_request(struct sockaddr_in6 *source,
                .relay_message_type = htons(DHCPV6_OPT_RELAY_MSG),
                .relay_message_len = htons(len),
        };
+       struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void *)data, len}};
+       struct interface *c;
+       struct odhcpd_ipaddr *ip;
+       struct sockaddr_in6 s;
+
+       if (h->msg_type == DHCPV6_MSG_RELAY_REPL ||
+           h->msg_type == DHCPV6_MSG_RECONFIGURE ||
+           h->msg_type == DHCPV6_MSG_REPLY ||
+           h->msg_type == DHCPV6_MSG_ADVERTISE)
+               return; /* Invalid message types for client */
+
+       syslog(LOG_NOTICE, "Got a DHCPv6-request on %s", iface->name);
 
        if (h->msg_type == DHCPV6_MSG_RELAY_FORW) { /* handle relay-forward */
                if (h->hop_count >= DHCPV6_HOP_COUNT_LIMIT)
-                       return; // Invalid hop count
-               else
-                       hdr.hop_count = h->hop_count + 1;
+                       return; /* Invalid hop count */
+
+               hdr.hop_count = h->hop_count + 1;
        }
 
        /* use memcpy here as the destination fields are unaligned */
-       uint32_t ifindex = iface->ifindex;
        memcpy(&hdr.peer_address, &source->sin6_addr, sizeof(struct in6_addr));
-       memcpy(&hdr.interface_id_data, &ifindex, sizeof(ifindex));
+       memcpy(&hdr.interface_id_data, &iface->ifindex, sizeof(iface->ifindex));
 
        /* Detect public IP of slave interface to use as link-address */
-       struct odhcpd_ipaddr *ip = relay_link_address(iface);
-       if (!ip) {
-               /* No suitable address! Is the slave not configured yet?
-                * Detect public IP of master interface and use it instead
-                * This is WRONG and probably violates the RFC. However
-                * otherwise we have a hen and egg problem because the
-                * slave-interface cannot be auto-configured. */
-               ip = relay_link_address(master);
-               if (!ip)
-                       return; /* Could not obtain a suitable address */
-       }
-
-       memcpy(&hdr.link_address, &ip->addr.in6, sizeof(hdr.link_address));
+       ip = relay_link_address(iface);
+       if (ip)
+               memcpy(&hdr.link_address, &ip->addr.in6, sizeof(hdr.link_address));
 
        memset(&s, 0, sizeof(s));
        s.sin6_family = AF_INET6;
        s.sin6_port = htons(DHCPV6_SERVER_PORT);
        inet_pton(AF_INET6, ALL_DHCPV6_SERVERS, &s.sin6_addr);
 
-       struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void*)data, len}};
-       odhcpd_send(master->dhcpv6_event.uloop.fd, &s, iov, 2, master);
+       avl_for_each_element(&interfaces, c, avl) {
+               if (!c->master || c->dhcpv6 != MODE_RELAY)
+                       continue;
+
+               if (!ip) {
+                       /* No suitable address! Is the slave not configured yet?
+                        * Detect public IP of master interface and use it instead
+                        * This is WRONG and probably violates the RFC. However
+                        * otherwise we have a hen and egg problem because the
+                        * slave-interface cannot be auto-configured. */
+                       ip = relay_link_address(c);
+                       if (!ip)
+                               continue; /* Could not obtain a suitable address */
+
+                       memcpy(&hdr.link_address, &ip->addr.in6, sizeof(hdr.link_address));
+                       ip = NULL;
+               }
+
+               syslog(LOG_NOTICE, "Sending a DHCPv6-relay-forward on %s", c->name);
+
+               odhcpd_send(c->dhcpv6_event.uloop.fd, &s, iov, 2, c);
+       }
 }