Coding Style cleanup; update CHANGELOG.
[platform/kernel/u-boot.git] / net / net.c
index 44feee2..641c37c 100644 (file)
--- a/net/net.c
+++ b/net/net.c
 #if defined(CONFIG_CMD_SNTP)
 #include "sntp.h"
 #endif
+#if defined(CONFIG_CDP_VERSION)
+#include <timestamp.h>
+#endif
+#if defined(CONFIG_CMD_DNS)
+#include "dns.h"
+#endif
 
 #if defined(CONFIG_CMD_NET)
 
 DECLARE_GLOBAL_DATA_PTR;
 
-#define ARP_TIMEOUT            5UL             /* Seconds before trying ARP again */
+#ifndef        CONFIG_ARP_TIMEOUT
+# define ARP_TIMEOUT           5000UL  /* Milliseconds before trying ARP again */
+#else
+# define ARP_TIMEOUT           CONFIG_ARP_TIMEOUT
+#endif
+
+
 #ifndef        CONFIG_NET_RETRY_COUNT
-# define ARP_TIMEOUT_COUNT     5               /* # of timeouts before giving up  */
+# define ARP_TIMEOUT_COUNT     5       /* # of timeouts before giving up  */
 #else
-# define ARP_TIMEOUT_COUNT  (CONFIG_NET_RETRY_COUNT)
+# define ARP_TIMEOUT_COUNT     CONFIG_NET_RETRY_COUNT
 #endif
 
 #if 0
@@ -129,9 +141,9 @@ uchar               NetOurEther[6];         /* Our ethernet address                 */
 uchar          NetServerEther[6] =     /* Boot server enet address             */
                        { 0, 0, 0, 0, 0, 0 };
 IPaddr_t       NetOurIP;               /* Our IP addr (0 = unknown)            */
-IPaddr_t       NetServerIP;            /* Our IP addr (0 = unknown)            */
-volatile uchar *NetRxPkt;              /* Current receive packet               */
-int            NetRxPktLen;            /* Current rx packet length             */
+IPaddr_t       NetServerIP;            /* Server IP addr (0 = unknown)         */
+volatile uchar *NetRxPacket;           /* Current receive packet               */
+int            NetRxPacketLen;         /* Current rx packet length             */
 unsigned       NetIPID;                /* IP packet ID                         */
 uchar          NetBcastAddr[6] =       /* Ethernet bcast address               */
                        { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
@@ -158,7 +170,7 @@ ushort              NetOurNativeVLAN = 0xFFFF;      /* ditto                        */
 char           BootFile[128];          /* Boot File name                       */
 
 #if defined(CONFIG_CMD_PING)
-IPaddr_t       NetPingIP;              /* the ip address to ping               */
+IPaddr_t       NetPingIP;              /* the ip address to ping               */
 
 static void PingStart(void);
 #endif
@@ -196,7 +208,7 @@ IPaddr_t    NetArpWaitReplyIP;
 uchar         *NetArpWaitPacketMAC;    /* MAC address of waiting packet's destination  */
 uchar         *NetArpWaitTxPacket;     /* THE transmit packet                  */
 int            NetArpWaitTxPacketSize;
-uchar          NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
+uchar          NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
 ulong          NetArpWaitTimerStart;
 int            NetArpWaitTry;
 
@@ -253,7 +265,7 @@ void ArpTimeoutCheck(void)
        t = get_timer(0);
 
        /* check for arp timeout */
-       if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
+       if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
                NetArpWaitTry++;
 
                if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
@@ -267,6 +279,30 @@ void ArpTimeoutCheck(void)
        }
 }
 
+static void
+NetInitLoop(proto_t protocol)
+{
+       static int env_changed_id = 0;
+       bd_t *bd = gd->bd;
+       int env_id = get_env_id ();
+
+       /* update only when the environment has changed */
+       if (env_changed_id != env_id) {
+               NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
+               NetOurGatewayIP = getenv_IPaddr ("gatewayip");
+               NetOurSubnetMask= getenv_IPaddr ("netmask");
+               NetServerIP = getenv_IPaddr ("serverip");
+               NetOurNativeVLAN = getenv_VLAN("nvlan");
+               NetOurVLAN = getenv_VLAN("vlan");
+#if defined(CONFIG_CMD_DNS)
+               NetOurDNSIP = getenv_IPaddr("dnsip");
+#endif
+               env_changed_id = env_id;
+       }
+
+       return;
+}
+
 /**********************************************************************/
 /*
  *     Main network processing loop.
@@ -321,7 +357,7 @@ restart:
 #ifdef CONFIG_NET_MULTI
        memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
 #else
-       memcpy (NetOurEther, bd->bi_enetaddr, 6);
+       eth_getenv_enetaddr("ethaddr", NetOurEther);
 #endif
 
        NetState = NETLOOP_CONTINUE;
@@ -331,65 +367,7 @@ restart:
         *      here on, this code is a state machine driven by received
         *      packets and timer events.
         */
