Pārlūkot izejas kodu

[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 14 gadus atpakaļ
vecāks
revīzija
2ce0d8f08b
2 mainītis faili ar 7 papildinājumiem un 8 dzēšanām
  1. 6
    7
      src/net/ipv4.c
  2. 1
    1
      src/usr/route.c

+ 6
- 7
src/net/ipv4.c Parādīt failu

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

+ 1
- 1
src/usr/route.c Parādīt failu

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

Notiek ielāde…
Atcelt
Saglabāt