net.c 41 KB

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