net.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  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(eth_get_dev()->name, 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(eth_get_dev()->name, 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. ushort *p;
  697. if (len > 0) {
  698. odd = 1 & (ulong)buff;
  699. if (odd) {
  700. result = *buff << 8;
  701. len--;
  702. buff++;
  703. }
  704. while (len > 1) {
  705. p = (ushort *)buff;
  706. result += *p++;
  707. buff = (uchar *)p;
  708. if (result & 0x80000000)
  709. result = (result & 0xFFFF) + (result >> 16);
  710. len -= 2;
  711. }
  712. if (len) {
  713. leftover = (signed short)(*(const signed char *)buff);
  714. /* CISCO SUCKS big time! (and blows too):
  715. * CDP uses the IP checksum algorithm with a twist;
  716. * for the last byte it *sign* extends and sums.
  717. */
  718. result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
  719. }
  720. while (result >> 16)
  721. result = (result & 0xFFFF) + (result >> 16);
  722. if (odd)
  723. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  724. }
  725. /* add up 16-bit and 17-bit words for 17+c bits */
  726. result = (result & 0xffff) + (result >> 16);
  727. /* add up 16-bit and 2-bit for 16+c bit */
  728. result = (result & 0xffff) + (result >> 16);
  729. /* add up carry.. */
  730. result = (result & 0xffff) + (result >> 16);
  731. /* negate */
  732. csum = ~(ushort)result;
  733. /* run time endian detection */
  734. if (csum != htons(csum)) /* little endian */
  735. csum = htons(csum);
  736. return csum;
  737. }
  738. int CDPSendTrigger(void)
  739. {
  740. volatile uchar *pkt;
  741. volatile ushort *s;
  742. volatile ushort *cp;
  743. Ethernet_t *et;
  744. int len;
  745. ushort chksum;
  746. #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
  747. defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
  748. char buf[32];
  749. #endif
  750. pkt = NetTxPacket;
  751. et = (Ethernet_t *)pkt;
  752. /* NOTE: trigger sent not on any VLAN */
  753. /* form ethernet header */
  754. memcpy(et->et_dest, NetCDPAddr, 6);
  755. memcpy(et->et_src, NetOurEther, 6);
  756. pkt += ETHER_HDR_SIZE;
  757. /* SNAP header */
  758. memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
  759. pkt += sizeof(CDP_SNAP_hdr);
  760. /* CDP header */
  761. *pkt++ = 0x02; /* CDP version 2 */
  762. *pkt++ = 180; /* TTL */
  763. s = (volatile ushort *)pkt;
  764. cp = s;
  765. *s++ = htons(0); /* checksum (0 for later calculation) */
  766. /* CDP fields */
  767. #ifdef CONFIG_CDP_DEVICE_ID
  768. *s++ = htons(CDP_DEVICE_ID_TLV);
  769. *s++ = htons(CONFIG_CDP_DEVICE_ID);
  770. memset(buf, 0, sizeof(buf));
  771. sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
  772. NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
  773. NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
  774. NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
  775. memcpy((uchar *)s, buf, 16);
  776. s += 16 / 2;
  777. #endif
  778. #ifdef CONFIG_CDP_PORT_ID
  779. *s++ = htons(CDP_PORT_ID_TLV);
  780. memset(buf, 0, sizeof(buf));
  781. sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
  782. len = strlen(buf);
  783. if (len & 1) /* make it even */
  784. len++;
  785. *s++ = htons(len + 4);
  786. memcpy((uchar *)s, buf, len);
  787. s += len / 2;
  788. #endif
  789. #ifdef CONFIG_CDP_CAPABILITIES
  790. *s++ = htons(CDP_CAPABILITIES_TLV);
  791. *s++ = htons(8);
  792. *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
  793. s += 2;
  794. #endif
  795. #ifdef CONFIG_CDP_VERSION
  796. *s++ = htons(CDP_VERSION_TLV);
  797. memset(buf, 0, sizeof(buf));
  798. strcpy(buf, CONFIG_CDP_VERSION);
  799. len = strlen(buf);
  800. if (len & 1) /* make it even */
  801. len++;
  802. *s++ = htons(len + 4);
  803. memcpy((uchar *)s, buf, len);
  804. s += len / 2;
  805. #endif
  806. #ifdef CONFIG_CDP_PLATFORM
  807. *s++ = htons(CDP_PLATFORM_TLV);
  808. memset(buf, 0, sizeof(buf));
  809. strcpy(buf, CONFIG_CDP_PLATFORM);
  810. len = strlen(buf);
  811. if (len & 1) /* make it even */
  812. len++;
  813. *s++ = htons(len + 4);
  814. memcpy((uchar *)s, buf, len);
  815. s += len / 2;
  816. #endif
  817. #ifdef CONFIG_CDP_TRIGGER
  818. *s++ = htons(CDP_TRIGGER_TLV);
  819. *s++ = htons(8);
  820. *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
  821. s += 2;
  822. #endif
  823. #ifdef CONFIG_CDP_POWER_CONSUMPTION
  824. *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
  825. *s++ = htons(6);
  826. *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
  827. #endif
  828. /* length of ethernet packet */
  829. len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
  830. et->et_protlen = htons(len);
  831. len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
  832. chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
  833. if (chksum == 0)
  834. chksum = 0xFFFF;
  835. *cp = htons(chksum);
  836. (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
  837. return 0;
  838. }
  839. static void
  840. CDPTimeout (void)
  841. {
  842. CDPSeq++;
  843. if (CDPSeq < 3) {
  844. NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
  845. CDPSendTrigger();
  846. return;
  847. }
  848. /* if not OK try again */
  849. if (!CDPOK)
  850. NetStartAgain();
  851. else
  852. NetState = NETLOOP_SUCCESS;
  853. }
  854. static void
  855. CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  856. {
  857. /* nothing */
  858. }
  859. static void
  860. CDPHandler(const uchar * pkt, unsigned len)
  861. {
  862. const uchar *t;
  863. const ushort *ss;
  864. ushort type, tlen;
  865. uchar applid;
  866. ushort vlan, nvlan;
  867. /* minimum size? */
  868. if (len < sizeof(CDP_SNAP_hdr) + 4)
  869. goto pkt_short;
  870. /* check for valid CDP SNAP header */
  871. if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
  872. return;
  873. pkt += sizeof(CDP_SNAP_hdr);
  874. len -= sizeof(CDP_SNAP_hdr);
  875. /* Version of CDP protocol must be >= 2 and TTL != 0 */
  876. if (pkt[0] < 0x02 || pkt[1] == 0)
  877. return;
  878. /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
  879. if (pkt[0] != 0x02)
  880. printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
  881. pkt[0] & 0xff);
  882. if (CDP_compute_csum(pkt, len) != 0)
  883. return;
  884. pkt += 4;
  885. len -= 4;
  886. vlan = htons(-1);
  887. nvlan = htons(-1);
  888. while (len > 0) {
  889. if (len < 4)
  890. goto pkt_short;
  891. ss = (const ushort *)pkt;
  892. type = ntohs(ss[0]);
  893. tlen = ntohs(ss[1]);
  894. if (tlen > len) {
  895. goto pkt_short;
  896. }
  897. pkt += tlen;
  898. len -= tlen;
  899. ss += 2; /* point ss to the data of the TLV */
  900. tlen -= 4;
  901. switch (type) {
  902. case CDP_DEVICE_ID_TLV:
  903. break;
  904. case CDP_ADDRESS_TLV:
  905. break;
  906. case CDP_PORT_ID_TLV:
  907. break;
  908. case CDP_CAPABILITIES_TLV:
  909. break;
  910. case CDP_VERSION_TLV:
  911. break;
  912. case CDP_PLATFORM_TLV:
  913. break;
  914. case CDP_NATIVE_VLAN_TLV:
  915. nvlan = *ss;
  916. break;
  917. case CDP_APPLIANCE_VLAN_TLV:
  918. t = (const uchar *)ss;
  919. while (tlen > 0) {
  920. if (tlen < 3)
  921. goto pkt_short;
  922. applid = t[0];
  923. ss = (const ushort *)(t + 1);
  924. #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
  925. if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
  926. vlan = *ss;
  927. #else
  928. vlan = ntohs(*ss); /* XXX will this work; dunno */
  929. #endif
  930. t += 3; tlen -= 3;
  931. }
  932. break;
  933. case CDP_TRIGGER_TLV:
  934. break;
  935. case CDP_POWER_CONSUMPTION_TLV:
  936. break;
  937. case CDP_SYSNAME_TLV:
  938. break;
  939. case CDP_SYSOBJECT_TLV:
  940. break;
  941. case CDP_MANAGEMENT_ADDRESS_TLV:
  942. break;
  943. }
  944. }
  945. CDPApplianceVLAN = vlan;
  946. CDPNativeVLAN = nvlan;
  947. CDPOK = 1;
  948. return;
  949. pkt_short:
  950. printf("** CDP packet is too short\n");
  951. return;
  952. }
  953. static void CDPStart(void)
  954. {
  955. #if defined(CONFIG_NET_MULTI)
  956. printf ("Using %s device\n", eth_get_name());
  957. #endif
  958. CDPSeq = 0;
  959. CDPOK = 0;
  960. CDPNativeVLAN = htons(-1);
  961. CDPApplianceVLAN = htons(-1);
  962. NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
  963. NetSetHandler (CDPDummyHandler);
  964. CDPSendTrigger();
  965. }
  966. #endif /* CFG_CMD_CDP */
  967. void
  968. NetReceive(volatile uchar * inpkt, int len)
  969. {
  970. Ethernet_t *et;
  971. IP_t *ip;
  972. ARP_t *arp;
  973. IPaddr_t tmp;
  974. int x;
  975. uchar *pkt;
  976. #if (CONFIG_COMMANDS & CFG_CMD_CDP)
  977. int iscdp;
  978. #endif
  979. ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
  980. #ifdef ET_DEBUG
  981. printf("packet received\n");
  982. #endif
  983. NetRxPkt = inpkt;
  984. NetRxPktLen = len;
  985. et = (Ethernet_t *)inpkt;
  986. /* too small packet? */
  987. if (len < ETHER_HDR_SIZE)
  988. return;
  989. #if (CONFIG_COMMANDS & CFG_CMD_CDP)
  990. /* keep track if packet is CDP */
  991. iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
  992. #endif
  993. myvlanid = ntohs(NetOurVLAN);
  994. if (myvlanid == (ushort)-1)
  995. myvlanid = VLAN_NONE;
  996. mynvlanid = ntohs(NetOurNativeVLAN);
  997. if (mynvlanid == (ushort)-1)
  998. mynvlanid = VLAN_NONE;
  999. x = ntohs(et->et_protlen);
  1000. #ifdef ET_DEBUG
  1001. printf("packet received\n");
  1002. #endif
  1003. if (x < 1514) {
  1004. /*
  1005. * Got a 802 packet. Check the other protocol field.
  1006. */
  1007. x = ntohs(et->et_prot);
  1008. ip = (IP_t *)(inpkt + E802_HDR_SIZE);
  1009. len -= E802_HDR_SIZE;
  1010. } else if (x != PROT_VLAN) { /* normal packet */
  1011. ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
  1012. len -= ETHER_HDR_SIZE;
  1013. } else { /* VLAN packet */
  1014. VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
  1015. #ifdef ET_DEBUG
  1016. printf("VLAN packet received\n");
  1017. #endif
  1018. /* too small packet? */
  1019. if (len < VLAN_ETHER_HDR_SIZE)
  1020. return;
  1021. /* if no VLAN active */
  1022. if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
  1023. #if (CONFIG_COMMANDS & CFG_CMD_CDP)
  1024. && iscdp == 0
  1025. #endif
  1026. )
  1027. return;
  1028. cti = ntohs(vet->vet_tag);
  1029. vlanid = cti & VLAN_IDMASK;
  1030. x = ntohs(vet->vet_type);
  1031. ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
  1032. len -= VLAN_ETHER_HDR_SIZE;
  1033. }
  1034. #ifdef ET_DEBUG
  1035. printf("Receive from protocol 0x%x\n", x);
  1036. #endif
  1037. #if (CONFIG_COMMANDS & CFG_CMD_CDP)
  1038. if (iscdp) {
  1039. CDPHandler((uchar *)ip, len);
  1040. return;
  1041. }
  1042. #endif
  1043. if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
  1044. if (vlanid == VLAN_NONE)
  1045. vlanid = (mynvlanid & VLAN_IDMASK);
  1046. /* not matched? */
  1047. if (vlanid != (myvlanid & VLAN_IDMASK))
  1048. return;
  1049. }
  1050. switch (x) {
  1051. case PROT_ARP:
  1052. /*
  1053. * We have to deal with two types of ARP packets:
  1054. * - REQUEST packets will be answered by sending our
  1055. * IP address - if we know it.
  1056. * - REPLY packates are expected only after we asked
  1057. * for the TFTP server's or the gateway's ethernet
  1058. * address; so if we receive such a packet, we set
  1059. * the server ethernet address
  1060. */
  1061. #ifdef ET_DEBUG
  1062. puts ("Got ARP\n");
  1063. #endif
  1064. arp = (ARP_t *)ip;
  1065. if (len < ARP_HDR_SIZE) {
  1066. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  1067. return;
  1068. }
  1069. if (ntohs(arp->ar_hrd) != ARP_ETHER) {
  1070. return;
  1071. }
  1072. if (ntohs(arp->ar_pro) != PROT_IP) {
  1073. return;
  1074. }
  1075. if (arp->ar_hln != 6) {
  1076. return;
  1077. }
  1078. if (arp->ar_pln != 4) {
  1079. return;
  1080. }
  1081. if (NetOurIP == 0) {
  1082. return;
  1083. }
  1084. if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
  1085. return;
  1086. }
  1087. switch (ntohs(arp->ar_op)) {
  1088. case ARPOP_REQUEST: /* reply with our IP address */
  1089. #ifdef ET_DEBUG
  1090. puts ("Got ARP REQUEST, return our IP\n");
  1091. #endif
  1092. pkt = (uchar *)et;
  1093. pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
  1094. arp->ar_op = htons(ARPOP_REPLY);
  1095. memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
  1096. NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
  1097. memcpy (&arp->ar_data[ 0], NetOurEther, 6);
  1098. NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
  1099. (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
  1100. return;
  1101. case ARPOP_REPLY: /* arp reply */
  1102. /* are we waiting for a reply */
  1103. if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
  1104. break;
  1105. #ifdef ET_DEBUG
  1106. printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
  1107. arp->ar_data[0], arp->ar_data[1],
  1108. arp->ar_data[2], arp->ar_data[3],
  1109. arp->ar_data[4], arp->ar_data[5]);
  1110. #endif
  1111. tmp = NetReadIP(&arp->ar_data[6]);
  1112. /* matched waiting packet's address */
  1113. if (tmp == NetArpWaitReplyIP) {
  1114. #ifdef ET_DEBUG
  1115. puts ("Got it\n");
  1116. #endif
  1117. /* save address for later use */
  1118. memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
  1119. #ifdef CONFIG_NETCONSOLE
  1120. (*packetHandler)(0,0,0,0);
  1121. #endif
  1122. /* modify header, and transmit it */
  1123. memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
  1124. (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
  1125. /* no arp request pending now */
  1126. NetArpWaitPacketIP = 0;
  1127. NetArpWaitTxPacketSize = 0;
  1128. NetArpWaitPacketMAC = NULL;
  1129. }
  1130. return;
  1131. default:
  1132. #ifdef ET_DEBUG
  1133. printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
  1134. #endif
  1135. return;
  1136. }
  1137. break;
  1138. case PROT_RARP:
  1139. #ifdef ET_DEBUG
  1140. puts ("Got RARP\n");
  1141. #endif
  1142. arp = (ARP_t *)ip;
  1143. if (len < ARP_HDR_SIZE) {
  1144. printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
  1145. return;
  1146. }
  1147. if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
  1148. (ntohs(arp->ar_hrd) != ARP_ETHER) ||
  1149. (ntohs(arp->ar_pro) != PROT_IP) ||
  1150. (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
  1151. puts ("invalid RARP header\n");
  1152. } else {
  1153. NetCopyIP(&NetOurIP, &arp->ar_data[16]);
  1154. if (NetServerIP == 0)
  1155. NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
  1156. memcpy (NetServerEther, &arp->ar_data[ 0], 6);
  1157. (*packetHandler)(0,0,0,0);
  1158. }
  1159. break;
  1160. case PROT_IP:
  1161. #ifdef ET_DEBUG
  1162. puts ("Got IP\n");
  1163. #endif
  1164. if (len < IP_HDR_SIZE) {
  1165. debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
  1166. return;
  1167. }
  1168. if (len < ntohs(ip->ip_len)) {
  1169. printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
  1170. return;
  1171. }
  1172. len = ntohs(ip->ip_len);
  1173. #ifdef ET_DEBUG
  1174. printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
  1175. #endif
  1176. if ((ip->ip_hl_v & 0xf0) != 0x40) {
  1177. return;
  1178. }
  1179. if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
  1180. return;
  1181. }
  1182. if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
  1183. puts ("checksum bad\n");
  1184. return;
  1185. }
  1186. tmp = NetReadIP(&ip->ip_dst);
  1187. if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
  1188. return;
  1189. }
  1190. /*
  1191. * watch for ICMP host redirects
  1192. *
  1193. * There is no real handler code (yet). We just watch
  1194. * for ICMP host redirect messages. In case anybody
  1195. * sees these messages: please contact me
  1196. * (wd@denx.de), or - even better - send me the
  1197. * necessary fixes :-)
  1198. *
  1199. * Note: in all cases where I have seen this so far
  1200. * it was a problem with the router configuration,
  1201. * for instance when a router was configured in the
  1202. * BOOTP reply, but the TFTP server was on the same
  1203. * subnet. So this is probably a warning that your
  1204. * configuration might be wrong. But I'm not really
  1205. * sure if there aren't any other situations.
  1206. */
  1207. if (ip->ip_p == IPPROTO_ICMP) {
  1208. ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
  1209. switch (icmph->type) {
  1210. case ICMP_REDIRECT:
  1211. if (icmph->code != ICMP_REDIR_HOST)
  1212. return;
  1213. puts (" ICMP Host Redirect to ");
  1214. print_IPaddr(icmph->un.gateway);
  1215. putc(' ');
  1216. return;
  1217. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  1218. case ICMP_ECHO_REPLY:
  1219. /*
  1220. * IP header OK. Pass the packet to the current handler.
  1221. */
  1222. /* XXX point to ip packet */
  1223. (*packetHandler)((uchar *)ip, 0, 0, 0);
  1224. return;
  1225. #endif
  1226. default:
  1227. return;
  1228. }
  1229. } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
  1230. return;
  1231. }
  1232. #ifdef CONFIG_UDP_CHECKSUM
  1233. if (ip->udp_xsum != 0) {
  1234. ulong xsum;
  1235. ushort *sumptr;
  1236. ushort sumlen;
  1237. xsum = ip->ip_p;
  1238. xsum += (ntohs(ip->udp_len));
  1239. xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
  1240. xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
  1241. xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
  1242. xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
  1243. sumlen = ntohs(ip->udp_len);
  1244. sumptr = (ushort *) &(ip->udp_src);
  1245. while (sumlen > 1) {
  1246. ushort sumdata;
  1247. sumdata = *sumptr++;
  1248. xsum += ntohs(sumdata);
  1249. sumlen -= 2;
  1250. }
  1251. if (sumlen > 0) {
  1252. ushort sumdata;
  1253. sumdata = *(unsigned char *) sumptr;
  1254. sumdata = (sumdata << 8) & 0xff00;
  1255. xsum += sumdata;
  1256. }
  1257. while ((xsum >> 16) != 0) {
  1258. xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
  1259. }
  1260. if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
  1261. printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum));
  1262. return;
  1263. }
  1264. }
  1265. #endif
  1266. #ifdef CONFIG_NETCONSOLE
  1267. nc_input_packet((uchar *)ip +IP_HDR_SIZE,
  1268. ntohs(ip->udp_dst),
  1269. ntohs(ip->udp_src),
  1270. ntohs(ip->udp_len) - 8);
  1271. #endif
  1272. /*
  1273. * IP header OK. Pass the packet to the current handler.
  1274. */
  1275. (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
  1276. ntohs(ip->udp_dst),
  1277. ntohs(ip->udp_src),
  1278. ntohs(ip->udp_len) - 8);
  1279. break;
  1280. }
  1281. }
  1282. /**********************************************************************/
  1283. static int net_check_prereq (proto_t protocol)
  1284. {
  1285. switch (protocol) {
  1286. /* Fall through */
  1287. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  1288. case PING:
  1289. if (NetPingIP == 0) {
  1290. puts ("*** ERROR: ping address not given\n");
  1291. return (1);
  1292. }
  1293. goto common;
  1294. #endif
  1295. #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
  1296. case SNTP:
  1297. if (NetNtpServerIP == 0) {
  1298. puts ("*** ERROR: NTP server address not given\n");
  1299. return (1);
  1300. }
  1301. goto common;
  1302. #endif
  1303. #if (CONFIG_COMMANDS & CFG_CMD_NFS)
  1304. case NFS:
  1305. #endif
  1306. case NETCONS:
  1307. case TFTP:
  1308. if (NetServerIP == 0) {
  1309. puts ("*** ERROR: `serverip' not set\n");
  1310. return (1);
  1311. }
  1312. #if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
  1313. common:
  1314. #endif
  1315. if (NetOurIP == 0) {
  1316. puts ("*** ERROR: `ipaddr' not set\n");
  1317. return (1);
  1318. }
  1319. /* Fall through */
  1320. case DHCP:
  1321. case RARP:
  1322. case BOOTP:
  1323. case CDP:
  1324. if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
  1325. #ifdef CONFIG_NET_MULTI
  1326. extern int eth_get_dev_index (void);
  1327. int num = eth_get_dev_index ();
  1328. switch (num) {
  1329. case -1:
  1330. puts ("*** ERROR: No ethernet found.\n");
  1331. return (1);
  1332. case 0:
  1333. puts ("*** ERROR: `ethaddr' not set\n");
  1334. break;
  1335. default:
  1336. printf ("*** ERROR: `eth%daddr' not set\n",
  1337. num);
  1338. break;
  1339. }
  1340. NetStartAgain ();
  1341. return (2);
  1342. #else
  1343. puts ("*** ERROR: `ethaddr' not set\n");
  1344. return (1);
  1345. #endif
  1346. }
  1347. /* Fall through */
  1348. default:
  1349. return (0);
  1350. }
  1351. return (0); /* OK */
  1352. }
  1353. /**********************************************************************/
  1354. int
  1355. NetCksumOk(uchar * ptr, int len)
  1356. {
  1357. return !((NetCksum(ptr, len) + 1) & 0xfffe);
  1358. }
  1359. unsigned
  1360. NetCksum(uchar * ptr, int len)
  1361. {
  1362. ulong xsum;
  1363. ushort *p = (ushort *)ptr;
  1364. xsum = 0;
  1365. while (len-- > 0)
  1366. xsum += *p++;
  1367. xsum = (xsum & 0xffff) + (xsum >> 16);
  1368. xsum = (xsum & 0xffff) + (xsum >> 16);
  1369. return (xsum & 0xffff);
  1370. }
  1371. int
  1372. NetEthHdrSize(void)
  1373. {
  1374. ushort myvlanid;
  1375. myvlanid = ntohs(NetOurVLAN);
  1376. if (myvlanid == (ushort)-1)
  1377. myvlanid = VLAN_NONE;
  1378. return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
  1379. }
  1380. int
  1381. NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
  1382. {
  1383. Ethernet_t *et = (Ethernet_t *)xet;
  1384. ushort myvlanid;
  1385. myvlanid = ntohs(NetOurVLAN);
  1386. if (myvlanid == (ushort)-1)
  1387. myvlanid = VLAN_NONE;
  1388. memcpy (et->et_dest, addr, 6);
  1389. memcpy (et->et_src, NetOurEther, 6);
  1390. if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
  1391. et->et_protlen = htons(prot);
  1392. return ETHER_HDR_SIZE;
  1393. } else {
  1394. VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
  1395. vet->vet_vlan_type = htons(PROT_VLAN);
  1396. vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
  1397. vet->vet_type = htons(prot);
  1398. return VLAN_ETHER_HDR_SIZE;
  1399. }
  1400. }
  1401. void
  1402. NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
  1403. {
  1404. volatile IP_t *ip = (IP_t *)xip;
  1405. /*
  1406. * If the data is an odd number of bytes, zero the
  1407. * byte after the last byte so that the checksum
  1408. * will work.
  1409. */
  1410. if (len & 1)
  1411. xip[IP_HDR_SIZE + len] = 0;
  1412. /*
  1413. * Construct an IP and UDP header.
  1414. * (need to set no fragment bit - XXX)
  1415. */
  1416. ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
  1417. ip->ip_tos = 0;
  1418. ip->ip_len = htons(IP_HDR_SIZE + len);
  1419. ip->ip_id = htons(NetIPID++);
  1420. ip->ip_off = htons(0x4000); /* No fragmentation */
  1421. ip->ip_ttl = 255;
  1422. ip->ip_p = 17; /* UDP */
  1423. ip->ip_sum = 0;
  1424. NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
  1425. NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
  1426. ip->udp_src = htons(sport);
  1427. ip->udp_dst = htons(dport);
  1428. ip->udp_len = htons(8 + len);
  1429. ip->udp_xsum = 0;
  1430. ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
  1431. }
  1432. void copy_filename (char *dst, char *src, int size)
  1433. {
  1434. if (*src && (*src == '"')) {
  1435. ++src;
  1436. --size;
  1437. }
  1438. while ((--size > 0) && *src && (*src != '"')) {
  1439. *dst++ = *src++;
  1440. }
  1441. *dst = '\0';
  1442. }
  1443. #endif /* CFG_CMD_NET */
  1444. void ip_to_string (IPaddr_t x, char *s)
  1445. {
  1446. x = ntohl (x);
  1447. sprintf (s, "%d.%d.%d.%d",
  1448. (int) ((x >> 24) & 0xff),
  1449. (int) ((x >> 16) & 0xff),
  1450. (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
  1451. );
  1452. }
  1453. IPaddr_t string_to_ip(char *s)
  1454. {
  1455. IPaddr_t addr;
  1456. char *e;
  1457. int i;
  1458. if (s == NULL)
  1459. return(0);
  1460. for (addr=0, i=0; i<4; ++i) {
  1461. ulong val = s ? simple_strtoul(s, &e, 10) : 0;
  1462. addr <<= 8;
  1463. addr |= (val & 0xFF);
  1464. if (s) {
  1465. s = (*e) ? e+1 : e;
  1466. }
  1467. }
  1468. return (htonl(addr));
  1469. }
  1470. void VLAN_to_string(ushort x, char *s)
  1471. {
  1472. x = ntohs(x);
  1473. if (x == (ushort)-1)
  1474. x = VLAN_NONE;
  1475. if (x == VLAN_NONE)
  1476. strcpy(s, "none");
  1477. else
  1478. sprintf(s, "%d", x & VLAN_IDMASK);
  1479. }
  1480. ushort string_to_VLAN(char *s)
  1481. {
  1482. ushort id;
  1483. if (s == NULL)
  1484. return htons(VLAN_NONE);
  1485. if (*s < '0' || *s > '9')
  1486. id = VLAN_NONE;
  1487. else
  1488. id = (ushort)simple_strtoul(s, NULL, 10);
  1489. return htons(id);
  1490. }
  1491. void print_IPaddr (IPaddr_t x)
  1492. {
  1493. char tmp[16];
  1494. ip_to_string (x, tmp);
  1495. puts (tmp);
  1496. }
  1497. IPaddr_t getenv_IPaddr (char *var)
  1498. {
  1499. return (string_to_ip(getenv(var)));
  1500. }
  1501. ushort getenv_VLAN(char *var)
  1502. {
  1503. return (string_to_VLAN(getenv(var)));
  1504. }