net.c 33 KB

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