net.c 37 KB

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