net.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * LiMon Monitor (LiMon) - Network.
  3. *
  4. * Copyright 1994 - 2000 Neil Russell.
  5. * (See License)
  6. *
  7. *
  8. * History
  9. * 9/16/00 bor adapted to TQM823L/STK8xxL board, RARP/TFTP boot added
  10. */
  11. #ifndef __NET_H__
  12. #define __NET_H__
  13. #if defined(CONFIG_8xx)
  14. #include <commproc.h>
  15. # if !defined(CONFIG_NET_MULTI)
  16. # if defined(FEC_ENET) || defined(SCC_ENET)
  17. # define CONFIG_NET_MULTI
  18. # endif
  19. # endif
  20. #endif /* CONFIG_8xx */
  21. #if defined(CONFIG_MPC5xxx)
  22. # if !defined(CONFIG_NET_MULTI)
  23. # if defined(CONFIG_MPC5xxx_FEC)
  24. # define CONFIG_NET_MULTI
  25. # endif
  26. # endif
  27. #endif /* CONFIG_MPC5xxx */
  28. #if !defined(CONFIG_NET_MULTI) && defined(CONFIG_CPM2)
  29. #include <config.h>
  30. #if defined(CONFIG_ETHER_ON_FCC)
  31. #if defined(CONFIG_ETHER_ON_SCC)
  32. #error "Ethernet not correctly defined"
  33. #endif /* CONFIG_ETHER_ON_SCC */
  34. #define CONFIG_NET_MULTI
  35. #if (CONFIG_ETHER_INDEX == 1)
  36. #define CONFIG_ETHER_ON_FCC1
  37. # define CONFIG_SYS_CMXFCR_MASK1 CONFIG_SYS_CMXFCR_MASK
  38. # define CONFIG_SYS_CMXFCR_VALUE1 CONFIG_SYS_CMXFCR_VALUE
  39. #elif (CONFIG_ETHER_INDEX == 2)
  40. #define CONFIG_ETHER_ON_FCC2
  41. # define CONFIG_SYS_CMXFCR_MASK2 CONFIG_SYS_CMXFCR_MASK
  42. # define CONFIG_SYS_CMXFCR_VALUE2 CONFIG_SYS_CMXFCR_VALUE
  43. #elif (CONFIG_ETHER_INDEX == 3)
  44. #define CONFIG_ETHER_ON_FCC3
  45. # define CONFIG_SYS_CMXFCR_MASK3 CONFIG_SYS_CMXFCR_MASK
  46. # define CONFIG_SYS_CMXFCR_VALUE3 CONFIG_SYS_CMXFCR_VALUE
  47. #endif /* CONFIG_ETHER_INDEX */
  48. #endif /* CONFIG_ETHER_ON_FCC */
  49. #endif /* !CONFIG_NET_MULTI && CONFIG_8260 */
  50. #include <asm/byteorder.h> /* for nton* / ntoh* stuff */
  51. /*
  52. * The number of receive packet buffers, and the required packet buffer
  53. * alignment in memory.
  54. *
  55. */
  56. #ifdef CONFIG_SYS_RX_ETH_BUFFER
  57. # define PKTBUFSRX CONFIG_SYS_RX_ETH_BUFFER
  58. #else
  59. # define PKTBUFSRX 4
  60. #endif
  61. #define PKTALIGN 32
  62. typedef ulong IPaddr_t;
  63. /**
  64. * An incoming packet handler.
  65. * @param pkt pointer to the application packet
  66. * @param dport destination UDP port
  67. * @param sip source IP address
  68. * @param sport source UDP port
  69. * @param len packet length
  70. */
  71. typedef void rxhand_f(uchar *pkt, unsigned dport,
  72. IPaddr_t sip, unsigned sport,
  73. unsigned len);
  74. /*
  75. * A timeout handler. Called after time interval has expired.
  76. */
  77. typedef void thand_f(void);
  78. #define NAMESIZE 16
  79. enum eth_state_t {
  80. ETH_STATE_INIT,
  81. ETH_STATE_PASSIVE,
  82. ETH_STATE_ACTIVE
  83. };
  84. struct eth_device {
  85. char name[NAMESIZE];
  86. unsigned char enetaddr[6];
  87. int iobase;
  88. int state;
  89. int (*init) (struct eth_device*, bd_t*);
  90. int (*send) (struct eth_device*, volatile void* packet, int length);
  91. int (*recv) (struct eth_device*);
  92. void (*halt) (struct eth_device*);
  93. #ifdef CONFIG_MCAST_TFTP
  94. int (*mcast) (struct eth_device*, u32 ip, u8 set);
  95. #endif
  96. int (*write_hwaddr) (struct eth_device*);
  97. struct eth_device *next;
  98. void *priv;
  99. };
  100. extern int eth_initialize(bd_t *bis); /* Initialize network subsystem */
  101. extern int eth_register(struct eth_device* dev);/* Register network device */
  102. extern void eth_try_another(int first_restart); /* Change the device */
  103. #ifdef CONFIG_NET_MULTI
  104. extern void eth_set_current(void); /* set nterface to ethcur var */
  105. #endif
  106. extern struct eth_device *eth_get_dev(void); /* get the current device MAC */
  107. extern struct eth_device *eth_get_dev_by_name(const char *devname);
  108. extern struct eth_device *eth_get_dev_by_index(int index); /* get dev @ index */
  109. extern int eth_get_dev_index (void); /* get the device index */
  110. extern void eth_parse_enetaddr(const char *addr, uchar *enetaddr);
  111. extern int eth_getenv_enetaddr(char *name, uchar *enetaddr);
  112. extern int eth_setenv_enetaddr(char *name, const uchar *enetaddr);
  113. /*
  114. * Get the hardware address for an ethernet interface .
  115. * Args:
  116. * base_name - base name for device (normally "eth")
  117. * index - device index number (0 for first)
  118. * enetaddr - returns 6 byte hardware address
  119. * Returns:
  120. * Return true if the address is valid.
  121. */
  122. extern int eth_getenv_enetaddr_by_index(const char *base_name, int index,
  123. uchar *enetaddr);
  124. extern int usb_eth_initialize(bd_t *bi);
  125. extern int eth_init(bd_t *bis); /* Initialize the device */
  126. extern int eth_send(volatile void *packet, int length); /* Send a packet */
  127. #ifdef CONFIG_API
  128. extern int eth_receive(volatile void *packet, int length); /* Receive a packet*/
  129. #endif
  130. extern int eth_rx(void); /* Check for received packets */
  131. extern void eth_halt(void); /* stop SCC */
  132. extern char *eth_get_name(void); /* get name of current device */
  133. /*
  134. * Set the hardware address for an ethernet interface based on 'eth%daddr'
  135. * environment variable (or just 'ethaddr' if eth_number is 0).
  136. * Args:
  137. * base_name - base name for device (normally "eth")
  138. * eth_number - value of %d (0 for first device of this type)
  139. * Returns:
  140. * 0 is success, non-zero is error status from driver.
  141. */
  142. int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
  143. int eth_number);
  144. #ifdef CONFIG_MCAST_TFTP
  145. int eth_mcast_join( IPaddr_t mcast_addr, u8 join);
  146. u32 ether_crc (size_t len, unsigned char const *p);
  147. #endif
  148. /**********************************************************************/
  149. /*
  150. * Protocol headers.
  151. */
  152. /*
  153. * Ethernet header
  154. */
  155. typedef struct {
  156. uchar et_dest[6]; /* Destination node */
  157. uchar et_src[6]; /* Source node */
  158. ushort et_protlen; /* Protocol or length */
  159. uchar et_dsap; /* 802 DSAP */
  160. uchar et_ssap; /* 802 SSAP */
  161. uchar et_ctl; /* 802 control */
  162. uchar et_snap1; /* SNAP */
  163. uchar et_snap2;
  164. uchar et_snap3;
  165. ushort et_prot; /* 802 protocol */
  166. } Ethernet_t;
  167. #define ETHER_HDR_SIZE 14 /* Ethernet header size */
  168. #define E802_HDR_SIZE 22 /* 802 ethernet header size */
  169. /*
  170. * Ethernet header
  171. */
  172. typedef struct {
  173. uchar vet_dest[6]; /* Destination node */
  174. uchar vet_src[6]; /* Source node */
  175. ushort vet_vlan_type; /* PROT_VLAN */
  176. ushort vet_tag; /* TAG of VLAN */
  177. ushort vet_type; /* protocol type */
  178. } VLAN_Ethernet_t;
  179. #define VLAN_ETHER_HDR_SIZE 18 /* VLAN Ethernet header size */
  180. #define PROT_IP 0x0800 /* IP protocol */
  181. #define PROT_ARP 0x0806 /* IP ARP protocol */
  182. #define PROT_RARP 0x8035 /* IP ARP protocol */
  183. #define PROT_VLAN 0x8100 /* IEEE 802.1q protocol */
  184. #define IPPROTO_ICMP 1 /* Internet Control Message Protocol */
  185. #define IPPROTO_UDP 17 /* User Datagram Protocol */
  186. /*
  187. * Internet Protocol (IP) header.
  188. */
  189. typedef struct {
  190. uchar ip_hl_v; /* header length and version */
  191. uchar ip_tos; /* type of service */
  192. ushort ip_len; /* total length */
  193. ushort ip_id; /* identification */
  194. ushort ip_off; /* fragment offset field */
  195. uchar ip_ttl; /* time to live */
  196. uchar ip_p; /* protocol */
  197. ushort ip_sum; /* checksum */
  198. IPaddr_t ip_src; /* Source IP address */
  199. IPaddr_t ip_dst; /* Destination IP address */
  200. ushort udp_src; /* UDP source port */
  201. ushort udp_dst; /* UDP destination port */
  202. ushort udp_len; /* Length of UDP packet */
  203. ushort udp_xsum; /* Checksum */
  204. } IP_t;
  205. #define IP_OFFS 0x1fff /* ip offset *= 8 */
  206. #define IP_FLAGS 0xe000 /* first 3 bits */
  207. #define IP_FLAGS_RES 0x8000 /* reserved */
  208. #define IP_FLAGS_DFRAG 0x4000 /* don't fragments */
  209. #define IP_FLAGS_MFRAG 0x2000 /* more fragments */
  210. #define IP_HDR_SIZE_NO_UDP (sizeof (IP_t) - 8)
  211. #define IP_HDR_SIZE (sizeof (IP_t))
  212. /*
  213. * Address Resolution Protocol (ARP) header.
  214. */
  215. typedef struct
  216. {
  217. ushort ar_hrd; /* Format of hardware address */
  218. # define ARP_ETHER 1 /* Ethernet hardware address */
  219. ushort ar_pro; /* Format of protocol address */
  220. uchar ar_hln; /* Length of hardware address */
  221. uchar ar_pln; /* Length of protocol address */
  222. ushort ar_op; /* Operation */
  223. # define ARPOP_REQUEST 1 /* Request to resolve address */
  224. # define ARPOP_REPLY 2 /* Response to previous request */
  225. # define RARPOP_REQUEST 3 /* Request to resolve address */
  226. # define RARPOP_REPLY 4 /* Response to previous request */
  227. /*
  228. * The remaining fields are variable in size, according to
  229. * the sizes above, and are defined as appropriate for
  230. * specific hardware/protocol combinations.
  231. */
  232. uchar ar_data[0];
  233. #if 0
  234. uchar ar_sha[]; /* Sender hardware address */
  235. uchar ar_spa[]; /* Sender protocol address */
  236. uchar ar_tha[]; /* Target hardware address */
  237. uchar ar_tpa[]; /* Target protocol address */
  238. #endif /* 0 */
  239. } ARP_t;
  240. #define ARP_HDR_SIZE (8+20) /* Size assuming ethernet */
  241. /*
  242. * ICMP stuff (just enough to handle (host) redirect messages)
  243. */
  244. #define ICMP_ECHO_REPLY 0 /* Echo reply */
  245. #define ICMP_REDIRECT 5 /* Redirect (change route) */
  246. #define ICMP_ECHO_REQUEST 8 /* Echo request */
  247. /* Codes for REDIRECT. */
  248. #define ICMP_REDIR_NET 0 /* Redirect Net */
  249. #define ICMP_REDIR_HOST 1 /* Redirect Host */
  250. typedef struct icmphdr {
  251. uchar type;
  252. uchar code;
  253. ushort checksum;
  254. union {
  255. struct {
  256. ushort id;
  257. ushort sequence;
  258. } echo;
  259. ulong gateway;
  260. struct {
  261. ushort __unused;
  262. ushort mtu;
  263. } frag;
  264. } un;
  265. } ICMP_t;
  266. /*
  267. * Maximum packet size; used to allocate packet storage.
  268. * TFTP packets can be 524 bytes + IP header + ethernet header.
  269. * Lets be conservative, and go for 38 * 16. (Must also be
  270. * a multiple of 32 bytes).
  271. */
  272. /*
  273. * AS.HARNOIS : Better to set PKTSIZE to maximum size because
  274. * traffic type is not always controlled
  275. * maximum packet size = 1518
  276. * maximum packet size and multiple of 32 bytes = 1536
  277. */
  278. #define PKTSIZE 1518
  279. #define PKTSIZE_ALIGN 1536
  280. /*#define PKTSIZE 608*/
  281. /*
  282. * Maximum receive ring size; that is, the number of packets
  283. * we can buffer before overflow happens. Basically, this just
  284. * needs to be enough to prevent a packet being discarded while
  285. * we are processing the previous one.
  286. */
  287. #define RINGSZ 4
  288. #define RINGSZ_LOG2 2
  289. /**********************************************************************/
  290. /*
  291. * Globals.
  292. *
  293. * Note:
  294. *
  295. * All variables of type IPaddr_t are stored in NETWORK byte order
  296. * (big endian).
  297. */
  298. /* net.c */
  299. /** BOOTP EXTENTIONS **/
  300. extern IPaddr_t NetOurGatewayIP; /* Our gateway IP addresse */
  301. extern IPaddr_t NetOurSubnetMask; /* Our subnet mask (0 = unknown)*/
  302. extern IPaddr_t NetOurDNSIP; /* Our Domain Name Server (0 = unknown)*/
  303. #if defined(CONFIG_BOOTP_DNS2)
  304. extern IPaddr_t NetOurDNS2IP; /* Our 2nd Domain Name Server (0 = unknown)*/
  305. #endif
  306. extern char NetOurNISDomain[32]; /* Our NIS domain */
  307. extern char NetOurHostName[32]; /* Our hostname */
  308. extern char NetOurRootPath[64]; /* Our root path */
  309. extern ushort NetBootFileSize; /* Our boot file size in blocks */
  310. /** END OF BOOTP EXTENTIONS **/
  311. extern ulong NetBootFileXferSize; /* size of bootfile in bytes */
  312. extern uchar NetOurEther[6]; /* Our ethernet address */
  313. extern uchar NetServerEther[6]; /* Boot server enet address */
  314. extern IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
  315. extern IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
  316. extern volatile uchar * NetTxPacket; /* THE transmit packet */
  317. extern volatile uchar * NetRxPackets[PKTBUFSRX];/* Receive packets */
  318. extern volatile uchar * NetRxPacket; /* Current receive packet */
  319. extern int NetRxPacketLen; /* Current rx packet length */
  320. extern unsigned NetIPID; /* IP ID (counting) */
  321. extern uchar NetBcastAddr[6]; /* Ethernet boardcast address */
  322. extern uchar NetEtherNullAddr[6];
  323. #define VLAN_NONE 4095 /* untagged */
  324. #define VLAN_IDMASK 0x0fff /* mask of valid vlan id */
  325. extern ushort NetOurVLAN; /* Our VLAN */
  326. extern ushort NetOurNativeVLAN; /* Our Native VLAN */
  327. extern uchar NetCDPAddr[6]; /* Ethernet CDP address */
  328. extern ushort CDPNativeVLAN; /* CDP returned native VLAN */
  329. extern ushort CDPApplianceVLAN; /* CDP returned appliance VLAN */
  330. extern int NetState; /* Network loop state */
  331. #define NETLOOP_CONTINUE 1
  332. #define NETLOOP_RESTART 2
  333. #define NETLOOP_SUCCESS 3
  334. #define NETLOOP_FAIL 4
  335. #ifdef CONFIG_NET_MULTI
  336. extern int NetRestartWrap; /* Tried all network devices */
  337. #endif
  338. typedef enum { BOOTP, RARP, ARP, TFTP, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP,
  339. TFTPSRV } proto_t;
  340. /* from net/net.c */
  341. extern char BootFile[128]; /* Boot File name */
  342. #if defined(CONFIG_CMD_DNS)
  343. extern char *NetDNSResolve; /* The host to resolve */
  344. extern char *NetDNSenvvar; /* the env var to put the ip into */
  345. #endif
  346. #if defined(CONFIG_CMD_PING)
  347. extern IPaddr_t NetPingIP; /* the ip address to ping */
  348. #endif
  349. #if defined(CONFIG_CMD_CDP)
  350. /* when CDP completes these hold the return values */
  351. extern ushort CDPNativeVLAN;
  352. extern ushort CDPApplianceVLAN;
  353. #endif
  354. #if defined(CONFIG_CMD_SNTP)
  355. extern IPaddr_t NetNtpServerIP; /* the ip address to NTP */
  356. extern int NetTimeOffset; /* offset time from UTC */
  357. #endif
  358. /* Initialize the network adapter */
  359. extern int NetLoop(proto_t);
  360. /* Shutdown adapters and cleanup */
  361. extern void NetStop(void);
  362. /* Load failed. Start again. */
  363. extern void NetStartAgain(void);
  364. /* Get size of the ethernet header when we send */
  365. extern int NetEthHdrSize(void);
  366. /* Set ethernet header; returns the size of the header */
  367. extern int NetSetEther(volatile uchar *, uchar *, uint);
  368. /* Set IP header */
  369. extern void NetSetIP(volatile uchar *, IPaddr_t, int, int, int);
  370. /* Checksum */
  371. extern int NetCksumOk(uchar *, int); /* Return true if cksum OK */
  372. extern uint NetCksum(uchar *, int); /* Calculate the checksum */
  373. /* Set callbacks */
  374. extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */
  375. extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
  376. /* Transmit "NetTxPacket" */
  377. extern void NetSendPacket(volatile uchar *, int);
  378. /* Transmit UDP packet, performing ARP request if needed */
  379. extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len);
  380. /* Processes a received packet */
  381. extern void NetReceive(volatile uchar *, int);
  382. /*
  383. * The following functions are a bit ugly, but necessary to deal with
  384. * alignment restrictions on ARM.
  385. *
  386. * We're using inline functions, which had the smallest memory
  387. * footprint in our tests.
  388. */
  389. /* return IP *in network byteorder* */
  390. static inline IPaddr_t NetReadIP(volatile void *from)
  391. {
  392. IPaddr_t ip;
  393. memcpy((void*)&ip, (void*)from, sizeof(ip));
  394. return ip;
  395. }
  396. /* return ulong *in network byteorder* */
  397. static inline ulong NetReadLong(ulong *from)
  398. {
  399. ulong l;
  400. memcpy((void*)&l, (void*)from, sizeof(l));
  401. return l;
  402. }
  403. /* write IP *in network byteorder* */
  404. static inline void NetWriteIP(void *to, IPaddr_t ip)
  405. {
  406. memcpy(to, (void*)&ip, sizeof(ip));
  407. }
  408. /* copy IP */
  409. static inline void NetCopyIP(volatile void *to, void *from)
  410. {
  411. memcpy((void*)to, from, sizeof(IPaddr_t));
  412. }
  413. /* copy ulong */
  414. static inline void NetCopyLong(ulong *to, ulong *from)
  415. {
  416. memcpy((void*)to, (void*)from, sizeof(ulong));
  417. }
  418. /**
  419. * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
  420. * @addr: Pointer to a six-byte array containing the Ethernet address
  421. *
  422. * Return true if the address is all zeroes.
  423. */
  424. static inline int is_zero_ether_addr(const u8 *addr)
  425. {
  426. return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
  427. }
  428. /**
  429. * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
  430. * @addr: Pointer to a six-byte array containing the Ethernet address
  431. *
  432. * Return true if the address is a multicast address.
  433. * By definition the broadcast address is also a multicast address.
  434. */
  435. static inline int is_multicast_ether_addr(const u8 *addr)
  436. {
  437. return (0x01 & addr[0]);
  438. }
  439. /*
  440. * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
  441. * @addr: Pointer to a six-byte array containing the Ethernet address
  442. *
  443. * Return true if the address is the broadcast address.
  444. */
  445. static inline int is_broadcast_ether_addr(const u8 *addr)
  446. {
  447. return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
  448. }
  449. /*
  450. * is_valid_ether_addr - Determine if the given Ethernet address is valid
  451. * @addr: Pointer to a six-byte array containing the Ethernet address
  452. *
  453. * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
  454. * a multicast address, and is not FF:FF:FF:FF:FF:FF.
  455. *
  456. * Return true if the address is valid.
  457. */
  458. static inline int is_valid_ether_addr(const u8 *addr)
  459. {
  460. /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
  461. * explicitly check for it here. */
  462. return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
  463. }
  464. /* Convert an IP address to a string */
  465. extern void ip_to_string (IPaddr_t x, char *s);
  466. /* Convert a string to ip address */
  467. extern IPaddr_t string_to_ip(const char *s);
  468. /* Convert a VLAN id to a string */
  469. extern void VLAN_to_string (ushort x, char *s);
  470. /* Convert a string to a vlan id */
  471. extern ushort string_to_VLAN(const char *s);
  472. /* read a VLAN id from an environment variable */
  473. extern ushort getenv_VLAN(char *);
  474. /* copy a filename (allow for "..." notation, limit length) */
  475. extern void copy_filename (char *dst, const char *src, int size);
  476. /* get a random source port */
  477. extern unsigned int random_port(void);
  478. /**********************************************************************/
  479. #endif /* __NET_H__ */