net.c 37 KB

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