]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - net/bootp.c
Merge with git+ssh://sr@pollux.denx.org/home/wd/git/u-boot/from_stefan
[karo-tx-uboot.git] / net / bootp.c
index b760b885b2afe08788b4457e3768b8493db25920..80f53bc8863fd8fca4ec9305a896accdcb6b8cb3 100644 (file)
@@ -31,7 +31,7 @@
 
 #define BOOTP_VENDOR_MAGIC     0x63825363      /* RFC1048 Magic Cookie         */
 
-#if (CONFIG_COMMANDS & CFG_CMD_NET)
+#if defined(CONFIG_CMD_NET)
 
 #define TIMEOUT                5               /* Seconds before trying BOOTP again    */
 #ifndef CONFIG_NET_RETRY_COUNT
@@ -53,7 +53,7 @@ int           BootpTry;
 ulong          seed1, seed2;
 #endif
 
-#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
+#if defined(CONFIG_CMD_DHCP)
 dhcp_state_t dhcp_state = INIT;
 unsigned long dhcp_leasetime = 0;
 IPaddr_t NetDHCPServerIP = 0;
@@ -76,12 +76,12 @@ static char *dhcpmsg2str(int type)
 }
 #endif
 
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX)
+#if defined(CONFIG_BOOTP_VENDOREX)
 extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */
 extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL  */
 #endif
 
-#endif /* CFG_CMD_DHCP */
+#endif
 
 static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
 {
@@ -117,10 +117,15 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
  */
 static void BootpCopyNetParams(Bootp_t *bp)
 {
+       IPaddr_t tmp_ip;
+
        NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
-       NetCopyIP(&NetServerIP, &bp->bp_siaddr);
+       NetCopyIP(&tmp_ip, &bp->bp_siaddr);
+       if (tmp_ip != 0)
+               NetCopyIP(&NetServerIP, &bp->bp_siaddr);
        memcpy (NetServerEther, ((Ethernet_t *)NetRxPkt)->et_src, 6);
-       copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
+       if (strlen(bp->bp_file) > 0)
+               copy_filename (BootFile, bp->bp_file, sizeof(BootFile));
 
        debug ("Bootfile: %s\n", BootFile);
 
@@ -143,7 +148,7 @@ static int truncate_sz (const char *name, int maxlen, int curlen)
        return (curlen);
 }
 
-#if !(CONFIG_COMMANDS & CFG_CMD_DHCP)
+#if !defined(CONFIG_CMD_DHCP)
 
 static void BootpVendorFieldProcess (u8 * ext)
 {
@@ -176,7 +181,7 @@ static void BootpVendorFieldProcess (u8 * ext)
                if (NetOurDNSIP == 0) {
                        NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2));
                }
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
+#if defined(CONFIG_BOOTP_DNS2)
                if ((NetOurDNS2IP == 0) && (size > 4)) {
                        NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4));
                }
@@ -263,7 +268,7 @@ static void BootpVendorProcess (u8 * ext, int size)
        }
 
 #ifdef DEBUG_BOOTP_EXT
-       printf ("[BOOTP] Received fields: \n");
+       puts ("[BOOTP] Received fields: \n");
        if (NetOurSubnetMask) {
                puts ("NetOurSubnetMask : ");
                print_IPaddr (NetOurSubnetMask);
@@ -325,7 +330,7 @@ BootpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
 
        /* Retrieve extended information (we must parse the vendor area) */
        if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
-               BootpVendorProcess(&bp->bp_vend[4], len);
+               BootpVendorProcess((uchar *)&bp->bp_vend[4], len);
 
        NetSetTimeout(0, (thand_f *)0);
 
@@ -339,7 +344,7 @@ BootpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
                         */
                        NetState = NETLOOP_SUCCESS;
                        return;
-#if (CONFIG_COMMANDS & CFG_CMD_NFS)
+#if defined(CONFIG_CMD_NFS)
                } else if (strcmp(s, "NFS") == 0) {
                        /*
                         * Use NFS to load the bootfile.
@@ -352,7 +357,7 @@ BootpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
 
        TftpStart();
 }
-#endif /* !CFG_CMD_DHCP */
+#endif
 
 /*
  *     Timeout on BOOTP/DHCP request.
@@ -372,17 +377,17 @@ BootpTimeout(void)
 /*
  *     Initialize BOOTP extension fields in the request.
  */
-#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
+#if defined(CONFIG_CMD_DHCP)
 static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP)
 {
        u8 *start = e;
        u8 *cnt;
 
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX)
+#if defined(CONFIG_BOOTP_VENDOREX)
        u8 *x;
 #endif
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SEND_HOSTNAME)
-       uchar *hostname;
+#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
+       char *hostname;
 #endif
 
        *e++ = 99;              /* RFC1048 Magic Cookie */
