net.c 37 KB

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