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