1 /*	$OpenBSD: if_ether.h,v 1.35 2005/03/28 06:19:58 tedu Exp $	*/
2 /*	$NetBSD: if_ether.h,v 1.22 1996/05/11 13:00:00 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)if_ether.h	8.1 (Berkeley) 6/10/93
33  */
34 
35 #ifndef _NETINET_IF_ETHER_H_
36 #define _NETINET_IF_ETHER_H_
37 
38 /*
39  * Some basic Ethernet constants.
40  */
41 #define	ETHER_ADDR_LEN	6	/* Ethernet address length		*/
42 #define ETHER_TYPE_LEN	2	/* Ethernet type field length		*/
43 #define ETHER_CRC_LEN	4	/* Ethernet CRC length			*/
44 #define ETHER_HDR_LEN	((ETHER_ADDR_LEN * 2) + ETHER_TYPE_LEN)
45 #define ETHER_MIN_LEN	64	/* Minimum frame length, CRC included	*/
46 #define ETHER_MAX_LEN	1518	/* Maximum frame length, CRC included	*/
47 #define ETHER_MAX_DIX_LEN	1536	/* Maximum DIX frame length	*/
48 #define ETHER_MAX_LEN_JUMBO	9018	/* max jumbo frame len, including CRC */
49 
50 /*
51  * Some Ethernet extensions.
52  */
53 #define ETHER_VLAN_ENCAP_LEN	4	/* len of 802.1Q VLAN encapsulation */
54 
55 /*
56  * Mbuf adjust factor to force 32-bit alignment of IP header.
57  * Drivers should do m_adj(m, ETHER_ALIGN) when setting up a
58  * receive so the upper layers get the IP header properly aligned
59  * past the 14-byte Ethernet header.
60  */
61 #define ETHER_ALIGN	2	/* driver adjust for IP hdr alignment */
62 
63 /*
64  * Ethernet address - 6 octets
65  */
66 struct ether_addr {
67 	u_int8_t ether_addr_octet[ETHER_ADDR_LEN];
68 };
69 
70 /*
71  * The length of the combined header.
72  */
73 struct	ether_header {
74 	u_int8_t  ether_dhost[ETHER_ADDR_LEN];
75 	u_int8_t  ether_shost[ETHER_ADDR_LEN];
76 	u_int16_t ether_type;
77 };
78 
79 #include <net/ethertypes.h>
80 
81 #define	ETHER_IS_MULTICAST(addr) (*(addr) & 0x01) /* is address mcast/bcast? */
82 
83 #define	ETHERMTU	(ETHER_MAX_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
84 #define	ETHERMIN	(ETHER_MIN_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
85 #define	ETHERMTU_JUMBO	(ETHER_MAX_LEN_JUMBO - ETHER_HDR_LEN - ETHER_CRC_LEN)
86 
87 /*
88  * Ethernet CRC32 polynomials (big- and little-endian verions).
89  */
90 #define	ETHER_CRC_POLY_LE	0xedb88320
91 #define	ETHER_CRC_POLY_BE	0x04c11db6
92 
93 /*
94  * Ethernet-specific mbuf flags.
95  */
96 #define M_HASFCS	M_LINK0	/* FCS included at end of frame */
97 
98 #ifdef _KERNEL
99 /*
100  * Macro to map an IP multicast address to an Ethernet multicast address.
101  * The high-order 25 bits of the Ethernet address are statically assigned,
102  * and the low-order 23 bits are taken from the low end of the IP address.
103  */
104 #define ETHER_MAP_IP_MULTICAST(ipaddr, enaddr)				\
105 	/* struct in_addr *ipaddr; */					\
106 	/* u_int8_t enaddr[ETHER_ADDR_LEN]; */				\
107 {									\
108 	(enaddr)[0] = 0x01;						\
109 	(enaddr)[1] = 0x00;						\
110 	(enaddr)[2] = 0x5e;						\
111 	(enaddr)[3] = ((u_int8_t *)ipaddr)[1] & 0x7f;			\
112 	(enaddr)[4] = ((u_int8_t *)ipaddr)[2];				\
113 	(enaddr)[5] = ((u_int8_t *)ipaddr)[3];				\
114 }
115 
116 /*
117  * Macro to map an IPv6 multicast address to an Ethernet multicast address.
118  * The high-order 16 bits of the Ethernet address are statically assigned,
119  * and the low-order 32 bits are taken from the low end of the IPv6 address.
120  */
121 #define ETHER_MAP_IPV6_MULTICAST(ip6addr, enaddr)			\
122 	/* struct in6_addr *ip6addr; */					\
123 	/* u_int8_t enaddr[ETHER_ADDR_LEN]; */				\
124 {									\
125 	(enaddr)[0] = 0x33;						\
126 	(enaddr)[1] = 0x33;						\
127 	(enaddr)[2] = ((u_int8_t *)ip6addr)[12];			\
128 	(enaddr)[3] = ((u_int8_t *)ip6addr)[13];			\
129 	(enaddr)[4] = ((u_int8_t *)ip6addr)[14];			\
130 	(enaddr)[5] = ((u_int8_t *)ip6addr)[15];			\
131 }
132 #endif
133 
134 /*
135  * Ethernet Address Resolution Protocol.
136  *
137  * See RFC 826 for protocol description.  Structure below is adapted
138  * to resolving internet addresses.  Field names used correspond to
139  * RFC 826.
140  */
141 struct	ether_arp {
142 	struct	 arphdr ea_hdr;			/* fixed-size header */
143 	u_int8_t arp_sha[ETHER_ADDR_LEN];	/* sender hardware address */
144 	u_int8_t arp_spa[4];			/* sender protocol address */
145 	u_int8_t arp_tha[ETHER_ADDR_LEN];	/* target hardware address */
146 	u_int8_t arp_tpa[4];			/* target protocol address */
147 };
148 #define	arp_hrd	ea_hdr.ar_hrd
149 #define	arp_pro	ea_hdr.ar_pro
150 #define	arp_hln	ea_hdr.ar_hln
151 #define	arp_pln	ea_hdr.ar_pln
152 #define	arp_op	ea_hdr.ar_op
153 
154 /*
155  * Structure shared between the ethernet driver modules and
156  * the address resolution code.  For example, each ec_softc or il_softc
157  * begins with this structure.
158  */
159 struct	arpcom {
160 	struct	 ifnet ac_if;			/* network-visible interface */
161 	u_int8_t ac_enaddr[ETHER_ADDR_LEN];	/* ethernet hardware address */
162 	char	 ac__pad[2];			/* pad for some machines */
163 	LIST_HEAD(, ether_multi) ac_multiaddrs;	/* list of ether multicast addrs */
164 	int	 ac_multicnt;			/* length of ac_multiaddrs list */
165 };
166 
167 struct llinfo_arp {
168 	LIST_ENTRY(llinfo_arp) la_list;
169 	struct	rtentry *la_rt;
170 	struct	mbuf *la_hold;		/* last packet until resolved/timeout */
171 	long	la_asked;		/* last time we QUERIED for this addr */
172 #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
173 };
174 
175 struct sockaddr_inarp {
176 	u_int8_t  sin_len;
177 	u_int8_t  sin_family;
178 	u_int16_t sin_port;
179 	struct	  in_addr sin_addr;
180 	struct	  in_addr sin_srcaddr;
181 	u_int16_t sin_tos;
182 	u_int16_t sin_other;
183 #define SIN_PROXY 1
184 };
185 
186 /*
187  * IP and ethernet specific routing flags
188  */
189 #define	RTF_USETRAILERS	  RTF_PROTO1	/* use trailers */
190 #define	RTF_ANNOUNCE	  RTF_PROTO2	/* announce new arp entry */
191 #define	RTF_PERMANENT_ARP RTF_PROTO3    /* only manual overwrite of entry */
192 
193 #ifdef	_KERNEL
194 extern u_int8_t etherbroadcastaddr[ETHER_ADDR_LEN];
195 extern u_int8_t ether_ipmulticast_min[ETHER_ADDR_LEN];
196 extern u_int8_t ether_ipmulticast_max[ETHER_ADDR_LEN];
197 extern struct ifqueue arpintrq;
198 
199 void	arpwhohas(struct arpcom *, struct in_addr *);
200 void	arpintr(void);
201 int	arpresolve(struct arpcom *,
202 	    struct rtentry *, struct mbuf *, struct sockaddr *, u_char *);
203 void	arp_ifinit(struct arpcom *, struct ifaddr *);
204 void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
205 
206 int	ether_addmulti(struct ifreq *, struct arpcom *);
207 int	ether_delmulti(struct ifreq *, struct arpcom *);
208 int	ether_multiaddr(struct sockaddr *, u_int8_t[], u_int8_t[]);
209 #endif /* _KERNEL */
210 
211 /*
212  * Ethernet multicast address structure.  There is one of these for each
213  * multicast address or range of multicast addresses that we are supposed
214  * to listen to on a particular interface.  They are kept in a linked list,
215  * rooted in the interface's arpcom structure.  (This really has nothing to
216  * do with ARP, or with the Internet address family, but this appears to be
217  * the minimally-disrupting place to put it.)
218  */
219 struct ether_multi {
220 	u_int8_t enm_addrlo[ETHER_ADDR_LEN]; /* low  or only address of range */
221 	u_int8_t enm_addrhi[ETHER_ADDR_LEN]; /* high or only address of range */
222 	struct	 arpcom *enm_ac;	/* back pointer to arpcom */
223 	u_int	 enm_refcount;		/* no. claims to this addr/range */
224 	LIST_ENTRY(ether_multi) enm_list;
225 };
226 
227 /*
228  * Structure used by macros below to remember position when stepping through
229  * all of the ether_multi records.
230  */
231 struct ether_multistep {
232 	struct ether_multi  *e_enm;
233 };
234 
235 /*
236  * Macro for looking up the ether_multi record for a given range of Ethernet
237  * multicast addresses connected to a given arpcom structure.  If no matching
238  * record is found, "enm" returns NULL.
239  */
240 #define ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm)			\
241 	/* u_int8_t addrlo[ETHER_ADDR_LEN]; */				\
242 	/* u_int8_t addrhi[ETHER_ADDR_LEN]; */				\
243 	/* struct arpcom *ac; */					\
244 	/* struct ether_multi *enm; */					\
245 {									\
246 	for ((enm) = (ac)->ac_multiaddrs.lh_first;			\
247 	    (enm) != NULL &&						\
248 	    (bcmp((enm)->enm_addrlo, (addrlo), ETHER_ADDR_LEN) != 0 ||	\
249 	     bcmp((enm)->enm_addrhi, (addrhi), ETHER_ADDR_LEN) != 0);	\
250 		(enm) = (enm)->enm_list.le_next);			\
251 }
252 
253 /*
254  * Macro to step through all of the ether_multi records, one at a time.
255  * The current position is remembered in "step", which the caller must
256  * provide.  ETHER_FIRST_MULTI(), below, must be called to initialize "step"
257  * and get the first record.  Both macros return a NULL "enm" when there
258  * are no remaining records.
259  */
260 #define ETHER_NEXT_MULTI(step, enm) \
261 	/* struct ether_multistep step; */  \
262 	/* struct ether_multi *enm; */  \
263 { \
264 	if (((enm) = (step).e_enm) != NULL) \
265 		(step).e_enm = (enm)->enm_list.le_next; \
266 }
267 
268 #define ETHER_FIRST_MULTI(step, ac, enm) \
269 	/* struct ether_multistep step; */ \
270 	/* struct arpcom *ac; */ \
271 	/* struct ether_multi *enm; */ \
272 { \
273 	(step).e_enm = (ac)->ac_multiaddrs.lh_first; \
274 	ETHER_NEXT_MULTI((step), (enm)); \
275 }
276 
277 #ifdef _KERNEL
278 
279 extern struct ifnet *myip_ifp;
280 
281 void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
282 int arpresolve(struct arpcom *, struct rtentry *, struct mbuf *,
283 		    struct sockaddr *, u_char *);
284 void arpintr(void);
285 int arpioctl(u_long, caddr_t);
286 void arp_ifinit(struct arpcom *, struct ifaddr *);
287 void arprequest(struct ifnet *, u_int32_t *, u_int32_t *, u_int8_t *);
288 void revarpinput(struct mbuf *);
289 void in_revarpinput(struct mbuf *);
290 void revarprequest(struct ifnet *);
291 int revarpwhoarewe(struct ifnet *, struct in_addr *, struct in_addr *);
292 int revarpwhoami(struct in_addr *, struct ifnet *);
293 int db_show_arptab(void);
294 
295 u_int32_t ether_crc32_le(const u_int8_t *, size_t);
296 u_int32_t ether_crc32_be(const u_int8_t *, size_t);
297 
298 #else
299 
300 __BEGIN_DECLS
301 char *ether_ntoa(struct ether_addr *);
302 struct ether_addr *ether_aton(const char *);
303 int ether_ntohost(char *, struct ether_addr *);
304 int ether_hostton(const char *, struct ether_addr *);
305 int ether_line(const char *, struct ether_addr *, char *);
306 __END_DECLS
307 
308 #endif /* _KERNEL */
309 #endif /* _NETINET_IF_ETHER_H_ */
310