net.c 37 KB

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