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(struct pkt_metadata *md, odp_port_t port)
115 /* It can be expensive to zero out all of the tunnel metadata. However,
116 * we can just zero out ip_dst and the rest of the data will never be
118 memset(md, 0, offsetof(struct pkt_metadata, in_port));
119 md->tunnel.ip_dst = 0;
120 md->tunnel.ipv6_dst = in6addr_any;
122 md->in_port.odp_port = port;
125 /* This function prefetches the cachelines touched by pkt_metadata_init()
126 * For performance reasons the two functions should be kept in sync. */
128 pkt_metadata_prefetch_init(struct pkt_metadata *md)
130 ovs_prefetch_range(md, offsetof(struct pkt_metadata, tunnel.ip_src));
133 bool dpid_from_string(const char *s, uint64_t *dpidp);
135 #define ETH_ADDR_LEN 6
137 static const struct eth_addr eth_addr_broadcast OVS_UNUSED
138 = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
140 static const struct eth_addr eth_addr_exact OVS_UNUSED
141 = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
143 static const struct eth_addr eth_addr_zero OVS_UNUSED
144 = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } };
146 static const struct eth_addr eth_addr_stp OVS_UNUSED
147 = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 } } };
149 static const struct eth_addr eth_addr_lacp OVS_UNUSED
150 = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 } } };
152 static const struct eth_addr eth_addr_bfd OVS_UNUSED
153 = { { { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 } } };
155 static inline bool eth_addr_is_broadcast(const struct eth_addr a)
157 return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
160 static inline bool eth_addr_is_multicast(const struct eth_addr a)
165 static inline bool eth_addr_is_local(const struct eth_addr a)
167 /* Local if it is either a locally administered address or a Nicira random
170 || (a.be16[0] == htons(0x0023)
171 && (a.be16[1] & htons(0xff80)) == htons(0x2080));
173 static inline bool eth_addr_is_zero(const struct eth_addr a)
175 return !(a.be16[0] | a.be16[1] | a.be16[2]);
178 static inline int eth_mask_is_exact(const struct eth_addr a)
180 return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
183 static inline int eth_addr_compare_3way(const struct eth_addr a,
184 const struct eth_addr b)
186 return memcmp(&a, &b, sizeof a);
189 static inline bool eth_addr_equals(const struct eth_addr a,
190 const struct eth_addr b)
192 return !eth_addr_compare_3way(a, b);
195 static inline bool eth_addr_equal_except(const struct eth_addr a,
196 const struct eth_addr b,
197 const struct eth_addr mask)
199 return !(((a.be16[0] ^ b.be16[0]) & mask.be16[0])
200 || ((a.be16[1] ^ b.be16[1]) & mask.be16[1])
201 || ((a.be16[2] ^ b.be16[2]) & mask.be16[2]));
204 static inline uint64_t eth_addr_to_uint64(const struct eth_addr ea)
206 return (((uint64_t) ntohs(ea.be16[0]) << 32)
207 | ((uint64_t) ntohs(ea.be16[1]) << 16)
208 | ntohs(ea.be16[2]));
211 static inline uint64_t eth_addr_vlan_to_uint64(const struct eth_addr ea,
214 return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
217 static inline void eth_addr_from_uint64(uint64_t x, struct eth_addr *ea)
219 ea->be16[0] = htons(x >> 32);
220 ea->be16[1] = htons((x & 0xFFFF0000) >> 16);
221 ea->be16[2] = htons(x & 0xFFFF);
224 static inline struct eth_addr eth_addr_invert(const struct eth_addr src)
228 for (int i = 0; i < ARRAY_SIZE(src.be16); i++) {
229 dst.be16[i] = ~src.be16[i];
235 static inline void eth_addr_mark_random(struct eth_addr *ea)
237 ea->ea[0] &= ~1; /* Unicast. */
238 ea->ea[0] |= 2; /* Private. */
241 static inline void eth_addr_random(struct eth_addr *ea)
243 random_bytes((uint8_t *)ea, sizeof *ea);
244 eth_addr_mark_random(ea);
247 static inline void eth_addr_nicira_random(struct eth_addr *ea)
251 /* Set the OUI to the Nicira one. */
256 /* Set the top bit to indicate random Nicira address. */
259 static inline uint32_t hash_mac(const struct eth_addr ea,
260 const uint16_t vlan, const uint32_t basis)
262 return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
265 bool eth_addr_is_reserved(const struct eth_addr);
266 bool eth_addr_from_string(const char *, struct eth_addr *);
268 void compose_rarp(struct dp_packet *, const struct eth_addr);
270 void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci);
271 void eth_pop_vlan(struct dp_packet *);
273 const char *eth_from_hex(const char *hex, struct dp_packet **packetp);
274 void eth_format_masked(const struct eth_addr ea,
275 const struct eth_addr *mask, struct ds *s);
277 void set_mpls_lse(struct dp_packet *, ovs_be32 label);
278 void push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse);
279 void pop_mpls(struct dp_packet *, ovs_be16 ethtype);
281 void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
282 void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
283 void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
284 void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
285 ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
290 * struct eth_addr mac;
292 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
295 #define ETH_ADDR_FMT \
296 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
297 #define ETH_ADDR_ARGS(EA) ETH_ADDR_BYTES_ARGS((EA).ea)
298 #define ETH_ADDR_BYTES_ARGS(EAB) \
299 (EAB)[0], (EAB)[1], (EAB)[2], (EAB)[3], (EAB)[4], (EAB)[5]
300 #define ETH_ADDR_STRLEN 17
304 * char *string = "1 00:11:22:33:44:55 2";
305 * struct eth_addr mac;
308 * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
309 * &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
313 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
314 #define ETH_ADDR_SCAN_ARGS(EA) \
315 &(EA).ea[0], &(EA).ea[1], &(EA).ea[2], &(EA).ea[3], &(EA).ea[4], &(EA).ea[5]
317 #define ETH_TYPE_IP 0x0800
318 #define ETH_TYPE_ARP 0x0806
319 #define ETH_TYPE_TEB 0x6558
320 #define ETH_TYPE_VLAN_8021Q 0x8100
321 #define ETH_TYPE_VLAN ETH_TYPE_VLAN_8021Q
322 #define ETH_TYPE_VLAN_8021AD 0x88a8
323 #define ETH_TYPE_IPV6 0x86dd
324 #define ETH_TYPE_LACP 0x8809
325 #define ETH_TYPE_RARP 0x8035
326 #define ETH_TYPE_MPLS 0x8847
327 #define ETH_TYPE_MPLS_MCAST 0x8848
329 static inline bool eth_type_mpls(ovs_be16 eth_type)
331 return eth_type == htons(ETH_TYPE_MPLS) ||
332 eth_type == htons(ETH_TYPE_MPLS_MCAST);
335 static inline bool eth_type_vlan(ovs_be16 eth_type)
337 return eth_type == htons(ETH_TYPE_VLAN_8021Q) ||
338 eth_type == htons(ETH_TYPE_VLAN_8021AD);
342 /* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame
344 #define ETH_TYPE_MIN 0x600
346 #define ETH_HEADER_LEN 14
347 #define ETH_PAYLOAD_MIN 46
348 #define ETH_PAYLOAD_MAX 1500
349 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
350 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
351 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
354 struct eth_addr eth_dst;
355 struct eth_addr eth_src;
358 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
360 #define LLC_DSAP_SNAP 0xaa
361 #define LLC_SSAP_SNAP 0xaa
362 #define LLC_CNTL_SNAP 3
364 #define LLC_HEADER_LEN 3
371 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
373 /* LLC field values used for STP frames. */
374 #define STP_LLC_SSAP 0x42
375 #define STP_LLC_DSAP 0x42
376 #define STP_LLC_CNTL 0x03
378 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
379 sizeof(SNAP_ORG_ETHERNET) == 3. */
380 #define SNAP_HEADER_LEN 5
386 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
388 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
390 struct llc_snap_header {
391 struct llc_header llc;
392 struct snap_header snap;
394 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
396 #define VLAN_VID_MASK 0x0fff
397 #define VLAN_VID_SHIFT 0
399 #define VLAN_PCP_MASK 0xe000
400 #define VLAN_PCP_SHIFT 13
402 #define VLAN_CFI 0x1000
403 #define VLAN_CFI_SHIFT 12
405 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
406 * returns the VLAN ID in host byte order. */
407 static inline uint16_t
408 vlan_tci_to_vid(ovs_be16 vlan_tci)
410 return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
413 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
414 * returns the priority code point (PCP) in host byte order. */
416 vlan_tci_to_pcp(ovs_be16 vlan_tci)
418 return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
421 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
422 * returns the Canonical Format Indicator (CFI). */
424 vlan_tci_to_cfi(ovs_be16 vlan_tci)
426 return (vlan_tci & htons(VLAN_CFI)) != 0;
429 #define VLAN_HEADER_LEN 4
431 ovs_be16 vlan_tci; /* Lowest 12 bits are VLAN ID. */
432 ovs_be16 vlan_next_type;
434 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
436 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
438 struct vlan_eth_header {
439 struct eth_addr veth_dst;
440 struct eth_addr veth_src;
441 ovs_be16 veth_type; /* Always htons(ETH_TYPE_VLAN). */
442 ovs_be16 veth_tci; /* Lowest 12 bits are VLAN ID. */
443 ovs_be16 veth_next_type;
445 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
447 /* MPLS related definitions */
448 #define MPLS_TTL_MASK 0x000000ff
449 #define MPLS_TTL_SHIFT 0
451 #define MPLS_BOS_MASK 0x00000100
452 #define MPLS_BOS_SHIFT 8
454 #define MPLS_TC_MASK 0x00000e00
455 #define MPLS_TC_SHIFT 9
457 #define MPLS_LABEL_MASK 0xfffff000
458 #define MPLS_LABEL_SHIFT 12
463 ovs_16aligned_be32 mpls_lse;
465 BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
467 /* Given a mpls label stack entry in network byte order
468 * return mpls label in host byte order */
469 static inline uint32_t
470 mpls_lse_to_label(ovs_be32 mpls_lse)
472 return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
475 /* Given a mpls label stack entry in network byte order
477 static inline uint8_t
478 mpls_lse_to_tc(ovs_be32 mpls_lse)
480 return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
483 /* Given a mpls label stack entry in network byte order
485 static inline uint8_t
486 mpls_lse_to_ttl(ovs_be32 mpls_lse)
488 return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
491 /* Set TTL in mpls lse. */
493 flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
495 *mpls_lse &= ~htonl(MPLS_TTL_MASK);
496 *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
499 /* Given a mpls label stack entry in network byte order
500 * return mpls BoS bit */
501 static inline uint8_t
502 mpls_lse_to_bos(ovs_be32 mpls_lse)
504 return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
507 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
508 #define IP_ARGS(ip) \
510 (ntohl(ip) >> 16) & 0xff, \
511 (ntohl(ip) >> 8) & 0xff, \
516 * char *string = "1 33.44.55.66 2";
520 * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
524 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
525 #define IP_SCAN_ARGS(ip) \
526 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
527 &((uint8_t *) ip)[1], \
528 &((uint8_t *) ip)[2], \
531 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
532 * high-order 1-bits and 32-N low-order 0-bits. */
534 ip_is_cidr(ovs_be32 netmask)
536 uint32_t x = ~ntohl(netmask);
537 return !(x & (x + 1));
540 ip_is_multicast(ovs_be32 ip)
542 return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
545 ip_is_local_multicast(ovs_be32 ip)
547 return (ip & htonl(0xffffff00)) == htonl(0xe0000000);
549 int ip_count_cidr_bits(ovs_be32 netmask);
550 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
551 bool ip_parse(const char *s, ovs_be32 *ip);
552 char *ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
553 OVS_WARN_UNUSED_RESULT;
554 char *ip_parse_cidr(const char *s, ovs_be32 *ip, unsigned int *plen)
555 OVS_WARN_UNUSED_RESULT;
556 char *ip_parse_masked_len(const char *s, int *n, ovs_be32 *ip, ovs_be32 *mask)
557 OVS_WARN_UNUSED_RESULT;
558 char *ip_parse_cidr_len(const char *s, int *n, ovs_be32 *ip,
560 OVS_WARN_UNUSED_RESULT;
562 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
563 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
564 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
567 #define IPPROTO_SCTP 132
571 #define IP_ECN_NOT_ECT 0x0
572 #define IP_ECN_ECT_1 0x01
573 #define IP_ECN_ECT_0 0x02
574 #define IP_ECN_CE 0x03
575 #define IP_ECN_MASK 0x03
576 #define IP_DSCP_MASK 0xfc
580 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
581 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
582 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
583 #define IP_IS_FRAGMENT(ip_frag_off) \
584 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
586 #define IP_HEADER_LEN 20
592 ovs_be16 ip_frag_off;
596 ovs_16aligned_be32 ip_src;
597 ovs_16aligned_be32 ip_dst;
600 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
602 #define ICMP_HEADER_LEN 8
616 ovs_16aligned_be32 gateway;
619 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
621 #define IGMP_HEADER_LEN 8
626 ovs_16aligned_be32 group;
628 BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header));
630 #define IGMPV3_HEADER_LEN 8
631 struct igmpv3_header {
638 BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header));
640 #define IGMPV3_RECORD_LEN 8
641 struct igmpv3_record {
645 ovs_16aligned_be32 maddr;
647 BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record));
649 #define IGMP_HOST_MEMBERSHIP_QUERY 0x11 /* From RFC1112 */
650 #define IGMP_HOST_MEMBERSHIP_REPORT 0x12 /* Ditto */
651 #define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */
652 #define IGMP_HOST_LEAVE_MESSAGE 0x17
653 #define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */
656 * IGMPv3 and MLDv2 use the same codes.
658 #define IGMPV3_MODE_IS_INCLUDE 1
659 #define IGMPV3_MODE_IS_EXCLUDE 2
660 #define IGMPV3_CHANGE_TO_INCLUDE_MODE 3
661 #define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4
662 #define IGMPV3_ALLOW_NEW_SOURCES 5
663 #define IGMPV3_BLOCK_OLD_SOURCES 6
665 #define SCTP_HEADER_LEN 12
669 ovs_16aligned_be32 sctp_vtag;
670 ovs_16aligned_be32 sctp_csum;
672 BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
674 #define UDP_HEADER_LEN 8
681 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
683 #define TCP_FIN 0x001
684 #define TCP_SYN 0x002
685 #define TCP_RST 0x004
686 #define TCP_PSH 0x008
687 #define TCP_ACK 0x010
688 #define TCP_URG 0x020
689 #define TCP_ECE 0x040
690 #define TCP_CWR 0x080
693 #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
694 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
695 #define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff))
696 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
698 #define TCP_HEADER_LEN 20
702 ovs_16aligned_be32 tcp_seq;
703 ovs_16aligned_be32 tcp_ack;
709 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
711 /* Connection states */
714 CS_ESTABLISHED_BIT = 1,
716 CS_REPLY_DIR_BIT = 3,
724 CS_NEW = (1 << CS_NEW_BIT),
725 CS_ESTABLISHED = (1 << CS_ESTABLISHED_BIT),
726 CS_RELATED = (1 << CS_RELATED_BIT),
727 CS_REPLY_DIR = (1 << CS_REPLY_DIR_BIT),
728 CS_INVALID = (1 << CS_INVALID_BIT),
729 CS_TRACKED = (1 << CS_TRACKED_BIT),
730 CS_SRC_NAT = (1 << CS_SRC_NAT_BIT),
731 CS_DST_NAT = (1 << CS_DST_NAT_BIT),
734 /* Undefined connection state bits. */
735 #define CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
736 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
737 | CS_SRC_NAT | CS_DST_NAT)
738 #define CS_UNSUPPORTED_MASK (~(uint32_t)CS_SUPPORTED_MASK)
740 #define ARP_HRD_ETHERNET 1
741 #define ARP_PRO_IP 0x0800
742 #define ARP_OP_REQUEST 1
743 #define ARP_OP_REPLY 2
744 #define ARP_OP_RARP 3
746 #define ARP_ETH_HEADER_LEN 28
747 struct arp_eth_header {
748 /* Generic members. */
749 ovs_be16 ar_hrd; /* Hardware type. */
750 ovs_be16 ar_pro; /* Protocol type. */
751 uint8_t ar_hln; /* Hardware address length. */
752 uint8_t ar_pln; /* Protocol address length. */
753 ovs_be16 ar_op; /* Opcode. */
755 /* Ethernet+IPv4 specific members. */
756 struct eth_addr ar_sha; /* Sender hardware address. */
757 ovs_16aligned_be32 ar_spa; /* Sender protocol address. */
758 struct eth_addr ar_tha; /* Target hardware address. */
759 ovs_16aligned_be32 ar_tpa; /* Target protocol address. */
761 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
763 #define IPV6_HEADER_LEN 40
765 /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
766 * most implementations, this one only requires 16-bit alignment. */
767 union ovs_16aligned_in6_addr {
769 ovs_16aligned_be32 be32[4];
772 /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
773 * one only requires 16-bit alignment. */
774 struct ovs_16aligned_ip6_hdr {
776 struct ovs_16aligned_ip6_hdrctl {
777 ovs_16aligned_be32 ip6_un1_flow;
778 ovs_be16 ip6_un1_plen;
780 uint8_t ip6_un1_hlim;
784 union ovs_16aligned_in6_addr ip6_src;
785 union ovs_16aligned_in6_addr ip6_dst;
788 /* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
789 * this one only requires 16-bit alignment. */
790 struct ovs_16aligned_ip6_frag {
792 uint8_t ip6f_reserved;
794 ovs_16aligned_be32 ip6f_ident;
797 #define ICMP6_HEADER_LEN 4
798 struct icmp6_header {
801 ovs_be16 icmp6_cksum;
803 BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header));
805 uint32_t packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *);
807 /* Neighbor Discovery option field.
808 * ND options are always a multiple of 8 bytes in size. */
811 uint8_t nd_opt_type; /* Values defined in icmp6.h */
812 uint8_t nd_opt_len; /* in units of 8 octets (the size of this struct) */
813 struct eth_addr nd_opt_mac; /* Ethernet address in the case of SLL or TLL options */
815 BUILD_ASSERT_DECL(ND_OPT_LEN == sizeof(struct ovs_nd_opt));
817 /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit
818 * alignment, this one only requires 16-bit alignment. */
819 #define ND_MSG_LEN 24
821 struct icmp6_header icmph;
822 ovs_16aligned_be32 rco_flags;
823 union ovs_16aligned_in6_addr target;
824 struct ovs_nd_opt options[0];
826 BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg));
829 * Use the same struct for MLD and MLD2, naming members as the defined fields in
830 * in the corresponding version of the protocol, though they are reserved in the
833 #define MLD_HEADER_LEN 8
841 BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header));
843 #define MLD2_RECORD_LEN 20
848 union ovs_16aligned_in6_addr maddr;
850 BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record));
852 #define MLD_QUERY 130
853 #define MLD_REPORT 131
855 #define MLD2_REPORT 143
857 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
858 #define IPV6_LABEL_MASK 0x000fffff
862 * char *string = "1 ::1 2";
863 * char ipv6_s[IPV6_SCAN_LEN + 1];
864 * struct in6_addr ipv6;
866 * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
867 * && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
871 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
872 #define IPV6_SCAN_LEN 46
874 extern const struct in6_addr in6addr_exact;
875 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
876 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
878 extern const struct in6_addr in6addr_all_hosts;
879 #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
880 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } }
882 static inline bool ipv6_addr_equals(const struct in6_addr *a,
883 const struct in6_addr *b)
885 #ifdef IN6_ARE_ADDR_EQUAL
886 return IN6_ARE_ADDR_EQUAL(a, b);
888 return !memcmp(a, b, sizeof(*a));
892 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
893 return ipv6_addr_equals(mask, &in6addr_any);
896 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
897 return ipv6_addr_equals(mask, &in6addr_exact);
900 static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) {
901 return ipv6_addr_equals(addr, &in6addr_all_hosts);
904 static inline bool ipv6_addr_is_set(const struct in6_addr *addr) {
905 return !ipv6_addr_equals(addr, &in6addr_any);
908 static inline bool ipv6_addr_is_multicast(const struct in6_addr *ip) {
909 return ip->s6_addr[0] == 0xff;
912 static inline struct in6_addr
913 in6_addr_mapped_ipv4(ovs_be32 ip4)
915 struct in6_addr ip6 = { .s6_addr = { [10] = 0xff, [11] = 0xff } };
916 memcpy(&ip6.s6_addr[12], &ip4, 4);
921 in6_addr_set_mapped_ipv4(struct in6_addr *ip6, ovs_be32 ip4)
923 *ip6 = in6_addr_mapped_ipv4(ip4);
926 static inline ovs_be32
927 in6_addr_get_mapped_ipv4(const struct in6_addr *addr)
929 union ovs_16aligned_in6_addr *taddr = (void *) addr;
930 if (IN6_IS_ADDR_V4MAPPED(addr)) {
931 return get_16aligned_be32(&taddr->be32[3]);
938 in6_addr_solicited_node(struct in6_addr *addr, const struct in6_addr *ip6)
940 union ovs_16aligned_in6_addr *taddr = (void *) addr;
941 memset(taddr->be16, 0, sizeof(taddr->be16));
942 taddr->be16[0] = htons(0xff02);
943 taddr->be16[5] = htons(0x1);
944 taddr->be16[6] = htons(0xff00);
945 memcpy(&addr->s6_addr[13], &ip6->s6_addr[13], 3);
949 * Generates ipv6 link local address from the given eth addr
950 * with prefix 'fe80::/64' and stores it in 'lla'
953 in6_generate_lla(struct eth_addr ea, struct in6_addr *lla)
955 union ovs_16aligned_in6_addr *taddr = (void *) lla;
956 memset(taddr->be16, 0, sizeof(taddr->be16));
957 taddr->be16[0] = htons(0xfe80);
958 taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]);
959 taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff);
960 taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]);
961 taddr->be16[7] = ea.be16[2];
965 ipv6_multicast_to_ethernet(struct eth_addr *eth, const struct in6_addr *ip6)
969 eth->ea[2] = ip6->s6_addr[12];
970 eth->ea[3] = ip6->s6_addr[13];
971 eth->ea[4] = ip6->s6_addr[14];
972 eth->ea[5] = ip6->s6_addr[15];
975 static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
977 return dl_type == htons(ETH_TYPE_IP)
978 || dl_type == htons(ETH_TYPE_IPV6);
983 /* GRE protocol header */
984 struct gre_base_hdr {
989 #define GRE_CSUM 0x8000
990 #define GRE_ROUTING 0x4000
991 #define GRE_KEY 0x2000
992 #define GRE_SEQ 0x1000
993 #define GRE_STRICT 0x0800
994 #define GRE_REC 0x0700
995 #define GRE_FLAGS 0x00F8
996 #define GRE_VERSION 0x0007
998 /* VXLAN protocol header */
1000 ovs_16aligned_be32 vx_flags;
1001 ovs_16aligned_be32 vx_vni;
1004 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
1006 void ipv6_format_addr(const struct in6_addr *addr, struct ds *);
1007 void ipv6_format_addr_bracket(const struct in6_addr *addr, struct ds *,
1009 void ipv6_format_mapped(const struct in6_addr *addr, struct ds *);
1010 void ipv6_format_masked(const struct in6_addr *addr,
1011 const struct in6_addr *mask, struct ds *);
1012 const char * ipv6_string_mapped(char *addr_str, const struct in6_addr *addr);
1013 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
1014 const struct in6_addr *mask);
1015 struct in6_addr ipv6_create_mask(int mask);
1016 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
1017 bool ipv6_is_cidr(const struct in6_addr *netmask);
1019 bool ipv6_parse(const char *s, struct in6_addr *ip);
1020 char *ipv6_parse_masked(const char *s, struct in6_addr *ipv6,
1021 struct in6_addr *mask);
1022 char *ipv6_parse_cidr(const char *s, struct in6_addr *ip, unsigned int *plen)
1023 OVS_WARN_UNUSED_RESULT;
1024 char *ipv6_parse_masked_len(const char *s, int *n, struct in6_addr *ipv6,
1025 struct in6_addr *mask);
1026 char *ipv6_parse_cidr_len(const char *s, int *n, struct in6_addr *ip,
1028 OVS_WARN_UNUSED_RESULT;
1030 void *eth_compose(struct dp_packet *, const struct eth_addr eth_dst,
1031 const struct eth_addr eth_src, uint16_t eth_type,
1033 void *snap_compose(struct dp_packet *, const struct eth_addr eth_dst,
1034 const struct eth_addr eth_src,
1035 unsigned int oui, uint16_t snap_type, size_t size);
1036 void packet_set_ipv4(struct dp_packet *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
1038 void packet_set_ipv6(struct dp_packet *, const ovs_be32 src[4],
1039 const ovs_be32 dst[4], uint8_t tc,
1040 ovs_be32 fl, uint8_t hlmit);
1041 void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1042 void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1043 void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1044 void packet_set_icmp(struct dp_packet *, uint8_t type, uint8_t code);
1045 void packet_set_nd(struct dp_packet *, const ovs_be32 target[4],
1046 const struct eth_addr sll, const struct eth_addr tll);
1048 void packet_format_tcp_flags(struct ds *, uint16_t);
1049 const char *packet_tcp_flag_to_string(uint32_t flag);
1050 void compose_arp__(struct dp_packet *);
1051 void compose_arp(struct dp_packet *, uint16_t arp_op,
1052 const struct eth_addr arp_sha,
1053 const struct eth_addr arp_tha, bool broadcast,
1054 ovs_be32 arp_spa, ovs_be32 arp_tpa);
1055 void compose_nd(struct dp_packet *, const struct eth_addr eth_src,
1056 struct in6_addr *, struct in6_addr *);
1057 uint32_t packet_csum_pseudoheader(const struct ip_header *);
1059 #endif /* packets.h */