Add complete support of DHCP relay interface ID option

RFC3315 specifies the following: "The relay agent MAY send the Interface-id
option to identify the interface on which the client message was received.
If a relay agent  receives a Relay-reply message with an Interface-id
option, the relay agent relays the message to the client through the
interface identified by the option."

The current implementation of the DHCP relay reply handling, the interface
ID field from the server response is ignored. Managing the interface ID
is very important especially as DHCP requests/replies use link-local
addresses. The consequence of this is that the interface must always be
specified because the routing layer cannot guess the correct interface.
Moreover, Mbed provides a mechanism to enable/disable the interface ID
option on a DHCP relay instance, so it is important to fully support it.

The reason why this issue has not been discoverd until now is that the DHCP
relay is mainly used on systems that use only one interface (such as Wi-SUN
routers). By default, when no interface ID is specified for the socket, the
latter will choose 6loWPAN interface by default. This means that if two
interfaces are used on the same device, the 6loWPAN interface is always
selected.

The commit adds code to retrieve the interface-id value contained within
the DHCP relay reply message and write it to a control message header
that is added to the socket message. This tells the socket which
interface to choose. If the interface-id option is not enabled on the
relay, this procedure is simply ignored.
pull/15329/head
YannCharbon 2022-08-30 16:50:24 +02:00
parent 807fd79651
commit 0d2badf42c
1 changed files with 22 additions and 0 deletions

View File

@ -442,6 +442,9 @@ void recv_dhcp_relay_msg(void *cb_res)
msg_len = socket_recvmsg(sckt_data->socket_id, &msghdr, NS_MSG_LEGACY0);
// Buffer to tell additional data for the socket (such as interface ID)
uint8_t ancillary_databuffer[NS_CMSG_SPACE(sizeof(ns_in6_pktinfo_t))];
tr_debug("dhcp Relay recv msg");
//Parse type
@ -471,6 +474,25 @@ void recv_dhcp_relay_msg(void *cb_res)
msghdr.msg_iovlen = 1;
msg_data.iov_base = relay_msg.relay_options.msg_ptr;
msg_data.iov_len = relay_msg.relay_options.len;
// Append a control message to tell the socket which interface to use if option is activated
if (memcmp(src_address.address, ns_in6addr_any, 16) != 0 && relay_srv->add_interface_id_option) {
ns_cmsghdr_t *cmsg;
ns_in6_pktinfo_t *pktinfo;
msghdr.msg_control = ancillary_databuffer;
msghdr.msg_controllen = sizeof(ancillary_databuffer);
cmsg = NS_CMSG_FIRSTHDR(&msghdr);
cmsg->cmsg_type = SOCKET_IPV6_PKTINFO;
cmsg->cmsg_level = SOCKET_IPPROTO_IPV6;
cmsg->cmsg_len = NS_CMSG_LEN(sizeof(ns_in6_pktinfo_t));
pktinfo = (ns_in6_pktinfo_t *)NS_CMSG_DATA(cmsg);
pktinfo->ipi6_ifindex = *relay_msg.relay_interface_id.msg_ptr;
memset(pktinfo->ipi6_addr, 0, 16); // Don't specify address (let socket choose appropriate by routing)
}
tr_debug("Forward Original relay msg to client");
} else {