net.c 39 KB

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