xref: /freebsd/sys/net/ethernet.h (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
1 /*
2  * Fundamental constants relating to ethernet.
3  *
4  * $FreeBSD$
5  *
6  */
7 
8 #ifndef _NET_ETHERNET_H_
9 #define _NET_ETHERNET_H_
10 
11 /*
12  * The number of bytes in an ethernet (MAC) address.
13  */
14 #define	ETHER_ADDR_LEN		6
15 
16 /*
17  * The number of bytes in the type field.
18  */
19 #define	ETHER_TYPE_LEN		2
20 
21 /*
22  * The number of bytes in the trailing CRC field.
23  */
24 #define	ETHER_CRC_LEN		4
25 
26 /*
27  * The length of the combined header.
28  */
29 #define	ETHER_HDR_LEN		(ETHER_ADDR_LEN*2+ETHER_TYPE_LEN)
30 
31 /*
32  * The minimum packet length.
33  */
34 #define	ETHER_MIN_LEN		64
35 
36 /*
37  * The maximum packet length.
38  */
39 #define	ETHER_MAX_LEN		1518
40 
41 /*
42  * A macro to validate a length with
43  */
44 #define	ETHER_IS_VALID_LEN(foo)	\
45 	((foo) >= ETHER_MIN_LEN && (foo) <= ETHER_MAX_LEN)
46 
47 /*
48  * Structure of a 10Mb/s Ethernet header.
49  */
50 struct	ether_header {
51 	u_char	ether_dhost[ETHER_ADDR_LEN];
52 	u_char	ether_shost[ETHER_ADDR_LEN];
53 	u_short	ether_type;
54 };
55 
56 /*
57  * Structure of a 48-bit Ethernet address.
58  */
59 struct	ether_addr {
60 	u_char octet[ETHER_ADDR_LEN];
61 };
62 
63 #define	ETHERTYPE_PUP		0x0200	/* PUP protocol */
64 #define	ETHERTYPE_IP		0x0800	/* IP protocol */
65 #define	ETHERTYPE_ARP		0x0806	/* Addr. resolution protocol */
66 #define	ETHERTYPE_REVARP	0x8035	/* reverse Addr. resolution protocol */
67 #define	ETHERTYPE_VLAN		0x8100	/* IEEE 802.1Q VLAN tagging */
68 #define	ETHERTYPE_IPV6		0x86dd	/* IPv6 */
69 #define	ETHERTYPE_LOOPBACK	0x9000	/* used to test interfaces */
70 /* XXX - add more useful types here */
71 
72 /*
73  * The ETHERTYPE_NTRAILER packet types starting at ETHERTYPE_TRAIL have
74  * (type-ETHERTYPE_TRAIL)*512 bytes of data followed
75  * by an ETHER type (as given above) and then the (variable-length) header.
76  */
77 #define	ETHERTYPE_TRAIL		0x1000		/* Trailer packet */
78 #define	ETHERTYPE_NTRAILER	16
79 
80 #define	ETHERMTU	(ETHER_MAX_LEN-ETHER_HDR_LEN-ETHER_CRC_LEN)
81 #define	ETHERMIN	(ETHER_MIN_LEN-ETHER_HDR_LEN-ETHER_CRC_LEN)
82 
83 #ifdef _KERNEL
84 
85 /*
86  * For device drivers to specify whether they support BPF or not
87  */
88 #define ETHER_BPF_UNSUPPORTED	0
89 #define ETHER_BPF_SUPPORTED	1
90 
91 struct ifnet;
92 struct mbuf;
93 
94 extern	void (*ng_ether_input_p)(struct ifnet *ifp,
95 		struct mbuf **mp, struct ether_header *eh);
96 extern	void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
97 		struct mbuf *m, struct ether_header *eh);
98 extern	int  (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
99 extern	void (*ng_ether_attach_p)(struct ifnet *ifp);
100 extern	void (*ng_ether_detach_p)(struct ifnet *ifp);
101 
102 extern	int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
103 extern	int (*vlan_input_tag_p)(struct ether_header *eh, struct mbuf *m,
104 		u_int16_t t);
105 #define _VLAN_INPUT(ifp, eh, m) do {				\
106 	if (vlan_input_p != NULL) {				\
107 		if ((*vlan_input_p)(eh, m) == -1)		\
108 			(ifp)->if_noproto++;			\
109 	} else {						\
110 		m_free(m);					\
111 		(ifp)->if_noproto++;				\
112 	}							\
113 } while (0)
114 
115 #define VLAN_INPUT(ifp, eh, m) do {				\
116 	/* XXX: lock */						\
117 	_VLAN_INPUT(ifp, eh, m);				\
118 	/* XXX: unlock */					\
119 } while (0)
120 
121 #define	_VLAN_INPUT_TAG(ifp, eh, m, t) do {			\
122 	if (vlan_input_tag_p != NULL) { 			\
123 		if ((*vlan_input_tag_p)(eh, m, t) == -1)	\
124 			(ifp)->if_noproto++;			\
125 	} else {						\
126 		m_free(m);					\
127 		(ifp)->if_noproto++;				\
128 	}							\
129 } while (0)
130 
131 #define VLAN_INPUT_TAG(ifp, eh, m, t) do {			\
132 	/* XXX: lock */						\
133 	_VLAN_INPUT_TAG(ifp, eh, m, t);				\
134 	/* XXX: unlock */					\
135 } while (0)
136 
137 #else /* _KERNEL */
138 
139 #include <sys/cdefs.h>
140 
141 /*
142  * Ethernet address conversion/parsing routines.
143  */
144 __BEGIN_DECLS
145 struct	ether_addr *ether_aton __P((const char *));
146 int	ether_hostton __P((const char *, struct ether_addr *));
147 int	ether_line __P((const char *, struct ether_addr *, char *));
148 char 	*ether_ntoa __P((const struct ether_addr *));
149 int	ether_ntohost __P((char *, const struct ether_addr *));
150 __END_DECLS
151 
152 #endif /* !_KERNEL */
153 
154 #endif /* !_NET_ETHERNET_H_ */
155