net.c 34 KB

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