net.c 35 KB

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