net.c 44 KB

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