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