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