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