net.c 39 KB

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