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