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