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