Browse Source

[ipv4] Use a zero address to indicate "no gateway", rather than INADDR_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.
tags/v1.0.0-rc1
Michael Brown 15 years ago
parent
commit
2ce0d8f08b
2 changed files with 7 additions and 8 deletions
  1. 6
    7
      src/net/ipv4.c
  2. 1
    1
      src/usr/route.c

+ 6
- 7
src/net/ipv4.c View File

40
  * @v netdev		Network device
40
  * @v netdev		Network device
41
  * @v address		IPv4 address
41
  * @v address		IPv4 address
42
  * @v netmask		Subnet mask
42
  * @v netmask		Subnet mask
43
- * @v gateway		Gateway address (or @c INADDR_NONE for no gateway)
43
+ * @v gateway		Gateway address (if any)
44
  * @ret miniroute	Routing table entry, or NULL
44
  * @ret miniroute	Routing table entry, or NULL
45
  */
45
  */
46
 static struct ipv4_miniroute * __malloc
46
 static struct ipv4_miniroute * __malloc
50
 
50
 
51
 	DBG ( "IPv4 add %s", inet_ntoa ( address ) );
51
 	DBG ( "IPv4 add %s", inet_ntoa ( address ) );
52
 	DBG ( "/%s ", inet_ntoa ( netmask ) );
52
 	DBG ( "/%s ", inet_ntoa ( netmask ) );
53
-	if ( gateway.s_addr != INADDR_NONE )
53
+	if ( gateway.s_addr )
54
 		DBG ( "gw %s ", inet_ntoa ( gateway ) );
54
 		DBG ( "gw %s ", inet_ntoa ( gateway ) );
55
 	DBG ( "via %s\n", netdev->name );
55
 	DBG ( "via %s\n", netdev->name );
56
 
56
 
70
 	/* Add to end of list if we have a gateway, otherwise
70
 	/* Add to end of list if we have a gateway, otherwise
71
 	 * to start of list.
71
 	 * to start of list.
72
 	 */
72
 	 */
73
-	if ( gateway.s_addr != INADDR_NONE ) {
73
+	if ( gateway.s_addr ) {
74
 		list_add_tail ( &miniroute->list, &ipv4_miniroutes );
74
 		list_add_tail ( &miniroute->list, &ipv4_miniroutes );
75
 	} else {
75
 	} else {
76
 		list_add ( &miniroute->list, &ipv4_miniroutes );
76
 		list_add ( &miniroute->list, &ipv4_miniroutes );
88
 
88
 
89
 	DBG ( "IPv4 del %s", inet_ntoa ( miniroute->address ) );
89
 	DBG ( "IPv4 del %s", inet_ntoa ( miniroute->address ) );
90
 	DBG ( "/%s ", inet_ntoa ( miniroute->netmask ) );
90
 	DBG ( "/%s ", inet_ntoa ( miniroute->netmask ) );
91
-	if ( miniroute->gateway.s_addr != INADDR_NONE )
91
+	if ( miniroute->gateway.s_addr )
92
 		DBG ( "gw %s ", inet_ntoa ( miniroute->gateway ) );
92
 		DBG ( "gw %s ", inet_ntoa ( miniroute->gateway ) );
93
 	DBG ( "via %s\n", miniroute->netdev->name );
93
 	DBG ( "via %s\n", miniroute->netdev->name );
94
 
94
 
120
 	list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
120
 	list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
121
 		local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
121
 		local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
122
 			    & miniroute->netmask.s_addr ) == 0 );
122
 			    & miniroute->netmask.s_addr ) == 0 );
123
-		has_gw = ( miniroute->gateway.s_addr != INADDR_NONE );
123
+		has_gw = ( miniroute->gateway.s_addr );
124
 		if ( local || has_gw ) {
124
 		if ( local || has_gw ) {
125
 			if ( ! local )
125
 			if ( ! local )
126
 				*dest = miniroute->gateway;
126
 				*dest = miniroute->gateway;
586
 	struct settings *settings;
586
 	struct settings *settings;
587
 	struct in_addr address = { 0 };
587
 	struct in_addr address = { 0 };
588
 	struct in_addr netmask = { 0 };
588
 	struct in_addr netmask = { 0 };
589
-	struct in_addr gateway = { INADDR_NONE };
589
+	struct in_addr gateway = { 0 };
590
 
590
 
591
 	/* Delete all existing routes */
591
 	/* Delete all existing routes */
592
 	list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
592
 	list_for_each_entry_safe ( miniroute, tmp, &ipv4_miniroutes, list )
613
 		/* Override with subnet mask, if present */
613
 		/* Override with subnet mask, if present */
614
 		fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
614
 		fetch_ipv4_setting ( settings, &netmask_setting, &netmask );
615
 		/* Get default gateway, if present */
615
 		/* Get default gateway, if present */
616
-		gateway.s_addr = INADDR_NONE;
617
 		fetch_ipv4_setting ( settings, &gateway_setting, &gateway );
616
 		fetch_ipv4_setting ( settings, &gateway_setting, &gateway );
618
 		/* Configure route */
617
 		/* Configure route */
619
 		miniroute = add_ipv4_miniroute ( netdev, address,
618
 		miniroute = add_ipv4_miniroute ( netdev, address,

+ 1
- 1
src/usr/route.c View File

36
 		printf ( "%s: %s/", miniroute->netdev->name,
36
 		printf ( "%s: %s/", miniroute->netdev->name,
37
 			 inet_ntoa ( miniroute->address ) );
37
 			 inet_ntoa ( miniroute->address ) );
38
 		printf ( "%s", inet_ntoa ( miniroute->netmask ) );
38
 		printf ( "%s", inet_ntoa ( miniroute->netmask ) );
39
-		if ( miniroute->gateway.s_addr != INADDR_NONE )
39
+		if ( miniroute->gateway.s_addr )
40
 			printf ( " gw %s", inet_ntoa ( miniroute->gateway ) );
40
 			printf ( " gw %s", inet_ntoa ( miniroute->gateway ) );
41
 		if ( ! ( miniroute->netdev->state & NETDEV_OPEN ) )
41
 		if ( ! ( miniroute->netdev->state & NETDEV_OPEN ) )
42
 			printf ( " (inaccessible)" );
42
 			printf ( " (inaccessible)" );

Loading…
Cancel
Save