net.c 24 KB

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