net.c 38 KB

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