net.c 34 KB

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