xref: /illumos-gate/usr/src/uts/common/sys/ethernet.h (revision 6f443ebc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5605445d5Sdg199075  * Common Development and Distribution License (the "License").
6605445d5Sdg199075  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22ba3594baSGarrett D'Amore  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
23*6f443ebcSRyan Zezeski  * Copyright 2021 Oxide Computer Company
24ba3594baSGarrett D'Amore  *
252a8164dfSZhong Wang  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * ethernet.h header for common Ethernet declarations.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #ifndef	_SYS_ETHERNET_H
347c478bd9Sstevel@tonic-gate #define	_SYS_ETHERNET_H
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
377c478bd9Sstevel@tonic-gate extern "C" {
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	ETHERADDRL	(6)		/* ethernet address length in octets */
417c478bd9Sstevel@tonic-gate #define	ETHERFCSL	(4)		/* ethernet FCS length in octets */
42ff3aea39SRobert Mustacchi #define	ETHERADDRSTRL	(18)		/* char size of ETHERADDRL with null */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Ethernet address - 6 octets
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate typedef uchar_t ether_addr_t[ETHERADDRL];
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Ethernet address - 6 octets
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate struct	ether_addr {
537c478bd9Sstevel@tonic-gate 	ether_addr_t	ether_addr_octet;
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * Structure of a 10Mb/s Ethernet header.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate struct	ether_header {
607c478bd9Sstevel@tonic-gate 	struct	ether_addr	ether_dhost;
617c478bd9Sstevel@tonic-gate 	struct	ether_addr	ether_shost;
627c478bd9Sstevel@tonic-gate 	ushort_t		ether_type;
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	ETHER_CFI	0
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate struct	ether_vlan_header {
687c478bd9Sstevel@tonic-gate 	struct	ether_addr	ether_dhost;
697c478bd9Sstevel@tonic-gate 	struct	ether_addr	ether_shost;
707c478bd9Sstevel@tonic-gate 	ushort_t		ether_tpid;
717c478bd9Sstevel@tonic-gate 	ushort_t		ether_tci;
727c478bd9Sstevel@tonic-gate 	ushort_t		ether_type;
737c478bd9Sstevel@tonic-gate };
747c478bd9Sstevel@tonic-gate 
75605445d5Sdg199075 /*
76605445d5Sdg199075  * The VLAN tag.  Available for applications that cannot make use of struct
77605445d5Sdg199075  * ether_vlan_header because they assume Ethernet encapsulation.
78605445d5Sdg199075  */
79605445d5Sdg199075 struct ether_vlan_extinfo {
80605445d5Sdg199075 	ushort_t		ether_tci;
81605445d5Sdg199075 	ushort_t		ether_type;
82605445d5Sdg199075 };
83605445d5Sdg199075 
847c478bd9Sstevel@tonic-gate #define	ETHERTYPE_PUP		(0x0200)	/* PUP protocol */
857c478bd9Sstevel@tonic-gate #define	ETHERTYPE_802_MIN	(0x0600)	/* Min valid ethernet type */
867c478bd9Sstevel@tonic-gate 						/* under IEEE 802.3 rules */
877c478bd9Sstevel@tonic-gate #define	ETHERTYPE_IP		(0x0800)	/* IP protocol */
887c478bd9Sstevel@tonic-gate #define	ETHERTYPE_ARP		(0x0806)	/* Addr. resolution protocol */
897c478bd9Sstevel@tonic-gate #define	ETHERTYPE_REVARP	(0x8035)	/* Reverse ARP */
907c478bd9Sstevel@tonic-gate #define	ETHERTYPE_AT		(0x809b)	/* AppleTalk protocol */
917c478bd9Sstevel@tonic-gate #define	ETHERTYPE_AARP		(0x80f3)	/* AppleTalk ARP */
92605445d5Sdg199075 #define	ETHERTYPE_VLAN		(0x8100)	/* 802.1Q VLAN */
937c478bd9Sstevel@tonic-gate #define	ETHERTYPE_IPV6		(0x86dd)	/* IPv6 */
947c478bd9Sstevel@tonic-gate #define	ETHERTYPE_SLOW		(0x8809)	/* Slow Protocol */
957c478bd9Sstevel@tonic-gate #define	ETHERTYPE_PPPOED	(0x8863)	/* PPPoE Discovery Stage */
967c478bd9Sstevel@tonic-gate #define	ETHERTYPE_PPPOES	(0x8864)	/* PPPoE Session Stage */
97a399b765Szf162725 #define	ETHERTYPE_EAPOL		(0x888e)	/* EAPOL protocol */
98a399b765Szf162725 #define	ETHERTYPE_RSN_PREAUTH	(0x88c7)	/* RSN PRE-Authentication */
994eaa4710SRishi Srivatsavai #define	ETHERTYPE_TRILL		(0x88c8)	/* TBD. TRILL frame */
1002a8164dfSZhong Wang #define	ETHERTYPE_FCOE		(0x8906)	/* FCoE */
1017c478bd9Sstevel@tonic-gate #define	ETHERTYPE_MAX		(0xffff)	/* Max valid ethernet type */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * The ETHERTYPE_NTRAILER packet types starting at ETHERTYPE_TRAIL have
1057c478bd9Sstevel@tonic-gate  * (type-ETHERTYPE_TRAIL)*512 bytes of data followed
1067c478bd9Sstevel@tonic-gate  * by an ETHER type (as given above) and then the (variable-length) header.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate #define	ETHERTYPE_TRAIL		(0x1000)	/* Trailer packet */
1097c478bd9Sstevel@tonic-gate #define	ETHERTYPE_NTRAILER	(16)
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #define	ETHERMTU		(1500)	/* max frame w/o header or fcs */
1127c478bd9Sstevel@tonic-gate #define	ETHERMIN		(60)	/* min frame w/header w/o fcs */
1137c478bd9Sstevel@tonic-gate #define	ETHERMAX		(1514)	/* max frame w/header w/o fcs */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Compare two Ethernet addresses - assumes that the two given
1177c478bd9Sstevel@tonic-gate  * pointers can be referenced as shorts.  On architectures
1187c478bd9Sstevel@tonic-gate  * where this is not the case, use bcmp instead.  Note that like
1197c478bd9Sstevel@tonic-gate  * bcmp, we return zero if they are the SAME.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__i386) || defined(__amd64)
1237c478bd9Sstevel@tonic-gate #define	ether_cmp(a, b) (((short *)b)[2] != ((short *)a)[2] || \
1247c478bd9Sstevel@tonic-gate 	((short *)b)[1] != ((short *)a)[1] || \
1257c478bd9Sstevel@tonic-gate 	((short *)b)[0] != ((short *)a)[0])
1267c478bd9Sstevel@tonic-gate #else
1277c478bd9Sstevel@tonic-gate #define	ether_cmp(a, b) (bcmp((caddr_t)a, (caddr_t)b, 6))
1287c478bd9Sstevel@tonic-gate #endif
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * Copy Ethernet addresses from a to b - assumes that the two given
1327c478bd9Sstevel@tonic-gate  * pointers can be referenced as shorts.  On architectures
1337c478bd9Sstevel@tonic-gate  * where this is not the case, use bcopy instead.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__i386) || defined(__amd64)
1377c478bd9Sstevel@tonic-gate #define	ether_copy(a, b) { ((short *)b)[0] = ((short *)a)[0]; \
1387c478bd9Sstevel@tonic-gate 	((short *)b)[1] = ((short *)a)[1]; ((short *)b)[2] = ((short *)a)[2]; }
1397c478bd9Sstevel@tonic-gate #else
1407c478bd9Sstevel@tonic-gate #define	ether_copy(a, b) (bcopy((caddr_t)a, (caddr_t)b, 6))
1417c478bd9Sstevel@tonic-gate #endif
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
144*6f443ebcSRyan Zezeski #define	ETHER_IS_MULTICAST(addr)	(((addr)[0] & 0x01) != 0)
145*6f443ebcSRyan Zezeski 
1467c478bd9Sstevel@tonic-gate extern int localetheraddr(struct ether_addr *, struct ether_addr *);
1477c478bd9Sstevel@tonic-gate extern char *ether_sprintf(struct ether_addr *);
1487c478bd9Sstevel@tonic-gate extern int ether_aton(char *, uchar_t *);
1497c478bd9Sstevel@tonic-gate #else	/* _KERNEL */
1507c478bd9Sstevel@tonic-gate extern char *ether_ntoa(const struct ether_addr *);
151ff3aea39SRobert Mustacchi extern char *ether_ntoa_r(const struct ether_addr *, char *);
1527c478bd9Sstevel@tonic-gate extern struct ether_addr *ether_aton(const char *);
153ff3aea39SRobert Mustacchi extern struct ether_addr *ether_aton_r(const char *, struct ether_addr *);
1547c478bd9Sstevel@tonic-gate extern int ether_ntohost(char *, const struct ether_addr *);
1557c478bd9Sstevel@tonic-gate extern int ether_hostton(const char *, struct ether_addr *);
1567c478bd9Sstevel@tonic-gate extern int ether_line(const char *, struct ether_addr *, char *);
1577c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate #endif
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate #endif	/* _SYS_ETHERNET_H */
164