net.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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. */
  10. /*
  11. * General Desription:
  12. *
  13. * The user interface supports commands for BOOTP, RARP, and TFTP.
  14. * Also, we support ARP internally. Depending on available data,
  15. * these interact as follows:
  16. *
  17. * BOOTP:
  18. *
  19. * Prerequisites: - own ethernet address
  20. * We want: - own IP address
  21. * - TFTP server IP address
  22. * - name of bootfile
  23. * Next step: ARP
  24. *
  25. * RARP:
  26. *
  27. * Prerequisites: - own ethernet address
  28. * We want: - own IP address
  29. * - TFTP server IP address
  30. * Next step: ARP
  31. *
  32. * ARP:
  33. *
  34. * Prerequisites: - own ethernet address
  35. * - own IP address
  36. * - TFTP server IP address
  37. * We want: - TFTP server ethernet address
  38. * Next step: TFTP
  39. *
  40. * DHCP:
  41. *
  42. * Prerequisites: - own ethernet address
  43. * We want: - IP, Netmask, ServerIP, Gateway IP
  44. * - bootfilename, lease time
  45. * Next step: - TFTP
  46. *
  47. * TFTP:
  48. *
  49. * Prerequisites: - own ethernet address
  50. * - own IP address
  51. * - TFTP server IP address
  52. * - TFTP server ethernet address
  53. * - name of bootfile (if unknown, we use a default name
  54. * derived from our own IP address)
  55. * We want: - load the boot file
  56. * Next step: none
  57. *
  58. * NFS:
  59. *
  60. * Prerequisites: - own ethernet address
  61. * - own IP address
  62. * - name of bootfile (if unknown, we use a default name
  63. * derived from our own IP address)
  64. * We want: - load the boot file
  65. * Next step: none
  66. *
  67. * SNTP:
  68. *
  69. * Prerequisites: - own ethernet address
  70. * - own IP address
  71. * We want: - network time
  72. * Next step: none
  73. */
  74. #include <common.h>
  75. #include <watchdog.h>
  76. #include <command.h>
  77. #include <linux/compiler.h>
  78. #include <net.h>
  79. #include "arp.h"
  80. #include "bootp.h"
  81. #include "tftp.h"
  82. #include "rarp.h"
  83. #include "nfs.h"
  84. #ifdef CONFIG_STATUS_LED
  85. #include <status_led.h>
  86. #include <miiphy.h>
  87. #endif
  88. #if defined(CONFIG_CMD_SNTP)
  89. #include "sntp.h"
  90. #endif
  91. #include "cdp.h"
  92. #if defined(CONFIG_CMD_DNS)
  93. #include "dns.h"
  94. #endif
  95. #include "ping.h"
  96. DECLARE_GLOBAL_DATA_PTR;
  97. /** BOOTP EXTENTIONS **/
  98. /* Our subnet mask (0=unknown) */
  99. IPaddr_t NetOurSubnetMask;
  100. /* Our gateways IP address */
  101. IPaddr_t NetOurGatewayIP;
  102. /* Our DNS IP address */
  103. IPaddr_t NetOurDNSIP;
  104. #if defined(CONFIG_BOOTP_DNS2)
  105. /* Our 2nd DNS IP address */
  106. IPaddr_t NetOurDNS2IP;
  107. #endif
  108. /* Our NIS domain */
  109. char NetOurNISDomain[32] = {0,};
  110. /* Our hostname */
  111. char NetOurHostName[32] = {0,};
  112. /* Our bootpath */
  113. char NetOurRootPath[64] = {0,};
  114. /* Our bootfile size in blocks */
  115. ushort NetBootFileSize;
  116. #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
  117. IPaddr_t Mcast_addr;
  118. #endif
  119. /** END OF BOOTP EXTENTIONS **/
  120. /* The actual transferred size of the bootfile (in bytes) */
  121. ulong NetBootFileXferSize;
  122. /* Our ethernet address */
  123. uchar NetOurEther[6];
  124. /* Boot server enet address */
  125. uchar NetServerEther[6];
  126. /* Our IP addr (0 = unknown) */
  127. IPaddr_t NetOurIP;
  128. /* Server IP addr (0 = unknown) */
  129. IPaddr_t NetServerIP;
  130. /* Current receive packet */
  131. uchar *NetRxPacket;
  132. /* Current rx packet length */
  133. int NetRxPacketLen;
  134. /* IP packet ID */
  135. unsigned NetIPID;
  136. /* Ethernet bcast address */
  137. uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  138. uchar NetEtherNullAddr[6];
  139. #ifdef CONFIG_API
  140. void (*push_packet)(void *, int len) = 0;
  141. #endif
  142. /* Network loop state */
  143. int NetState;
  144. /* Tried all network devices */
  145. int NetRestartWrap;
  146. /* Network loop restarted */
  147. static int NetRestarted;
  148. /* At least one device configured */
  149. static int NetDevExists;
  150. /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
  151. /* default is without VLAN */
  152. ushort NetOurVLAN = 0xFFFF;
  153. /* ditto */
  154. ushort NetOurNativeVLAN = 0xFFFF;
  155. /* Boot File name */
  156. char BootFile[128];
  157. #if defined(CONFIG_CMD_SNTP)
  158. /* NTP server IP address */
  159. IPaddr_t NetNtpServerIP;
  160. /* offset time from UTC */
  161. int NetTimeOffset;
  162. #endif
  163. uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
  164. /* Receive packet */
  165. uchar *NetRxPackets[PKTBUFSRX];
  166. /* Current RX packet handler */
  167. static rxhand_f *packetHandler;
  168. #ifdef CONFIG_CMD_TFTPPUT
  169. static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */
  170. #endif
  171. /* Current timeout handler */
  172. static thand_f *timeHandler;
  173. /* Time base value */
  174. static ulong timeStart;
  175. /* Current timeout value */
  176. static ulong timeDelta;
  177. /* THE transmit packet */
  178. uchar *NetTxPacket;
  179. static int net_check_prereq(enum proto_t protocol);
  180. static int NetTryCount;
  181. /**********************************************************************/
  182. /*
  183. * Check if autoload is enabled. If so, use either NFS or TFTP to download
  184. * the boot file.
  185. */
  186. void net_auto_load(void)
  187. {
  188. const char *s = getenv("autoload");
  189. if (s != NULL) {
  190. if (*s == 'n') {
  191. /*
  192. * Just use BOOTP/RARP to configure system;
  193. * Do not use TFTP to load the bootfile.
  194. */
  195. NetState = NETLOOP_SUCCESS;
  196. return;
  197. }
  198. #if defined(CONFIG_CMD_NFS)
  199. if (strcmp(s, "NFS") == 0) {
  200. /*
  201. * Use NFS to load the bootfile.
  202. */
  203. NfsStart();
  204. return;
  205. }
  206. #endif
  207. }
  208. TftpStart(TFTPGET);
  209. }
  210. static void NetInitLoop(enum proto_t protocol)
  211. {
  212. static int env_changed_id;
  213. int env_id = get_env_id();
  214. /* update only when the environment has changed */
  215. if (env_changed_id != env_id) {
  216. NetOurIP = getenv_IPaddr("ipaddr");
  217. NetOurGatewayIP = getenv_IPaddr("gatewayip");
  218. NetOurSubnetMask = getenv_IPaddr("netmask");
  219. NetServerIP = getenv_IPaddr("serverip");
  220. NetOurNativeVLAN = getenv_VLAN("nvlan");
  221. NetOurVLAN = getenv_VLAN("vlan");
  222. #if defined(CONFIG_CMD_DNS)
  223. NetOurDNSIP = getenv_IPaddr("dnsip");
  224. #endif
  225. env_changed_id = env_id;
  226. }
  227. return;
  228. }
  229. /**********************************************************************/
  230. /*
  231. * Main network processing loop.
  232. */
  233. int NetLoop(enum proto_t protocol)
  234. {
  235. bd_t *bd = gd->bd;
  236. int ret = -1;
  237. NetRestarted = 0;
  238. NetDevExists = 0;
  239. NetTxPacket = NULL;
  240. NetTryCount = 1;
  241. ArpInit();
  242. if (!NetTxPacket) {
  243. int i;
  244. /*
  245. * Setup packet buffers, aligned correctly.
  246. */
  247. NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
  248. NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
  249. for (i = 0; i < PKTBUFSRX; i++)
  250. NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
  251. }
  252. bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
  253. eth_halt();
  254. eth_set_current();
  255. if (eth_init(bd) < 0) {
  256. eth_halt();
  257. return -1;
  258. }
  259. restart:
  260. memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
  261. NetState = NETLOOP_CONTINUE;
  262. /*
  263. * Start the ball rolling with the given start function. From
  264. * here on, this code is a state machine driven by received
  265. * packets and timer events.
  266. */
  267. NetInitLoop(protocol);
  268. switch (net_check_prereq(protocol)) {
  269. case 1:
  270. /* network not configured */
  271. eth_halt();
  272. return -1;
  273. case 2:
  274. /* network device not configured */
  275. break;
  276. case 0:
  277. NetDevExists = 1;
  278. NetBootFileXferSize = 0;
  279. switch (protocol) {
  280. case TFTPGET:
  281. #ifdef CONFIG_CMD_TFTPPUT
  282. case TFTPPUT:
  283. #endif
  284. /* always use ARP to get server ethernet address */
  285. TftpStart(protocol);
  286. break;
  287. #ifdef CONFIG_CMD_TFTPSRV
  288. case TFTPSRV:
  289. TftpStartServer();
  290. break;
  291. #endif
  292. #if defined(CONFIG_CMD_DHCP)
  293. case DHCP:
  294. BootpTry = 0;
  295. NetOurIP = 0;
  296. DhcpRequest(); /* Basically same as BOOTP */
  297. break;
  298. #endif
  299. case BOOTP:
  300. BootpTry = 0;
  301. NetOurIP = 0;
  302. BootpRequest();
  303. break;
  304. #if defined(CONFIG_CMD_RARP)
  305. case RARP:
  306. RarpTry = 0;
  307. NetOurIP = 0;
  308. RarpRequest();
  309. break;
  310. #endif
  311. #if defined(CONFIG_CMD_PING)
  312. case PING:
  313. ping_start();
  314. break;
  315. #endif
  316. #if defined(CONFIG_CMD_NFS)
  317. case NFS:
  318. NfsStart();
  319. break;
  320. #endif
  321. #if defined(CONFIG_CMD_CDP)
  322. case CDP:
  323. CDPStart();
  324. break;
  325. #endif
  326. #ifdef CONFIG_NETCONSOLE
  327. case NETCONS:
  328. NcStart();
  329. break;
  330. #endif
  331. #if defined(CONFIG_CMD_SNTP)
  332. case SNTP:
  333. SntpStart();
  334. break;
  335. #endif
  336. #if defined(CONFIG_CMD_DNS)
  337. case DNS:
  338. DnsStart();
  339. break;
  340. #endif
  341. default:
  342. break;
  343. }
  344. break;
  345. }
  346. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  347. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
  348. defined(CONFIG_STATUS_LED) && \
  349. defined(STATUS_LED_RED)
  350. /*
  351. * Echo the inverted link state to the fault LED.
  352. */
  353. if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
  354. status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
  355. else
  356. status_led_set(STATUS_LED_RED, STATUS_LED_ON);
  357. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  358. #endif /* CONFIG_MII, ... */
  359. /*
  360. * Main packet reception loop. Loop receiving packets until
  361. * someone sets `NetState' to a state that terminates.
  362. */
  363. for (;;) {
  364. WATCHDOG_RESET();
  365. #ifdef CONFIG_SHOW_ACTIVITY
  366. show_activity(1);
  367. #endif
  368. /*
  369. * Check the ethernet for a new packet. The ethernet
  370. * receive routine will process it.
  371. */
  372. eth_rx();
  373. /*
  374. * Abort if ctrl-c was pressed.
  375. */
  376. if (ctrlc()) {
  377. eth_halt();
  378. puts("\nAbort\n");
  379. goto done;
  380. }
  381. ArpTimeoutCheck();
  382. /*
  383. * Check for a timeout, and run the timeout handler
  384. * if we have one.
  385. */
  386. if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
  387. thand_f *x;
  388. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  389. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
  390. defined(CONFIG_STATUS_LED) && \
  391. defined(STATUS_LED_RED)
  392. /*
  393. * Echo the inverted link state to the fault LED.
  394. */
  395. if (miiphy_link(eth_get_dev()->name,
  396. CONFIG_SYS_FAULT_MII_ADDR)) {
  397. status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
  398. } else {
  399. status_led_set(STATUS_LED_RED, STATUS_LED_ON);
  400. }
  401. #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
  402. #endif /* CONFIG_MII, ... */
  403. x = timeHandler;
  404. timeHandler = (thand_f *)0;
  405. (*x)();
  406. }
  407. switch (NetState) {
  408. case NETLOOP_RESTART:
  409. NetRestarted = 1;
  410. goto restart;
  411. case NETLOOP_SUCCESS:
  412. if (NetBootFileXferSize > 0) {
  413. char buf[20];
  414. printf("Bytes transferred = %ld (%lx hex)\n",
  415. NetBootFileXferSize,
  416. NetBootFileXferSize);
  417. sprintf(buf, "%lX", NetBootFileXferSize);
  418. setenv("filesize", buf);
  419. sprintf(buf, "%lX", (unsigned long)load_addr);
  420. setenv("fileaddr", buf);
  421. }
  422. eth_halt();
  423. ret = NetBootFileXferSize;
  424. goto done;
  425. case NETLOOP_FAIL:
  426. goto done;
  427. }
  428. }
  429. done:
  430. #ifdef CONFIG_CMD_TFTPPUT
  431. /* Clear out the handlers */
  432. NetSetHandler(NULL);
  433. net_set_icmp_handler(NULL);
  434. #endif
  435. return ret;
  436. }
  437. /**********************************************************************/
  438. static void
  439. startAgainTimeout(void)
  440. {
  441. NetState = NETLOOP_RESTART;
  442. }
  443. static void
  444. startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
  445. unsigned src, unsigned len)
  446. {
  447. /* Totally ignore the packet */
  448. }
  449. void NetStartAgain(void)
  450. {
  451. char *nretry;
  452. int retry_forever = 0;
  453. unsigned long retrycnt = 0;
  454. nretry = getenv("netretry");
  455. if (nretry) {
  456. if (!strcmp(nretry, "yes"))
  457. retry_forever = 1;
  458. else if (!strcmp(nretry, "no"))
  459. retrycnt = 0;
  460. else if (!strcmp(nretry, "once"))
  461. retrycnt = 1;
  462. else
  463. retrycnt = simple_strtoul(nretry, NULL, 0);
  464. } else
  465. retry_forever = 1;
  466. if ((!retry_forever) && (NetTryCount >= retrycnt)) {
  467. eth_halt();
  468. NetState = NETLOOP_FAIL;
  469. return;
  470. }
  471. NetTryCount++;
  472. eth_halt();
  473. #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
  474. eth_try_another(!NetRestarted);
  475. #endif
  476. eth_init(gd->bd);
  477. if (NetRestartWrap) {
  478. NetRestartWrap = 0;
  479. if (NetDevExists) {
  480. NetSetTimeout(10000UL, startAgainTimeout);
  481. NetSetHandler(startAgainHandler);
  482. } else {
  483. NetState = NETLOOP_FAIL;
  484. }
  485. } else {
  486. NetState = NETLOOP_RESTART;
  487. }
  488. }
  489. /**********************************************************************/
  490. /*
  491. * Miscelaneous bits.
  492. */
  493. rxhand_f *
  494. NetGetHandler(void)
  495. {
  496. return packetHandler;
  497. }
  498. void
  499. NetSetHandler(rxhand_f *f)
  500. {
  501. packetHandler = f;
  502. }
  503. #ifdef CONFIG_CMD_TFTPPUT
  504. void net_set_icmp_handler(rxhand_icmp_f *f)
  505. {
  506. packet_icmp_handler = f;
  507. }
  508. #endif
  509. void
  510. NetSetTimeout(ulong iv, thand_f *f)
  511. {
  512. if (iv == 0) {
  513. timeHandler = (thand_f *)0;
  514. } else {
  515. timeHandler = f;
  516. timeStart = get_timer(0);
  517. timeDelta = iv;
  518. }
  519. }
  520. void
  521. NetSendPacket(uchar *pkt, int len)
  522. {
  523. (void) eth_send(pkt, len);
  524. }
  525. int
  526. NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
  527. {
  528. uchar *pkt;
  529. /* convert to new style broadcast */
  530. if (dest == 0)
  531. dest = 0xFFFFFFFF;
  532. /* if broadcast, make the ether address a broadcast and don't do ARP */
  533. if (dest == 0xFFFFFFFF)
  534. ether = NetBcastAddr;
  535. /*
  536. * if MAC address was not discovered yet, save the packet and do
  537. * an ARP request
  538. */
  539. if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
  540. debug("sending ARP for %08x\n", dest);
  541. NetArpWaitPacketIP = dest;
  542. NetArpWaitPacketMAC = ether;
  543. pkt = NetArpWaitTxPacket;
  544. pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
  545. NetSetIP(pkt, dest, dport, sport, len);
  546. memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
  547. (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
  548. /* size of the waiting packet */
  549. NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
  550. IP_HDR_SIZE + len;
  551. /* and do the ARP request */
  552. NetArpWaitTry = 1;
  553. NetArpWaitTimerStart = get_timer(0);
  554. ArpRequest();
  555. return 1; /* waiting */
  556. }
  557. debug("sending UDP to %08x/%pM\n", dest, ether);
  558. pkt = (uchar *)NetTxPacket;
  559. pkt += NetSetEther(pkt, ether, PROT_IP);
  560. NetSetIP(pkt, dest, dport, sport, len);
  561. (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
  562. return 0; /* transmitted */
  563. }
  564. #ifdef CONFIG_IP_DEFRAG
  565. /*
  566. * This function collects fragments in a single packet, according
  567. * to the algorithm in RFC815. It returns NULL or the pointer to
  568. * a complete packet, in static storage
  569. */
  570. #ifndef CONFIG_NET_MAXDEFRAG
  571. #define CONFIG_NET_MAXDEFRAG 16384
  572. #endif
  573. /*
  574. * MAXDEFRAG, above, is chosen in the config file and is real data
  575. * so we need to add the NFS overhead, which is more than TFTP.
  576. * To use sizeof in the internal unnamed structures, we need a real
  577. * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
  578. * The compiler doesn't complain nor allocates the actual structure
  579. */
  580. static struct rpc_t rpc_specimen;
  581. #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
  582. #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
  583. /*
  584. * this is the packet being assembled, either data or frag control.
  585. * Fragments go by 8 bytes, so this union must be 8 bytes long
  586. */
  587. struct hole {
  588. /* first_byte is address of this structure */
  589. u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
  590. u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
  591. u16 prev_hole; /* index of prev, 0 == none */
  592. u16 unused;
  593. };
  594. static IP_t *__NetDefragment(IP_t *ip, int *lenp)
  595. {
  596. static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
  597. static u16 first_hole, total_len;
  598. struct hole *payload, *thisfrag, *h, *newh;
  599. IP_t *localip = (IP_t *)pkt_buff;
  600. uchar *indata = (uchar *)ip;
  601. int offset8, start, len, done = 0;
  602. u16 ip_off = ntohs(ip->ip_off);
  603. /* payload starts after IP header, this fragment is in there */
  604. payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
  605. offset8 = (ip_off & IP_OFFS);
  606. thisfrag = payload + offset8;
  607. start = offset8 * 8;
  608. len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
  609. if (start + len > IP_MAXUDP) /* fragment extends too far */
  610. return NULL;
  611. if (!total_len || localip->ip_id != ip->ip_id) {
  612. /* new (or different) packet, reset structs */
  613. total_len = 0xffff;
  614. payload[0].last_byte = ~0;
  615. payload[0].next_hole = 0;
  616. payload[0].prev_hole = 0;
  617. first_hole = 0;
  618. /* any IP header will work, copy the first we received */
  619. memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
  620. }
  621. /*
  622. * What follows is the reassembly algorithm. We use the payload
  623. * array as a linked list of hole descriptors, as each hole starts
  624. * at a multiple of 8 bytes. However, last byte can be whatever value,
  625. * so it is represented as byte count, not as 8-byte blocks.
  626. */
  627. h = payload + first_hole;
  628. while (h->last_byte < start) {
  629. if (!h->next_hole) {
  630. /* no hole that far away */
  631. return NULL;
  632. }
  633. h = payload + h->next_hole;
  634. }
  635. /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
  636. if (offset8 + ((len + 7) / 8) <= h - payload) {
  637. /* no overlap with holes (dup fragment?) */
  638. return NULL;
  639. }
  640. if (!(ip_off & IP_FLAGS_MFRAG)) {
  641. /* no more fragmentss: truncate this (last) hole */
  642. total_len = start + len;
  643. h->last_byte = start + len;
  644. }
  645. /*
  646. * There is some overlap: fix the hole list. This code doesn't
  647. * deal with a fragment that overlaps with two different holes
  648. * (thus being a superset of a previously-received fragment).
  649. */
  650. if ((h >= thisfrag) && (h->last_byte <= start + len)) {
  651. /* complete overlap with hole: remove hole */
  652. if (!h->prev_hole && !h->next_hole) {
  653. /* last remaining hole */
  654. done = 1;
  655. } else if (!h->prev_hole) {
  656. /* first hole */
  657. first_hole = h->next_hole;
  658. payload[h->next_hole].prev_hole = 0;
  659. } else if (!h->next_hole) {
  660. /* last hole */
  661. payload[h->prev_hole].next_hole = 0;
  662. } else {
  663. /* in the middle of the list */
  664. payload[h->next_hole].prev_hole = h->prev_hole;
  665. payload[h->prev_hole].next_hole = h->next_hole;
  666. }
  667. } else if (h->last_byte <= start + len) {
  668. /* overlaps with final part of the hole: shorten this hole */
  669. h->last_byte = start;
  670. } else if (h >= thisfrag) {
  671. /* overlaps with initial part of the hole: move this hole */
  672. newh = thisfrag + (len / 8);
  673. *newh = *h;
  674. h = newh;
  675. if (h->next_hole)
  676. payload[h->next_hole].prev_hole = (h - payload);
  677. if (h->prev_hole)
  678. payload[h->prev_hole].next_hole = (h - payload);
  679. else
  680. first_hole = (h - payload);
  681. } else {
  682. /* fragment sits in the middle: split the hole */
  683. newh = thisfrag + (len / 8);
  684. *newh = *h;
  685. h->last_byte = start;
  686. h->next_hole = (newh - payload);
  687. newh->prev_hole = (h - payload);
  688. if (newh->next_hole)
  689. payload[newh->next_hole].prev_hole = (newh - payload);
  690. }
  691. /* finally copy this fragment and possibly return whole packet */
  692. memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
  693. if (!done)
  694. return NULL;
  695. localip->ip_len = htons(total_len);
  696. *lenp = total_len + IP_HDR_SIZE_NO_UDP;
  697. return localip;
  698. }
  699. static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
  700. {
  701. u16 ip_off = ntohs(ip->ip_off);
  702. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  703. return ip; /* not a fragment */
  704. return __NetDefragment(ip, lenp);
  705. }
  706. #else /* !CONFIG_IP_DEFRAG */
  707. static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
  708. {
  709. u16 ip_off = ntohs(ip->ip_off);
  710. if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
  711. return ip; /* not a fragment */
  712. return NULL;
  713. }
  714. #endif
  715. /**
  716. * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
  717. * drop others.
  718. *
  719. * @parma ip IP packet containing the ICMP
  720. */
  721. static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et)
  722. {
  723. ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
  724. switch (icmph->type) {
  725. case ICMP_REDIRECT:
  726. if (icmph->code != ICMP_REDIR_HOST)
  727. return;
  728. printf(" ICMP Host Redirect to %pI4 ",
  729. &icmph->un.gateway);
  730. break;
  731. default:
  732. #if defined(CONFIG_CMD_PING)
  733. ping_receive(et, ip, len);
  734. #endif
  735. #ifdef CONFIG_CMD_TFTPPUT
  736. if (packet_icmp_handler)
  737. packet_icmp_handler(icmph->type, icmph->code,
  738. ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
  739. icmph->un.data, ntohs(ip->udp_len));
  740. #endif
  741. break;
  742. }
  743. }
  744. void
  745. NetReceive(uchar *inpkt, int len)
  746. {
  747. Ethernet_t *et;
  748. IP_t *ip;
  749. IPaddr_t tmp;
  750. IPaddr_t src_ip;
  751. int x;
  752. #if defined(CONFIG_CMD_CDP)
  753. int iscdp;
  754. #endif
  755. ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
  756. debug("packet received\n");
  757. NetRxPacket = inpkt;
  758. NetRxPacketLen = len;
  759. et = (Ethernet_t *)inpkt;
  760. /* too small packet? */
  761. if (len < ETHER_HDR_SIZE)
  762. return;
  763. #ifdef CONFIG_API
  764. if (push_packet) {
  765. (*push_packet)(inpkt, len);
  766. return;
  767. }
  768. #endif
  769. #if defined(CONFIG_CMD_CDP)
  770. /* keep track if packet is CDP */
  771. iscdp = is_cdp_packet(et->et_dest);
  772. #endif
  773. myvlanid = ntohs(NetOurVLAN);
  774. if (myvlanid == (ushort)-1)
  775. myvlanid = VLAN_NONE;
  776. mynvlanid = ntohs(NetOurNativeVLAN);
  777. if (mynvlanid == (ushort)-1)
  778. mynvlanid = VLAN_NONE;
  779. x = ntohs(et->et_protlen);
  780. debug("packet received\n");
  781. if (x < 1514) {
  782. /*
  783. * Got a 802 packet. Check the other protocol field.
  784. */
  785. x = ntohs(et->et_prot);
  786. ip = (IP_t *)(inpkt + E802_HDR_SIZE);
  787. len -= E802_HDR_SIZE;
  788. } else if (x != PROT_VLAN) { /* normal packet */
  789. ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
  790. len -= ETHER_HDR_SIZE;
  791. } else { /* VLAN packet */
  792. VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
  793. debug("VLAN packet received\n");
  794. /* too small packet? */
  795. if (len < VLAN_ETHER_HDR_SIZE)
  796. return;
  797. /* if no VLAN active */
  798. if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
  799. #if defined(CONFIG_CMD_CDP)
  800. && iscdp == 0
  801. #endif
  802. )
  803. return;
  804. cti = ntohs(vet->vet_tag);
  805. vlanid = cti & VLAN_IDMASK;
  806. x = ntohs(vet->vet_type);
  807. ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
  808. len -= VLAN_ETHER_HDR_SIZE;
  809. }
  810. debug("Receive from protocol 0x%x\n", x);
  811. #if defined(CONFIG_CMD_CDP)
  812. if (iscdp) {
  813. CDPHandler((uchar *)ip, len);
  814. return;
  815. }
  816. #endif
  817. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  818. if (vlanid == VLAN_NONE)
  819. vlanid = (mynvlanid & VLAN_IDMASK);
  820. /* not matched? */
  821. if (vlanid != (myvlanid & VLAN_IDMASK))
  822. return;
  823. }
  824. switch (x) {
  825. case PROT_ARP:
  826. ArpReceive(et, ip, len);
  827. break;
  828. #ifdef CONFIG_CMD_RARP
  829. case PROT_RARP:
  830. rarp_receive(ip, len);
  831. break;
  832. #endif
  833. case PROT_IP:
  834. debug("Got IP\n");
  835. /* Before we start poking the header, make sure it is there */
  836. if (len < IP_HDR_SIZE) {
  837. debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
  838. return;
  839. }
  840. /* Check the packet length */
  841. if (len < ntohs(ip->ip_len)) {
  842. printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
  843. return;
  844. }
  845. len = ntohs(ip->ip_len);
  846. debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
  847. /* Can't deal with anything except IPv4 */
  848. if ((ip->ip_hl_v & 0xf0) != 0x40)
  849. return;
  850. /* Can't deal with IP options (headers != 20 bytes) */
  851. if ((ip->ip_hl_v & 0x0f) > 0x05)
  852. return;
  853. /* Check the Checksum of the header */
  854. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
  855. puts("checksum bad\n");
  856. return;
  857. }
  858. /* If it is not for us, ignore it */
  859. tmp = NetReadIP(&ip->ip_dst);
  860. if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
  861. #ifdef CONFIG_MCAST_TFTP
  862. if (Mcast_addr != tmp)
  863. #endif
  864. return;
  865. }
  866. /* Read source IP address for later use */
  867. src_ip = NetReadIP(&ip->ip_src);
  868. /*
  869. * The function returns the unchanged packet if it's not
  870. * a fragment, and either the complete packet or NULL if
  871. * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
  872. */
  873. ip = NetDefragment(ip, &len);
  874. if (!ip)
  875. return;
  876. /*
  877. * watch for ICMP host redirects
  878. *
  879. * There is no real handler code (yet). We just watch
  880. * for ICMP host redirect messages. In case anybody
  881. * sees these messages: please contact me
  882. * (wd@denx.de), or - even better - send me the
  883. * necessary fixes :-)
  884. *
  885. * Note: in all cases where I have seen this so far
  886. * it was a problem with the router configuration,
  887. * for instance when a router was configured in the
  888. * BOOTP reply, but the TFTP server was on the same
  889. * subnet. So this is probably a warning that your
  890. * configuration might be wrong. But I'm not really
  891. * sure if there aren't any other situations.
  892. *
  893. * Simon Glass <sjg@chromium.org>: We get an ICMP when
  894. * we send a tftp packet to a dead connection, or when
  895. * there is no server at the other end.
  896. */
  897. if (ip->ip_p == IPPROTO_ICMP) {
  898. receive_icmp(ip, len, src_ip, et);
  899. return;
  900. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  901. return;
  902. }
  903. #ifdef CONFIG_UDP_CHECKSUM
  904. if (ip->udp_xsum != 0) {
  905. ulong xsum;
  906. ushort *sumptr;
  907. ushort sumlen;
  908. xsum = ip->ip_p;
  909. xsum += (ntohs(ip->udp_len));
  910. xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
  911. xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
  912. xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
  913. xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
  914. sumlen = ntohs(ip->udp_len);
  915. sumptr = (ushort *) &(ip->udp_src);
  916. while (sumlen > 1) {
  917. ushort sumdata;
  918. sumdata = *sumptr++;
  919. xsum += ntohs(sumdata);
  920. sumlen -= 2;
  921. }
  922. if (sumlen > 0) {
  923. ushort sumdata;
  924. sumdata = *(unsigned char *) sumptr;
  925. sumdata = (sumdata << 8) & 0xff00;
  926. xsum += sumdata;
  927. }
  928. while ((xsum >> 16) != 0) {
  929. xsum = (xsum & 0x0000ffff) +
  930. ((xsum >> 16) & 0x0000ffff);
  931. }
  932. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  933. printf(" UDP wrong checksum %08lx %08x\n",
  934. xsum, ntohs(ip->udp_xsum));
  935. return;
  936. }
  937. }
  938. #endif
  939. #ifdef CONFIG_NETCONSOLE
  940. nc_input_packet((uchar *)ip + IP_HDR_SIZE,
  941. ntohs(ip->udp_dst),
  942. ntohs(ip->udp_src),
  943. ntohs(ip->udp_len) - 8);
  944. #endif
  945. /*
  946. * IP header OK. Pass the packet to the current handler.
  947. */
  948. (*packetHandler)((uchar *)ip + IP_HDR_SIZE,
  949. ntohs(ip->udp_dst),
  950. src_ip,
  951. ntohs(ip->udp_src),
  952. ntohs(ip->udp_len) - 8);
  953. break;
  954. }
  955. }
  956. /**********************************************************************/
  957. static int net_check_prereq(enum proto_t protocol)
  958. {
  959. switch (protocol) {
  960. /* Fall through */
  961. #if defined(CONFIG_CMD_PING)
  962. case PING:
  963. if (NetPingIP == 0) {
  964. puts("*** ERROR: ping address not given\n");
  965. return 1;
  966. }
  967. goto common;
  968. #endif
  969. #if defined(CONFIG_CMD_SNTP)
  970. case SNTP:
  971. if (NetNtpServerIP == 0) {
  972. puts("*** ERROR: NTP server address not given\n");
  973. return 1;
  974. }
  975. goto common;
  976. #endif
  977. #if defined(CONFIG_CMD_DNS)
  978. case DNS:
  979. if (NetOurDNSIP == 0) {
  980. puts("*** ERROR: DNS server address not given\n");
  981. return 1;
  982. }
  983. goto common;
  984. #endif
  985. #if defined(CONFIG_CMD_NFS)
  986. case NFS:
  987. #endif
  988. case TFTPGET:
  989. case TFTPPUT:
  990. if (NetServerIP == 0) {
  991. puts("*** ERROR: `serverip' not set\n");
  992. return 1;
  993. }
  994. #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
  995. defined(CONFIG_CMD_DNS)
  996. common:
  997. #endif
  998. /* Fall through */
  999. case NETCONS:
  1000. case TFTPSRV:
  1001. if (NetOurIP == 0) {
  1002. puts("*** ERROR: `ipaddr' not set\n");
  1003. return 1;
  1004. }
  1005. /* Fall through */
  1006. #ifdef CONFIG_CMD_RARP
  1007. case RARP:
  1008. #endif
  1009. case BOOTP:
  1010. case CDP:
  1011. case DHCP:
  1012. if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  1013. int num = eth_get_dev_index();
  1014. switch (num) {
  1015. case -1:
  1016. puts("*** ERROR: No ethernet found.\n");
  1017. return 1;
  1018. case 0:
  1019. puts("*** ERROR: `ethaddr' not set\n");
  1020. break;
  1021. default:
  1022. printf("*** ERROR: `eth%daddr' not set\n",
  1023. num);
  1024. break;
  1025. }
  1026. NetStartAgain();
  1027. return 2;
  1028. }
  1029. /* Fall through */
  1030. default:
  1031. return 0;
  1032. }
  1033. return 0; /* OK */
  1034. }
  1035. /**********************************************************************/
  1036. int
  1037. NetCksumOk(uchar *ptr, int len)
  1038. {
  1039. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  1040. }
  1041. unsigned
  1042. NetCksum(uchar *ptr, int len)
  1043. {
  1044. ulong xsum;
  1045. ushort *p = (ushort *)ptr;
  1046. xsum = 0;
  1047. while (len-- > 0)
  1048. xsum += *p++;
  1049. xsum = (xsum & 0xffff) + (xsum >> 16);
  1050. xsum = (xsum & 0xffff) + (xsum >> 16);
  1051. return xsum & 0xffff;
  1052. }
  1053. int
  1054. NetEthHdrSize(void)
  1055. {
  1056. ushort myvlanid;
  1057. myvlanid = ntohs(NetOurVLAN);
  1058. if (myvlanid == (ushort)-1)
  1059. myvlanid = VLAN_NONE;
  1060. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
  1061. VLAN_ETHER_HDR_SIZE;
  1062. }
  1063. int
  1064. NetSetEther(uchar *xet, uchar * addr, uint prot)
  1065. {
  1066. Ethernet_t *et = (Ethernet_t *)xet;
  1067. ushort myvlanid;
  1068. myvlanid = ntohs(NetOurVLAN);
  1069. if (myvlanid == (ushort)-1)
  1070. myvlanid = VLAN_NONE;
  1071. memcpy(et->et_dest, addr, 6);
  1072. memcpy(et->et_src, NetOurEther, 6);
  1073. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1074. et->et_protlen = htons(prot);
  1075. return ETHER_HDR_SIZE;
  1076. } else {
  1077. VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
  1078. vet->vet_vlan_type = htons(PROT_VLAN);
  1079. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1080. vet->vet_type = htons(prot);
  1081. return VLAN_ETHER_HDR_SIZE;
  1082. }
  1083. }
  1084. void
  1085. NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
  1086. {
  1087. IP_t *ip = (IP_t *)xip;
  1088. /*
  1089. * If the data is an odd number of bytes, zero the
  1090. * byte after the last byte so that the checksum
  1091. * will work.
  1092. */
  1093. if (len & 1)
  1094. xip[IP_HDR_SIZE + len] = 0;
  1095. /*
  1096. * Construct an IP and UDP header.
  1097. * (need to set no fragment bit - XXX)
  1098. */
  1099. /* IP_HDR_SIZE / 4 (not including UDP) */
  1100. ip->ip_hl_v = 0x45;
  1101. ip->ip_tos = 0;
  1102. ip->ip_len = htons(IP_HDR_SIZE + len);
  1103. ip->ip_id = htons(NetIPID++);
  1104. ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
  1105. ip->ip_ttl = 255;
  1106. ip->ip_p = 17; /* UDP */
  1107. ip->ip_sum = 0;
  1108. /* already in network byte order */
  1109. NetCopyIP((void *)&ip->ip_src, &NetOurIP);
  1110. /* - "" - */
  1111. NetCopyIP((void *)&ip->ip_dst, &dest);
  1112. ip->udp_src = htons(sport);
  1113. ip->udp_dst = htons(dport);
  1114. ip->udp_len = htons(8 + len);
  1115. ip->udp_xsum = 0;
  1116. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  1117. }
  1118. void copy_filename(char *dst, const char *src, int size)
  1119. {
  1120. if (*src && (*src == '"')) {
  1121. ++src;
  1122. --size;
  1123. }
  1124. while ((--size > 0) && *src && (*src != '"'))
  1125. *dst++ = *src++;
  1126. *dst = '\0';
  1127. }
  1128. #if defined(CONFIG_CMD_NFS) || \
  1129. defined(CONFIG_CMD_SNTP) || \
  1130. defined(CONFIG_CMD_DNS)
  1131. /*
  1132. * make port a little random (1024-17407)
  1133. * This keeps the math somewhat trivial to compute, and seems to work with
  1134. * all supported protocols/clients/servers
  1135. */
  1136. unsigned int random_port(void)
  1137. {
  1138. return 1024 + (get_timer(0) % 0x4000);
  1139. }
  1140. #endif
  1141. void ip_to_string(IPaddr_t x, char *s)
  1142. {
  1143. x = ntohl(x);
  1144. sprintf(s, "%d.%d.%d.%d",
  1145. (int) ((x >> 24) & 0xff),
  1146. (int) ((x >> 16) & 0xff),
  1147. (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
  1148. );
  1149. }
  1150. void VLAN_to_string(ushort x, char *s)
  1151. {
  1152. x = ntohs(x);
  1153. if (x == (ushort)-1)
  1154. x = VLAN_NONE;
  1155. if (x == VLAN_NONE)
  1156. strcpy(s, "none");
  1157. else
  1158. sprintf(s, "%d", x & VLAN_IDMASK);
  1159. }
  1160. ushort string_to_VLAN(const char *s)
  1161. {
  1162. ushort id;
  1163. if (s == NULL)
  1164. return htons(VLAN_NONE);
  1165. if (*s < '0' || *s > '9')
  1166. id = VLAN_NONE;
  1167. else
  1168. id = (ushort)simple_strtoul(s, NULL, 10);
  1169. return htons(id);
  1170. }
  1171. ushort getenv_VLAN(char *var)
  1172. {
  1173. return string_to_VLAN(getenv(var));
  1174. }