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