net.c 39 KB

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