net.c 37 KB

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