net.c 39 KB

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