net.c 37 KB

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