net.c 38 KB

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