@@ -420,7 +425,7 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R
                *e++ = tmp >> 8;
                *e++ = tmp & 0xff;
        }
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SEND_HOSTNAME)
+#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
        if ((hostname = getenv ("hostname"))) {
                int hostnamelen = strlen (hostname);
 
@@ -431,7 +436,7 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R
        }
 #endif
 
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX)
+#if defined(CONFIG_BOOTP_VENDOREX)
        if ((x = dhcp_vendorex_prep (e)))
                return x - start;
 #endif
@@ -439,33 +444,41 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R
        *e++ = 55;              /* Parameter Request List */
         cnt = e++;             /* Pointer to count of requested items */
        *cnt = 0;
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SUBNETMASK)
+#if defined(CONFIG_BOOTP_SUBNETMASK)
        *e++  = 1;              /* Subnet Mask */
        *cnt += 1;
 #endif
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_GATEWAY)
+#if defined(CONFIG_BOOTP_TIMEOFFSET)
+       *e++  = 2;
+       *cnt += 1;
+#endif
+#if defined(CONFIG_BOOTP_GATEWAY)
        *e++  = 3;              /* Router Option */
        *cnt += 1;
 #endif
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS)
+#if defined(CONFIG_BOOTP_DNS)
        *e++  = 6;              /* DNS Server(s) */
        *cnt += 1;
 #endif
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_HOSTNAME)
+#if defined(CONFIG_BOOTP_HOSTNAME)
        *e++  = 12;             /* Hostname */
        *cnt += 1;
 #endif
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTFILESIZE)
+#if defined(CONFIG_BOOTP_BOOTFILESIZE)
        *e++  = 13;             /* Boot File Size */
        *cnt += 1;
 #endif
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTPATH)
+#if defined(CONFIG_BOOTP_BOOTPATH)
        *e++  = 17;             /* Boot path */
        *cnt += 1;
 #endif
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NISDOMAIN)
+#if defined(CONFIG_BOOTP_NISDOMAIN)
        *e++  = 40;             /* NIS Domain name request */
        *cnt += 1;
+#endif
+#if defined(CONFIG_BOOTP_NTPSERVER)
+       *e++  = 42;
+       *cnt += 1;
 #endif
        *e++  = 255;            /* End of the list */
 
@@ -478,9 +491,9 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R
        return e - start;
 }
 
-#else  /* CFG_CMD_DHCP */
+#else
 /*
- *     Warning: no field size check - change CONFIG_BOOTP_MASK at your own risk!
+ *     Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
  */
 static int BootpExtended (u8 * e)
 {
@@ -491,7 +504,7 @@ static int BootpExtended (u8 * e)
        *e++ = 83;
        *e++ = 99;
 
-#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
+#if defined(CONFIG_CMD_DHCP)
        *e++ = 53;              /* DHCP Message Type */
        *e++ = 1;
        *e++ = DHCP_DISCOVER;
@@ -500,45 +513,45 @@ static int BootpExtended (u8 * e)
        *e++ = 2;
        *e++ = (576 - 312 + OPT_SIZE) >> 16;
        *e++ = (576 - 312 + OPT_SIZE) & 0xff;
-#endif /* CFG_CMD_DHCP */
+#endif
 
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SUBNETMASK)
+#if defined(CONFIG_BOOTP_SUBNETMASK)
        *e++ = 1;               /* Subnet mask request */
        *e++ = 4;
        e   += 4;
 #endif
 
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_GATEWAY)
+#if defined(CONFIG_BOOTP_GATEWAY)
        *e++ = 3;               /* Default gateway request */
        *e++ = 4;
        e   += 4;
 #endif
 
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS)
+#if defined(CONFIG_BOOTP_DNS)
        *e++ = 6;               /* Domain Name Server */
        *e++ = 4;
        e   += 4;
 #endif
 
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_HOSTNAME)
+#if defined(CONFIG_BOOTP_HOSTNAME)
        *e++ = 12;              /* Host name request */
        *e++ = 32;
        e   += 32;
 #endif
 
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTFILESIZE)
+#if defined(CONFIG_BOOTP_BOOTFILESIZE)
        *e++ = 13;              /* Boot file size */
        *e++ = 2;
        e   += 2;
 #endif
 
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTPATH)
+#if defined(CONFIG_BOOTP_BOOTPATH)
        *e++ = 17;              /* Boot path */
        *e++ = 32;
        e   += 32;
 #endif
 
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NISDOMAIN)
+#if defined(CONFIG_BOOTP_NISDOMAIN)
        *e++ = 40;              /* NIS Domain name request */
        *e++ = 32;
        e   += 32;
