net.c 35 KB

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