net.c 33 KB

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