dhcpv6: add DHCPv4-over-DHCPv6 support
[project/odhcpd.git] / src / dhcpv6.c
index 554939459c2f8c4c3e7b6e60733d8036422d2a2b..cc068a35a8243de8da100b8a6fdb3b08a4231c42 100644 (file)
@@ -1,5 +1,6 @@
 /**
  * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
+ * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License v2 as published by
 #include <sys/timerfd.h>
 #include <arpa/inet.h>
 
+#include <libubox/utils.h>
+
 #include "odhcpd.h"
 #include "dhcpv6.h"
-
+#ifdef DHCPV4_SUPPORT
+#include "dhcpv4.h"
+#endif
 
 static void relay_client_request(struct sockaddr_in6 *source,
                const void *data, size_t len, struct interface *iface);
@@ -37,22 +42,23 @@ static void handle_client_request(void *addr, void *data, size_t len,
 /* Create socket and register events */
 int dhcpv6_init(void)
 {
-       dhcpv6_ia_init();
-       return 0;
+       return dhcpv6_ia_init();
 }
 
 int dhcpv6_setup_interface(struct interface *iface, bool enable)
 {
        int ret = 0;
 
-       if (iface->dhcpv6_event.uloop.fd > 0) {
+       enable = enable && (iface->dhcpv6 != MODE_DISABLED);
+
+       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;
        }
 
        /* Configure multicast settings */
-       if (enable && iface->dhcpv6) {
+       if (enable) {
                struct sockaddr_in6 bind_addr = {AF_INET6, htons(DHCPV6_SERVER_PORT),
                                        0, IN6ADDR_ANY_INIT, 0};
                struct ipv6_mreq mreq;
@@ -145,10 +151,10 @@ int dhcpv6_setup_interface(struct interface *iface, bool enable)
                odhcpd_register(&iface->dhcpv6_event);
        }
 
-       ret = dhcpv6_setup_ia_interface(iface, 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;
        }
@@ -161,6 +167,7 @@ enum {
        IOV_DEST,
        IOV_MAXRT,
 #define IOV_STAT IOV_MAXRT
+       IOV_RAPID_COMMIT,
        IOV_DNS,
        IOV_DNS_ADDR,
        IOV_SEARCH,
@@ -170,13 +177,18 @@ enum {
        IOV_CERID,
        IOV_DHCPV6_RAW,
        IOV_RELAY_MSG,
+       IOV_DHCPV4O6_SERVER,
        IOV_TOTAL
 };
 
 static void handle_nested_message(uint8_t *data, size_t len,
-               uint8_t **opts, uint8_t **end, struct iovec iov[IOV_TOTAL])
+                                 struct dhcpv6_client_header **c_hdr, uint8_t **opts,
+                                 uint8_t **end, struct iovec iov[IOV_TOTAL])
 {
-       struct dhcpv6_relay_header *hdr = (struct dhcpv6_relay_header*)data;
+       struct dhcpv6_relay_header *r_hdr = (struct dhcpv6_relay_header *)data;
+       uint16_t otype, olen;
+       uint8_t *odata;
+
        if (iov[IOV_NESTED].iov_base == NULL) {
                iov[IOV_NESTED].iov_base = data;
                iov[IOV_NESTED].iov_len = len;
@@ -185,22 +197,20 @@ static void handle_nested_message(uint8_t *data, size_t len,
        if (len < sizeof(struct dhcpv6_client_header))
                return;
 
-       if (hdr->msg_type != DHCPV6_MSG_RELAY_FORW) {
-               iov[IOV_NESTED].iov_len = data - (uint8_t*)iov[IOV_NESTED].iov_base;
-               struct dhcpv6_client_header *hdr = (void*)data;
-               *opts = (uint8_t*)&hdr[1];
+       if (r_hdr->msg_type != DHCPV6_MSG_RELAY_FORW) {
+               iov[IOV_NESTED].iov_len = data - (uint8_t *)iov[IOV_NESTED].iov_base;
+               *c_hdr = (void *)data;
+               *opts = (uint8_t *)&(*c_hdr)[1];
                *end = data + len;
                return;
        }
 
-       uint16_t otype, olen;
-       uint8_t *odata;
-       dhcpv6_for_each_option(hdr->options, data + len, otype, olen, odata) {
+       dhcpv6_for_each_option(r_hdr->options, data + len, otype, olen, odata) {
                if (otype == DHCPV6_OPT_RELAY_MSG) {
                        iov[IOV_RELAY_MSG].iov_base = odata + olen;
-                       iov[IOV_RELAY_MSG].iov_len = (((uint8_t*)iov[IOV_NESTED].iov_base) + 
+                       iov[IOV_RELAY_MSG].iov_len = (((uint8_t *)iov[IOV_NESTED].iov_base) +
                                        iov[IOV_NESTED].iov_len) - (odata + olen);
-                       handle_nested_message(odata, olen, opts, end, iov);
+                       handle_nested_message(odata, olen, c_hdr, opts, end, iov);
                        return;
                }
        }
@@ -228,16 +238,80 @@ static void update_nested_message(uint8_t *data, size_t len, ssize_t pdiff)
        }
 }
 
+#ifdef DHCPV4_SUPPORT
+
+struct dhcpv4_msg_data {
+       uint8_t *msg;
+       size_t maxsize;
+       ssize_t len;
+};
+
+static int send_reply(_unused const void *buf, size_t len,
+                     _unused const struct sockaddr *dest, _unused socklen_t dest_len,
+                     _unused void *opaque)
+{
+       struct dhcpv4_msg_data *reply = opaque;
+
+       if (len > reply->maxsize) {
+               syslog(LOG_ERR, "4o6: reply too large, %u > %u", len, reply->maxsize);
+               reply->len = -1;
+       } else {
+               memcpy(reply->msg, buf, len);
+               reply->len = len;
+       }
+
+       return reply->len;
+}
+
+static ssize_t dhcpv6_4o6_query(uint8_t *buf, size_t buflen,
+                               struct interface *iface,
+                               const struct sockaddr_in6 *addr,
+                               const void *data, const uint8_t *end)
+{
+       const struct dhcpv6_client_header *hdr = data;
+       uint16_t otype, olen, msgv4_len = 0;
+       uint8_t *msgv4_data = NULL;
+       uint8_t *start = (uint8_t *)&hdr[1], *odata;
+       struct sockaddr_in addrv4;
+       struct dhcpv4_msg_data reply = { .msg = buf, .maxsize = buflen, .len = -1 };
+
+       dhcpv6_for_each_option(start, end, otype, olen, odata) {
+               if (otype == DHCPV6_OPT_DHCPV4_MSG) {
+                       msgv4_data = odata;
+                       msgv4_len = olen;
+               }
+       }
+
+       if (!msgv4_data || msgv4_len == 0) {
+               syslog(LOG_ERR, "4o6: missing DHCPv4 message option (%d)", DHCPV6_OPT_DHCPV4_MSG);
+               return -1;
+       }
+
+       // Dummy IPv4 address
+       memset(&addrv4, 0, sizeof(addrv4));
+       addrv4.sin_family = AF_INET;
+       addrv4.sin_addr.s_addr = INADDR_ANY;
+       addrv4.sin_port = htons(DHCPV4_CLIENT_PORT);
+
+       dhcpv4_handle_msg(&addrv4, msgv4_data, msgv4_len,
+                         iface, NULL, send_reply, &reply);
+
+       return reply.len;
+}
+#endif /* DHCPV4_SUPPORT */
+
 /* Simple DHCPv6-server for information requests */
 static void handle_client_request(void *addr, void *data, size_t len,
                struct interface *iface, void *dest_addr)
 {
        struct dhcpv6_client_header *hdr = data;
+       uint8_t *opts = (uint8_t *)&hdr[1], *opts_end = (uint8_t *)data + len;
+       bool o_rapid_commit = false;
 
        if (len < sizeof(*hdr))
                return;
 
-       syslog(LOG_NOTICE, "Got DHCPv6 request");
+       syslog(LOG_DEBUG, "Got a DHCPv6-request on %s", iface->name);
 
        /* Construct reply message */
        struct __attribute__((packed)) {
@@ -269,6 +343,11 @@ static void handle_client_request(void *addr, void *data, size_t len,
        } maxrt = {htons(DHCPV6_OPT_SOL_MAX_RT), htons(sizeof(maxrt) - 4),
                        htonl(60)};
 
+       struct __attribute__((packed)) {
+               uint16_t type;
+               uint16_t len;
+       } rapid_commit = {htons(DHCPV6_OPT_RAPID_COMMIT), 0};
+
        struct __attribute__((packed)) {
                uint16_t type;
                uint16_t len;
@@ -318,6 +397,11 @@ static void handle_client_request(void *addr, void *data, size_t len,
        } search = {htons(DHCPV6_OPT_DNS_DOMAIN), htons(search_len)};
 
 
+       struct __attribute__((packed)) {
+               uint16_t type;
+               uint16_t len;
+       } dhcpv4o6_server = {htons(DHCPV6_OPT_4O6_SERVER), 0};
+
        struct dhcpv6_cer_id cerid = {
 #ifdef EXT_CER_ID
                .type = htons(EXT_CER_ID),
@@ -332,6 +416,7 @@ static void handle_client_request(void *addr, void *data, size_t len,
                [IOV_NESTED] = {NULL, 0},
                [IOV_DEST] = {&dest, (uint8_t*)&dest.clientid_type - (uint8_t*)&dest},
                [IOV_MAXRT] = {&maxrt, sizeof(maxrt)},
+               [IOV_RAPID_COMMIT] = {&rapid_commit, 0},
                [IOV_DNS] = {&dns, (dns_cnt) ? sizeof(dns) : 0},
                [IOV_DNS_ADDR] = {dns_addr_ptr, dns_cnt * sizeof(*dns_addr_ptr)},
                [IOV_SEARCH] = {&search, (search_len) ? sizeof(search) : 0},
@@ -339,32 +424,45 @@ static void handle_client_request(void *addr, void *data, size_t len,
                [IOV_PDBUF] = {pdbuf, 0},
                [IOV_CERID] = {&cerid, 0},
                [IOV_DHCPV6_RAW] = {iface->dhcpv6_raw, iface->dhcpv6_raw_len},
-               [IOV_RELAY_MSG] = {NULL, 0}
+               [IOV_RELAY_MSG] = {NULL, 0},
+               [IOV_DHCPV4O6_SERVER] = {&dhcpv4o6_server, 0},
        };
 
-       uint8_t *opts = (uint8_t*)&hdr[1], *opts_end = (uint8_t*)data + len;
        if (hdr->msg_type == DHCPV6_MSG_RELAY_FORW)
-               handle_nested_message(data, len, &opts, &opts_end, iov);
-
-       memcpy(dest.tr_id, &opts[-3], sizeof(dest.tr_id));
-
-       if (opts[-4] == DHCPV6_MSG_ADVERTISE || opts[-4] == DHCPV6_MSG_REPLY || opts[-4] == DHCPV6_MSG_RELAY_REPL)
-               return;
+               handle_nested_message(data, len, &hdr, &opts, &opts_end, iov);
+
+       switch (hdr->msg_type) {
+       case DHCPV6_MSG_SOLICIT:
+       case DHCPV6_MSG_REQUEST:
+       case DHCPV6_MSG_CONFIRM:
+       case DHCPV6_MSG_RENEW:
+       case DHCPV6_MSG_REBIND:
+       case DHCPV6_MSG_RELEASE:
+       case DHCPV6_MSG_DECLINE:
+       case DHCPV6_MSG_INFORMATION_REQUEST:
+       case DHCPV6_MSG_RELAY_FORW:
+#ifdef DHCPV4_SUPPORT
+       case DHCPV6_MSG_DHCPV4_QUERY:
+#endif
+               break; /* Valid message types for clients */
+       case DHCPV6_MSG_ADVERTISE:
+       case DHCPV6_MSG_REPLY:
+       case DHCPV6_MSG_RECONFIGURE:
+       case DHCPV6_MSG_RELAY_REPL:
+       case DHCPV6_MSG_DHCPV4_RESPONSE:
+#ifndef DHCPV4_SUPPORT
+       case DHCPV6_MSG_DHCPV4_QUERY:
+#endif
+       default:
+               return; /* Invalid message types for clients */
+       }
 
        if (!IN6_IS_ADDR_MULTICAST((struct in6_addr *)dest_addr) && iov[IOV_NESTED].iov_len == 0 &&
-               (opts[-4] == DHCPV6_MSG_SOLICIT || opts[-4] == DHCPV6_MSG_CONFIRM ||
-                opts[-4] == DHCPV6_MSG_REBIND || opts[-4] == DHCPV6_MSG_INFORMATION_REQUEST))
+           (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_CONFIRM ||
+            hdr->msg_type == DHCPV6_MSG_REBIND || hdr->msg_type == DHCPV6_MSG_INFORMATION_REQUEST))
                return;
 
-       if (opts[-4] == DHCPV6_MSG_SOLICIT) {
-               dest.msg_type = DHCPV6_MSG_ADVERTISE;
-       } else if (opts[-4] == DHCPV6_MSG_INFORMATION_REQUEST) {
-               iov[IOV_REFRESH].iov_base = &refresh;
-               iov[IOV_REFRESH].iov_len = sizeof(refresh);
-
-               /* Return inf max rt option in reply to information request */
-               maxrt.type = htons(DHCPV6_OPT_INF_MAX_RT);
-       }
+       memcpy(dest.tr_id, hdr->transaction_id, sizeof(dest.tr_id));
 
        /* Go through options and find what we need */
        uint16_t otype, olen;
@@ -401,12 +499,29 @@ static void handle_client_request(void *addr, void *data, size_t len,
                                free(addrs);
                        }
 #endif
+               } else if (otype == DHCPV6_OPT_RAPID_COMMIT && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
+                       iov[IOV_RAPID_COMMIT].iov_len = sizeof(rapid_commit);
+                       o_rapid_commit = true;
+               } else if (otype == DHCPV6_OPT_ORO) {
+                       for (int i=0; i < olen/2; i++) {
+                               uint16_t option = ntohs(((uint16_t *)odata)[i]);
+                               switch (option) {
+#ifdef DHCPV4_SUPPORT
+                               case DHCPV6_OPT_4O6_SERVER:
+                                       if (iface->dhcpv4)
+                                               iov[IOV_DHCPV4O6_SERVER].iov_len = sizeof(dhcpv4o6_server);
+                                       break;
+#endif /* DHCPV4_SUPPORT */
+                               default:
+                                       break;
+                               }
+                       }
                }
        }
 
        if (!IN6_IS_ADDR_MULTICAST((struct in6_addr *)dest_addr) && iov[IOV_NESTED].iov_len == 0 &&
-               (opts[-4] == DHCPV6_MSG_REQUEST || opts[-4] == DHCPV6_MSG_RENEW ||
-                opts[-4] == DHCPV6_MSG_RELEASE || opts[-4] == DHCPV6_MSG_DECLINE)) {
+           (hdr->msg_type == DHCPV6_MSG_REQUEST || hdr->msg_type == DHCPV6_MSG_RENEW ||
+            hdr->msg_type == DHCPV6_MSG_RELEASE || hdr->msg_type == DHCPV6_MSG_DECLINE)) {
                iov[IOV_STAT].iov_base = &stat;
                iov[IOV_STAT].iov_len = sizeof(stat);
 
@@ -417,19 +532,59 @@ static void handle_client_request(void *addr, void *data, size_t len,
                return;
        }
 
-       if (opts[-4] != DHCPV6_MSG_INFORMATION_REQUEST) {
-               ssize_t ialen = dhcpv6_handle_ia(pdbuf, sizeof(pdbuf), iface, addr, &opts[-4], opts_end);
+       if (hdr->msg_type == DHCPV6_MSG_SOLICIT && !o_rapid_commit) {
+               dest.msg_type = DHCPV6_MSG_ADVERTISE;
+       } else if (hdr->msg_type == DHCPV6_MSG_INFORMATION_REQUEST) {
+               iov[IOV_REFRESH].iov_base = &refresh;
+               iov[IOV_REFRESH].iov_len = sizeof(refresh);
+
+               /* Return inf max rt option in reply to information request */
+               maxrt.type = htons(DHCPV6_OPT_INF_MAX_RT);
+       }
+
+#ifdef DHCPV4_SUPPORT
+       if (hdr->msg_type == DHCPV6_MSG_DHCPV4_QUERY) {
+               struct _packed dhcpv4_msg_data {
+                       uint16_t type;
+                       uint16_t len;
+                       uint8_t msg[1];
+               } *msg_opt = (struct dhcpv4_msg_data*)pdbuf;
+               ssize_t msglen;
+
+               memset(pdbuf, 0, sizeof(pdbuf));
+
+               msglen = dhcpv6_4o6_query(msg_opt->msg, sizeof(pdbuf) - sizeof(*msg_opt) + 1,
+                                               iface, addr, (const void *)hdr, opts_end);
+               if (msglen <= 0) {
+                       syslog(LOG_ERR, "4o6: query failed");
+                       return;
+               }
+
+               msg_opt->type = htons(DHCPV6_OPT_DHCPV4_MSG);
+               msg_opt->len = htons(msglen);
+               iov[IOV_PDBUF].iov_len = sizeof(*msg_opt) - 1 + msglen;
+               dest.msg_type = DHCPV6_MSG_DHCPV4_RESPONSE;
+       } else
+#endif /* DHCPV4_SUPPORT */
+       if (hdr->msg_type != DHCPV6_MSG_INFORMATION_REQUEST) {
+               ssize_t ialen = dhcpv6_ia_handle_IAs(pdbuf, sizeof(pdbuf), iface, addr, (const void *)hdr, opts_end);
+
                iov[IOV_PDBUF].iov_len = ialen;
-               if (ialen < 0 || (ialen == 0 && (opts[-4] == DHCPV6_MSG_REBIND || opts[-4] == DHCPV6_MSG_CONFIRM)))
+               if (ialen < 0 ||
+                   (ialen == 0 && (hdr->msg_type == DHCPV6_MSG_REBIND || hdr->msg_type == DHCPV6_MSG_CONFIRM)))
                        return;
        }
 
        if (iov[IOV_NESTED].iov_len > 0) /* Update length */
                update_nested_message(data, len, iov[IOV_DEST].iov_len + iov[IOV_MAXRT].iov_len +
-                               iov[IOV_DNS].iov_len + iov[IOV_DNS_ADDR].iov_len +
-                               iov[IOV_SEARCH].iov_len + iov[IOV_SEARCH_DOMAIN].iov_len +
-                               iov[IOV_PDBUF].iov_len + iov[IOV_CERID].iov_len +
-                               iov[IOV_DHCPV6_RAW].iov_len - (4 + opts_end - opts));
+                                     iov[IOV_RAPID_COMMIT].iov_len + iov[IOV_DNS].iov_len +
+                                     iov[IOV_DNS_ADDR].iov_len + iov[IOV_SEARCH].iov_len +
+                                     iov[IOV_SEARCH_DOMAIN].iov_len + iov[IOV_PDBUF].iov_len +
+                                     iov[IOV_DHCPV4O6_SERVER].iov_len +
+                                     iov[IOV_CERID].iov_len + iov[IOV_DHCPV6_RAW].iov_len -
+                                     (4 + opts_end - opts));
+
+       syslog(LOG_DEBUG, "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);
 }
@@ -459,19 +614,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_DEBUG, "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) {
@@ -521,7 +674,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;
@@ -535,6 +688,9 @@ static void relay_server_response(uint8_t *data, size_t len)
        }
 
        struct iovec iov = {payload_data, payload_len};
+
+       syslog(LOG_DEBUG, "Sending a DHCPv6-reply on %s", iface->name);
+
        odhcpd_send(iface->dhcpv6_event.uloop.fd, &target, &iov, 1, iface);
 }
 
@@ -563,19 +719,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,
@@ -585,39 +729,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_DEBUG, "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_DEBUG, "Sending a DHCPv6-relay-forward on %s", c->name);
+
+               odhcpd_send(c->dhcpv6_event.uloop.fd, &s, iov, 2, c);
+       }
 }