2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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>
22 #include <netinet/in.h>
27 #include "openvswitch/types.h"
28 #include "odp-netlink.h"
31 #include "tun-metadata.h"
32 #include "unaligned.h"
38 /* Tunnel information used in flow key and metadata. */
41 struct in6_addr ipv6_dst;
43 struct in6_addr ipv6_src;
52 uint8_t pad1[5]; /* Pad to 64 bits. */
53 struct tun_metadata metadata;
56 /* Some flags are exposed through OpenFlow while others are used only
60 #define FLOW_TNL_F_OAM (1 << 0)
62 #define FLOW_TNL_PUB_F_MASK ((1 << 1) - 1)
65 #define FLOW_TNL_F_DONT_FRAGMENT (1 << 1)
66 #define FLOW_TNL_F_CSUM (1 << 2)
67 #define FLOW_TNL_F_KEY (1 << 3)
69 #define FLOW_TNL_F_MASK ((1 << 4) - 1)
71 /* Purely internal to OVS userspace. These flags should never be exposed to
72 * the outside world and so aren't included in the flags mask. */
74 /* Tunnel information is in userspace datapath format. */
75 #define FLOW_TNL_F_UDPIF (1 << 4)
77 static inline bool ipv6_addr_is_set(const struct in6_addr *addr);
80 flow_tnl_dst_is_set(const struct flow_tnl *tnl)
82 return tnl->ip_dst || ipv6_addr_is_set(&tnl->ipv6_dst);
85 struct in6_addr flow_tnl_dst(const struct flow_tnl *tnl);
86 struct in6_addr flow_tnl_src(const struct flow_tnl *tnl);
88 /* Returns an offset to 'src' covering all the meaningful fields in 'src'. */
90 flow_tnl_size(const struct flow_tnl *src)
92 if (!flow_tnl_dst_is_set(src)) {
93 /* Covers ip_dst and ipv6_dst only. */
94 return offsetof(struct flow_tnl, ip_src);
96 if (src->flags & FLOW_TNL_F_UDPIF) {
97 /* Datapath format, cover all options we have. */
98 return offsetof(struct flow_tnl, metadata.opts)
99 + src->metadata.present.len;
101 if (!src->metadata.present.map) {
102 /* No TLVs, opts is irrelevant. */
103 return offsetof(struct flow_tnl, metadata.opts);
105 /* Have decoded TLVs, opts is relevant. */
109 /* Copy flow_tnl, but avoid copying unused portions of tun_metadata. Unused
110 * data in 'dst' is NOT cleared, so this must not be used in cases where the
111 * uninitialized portion may be hashed over. */
113 flow_tnl_copy__(struct flow_tnl *dst, const struct flow_tnl *src)
115 memcpy(dst, src, flow_tnl_size(src));
119 flow_tnl_equal(const struct flow_tnl *a, const struct flow_tnl *b)
121 size_t a_size = flow_tnl_size(a);
123 return a_size == flow_tnl_size(b) && !memcmp(a, b, a_size);
126 /* Unfortunately, a "struct flow" sometimes has to handle OpenFlow port
127 * numbers and other times datapath (dpif) port numbers. This union allows
134 /* Datapath packet metadata */
135 struct pkt_metadata {
136 uint32_t recirc_id; /* Recirculation id carried with the
137 recirculating packets. 0 for packets
138 received from the wire. */
139 uint32_t dp_hash; /* hash value computed by the recirculation
141 uint32_t skb_priority; /* Packet priority for QoS. */
142 uint32_t pkt_mark; /* Packet mark. */
143 uint16_t ct_state; /* Connection state. */
144 uint16_t ct_zone; /* Connection zone. */
145 uint32_t ct_mark; /* Connection mark. */
146 ovs_u128 ct_label; /* Connection label. */
147 union flow_in_port in_port; /* Input port. */
148 struct flow_tnl tunnel; /* Encapsulating tunnel parameters. Note that
149 * if 'ip_dst' == 0, the rest of the fields may
150 * be uninitialized. */
154 pkt_metadata_init(struct pkt_metadata *md, odp_port_t port)
156 /* It can be expensive to zero out all of the tunnel metadata. However,
157 * we can just zero out ip_dst and the rest of the data will never be
159 memset(md, 0, offsetof(struct pkt_metadata, in_port));
160 md->tunnel.ip_dst = 0;
161 md->tunnel.ipv6_dst = in6addr_any;
163 md->in_port.odp_port = port;
166 bool dpid_from_string(const char *s, uint64_t *dpidp);
168 #define ETH_ADDR_LEN 6
170 static const struct eth_addr eth_addr_broadcast OVS_UNUSED
171 = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
173 static const struct eth_addr eth_addr_exact OVS_UNUSED
174 = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
176 static const struct eth_addr eth_addr_zero OVS_UNUSED
177 = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } };
179 static const struct eth_addr eth_addr_stp OVS_UNUSED
180 = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 } } };
182 static const struct eth_addr eth_addr_lacp OVS_UNUSED
183 = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 } } };
185 static const struct eth_addr eth_addr_bfd OVS_UNUSED
186 = { { { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 } } };
188 static inline bool eth_addr_is_broadcast(const struct eth_addr a)
190 return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
193 static inline bool eth_addr_is_multicast(const struct eth_addr a)
198 static inline bool eth_addr_is_local(const struct eth_addr a)
200 /* Local if it is either a locally administered address or a Nicira random
203 || (a.be16[0] == htons(0x0023)
204 && (a.be16[1] & htons(0xff80)) == htons(0x2080));
206 static inline bool eth_addr_is_zero(const struct eth_addr a)
208 return !(a.be16[0] | a.be16[1] | a.be16[2]);
211 static inline int eth_mask_is_exact(const struct eth_addr a)
213 return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
216 static inline int eth_addr_compare_3way(const struct eth_addr a,
217 const struct eth_addr b)
219 return memcmp(&a, &b, sizeof a);
222 static inline bool eth_addr_equals(const struct eth_addr a,
223 const struct eth_addr b)
225 return !eth_addr_compare_3way(a, b);
228 static inline bool eth_addr_equal_except(const struct eth_addr a,
229 const struct eth_addr b,
230 const struct eth_addr mask)
232 return !(((a.be16[0] ^ b.be16[0]) & mask.be16[0])
233 || ((a.be16[1] ^ b.be16[1]) & mask.be16[1])
234 || ((a.be16[2] ^ b.be16[2]) & mask.be16[2]));
237 static inline uint64_t eth_addr_to_uint64(const struct eth_addr ea)
239 return (((uint64_t) ntohs(ea.be16[0]) << 32)
240 | ((uint64_t) ntohs(ea.be16[1]) << 16)
241 | ntohs(ea.be16[2]));
244 static inline uint64_t eth_addr_vlan_to_uint64(const struct eth_addr ea,
247 return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
250 static inline void eth_addr_from_uint64(uint64_t x, struct eth_addr *ea)
252 ea->be16[0] = htons(x >> 32);
253 ea->be16[1] = htons((x & 0xFFFF0000) >> 16);
254 ea->be16[2] = htons(x & 0xFFFF);
257 static inline struct eth_addr eth_addr_invert(const struct eth_addr src)
261 for (int i = 0; i < ARRAY_SIZE(src.be16); i++) {
262 dst.be16[i] = ~src.be16[i];
268 static inline void eth_addr_mark_random(struct eth_addr *ea)
270 ea->ea[0] &= ~1; /* Unicast. */
271 ea->ea[0] |= 2; /* Private. */
274 static inline void eth_addr_random(struct eth_addr *ea)
276 random_bytes((uint8_t *)ea, sizeof *ea);
277 eth_addr_mark_random(ea);
280 static inline void eth_addr_nicira_random(struct eth_addr *ea)
284 /* Set the OUI to the Nicira one. */
289 /* Set the top bit to indicate random Nicira address. */
292 static inline uint32_t hash_mac(const struct eth_addr ea,
293 const uint16_t vlan, const uint32_t basis)
295 return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
298 bool eth_addr_is_reserved(const struct eth_addr);
299 bool eth_addr_from_string(const char *, struct eth_addr *);
301 void compose_rarp(struct dp_packet *, const struct eth_addr);
303 void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci);
304 void eth_pop_vlan(struct dp_packet *);
306 const char *eth_from_hex(const char *hex, struct dp_packet **packetp);
307 void eth_format_masked(const struct eth_addr ea,
308 const struct eth_addr *mask, struct ds *s);
310 void set_mpls_lse(struct dp_packet *, ovs_be32 label);
311 void push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse);
312 void pop_mpls(struct dp_packet *, ovs_be16 ethtype);
314 void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
315 void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
316 void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
317 void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
318 ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
323 * struct eth_addr mac;
325 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
328 #define ETH_ADDR_FMT \
329 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
330 #define ETH_ADDR_ARGS(EA) ETH_ADDR_BYTES_ARGS((EA).ea)
331 #define ETH_ADDR_BYTES_ARGS(EAB) \
332 (EAB)[0], (EAB)[1], (EAB)[2], (EAB)[3], (EAB)[4], (EAB)[5]
333 #define ETH_ADDR_STRLEN 17
337 * char *string = "1 00:11:22:33:44:55 2";
338 * struct eth_addr mac;
341 * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
342 * &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
346 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
347 #define ETH_ADDR_SCAN_ARGS(EA) \
348 &(EA).ea[0], &(EA).ea[1], &(EA).ea[2], &(EA).ea[3], &(EA).ea[4], &(EA).ea[5]
350 #define ETH_TYPE_IP 0x0800
351 #define ETH_TYPE_ARP 0x0806
352 #define ETH_TYPE_TEB 0x6558
353 #define ETH_TYPE_VLAN_8021Q 0x8100
354 #define ETH_TYPE_VLAN ETH_TYPE_VLAN_8021Q
355 #define ETH_TYPE_VLAN_8021AD 0x88a8
356 #define ETH_TYPE_IPV6 0x86dd
357 #define ETH_TYPE_LACP 0x8809
358 #define ETH_TYPE_RARP 0x8035
359 #define ETH_TYPE_MPLS 0x8847
360 #define ETH_TYPE_MPLS_MCAST 0x8848
362 static inline bool eth_type_mpls(ovs_be16 eth_type)
364 return eth_type == htons(ETH_TYPE_MPLS) ||
365 eth_type == htons(ETH_TYPE_MPLS_MCAST);
368 static inline bool eth_type_vlan(ovs_be16 eth_type)
370 return eth_type == htons(ETH_TYPE_VLAN_8021Q) ||
371 eth_type == htons(ETH_TYPE_VLAN_8021AD);
375 /* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame
377 #define ETH_TYPE_MIN 0x600
379 #define ETH_HEADER_LEN 14
380 #define ETH_PAYLOAD_MIN 46
381 #define ETH_PAYLOAD_MAX 1500
382 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
383 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
384 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
387 struct eth_addr eth_dst;
388 struct eth_addr eth_src;
391 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
393 #define LLC_DSAP_SNAP 0xaa
394 #define LLC_SSAP_SNAP 0xaa
395 #define LLC_CNTL_SNAP 3
397 #define LLC_HEADER_LEN 3
404 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
406 /* LLC field values used for STP frames. */
407 #define STP_LLC_SSAP 0x42
408 #define STP_LLC_DSAP 0x42
409 #define STP_LLC_CNTL 0x03
411 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
412 sizeof(SNAP_ORG_ETHERNET) == 3. */
413 #define SNAP_HEADER_LEN 5
419 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
421 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
423 struct llc_snap_header {
424 struct llc_header llc;
425 struct snap_header snap;
427 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
429 #define VLAN_VID_MASK 0x0fff
430 #define VLAN_VID_SHIFT 0
432 #define VLAN_PCP_MASK 0xe000
433 #define VLAN_PCP_SHIFT 13
435 #define VLAN_CFI 0x1000
436 #define VLAN_CFI_SHIFT 12
438 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
439 * returns the VLAN ID in host byte order. */
440 static inline uint16_t
441 vlan_tci_to_vid(ovs_be16 vlan_tci)
443 return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
446 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
447 * returns the priority code point (PCP) in host byte order. */
449 vlan_tci_to_pcp(ovs_be16 vlan_tci)
451 return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
454 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
455 * returns the Canonical Format Indicator (CFI). */
457 vlan_tci_to_cfi(ovs_be16 vlan_tci)
459 return (vlan_tci & htons(VLAN_CFI)) != 0;
462 #define VLAN_HEADER_LEN 4
464 ovs_be16 vlan_tci; /* Lowest 12 bits are VLAN ID. */
465 ovs_be16 vlan_next_type;
467 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
469 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
471 struct vlan_eth_header {
472 struct eth_addr veth_dst;
473 struct eth_addr veth_src;
474 ovs_be16 veth_type; /* Always htons(ETH_TYPE_VLAN). */
475 ovs_be16 veth_tci; /* Lowest 12 bits are VLAN ID. */
476 ovs_be16 veth_next_type;
478 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
480 /* MPLS related definitions */
481 #define MPLS_TTL_MASK 0x000000ff
482 #define MPLS_TTL_SHIFT 0
484 #define MPLS_BOS_MASK 0x00000100
485 #define MPLS_BOS_SHIFT 8
487 #define MPLS_TC_MASK 0x00000e00
488 #define MPLS_TC_SHIFT 9
490 #define MPLS_LABEL_MASK 0xfffff000
491 #define MPLS_LABEL_SHIFT 12
496 ovs_16aligned_be32 mpls_lse;
498 BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
500 /* Given a mpls label stack entry in network byte order
501 * return mpls label in host byte order */
502 static inline uint32_t
503 mpls_lse_to_label(ovs_be32 mpls_lse)
505 return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
508 /* Given a mpls label stack entry in network byte order
510 static inline uint8_t
511 mpls_lse_to_tc(ovs_be32 mpls_lse)
513 return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
516 /* Given a mpls label stack entry in network byte order
518 static inline uint8_t
519 mpls_lse_to_ttl(ovs_be32 mpls_lse)
521 return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
524 /* Set TTL in mpls lse. */
526 flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
528 *mpls_lse &= ~htonl(MPLS_TTL_MASK);
529 *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
532 /* Given a mpls label stack entry in network byte order
533 * return mpls BoS bit */
534 static inline uint8_t
535 mpls_lse_to_bos(ovs_be32 mpls_lse)
537 return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
540 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
541 #define IP_ARGS(ip) \
543 (ntohl(ip) >> 16) & 0xff, \
544 (ntohl(ip) >> 8) & 0xff, \
549 * char *string = "1 33.44.55.66 2";
553 * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
557 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
558 #define IP_SCAN_ARGS(ip) \
559 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
560 &((uint8_t *) ip)[1], \
561 &((uint8_t *) ip)[2], \
564 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
565 * high-order 1-bits and 32-N low-order 0-bits. */
567 ip_is_cidr(ovs_be32 netmask)
569 uint32_t x = ~ntohl(netmask);
570 return !(x & (x + 1));
573 ip_is_multicast(ovs_be32 ip)
575 return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
578 ip_is_local_multicast(ovs_be32 ip)
580 return (ip & htonl(0xffffff00)) == htonl(0xe0000000);
582 int ip_count_cidr_bits(ovs_be32 netmask);
583 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
584 bool ip_parse(const char *s, ovs_be32 *ip);
585 char *ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
586 OVS_WARN_UNUSED_RESULT;
587 char *ip_parse_cidr(const char *s, ovs_be32 *ip, unsigned int *plen)
588 OVS_WARN_UNUSED_RESULT;
590 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
591 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
592 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
595 #define IPPROTO_SCTP 132
599 #define IP_ECN_NOT_ECT 0x0
600 #define IP_ECN_ECT_1 0x01
601 #define IP_ECN_ECT_0 0x02
602 #define IP_ECN_CE 0x03
603 #define IP_ECN_MASK 0x03
604 #define IP_DSCP_MASK 0xfc
608 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
609 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
610 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
611 #define IP_IS_FRAGMENT(ip_frag_off) \
612 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
614 #define IP_HEADER_LEN 20
620 ovs_be16 ip_frag_off;
624 ovs_16aligned_be32 ip_src;
625 ovs_16aligned_be32 ip_dst;
628 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
630 #define ICMP_HEADER_LEN 8
644 ovs_16aligned_be32 gateway;
647 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
649 #define IGMP_HEADER_LEN 8
654 ovs_16aligned_be32 group;
656 BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header));
658 #define IGMPV3_HEADER_LEN 8
659 struct igmpv3_header {
666 BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header));
668 #define IGMPV3_RECORD_LEN 8
669 struct igmpv3_record {
673 ovs_16aligned_be32 maddr;
675 BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record));
677 #define IGMP_HOST_MEMBERSHIP_QUERY 0x11 /* From RFC1112 */
678 #define IGMP_HOST_MEMBERSHIP_REPORT 0x12 /* Ditto */
679 #define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */
680 #define IGMP_HOST_LEAVE_MESSAGE 0x17
681 #define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */
684 * IGMPv3 and MLDv2 use the same codes.
686 #define IGMPV3_MODE_IS_INCLUDE 1
687 #define IGMPV3_MODE_IS_EXCLUDE 2
688 #define IGMPV3_CHANGE_TO_INCLUDE_MODE 3
689 #define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4
690 #define IGMPV3_ALLOW_NEW_SOURCES 5
691 #define IGMPV3_BLOCK_OLD_SOURCES 6
693 #define SCTP_HEADER_LEN 12
697 ovs_16aligned_be32 sctp_vtag;
698 ovs_16aligned_be32 sctp_csum;
700 BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
702 #define UDP_HEADER_LEN 8
709 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
711 #define TCP_FIN 0x001
712 #define TCP_SYN 0x002
713 #define TCP_RST 0x004
714 #define TCP_PSH 0x008
715 #define TCP_ACK 0x010
716 #define TCP_URG 0x020
717 #define TCP_ECE 0x040
718 #define TCP_CWR 0x080
721 #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
722 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
723 #define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff))
724 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
726 #define TCP_HEADER_LEN 20
730 ovs_16aligned_be32 tcp_seq;
731 ovs_16aligned_be32 tcp_ack;
737 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
739 /* Connection states */
742 CS_ESTABLISHED_BIT = 1,
744 CS_REPLY_DIR_BIT = 3,
752 CS_NEW = (1 << CS_NEW_BIT),
753 CS_ESTABLISHED = (1 << CS_ESTABLISHED_BIT),
754 CS_RELATED = (1 << CS_RELATED_BIT),
755 CS_REPLY_DIR = (1 << CS_REPLY_DIR_BIT),
756 CS_INVALID = (1 << CS_INVALID_BIT),
757 CS_TRACKED = (1 << CS_TRACKED_BIT),
758 CS_SRC_NAT = (1 << CS_SRC_NAT_BIT),
759 CS_DST_NAT = (1 << CS_DST_NAT_BIT),
762 /* Undefined connection state bits. */
763 #define CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \
764 | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
765 | CS_SRC_NAT | CS_DST_NAT)
766 #define CS_UNSUPPORTED_MASK (~(uint32_t)CS_SUPPORTED_MASK)
768 #define ARP_HRD_ETHERNET 1
769 #define ARP_PRO_IP 0x0800
770 #define ARP_OP_REQUEST 1
771 #define ARP_OP_REPLY 2
772 #define ARP_OP_RARP 3
774 #define ARP_ETH_HEADER_LEN 28
775 struct arp_eth_header {
776 /* Generic members. */
777 ovs_be16 ar_hrd; /* Hardware type. */
778 ovs_be16 ar_pro; /* Protocol type. */
779 uint8_t ar_hln; /* Hardware address length. */
780 uint8_t ar_pln; /* Protocol address length. */
781 ovs_be16 ar_op; /* Opcode. */
783 /* Ethernet+IPv4 specific members. */
784 struct eth_addr ar_sha; /* Sender hardware address. */
785 ovs_16aligned_be32 ar_spa; /* Sender protocol address. */
786 struct eth_addr ar_tha; /* Target hardware address. */
787 ovs_16aligned_be32 ar_tpa; /* Target protocol address. */
789 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
791 #define IPV6_HEADER_LEN 40
793 /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
794 * most implementations, this one only requires 16-bit alignment. */
795 union ovs_16aligned_in6_addr {
797 ovs_16aligned_be32 be32[4];
800 /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
801 * one only requires 16-bit alignment. */
802 struct ovs_16aligned_ip6_hdr {
804 struct ovs_16aligned_ip6_hdrctl {
805 ovs_16aligned_be32 ip6_un1_flow;
806 ovs_be16 ip6_un1_plen;
808 uint8_t ip6_un1_hlim;
812 union ovs_16aligned_in6_addr ip6_src;
813 union ovs_16aligned_in6_addr ip6_dst;
816 /* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
817 * this one only requires 16-bit alignment. */
818 struct ovs_16aligned_ip6_frag {
820 uint8_t ip6f_reserved;
822 ovs_16aligned_be32 ip6f_ident;
825 #define ICMP6_HEADER_LEN 4
826 struct icmp6_header {
829 ovs_be16 icmp6_cksum;
831 BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header));
833 uint32_t packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *);
835 /* Neighbor Discovery option field.
836 * ND options are always a multiple of 8 bytes in size. */
839 uint8_t nd_opt_type; /* Values defined in icmp6.h */
840 uint8_t nd_opt_len; /* in units of 8 octets (the size of this struct) */
841 struct eth_addr nd_opt_mac; /* Ethernet address in the case of SLL or TLL options */
843 BUILD_ASSERT_DECL(ND_OPT_LEN == sizeof(struct ovs_nd_opt));
845 /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit
846 * alignment, this one only requires 16-bit alignment. */
847 #define ND_MSG_LEN 24
849 struct icmp6_header icmph;
850 ovs_16aligned_be32 rco_flags;
851 union ovs_16aligned_in6_addr target;
852 struct ovs_nd_opt options[0];
854 BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg));
857 * Use the same struct for MLD and MLD2, naming members as the defined fields in
858 * in the corresponding version of the protocol, though they are reserved in the
861 #define MLD_HEADER_LEN 8
869 BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header));
871 #define MLD2_RECORD_LEN 20
876 union ovs_16aligned_in6_addr maddr;
878 BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record));
880 #define MLD_QUERY 130
881 #define MLD_REPORT 131
883 #define MLD2_REPORT 143
885 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
886 #define IPV6_LABEL_MASK 0x000fffff
890 * char *string = "1 ::1 2";
891 * char ipv6_s[IPV6_SCAN_LEN + 1];
892 * struct in6_addr ipv6;
894 * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
895 * && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
899 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
900 #define IPV6_SCAN_LEN 46
902 extern const struct in6_addr in6addr_exact;
903 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
904 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
906 extern const struct in6_addr in6addr_all_hosts;
907 #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
908 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } }
910 static inline bool ipv6_addr_equals(const struct in6_addr *a,
911 const struct in6_addr *b)
913 #ifdef IN6_ARE_ADDR_EQUAL
914 return IN6_ARE_ADDR_EQUAL(a, b);
916 return !memcmp(a, b, sizeof(*a));
920 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
921 return ipv6_addr_equals(mask, &in6addr_any);
924 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
925 return ipv6_addr_equals(mask, &in6addr_exact);
928 static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) {
929 return ipv6_addr_equals(addr, &in6addr_all_hosts);
932 static inline bool ipv6_addr_is_set(const struct in6_addr *addr) {
933 return !ipv6_addr_equals(addr, &in6addr_any);
936 static inline bool ipv6_addr_is_multicast(const struct in6_addr *ip) {
937 return ip->s6_addr[0] == 0xff;
940 static inline struct in6_addr
941 in6_addr_mapped_ipv4(ovs_be32 ip4)
943 struct in6_addr ip6 = { .s6_addr = { [10] = 0xff, [11] = 0xff } };
944 memcpy(&ip6.s6_addr[12], &ip4, 4);
949 in6_addr_set_mapped_ipv4(struct in6_addr *ip6, ovs_be32 ip4)
951 *ip6 = in6_addr_mapped_ipv4(ip4);
954 static inline ovs_be32
955 in6_addr_get_mapped_ipv4(const struct in6_addr *addr)
957 union ovs_16aligned_in6_addr *taddr = (void *) addr;
958 if (IN6_IS_ADDR_V4MAPPED(addr)) {
959 return get_16aligned_be32(&taddr->be32[3]);
966 in6_addr_solicited_node(struct in6_addr *addr, const struct in6_addr *ip6)
968 union ovs_16aligned_in6_addr *taddr = (void *) addr;
969 memset(taddr->be16, 0, sizeof(taddr->be16));
970 taddr->be16[0] = htons(0xff02);
971 taddr->be16[5] = htons(0x1);
972 taddr->be16[6] = htons(0xff00);
973 memcpy(&addr->s6_addr[13], &ip6->s6_addr[13], 3);
977 ipv6_multicast_to_ethernet(struct eth_addr *eth, const struct in6_addr *ip6)
981 eth->ea[2] = ip6->s6_addr[12];
982 eth->ea[3] = ip6->s6_addr[13];
983 eth->ea[4] = ip6->s6_addr[14];
984 eth->ea[5] = ip6->s6_addr[15];
987 static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
989 return dl_type == htons(ETH_TYPE_IP)
990 || dl_type == htons(ETH_TYPE_IPV6);
995 /* GRE protocol header */
996 struct gre_base_hdr {
1001 #define GRE_CSUM 0x8000
1002 #define GRE_ROUTING 0x4000
1003 #define GRE_KEY 0x2000
1004 #define GRE_SEQ 0x1000
1005 #define GRE_STRICT 0x0800
1006 #define GRE_REC 0x0700
1007 #define GRE_FLAGS 0x00F8
1008 #define GRE_VERSION 0x0007
1010 /* VXLAN protocol header */
1012 ovs_16aligned_be32 vx_flags;
1013 ovs_16aligned_be32 vx_vni;
1016 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
1018 void ipv6_format_addr(const struct in6_addr *addr, struct ds *);
1019 void ipv6_format_addr_bracket(const struct in6_addr *addr, struct ds *,
1021 void ipv6_format_mapped(const struct in6_addr *addr, struct ds *);
1022 void ipv6_format_masked(const struct in6_addr *addr,
1023 const struct in6_addr *mask, struct ds *);
1024 const char * ipv6_string_mapped(char *addr_str, const struct in6_addr *addr);
1025 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
1026 const struct in6_addr *mask);
1027 struct in6_addr ipv6_create_mask(int mask);
1028 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
1029 bool ipv6_is_cidr(const struct in6_addr *netmask);
1031 bool ipv6_parse(const char *s, struct in6_addr *ip);
1032 char *ipv6_parse_masked(const char *s, struct in6_addr *ipv6,
1033 struct in6_addr *mask);
1034 char *ipv6_parse_cidr(const char *s, struct in6_addr *ip, unsigned int *plen)
1035 OVS_WARN_UNUSED_RESULT;
1037 void *eth_compose(struct dp_packet *, const struct eth_addr eth_dst,
1038 const struct eth_addr eth_src, uint16_t eth_type,
1040 void *snap_compose(struct dp_packet *, const struct eth_addr eth_dst,
1041 const struct eth_addr eth_src,
1042 unsigned int oui, uint16_t snap_type, size_t size);
1043 void packet_set_ipv4(struct dp_packet *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
1045 void packet_set_ipv6(struct dp_packet *, uint8_t proto, const ovs_be32 src[4],
1046 const ovs_be32 dst[4], uint8_t tc,
1047 ovs_be32 fl, uint8_t hlmit);
1048 void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1049 void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1050 void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1051 void packet_set_icmp(struct dp_packet *, uint8_t type, uint8_t code);
1052 void packet_set_nd(struct dp_packet *, const ovs_be32 target[4],
1053 const struct eth_addr sll, const struct eth_addr tll);
1055 void packet_format_tcp_flags(struct ds *, uint16_t);
1056 const char *packet_tcp_flag_to_string(uint32_t flag);
1057 void compose_arp(struct dp_packet *, uint16_t arp_op,
1058 const struct eth_addr arp_sha,
1059 const struct eth_addr arp_tha, bool broadcast,
1060 ovs_be32 arp_spa, ovs_be32 arp_tpa);
1061 void compose_nd(struct dp_packet *, const struct eth_addr eth_src,
1062 struct in6_addr *, struct in6_addr *);
1063 uint32_t packet_csum_pseudoheader(const struct ip_header *);
1065 #endif /* packets.h */