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