Merge with /home/git/u-boot
[oweals/u-boot.git] / net / net.c
index 7c3744dcefa644624ffd9f60fbbb84e7a8e2a4ac..1d1c98f3c20204c74ef3173b50537e21af8c60cd 100644 (file)
--- a/net/net.c
+++ b/net/net.c
  *
  * DHCP:
  *
- *     Prerequisites:   - own ethernet address
- *     We want:         - IP, Netmask, ServerIP, Gateway IP
- *                      - bootfilename, lease time
- *     Next step:       - TFTP
+ *     Prerequisites:  - own ethernet address
+ *     We want:                - IP, Netmask, ServerIP, Gateway IP
+ *                     - bootfilename, lease time
+ *     Next step:      - TFTP
  *
  * TFTP:
  *
@@ -67,7 +67,7 @@
  *
  * SNTP:
  *
- *     Prerequisites:  - own ethernet address
+ *     Prerequisites:  - own ethernet address
  *                     - own IP address
  *     We want:        - network time
  *     Next step:      none
@@ -92,6 +92,8 @@
 
 #if (CONFIG_COMMANDS & CFG_CMD_NET)
 
+DECLARE_GLOBAL_DATA_PTR;
+
 #define ARP_TIMEOUT            5               /* Seconds before trying ARP again */
 #ifndef        CONFIG_NET_RETRY_COUNT
 # define ARP_TIMEOUT_COUNT     5               /* # of timeouts before giving up  */
@@ -185,7 +187,7 @@ static int net_check_prereq (proto_t protocol);
 IPaddr_t       NetArpWaitPacketIP;
 IPaddr_t       NetArpWaitReplyIP;
 uchar         *NetArpWaitPacketMAC;    /* MAC address of waiting packet's destination  */
-uchar          *NetArpWaitTxPacket;    /* THE transmit packet                  */
+uchar         *NetArpWaitTxPacket;     /* THE transmit packet                  */
 int            NetArpWaitTxPacketSize;
 uchar          NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
 ulong          NetArpWaitTimerStart;
@@ -212,8 +214,8 @@ void ArpRequest (void)
        arp->ar_pln = 4;
        arp->ar_op = htons (ARPOP_REQUEST);
 
-       memcpy (&arp->ar_data[0], NetOurEther, 6);              /* source ET addr       */
-       NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP);     /* source IP addr       */
+       memcpy (&arp->ar_data[0], NetOurEther, 6);              /* source ET addr       */
+       NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP);     /* source IP addr       */
        for (i = 10; i < 16; ++i) {
                arp->ar_data[i] = 0;                            /* dest ET addr = 0     */
        }
@@ -222,8 +224,10 @@ void ArpRequest (void)
            (NetOurIP & NetOurSubnetMask)) {
                if (NetOurGatewayIP == 0) {
                        puts ("## Warning: gatewayip needed but not set\n");
+                       NetArpWaitReplyIP = NetArpWaitPacketIP;
+               } else {
+                       NetArpWaitReplyIP = NetOurGatewayIP;
                }
-               NetArpWaitReplyIP = NetOurGatewayIP;
        } else {
                NetArpWaitReplyIP = NetArpWaitPacketIP;
        }
