net.c 39 KB

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