-
-       switch (protocol) {
-#if defined(CONFIG_CMD_NFS)
-       case NFS:
-#endif
-#if defined(CONFIG_CMD_PING)
-       case PING:
-#endif
-#if defined(CONFIG_CMD_SNTP)
-       case SNTP:
-#endif
-       case NETCONS:
-       case TFTP:
-               NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
-               NetOurGatewayIP = getenv_IPaddr ("gatewayip");
-               NetOurSubnetMask= getenv_IPaddr ("netmask");
-               NetOurVLAN = getenv_VLAN("vlan");
-               NetOurNativeVLAN = getenv_VLAN("nvlan");
-
-               switch (protocol) {
-#if defined(CONFIG_CMD_NFS)
-               case NFS:
-#endif
-               case NETCONS:
-               case TFTP:
-                       NetServerIP = getenv_IPaddr ("serverip");
-                       break;
-#if defined(CONFIG_CMD_PING)
-               case PING:
-                       /* nothing */
-                       break;
-#endif
-#if defined(CONFIG_CMD_SNTP)
-               case SNTP:
-                       /* nothing */
-                       break;
-#endif
-               default:
-                       break;
-               }
-
-               break;
-       case BOOTP:
-       case RARP:
-               /*
-                * initialize our IP addr to 0 in order to accept ANY
-                * IP addr assigned to us by the BOOTP / RARP server
-                */
-               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");
-               break;
-       default:
-               break;
-       }
+       NetInitLoop(protocol);
 
        switch (net_check_prereq (protocol)) {
        case 1:
@@ -415,21 +393,21 @@ restart:
 
 #if defined(CONFIG_CMD_DHCP)
                case DHCP:
-                       /* Start with a clean slate... */
                        BootpTry = 0;
                        NetOurIP = 0;
-                       NetServerIP = getenv_IPaddr ("serverip");
                        DhcpRequest();          /* Basically same as BOOTP */
                        break;
 #endif
 
                case BOOTP:
                        BootpTry = 0;
+                       NetOurIP = 0;
                        BootpRequest ();
                        break;
 
                case RARP:
                        RarpTry = 0;
+                       NetOurIP = 0;
                        RarpRequest ();
                        break;
 #if defined(CONFIG_CMD_PING)
@@ -457,6 +435,11 @@ restart:
                        SntpStart();
                        break;
 #endif
+#if defined(CONFIG_CMD_DNS)
+               case DNS:
+                       DnsStart();
+                       break;
+#endif
                default:
                        break;
                }
@@ -466,16 +449,16 @@ restart:
        }
 
 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
-#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
+#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
        /*
         * Echo the inverted link state to the fault LED.
         */
-       if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
+       if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
                status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
        } else {
                status_led_set (STATUS_LED_RED, STATUS_LED_ON);
        }
-#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
+#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
 #endif /* CONFIG_MII, ... */
 
        /*
@@ -494,7 +477,7 @@ restart:
                 *      Check the ethernet for a new packet.  The ethernet
                 *      receive routine will process it.
                 */
-                       eth_rx();
+               eth_rx();
 
                /*
                 *      Abort if ctrl-c was pressed.
@@ -515,18 +498,18 @@ restart:
                        thand_f *x;
 
 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
-#  if defined(CFG_FAULT_ECHO_LINK_DOWN) && \
+#  if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
       defined(CONFIG_STATUS_LED) &&       \
       defined(STATUS_LED_RED)
                        /*
                         * Echo the inverted link state to the fault LED.
                         */
-                       if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
+                       if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
                                status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
                        } else {
                                status_led_set (STATUS_LED_RED, STATUS_LED_ON);
                        }
