Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[ipv4] Use a zero address to indicate "no gateway", rather than INADD…
…R_NONE

ipv4.c uses a gateway address of INADDR_NONE to represent "no
gateway".  It initialises the gateway address to INADDR_NONE before
calling fetch_ipv4_setting() to retrieve the configured gateway
address (if any).

However, as of commit 612f4e7 "[settings] Avoid returning
uninitialised data on error in fetch_xxx_setting()",
fetch_ipv4_setting() will zero the IP address if the setting does not
exist, rather than leaving it unaltered.

Fix by using a zero IP address to indicate "no gateway", so that a
non-existent gateway address setting will be treated as such.
  • Loading branch information
Michael Brown committed Nov 16, 2009
1 parent 76d5e49 commit 2ce0d8f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 6 additions & 7 deletions src/net/ipv4.c
Expand Up @@ -40,7 +40,7 @@ static LIST_HEAD ( frag_buffers );
* @v netdev Network device
* @v address IPv4 address
* @v netmask Subnet mask
* @v gateway Gateway address (or @c INADDR_NONE for no gateway)
* @v gateway Gateway address (if any)
* @ret miniroute Routing table entry, or NULL
*/
static struct ipv4_miniroute * __malloc
Expand All @@ -50,7 +50,7 @@ add_ipv4_miniroute ( struct net_device *netdev, struct in_addr address,

DBG ( "IPv4 add %s", inet_ntoa ( address ) );
DBG ( "/%s ", inet_ntoa ( netmask ) );
if ( gateway.s_addr != INADDR_NONE )
if ( gateway.s_addr )
DBG ( "gw %s ", inet_ntoa ( gateway ) );
DBG ( "via %s\n", netdev->name );

Expand All @@ -70,7 +70,7 @@ add_ipv4_miniroute ( struct net_device *netdev, struct in_addr address,
/* Add to end of list if we have a gateway, otherwise
* to start of list.
*/
if ( gateway.s_addr != INADDR_NONE ) {
if ( gateway.s_addr ) {
list_add_tail ( &miniroute->list, &ipv4_miniroutes );
} else {
list_add ( &miniroute->list, &ipv4_miniroutes );
Expand All @@ -88,7 +88,7 @@ static void del_ipv4_miniroute ( struct ipv4_miniroute *miniroute ) {

DBG ( "IPv4 del %s", inet_ntoa ( miniroute->address ) );
DBG ( "/%s ", inet_ntoa ( miniroute->netmask ) );
if ( miniroute->gateway.s_addr != INADDR_NONE )
if ( miniroute->gateway.s_addr )
DBG ( "gw %s ", inet_ntoa ( miniroute->gateway ) );
DBG ( "via %s\n", miniroute->netdev->name );

Expand Down Expand Up @@ -120,7 +120,7 @@ static struct ipv4_miniroute * ipv4_route ( struct in_addr *dest ) {
list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
& miniroute->netmask.s_addr ) == 0 );
has_gw = ( miniroute->gateway.s_addr != INADDR_NONE );
has_gw = ( miniroute->gateway.s_addr );
if ( local || has_gw ) {
if ( ! local )
*dest = miniroute->gateway;
Expand Down Expand Up @@ -586,7 +586,7 @@ static int ipv4_create_routes ( void ) {
struct settings *settings;
struct in_addr address = { 0 };
struct in_addr netmask = { 0 };
struct in_addr gateway = { INADDR_NONE };
struct in_addr gateway = { 0 };

/* Delete all existing routes */
list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
Expand All @@ -613,7 +613,6 @@ static int ipv4_create_routes ( void ) {
/* Override with subnet mask, if present */
fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
/* Get default gateway, if present */
gateway.s_addr = INADDR_NONE;
fetch_ipv4_setting ( settings, &gateway_setting, &gateway );
/* Configure route */
miniroute = add_ipv4_miniroute ( netdev, address,
Expand Down
2 changes: 1 addition & 1 deletion src/usr/route.c
Expand Up @@ -36,7 +36,7 @@ void route ( void ) {
printf ( "%s: %s/", miniroute->netdev->name,
inet_ntoa ( miniroute->address ) );
printf ( "%s", inet_ntoa ( miniroute->netmask ) );
if ( miniroute->gateway.s_addr != INADDR_NONE )
if ( miniroute->gateway.s_addr )
printf ( " gw %s", inet_ntoa ( miniroute->gateway ) );
if ( ! ( miniroute->netdev->state & NETDEV_OPEN ) )
printf ( " (inaccessible)" );
Expand Down

0 comments on commit 2ce0d8f

Please sign in to comment.