net: cosmetic: Rename "x" to "eth_proto"
[platform/kernel/u-boot.git] / net / net.c
index ae72746..67921c3 100644 (file)
--- a/net/net.c
+++ b/net/net.c
@@ -82,9 +82,7 @@
 #include "arp.h"
 #include "bootp.h"
 #include "tftp.h"
-#ifdef CONFIG_CMD_RARP
 #include "rarp.h"
-#endif
 #include "nfs.h"
 #ifdef CONFIG_STATUS_LED
 #include <status_led.h>
@@ -97,6 +95,7 @@
 #if defined(CONFIG_CMD_DNS)
 #include "dns.h"
 #endif
+#include "ping.h"
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -167,13 +166,6 @@ ushort             NetOurNativeVLAN = 0xFFFF;
 /* Boot File name */
 char           BootFile[128];
 
-#if defined(CONFIG_CMD_PING)
-/* the ip address to ping */
-IPaddr_t       NetPingIP;
-
-static void PingStart(void);
-#endif
-
 #if defined(CONFIG_CMD_SNTP)
 /* NTP server IP address */
 IPaddr_t       NetNtpServerIP;
@@ -356,7 +348,7 @@ restart:
 #endif
 #if defined(CONFIG_CMD_PING)
                case PING:
-                       PingStart();
+                       ping_start();
                        break;
 #endif
 #if defined(CONFIG_CMD_NFS)
@@ -599,8 +591,8 @@ NetSendPacket(uchar *pkt, int len)
        (void) eth_send(pkt, len);
 }
 
-int
-NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
+int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
+               int payload_len)
 {
        uchar *pkt;
 
@@ -626,13 +618,14 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
                pkt = NetArpWaitTxPacket;
                pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
 
-               NetSetIP(pkt, dest, dport, sport, len);
-               memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
-                      (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
+               NetSetIP(pkt, dest, dport, sport, payload_len);
+               memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket +
+                      (pkt - (uchar *)NetArpWaitTxPacket) +
+                      IP_UDP_HDR_SIZE, payload_len);
 
                /* size of the waiting packet */
                NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
-                       IP_HDR_SIZE + len;
+                       IP_UDP_HDR_SIZE + payload_len;
 
                /* and do the ARP request */
                NetArpWaitTry = 1;
@@ -645,100 +638,13 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
 
        pkt = (uchar *)NetTxPacket;
        pkt += NetSetEther(pkt, ether, PROT_IP);
-       NetSetIP(pkt, dest, dport, sport, len);
-       (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
+       NetSetIP(pkt, dest, dport, sport, payload_len);
+       eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE +
+               payload_len);
 
        return 0;       /* transmitted */
 }
 
-#if defined(CONFIG_CMD_PING)
-static ushort PingSeqNo;
-
-int PingSend(void)
-{
-       static uchar mac[6];
-       IP_t *ip;
-       ushort *s;
-       uchar *pkt;
-
-       /* XXX always send arp request */
-
-       memcpy(mac, NetEtherNullAddr, 6);
-
-       debug("sending ARP for %08x\n", NetPingIP);
-
-       NetArpWaitPacketIP = NetPingIP;
-       NetArpWaitPacketMAC = mac;
-
-       pkt = NetArpWaitTxPacket;
-       pkt += NetSetEther(pkt, mac, PROT_IP);
-
-       ip = (IP_t *)pkt;
-
-       /*
-        * Construct an IP and ICMP header.
-        * (need to set no fragment bit - XXX)
-        */
-       /* IP_HDR_SIZE / 4 (not including UDP) */
-       ip->ip_hl_v  = 0x45;
-       ip->ip_tos   = 0;
-       ip->ip_len   = htons(IP_HDR_SIZE_NO_UDP + 8);
-       ip->ip_id    = htons(NetIPID++);
-       ip->ip_off   = htons(IP_FLAGS_DFRAG);   /* Don't fragment */
-       ip->ip_ttl   = 255;
-       ip->ip_p     = 0x01;            /* ICMP */
-       ip->ip_sum   = 0;
-       /* already in network byte order */
-       NetCopyIP((void *)&ip->ip_src, &NetOurIP);
-       /* - "" - */
-       NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
-       ip->ip_sum   = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
-
-       s = &ip->udp_src;               /* XXX ICMP starts here */
-       s[0] = htons(0x0800);           /* echo-request, code */
-       s[1] = 0;                       /* checksum */
-       s[2] = 0;                       /* identifier */
-       s[3] = htons(PingSeqNo++);      /* sequence number */
-       s[1] = ~NetCksum((uchar *)s, 8/2);
-
-       /* size of the waiting packet */
-       NetArpWaitTxPacketSize =
-               (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
-
-       /* and do the ARP request */
-       NetArpWaitTry = 1;
-       NetArpWaitTimerStart = get_timer(0);
-       ArpRequest();
-       return 1;       /* waiting */
-}
-
-static void
-PingTimeout(void)
-{
-       eth_halt();
-       NetState = NETLOOP_FAIL;        /* we did not get the reply */
-}
-
-static void
-PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
-           unsigned len)
-{
-       if (sip != NetPingIP)
-               return;
-
-       NetState = NETLOOP_SUCCESS;
-}
-
-static void PingStart(void)
-{
-       printf("Using %s device\n", eth_get_name());
-       NetSetTimeout(10000UL, PingTimeout);
-       NetSetHandler(PingHandler);
-
-       PingSend();
-}
-#endif
-
 #ifdef CONFIG_IP_DEFRAG
 /*
  * This function collects fragments in a single packet, according
@@ -758,7 +664,7 @@ static void PingStart(void)
 static struct rpc_t rpc_specimen;
 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
 
-#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
+#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
 
 /*
  * this is the packet being assembled, either data or frag control.
@@ -772,22 +678,22 @@ struct hole {
        u16 unused;
 };
 
-static IP_t *__NetDefragment(IP_t *ip, int *lenp)
+static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
 {
        static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
        static u16 first_hole, total_len;
        struct hole *payload, *thisfrag, *h, *newh;
-       IP_t *localip = (IP_t *)pkt_buff;
+       struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
        uchar *indata = (uchar *)ip;
        int offset8, start, len, done = 0;
        u16 ip_off = ntohs(ip->ip_off);
 
        /* payload starts after IP header, this fragment is in there */
