net.c 36 KB

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