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