-#  endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
+#  endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
 #endif /* CONFIG_MII, ... */
                        x = timeHandler;
                        timeHandler = (thand_f *)0;
@@ -592,7 +575,7 @@ void NetStartAgain (void)
                return;
        }
 #ifndef CONFIG_NET_MULTI
-       NetSetTimeout (10UL * CFG_HZ, startAgainTimeout);
+       NetSetTimeout (10000UL, startAgainTimeout);
        NetSetHandler (startAgainHandler);
 #else  /* !CONFIG_NET_MULTI*/
        eth_halt ();
@@ -603,7 +586,7 @@ void NetStartAgain (void)
        if (NetRestartWrap) {
                NetRestartWrap = 0;
                if (NetDevExists && !once) {
-                       NetSetTimeout (10UL * CFG_HZ, startAgainTimeout);
+                       NetSetTimeout (10000UL, startAgainTimeout);
                        NetSetHandler (startAgainHandler);
                } else {
                        NetState = NETLOOP_FAIL;
@@ -684,8 +667,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
        }
 
 #ifdef ET_DEBUG
-       printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
-               dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
+       printf("sending UDP to %08lx/%pM\n", dest, ether);
 #endif
 
        pkt = (uchar *)NetTxPacket;
@@ -729,7 +711,7 @@ int PingSend(void)
        ip->ip_tos   = 0;
        ip->ip_len   = htons(IP_HDR_SIZE_NO_UDP + 8);
        ip->ip_id    = htons(NetIPID++);
-       ip->ip_off   = htons(0x4000);   /* No fragmentation */
+       ip->ip_off   = htons(IP_FLAGS_DFRAG);   /* Don't fragment */
        ip->ip_ttl   = 255;
        ip->ip_p     = 0x01;            /* ICMP */
        ip->ip_sum   = 0;
@@ -740,7 +722,7 @@ int PingSend(void)
        s = &ip->udp_src;               /* XXX ICMP starts here */
        s[0] = htons(0x0800);           /* echo-request, code */
        s[1] = 0;                       /* checksum */
-       s[2] = 0;                       /* identifier */
+       s[2] = 0;                       /* identifier */
        s[3] = htons(PingSeqNo++);      /* sequence number */
        s[1] = ~NetCksum((uchar *)s, 8/2);
 
@@ -779,7 +761,7 @@ static void PingStart(void)
 #if defined(CONFIG_NET_MULTI)
        printf ("Using %s device\n", eth_get_name());
 #endif /* CONFIG_NET_MULTI */
-       NetSetTimeout (10UL * CFG_HZ, PingTimeout);
+       NetSetTimeout (10000UL, PingTimeout);
        NetSetHandler (PingHandler);
 
        PingSend();
@@ -802,7 +784,7 @@ static void PingStart(void)
 #define CDP_SYSOBJECT_TLV              0x0015
 #define CDP_MANAGEMENT_ADDRESS_TLV     0x0016
 
-#define CDP_TIMEOUT                    (CFG_HZ/4)      /* one packet every 250ms */
+#define CDP_TIMEOUT                    250UL   /* one packet every 250ms */
 
 static int CDPSeq;
 static int CDPOK;
@@ -906,11 +888,7 @@ int CDPSendTrigger(void)
 #ifdef CONFIG_CDP_DEVICE_ID
        *s++ = htons(CDP_DEVICE_ID_TLV);
        *s++ = htons(CONFIG_CDP_DEVICE_ID);
-       memset(buf, 0, sizeof(buf));
-       sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
-               NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
-               NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
-               NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
+       sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
        memcpy((uchar *)s, buf, 16);
        s += 16 / 2;
 #endif
@@ -1158,8 +1136,8 @@ NetReceive(volatile uchar * inpkt, int len)
        printf("packet received\n");
 #endif
 
-       NetRxPkt = inpkt;
-       NetRxPktLen = len;
+       NetRxPacket = inpkt;
+       NetRxPacketLen = len;
        et = (Ethernet_t *)inpkt;
 
        /* too small packet? */
@@ -1309,11 +1287,18 @@ NetReceive(volatile uchar * inpkt, int len)
                        /* are we waiting for a reply */
                        if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
                                break;
+
+#ifdef CONFIG_KEEP_SERVERADDR
+                       if (NetServerIP == NetArpWaitPacketIP) {
+                               char buf[20];
+                               sprintf(buf, "%pM", arp->ar_data);
+                               setenv("serveraddr", buf);
+                       }
+#endif
+
 #ifdef ET_DEBUG
-                       printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
-                               arp->ar_data[0], arp->ar_data[1],
-                               arp->ar_data[2], arp->ar_data[3],
-                               arp->ar_data[4], arp->ar_data[5]);
+                       printf("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
+                               arp->ar_data);
 #endif
 
                        tmp = NetReadIP(&arp->ar_data[6]);
