net.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  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. * SNTP:
  68. *
  69. * Prerequisites: - own ethernet address
  70. * - own IP address
  71. * We want: - network time
  72. * Next step: none
  73. */
  74. #include <common.h>
  75. #include <watchdog.h>
  76. #include <command.h>
  77. #include <net.h>
  78. #include "bootp.h"
  79. #include "tftp.h"
  80. #include "rarp.h"
  81. #include "nfs.h"
  82. #ifdef CONFIG_STATUS_LED
  83. #include <status_led.h>
  84. #include <miiphy.h>
  85. #endif
  86. #if defined(CONFIG_CMD_SNTP)
  87. #include "sntp.h"
  88. #endif
  89. #if defined(CONFIG_CMD_NET)
  90. DECLARE_GLOBAL_DATA_PTR;
  91. #ifndef CONFIG_ARP_TIMEOUT
  92. # define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
  93. #else
  94. # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
  95. #endif
  96. #ifndef CONFIG_NET_RETRY_COUNT
  97. # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  98. #else
  99. # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
  100. #endif
  101. #if 0
  102. #define ET_DEBUG
  103. #endif
  104. /** BOOTP EXTENTIONS **/
  105. IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
  106. IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
  107. IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
  108. #if defined(CONFIG_BOOTP_DNS2)
  109. IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
  110. #endif
  111. char NetOurNISDomain[32]={0,}; /* Our NIS domain */
  112. char NetOurHostName[32]={0,}; /* Our hostname */
  113. char NetOurRootPath[64]={0,}; /* Our bootpath */
  114. ushort NetBootFileSize=0; /* Our bootfile size in blocks */
  115. #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
  116. IPaddr_t Mcast_addr;
  117. #endif
  118. /** END OF BOOTP EXTENTIONS **/
  119. ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
  120. uchar NetOurEther[6]; /* Our ethernet address */
  121. uchar NetServerEther[6] = /* Boot server enet address */
  122. { 0, 0, 0, 0, 0, 0 };
  123. IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
  124. IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
  125. volatile uchar *NetRxPkt; /* Current receive packet */
  126. int NetRxPktLen; /* Current rx packet length */
  127. unsigned NetIPID; /* IP packet ID */
  128. uchar NetBcastAddr[6] = /* Ethernet bcast address */
  129. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  130. uchar NetEtherNullAddr[6] =
  131. { 0, 0, 0, 0, 0, 0 };
  132. #ifdef CONFIG_API
  133. void (*push_packet)(volatile void *, int len) = 0;
  134. #endif
  135. #if defined(CONFIG_CMD_CDP)
  136. uchar NetCDPAddr[6] = /* Ethernet bcast address */
  137. { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
  138. #endif
  139. int NetState; /* Network loop state */
  140. #ifdef CONFIG_NET_MULTI
  141. int NetRestartWrap = 0; /* Tried all network devices */
  142. static int NetRestarted = 0; /* Network loop restarted */
  143. static int NetDevExists = 0; /* At least one device configured */
  144. #endif
  145. /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
  146. ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
  147. ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
  148. char BootFile[128]; /* Boot File name */
  149. #if defined(CONFIG_CMD_PING)
  150. IPaddr_t NetPingIP; /* the ip address to ping */
  151. static void PingStart(void);
  152. #endif
  153. #if defined(CONFIG_CMD_CDP)
  154. static void CDPStart(void);
  155. #endif
  156. #if defined(CONFIG_CMD_SNTP)
  157. IPaddr_t NetNtpServerIP; /* NTP server IP address */
  158. int NetTimeOffset=0; /* offset time from UTC */
  159. #endif
  160. #ifdef CONFIG_NETCONSOLE
  161. void NcStart(void);
  162. int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
  163. #endif
  164. volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
  165. volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
  166. static rxhand_f *packetHandler; /* Current RX packet handler */
  167. static thand_f *timeHandler; /* Current timeout handler */
  168. static ulong timeStart; /* Time base value */
  169. static ulong timeDelta; /* Current timeout value */
  170. volatile uchar *NetTxPacket = 0; /* THE transmit packet */
  171. static int net_check_prereq (proto_t protocol);
  172. /**********************************************************************/
  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. ulong NetArpWaitTimerStart;
  180. int NetArpWaitTry;
  181. void ArpRequest (void)
  182. {
  183. int i;
  184. volatile uchar *pkt;
  185. ARP_t *arp;
  186. #ifdef ET_DEBUG
  187. printf ("ARP broadcast %d\n", NetArpWaitTry);
  188. #endif
  189. pkt = NetTxPacket;
  190. pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
  191. arp = (ARP_t *) pkt;
  192. arp->ar_hrd = htons (ARP_ETHER);
  193. arp->ar_pro = htons (PROT_IP);
  194. arp->ar_hln = 6;
  195. arp->ar_pln = 4;
  196. arp->ar_op = htons (ARPOP_REQUEST);
  197. memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
  198. NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
  199. for (i = 10; i < 16; ++i) {
  200. arp->ar_data[i] = 0; /* dest ET addr = 0 */
  201. }
  202. if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
  203. (NetOurIP & NetOurSubnetMask)) {
  204. if (NetOurGatewayIP == 0) {
  205. puts ("## Warning: gatewayip needed but not set\n");
  206. NetArpWaitReplyIP = NetArpWaitPacketIP;
  207. } else {
  208. NetArpWaitReplyIP = NetOurGatewayIP;
  209. }
  210. } else {
  211. NetArpWaitReplyIP = NetArpWaitPacketIP;
  212. }
  213. NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
  214. (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
  215. }
  216. void ArpTimeoutCheck(void)
  217. {
  218. ulong t;
  219. if (!NetArpWaitPacketIP)
  220. return;
  221. t = get_timer(0);
  222. /* check for arp timeout */
  223. if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
  224. NetArpWaitTry++;
  225. if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
  226. puts ("\nARP Retry count exceeded; starting again\n");
  227. NetArpWaitTry = 0;
  228. NetStartAgain();
  229. } else {
  230. NetArpWaitTimerStart = t;
  231. ArpRequest();
  232. }
  233. }
  234. }
  235. /**********************************************************************/
  236. /*
  237. * Main network processing loop.
  238. */
  239. int
  240. NetLoop(proto_t protocol)
  241. {
  242. bd_t *bd = gd->bd;
  243. #ifdef CONFIG_NET_MULTI
  244. NetRestarted = 0;
  245. NetDevExists = 0;
  246. #endif
  247. /* XXX problem with bss workaround */
  248. NetArpWaitPacketMAC = NULL;
  249. NetArpWaitTxPacket = NULL;
  250. NetArpWaitPacketIP = 0;
  251. NetArpWaitReplyIP = 0;
  252. NetArpWaitTxPacket = NULL;
  253. NetTxPacket = NULL;
  254. if (!NetTxPacket) {
  255. int i;
  256. /*
  257. * Setup packet buffers, aligned correctly.
  258. */
  259. NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
  260. NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
  261. for (i = 0; i < PKTBUFSRX; i++) {
  262. NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
  263. }
  264. }
  265. if (!NetArpWaitTxPacket) {
  266. NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
  267. NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
  268. NetArpWaitTxPacketSize = 0;
  269. }
  270. eth_halt();
  271. #ifdef CONFIG_NET_MULTI
  272. eth_set_current();
  273. #endif
  274. if (eth_init(bd) < 0) {
  275. eth_halt();
  276. return(-1);
  277. }
  278. restart:
  279. #ifdef CONFIG_NET_MULTI
  280. memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
  281. #else
  282. memcpy (NetOurEther, bd->bi_enetaddr, 6);
  283. #endif
  284. NetState = NETLOOP_CONTINUE;
  285. /*
  286. * Start the ball rolling with the given start function. From
  287. * here on, this code is a state machine driven by received
  288. * packets and timer events.
  289. */
  290. switch (protocol) {
  291. #if defined(CONFIG_CMD_NFS)
  292. case NFS:
  293. #endif
  294. #if defined(CONFIG_CMD_PING)
  295. case PING:
  296. #endif
  297. #if defined(CONFIG_CMD_SNTP)
  298. case SNTP:
  299. #endif
  300. case NETCONS:
  301. case TFTP:
  302. NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
  303. NetOurGatewayIP = getenv_IPaddr ("gatewayip");
  304. NetOurSubnetMask= getenv_IPaddr ("netmask");
  305. NetOurVLAN = getenv_VLAN("vlan");
  306. NetOurNativeVLAN = getenv_VLAN("nvlan");
  307. switch (protocol) {
  308. #if defined(CONFIG_CMD_NFS)
  309. case NFS:
  310. #endif
  311. case NETCONS:
  312. case TFTP:
  313. NetServerIP = getenv_IPaddr ("serverip");
  314. break;
  315. #if defined(CONFIG_CMD_PING)
  316. case PING:
  317. /* nothing */
  318. break;
  319. #endif
  320. #if defined(CONFIG_CMD_SNTP)
  321. case SNTP:
  322. /* nothing */
  323. break;
  324. #endif
  325. default:
  326. break;
  327. }
  328. break;
  329. case BOOTP:
  330. case RARP:
  331. /*
  332. * initialize our IP addr to 0 in order to accept ANY
  333. * IP addr assigned to us by the BOOTP / RARP server
  334. */
  335. NetOurIP = 0;
  336. NetServerIP = getenv_IPaddr ("serverip");
  337. NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
  338. NetOurNativeVLAN = getenv_VLAN("nvlan");
  339. case CDP:
  340. NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
  341. NetOurNativeVLAN = getenv_VLAN("nvlan");
  342. break;
  343. default:
  344. break;
  345. }
  346. switch (net_check_prereq (protocol)) {
  347. case 1:
  348. /* network not configured */
  349. eth_halt();
  350. return (-1);
  351. #ifdef CONFIG_NET_MULTI
  352. case 2:
  353. /* network device not configured */
  354. break;
  355. #endif /* CONFIG_NET_MULTI */
  356. case 0:
  357. #ifdef CONFIG_NET_MULTI
  358. NetDevExists = 1;
  359. #endif
  360. switch (protocol) {
  361. case TFTP:
  362. /* always use ARP to get server ethernet address */
  363. TftpStart();
  364. break;
  365. #if defined(CONFIG_CMD_DHCP)
  366. case DHCP:
  367. /* Start with a clean slate... */
  368. BootpTry = 0;
  369. NetOurIP = 0;
  370. NetServerIP = getenv_IPaddr ("serverip");
  371. DhcpRequest(); /* Basically same as BOOTP */
  372. break;
  373. #endif
  374. case BOOTP:
  375. BootpTry = 0;
  376. BootpRequest ();
  377. break;
  378. case RARP:
  379. RarpTry = 0;
  380. RarpRequest ();
  381. break;
  382. #if defined(CONFIG_CMD_PING)
  383. case PING:
  384. PingStart();
  385. break;
  386. #endif
  387. #if defined(CONFIG_CMD_NFS)
  388. case NFS:
  389. NfsStart();
  390. break;
  391. #endif
  392. #if defined(CONFIG_CMD_CDP)
  393. case CDP:
  394. CDPStart();
  395. break;
  396. #endif
  397. #ifdef CONFIG_NETCONSOLE
  398. case NETCONS:
  399. NcStart();
  400. break;
  401. #endif
  402. #if defined(CONFIG_CMD_SNTP)
  403. case SNTP:
  404. SntpStart();
  405. break;
  406. #endif
  407. default:
  408. break;
  409. }
  410. NetBootFileXferSize = 0;
  411. break;
  412. }
  413. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  414. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
  415. /*
  416. * Echo the inverted link state to the fault LED.
  417. */
  418. if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
  419. status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
  420. } else {
  421. status_led_set (STATUS_LED_RED, STATUS_LED_ON);
  422. }
  423. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  424. #endif /* CONFIG_MII, ... */
  425. /*
  426. * Main packet reception loop. Loop receiving packets until
  427. * someone sets `NetState' to a state that terminates.
  428. */
  429. for (;;) {
  430. WATCHDOG_RESET();
  431. #ifdef CONFIG_SHOW_ACTIVITY
  432. {
  433. extern void show_activity(int arg);
  434. show_activity(1);
  435. }
  436. #endif
  437. /*
  438. * Check the ethernet for a new packet. The ethernet
  439. * receive routine will process it.
  440. */
  441. eth_rx();
  442. /*
  443. * Abort if ctrl-c was pressed.
  444. */
  445. if (ctrlc()) {
  446. eth_halt();
  447. puts ("\nAbort\n");
  448. return (-1);
  449. }
  450. ArpTimeoutCheck();
  451. /*
  452. * Check for a timeout, and run the timeout handler
  453. * if we have one.
  454. */
  455. if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
  456. thand_f *x;
  457. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  458. # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
  459. defined(CONFIG_STATUS_LED) && \
  460. defined(STATUS_LED_RED)
  461. /*
  462. * Echo the inverted link state to the fault LED.
  463. */
  464. if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
  465. status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
  466. } else {
  467. status_led_set (STATUS_LED_RED, STATUS_LED_ON);
  468. }
  469. # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  470. #endif /* CONFIG_MII, ... */
  471. x = timeHandler;
  472. timeHandler = (thand_f *)0;
  473. (*x)();
  474. }
  475. switch (NetState) {
  476. case NETLOOP_RESTART:
  477. #ifdef CONFIG_NET_MULTI
  478. NetRestarted = 1;
  479. #endif
  480. goto restart;
  481. case NETLOOP_SUCCESS:
  482. if (NetBootFileXferSize > 0) {
  483. char buf[20];
  484. printf("Bytes transferred = %ld (%lx hex)\n",
  485. NetBootFileXferSize,
  486. NetBootFileXferSize);
  487. sprintf(buf, "%lX", NetBootFileXferSize);
  488. setenv("filesize", buf);
  489. sprintf(buf, "%lX", (unsigned long)load_addr);
  490. setenv("fileaddr", buf);
  491. }
  492. eth_halt();
  493. return NetBootFileXferSize;
  494. case NETLOOP_FAIL:
  495. return (-1);
  496. }
  497. }
  498. }
  499. /**********************************************************************/
  500. static void
  501. startAgainTimeout(void)
  502. {
  503. NetState = NETLOOP_RESTART;
  504. }
  505. static void
  506. startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
  507. {
  508. /* Totally ignore the packet */
  509. }
  510. void NetStartAgain (void)
  511. {
  512. char *nretry;
  513. int noretry = 0, once = 0;
  514. if ((nretry = getenv ("netretry")) != NULL) {
  515. noretry = (strcmp (nretry, "no") == 0);
  516. once = (strcmp (nretry, "once") == 0);
  517. }
  518. if (noretry) {
  519. eth_halt ();
  520. NetState = NETLOOP_FAIL;
  521. return;
  522. }
  523. #ifndef CONFIG_NET_MULTI
  524. NetSetTimeout (10000UL, startAgainTimeout);
  525. NetSetHandler (startAgainHandler);
  526. #else /* !CONFIG_NET_MULTI*/
  527. eth_halt ();
  528. #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
  529. eth_try_another (!NetRestarted);
  530. #endif
  531. eth_init (gd->bd);
  532. if (NetRestartWrap) {
  533. NetRestartWrap = 0;
  534. if (NetDevExists && !once) {
  535. NetSetTimeout (10000UL, startAgainTimeout);
  536. NetSetHandler (startAgainHandler);
  537. } else {
  538. NetState = NETLOOP_FAIL;
  539. }
  540. } else {
  541. NetState = NETLOOP_RESTART;
  542. }
  543. #endif /* CONFIG_NET_MULTI */
  544. }
  545. /**********************************************************************/
  546. /*
  547. * Miscelaneous bits.
  548. */
  549. void
  550. NetSetHandler(rxhand_f * f)
  551. {
  552. packetHandler = f;
  553. }
  554. void
  555. NetSetTimeout(ulong iv, thand_f * f)
  556. {
  557. if (iv == 0) {
  558. timeHandler = (thand_f *)0;
  559. } else {
  560. timeHandler = f;
  561. timeStart = get_timer(0);
  562. timeDelta = iv;
  563. }
  564. }
  565. void
  566. NetSendPacket(volatile uchar * pkt, int len)
  567. {
  568. (void) eth_send(pkt, len);
  569. }
  570. int
  571. NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
  572. {
  573. uchar *pkt;
  574. /* convert to new style broadcast */
  575. if (dest == 0)
  576. dest = 0xFFFFFFFF;
  577. /* if broadcast, make the ether address a broadcast and don't do ARP */
  578. if (dest == 0xFFFFFFFF)
  579. ether = NetBcastAddr;
  580. /* if MAC address was not discovered yet, save the packet and do an ARP request */
  581. if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
  582. #ifdef ET_DEBUG
  583. printf("sending ARP for %08lx\n", dest);
  584. #endif
  585. NetArpWaitPacketIP = dest;
  586. NetArpWaitPacketMAC = ether;
  587. pkt = NetArpWaitTxPacket;
  588. pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
  589. NetSetIP (pkt, dest, dport, sport, len);
  590. memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
  591. /* size of the waiting packet */
  592. NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
  593. /* and do the ARP request */
  594. NetArpWaitTry = 1;
  595. NetArpWaitTimerStart = get_timer(0);
  596. ArpRequest();
  597. return 1; /* waiting */
  598. }
  599. #ifdef ET_DEBUG
  600. printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
  601. dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
  602. #endif
  603. pkt = (uchar *)NetTxPacket;
  604. pkt += NetSetEther (pkt, ether, PROT_IP);
  605. NetSetIP (pkt, dest, dport, sport, len);
  606. (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
  607. return 0; /* transmitted */
  608. }
  609. #if defined(CONFIG_CMD_PING)
  610. static ushort PingSeqNo;
  611. int PingSend(void)
  612. {
  613. static uchar mac[6];
  614. volatile IP_t *ip;
  615. volatile ushort *s;
  616. uchar *pkt;
  617. /* XXX always send arp request */
  618. memcpy(mac, NetEtherNullAddr, 6);
  619. #ifdef ET_DEBUG
  620. printf("sending ARP for %08lx\n", NetPingIP);
  621. #endif
  622. NetArpWaitPacketIP = NetPingIP;
  623. NetArpWaitPacketMAC = mac;
  624. pkt = NetArpWaitTxPacket;
  625. pkt += NetSetEther(pkt, mac, PROT_IP);
  626. ip = (volatile IP_t *)pkt;
  627. /*
  628. * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
  629. */
  630. ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
  631. ip->ip_tos = 0;
  632. ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
  633. ip->ip_id = htons(NetIPID++);
  634. ip->ip_off = htons(0x4000); /* No fragmentation */
  635. ip->ip_ttl = 255;
  636. ip->ip_p = 0x01; /* ICMP */
  637. ip->ip_sum = 0;
  638. NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
  639. NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
  640. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  641. s = &ip->udp_src; /* XXX ICMP starts here */
  642. s[0] = htons(0x0800); /* echo-request, code */
  643. s[1] = 0; /* checksum */
  644. s[2] = 0; /* identifier */
  645. s[3] = htons(PingSeqNo++); /* sequence number */
  646. s[1] = ~NetCksum((uchar *)s, 8/2);
  647. /* size of the waiting packet */
  648. NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
  649. /* and do the ARP request */
  650. NetArpWaitTry = 1;
  651. NetArpWaitTimerStart = get_timer(0);
  652. ArpRequest();
  653. return 1; /* waiting */
  654. }
  655. static void
  656. PingTimeout (void)
  657. {
  658. eth_halt();
  659. NetState = NETLOOP_FAIL; /* we did not get the reply */
  660. }
  661. static void
  662. PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  663. {
  664. IPaddr_t tmp;
  665. volatile IP_t *ip = (volatile IP_t *)pkt;
  666. tmp = NetReadIP((void *)&ip->ip_src);
  667. if (tmp != NetPingIP)
  668. return;
  669. NetState = NETLOOP_SUCCESS;
  670. }
  671. static void PingStart(void)
  672. {
  673. #if defined(CONFIG_NET_MULTI)
  674. printf ("Using %s device\n", eth_get_name());
  675. #endif /* CONFIG_NET_MULTI */
  676. NetSetTimeout (10000UL, PingTimeout);
  677. NetSetHandler (PingHandler);
  678. PingSend();
  679. }
  680. #endif
  681. #if defined(CONFIG_CMD_CDP)
  682. #define CDP_DEVICE_ID_TLV 0x0001
  683. #define CDP_ADDRESS_TLV 0x0002
  684. #define CDP_PORT_ID_TLV 0x0003
  685. #define CDP_CAPABILITIES_TLV 0x0004
  686. #define CDP_VERSION_TLV 0x0005
  687. #define CDP_PLATFORM_TLV 0x0006
  688. #define CDP_NATIVE_VLAN_TLV 0x000a
  689. #define CDP_APPLIANCE_VLAN_TLV 0x000e
  690. #define CDP_TRIGGER_TLV 0x000f
  691. #define CDP_POWER_CONSUMPTION_TLV 0x0010
  692. #define CDP_SYSNAME_TLV 0x0014
  693. #define CDP_SYSOBJECT_TLV 0x0015
  694. #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
  695. #define CDP_TIMEOUT 250UL /* one packet every 250ms */
  696. static int CDPSeq;
  697. static int CDPOK;
  698. ushort CDPNativeVLAN;
  699. ushort CDPApplianceVLAN;
  700. static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
  701. static ushort CDP_compute_csum(const uchar *buff, ushort len)
  702. {
  703. ushort csum;
  704. int odd;
  705. ulong result = 0;
  706. ushort leftover;
  707. ushort *p;
  708. if (len > 0) {
  709. odd = 1 & (ulong)buff;
  710. if (odd) {
  711. result = *buff << 8;
  712. len--;
  713. buff++;
  714. }
  715. while (len > 1) {
  716. p = (ushort *)buff;
  717. result += *p++;
  718. buff = (uchar *)p;
  719. if (result & 0x80000000)
  720. result = (result & 0xFFFF) + (result >> 16);
  721. len -= 2;
  722. }
  723. if (len) {
  724. leftover = (signed short)(*(const signed char *)buff);
  725. /* CISCO SUCKS big time! (and blows too):
  726. * CDP uses the IP checksum algorithm with a twist;
  727. * for the last byte it *sign* extends and sums.
  728. */
  729. result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
  730. }
  731. while (result >> 16)
  732. result = (result & 0xFFFF) + (result >> 16);
  733. if (odd)
  734. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  735. }
  736. /* add up 16-bit and 17-bit words for 17+c bits */
  737. result = (result & 0xffff) + (result >> 16);
  738. /* add up 16-bit and 2-bit for 16+c bit */
  739. result = (result & 0xffff) + (result >> 16);
  740. /* add up carry.. */
  741. result = (result & 0xffff) + (result >> 16);
  742. /* negate */
  743. csum = ~(ushort)result;
  744. /* run time endian detection */
  745. if (csum != htons(csum)) /* little endian */
  746. csum = htons(csum);
  747. return csum;
  748. }
  749. int CDPSendTrigger(void)
  750. {
  751. volatile uchar *pkt;
  752. volatile ushort *s;
  753. volatile ushort *cp;
  754. Ethernet_t *et;
  755. int len;
  756. ushort chksum;
  757. #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
  758. defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
  759. char buf[32];
  760. #endif
  761. pkt = NetTxPacket;
  762. et = (Ethernet_t *)pkt;
  763. /* NOTE: trigger sent not on any VLAN */
  764. /* form ethernet header */
  765. memcpy(et->et_dest, NetCDPAddr, 6);
  766. memcpy(et->et_src, NetOurEther, 6);
  767. pkt += ETHER_HDR_SIZE;
  768. /* SNAP header */
  769. memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
  770. pkt += sizeof(CDP_SNAP_hdr);
  771. /* CDP header */
  772. *pkt++ = 0x02; /* CDP version 2 */
  773. *pkt++ = 180; /* TTL */
  774. s = (volatile ushort *)pkt;
  775. cp = s;
  776. *s++ = htons(0); /* checksum (0 for later calculation) */
  777. /* CDP fields */
  778. #ifdef CONFIG_CDP_DEVICE_ID
  779. *s++ = htons(CDP_DEVICE_ID_TLV);
  780. *s++ = htons(CONFIG_CDP_DEVICE_ID);
  781. memset(buf, 0, sizeof(buf));
  782. sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
  783. NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
  784. NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
  785. NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
  786. memcpy((uchar *)s, buf, 16);
  787. s += 16 / 2;
  788. #endif
  789. #ifdef CONFIG_CDP_PORT_ID
  790. *s++ = htons(CDP_PORT_ID_TLV);
  791. memset(buf, 0, sizeof(buf));
  792. sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
  793. len = strlen(buf);
  794. if (len & 1) /* make it even */
  795. len++;
  796. *s++ = htons(len + 4);
  797. memcpy((uchar *)s, buf, len);
  798. s += len / 2;
  799. #endif
  800. #ifdef CONFIG_CDP_CAPABILITIES
  801. *s++ = htons(CDP_CAPABILITIES_TLV);
  802. *s++ = htons(8);
  803. *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
  804. s += 2;
  805. #endif
  806. #ifdef CONFIG_CDP_VERSION
  807. *s++ = htons(CDP_VERSION_TLV);
  808. memset(buf, 0, sizeof(buf));
  809. strcpy(buf, CONFIG_CDP_VERSION);
  810. len = strlen(buf);
  811. if (len & 1) /* make it even */
  812. len++;
  813. *s++ = htons(len + 4);
  814. memcpy((uchar *)s, buf, len);
  815. s += len / 2;
  816. #endif
  817. #ifdef CONFIG_CDP_PLATFORM
  818. *s++ = htons(CDP_PLATFORM_TLV);
  819. memset(buf, 0, sizeof(buf));
  820. strcpy(buf, CONFIG_CDP_PLATFORM);
  821. len = strlen(buf);
  822. if (len & 1) /* make it even */
  823. len++;
  824. *s++ = htons(len + 4);
  825. memcpy((uchar *)s, buf, len);
  826. s += len / 2;
  827. #endif
  828. #ifdef CONFIG_CDP_TRIGGER
  829. *s++ = htons(CDP_TRIGGER_TLV);
  830. *s++ = htons(8);
  831. *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
  832. s += 2;
  833. #endif
  834. #ifdef CONFIG_CDP_POWER_CONSUMPTION
  835. *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
  836. *s++ = htons(6);
  837. *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
  838. #endif
  839. /* length of ethernet packet */
  840. len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
  841. et->et_protlen = htons(len);
  842. len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
  843. chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
  844. if (chksum == 0)
  845. chksum = 0xFFFF;
  846. *cp = htons(chksum);
  847. (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
  848. return 0;
  849. }
  850. static void
  851. CDPTimeout (void)
  852. {
  853. CDPSeq++;
  854. if (CDPSeq < 3) {
  855. NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
  856. CDPSendTrigger();
  857. return;
  858. }
  859. /* if not OK try again */
  860. if (!CDPOK)
  861. NetStartAgain();
  862. else
  863. NetState = NETLOOP_SUCCESS;
  864. }
  865. static void
  866. CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  867. {
  868. /* nothing */
  869. }
  870. static void
  871. CDPHandler(const uchar * pkt, unsigned len)
  872. {
  873. const uchar *t;
  874. const ushort *ss;
  875. ushort type, tlen;
  876. uchar applid;
  877. ushort vlan, nvlan;
  878. /* minimum size? */
  879. if (len < sizeof(CDP_SNAP_hdr) + 4)
  880. goto pkt_short;
  881. /* check for valid CDP SNAP header */
  882. if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
  883. return;
  884. pkt += sizeof(CDP_SNAP_hdr);
  885. len -= sizeof(CDP_SNAP_hdr);
  886. /* Version of CDP protocol must be >= 2 and TTL != 0 */
  887. if (pkt[0] < 0x02 || pkt[1] == 0)
  888. return;
  889. /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
  890. if (pkt[0] != 0x02)
  891. printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
  892. pkt[0] & 0xff);
  893. if (CDP_compute_csum(pkt, len) != 0)
  894. return;
  895. pkt += 4;
  896. len -= 4;
  897. vlan = htons(-1);
  898. nvlan = htons(-1);
  899. while (len > 0) {
  900. if (len < 4)
  901. goto pkt_short;
  902. ss = (const ushort *)pkt;
  903. type = ntohs(ss[0]);
  904. tlen = ntohs(ss[1]);
  905. if (tlen > len) {
  906. goto pkt_short;
  907. }
  908. pkt += tlen;
  909. len -= tlen;
  910. ss += 2; /* point ss to the data of the TLV */
  911. tlen -= 4;
  912. switch (type) {
  913. case CDP_DEVICE_ID_TLV:
  914. break;
  915. case CDP_ADDRESS_TLV:
  916. break;
  917. case CDP_PORT_ID_TLV:
  918. break;
  919. case CDP_CAPABILITIES_TLV:
  920. break;
  921. case CDP_VERSION_TLV:
  922. break;
  923. case CDP_PLATFORM_TLV:
  924. break;
  925. case CDP_NATIVE_VLAN_TLV:
  926. nvlan = *ss;
  927. break;
  928. case CDP_APPLIANCE_VLAN_TLV:
  929. t = (const uchar *)ss;
  930. while (tlen > 0) {
  931. if (tlen < 3)
  932. goto pkt_short;
  933. applid = t[0];
  934. ss = (const ushort *)(t + 1);
  935. #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
  936. if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
  937. vlan = *ss;
  938. #else
  939. vlan = ntohs(*ss); /* XXX will this work; dunno */
  940. #endif
  941. t += 3; tlen -= 3;
  942. }
  943. break;
  944. case CDP_TRIGGER_TLV:
  945. break;
  946. case CDP_POWER_CONSUMPTION_TLV:
  947. break;
  948. case CDP_SYSNAME_TLV:
  949. break;
  950. case CDP_SYSOBJECT_TLV:
  951. break;
  952. case CDP_MANAGEMENT_ADDRESS_TLV:
  953. break;
  954. }
  955. }
  956. CDPApplianceVLAN = vlan;
  957. CDPNativeVLAN = nvlan;
  958. CDPOK = 1;
  959. return;
  960. pkt_short:
  961. printf("** CDP packet is too short\n");
  962. return;
  963. }
  964. static void CDPStart(void)
  965. {
  966. #if defined(CONFIG_NET_MULTI)
  967. printf ("Using %s device\n", eth_get_name());
  968. #endif
  969. CDPSeq = 0;
  970. CDPOK = 0;
  971. CDPNativeVLAN = htons(-1);
  972. CDPApplianceVLAN = htons(-1);
  973. NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
  974. NetSetHandler (CDPDummyHandler);
  975. CDPSendTrigger();
  976. }
  977. #endif
  978. void
  979. NetReceive(volatile uchar * inpkt, int len)
  980. {
  981. Ethernet_t *et;
  982. IP_t *ip;
  983. ARP_t *arp;
  984. IPaddr_t tmp;
  985. int x;
  986. uchar *pkt;
  987. #if defined(CONFIG_CMD_CDP)
  988. int iscdp;
  989. #endif
  990. ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
  991. #ifdef ET_DEBUG
  992. printf("packet received\n");
  993. #endif
  994. NetRxPkt = inpkt;
  995. NetRxPktLen = len;
  996. et = (Ethernet_t *)inpkt;
  997. /* too small packet? */
  998. if (len < ETHER_HDR_SIZE)
  999. return;
  1000. #ifdef CONFIG_API
  1001. if (push_packet) {
  1002. (*push_packet)(inpkt, len);
  1003. return;
  1004. }
  1005. #endif
  1006. #if defined(CONFIG_CMD_CDP)
  1007. /* keep track if packet is CDP */
  1008. iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
  1009. #endif
  1010. myvlanid = ntohs(NetOurVLAN);
  1011. if (myvlanid == (ushort)-1)
  1012. myvlanid = VLAN_NONE;
  1013. mynvlanid = ntohs(NetOurNativeVLAN);
  1014. if (mynvlanid == (ushort)-1)
  1015. mynvlanid = VLAN_NONE;
  1016. x = ntohs(et->et_protlen);
  1017. #ifdef ET_DEBUG
  1018. printf("packet received\n");
  1019. #endif
  1020. if (x < 1514) {
  1021. /*
  1022. * Got a 802 packet. Check the other protocol field.
  1023. */
  1024. x = ntohs(et->et_prot);
  1025. ip = (IP_t *)(inpkt + E802_HDR_SIZE);
  1026. len -= E802_HDR_SIZE;
  1027. } else if (x != PROT_VLAN) { /* normal packet */
  1028. ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
  1029. len -= ETHER_HDR_SIZE;
  1030. } else { /* VLAN packet */
  1031. VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
  1032. #ifdef ET_DEBUG
  1033. printf("VLAN packet received\n");
  1034. #endif
  1035. /* too small packet? */
  1036. if (len < VLAN_ETHER_HDR_SIZE)
  1037. return;
  1038. /* if no VLAN active */
  1039. if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
  1040. #if defined(CONFIG_CMD_CDP)
  1041. && iscdp == 0
  1042. #endif
  1043. )
  1044. return;
  1045. cti = ntohs(vet->vet_tag);
  1046. vlanid = cti & VLAN_IDMASK;
  1047. x = ntohs(vet->vet_type);
  1048. ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
  1049. len -= VLAN_ETHER_HDR_SIZE;
  1050. }
  1051. #ifdef ET_DEBUG
  1052. printf("Receive from protocol 0x%x\n", x);
  1053. #endif
  1054. #if defined(CONFIG_CMD_CDP)
  1055. if (iscdp) {
  1056. CDPHandler((uchar *)ip, len);
  1057. return;
  1058. }
  1059. #endif
  1060. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  1061. if (vlanid == VLAN_NONE)
  1062. vlanid = (mynvlanid & VLAN_IDMASK);
  1063. /* not matched? */
  1064. if (vlanid != (myvlanid & VLAN_IDMASK))
  1065. return;
  1066. }
  1067. switch (x) {
  1068. case PROT_ARP:
  1069. /*
  1070. * We have to deal with two types of ARP packets:
  1071. * - REQUEST packets will be answered by sending our
  1072. * IP address - if we know it.
  1073. * - REPLY packates are expected only after we asked
  1074. * for the TFTP server's or the gateway's ethernet
  1075. * address; so if we receive such a packet, we set
  1076. * the server ethernet address
  1077. */
  1078. #ifdef ET_DEBUG
  1079. puts ("Got ARP\n");
  1080. #endif
  1081. arp = (ARP_t *)ip;
  1082. if (len < ARP_HDR_SIZE) {
  1083. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  1084. return;
  1085. }
  1086. if (ntohs(arp->ar_hrd) != ARP_ETHER) {
  1087. return;
  1088. }
  1089. if (ntohs(arp->ar_pro) != PROT_IP) {
  1090. return;
  1091. }
  1092. if (arp->ar_hln != 6) {
  1093. return;
  1094. }
  1095. if (arp->ar_pln != 4) {
  1096. return;
  1097. }
  1098. if (NetOurIP == 0) {
  1099. return;
  1100. }
  1101. if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
  1102. return;
  1103. }
  1104. switch (ntohs(arp->ar_op)) {
  1105. case ARPOP_REQUEST: /* reply with our IP address */
  1106. #ifdef ET_DEBUG
  1107. puts ("Got ARP REQUEST, return our IP\n");
  1108. #endif
  1109. pkt = (uchar *)et;
  1110. pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
  1111. arp->ar_op = htons(ARPOP_REPLY);
  1112. memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
  1113. NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
  1114. memcpy (&arp->ar_data[ 0], NetOurEther, 6);
  1115. NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
  1116. (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
  1117. return;
  1118. case ARPOP_REPLY: /* arp reply */
  1119. /* are we waiting for a reply */
  1120. if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
  1121. break;
  1122. #ifdef ET_DEBUG
  1123. printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
  1124. arp->ar_data[0], arp->ar_data[1],
  1125. arp->ar_data[2], arp->ar_data[3],
  1126. arp->ar_data[4], arp->ar_data[5]);
  1127. #endif
  1128. tmp = NetReadIP(&arp->ar_data[6]);
  1129. /* matched waiting packet's address */
  1130. if (tmp == NetArpWaitReplyIP) {
  1131. #ifdef ET_DEBUG
  1132. puts ("Got it\n");
  1133. #endif
  1134. /* save address for later use */
  1135. memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
  1136. #ifdef CONFIG_NETCONSOLE
  1137. (*packetHandler)(0,0,0,0);
  1138. #endif
  1139. /* modify header, and transmit it */
  1140. memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
  1141. (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
  1142. /* no arp request pending now */
  1143. NetArpWaitPacketIP = 0;
  1144. NetArpWaitTxPacketSize = 0;
  1145. NetArpWaitPacketMAC = NULL;
  1146. }
  1147. return;
  1148. default:
  1149. #ifdef ET_DEBUG
  1150. printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
  1151. #endif
  1152. return;
  1153. }
  1154. break;
  1155. case PROT_RARP:
  1156. #ifdef ET_DEBUG
  1157. puts ("Got RARP\n");
  1158. #endif
  1159. arp = (ARP_t *)ip;
  1160. if (len < ARP_HDR_SIZE) {
  1161. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  1162. return;
  1163. }
  1164. if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
  1165. (ntohs(arp->ar_hrd) != ARP_ETHER) ||
  1166. (ntohs(arp->ar_pro) != PROT_IP) ||
  1167. (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
  1168. puts ("invalid RARP header\n");
  1169. } else {
  1170. NetCopyIP(&NetOurIP, &arp->ar_data[16]);
  1171. if (NetServerIP == 0)
  1172. NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
  1173. memcpy (NetServerEther, &arp->ar_data[ 0], 6);
  1174. (*packetHandler)(0,0,0,0);
  1175. }
  1176. break;
  1177. case PROT_IP:
  1178. #ifdef ET_DEBUG
  1179. puts ("Got IP\n");
  1180. #endif
  1181. if (len < IP_HDR_SIZE) {
  1182. debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
  1183. return;
  1184. }
  1185. if (len < ntohs(ip->ip_len)) {
  1186. printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
  1187. return;
  1188. }
  1189. len = ntohs(ip->ip_len);
  1190. #ifdef ET_DEBUG
  1191. printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
  1192. #endif
  1193. if ((ip->ip_hl_v & 0xf0) != 0x40) {
  1194. return;
  1195. }
  1196. if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
  1197. return;
  1198. }
  1199. /* can't deal with headers > 20 bytes */
  1200. if ((ip->ip_hl_v & 0x0f) > 0x05) {
  1201. return;
  1202. }
  1203. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
  1204. puts ("checksum bad\n");
  1205. return;
  1206. }
  1207. tmp = NetReadIP(&ip->ip_dst);
  1208. if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
  1209. #ifdef CONFIG_MCAST_TFTP
  1210. if (Mcast_addr != tmp)
  1211. #endif
  1212. return;
  1213. }
  1214. /*
  1215. * watch for ICMP host redirects
  1216. *
  1217. * There is no real handler code (yet). We just watch
  1218. * for ICMP host redirect messages. In case anybody
  1219. * sees these messages: please contact me
  1220. * (wd@denx.de), or - even better - send me the
  1221. * necessary fixes :-)
  1222. *
  1223. * Note: in all cases where I have seen this so far
  1224. * it was a problem with the router configuration,
  1225. * for instance when a router was configured in the
  1226. * BOOTP reply, but the TFTP server was on the same
  1227. * subnet. So this is probably a warning that your
  1228. * configuration might be wrong. But I'm not really
  1229. * sure if there aren't any other situations.
  1230. */
  1231. if (ip->ip_p == IPPROTO_ICMP) {
  1232. ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
  1233. switch (icmph->type) {
  1234. case ICMP_REDIRECT:
  1235. if (icmph->code != ICMP_REDIR_HOST)
  1236. return;
  1237. puts (" ICMP Host Redirect to ");
  1238. print_IPaddr(icmph->un.gateway);
  1239. putc(' ');
  1240. return;
  1241. #if defined(CONFIG_CMD_PING)
  1242. case ICMP_ECHO_REPLY:
  1243. /*
  1244. * IP header OK. Pass the packet to the current handler.
  1245. */
  1246. /* XXX point to ip packet */
  1247. (*packetHandler)((uchar *)ip, 0, 0, 0);
  1248. return;
  1249. case ICMP_ECHO_REQUEST:
  1250. #ifdef ET_DEBUG
  1251. printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
  1252. ETHER_HDR_SIZE + len);
  1253. #endif
  1254. memcpy (&et->et_dest[0], &et->et_src[0], 6);
  1255. memcpy (&et->et_src[ 0], NetOurEther, 6);
  1256. ip->ip_sum = 0;
  1257. ip->ip_off = 0;
  1258. NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
  1259. NetCopyIP((void*)&ip->ip_src, &NetOurIP);
  1260. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
  1261. icmph->type = ICMP_ECHO_REPLY;
  1262. icmph->checksum = 0;
  1263. icmph->checksum = ~NetCksum((uchar *)icmph,
  1264. (len - IP_HDR_SIZE_NO_UDP) >> 1);
  1265. (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
  1266. return;
  1267. #endif
  1268. default:
  1269. return;
  1270. }
  1271. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  1272. return;
  1273. }
  1274. #ifdef CONFIG_UDP_CHECKSUM
  1275. if (ip->udp_xsum != 0) {
  1276. ulong xsum;
  1277. ushort *sumptr;
  1278. ushort sumlen;
  1279. xsum = ip->ip_p;
  1280. xsum += (ntohs(ip->udp_len));
  1281. xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
  1282. xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
  1283. xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
  1284. xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
  1285. sumlen = ntohs(ip->udp_len);
  1286. sumptr = (ushort *) &(ip->udp_src);
  1287. while (sumlen > 1) {
  1288. ushort sumdata;
  1289. sumdata = *sumptr++;
  1290. xsum += ntohs(sumdata);
  1291. sumlen -= 2;
  1292. }
  1293. if (sumlen > 0) {
  1294. ushort sumdata;
  1295. sumdata = *(unsigned char *) sumptr;
  1296. sumdata = (sumdata << 8) & 0xff00;
  1297. xsum += sumdata;
  1298. }
  1299. while ((xsum >> 16) != 0) {
  1300. xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
  1301. }
  1302. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  1303. printf(" UDP wrong checksum %08lx %08x\n",
  1304. xsum, ntohs(ip->udp_xsum));
  1305. return;
  1306. }
  1307. }
  1308. #endif
  1309. #ifdef CONFIG_NETCONSOLE
  1310. nc_input_packet((uchar *)ip +IP_HDR_SIZE,
  1311. ntohs(ip->udp_dst),
  1312. ntohs(ip->udp_src),
  1313. ntohs(ip->udp_len) - 8);
  1314. #endif
  1315. /*
  1316. * IP header OK. Pass the packet to the current handler.
  1317. */
  1318. (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
  1319. ntohs(ip->udp_dst),
  1320. ntohs(ip->udp_src),
  1321. ntohs(ip->udp_len) - 8);
  1322. break;
  1323. }
  1324. }
  1325. /**********************************************************************/
  1326. static int net_check_prereq (proto_t protocol)
  1327. {
  1328. switch (protocol) {
  1329. /* Fall through */
  1330. #if defined(CONFIG_CMD_PING)
  1331. case PING:
  1332. if (NetPingIP == 0) {
  1333. puts ("*** ERROR: ping address not given\n");
  1334. return (1);
  1335. }
  1336. goto common;
  1337. #endif
  1338. #if defined(CONFIG_CMD_SNTP)
  1339. case SNTP:
  1340. if (NetNtpServerIP == 0) {
  1341. puts ("*** ERROR: NTP server address not given\n");
  1342. return (1);
  1343. }
  1344. goto common;
  1345. #endif
  1346. #if defined(CONFIG_CMD_NFS)
  1347. case NFS:
  1348. #endif
  1349. case NETCONS:
  1350. case TFTP:
  1351. if (NetServerIP == 0) {
  1352. puts ("*** ERROR: `serverip' not set\n");
  1353. return (1);
  1354. }
  1355. #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
  1356. common:
  1357. #endif
  1358. if (NetOurIP == 0) {
  1359. puts ("*** ERROR: `ipaddr' not set\n");
  1360. return (1);
  1361. }
  1362. /* Fall through */
  1363. case DHCP:
  1364. case RARP:
  1365. case BOOTP:
  1366. case CDP:
  1367. if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  1368. #ifdef CONFIG_NET_MULTI
  1369. extern int eth_get_dev_index (void);
  1370. int num = eth_get_dev_index ();
  1371. switch (num) {
  1372. case -1:
  1373. puts ("*** ERROR: No ethernet found.\n");
  1374. return (1);
  1375. case 0:
  1376. puts ("*** ERROR: `ethaddr' not set\n");
  1377. break;
  1378. default:
  1379. printf ("*** ERROR: `eth%daddr' not set\n",
  1380. num);
  1381. break;
  1382. }
  1383. NetStartAgain ();
  1384. return (2);
  1385. #else
  1386. puts ("*** ERROR: `ethaddr' not set\n");
  1387. return (1);
  1388. #endif
  1389. }
  1390. /* Fall through */
  1391. default:
  1392. return (0);
  1393. }
  1394. return (0); /* OK */
  1395. }
  1396. /**********************************************************************/
  1397. int
  1398. NetCksumOk(uchar * ptr, int len)
  1399. {
  1400. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  1401. }
  1402. unsigned
  1403. NetCksum(uchar * ptr, int len)
  1404. {
  1405. ulong xsum;
  1406. ushort *p = (ushort *)ptr;
  1407. xsum = 0;
  1408. while (len-- > 0)
  1409. xsum += *p++;
  1410. xsum = (xsum & 0xffff) + (xsum >> 16);
  1411. xsum = (xsum & 0xffff) + (xsum >> 16);
  1412. return (xsum & 0xffff);
  1413. }
  1414. int
  1415. NetEthHdrSize(void)
  1416. {
  1417. ushort myvlanid;
  1418. myvlanid = ntohs(NetOurVLAN);
  1419. if (myvlanid == (ushort)-1)
  1420. myvlanid = VLAN_NONE;
  1421. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
  1422. }
  1423. int
  1424. NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
  1425. {
  1426. Ethernet_t *et = (Ethernet_t *)xet;
  1427. ushort myvlanid;
  1428. myvlanid = ntohs(NetOurVLAN);
  1429. if (myvlanid == (ushort)-1)
  1430. myvlanid = VLAN_NONE;
  1431. memcpy (et->et_dest, addr, 6);
  1432. memcpy (et->et_src, NetOurEther, 6);
  1433. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1434. et->et_protlen = htons(prot);
  1435. return ETHER_HDR_SIZE;
  1436. } else {
  1437. VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
  1438. vet->vet_vlan_type = htons(PROT_VLAN);
  1439. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1440. vet->vet_type = htons(prot);
  1441. return VLAN_ETHER_HDR_SIZE;
  1442. }
  1443. }
  1444. void
  1445. NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
  1446. {
  1447. volatile IP_t *ip = (IP_t *)xip;
  1448. /*
  1449. * If the data is an odd number of bytes, zero the
  1450. * byte after the last byte so that the checksum
  1451. * will work.
  1452. */
  1453. if (len & 1)
  1454. xip[IP_HDR_SIZE + len] = 0;
  1455. /*
  1456. * Construct an IP and UDP header.
  1457. * (need to set no fragment bit - XXX)
  1458. */
  1459. ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
  1460. ip->ip_tos = 0;
  1461. ip->ip_len = htons(IP_HDR_SIZE + len);
  1462. ip->ip_id = htons(NetIPID++);
  1463. ip->ip_off = htons(0x4000); /* No fragmentation */
  1464. ip->ip_ttl = 255;
  1465. ip->ip_p = 17; /* UDP */
  1466. ip->ip_sum = 0;
  1467. NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
  1468. NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
  1469. ip->udp_src = htons(sport);
  1470. ip->udp_dst = htons(dport);
  1471. ip->udp_len = htons(8 + len);
  1472. ip->udp_xsum = 0;
  1473. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  1474. }
  1475. void copy_filename (char *dst, char *src, int size)
  1476. {
  1477. if (*src && (*src == '"')) {
  1478. ++src;
  1479. --size;
  1480. }
  1481. while ((--size > 0) && *src && (*src != '"')) {
  1482. *dst++ = *src++;
  1483. }
  1484. *dst = '\0';
  1485. }
  1486. #endif
  1487. void ip_to_string (IPaddr_t x, char *s)
  1488. {
  1489. x = ntohl (x);
  1490. sprintf (s, "%d.%d.%d.%d",
  1491. (int) ((x >> 24) & 0xff),
  1492. (int) ((x >> 16) & 0xff),
  1493. (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
  1494. );
  1495. }
  1496. IPaddr_t string_to_ip(char *s)
  1497. {
  1498. IPaddr_t addr;
  1499. char *e;
  1500. int i;
  1501. if (s == NULL)
  1502. return(0);
  1503. for (addr=0, i=0; i<4; ++i) {
  1504. ulong val = s ? simple_strtoul(s, &e, 10) : 0;
  1505. addr <<= 8;
  1506. addr |= (val & 0xFF);
  1507. if (s) {
  1508. s = (*e) ? e+1 : e;
  1509. }
  1510. }
  1511. return (htonl(addr));
  1512. }
  1513. void VLAN_to_string(ushort x, char *s)
  1514. {
  1515. x = ntohs(x);
  1516. if (x == (ushort)-1)
  1517. x = VLAN_NONE;
  1518. if (x == VLAN_NONE)
  1519. strcpy(s, "none");
  1520. else
  1521. sprintf(s, "%d", x & VLAN_IDMASK);
  1522. }
  1523. ushort string_to_VLAN(char *s)
  1524. {
  1525. ushort id;
  1526. if (s == NULL)
  1527. return htons(VLAN_NONE);
  1528. if (*s < '0' || *s > '9')
  1529. id = VLAN_NONE;
  1530. else
  1531. id = (ushort)simple_strtoul(s, NULL, 10);
  1532. return htons(id);
  1533. }
  1534. void print_IPaddr (IPaddr_t x)
  1535. {
  1536. char tmp[16];
  1537. ip_to_string (x, tmp);
  1538. puts (tmp);
  1539. }
  1540. IPaddr_t getenv_IPaddr (char *var)
  1541. {
  1542. return (string_to_ip(getenv(var)));
  1543. }
  1544. ushort getenv_VLAN(char *var)
  1545. {
  1546. return (string_to_VLAN(getenv(var)));
  1547. }