net.c 32 KB

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