@@ -1379,7 +1364,7 @@ NetReceive(volatile uchar * inpkt, int len)
                puts ("Got IP\n");
 #endif
                if (len < IP_HDR_SIZE) {
-                       debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
+                       debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
                        return;
                }
                if (len < ntohs(ip->ip_len)) {
@@ -1393,7 +1378,12 @@ NetReceive(volatile uchar * inpkt, int len)
                if ((ip->ip_hl_v & 0xf0) != 0x40) {
                        return;
                }
-               if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
+               /* Can't deal with fragments */
+               if (ip->ip_off & htons(IP_OFFS | IP_FLAGS_MFRAG)) {
+                       return;
+               }
+               /* can't deal with headers > 20 bytes */
+               if ((ip->ip_hl_v & 0x0f) > 0x05) {
                        return;
                }
                if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
@@ -1431,9 +1421,7 @@ NetReceive(volatile uchar * inpkt, int len)
                        case ICMP_REDIRECT:
                                if (icmph->code != ICMP_REDIR_HOST)
                                        return;
-                               puts (" ICMP Host Redirect to ");
-                               print_IPaddr(icmph->un.gateway);
-                               putc(' ');
+                               printf (" ICMP Host Redirect to %pI4 ", &icmph->un.gateway);
                                return;
 #if defined(CONFIG_CMD_PING)
                        case ICMP_ECHO_REPLY:
@@ -1505,7 +1493,8 @@ NetReceive(volatile uchar * inpkt, int len)
                                xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
                        }
                        if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
-                               printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum));
+                               printf(" UDP wrong checksum %08lx %08x\n",
+                                       xsum, ntohs(ip->udp_xsum));
                                return;
                        }
                }
@@ -1552,6 +1541,14 @@ static int net_check_prereq (proto_t protocol)
                }
                goto common;
 #endif
+#if defined(CONFIG_CMD_DNS)
+       case DNS:
+               if (NetOurDNSIP == 0) {
+                       puts("*** ERROR: DNS server address not given\n");
+                       return 1;
+               }
+               goto common;
+#endif
 #if defined(CONFIG_CMD_NFS)
        case NFS:
 #endif
@@ -1669,7 +1666,7 @@ NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
 void
 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
 {
-       volatile IP_t *ip = (IP_t *)xip;
+       IP_t *ip = (IP_t *)xip;
 
        /*
         *      If the data is an odd number of bytes, zero the
@@ -1687,7 +1684,7 @@ NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
        ip->ip_tos   = 0;
        ip->ip_len   = htons(IP_HDR_SIZE + len);
        ip->ip_id    = htons(NetIPID++);
-       ip->ip_off   = htons(0x4000);   /* No fragmentation */
+       ip->ip_off   = htons(IP_FLAGS_DFRAG);   /* Don't fragment */
        ip->ip_ttl   = 255;
        ip->ip_p     = 17;              /* UDP */
        ip->ip_sum   = 0;
@@ -1715,6 +1712,16 @@ void copy_filename (char *dst, char *src, int size)
 
 #endif
 
+#if defined(CONFIG_CMD_NFS) || defined(CONFIG_CMD_SNTP) || defined(CONFIG_CMD_DNS)
+/*
+ * make port a little random, but use something trivial to compute
+ */
+unsigned int random_port(void)
+{
+       return 1024 + (get_timer(0) % 0x8000);;
+}
+#endif
+
 void ip_to_string (IPaddr_t x, char *s)
 {
        x = ntohl (x);
@@ -1774,15 +1781,6 @@ ushort string_to_VLAN(char *s)
        return htons(id);
 }
 
-void print_IPaddr (IPaddr_t x)
-{
-       char tmp[16];
-
-       ip_to_string (x, tmp);
-
-       puts (tmp);
-}
-
 IPaddr_t getenv_IPaddr (char *var)
 {
        return (string_to_ip(getenv(var)));