net.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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. printf("Bytes transferred = %ld (%lx hex)\n",
  475. NetBootFileXferSize,
  476. NetBootFileXferSize);
  477. setenv_hex("filesize", NetBootFileXferSize);
  478. setenv_hex("fileaddr", load_addr);
  479. }
  480. if (protocol != NETCONS)
  481. eth_halt();
  482. else
  483. eth_halt_state_only();
  484. eth_set_last_protocol(protocol);
  485. ret = NetBootFileXferSize;
  486. debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
  487. goto done;
  488. case NETLOOP_FAIL:
  489. net_cleanup_loop();
  490. /* Invalidate the last protocol */
  491. eth_set_last_protocol(BOOTP);
  492. debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
  493. goto done;
  494. case NETLOOP_CONTINUE:
  495. continue;
  496. }
  497. }
  498. done:
  499. #ifdef CONFIG_CMD_TFTPPUT
  500. /* Clear out the handlers */
  501. net_set_udp_handler(NULL);
  502. net_set_icmp_handler(NULL);
  503. #endif
  504. return ret;
  505. }
  506. /**********************************************************************/
  507. static void
  508. startAgainTimeout(void)
  509. {
  510. net_set_state(NETLOOP_RESTART);
  511. }
  512. void NetStartAgain(void)
  513. {
  514. char *nretry;
  515. int retry_forever = 0;
  516. unsigned long retrycnt = 0;
  517. nretry = getenv("netretry");
  518. if (nretry) {
  519. if (!strcmp(nretry, "yes"))
  520. retry_forever = 1;
  521. else if (!strcmp(nretry, "no"))
  522. retrycnt = 0;
  523. else if (!strcmp(nretry, "once"))
  524. retrycnt = 1;
  525. else
  526. retrycnt = simple_strtoul(nretry, NULL, 0);
  527. } else
  528. retry_forever = 1;
  529. if ((!retry_forever) && (NetTryCount >= retrycnt)) {
  530. eth_halt();
  531. net_set_state(NETLOOP_FAIL);
  532. return;
  533. }
  534. NetTryCount++;
  535. eth_halt();
  536. #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
  537. eth_try_another(!NetRestarted);
  538. #endif
  539. eth_init(gd->bd);
  540. if (NetRestartWrap) {
  541. NetRestartWrap = 0;
  542. if (NetDevExists) {
  543. NetSetTimeout(10000UL, startAgainTimeout);
  544. net_set_udp_handler(NULL);
  545. } else {
  546. net_set_state(NETLOOP_FAIL);
  547. }
  548. } else {
  549. net_set_state(NETLOOP_RESTART);
  550. }
  551. }
  552. /**********************************************************************/
  553. /*
  554. * Miscelaneous bits.
  555. */
  556. static void dummy_handler(uchar *pkt, unsigned dport,
  557. IPaddr_t sip, unsigned sport,
  558. unsigned len)
  559. {
  560. }
  561. rxhand_f *net_get_udp_handler(void)
  562. {
  563. return udp_packet_handler;
  564. }
  565. void net_set_udp_handler(rxhand_f *f)
  566. {
  567. debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
  568. if (f == NULL)
  569. udp_packet_handler = dummy_handler;
  570. else
  571. udp_packet_handler = f;
  572. }
  573. rxhand_f *net_get_arp_handler(void)
  574. {
  575. return arp_packet_handler;
  576. }
  577. void net_set_arp_handler(rxhand_f *f)
  578. {
  579. debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
  580. if (f == NULL)
  581. arp_packet_handler = dummy_handler;
  582. else
  583. arp_packet_handler = f;
  584. }
  585. #ifdef CONFIG_CMD_TFTPPUT
  586. void net_set_icmp_handler(rxhand_icmp_f *f)
  587. {
  588. packet_icmp_handler = f;
  589. }
  590. #endif
  591. void
  592. NetSetTimeout(ulong iv, thand_f *f)
  593. {
  594. if (iv == 0) {
  595. debug_cond(DEBUG_INT_STATE,
  596. "--- NetLoop timeout handler cancelled\n");
  597. timeHandler = (thand_f *)0;
  598. } else {
  599. debug_cond(DEBUG_INT_STATE,
  600. "--- NetLoop timeout handler set (%p)\n", f);
  601. timeHandler = f;
  602. timeStart = get_timer(0);
  603. timeDelta = iv * CONFIG_SYS_HZ / 1000;
  604. }
  605. }
  606. int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
  607. int payload_len)
  608. {
  609. uchar *pkt;
  610. int eth_hdr_size;
  611. int pkt_hdr_size;
  612. /* make sure the NetTxPacket is initialized (NetInit() was called) */
  613. assert(NetTxPacket != NULL);
  614. if (NetTxPacket == NULL)
  615. return -1;
  616. /* convert to new style broadcast */
  617. if (dest == 0)
  618. dest = 0xFFFFFFFF;
  619. /* if broadcast, make the ether address a broadcast and don't do ARP */
  620. if (dest == 0xFFFFFFFF)
  621. ether = NetBcastAddr;
  622. pkt = (uchar *)NetTxPacket;
  623. eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
  624. pkt += eth_hdr_size;
  625. net_set_udp_header(pkt, dest, dport, sport, payload_len);
  626. pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
  627. /* if MAC address was not discovered yet, do an ARP request */
  628. if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
  629. debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
  630. /* save the ip and eth addr for the packet to send after arp */
  631. NetArpWaitPacketIP = dest;
  632. NetArpWaitPacketMAC = ether;
  633. /* size of the waiting packet */
  634. NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
  635. /* and do the ARP request */
  636. NetArpWaitTry = 1;
  637. NetArpWaitTimerStart = get_timer(0);
  638. ArpRequest();
  639. return 1; /* waiting */
  640. } else {
  641. debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
  642. &dest, ether);
  643. NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
  644. return 0; /* transmitted */
  645. }
  646. }
  647. #ifdef CONFIG_IP_DEFRAG
  648. /*
  649. * This function collects fragments in a single packet, according
  650. * to the algorithm in RFC815. It returns NULL or the pointer to
  651. * a complete packet, in static storage
  652. */
  653. #ifndef CONFIG_NET_MAXDEFRAG
  654. #define CONFIG_NET_MAXDEFRAG 16384
  655. #endif
  656. /*
  657. * MAXDEFRAG, above, is chosen in the config file and is real data
  658. * so we need to add the NFS overhead, which is more than TFTP.
  659. * To use sizeof in the internal unnamed structures, we need a real
  660. * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
  661. * The compiler doesn't complain nor allocates the actual structure
  662. */
  663. static struct rpc_t rpc_specimen;
  664. #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
  665. #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
  666. /*
  667. * this is the packet being assembled, either data or frag control.
  668. * Fragments go by 8 bytes, so this union must be 8 bytes long
  669. */
  670. struct hole {
  671. /* first_byte is address of this structure */
  672. u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
  673. u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
  674. u16 prev_hole; /* index of prev, 0 == none */
  675. u16 unused;
  676. };
  677. static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
  678. {
  679. static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
  680. static u16 first_hole, total_len;
  681. struct hole *payload, *thisfrag, *h, *newh;
  682. struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
  683. uchar *indata = (uchar *)ip;
  684. int offset8, start, len, done = 0;
  685. u16 ip_off = ntohs(ip->ip_off);
  686. /* payload starts after IP header, this fragment is in there */
  687. payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
  688. offset8 = (ip_off & IP_OFFS);
  689. thisfrag = payload + offset8;
  690. start = offset8 * 8;
  691. len = ntohs(ip->ip_len) - IP_HDR_SIZE;
  692. if (start + len > IP_MAXUDP) /* fragment extends too far */
  693. return NULL;
  694. if (!total_len || localip->ip_id != ip->ip_id) {
  695. /* new (or different) packet, reset structs */
  696. total_len = 0xffff;
  697. payload[0].last_byte = ~0;
  698. payload[0].next_hole = 0;
  699. payload[0].prev_hole = 0;
  700. first_hole = 0;
  701. /* any IP header will work, copy the first we received */
  702. memcpy(localip, ip, IP_HDR_SIZE);
  703. }
  704. /*
  705. * What follows is the reassembly algorithm. We use the payload
  706. * array as a linked list of hole descriptors, as each hole starts
  707. * at a multiple of 8 bytes. However, last byte can be whatever value,
  708. * so it is represented as byte count, not as 8-byte blocks.
  709. */
  710. h = payload + first_hole;
  711. while (h->last_byte < start) {
  712. if (!h->next_hole) {
  713. /* no hole that far away */
  714. return NULL;
  715. }
  716. h = payload + h->next_hole;
  717. }
  718. /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
  719. if (offset8 + ((len + 7) / 8) <= h - payload) {
  720. /* no overlap with holes (dup fragment?) */
  721. return NULL;
  722. }
  723. if (!(ip_off & IP_FLAGS_MFRAG)) {
  724. /* no more fragmentss: truncate this (last) hole */
  725. total_len = start + len;
  726. h->last_byte = start + len;
  727. }
  728. /*
  729. * There is some overlap: fix the hole list. This code doesn't
  730. * deal with a fragment that overlaps with two different holes
  731. * (thus being a superset of a previously-received fragment).
  732. */
  733. if ((h >= thisfrag) && (h->last_byte <= start + len)) {
  734. /* complete overlap with hole: remove hole */
  735. if (!h->prev_hole && !h->next_hole) {
  736. /* last remaining hole */
  737. done = 1;
  738. } else if (!h->prev_hole) {
  739. /* first hole */
  740. first_hole = h->next_hole;
  741. payload[h->next_hole].prev_hole = 0;
  742. } else if (!h->next_hole) {
  743. /* last hole */
  744. payload[h->prev_hole].next_hole = 0;
  745. } else {
  746. /* in the middle of the list */
  747. payload[h->next_hole].prev_hole = h->prev_hole;
  748. payload[h->prev_hole].next_hole = h->next_hole;
  749. }
  750. } else if (h->last_byte <= start + len) {
  751. /* overlaps with final part of the hole: shorten this hole */
  752. h->last_byte = start;
  753. } else if (h >= thisfrag) {
  754. /* overlaps with initial part of the hole: move this hole */
  755. newh = thisfrag + (len / 8);
  756. *newh = *h;
  757. h = newh;
  758. if (h->next_hole)
  759. payload[h->next_hole].prev_hole = (h - payload);
  760. if (h->prev_hole)
  761. payload[h->prev_hole].next_hole = (h - payload);
  762. else
  763. first_hole = (h - payload);
  764. } else {
  765. /* fragment sits in the middle: split the hole */
  766. newh = thisfrag + (len / 8);
  767. *newh = *h;
  768. h->last_byte = start;
  769. h->next_hole = (newh - payload);
  770. newh->prev_hole = (h - payload);
  771. if (newh->next_hole)
  772. payload[newh->next_hole].prev_hole = (newh - payload);
  773. }
  774. /* finally copy this fragment and possibly return whole packet */
  775. memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
  776. if (!done)
  777. return NULL;
  778. localip->ip_len = htons(total_len);
  779. *lenp = total_len + IP_HDR_SIZE;
  780. return localip;
  781. }
  782. static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
  783. {
  784. u16 ip_off = ntohs(ip->ip_off);
  785. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  786. return ip; /* not a fragment */
  787. return __NetDefragment(ip, lenp);
  788. }
  789. #else /* !CONFIG_IP_DEFRAG */
  790. static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
  791. {
  792. u16 ip_off = ntohs(ip->ip_off);
  793. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  794. return ip; /* not a fragment */
  795. return NULL;
  796. }
  797. #endif
  798. /**
  799. * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
  800. * drop others.
  801. *
  802. * @parma ip IP packet containing the ICMP
  803. */
  804. static void receive_icmp(struct ip_udp_hdr *ip, int len,
  805. IPaddr_t src_ip, struct ethernet_hdr *et)
  806. {
  807. struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
  808. switch (icmph->type) {
  809. case ICMP_REDIRECT:
  810. if (icmph->code != ICMP_REDIR_HOST)
  811. return;
  812. printf(" ICMP Host Redirect to %pI4 ",
  813. &icmph->un.gateway);
  814. break;
  815. default:
  816. #if defined(CONFIG_CMD_PING)
  817. ping_receive(et, ip, len);
  818. #endif
  819. #ifdef CONFIG_CMD_TFTPPUT
  820. if (packet_icmp_handler)
  821. packet_icmp_handler(icmph->type, icmph->code,
  822. ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
  823. icmph->un.data, ntohs(ip->udp_len));
  824. #endif
  825. break;
  826. }
  827. }
  828. void
  829. NetReceive(uchar *inpkt, int len)
  830. {
  831. struct ethernet_hdr *et;
  832. struct ip_udp_hdr *ip;
  833. IPaddr_t dst_ip;
  834. IPaddr_t src_ip;
  835. int eth_proto;
  836. #if defined(CONFIG_CMD_CDP)
  837. int iscdp;
  838. #endif
  839. ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
  840. debug_cond(DEBUG_NET_PKT, "packet received\n");
  841. NetRxPacket = inpkt;
  842. NetRxPacketLen = len;
  843. et = (struct ethernet_hdr *)inpkt;
  844. /* too small packet? */
  845. if (len < ETHER_HDR_SIZE)
  846. return;
  847. #ifdef CONFIG_API
  848. if (push_packet) {
  849. (*push_packet)(inpkt, len);
  850. return;
  851. }
  852. #endif
  853. #if defined(CONFIG_CMD_CDP)
  854. /* keep track if packet is CDP */
  855. iscdp = is_cdp_packet(et->et_dest);
  856. #endif
  857. myvlanid = ntohs(NetOurVLAN);
  858. if (myvlanid == (ushort)-1)
  859. myvlanid = VLAN_NONE;
  860. mynvlanid = ntohs(NetOurNativeVLAN);
  861. if (mynvlanid == (ushort)-1)
  862. mynvlanid = VLAN_NONE;
  863. eth_proto = ntohs(et->et_protlen);
  864. if (eth_proto < 1514) {
  865. struct e802_hdr *et802 = (struct e802_hdr *)et;
  866. /*
  867. * Got a 802.2 packet. Check the other protocol field.
  868. * XXX VLAN over 802.2+SNAP not implemented!
  869. */
  870. eth_proto = ntohs(et802->et_prot);
  871. ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
  872. len -= E802_HDR_SIZE;
  873. } else if (eth_proto != PROT_VLAN) { /* normal packet */
  874. ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
  875. len -= ETHER_HDR_SIZE;
  876. } else { /* VLAN packet */
  877. struct vlan_ethernet_hdr *vet =
  878. (struct vlan_ethernet_hdr *)et;
  879. debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
  880. /* too small packet? */
  881. if (len < VLAN_ETHER_HDR_SIZE)
  882. return;
  883. /* if no VLAN active */
  884. if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
  885. #if defined(CONFIG_CMD_CDP)
  886. && iscdp == 0
  887. #endif
  888. )
  889. return;
  890. cti = ntohs(vet->vet_tag);
  891. vlanid = cti & VLAN_IDMASK;
  892. eth_proto = ntohs(vet->vet_type);
  893. ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
  894. len -= VLAN_ETHER_HDR_SIZE;
  895. }
  896. debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
  897. #if defined(CONFIG_CMD_CDP)
  898. if (iscdp) {
  899. cdp_receive((uchar *)ip, len);
  900. return;
  901. }
  902. #endif
  903. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  904. if (vlanid == VLAN_NONE)
  905. vlanid = (mynvlanid & VLAN_IDMASK);
  906. /* not matched? */
  907. if (vlanid != (myvlanid & VLAN_IDMASK))
  908. return;
  909. }
  910. switch (eth_proto) {
  911. case PROT_ARP:
  912. ArpReceive(et, ip, len);
  913. break;
  914. #ifdef CONFIG_CMD_RARP
  915. case PROT_RARP:
  916. rarp_receive(ip, len);
  917. break;
  918. #endif
  919. case PROT_IP:
  920. debug_cond(DEBUG_NET_PKT, "Got IP\n");
  921. /* Before we start poking the header, make sure it is there */
  922. if (len < IP_UDP_HDR_SIZE) {
  923. debug("len bad %d < %lu\n", len,
  924. (ulong)IP_UDP_HDR_SIZE);
  925. return;
  926. }
  927. /* Check the packet length */
  928. if (len < ntohs(ip->ip_len)) {
  929. debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
  930. return;
  931. }
  932. len = ntohs(ip->ip_len);
  933. debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
  934. len, ip->ip_hl_v & 0xff);
  935. /* Can't deal with anything except IPv4 */
  936. if ((ip->ip_hl_v & 0xf0) != 0x40)
  937. return;
  938. /* Can't deal with IP options (headers != 20 bytes) */
  939. if ((ip->ip_hl_v & 0x0f) > 0x05)
  940. return;
  941. /* Check the Checksum of the header */
  942. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
  943. debug("checksum bad\n");
  944. return;
  945. }
  946. /* If it is not for us, ignore it */
  947. dst_ip = NetReadIP(&ip->ip_dst);
  948. if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
  949. #ifdef CONFIG_MCAST_TFTP
  950. if (Mcast_addr != dst_ip)
  951. #endif
  952. return;
  953. }
  954. /* Read source IP address for later use */
  955. src_ip = NetReadIP(&ip->ip_src);
  956. /*
  957. * The function returns the unchanged packet if it's not
  958. * a fragment, and either the complete packet or NULL if
  959. * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
  960. */
  961. ip = NetDefragment(ip, &len);
  962. if (!ip)
  963. return;
  964. /*
  965. * watch for ICMP host redirects
  966. *
  967. * There is no real handler code (yet). We just watch
  968. * for ICMP host redirect messages. In case anybody
  969. * sees these messages: please contact me
  970. * (wd@denx.de), or - even better - send me the
  971. * necessary fixes :-)
  972. *
  973. * Note: in all cases where I have seen this so far
  974. * it was a problem with the router configuration,
  975. * for instance when a router was configured in the
  976. * BOOTP reply, but the TFTP server was on the same
  977. * subnet. So this is probably a warning that your
  978. * configuration might be wrong. But I'm not really
  979. * sure if there aren't any other situations.
  980. *
  981. * Simon Glass <sjg@chromium.org>: We get an ICMP when
  982. * we send a tftp packet to a dead connection, or when
  983. * there is no server at the other end.
  984. */
  985. if (ip->ip_p == IPPROTO_ICMP) {
  986. receive_icmp(ip, len, src_ip, et);
  987. return;
  988. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  989. return;
  990. }
  991. debug_cond(DEBUG_DEV_PKT,
  992. "received UDP (to=%pI4, from=%pI4, len=%d)\n",
  993. &dst_ip, &src_ip, len);
  994. #ifdef CONFIG_UDP_CHECKSUM
  995. if (ip->udp_xsum != 0) {
  996. ulong xsum;
  997. ushort *sumptr;
  998. ushort sumlen;
  999. xsum = ip->ip_p;
  1000. xsum += (ntohs(ip->udp_len));
  1001. xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
  1002. xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
  1003. xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
  1004. xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
  1005. sumlen = ntohs(ip->udp_len);
  1006. sumptr = (ushort *) &(ip->udp_src);
  1007. while (sumlen > 1) {
  1008. ushort sumdata;
  1009. sumdata = *sumptr++;
  1010. xsum += ntohs(sumdata);
  1011. sumlen -= 2;
  1012. }
  1013. if (sumlen > 0) {
  1014. ushort sumdata;
  1015. sumdata = *(unsigned char *) sumptr;
  1016. sumdata = (sumdata << 8) & 0xff00;
  1017. xsum += sumdata;
  1018. }
  1019. while ((xsum >> 16) != 0) {
  1020. xsum = (xsum & 0x0000ffff) +
  1021. ((xsum >> 16) & 0x0000ffff);
  1022. }
  1023. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  1024. printf(" UDP wrong checksum %08lx %08x\n",
  1025. xsum, ntohs(ip->udp_xsum));
  1026. return;
  1027. }
  1028. }
  1029. #endif
  1030. #ifdef CONFIG_NETCONSOLE
  1031. nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
  1032. src_ip,
  1033. ntohs(ip->udp_dst),
  1034. ntohs(ip->udp_src),
  1035. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  1036. #endif
  1037. /*
  1038. * IP header OK. Pass the packet to the current handler.
  1039. */
  1040. (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
  1041. ntohs(ip->udp_dst),
  1042. src_ip,
  1043. ntohs(ip->udp_src),
  1044. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  1045. break;
  1046. }
  1047. }
  1048. /**********************************************************************/
  1049. static int net_check_prereq(enum proto_t protocol)
  1050. {
  1051. switch (protocol) {
  1052. /* Fall through */
  1053. #if defined(CONFIG_CMD_PING)
  1054. case PING:
  1055. if (NetPingIP == 0) {
  1056. puts("*** ERROR: ping address not given\n");
  1057. return 1;
  1058. }
  1059. goto common;
  1060. #endif
  1061. #if defined(CONFIG_CMD_SNTP)
  1062. case SNTP:
  1063. if (NetNtpServerIP == 0) {
  1064. puts("*** ERROR: NTP server address not given\n");
  1065. return 1;
  1066. }
  1067. goto common;
  1068. #endif
  1069. #if defined(CONFIG_CMD_DNS)
  1070. case DNS:
  1071. if (NetOurDNSIP == 0) {
  1072. puts("*** ERROR: DNS server address not given\n");
  1073. return 1;
  1074. }
  1075. goto common;
  1076. #endif
  1077. #if defined(CONFIG_CMD_NFS)
  1078. case NFS:
  1079. #endif
  1080. case TFTPGET:
  1081. case TFTPPUT:
  1082. if (NetServerIP == 0) {
  1083. puts("*** ERROR: `serverip' not set\n");
  1084. return 1;
  1085. }
  1086. #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
  1087. defined(CONFIG_CMD_DNS)
  1088. common:
  1089. #endif
  1090. /* Fall through */
  1091. case NETCONS:
  1092. case TFTPSRV:
  1093. if (NetOurIP == 0) {
  1094. puts("*** ERROR: `ipaddr' not set\n");
  1095. return 1;
  1096. }
  1097. /* Fall through */
  1098. #ifdef CONFIG_CMD_RARP
  1099. case RARP:
  1100. #endif
  1101. case BOOTP:
  1102. case CDP:
  1103. case DHCP:
  1104. case LINKLOCAL:
  1105. if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  1106. int num = eth_get_dev_index();
  1107. switch (num) {
  1108. case -1:
  1109. puts("*** ERROR: No ethernet found.\n");
  1110. return 1;
  1111. case 0:
  1112. puts("*** ERROR: `ethaddr' not set\n");
  1113. break;
  1114. default:
  1115. printf("*** ERROR: `eth%daddr' not set\n",
  1116. num);
  1117. break;
  1118. }
  1119. NetStartAgain();
  1120. return 2;
  1121. }
  1122. /* Fall through */
  1123. default:
  1124. return 0;
  1125. }
  1126. return 0; /* OK */
  1127. }
  1128. /**********************************************************************/
  1129. int
  1130. NetCksumOk(uchar *ptr, int len)
  1131. {
  1132. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  1133. }
  1134. unsigned
  1135. NetCksum(uchar *ptr, int len)
  1136. {
  1137. ulong xsum;
  1138. ushort *p = (ushort *)ptr;
  1139. xsum = 0;
  1140. while (len-- > 0)
  1141. xsum += *p++;
  1142. xsum = (xsum & 0xffff) + (xsum >> 16);
  1143. xsum = (xsum & 0xffff) + (xsum >> 16);
  1144. return xsum & 0xffff;
  1145. }
  1146. int
  1147. NetEthHdrSize(void)
  1148. {
  1149. ushort myvlanid;
  1150. myvlanid = ntohs(NetOurVLAN);
  1151. if (myvlanid == (ushort)-1)
  1152. myvlanid = VLAN_NONE;
  1153. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
  1154. VLAN_ETHER_HDR_SIZE;
  1155. }
  1156. int
  1157. NetSetEther(uchar *xet, uchar * addr, uint prot)
  1158. {
  1159. struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
  1160. ushort myvlanid;
  1161. myvlanid = ntohs(NetOurVLAN);
  1162. if (myvlanid == (ushort)-1)
  1163. myvlanid = VLAN_NONE;
  1164. memcpy(et->et_dest, addr, 6);
  1165. memcpy(et->et_src, NetOurEther, 6);
  1166. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1167. et->et_protlen = htons(prot);
  1168. return ETHER_HDR_SIZE;
  1169. } else {
  1170. struct vlan_ethernet_hdr *vet =
  1171. (struct vlan_ethernet_hdr *)xet;
  1172. vet->vet_vlan_type = htons(PROT_VLAN);
  1173. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1174. vet->vet_type = htons(prot);
  1175. return VLAN_ETHER_HDR_SIZE;
  1176. }
  1177. }
  1178. int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
  1179. {
  1180. ushort protlen;
  1181. memcpy(et->et_dest, addr, 6);
  1182. memcpy(et->et_src, NetOurEther, 6);
  1183. protlen = ntohs(et->et_protlen);
  1184. if (protlen == PROT_VLAN) {
  1185. struct vlan_ethernet_hdr *vet =
  1186. (struct vlan_ethernet_hdr *)et;
  1187. vet->vet_type = htons(prot);
  1188. return VLAN_ETHER_HDR_SIZE;
  1189. } else if (protlen > 1514) {
  1190. et->et_protlen = htons(prot);
  1191. return ETHER_HDR_SIZE;
  1192. } else {
  1193. /* 802.2 + SNAP */
  1194. struct e802_hdr *et802 = (struct e802_hdr *)et;
  1195. et802->et_prot = htons(prot);
  1196. return E802_HDR_SIZE;
  1197. }
  1198. }
  1199. void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
  1200. {
  1201. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
  1202. /*
  1203. * Construct an IP header.
  1204. */
  1205. /* IP_HDR_SIZE / 4 (not including UDP) */
  1206. ip->ip_hl_v = 0x45;
  1207. ip->ip_tos = 0;
  1208. ip->ip_len = htons(IP_HDR_SIZE);
  1209. ip->ip_id = htons(NetIPID++);
  1210. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  1211. ip->ip_ttl = 255;
  1212. ip->ip_sum = 0;
  1213. /* already in network byte order */
  1214. NetCopyIP((void *)&ip->ip_src, &source);
  1215. /* already in network byte order */
  1216. NetCopyIP((void *)&ip->ip_dst, &dest);
  1217. }
  1218. void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
  1219. int len)
  1220. {
  1221. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
  1222. /*
  1223. * If the data is an odd number of bytes, zero the
  1224. * byte after the last byte so that the checksum
  1225. * will work.
  1226. */
  1227. if (len & 1)
  1228. pkt[IP_UDP_HDR_SIZE + len] = 0;
  1229. net_set_ip_header(pkt, dest, NetOurIP);
  1230. ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
  1231. ip->ip_p = IPPROTO_UDP;
  1232. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
  1233. ip->udp_src = htons(sport);
  1234. ip->udp_dst = htons(dport);
  1235. ip->udp_len = htons(UDP_HDR_SIZE + len);
  1236. ip->udp_xsum = 0;
  1237. }
  1238. void copy_filename(char *dst, const char *src, int size)
  1239. {
  1240. if (*src && (*src == '"')) {
  1241. ++src;
  1242. --size;
  1243. }
  1244. while ((--size > 0) && *src && (*src != '"'))
  1245. *dst++ = *src++;
  1246. *dst = '\0';
  1247. }
  1248. #if defined(CONFIG_CMD_NFS) || \
  1249. defined(CONFIG_CMD_SNTP) || \
  1250. defined(CONFIG_CMD_DNS)
  1251. /*
  1252. * make port a little random (1024-17407)
  1253. * This keeps the math somewhat trivial to compute, and seems to work with
  1254. * all supported protocols/clients/servers
  1255. */
  1256. unsigned int random_port(void)
  1257. {
  1258. return 1024 + (get_timer(0) % 0x4000);
  1259. }
  1260. #endif
  1261. void ip_to_string(IPaddr_t x, char *s)
  1262. {
  1263. x = ntohl(x);
  1264. sprintf(s, "%d.%d.%d.%d",
  1265. (int) ((x >> 24) & 0xff),
  1266. (int) ((x >> 16) & 0xff),
  1267. (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
  1268. );
  1269. }
  1270. void VLAN_to_string(ushort x, char *s)
  1271. {
  1272. x = ntohs(x);
  1273. if (x == (ushort)-1)
  1274. x = VLAN_NONE;
  1275. if (x == VLAN_NONE)
  1276. strcpy(s, "none");
  1277. else
  1278. sprintf(s, "%d", x & VLAN_IDMASK);
  1279. }
  1280. ushort string_to_VLAN(const char *s)
  1281. {
  1282. ushort id;
  1283. if (s == NULL)
  1284. return htons(VLAN_NONE);
  1285. if (*s < '0' || *s > '9')
  1286. id = VLAN_NONE;
  1287. else
  1288. id = (ushort)simple_strtoul(s, NULL, 10);
  1289. return htons(id);
  1290. }
  1291. ushort getenv_VLAN(char *var)
  1292. {
  1293. return string_to_VLAN(getenv(var));
  1294. }