net.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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_LED_STATUS)
  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) && !defined(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_LED_STATUS) && \
  458. defined(CONFIG_LED_STATUS_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(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
  464. else
  465. status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_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_LED_STATUS) && \
  516. defined(CONFIG_LED_STATUS_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(CONFIG_LED_STATUS_RED,
  523. CONFIG_LED_STATUS_OFF);
  524. else
  525. status_led_set(CONFIG_LED_STATUS_RED,
  526. CONFIG_LED_STATUS_ON);
  527. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  528. #endif /* CONFIG_MII, ... */
  529. debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
  530. x = time_handler;
  531. time_handler = (thand_f *)0;
  532. (*x)();
  533. }
  534. if (net_state == NETLOOP_FAIL)
  535. ret = net_start_again();
  536. switch (net_state) {
  537. case NETLOOP_RESTART:
  538. net_restarted = 1;
  539. goto restart;
  540. case NETLOOP_SUCCESS:
  541. net_cleanup_loop();
  542. if (net_boot_file_size > 0) {
  543. printf("Bytes transferred = %d (%x hex)\n",
  544. net_boot_file_size, net_boot_file_size);
  545. setenv_hex("filesize", net_boot_file_size);
  546. setenv_hex("fileaddr", load_addr);
  547. }
  548. if (protocol != NETCONS)
  549. eth_halt();
  550. else
  551. eth_halt_state_only();
  552. eth_set_last_protocol(protocol);
  553. ret = net_boot_file_size;
  554. debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
  555. goto done;
  556. case NETLOOP_FAIL:
  557. net_cleanup_loop();
  558. /* Invalidate the last protocol */
  559. eth_set_last_protocol(BOOTP);
  560. debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
  561. goto done;
  562. case NETLOOP_CONTINUE:
  563. continue;
  564. }
  565. }
  566. done:
  567. #ifdef CONFIG_USB_KEYBOARD
  568. net_busy_flag = 0;
  569. #endif
  570. #ifdef CONFIG_CMD_TFTPPUT
  571. /* Clear out the handlers */
  572. net_set_udp_handler(NULL);
  573. net_set_icmp_handler(NULL);
  574. #endif
  575. return ret;
  576. }
  577. /**********************************************************************/
  578. static void start_again_timeout_handler(void)
  579. {
  580. net_set_state(NETLOOP_RESTART);
  581. }
  582. int net_start_again(void)
  583. {
  584. char *nretry;
  585. int retry_forever = 0;
  586. unsigned long retrycnt = 0;
  587. int ret;
  588. nretry = getenv("netretry");
  589. if (nretry) {
  590. if (!strcmp(nretry, "yes"))
  591. retry_forever = 1;
  592. else if (!strcmp(nretry, "no"))
  593. retrycnt = 0;
  594. else if (!strcmp(nretry, "once"))
  595. retrycnt = 1;
  596. else
  597. retrycnt = simple_strtoul(nretry, NULL, 0);
  598. } else {
  599. retrycnt = 0;
  600. retry_forever = 0;
  601. }
  602. if ((!retry_forever) && (net_try_count >= retrycnt)) {
  603. eth_halt();
  604. net_set_state(NETLOOP_FAIL);
  605. /*
  606. * We don't provide a way for the protocol to return an error,
  607. * but this is almost always the reason.
  608. */
  609. return -ETIMEDOUT;
  610. }
  611. net_try_count++;
  612. eth_halt();
  613. #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
  614. eth_try_another(!net_restarted);
  615. #endif
  616. ret = eth_init();
  617. if (net_restart_wrap) {
  618. net_restart_wrap = 0;
  619. if (net_dev_exists) {
  620. net_set_timeout_handler(10000UL,
  621. start_again_timeout_handler);
  622. net_set_udp_handler(NULL);
  623. } else {
  624. net_set_state(NETLOOP_FAIL);
  625. }
  626. } else {
  627. net_set_state(NETLOOP_RESTART);
  628. }
  629. return ret;
  630. }
  631. /**********************************************************************/
  632. /*
  633. * Miscelaneous bits.
  634. */
  635. static void dummy_handler(uchar *pkt, unsigned dport,
  636. struct in_addr sip, unsigned sport,
  637. unsigned len)
  638. {
  639. }
  640. rxhand_f *net_get_udp_handler(void)
  641. {
  642. return udp_packet_handler;
  643. }
  644. void net_set_udp_handler(rxhand_f *f)
  645. {
  646. debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
  647. if (f == NULL)
  648. udp_packet_handler = dummy_handler;
  649. else
  650. udp_packet_handler = f;
  651. }
  652. rxhand_f *net_get_arp_handler(void)
  653. {
  654. return arp_packet_handler;
  655. }
  656. void net_set_arp_handler(rxhand_f *f)
  657. {
  658. debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
  659. if (f == NULL)
  660. arp_packet_handler = dummy_handler;
  661. else
  662. arp_packet_handler = f;
  663. }
  664. #ifdef CONFIG_CMD_TFTPPUT
  665. void net_set_icmp_handler(rxhand_icmp_f *f)
  666. {
  667. packet_icmp_handler = f;
  668. }
  669. #endif
  670. void net_set_timeout_handler(ulong iv, thand_f *f)
  671. {
  672. if (iv == 0) {
  673. debug_cond(DEBUG_INT_STATE,
  674. "--- net_loop timeout handler cancelled\n");
  675. time_handler = (thand_f *)0;
  676. } else {
  677. debug_cond(DEBUG_INT_STATE,
  678. "--- net_loop timeout handler set (%p)\n", f);
  679. time_handler = f;
  680. time_start = get_timer(0);
  681. time_delta = iv * CONFIG_SYS_HZ / 1000;
  682. }
  683. }
  684. int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
  685. int payload_len)
  686. {
  687. uchar *pkt;
  688. int eth_hdr_size;
  689. int pkt_hdr_size;
  690. /* make sure the net_tx_packet is initialized (net_init() was called) */
  691. assert(net_tx_packet != NULL);
  692. if (net_tx_packet == NULL)
  693. return -1;
  694. /* convert to new style broadcast */
  695. if (dest.s_addr == 0)
  696. dest.s_addr = 0xFFFFFFFF;
  697. /* if broadcast, make the ether address a broadcast and don't do ARP */
  698. if (dest.s_addr == 0xFFFFFFFF)
  699. ether = (uchar *)net_bcast_ethaddr;
  700. pkt = (uchar *)net_tx_packet;
  701. eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
  702. pkt += eth_hdr_size;
  703. net_set_udp_header(pkt, dest, dport, sport, payload_len);
  704. pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
  705. /* if MAC address was not discovered yet, do an ARP request */
  706. if (memcmp(ether, net_null_ethaddr, 6) == 0) {
  707. debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
  708. /* save the ip and eth addr for the packet to send after arp */
  709. net_arp_wait_packet_ip = dest;
  710. arp_wait_packet_ethaddr = ether;
  711. /* size of the waiting packet */
  712. arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
  713. /* and do the ARP request */
  714. arp_wait_try = 1;
  715. arp_wait_timer_start = get_timer(0);
  716. arp_request();
  717. return 1; /* waiting */
  718. } else {
  719. debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
  720. &dest, ether);
  721. net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
  722. return 0; /* transmitted */
  723. }
  724. }
  725. #ifdef CONFIG_IP_DEFRAG
  726. /*
  727. * This function collects fragments in a single packet, according
  728. * to the algorithm in RFC815. It returns NULL or the pointer to
  729. * a complete packet, in static storage
  730. */
  731. #ifndef CONFIG_NET_MAXDEFRAG
  732. #define CONFIG_NET_MAXDEFRAG 16384
  733. #endif
  734. #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
  735. #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
  736. /*
  737. * this is the packet being assembled, either data or frag control.
  738. * Fragments go by 8 bytes, so this union must be 8 bytes long
  739. */
  740. struct hole {
  741. /* first_byte is address of this structure */
  742. u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
  743. u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
  744. u16 prev_hole; /* index of prev, 0 == none */
  745. u16 unused;
  746. };
  747. static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
  748. {
  749. static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
  750. static u16 first_hole, total_len;
  751. struct hole *payload, *thisfrag, *h, *newh;
  752. struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
  753. uchar *indata = (uchar *)ip;
  754. int offset8, start, len, done = 0;
  755. u16 ip_off = ntohs(ip->ip_off);
  756. /* payload starts after IP header, this fragment is in there */
  757. payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
  758. offset8 = (ip_off & IP_OFFS);
  759. thisfrag = payload + offset8;
  760. start = offset8 * 8;
  761. len = ntohs(ip->ip_len) - IP_HDR_SIZE;
  762. if (start + len > IP_MAXUDP) /* fragment extends too far */
  763. return NULL;
  764. if (!total_len || localip->ip_id != ip->ip_id) {
  765. /* new (or different) packet, reset structs */
  766. total_len = 0xffff;
  767. payload[0].last_byte = ~0;
  768. payload[0].next_hole = 0;
  769. payload[0].prev_hole = 0;
  770. first_hole = 0;
  771. /* any IP header will work, copy the first we received */
  772. memcpy(localip, ip, IP_HDR_SIZE);
  773. }
  774. /*
  775. * What follows is the reassembly algorithm. We use the payload
  776. * array as a linked list of hole descriptors, as each hole starts
  777. * at a multiple of 8 bytes. However, last byte can be whatever value,
  778. * so it is represented as byte count, not as 8-byte blocks.
  779. */
  780. h = payload + first_hole;
  781. while (h->last_byte < start) {
  782. if (!h->next_hole) {
  783. /* no hole that far away */
  784. return NULL;
  785. }
  786. h = payload + h->next_hole;
  787. }
  788. /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
  789. if (offset8 + ((len + 7) / 8) <= h - payload) {
  790. /* no overlap with holes (dup fragment?) */
  791. return NULL;
  792. }
  793. if (!(ip_off & IP_FLAGS_MFRAG)) {
  794. /* no more fragmentss: truncate this (last) hole */
  795. total_len = start + len;
  796. h->last_byte = start + len;
  797. }
  798. /*
  799. * There is some overlap: fix the hole list. This code doesn't
  800. * deal with a fragment that overlaps with two different holes
  801. * (thus being a superset of a previously-received fragment).
  802. */
  803. if ((h >= thisfrag) && (h->last_byte <= start + len)) {
  804. /* complete overlap with hole: remove hole */
  805. if (!h->prev_hole && !h->next_hole) {
  806. /* last remaining hole */
  807. done = 1;
  808. } else if (!h->prev_hole) {
  809. /* first hole */
  810. first_hole = h->next_hole;
  811. payload[h->next_hole].prev_hole = 0;
  812. } else if (!h->next_hole) {
  813. /* last hole */
  814. payload[h->prev_hole].next_hole = 0;
  815. } else {
  816. /* in the middle of the list */
  817. payload[h->next_hole].prev_hole = h->prev_hole;
  818. payload[h->prev_hole].next_hole = h->next_hole;
  819. }
  820. } else if (h->last_byte <= start + len) {
  821. /* overlaps with final part of the hole: shorten this hole */
  822. h->last_byte = start;
  823. } else if (h >= thisfrag) {
  824. /* overlaps with initial part of the hole: move this hole */
  825. newh = thisfrag + (len / 8);
  826. *newh = *h;
  827. h = newh;
  828. if (h->next_hole)
  829. payload[h->next_hole].prev_hole = (h - payload);
  830. if (h->prev_hole)
  831. payload[h->prev_hole].next_hole = (h - payload);
  832. else
  833. first_hole = (h - payload);
  834. } else {
  835. /* fragment sits in the middle: split the hole */
  836. newh = thisfrag + (len / 8);
  837. *newh = *h;
  838. h->last_byte = start;
  839. h->next_hole = (newh - payload);
  840. newh->prev_hole = (h - payload);
  841. if (newh->next_hole)
  842. payload[newh->next_hole].prev_hole = (newh - payload);
  843. }
  844. /* finally copy this fragment and possibly return whole packet */
  845. memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
  846. if (!done)
  847. return NULL;
  848. localip->ip_len = htons(total_len);
  849. *lenp = total_len + IP_HDR_SIZE;
  850. return localip;
  851. }
  852. static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
  853. int *lenp)
  854. {
  855. u16 ip_off = ntohs(ip->ip_off);
  856. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  857. return ip; /* not a fragment */
  858. return __net_defragment(ip, lenp);
  859. }
  860. #else /* !CONFIG_IP_DEFRAG */
  861. static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
  862. int *lenp)
  863. {
  864. u16 ip_off = ntohs(ip->ip_off);
  865. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  866. return ip; /* not a fragment */
  867. return NULL;
  868. }
  869. #endif
  870. /**
  871. * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
  872. * drop others.
  873. *
  874. * @parma ip IP packet containing the ICMP
  875. */
  876. static void receive_icmp(struct ip_udp_hdr *ip, int len,
  877. struct in_addr src_ip, struct ethernet_hdr *et)
  878. {
  879. struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
  880. switch (icmph->type) {
  881. case ICMP_REDIRECT:
  882. if (icmph->code != ICMP_REDIR_HOST)
  883. return;
  884. printf(" ICMP Host Redirect to %pI4 ",
  885. &icmph->un.gateway);
  886. break;
  887. default:
  888. #if defined(CONFIG_CMD_PING)
  889. ping_receive(et, ip, len);
  890. #endif
  891. #ifdef CONFIG_CMD_TFTPPUT
  892. if (packet_icmp_handler)
  893. packet_icmp_handler(icmph->type, icmph->code,
  894. ntohs(ip->udp_dst), src_ip,
  895. ntohs(ip->udp_src), icmph->un.data,
  896. ntohs(ip->udp_len));
  897. #endif
  898. break;
  899. }
  900. }
  901. void net_process_received_packet(uchar *in_packet, int len)
  902. {
  903. struct ethernet_hdr *et;
  904. struct ip_udp_hdr *ip;
  905. struct in_addr dst_ip;
  906. struct in_addr src_ip;
  907. int eth_proto;
  908. #if defined(CONFIG_CMD_CDP)
  909. int iscdp;
  910. #endif
  911. ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
  912. debug_cond(DEBUG_NET_PKT, "packet received\n");
  913. net_rx_packet = in_packet;
  914. net_rx_packet_len = len;
  915. et = (struct ethernet_hdr *)in_packet;
  916. /* too small packet? */
  917. if (len < ETHER_HDR_SIZE)
  918. return;
  919. #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
  920. if (push_packet) {
  921. (*push_packet)(in_packet, len);
  922. return;
  923. }
  924. #endif
  925. #if defined(CONFIG_CMD_CDP)
  926. /* keep track if packet is CDP */
  927. iscdp = is_cdp_packet(et->et_dest);
  928. #endif
  929. myvlanid = ntohs(net_our_vlan);
  930. if (myvlanid == (ushort)-1)
  931. myvlanid = VLAN_NONE;
  932. mynvlanid = ntohs(net_native_vlan);
  933. if (mynvlanid == (ushort)-1)
  934. mynvlanid = VLAN_NONE;
  935. eth_proto = ntohs(et->et_protlen);
  936. if (eth_proto < 1514) {
  937. struct e802_hdr *et802 = (struct e802_hdr *)et;
  938. /*
  939. * Got a 802.2 packet. Check the other protocol field.
  940. * XXX VLAN over 802.2+SNAP not implemented!
  941. */
  942. eth_proto = ntohs(et802->et_prot);
  943. ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
  944. len -= E802_HDR_SIZE;
  945. } else if (eth_proto != PROT_VLAN) { /* normal packet */
  946. ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
  947. len -= ETHER_HDR_SIZE;
  948. } else { /* VLAN packet */
  949. struct vlan_ethernet_hdr *vet =
  950. (struct vlan_ethernet_hdr *)et;
  951. debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
  952. /* too small packet? */
  953. if (len < VLAN_ETHER_HDR_SIZE)
  954. return;
  955. /* if no VLAN active */
  956. if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
  957. #if defined(CONFIG_CMD_CDP)
  958. && iscdp == 0
  959. #endif
  960. )
  961. return;
  962. cti = ntohs(vet->vet_tag);
  963. vlanid = cti & VLAN_IDMASK;
  964. eth_proto = ntohs(vet->vet_type);
  965. ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
  966. len -= VLAN_ETHER_HDR_SIZE;
  967. }
  968. debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
  969. #if defined(CONFIG_CMD_CDP)
  970. if (iscdp) {
  971. cdp_receive((uchar *)ip, len);
  972. return;
  973. }
  974. #endif
  975. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  976. if (vlanid == VLAN_NONE)
  977. vlanid = (mynvlanid & VLAN_IDMASK);
  978. /* not matched? */
  979. if (vlanid != (myvlanid & VLAN_IDMASK))
  980. return;
  981. }
  982. switch (eth_proto) {
  983. case PROT_ARP:
  984. arp_receive(et, ip, len);
  985. break;
  986. #ifdef CONFIG_CMD_RARP
  987. case PROT_RARP:
  988. rarp_receive(ip, len);
  989. break;
  990. #endif
  991. case PROT_IP:
  992. debug_cond(DEBUG_NET_PKT, "Got IP\n");
  993. /* Before we start poking the header, make sure it is there */
  994. if (len < IP_UDP_HDR_SIZE) {
  995. debug("len bad %d < %lu\n", len,
  996. (ulong)IP_UDP_HDR_SIZE);
  997. return;
  998. }
  999. /* Check the packet length */
  1000. if (len < ntohs(ip->ip_len)) {
  1001. debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
  1002. return;
  1003. }
  1004. len = ntohs(ip->ip_len);
  1005. debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
  1006. len, ip->ip_hl_v & 0xff);
  1007. /* Can't deal with anything except IPv4 */
  1008. if ((ip->ip_hl_v & 0xf0) != 0x40)
  1009. return;
  1010. /* Can't deal with IP options (headers != 20 bytes) */
  1011. if ((ip->ip_hl_v & 0x0f) > 0x05)
  1012. return;
  1013. /* Check the Checksum of the header */
  1014. if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
  1015. debug("checksum bad\n");
  1016. return;
  1017. }
  1018. /* If it is not for us, ignore it */
  1019. dst_ip = net_read_ip(&ip->ip_dst);
  1020. if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
  1021. dst_ip.s_addr != 0xFFFFFFFF) {
  1022. #ifdef CONFIG_MCAST_TFTP
  1023. if (net_mcast_addr != dst_ip)
  1024. #endif
  1025. return;
  1026. }
  1027. /* Read source IP address for later use */
  1028. src_ip = net_read_ip(&ip->ip_src);
  1029. /*
  1030. * The function returns the unchanged packet if it's not
  1031. * a fragment, and either the complete packet or NULL if
  1032. * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
  1033. */
  1034. ip = net_defragment(ip, &len);
  1035. if (!ip)
  1036. return;
  1037. /*
  1038. * watch for ICMP host redirects
  1039. *
  1040. * There is no real handler code (yet). We just watch
  1041. * for ICMP host redirect messages. In case anybody
  1042. * sees these messages: please contact me
  1043. * (wd@denx.de), or - even better - send me the
  1044. * necessary fixes :-)
  1045. *
  1046. * Note: in all cases where I have seen this so far
  1047. * it was a problem with the router configuration,
  1048. * for instance when a router was configured in the
  1049. * BOOTP reply, but the TFTP server was on the same
  1050. * subnet. So this is probably a warning that your
  1051. * configuration might be wrong. But I'm not really
  1052. * sure if there aren't any other situations.
  1053. *
  1054. * Simon Glass <sjg@chromium.org>: We get an ICMP when
  1055. * we send a tftp packet to a dead connection, or when
  1056. * there is no server at the other end.
  1057. */
  1058. if (ip->ip_p == IPPROTO_ICMP) {
  1059. receive_icmp(ip, len, src_ip, et);
  1060. return;
  1061. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  1062. return;
  1063. }
  1064. debug_cond(DEBUG_DEV_PKT,
  1065. "received UDP (to=%pI4, from=%pI4, len=%d)\n",
  1066. &dst_ip, &src_ip, len);
  1067. #ifdef CONFIG_UDP_CHECKSUM
  1068. if (ip->udp_xsum != 0) {
  1069. ulong xsum;
  1070. ushort *sumptr;
  1071. ushort sumlen;
  1072. xsum = ip->ip_p;
  1073. xsum += (ntohs(ip->udp_len));
  1074. xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
  1075. xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
  1076. xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
  1077. xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
  1078. sumlen = ntohs(ip->udp_len);
  1079. sumptr = (ushort *)&(ip->udp_src);
  1080. while (sumlen > 1) {
  1081. ushort sumdata;
  1082. sumdata = *sumptr++;
  1083. xsum += ntohs(sumdata);
  1084. sumlen -= 2;
  1085. }
  1086. if (sumlen > 0) {
  1087. ushort sumdata;
  1088. sumdata = *(unsigned char *)sumptr;
  1089. sumdata = (sumdata << 8) & 0xff00;
  1090. xsum += sumdata;
  1091. }
  1092. while ((xsum >> 16) != 0) {
  1093. xsum = (xsum & 0x0000ffff) +
  1094. ((xsum >> 16) & 0x0000ffff);
  1095. }
  1096. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  1097. printf(" UDP wrong checksum %08lx %08x\n",
  1098. xsum, ntohs(ip->udp_xsum));
  1099. return;
  1100. }
  1101. }
  1102. #endif
  1103. #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
  1104. nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
  1105. src_ip,
  1106. ntohs(ip->udp_dst),
  1107. ntohs(ip->udp_src),
  1108. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  1109. #endif
  1110. /*
  1111. * IP header OK. Pass the packet to the current handler.
  1112. */
  1113. (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
  1114. ntohs(ip->udp_dst),
  1115. src_ip,
  1116. ntohs(ip->udp_src),
  1117. ntohs(ip->udp_len) - UDP_HDR_SIZE);
  1118. break;
  1119. }
  1120. }
  1121. /**********************************************************************/
  1122. static int net_check_prereq(enum proto_t protocol)
  1123. {
  1124. switch (protocol) {
  1125. /* Fall through */
  1126. #if defined(CONFIG_CMD_PING)
  1127. case PING:
  1128. if (net_ping_ip.s_addr == 0) {
  1129. puts("*** ERROR: ping address not given\n");
  1130. return 1;
  1131. }
  1132. goto common;
  1133. #endif
  1134. #if defined(CONFIG_CMD_SNTP)
  1135. case SNTP:
  1136. if (net_ntp_server.s_addr == 0) {
  1137. puts("*** ERROR: NTP server address not given\n");
  1138. return 1;
  1139. }
  1140. goto common;
  1141. #endif
  1142. #if defined(CONFIG_CMD_DNS)
  1143. case DNS:
  1144. if (net_dns_server.s_addr == 0) {
  1145. puts("*** ERROR: DNS server address not given\n");
  1146. return 1;
  1147. }
  1148. goto common;
  1149. #endif
  1150. #if defined(CONFIG_CMD_NFS)
  1151. case NFS:
  1152. #endif
  1153. /* Fall through */
  1154. case TFTPGET:
  1155. case TFTPPUT:
  1156. if (net_server_ip.s_addr == 0) {
  1157. puts("*** ERROR: `serverip' not set\n");
  1158. return 1;
  1159. }
  1160. #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
  1161. defined(CONFIG_CMD_DNS)
  1162. common:
  1163. #endif
  1164. /* Fall through */
  1165. case NETCONS:
  1166. case TFTPSRV:
  1167. if (net_ip.s_addr == 0) {
  1168. puts("*** ERROR: `ipaddr' not set\n");
  1169. return 1;
  1170. }
  1171. /* Fall through */
  1172. #ifdef CONFIG_CMD_RARP
  1173. case RARP:
  1174. #endif
  1175. case BOOTP:
  1176. case CDP:
  1177. case DHCP:
  1178. case LINKLOCAL:
  1179. if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
  1180. int num = eth_get_dev_index();
  1181. switch (num) {
  1182. case -1:
  1183. puts("*** ERROR: No ethernet found.\n");
  1184. return 1;
  1185. case 0:
  1186. puts("*** ERROR: `ethaddr' not set\n");
  1187. break;
  1188. default:
  1189. printf("*** ERROR: `eth%daddr' not set\n",
  1190. num);
  1191. break;
  1192. }
  1193. net_start_again();
  1194. return 2;
  1195. }
  1196. /* Fall through */
  1197. default:
  1198. return 0;
  1199. }
  1200. return 0; /* OK */
  1201. }
  1202. /**********************************************************************/
  1203. int
  1204. net_eth_hdr_size(void)
  1205. {
  1206. ushort myvlanid;
  1207. myvlanid = ntohs(net_our_vlan);
  1208. if (myvlanid == (ushort)-1)
  1209. myvlanid = VLAN_NONE;
  1210. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
  1211. VLAN_ETHER_HDR_SIZE;
  1212. }
  1213. int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
  1214. {
  1215. struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
  1216. ushort myvlanid;
  1217. myvlanid = ntohs(net_our_vlan);
  1218. if (myvlanid == (ushort)-1)
  1219. myvlanid = VLAN_NONE;
  1220. memcpy(et->et_dest, dest_ethaddr, 6);
  1221. memcpy(et->et_src, net_ethaddr, 6);
  1222. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1223. et->et_protlen = htons(prot);
  1224. return ETHER_HDR_SIZE;
  1225. } else {
  1226. struct vlan_ethernet_hdr *vet =
  1227. (struct vlan_ethernet_hdr *)xet;
  1228. vet->vet_vlan_type = htons(PROT_VLAN);
  1229. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1230. vet->vet_type = htons(prot);
  1231. return VLAN_ETHER_HDR_SIZE;
  1232. }
  1233. }
  1234. int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
  1235. {
  1236. ushort protlen;
  1237. memcpy(et->et_dest, addr, 6);
  1238. memcpy(et->et_src, net_ethaddr, 6);
  1239. protlen = ntohs(et->et_protlen);
  1240. if (protlen == PROT_VLAN) {
  1241. struct vlan_ethernet_hdr *vet =
  1242. (struct vlan_ethernet_hdr *)et;
  1243. vet->vet_type = htons(prot);
  1244. return VLAN_ETHER_HDR_SIZE;
  1245. } else if (protlen > 1514) {
  1246. et->et_protlen = htons(prot);
  1247. return ETHER_HDR_SIZE;
  1248. } else {
  1249. /* 802.2 + SNAP */
  1250. struct e802_hdr *et802 = (struct e802_hdr *)et;
  1251. et802->et_prot = htons(prot);
  1252. return E802_HDR_SIZE;
  1253. }
  1254. }
  1255. void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source)
  1256. {
  1257. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
  1258. /*
  1259. * Construct an IP header.
  1260. */
  1261. /* IP_HDR_SIZE / 4 (not including UDP) */
  1262. ip->ip_hl_v = 0x45;
  1263. ip->ip_tos = 0;
  1264. ip->ip_len = htons(IP_HDR_SIZE);
  1265. ip->ip_id = htons(net_ip_id++);
  1266. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  1267. ip->ip_ttl = 255;
  1268. ip->ip_sum = 0;
  1269. /* already in network byte order */
  1270. net_copy_ip((void *)&ip->ip_src, &source);
  1271. /* already in network byte order */
  1272. net_copy_ip((void *)&ip->ip_dst, &dest);
  1273. }
  1274. void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
  1275. int len)
  1276. {
  1277. struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
  1278. /*
  1279. * If the data is an odd number of bytes, zero the
  1280. * byte after the last byte so that the checksum
  1281. * will work.
  1282. */
  1283. if (len & 1)
  1284. pkt[IP_UDP_HDR_SIZE + len] = 0;
  1285. net_set_ip_header(pkt, dest, net_ip);
  1286. ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
  1287. ip->ip_p = IPPROTO_UDP;
  1288. ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
  1289. ip->udp_src = htons(sport);
  1290. ip->udp_dst = htons(dport);
  1291. ip->udp_len = htons(UDP_HDR_SIZE + len);
  1292. ip->udp_xsum = 0;
  1293. }
  1294. void copy_filename(char *dst, const char *src, int size)
  1295. {
  1296. if (*src && (*src == '"')) {
  1297. ++src;
  1298. --size;
  1299. }
  1300. while ((--size > 0) && *src && (*src != '"'))
  1301. *dst++ = *src++;
  1302. *dst = '\0';
  1303. }
  1304. #if defined(CONFIG_CMD_NFS) || \
  1305. defined(CONFIG_CMD_SNTP) || \
  1306. defined(CONFIG_CMD_DNS)
  1307. /*
  1308. * make port a little random (1024-17407)
  1309. * This keeps the math somewhat trivial to compute, and seems to work with
  1310. * all supported protocols/clients/servers
  1311. */
  1312. unsigned int random_port(void)
  1313. {
  1314. return 1024 + (get_timer(0) % 0x4000);
  1315. }
  1316. #endif
  1317. void ip_to_string(struct in_addr x, char *s)
  1318. {
  1319. x.s_addr = ntohl(x.s_addr);
  1320. sprintf(s, "%d.%d.%d.%d",
  1321. (int) ((x.s_addr >> 24) & 0xff),
  1322. (int) ((x.s_addr >> 16) & 0xff),
  1323. (int) ((x.s_addr >> 8) & 0xff),
  1324. (int) ((x.s_addr >> 0) & 0xff)
  1325. );
  1326. }
  1327. void vlan_to_string(ushort x, char *s)
  1328. {
  1329. x = ntohs(x);
  1330. if (x == (ushort)-1)
  1331. x = VLAN_NONE;
  1332. if (x == VLAN_NONE)
  1333. strcpy(s, "none");
  1334. else
  1335. sprintf(s, "%d", x & VLAN_IDMASK);
  1336. }
  1337. ushort string_to_vlan(const char *s)
  1338. {
  1339. ushort id;
  1340. if (s == NULL)
  1341. return htons(VLAN_NONE);
  1342. if (*s < '0' || *s > '9')
  1343. id = VLAN_NONE;
  1344. else
  1345. id = (ushort)simple_strtoul(s, NULL, 10);
  1346. return htons(id);
  1347. }
  1348. ushort getenv_vlan(char *var)
  1349. {
  1350. return string_to_vlan(getenv(var));
  1351. }