@@ -548,7 +561,7 @@ static int BootpExtended (u8 * e)
 
        return e - start;
 }
-#endif /* CFG_CMD_DHCP */
+#endif
 
 void
 BootpRequest (void)
@@ -557,7 +570,7 @@ BootpRequest (void)
        Bootp_t *bp;
        int ext_len, pktlen, iplen;
 
-#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
+#if defined(CONFIG_CMD_DHCP)
        dhcp_state = INIT;
 #endif
 
@@ -565,7 +578,7 @@ BootpRequest (void)
        unsigned char bi_enetaddr[6];
        int   reg;
        char  *e,*s;
-       uchar tmp[64];
+       char tmp[64];
        ulong tst1, tst2, sum, m_mask, m_value = 0;
 
        if (BootpTry ==0) {
@@ -580,7 +593,7 @@ BootpRequest (void)
                        }
                }
 #ifdef DEBUG
-               printf("BootpRequest => Our Mac: ");
+               puts ("BootpRequest => Our Mac: ");
                for (reg=0; reg<6; reg++) {
                        printf ("%x%c",
                                bi_enetaddr[reg],
@@ -639,8 +652,7 @@ BootpRequest (void)
        pkt = NetTxPacket;
        memset ((void*)pkt, 0, PKTSIZE);
 
-       NetSetEther(pkt, NetBcastAddr, PROT_IP);
-       pkt += ETHER_HDR_SIZE;
+       pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
 
        /*
         * Next line results in incorrect packet size being transmitted, resulting
@@ -666,11 +678,11 @@ BootpRequest (void)
        copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file));
 
        /* Request additional information from the BOOTP/DHCP server */
-#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
-       ext_len = DhcpExtended(bp->bp_vend, DHCP_DISCOVER, 0, 0);
+#if defined(CONFIG_CMD_DHCP)
+       ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0);
 #else
-       ext_len = BootpExtended(bp->bp_vend);
-#endif /* CFG_CMD_DHCP */
+       ext_len = BootpExtended((u8 *)bp->bp_vend);
+#endif
 
        /*
         *      Bootp ID is the lower 4 bytes of our ethernet address
@@ -693,17 +705,17 @@ BootpRequest (void)
        NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
        NetSetTimeout(SELECT_TIMEOUT * CFG_HZ, BootpTimeout);
 
-#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
+#if defined(CONFIG_CMD_DHCP)
        dhcp_state = SELECTING;
        NetSetHandler(DhcpHandler);
 #else
        NetSetHandler(BootpHandler);
-#endif /* CFG_CMD_DHCP */
+#endif
        NetSendPacket(NetTxPacket, pktlen);
 }
 
-#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
-static void DhcpOptionsProcess (uchar * popt)
+#if defined(CONFIG_CMD_DHCP)
+static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp)
 {
        uchar *end = popt + BOOTP_HDR_SIZE;
        int oplen, size;
@@ -714,12 +726,18 @@ static void DhcpOptionsProcess (uchar * popt)
                case 1:
                        NetCopyIP (&NetOurSubnetMask, (popt + 2));
                        break;
+#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
+               case 2:         /* Time offset  */
+                       NetCopyLong (&NetTimeOffset, (ulong *) (popt + 2));
+                       NetTimeOffset = ntohl (NetTimeOffset);
+                       break;
+#endif
                case 3:
                        NetCopyIP (&NetOurGatewayIP, (popt + 2));
                        break;
                case 6:
                        NetCopyIP (&NetOurDNSIP, (popt + 2));
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
+#if defined(CONFIG_BOOTP_DNS2)
                        if (*(popt + 1) > 4) {
                                NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4));
                        }
@@ -737,6 +755,11 @@ static void DhcpOptionsProcess (uchar * popt)
                        memcpy (&NetOurRootPath, popt + 2, size);
                        NetOurRootPath[size] = 0;
                        break;
+#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
+               case 42:        /* NTP server IP */
+                       NetCopyIP (&NetNtpServerIP, (popt + 2));
+                       break;
+#endif
                case 51:
                        NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
                        break;
@@ -749,8 +772,36 @@ static void DhcpOptionsProcess (uchar * popt)
                        break;
                case 59:        /* Ignore Rebinding Time Option */
                        break;
