net.c 38 KB

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