net.c 40 KB

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