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