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"
30 #include "tun-metadata.h"
36 /* Tunnel information used in flow key and metadata. */
48 uint8_t pad1[5]; /* Pad to 64 bits. */
49 struct tun_metadata metadata;
52 /* Some flags are exposed through OpenFlow while others are used only
56 #define FLOW_TNL_F_OAM (1 << 0)
58 #define FLOW_TNL_PUB_F_MASK ((1 << 1) - 1)
61 #define FLOW_TNL_F_DONT_FRAGMENT (1 << 1)
62 #define FLOW_TNL_F_CSUM (1 << 2)
63 #define FLOW_TNL_F_KEY (1 << 3)
65 #define FLOW_TNL_F_MASK ((1 << 4) - 1)
67 /* Purely internal to OVS userspace. These flags should never be exposed to
68 * the outside world and so aren't included in the flags mask. */
70 /* Tunnel information is in userspace datapath format. */
71 #define FLOW_TNL_F_UDPIF (1 << 4)
73 /* Returns an offset to 'src' covering all the meaningful fields in 'src'. */
75 flow_tnl_size(const struct flow_tnl *src)
78 /* Covers ip_dst only. */
79 return offsetof(struct flow_tnl, ip_src);
81 if (src->flags & FLOW_TNL_F_UDPIF) {
82 /* Datapath format, cover all options we have. */
83 return offsetof(struct flow_tnl, metadata.opts)
84 + src->metadata.present.len;
86 if (!src->metadata.present.map) {
87 /* No TLVs, opts is irrelevant. */
88 return offsetof(struct flow_tnl, metadata.opts);
90 /* Have decoded TLVs, opts is relevant. */
94 /* Copy flow_tnl, but avoid copying unused portions of tun_metadata. Unused
95 * data in 'dst' is NOT cleared, so this must not be used in cases where the
96 * uninitialized portion may be hashed over. */
98 flow_tnl_copy__(struct flow_tnl *dst, const struct flow_tnl *src)
100 memcpy(dst, src, flow_tnl_size(src));
104 flow_tnl_equal(const struct flow_tnl *a, const struct flow_tnl *b)
106 size_t a_size = flow_tnl_size(a);
108 return a_size == flow_tnl_size(b) && !memcmp(a, b, a_size);
111 /* Unfortunately, a "struct flow" sometimes has to handle OpenFlow port
112 * numbers and other times datapath (dpif) port numbers. This union allows
119 /* Datapath packet metadata */
120 struct pkt_metadata {
121 uint32_t recirc_id; /* Recirculation id carried with the
122 recirculating packets. 0 for packets
123 received from the wire. */
124 uint32_t dp_hash; /* hash value computed by the recirculation
126 uint32_t skb_priority; /* Packet priority for QoS. */
127 uint32_t pkt_mark; /* Packet mark. */
128 union flow_in_port in_port; /* Input port. */
129 struct flow_tnl tunnel; /* Encapsulating tunnel parameters. Note that
130 * if 'ip_dst' == 0, the rest of the fields may
131 * be uninitialized. */
135 pkt_metadata_init(struct pkt_metadata *md, odp_port_t port)
137 /* It can be expensive to zero out all of the tunnel metadata. However,
138 * we can just zero out ip_dst and the rest of the data will never be
140 memset(md, 0, offsetof(struct pkt_metadata, tunnel));
141 md->tunnel.ip_dst = 0;
143 md->in_port.odp_port = port;
146 bool dpid_from_string(const char *s, uint64_t *dpidp);
148 #define ETH_ADDR_LEN 6
150 static const struct eth_addr eth_addr_broadcast OVS_UNUSED
151 = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
153 static const struct eth_addr eth_addr_exact OVS_UNUSED
154 = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
156 static const struct eth_addr eth_addr_zero OVS_UNUSED
157 = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } };
159 static const struct eth_addr eth_addr_stp OVS_UNUSED
160 = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 } } };
162 static const struct eth_addr eth_addr_lacp OVS_UNUSED
163 = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 } } };
165 static const struct eth_addr eth_addr_bfd OVS_UNUSED
166 = { { { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 } } };
168 static inline bool eth_addr_is_broadcast(const struct eth_addr a)
170 return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
173 static inline bool eth_addr_is_multicast(const struct eth_addr a)
178 static inline bool eth_addr_is_local(const struct eth_addr a)
180 /* Local if it is either a locally administered address or a Nicira random
183 || (a.be16[0] == htons(0x0023)
184 && (a.be16[1] & htons(0xff80)) == htons(0x2080));
186 static inline bool eth_addr_is_zero(const struct eth_addr a)
188 return !(a.be16[0] | a.be16[1] | a.be16[2]);
191 static inline int eth_mask_is_exact(const struct eth_addr a)
193 return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
196 static inline int eth_addr_compare_3way(const struct eth_addr a,
197 const struct eth_addr b)
199 return memcmp(&a, &b, sizeof a);
202 static inline bool eth_addr_equals(const struct eth_addr a,
203 const struct eth_addr b)
205 return !eth_addr_compare_3way(a, b);
208 static inline bool eth_addr_equal_except(const struct eth_addr a,
209 const struct eth_addr b,
210 const struct eth_addr mask)
212 return !(((a.be16[0] ^ b.be16[0]) & mask.be16[0])
213 || ((a.be16[1] ^ b.be16[1]) & mask.be16[1])
214 || ((a.be16[2] ^ b.be16[2]) & mask.be16[2]));
217 static inline uint64_t eth_addr_to_uint64(const struct eth_addr ea)
219 return (((uint64_t) ntohs(ea.be16[0]) << 32)
220 | ((uint64_t) ntohs(ea.be16[1]) << 16)
221 | ntohs(ea.be16[2]));
224 static inline uint64_t eth_addr_vlan_to_uint64(const struct eth_addr ea,
227 return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
230 static inline void eth_addr_from_uint64(uint64_t x, struct eth_addr *ea)
232 ea->be16[0] = htons(x >> 32);
233 ea->be16[1] = htons(x >> 16);
234 ea->be16[2] = htons(x);
237 static inline struct eth_addr eth_addr_invert(const struct eth_addr src)
241 for (int i = 0; i < ARRAY_SIZE(src.be16); i++) {
242 dst.be16[i] = ~src.be16[i];
248 static inline void eth_addr_mark_random(struct eth_addr *ea)
250 ea->ea[0] &= ~1; /* Unicast. */
251 ea->ea[0] |= 2; /* Private. */
254 static inline void eth_addr_random(struct eth_addr *ea)
256 random_bytes((uint8_t *)ea, sizeof *ea);
257 eth_addr_mark_random(ea);
260 static inline void eth_addr_nicira_random(struct eth_addr *ea)
264 /* Set the OUI to the Nicira one. */
269 /* Set the top bit to indicate random Nicira address. */
272 static inline uint32_t hash_mac(const struct eth_addr ea,
273 const uint16_t vlan, const uint32_t basis)
275 return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
278 bool eth_addr_is_reserved(const struct eth_addr);
279 bool eth_addr_from_string(const char *, struct eth_addr *);
281 void compose_rarp(struct dp_packet *, const struct eth_addr);
283 void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci);
284 void eth_pop_vlan(struct dp_packet *);
286 const char *eth_from_hex(const char *hex, struct dp_packet **packetp);
287 void eth_format_masked(const struct eth_addr ea,
288 const struct eth_addr *mask, struct ds *s);
290 void set_mpls_lse(struct dp_packet *, ovs_be32 label);
291 void push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse);
292 void pop_mpls(struct dp_packet *, ovs_be16 ethtype);
294 void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
295 void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
296 void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
297 void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
298 ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
303 * struct eth_addr mac;
305 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
308 #define ETH_ADDR_FMT \
309 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
310 #define ETH_ADDR_ARGS(EA) \
311 (EA).ea[0], (EA).ea[1], (EA).ea[2], (EA).ea[3], (EA).ea[4], (EA).ea[5]
315 * char *string = "1 00:11:22:33:44:55 2";
316 * struct eth_addr mac;
319 * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
320 * &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
324 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
325 #define ETH_ADDR_SCAN_ARGS(EA) \
326 &(EA).ea[0], &(EA).ea[1], &(EA).ea[2], &(EA).ea[3], &(EA).ea[4], &(EA).ea[5]
328 #define ETH_TYPE_IP 0x0800
329 #define ETH_TYPE_ARP 0x0806
330 #define ETH_TYPE_TEB 0x6558
331 #define ETH_TYPE_VLAN_8021Q 0x8100
332 #define ETH_TYPE_VLAN ETH_TYPE_VLAN_8021Q
333 #define ETH_TYPE_VLAN_8021AD 0x88a8
334 #define ETH_TYPE_IPV6 0x86dd
335 #define ETH_TYPE_LACP 0x8809
336 #define ETH_TYPE_RARP 0x8035
337 #define ETH_TYPE_MPLS 0x8847
338 #define ETH_TYPE_MPLS_MCAST 0x8848
340 static inline bool eth_type_mpls(ovs_be16 eth_type)
342 return eth_type == htons(ETH_TYPE_MPLS) ||
343 eth_type == htons(ETH_TYPE_MPLS_MCAST);
346 static inline bool eth_type_vlan(ovs_be16 eth_type)
348 return eth_type == htons(ETH_TYPE_VLAN_8021Q) ||
349 eth_type == htons(ETH_TYPE_VLAN_8021AD);
353 /* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame
355 #define ETH_TYPE_MIN 0x600
357 #define ETH_HEADER_LEN 14
358 #define ETH_PAYLOAD_MIN 46
359 #define ETH_PAYLOAD_MAX 1500
360 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
361 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
362 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
365 struct eth_addr eth_dst;
366 struct eth_addr eth_src;
369 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
371 #define LLC_DSAP_SNAP 0xaa
372 #define LLC_SSAP_SNAP 0xaa
373 #define LLC_CNTL_SNAP 3
375 #define LLC_HEADER_LEN 3
382 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
384 /* LLC field values used for STP frames. */
385 #define STP_LLC_SSAP 0x42
386 #define STP_LLC_DSAP 0x42
387 #define STP_LLC_CNTL 0x03
389 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
390 sizeof(SNAP_ORG_ETHERNET) == 3. */
391 #define SNAP_HEADER_LEN 5
397 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
399 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
401 struct llc_snap_header {
402 struct llc_header llc;
403 struct snap_header snap;
405 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
407 #define VLAN_VID_MASK 0x0fff
408 #define VLAN_VID_SHIFT 0
410 #define VLAN_PCP_MASK 0xe000
411 #define VLAN_PCP_SHIFT 13
413 #define VLAN_CFI 0x1000
414 #define VLAN_CFI_SHIFT 12
416 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
417 * returns the VLAN ID in host byte order. */
418 static inline uint16_t
419 vlan_tci_to_vid(ovs_be16 vlan_tci)
421 return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
424 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
425 * returns the priority code point (PCP) in host byte order. */
427 vlan_tci_to_pcp(ovs_be16 vlan_tci)
429 return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
432 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
433 * returns the Canonical Format Indicator (CFI). */
435 vlan_tci_to_cfi(ovs_be16 vlan_tci)
437 return (vlan_tci & htons(VLAN_CFI)) != 0;
440 #define VLAN_HEADER_LEN 4
442 ovs_be16 vlan_tci; /* Lowest 12 bits are VLAN ID. */
443 ovs_be16 vlan_next_type;
445 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
447 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
449 struct vlan_eth_header {
450 struct eth_addr veth_dst;
451 struct eth_addr veth_src;
452 ovs_be16 veth_type; /* Always htons(ETH_TYPE_VLAN). */
453 ovs_be16 veth_tci; /* Lowest 12 bits are VLAN ID. */
454 ovs_be16 veth_next_type;
456 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
458 /* MPLS related definitions */
459 #define MPLS_TTL_MASK 0x000000ff
460 #define MPLS_TTL_SHIFT 0
462 #define MPLS_BOS_MASK 0x00000100
463 #define MPLS_BOS_SHIFT 8
465 #define MPLS_TC_MASK 0x00000e00
466 #define MPLS_TC_SHIFT 9
468 #define MPLS_LABEL_MASK 0xfffff000
469 #define MPLS_LABEL_SHIFT 12
474 ovs_16aligned_be32 mpls_lse;
476 BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
478 /* Given a mpls label stack entry in network byte order
479 * return mpls label in host byte order */
480 static inline uint32_t
481 mpls_lse_to_label(ovs_be32 mpls_lse)
483 return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
486 /* Given a mpls label stack entry in network byte order
488 static inline uint8_t
489 mpls_lse_to_tc(ovs_be32 mpls_lse)
491 return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
494 /* Given a mpls label stack entry in network byte order
496 static inline uint8_t
497 mpls_lse_to_ttl(ovs_be32 mpls_lse)
499 return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
502 /* Set TTL in mpls lse. */
504 flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
506 *mpls_lse &= ~htonl(MPLS_TTL_MASK);
507 *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
510 /* Given a mpls label stack entry in network byte order
511 * return mpls BoS bit */
512 static inline uint8_t
513 mpls_lse_to_bos(ovs_be32 mpls_lse)
515 return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
518 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
519 #define IP_ARGS(ip) \
521 (ntohl(ip) >> 16) & 0xff, \
522 (ntohl(ip) >> 8) & 0xff, \
527 * char *string = "1 33.44.55.66 2";
531 * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
535 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
536 #define IP_SCAN_ARGS(ip) \
537 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
538 &((uint8_t *) ip)[1], \
539 &((uint8_t *) ip)[2], \
542 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
543 * high-order 1-bits and 32-N low-order 0-bits. */
545 ip_is_cidr(ovs_be32 netmask)
547 uint32_t x = ~ntohl(netmask);
548 return !(x & (x + 1));
551 ip_is_multicast(ovs_be32 ip)
553 return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
556 ip_is_local_multicast(ovs_be32 ip)
558 return (ip & htonl(0xffffff00)) == htonl(0xe0000000);
560 int ip_count_cidr_bits(ovs_be32 netmask);
561 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
563 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
564 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
565 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
568 #define IPPROTO_SCTP 132
572 #define IP_ECN_NOT_ECT 0x0
573 #define IP_ECN_ECT_1 0x01
574 #define IP_ECN_ECT_0 0x02
575 #define IP_ECN_CE 0x03
576 #define IP_ECN_MASK 0x03
577 #define IP_DSCP_MASK 0xfc
581 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
582 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
583 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
584 #define IP_IS_FRAGMENT(ip_frag_off) \
585 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
587 #define IP_HEADER_LEN 20
593 ovs_be16 ip_frag_off;
597 ovs_16aligned_be32 ip_src;
598 ovs_16aligned_be32 ip_dst;
601 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
603 #define ICMP_HEADER_LEN 8
617 ovs_16aligned_be32 gateway;
620 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
622 #define IGMP_HEADER_LEN 8
627 ovs_16aligned_be32 group;
629 BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header));
631 #define IGMPV3_HEADER_LEN 8
632 struct igmpv3_header {
639 BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header));
641 #define IGMPV3_RECORD_LEN 8
642 struct igmpv3_record {
646 ovs_16aligned_be32 maddr;
648 BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record));
650 #define IGMP_HOST_MEMBERSHIP_QUERY 0x11 /* From RFC1112 */
651 #define IGMP_HOST_MEMBERSHIP_REPORT 0x12 /* Ditto */
652 #define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */
653 #define IGMP_HOST_LEAVE_MESSAGE 0x17
654 #define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */
657 * IGMPv3 and MLDv2 use the same codes.
659 #define IGMPV3_MODE_IS_INCLUDE 1
660 #define IGMPV3_MODE_IS_EXCLUDE 2
661 #define IGMPV3_CHANGE_TO_INCLUDE_MODE 3
662 #define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4
663 #define IGMPV3_ALLOW_NEW_SOURCES 5
664 #define IGMPV3_BLOCK_OLD_SOURCES 6
666 #define SCTP_HEADER_LEN 12
670 ovs_16aligned_be32 sctp_vtag;
671 ovs_16aligned_be32 sctp_csum;
673 BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
675 #define UDP_HEADER_LEN 8
682 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
684 #define TCP_FIN 0x001
685 #define TCP_SYN 0x002
686 #define TCP_RST 0x004
687 #define TCP_PSH 0x008
688 #define TCP_ACK 0x010
689 #define TCP_URG 0x020
690 #define TCP_ECE 0x040
691 #define TCP_CWR 0x080
694 #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
695 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
696 #define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff))
697 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
699 #define TCP_HEADER_LEN 20
703 ovs_16aligned_be32 tcp_seq;
704 ovs_16aligned_be32 tcp_ack;
710 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
712 #define ARP_HRD_ETHERNET 1
713 #define ARP_PRO_IP 0x0800
714 #define ARP_OP_REQUEST 1
715 #define ARP_OP_REPLY 2
716 #define ARP_OP_RARP 3
718 #define ARP_ETH_HEADER_LEN 28
719 struct arp_eth_header {
720 /* Generic members. */
721 ovs_be16 ar_hrd; /* Hardware type. */
722 ovs_be16 ar_pro; /* Protocol type. */
723 uint8_t ar_hln; /* Hardware address length. */
724 uint8_t ar_pln; /* Protocol address length. */
725 ovs_be16 ar_op; /* Opcode. */
727 /* Ethernet+IPv4 specific members. */
728 struct eth_addr ar_sha; /* Sender hardware address. */
729 ovs_16aligned_be32 ar_spa; /* Sender protocol address. */
730 struct eth_addr ar_tha; /* Target hardware address. */
731 ovs_16aligned_be32 ar_tpa; /* Target protocol address. */
733 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
735 /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
736 * most implementations, this one only requires 16-bit alignment. */
737 union ovs_16aligned_in6_addr {
739 ovs_16aligned_be32 be32[4];
742 /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
743 * one only requires 16-bit alignment. */
744 struct ovs_16aligned_ip6_hdr {
746 struct ovs_16aligned_ip6_hdrctl {
747 ovs_16aligned_be32 ip6_un1_flow;
748 ovs_be16 ip6_un1_plen;
750 uint8_t ip6_un1_hlim;
754 union ovs_16aligned_in6_addr ip6_src;
755 union ovs_16aligned_in6_addr ip6_dst;
758 /* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
759 * this one only requires 16-bit alignment. */
760 struct ovs_16aligned_ip6_frag {
762 uint8_t ip6f_reserved;
764 ovs_16aligned_be32 ip6f_ident;
767 #define ICMP6_HEADER_LEN 4
768 struct icmp6_header {
771 ovs_be16 icmp6_cksum;
773 BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header));
775 /* Neighbor Discovery option field.
776 * ND options are always a multiple of 8 bytes in size. */
779 uint8_t nd_opt_type; /* Values defined in icmp6.h */
780 uint8_t nd_opt_len; /* in units of 8 octets (the size of this struct) */
781 struct eth_addr nd_opt_mac; /* Ethernet address in the case of SLL or TLL options */
783 BUILD_ASSERT_DECL(ND_OPT_LEN == sizeof(struct ovs_nd_opt));
785 /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit
786 * alignment, this one only requires 16-bit alignment. */
787 #define ND_MSG_LEN 24
789 struct icmp6_header icmph;
790 ovs_16aligned_be32 rco_flags;
791 union ovs_16aligned_in6_addr target;
792 struct ovs_nd_opt options[0];
794 BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg));
797 * Use the same struct for MLD and MLD2, naming members as the defined fields in
798 * in the corresponding version of the protocol, though they are reserved in the
801 #define MLD_HEADER_LEN 8
809 BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header));
811 #define MLD2_RECORD_LEN 20
816 union ovs_16aligned_in6_addr maddr;
818 BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record));
820 #define MLD_QUERY 130
821 #define MLD_REPORT 131
823 #define MLD2_REPORT 143
825 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
826 #define IPV6_LABEL_MASK 0x000fffff
830 * char *string = "1 ::1 2";
831 * char ipv6_s[IPV6_SCAN_LEN + 1];
832 * struct in6_addr ipv6;
834 * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
835 * && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
839 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
840 #define IPV6_SCAN_LEN 46
842 extern const struct in6_addr in6addr_exact;
843 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
844 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
846 extern const struct in6_addr in6addr_all_hosts;
847 #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
848 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } }
850 static inline bool ipv6_addr_equals(const struct in6_addr *a,
851 const struct in6_addr *b)
853 #ifdef IN6_ARE_ADDR_EQUAL
854 return IN6_ARE_ADDR_EQUAL(a, b);
856 return !memcmp(a, b, sizeof(*a));
860 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
861 return ipv6_addr_equals(mask, &in6addr_any);
864 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
865 return ipv6_addr_equals(mask, &in6addr_exact);
868 static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) {
869 return ipv6_addr_equals(addr, &in6addr_all_hosts);
872 static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
874 return dl_type == htons(ETH_TYPE_IP)
875 || dl_type == htons(ETH_TYPE_IPV6);
880 /* GRE protocol header */
881 struct gre_base_hdr {
886 #define GRE_CSUM 0x8000
887 #define GRE_ROUTING 0x4000
888 #define GRE_KEY 0x2000
889 #define GRE_SEQ 0x1000
890 #define GRE_STRICT 0x0800
891 #define GRE_REC 0x0700
892 #define GRE_FLAGS 0x00F8
893 #define GRE_VERSION 0x0007
895 /* VXLAN protocol header */
897 ovs_16aligned_be32 vx_flags;
898 ovs_16aligned_be32 vx_vni;
901 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
903 void format_ipv6_addr(char *addr_str, const struct in6_addr *addr);
904 void print_ipv6_addr(struct ds *string, const struct in6_addr *addr);
905 void print_ipv6_mapped(struct ds *string, const struct in6_addr *addr);
906 void print_ipv6_masked(struct ds *string, const struct in6_addr *addr,
907 const struct in6_addr *mask);
908 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
909 const struct in6_addr *mask);
910 struct in6_addr ipv6_create_mask(int mask);
911 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
912 bool ipv6_is_cidr(const struct in6_addr *netmask);
914 void *eth_compose(struct dp_packet *, const struct eth_addr eth_dst,
915 const struct eth_addr eth_src, uint16_t eth_type,
917 void *snap_compose(struct dp_packet *, const struct eth_addr eth_dst,
918 const struct eth_addr eth_src,
919 unsigned int oui, uint16_t snap_type, size_t size);
920 void packet_set_ipv4(struct dp_packet *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
922 void packet_set_ipv6(struct dp_packet *, uint8_t proto, const ovs_be32 src[4],
923 const ovs_be32 dst[4], uint8_t tc,
924 ovs_be32 fl, uint8_t hlmit);
925 void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
926 void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
927 void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
928 void packet_set_nd(struct dp_packet *, const ovs_be32 target[4],
929 const struct eth_addr sll, const struct eth_addr tll);
931 void packet_format_tcp_flags(struct ds *, uint16_t);
932 const char *packet_tcp_flag_to_string(uint32_t flag);
933 void compose_arp(struct dp_packet *, uint16_t arp_op,
934 const struct eth_addr arp_sha,
935 const struct eth_addr arp_tha, bool broadcast,
936 ovs_be32 arp_spa, ovs_be32 arp_tpa);
937 uint32_t packet_csum_pseudoheader(const struct ip_header *);
939 #endif /* packets.h */