usb ehci driver: Get rid of echi_alloc/free
[barebox-mini2440.git] / net / net.c
blob49250141ecbc81fa7b80f46bd216b2a6c7b53164
1 /*
2 * Copied from Linux Monitor (LiMon) - Networking.
4 * Copyright 1994 - 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 */
12 * General Desription:
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
18 * BOOTP:
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
23 * - name of bootfile
24 * Next step: ARP
26 * RARP:
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
31 * Next step: ARP
33 * ARP:
35 * Prerequisites: - own ethernet address
36 * - own IP address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
39 * Next step: TFTP
41 * DHCP:
43 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
46 * Next step: - TFTP
48 * TFTP:
50 * Prerequisites: - own ethernet address
51 * - own IP address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
57 * Next step: none
59 * NFS:
61 * Prerequisites: - own ethernet address
62 * - own IP address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
66 * Next step: none
68 * SNTP:
70 * Prerequisites: - own ethernet address
71 * - own IP address
72 * We want: - network time
73 * Next step: none
77 #include <common.h>
78 #include <clock.h>
79 #include <watchdog.h>
80 #include <command.h>
81 #include <environment.h>
82 #include <param.h>
83 #include <net.h>
84 #include <driver.h>
85 #include <errno.h>
86 #include <linux/ctype.h>
87 #include "bootp.h"
88 #include "tftp.h"
89 #include "rarp.h"
90 #include "nfs.h"
91 #ifdef CONFIG_STATUS_LED
92 #include <status_led.h>
93 #include <miiphy.h>
94 #endif
95 #ifdef CONFIG_NET_SNTP
96 #include "sntp.h"
97 #endif
99 #define ARP_TIMEOUT (5 * SECOND) /* Seconds before trying ARP again */
100 #ifndef CONFIG_NET_RETRY_COUNT
101 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
102 #else
103 # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
104 #endif
107 /** BOOTP EXTENTIONS **/
109 IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
110 IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
111 IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
112 #ifdef CONFIG_BOOTP_DNS2
113 IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
114 #endif
115 char NetOurNISDomain[32]={0,}; /* Our NIS domain */
116 char NetOurHostName[32]={0,}; /* Our hostname */
117 char NetOurRootPath[64]={0,}; /* Our bootpath */
118 ushort NetBootFileSize=0; /* Our bootfile size in blocks */
120 /** END OF BOOTP EXTENTIONS **/
122 ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
123 uchar NetOurEther[6]; /* Our ethernet address */
124 uchar NetServerEther[6] = /* Boot server enet address */
125 { 0, 0, 0, 0, 0, 0 };
126 IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
127 IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
128 uchar *NetRxPkt; /* Current receive packet */
129 int NetRxPktLen; /* Current rx packet length */
130 unsigned NetIPID; /* IP packet ID */
131 uchar NetBcastAddr[6] = /* Ethernet bcast address */
132 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
133 uchar NetEtherNullAddr[6] =
134 { 0, 0, 0, 0, 0, 0 };
135 int NetState; /* Network loop state */
137 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
138 ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
139 ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
141 char BootFile[128]; /* Boot File name */
143 #ifdef CONFIG_NET_PING
144 IPaddr_t NetPingIP; /* the ip address to ping */
146 extern void PingStart(void);
147 #endif
149 #ifdef CONFIG_NET_SNTP
150 IPaddr_t NetNtpServerIP; /* NTP server IP address */
151 int NetTimeOffset=0; /* offset time from UTC */
152 #endif
154 #ifdef CONFIG_NETCONSOLE
155 void NcStart(void);
156 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
157 #endif
159 uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
161 uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
163 static rxhand_f *packetHandler; /* Current RX packet handler */
164 static thand_f *timeHandler; /* Current timeout handler */
165 static uint64_t timeStart; /* Time base value */
166 static uint64_t timeDelta; /* Current timeout value */
167 uchar *NetTxPacket = 0; /* THE transmit packet */
169 static int net_check_prereq (proto_t protocol);
171 /**********************************************************************/
173 IPaddr_t NetArpWaitPacketIP;
174 IPaddr_t NetArpWaitReplyIP;
175 uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
176 uchar *NetArpWaitTxPacket; /* THE transmit packet */
177 int NetArpWaitTxPacketSize;
178 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
179 uint64_t NetArpWaitTimerStart;
180 int NetArpWaitTry;
182 void ArpRequest (void)
184 int i;
185 uchar *pkt;
186 ARP_t *arp;
188 #ifdef ET_DEBUG
189 printf ("ARP broadcast %d\n", NetArpWaitTry);
190 #endif
191 pkt = NetTxPacket;
193 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
195 arp = (ARP_t *) pkt;
197 arp->ar_hrd = htons (ARP_ETHER);
198 arp->ar_pro = htons (PROT_IP);
199 arp->ar_hln = 6;
200 arp->ar_pln = 4;
201 arp->ar_op = htons (ARPOP_REQUEST);
203 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
204 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
205 for (i = 10; i < 16; ++i) {
206 arp->ar_data[i] = 0; /* dest ET addr = 0 */
209 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
210 (NetOurIP & NetOurSubnetMask)) {
211 if (NetOurGatewayIP == 0) {
212 puts ("## Warning: gatewayip needed but not set\n");
213 NetArpWaitReplyIP = NetArpWaitPacketIP;
214 } else {
215 NetArpWaitReplyIP = NetOurGatewayIP;
217 } else {
218 NetArpWaitReplyIP = NetArpWaitPacketIP;
221 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
222 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
225 void ArpTimeoutCheck(void)
227 if (!NetArpWaitPacketIP)
228 return;
230 /* check for arp timeout */
231 if (is_timeout(NetArpWaitTimerStart, ARP_TIMEOUT)) {
232 NetArpWaitTry++;
234 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
235 puts ("\nARP Retry count exceeded; starting again\n");
236 NetArpWaitTry = 0;
237 NetStartAgain();
238 } else {
239 NetArpWaitTimerStart = get_time_ns();
240 ArpRequest();
245 /**********************************************************************/
247 * Main network processing loop.
251 NetLoop(proto_t protocol)
253 struct eth_device *eth_current = eth_get_current();
254 IPaddr_t ip;
256 if (!eth_current) {
257 printf("Current ethernet device not set!\n");
258 return -1;
261 ip = dev_get_param_ip(&eth_current->dev, "ipaddr");
262 NetCopyIP(&NetOurIP, &ip);
264 /* XXX problem with bss workaround */
265 NetArpWaitPacketMAC = NULL;
266 NetArpWaitTxPacket = NULL;
267 NetArpWaitPacketIP = 0;
268 NetArpWaitReplyIP = 0;
269 NetArpWaitTxPacket = NULL;
270 NetTxPacket = NULL;
272 if (!NetTxPacket) {
273 int i;
275 * Setup packet buffers, aligned correctly.
277 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
278 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
279 for (i = 0; i < PKTBUFSRX; i++) {
280 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
284 if (!NetArpWaitTxPacket) {
285 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
286 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
287 NetArpWaitTxPacketSize = 0;
290 if (eth_open() < 0)
291 return -1;
293 restart:
294 string_to_ethaddr(dev_get_param(&eth_get_current()->dev, "ethaddr"),
295 NetOurEther);
297 NetState = NETLOOP_CONTINUE;
299 NetOurGatewayIP = dev_get_param_ip(&eth_current->dev, "gateway");
300 NetOurSubnetMask = dev_get_param_ip(&eth_current->dev, "netmask");
301 NetOurVLAN = getenv_VLAN("vlan");
302 NetOurNativeVLAN = getenv_VLAN("nvlan");
303 NetServerIP = dev_get_param_ip(&eth_current->dev, "serverip");
306 * Start the ball rolling with the given start function. From
307 * here on, this code is a state machine driven by received
308 * packets and timer events.
311 switch (net_check_prereq (protocol)) {
312 case 1:
313 /* network not configured */
314 eth_halt();
315 return -1;
316 case 0:
317 switch (protocol) {
318 #ifdef CONFIG_NET_TFTP
319 case TFTP:
320 /* always use ARP to get server ethernet address */
321 TftpStart();
322 break;
323 #endif
324 #ifdef CONFIG_NET_DHCP
325 case DHCP:
326 /* Start with a clean slate... */
327 BootpTry = 0;
328 NetOurIP = 0;
329 DhcpRequest(); /* Basically same as BOOTP */
330 break;
331 #endif
332 #ifdef CONFIG_NET_BOOTP
333 case BOOTP:
334 BootpTry = 0;
335 BootpRequest ();
336 break;
337 #endif
338 #ifdef CONFIG_NET_RARP
339 case RARP:
340 NetOurIP = 0;
341 RarpTry = 0;
342 RarpRequest ();
343 break;
344 #endif
345 #ifdef CONFIG_NET_PING
346 case PING:
347 PingStart();
348 break;
349 #endif
350 #ifdef CONFIG_NET_NFS
351 case NFS:
352 NfsStart();
353 break;
354 #endif
355 #ifdef CONFIG_NETCONSOLE
356 case NETCONS:
357 NcStart();
358 break;
359 #endif
360 #ifdef CONFIG_NET_SNTP
361 case SNTP:
362 SntpStart();
363 break;
364 #endif
365 default:
366 break;
369 NetBootFileXferSize = 0;
370 break;
374 * Main packet reception loop. Loop receiving packets until
375 * someone sets `NetState' to a state that terminates.
377 for (;;) {
378 WATCHDOG_RESET();
379 #ifdef CONFIG_SHOW_ACTIVITY
381 extern void show_activity(int arg);
382 show_activity(1);
384 #endif
386 * Check the ethernet for a new packet. The ethernet
387 * receive routine will process it.
389 eth_rx();
392 * Abort if ctrl-c was pressed.
394 if (ctrlc()) {
395 eth_halt();
396 puts ("\nAbort\n");
397 return (-1);
400 ArpTimeoutCheck();
403 * Check for a timeout, and run the timeout handler
404 * if we have one.
406 if (timeHandler && is_timeout(timeStart, timeDelta)) {
407 thand_f *x;
408 x = timeHandler;
409 timeHandler = (thand_f *)0;
410 (*x)();
414 switch (NetState) {
416 case NETLOOP_RESTART:
417 goto restart;
419 case NETLOOP_SUCCESS:
420 if (NetBootFileXferSize > 0) {
421 char buf[10];
422 printf("Bytes transferred = %ld (%lx hex)\n",
423 NetBootFileXferSize,
424 NetBootFileXferSize);
425 sprintf(buf, "0x%lx", NetBootFileXferSize);
426 setenv("filesize", buf);
428 eth_halt();
429 return NetBootFileXferSize;
431 case NETLOOP_FAIL:
432 eth_halt();
433 return (-1);
438 /**********************************************************************/
440 static void
441 startAgainTimeout(void)
443 NetState = NETLOOP_RESTART;
446 static void
447 startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
449 /* Totally ignore the packet */
452 void NetStartAgain (void)
454 const char *nretry;
455 int noretry = 0, once = 0;
457 if ((nretry = getenv ("netretry")) != NULL) {
458 noretry = (strcmp (nretry, "no") == 0);
459 once = (strcmp (nretry, "once") == 0);
461 if (noretry) {
462 eth_halt ();
463 NetState = NETLOOP_FAIL;
464 return;
466 NetSetTimeout (10 * SECOND, startAgainTimeout);
467 NetSetHandler (startAgainHandler);
470 /**********************************************************************/
472 * Miscelaneous bits.
475 void
476 NetSetHandler(rxhand_f * f)
478 packetHandler = f;
482 void
483 NetSetTimeout(uint64_t iv, thand_f * f)
485 if (iv == 0) {
486 timeHandler = (thand_f *)0;
487 } else {
488 timeHandler = f;
489 timeStart = get_time_ns();
490 timeDelta = iv;
495 void
496 NetSendPacket(uchar * pkt, int len)
498 (void) eth_send(pkt, len);
502 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
504 uchar *pkt;
506 /* convert to new style broadcast */
507 if (dest == 0)
508 dest = 0xFFFFFFFF;
510 /* if broadcast, make the ether address a broadcast and don't do ARP */
511 if (dest == 0xFFFFFFFF)
512 ether = NetBcastAddr;
514 /* if MAC address was not discovered yet, save the packet and do an ARP request */
515 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
517 #ifdef ET_DEBUG
518 printf("sending ARP for %08lx\n", dest);
519 #endif
520 NetArpWaitPacketIP = dest;
521 NetArpWaitPacketMAC = ether;
523 pkt = NetArpWaitTxPacket;
524 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
526 NetSetIP (pkt, dest, dport, sport, len);
527 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
529 /* size of the waiting packet */
530 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
532 /* and do the ARP request */
533 NetArpWaitTry = 1;
534 NetArpWaitTimerStart = get_time_ns();
535 ArpRequest();
536 return 1; /* waiting */
539 #ifdef ET_DEBUG
540 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
541 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
542 #endif
544 pkt = (uchar *)NetTxPacket;
545 pkt += NetSetEther (pkt, ether, PROT_IP);
546 NetSetIP (pkt, dest, dport, sport, len);
547 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
549 return 0; /* transmitted */
552 void
553 NetReceive(uchar * inpkt, int len)
555 Ethernet_t *et;
556 IP_t *ip;
557 ARP_t *arp;
558 IPaddr_t tmp;
559 int x;
560 uchar *pkt;
561 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
563 #ifdef ET_DEBUG
564 printf("packet received\n");
565 #endif
567 NetRxPkt = inpkt;
568 NetRxPktLen = len;
569 et = (Ethernet_t *)inpkt;
571 /* too small packet? */
572 if (len < ETHER_HDR_SIZE)
573 return;
575 myvlanid = ntohs(NetOurVLAN);
576 if (myvlanid == (ushort)-1)
577 myvlanid = VLAN_NONE;
578 mynvlanid = ntohs(NetOurNativeVLAN);
579 if (mynvlanid == (ushort)-1)
580 mynvlanid = VLAN_NONE;
582 x = ntohs(et->et_protlen);
584 #ifdef ET_DEBUG
585 printf("packet received\n");
586 #endif
588 if (x < 1514) {
590 * Got a 802 packet. Check the other protocol field.
592 x = ntohs(et->et_prot);
594 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
595 len -= E802_HDR_SIZE;
597 } else if (x != PROT_VLAN) { /* normal packet */
598 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
599 len -= ETHER_HDR_SIZE;
601 } else { /* VLAN packet */
602 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
604 #ifdef ET_DEBUG
605 printf("VLAN packet received\n");
606 #endif
607 /* too small packet? */
608 if (len < VLAN_ETHER_HDR_SIZE)
609 return;
611 /* if no VLAN active */
612 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
614 return;
616 cti = ntohs(vet->vet_tag);
617 vlanid = cti & VLAN_IDMASK;
618 x = ntohs(vet->vet_type);
620 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
621 len -= VLAN_ETHER_HDR_SIZE;
624 #ifdef ET_DEBUG
625 printf("Receive from protocol 0x%x\n", x);
626 #endif
628 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
629 if (vlanid == VLAN_NONE)
630 vlanid = (mynvlanid & VLAN_IDMASK);
631 /* not matched? */
632 if (vlanid != (myvlanid & VLAN_IDMASK))
633 return;
636 switch (x) {
638 case PROT_ARP:
640 * We have to deal with two types of ARP packets:
641 * - REQUEST packets will be answered by sending our
642 * IP address - if we know it.
643 * - REPLY packets are expected only after we asked
644 * for the TFTP server's or the gateway's ethernet
645 * address; so if we receive such a packet, we set
646 * the server ethernet address
648 #ifdef ET_DEBUG
649 puts ("Got ARP\n");
650 #endif
651 arp = (ARP_t *)ip;
652 if (len < ARP_HDR_SIZE) {
653 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
654 return;
656 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
657 return;
659 if (ntohs(arp->ar_pro) != PROT_IP) {
660 return;
662 if (arp->ar_hln != 6) {
663 return;
665 if (arp->ar_pln != 4) {
666 return;
669 if (NetOurIP == 0) {
670 return;
673 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
674 return;
677 switch (ntohs(arp->ar_op)) {
678 case ARPOP_REQUEST: /* reply with our IP address */
679 #ifdef ET_DEBUG
680 puts ("Got ARP REQUEST, return our IP\n");
681 #endif
682 pkt = (uchar *)et;
683 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
684 arp->ar_op = htons(ARPOP_REPLY);
685 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
686 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
687 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
688 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
689 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
690 return;
692 case ARPOP_REPLY: /* arp reply */
693 /* are we waiting for a reply */
694 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
695 break;
696 #ifdef ET_DEBUG
697 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
698 arp->ar_data[0], arp->ar_data[1],
699 arp->ar_data[2], arp->ar_data[3],
700 arp->ar_data[4], arp->ar_data[5]);
701 #endif
703 tmp = NetReadIP(&arp->ar_data[6]);
705 /* matched waiting packet's address */
706 if (tmp == NetArpWaitReplyIP) {
707 #ifdef ET_DEBUG
708 puts ("Got it\n");
709 #endif
710 /* save address for later use */
711 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
713 #ifdef CONFIG_NETCONSOLE
714 (*packetHandler)(0,0,0,0);
715 #endif
716 /* modify header, and transmit it */
717 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
718 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
720 /* no arp request pending now */
721 NetArpWaitPacketIP = 0;
722 NetArpWaitTxPacketSize = 0;
723 NetArpWaitPacketMAC = NULL;
726 return;
727 default:
728 #ifdef ET_DEBUG
729 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
730 #endif
731 return;
733 break;
735 case PROT_RARP:
736 #ifdef ET_DEBUG
737 puts ("Got RARP\n");
738 #endif
739 arp = (ARP_t *)ip;
740 if (len < ARP_HDR_SIZE) {
741 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
742 return;
745 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
746 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
747 (ntohs(arp->ar_pro) != PROT_IP) ||
748 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
750 puts ("invalid RARP header\n");
751 } else {
752 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
753 if (NetServerIP == 0)
754 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
755 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
757 (*packetHandler)(0,0,0,0);
759 break;
761 case PROT_IP:
762 #ifdef ET_DEBUG
763 puts ("Got IP\n");
764 #endif
765 if (len < IP_HDR_SIZE) {
766 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
767 return;
769 if (len < ntohs(ip->ip_len)) {
770 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
771 return;
773 len = ntohs(ip->ip_len);
774 #ifdef ET_DEBUG
775 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
776 #endif
777 if ((ip->ip_hl_v & 0xf0) != 0x40) {
778 return;
780 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
781 return;
783 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
784 puts ("checksum bad\n");
785 return;
787 tmp = NetReadIP(&ip->ip_dst);
788 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
789 return;
792 * watch for ICMP host redirects
794 * There is no real handler code (yet). We just watch
795 * for ICMP host redirect messages. In case anybody
796 * sees these messages: please contact me
797 * (wd@denx.de), or - even better - send me the
798 * necessary fixes :-)
800 * Note: in all cases where I have seen this so far
801 * it was a problem with the router configuration,
802 * for instance when a router was configured in the
803 * BOOTP reply, but the TFTP server was on the same
804 * subnet. So this is probably a warning that your
805 * configuration might be wrong. But I'm not really
806 * sure if there aren't any other situations.
808 if (ip->ip_p == IPPROTO_ICMP) {
809 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
811 switch (icmph->type) {
812 case ICMP_REDIRECT:
813 if (icmph->code != ICMP_REDIR_HOST)
814 return;
815 puts (" ICMP Host Redirect to ");
816 print_IPaddr(icmph->un.gateway);
817 putchar(' ');
818 return;
819 #ifdef CONFIG_NET_PING
820 case ICMP_ECHO_REPLY:
822 * IP header OK. Pass the packet to the current handler.
824 /* XXX point to ip packet */
825 (*packetHandler)((uchar *)ip, 0, 0, 0);
826 return;
827 #endif
828 default:
829 return;
831 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
832 return;
835 #ifdef CONFIG_UDP_CHECKSUM
836 if (ip->udp_xsum != 0) {
837 ulong xsum;
838 ushort *sumptr;
839 ushort sumlen;
841 xsum = ip->ip_p;
842 xsum += (ntohs(ip->udp_len));
843 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
844 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
845 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
846 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
848 sumlen = ntohs(ip->udp_len);
849 sumptr = (ushort *) &(ip->udp_src);
851 while (sumlen > 1) {
852 ushort sumdata;
854 sumdata = *sumptr++;
855 xsum += ntohs(sumdata);
856 sumlen -= 2;
858 if (sumlen > 0) {
859 ushort sumdata;
861 sumdata = *(unsigned char *) sumptr;
862 sumdata = (sumdata << 8) & 0xff00;
863 xsum += sumdata;
865 while ((xsum >> 16) != 0) {
866 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
868 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
869 printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum));
870 return;
873 #endif
875 #ifdef CONFIG_NETCONSOLE
876 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
877 ntohs(ip->udp_dst),
878 ntohs(ip->udp_src),
879 ntohs(ip->udp_len) - 8);
880 #endif
882 * IP header OK. Pass the packet to the current handler.
884 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
885 ntohs(ip->udp_dst),
886 ntohs(ip->udp_src),
887 ntohs(ip->udp_len) - 8);
888 break;
893 /**********************************************************************/
895 static int net_check_prereq (proto_t protocol)
897 struct eth_device *edev = eth_get_current();
899 switch (protocol) {
900 /* Fall through */
901 #ifdef CONFIG_NET_PING
902 case PING:
903 if (NetPingIP == 0) {
904 puts ("*** ERROR: ping address not given\n");
905 return (1);
907 goto common;
908 #endif
909 #ifdef CONFIG_NET_SNTP
910 case SNTP:
911 if (NetNtpServerIP == 0) {
912 puts ("*** ERROR: NTP server address not given\n");
913 return (1);
915 goto common;
916 #endif
917 #ifdef CONFIG_NET_NFS
918 case NFS:
919 #endif
920 case NETCONS:
921 case TFTP:
922 if (NetServerIP == 0) {
923 printf("*** ERROR: `%s.serverip' not set\n", dev_id(&edev->dev));
924 return (1);
926 common:
928 if (NetOurIP == 0) {
929 printf("*** ERROR: `%s.ipaddr' not set\n", dev_id(&edev->dev));
930 return (1);
932 /* Fall through */
934 case DHCP:
935 case RARP:
936 case BOOTP:
937 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
938 printf("*** ERROR: `%s.ethaddr' not set\n", dev_id(&edev->dev));
939 return (1);
941 /* Fall through */
942 default:
943 return (0);
945 return (0); /* OK */
947 /**********************************************************************/
950 NetCksumOk(uchar * ptr, int len)
952 return !((NetCksum(ptr, len) + 1) & 0xfffe);
956 unsigned
957 NetCksum(uchar * ptr, int len)
959 ulong xsum;
960 ushort *p = (ushort *)ptr;
962 xsum = 0;
963 while (len-- > 0)
964 xsum += *p++;
965 xsum = (xsum & 0xffff) + (xsum >> 16);
966 xsum = (xsum & 0xffff) + (xsum >> 16);
967 return (xsum & 0xffff);
971 NetEthHdrSize(void)
973 ushort myvlanid;
975 myvlanid = ntohs(NetOurVLAN);
976 if (myvlanid == (ushort)-1)
977 myvlanid = VLAN_NONE;
979 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
983 NetSetEther(uchar * xet, uchar * addr, uint prot)
985 Ethernet_t *et = (Ethernet_t *)xet;
986 ushort myvlanid;
988 myvlanid = ntohs(NetOurVLAN);
989 if (myvlanid == (ushort)-1)
990 myvlanid = VLAN_NONE;
992 memcpy (et->et_dest, addr, 6);
993 memcpy (et->et_src, NetOurEther, 6);
994 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
995 et->et_protlen = htons(prot);
996 return ETHER_HDR_SIZE;
997 } else {
998 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1000 vet->vet_vlan_type = htons(PROT_VLAN);
1001 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1002 vet->vet_type = htons(prot);
1003 return VLAN_ETHER_HDR_SIZE;
1007 void
1008 NetSetIP(uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1010 IP_t *ip = (IP_t *)xip;
1013 * If the data is an odd number of bytes, zero the
1014 * byte after the last byte so that the checksum
1015 * will work.
1017 if (len & 1)
1018 xip[IP_HDR_SIZE + len] = 0;
1021 * Construct an IP and UDP header.
1022 * (need to set no fragment bit - XXX)
1024 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1025 ip->ip_tos = 0;
1026 ip->ip_len = htons(IP_HDR_SIZE + len);
1027 ip->ip_id = htons(NetIPID++);
1028 ip->ip_off = htons(0x4000); /* No fragmentation */
1029 ip->ip_ttl = 255;
1030 ip->ip_p = 17; /* UDP */
1031 ip->ip_sum = 0;
1032 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1033 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1034 ip->udp_src = htons(sport);
1035 ip->udp_dst = htons(dport);
1036 ip->udp_len = htons(8 + len);
1037 ip->udp_xsum = 0;
1038 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1041 char *ip_to_string (IPaddr_t x, char *s)
1043 x = ntohl (x);
1044 sprintf (s, "%d.%d.%d.%d",
1045 (int) ((x >> 24) & 0xff),
1046 (int) ((x >> 16) & 0xff),
1047 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1049 return s;
1052 int string_to_ip(const char *s, IPaddr_t *ip)
1054 IPaddr_t addr = 0;
1055 char *e;
1056 int i;
1058 if (!s)
1059 return -EINVAL;
1061 for (i = 0; i < 4; i++) {
1062 ulong val;
1064 if (!isdigit(*s))
1065 return -EINVAL;
1067 val = simple_strtoul(s, &e, 10);
1068 addr <<= 8;
1069 addr |= (val & 0xFF);
1071 if (*e != '.' && i != 3)
1072 return -EINVAL;
1074 s = e + 1;
1077 *ip = htonl(addr);
1078 return 0;
1081 void VLAN_to_string(ushort x, char *s)
1083 x = ntohs(x);
1085 if (x == (ushort)-1)
1086 x = VLAN_NONE;
1088 if (x == VLAN_NONE)
1089 strcpy(s, "none");
1090 else
1091 sprintf(s, "%d", x & VLAN_IDMASK);
1094 ushort string_to_VLAN(const char *s)
1096 ushort id;
1098 if (s == NULL)
1099 return htons(VLAN_NONE);
1101 if (*s < '0' || *s > '9')
1102 id = VLAN_NONE;
1103 else
1104 id = (ushort)simple_strtoul(s, NULL, 10);
1106 return htons(id);
1109 void print_IPaddr (IPaddr_t x)
1111 char tmp[16];
1113 ip_to_string (x, tmp);
1115 puts (tmp);
1118 ushort getenv_VLAN(char *var)
1120 return (string_to_VLAN(getenv(var)));
1123 int string_to_ethaddr(const char *str, char *enetaddr)
1125 ulong reg;
1126 char *e;
1128 if (!str || strlen(str) != 17)
1129 return -1;
1131 if (str[2] != ':' || str[5] != ':' || str[8] != ':' ||
1132 str[11] != ':' || str[14] != ':')
1133 return -1;
1135 for (reg = 0; reg < 6; ++reg) {
1136 enetaddr[reg] = simple_strtoul (str, &e, 16);
1137 str = e + 1;
1140 return 0;
1143 void ethaddr_to_string(const unsigned char *enetaddr, char *str)
1145 sprintf (str, "%02X:%02X:%02X:%02X:%02X:%02X",
1146 enetaddr[0], enetaddr[1], enetaddr[2], enetaddr[3],
1147 enetaddr[4], enetaddr[5]);