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