1 /* $OpenBSD: if_ether.h,v 1.82 2021/04/23 21:55:36 bluhm 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 * The maximum supported Ethernet length and some space for encapsulation. 64 */ 65 #define ETHER_MAX_HARDMTU_LEN 65435 66 67 /* 68 * Ethernet address - 6 octets 69 */ 70 struct ether_addr { 71 u_int8_t ether_addr_octet[ETHER_ADDR_LEN]; 72 }; 73 74 /* 75 * The length of the combined header. 76 */ 77 struct ether_header { 78 u_int8_t ether_dhost[ETHER_ADDR_LEN]; 79 u_int8_t ether_shost[ETHER_ADDR_LEN]; 80 u_int16_t ether_type; 81 }; 82 83 /* 84 * VLAN headers. 85 */ 86 87 struct ether_vlan_header { 88 u_char evl_dhost[ETHER_ADDR_LEN]; 89 u_char evl_shost[ETHER_ADDR_LEN]; 90 u_int16_t evl_encap_proto; 91 u_int16_t evl_tag; 92 u_int16_t evl_proto; 93 }; 94 95 #define EVL_VLID_MASK 0xFFF 96 #define EVL_VLID_NULL 0x000 97 /* 0x000 and 0xfff are reserved */ 98 #define EVL_VLID_MIN 0x001 99 #define EVL_VLID_MAX 0xFFE 100 #define EVL_VLANOFTAG(tag) ((tag) & EVL_VLID_MASK) 101 102 #define EVL_PRIO_MAX 7 103 #define EVL_PRIO_BITS 13 104 #define EVL_PRIOFTAG(tag) (((tag) >> EVL_PRIO_BITS) & 7) 105 106 #define EVL_ENCAPLEN 4 /* length in octets of encapsulation */ 107 108 #include <net/ethertypes.h> 109 110 #define ETHER_IS_MULTICAST(addr) (*(addr) & 0x01) /* is address mcast/bcast? */ 111 #define ETHER_IS_BROADCAST(addr) \ 112 (((addr)[0] & (addr)[1] & (addr)[2] & \ 113 (addr)[3] & (addr)[4] & (addr)[5]) == 0xff) 114 #define ETHER_IS_ANYADDR(addr) \ 115 (((addr)[0] | (addr)[1] | (addr)[2] | \ 116 (addr)[3] | (addr)[4] | (addr)[5]) == 0x00) 117 #define ETHER_IS_EQ(a1, a2) (memcmp((a1), (a2), ETHER_ADDR_LEN) == 0) 118 119 /* 120 * It can be faster to work with ethernet addresses as a uint64_t. 121 * Provide some constants and functionality centrally to better 122 * support this. 123 */ 124 125 #define ETH64_IS_MULTICAST(_e64) ((_e64) & 0x010000000000ULL) 126 #define ETH64_IS_BROADCAST(_e64) ((_e64) == 0xffffffffffffULL) 127 #define ETH64_IS_ANYADDR(_e64) ((_e64) == 0x000000000000ULL) 128 129 #define ETH64_8021_RSVD_PREFIX 0x0180c2000000ULL 130 #define ETH64_8021_RSVD_MASK 0xfffffffffff0ULL 131 #define ETH64_IS_8021_RSVD(_e64) \ 132 (((_e64) & ETH64_8021_RSVD_MASK) == ETH64_8021_RSVD_PREFIX) 133 134 /* 135 * Ethernet MTU constants. 136 */ 137 #define ETHERMTU (ETHER_MAX_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN) 138 #define ETHERMIN (ETHER_MIN_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN) 139 140 /* 141 * Ethernet CRC32 polynomials (big- and little-endian versions). 142 */ 143 #define ETHER_CRC_POLY_LE 0xedb88320 144 #define ETHER_CRC_POLY_BE 0x04c11db6 145 146 /* 147 * Ethernet Address Resolution Protocol. 148 * 149 * See RFC 826 for protocol description. Structure below is adapted 150 * to resolving internet addresses. Field names used correspond to 151 * RFC 826. 152 */ 153 struct ether_arp { 154 struct arphdr ea_hdr; /* fixed-size header */ 155 u_int8_t arp_sha[ETHER_ADDR_LEN]; /* sender hardware address */ 156 u_int8_t arp_spa[4]; /* sender protocol address */ 157 u_int8_t arp_tha[ETHER_ADDR_LEN]; /* target hardware address */ 158 u_int8_t arp_tpa[4]; /* target protocol address */ 159 }; 160 #define arp_hrd ea_hdr.ar_hrd 161 #define arp_pro ea_hdr.ar_pro 162 #define arp_hln ea_hdr.ar_hln 163 #define arp_pln ea_hdr.ar_pln 164 #define arp_op ea_hdr.ar_op 165 166 struct sockaddr_inarp { 167 u_int8_t sin_len; 168 u_int8_t sin_family; 169 u_int16_t sin_port; 170 struct in_addr sin_addr; 171 struct in_addr sin_srcaddr; 172 u_int16_t sin_tos; 173 u_int16_t sin_other; 174 #define SIN_PROXY 1 175 }; 176 177 /* 178 * IP and ethernet specific routing flags 179 */ 180 #define RTF_USETRAILERS RTF_PROTO1 /* use trailers */ 181 #define RTF_PERMANENT_ARP RTF_PROTO3 /* only manual overwrite of entry */ 182 183 #ifdef _KERNEL 184 /* 185 * Macro to map an IP multicast address to an Ethernet multicast address. 186 * The high-order 25 bits of the Ethernet address are statically assigned, 187 * and the low-order 23 bits are taken from the low end of the IP address. 188 */ 189 #define ETHER_MAP_IP_MULTICAST(ipaddr, enaddr) \ 190 /* struct in_addr *ipaddr; */ \ 191 /* u_int8_t enaddr[ETHER_ADDR_LEN]; */ \ 192 do { \ 193 (enaddr)[0] = 0x01; \ 194 (enaddr)[1] = 0x00; \ 195 (enaddr)[2] = 0x5e; \ 196 (enaddr)[3] = ((u_int8_t *)ipaddr)[1] & 0x7f; \ 197 (enaddr)[4] = ((u_int8_t *)ipaddr)[2]; \ 198 (enaddr)[5] = ((u_int8_t *)ipaddr)[3]; \ 199 } while (/* CONSTCOND */ 0) 200 201 /* 202 * Macro to map an IPv6 multicast address to an Ethernet multicast address. 203 * The high-order 16 bits of the Ethernet address are statically assigned, 204 * and the low-order 32 bits are taken from the low end of the IPv6 address. 205 */ 206 #define ETHER_MAP_IPV6_MULTICAST(ip6addr, enaddr) \ 207 /* struct in6_addr *ip6addr; */ \ 208 /* u_int8_t enaddr[ETHER_ADDR_LEN]; */ \ 209 do { \ 210 (enaddr)[0] = 0x33; \ 211 (enaddr)[1] = 0x33; \ 212 (enaddr)[2] = ((u_int8_t *)ip6addr)[12]; \ 213 (enaddr)[3] = ((u_int8_t *)ip6addr)[13]; \ 214 (enaddr)[4] = ((u_int8_t *)ip6addr)[14]; \ 215 (enaddr)[5] = ((u_int8_t *)ip6addr)[15]; \ 216 } while (/* CONSTCOND */ 0) 217 218 #include <net/if_var.h> /* for "struct ifnet" */ 219 220 struct ether_brport { 221 struct mbuf *(*eb_input)(struct ifnet *, struct mbuf *, 222 uint64_t, void *); 223 void *eb_port; 224 }; 225 226 /* 227 * Structure shared between the ethernet driver modules and 228 * the address resolution code. For example, each ec_softc or il_softc 229 * begins with this structure. 230 */ 231 struct arpcom { 232 struct ifnet ac_if; /* network-visible interface */ 233 u_int8_t ac_enaddr[ETHER_ADDR_LEN]; /* ethernet hardware address */ 234 char ac__pad[2]; /* pad for some machines */ 235 LIST_HEAD(, ether_multi) ac_multiaddrs; /* list of multicast addrs */ 236 int ac_multicnt; /* length of ac_multiaddrs */ 237 int ac_multirangecnt; /* number of mcast ranges */ 238 239 void *ac_trunkport; 240 const struct ether_brport *ac_brport; 241 }; 242 243 extern int arpt_keep; /* arp resolved cache expire */ 244 extern int arpt_down; /* arp down cache expire */ 245 246 extern u_int8_t etherbroadcastaddr[ETHER_ADDR_LEN]; 247 extern u_int8_t etheranyaddr[ETHER_ADDR_LEN]; 248 extern u_int8_t ether_ipmulticast_min[ETHER_ADDR_LEN]; 249 extern u_int8_t ether_ipmulticast_max[ETHER_ADDR_LEN]; 250 251 #ifdef NFSCLIENT 252 extern unsigned int revarp_ifidx; 253 #endif /* NFSCLIENT */ 254 255 void revarpinput(struct ifnet *, struct mbuf *); 256 void revarprequest(struct ifnet *); 257 int revarpwhoarewe(struct ifnet *, struct in_addr *, struct in_addr *); 258 int revarpwhoami(struct in_addr *, struct ifnet *); 259 260 void arpinit(void); 261 void arpinput(struct ifnet *, struct mbuf *); 262 void arprequest(struct ifnet *, u_int32_t *, u_int32_t *, u_int8_t *); 263 void arpwhohas(struct arpcom *, struct in_addr *); 264 int arpproxy(struct in_addr, unsigned int); 265 int arpresolve(struct ifnet *, struct rtentry *, struct mbuf *, 266 struct sockaddr *, u_char *); 267 void arp_rtrequest(struct ifnet *, int, struct rtentry *); 268 269 void ether_fakeaddr(struct ifnet *); 270 int ether_addmulti(struct ifreq *, struct arpcom *); 271 int ether_delmulti(struct ifreq *, struct arpcom *); 272 int ether_multiaddr(struct sockaddr *, u_int8_t[], u_int8_t[]); 273 void ether_ifattach(struct ifnet *); 274 void ether_ifdetach(struct ifnet *); 275 int ether_ioctl(struct ifnet *, struct arpcom *, u_long, caddr_t); 276 void ether_input(struct ifnet *, struct mbuf *); 277 int ether_resolve(struct ifnet *, struct mbuf *, struct sockaddr *, 278 struct rtentry *, struct ether_header *); 279 struct mbuf * 280 ether_encap(struct ifnet *, struct mbuf *, struct sockaddr *, 281 struct rtentry *, int *); 282 int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *, 283 struct rtentry *); 284 void ether_rtrequest(struct ifnet *, int, struct rtentry *); 285 char *ether_sprintf(u_char *); 286 287 int ether_brport_isset(struct ifnet *); 288 void ether_brport_set(struct ifnet *, const struct ether_brport *); 289 void ether_brport_clr(struct ifnet *); 290 const struct ether_brport * 291 ether_brport_get(struct ifnet *); 292 const struct ether_brport * 293 ether_brport_get_locked(struct ifnet *); 294 295 uint64_t ether_addr_to_e64(const struct ether_addr *); 296 void ether_e64_to_addr(struct ether_addr *, uint64_t); 297 298 /* 299 * Ethernet multicast address structure. There is one of these for each 300 * multicast address or range of multicast addresses that we are supposed 301 * to listen to on a particular interface. They are kept in a linked list, 302 * rooted in the interface's arpcom structure. (This really has nothing to 303 * do with ARP, or with the Internet address family, but this appears to be 304 * the minimally-disrupting place to put it.) 305 */ 306 struct ether_multi { 307 u_int8_t enm_addrlo[ETHER_ADDR_LEN]; /* low or only address of range */ 308 u_int8_t enm_addrhi[ETHER_ADDR_LEN]; /* high or only address of range */ 309 u_int enm_refcount; /* no. claims to this addr/range */ 310 LIST_ENTRY(ether_multi) enm_list; 311 }; 312 313 /* 314 * Structure used by macros below to remember position when stepping through 315 * all of the ether_multi records. 316 */ 317 struct ether_multistep { 318 struct ether_multi *e_enm; 319 }; 320 321 /* 322 * Macro for looking up the ether_multi record for a given range of Ethernet 323 * multicast addresses connected to a given arpcom structure. If no matching 324 * record is found, "enm" returns NULL. 325 */ 326 #define ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm) \ 327 /* u_int8_t addrlo[ETHER_ADDR_LEN]; */ \ 328 /* u_int8_t addrhi[ETHER_ADDR_LEN]; */ \ 329 /* struct arpcom *ac; */ \ 330 /* struct ether_multi *enm; */ \ 331 do { \ 332 for ((enm) = LIST_FIRST(&(ac)->ac_multiaddrs); \ 333 (enm) != NULL && \ 334 (memcmp((enm)->enm_addrlo, (addrlo), ETHER_ADDR_LEN) != 0 ||\ 335 memcmp((enm)->enm_addrhi, (addrhi), ETHER_ADDR_LEN) != 0); \ 336 (enm) = LIST_NEXT((enm), enm_list)); \ 337 } while (/* CONSTCOND */ 0) 338 339 /* 340 * Macro to step through all of the ether_multi records, one at a time. 341 * The current position is remembered in "step", which the caller must 342 * provide. ETHER_FIRST_MULTI(), below, must be called to initialize "step" 343 * and get the first record. Both macros return a NULL "enm" when there 344 * are no remaining records. 345 */ 346 #define ETHER_NEXT_MULTI(step, enm) \ 347 /* struct ether_multistep step; */ \ 348 /* struct ether_multi *enm; */ \ 349 do { \ 350 if (((enm) = (step).e_enm) != NULL) \ 351 (step).e_enm = LIST_NEXT((enm), enm_list); \ 352 } while (/* CONSTCOND */ 0) 353 354 #define ETHER_FIRST_MULTI(step, ac, enm) \ 355 /* struct ether_multistep step; */ \ 356 /* struct arpcom *ac; */ \ 357 /* struct ether_multi *enm; */ \ 358 do { \ 359 (step).e_enm = LIST_FIRST(&(ac)->ac_multiaddrs); \ 360 ETHER_NEXT_MULTI((step), (enm)); \ 361 } while (/* CONSTCOND */ 0) 362 363 u_int32_t ether_crc32_le_update(u_int32_t crc, const u_int8_t *, size_t); 364 u_int32_t ether_crc32_be_update(u_int32_t crc, const u_int8_t *, size_t); 365 u_int32_t ether_crc32_le(const u_int8_t *, size_t); 366 u_int32_t ether_crc32_be(const u_int8_t *, size_t); 367 368 #else /* _KERNEL */ 369 370 __BEGIN_DECLS 371 char *ether_ntoa(struct ether_addr *); 372 struct ether_addr *ether_aton(const char *); 373 int ether_ntohost(char *, struct ether_addr *); 374 int ether_hostton(const char *, struct ether_addr *); 375 int ether_line(const char *, struct ether_addr *, char *); 376 __END_DECLS 377 378 #endif /* _KERNEL */ 379 #endif /* _NETINET_IF_ETHER_H_ */ 380