net.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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 <command.h>
  76. #include <net.h>
  77. #if defined(CONFIG_STATUS_LED)
  78. #include <miiphy.h>
  79. #include <status_led.h>
  80. #endif
  81. #include <watchdog.h>
  82. #include <linux/compiler.h>
  83. #include "arp.h"
  84. #include "bootp.h"
  85. #include "cdp.h"
  86. #if defined(CONFIG_CMD_DNS)
  87. #include "dns.h"
  88. #endif
  89. #include "nfs.h"
  90. #include "ping.h"
  91. #include "rarp.h"
  92. #if defined(CONFIG_CMD_SNTP)
  93. #include "sntp.h"
  94. #endif
  95. #include "tftp.h"
  96. DECLARE_GLOBAL_DATA_PTR;
  97. /** BOOTP EXTENTIONS **/
  98. /* Our subnet mask (0=unknown) */
  99. IPaddr_t NetOurSubnetMask;
  100. /* Our gateways IP address */
  101. IPaddr_t NetOurGatewayIP;
  102. /* Our DNS IP address */
  103. IPaddr_t NetOurDNSIP;
  104. #if defined(CONFIG_BOOTP_DNS2)
  105. /* Our 2nd DNS IP address */
  106. IPaddr_t NetOurDNS2IP;
  107. #endif
  108. /* Our NIS domain */
  109. char NetOurNISDomain[32] = {0,};
  110. /* Our hostname */
  111. char NetOurHostName[32] = {0,};
  112. /* Our bootpath */
  113. char NetOurRootPath[64] = {0,};
  114. /* Our bootfile size in blocks */
  115. ushort NetBootFileSize;
  116. #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
  117. IPaddr_t Mcast_addr;
  118. #endif
  119. /** END OF BOOTP EXTENTIONS **/
  120. /* The actual transferred size of the bootfile (in bytes) */
  121. ulong NetBootFileXferSize;
  122. /* Our ethernet address */
  123. uchar NetOurEther[6];
  124. /* Boot server enet address */
  125. uchar NetServerEther[6];
  126. /* Our IP addr (0 = unknown) */
  127. IPaddr_t NetOurIP;
  128. /* Server IP addr (0 = unknown) */
  129. IPaddr_t NetServerIP;
  130. /* Current receive packet */
  131. uchar *NetRxPacket;
  132. /* Current rx packet length */
  133. int NetRxPacketLen;
  134. /* IP packet ID */
  135. unsigned NetIPID;
  136. /* Ethernet bcast address */
  137. uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  138. uchar NetEtherNullAddr[6];
  139. #ifdef CONFIG_API
  140. void (*push_packet)(void *, int len) = 0;
  141. #endif
  142. /* Network loop state */
  143. enum net_loop_state net_state;
  144. /* Tried all network devices */
  145. int NetRestartWrap;
  146. /* Network loop restarted */
  147. static int NetRestarted;
  148. /* At least one device configured */
  149. static int NetDevExists;
  150. /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
  151. /* default is without VLAN */
  152. ushort NetOurVLAN = 0xFFFF;
  153. /* ditto */
  154. ushort NetOurNativeVLAN = 0xFFFF;
  155. /* Boot File name */
  156. char BootFile[128];
  157. #if defined(CONFIG_CMD_SNTP)
  158. /* NTP server IP address */
  159. IPaddr_t NetNtpServerIP;
  160. /* offset time from UTC */
  161. int NetTimeOffset;
  162. #endif
  163. uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
  164. /* Receive packet */
  165. uchar *NetRxPackets[PKTBUFSRX];
  166. /* Current UDP RX packet handler */
  167. static rxhand_f *udp_packet_handler;
  168. /* Current ARP RX packet handler */
  169. static rxhand_f *arp_packet_handler;
  170. #ifdef CONFIG_CMD_TFTPPUT
  171. /* Current ICMP rx handler */
  172. static rxhand_icmp_f *packet_icmp_handler;
  173. #endif
  174. /* Current timeout handler */
  175. static thand_f *timeHandler;
  176. /* Time base value */
  177. static ulong timeStart;
  178. /* Current timeout value */
  179. static ulong timeDelta;
  180. /* THE transmit packet */
  181. uchar *NetTxPacket;
  182. static int net_check_prereq(enum proto_t protocol);
  183. static int NetTryCount;
  184. /**********************************************************************/
  185. /*
  186. * Check if autoload is enabled. If so, use either NFS or TFTP to download
  187. * the boot file.
  188. */
  189. void net_auto_load(void)
  190. {
  191. const char *s = getenv("autoload");
  192. if (s != NULL) {
  193. if (*s == 'n') {
  194. /*
  195. * Just use BOOTP/RARP to configure system;
  196. * Do not use TFTP to load the bootfile.
  197. */
  198. net_set_state(NETLOOP_SUCCESS);
  199. return;
  200. }
  201. #if defined(CONFIG_CMD_NFS)
  202. if (strcmp(s, "NFS") == 0) {
  203. /*
  204. * Use NFS to load the bootfile.
  205. */
  206. NfsStart();
  207. return;
  208. }
  209. #endif
  210. }
  211. TftpStart(TFTPGET);
  212. }
  213. static void NetInitLoop(enum proto_t protocol)
  214. {
  215. static int env_changed_id;
  216. int env_id = get_env_id();
  217. /* update only when the environment has changed */
  218. if (env_changed_id != env_id) {
  219. NetOurIP = getenv_IPaddr("ipaddr");
  220. NetOurGatewayIP = getenv_IPaddr("gatewayip");
  221. NetOurSubnetMask = getenv_IPaddr("netmask");
  222. NetServerIP = getenv_IPaddr("serverip");
  223. NetOurNativeVLAN = getenv_VLAN("nvlan");
  224. NetOurVLAN = getenv_VLAN("vlan");
  225. #if defined(CONFIG_CMD_DNS)
  226. NetOurDNSIP = getenv_IPaddr("dnsip");
  227. #endif
  228. env_changed_id = env_id;
  229. }
  230. return;
  231. }
  232. static void net_clear_handlers(void)
  233. {
  234. net_set_udp_handler(NULL);
  235. net_set_arp_handler(NULL);
  236. NetSetTimeout(0, NULL);
  237. }
  238. static void net_cleanup_loop(void)
  239. {
  240. net_clear_handlers();
  241. }
  242. /**********************************************************************/
  243. /*
  244. * Main network processing loop.
  245. */
  246. int NetLoop(enum proto_t protocol)
  247. {
  248. int i;
  249. bd_t *bd = gd->bd;
  250. int ret = -1;
  251. NetRestarted = 0;
  252. NetDevExists = 0;
  253. NetTxPacket = NULL;
  254. NetTryCount = 1;
  255. ArpInit();
  256. net_clear_handlers();
  257. /*
  258. * Setup packet buffers, aligned correctly.
  259. */
  260. NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
  261. NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
  262. for (i = 0; i < PKTBUFSRX; i++)
  263. NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
  264. bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
  265. eth_halt();
  266. eth_set_current();
  267. if (eth_init(bd) < 0) {
  268. eth_halt();
  269. return -1;
  270. }
  271. restart:
  272. memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
  273. net_set_state(NETLOOP_CONTINUE);
  274. /*
  275. * Start the ball rolling with the given start function. From
  276. * here on, this code is a state machine driven by received
  277. * packets and timer events.
  278. */
  279. NetInitLoop(protocol);
  280. switch (net_check_prereq(protocol)) {
  281. case 1:
  282. /* network not configured */
  283. eth_halt();
  284. return -1;
  285. case 2:
  286. /* network device not configured */
  287. break;
  288. case 0:
  289. NetDevExists = 1;
  290. NetBootFileXferSize = 0;
  291. switch (protocol) {
  292. case TFTPGET:
  293. #ifdef CONFIG_CMD_TFTPPUT
  294. case TFTPPUT:
  295. #endif
  296. /* always use ARP to get server ethernet address */
  297. TftpStart(protocol);
  298. break;
  299. #ifdef CONFIG_CMD_TFTPSRV
  300. case TFTPSRV:
  301. TftpStartServer();
  302. break;
  303. #endif
  304. #if defined(CONFIG_CMD_DHCP)
  305. case DHCP:
  306. BootpTry = 0;
  307. NetOurIP = 0;
  308. DhcpRequest(); /* Basically same as BOOTP */
  309. break;
  310. #endif
  311. case BOOTP:
  312. BootpTry = 0;
  313. NetOurIP = 0;
  314. BootpRequest();
  315. break;
  316. #if defined(CONFIG_CMD_RARP)
  317. case RARP:
  318. RarpTry = 0;
  319. NetOurIP = 0;
  320. RarpRequest();
  321. break;
  322. #endif
  323. #if defined(CONFIG_CMD_PING)
  324. case PING:
  325. ping_start();
  326. break;
  327. #endif
  328. #if defined(CONFIG_CMD_NFS)
  329. case NFS:
  330. NfsStart();
  331. break;
  332. #endif
  333. #if defined(CONFIG_CMD_CDP)
  334. case CDP:
  335. CDPStart();
  336. break;
  337. #endif
  338. #ifdef CONFIG_NETCONSOLE
  339. case NETCONS:
  340. NcStart();
  341. break;
  342. #endif
  343. #if defined(CONFIG_CMD_SNTP)
  344. case SNTP:
  345. SntpStart();
  346. break;
  347. #endif
  348. #if defined(CONFIG_CMD_DNS)
  349. case DNS:
  350. DnsStart();
  351. break;
  352. #endif
  353. default:
  354. break;
  355. }
  356. break;
  357. }
  358. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  359. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
  360. defined(CONFIG_STATUS_LED) && \
  361. defined(STATUS_LED_RED)
  362. /*
  363. * Echo the inverted link state to the fault LED.
  364. */
  365. if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
  366. status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
  367. else
  368. status_led_set(STATUS_LED_RED, STATUS_LED_ON);
  369. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  370. #endif /* CONFIG_MII, ... */
  371. /*
  372. * Main packet reception loop. Loop receiving packets until
  373. * someone sets `net_state' to a state that terminates.
  374. */
  375. for (;;) {
  376. WATCHDOG_RESET();
  377. #ifdef CONFIG_SHOW_ACTIVITY
  378. show_activity(1);
  379. #endif
  380. /*
  381. * Check the ethernet for a new packet. The ethernet
  382. * receive routine will process it.
  383. */
  384. eth_rx();
  385. /*
  386. * Abort if ctrl-c was pressed.
  387. */
  388. if (ctrlc()) {
  389. net_cleanup_loop();
  390. eth_halt();
  391. puts("\nAbort\n");
  392. goto done;
  393. }
  394. ArpTimeoutCheck();
  395. /*
  396. * Check for a timeout, and run the timeout handler
  397. * if we have one.
  398. */
  399. if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
  400. thand_f *x;
  401. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  402. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
  403. defined(CONFIG_STATUS_LED) && \
  404. defined(STATUS_LED_RED)
  405. /*
  406. * Echo the inverted link state to the fault LED.
  407. */
  408. if (miiphy_link(eth_get_dev()->name,
  409. CONFIG_SYS_FAULT_MII_ADDR)) {
  410. status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
  411. } else {
  412. status_led_set(STATUS_LED_RED, STATUS_LED_ON);
  413. }
  414. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  415. #endif /* CONFIG_MII, ... */
  416. x = timeHandler;
  417. timeHandler = (thand_f *)0;
  418. (*x)();
  419. }
  420. switch (net_state) {
  421. case NETLOOP_RESTART:
  422. NetRestarted = 1;
  423. goto restart;
  424. case NETLOOP_SUCCESS:
  425. net_cleanup_loop();
  426. if (NetBootFileXferSize > 0) {
  427. char buf[20];
  428. printf("Bytes transferred = %ld (%lx hex)\n",
  429. NetBootFileXferSize,
  430. NetBootFileXferSize);
  431. sprintf(buf, "%lX", NetBootFileXferSize);
  432. setenv("filesize", buf);
  433. sprintf(buf, "%lX", (unsigned long)load_addr);
  434. setenv("fileaddr", buf);
  435. }
  436. eth_halt();
  437. ret = NetBootFileXferSize;
  438. goto done;
  439. case NETLOOP_FAIL:
  440. net_cleanup_loop();
  441. goto done;
  442. case NETLOOP_CONTINUE:
  443. continue;
  444. }
  445. }
  446. done:
  447. #ifdef CONFIG_CMD_TFTPPUT
  448. /* Clear out the handlers */
  449. net_set_udp_handler(NULL);
  450. net_set_icmp_handler(NULL);
  451. #endif
  452. return ret;
  453. }
  454. /**********************************************************************/
  455. static void
  456. startAgainTimeout(void)
  457. {
  458. net_set_state(NETLOOP_RESTART);
  459. }
  460. void NetStartAgain(void)
  461. {
  462. char *nretry;
  463. int retry_forever = 0;
  464. unsigned long retrycnt = 0;
  465. nretry = getenv("netretry");
  466. if (nretry) {
  467. if (!strcmp(nretry, "yes"))
  468. retry_forever = 1;
  469. else if (!strcmp(nretry, "no"))
  470. retrycnt = 0;
  471. else if (!strcmp(nretry, "once"))
  472. retrycnt = 1;
  473. else
  474. retrycnt = simple_strtoul(nretry, NULL, 0);
  475. } else
  476. retry_forever = 1;
  477. if ((!retry_forever) && (NetTryCount >= retrycnt)) {
  478. eth_halt();
  479. net_set_state(NETLOOP_FAIL);
  480. return;
  481. }
  482. NetTryCount++;
  483. eth_halt();
  484. #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
  485. eth_try_another(!NetRestarted);
  486. #endif
  487. eth_init(gd->bd);
  488. if (NetRestartWrap) {
  489. NetRestartWrap = 0;
  490. if (NetDevExists) {
  491. NetSetTimeout(10000UL, startAgainTimeout);
  492. net_set_udp_handler(NULL);
  493. } else {
  494. net_set_state(NETLOOP_FAIL);
  495. }
  496. } else {
  497. net_set_state(NETLOOP_RESTART);
  498. }
  499. }
  500. /**********************************************************************/
  501. /*
  502. * Miscelaneous bits.
  503. */
  504. static void dummy_handler(uchar *pkt, unsigned dport,
  505. IPaddr_t sip, unsigned sport,
  506. unsigned len)
  507. {
  508. }
  509. rxhand_f *net_get_udp_handler(void)
  510. {
  511. return udp_packet_handler;
  512. }
  513. void net_set_udp_handler(rxhand_f *f)
  514. {
  515. if (f == NULL)
  516. udp_packet_handler = dummy_handler;
  517. else
  518. udp_packet_handler = f;
  519. }
  520. rxhand_f *net_get_arp_handler(void)
  521. {
  522. return arp_packet_handler;
  523. }
  524. void net_set_arp_handler(rxhand_f *f)
  525. {
  526. if (f == NULL)
  527. arp_packet_handler = dummy_handler;
  528. else
  529. arp_packet_handler = f;
  530. }
  531. #ifdef CONFIG_CMD_TFTPPUT
  532. void net_set_icmp_handler(rxhand_icmp_f *f)
  533. {
  534. packet_icmp_handler = f;
  535. }
  536. #endif
  537. void
  538. NetSetTimeout(ulong iv, thand_f *f)
  539. {
  540. if (iv == 0) {
  541. timeHandler = (thand_f *)0;
  542. } else {
  543. timeHandler = f;
  544. timeStart = get_timer(0);
  545. timeDelta = iv;
  546. }
  547. }
  548. int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
  549. int payload_len)
  550. {
  551. uchar *pkt;
  552. int need_arp = 0;
  553. int eth_hdr_size;
  554. int pkt_hdr_size;
  555. /* convert to new style broadcast */
  556. if (dest == 0)
  557. dest = 0xFFFFFFFF;
  558. /* if broadcast, make the ether address a broadcast and don't do ARP */
  559. if (dest == 0xFFFFFFFF)
  560. ether = NetBcastAddr;
  561. /*
  562. * if MAC address was not discovered yet, save the packet and do
  563. * an ARP request
  564. */
  565. if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
  566. need_arp = 1;
  567. pkt = NetArpWaitTxPacket;
  568. } else
  569. pkt = (uchar *)NetTxPacket;
  570. eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
  571. pkt += eth_hdr_size;
  572. net_set_udp_header(pkt, dest, dport, sport, payload_len);
  573. pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
  574. if (need_arp) {
  575. debug("sending ARP for %pI4\n", &dest);
  576. /* save the ip and eth addr for the packet to send after arp */
  577. NetArpWaitPacketIP = dest;
  578. NetArpWaitPacketMAC = ether;
  579. /*
  580. * Copy the packet data from the NetTxPacket into the
  581. * NetArpWaitTxPacket to send after arp
  582. */
  583. memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket +
  584. pkt_hdr_size, payload_len);
  585. /* size of the waiting packet */
  586. NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
  587. /* and do the ARP request */
  588. NetArpWaitTry = 1;
  589. NetArpWaitTimerStart = get_timer(0);
  590. ArpRequest();
  591. return 1; /* waiting */
  592. } else {
  593. debug("sending UDP to %pI4/%pM\n", &dest, ether);
  594. NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
  595. return 0; /* transmitted */
  596. }
  597. }
  598. #ifdef CONFIG_IP_DEFRAG
  599. /*
  600. * This function collects fragments in a single packet, according
  601. * to the algorithm in RFC815. It returns NULL or the pointer to
  602. * a complete packet, in static storage
  603. */
  604. #ifndef CONFIG_NET_MAXDEFRAG
  605. #define CONFIG_NET_MAXDEFRAG 16384
  606. #endif
  607. /*
  608. * MAXDEFRAG, above, is chosen in the config file and is real data
  609. * so we need to add the NFS overhead, which is more than TFTP.
  610. * To use sizeof in the internal unnamed structures, we need a real
  611. * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
  612. * The compiler doesn't complain nor allocates the actual structure
  613. */
  614. static struct rpc_t rpc_specimen;
  615. #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
  616. #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
  617. /*
  618. * this is the packet being assembled, either data or frag control.
  619. * Fragments go by 8 bytes, so this union must be 8 bytes long
  620. */
  621. struct hole {
  622. /* first_byte is address of this structure */
  623. u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
  624. u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
  625. u16 prev_hole; /* index of prev, 0 == none */
  626. u16 unused;
  627. };
  628. static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
  629. {
  630. static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
  631. static u16 first_hole, total_len;
  632. struct hole *payload, *thisfrag, *h, *newh;
  633. struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
  634. uchar *indata = (uchar *)ip;
  635. int offset8, start, len, done = 0;
  636. u16 ip_off = ntohs(ip->ip_off);
  637. /* payload starts after IP header, this fragment is in there */
  638. payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
  639. offset8 = (ip_off & IP_OFFS);
  640. thisfrag = payload + offset8;
  641. start = offset8 * 8;
  642. len = ntohs(ip->ip_len) - IP_HDR_SIZE;
  643. if (start + len > IP_MAXUDP) /* fragment extends too far */
  644. return NULL;
  645. if (!total_len || localip->ip_id != ip->ip_id) {
  646. /* new (or different) packet, reset structs */
  647. total_len = 0xffff;
  648. payload[0].last_byte = ~0;
  649. payload[0].next_hole = 0;
  650. payload[0].prev_hole = 0;
  651. first_hole = 0;
  652. /* any IP header will work, copy the first we received */
  653. memcpy(localip, ip, IP_HDR_SIZE);
  654. }
  655. /*
  656. * What follows is the reassembly algorithm. We use the payload
  657. * array as a linked list of hole descriptors, as each hole starts
  658. * at a multiple of 8 bytes. However, last byte can be whatever value,
  659. * so it is represented as byte count, not as 8-byte blocks.
  660. */
  661. h = payload + first_hole;
  662. while (h->last_byte < start) {
  663. if (!h->next_hole) {
  664. /* no hole that far away */
  665. return NULL;
  666. }
  667. h = payload + h->next_hole;
  668. }
  669. /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
  670. if (offset8 + ((len + 7) / 8) <= h - payload) {
  671. /* no overlap with holes (dup fragment?) */
  672. return NULL;
  673. }
  674. if (!(ip_off & IP_FLAGS_MFRAG)) {
  675. /* no more fragmentss: truncate this (last) hole */
  676. total_len = start + len;
  677. h->last_byte = start + len;
  678. }
  679. /*
  680. * There is some overlap: fix the hole list. This code doesn't
  681. * deal with a fragment that overlaps with two different holes
  682. * (thus being a superset of a previously-received fragment).
  683. */
  684. if ((h >= thisfrag) && (h->last_byte <= start + len)) {
  685. /* complete overlap with hole: remove hole */
  686. if (!h->prev_hole && !h->next_hole) {
  687. /* last remaining hole */
  688. done = 1;
  689. } else if (!h->prev_hole) {
  690. /* first hole */
  691. first_hole = h->next_hole;
  692. payload[h->next_hole].prev_hole = 0;
  693. } else if (!h->next_hole) {
  694. /* last hole */
  695. payload[h->prev_hole].next_hole = 0;
  696. } else {
  697. /* in the middle of the list */
  698. payload[h->next_hole].prev_hole = h->prev_hole;
  699. payload[h->prev_hole].next_hole = h->next_hole;
  700. }
  701. } else if (h->last_byte <= start + len) {
  702. /* overlaps with final part of the hole: shorten this hole */
  703. h->last_byte = start;
  704. } else if (h >= thisfrag) {
  705. /* overlaps with initial part of the hole: move this hole */
  706. newh = thisfrag + (len / 8);
  707. *newh = *h;
  708. h = newh;
  709. if (h->next_hole)
  710. payload[h->next_hole].prev_hole = (h - payload);
  711. if (h->prev_hole)
  712. payload[h->prev_hole].next_hole = (h - payload);
  713. else
  714. first_hole = (h - payload);
  715. } else {
  716. /* fragment sits in the middle: split the hole */
  717. newh = thisfrag + (len / 8);
  718. *newh = *h;
  719. h->last_byte = start;
  720. h->next_hole = (newh - payload);
  721. newh->prev_hole = (h - payload);
  722. if (newh->next_hole)
  723. payload[newh->next_hole].prev_hole = (newh - payload);
  724. }
  725. /* finally copy this fragment and possibly return whole packet */
  726. memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
  727. if (!done)
  728. return NULL;
  729. localip->ip_len = htons(total_len);
  730. *lenp = total_len + IP_HDR_SIZE;
  731. return localip;
  732. }
  733. static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
  734. {
  735. u16 ip_off = ntohs(ip->ip_off);
  736. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  737. return ip; /* not a fragment */
  738. return __NetDefragment(ip, lenp);
  739. }
  740. #else /* !CONFIG_IP_DEFRAG */
  741. static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
  742. {
  743. u16 ip_off = ntohs(ip->ip_off);
  744. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  745. return ip; /* not a fragment */
  746. return NULL;
  747. }
  748. #endif
  749. /**
  750. * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
  751. * drop others.
  752. *
  753. * @parma ip IP packet containing the ICMP
  754. */
  755. static void receive_icmp(struct ip_udp_hdr *ip, int len,
  756. IPaddr_t src_ip, struct ethernet_hdr *et)
  757. {
  758. struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
  759. switch (icmph->type) {
  760. case ICMP_REDIRECT:
  761. if (icmph->code != ICMP_REDIR_HOST)
  762. return;
  763. printf(" ICMP Host Redirect to %pI4 ",
  764. &icmph->un.gateway);
  765. break;
  766. default:
  767. #if defined(CONFIG_CMD_PING)
  768. ping_receive(et, ip, len);
  769. #endif
  770. #ifdef CONFIG_CMD_TFTPPUT
  771. if (packet_icmp_handler)
  772. packet_icmp_handler(icmph->type, icmph->code,
  773. ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
  774. icmph->un.data, ntohs(ip->udp_len));
  775. #endif
  776. break;
  777. }
  778. }
  779. void
  780. NetReceive(uchar *inpkt, int len)
  781. {
  782. struct ethernet_hdr *et;
  783. struct ip_udp_hdr *ip;
  784. IPaddr_t dst_ip;
  785. IPaddr_t src_ip;
  786. int eth_proto;
  787. #if defined(CONFIG_CMD_CDP)
  788. int iscdp;
  789. #endif
  790. ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
  791. debug("packet received\n");
  792. NetRxPacket = inpkt;
  793. NetRxPacketLen = len;
  794. et = (struct ethernet_hdr *)inpkt;
  795. /* too small packet? */
  796. if (len < ETHER_HDR_SIZE)
  797. return;
  798. #ifdef CONFIG_API
  799. if (push_packet) {
  800. (*push_packet)(inpkt, len);
  801. return;
  802. }
  803. #endif
  804. #if defined(CONFIG_CMD_CDP)
  805. /* keep track if packet is CDP */
  806. iscdp = is_cdp_packet(et->et_dest);
  807. #endif
  808. myvlanid = ntohs(NetOurVLAN);
  809. if (myvlanid == (ushort)-1)
  810. myvlanid = VLAN_NONE;
  811. mynvlanid = ntohs(NetOurNativeVLAN);
  812. if (mynvlanid == (ushort)-1)
  813. mynvlanid = VLAN_NONE;
  814. eth_proto = ntohs(et->et_protlen);
  815. debug("packet received\n");
  816. if (eth_proto < 1514) {
  817. struct e802_hdr *et802 = (struct e802_hdr *)et;
  818. /*
  819. * Got a 802.2 packet. Check the other protocol field.
  820. * XXX VLAN over 802.2+SNAP not implemented!
  821. */
  822. eth_proto = ntohs(et802->et_prot);
  823. ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
  824. len -= E802_HDR_SIZE;
  825. } else if (eth_proto != PROT_VLAN) { /* normal packet */
  826. ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
  827. len -= ETHER_HDR_SIZE;
  828. } else { /* VLAN packet */
  829. struct vlan_ethernet_hdr *vet =
  830. (struct vlan_ethernet_hdr *)et;
  831. debug("VLAN packet received\n");
  832. /* too small packet? */
  833. if (len < VLAN_ETHER_HDR_SIZE)
  834. return;
  835. /* if no VLAN active */
  836. if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
  837. #if defined(CONFIG_CMD_CDP)
  838. && iscdp == 0
  839. #endif
  840. )
  841. return;
  842. cti = ntohs(vet->vet_tag);
  843. vlanid = cti & VLAN_IDMASK;
  844. eth_proto = ntohs(vet->vet_type);
  845. ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
  846. len -= VLAN_ETHER_HDR_SIZE;
  847. }
  848. debug("Receive from protocol 0x%x\n", eth_proto);
  849. #if defined(CONFIG_CMD_CDP)
  850. if (iscdp) {
  851. cdp_receive((uchar *)ip, len);
  852. return;
  853. }
  854. #endif
  855. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  856. if (vlanid == VLAN_NONE)
  857. vlanid = (mynvlanid & VLAN_IDMASK);
  858. /* not matched? */
  859. if (vlanid != (myvlanid & VLAN_IDMASK))
  860. return;
  861. }
  862. switch (eth_proto) {
  863. case PROT_ARP:
  864. ArpReceive(et, ip, len);
  865. break;
  866. #ifdef CONFIG_CMD_RARP
  867. case PROT_RARP:
  868. rarp_receive(ip, len);
  869. break;
  870. #endif
  871. case PROT_IP:
  872. debug("Got IP\n");
  873. /* Before we start poking the header, make sure it is there */
  874. if (len < IP_UDP_HDR_SIZE) {
  875. debug("len bad %d < %lu\n", len,
  876. (ulong)IP_UDP_HDR_SIZE);
  877. return;
  878. }
  879. /* Check the packet length */
  880. if (len < ntohs(ip->ip_len)) {
  881. printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
  882. return;
  883. }
  884. len = ntohs(ip->ip_len);
  885. debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
  886. /* Can't deal with anything except IPv4 */
  887. if ((ip->ip_hl_v & 0xf0) != 0x40)
  888. return;
  889. /* Can't deal with IP options (headers != 20 bytes) */
  890. if ((ip->ip_hl_v & 0x0f) > 0x05)
  891. return;
  892. /* Check the Checksum of the header */
  893. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
  894. puts("checksum bad\n");
  895. return;
  896. }
  897. /* If it is not for us, ignore it */
  898. dst_ip = NetReadIP(&ip->ip_dst);
  899. if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
  900. #ifdef CONFIG_MCAST_TFTP
  901. if (Mcast_addr != dst_ip)
  902. #endif
  903. return;
  904. }
  905. /* Read source IP address for later use */
  906. src_ip = NetReadIP(&ip->ip_src);
  907. /*
  908. * The function returns the unchanged packet if it's not
  909. * a fragment, and either the complete packet or NULL if
  910. * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
  911. */
  912. ip = NetDefragment(ip, &len);
  913. if (!ip)
  914. return;
  915. /*
  916. * watch for ICMP host redirects
  917. *
  918. * There is no real handler code (yet). We just watch
  919. * for ICMP host redirect messages. In case anybody
  920. * sees these messages: please contact me
  921. * (wd@denx.de), or - even better - send me the
  922. * necessary fixes :-)
  923. *
  924. * Note: in all cases where I have seen this so far
  925. * it was a problem with the router configuration,
  926. * for instance when a router was configured in the
  927. * BOOTP reply, but the TFTP server was on the same
  928. * subnet. So this is probably a warning that your
  929. * configuration might be wrong. But I'm not really
  930. * sure if there aren't any other situations.
  931. *
  932. * Simon Glass <sjg@chromium.org>: We get an ICMP when
  933. * we send a tftp packet to a dead connection, or when
  934. * there is no server at the other end.
  935. */
  936. if (ip->ip_p == IPPROTO_ICMP) {
  937. receive_icmp(ip, len, src_ip, et);
  938. return;
  939. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  940. return;
  941. }
  942. #ifdef CONFIG_UDP_CHECKSUM
  943. if (ip->udp_xsum != 0) {
  944. ulong xsum;
  945. ushort *sumptr;
  946. ushort sumlen;
  947. xsum = ip->ip_p;
  948. xsum += (ntohs(ip->udp_len));
  949. xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
  950. xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
  951. xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
  952. xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
  953. sumlen = ntohs(ip->udp_len);
  954. sumptr = (ushort *) &(ip->udp_src);
  955. while (sumlen > 1) {
  956. ushort sumdata;
  957. sumdata = *sumptr++;
  958. xsum += ntohs(sumdata);
  959. sumlen -= 2;
  960. }
  961. if (sumlen > 0) {
  962. ushort sumdata;
  963. sumdata = *(unsigned char *) sumptr;
  964. sumdata = (sumdata << 8) & 0xff00;
  965. xsum += sumdata;
  966. }
  967. while ((xsum >> 16) != 0) {
  968. xsum = (xsum & 0x0000ffff) +
  969. ((xsum >> 16) & 0x0000ffff);
  970. }
  971. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  972. printf(" UDP wrong checksum %08lx %08x\n",
  973. xsum, ntohs(ip->udp_xsum));
  974. return;
  975. }
  976. }
  977. #endif
  978. #ifdef CONFIG_NETCONSOLE
  979. nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
  980. ntohs(ip->udp_dst),
  981. ntohs(ip->udp_src),
  982. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  983. #endif
  984. /*
  985. * IP header OK. Pass the packet to the current handler.
  986. */
  987. (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
  988. ntohs(ip->udp_dst),
  989. src_ip,
  990. ntohs(ip->udp_src),
  991. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  992. break;
  993. }
  994. }
  995. /**********************************************************************/
  996. static int net_check_prereq(enum proto_t protocol)
  997. {
  998. switch (protocol) {
  999. /* Fall through */
  1000. #if defined(CONFIG_CMD_PING)
  1001. case PING:
  1002. if (NetPingIP == 0) {
  1003. puts("*** ERROR: ping address not given\n");
  1004. return 1;
  1005. }
  1006. goto common;
  1007. #endif
  1008. #if defined(CONFIG_CMD_SNTP)
  1009. case SNTP:
  1010. if (NetNtpServerIP == 0) {
  1011. puts("*** ERROR: NTP server address not given\n");
  1012. return 1;
  1013. }
  1014. goto common;
  1015. #endif
  1016. #if defined(CONFIG_CMD_DNS)
  1017. case DNS:
  1018. if (NetOurDNSIP == 0) {
  1019. puts("*** ERROR: DNS server address not given\n");
  1020. return 1;
  1021. }
  1022. goto common;
  1023. #endif
  1024. #if defined(CONFIG_CMD_NFS)
  1025. case NFS:
  1026. #endif
  1027. case TFTPGET:
  1028. case TFTPPUT:
  1029. if (NetServerIP == 0) {
  1030. puts("*** ERROR: `serverip' not set\n");
  1031. return 1;
  1032. }
  1033. #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
  1034. defined(CONFIG_CMD_DNS)
  1035. common:
  1036. #endif
  1037. /* Fall through */
  1038. case NETCONS:
  1039. case TFTPSRV:
  1040. if (NetOurIP == 0) {
  1041. puts("*** ERROR: `ipaddr' not set\n");
  1042. return 1;
  1043. }
  1044. /* Fall through */
  1045. #ifdef CONFIG_CMD_RARP
  1046. case RARP:
  1047. #endif
  1048. case BOOTP:
  1049. case CDP:
  1050. case DHCP:
  1051. if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  1052. int num = eth_get_dev_index();
  1053. switch (num) {
  1054. case -1:
  1055. puts("*** ERROR: No ethernet found.\n");
  1056. return 1;
  1057. case 0:
  1058. puts("*** ERROR: `ethaddr' not set\n");
  1059. break;
  1060. default:
  1061. printf("*** ERROR: `eth%daddr' not set\n",
  1062. num);
  1063. break;
  1064. }
  1065. NetStartAgain();
  1066. return 2;
  1067. }
  1068. /* Fall through */
  1069. default:
  1070. return 0;
  1071. }
  1072. return 0; /* OK */
  1073. }
  1074. /**********************************************************************/
  1075. int
  1076. NetCksumOk(uchar *ptr, int len)
  1077. {
  1078. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  1079. }
  1080. unsigned
  1081. NetCksum(uchar *ptr, int len)
  1082. {
  1083. ulong xsum;
  1084. ushort *p = (ushort *)ptr;
  1085. xsum = 0;
  1086. while (len-- > 0)
  1087. xsum += *p++;
  1088. xsum = (xsum & 0xffff) + (xsum >> 16);
  1089. xsum = (xsum & 0xffff) + (xsum >> 16);
  1090. return xsum & 0xffff;
  1091. }
  1092. int
  1093. NetEthHdrSize(void)
  1094. {
  1095. ushort myvlanid;
  1096. myvlanid = ntohs(NetOurVLAN);
  1097. if (myvlanid == (ushort)-1)
  1098. myvlanid = VLAN_NONE;
  1099. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
  1100. VLAN_ETHER_HDR_SIZE;
  1101. }
  1102. int
  1103. NetSetEther(uchar *xet, uchar * addr, uint prot)
  1104. {
  1105. struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
  1106. ushort myvlanid;
  1107. myvlanid = ntohs(NetOurVLAN);
  1108. if (myvlanid == (ushort)-1)
  1109. myvlanid = VLAN_NONE;
  1110. memcpy(et->et_dest, addr, 6);
  1111. memcpy(et->et_src, NetOurEther, 6);
  1112. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1113. et->et_protlen = htons(prot);
  1114. return ETHER_HDR_SIZE;
  1115. } else {
  1116. struct vlan_ethernet_hdr *vet =
  1117. (struct vlan_ethernet_hdr *)xet;
  1118. vet->vet_vlan_type = htons(PROT_VLAN);
  1119. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1120. vet->vet_type = htons(prot);
  1121. return VLAN_ETHER_HDR_SIZE;
  1122. }
  1123. }
  1124. void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
  1125. {
  1126. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
  1127. /*
  1128. * Construct an IP header.
  1129. */
  1130. /* IP_HDR_SIZE / 4 (not including UDP) */
  1131. ip->ip_hl_v = 0x45;
  1132. ip->ip_tos = 0;
  1133. ip->ip_len = htons(IP_HDR_SIZE);
  1134. ip->ip_id = htons(NetIPID++);
  1135. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  1136. ip->ip_ttl = 255;
  1137. ip->ip_sum = 0;
  1138. /* already in network byte order */
  1139. NetCopyIP((void *)&ip->ip_src, &source);
  1140. /* already in network byte order */
  1141. NetCopyIP((void *)&ip->ip_dst, &dest);
  1142. }
  1143. void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
  1144. int len)
  1145. {
  1146. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
  1147. /*
  1148. * If the data is an odd number of bytes, zero the
  1149. * byte after the last byte so that the checksum
  1150. * will work.
  1151. */
  1152. if (len & 1)
  1153. pkt[IP_UDP_HDR_SIZE + len] = 0;
  1154. net_set_ip_header(pkt, dest, NetOurIP);
  1155. ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
  1156. ip->ip_p = IPPROTO_UDP;
  1157. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
  1158. ip->udp_src = htons(sport);
  1159. ip->udp_dst = htons(dport);
  1160. ip->udp_len = htons(UDP_HDR_SIZE + len);
  1161. ip->udp_xsum = 0;
  1162. }
  1163. void copy_filename(char *dst, const char *src, int size)
  1164. {
  1165. if (*src && (*src == '"')) {
  1166. ++src;
  1167. --size;
  1168. }
  1169. while ((--size > 0) && *src && (*src != '"'))
  1170. *dst++ = *src++;
  1171. *dst = '\0';
  1172. }
  1173. #if defined(CONFIG_CMD_NFS) || \
  1174. defined(CONFIG_CMD_SNTP) || \
  1175. defined(CONFIG_CMD_DNS)
  1176. /*
  1177. * make port a little random (1024-17407)
  1178. * This keeps the math somewhat trivial to compute, and seems to work with
  1179. * all supported protocols/clients/servers
  1180. */
  1181. unsigned int random_port(void)
  1182. {
  1183. return 1024 + (get_timer(0) % 0x4000);
  1184. }
  1185. #endif
  1186. void ip_to_string(IPaddr_t x, char *s)
  1187. {
  1188. x = ntohl(x);
  1189. sprintf(s, "%d.%d.%d.%d",
  1190. (int) ((x >> 24) & 0xff),
  1191. (int) ((x >> 16) & 0xff),
  1192. (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
  1193. );
  1194. }
  1195. void VLAN_to_string(ushort x, char *s)
  1196. {
  1197. x = ntohs(x);
  1198. if (x == (ushort)-1)
  1199. x = VLAN_NONE;
  1200. if (x == VLAN_NONE)
  1201. strcpy(s, "none");
  1202. else
  1203. sprintf(s, "%d", x & VLAN_IDMASK);
  1204. }
  1205. ushort string_to_VLAN(const char *s)
  1206. {
  1207. ushort id;
  1208. if (s == NULL)
  1209. return htons(VLAN_NONE);
  1210. if (*s < '0' || *s > '9')
  1211. id = VLAN_NONE;
  1212. else
  1213. id = (ushort)simple_strtoul(s, NULL, 10);
  1214. return htons(id);
  1215. }
  1216. ushort getenv_VLAN(char *var)
  1217. {
  1218. return string_to_VLAN(getenv(var));
  1219. }