+               case 66:        /* Ignore TFTP server name */
+                       break;
+               case 67:        /* vendor opt bootfile */
+                       /*
+                        * I can't use dhcp_vendorex_proc here because I need
+                        * to write into the bootp packet - even then I had to
+                        * pass the bootp packet pointer into here as the
+                        * second arg
+                        */
+                       size = truncate_sz ("Opt Boot File",
+                                           sizeof(bp->bp_file),
+                                           oplen);
+                       if (bp->bp_file[0] == '\0' && size > 0) {
+                               /*
+                                * only use vendor boot file if we didn't
+                                * receive a boot file in the main non-vendor
+                                * part of the packet - god only knows why
+                                * some vendors chose not to use this perfectly
+                                * good spot to store the boot file (join on
+                                * Tru64 Unix) it seems mind bogglingly crazy
+                                * to me
+                                */
+                               printf("*** WARNING: using vendor "
+                                       "optional boot file\n");
+                               memcpy(bp->bp_file, popt + 2, size);
+                               bp->bp_file[size] = '\0';
+                       }
+                       break;
                default:
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX)
+#if defined(CONFIG_BOOTP_VENDOREX)
                        if (dhcp_vendorex_proc (popt))
                                break;
 #endif
@@ -786,8 +837,7 @@ static void DhcpSendRequestPkt(Bootp_t *bp_offer)
        pkt = NetTxPacket;
        memset ((void*)pkt, 0, PKTSIZE);
 
-       NetSetEther(pkt, NetBcastAddr, PROT_IP);
-       pkt += ETHER_HDR_SIZE;
+       pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP);
 
        iphdr = pkt;            /* We'll need this later to set proper pkt size */
        pkt += IP_HDR_SIZE;
@@ -801,7 +851,12 @@ static void DhcpSendRequestPkt(Bootp_t *bp_offer)
        NetCopyIP(&bp->bp_ciaddr, &bp_offer->bp_ciaddr); /* both in network byte order */
        NetCopyIP(&bp->bp_yiaddr, &bp_offer->bp_yiaddr);
        NetCopyIP(&bp->bp_siaddr, &bp_offer->bp_siaddr);
-       NetCopyIP(&bp->bp_giaddr, &bp_offer->bp_giaddr);
+       /*
+        * RFC3046 requires Relay Agents to discard packets with
+        * nonzero and offered giaddr
+        */
+       NetWriteIP(&bp->bp_giaddr, 0);
+
        memcpy (bp->bp_chaddr, NetOurEther, 6);
 
        /*
@@ -814,7 +869,7 @@ static void DhcpSendRequestPkt(Bootp_t *bp_offer)
         * Copy options from OFFER packet if present
         */
        NetCopyIP(&OfferedIP, &bp->bp_yiaddr);
-       extlen = DhcpExtended(bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
+       extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP);
 
        pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + extlen;
        iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen;
@@ -860,7 +915,7 @@ DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
                        dhcp_state = REQUESTING;
 
                        if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
-                               DhcpOptionsProcess(&bp->bp_vend[4]);
+                               DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
 
                        BootpCopyNetParams(bp); /* Store net params from reply */
 
@@ -875,16 +930,16 @@ DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
        case REQUESTING:
                debug ("DHCP State: REQUESTING\n");
 
-               if ( DhcpMessageType(bp->bp_vend) == DHCP_ACK ) {
+               if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) {
                        char *s;
 
                        if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
-                               DhcpOptionsProcess(&bp->bp_vend[4]);
+                               DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
                        BootpCopyNetParams(bp); /* Store net params from reply */
                        dhcp_state = BOUND;
-                       printf("DHCP client bound to address ");
+                       puts ("DHCP client bound to address ");
                        print_IPaddr(NetOurIP);
-                       printf("\n");
+                       putc ('\n');
 
                        /* Obey the 'autoload' setting */
                        if ((s = getenv("autoload")) != NULL) {
@@ -895,7 +950,7 @@ DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
                                         */
                                        NetState = NETLOOP_SUCCESS;
                                        return;
-#if (CONFIG_COMMANDS & CFG_CMD_NFS)
+#if defined(CONFIG_CMD_NFS)
                                } else if (strcmp(s, "NFS") == 0) {
                                        /*
                                         * Use NFS to load the bootfile.
@@ -910,7 +965,7 @@ DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
                }
                break;
        default:
-               printf("DHCP: INVALID STATE\n");
+               puts ("DHCP: INVALID STATE\n");
                break;
        }
 
@@ -920,6 +975,6 @@ void DhcpRequest(void)
 {
        BootpRequest();
 }
-#endif /* CFG_CMD_DHCP */
+#endif
 
-#endif /* CFG_CMD_NET */
+#endif