-       payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
+       payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
        offset8 =  (ip_off & IP_OFFS);
        thisfrag = payload + offset8;
        start = offset8 * 8;
-       len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
+       len = ntohs(ip->ip_len) - IP_HDR_SIZE;
 
        if (start + len > IP_MAXUDP) /* fragment extends too far */
                return NULL;
@@ -800,7 +706,7 @@ static IP_t *__NetDefragment(IP_t *ip, int *lenp)
                payload[0].prev_hole = 0;
                first_hole = 0;
                /* any IP header will work, copy the first we received */
-               memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
+               memcpy(localip, ip, IP_HDR_SIZE);
        }
 
        /*
@@ -883,16 +789,16 @@ static IP_t *__NetDefragment(IP_t *ip, int *lenp)
        }
 
        /* finally copy this fragment and possibly return whole packet */
-       memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
+       memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
        if (!done)
                return NULL;
 
        localip->ip_len = htons(total_len);
-       *lenp = total_len + IP_HDR_SIZE_NO_UDP;
+       *lenp = total_len + IP_HDR_SIZE;
        return localip;
 }
 
-static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
+static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
 {
        u16 ip_off = ntohs(ip->ip_off);
        if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
@@ -902,7 +808,7 @@ static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
 
 #else /* !CONFIG_IP_DEFRAG */
 
-static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
+static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
 {
        u16 ip_off = ntohs(ip->ip_off);
        if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
@@ -917,9 +823,10 @@ static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
  *
  * @parma ip   IP packet containing the ICMP
  */
-static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et)
+static void receive_icmp(struct ip_udp_hdr *ip, int len,
+                       IPaddr_t src_ip, struct ethernet_hdr *et)
 {
-       ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
+       struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
 
        switch (icmph->type) {
        case ICMP_REDIRECT:
@@ -928,41 +835,10 @@ static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et)
                printf(" ICMP Host Redirect to %pI4 ",
                        &icmph->un.gateway);
                break;
+       default:
 #if defined(CONFIG_CMD_PING)
-       case ICMP_ECHO_REPLY:
-               /*
-                       * IP header OK.  Pass the packet to the
-                       * current handler.
-                       */
-               /*
-                * XXX point to ip packet - should this use
-                * packet_icmp_handler?
-                */
-               (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
-               break;
-       case ICMP_ECHO_REQUEST:
-               debug("Got ICMP ECHO REQUEST, return %d bytes\n",
-                       ETHER_HDR_SIZE + len);
-
-               memcpy(&et->et_dest[0], &et->et_src[0], 6);
-               memcpy(&et->et_src[0], NetOurEther, 6);
-
-               ip->ip_sum = 0;
-               ip->ip_off = 0;
-               NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
-               NetCopyIP((void *)&ip->ip_src, &NetOurIP);
-               ip->ip_sum = ~NetCksum((uchar *)ip,
-                                       IP_HDR_SIZE_NO_UDP >> 1);
-
-               icmph->type = ICMP_ECHO_REPLY;
-               icmph->checksum = 0;
-               icmph->checksum = ~NetCksum((uchar *)icmph,
-                       (len - IP_HDR_SIZE_NO_UDP) >> 1);
-               (void) eth_send((uchar *)et,
-                               ETHER_HDR_SIZE + len);
-               break;
+               ping_receive(et, ip, len);
 #endif
-       default:
 #ifdef CONFIG_CMD_TFTPPUT
                if (packet_icmp_handler)
                        packet_icmp_handler(icmph->type, icmph->code,
@@ -976,14 +852,11 @@ static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et)
 void
 NetReceive(uchar *inpkt, int len)
 {
-       Ethernet_t *et;
-       IP_t    *ip;
-#ifdef CONFIG_CMD_RARP
-       ARP_t   *arp;
-#endif
-       IPaddr_t tmp;
+       struct ethernet_hdr *et;
+       struct ip_udp_hdr *ip;
+       IPaddr_t dst_ip;
        IPaddr_t src_ip;
-       int     x;
+       int eth_proto;
 #if defined(CONFIG_CMD_CDP)
        int iscdp;
 #endif
@@ -993,7 +866,7 @@ NetReceive(uchar *inpkt, int len)
 
        NetRxPacket = inpkt;
        NetRxPacketLen = len;
-       et = (Ethernet_t *)inpkt;
+       et = (struct ethernet_hdr *)inpkt;
 
        /* too small packet? */
        if (len < ETHER_HDR_SIZE)
@@ -1018,25 +891,28 @@ NetReceive(uchar *inpkt, int len)
        if (mynvlanid == (ushort)-1)
                mynvlanid = VLAN_NONE;
 
-       x = ntohs(et->et_protlen);
+       eth_proto = ntohs(et->et_protlen);
 
        debug("packet received\n");
 
-       if (x < 1514) {
+       if (eth_proto < 1514) {
+               struct e802_hdr *et802 = (struct e802_hdr *)et;
                /*
-                *      Got a 802 packet.  Check the other protocol field.
+                *      Got a 802.2 packet.  Check the other protocol field.
+                *      XXX VLAN over 802.2+SNAP not implemented!
                 */
-               x = ntohs(et->et_prot);
+               eth_proto = ntohs(et802->et_prot);
 
-               ip = (IP_t *)(inpkt + E802_HDR_SIZE);
+               ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
                len -= E802_HDR_SIZE;
 
-       } else if (x != PROT_VLAN) {    /* normal packet */
-               ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
+       } else if (eth_proto != PROT_VLAN) {    /* normal packet */
+               ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
                len -= ETHER_HDR_SIZE;
 
        } else {                        /* VLAN packet */
-               VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
+               struct vlan_ethernet_hdr *vet =
+                       (struct vlan_ethernet_hdr *)et;
 
                debug("VLAN packet received\n");
 
@@ -1054,13 +930,13 @@ NetReceive(uchar *inpkt, int len)
 
                cti = ntohs(vet->vet_tag);
                vlanid = cti & VLAN_IDMASK;
-               x = ntohs(vet->vet_type);
+               eth_proto = ntohs(vet->vet_type);
 
-               ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
+               ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
                len -= VLAN_ETHER_HDR_SIZE;
        }
 
-       debug("Receive from protocol 0x%x\n", x);
+       debug("Receive from protocol 0x%x\n", eth_proto);
 
 #if defined(CONFIG_CMD_CDP)
        if (iscdp) {
@@ -1077,7 +953,7 @@ NetReceive(uchar *inpkt, int len)
                        return;
        }
 
-       switch (x) {
+       switch (eth_proto) {
 
        case PROT_ARP:
                ArpReceive(et, ip, len);
@@ -1085,34 +961,15 @@ NetReceive(uchar *inpkt, int len)
 
 #ifdef CONFIG_CMD_RARP
        case PROT_RARP:
-               debug("Got RARP\n");
-               arp = (ARP_t *)ip;
-               if (len < ARP_HDR_SIZE) {
-                       printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
-                       return;
-               }
-
-               if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
-                       (ntohs(arp->ar_hrd) != ARP_ETHER)   ||
-                       (ntohs(arp->ar_pro) != PROT_IP)     ||
-                       (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
-
-                       puts("invalid RARP header\n");
-               } else {
-                       NetCopyIP(&NetOurIP, &arp->ar_data[16]);
-                       if (NetServerIP == 0)
-                               NetCopyIP(&NetServerIP, &arp->ar_data[6]);
-                       memcpy(NetServerEther, &arp->ar_data[0], 6);
-
-                       (*packetHandler)(0, 0, 0, 0, 0);
-               }
+               rarp_receive(ip, len);
                break;
 #endif
        case PROT_IP:
                debug("Got IP\n");
                /* Before we start poking the header, make sure it is there */
-               if (len < IP_HDR_SIZE) {
-                       debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
+               if (len < IP_UDP_HDR_SIZE) {
+                       debug("len bad %d < %lu\n", len,
+                               (ulong)IP_UDP_HDR_SIZE);
                        return;
                }
                /* Check the packet length */
@@ -1130,15 +987,15 @@ NetReceive(uchar *inpkt, int len)
                if ((ip->ip_hl_v & 0x0f) > 0x05)
                        return;
                /* Check the Checksum of the header */
-               if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
+               if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
                        puts("checksum bad\n");
                        return;
                }
                /* If it is not for us, ignore it */
-               tmp = NetReadIP(&ip->ip_dst);
-               if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
+               dst_ip = NetReadIP(&ip->ip_dst);
+               if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
 #ifdef CONFIG_MCAST_TFTP
-                       if (Mcast_addr != tmp)
+                       if (Mcast_addr != dst_ip)
 #endif
                                return;
                }
@@ -1224,19 +1081,19 @@ NetReceive(uchar *inpkt, int len)
 
 
 #ifdef CONFIG_NETCONSOLE
-               nc_input_packet((uchar *)ip + IP_HDR_SIZE,
-                                               ntohs(ip->udp_dst),
-                                               ntohs(ip->udp_src),
-                                               ntohs(ip->udp_len) - 8);
+               nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
+                                       ntohs(ip->udp_dst),
+                                       ntohs(ip->udp_src),
+                                       ntohs(ip->udp_len) - UDP_HDR_SIZE);
 #endif
                /*
                 *      IP header OK.  Pass the packet to the current handler.
                 */
-               (*packetHandler)((uchar *)ip + IP_HDR_SIZE,
-                                               ntohs(ip->udp_dst),
-                                               src_ip,
-                                               ntohs(ip->udp_src),
-                                               ntohs(ip->udp_len) - 8);
+               (*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE,
+                                       ntohs(ip->udp_dst),
+                                       src_ip,
+                                       ntohs(ip->udp_src),
+                                       ntohs(ip->udp_len) - UDP_HDR_SIZE);
                break;
        }
 }
@@ -1365,7 +1222,7 @@ NetEthHdrSize(void)
 int
 NetSetEther(uchar *xet, uchar * addr, uint prot)
 {
-       Ethernet_t *et = (Ethernet_t *)xet;
+       struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
        ushort myvlanid;
 
        myvlanid = ntohs(NetOurVLAN);
@@ -1378,7 +1235,8 @@ NetSetEther(uchar *xet, uchar * addr, uint prot)
                et->et_protlen = htons(prot);
                return ETHER_HDR_SIZE;
        } else {
-               VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
+               struct vlan_ethernet_hdr *vet =
+                       (struct vlan_ethernet_hdr *)xet;
 
                vet->vet_vlan_type = htons(PROT_VLAN);
                vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
@@ -1387,10 +1245,9 @@ NetSetEther(uchar *xet, uchar * addr, uint prot)
        }
 }
 
-void
-NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
+void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
 {
-       IP_t *ip = (IP_t *)xip;
+       struct ip_udp_hdr *ip = (struct ip_udp_hdr *)xip;
 
        /*
         *      If the data is an odd number of bytes, zero the
@@ -1398,7 +1255,7 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
         *      will work.
         */
        if (len & 1)
-               xip[IP_HDR_SIZE + len] = 0;
+               xip[IP_UDP_HDR_SIZE + len] = 0;
 
        /*
         *      Construct an IP and UDP header.
@@ -1407,7 +1264,7 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
        /* IP_HDR_SIZE / 4 (not including UDP) */
        ip->ip_hl_v  = 0x45;
        ip->ip_tos   = 0;
-       ip->ip_len   = htons(IP_HDR_SIZE + len);
+       ip->ip_len   = htons(IP_UDP_HDR_SIZE + len);
        ip->ip_id    = htons(NetIPID++);
        ip->ip_off   = htons(IP_FLAGS_DFRAG);   /* Don't fragment */
        ip->ip_ttl   = 255;
@@ -1419,9 +1276,9 @@ NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
        NetCopyIP((void *)&ip->ip_dst, &dest);
        ip->udp_src  = htons(sport);
        ip->udp_dst  = htons(dport);
-       ip->udp_len  = htons(8 + len);
+       ip->udp_len  = htons(UDP_HDR_SIZE + len);
        ip->udp_xsum = 0;
-       ip->ip_sum   = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
+       ip->ip_sum   = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
 }
 
 void copy_filename(char *dst, const char *src, int size)