net.c 38 KB

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