[FreeRTOS+TCP] FreeRTOS_DHCP.c dhcpGATEWAY_OPTION_CODE

flandroid wrote on Monday, May 30, 2016:

Hello,

I am actually reading the FreeRTOS DHCP implementation and I found something that bother me.

In FreeRTOS_DHCP.c, line “~693” you have :

						case dhcpGATEWAY_OPTION_CODE :

							if( ucLength == sizeof( uint32_t ) )
							{
								/* ulProcessed is not incremented in this case
								because the gateway is not essential. */
								xNetworkAddressing.ulGatewayAddress = ulParameter;
							}
							break;

In the code, if we receive the dhcpGATEWAY_OPTION_CODE (=3), and the size of the parameter (ucLength) is 4 bytes, we take the parameter and use it as the gateway address.

In the RFC 2132 (https://tools.ietf.org/html/rfc2132) which defines the DHCP option format, you can read :

***3.5. Router Option

The router option specifies a list of IP addresses for routers on the
client’s subnet. Routers SHOULD be listed in order of preference.

The code for the router option is 3. The minimum length for the
router option is 4 octets, and the length MUST always be a multiple
of 4.

Code Len Address 1Address 2

±----±----±----±----±----±----±----±----±-
| 3 | n | a1 | a2 | a3 | a4 | a1 | a2 | …
±----±----±----±----±----±----±----±----±-*

As you can see the lenght can be different from 4.

In your implementation, in the case (probably a very rare one) where the router option contains more than one IP address, it should be more “clever” to take at least the first one ?

Without the gateway address, the device will not be able to connect to internet.

Thank for your time.

PS : Sorry for my bad English

heinbali01 wrote on Thursday, June 02, 2016:

Hi Florian,

Three days ago I answered to your post, but I forgot to check if Sourceforge had received my answer. Sorry about that.

Yes you are right, and thanks for reporting it. A DHCP server can send a list (an array) of gateway addresses.

So lets change the code to :

case dhcpGATEWAY_OPTION_CODE :

    if( ucLength >= sizeof( uint32_t ) )
    {
        /* ulProcessed is not incremented in this case
        because the gateway is not essential. */
        xNetworkAddressing.ulGatewayAddress = ulParameter;
    }
    break;

The bug went unnoticed because the routers that I played with only reported a single GW address.

Regards.