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 uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
151 = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
153 static const uint8_t eth_addr_zero[ETH_ADDR_LEN] OVS_UNUSED
154 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
156 static const uint8_t eth_addr_stp[ETH_ADDR_LEN] OVS_UNUSED
157 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 };
159 static const uint8_t eth_addr_lacp[ETH_ADDR_LEN] OVS_UNUSED
160 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 };
162 static const uint8_t eth_addr_bfd[ETH_ADDR_LEN] OVS_UNUSED
163 = { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 };
165 static inline bool eth_addr_is_broadcast(const uint8_t ea[ETH_ADDR_LEN])
167 return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
170 static inline bool eth_addr_is_multicast(const uint8_t ea[ETH_ADDR_LEN])
174 static inline bool eth_addr_is_local(const uint8_t ea[ETH_ADDR_LEN])
176 /* Local if it is either a locally administered address or a Nicira random
179 || (ea[0] == 0x00 && ea[1] == 0x23 && ea[2] == 0x20 && ea[3] & 0x80);
181 static inline bool eth_addr_is_zero(const uint8_t ea[ETH_ADDR_LEN])
183 return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
186 static inline int eth_mask_is_exact(const uint8_t ea[ETH_ADDR_LEN])
188 return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
191 static inline int eth_addr_compare_3way(const uint8_t a[ETH_ADDR_LEN],
192 const uint8_t b[ETH_ADDR_LEN])
194 return memcmp(a, b, ETH_ADDR_LEN);
196 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
197 const uint8_t b[ETH_ADDR_LEN])
199 return !eth_addr_compare_3way(a, b);
201 static inline bool eth_addr_equal_except(const uint8_t a[ETH_ADDR_LEN],
202 const uint8_t b[ETH_ADDR_LEN],
203 const uint8_t mask[ETH_ADDR_LEN])
205 return !(((a[0] ^ b[0]) & mask[0])
206 || ((a[1] ^ b[1]) & mask[1])
207 || ((a[2] ^ b[2]) & mask[2])
208 || ((a[3] ^ b[3]) & mask[3])
209 || ((a[4] ^ b[4]) & mask[4])
210 || ((a[5] ^ b[5]) & mask[5]));
212 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
214 return (((uint64_t) ea[0] << 40)
215 | ((uint64_t) ea[1] << 32)
216 | ((uint64_t) ea[2] << 24)
217 | ((uint64_t) ea[3] << 16)
218 | ((uint64_t) ea[4] << 8)
221 static inline uint64_t eth_addr_vlan_to_uint64(const uint8_t ea[ETH_ADDR_LEN],
224 return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
226 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
235 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
237 ea[0] &= ~1; /* Unicast. */
238 ea[0] |= 2; /* Private. */
240 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
242 random_bytes(ea, ETH_ADDR_LEN);
243 eth_addr_mark_random(ea);
245 static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
249 /* Set the OUI to the Nicira one. */
254 /* Set the top bit to indicate random Nicira address. */
257 static inline uint32_t hash_mac(const uint8_t ea[ETH_ADDR_LEN],
258 const uint16_t vlan, const uint32_t basis)
260 return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
263 bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN]);
264 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
266 void compose_rarp(struct dp_packet *, const uint8_t eth_src[ETH_ADDR_LEN]);
268 void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci);
269 void eth_pop_vlan(struct dp_packet *);
271 const char *eth_from_hex(const char *hex, struct dp_packet **packetp);
272 void eth_format_masked(const uint8_t eth[ETH_ADDR_LEN],
273 const uint8_t mask[ETH_ADDR_LEN], struct ds *s);
274 void eth_addr_bitand(const uint8_t src[ETH_ADDR_LEN],
275 const uint8_t mask[ETH_ADDR_LEN],
276 uint8_t dst[ETH_ADDR_LEN]);
278 void set_mpls_lse(struct dp_packet *, ovs_be32 label);
279 void push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse);
280 void pop_mpls(struct dp_packet *, ovs_be16 ethtype);
282 void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
283 void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
284 void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
285 void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
286 ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
291 * uint8_t mac[ETH_ADDR_LEN];
293 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
296 #define ETH_ADDR_FMT \
297 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
298 #define ETH_ADDR_ARGS(ea) \
299 (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
303 * char *string = "1 00:11:22:33:44:55 2";
304 * uint8_t mac[ETH_ADDR_LEN];
307 * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
308 * &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
312 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
313 #define ETH_ADDR_SCAN_ARGS(ea) \
314 &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
316 #define ETH_TYPE_IP 0x0800
317 #define ETH_TYPE_ARP 0x0806
318 #define ETH_TYPE_TEB 0x6558
319 #define ETH_TYPE_VLAN_8021Q 0x8100
320 #define ETH_TYPE_VLAN ETH_TYPE_VLAN_8021Q
321 #define ETH_TYPE_VLAN_8021AD 0x88a8
322 #define ETH_TYPE_IPV6 0x86dd
323 #define ETH_TYPE_LACP 0x8809
324 #define ETH_TYPE_RARP 0x8035
325 #define ETH_TYPE_MPLS 0x8847
326 #define ETH_TYPE_MPLS_MCAST 0x8848
328 static inline bool eth_type_mpls(ovs_be16 eth_type)
330 return eth_type == htons(ETH_TYPE_MPLS) ||
331 eth_type == htons(ETH_TYPE_MPLS_MCAST);
334 static inline bool eth_type_vlan(ovs_be16 eth_type)
336 return eth_type == htons(ETH_TYPE_VLAN_8021Q) ||
337 eth_type == htons(ETH_TYPE_VLAN_8021AD);
341 /* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame
343 #define ETH_TYPE_MIN 0x600
345 #define ETH_HEADER_LEN 14
346 #define ETH_PAYLOAD_MIN 46
347 #define ETH_PAYLOAD_MAX 1500
348 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
349 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
350 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
353 uint8_t eth_dst[ETH_ADDR_LEN];
354 uint8_t eth_src[ETH_ADDR_LEN];
357 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
359 #define LLC_DSAP_SNAP 0xaa
360 #define LLC_SSAP_SNAP 0xaa
361 #define LLC_CNTL_SNAP 3
363 #define LLC_HEADER_LEN 3
370 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
372 /* LLC field values used for STP frames. */
373 #define STP_LLC_SSAP 0x42
374 #define STP_LLC_DSAP 0x42
375 #define STP_LLC_CNTL 0x03
377 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
378 sizeof(SNAP_ORG_ETHERNET) == 3. */
379 #define SNAP_HEADER_LEN 5
385 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
387 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
389 struct llc_snap_header {
390 struct llc_header llc;
391 struct snap_header snap;
393 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
395 #define VLAN_VID_MASK 0x0fff
396 #define VLAN_VID_SHIFT 0
398 #define VLAN_PCP_MASK 0xe000
399 #define VLAN_PCP_SHIFT 13
401 #define VLAN_CFI 0x1000
402 #define VLAN_CFI_SHIFT 12
404 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
405 * returns the VLAN ID in host byte order. */
406 static inline uint16_t
407 vlan_tci_to_vid(ovs_be16 vlan_tci)
409 return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
412 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
413 * returns the priority code point (PCP) in host byte order. */
415 vlan_tci_to_pcp(ovs_be16 vlan_tci)
417 return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
420 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
421 * returns the Canonical Format Indicator (CFI). */
423 vlan_tci_to_cfi(ovs_be16 vlan_tci)
425 return (vlan_tci & htons(VLAN_CFI)) != 0;
428 #define VLAN_HEADER_LEN 4
430 ovs_be16 vlan_tci; /* Lowest 12 bits are VLAN ID. */
431 ovs_be16 vlan_next_type;
433 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
435 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
437 struct vlan_eth_header {
438 uint8_t veth_dst[ETH_ADDR_LEN];
439 uint8_t veth_src[ETH_ADDR_LEN];
440 ovs_be16 veth_type; /* Always htons(ETH_TYPE_VLAN). */
441 ovs_be16 veth_tci; /* Lowest 12 bits are VLAN ID. */
442 ovs_be16 veth_next_type;
444 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
446 /* MPLS related definitions */
447 #define MPLS_TTL_MASK 0x000000ff
448 #define MPLS_TTL_SHIFT 0
450 #define MPLS_BOS_MASK 0x00000100
451 #define MPLS_BOS_SHIFT 8
453 #define MPLS_TC_MASK 0x00000e00
454 #define MPLS_TC_SHIFT 9
456 #define MPLS_LABEL_MASK 0xfffff000
457 #define MPLS_LABEL_SHIFT 12
462 ovs_16aligned_be32 mpls_lse;
464 BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
466 /* Given a mpls label stack entry in network byte order
467 * return mpls label in host byte order */
468 static inline uint32_t
469 mpls_lse_to_label(ovs_be32 mpls_lse)
471 return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
474 /* Given a mpls label stack entry in network byte order
476 static inline uint8_t
477 mpls_lse_to_tc(ovs_be32 mpls_lse)
479 return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
482 /* Given a mpls label stack entry in network byte order
484 static inline uint8_t
485 mpls_lse_to_ttl(ovs_be32 mpls_lse)
487 return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
490 /* Set TTL in mpls lse. */
492 flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
494 *mpls_lse &= ~htonl(MPLS_TTL_MASK);
495 *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
498 /* Given a mpls label stack entry in network byte order
499 * return mpls BoS bit */
500 static inline uint8_t
501 mpls_lse_to_bos(ovs_be32 mpls_lse)
503 return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
506 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
507 #define IP_ARGS(ip) \
509 (ntohl(ip) >> 16) & 0xff, \
510 (ntohl(ip) >> 8) & 0xff, \
515 * char *string = "1 33.44.55.66 2";
519 * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
523 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
524 #define IP_SCAN_ARGS(ip) \
525 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
526 &((uint8_t *) ip)[1], \
527 &((uint8_t *) ip)[2], \
530 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
531 * high-order 1-bits and 32-N low-order 0-bits. */
533 ip_is_cidr(ovs_be32 netmask)
535 uint32_t x = ~ntohl(netmask);
536 return !(x & (x + 1));
539 ip_is_multicast(ovs_be32 ip)
541 return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
544 ip_is_local_multicast(ovs_be32 ip)
546 return (ip & htonl(0xffffff00)) == htonl(0xe0000000);
548 int ip_count_cidr_bits(ovs_be32 netmask);
549 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
551 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
552 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
553 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
556 #define IPPROTO_SCTP 132
560 #define IP_ECN_NOT_ECT 0x0
561 #define IP_ECN_ECT_1 0x01
562 #define IP_ECN_ECT_0 0x02
563 #define IP_ECN_CE 0x03
564 #define IP_ECN_MASK 0x03
565 #define IP_DSCP_MASK 0xfc
569 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
570 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
571 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
572 #define IP_IS_FRAGMENT(ip_frag_off) \
573 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
575 #define IP_HEADER_LEN 20
581 ovs_be16 ip_frag_off;
585 ovs_16aligned_be32 ip_src;
586 ovs_16aligned_be32 ip_dst;
589 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
591 #define ICMP_HEADER_LEN 8
605 ovs_16aligned_be32 gateway;
608 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
610 #define IGMP_HEADER_LEN 8
615 ovs_16aligned_be32 group;
617 BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header));
619 #define IGMPV3_HEADER_LEN 8
620 struct igmpv3_header {
627 BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header));
629 #define IGMPV3_RECORD_LEN 8
630 struct igmpv3_record {
634 ovs_16aligned_be32 maddr;
636 BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record));
638 #define IGMP_HOST_MEMBERSHIP_QUERY 0x11 /* From RFC1112 */
639 #define IGMP_HOST_MEMBERSHIP_REPORT 0x12 /* Ditto */
640 #define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */
641 #define IGMP_HOST_LEAVE_MESSAGE 0x17
642 #define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */
645 * IGMPv3 and MLDv2 use the same codes.
647 #define IGMPV3_MODE_IS_INCLUDE 1
648 #define IGMPV3_MODE_IS_EXCLUDE 2
649 #define IGMPV3_CHANGE_TO_INCLUDE_MODE 3
650 #define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4
651 #define IGMPV3_ALLOW_NEW_SOURCES 5
652 #define IGMPV3_BLOCK_OLD_SOURCES 6
654 #define SCTP_HEADER_LEN 12
658 ovs_16aligned_be32 sctp_vtag;
659 ovs_16aligned_be32 sctp_csum;
661 BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
663 #define UDP_HEADER_LEN 8
670 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
672 #define TCP_FIN 0x001
673 #define TCP_SYN 0x002
674 #define TCP_RST 0x004
675 #define TCP_PSH 0x008
676 #define TCP_ACK 0x010
677 #define TCP_URG 0x020
678 #define TCP_ECE 0x040
679 #define TCP_CWR 0x080
682 #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
683 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
684 #define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff))
685 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
687 #define TCP_HEADER_LEN 20
691 ovs_16aligned_be32 tcp_seq;
692 ovs_16aligned_be32 tcp_ack;
698 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
700 #define ARP_HRD_ETHERNET 1
701 #define ARP_PRO_IP 0x0800
702 #define ARP_OP_REQUEST 1
703 #define ARP_OP_REPLY 2
704 #define ARP_OP_RARP 3
706 #define ARP_ETH_HEADER_LEN 28
707 struct arp_eth_header {
708 /* Generic members. */
709 ovs_be16 ar_hrd; /* Hardware type. */
710 ovs_be16 ar_pro; /* Protocol type. */
711 uint8_t ar_hln; /* Hardware address length. */
712 uint8_t ar_pln; /* Protocol address length. */
713 ovs_be16 ar_op; /* Opcode. */
715 /* Ethernet+IPv4 specific members. */
716 uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
717 ovs_16aligned_be32 ar_spa; /* Sender protocol address. */
718 uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
719 ovs_16aligned_be32 ar_tpa; /* Target protocol address. */
721 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
723 /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
724 * most implementations, this one only requires 16-bit alignment. */
725 union ovs_16aligned_in6_addr {
727 ovs_16aligned_be32 be32[4];
730 /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
731 * one only requires 16-bit alignment. */
732 struct ovs_16aligned_ip6_hdr {
734 struct ovs_16aligned_ip6_hdrctl {
735 ovs_16aligned_be32 ip6_un1_flow;
736 ovs_be16 ip6_un1_plen;
738 uint8_t ip6_un1_hlim;
742 union ovs_16aligned_in6_addr ip6_src;
743 union ovs_16aligned_in6_addr ip6_dst;
746 /* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
747 * this one only requires 16-bit alignment. */
748 struct ovs_16aligned_ip6_frag {
750 uint8_t ip6f_reserved;
752 ovs_16aligned_be32 ip6f_ident;
755 #define ICMP6_HEADER_LEN 4
756 struct icmp6_header {
759 ovs_be16 icmp6_cksum;
761 BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header));
763 /* Neighbor Discovery option field.
764 * ND options are always a multiple of 8 bytes in size. */
767 uint8_t nd_opt_type; /* Values defined in icmp6.h */
768 uint8_t nd_opt_len; /* in units of 8 octets (the size of this struct) */
769 uint8_t nd_opt_data[6]; /* Ethernet address in the case of SLL or TLL options */
771 BUILD_ASSERT_DECL(ND_OPT_LEN == sizeof(struct ovs_nd_opt));
773 /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit
774 * alignment, this one only requires 16-bit alignment. */
775 #define ND_MSG_LEN 24
777 struct icmp6_header icmph;
778 ovs_16aligned_be32 rco_flags;
779 union ovs_16aligned_in6_addr target;
780 struct ovs_nd_opt options[0];
782 BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg));
785 * Use the same struct for MLD and MLD2, naming members as the defined fields in
786 * in the corresponding version of the protocol, though they are reserved in the
789 #define MLD_HEADER_LEN 8
797 BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header));
799 #define MLD2_RECORD_LEN 20
804 union ovs_16aligned_in6_addr maddr;
806 BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record));
808 #define MLD_QUERY 130
809 #define MLD_REPORT 131
811 #define MLD2_REPORT 143
813 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
814 #define IPV6_LABEL_MASK 0x000fffff
818 * char *string = "1 ::1 2";
819 * char ipv6_s[IPV6_SCAN_LEN + 1];
820 * struct in6_addr ipv6;
822 * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
823 * && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
827 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
828 #define IPV6_SCAN_LEN 46
830 extern const struct in6_addr in6addr_exact;
831 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
832 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
834 extern const struct in6_addr in6addr_all_hosts;
835 #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
836 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } }
838 static inline bool ipv6_addr_equals(const struct in6_addr *a,
839 const struct in6_addr *b)
841 #ifdef IN6_ARE_ADDR_EQUAL
842 return IN6_ARE_ADDR_EQUAL(a, b);
844 return !memcmp(a, b, sizeof(*a));
848 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
849 return ipv6_addr_equals(mask, &in6addr_any);
852 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
853 return ipv6_addr_equals(mask, &in6addr_exact);
856 static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) {
857 return ipv6_addr_equals(addr, &in6addr_all_hosts);
860 static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
862 return dl_type == htons(ETH_TYPE_IP)
863 || dl_type == htons(ETH_TYPE_IPV6);
868 /* GRE protocol header */
869 struct gre_base_hdr {
874 #define GRE_CSUM 0x8000
875 #define GRE_ROUTING 0x4000
876 #define GRE_KEY 0x2000
877 #define GRE_SEQ 0x1000
878 #define GRE_STRICT 0x0800
879 #define GRE_REC 0x0700
880 #define GRE_FLAGS 0x00F8
881 #define GRE_VERSION 0x0007
883 /* VXLAN protocol header */
885 ovs_16aligned_be32 vx_flags;
886 ovs_16aligned_be32 vx_vni;
889 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
891 void format_ipv6_addr(char *addr_str, const struct in6_addr *addr);
892 void print_ipv6_addr(struct ds *string, const struct in6_addr *addr);
893 void print_ipv6_mapped(struct ds *string, const struct in6_addr *addr);
894 void print_ipv6_masked(struct ds *string, const struct in6_addr *addr,
895 const struct in6_addr *mask);
896 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
897 const struct in6_addr *mask);
898 struct in6_addr ipv6_create_mask(int mask);
899 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
900 bool ipv6_is_cidr(const struct in6_addr *netmask);
902 void *eth_compose(struct dp_packet *, const uint8_t eth_dst[ETH_ADDR_LEN],
903 const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
905 void *snap_compose(struct dp_packet *, const uint8_t eth_dst[ETH_ADDR_LEN],
906 const uint8_t eth_src[ETH_ADDR_LEN],
907 unsigned int oui, uint16_t snap_type, size_t size);
908 void packet_set_ipv4(struct dp_packet *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
910 void packet_set_ipv6(struct dp_packet *, uint8_t proto, const ovs_be32 src[4],
911 const ovs_be32 dst[4], uint8_t tc,
912 ovs_be32 fl, uint8_t hlmit);
913 void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
914 void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
915 void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
916 void packet_set_nd(struct dp_packet *, const ovs_be32 target[4],
917 const uint8_t sll[6], const uint8_t tll[6]);
919 void packet_format_tcp_flags(struct ds *, uint16_t);
920 const char *packet_tcp_flag_to_string(uint32_t flag);
921 void compose_arp(struct dp_packet *, uint16_t arp_op,
922 const uint8_t arp_sha[ETH_ADDR_LEN],
923 const uint8_t arp_tha[ETH_ADDR_LEN], bool broadcast,
924 ovs_be32 arp_spa, ovs_be32 arp_tpa);
925 uint32_t packet_csum_pseudoheader(const struct ip_header *);
927 #endif /* packets.h */