2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/types.h>
25 #include "openvswitch/geneve.h"
26 #include "openvswitch/packets.h"
27 #include "openvswitch/types.h"
28 #include "odp-netlink.h"
31 #include "tun-metadata.h"
32 #include "unaligned.h"
38 /* Purely internal to OVS userspace. These flags should never be exposed to
39 * the outside world and so aren't included in the flags mask. */
41 /* Tunnel information is in userspace datapath format. */
42 #define FLOW_TNL_F_UDPIF (1 << 4)
44 static inline bool ipv6_addr_is_set(const struct in6_addr
*addr
);
47 flow_tnl_dst_is_set(const struct flow_tnl
*tnl
)
49 return tnl
->ip_dst
|| ipv6_addr_is_set(&tnl
->ipv6_dst
);
52 struct in6_addr
flow_tnl_dst(const struct flow_tnl
*tnl
);
53 struct in6_addr
flow_tnl_src(const struct flow_tnl
*tnl
);
55 /* Returns an offset to 'src' covering all the meaningful fields in 'src'. */
57 flow_tnl_size(const struct flow_tnl
*src
)
59 if (!flow_tnl_dst_is_set(src
)) {
60 /* Covers ip_dst and ipv6_dst only. */
61 return offsetof(struct flow_tnl
, ip_src
);
63 if (src
->flags
& FLOW_TNL_F_UDPIF
) {
64 /* Datapath format, cover all options we have. */
65 return offsetof(struct flow_tnl
, metadata
.opts
)
66 + src
->metadata
.present
.len
;
68 if (!src
->metadata
.present
.map
) {
69 /* No TLVs, opts is irrelevant. */
70 return offsetof(struct flow_tnl
, metadata
.opts
);
72 /* Have decoded TLVs, opts is relevant. */
76 /* Copy flow_tnl, but avoid copying unused portions of tun_metadata. Unused
77 * data in 'dst' is NOT cleared, so this must not be used in cases where the
78 * uninitialized portion may be hashed over. */
80 flow_tnl_copy__(struct flow_tnl
*dst
, const struct flow_tnl
*src
)
82 memcpy(dst
, src
, flow_tnl_size(src
));
86 flow_tnl_equal(const struct flow_tnl
*a
, const struct flow_tnl
*b
)
88 size_t a_size
= flow_tnl_size(a
);
90 return a_size
== flow_tnl_size(b
) && !memcmp(a
, b
, a_size
);
93 /* Datapath packet metadata */
95 uint32_t recirc_id
; /* Recirculation id carried with the
96 recirculating packets. 0 for packets
97 received from the wire. */
98 uint32_t dp_hash
; /* hash value computed by the recirculation
100 uint32_t skb_priority
; /* Packet priority for QoS. */
101 uint32_t pkt_mark
; /* Packet mark. */
102 uint16_t ct_state
; /* Connection state. */
103 uint16_t ct_zone
; /* Connection zone. */
104 uint32_t ct_mark
; /* Connection mark. */
105 ovs_u128 ct_label
; /* Connection label. */
106 union flow_in_port in_port
; /* Input port. */
107 struct flow_tnl tunnel
; /* Encapsulating tunnel parameters. Note that
108 * if 'ip_dst' == 0, the rest of the fields may
109 * be uninitialized. */
113 pkt_metadata_init_tnl(struct pkt_metadata
*md
)
115 /* Zero up through the tunnel metadata options. The length and table
116 * are before this and as long as they are empty, the options won't
118 memset(md
, 0, offsetof(struct pkt_metadata
, tunnel
.metadata
.opts
));
122 pkt_metadata_init(struct pkt_metadata
*md
, odp_port_t port
)
124 /* It can be expensive to zero out all of the tunnel metadata. However,
125 * we can just zero out ip_dst and the rest of the data will never be
127 memset(md
, 0, offsetof(struct pkt_metadata
, in_port
));
128 md
->tunnel
.ip_dst
= 0;
129 md
->tunnel
.ipv6_dst
= in6addr_any
;
131 md
->in_port
.odp_port
= port
;
134 /* This function prefetches the cachelines touched by pkt_metadata_init()
135 * For performance reasons the two functions should be kept in sync. */
137 pkt_metadata_prefetch_init(struct pkt_metadata
*md
)
139 ovs_prefetch_range(md
, offsetof(struct pkt_metadata
, tunnel
.ip_src
));
142 bool dpid_from_string(const char *s
, uint64_t *dpidp
);
144 #define ETH_ADDR_LEN 6
146 static const struct eth_addr eth_addr_broadcast OVS_UNUSED
147 = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
149 static const struct eth_addr eth_addr_exact OVS_UNUSED
150 = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
152 static const struct eth_addr eth_addr_zero OVS_UNUSED
153 = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } };
155 static const struct eth_addr eth_addr_stp OVS_UNUSED
156 = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 } } };
158 static const struct eth_addr eth_addr_lacp OVS_UNUSED
159 = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 } } };
161 static const struct eth_addr eth_addr_bfd OVS_UNUSED
162 = { { { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 } } };
164 static inline bool eth_addr_is_broadcast(const struct eth_addr a
)
166 return (a
.be16
[0] & a
.be16
[1] & a
.be16
[2]) == htons(0xffff);
169 static inline bool eth_addr_is_multicast(const struct eth_addr a
)
174 static inline bool eth_addr_is_local(const struct eth_addr a
)
176 /* Local if it is either a locally administered address or a Nicira random
179 || (a
.be16
[0] == htons(0x0023)
180 && (a
.be16
[1] & htons(0xff80)) == htons(0x2080));
182 static inline bool eth_addr_is_zero(const struct eth_addr a
)
184 return !(a
.be16
[0] | a
.be16
[1] | a
.be16
[2]);
187 static inline int eth_mask_is_exact(const struct eth_addr a
)
189 return (a
.be16
[0] & a
.be16
[1] & a
.be16
[2]) == htons(0xffff);
192 static inline int eth_addr_compare_3way(const struct eth_addr a
,
193 const struct eth_addr b
)
195 return memcmp(&a
, &b
, sizeof a
);
198 static inline bool eth_addr_equals(const struct eth_addr a
,
199 const struct eth_addr b
)
201 return !eth_addr_compare_3way(a
, b
);
204 static inline bool eth_addr_equal_except(const struct eth_addr a
,
205 const struct eth_addr b
,
206 const struct eth_addr mask
)
208 return !(((a
.be16
[0] ^ b
.be16
[0]) & mask
.be16
[0])
209 || ((a
.be16
[1] ^ b
.be16
[1]) & mask
.be16
[1])
210 || ((a
.be16
[2] ^ b
.be16
[2]) & mask
.be16
[2]));
213 static inline uint64_t eth_addr_to_uint64(const struct eth_addr ea
)
215 return (((uint64_t) ntohs(ea
.be16
[0]) << 32)
216 | ((uint64_t) ntohs(ea
.be16
[1]) << 16)
217 | ntohs(ea
.be16
[2]));
220 static inline uint64_t eth_addr_vlan_to_uint64(const struct eth_addr ea
,
223 return (((uint64_t)vlan
<< 48) | eth_addr_to_uint64(ea
));
226 static inline void eth_addr_from_uint64(uint64_t x
, struct eth_addr
*ea
)
228 ea
->be16
[0] = htons(x
>> 32);
229 ea
->be16
[1] = htons((x
& 0xFFFF0000) >> 16);
230 ea
->be16
[2] = htons(x
& 0xFFFF);
233 static inline struct eth_addr
eth_addr_invert(const struct eth_addr src
)
237 for (int i
= 0; i
< ARRAY_SIZE(src
.be16
); i
++) {
238 dst
.be16
[i
] = ~src
.be16
[i
];
244 static inline void eth_addr_mark_random(struct eth_addr
*ea
)
246 ea
->ea
[0] &= ~1; /* Unicast. */
247 ea
->ea
[0] |= 2; /* Private. */
250 static inline void eth_addr_random(struct eth_addr
*ea
)
252 random_bytes((uint8_t *)ea
, sizeof *ea
);
253 eth_addr_mark_random(ea
);
256 static inline void eth_addr_nicira_random(struct eth_addr
*ea
)
260 /* Set the OUI to the Nicira one. */
265 /* Set the top bit to indicate random Nicira address. */
268 static inline uint32_t hash_mac(const struct eth_addr ea
,
269 const uint16_t vlan
, const uint32_t basis
)
271 return hash_uint64_basis(eth_addr_vlan_to_uint64(ea
, vlan
), basis
);
274 bool eth_addr_is_reserved(const struct eth_addr
);
275 bool eth_addr_from_string(const char *, struct eth_addr
*);
277 void compose_rarp(struct dp_packet
*, const struct eth_addr
);
279 void eth_push_vlan(struct dp_packet
*, ovs_be16 tpid
, ovs_be16 tci
);
280 void eth_pop_vlan(struct dp_packet
*);
282 const char *eth_from_hex(const char *hex
, struct dp_packet
**packetp
);
283 void eth_format_masked(const struct eth_addr ea
,
284 const struct eth_addr
*mask
, struct ds
*s
);
286 void set_mpls_lse(struct dp_packet
*, ovs_be32 label
);
287 void push_mpls(struct dp_packet
*packet
, ovs_be16 ethtype
, ovs_be32 lse
);
288 void pop_mpls(struct dp_packet
*, ovs_be16 ethtype
);
290 void set_mpls_lse_ttl(ovs_be32
*lse
, uint8_t ttl
);
291 void set_mpls_lse_tc(ovs_be32
*lse
, uint8_t tc
);
292 void set_mpls_lse_label(ovs_be32
*lse
, ovs_be32 label
);
293 void set_mpls_lse_bos(ovs_be32
*lse
, uint8_t bos
);
294 ovs_be32
set_mpls_lse_values(uint8_t ttl
, uint8_t tc
, uint8_t bos
,
299 * struct eth_addr mac;
301 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
304 #define ETH_ADDR_FMT \
305 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
306 #define ETH_ADDR_ARGS(EA) ETH_ADDR_BYTES_ARGS((EA).ea)
307 #define ETH_ADDR_BYTES_ARGS(EAB) \
308 (EAB)[0], (EAB)[1], (EAB)[2], (EAB)[3], (EAB)[4], (EAB)[5]
309 #define ETH_ADDR_STRLEN 17
313 * char *string = "1 00:11:22:33:44:55 2";
314 * struct eth_addr mac;
317 * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
318 * &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
322 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
323 #define ETH_ADDR_SCAN_ARGS(EA) \
324 &(EA).ea[0], &(EA).ea[1], &(EA).ea[2], &(EA).ea[3], &(EA).ea[4], &(EA).ea[5]
326 #define ETH_TYPE_IP 0x0800
327 #define ETH_TYPE_ARP 0x0806
328 #define ETH_TYPE_TEB 0x6558
329 #define ETH_TYPE_VLAN_8021Q 0x8100
330 #define ETH_TYPE_VLAN ETH_TYPE_VLAN_8021Q
331 #define ETH_TYPE_VLAN_8021AD 0x88a8
332 #define ETH_TYPE_IPV6 0x86dd
333 #define ETH_TYPE_LACP 0x8809
334 #define ETH_TYPE_RARP 0x8035
335 #define ETH_TYPE_MPLS 0x8847
336 #define ETH_TYPE_MPLS_MCAST 0x8848
338 static inline bool eth_type_mpls(ovs_be16 eth_type
)
340 return eth_type
== htons(ETH_TYPE_MPLS
) ||
341 eth_type
== htons(ETH_TYPE_MPLS_MCAST
);
344 static inline bool eth_type_vlan(ovs_be16 eth_type
)
346 return eth_type
== htons(ETH_TYPE_VLAN_8021Q
) ||
347 eth_type
== htons(ETH_TYPE_VLAN_8021AD
);
351 /* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame
353 #define ETH_TYPE_MIN 0x600
355 #define ETH_HEADER_LEN 14
356 #define ETH_PAYLOAD_MIN 46
357 #define ETH_PAYLOAD_MAX 1500
358 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
359 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
360 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
363 struct eth_addr eth_dst
;
364 struct eth_addr eth_src
;
367 BUILD_ASSERT_DECL(ETH_HEADER_LEN
== sizeof(struct eth_header
));
369 #define LLC_DSAP_SNAP 0xaa
370 #define LLC_SSAP_SNAP 0xaa
371 #define LLC_CNTL_SNAP 3
373 #define LLC_HEADER_LEN 3
380 BUILD_ASSERT_DECL(LLC_HEADER_LEN
== sizeof(struct llc_header
));
382 /* LLC field values used for STP frames. */
383 #define STP_LLC_SSAP 0x42
384 #define STP_LLC_DSAP 0x42
385 #define STP_LLC_CNTL 0x03
387 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
388 sizeof(SNAP_ORG_ETHERNET) == 3. */
389 #define SNAP_HEADER_LEN 5
395 BUILD_ASSERT_DECL(SNAP_HEADER_LEN
== sizeof(struct snap_header
));
397 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
399 struct llc_snap_header
{
400 struct llc_header llc
;
401 struct snap_header snap
;
403 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN
== sizeof(struct llc_snap_header
));
405 #define VLAN_VID_MASK 0x0fff
406 #define VLAN_VID_SHIFT 0
408 #define VLAN_PCP_MASK 0xe000
409 #define VLAN_PCP_SHIFT 13
411 #define VLAN_CFI 0x1000
412 #define VLAN_CFI_SHIFT 12
414 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
415 * returns the VLAN ID in host byte order. */
416 static inline uint16_t
417 vlan_tci_to_vid(ovs_be16 vlan_tci
)
419 return (ntohs(vlan_tci
) & VLAN_VID_MASK
) >> VLAN_VID_SHIFT
;
422 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
423 * returns the priority code point (PCP) in host byte order. */
425 vlan_tci_to_pcp(ovs_be16 vlan_tci
)
427 return (ntohs(vlan_tci
) & VLAN_PCP_MASK
) >> VLAN_PCP_SHIFT
;
430 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
431 * returns the Canonical Format Indicator (CFI). */
433 vlan_tci_to_cfi(ovs_be16 vlan_tci
)
435 return (vlan_tci
& htons(VLAN_CFI
)) != 0;
438 #define VLAN_HEADER_LEN 4
440 ovs_be16 vlan_tci
; /* Lowest 12 bits are VLAN ID. */
441 ovs_be16 vlan_next_type
;
443 BUILD_ASSERT_DECL(VLAN_HEADER_LEN
== sizeof(struct vlan_header
));
445 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
447 struct vlan_eth_header
{
448 struct eth_addr veth_dst
;
449 struct eth_addr veth_src
;
450 ovs_be16 veth_type
; /* Always htons(ETH_TYPE_VLAN). */
451 ovs_be16 veth_tci
; /* Lowest 12 bits are VLAN ID. */
452 ovs_be16 veth_next_type
;
454 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN
== sizeof(struct vlan_eth_header
));
456 /* MPLS related definitions */
457 #define MPLS_TTL_MASK 0x000000ff
458 #define MPLS_TTL_SHIFT 0
460 #define MPLS_BOS_MASK 0x00000100
461 #define MPLS_BOS_SHIFT 8
463 #define MPLS_TC_MASK 0x00000e00
464 #define MPLS_TC_SHIFT 9
466 #define MPLS_LABEL_MASK 0xfffff000
467 #define MPLS_LABEL_SHIFT 12
472 ovs_16aligned_be32 mpls_lse
;
474 BUILD_ASSERT_DECL(MPLS_HLEN
== sizeof(struct mpls_hdr
));
476 /* Given a mpls label stack entry in network byte order
477 * return mpls label in host byte order */
478 static inline uint32_t
479 mpls_lse_to_label(ovs_be32 mpls_lse
)
481 return (ntohl(mpls_lse
) & MPLS_LABEL_MASK
) >> MPLS_LABEL_SHIFT
;
484 /* Given a mpls label stack entry in network byte order
486 static inline uint8_t
487 mpls_lse_to_tc(ovs_be32 mpls_lse
)
489 return (ntohl(mpls_lse
) & MPLS_TC_MASK
) >> MPLS_TC_SHIFT
;
492 /* Given a mpls label stack entry in network byte order
494 static inline uint8_t
495 mpls_lse_to_ttl(ovs_be32 mpls_lse
)
497 return (ntohl(mpls_lse
) & MPLS_TTL_MASK
) >> MPLS_TTL_SHIFT
;
500 /* Set TTL in mpls lse. */
502 flow_set_mpls_lse_ttl(ovs_be32
*mpls_lse
, uint8_t ttl
)
504 *mpls_lse
&= ~htonl(MPLS_TTL_MASK
);
505 *mpls_lse
|= htonl(ttl
<< MPLS_TTL_SHIFT
);
508 /* Given a mpls label stack entry in network byte order
509 * return mpls BoS bit */
510 static inline uint8_t
511 mpls_lse_to_bos(ovs_be32 mpls_lse
)
513 return (mpls_lse
& htonl(MPLS_BOS_MASK
)) != 0;
516 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
517 #define IP_ARGS(ip) \
519 (ntohl(ip) >> 16) & 0xff, \
520 (ntohl(ip) >> 8) & 0xff, \
525 * char *string = "1 33.44.55.66 2";
529 * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
533 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
534 #define IP_SCAN_ARGS(ip) \
535 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
536 &((uint8_t *) ip)[1], \
537 &((uint8_t *) ip)[2], \
540 #define IP_PORT_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8":%"SCNu16
541 #define IP_PORT_SCAN_ARGS(ip, port) \
542 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
543 &((uint8_t *) ip)[1], \
544 &((uint8_t *) ip)[2], \
545 &((uint8_t *) ip)[3], \
546 ((void) (ovs_be16) *(port), (uint16_t *) port)
548 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
549 * high-order 1-bits and 32-N low-order 0-bits. */
551 ip_is_cidr(ovs_be32 netmask
)
553 uint32_t x
= ~ntohl(netmask
);
554 return !(x
& (x
+ 1));
557 ip_is_multicast(ovs_be32 ip
)
559 return (ip
& htonl(0xf0000000)) == htonl(0xe0000000);
562 ip_is_local_multicast(ovs_be32 ip
)
564 return (ip
& htonl(0xffffff00)) == htonl(0xe0000000);
566 int ip_count_cidr_bits(ovs_be32 netmask
);
567 void ip_format_masked(ovs_be32 ip
, ovs_be32 mask
, struct ds
*);
568 bool ip_parse(const char *s
, ovs_be32
*ip
);
569 char *ip_parse_port(const char *s
, ovs_be32
*ip
, ovs_be16
*port
)
570 OVS_WARN_UNUSED_RESULT
;
571 char *ip_parse_masked(const char *s
, ovs_be32
*ip
, ovs_be32
*mask
)
572 OVS_WARN_UNUSED_RESULT
;
573 char *ip_parse_cidr(const char *s
, ovs_be32
*ip
, unsigned int *plen
)
574 OVS_WARN_UNUSED_RESULT
;
575 char *ip_parse_masked_len(const char *s
, int *n
, ovs_be32
*ip
, ovs_be32
*mask
)
576 OVS_WARN_UNUSED_RESULT
;
577 char *ip_parse_cidr_len(const char *s
, int *n
, ovs_be32
*ip
,
579 OVS_WARN_UNUSED_RESULT
;
581 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
582 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
583 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
586 #define IPPROTO_SCTP 132
590 #define IP_ECN_NOT_ECT 0x0
591 #define IP_ECN_ECT_1 0x01
592 #define IP_ECN_ECT_0 0x02
593 #define IP_ECN_CE 0x03
594 #define IP_ECN_MASK 0x03
595 #define IP_DSCP_MASK 0xfc
598 IP_ECN_is_ce(uint8_t dsfield
)
600 return (dsfield
& IP_ECN_MASK
) == IP_ECN_CE
;
605 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
606 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
607 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
608 #define IP_IS_FRAGMENT(ip_frag_off) \
609 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
611 #define IP_HEADER_LEN 20
617 ovs_be16 ip_frag_off
;
621 ovs_16aligned_be32 ip_src
;
622 ovs_16aligned_be32 ip_dst
;
624 BUILD_ASSERT_DECL(IP_HEADER_LEN
== sizeof(struct ip_header
));
627 #define ICMP4_ECHO_REPLY 0
628 #define ICMP4_DST_UNREACH 3
629 #define ICMP4_SOURCEQUENCH 4
630 #define ICMP4_REDIRECT 5
631 #define ICMP4_ECHO_REQUEST 8
632 #define ICMP4_TIME_EXCEEDED 11
633 #define ICMP4_PARAM_PROB 12
634 #define ICMP4_TIMESTAMP 13
635 #define ICMP4_TIMESTAMPREPLY 14
636 #define ICMP4_INFOREQUEST 15
637 #define ICMP4_INFOREPLY 16
639 #define ICMP_HEADER_LEN 8
653 ovs_16aligned_be32 gateway
;
656 BUILD_ASSERT_DECL(ICMP_HEADER_LEN
== sizeof(struct icmp_header
));
658 #define IGMP_HEADER_LEN 8
663 ovs_16aligned_be32 group
;
665 BUILD_ASSERT_DECL(IGMP_HEADER_LEN
== sizeof(struct igmp_header
));
667 #define IGMPV3_HEADER_LEN 8
668 struct igmpv3_header
{
675 BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN
== sizeof(struct igmpv3_header
));
677 #define IGMPV3_RECORD_LEN 8
678 struct igmpv3_record
{
682 ovs_16aligned_be32 maddr
;
684 BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN
== sizeof(struct igmpv3_record
));
686 #define IGMP_HOST_MEMBERSHIP_QUERY 0x11 /* From RFC1112 */
687 #define IGMP_HOST_MEMBERSHIP_REPORT 0x12 /* Ditto */
688 #define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */
689 #define IGMP_HOST_LEAVE_MESSAGE 0x17
690 #define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */
693 * IGMPv3 and MLDv2 use the same codes.
695 #define IGMPV3_MODE_IS_INCLUDE 1
696 #define IGMPV3_MODE_IS_EXCLUDE 2
697 #define IGMPV3_CHANGE_TO_INCLUDE_MODE 3
698 #define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4
699 #define IGMPV3_ALLOW_NEW_SOURCES 5
700 #define IGMPV3_BLOCK_OLD_SOURCES 6
702 #define SCTP_HEADER_LEN 12
706 ovs_16aligned_be32 sctp_vtag
;
707 ovs_16aligned_be32 sctp_csum
;
709 BUILD_ASSERT_DECL(SCTP_HEADER_LEN
== sizeof(struct sctp_header
));
711 #define UDP_HEADER_LEN 8
718 BUILD_ASSERT_DECL(UDP_HEADER_LEN
== sizeof(struct udp_header
));
720 #define TCP_FIN 0x001
721 #define TCP_SYN 0x002
722 #define TCP_RST 0x004
723 #define TCP_PSH 0x008
724 #define TCP_ACK 0x010
725 #define TCP_URG 0x020
726 #define TCP_ECE 0x040
727 #define TCP_CWR 0x080
730 #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
731 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
732 #define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff))
733 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
735 #define TCP_HEADER_LEN 20
739 ovs_16aligned_be32 tcp_seq
;
740 ovs_16aligned_be32 tcp_ack
;
746 BUILD_ASSERT_DECL(TCP_HEADER_LEN
== sizeof(struct tcp_header
));
748 /* Connection states */
751 CS_ESTABLISHED_BIT
= 1,
753 CS_REPLY_DIR_BIT
= 3,
761 CS_NEW
= (1 << CS_NEW_BIT
),
762 CS_ESTABLISHED
= (1 << CS_ESTABLISHED_BIT
),
763 CS_RELATED
= (1 << CS_RELATED_BIT
),
764 CS_REPLY_DIR
= (1 << CS_REPLY_DIR_BIT
),
765 CS_INVALID
= (1 << CS_INVALID_BIT
),
766 CS_TRACKED
= (1 << CS_TRACKED_BIT
),
767 CS_SRC_NAT
= (1 << CS_SRC_NAT_BIT
),
768 CS_DST_NAT
= (1 << CS_DST_NAT_BIT
),
771 /* Undefined connection state bits. */
772 #define CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
773 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
774 | CS_SRC_NAT | CS_DST_NAT)
775 #define CS_UNSUPPORTED_MASK (~(uint32_t)CS_SUPPORTED_MASK)
777 #define ARP_HRD_ETHERNET 1
778 #define ARP_PRO_IP 0x0800
779 #define ARP_OP_REQUEST 1
780 #define ARP_OP_REPLY 2
781 #define ARP_OP_RARP 3
783 #define ARP_ETH_HEADER_LEN 28
784 struct arp_eth_header
{
785 /* Generic members. */
786 ovs_be16 ar_hrd
; /* Hardware type. */
787 ovs_be16 ar_pro
; /* Protocol type. */
788 uint8_t ar_hln
; /* Hardware address length. */
789 uint8_t ar_pln
; /* Protocol address length. */
790 ovs_be16 ar_op
; /* Opcode. */
792 /* Ethernet+IPv4 specific members. */
793 struct eth_addr ar_sha
; /* Sender hardware address. */
794 ovs_16aligned_be32 ar_spa
; /* Sender protocol address. */
795 struct eth_addr ar_tha
; /* Target hardware address. */
796 ovs_16aligned_be32 ar_tpa
; /* Target protocol address. */
798 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN
== sizeof(struct arp_eth_header
));
800 #define IPV6_HEADER_LEN 40
802 /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
803 * most implementations, this one only requires 16-bit alignment. */
804 union ovs_16aligned_in6_addr
{
806 ovs_16aligned_be32 be32
[4];
809 /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
810 * one only requires 16-bit alignment. */
811 struct ovs_16aligned_ip6_hdr
{
813 struct ovs_16aligned_ip6_hdrctl
{
814 ovs_16aligned_be32 ip6_un1_flow
;
815 ovs_be16 ip6_un1_plen
;
817 uint8_t ip6_un1_hlim
;
821 union ovs_16aligned_in6_addr ip6_src
;
822 union ovs_16aligned_in6_addr ip6_dst
;
825 /* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
826 * this one only requires 16-bit alignment. */
827 struct ovs_16aligned_ip6_frag
{
829 uint8_t ip6f_reserved
;
831 ovs_16aligned_be32 ip6f_ident
;
834 #define ICMP6_HEADER_LEN 4
835 struct icmp6_header
{
838 ovs_be16 icmp6_cksum
;
840 BUILD_ASSERT_DECL(ICMP6_HEADER_LEN
== sizeof(struct icmp6_header
));
842 uint32_t packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr
*);
843 uint16_t packet_csum_upperlayer6(const struct ovs_16aligned_ip6_hdr
*,
844 const void *, uint8_t, uint16_t);
846 /* Neighbor Discovery option field.
847 * ND options are always a multiple of 8 bytes in size. */
850 uint8_t nd_opt_type
; /* Values defined in icmp6.h */
851 uint8_t nd_opt_len
; /* in units of 8 octets (the size of this struct) */
852 struct eth_addr nd_opt_mac
; /* Ethernet address in the case of SLL or TLL options */
854 BUILD_ASSERT_DECL(ND_OPT_LEN
== sizeof(struct ovs_nd_opt
));
856 /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit
857 * alignment, this one only requires 16-bit alignment. */
858 #define ND_MSG_LEN 24
860 struct icmp6_header icmph
;
861 ovs_16aligned_be32 rso_flags
;
862 union ovs_16aligned_in6_addr target
;
863 struct ovs_nd_opt options
[0];
865 BUILD_ASSERT_DECL(ND_MSG_LEN
== sizeof(struct ovs_nd_msg
));
867 #define ND_RSO_ROUTER 0x80000000
868 #define ND_RSO_SOLICITED 0x40000000
869 #define ND_RSO_OVERRIDE 0x20000000
872 * Use the same struct for MLD and MLD2, naming members as the defined fields in
873 * in the corresponding version of the protocol, though they are reserved in the
876 #define MLD_HEADER_LEN 8
884 BUILD_ASSERT_DECL(MLD_HEADER_LEN
== sizeof(struct mld_header
));
886 #define MLD2_RECORD_LEN 20
891 union ovs_16aligned_in6_addr maddr
;
893 BUILD_ASSERT_DECL(MLD2_RECORD_LEN
== sizeof(struct mld2_record
));
895 #define MLD_QUERY 130
896 #define MLD_REPORT 131
898 #define MLD2_REPORT 143
900 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
901 #define IPV6_LABEL_MASK 0x000fffff
905 * char *string = "1 ::1 2";
906 * char ipv6_s[IPV6_SCAN_LEN + 1];
907 * struct in6_addr ipv6;
909 * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
910 * && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
914 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
915 #define IPV6_SCAN_LEN 46
917 extern const struct in6_addr in6addr_exact
;
918 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
919 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
921 extern const struct in6_addr in6addr_all_hosts
;
922 #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
923 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } }
925 static inline bool ipv6_addr_equals(const struct in6_addr
*a
,
926 const struct in6_addr
*b
)
928 #ifdef IN6_ARE_ADDR_EQUAL
929 return IN6_ARE_ADDR_EQUAL(a
, b
);
931 return !memcmp(a
, b
, sizeof(*a
));
935 /* Checks the IPv6 address in 'mask' for all zeroes. */
936 static inline bool ipv6_mask_is_any(const struct in6_addr
*mask
) {
937 return ipv6_addr_equals(mask
, &in6addr_any
);
940 static inline bool ipv6_mask_is_exact(const struct in6_addr
*mask
) {
941 return ipv6_addr_equals(mask
, &in6addr_exact
);
944 static inline bool ipv6_is_all_hosts(const struct in6_addr
*addr
) {
945 return ipv6_addr_equals(addr
, &in6addr_all_hosts
);
948 static inline bool ipv6_addr_is_set(const struct in6_addr
*addr
) {
949 return !ipv6_addr_equals(addr
, &in6addr_any
);
952 static inline bool ipv6_addr_is_multicast(const struct in6_addr
*ip
) {
953 return ip
->s6_addr
[0] == 0xff;
956 static inline struct in6_addr
957 in6_addr_mapped_ipv4(ovs_be32 ip4
)
959 struct in6_addr ip6
= { .s6_addr
= { [10] = 0xff, [11] = 0xff } };
960 memcpy(&ip6
.s6_addr
[12], &ip4
, 4);
965 in6_addr_set_mapped_ipv4(struct in6_addr
*ip6
, ovs_be32 ip4
)
967 *ip6
= in6_addr_mapped_ipv4(ip4
);
970 static inline ovs_be32
971 in6_addr_get_mapped_ipv4(const struct in6_addr
*addr
)
973 union ovs_16aligned_in6_addr
*taddr
= (void *) addr
;
974 if (IN6_IS_ADDR_V4MAPPED(addr
)) {
975 return get_16aligned_be32(&taddr
->be32
[3]);
982 in6_addr_solicited_node(struct in6_addr
*addr
, const struct in6_addr
*ip6
)
984 union ovs_16aligned_in6_addr
*taddr
= (void *) addr
;
985 memset(taddr
->be16
, 0, sizeof(taddr
->be16
));
986 taddr
->be16
[0] = htons(0xff02);
987 taddr
->be16
[5] = htons(0x1);
988 taddr
->be16
[6] = htons(0xff00);
989 memcpy(&addr
->s6_addr
[13], &ip6
->s6_addr
[13], 3);
993 * Generates ipv6 link local address from the given eth addr
994 * with prefix 'fe80::/64' and stores it in 'lla'
997 in6_generate_lla(struct eth_addr ea
, struct in6_addr
*lla
)
999 union ovs_16aligned_in6_addr
*taddr
= (void *) lla
;
1000 memset(taddr
->be16
, 0, sizeof(taddr
->be16
));
1001 taddr
->be16
[0] = htons(0xfe80);
1002 taddr
->be16
[4] = htons(((ea
.ea
[0] ^ 0x02) << 8) | ea
.ea
[1]);
1003 taddr
->be16
[5] = htons(ea
.ea
[2] << 8 | 0x00ff);
1004 taddr
->be16
[6] = htons(0xfe << 8 | ea
.ea
[3]);
1005 taddr
->be16
[7] = ea
.be16
[2];
1008 /* Returns true if 'addr' is a link local address. Otherwise, false. */
1010 in6_is_lla(struct in6_addr
*addr
)
1013 return addr
->s6_addr32
[0] == htonl(0xfe800000) && !(addr
->s6_addr32
[1]);
1015 return addr
->s6_addr
[0] == 0xfe && addr
->s6_addr
[1] == 0x80 &&
1016 !(addr
->s6_addr
[2] | addr
->s6_addr
[3] | addr
->s6_addr
[4] |
1017 addr
->s6_addr
[5] | addr
->s6_addr
[6] | addr
->s6_addr
[7]);
1022 ipv6_multicast_to_ethernet(struct eth_addr
*eth
, const struct in6_addr
*ip6
)
1026 eth
->ea
[2] = ip6
->s6_addr
[12];
1027 eth
->ea
[3] = ip6
->s6_addr
[13];
1028 eth
->ea
[4] = ip6
->s6_addr
[14];
1029 eth
->ea
[5] = ip6
->s6_addr
[15];
1032 static inline bool dl_type_is_ip_any(ovs_be16 dl_type
)
1034 return dl_type
== htons(ETH_TYPE_IP
)
1035 || dl_type
== htons(ETH_TYPE_IPV6
);
1040 /* GRE protocol header */
1041 struct gre_base_hdr
{
1046 #define GRE_CSUM 0x8000
1047 #define GRE_ROUTING 0x4000
1048 #define GRE_KEY 0x2000
1049 #define GRE_SEQ 0x1000
1050 #define GRE_STRICT 0x0800
1051 #define GRE_REC 0x0700
1052 #define GRE_FLAGS 0x00F8
1053 #define GRE_VERSION 0x0007
1055 /* VXLAN protocol header */
1057 ovs_16aligned_be32 vx_flags
;
1058 ovs_16aligned_be32 vx_vni
;
1061 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
1063 void ipv6_format_addr(const struct in6_addr
*addr
, struct ds
*);
1064 void ipv6_format_addr_bracket(const struct in6_addr
*addr
, struct ds
*,
1066 void ipv6_format_mapped(const struct in6_addr
*addr
, struct ds
*);
1067 void ipv6_format_masked(const struct in6_addr
*addr
,
1068 const struct in6_addr
*mask
, struct ds
*);
1069 const char * ipv6_string_mapped(char *addr_str
, const struct in6_addr
*addr
);
1070 struct in6_addr
ipv6_addr_bitand(const struct in6_addr
*src
,
1071 const struct in6_addr
*mask
);
1072 struct in6_addr
ipv6_addr_bitxor(const struct in6_addr
*a
,
1073 const struct in6_addr
*b
);
1074 bool ipv6_is_zero(const struct in6_addr
*a
);
1075 struct in6_addr
ipv6_create_mask(int mask
);
1076 int ipv6_count_cidr_bits(const struct in6_addr
*netmask
);
1077 bool ipv6_is_cidr(const struct in6_addr
*netmask
);
1079 bool ipv6_parse(const char *s
, struct in6_addr
*ip
);
1080 char *ipv6_parse_masked(const char *s
, struct in6_addr
*ipv6
,
1081 struct in6_addr
*mask
);
1082 char *ipv6_parse_cidr(const char *s
, struct in6_addr
*ip
, unsigned int *plen
)
1083 OVS_WARN_UNUSED_RESULT
;
1084 char *ipv6_parse_masked_len(const char *s
, int *n
, struct in6_addr
*ipv6
,
1085 struct in6_addr
*mask
);
1086 char *ipv6_parse_cidr_len(const char *s
, int *n
, struct in6_addr
*ip
,
1088 OVS_WARN_UNUSED_RESULT
;
1090 void *eth_compose(struct dp_packet
*, const struct eth_addr eth_dst
,
1091 const struct eth_addr eth_src
, uint16_t eth_type
,
1093 void *snap_compose(struct dp_packet
*, const struct eth_addr eth_dst
,
1094 const struct eth_addr eth_src
,
1095 unsigned int oui
, uint16_t snap_type
, size_t size
);
1096 void packet_set_ipv4(struct dp_packet
*, ovs_be32 src
, ovs_be32 dst
, uint8_t tos
,
1098 void packet_set_ipv4_addr(struct dp_packet
*packet
, ovs_16aligned_be32
*addr
,
1100 void packet_set_ipv6(struct dp_packet
*, const ovs_be32 src
[4],
1101 const ovs_be32 dst
[4], uint8_t tc
,
1102 ovs_be32 fl
, uint8_t hlmit
);
1103 void packet_set_tcp_port(struct dp_packet
*, ovs_be16 src
, ovs_be16 dst
);
1104 void packet_set_udp_port(struct dp_packet
*, ovs_be16 src
, ovs_be16 dst
);
1105 void packet_set_sctp_port(struct dp_packet
*, ovs_be16 src
, ovs_be16 dst
);
1106 void packet_set_icmp(struct dp_packet
*, uint8_t type
, uint8_t code
);
1107 void packet_set_nd(struct dp_packet
*, const ovs_be32 target
[4],
1108 const struct eth_addr sll
, const struct eth_addr tll
);
1110 void packet_format_tcp_flags(struct ds
*, uint16_t);
1111 const char *packet_tcp_flag_to_string(uint32_t flag
);
1112 void compose_arp__(struct dp_packet
*);
1113 void compose_arp(struct dp_packet
*, uint16_t arp_op
,
1114 const struct eth_addr arp_sha
,
1115 const struct eth_addr arp_tha
, bool broadcast
,
1116 ovs_be32 arp_spa
, ovs_be32 arp_tpa
);
1117 void compose_nd_ns(struct dp_packet
*, const struct eth_addr eth_src
,
1118 const struct in6_addr
*ipv6_src
,
1119 const struct in6_addr
*ipv6_dst
);
1120 void compose_nd_na(struct dp_packet
*, const struct eth_addr eth_src
,
1121 const struct eth_addr eth_dst
,
1122 const struct in6_addr
*ipv6_src
,
1123 const struct in6_addr
*ipv6_dst
,
1124 ovs_be32 rso_flags
);
1125 uint32_t packet_csum_pseudoheader(const struct ip_header
*);
1126 void IP_ECN_set_ce(struct dp_packet
*pkt
, bool is_ipv6
);
1128 #endif /* packets.h */