xref: /openbsd-src/sys/netinet/if_ether.h (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: if_ether.h,v 1.44 2008/11/08 12:54:58 dlg 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 
49 /*
50  * Some Ethernet extensions.
51  */
52 #define ETHER_VLAN_ENCAP_LEN	4	/* len of 802.1Q VLAN encapsulation */
53 
54 /*
55  * Mbuf adjust factor to force 32-bit alignment of IP header.
56  * Drivers should do m_adj(m, ETHER_ALIGN) when setting up a
57  * receive so the upper layers get the IP header properly aligned
58  * past the 14-byte Ethernet header.
59  */
60 #define ETHER_ALIGN	2	/* driver adjust for IP hdr alignment */
61 
62 /*
63  * Ethernet address - 6 octets
64  */
65 struct ether_addr {
66 	u_int8_t ether_addr_octet[ETHER_ADDR_LEN];
67 };
68 
69 /*
70  * The length of the combined header.
71  */
72 struct	ether_header {
73 	u_int8_t  ether_dhost[ETHER_ADDR_LEN];
74 	u_int8_t  ether_shost[ETHER_ADDR_LEN];
75 	u_int16_t ether_type;
76 };
77 
78 #include <net/ethertypes.h>
79 
80 #define	ETHER_IS_MULTICAST(addr) (*(addr) & 0x01) /* is address mcast/bcast? */
81 
82 #define	ETHERMTU	(ETHER_MAX_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
83 #define	ETHERMIN	(ETHER_MIN_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
84 
85 /*
86  * Ethernet CRC32 polynomials (big- and little-endian verions).
87  */
88 #define	ETHER_CRC_POLY_LE	0xedb88320
89 #define	ETHER_CRC_POLY_BE	0x04c11db6
90 
91 #ifdef _KERNEL
92 /*
93  * Macro to map an IP multicast address to an Ethernet multicast address.
94  * The high-order 25 bits of the Ethernet address are statically assigned,
95  * and the low-order 23 bits are taken from the low end of the IP address.
96  */
97 #define ETHER_MAP_IP_MULTICAST(ipaddr, enaddr)				\
98 	/* struct in_addr *ipaddr; */					\
99 	/* u_int8_t enaddr[ETHER_ADDR_LEN]; */				\
100 do {									\
101 	(enaddr)[0] = 0x01;						\
102 	(enaddr)[1] = 0x00;						\
103 	(enaddr)[2] = 0x5e;						\
104 	(enaddr)[3] = ((u_int8_t *)ipaddr)[1] & 0x7f;			\
105 	(enaddr)[4] = ((u_int8_t *)ipaddr)[2];				\
106 	(enaddr)[5] = ((u_int8_t *)ipaddr)[3];				\
107 } while (/* CONSTCOND */ 0)
108 
109 /*
110  * Macro to map an IPv6 multicast address to an Ethernet multicast address.
111  * The high-order 16 bits of the Ethernet address are statically assigned,
112  * and the low-order 32 bits are taken from the low end of the IPv6 address.
113  */
114 #define ETHER_MAP_IPV6_MULTICAST(ip6addr, enaddr)			\
115 	/* struct in6_addr *ip6addr; */					\
116 	/* u_int8_t enaddr[ETHER_ADDR_LEN]; */				\
117 do {									\
118 	(enaddr)[0] = 0x33;						\
119 	(enaddr)[1] = 0x33;						\
120 	(enaddr)[2] = ((u_int8_t *)ip6addr)[12];			\
121 	(enaddr)[3] = ((u_int8_t *)ip6addr)[13];			\
122 	(enaddr)[4] = ((u_int8_t *)ip6addr)[14];			\
123 	(enaddr)[5] = ((u_int8_t *)ip6addr)[15];			\
124 } while (/* CONSTCOND */ 0)
125 #endif
126 
127 /*
128  * Ethernet Address Resolution Protocol.
129  *
130  * See RFC 826 for protocol description.  Structure below is adapted
131  * to resolving internet addresses.  Field names used correspond to
132  * RFC 826.
133  */
134 struct	ether_arp {
135 	struct	 arphdr ea_hdr;			/* fixed-size header */
136 	u_int8_t arp_sha[ETHER_ADDR_LEN];	/* sender hardware address */
137 	u_int8_t arp_spa[4];			/* sender protocol address */
138 	u_int8_t arp_tha[ETHER_ADDR_LEN];	/* target hardware address */
139 	u_int8_t arp_tpa[4];			/* target protocol address */
140 };
141 #define	arp_hrd	ea_hdr.ar_hrd
142 #define	arp_pro	ea_hdr.ar_pro
143 #define	arp_hln	ea_hdr.ar_hln
144 #define	arp_pln	ea_hdr.ar_pln
145 #define	arp_op	ea_hdr.ar_op
146 
147 /*
148  * Structure shared between the ethernet driver modules and
149  * the address resolution code.  For example, each ec_softc or il_softc
150  * begins with this structure.
151  */
152 struct	arpcom {
153 	struct	 ifnet ac_if;			/* network-visible interface */
154 	u_int8_t ac_enaddr[ETHER_ADDR_LEN];	/* ethernet hardware address */
155 	char	 ac__pad[2];			/* pad for some machines */
156 	LIST_HEAD(, ether_multi) ac_multiaddrs;	/* list of multicast addrs */
157 	int	 ac_multicnt;			/* length of ac_multiaddrs */
158 	int	 ac_multirangecnt;		/* number of mcast ranges */
159 
160 };
161 
162 struct llinfo_arp {
163 	LIST_ENTRY(llinfo_arp) la_list;
164 	struct	rtentry *la_rt;
165 	struct	mbuf *la_hold_head;	/* packet hold queue */
166 	struct	mbuf *la_hold_tail;
167 	int	la_hold_count;		/* number of packets queued */
168 	long	la_asked;		/* last time we QUERIED for this addr */
169 #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
170 };
171 #define MAX_HOLD_QUEUE 10
172 #define MAX_HOLD_TOTAL 100
173 
174 struct sockaddr_inarp {
175 	u_int8_t  sin_len;
176 	u_int8_t  sin_family;
177 	u_int16_t sin_port;
178 	struct	  in_addr sin_addr;
179 	struct	  in_addr sin_srcaddr;
180 	u_int16_t sin_tos;
181 	u_int16_t sin_other;
182 #define SIN_PROXY 1
183 };
184 
185 /*
186  * IP and ethernet specific routing flags
187  */
188 #define	RTF_USETRAILERS	  RTF_PROTO1	/* use trailers */
189 #define	RTF_ANNOUNCE	  RTF_PROTO2	/* announce new arp entry */
190 #define	RTF_PERMANENT_ARP RTF_PROTO3    /* only manual overwrite of entry */
191 
192 #ifdef	_KERNEL
193 extern u_int8_t etherbroadcastaddr[ETHER_ADDR_LEN];
194 extern u_int8_t ether_ipmulticast_min[ETHER_ADDR_LEN];
195 extern u_int8_t ether_ipmulticast_max[ETHER_ADDR_LEN];
196 extern struct ifqueue arpintrq;
197 
198 void	arpwhohas(struct arpcom *, struct in_addr *);
199 void	arpintr(void);
200 int	arpresolve(struct arpcom *,
201 	    struct rtentry *, struct mbuf *, struct sockaddr *, u_char *);
202 void	arp_ifinit(struct arpcom *, struct ifaddr *);
203 void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
204 
205 int	ether_addmulti(struct ifreq *, struct arpcom *);
206 int	ether_delmulti(struct ifreq *, struct arpcom *);
207 int	ether_multiaddr(struct sockaddr *, u_int8_t[], u_int8_t[]);
208 #endif /* _KERNEL */
209 
210 /*
211  * Ethernet multicast address structure.  There is one of these for each
212  * multicast address or range of multicast addresses that we are supposed
213  * to listen to on a particular interface.  They are kept in a linked list,
214  * rooted in the interface's arpcom structure.  (This really has nothing to
215  * do with ARP, or with the Internet address family, but this appears to be
216  * the minimally-disrupting place to put it.)
217  */
218 struct ether_multi {
219 	u_int8_t enm_addrlo[ETHER_ADDR_LEN]; /* low  or only address of range */
220 	u_int8_t enm_addrhi[ETHER_ADDR_LEN]; /* high or only address of range */
221 	struct	 arpcom *enm_ac;	/* back pointer to arpcom */
222 	u_int	 enm_refcount;		/* no. claims to this addr/range */
223 	LIST_ENTRY(ether_multi) enm_list;
224 };
225 
226 /*
227  * Structure used by macros below to remember position when stepping through
228  * all of the ether_multi records.
229  */
230 struct ether_multistep {
231 	struct ether_multi  *e_enm;
232 };
233 
234 /*
235  * Macro for looking up the ether_multi record for a given range of Ethernet
236  * multicast addresses connected to a given arpcom structure.  If no matching
237  * record is found, "enm" returns NULL.
238  */
239 #define ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm)			\
240 	/* u_int8_t addrlo[ETHER_ADDR_LEN]; */				\
241 	/* u_int8_t addrhi[ETHER_ADDR_LEN]; */				\
242 	/* struct arpcom *ac; */					\
243 	/* struct ether_multi *enm; */					\
244 do {									\
245 	for ((enm) = LIST_FIRST(&(ac)->ac_multiaddrs);			\
246 	    (enm) != LIST_END(&(ac)->ac_multiaddrs) &&			\
247 	    (bcmp((enm)->enm_addrlo, (addrlo), ETHER_ADDR_LEN) != 0 ||	\
248 	     bcmp((enm)->enm_addrhi, (addrhi), ETHER_ADDR_LEN) != 0);	\
249 		(enm) = LIST_NEXT((enm), enm_list));			\
250 } while (/* CONSTCOND */ 0)
251 
252 /*
253  * Macro to step through all of the ether_multi records, one at a time.
254  * The current position is remembered in "step", which the caller must
255  * provide.  ETHER_FIRST_MULTI(), below, must be called to initialize "step"
256  * and get the first record.  Both macros return a NULL "enm" when there
257  * are no remaining records.
258  */
259 #define ETHER_NEXT_MULTI(step, enm)					\
260 	/* struct ether_multistep step; */				\
261 	/* struct ether_multi *enm; */					\
262 do {									\
263 	if (((enm) = (step).e_enm) != NULL)				\
264 		(step).e_enm = LIST_NEXT((enm), enm_list);		\
265 } while (/* CONSTCOND */ 0)
266 
267 #define ETHER_FIRST_MULTI(step, ac, enm)				\
268 	/* struct ether_multistep step; */				\
269 	/* struct arpcom *ac; */					\
270 	/* struct ether_multi *enm; */					\
271 do {									\
272 	(step).e_enm = LIST_FIRST(&(ac)->ac_multiaddrs);		\
273 	ETHER_NEXT_MULTI((step), (enm));				\
274 } while (/* CONSTCOND */ 0)
275 
276 #ifdef _KERNEL
277 
278 extern struct ifnet *myip_ifp;
279 
280 int arpioctl(u_long, caddr_t);
281 void arprequest(struct ifnet *, u_int32_t *, u_int32_t *, u_int8_t *);
282 void revarpinput(struct mbuf *);
283 void in_revarpinput(struct mbuf *);
284 void revarprequest(struct ifnet *);
285 int revarpwhoarewe(struct ifnet *, struct in_addr *, struct in_addr *);
286 int revarpwhoami(struct in_addr *, struct ifnet *);
287 int db_show_arptab(void);
288 
289 u_int32_t ether_crc32_le_update(u_int32_t crc, const u_int8_t *, size_t);
290 u_int32_t ether_crc32_be_update(u_int32_t crc, const u_int8_t *, size_t);
291 u_int32_t ether_crc32_le(const u_int8_t *, size_t);
292 u_int32_t ether_crc32_be(const u_int8_t *, size_t);
293 
294 #else
295 
296 __BEGIN_DECLS
297 char *ether_ntoa(struct ether_addr *);
298 struct ether_addr *ether_aton(const char *);
299 int ether_ntohost(char *, struct ether_addr *);
300 int ether_hostton(const char *, struct ether_addr *);
301 int ether_line(const char *, struct ether_addr *, char *);
302 __END_DECLS
303 
304 #endif /* _KERNEL */
305 #endif /* _NETINET_IF_ETHER_H_ */
306