net.c 38 KB

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