ofproto-dpif-rid: Make lookups cheaper.
[cascardo/ovs.git] / lib / packets.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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 "geneve.h"
27 #include "openvswitch/types.h"
28 #include "random.h"
29 #include "hash.h"
30 #include "tun-metadata.h"
31 #include "util.h"
32
33 struct dp_packet;
34 struct ds;
35
36 /* Tunnel information used in flow key and metadata. */
37 struct flow_tnl {
38     ovs_be32 ip_dst;
39     ovs_be32 ip_src;
40     ovs_be64 tun_id;
41     uint16_t flags;
42     uint8_t ip_tos;
43     uint8_t ip_ttl;
44     ovs_be16 tp_src;
45     ovs_be16 tp_dst;
46     ovs_be16 gbp_id;
47     uint8_t  gbp_flags;
48     uint8_t  pad1[5];        /* Pad to 64 bits. */
49     struct tun_metadata metadata;
50 };
51
52 /* Some flags are exposed through OpenFlow while others are used only
53  * internally. */
54
55 /* Public flags */
56 #define FLOW_TNL_F_OAM (1 << 0)
57
58 #define FLOW_TNL_PUB_F_MASK ((1 << 1) - 1)
59
60 /* Private flags */
61 #define FLOW_TNL_F_DONT_FRAGMENT (1 << 1)
62 #define FLOW_TNL_F_CSUM (1 << 2)
63 #define FLOW_TNL_F_KEY (1 << 3)
64
65 #define FLOW_TNL_F_MASK ((1 << 4) - 1)
66
67 /* Purely internal to OVS userspace. These flags should never be exposed to
68  * the outside world and so aren't included in the flags mask. */
69
70 /* Tunnel information is in userspace datapath format. */
71 #define FLOW_TNL_F_UDPIF (1 << 4)
72
73 /* Returns an offset to 'src' covering all the meaningful fields in 'src'. */
74 static inline size_t
75 flow_tnl_size(const struct flow_tnl *src)
76 {
77     if (!src->ip_dst) {
78         /* Covers ip_dst only. */
79         return offsetof(struct flow_tnl, ip_src);
80     }
81     if (src->flags & FLOW_TNL_F_UDPIF) {
82         /* Datapath format, cover all options we have. */
83         return offsetof(struct flow_tnl, metadata.opts)
84             + src->metadata.present.len;
85     }
86     if (!src->metadata.present.map) {
87         /* No TLVs, opts is irrelevant. */
88         return offsetof(struct flow_tnl, metadata.opts);
89     }
90     /* Have decoded TLVs, opts is relevant. */
91     return sizeof *src;
92 }
93
94 /* Copy flow_tnl, but avoid copying unused portions of tun_metadata.  Unused
95  * data in 'dst' is NOT cleared, so this must not be used in cases where the
96  * uninitialized portion may be hashed over. */
97 static inline void
98 flow_tnl_copy__(struct flow_tnl *dst, const struct flow_tnl *src)
99 {
100     memcpy(dst, src, flow_tnl_size(src));
101 }
102
103 static inline bool
104 flow_tnl_equal(const struct flow_tnl *a, const struct flow_tnl *b)
105 {
106     size_t a_size = flow_tnl_size(a);
107
108     return a_size == flow_tnl_size(b) && !memcmp(a, b, a_size);
109 }
110
111 /* Unfortunately, a "struct flow" sometimes has to handle OpenFlow port
112  * numbers and other times datapath (dpif) port numbers.  This union allows
113  * access to both. */
114 union flow_in_port {
115     odp_port_t odp_port;
116     ofp_port_t ofp_port;
117 };
118
119 /* Datapath packet metadata */
120 struct pkt_metadata {
121     uint32_t recirc_id;         /* Recirculation id carried with the
122                                    recirculating packets. 0 for packets
123                                    received from the wire. */
124     uint32_t dp_hash;           /* hash value computed by the recirculation
125                                    action. */
126     uint32_t skb_priority;      /* Packet priority for QoS. */
127     uint32_t pkt_mark;          /* Packet mark. */
128     union flow_in_port in_port; /* Input port. */
129     struct flow_tnl tunnel;     /* Encapsulating tunnel parameters. Note that
130                                  * if 'ip_dst' == 0, the rest of the fields may
131                                  * be uninitialized. */
132 };
133
134 static inline void
135 pkt_metadata_init(struct pkt_metadata *md, odp_port_t port)
136 {
137     /* It can be expensive to zero out all of the tunnel metadata. However,
138      * we can just zero out ip_dst and the rest of the data will never be
139      * looked at. */
140     memset(md, 0, offsetof(struct pkt_metadata, tunnel));
141     md->tunnel.ip_dst = 0;
142
143     md->in_port.odp_port = port;
144 }
145
146 bool dpid_from_string(const char *s, uint64_t *dpidp);
147
148 #define ETH_ADDR_LEN           6
149
150 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
151     = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
152
153 static const uint8_t eth_addr_zero[ETH_ADDR_LEN] OVS_UNUSED
154     = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
155
156 static const uint8_t eth_addr_stp[ETH_ADDR_LEN] OVS_UNUSED
157     = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 };
158
159 static const uint8_t eth_addr_lacp[ETH_ADDR_LEN] OVS_UNUSED
160     = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 };
161
162 static const uint8_t eth_addr_bfd[ETH_ADDR_LEN] OVS_UNUSED
163     = { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 };
164
165 static inline bool eth_addr_is_broadcast(const uint8_t ea[ETH_ADDR_LEN])
166 {
167     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
168 }
169
170 static inline bool eth_addr_is_multicast(const uint8_t ea[ETH_ADDR_LEN])
171 {
172     return ea[0] & 1;
173 }
174 static inline bool eth_addr_is_local(const uint8_t ea[ETH_ADDR_LEN])
175 {
176     /* Local if it is either a locally administered address or a Nicira random
177      * address. */
178     return ea[0] & 2
179        || (ea[0] == 0x00 && ea[1] == 0x23 && ea[2] == 0x20 && ea[3] & 0x80);
180 }
181 static inline bool eth_addr_is_zero(const uint8_t ea[ETH_ADDR_LEN])
182 {
183     return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
184 }
185
186 static inline int eth_mask_is_exact(const uint8_t ea[ETH_ADDR_LEN])
187 {
188     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
189 }
190
191 static inline int eth_addr_compare_3way(const uint8_t a[ETH_ADDR_LEN],
192                                         const uint8_t b[ETH_ADDR_LEN])
193 {
194     return memcmp(a, b, ETH_ADDR_LEN);
195 }
196 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
197                                    const uint8_t b[ETH_ADDR_LEN])
198 {
199     return !eth_addr_compare_3way(a, b);
200 }
201 static inline bool eth_addr_equal_except(const uint8_t a[ETH_ADDR_LEN],
202                                     const uint8_t b[ETH_ADDR_LEN],
203                                     const uint8_t mask[ETH_ADDR_LEN])
204 {
205     return !(((a[0] ^ b[0]) & mask[0])
206              || ((a[1] ^ b[1]) & mask[1])
207              || ((a[2] ^ b[2]) & mask[2])
208              || ((a[3] ^ b[3]) & mask[3])
209              || ((a[4] ^ b[4]) & mask[4])
210              || ((a[5] ^ b[5]) & mask[5]));
211 }
212 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
213 {
214     return (((uint64_t) ea[0] << 40)
215             | ((uint64_t) ea[1] << 32)
216             | ((uint64_t) ea[2] << 24)
217             | ((uint64_t) ea[3] << 16)
218             | ((uint64_t) ea[4] << 8)
219             | ea[5]);
220 }
221 static inline uint64_t eth_addr_vlan_to_uint64(const uint8_t ea[ETH_ADDR_LEN],
222                                                uint16_t vlan)
223 {
224     return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
225 }
226 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
227 {
228     ea[0] = x >> 40;
229     ea[1] = x >> 32;
230     ea[2] = x >> 24;
231     ea[3] = x >> 16;
232     ea[4] = x >> 8;
233     ea[5] = x;
234 }
235 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
236 {
237     ea[0] &= ~1;                /* Unicast. */
238     ea[0] |= 2;                 /* Private. */
239 }
240 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
241 {
242     random_bytes(ea, ETH_ADDR_LEN);
243     eth_addr_mark_random(ea);
244 }
245 static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
246 {
247     eth_addr_random(ea);
248
249     /* Set the OUI to the Nicira one. */
250     ea[0] = 0x00;
251     ea[1] = 0x23;
252     ea[2] = 0x20;
253
254     /* Set the top bit to indicate random Nicira address. */
255     ea[3] |= 0x80;
256 }
257 static inline uint32_t hash_mac(const uint8_t ea[ETH_ADDR_LEN],
258                                 const uint16_t vlan, const uint32_t basis)
259 {
260     return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
261 }
262
263 bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN]);
264 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
265
266 void compose_rarp(struct dp_packet *, const uint8_t eth_src[ETH_ADDR_LEN]);
267
268 void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci);
269 void eth_pop_vlan(struct dp_packet *);
270
271 const char *eth_from_hex(const char *hex, struct dp_packet **packetp);
272 void eth_format_masked(const uint8_t eth[ETH_ADDR_LEN],
273                        const uint8_t mask[ETH_ADDR_LEN], struct ds *s);
274 void eth_addr_bitand(const uint8_t src[ETH_ADDR_LEN],
275                      const uint8_t mask[ETH_ADDR_LEN],
276                      uint8_t dst[ETH_ADDR_LEN]);
277
278 void set_mpls_lse(struct dp_packet *, ovs_be32 label);
279 void push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse);
280 void pop_mpls(struct dp_packet *, ovs_be16 ethtype);
281
282 void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
283 void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
284 void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
285 void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
286 ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
287                              ovs_be32 label);
288
289 /* Example:
290  *
291  * uint8_t mac[ETH_ADDR_LEN];
292  *    [...]
293  * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
294  *
295  */
296 #define ETH_ADDR_FMT                                                    \
297     "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
298 #define ETH_ADDR_ARGS(ea)                                   \
299     (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
300
301 /* Example:
302  *
303  * char *string = "1 00:11:22:33:44:55 2";
304  * uint8_t mac[ETH_ADDR_LEN];
305  * int a, b;
306  *
307  * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
308  *              &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
309  *     ...
310  * }
311  */
312 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
313 #define ETH_ADDR_SCAN_ARGS(ea) \
314         &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
315
316 #define ETH_TYPE_IP            0x0800
317 #define ETH_TYPE_ARP           0x0806
318 #define ETH_TYPE_TEB           0x6558
319 #define ETH_TYPE_VLAN_8021Q    0x8100
320 #define ETH_TYPE_VLAN          ETH_TYPE_VLAN_8021Q
321 #define ETH_TYPE_VLAN_8021AD   0x88a8
322 #define ETH_TYPE_IPV6          0x86dd
323 #define ETH_TYPE_LACP          0x8809
324 #define ETH_TYPE_RARP          0x8035
325 #define ETH_TYPE_MPLS          0x8847
326 #define ETH_TYPE_MPLS_MCAST    0x8848
327
328 static inline bool eth_type_mpls(ovs_be16 eth_type)
329 {
330     return eth_type == htons(ETH_TYPE_MPLS) ||
331         eth_type == htons(ETH_TYPE_MPLS_MCAST);
332 }
333
334 static inline bool eth_type_vlan(ovs_be16 eth_type)
335 {
336     return eth_type == htons(ETH_TYPE_VLAN_8021Q) ||
337         eth_type == htons(ETH_TYPE_VLAN_8021AD);
338 }
339
340
341 /* Minimum value for an Ethernet type.  Values below this are IEEE 802.2 frame
342  * lengths. */
343 #define ETH_TYPE_MIN           0x600
344
345 #define ETH_HEADER_LEN 14
346 #define ETH_PAYLOAD_MIN 46
347 #define ETH_PAYLOAD_MAX 1500
348 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
349 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
350 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
351 OVS_PACKED(
352 struct eth_header {
353     uint8_t eth_dst[ETH_ADDR_LEN];
354     uint8_t eth_src[ETH_ADDR_LEN];
355     ovs_be16 eth_type;
356 });
357 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
358
359 #define LLC_DSAP_SNAP 0xaa
360 #define LLC_SSAP_SNAP 0xaa
361 #define LLC_CNTL_SNAP 3
362
363 #define LLC_HEADER_LEN 3
364 OVS_PACKED(
365 struct llc_header {
366     uint8_t llc_dsap;
367     uint8_t llc_ssap;
368     uint8_t llc_cntl;
369 });
370 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
371
372 /* LLC field values used for STP frames. */
373 #define STP_LLC_SSAP 0x42
374 #define STP_LLC_DSAP 0x42
375 #define STP_LLC_CNTL 0x03
376
377 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
378                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
379 #define SNAP_HEADER_LEN 5
380 OVS_PACKED(
381 struct snap_header {
382     uint8_t snap_org[3];
383     ovs_be16 snap_type;
384 });
385 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
386
387 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
388 OVS_PACKED(
389 struct llc_snap_header {
390     struct llc_header llc;
391     struct snap_header snap;
392 });
393 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
394
395 #define VLAN_VID_MASK 0x0fff
396 #define VLAN_VID_SHIFT 0
397
398 #define VLAN_PCP_MASK 0xe000
399 #define VLAN_PCP_SHIFT 13
400
401 #define VLAN_CFI 0x1000
402 #define VLAN_CFI_SHIFT 12
403
404 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
405  * returns the VLAN ID in host byte order. */
406 static inline uint16_t
407 vlan_tci_to_vid(ovs_be16 vlan_tci)
408 {
409     return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
410 }
411
412 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
413  * returns the priority code point (PCP) in host byte order. */
414 static inline int
415 vlan_tci_to_pcp(ovs_be16 vlan_tci)
416 {
417     return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
418 }
419
420 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
421  * returns the Canonical Format Indicator (CFI). */
422 static inline int
423 vlan_tci_to_cfi(ovs_be16 vlan_tci)
424 {
425     return (vlan_tci & htons(VLAN_CFI)) != 0;
426 }
427
428 #define VLAN_HEADER_LEN 4
429 struct vlan_header {
430     ovs_be16 vlan_tci;          /* Lowest 12 bits are VLAN ID. */
431     ovs_be16 vlan_next_type;
432 };
433 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
434
435 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
436 OVS_PACKED(
437 struct vlan_eth_header {
438     uint8_t veth_dst[ETH_ADDR_LEN];
439     uint8_t veth_src[ETH_ADDR_LEN];
440     ovs_be16 veth_type;         /* Always htons(ETH_TYPE_VLAN). */
441     ovs_be16 veth_tci;          /* Lowest 12 bits are VLAN ID. */
442     ovs_be16 veth_next_type;
443 });
444 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
445
446 /* MPLS related definitions */
447 #define MPLS_TTL_MASK       0x000000ff
448 #define MPLS_TTL_SHIFT      0
449
450 #define MPLS_BOS_MASK       0x00000100
451 #define MPLS_BOS_SHIFT      8
452
453 #define MPLS_TC_MASK        0x00000e00
454 #define MPLS_TC_SHIFT       9
455
456 #define MPLS_LABEL_MASK     0xfffff000
457 #define MPLS_LABEL_SHIFT    12
458
459 #define MPLS_HLEN           4
460
461 struct mpls_hdr {
462     ovs_16aligned_be32 mpls_lse;
463 };
464 BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
465
466 /* Given a mpls label stack entry in network byte order
467  * return mpls label in host byte order */
468 static inline uint32_t
469 mpls_lse_to_label(ovs_be32 mpls_lse)
470 {
471     return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
472 }
473
474 /* Given a mpls label stack entry in network byte order
475  * return mpls tc */
476 static inline uint8_t
477 mpls_lse_to_tc(ovs_be32 mpls_lse)
478 {
479     return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
480 }
481
482 /* Given a mpls label stack entry in network byte order
483  * return mpls ttl */
484 static inline uint8_t
485 mpls_lse_to_ttl(ovs_be32 mpls_lse)
486 {
487     return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
488 }
489
490 /* Set TTL in mpls lse. */
491 static inline void
492 flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
493 {
494     *mpls_lse &= ~htonl(MPLS_TTL_MASK);
495     *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
496 }
497
498 /* Given a mpls label stack entry in network byte order
499  * return mpls BoS bit  */
500 static inline uint8_t
501 mpls_lse_to_bos(ovs_be32 mpls_lse)
502 {
503     return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
504 }
505
506 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
507 #define IP_ARGS(ip)                             \
508     ntohl(ip) >> 24,                            \
509     (ntohl(ip) >> 16) & 0xff,                   \
510     (ntohl(ip) >> 8) & 0xff,                    \
511     ntohl(ip) & 0xff
512
513 /* Example:
514  *
515  * char *string = "1 33.44.55.66 2";
516  * ovs_be32 ip;
517  * int a, b;
518  *
519  * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
520  *     ...
521  * }
522  */
523 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
524 #define IP_SCAN_ARGS(ip)                                    \
525         ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]),    \
526         &((uint8_t *) ip)[1],                               \
527         &((uint8_t *) ip)[2],                               \
528         &((uint8_t *) ip)[3]
529
530 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
531  * high-order 1-bits and 32-N low-order 0-bits. */
532 static inline bool
533 ip_is_cidr(ovs_be32 netmask)
534 {
535     uint32_t x = ~ntohl(netmask);
536     return !(x & (x + 1));
537 }
538 static inline bool
539 ip_is_multicast(ovs_be32 ip)
540 {
541     return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
542 }
543 static inline bool
544 ip_is_local_multicast(ovs_be32 ip)
545 {
546     return (ip & htonl(0xffffff00)) == htonl(0xe0000000);
547 }
548 int ip_count_cidr_bits(ovs_be32 netmask);
549 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
550
551 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
552 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
553 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
554
555 #ifndef IPPROTO_SCTP
556 #define IPPROTO_SCTP 132
557 #endif
558
559 /* TOS fields. */
560 #define IP_ECN_NOT_ECT 0x0
561 #define IP_ECN_ECT_1 0x01
562 #define IP_ECN_ECT_0 0x02
563 #define IP_ECN_CE 0x03
564 #define IP_ECN_MASK 0x03
565 #define IP_DSCP_MASK 0xfc
566
567 #define IP_VERSION 4
568
569 #define IP_DONT_FRAGMENT  0x4000 /* Don't fragment. */
570 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
571 #define IP_FRAG_OFF_MASK  0x1fff /* Fragment offset. */
572 #define IP_IS_FRAGMENT(ip_frag_off) \
573         ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
574
575 #define IP_HEADER_LEN 20
576 struct ip_header {
577     uint8_t ip_ihl_ver;
578     uint8_t ip_tos;
579     ovs_be16 ip_tot_len;
580     ovs_be16 ip_id;
581     ovs_be16 ip_frag_off;
582     uint8_t ip_ttl;
583     uint8_t ip_proto;
584     ovs_be16 ip_csum;
585     ovs_16aligned_be32 ip_src;
586     ovs_16aligned_be32 ip_dst;
587 };
588
589 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
590
591 #define ICMP_HEADER_LEN 8
592 struct icmp_header {
593     uint8_t icmp_type;
594     uint8_t icmp_code;
595     ovs_be16 icmp_csum;
596     union {
597         struct {
598             ovs_be16 id;
599             ovs_be16 seq;
600         } echo;
601         struct {
602             ovs_be16 empty;
603             ovs_be16 mtu;
604         } frag;
605         ovs_16aligned_be32 gateway;
606     } icmp_fields;
607 };
608 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
609
610 #define IGMP_HEADER_LEN 8
611 struct igmp_header {
612     uint8_t igmp_type;
613     uint8_t igmp_code;
614     ovs_be16 igmp_csum;
615     ovs_16aligned_be32 group;
616 };
617 BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header));
618
619 #define IGMPV3_HEADER_LEN 8
620 struct igmpv3_header {
621     uint8_t type;
622     uint8_t rsvr1;
623     ovs_be16 csum;
624     ovs_be16 rsvr2;
625     ovs_be16 ngrp;
626 };
627 BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header));
628
629 #define IGMPV3_RECORD_LEN 8
630 struct igmpv3_record {
631     uint8_t type;
632     uint8_t aux_len;
633     ovs_be16 nsrcs;
634     ovs_16aligned_be32 maddr;
635 };
636 BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record));
637
638 #define IGMP_HOST_MEMBERSHIP_QUERY    0x11 /* From RFC1112 */
639 #define IGMP_HOST_MEMBERSHIP_REPORT   0x12 /* Ditto */
640 #define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */
641 #define IGMP_HOST_LEAVE_MESSAGE       0x17
642 #define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */
643
644 /*
645  * IGMPv3 and MLDv2 use the same codes.
646  */
647 #define IGMPV3_MODE_IS_INCLUDE 1
648 #define IGMPV3_MODE_IS_EXCLUDE 2
649 #define IGMPV3_CHANGE_TO_INCLUDE_MODE 3
650 #define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4
651 #define IGMPV3_ALLOW_NEW_SOURCES 5
652 #define IGMPV3_BLOCK_OLD_SOURCES 6
653
654 #define SCTP_HEADER_LEN 12
655 struct sctp_header {
656     ovs_be16 sctp_src;
657     ovs_be16 sctp_dst;
658     ovs_16aligned_be32 sctp_vtag;
659     ovs_16aligned_be32 sctp_csum;
660 };
661 BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
662
663 #define UDP_HEADER_LEN 8
664 struct udp_header {
665     ovs_be16 udp_src;
666     ovs_be16 udp_dst;
667     ovs_be16 udp_len;
668     ovs_be16 udp_csum;
669 };
670 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
671
672 #define TCP_FIN 0x001
673 #define TCP_SYN 0x002
674 #define TCP_RST 0x004
675 #define TCP_PSH 0x008
676 #define TCP_ACK 0x010
677 #define TCP_URG 0x020
678 #define TCP_ECE 0x040
679 #define TCP_CWR 0x080
680 #define TCP_NS  0x100
681
682 #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
683 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
684 #define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff))
685 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
686
687 #define TCP_HEADER_LEN 20
688 struct tcp_header {
689     ovs_be16 tcp_src;
690     ovs_be16 tcp_dst;
691     ovs_16aligned_be32 tcp_seq;
692     ovs_16aligned_be32 tcp_ack;
693     ovs_be16 tcp_ctl;
694     ovs_be16 tcp_winsz;
695     ovs_be16 tcp_csum;
696     ovs_be16 tcp_urg;
697 };
698 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
699
700 #define ARP_HRD_ETHERNET 1
701 #define ARP_PRO_IP 0x0800
702 #define ARP_OP_REQUEST 1
703 #define ARP_OP_REPLY 2
704 #define ARP_OP_RARP 3
705
706 #define ARP_ETH_HEADER_LEN 28
707 struct arp_eth_header {
708     /* Generic members. */
709     ovs_be16 ar_hrd;           /* Hardware type. */
710     ovs_be16 ar_pro;           /* Protocol type. */
711     uint8_t ar_hln;            /* Hardware address length. */
712     uint8_t ar_pln;            /* Protocol address length. */
713     ovs_be16 ar_op;            /* Opcode. */
714
715     /* Ethernet+IPv4 specific members. */
716     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
717     ovs_16aligned_be32 ar_spa;           /* Sender protocol address. */
718     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
719     ovs_16aligned_be32 ar_tpa;           /* Target protocol address. */
720 };
721 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
722
723 /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
724  * most implementations, this one only requires 16-bit alignment. */
725 union ovs_16aligned_in6_addr {
726     ovs_be16 be16[8];
727     ovs_16aligned_be32 be32[4];
728 };
729
730 /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
731  * one only requires 16-bit alignment. */
732 struct ovs_16aligned_ip6_hdr {
733     union {
734         struct ovs_16aligned_ip6_hdrctl {
735             ovs_16aligned_be32 ip6_un1_flow;
736             ovs_be16 ip6_un1_plen;
737             uint8_t ip6_un1_nxt;
738             uint8_t ip6_un1_hlim;
739         } ip6_un1;
740         uint8_t ip6_un2_vfc;
741     } ip6_ctlun;
742     union ovs_16aligned_in6_addr ip6_src;
743     union ovs_16aligned_in6_addr ip6_dst;
744 };
745
746 /* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
747  * this one only requires 16-bit alignment. */
748 struct ovs_16aligned_ip6_frag {
749     uint8_t ip6f_nxt;
750     uint8_t ip6f_reserved;
751     ovs_be16 ip6f_offlg;
752     ovs_16aligned_be32 ip6f_ident;
753 };
754
755 #define ICMP6_HEADER_LEN 4
756 struct icmp6_header {
757     uint8_t icmp6_type;
758     uint8_t icmp6_code;
759     ovs_be16 icmp6_cksum;
760 };
761 BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header));
762
763 /* Neighbor Discovery option field.
764  * ND options are always a multiple of 8 bytes in size. */
765 #define ND_OPT_LEN 8
766 struct ovs_nd_opt {
767     uint8_t  nd_opt_type;      /* Values defined in icmp6.h */
768     uint8_t  nd_opt_len;       /* in units of 8 octets (the size of this struct) */
769     uint8_t  nd_opt_data[6];   /* Ethernet address in the case of SLL or TLL options */
770 };
771 BUILD_ASSERT_DECL(ND_OPT_LEN == sizeof(struct ovs_nd_opt));
772
773 /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit
774  * alignment, this one only requires 16-bit alignment. */
775 #define ND_MSG_LEN 24
776 struct ovs_nd_msg {
777     struct icmp6_header icmph;
778     ovs_16aligned_be32 rco_flags;
779     union ovs_16aligned_in6_addr target;
780     struct ovs_nd_opt options[0];
781 };
782 BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg));
783
784 /*
785  * Use the same struct for MLD and MLD2, naming members as the defined fields in
786  * in the corresponding version of the protocol, though they are reserved in the
787  * other one.
788  */
789 #define MLD_HEADER_LEN 8
790 struct mld_header {
791     uint8_t type;
792     uint8_t code;
793     ovs_be16 csum;
794     ovs_be16 mrd;
795     ovs_be16 ngrp;
796 };
797 BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header));
798
799 #define MLD2_RECORD_LEN 20
800 struct mld2_record {
801     uint8_t type;
802     uint8_t aux_len;
803     ovs_be16 nsrcs;
804     union ovs_16aligned_in6_addr maddr;
805 };
806 BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record));
807
808 #define MLD_QUERY 130
809 #define MLD_REPORT 131
810 #define MLD_DONE 132
811 #define MLD2_REPORT 143
812
813 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
814 #define IPV6_LABEL_MASK 0x000fffff
815
816 /* Example:
817  *
818  * char *string = "1 ::1 2";
819  * char ipv6_s[IPV6_SCAN_LEN + 1];
820  * struct in6_addr ipv6;
821  *
822  * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
823  *     && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
824  *     ...
825  * }
826  */
827 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
828 #define IPV6_SCAN_LEN 46
829
830 extern const struct in6_addr in6addr_exact;
831 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
832                                  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
833
834 extern const struct in6_addr in6addr_all_hosts;
835 #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
836                                      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } }
837
838 static inline bool ipv6_addr_equals(const struct in6_addr *a,
839                                     const struct in6_addr *b)
840 {
841 #ifdef IN6_ARE_ADDR_EQUAL
842     return IN6_ARE_ADDR_EQUAL(a, b);
843 #else
844     return !memcmp(a, b, sizeof(*a));
845 #endif
846 }
847
848 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
849     return ipv6_addr_equals(mask, &in6addr_any);
850 }
851
852 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
853     return ipv6_addr_equals(mask, &in6addr_exact);
854 }
855
856 static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) {
857     return ipv6_addr_equals(addr, &in6addr_all_hosts);
858 }
859
860 static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
861 {
862     return dl_type == htons(ETH_TYPE_IP)
863         || dl_type == htons(ETH_TYPE_IPV6);
864 }
865
866 /* Tunnel header */
867
868 /* GRE protocol header */
869 struct gre_base_hdr {
870     ovs_be16 flags;
871     ovs_be16 protocol;
872 };
873
874 #define GRE_CSUM        0x8000
875 #define GRE_ROUTING     0x4000
876 #define GRE_KEY         0x2000
877 #define GRE_SEQ         0x1000
878 #define GRE_STRICT      0x0800
879 #define GRE_REC         0x0700
880 #define GRE_FLAGS       0x00F8
881 #define GRE_VERSION     0x0007
882
883 /* VXLAN protocol header */
884 struct vxlanhdr {
885     ovs_16aligned_be32 vx_flags;
886     ovs_16aligned_be32 vx_vni;
887 };
888
889 #define VXLAN_FLAGS 0x08000000  /* struct vxlanhdr.vx_flags required value. */
890
891 void format_ipv6_addr(char *addr_str, const struct in6_addr *addr);
892 void print_ipv6_addr(struct ds *string, const struct in6_addr *addr);
893 void print_ipv6_mapped(struct ds *string, const struct in6_addr *addr);
894 void print_ipv6_masked(struct ds *string, const struct in6_addr *addr,
895                        const struct in6_addr *mask);
896 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
897                                  const struct in6_addr *mask);
898 struct in6_addr ipv6_create_mask(int mask);
899 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
900 bool ipv6_is_cidr(const struct in6_addr *netmask);
901
902 void *eth_compose(struct dp_packet *, const uint8_t eth_dst[ETH_ADDR_LEN],
903                   const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
904                   size_t size);
905 void *snap_compose(struct dp_packet *, const uint8_t eth_dst[ETH_ADDR_LEN],
906                    const uint8_t eth_src[ETH_ADDR_LEN],
907                    unsigned int oui, uint16_t snap_type, size_t size);
908 void packet_set_ipv4(struct dp_packet *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
909                      uint8_t ttl);
910 void packet_set_ipv6(struct dp_packet *, uint8_t proto, const ovs_be32 src[4],
911                      const ovs_be32 dst[4], uint8_t tc,
912                      ovs_be32 fl, uint8_t hlmit);
913 void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
914 void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
915 void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
916 void packet_set_nd(struct dp_packet *, const ovs_be32 target[4],
917                    const uint8_t sll[6], const uint8_t tll[6]);
918
919 void packet_format_tcp_flags(struct ds *, uint16_t);
920 const char *packet_tcp_flag_to_string(uint32_t flag);
921 void compose_arp(struct dp_packet *, uint16_t arp_op,
922                  const uint8_t arp_sha[ETH_ADDR_LEN],
923                  const uint8_t arp_tha[ETH_ADDR_LEN], bool broadcast,
924                  ovs_be32 arp_spa, ovs_be32 arp_tpa);
925 uint32_t packet_csum_pseudoheader(const struct ip_header *);
926
927 #endif /* packets.h */