net.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * Copied from Linux Monitor (LiMon) - Networking.
  3. *
  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. */
  10. /*
  11. * General Desription:
  12. *
  13. * The user interface supports commands for BOOTP, RARP, and TFTP.
  14. * Also, we support ARP internally. Depending on available data,
  15. * these interact as follows:
  16. *
  17. * BOOTP:
  18. *
  19. * Prerequisites: - own ethernet address
  20. * We want: - own IP address
  21. * - TFTP server IP address
  22. * - name of bootfile
  23. * Next step: ARP
  24. *
  25. * RARP:
  26. *
  27. * Prerequisites: - own ethernet address
  28. * We want: - own IP address
  29. * - TFTP server IP address
  30. * Next step: ARP
  31. *
  32. * ARP:
  33. *
  34. * Prerequisites: - own ethernet address
  35. * - own IP address
  36. * - TFTP server IP address
  37. * We want: - TFTP server ethernet address
  38. * Next step: TFTP
  39. *
  40. * DHCP:
  41. *
  42. * Prerequisites: - own ethernet address
  43. * We want: - IP, Netmask, ServerIP, Gateway IP
  44. * - bootfilename, lease time
  45. * Next step: - TFTP
  46. *
  47. * TFTP:
  48. *
  49. * Prerequisites: - own ethernet address
  50. * - own IP address
  51. * - TFTP server IP address
  52. * - TFTP server ethernet address
  53. * - name of bootfile (if unknown, we use a default name
  54. * derived from our own IP address)
  55. * We want: - load the boot file
  56. * Next step: none
  57. *
  58. * NFS:
  59. *
  60. * Prerequisites: - own ethernet address
  61. * - own IP address
  62. * - name of bootfile (if unknown, we use a default name
  63. * derived from our own IP address)
  64. * We want: - load the boot file
  65. * Next step: none
  66. */
  67. #include <common.h>
  68. #include <watchdog.h>
  69. #include <command.h>
  70. #include <net.h>
  71. #include "bootp.h"
  72. #include "tftp.h"
  73. #include "rarp.h"
  74. #include "nfs.h"
  75. #ifdef CONFIG_STATUS_LED
  76. #include <status_led.h>
  77. #include <miiphy.h>
  78. #endif
  79. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  80. #define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
  81. #ifndef CONFIG_NET_RETRY_COUNT
  82. # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  83. #else
  84. # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
  85. #endif
  86. #if 0
  87. #define ET_DEBUG
  88. #endif
  89. /** BOOTP EXTENTIONS **/
  90. IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
  91. IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
  92. IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
  93. #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
  94. IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
  95. #endif
  96. char NetOurNISDomain[32]={0,}; /* Our NIS domain */
  97. char NetOurHostName[32]={0,}; /* Our hostname */
  98. char NetOurRootPath[64]={0,}; /* Our bootpath */
  99. ushort NetBootFileSize=0; /* Our bootfile size in blocks */
  100. /** END OF BOOTP EXTENTIONS **/
  101. ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
  102. uchar NetOurEther[6]; /* Our ethernet address */
  103. uchar NetServerEther[6] = /* Boot server enet address */
  104. { 0, 0, 0, 0, 0, 0 };
  105. IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
  106. IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
  107. volatile uchar *NetRxPkt; /* Current receive packet */
  108. int NetRxPktLen; /* Current rx packet length */
  109. unsigned NetIPID; /* IP packet ID */
  110. uchar NetBcastAddr[6] = /* Ethernet bcast address */
  111. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  112. uchar NetEtherNullAddr[6] =
  113. { 0, 0, 0, 0, 0, 0 };
  114. int NetState; /* Network loop state */
  115. #ifdef CONFIG_NET_MULTI
  116. int NetRestartWrap = 0; /* Tried all network devices */
  117. static int NetRestarted = 0; /* Network loop restarted */
  118. static int NetDevExists = 0; /* At least one device configured */
  119. #endif
  120. char BootFile[128]; /* Boot File name */
  121. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  122. IPaddr_t NetPingIP; /* the ip address to ping */
  123. static void PingStart(void);
  124. #endif
  125. volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
  126. volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
  127. static rxhand_f *packetHandler; /* Current RX packet handler */
  128. static thand_f *timeHandler; /* Current timeout handler */
  129. static ulong timeStart; /* Time base value */
  130. static ulong timeDelta; /* Current timeout value */
  131. volatile uchar *NetTxPacket = 0; /* THE transmit packet */
  132. static int net_check_prereq (proto_t protocol);
  133. /**********************************************************************/
  134. IPaddr_t NetArpWaitPacketIP;
  135. IPaddr_t NetArpWaitReplyIP;
  136. uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
  137. uchar *NetArpWaitTxPacket; /* THE transmit packet */
  138. int NetArpWaitTxPacketSize;
  139. uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
  140. ulong NetArpWaitTimerStart;
  141. int NetArpWaitTry;
  142. void ArpRequest(void)
  143. {
  144. int i;
  145. volatile uchar *pkt;
  146. ARP_t * arp;
  147. #ifdef ET_DEBUG
  148. printf("ARP broadcast %d\n", NetArpWaitTry);
  149. #endif
  150. pkt = NetTxPacket;
  151. NetSetEther(pkt, NetBcastAddr, PROT_ARP);
  152. pkt += ETHER_HDR_SIZE;
  153. arp = (ARP_t *)pkt;
  154. arp->ar_hrd = htons(ARP_ETHER);
  155. arp->ar_pro = htons(PROT_IP);
  156. arp->ar_hln = 6;
  157. arp->ar_pln = 4;
  158. arp->ar_op = htons(ARPOP_REQUEST);
  159. memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
  160. NetWriteIP((uchar*)&arp->ar_data[6], NetOurIP); /* source IP addr */
  161. for (i=10; i<16; ++i) {
  162. arp->ar_data[i] = 0; /* dest ET addr = 0 */
  163. }
  164. if((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) {
  165. if (NetOurGatewayIP == 0) {
  166. puts ("## Warning: gatewayip needed but not set\n");
  167. }
  168. NetArpWaitReplyIP = NetOurGatewayIP;
  169. } else
  170. NetArpWaitReplyIP = NetArpWaitPacketIP;
  171. NetWriteIP((uchar*)&arp->ar_data[16], NetArpWaitReplyIP);
  172. (void) eth_send(NetTxPacket, ETHER_HDR_SIZE + ARP_HDR_SIZE);
  173. }
  174. void ArpTimeoutCheck(void)
  175. {
  176. ulong t;
  177. if (!NetArpWaitPacketIP)
  178. return;
  179. t = get_timer(0);
  180. /* check for arp timeout */
  181. if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
  182. NetArpWaitTry++;
  183. if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
  184. puts ("\nARP Retry count exceeded; starting again\n");
  185. NetArpWaitTry = 0;
  186. NetStartAgain();
  187. } else {
  188. NetArpWaitTimerStart = t;
  189. ArpRequest();
  190. }
  191. }
  192. }
  193. /**********************************************************************/
  194. /*
  195. * Main network processing loop.
  196. */
  197. int
  198. NetLoop(proto_t protocol)
  199. {
  200. DECLARE_GLOBAL_DATA_PTR;
  201. bd_t *bd = gd->bd;
  202. #ifdef CONFIG_NET_MULTI
  203. NetRestarted = 0;
  204. NetDevExists = 0;
  205. #endif
  206. /* XXX problem with bss workaround */
  207. NetArpWaitPacketMAC = NULL;
  208. NetArpWaitTxPacket = NULL;
  209. NetArpWaitPacketIP = 0;
  210. NetArpWaitReplyIP = 0;
  211. NetArpWaitTxPacket = NULL;
  212. NetTxPacket = NULL;
  213. if (!NetTxPacket) {
  214. int i;
  215. /*
  216. * Setup packet buffers, aligned correctly.
  217. */
  218. NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
  219. NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
  220. for (i = 0; i < PKTBUFSRX; i++) {
  221. NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
  222. }
  223. }
  224. if (!NetArpWaitTxPacket) {
  225. NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
  226. NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
  227. NetArpWaitTxPacketSize = 0;
  228. }
  229. eth_halt();
  230. if(eth_init(bd) < 0)
  231. return(-1);
  232. restart:
  233. #ifdef CONFIG_NET_MULTI
  234. memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
  235. #else
  236. memcpy (NetOurEther, bd->bi_enetaddr, 6);
  237. #endif
  238. NetState = NETLOOP_CONTINUE;
  239. /*
  240. * Start the ball rolling with the given start function. From
  241. * here on, this code is a state machine driven by received
  242. * packets and timer events.
  243. */
  244. switch (protocol) {
  245. #if (CONFIG_COMMANDS & CFG_CMD_NFS)
  246. case NFS:
  247. #endif
  248. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  249. case PING:
  250. #endif
  251. case TFTP:
  252. NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
  253. NetOurGatewayIP = getenv_IPaddr ("gatewayip");
  254. NetOurSubnetMask= getenv_IPaddr ("netmask");
  255. switch (protocol) {
  256. #if (CONFIG_COMMANDS & CFG_CMD_NFS)
  257. case NFS:
  258. #endif
  259. case TFTP:
  260. NetServerIP = getenv_IPaddr ("serverip");
  261. break;
  262. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  263. case PING:
  264. /* nothing */
  265. break;
  266. #endif
  267. default:
  268. break;
  269. }
  270. break;
  271. case BOOTP:
  272. case RARP:
  273. /*
  274. * initialize our IP addr to 0 in order to accept ANY
  275. * IP addr assigned to us by the BOOTP / RARP server
  276. */
  277. NetOurIP = 0;
  278. NetServerIP = 0;
  279. break;
  280. default:
  281. break;
  282. }
  283. switch (net_check_prereq (protocol)) {
  284. case 1:
  285. /* network not configured */
  286. return (-1);
  287. #ifdef CONFIG_NET_MULTI
  288. case 2:
  289. /* network device not configured */
  290. break;
  291. #endif /* CONFIG_NET_MULTI */
  292. case 0:
  293. #ifdef CONFIG_NET_MULTI
  294. NetDevExists = 1;
  295. #endif
  296. switch (protocol) {
  297. case TFTP:
  298. /* always use ARP to get server ethernet address */
  299. TftpStart();
  300. break;
  301. #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
  302. case DHCP:
  303. /* Start with a clean slate... */
  304. NetOurIP = 0;
  305. NetServerIP = 0;
  306. DhcpRequest(); /* Basically same as BOOTP */
  307. break;
  308. #endif /* CFG_CMD_DHCP */
  309. case BOOTP:
  310. BootpTry = 0;
  311. BootpRequest ();
  312. break;
  313. case RARP:
  314. RarpTry = 0;
  315. RarpRequest ();
  316. break;
  317. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  318. case PING:
  319. PingStart();
  320. break;
  321. #endif
  322. #if (CONFIG_COMMANDS & CFG_CMD_NFS)
  323. case NFS:
  324. NfsStart();
  325. break;
  326. #endif
  327. default:
  328. break;
  329. }
  330. NetBootFileXferSize = 0;
  331. break;
  332. }
  333. #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  334. #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
  335. /*
  336. * Echo the inverted link state to the fault LED.
  337. */
  338. if(miiphy_link(CFG_FAULT_MII_ADDR)) {
  339. status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
  340. } else {
  341. status_led_set (STATUS_LED_RED, STATUS_LED_ON);
  342. }
  343. #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
  344. #endif /* CONFIG_MII, ... */
  345. /*
  346. * Main packet reception loop. Loop receiving packets until
  347. * someone sets `NetQuit'.
  348. */
  349. for (;;) {
  350. WATCHDOG_RESET();
  351. #ifdef CONFIG_SHOW_ACTIVITY
  352. {
  353. extern void show_activity(int arg);
  354. show_activity(1);
  355. }
  356. #endif
  357. /*
  358. * Check the ethernet for a new packet. The ethernet
  359. * receive routine will process it.
  360. */
  361. eth_rx();
  362. /*
  363. * Abort if ctrl-c was pressed.
  364. */
  365. if (ctrlc()) {
  366. eth_halt();
  367. printf("\nAbort\n");
  368. return (-1);
  369. }
  370. ArpTimeoutCheck();
  371. /*
  372. * Check for a timeout, and run the timeout handler
  373. * if we have one.
  374. */
  375. if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
  376. thand_f *x;
  377. #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  378. #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
  379. /*
  380. * Echo the inverted link state to the fault LED.
  381. */
  382. if(miiphy_link(CFG_FAULT_MII_ADDR)) {
  383. status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
  384. } else {
  385. status_led_set (STATUS_LED_RED, STATUS_LED_ON);
  386. }
  387. #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
  388. #endif /* CONFIG_MII, ... */
  389. x = timeHandler;
  390. timeHandler = (thand_f *)0;
  391. (*x)();
  392. }
  393. switch (NetState) {
  394. case NETLOOP_RESTART:
  395. #ifdef CONFIG_NET_MULTI
  396. NetRestarted = 1;
  397. #endif
  398. goto restart;
  399. case NETLOOP_SUCCESS:
  400. if (NetBootFileXferSize > 0) {
  401. char buf[10];
  402. printf("Bytes transferred = %ld (%lx hex)\n",
  403. NetBootFileXferSize,
  404. NetBootFileXferSize);
  405. sprintf(buf, "%lx", NetBootFileXferSize);
  406. setenv("filesize", buf);
  407. }
  408. eth_halt();
  409. return NetBootFileXferSize;
  410. case NETLOOP_FAIL:
  411. return (-1);
  412. }
  413. }
  414. }
  415. /**********************************************************************/
  416. static void
  417. startAgainTimeout(void)
  418. {
  419. NetState = NETLOOP_RESTART;
  420. }
  421. static void
  422. startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
  423. {
  424. /* Totally ignore the packet */
  425. }
  426. void
  427. NetStartAgain(void)
  428. {
  429. #ifndef CONFIG_NET_MULTI
  430. NetSetTimeout(10 * CFG_HZ, startAgainTimeout);
  431. NetSetHandler(startAgainHandler);
  432. #else
  433. DECLARE_GLOBAL_DATA_PTR;
  434. eth_halt();
  435. eth_try_another(!NetRestarted);
  436. eth_init(gd->bd);
  437. if (NetRestartWrap)
  438. {
  439. NetRestartWrap = 0;
  440. if (NetDevExists)
  441. {
  442. NetSetTimeout(10 * CFG_HZ, startAgainTimeout);
  443. NetSetHandler(startAgainHandler);
  444. }
  445. else
  446. {
  447. NetState = NETLOOP_FAIL;
  448. }
  449. }
  450. else
  451. {
  452. NetState = NETLOOP_RESTART;
  453. }
  454. #endif
  455. }
  456. /**********************************************************************/
  457. /*
  458. * Miscelaneous bits.
  459. */
  460. void
  461. NetSetHandler(rxhand_f * f)
  462. {
  463. packetHandler = f;
  464. }
  465. void
  466. NetSetTimeout(int iv, thand_f * f)
  467. {
  468. if (iv == 0) {
  469. timeHandler = (thand_f *)0;
  470. } else {
  471. timeHandler = f;
  472. timeStart = get_timer(0);
  473. timeDelta = iv;
  474. }
  475. }
  476. void
  477. NetSendPacket(volatile uchar * pkt, int len)
  478. {
  479. (void) eth_send(pkt, len);
  480. }
  481. int
  482. NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
  483. {
  484. /* convert to new style broadcast */
  485. if (dest == 0)
  486. dest = 0xFFFFFFFF;
  487. /* if broadcast, make the ether address a broadcast and don't do ARP */
  488. if (dest == 0xFFFFFFFF)
  489. ether = NetBcastAddr;
  490. /* if MAC address was not discovered yet, save the packet and do an ARP request */
  491. if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
  492. #ifdef ET_DEBUG
  493. printf("sending ARP for %08lx\n", dest);
  494. #endif
  495. NetArpWaitPacketIP = dest;
  496. NetArpWaitPacketMAC = ether;
  497. NetSetEther (NetArpWaitTxPacket, NetArpWaitPacketMAC, PROT_IP);
  498. NetSetIP (NetArpWaitTxPacket + ETHER_HDR_SIZE, dest, dport, sport, len);
  499. memcpy(NetArpWaitTxPacket + ETHER_HDR_SIZE + IP_HDR_SIZE,
  500. (uchar *)NetTxPacket + ETHER_HDR_SIZE + IP_HDR_SIZE, len);
  501. /* size of the waiting packet */
  502. NetArpWaitTxPacketSize = ETHER_HDR_SIZE + IP_HDR_SIZE + len;
  503. /* and do the ARP request */
  504. NetArpWaitTry = 1;
  505. NetArpWaitTimerStart = get_timer(0);
  506. ArpRequest();
  507. return 1; /* waiting */
  508. }
  509. #ifdef ET_DEBUG
  510. printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
  511. dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
  512. #endif
  513. NetSetEther (NetTxPacket, ether, PROT_IP);
  514. NetSetIP (NetTxPacket + ETHER_HDR_SIZE, dest, dport, sport, len);
  515. (void) eth_send(NetTxPacket, ETHER_HDR_SIZE + IP_HDR_SIZE + len);
  516. return 0; /* transmited */
  517. }
  518. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  519. static ushort PingSeqNo;
  520. int PingSend(void)
  521. {
  522. static uchar mac[6];
  523. volatile IP_t *ip;
  524. volatile ushort *s;
  525. /* XXX always send arp request */
  526. memcpy(mac, NetEtherNullAddr, 6);
  527. #ifdef ET_DEBUG
  528. printf("sending ARP for %08lx\n", NetPingIP);
  529. #endif
  530. NetArpWaitPacketIP = NetPingIP;
  531. NetArpWaitPacketMAC = mac;
  532. NetSetEther(NetArpWaitTxPacket, mac, PROT_IP);
  533. ip = (volatile IP_t *)(NetArpWaitTxPacket + ETHER_HDR_SIZE);
  534. /*
  535. * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
  536. */
  537. ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
  538. ip->ip_tos = 0;
  539. ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
  540. ip->ip_id = htons(NetIPID++);
  541. ip->ip_off = htons(0x4000); /* No fragmentation */
  542. ip->ip_ttl = 255;
  543. ip->ip_p = 0x01; /* ICMP */
  544. ip->ip_sum = 0;
  545. NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
  546. NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
  547. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  548. s = &ip->udp_src; /* XXX ICMP starts here */
  549. s[0] = htons(0x0800); /* echo-request, code */
  550. s[1] = 0; /* checksum */
  551. s[2] = 0; /* identifier */
  552. s[3] = htons(PingSeqNo++); /* sequence number */
  553. s[1] = ~NetCksum((uchar *)s, 8/2);
  554. /* size of the waiting packet */
  555. NetArpWaitTxPacketSize = ETHER_HDR_SIZE + IP_HDR_SIZE_NO_UDP + 8;
  556. /* and do the ARP request */
  557. NetArpWaitTry = 1;
  558. NetArpWaitTimerStart = get_timer(0);
  559. ArpRequest();
  560. return 1; /* waiting */
  561. }
  562. static void
  563. PingTimeout (void)
  564. {
  565. eth_halt();
  566. NetState = NETLOOP_FAIL; /* we did not get the reply */
  567. }
  568. static void
  569. PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  570. {
  571. IPaddr_t tmp;
  572. volatile IP_t *ip = (volatile IP_t *)pkt;
  573. tmp = NetReadIP((void *)&ip->ip_src);
  574. if (tmp != NetPingIP)
  575. return;
  576. NetState = NETLOOP_SUCCESS;
  577. }
  578. static void PingStart(void)
  579. {
  580. NetSetTimeout (10 * CFG_HZ, PingTimeout);
  581. NetSetHandler (PingHandler);
  582. PingSend();
  583. }
  584. #endif
  585. void
  586. NetReceive(volatile uchar * pkt, int len)
  587. {
  588. Ethernet_t *et;
  589. IP_t *ip;
  590. ARP_t *arp;
  591. IPaddr_t tmp;
  592. int x;
  593. NetRxPkt = pkt;
  594. NetRxPktLen = len;
  595. et = (Ethernet_t *)pkt;
  596. x = ntohs(et->et_protlen);
  597. if (x < 1514) {
  598. /*
  599. * Got a 802 packet. Check the other protocol field.
  600. */
  601. x = ntohs(et->et_prot);
  602. ip = (IP_t *)(pkt + E802_HDR_SIZE);
  603. len -= E802_HDR_SIZE;
  604. } else {
  605. ip = (IP_t *)(pkt + ETHER_HDR_SIZE);
  606. len -= ETHER_HDR_SIZE;
  607. }
  608. #ifdef ET_DEBUG
  609. printf("Receive from protocol 0x%x\n", x);
  610. #endif
  611. switch (x) {
  612. case PROT_ARP:
  613. /*
  614. * We have to deal with two types of ARP packets:
  615. * - REQUEST packets will be answered by sending our
  616. * IP address - if we know it.
  617. * - REPLY packates are expected only after we asked
  618. * for the TFTP server's or the gateway's ethernet
  619. * address; so if we receive such a packet, we set
  620. * the server ethernet address
  621. */
  622. #ifdef ET_DEBUG
  623. printf("Got ARP\n");
  624. #endif
  625. arp = (ARP_t *)ip;
  626. if (len < ARP_HDR_SIZE) {
  627. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  628. return;
  629. }
  630. if (ntohs(arp->ar_hrd) != ARP_ETHER) {
  631. return;
  632. }
  633. if (ntohs(arp->ar_pro) != PROT_IP) {
  634. return;
  635. }
  636. if (arp->ar_hln != 6) {
  637. return;
  638. }
  639. if (arp->ar_pln != 4) {
  640. return;
  641. }
  642. if (NetOurIP == 0) {
  643. return;
  644. }
  645. if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
  646. return;
  647. }
  648. switch (ntohs(arp->ar_op)) {
  649. case ARPOP_REQUEST: /* reply with our IP address */
  650. #ifdef ET_DEBUG
  651. printf("Got ARP REQUEST, return our IP\n");
  652. #endif
  653. NetSetEther((uchar *)et, et->et_src, PROT_ARP);
  654. arp->ar_op = htons(ARPOP_REPLY);
  655. memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
  656. NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
  657. memcpy (&arp->ar_data[ 0], NetOurEther, 6);
  658. NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
  659. (void) eth_send((uchar *)et, ((uchar *)arp-pkt) + ARP_HDR_SIZE);
  660. return;
  661. case ARPOP_REPLY: /* arp reply */
  662. /* are we waiting for a reply */
  663. if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
  664. break;
  665. #ifdef ET_DEBUG
  666. printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
  667. arp->ar_data[0], arp->ar_data[1],
  668. arp->ar_data[2], arp->ar_data[3],
  669. arp->ar_data[4], arp->ar_data[5]);
  670. #endif
  671. tmp = NetReadIP(&arp->ar_data[6]);
  672. /* matched waiting packet's address */
  673. if (tmp == NetArpWaitReplyIP) {
  674. #ifdef ET_DEBUG
  675. printf("Got it\n");
  676. #endif
  677. /* save address for later use */
  678. memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
  679. /* modify header, and transmit it */
  680. memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
  681. (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
  682. /* no arp request pending now */
  683. NetArpWaitPacketIP = 0;
  684. NetArpWaitTxPacketSize = 0;
  685. NetArpWaitPacketMAC = NULL;
  686. }
  687. return;
  688. default:
  689. #ifdef ET_DEBUG
  690. printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
  691. #endif
  692. return;
  693. }
  694. case PROT_RARP:
  695. #ifdef ET_DEBUG
  696. printf("Got RARP\n");
  697. #endif
  698. arp = (ARP_t *)ip;
  699. if (len < ARP_HDR_SIZE) {
  700. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  701. return;
  702. }
  703. if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
  704. (ntohs(arp->ar_hrd) != ARP_ETHER) ||
  705. (ntohs(arp->ar_pro) != PROT_IP) ||
  706. (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
  707. printf("invalid RARP header\n");
  708. } else {
  709. NetCopyIP(&NetOurIP, &arp->ar_data[16]);
  710. NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
  711. memcpy (NetServerEther, &arp->ar_data[ 0], 6);
  712. (*packetHandler)(0,0,0,0);
  713. }
  714. break;
  715. case PROT_IP:
  716. #ifdef ET_DEBUG
  717. printf("Got IP\n");
  718. #endif
  719. if (len < IP_HDR_SIZE) {
  720. debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
  721. return;
  722. }
  723. if (len < ntohs(ip->ip_len)) {
  724. printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
  725. return;
  726. }
  727. len = ntohs(ip->ip_len);
  728. #ifdef ET_DEBUG
  729. printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
  730. #endif
  731. if ((ip->ip_hl_v & 0xf0) != 0x40) {
  732. return;
  733. }
  734. if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
  735. return;
  736. }
  737. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
  738. printf("checksum bad\n");
  739. return;
  740. }
  741. tmp = NetReadIP(&ip->ip_dst);
  742. if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
  743. return;
  744. }
  745. /*
  746. * watch for ICMP host redirects
  747. *
  748. * There is no real handler code (yet). We just watch
  749. * for ICMP host redirect messages. In case anybody
  750. * sees these messages: please contact me
  751. * (wd@denx.de), or - even better - send me the
  752. * necessary fixes :-)
  753. *
  754. * Note: in all cases where I have seen this so far
  755. * it was a problem with the router configuration,
  756. * for instance when a router was configured in the
  757. * BOOTP reply, but the TFTP server was on the same
  758. * subnet. So this is probably a warning that your
  759. * configuration might be wrong. But I'm not really
  760. * sure if there aren't any other situations.
  761. */
  762. if (ip->ip_p == IPPROTO_ICMP) {
  763. ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
  764. switch (icmph->type) {
  765. case ICMP_REDIRECT:
  766. if (icmph->code != ICMP_REDIR_HOST)
  767. return;
  768. puts (" ICMP Host Redirect to ");
  769. print_IPaddr(icmph->un.gateway);
  770. putc(' ');
  771. break;
  772. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  773. case ICMP_ECHO_REPLY:
  774. /*
  775. * IP header OK. Pass the packet to the current handler.
  776. */
  777. /* XXX point to ip packet */
  778. (*packetHandler)((uchar *)ip, 0, 0, 0);
  779. break;
  780. #endif
  781. default:
  782. return;
  783. }
  784. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  785. return;
  786. }
  787. /*
  788. * IP header OK. Pass the packet to the current handler.
  789. */
  790. (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
  791. ntohs(ip->udp_dst),
  792. ntohs(ip->udp_src),
  793. ntohs(ip->udp_len) - 8);
  794. break;
  795. }
  796. }
  797. /**********************************************************************/
  798. static int net_check_prereq (proto_t protocol)
  799. {
  800. switch (protocol) {
  801. /* Fall through */
  802. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  803. case PING:
  804. if (NetPingIP == 0) {
  805. puts ("*** ERROR: ping address not given\n");
  806. return (1);
  807. }
  808. goto common;
  809. #endif
  810. #if (CONFIG_COMMANDS & CFG_CMD_NFS)
  811. case NFS:
  812. #endif
  813. case TFTP:
  814. if (NetServerIP == 0) {
  815. puts ("*** ERROR: `serverip' not set\n");
  816. return (1);
  817. }
  818. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  819. common:
  820. #endif
  821. if (NetOurIP == 0) {
  822. puts ("*** ERROR: `ipaddr' not set\n");
  823. return (1);
  824. }
  825. /* Fall through */
  826. case DHCP:
  827. case RARP:
  828. case BOOTP:
  829. if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  830. #ifdef CONFIG_NET_MULTI
  831. extern int eth_get_dev_index (void);
  832. int num = eth_get_dev_index();
  833. switch (num) {
  834. case -1:
  835. puts ("*** ERROR: No ethernet found.\n");
  836. return (1);
  837. case 0:
  838. puts ("*** ERROR: `ethaddr' not set\n");
  839. break;
  840. default:
  841. printf ("*** ERROR: `eth%daddr' not set\n",
  842. num);
  843. break;
  844. }
  845. NetStartAgain ();
  846. return (2);
  847. #else
  848. puts ("*** ERROR: `ethaddr' not set\n");
  849. return (1);
  850. #endif
  851. }
  852. /* Fall through */
  853. default:
  854. return(0);
  855. }
  856. return (0); /* OK */
  857. }
  858. /**********************************************************************/
  859. int
  860. NetCksumOk(uchar * ptr, int len)
  861. {
  862. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  863. }
  864. unsigned
  865. NetCksum(uchar * ptr, int len)
  866. {
  867. ulong xsum;
  868. xsum = 0;
  869. while (len-- > 0)
  870. xsum += *((ushort *)ptr)++;
  871. xsum = (xsum & 0xffff) + (xsum >> 16);
  872. xsum = (xsum & 0xffff) + (xsum >> 16);
  873. return (xsum & 0xffff);
  874. }
  875. void
  876. NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
  877. {
  878. Ethernet_t *et = (Ethernet_t *)xet;
  879. memcpy (et->et_dest, addr, 6);
  880. memcpy (et->et_src, NetOurEther, 6);
  881. et->et_protlen = htons(prot);
  882. }
  883. void
  884. NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
  885. {
  886. volatile IP_t *ip = (IP_t *)xip;
  887. /*
  888. * If the data is an odd number of bytes, zero the
  889. * byte after the last byte so that the checksum
  890. * will work.
  891. */
  892. if (len & 1)
  893. xip[IP_HDR_SIZE + len] = 0;
  894. /*
  895. * Construct an IP and UDP header.
  896. (need to set no fragment bit - XXX)
  897. */
  898. ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
  899. ip->ip_tos = 0;
  900. ip->ip_len = htons(IP_HDR_SIZE + len);
  901. ip->ip_id = htons(NetIPID++);
  902. ip->ip_off = htons(0x4000); /* No fragmentation */
  903. ip->ip_ttl = 255;
  904. ip->ip_p = 17; /* UDP */
  905. ip->ip_sum = 0;
  906. NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
  907. NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
  908. ip->udp_src = htons(sport);
  909. ip->udp_dst = htons(dport);
  910. ip->udp_len = htons(8 + len);
  911. ip->udp_xsum = 0;
  912. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  913. }
  914. void copy_filename (uchar *dst, uchar *src, int size)
  915. {
  916. if (*src && (*src == '"')) {
  917. ++src;
  918. --size;
  919. }
  920. while ((--size > 0) && *src && (*src != '"')) {
  921. *dst++ = *src++;
  922. }
  923. *dst = '\0';
  924. }
  925. #endif /* CFG_CMD_NET */
  926. void ip_to_string (IPaddr_t x, char *s)
  927. {
  928. x = ntohl(x);
  929. sprintf (s,"%d.%d.%d.%d",
  930. (int)((x >> 24) & 0xff),
  931. (int)((x >> 16) & 0xff),
  932. (int)((x >> 8) & 0xff),
  933. (int)((x >> 0) & 0xff)
  934. );
  935. }
  936. IPaddr_t string_to_ip(char *s)
  937. {
  938. IPaddr_t addr;
  939. char *e;
  940. int i;
  941. if (s == NULL)
  942. return(0);
  943. for (addr=0, i=0; i<4; ++i) {
  944. ulong val = s ? simple_strtoul(s, &e, 10) : 0;
  945. addr <<= 8;
  946. addr |= (val & 0xFF);
  947. if (s) {
  948. s = (*e) ? e+1 : e;
  949. }
  950. }
  951. return (htonl(addr));
  952. }
  953. void print_IPaddr (IPaddr_t x)
  954. {
  955. char tmp[16];
  956. ip_to_string(x, tmp);
  957. puts(tmp);
  958. }
  959. IPaddr_t getenv_IPaddr (char *var)
  960. {
  961. return (string_to_ip(getenv(var)));
  962. }