[IPv4] Use default netmasks when no subnet mask is specified.
authorMichael Brown <mcb30@etherboot.org>
Sun, 23 Mar 2008 22:20:47 +0000 (22:20 +0000)
committerMichael Brown <mcb30@etherboot.org>
Sun, 23 Mar 2008 22:20:47 +0000 (22:20 +0000)
src/include/gpxe/in.h
src/net/ipv4.c

index d502c4c..89530a5 100644 (file)
 
 #define INADDR_BROADCAST 0xffffffff
 
-#define IN_MULTICAST(addr) ( ( (addr) & 0xf0000000 ) == 0xe0000000 )
+#define        IN_CLASSA(addr)         ( ( (addr) & 0x80000000 ) == 0x00000000 )
+#define        IN_CLASSA_NET           0xff000000
+#define        IN_CLASSB(addr)         ( ( (addr) & 0xc0000000 ) == 0x80000000 )
+#define        IN_CLASSB_NET           0xffff0000
+#define        IN_CLASSC(addr)         ( ( (addr) & 0xe0000000 ) == 0xc0000000 )
+#define        IN_CLASSC_NET           0xffffff00
+#define IN_MULTICAST(addr)     ( ( (addr) & 0xf0000000 ) == 0xe0000000 )
 
 /**
  * IP address structure
index ee88dd8..67bfc2d 100644 (file)
@@ -116,18 +116,31 @@ static int ipv4_create_routes ( void ) {
        /* Create a route for each configured network device */
        for_each_netdev ( netdev ) {
                settings = netdev_settings ( netdev );
+               /* Get IPv4 address */
                address.s_addr = 0;
                fetch_ipv4_setting ( settings, DHCP_EB_YIADDR, &address );
-               netmask.s_addr = 0;
+               if ( ! address.s_addr )
+                       continue;
+               /* Calculate default netmask */
+               if ( IN_CLASSA ( ntohl ( address.s_addr ) ) ) {
+                       netmask.s_addr = htonl ( IN_CLASSA_NET );
+               } else if ( IN_CLASSB ( ntohl ( address.s_addr ) ) ) {
+                       netmask.s_addr = htonl ( IN_CLASSB_NET );
+               } else if ( IN_CLASSC ( ntohl ( address.s_addr ) ) ) {
+                       netmask.s_addr = htonl ( IN_CLASSC_NET );
+               } else {
+                       netmask.s_addr = 0;
+               }
+               /* Override with subnet mask, if present */
                fetch_ipv4_setting ( settings, DHCP_SUBNET_MASK, &netmask );
+               /* Get default gateway, if present */
                gateway.s_addr = INADDR_NONE;
                fetch_ipv4_setting ( settings, DHCP_ROUTERS, &gateway );
-               if ( address.s_addr ) {
-                       miniroute = add_ipv4_miniroute ( netdev, address,
-                                                        netmask, gateway );
-                       if ( ! miniroute )
-                               return -ENOMEM;
-               }
+               /* Configure route */
+               miniroute = add_ipv4_miniroute ( netdev, address,
+                                                netmask, gateway );
+               if ( ! miniroute )
+                       return -ENOMEM;
        }
 
        return 0;