The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
ethernet.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: 493eab4730bfc49eed40df89083939fbe8208475 $
20  *
21  * @file protocols/ethernet/ethernet.h
22  * @brief Structures and functions for parsing ethernet headers.
23  *
24  * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
25  */
26 #include <freeradius-devel/util/inet.h>
27 #include <stdint.h>
28 #include <stddef.h>
29 
30 /*
31  * The number of bytes in an ethernet (MAC) address.
32  */
33 #define ETHER_ADDR_LEN 6
34 
35 /** Unpack the Priority Code Point from the TCI
36  *
37  */
38 #define VLAN_PCP_UNPACK(_vlan) (((*(uint8_t const *)&(_vlan)->tag_control) & 0xe0) >> 5)
39 
40 /** Unpack the Drop Eligible Indicator from the TCI
41  *
42  */
43 #define VLAN_DEI_UNPACK(_vlan) (((*(uint8_t const *)&(_vlan)->tag_control) & 0x10) >> 4)
44 
45 /** Unpack the VLAN ID from the TCI
46  *
47  */
48 #define VLAN_VID_UNPACK(_vlan) ((htons((_vlan)->tag_control) & 0x0fff))
49 
50 /** Pack the PCP (Priority Code Point) DEI (Drop Eligible Indicator) and VID (VLAN ID)
51  *
52  * Packs the PCP, DEI and VID into the TCI (Tag control information). Output will be a 16bit integer
53  * in network byte order.
54  *
55  * @param[in] _pcp Priority Code Point, a 3 bit value
56  * indicating the relative priority of the packet.
57  * @param[in] _dei Drop eligible indicator. Boolean indicating
58  * whether this packet should be dropped in case of congestion.
59  * @param[in] _vid 12 bit VLAN identifier.
60  */
61 #define VLAN_TCI_PACK(_pcp, _dei, _vid) htons((((uint16_t)(_pcp) & 0xe0) << 13) | (((uint16_t)(_dei) & 0x01) << 12) | ((_vid) & 0x0fff))
62 
63 /** A VLAN header
64  *
65  * Represents a single layer of 802.1Q or QinQ tagging.
66  */
67 typedef struct CC_HINT(__packed__) {
68  uint16_t tag_type; //!< Tag type. One of (0x8100 - CVLAN, 0x9100,
69  ///< 0x9200, 0x9300 - SVLAN).
70  uint16_t tag_control; //!< - 3 bits priority.
71  ///< - 1 bit DEI.
72  ///< - 12 bits VID.
74 
75 /** Structure of a DEC/Intel/Xerox or 802.3 Ethernet header
76  *
77  */
78 typedef struct CC_HINT(__packed__) {
83 
84 /** Src/dst link layer information
85  *
86  */
87 typedef struct {
90  uint16_t ether_type; //!< Ether type. Usually 0x0800 (IPv4) 0x086DD (IPv6).
91 
92  uint16_t cvlan_tpid; //!< CVLAN tag type. If 0, no CVLAN/SVLAN present.
93  uint8_t cvlan_pcp; //!< CVLAN priority code point 0-6.
94  uint8_t cvlan_dei; //!< CVLAN drop eligible indicator.
95  uint16_t cvlan_vid; //!< CVLAN vlan ID.
96 
97  uint16_t svlan_tpid; //!< SVLAN tag type. If 0, no SVLAN present.
98  uint8_t svlan_pcp; //!< SVLAN priority code point 0-6.
99  uint8_t svlan_dei; //!< SVLAN drop eligible indicator.
100  uint16_t svlan_vid; //!< SVLAN vlan ID.
101 
102  size_t payload_len; //!< Remaining bytes after the ethernet header has been parsed.
104 
105 /** Protocol options for ethernet
106  *
107  */
108 typedef enum {
109  PROTO_OPT_ETHERNET_SVLAN_TPID = 0, //!< Outer VLAN tag type.
110  PROTO_OPT_ETHERNET_SVLAN_PCP, //!< Outer VLAN priority code point.
111  PROTO_OPT_ETHERNET_SVLAN_DEI, //!< Outer VLAN drop eligible indicator.
112  PROTO_OPT_ETHERNET_SVLAN_VID, //!< Outer VLAN ID.
113  PROTO_OPT_ETHERNET_CVLAN_TPID, //!< Inner VLAN tag type.
114  PROTO_OPT_ETHERNET_CVLAN_PCP, //!< Inner VLAN priority code point.
115  PROTO_OPT_ETHERNET_CVLAN_DEI, //!< Inner VLAN drop eligible indicator.
116  PROTO_OPT_ETHERNET_CVLAN_VID //!< Inner VLAN ID.
uint16_t ether_type
Ether type. Usually 0x0800 (IPv4) 0x086DD (IPv6).
Definition: ethernet.h:90
fr_ethernet_t src_addr
Definition: ethernet.h:88
uint16_t svlan_tpid
SVLAN tag type. If 0, no SVLAN present.
Definition: ethernet.h:97
uint16_t cvlan_tpid
CVLAN tag type. If 0, no CVLAN/SVLAN present.
Definition: ethernet.h:92
uint16_t cvlan_vid
CVLAN vlan ID.
Definition: ethernet.h:95
fr_ethernet_t dst_addr
Definition: ethernet.h:89
uint16_t svlan_vid
SVLAN vlan ID.
Definition: ethernet.h:100
uint8_t cvlan_dei
CVLAN drop eligible indicator.
Definition: ethernet.h:94
size_t payload_len
Remaining bytes after the ethernet header has been parsed.
Definition: ethernet.h:102
uint8_t svlan_dei
SVLAN drop eligible indicator.
Definition: ethernet.h:99
uint16_t tag_control
Definition: ethernet.h:70
#define ETHER_ADDR_LEN
Definition: ethernet.h:33
fr_ethernet_options_t
Protocol options for ethernet.
Definition: ethernet.h:108
@ PROTO_OPT_ETHERNET_SVLAN_PCP
Outer VLAN priority code point.
Definition: ethernet.h:110
@ PROTO_OPT_ETHERNET_CVLAN_PCP
Inner VLAN priority code point.
Definition: ethernet.h:114
@ PROTO_OPT_ETHERNET_CVLAN_VID
Inner VLAN ID.
Definition: ethernet.h:116
@ PROTO_OPT_ETHERNET_SVLAN_VID
Outer VLAN ID.
Definition: ethernet.h:112
@ PROTO_OPT_ETHERNET_CVLAN_DEI
Inner VLAN drop eligible indicator.
Definition: ethernet.h:115
@ PROTO_OPT_ETHERNET_SVLAN_DEI
Outer VLAN drop eligible indicator.
Definition: ethernet.h:111
@ PROTO_OPT_ETHERNET_SVLAN_TPID
Outer VLAN tag type.
Definition: ethernet.h:109
@ PROTO_OPT_ETHERNET_CVLAN_TPID
Inner VLAN tag type.
Definition: ethernet.h:113
uint16_t tag_type
Tag type.
Definition: ethernet.h:68
uint16_t ether_type
Definition: ethernet.h:81
uint8_t svlan_pcp
SVLAN priority code point 0-6.
Definition: ethernet.h:98
uint8_t cvlan_pcp
CVLAN priority code point 0-6.
Definition: ethernet.h:93
Structure of a DEC/Intel/Xerox or 802.3 Ethernet header.
Definition: ethernet.h:78
Src/dst link layer information.
Definition: ethernet.h:87
A VLAN header.
Definition: ethernet.h:67
Struct to represent an ethernet address.
Definition: inet.h:45
unsigned short uint16_t
Definition: merged_model.c:31
unsigned char uint8_t
Definition: merged_model.c:30