net.c 31 KB

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