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