net.c 37 KB

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