]> git.proxmox.com Git - mirror_ovs.git/blame - lib/packets.h
dpif-netdev: Avoid reordering of packets in a batch with same megaflow
[mirror_ovs.git] / lib / packets.h
CommitLineData
064af421 1/*
b2342f7a 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421 15 */
b9e8b45a 16
064af421
BP
17#ifndef PACKETS_H
18#define PACKETS_H 1
19
ab9c78ff 20#include <inttypes.h>
26233bb4 21#include <sys/types.h>
064af421
BP
22#include <stdint.h>
23#include <string.h>
24#include "compiler.h"
b211014d 25#include "openvswitch/geneve.h"
b2bd6da6 26#include "openvswitch/packets.h"
0596e897 27#include "openvswitch/types.h"
3d2fbd70 28#include "openvswitch/nsh.h"
07659514 29#include "odp-netlink.h"
064af421 30#include "random.h"
7e36ac42 31#include "hash.h"
9558d2a5 32#include "tun-metadata.h"
754c1feb 33#include "unaligned.h"
064af421 34#include "util.h"
7dc18ae9 35#include "timeval.h"
064af421 36
cf62fa4c 37struct dp_packet;
d31f1109 38struct ds;
b9e8b45a 39
59781952
JR
40/* Purely internal to OVS userspace. These flags should never be exposed to
41 * the outside world and so aren't included in the flags mask. */
42
43/* Tunnel information is in userspace datapath format. */
44#define FLOW_TNL_F_UDPIF (1 << 4)
45
ffe4c74f
JB
46static inline bool ipv6_addr_is_set(const struct in6_addr *addr);
47
48static inline bool
49flow_tnl_dst_is_set(const struct flow_tnl *tnl)
50{
51 return tnl->ip_dst || ipv6_addr_is_set(&tnl->ipv6_dst);
52}
53
54struct in6_addr flow_tnl_dst(const struct flow_tnl *tnl);
55struct in6_addr flow_tnl_src(const struct flow_tnl *tnl);
56
59781952
JR
57/* Returns an offset to 'src' covering all the meaningful fields in 'src'. */
58static inline size_t
59flow_tnl_size(const struct flow_tnl *src)
60{
ffe4c74f
JB
61 if (!flow_tnl_dst_is_set(src)) {
62 /* Covers ip_dst and ipv6_dst only. */
59781952
JR
63 return offsetof(struct flow_tnl, ip_src);
64 }
65 if (src->flags & FLOW_TNL_F_UDPIF) {
66 /* Datapath format, cover all options we have. */
67 return offsetof(struct flow_tnl, metadata.opts)
68 + src->metadata.present.len;
69 }
70 if (!src->metadata.present.map) {
71 /* No TLVs, opts is irrelevant. */
72 return offsetof(struct flow_tnl, metadata.opts);
73 }
74 /* Have decoded TLVs, opts is relevant. */
75 return sizeof *src;
76}
77
78/* Copy flow_tnl, but avoid copying unused portions of tun_metadata. Unused
79 * data in 'dst' is NOT cleared, so this must not be used in cases where the
80 * uninitialized portion may be hashed over. */
81static inline void
82flow_tnl_copy__(struct flow_tnl *dst, const struct flow_tnl *src)
83{
84 memcpy(dst, src, flow_tnl_size(src));
85}
86
87static inline bool
88flow_tnl_equal(const struct flow_tnl *a, const struct flow_tnl *b)
89{
90 size_t a_size = flow_tnl_size(a);
91
92 return a_size == flow_tnl_size(b) && !memcmp(a, b, a_size);
93}
94
758c456d
JR
95/* Datapath packet metadata */
96struct pkt_metadata {
99fc16c0 97PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE, cacheline0,
572f732a
AZ
98 uint32_t recirc_id; /* Recirculation id carried with the
99 recirculating packets. 0 for packets
100 received from the wire. */
101 uint32_t dp_hash; /* hash value computed by the recirculation
102 action. */
758c456d
JR
103 uint32_t skb_priority; /* Packet priority for QoS. */
104 uint32_t pkt_mark; /* Packet mark. */
2a28ccc8 105 uint8_t ct_state; /* Connection state. */
daf4d3c1 106 bool ct_orig_tuple_ipv6;
07659514 107 uint16_t ct_zone; /* Connection zone. */
8e53fe8c 108 uint32_t ct_mark; /* Connection mark. */
9daf2348 109 ovs_u128 ct_label; /* Connection label. */
99fc16c0
BB
110 union flow_in_port in_port; /* Input port. */
111);
112
113PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE, cacheline1,
daf4d3c1
JR
114 union { /* Populated only for non-zero 'ct_state'. */
115 struct ovs_key_ct_tuple_ipv4 ipv4;
116 struct ovs_key_ct_tuple_ipv6 ipv6; /* Used only if */
117 } ct_orig_tuple; /* 'ct_orig_tuple_ipv6' is set */
99fc16c0
BB
118);
119
120PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE, cacheline2,
59781952
JR
121 struct flow_tnl tunnel; /* Encapsulating tunnel parameters. Note that
122 * if 'ip_dst' == 0, the rest of the fields may
123 * be uninitialized. */
99fc16c0 124);
758c456d
JR
125};
126
99fc16c0
BB
127BUILD_ASSERT_DECL(offsetof(struct pkt_metadata, cacheline0) == 0);
128BUILD_ASSERT_DECL(offsetof(struct pkt_metadata, cacheline1) ==
129 CACHE_LINE_SIZE);
130BUILD_ASSERT_DECL(offsetof(struct pkt_metadata, cacheline2) ==
131 2 * CACHE_LINE_SIZE);
132
6b241d64
PS
133static inline void
134pkt_metadata_init_tnl(struct pkt_metadata *md)
135{
136 /* Zero up through the tunnel metadata options. The length and table
137 * are before this and as long as they are empty, the options won't
138 * be looked at. */
139 memset(md, 0, offsetof(struct pkt_metadata, tunnel.metadata.opts));
140}
141
35303d71
JG
142static inline void
143pkt_metadata_init(struct pkt_metadata *md, odp_port_t port)
144{
af697f26
DDP
145 /* This is called for every packet in userspace datapath and affects
146 * performance if all the metadata is initialized. Hence, fields should
147 * only be zeroed out when necessary.
148 *
149 * Initialize only till ct_state. Once the ct_state is zeroed out rest
150 * of ct fields will not be looked at unless ct_state != 0.
151 */
152 memset(md, 0, offsetof(struct pkt_metadata, ct_orig_tuple_ipv6));
153
35303d71
JG
154 /* It can be expensive to zero out all of the tunnel metadata. However,
155 * we can just zero out ip_dst and the rest of the data will never be
156 * looked at. */
35303d71 157 md->tunnel.ip_dst = 0;
ffe4c74f 158 md->tunnel.ipv6_dst = in6addr_any;
35303d71
JG
159 md->in_port.odp_port = port;
160}
b5e7e61a 161
a90ed026 162/* This function prefetches the cachelines touched by pkt_metadata_init()
2daae85a
BB
163 * and pkt_metadata_init_tnl(). For performance reasons the two functions
164 * should be kept in sync. */
a90ed026
DDP
165static inline void
166pkt_metadata_prefetch_init(struct pkt_metadata *md)
167{
99fc16c0
BB
168 /* Prefetch cacheline0 as members till ct_state and odp_port will
169 * be initialized later in pkt_metadata_init(). */
170 OVS_PREFETCH(md->cacheline0);
171
2daae85a
BB
172 /* Prefetch cacheline1 as members of this cacheline will be zeroed out
173 * in pkt_metadata_init_tnl(). */
174 OVS_PREFETCH(md->cacheline1);
175
99fc16c0
BB
176 /* Prefetch cachline2 as ip_dst & ipv6_dst fields will be initialized. */
177 OVS_PREFETCH(md->cacheline2);
a90ed026
DDP
178}
179
76343538
BP
180bool dpid_from_string(const char *s, uint64_t *dpidp);
181
064af421
BP
182#define ETH_ADDR_LEN 6
183
74ff3298 184static const struct eth_addr eth_addr_broadcast OVS_UNUSED
134fefa4 185 = ETH_ADDR_C(ff,ff,ff,ff,ff,ff);
064af421 186
74ff3298 187static const struct eth_addr eth_addr_exact OVS_UNUSED
134fefa4 188 = ETH_ADDR_C(ff,ff,ff,ff,ff,ff);
eb0b295e 189
74ff3298 190static const struct eth_addr eth_addr_zero OVS_UNUSED
134fefa4 191 = ETH_ADDR_C(00,00,00,00,00,00);
b2342f7a 192static const struct eth_addr64 eth_addr64_zero OVS_UNUSED
134fefa4 193 = ETH_ADDR64_C(00,00,00,00,00,00,00,00);
ba186119 194
74ff3298 195static const struct eth_addr eth_addr_stp OVS_UNUSED
134fefa4 196 = ETH_ADDR_C(01,80,c2,00,00,00);
c25c91fd 197
74ff3298 198static const struct eth_addr eth_addr_lacp OVS_UNUSED
134fefa4 199 = ETH_ADDR_C(01,80,c2,00,00,02);
de8d2ef9 200
74ff3298 201static const struct eth_addr eth_addr_bfd OVS_UNUSED
134fefa4 202 = ETH_ADDR_C(00,23,20,00,00,01);
74ff3298
JR
203
204static inline bool eth_addr_is_broadcast(const struct eth_addr a)
064af421 205{
74ff3298 206 return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
064af421
BP
207}
208
74ff3298 209static inline bool eth_addr_is_multicast(const struct eth_addr a)
064af421 210{
74ff3298 211 return a.ea[0] & 1;
064af421 212}
74ff3298
JR
213
214static inline bool eth_addr_is_local(const struct eth_addr a)
064af421 215{
70150daf
JG
216 /* Local if it is either a locally administered address or a Nicira random
217 * address. */
74ff3298
JR
218 return a.ea[0] & 2
219 || (a.be16[0] == htons(0x0023)
220 && (a.be16[1] & htons(0xff80)) == htons(0x2080));
064af421 221}
74ff3298 222static inline bool eth_addr_is_zero(const struct eth_addr a)
064af421 223{
74ff3298 224 return !(a.be16[0] | a.be16[1] | a.be16[2]);
064af421 225}
b2342f7a
BP
226static inline bool eth_addr64_is_zero(const struct eth_addr64 a)
227{
228 return !(a.be16[0] | a.be16[1] | a.be16[2] | a.be16[3]);
229}
3b4d8ad3 230
74ff3298 231static inline int eth_mask_is_exact(const struct eth_addr a)
3b4d8ad3 232{
74ff3298 233 return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
3b4d8ad3
JS
234}
235
74ff3298
JR
236static inline int eth_addr_compare_3way(const struct eth_addr a,
237 const struct eth_addr b)
130f6e5f 238{
74ff3298 239 return memcmp(&a, &b, sizeof a);
130f6e5f 240}
b2342f7a
BP
241static inline int eth_addr64_compare_3way(const struct eth_addr64 a,
242 const struct eth_addr64 b)
243{
244 return memcmp(&a, &b, sizeof a);
245}
74ff3298
JR
246
247static inline bool eth_addr_equals(const struct eth_addr a,
248 const struct eth_addr b)
064af421 249{
130f6e5f 250 return !eth_addr_compare_3way(a, b);
064af421 251}
b2342f7a
BP
252static inline bool eth_addr64_equals(const struct eth_addr64 a,
253 const struct eth_addr64 b)
254{
255 return !eth_addr64_compare_3way(a, b);
256}
74ff3298
JR
257
258static inline bool eth_addr_equal_except(const struct eth_addr a,
259 const struct eth_addr b,
260 const struct eth_addr mask)
3b4d8ad3 261{
74ff3298
JR
262 return !(((a.be16[0] ^ b.be16[0]) & mask.be16[0])
263 || ((a.be16[1] ^ b.be16[1]) & mask.be16[1])
264 || ((a.be16[2] ^ b.be16[2]) & mask.be16[2]));
3b4d8ad3 265}
74ff3298
JR
266
267static inline uint64_t eth_addr_to_uint64(const struct eth_addr ea)
064af421 268{
74ff3298
JR
269 return (((uint64_t) ntohs(ea.be16[0]) << 32)
270 | ((uint64_t) ntohs(ea.be16[1]) << 16)
271 | ntohs(ea.be16[2]));
064af421 272}
74ff3298
JR
273
274static inline uint64_t eth_addr_vlan_to_uint64(const struct eth_addr ea,
7e36ac42
AZ
275 uint16_t vlan)
276{
277 return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
278}
74ff3298
JR
279
280static inline void eth_addr_from_uint64(uint64_t x, struct eth_addr *ea)
281{
282 ea->be16[0] = htons(x >> 32);
37ba4764
AC
283 ea->be16[1] = htons((x & 0xFFFF0000) >> 16);
284 ea->be16[2] = htons(x & 0xFFFF);
74ff3298
JR
285}
286
287static inline struct eth_addr eth_addr_invert(const struct eth_addr src)
064af421 288{
74ff3298
JR
289 struct eth_addr dst;
290
291 for (int i = 0; i < ARRAY_SIZE(src.be16); i++) {
292 dst.be16[i] = ~src.be16[i];
293 }
294
295 return dst;
064af421 296}
74ff3298
JR
297
298static inline void eth_addr_mark_random(struct eth_addr *ea)
064af421 299{
74ff3298
JR
300 ea->ea[0] &= ~1; /* Unicast. */
301 ea->ea[0] |= 2; /* Private. */
064af421 302}
74ff3298
JR
303
304static inline void eth_addr_random(struct eth_addr *ea)
064af421 305{
74ff3298 306 random_bytes((uint8_t *)ea, sizeof *ea);
064af421
BP
307 eth_addr_mark_random(ea);
308}
74ff3298
JR
309
310static inline void eth_addr_nicira_random(struct eth_addr *ea)
70150daf
JG
311{
312 eth_addr_random(ea);
313
314 /* Set the OUI to the Nicira one. */
74ff3298
JR
315 ea->ea[0] = 0x00;
316 ea->ea[1] = 0x23;
317 ea->ea[2] = 0x20;
70150daf
JG
318
319 /* Set the top bit to indicate random Nicira address. */
74ff3298 320 ea->ea[3] |= 0x80;
70150daf 321}
74ff3298 322static inline uint32_t hash_mac(const struct eth_addr ea,
7e36ac42
AZ
323 const uint16_t vlan, const uint32_t basis)
324{
325 return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
326}
064af421 327
74ff3298
JR
328bool eth_addr_is_reserved(const struct eth_addr);
329bool eth_addr_from_string(const char *, struct eth_addr *);
76343538 330
74ff3298 331void compose_rarp(struct dp_packet *, const struct eth_addr);
b9e8b45a 332
cf62fa4c
PS
333void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci);
334void eth_pop_vlan(struct dp_packet *);
7c66b273 335
cf62fa4c 336const char *eth_from_hex(const char *hex, struct dp_packet **packetp);
74ff3298
JR
337void eth_format_masked(const struct eth_addr ea,
338 const struct eth_addr *mask, struct ds *s);
e22f1753 339
cf62fa4c
PS
340void set_mpls_lse(struct dp_packet *, ovs_be32 label);
341void push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse);
342void pop_mpls(struct dp_packet *, ovs_be16 ethtype);
b02475c5 343
b676167a 344void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
b02475c5
SH
345void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
346void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
347void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
348ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
349 ovs_be32 label);
350
eaa71334
BP
351/* Example:
352 *
74ff3298 353 * struct eth_addr mac;
eaa71334
BP
354 * [...]
355 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
356 *
357 */
064af421
BP
358#define ETH_ADDR_FMT \
359 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
ca92d173
AC
360#define ETH_ADDR_ARGS(EA) ETH_ADDR_BYTES_ARGS((EA).ea)
361#define ETH_ADDR_BYTES_ARGS(EAB) \
362 (EAB)[0], (EAB)[1], (EAB)[2], (EAB)[3], (EAB)[4], (EAB)[5]
b0d390e5 363#define ETH_ADDR_STRLEN 17
064af421 364
b2342f7a
BP
365/* Example:
366 *
367 * struct eth_addr64 eui64;
368 * [...]
369 * printf("The EUI-64 address is "ETH_ADDR64_FMT"\n", ETH_ADDR64_ARGS(mac));
370 *
371 */
372#define ETH_ADDR64_FMT \
373 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":" \
374 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
375#define ETH_ADDR64_ARGS(EA) ETH_ADDR64_BYTES_ARGS((EA).ea64)
376#define ETH_ADDR64_BYTES_ARGS(EAB) \
377 (EAB)[0], (EAB)[1], (EAB)[2], (EAB)[3], \
378 (EAB)[4], (EAB)[5], (EAB)[6], (EAB)[7]
379#define ETH_ADDR64_STRLEN 23
380
eaa71334
BP
381/* Example:
382 *
383 * char *string = "1 00:11:22:33:44:55 2";
74ff3298 384 * struct eth_addr mac;
eaa71334
BP
385 * int a, b;
386 *
c2c28dfd
BP
387 * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
388 * &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
eaa71334
BP
389 * ...
390 * }
391 */
392#define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
74ff3298
JR
393#define ETH_ADDR_SCAN_ARGS(EA) \
394 &(EA).ea[0], &(EA).ea[1], &(EA).ea[2], &(EA).ea[3], &(EA).ea[4], &(EA).ea[5]
eaa71334 395
064af421
BP
396#define ETH_TYPE_IP 0x0800
397#define ETH_TYPE_ARP 0x0806
a36de779 398#define ETH_TYPE_TEB 0x6558
3e34fbdd
IY
399#define ETH_TYPE_VLAN_8021Q 0x8100
400#define ETH_TYPE_VLAN ETH_TYPE_VLAN_8021Q
401#define ETH_TYPE_VLAN_8021AD 0x88a8
d31f1109 402#define ETH_TYPE_IPV6 0x86dd
c25c91fd 403#define ETH_TYPE_LACP 0x8809
38f7147c 404#define ETH_TYPE_RARP 0x8035
4fba171d
BP
405#define ETH_TYPE_MPLS 0x8847
406#define ETH_TYPE_MPLS_MCAST 0x8848
3d2fbd70 407#define ETH_TYPE_NSH 0x894f
7dc18ae9
WT
408#define ETH_TYPE_ERSPAN1 0x88be /* version 1 type II */
409#define ETH_TYPE_ERSPAN2 0x22eb /* version 2 type III */
064af421 410
b02475c5
SH
411static inline bool eth_type_mpls(ovs_be16 eth_type)
412{
413 return eth_type == htons(ETH_TYPE_MPLS) ||
414 eth_type == htons(ETH_TYPE_MPLS_MCAST);
415}
416
d6943394
TH
417static inline bool eth_type_vlan(ovs_be16 eth_type)
418{
419 return eth_type == htons(ETH_TYPE_VLAN_8021Q) ||
420 eth_type == htons(ETH_TYPE_VLAN_8021AD);
421}
422
423
36956a7d
BP
424/* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame
425 * lengths. */
426#define ETH_TYPE_MIN 0x600
427
064af421
BP
428#define ETH_HEADER_LEN 14
429#define ETH_PAYLOAD_MIN 46
430#define ETH_PAYLOAD_MAX 1500
431#define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
432#define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
433#define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
434struct eth_header {
74ff3298
JR
435 struct eth_addr eth_dst;
436 struct eth_addr eth_src;
d5ca4fce 437 ovs_be16 eth_type;
1620b7ea 438};
064af421
BP
439BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
440
88fc5281
JS
441void push_eth(struct dp_packet *packet, const struct eth_addr *dst,
442 const struct eth_addr *src);
443void pop_eth(struct dp_packet *packet);
444
f59cb331
YY
445void push_nsh(struct dp_packet *packet, const struct nsh_hdr *nsh_hdr_src);
446bool pop_nsh(struct dp_packet *packet);
1fc11c59 447
064af421
BP
448#define LLC_DSAP_SNAP 0xaa
449#define LLC_SSAP_SNAP 0xaa
450#define LLC_CNTL_SNAP 3
451
452#define LLC_HEADER_LEN 3
453struct llc_header {
454 uint8_t llc_dsap;
455 uint8_t llc_ssap;
456 uint8_t llc_cntl;
1620b7ea 457};
064af421
BP
458BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
459
9efd308e
DV
460/* LLC field values used for STP frames. */
461#define STP_LLC_SSAP 0x42
462#define STP_LLC_DSAP 0x42
463#define STP_LLC_CNTL 0x03
464
064af421
BP
465#define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
466 sizeof(SNAP_ORG_ETHERNET) == 3. */
467#define SNAP_HEADER_LEN 5
13b6bae6 468OVS_PACKED(
064af421
BP
469struct snap_header {
470 uint8_t snap_org[3];
d5ca4fce 471 ovs_be16 snap_type;
13b6bae6 472});
064af421
BP
473BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
474
475#define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
13b6bae6 476OVS_PACKED(
064af421
BP
477struct llc_snap_header {
478 struct llc_header llc;
479 struct snap_header snap;
13b6bae6 480});
064af421
BP
481BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
482
483#define VLAN_VID_MASK 0x0fff
33ce24ed
BP
484#define VLAN_VID_SHIFT 0
485
064af421 486#define VLAN_PCP_MASK 0xe000
33ce24ed 487#define VLAN_PCP_SHIFT 13
064af421 488
26233bb4 489#define VLAN_CFI 0x1000
e6cc0bab 490#define VLAN_CFI_SHIFT 12
26233bb4
BP
491
492/* Given the vlan_tci field from an 802.1Q header, in network byte order,
493 * returns the VLAN ID in host byte order. */
494static inline uint16_t
d5ca4fce 495vlan_tci_to_vid(ovs_be16 vlan_tci)
26233bb4
BP
496{
497 return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
498}
499
500/* Given the vlan_tci field from an 802.1Q header, in network byte order,
501 * returns the priority code point (PCP) in host byte order. */
502static inline int
d5ca4fce 503vlan_tci_to_pcp(ovs_be16 vlan_tci)
26233bb4
BP
504{
505 return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
506}
507
e6cc0bab
AZ
508/* Given the vlan_tci field from an 802.1Q header, in network byte order,
509 * returns the Canonical Format Indicator (CFI). */
510static inline int
511vlan_tci_to_cfi(ovs_be16 vlan_tci)
512{
513 return (vlan_tci & htons(VLAN_CFI)) != 0;
514}
515
064af421
BP
516#define VLAN_HEADER_LEN 4
517struct vlan_header {
d5ca4fce
EJ
518 ovs_be16 vlan_tci; /* Lowest 12 bits are VLAN ID. */
519 ovs_be16 vlan_next_type;
064af421
BP
520};
521BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
522
523#define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
524struct vlan_eth_header {
74ff3298
JR
525 struct eth_addr veth_dst;
526 struct eth_addr veth_src;
d5ca4fce
EJ
527 ovs_be16 veth_type; /* Always htons(ETH_TYPE_VLAN). */
528 ovs_be16 veth_tci; /* Lowest 12 bits are VLAN ID. */
529 ovs_be16 veth_next_type;
1620b7ea 530};
064af421
BP
531BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
532
b02475c5
SH
533/* MPLS related definitions */
534#define MPLS_TTL_MASK 0x000000ff
535#define MPLS_TTL_SHIFT 0
536
537#define MPLS_BOS_MASK 0x00000100
538#define MPLS_BOS_SHIFT 8
539
540#define MPLS_TC_MASK 0x00000e00
541#define MPLS_TC_SHIFT 9
542
543#define MPLS_LABEL_MASK 0xfffff000
544#define MPLS_LABEL_SHIFT 12
545
546#define MPLS_HLEN 4
547
548struct mpls_hdr {
5fa008d4 549 ovs_16aligned_be32 mpls_lse;
b02475c5
SH
550};
551BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
552
553/* Given a mpls label stack entry in network byte order
554 * return mpls label in host byte order */
555static inline uint32_t
556mpls_lse_to_label(ovs_be32 mpls_lse)
557{
558 return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
559}
560
561/* Given a mpls label stack entry in network byte order
562 * return mpls tc */
563static inline uint8_t
564mpls_lse_to_tc(ovs_be32 mpls_lse)
565{
566 return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
567}
568
569/* Given a mpls label stack entry in network byte order
570 * return mpls ttl */
571static inline uint8_t
572mpls_lse_to_ttl(ovs_be32 mpls_lse)
573{
574 return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
575}
576
577/* Set TTL in mpls lse. */
578static inline void
579flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
580{
581 *mpls_lse &= ~htonl(MPLS_TTL_MASK);
582 *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
583}
584
585/* Given a mpls label stack entry in network byte order
586 * return mpls BoS bit */
587static inline uint8_t
588mpls_lse_to_bos(ovs_be32 mpls_lse)
589{
590 return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
591}
592
ed36537e 593#define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
064af421 594#define IP_ARGS(ip) \
ed36537e
BP
595 ntohl(ip) >> 24, \
596 (ntohl(ip) >> 16) & 0xff, \
597 (ntohl(ip) >> 8) & 0xff, \
598 ntohl(ip) & 0xff
064af421 599
3bffc610
BP
600/* Example:
601 *
602 * char *string = "1 33.44.55.66 2";
603 * ovs_be32 ip;
604 * int a, b;
605 *
c2c28dfd 606 * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
3bffc610
BP
607 * ...
608 * }
609 */
610#define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
611#define IP_SCAN_ARGS(ip) \
612 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
613 &((uint8_t *) ip)[1], \
614 &((uint8_t *) ip)[2], \
615 &((uint8_t *) ip)[3]
3bffc610 616
e2bfcad6 617#define IP_PORT_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8":%"SCNu16
618#define IP_PORT_SCAN_ARGS(ip, port) \
619 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
620 &((uint8_t *) ip)[1], \
621 &((uint8_t *) ip)[2], \
622 &((uint8_t *) ip)[3], \
623 ((void) (ovs_be16) *(port), (uint16_t *) port)
624
0596e897
BP
625/* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
626 * high-order 1-bits and 32-N low-order 0-bits. */
627static inline bool
628ip_is_cidr(ovs_be32 netmask)
629{
630 uint32_t x = ~ntohl(netmask);
631 return !(x & (x + 1));
632}
b37e6334
BP
633static inline bool
634ip_is_multicast(ovs_be32 ip)
635{
636 return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
637}
7e598a7d
FL
638static inline bool
639ip_is_local_multicast(ovs_be32 ip)
640{
641 return (ip & htonl(0xffffff00)) == htonl(0xe0000000);
642}
aad29cd1
BP
643int ip_count_cidr_bits(ovs_be32 netmask);
644void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
2b02db1b 645bool ip_parse(const char *s, ovs_be32 *ip);
e2bfcad6 646char *ip_parse_port(const char *s, ovs_be32 *ip, ovs_be16 *port)
647 OVS_WARN_UNUSED_RESULT;
61440451
BP
648char *ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
649 OVS_WARN_UNUSED_RESULT;
2b02db1b
BP
650char *ip_parse_cidr(const char *s, ovs_be32 *ip, unsigned int *plen)
651 OVS_WARN_UNUSED_RESULT;
7dc88496
NS
652char *ip_parse_masked_len(const char *s, int *n, ovs_be32 *ip, ovs_be32 *mask)
653 OVS_WARN_UNUSED_RESULT;
654char *ip_parse_cidr_len(const char *s, int *n, ovs_be32 *ip,
655 unsigned int *plen)
656 OVS_WARN_UNUSED_RESULT;
0596e897 657
064af421
BP
658#define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
659#define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
660#define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
661
6c99379e
BP
662#ifndef IPPROTO_SCTP
663#define IPPROTO_SCTP 132
664#endif
665
81f97b1e
JR
666#ifndef IPPROTO_DCCP
667#define IPPROTO_DCCP 33
668#endif
669
670#ifndef IPPROTO_IGMP
671#define IPPROTO_IGMP 2
672#endif
673
674#ifndef IPPROTO_UDPLITE
675#define IPPROTO_UDPLITE 136
676#endif
677
f1193301 678/* TOS fields. */
495fe264
JG
679#define IP_ECN_NOT_ECT 0x0
680#define IP_ECN_ECT_1 0x01
681#define IP_ECN_ECT_0 0x02
682#define IP_ECN_CE 0x03
f1193301
BP
683#define IP_ECN_MASK 0x03
684#define IP_DSCP_MASK 0xfc
685
1bc3f0ed
PS
686static inline int
687IP_ECN_is_ce(uint8_t dsfield)
688{
689 return (dsfield & IP_ECN_MASK) == IP_ECN_CE;
690}
691
064af421
BP
692#define IP_VERSION 4
693
694#define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
695#define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
696#define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
697#define IP_IS_FRAGMENT(ip_frag_off) \
698 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
699
700#define IP_HEADER_LEN 20
701struct ip_header {
702 uint8_t ip_ihl_ver;
703 uint8_t ip_tos;
d5ca4fce
EJ
704 ovs_be16 ip_tot_len;
705 ovs_be16 ip_id;
706 ovs_be16 ip_frag_off;
064af421
BP
707 uint8_t ip_ttl;
708 uint8_t ip_proto;
d5ca4fce 709 ovs_be16 ip_csum;
7c457c33
BP
710 ovs_16aligned_be32 ip_src;
711 ovs_16aligned_be32 ip_dst;
064af421
BP
712};
713BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
714
caf64dc9
DDP
715/* ICMPv4 types. */
716#define ICMP4_ECHO_REPLY 0
717#define ICMP4_DST_UNREACH 3
718#define ICMP4_SOURCEQUENCH 4
719#define ICMP4_REDIRECT 5
720#define ICMP4_ECHO_REQUEST 8
721#define ICMP4_TIME_EXCEEDED 11
722#define ICMP4_PARAM_PROB 12
723#define ICMP4_TIMESTAMP 13
724#define ICMP4_TIMESTAMPREPLY 14
725#define ICMP4_INFOREQUEST 15
726#define ICMP4_INFOREPLY 16
727
c4ccff78 728#define ICMP_HEADER_LEN 8
064af421
BP
729struct icmp_header {
730 uint8_t icmp_type;
731 uint8_t icmp_code;
d5ca4fce 732 ovs_be16 icmp_csum;
c4ccff78
JG
733 union {
734 struct {
735 ovs_be16 id;
736 ovs_be16 seq;
737 } echo;
738 struct {
739 ovs_be16 empty;
740 ovs_be16 mtu;
741 } frag;
7c457c33 742 ovs_16aligned_be32 gateway;
c4ccff78 743 } icmp_fields;
064af421
BP
744};
745BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
746
8e451a96
DB
747#define IGMP_HEADER_LEN 8
748struct igmp_header {
749 uint8_t igmp_type;
750 uint8_t igmp_code;
751 ovs_be16 igmp_csum;
752 ovs_16aligned_be32 group;
753};
754BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header));
755
e3102e42
TLSC
756#define IGMPV3_HEADER_LEN 8
757struct igmpv3_header {
758 uint8_t type;
759 uint8_t rsvr1;
760 ovs_be16 csum;
761 ovs_be16 rsvr2;
762 ovs_be16 ngrp;
763};
764BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header));
765
766#define IGMPV3_RECORD_LEN 8
767struct igmpv3_record {
768 uint8_t type;
769 uint8_t aux_len;
770 ovs_be16 nsrcs;
771 ovs_16aligned_be32 maddr;
772};
773BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record));
774
8e451a96
DB
775#define IGMP_HOST_MEMBERSHIP_QUERY 0x11 /* From RFC1112 */
776#define IGMP_HOST_MEMBERSHIP_REPORT 0x12 /* Ditto */
777#define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */
778#define IGMP_HOST_LEAVE_MESSAGE 0x17
779#define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */
780
06994f87
TLSC
781/*
782 * IGMPv3 and MLDv2 use the same codes.
783 */
e3102e42
TLSC
784#define IGMPV3_MODE_IS_INCLUDE 1
785#define IGMPV3_MODE_IS_EXCLUDE 2
786#define IGMPV3_CHANGE_TO_INCLUDE_MODE 3
787#define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4
788#define IGMPV3_ALLOW_NEW_SOURCES 5
789#define IGMPV3_BLOCK_OLD_SOURCES 6
790
c6bcb685
JS
791#define SCTP_HEADER_LEN 12
792struct sctp_header {
793 ovs_be16 sctp_src;
794 ovs_be16 sctp_dst;
5fa008d4
BP
795 ovs_16aligned_be32 sctp_vtag;
796 ovs_16aligned_be32 sctp_csum;
c6bcb685
JS
797};
798BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
799
064af421
BP
800#define UDP_HEADER_LEN 8
801struct udp_header {
d5ca4fce
EJ
802 ovs_be16 udp_src;
803 ovs_be16 udp_dst;
804 ovs_be16 udp_len;
805 ovs_be16 udp_csum;
064af421
BP
806};
807BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
808
bc61c7b4
IS
809#define ESP_HEADER_LEN 8
810struct esp_header {
811 ovs_be32 spi;
812 ovs_be32 seq_no;
813};
814BUILD_ASSERT_DECL(ESP_HEADER_LEN == sizeof(struct esp_header));
815
816#define ESP_TRAILER_LEN 2
817struct esp_trailer {
818 uint8_t pad_len;
819 uint8_t next_hdr;
820};
821BUILD_ASSERT_DECL(ESP_TRAILER_LEN == sizeof(struct esp_trailer));
822
a66733a8
JR
823#define TCP_FIN 0x001
824#define TCP_SYN 0x002
825#define TCP_RST 0x004
826#define TCP_PSH 0x008
827#define TCP_ACK 0x010
828#define TCP_URG 0x020
829#define TCP_ECE 0x040
830#define TCP_CWR 0x080
831#define TCP_NS 0x100
064af421 832
df9b6612 833#define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
a66733a8 834#define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
419681da 835#define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff))
d84d4b88 836#define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
064af421
BP
837
838#define TCP_HEADER_LEN 20
839struct tcp_header {
d5ca4fce
EJ
840 ovs_be16 tcp_src;
841 ovs_be16 tcp_dst;
7c457c33
BP
842 ovs_16aligned_be32 tcp_seq;
843 ovs_16aligned_be32 tcp_ack;
d5ca4fce
EJ
844 ovs_be16 tcp_ctl;
845 ovs_be16 tcp_winsz;
846 ovs_be16 tcp_csum;
847 ovs_be16 tcp_urg;
064af421
BP
848};
849BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
850
fd6cd1bf
BP
851/* Connection states.
852 *
853 * Names like CS_RELATED are bit values, e.g. 1 << 2.
854 * Names like CS_RELATED_BIT are bit indexes, e.g. 2. */
855#define CS_STATES \
856 CS_STATE(NEW, 0, "new") \
857 CS_STATE(ESTABLISHED, 1, "est") \
858 CS_STATE(RELATED, 2, "rel") \
859 CS_STATE(REPLY_DIR, 3, "rpl") \
860 CS_STATE(INVALID, 4, "inv") \
861 CS_STATE(TRACKED, 5, "trk") \
862 CS_STATE(SRC_NAT, 6, "snat") \
863 CS_STATE(DST_NAT, 7, "dnat")
aa68cf38
RB
864
865enum {
fd6cd1bf
BP
866#define CS_STATE(ENUM, INDEX, NAME) \
867 CS_##ENUM = 1 << INDEX, \
868 CS_##ENUM##_BIT = INDEX,
869 CS_STATES
870#undef CS_STATE
aa68cf38 871};
07659514
JS
872
873/* Undefined connection state bits. */
fd6cd1bf
BP
874enum {
875#define CS_STATE(ENUM, INDEX, NAME) +CS_##ENUM
876 CS_SUPPORTED_MASK = CS_STATES
877#undef CS_STATE
878};
07659514
JS
879#define CS_UNSUPPORTED_MASK (~(uint32_t)CS_SUPPORTED_MASK)
880
064af421
BP
881#define ARP_HRD_ETHERNET 1
882#define ARP_PRO_IP 0x0800
883#define ARP_OP_REQUEST 1
884#define ARP_OP_REPLY 2
7cb57d10 885#define ARP_OP_RARP 3
064af421
BP
886
887#define ARP_ETH_HEADER_LEN 28
888struct arp_eth_header {
889 /* Generic members. */
d5ca4fce
EJ
890 ovs_be16 ar_hrd; /* Hardware type. */
891 ovs_be16 ar_pro; /* Protocol type. */
064af421
BP
892 uint8_t ar_hln; /* Hardware address length. */
893 uint8_t ar_pln; /* Protocol address length. */
d5ca4fce 894 ovs_be16 ar_op; /* Opcode. */
064af421
BP
895
896 /* Ethernet+IPv4 specific members. */
74ff3298
JR
897 struct eth_addr ar_sha; /* Sender hardware address. */
898 ovs_16aligned_be32 ar_spa; /* Sender protocol address. */
899 struct eth_addr ar_tha; /* Target hardware address. */
900 ovs_16aligned_be32 ar_tpa; /* Target protocol address. */
7c457c33 901};
064af421
BP
902BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
903
370e373b
TLSC
904#define IPV6_HEADER_LEN 40
905
4528f34f
BP
906/* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
907 * most implementations, this one only requires 16-bit alignment. */
908union ovs_16aligned_in6_addr {
909 ovs_be16 be16[8];
910 ovs_16aligned_be32 be32[4];
911};
912
913/* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
914 * one only requires 16-bit alignment. */
915struct ovs_16aligned_ip6_hdr {
916 union {
917 struct ovs_16aligned_ip6_hdrctl {
918 ovs_16aligned_be32 ip6_un1_flow;
919 ovs_be16 ip6_un1_plen;
920 uint8_t ip6_un1_nxt;
921 uint8_t ip6_un1_hlim;
922 } ip6_un1;
923 uint8_t ip6_un2_vfc;
924 } ip6_ctlun;
925 union ovs_16aligned_in6_addr ip6_src;
926 union ovs_16aligned_in6_addr ip6_dst;
927};
928
929/* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
930 * this one only requires 16-bit alignment. */
931struct ovs_16aligned_ip6_frag {
932 uint8_t ip6f_nxt;
933 uint8_t ip6f_reserved;
934 ovs_be16 ip6f_offlg;
935 ovs_16aligned_be32 ip6f_ident;
936};
937
5abf65d0
JG
938#define ICMP6_HEADER_LEN 4
939struct icmp6_header {
940 uint8_t icmp6_type;
941 uint8_t icmp6_code;
942 ovs_be16 icmp6_cksum;
5abf65d0
JG
943};
944BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header));
945
edd1bef4
DB
946#define ICMP6_ERROR_HEADER_LEN 8
947struct icmp6_error_header {
948 struct icmp6_header icmp6_base;
949 ovs_be32 icmp6_error_ext;
950};
951BUILD_ASSERT_DECL(ICMP6_ERROR_HEADER_LEN == sizeof(struct icmp6_error_header));
952
370e373b 953uint32_t packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *);
46445c63
EC
954uint16_t packet_csum_upperlayer6(const struct ovs_16aligned_ip6_hdr *,
955 const void *, uint8_t, uint16_t);
370e373b 956
e60e935b
SRCSA
957/* Neighbor Discovery option field.
958 * ND options are always a multiple of 8 bytes in size. */
86d46f3c
ZKL
959#define ND_LLA_OPT_LEN 8
960struct ovs_nd_lla_opt {
961 uint8_t type; /* One of ND_OPT_*_LINKADDR. */
962 uint8_t len;
963 struct eth_addr mac;
e60e935b 964};
86d46f3c 965BUILD_ASSERT_DECL(ND_LLA_OPT_LEN == sizeof(struct ovs_nd_lla_opt));
e60e935b 966
b24ab67c
ZKL
967/* Neighbor Discovery option: Prefix Information. */
968#define ND_PREFIX_OPT_LEN 32
969struct ovs_nd_prefix_opt {
970 uint8_t type; /* ND_OPT_PREFIX_INFORMATION. */
971 uint8_t len; /* Always 4. */
972 uint8_t prefix_len;
973 uint8_t la_flags; /* ND_PREFIX_* flags. */
974 ovs_16aligned_be32 valid_lifetime;
975 ovs_16aligned_be32 preferred_lifetime;
976 ovs_16aligned_be32 reserved; /* Always 0. */
977 union ovs_16aligned_in6_addr prefix;
978};
979BUILD_ASSERT_DECL(ND_PREFIX_OPT_LEN == sizeof(struct ovs_nd_prefix_opt));
980
981#define ND_PREFIX_ON_LINK 0x80
982#define ND_PREFIX_AUTONOMOUS_ADDRESS 0x40
983
984/* Neighbor Discovery option: MTU. */
985#define ND_MTU_OPT_LEN 8
4446661a 986#define ND_MTU_DEFAULT 0
b24ab67c
ZKL
987struct ovs_nd_mtu_opt {
988 uint8_t type; /* ND_OPT_MTU */
989 uint8_t len; /* Always 1. */
990 ovs_be16 reserved; /* Always 0. */
991 ovs_16aligned_be32 mtu;
992};
993BUILD_ASSERT_DECL(ND_MTU_OPT_LEN == sizeof(struct ovs_nd_mtu_opt));
994
e60e935b
SRCSA
995/* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit
996 * alignment, this one only requires 16-bit alignment. */
997#define ND_MSG_LEN 24
998struct ovs_nd_msg {
999 struct icmp6_header icmph;
29d5e9a7 1000 ovs_16aligned_be32 rso_flags;
e60e935b 1001 union ovs_16aligned_in6_addr target;
86d46f3c 1002 struct ovs_nd_lla_opt options[0];
e60e935b
SRCSA
1003};
1004BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg));
1005
b24ab67c 1006/* Neighbor Discovery packet flags. */
fa7f915c
JP
1007#define ND_RSO_ROUTER 0x80000000
1008#define ND_RSO_SOLICITED 0x40000000
1009#define ND_RSO_OVERRIDE 0x20000000
1010
b24ab67c
ZKL
1011#define RA_MSG_LEN 16
1012struct ovs_ra_msg {
1013 struct icmp6_header icmph;
1014 uint8_t cur_hop_limit;
1015 uint8_t mo_flags; /* ND_RA_MANAGED_ADDRESS and ND_RA_OTHER_CONFIG flags. */
1016 ovs_be16 router_lifetime;
1017 ovs_be32 reachable_time;
1018 ovs_be32 retrans_timer;
86d46f3c 1019 struct ovs_nd_lla_opt options[0];
b24ab67c
ZKL
1020};
1021BUILD_ASSERT_DECL(RA_MSG_LEN == sizeof(struct ovs_ra_msg));
1022
1023#define ND_RA_MANAGED_ADDRESS 0x80
1024#define ND_RA_OTHER_CONFIG 0x40
1025
4446661a
MM
1026/* Defaults based on MaxRtrInterval and MinRtrInterval from RFC 4861 section
1027 * 6.2.1
1028 */
1029#define ND_RA_MAX_INTERVAL_DEFAULT 600
1030
1031static inline int
1032nd_ra_min_interval_default(int max)
1033{
1034 return max >= 9 ? max / 3 : max * 3 / 4;
1035}
1036
06994f87
TLSC
1037/*
1038 * Use the same struct for MLD and MLD2, naming members as the defined fields in
1039 * in the corresponding version of the protocol, though they are reserved in the
1040 * other one.
1041 */
1042#define MLD_HEADER_LEN 8
1043struct mld_header {
1044 uint8_t type;
1045 uint8_t code;
1046 ovs_be16 csum;
1047 ovs_be16 mrd;
1048 ovs_be16 ngrp;
1049};
1050BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header));
1051
1052#define MLD2_RECORD_LEN 20
1053struct mld2_record {
1054 uint8_t type;
1055 uint8_t aux_len;
1056 ovs_be16 nsrcs;
1057 union ovs_16aligned_in6_addr maddr;
1058};
1059BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record));
1060
1061#define MLD_QUERY 130
1062#define MLD_REPORT 131
1063#define MLD_DONE 132
1064#define MLD2_REPORT 143
1065
fa8223b7
JP
1066/* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
1067#define IPV6_LABEL_MASK 0x000fffff
1068
3bffc610
BP
1069/* Example:
1070 *
1071 * char *string = "1 ::1 2";
1072 * char ipv6_s[IPV6_SCAN_LEN + 1];
1073 * struct in6_addr ipv6;
1074 *
c2c28dfd 1075 * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
3bffc610
BP
1076 * && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
1077 * ...
1078 * }
1079 */
1080#define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
1081#define IPV6_SCAN_LEN 46
1082
d31f1109
JP
1083extern const struct in6_addr in6addr_exact;
1084#define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
1085 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
1086
06994f87
TLSC
1087extern const struct in6_addr in6addr_all_hosts;
1088#define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
1089 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } }
1090
b24ab67c
ZKL
1091extern const struct in6_addr in6addr_all_routers;
1092#define IN6ADDR_ALL_ROUTERS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
1093 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02 } } }
1094
d31f1109
JP
1095static inline bool ipv6_addr_equals(const struct in6_addr *a,
1096 const struct in6_addr *b)
1097{
1098#ifdef IN6_ARE_ADDR_EQUAL
1099 return IN6_ARE_ADDR_EQUAL(a, b);
1100#else
1101 return !memcmp(a, b, sizeof(*a));
1102#endif
1103}
1104
33ac3c83 1105/* Checks the IPv6 address in 'mask' for all zeroes. */
d31f1109
JP
1106static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
1107 return ipv6_addr_equals(mask, &in6addr_any);
1108}
1109
1110static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
1111 return ipv6_addr_equals(mask, &in6addr_exact);
1112}
1113
06994f87
TLSC
1114static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) {
1115 return ipv6_addr_equals(addr, &in6addr_all_hosts);
1116}
1117
8c46162b
JB
1118static inline bool ipv6_addr_is_set(const struct in6_addr *addr) {
1119 return !ipv6_addr_equals(addr, &in6addr_any);
1120}
1121
1122static inline bool ipv6_addr_is_multicast(const struct in6_addr *ip) {
1123 return ip->s6_addr[0] == 0xff;
1124}
1125
12d0ee08
BP
1126static inline struct in6_addr
1127in6_addr_mapped_ipv4(ovs_be32 ip4)
1128{
274cd1f1
YHW
1129 struct in6_addr ip6;
1130 memset(&ip6, 0, sizeof(ip6));
1131 ip6.s6_addr[10] = 0xff, ip6.s6_addr[11] = 0xff;
12d0ee08
BP
1132 memcpy(&ip6.s6_addr[12], &ip4, 4);
1133 return ip6;
1134}
1135
754c1feb 1136static inline void
12d0ee08 1137in6_addr_set_mapped_ipv4(struct in6_addr *ip6, ovs_be32 ip4)
754c1feb 1138{
12d0ee08 1139 *ip6 = in6_addr_mapped_ipv4(ip4);
754c1feb
TLSC
1140}
1141
1142static inline ovs_be32
1143in6_addr_get_mapped_ipv4(const struct in6_addr *addr)
1144{
8bfc3c10
SS
1145 union ovs_16aligned_in6_addr *taddr =
1146 (union ovs_16aligned_in6_addr *) addr;
754c1feb
TLSC
1147 if (IN6_IS_ADDR_V4MAPPED(addr)) {
1148 return get_16aligned_be32(&taddr->be32[3]);
1149 } else {
1150 return INADDR_ANY;
1151 }
1152}
1153
c2b878e0
TLSC
1154static inline void
1155in6_addr_solicited_node(struct in6_addr *addr, const struct in6_addr *ip6)
1156{
8bfc3c10
SS
1157 union ovs_16aligned_in6_addr *taddr =
1158 (union ovs_16aligned_in6_addr *) addr;
c2b878e0
TLSC
1159 memset(taddr->be16, 0, sizeof(taddr->be16));
1160 taddr->be16[0] = htons(0xff02);
1161 taddr->be16[5] = htons(0x1);
1162 taddr->be16[6] = htons(0xff00);
1163 memcpy(&addr->s6_addr[13], &ip6->s6_addr[13], 3);
1164}
1165
7cc0741e
NS
1166/*
1167 * Generates ipv6 EUI64 address from the given eth addr
1168 * and prefix and stores it in 'lla'
1169 */
1170static inline void
1171in6_generate_eui64(struct eth_addr ea, struct in6_addr *prefix,
1172 struct in6_addr *lla)
1173{
8bfc3c10
SS
1174 union ovs_16aligned_in6_addr *taddr =
1175 (union ovs_16aligned_in6_addr *) lla;
1176 union ovs_16aligned_in6_addr *prefix_taddr =
1177 (union ovs_16aligned_in6_addr *) prefix;
7cc0741e
NS
1178 taddr->be16[0] = prefix_taddr->be16[0];
1179 taddr->be16[1] = prefix_taddr->be16[1];
1180 taddr->be16[2] = prefix_taddr->be16[2];
1181 taddr->be16[3] = prefix_taddr->be16[3];
1182 taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]);
1183 taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff);
1184 taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]);
1185 taddr->be16[7] = ea.be16[2];
1186}
1187
685f4dfe
NS
1188/*
1189 * Generates ipv6 link local address from the given eth addr
1190 * with prefix 'fe80::/64' and stores it in 'lla'
1191 */
1192static inline void
1193in6_generate_lla(struct eth_addr ea, struct in6_addr *lla)
1194{
8bfc3c10
SS
1195 union ovs_16aligned_in6_addr *taddr =
1196 (union ovs_16aligned_in6_addr *) lla;
685f4dfe
NS
1197 memset(taddr->be16, 0, sizeof(taddr->be16));
1198 taddr->be16[0] = htons(0xfe80);
1199 taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]);
1200 taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff);
1201 taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]);
1202 taddr->be16[7] = ea.be16[2];
1203}
1204
6d1a4f16
JP
1205/* Returns true if 'addr' is a link local address. Otherwise, false. */
1206static inline bool
1207in6_is_lla(struct in6_addr *addr)
1208{
1209#ifdef s6_addr32
1210 return addr->s6_addr32[0] == htonl(0xfe800000) && !(addr->s6_addr32[1]);
1211#else
bf32e3e2
JP
1212 return addr->s6_addr[0] == 0xfe && addr->s6_addr[1] == 0x80 &&
1213 !(addr->s6_addr[2] | addr->s6_addr[3] | addr->s6_addr[4] |
1214 addr->s6_addr[5] | addr->s6_addr[6] | addr->s6_addr[7]);
6d1a4f16
JP
1215#endif
1216}
1217
c2b878e0
TLSC
1218static inline void
1219ipv6_multicast_to_ethernet(struct eth_addr *eth, const struct in6_addr *ip6)
1220{
1221 eth->ea[0] = 0x33;
1222 eth->ea[1] = 0x33;
1223 eth->ea[2] = ip6->s6_addr[12];
1224 eth->ea[3] = ip6->s6_addr[13];
1225 eth->ea[4] = ip6->s6_addr[14];
1226 eth->ea[5] = ip6->s6_addr[15];
1227}
1228
e8c16d83
SH
1229static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
1230{
1231 return dl_type == htons(ETH_TYPE_IP)
1232 || dl_type == htons(ETH_TYPE_IPV6);
1233}
1234
a36de779 1235/* Tunnel header */
e5a1caee 1236
a36de779
PS
1237/* GRE protocol header */
1238struct gre_base_hdr {
1239 ovs_be16 flags;
1240 ovs_be16 protocol;
1241};
1242
1243#define GRE_CSUM 0x8000
1244#define GRE_ROUTING 0x4000
1245#define GRE_KEY 0x2000
1246#define GRE_SEQ 0x1000
1247#define GRE_STRICT 0x0800
1248#define GRE_REC 0x0700
1249#define GRE_FLAGS 0x00F8
1250#define GRE_VERSION 0x0007
1251
7dc18ae9
WT
1252/*
1253 * ERSPAN protocol header and metadata
1254 *
1255 * Version 1 (Type II) header (8 octets [42:49])
1256 * 0 1 2 3
1257 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1258 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1259 * | Ver | VLAN | COS | En|T| Session ID |
1260 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1261 * | Reserved | Index |
1262 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1263 *
1264 *
1265 * ERSPAN Version 2 (Type III) header (12 octets [42:49])
1266 * 0 1 2 3
1267 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1268 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1269 * | Ver | VLAN | COS |BSO|T| Session ID |
1270 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1271 * | Timestamp |
1272 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1273 * | SGT |P| FT | Hw ID |D|Gra|O|
1274 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1275 *
1276 */
1277
1278/* ERSPAN has fixed 8-byte GRE header */
1279#define ERSPAN_GREHDR_LEN 8
1280#define ERSPAN_HDR(gre_base_hdr) \
1281 ((struct erspan_base_hdr *)((char *)gre_base_hdr + ERSPAN_GREHDR_LEN))
1282
1283#define ERSPAN_V1_MDSIZE 4
1284#define ERSPAN_V2_MDSIZE 8
1285
1286#define ERSPAN_SID_MASK 0x03ff /* 10-bit Session ID. */
1287#define ERSPAN_IDX_MASK 0xfffff /* v1 Index */
1288#define ERSPAN_HWID_MASK 0x03f0
1289#define ERSPAN_DIR_MASK 0x0008
1290
1291struct erspan_base_hdr {
506ac275
WT
1292#ifdef WORDS_BIGENDIAN
1293 uint8_t ver:4,
1294 vlan_upper:4;
1295 uint8_t vlan:8;
1296 uint8_t cos:3,
1297 en:2,
1298 t:1,
1299 session_id_upper:2;
1300 uint8_t session_id:8;
1301#else
7dc18ae9
WT
1302 uint8_t vlan_upper:4,
1303 ver:4;
1304 uint8_t vlan:8;
1305 uint8_t session_id_upper:2,
1306 t:1,
1307 en:2,
1308 cos:3;
1309 uint8_t session_id:8;
506ac275 1310#endif
7dc18ae9
WT
1311};
1312
1313struct erspan_md2 {
1314 ovs_16aligned_be32 timestamp;
1315 ovs_be16 sgt;
506ac275
WT
1316#ifdef WORDS_BIGENDIAN
1317 uint8_t p:1,
1318 ft:5,
1319 hwid_upper:2;
1320 uint8_t hwid:4,
1321 dir:1,
1322 gra:2,
1323 o:1;
1324#else
7dc18ae9
WT
1325 uint8_t hwid_upper:2,
1326 ft:5,
1327 p:1;
1328 uint8_t o:1,
1329 gra:2,
1330 dir:1,
1331 hwid:4;
506ac275 1332#endif
7dc18ae9
WT
1333};
1334
1335struct erspan_metadata {
1336 int version;
1337 union {
1338 ovs_be32 index; /* Version 1 (type II)*/
1339 struct erspan_md2 md2; /* Version 2 (type III) */
1340 } u;
1341};
1342
1343static inline uint16_t get_sid(const struct erspan_base_hdr *ershdr)
1344{
1345 return (ershdr->session_id_upper << 8) + ershdr->session_id;
1346}
1347
1348static inline void set_sid(struct erspan_base_hdr *ershdr, uint16_t id)
1349{
1350 ershdr->session_id = id & 0xff;
1351 ershdr->session_id_upper = (id >> 8) &0x3;
1352}
1353
1354static inline uint8_t get_hwid(const struct erspan_md2 *md2)
1355{
1356 return (md2->hwid_upper << 4) + md2->hwid;
1357}
1358
1359static inline void set_hwid(struct erspan_md2 *md2, uint8_t hwid)
1360{
1361 md2->hwid = hwid & 0xf;
1362 md2->hwid_upper = (hwid >> 4) & 0x3;
1363}
1364
1365/* ERSPAN timestamp granularity
1366 * 00b --> granularity = 100 microseconds
1367 * 01b --> granularity = 100 nanoseconds
1368 * 10b --> granularity = IEEE 1588
1369 * Here we only support 100 microseconds.
1370 */
1371enum erspan_ts_gra {
1372 ERSPAN_100US,
1373 ERSPAN_100NS,
1374 ERSPAN_IEEE1588,
1375};
1376
1377static inline ovs_be32 get_erspan_ts(enum erspan_ts_gra gra)
1378{
1379 ovs_be32 ts = 0;
1380
1381 switch (gra) {
1382 case ERSPAN_100US:
1383 ts = htonl((uint32_t)(time_wall_usec() / 100));
1384 break;
1385 case ERSPAN_100NS:
1386 /* fall back */
1387 case ERSPAN_IEEE1588:
1388 /* fall back */
1389 default:
1390 OVS_NOT_REACHED();
1391 break;
1392 }
1393 return ts;
1394}
1395
a36de779
PS
1396/* VXLAN protocol header */
1397struct vxlanhdr {
439f39cb
GS
1398 union {
1399 ovs_16aligned_be32 vx_flags; /* VXLAN flags. */
1400 struct {
1401 uint8_t flags; /* VXLAN GPE flags. */
1402 uint8_t reserved[2]; /* 16 bits reserved. */
1403 uint8_t next_protocol; /* Next Protocol field for VXLAN GPE. */
1404 } vx_gpe;
1405 };
a36de779
PS
1406 ovs_16aligned_be32 vx_vni;
1407};
439f39cb 1408BUILD_ASSERT_DECL(sizeof(struct vxlanhdr) == 8);
a36de779
PS
1409
1410#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
1411
439f39cb
GS
1412/*
1413 * VXLAN Generic Protocol Extension (VXLAN_F_GPE):
1414 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1415 * |R|R|Ver|I|P|R|O| Reserved |Next Protocol |
1416 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1417 * | VXLAN Network Identifier (VNI) | Reserved |
1418 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1419 *
1420 * Ver = Version. Indicates VXLAN GPE protocol version.
1421 *
1422 * P = Next Protocol Bit. The P bit is set to indicate that the
1423 * Next Protocol field is present.
1424 *
1425 * O = OAM Flag Bit. The O bit is set to indicate that the packet
1426 * is an OAM packet.
1427 *
1428 * Next Protocol = This 8 bit field indicates the protocol header
1429 * immediately following the VXLAN GPE header.
1430 *
1431 * https://tools.ietf.org/html/draft-ietf-nvo3-vxlan-gpe-01
1432 */
1433
1434/* Fields in struct vxlanhdr.vx_gpe.flags */
1435#define VXLAN_GPE_FLAGS_VER 0x30 /* Version. */
1436#define VLXAN_GPE_FLAGS_P 0x04 /* Next Protocol Bit. */
1437#define VXLAN_GPE_FLAGS_O 0x01 /* OAM Bit. */
1438
1439/* VXLAN-GPE header flags. */
1440#define VXLAN_HF_VER ((1U <<29) | (1U <<28))
1441#define VXLAN_HF_NP (1U <<26)
1442#define VXLAN_HF_OAM (1U <<24)
1443
1444#define VXLAN_GPE_USED_BITS (VXLAN_HF_VER | VXLAN_HF_NP | VXLAN_HF_OAM | \
1445 0xff)
1446
1447/* VXLAN-GPE header Next Protocol. */
1448#define VXLAN_GPE_NP_IPV4 0x01
1449#define VXLAN_GPE_NP_IPV6 0x02
1450#define VXLAN_GPE_NP_ETHERNET 0x03
1451#define VXLAN_GPE_NP_NSH 0x04
1452
1453#define VXLAN_F_GPE 0x4000
1454#define VXLAN_HF_GPE 0x04000000
1455
2482b0b0
JS
1456/* Input values for PACKET_TYPE macros have to be in host byte order.
1457 * The _BE postfix indicates result is in network byte order. Otherwise result
1458 * is in host byte order. */
1459#define PACKET_TYPE(NS, NS_TYPE) ((uint32_t) ((NS) << 16 | (NS_TYPE)))
1460#define PACKET_TYPE_BE(NS, NS_TYPE) (htonl((NS) << 16 | (NS_TYPE)))
1461
1462/* Returns the host byte ordered namespace of 'packet type'. */
1463static inline uint16_t
1464pt_ns(ovs_be32 packet_type)
1465{
1466 return ntohl(packet_type) >> 16;
1467}
1468
1469/* Returns the network byte ordered namespace type of 'packet type'. */
1470static inline ovs_be16
1471pt_ns_type_be(ovs_be32 packet_type)
1472{
1473 return be32_to_be16(packet_type);
1474}
1475
1476/* Returns the host byte ordered namespace type of 'packet type'. */
1477static inline uint16_t
1478pt_ns_type(ovs_be32 packet_type)
1479{
1480 return ntohs(pt_ns_type_be(packet_type));
1481}
1482
1483/* Well-known packet_type field values. */
1484enum packet_type {
f839892a
JS
1485 PT_ETH = PACKET_TYPE(OFPHTN_ONF, 0x0000), /* Default PT: Ethernet */
1486 PT_USE_NEXT_PROTO = PACKET_TYPE(OFPHTN_ONF, 0xfffe), /* Pseudo PT for decap. */
2482b0b0
JS
1487 PT_IPV4 = PACKET_TYPE(OFPHTN_ETHERTYPE, ETH_TYPE_IP),
1488 PT_IPV6 = PACKET_TYPE(OFPHTN_ETHERTYPE, ETH_TYPE_IPV6),
1489 PT_MPLS = PACKET_TYPE(OFPHTN_ETHERTYPE, ETH_TYPE_MPLS),
1490 PT_MPLS_MC = PACKET_TYPE(OFPHTN_ETHERTYPE, ETH_TYPE_MPLS_MCAST),
3d2fbd70 1491 PT_NSH = PACKET_TYPE(OFPHTN_ETHERTYPE, ETH_TYPE_NSH),
2482b0b0
JS
1492 PT_UNKNOWN = PACKET_TYPE(0xffff, 0xffff), /* Unknown packet type. */
1493};
1494
1495
ac6d120f 1496void ipv6_format_addr(const struct in6_addr *addr, struct ds *);
9ac0aada
JR
1497void ipv6_format_addr_bracket(const struct in6_addr *addr, struct ds *,
1498 bool bracket);
ac6d120f
JP
1499void ipv6_format_mapped(const struct in6_addr *addr, struct ds *);
1500void ipv6_format_masked(const struct in6_addr *addr,
1501 const struct in6_addr *mask, struct ds *);
bed610e8 1502const char * ipv6_string_mapped(char *addr_str, const struct in6_addr *addr);
d31f1109
JP
1503struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
1504 const struct in6_addr *mask);
b0ad27f3
JP
1505struct in6_addr ipv6_addr_bitxor(const struct in6_addr *a,
1506 const struct in6_addr *b);
1507bool ipv6_is_zero(const struct in6_addr *a);
d31f1109
JP
1508struct in6_addr ipv6_create_mask(int mask);
1509int ipv6_count_cidr_bits(const struct in6_addr *netmask);
1510bool ipv6_is_cidr(const struct in6_addr *netmask);
2b02db1b
BP
1511
1512bool ipv6_parse(const char *s, struct in6_addr *ip);
fac5bd3c
JP
1513char *ipv6_parse_masked(const char *s, struct in6_addr *ipv6,
1514 struct in6_addr *mask);
2b02db1b
BP
1515char *ipv6_parse_cidr(const char *s, struct in6_addr *ip, unsigned int *plen)
1516 OVS_WARN_UNUSED_RESULT;
7dc88496
NS
1517char *ipv6_parse_masked_len(const char *s, int *n, struct in6_addr *ipv6,
1518 struct in6_addr *mask);
1519char *ipv6_parse_cidr_len(const char *s, int *n, struct in6_addr *ip,
1520 unsigned int *plen)
1521 OVS_WARN_UNUSED_RESULT;
d31f1109 1522
74ff3298
JR
1523void *eth_compose(struct dp_packet *, const struct eth_addr eth_dst,
1524 const struct eth_addr eth_src, uint16_t eth_type,
5de1bb5c 1525 size_t size);
74ff3298
JR
1526void *snap_compose(struct dp_packet *, const struct eth_addr eth_dst,
1527 const struct eth_addr eth_src,
5de1bb5c 1528 unsigned int oui, uint16_t snap_type, size_t size);
cf62fa4c 1529void packet_set_ipv4(struct dp_packet *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
c97664b3 1530 uint8_t ttl);
fc052306
ZB
1531void packet_set_ipv4_addr(struct dp_packet *packet, ovs_16aligned_be32 *addr,
1532 ovs_be32 new_addr);
932c96b7
JR
1533void packet_set_ipv6(struct dp_packet *, const struct in6_addr *src,
1534 const struct in6_addr *dst, uint8_t tc,
038341d1 1535 ovs_be32 fl, uint8_t hlmit);
0e29d884
DB
1536void packet_set_ipv6_addr(struct dp_packet *packet, uint8_t proto,
1537 ovs_16aligned_be32 addr[4],
1538 const struct in6_addr *new_addr,
1539 bool recalculate_csum);
cf62fa4c
PS
1540void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1541void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1542void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
b8786b18 1543void packet_set_icmp(struct dp_packet *, uint8_t type, uint8_t code);
932c96b7 1544void packet_set_nd(struct dp_packet *, const struct in6_addr *target,
74ff3298 1545 const struct eth_addr sll, const struct eth_addr tll);
40f78b38 1546
a66733a8 1547void packet_format_tcp_flags(struct ds *, uint16_t);
61bf6666 1548const char *packet_tcp_flag_to_string(uint32_t flag);
6335d074 1549void compose_arp__(struct dp_packet *);
eb0b295e 1550void compose_arp(struct dp_packet *, uint16_t arp_op,
74ff3298
JR
1551 const struct eth_addr arp_sha,
1552 const struct eth_addr arp_tha, bool broadcast,
eb0b295e 1553 ovs_be32 arp_spa, ovs_be32 arp_tpa);
16187903
JP
1554void compose_nd_ns(struct dp_packet *, const struct eth_addr eth_src,
1555 const struct in6_addr *ipv6_src,
1556 const struct in6_addr *ipv6_dst);
1557void compose_nd_na(struct dp_packet *, const struct eth_addr eth_src,
1558 const struct eth_addr eth_dst,
1559 const struct in6_addr *ipv6_src,
1560 const struct in6_addr *ipv6_dst,
1561 ovs_be32 rso_flags);
b24ab67c
ZKL
1562void compose_nd_ra(struct dp_packet *,
1563 const struct eth_addr eth_src,
1564 const struct eth_addr eth_dst,
1565 const struct in6_addr *ipv6_src,
1566 const struct in6_addr *ipv6_dst,
1567 uint8_t cur_hop_limit, uint8_t mo_flags,
1568 ovs_be16 router_lt, ovs_be32 reachable_time,
4446661a 1569 ovs_be32 retrans_timer, uint32_t mtu);
b24ab67c
ZKL
1570void packet_put_ra_prefix_opt(struct dp_packet *,
1571 uint8_t plen, uint8_t la_flags,
1572 ovs_be32 valid_lifetime,
1573 ovs_be32 preferred_lifetime,
1574 const ovs_be128 router_prefix);
0292a0c9 1575uint32_t packet_csum_pseudoheader(const struct ip_header *);
1bc3f0ed 1576void IP_ECN_set_ce(struct dp_packet *pkt, bool is_ipv6);
12113c39 1577
ea991ad2
NS
1578#define DNS_HEADER_LEN 12
1579struct dns_header {
1580 ovs_be16 id;
1581 uint8_t lo_flag; /* QR (1), OPCODE (4), AA (1), TC (1) and RD (1) */
1582 uint8_t hi_flag; /* RA (1), Z (3) and RCODE (4) */
1583 ovs_be16 qdcount; /* Num of entries in the question section. */
1584 ovs_be16 ancount; /* Num of resource records in the answer section. */
1585
1586 /* Num of name server records in the authority record section. */
1587 ovs_be16 nscount;
1588
1589 /* Num of resource records in the additional records section. */
1590 ovs_be16 arcount;
1591};
1592
1593BUILD_ASSERT_DECL(DNS_HEADER_LEN == sizeof(struct dns_header));
1594
1595#define DNS_QUERY_TYPE_A 0x01
1596#define DNS_QUERY_TYPE_AAAA 0x1c
1597#define DNS_QUERY_TYPE_ANY 0xff
1598
1599#define DNS_CLASS_IN 0x01
1600#define DNS_DEFAULT_RR_TTL 3600
1601
064af421 1602#endif /* packets.h */