@@ -264,8 +268,6 @@ void ArpTimeoutCheck(void)
 int
 NetLoop(proto_t protocol)
 {
-       DECLARE_GLOBAL_DATA_PTR;
-
        bd_t *bd = gd->bd;
 
 #ifdef CONFIG_NET_MULTI
@@ -372,11 +374,11 @@ restart:
                 */
                NetOurIP = 0;
                NetServerIP = getenv_IPaddr ("serverip");
-               NetOurVLAN = getenv_VLAN("vlan");       /* VLANs must be read */
-               NetOurNativeVLAN = getenv_VLAN("nvlan");
-       case CDP:
-               NetOurVLAN = getenv_VLAN("vlan");       /* VLANs must be read */
-               NetOurNativeVLAN = getenv_VLAN("nvlan");
+               NetOurVLAN = getenv_VLAN("vlan");       /* VLANs must be read */
+               NetOurNativeVLAN = getenv_VLAN("nvlan");
+       case CDP:
+               NetOurVLAN = getenv_VLAN("vlan");       /* VLANs must be read */
+               NetOurNativeVLAN = getenv_VLAN("nvlan");
                break;
        default:
                break;
@@ -461,7 +463,7 @@ restart:
        /*
         * Echo the inverted link state to the fault LED.
         */
-       if(miiphy_link(CFG_FAULT_MII_ADDR)) {
+       if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
                status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
        } else {
                status_led_set (STATUS_LED_RED, STATUS_LED_ON);
@@ -506,13 +508,13 @@ restart:
                        thand_f *x;
 
 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
-#  if defined(CFG_FAULT_ECHO_LINK_DOWN) &&
-      defined(CONFIG_STATUS_LED) &&
+#  if defined(CFG_FAULT_ECHO_LINK_DOWN) && \
+      defined(CONFIG_STATUS_LED) &&       \
       defined(STATUS_LED_RED)
                        /*
                         * Echo the inverted link state to the fault LED.
                         */
-                       if(miiphy_link(CFG_FAULT_MII_ADDR)) {
+                       if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
                                status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
                        } else {
                                status_led_set (STATUS_LED_RED, STATUS_LED_ON);
@@ -570,9 +572,6 @@ startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
 
 void NetStartAgain (void)
 {
-#ifdef CONFIG_NET_MULTI
-       DECLARE_GLOBAL_DATA_PTR;
-#endif
        char *nretry;
        int noretry = 0, once = 0;
 
@@ -810,6 +809,7 @@ static ushort CDP_compute_csum(const uchar *buff, ushort len)
        int     odd;
        ulong   result = 0;
        ushort  leftover;
+       ushort *p;
 
        if (len > 0) {
                odd = 1 & (ulong)buff;
@@ -819,14 +819,19 @@ static ushort CDP_compute_csum(const uchar *buff, ushort len)
                        buff++;
                }
                while (len > 1) {
-                       result += *((const ushort *)buff)++;
+                       p = (ushort *)buff;
+                       result += *p++;
+                       buff = (uchar *)p;
                        if (result & 0x80000000)
                                result = (result & 0xFFFF) + (result >> 16);
                        len -= 2;
                }
                if (len) {
                        leftover = (signed short)(*(const signed char *)buff);
-                       /* * XXX CISCO SUCKS big time! (and blows too) */
+                       /* CISCO SUCKS big time! (and blows too):
+                        * CDP uses the IP checksum algorithm with a twist;
+                        * for the last byte it *sign* extends and sums.
+                        */
                        result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
                }
                while (result >> 16)
@@ -1405,12 +1410,12 @@ NetReceive(volatile uchar * inpkt, int len)
 
                        switch (icmph->type) {
                        case ICMP_REDIRECT:
-                       if (icmph->code != ICMP_REDIR_HOST)
+                               if (icmph->code != ICMP_REDIR_HOST)
+                                       return;
+                               puts (" ICMP Host Redirect to ");
+                               print_IPaddr(icmph->un.gateway);
+                               putc(' ');
                                return;
-                       puts (" ICMP Host Redirect to ");
-                       print_IPaddr(icmph->un.gateway);
-                       putc(' ');
-                               break;
 #if (CONFIG_COMMANDS & CFG_CMD_PING)
                        case ICMP_ECHO_REPLY:
                                /*
@@ -1418,7 +1423,7 @@ NetReceive(volatile uchar * inpkt, int len)
                                 */
                                /* XXX point to ip packet */
                                (*packetHandler)((uchar *)ip, 0, 0, 0);
-                               break;
+                               return;
 #endif
                        default:
                                return;
@@ -1427,6 +1432,46 @@ NetReceive(volatile uchar * inpkt, int len)
                        return;
                }
 
+#ifdef CONFIG_UDP_CHECKSUM
+               if (ip->udp_xsum != 0) {
+                       ulong   xsum;
+                       ushort *sumptr;
+                       ushort  sumlen;
+
+                       xsum  = ip->ip_p;
+                       xsum += (ntohs(ip->udp_len));
+                       xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
+                       xsum += (ntohl(ip->ip_src) >>  0) & 0x0000ffff;
+                       xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
+                       xsum += (ntohl(ip->ip_dst) >>  0) & 0x0000ffff;
+
+                       sumlen = ntohs(ip->udp_len);
+                       sumptr = (ushort *) &(ip->udp_src);
+
+                       while (sumlen > 1) {
+                               ushort sumdata;
+
+                               sumdata = *sumptr++;
+                               xsum += ntohs(sumdata);
+                               sumlen -= 2;
+                       }
+                       if (sumlen > 0) {
+                               ushort sumdata;
+
+                               sumdata = *(unsigned char *) sumptr;
+                               sumdata = (sumdata << 8) & 0xff00;
+                               xsum += sumdata;
+                       }
+                       while ((xsum >> 16) != 0) {
+                               xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
+                       }
+                       if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
+                               printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum));
+                               return;
+                       }
+               }
+#endif
+
 #ifdef CONFIG_NETCONSOLE
                nc_input_packet((uchar *)ip +IP_HDR_SIZE,
                                                ntohs(ip->udp_dst),
@@ -1477,7 +1522,7 @@ static int net_check_prereq (proto_t protocol)
                        return (1);
                }
 #if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
-             common:
+    common:
 #endif
 
                if (NetOurIP == 0) {
@@ -1534,10 +1579,11 @@ unsigned
 NetCksum(uchar * ptr, int len)
 {
        ulong   xsum;
+       ushort *p = (ushort *)ptr;
 
        xsum = 0;
        while (len-- > 0)
-               xsum += *((ushort *)ptr)++;
+               xsum += *p++;
        xsum = (xsum & 0xffff) + (xsum >> 16);
        xsum = (xsum & 0xffff) + (xsum >> 16);
        return (xsum & 0xffff);
@@ -1614,7 +1660,7 @@ NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
        ip->ip_sum   = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
 }
 
-void copy_filename (uchar *dst, uchar *src, int size)
+void copy_filename (char *dst, char *src, int size)
 {
        if (*src && (*src == '"')) {
                ++src;