net.c 38 KB

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