Update copyright on all non-GPL files
[sliver-openvswitch.git] / include / packets.h
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33 #ifndef PACKETS_H
34 #define PACKETS_H 1
35
36 #include <stdint.h>
37 #include "util.h"
38
39 #define ETH_ADDR_LEN           6
40
41 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
42 {
43     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
44 }
45 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
46 {
47     return ea[0] & 1;
48 }
49 static inline bool eth_addr_is_local(const uint8_t ea[6]) 
50 {
51     return ea[0] & 2;
52 }
53
54 #define ETH_TYPE_IP            0x0800
55 #define ETH_TYPE_ARP           0x0806
56 #define ETH_TYPE_VLAN          0x8100
57
58 #define ETH_HEADER_LEN 14
59 #define ETH_PAYLOAD_MIN 46
60 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
61 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + 1500)
62 struct eth_header {
63     uint8_t eth_dst[ETH_ADDR_LEN];
64     uint8_t eth_src[ETH_ADDR_LEN];
65     uint16_t eth_type;
66 };
67 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
68
69 #define LLC_DSAP_SNAP 0xaa
70 #define LLC_SSAP_SNAP 0xaa
71 #define LLC_CNTL_SNAP 3
72
73 #define LLC_HEADER_LEN 3
74 struct llc_header {
75     uint8_t llc_dsap;
76     uint8_t llc_ssap;
77     uint8_t llc_cntl;
78 };
79 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
80
81 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
82                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
83 #define SNAP_HEADER_LEN 5
84 struct snap_header {
85     uint8_t snap_org[3];
86     uint16_t snap_type;
87 } __attribute__((packed));
88 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
89
90 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
91 struct llc_snap_header {
92     struct llc_header llc;
93     struct snap_header snap;
94 };
95 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
96
97 #define VLAN_VID 0x0fff
98
99 #define VLAN_HEADER_LEN 4
100 struct vlan_header {
101     uint16_t vlan_tci;          /* Lowest 12 bits are VLAN ID. */
102     uint16_t vlan_next_type;
103 };
104 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
105
106 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
107 struct vlan_eth_header {
108     uint8_t veth_dst[ETH_ADDR_LEN];
109     uint8_t veth_src[ETH_ADDR_LEN];
110     uint16_t veth_type;         /* Always htons(ETH_TYPE_VLAN). */
111     uint16_t veth_tci;          /* Lowest 12 bits are VLAN ID. */
112     uint16_t veth_next_type;
113 };
114 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
115
116 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
117 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
118
119 #define IP_TYPE_TCP 6
120 #define IP_TYPE_UDP 17
121
122 #define IP_HEADER_LEN 20
123 struct ip_header {
124     uint8_t ip_ihl_ver;
125     uint8_t ip_tos;
126     uint16_t ip_tot_len;
127     uint16_t ip_id;
128     uint16_t ip_frag_off;
129     uint8_t ip_ttl;
130     uint8_t ip_proto;
131     uint16_t ip_csum;
132     uint32_t ip_src;
133     uint32_t ip_dst;
134 };
135 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
136
137 #define UDP_HEADER_LEN 8
138 struct udp_header {
139     uint16_t udp_src;
140     uint16_t udp_dst;
141     uint16_t udp_len;
142     uint16_t udp_csum;
143 };
144 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
145
146 #define TCP_FIN 0x01
147 #define TCP_SYN 0x02
148 #define TCP_RST 0x04
149 #define TCP_PSH 0x08
150 #define TCP_ACK 0x10
151 #define TCP_URG 0x20
152
153 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
154 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
155
156 #define TCP_HEADER_LEN 20
157 struct tcp_header {
158     uint16_t tcp_src;
159     uint16_t tcp_dst;
160     uint32_t tcp_seq;
161     uint32_t tcp_ack;
162     uint16_t tcp_ctl;
163     uint16_t tcp_winsz;
164     uint16_t tcp_csum;
165     uint16_t tcp_urg;
166 };
167 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
168
169 #define ARP_HRD_ETHERNET 1
170 #define ARP_PRO_IP 0x0800
171 #define ARP_OP_REQUEST 1
172 #define ARP_OP_REPLY 2
173
174 #define ARP_ETH_HEADER_LEN 28
175 struct arp_eth_header {
176     /* Generic members. */
177     uint16_t ar_hrd;           /* Hardware type. */
178     uint16_t ar_pro;           /* Protocol type. */
179     uint8_t ar_hln;            /* Hardware address length. */
180     uint8_t ar_pln;            /* Protocol address length. */
181     uint16_t ar_op;            /* Opcode. */
182
183     /* Ethernet+IPv4 specific members. */
184     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
185     uint32_t ar_spa;           /* Sender protocol address. */
186     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
187     uint32_t ar_tpa;           /* Target protocol address. */
188 } __attribute__((packed));
189 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
190
191 #endif /* packets.h */