net.c 31 KB

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