net.c 35 KB

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