net.c 38 KB

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