datapath: Combine dl_vlan and dl_vlan_pcp.
[sliver-openvswitch.git] / lib / packets.h
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef PACKETS_H
18 #define PACKETS_H 1
19
20 #include <inttypes.h>
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include "compiler.h"
26 #include "random.h"
27 #include "util.h"
28
29 struct ofpbuf;
30
31 bool dpid_from_string(const char *s, uint64_t *dpidp);
32
33 #define ETH_ADDR_LEN           6
34
35 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
36     = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
37
38 static const uint8_t eth_addr_stp[ETH_ADDR_LEN] OVS_UNUSED
39     = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
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
46 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
47 {
48     return ea[0] & 1;
49 }
50 static inline bool eth_addr_is_local(const uint8_t ea[6])
51 {
52     /* Local if it is either a locally administered address or a Nicira random
53      * address. */
54     return !!(ea[0] & 2)
55        || (ea[0] == 0x00 && ea[1] == 0x23 && ea[2] == 0x20 && !!(ea[3] & 0x80));
56 }
57 static inline bool eth_addr_is_zero(const uint8_t ea[6])
58 {
59     return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
60 }
61 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
62                                    const uint8_t b[ETH_ADDR_LEN])
63 {
64     return !memcmp(a, b, ETH_ADDR_LEN);
65 }
66 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
67 {
68     return (((uint64_t) ea[0] << 40)
69             | ((uint64_t) ea[1] << 32)
70             | ((uint64_t) ea[2] << 24)
71             | ((uint64_t) ea[3] << 16)
72             | ((uint64_t) ea[4] << 8)
73             | ea[5]);
74 }
75 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
76 {
77     ea[0] = x >> 40;
78     ea[1] = x >> 32;
79     ea[2] = x >> 24;
80     ea[3] = x >> 16;
81     ea[4] = x >> 8;
82     ea[5] = x;
83 }
84 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
85 {
86     ea[0] &= ~1;                /* Unicast. */
87     ea[0] |= 2;                 /* Private. */
88 }
89 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
90 {
91     random_bytes(ea, ETH_ADDR_LEN);
92     eth_addr_mark_random(ea);
93 }
94 static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
95 {
96     eth_addr_random(ea);
97
98     /* Set the OUI to the Nicira one. */
99     ea[0] = 0x00;
100     ea[1] = 0x23;
101     ea[2] = 0x20;
102
103     /* Set the top bit to indicate random Nicira address. */
104     ea[3] |= 0x80;
105 }
106 /* Returns true if 'ea' is a reserved multicast address, that a bridge must
107  * never forward, false otherwise. */
108 static inline bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN])
109 {
110     return (ea[0] == 0x01
111             && ea[1] == 0x80
112             && ea[2] == 0xc2
113             && ea[3] == 0x00
114             && ea[4] == 0x00
115             && (ea[5] & 0xf0) == 0x00);
116 }
117
118 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
119
120 void compose_benign_packet(struct ofpbuf *, const char *tag,
121                            uint16_t snap_type,
122                            const uint8_t eth_src[ETH_ADDR_LEN]);
123
124 /* Example:
125  *
126  * uint8_t mac[ETH_ADDR_LEN];
127  *    [...]
128  * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
129  *
130  */
131 #define ETH_ADDR_FMT                                                    \
132     "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
133 #define ETH_ADDR_ARGS(ea)                                   \
134     (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
135
136 /* Example:
137  *
138  * char *string = "1 00:11:22:33:44:55 2";
139  * uint8_t mac[ETH_ADDR_LEN];
140  * int a, b;
141  *
142  * if (sscanf(string, "%d"ETH_ADDR_SCAN_FMT"%d",
143  *     &a, ETH_ADDR_SCAN_ARGS(mac), &b) == 1 + ETH_ADDR_SCAN_COUNT + 1) {
144  *     ...
145  * }
146  */
147 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
148 #define ETH_ADDR_SCAN_ARGS(ea) \
149         &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
150 #define ETH_ADDR_SCAN_COUNT 6
151
152 #define ETH_TYPE_IP            0x0800
153 #define ETH_TYPE_ARP           0x0806
154 #define ETH_TYPE_VLAN          0x8100
155
156 #define ETH_HEADER_LEN 14
157 #define ETH_PAYLOAD_MIN 46
158 #define ETH_PAYLOAD_MAX 1500
159 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
160 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
161 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
162 struct eth_header {
163     uint8_t eth_dst[ETH_ADDR_LEN];
164     uint8_t eth_src[ETH_ADDR_LEN];
165     uint16_t eth_type;
166 } __attribute__((packed));
167 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
168
169 #define LLC_DSAP_SNAP 0xaa
170 #define LLC_SSAP_SNAP 0xaa
171 #define LLC_CNTL_SNAP 3
172
173 #define LLC_HEADER_LEN 3
174 struct llc_header {
175     uint8_t llc_dsap;
176     uint8_t llc_ssap;
177     uint8_t llc_cntl;
178 } __attribute__((packed));
179 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
180
181 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
182                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
183 #define SNAP_HEADER_LEN 5
184 struct snap_header {
185     uint8_t snap_org[3];
186     uint16_t snap_type;
187 } __attribute__((packed));
188 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
189
190 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
191 struct llc_snap_header {
192     struct llc_header llc;
193     struct snap_header snap;
194 } __attribute__((packed));
195 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
196
197 #define VLAN_VID_MASK 0x0fff
198 #define VLAN_VID_SHIFT 0
199
200 #define VLAN_PCP_MASK 0xe000
201 #define VLAN_PCP_SHIFT 13
202
203 #define VLAN_CFI 0x1000
204
205 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
206  * returns the VLAN ID in host byte order. */
207 static inline uint16_t
208 vlan_tci_to_vid(uint16_t vlan_tci)
209 {
210     return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
211 }
212
213 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
214  * returns the priority code point (PCP) in host byte order. */
215 static inline int
216 vlan_tci_to_pcp(uint16_t vlan_tci)
217 {
218     return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
219 }
220
221 #define VLAN_HEADER_LEN 4
222 struct vlan_header {
223     uint16_t vlan_tci;          /* Lowest 12 bits are VLAN ID. */
224     uint16_t vlan_next_type;
225 };
226 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
227
228 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
229 struct vlan_eth_header {
230     uint8_t veth_dst[ETH_ADDR_LEN];
231     uint8_t veth_src[ETH_ADDR_LEN];
232     uint16_t veth_type;         /* Always htons(ETH_TYPE_VLAN). */
233     uint16_t veth_tci;          /* Lowest 12 bits are VLAN ID. */
234     uint16_t veth_next_type;
235 } __attribute__((packed));
236 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
237
238 /* The "(void) (ip)[0]" below has no effect on the value, since it's the first
239  * argument of a comma expression, but it makes sure that 'ip' is a pointer.
240  * This is useful since a common mistake is to pass an integer instead of a
241  * pointer to IP_ARGS. */
242 #define IP_FMT "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
243 #define IP_ARGS(ip)                             \
244         ((void) (ip)[0], ((uint8_t *) ip)[0]),  \
245         ((uint8_t *) ip)[1],                    \
246         ((uint8_t *) ip)[2],                    \
247         ((uint8_t *) ip)[3]
248
249 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
250 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
251 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
252
253 /* TOS fields. */
254 #define IP_ECN_MASK 0x03
255 #define IP_DSCP_MASK 0xfc
256
257 #define IP_TYPE_ICMP 1
258 #define IP_TYPE_TCP 6
259 #define IP_TYPE_UDP 17
260
261 #define IP_VERSION 4
262
263 #define IP_DONT_FRAGMENT  0x4000 /* Don't fragment. */
264 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
265 #define IP_FRAG_OFF_MASK  0x1fff /* Fragment offset. */
266 #define IP_IS_FRAGMENT(ip_frag_off) \
267         ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
268
269 #define IP_HEADER_LEN 20
270 struct ip_header {
271     uint8_t ip_ihl_ver;
272     uint8_t ip_tos;
273     uint16_t ip_tot_len;
274     uint16_t ip_id;
275     uint16_t ip_frag_off;
276     uint8_t ip_ttl;
277     uint8_t ip_proto;
278     uint16_t ip_csum;
279     uint32_t ip_src;
280     uint32_t ip_dst;
281 };
282 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
283
284 #define ICMP_HEADER_LEN 4
285 struct icmp_header {
286     uint8_t icmp_type;
287     uint8_t icmp_code;
288     uint16_t icmp_csum;
289 };
290 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
291
292 #define UDP_HEADER_LEN 8
293 struct udp_header {
294     uint16_t udp_src;
295     uint16_t udp_dst;
296     uint16_t udp_len;
297     uint16_t udp_csum;
298 };
299 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
300
301 #define TCP_FIN 0x01
302 #define TCP_SYN 0x02
303 #define TCP_RST 0x04
304 #define TCP_PSH 0x08
305 #define TCP_ACK 0x10
306 #define TCP_URG 0x20
307
308 #define TCP_FLAGS(tcp_ctl) (htons(tcp_ctl) & 0x003f)
309 #define TCP_OFFSET(tcp_ctl) (htons(tcp_ctl) >> 12)
310
311 #define TCP_HEADER_LEN 20
312 struct tcp_header {
313     uint16_t tcp_src;
314     uint16_t tcp_dst;
315     uint32_t tcp_seq;
316     uint32_t tcp_ack;
317     uint16_t tcp_ctl;
318     uint16_t tcp_winsz;
319     uint16_t tcp_csum;
320     uint16_t tcp_urg;
321 };
322 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
323
324 #define ARP_HRD_ETHERNET 1
325 #define ARP_PRO_IP 0x0800
326 #define ARP_OP_REQUEST 1
327 #define ARP_OP_REPLY 2
328
329 #define ARP_ETH_HEADER_LEN 28
330 struct arp_eth_header {
331     /* Generic members. */
332     uint16_t ar_hrd;           /* Hardware type. */
333     uint16_t ar_pro;           /* Protocol type. */
334     uint8_t ar_hln;            /* Hardware address length. */
335     uint8_t ar_pln;            /* Protocol address length. */
336     uint16_t ar_op;            /* Opcode. */
337
338     /* Ethernet+IPv4 specific members. */
339     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
340     uint32_t ar_spa;           /* Sender protocol address. */
341     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
342     uint32_t ar_tpa;           /* Target protocol address. */
343 } __attribute__((packed));
344 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
345
346 #endif /* packets.h */