net.c 36 KB

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