net.c 37 KB

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