18a3b17ca83fffbdd8e2c3e6ab23741bfe400a98
[sliver-openvswitch.git] / lib / packets.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef PACKETS_H
18 #define PACKETS_H 1
19
20 #include <inttypes.h>
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include "compiler.h"
26 #include "flow.h"
27 #include "openvswitch/types.h"
28 #include "random.h"
29 #include "util.h"
30
31 struct ofpbuf;
32 struct ds;
33
34 /* Datapath packet metadata */
35 struct pkt_metadata {
36     struct flow_tnl tunnel;     /* Encapsulating tunnel parameters. */
37     uint32_t skb_priority;      /* Packet priority for QoS. */
38     uint32_t pkt_mark;          /* Packet mark. */
39     union flow_in_port in_port; /* Input port. */
40 };
41
42 #define PKT_METADATA_INITIALIZER(PORT) \
43     (struct pkt_metadata){ { 0, 0, 0, 0, 0, 0}, 0, 0, {(PORT)} }
44
45 #define PKT_METADATA_INITIALIZER_FLOW(FLOW) \
46     (struct pkt_metadata){ (FLOW)->tunnel, (FLOW)->skb_priority, \
47             (FLOW)->pkt_mark, (FLOW)->in_port }
48
49 bool dpid_from_string(const char *s, uint64_t *dpidp);
50
51 #define ETH_ADDR_LEN           6
52
53 static const uint8_t eth_addr_broadcast[ETH_ADDR_LEN] OVS_UNUSED
54     = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
55
56 static const uint8_t eth_addr_stp[ETH_ADDR_LEN] OVS_UNUSED
57     = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 };
58
59 static const uint8_t eth_addr_lacp[ETH_ADDR_LEN] OVS_UNUSED
60     = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 };
61
62 static const uint8_t eth_addr_bfd[ETH_ADDR_LEN] OVS_UNUSED
63     = { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 };
64
65 static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
66 {
67     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
68 }
69
70 static inline bool eth_addr_is_multicast(const uint8_t ea[6])
71 {
72     return ea[0] & 1;
73 }
74 static inline bool eth_addr_is_local(const uint8_t ea[6])
75 {
76     /* Local if it is either a locally administered address or a Nicira random
77      * address. */
78     return ea[0] & 2
79        || (ea[0] == 0x00 && ea[1] == 0x23 && ea[2] == 0x20 && ea[3] & 0x80);
80 }
81 static inline bool eth_addr_is_zero(const uint8_t ea[6])
82 {
83     return !(ea[0] | ea[1] | ea[2] | ea[3] | ea[4] | ea[5]);
84 }
85
86 static inline int eth_mask_is_exact(const uint8_t ea[ETH_ADDR_LEN])
87 {
88     return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
89 }
90
91 static inline int eth_addr_compare_3way(const uint8_t a[ETH_ADDR_LEN],
92                                         const uint8_t b[ETH_ADDR_LEN])
93 {
94     return memcmp(a, b, ETH_ADDR_LEN);
95 }
96 static inline bool eth_addr_equals(const uint8_t a[ETH_ADDR_LEN],
97                                    const uint8_t b[ETH_ADDR_LEN])
98 {
99     return !eth_addr_compare_3way(a, b);
100 }
101 static inline bool eth_addr_equal_except(const uint8_t a[ETH_ADDR_LEN],
102                                     const uint8_t b[ETH_ADDR_LEN],
103                                     const uint8_t mask[ETH_ADDR_LEN])
104 {
105     return !(((a[0] ^ b[0]) & mask[0])
106              || ((a[1] ^ b[1]) & mask[1])
107              || ((a[2] ^ b[2]) & mask[2])
108              || ((a[3] ^ b[3]) & mask[3])
109              || ((a[4] ^ b[4]) & mask[4])
110              || ((a[5] ^ b[5]) & mask[5]));
111 }
112 static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
113 {
114     return (((uint64_t) ea[0] << 40)
115             | ((uint64_t) ea[1] << 32)
116             | ((uint64_t) ea[2] << 24)
117             | ((uint64_t) ea[3] << 16)
118             | ((uint64_t) ea[4] << 8)
119             | ea[5]);
120 }
121 static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
122 {
123     ea[0] = x >> 40;
124     ea[1] = x >> 32;
125     ea[2] = x >> 24;
126     ea[3] = x >> 16;
127     ea[4] = x >> 8;
128     ea[5] = x;
129 }
130 static inline void eth_addr_mark_random(uint8_t ea[ETH_ADDR_LEN])
131 {
132     ea[0] &= ~1;                /* Unicast. */
133     ea[0] |= 2;                 /* Private. */
134 }
135 static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
136 {
137     random_bytes(ea, ETH_ADDR_LEN);
138     eth_addr_mark_random(ea);
139 }
140 static inline void eth_addr_nicira_random(uint8_t ea[ETH_ADDR_LEN])
141 {
142     eth_addr_random(ea);
143
144     /* Set the OUI to the Nicira one. */
145     ea[0] = 0x00;
146     ea[1] = 0x23;
147     ea[2] = 0x20;
148
149     /* Set the top bit to indicate random Nicira address. */
150     ea[3] |= 0x80;
151 }
152
153 bool eth_addr_is_reserved(const uint8_t ea[ETH_ADDR_LEN]);
154 bool eth_addr_from_string(const char *, uint8_t ea[ETH_ADDR_LEN]);
155
156 void compose_rarp(struct ofpbuf *, const uint8_t eth_src[ETH_ADDR_LEN]);
157
158 void eth_push_vlan(struct ofpbuf *, ovs_be16 tpid, ovs_be16 tci);
159 void eth_pop_vlan(struct ofpbuf *);
160
161 const char *eth_from_hex(const char *hex, struct ofpbuf **packetp);
162 void eth_format_masked(const uint8_t eth[ETH_ADDR_LEN],
163                        const uint8_t mask[ETH_ADDR_LEN], struct ds *s);
164 void eth_addr_bitand(const uint8_t src[ETH_ADDR_LEN],
165                      const uint8_t mask[ETH_ADDR_LEN],
166                      uint8_t dst[ETH_ADDR_LEN]);
167
168 void set_mpls_lse(struct ofpbuf *, ovs_be32 label);
169 void push_mpls(struct ofpbuf *packet, ovs_be16 ethtype, ovs_be32 lse);
170 void pop_mpls(struct ofpbuf *, ovs_be16 ethtype);
171
172 void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
173 void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
174 void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
175 void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
176 ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
177                              ovs_be32 label);
178
179 /* Example:
180  *
181  * uint8_t mac[ETH_ADDR_LEN];
182  *    [...]
183  * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
184  *
185  */
186 #define ETH_ADDR_FMT                                                    \
187     "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
188 #define ETH_ADDR_ARGS(ea)                                   \
189     (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
190
191 /* Example:
192  *
193  * char *string = "1 00:11:22:33:44:55 2";
194  * uint8_t mac[ETH_ADDR_LEN];
195  * int a, b;
196  *
197  * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
198  *              &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
199  *     ...
200  * }
201  */
202 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
203 #define ETH_ADDR_SCAN_ARGS(ea) \
204         &(ea)[0], &(ea)[1], &(ea)[2], &(ea)[3], &(ea)[4], &(ea)[5]
205
206 #define ETH_TYPE_IP            0x0800
207 #define ETH_TYPE_ARP           0x0806
208 #define ETH_TYPE_VLAN_8021Q    0x8100
209 #define ETH_TYPE_VLAN          ETH_TYPE_VLAN_8021Q
210 #define ETH_TYPE_VLAN_8021AD   0x88a8
211 #define ETH_TYPE_IPV6          0x86dd
212 #define ETH_TYPE_LACP          0x8809
213 #define ETH_TYPE_RARP          0x8035
214 #define ETH_TYPE_MPLS          0x8847
215 #define ETH_TYPE_MPLS_MCAST    0x8848
216
217 static inline bool eth_type_mpls(ovs_be16 eth_type)
218 {
219     return eth_type == htons(ETH_TYPE_MPLS) ||
220         eth_type == htons(ETH_TYPE_MPLS_MCAST);
221 }
222
223 /* Minimum value for an Ethernet type.  Values below this are IEEE 802.2 frame
224  * lengths. */
225 #define ETH_TYPE_MIN           0x600
226
227 #define ETH_HEADER_LEN 14
228 #define ETH_PAYLOAD_MIN 46
229 #define ETH_PAYLOAD_MAX 1500
230 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
231 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
232 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
233 OVS_PACKED(
234 struct eth_header {
235     uint8_t eth_dst[ETH_ADDR_LEN];
236     uint8_t eth_src[ETH_ADDR_LEN];
237     ovs_be16 eth_type;
238 });
239 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
240
241 #define LLC_DSAP_SNAP 0xaa
242 #define LLC_SSAP_SNAP 0xaa
243 #define LLC_CNTL_SNAP 3
244
245 #define LLC_HEADER_LEN 3
246 OVS_PACKED(
247 struct llc_header {
248     uint8_t llc_dsap;
249     uint8_t llc_ssap;
250     uint8_t llc_cntl;
251 });
252 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
253
254 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
255                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
256 #define SNAP_HEADER_LEN 5
257 OVS_PACKED(
258 struct snap_header {
259     uint8_t snap_org[3];
260     ovs_be16 snap_type;
261 });
262 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
263
264 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
265 OVS_PACKED(
266 struct llc_snap_header {
267     struct llc_header llc;
268     struct snap_header snap;
269 });
270 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
271
272 #define VLAN_VID_MASK 0x0fff
273 #define VLAN_VID_SHIFT 0
274
275 #define VLAN_PCP_MASK 0xe000
276 #define VLAN_PCP_SHIFT 13
277
278 #define VLAN_CFI 0x1000
279 #define VLAN_CFI_SHIFT 12
280
281 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
282  * returns the VLAN ID in host byte order. */
283 static inline uint16_t
284 vlan_tci_to_vid(ovs_be16 vlan_tci)
285 {
286     return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
287 }
288
289 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
290  * returns the priority code point (PCP) in host byte order. */
291 static inline int
292 vlan_tci_to_pcp(ovs_be16 vlan_tci)
293 {
294     return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
295 }
296
297 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
298  * returns the Canonical Format Indicator (CFI). */
299 static inline int
300 vlan_tci_to_cfi(ovs_be16 vlan_tci)
301 {
302     return (vlan_tci & htons(VLAN_CFI)) != 0;
303 }
304
305 #define VLAN_HEADER_LEN 4
306 struct vlan_header {
307     ovs_be16 vlan_tci;          /* Lowest 12 bits are VLAN ID. */
308     ovs_be16 vlan_next_type;
309 };
310 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
311
312 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
313 OVS_PACKED(
314 struct vlan_eth_header {
315     uint8_t veth_dst[ETH_ADDR_LEN];
316     uint8_t veth_src[ETH_ADDR_LEN];
317     ovs_be16 veth_type;         /* Always htons(ETH_TYPE_VLAN). */
318     ovs_be16 veth_tci;          /* Lowest 12 bits are VLAN ID. */
319     ovs_be16 veth_next_type;
320 });
321 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
322
323 /* MPLS related definitions */
324 #define MPLS_TTL_MASK       0x000000ff
325 #define MPLS_TTL_SHIFT      0
326
327 #define MPLS_BOS_MASK       0x00000100
328 #define MPLS_BOS_SHIFT      8
329
330 #define MPLS_TC_MASK        0x00000e00
331 #define MPLS_TC_SHIFT       9
332
333 #define MPLS_LABEL_MASK     0xfffff000
334 #define MPLS_LABEL_SHIFT    12
335
336 #define MPLS_HLEN           4
337
338 struct mpls_hdr {
339     ovs_be32 mpls_lse;
340 };
341 BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
342
343 /* Given a mpls label stack entry in network byte order
344  * return mpls label in host byte order */
345 static inline uint32_t
346 mpls_lse_to_label(ovs_be32 mpls_lse)
347 {
348     return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
349 }
350
351 /* Given a mpls label stack entry in network byte order
352  * return mpls tc */
353 static inline uint8_t
354 mpls_lse_to_tc(ovs_be32 mpls_lse)
355 {
356     return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
357 }
358
359 /* Given a mpls label stack entry in network byte order
360  * return mpls ttl */
361 static inline uint8_t
362 mpls_lse_to_ttl(ovs_be32 mpls_lse)
363 {
364     return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
365 }
366
367 /* Set TTL in mpls lse. */
368 static inline void
369 flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
370 {
371     *mpls_lse &= ~htonl(MPLS_TTL_MASK);
372     *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
373 }
374
375 /* Given a mpls label stack entry in network byte order
376  * return mpls BoS bit  */
377 static inline uint8_t
378 mpls_lse_to_bos(ovs_be32 mpls_lse)
379 {
380     return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
381 }
382
383 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
384 #define IP_ARGS(ip)                             \
385     ntohl(ip) >> 24,                            \
386     (ntohl(ip) >> 16) & 0xff,                   \
387     (ntohl(ip) >> 8) & 0xff,                    \
388     ntohl(ip) & 0xff
389
390 /* Example:
391  *
392  * char *string = "1 33.44.55.66 2";
393  * ovs_be32 ip;
394  * int a, b;
395  *
396  * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
397  *     ...
398  * }
399  */
400 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
401 #define IP_SCAN_ARGS(ip)                                    \
402         ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]),    \
403         &((uint8_t *) ip)[1],                               \
404         &((uint8_t *) ip)[2],                               \
405         &((uint8_t *) ip)[3]
406
407 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
408  * high-order 1-bits and 32-N low-order 0-bits. */
409 static inline bool
410 ip_is_cidr(ovs_be32 netmask)
411 {
412     uint32_t x = ~ntohl(netmask);
413     return !(x & (x + 1));
414 }
415 static inline bool
416 ip_is_multicast(ovs_be32 ip)
417 {
418     return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
419 }
420 int ip_count_cidr_bits(ovs_be32 netmask);
421 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
422
423 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
424 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
425 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
426
427 #ifndef IPPROTO_SCTP
428 #define IPPROTO_SCTP 132
429 #endif
430
431 /* TOS fields. */
432 #define IP_ECN_NOT_ECT 0x0
433 #define IP_ECN_ECT_1 0x01
434 #define IP_ECN_ECT_0 0x02
435 #define IP_ECN_CE 0x03
436 #define IP_ECN_MASK 0x03
437 #define IP_DSCP_MASK 0xfc
438
439 #define IP_VERSION 4
440
441 #define IP_DONT_FRAGMENT  0x4000 /* Don't fragment. */
442 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
443 #define IP_FRAG_OFF_MASK  0x1fff /* Fragment offset. */
444 #define IP_IS_FRAGMENT(ip_frag_off) \
445         ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
446
447 #define IP_HEADER_LEN 20
448 struct ip_header {
449     uint8_t ip_ihl_ver;
450     uint8_t ip_tos;
451     ovs_be16 ip_tot_len;
452     ovs_be16 ip_id;
453     ovs_be16 ip_frag_off;
454     uint8_t ip_ttl;
455     uint8_t ip_proto;
456     ovs_be16 ip_csum;
457     ovs_16aligned_be32 ip_src;
458     ovs_16aligned_be32 ip_dst;
459 };
460 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
461
462 #define ICMP_HEADER_LEN 8
463 struct icmp_header {
464     uint8_t icmp_type;
465     uint8_t icmp_code;
466     ovs_be16 icmp_csum;
467     union {
468         struct {
469             ovs_be16 id;
470             ovs_be16 seq;
471         } echo;
472         struct {
473             ovs_be16 empty;
474             ovs_be16 mtu;
475         } frag;
476         ovs_16aligned_be32 gateway;
477     } icmp_fields;
478     uint8_t icmp_data[0];
479 };
480 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
481
482 #define SCTP_HEADER_LEN 12
483 struct sctp_header {
484     ovs_be16 sctp_src;
485     ovs_be16 sctp_dst;
486     ovs_be32 sctp_vtag;
487     ovs_be32 sctp_csum;
488 };
489 BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
490
491 #define UDP_HEADER_LEN 8
492 struct udp_header {
493     ovs_be16 udp_src;
494     ovs_be16 udp_dst;
495     ovs_be16 udp_len;
496     ovs_be16 udp_csum;
497 };
498 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
499
500 #define TCP_FIN 0x001
501 #define TCP_SYN 0x002
502 #define TCP_RST 0x004
503 #define TCP_PSH 0x008
504 #define TCP_ACK 0x010
505 #define TCP_URG 0x020
506 #define TCP_ECE 0x040
507 #define TCP_CWR 0x080
508 #define TCP_NS  0x100
509
510 #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
511 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
512 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
513
514 #define TCP_HEADER_LEN 20
515 struct tcp_header {
516     ovs_be16 tcp_src;
517     ovs_be16 tcp_dst;
518     ovs_16aligned_be32 tcp_seq;
519     ovs_16aligned_be32 tcp_ack;
520     ovs_be16 tcp_ctl;
521     ovs_be16 tcp_winsz;
522     ovs_be16 tcp_csum;
523     ovs_be16 tcp_urg;
524 };
525 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
526
527 #define ARP_HRD_ETHERNET 1
528 #define ARP_PRO_IP 0x0800
529 #define ARP_OP_REQUEST 1
530 #define ARP_OP_REPLY 2
531 #define ARP_OP_RARP 3
532
533 #define ARP_ETH_HEADER_LEN 28
534 struct arp_eth_header {
535     /* Generic members. */
536     ovs_be16 ar_hrd;           /* Hardware type. */
537     ovs_be16 ar_pro;           /* Protocol type. */
538     uint8_t ar_hln;            /* Hardware address length. */
539     uint8_t ar_pln;            /* Protocol address length. */
540     ovs_be16 ar_op;            /* Opcode. */
541
542     /* Ethernet+IPv4 specific members. */
543     uint8_t ar_sha[ETH_ADDR_LEN]; /* Sender hardware address. */
544     ovs_16aligned_be32 ar_spa;           /* Sender protocol address. */
545     uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
546     ovs_16aligned_be32 ar_tpa;           /* Target protocol address. */
547 };
548 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
549
550 /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
551  * most implementations, this one only requires 16-bit alignment. */
552 union ovs_16aligned_in6_addr {
553     ovs_be16 be16[8];
554     ovs_16aligned_be32 be32[4];
555 };
556
557 /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
558  * one only requires 16-bit alignment. */
559 struct ovs_16aligned_ip6_hdr {
560     union {
561         struct ovs_16aligned_ip6_hdrctl {
562             ovs_16aligned_be32 ip6_un1_flow;
563             ovs_be16 ip6_un1_plen;
564             uint8_t ip6_un1_nxt;
565             uint8_t ip6_un1_hlim;
566         } ip6_un1;
567         uint8_t ip6_un2_vfc;
568     } ip6_ctlun;
569     union ovs_16aligned_in6_addr ip6_src;
570     union ovs_16aligned_in6_addr ip6_dst;
571 };
572
573 /* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
574  * this one only requires 16-bit alignment. */
575 struct ovs_16aligned_ip6_frag {
576     uint8_t ip6f_nxt;
577     uint8_t ip6f_reserved;
578     ovs_be16 ip6f_offlg;
579     ovs_16aligned_be32 ip6f_ident;
580 };
581
582 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
583 #define IPV6_LABEL_MASK 0x000fffff
584
585 /* Example:
586  *
587  * char *string = "1 ::1 2";
588  * char ipv6_s[IPV6_SCAN_LEN + 1];
589  * struct in6_addr ipv6;
590  *
591  * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
592  *     && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
593  *     ...
594  * }
595  */
596 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
597 #define IPV6_SCAN_LEN 46
598
599 extern const struct in6_addr in6addr_exact;
600 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
601                                  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
602
603 static inline bool ipv6_addr_equals(const struct in6_addr *a,
604                                     const struct in6_addr *b)
605 {
606 #ifdef IN6_ARE_ADDR_EQUAL
607     return IN6_ARE_ADDR_EQUAL(a, b);
608 #else
609     return !memcmp(a, b, sizeof(*a));
610 #endif
611 }
612
613 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
614     return ipv6_addr_equals(mask, &in6addr_any);
615 }
616
617 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
618     return ipv6_addr_equals(mask, &in6addr_exact);
619 }
620
621 static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
622 {
623     return dl_type == htons(ETH_TYPE_IP)
624         || dl_type == htons(ETH_TYPE_IPV6);
625 }
626
627 static inline bool is_ip_any(const struct flow *flow)
628 {
629     return dl_type_is_ip_any(flow->dl_type);
630 }
631
632 static inline bool is_icmpv4(const struct flow *flow)
633 {
634     return (flow->dl_type == htons(ETH_TYPE_IP)
635             && flow->nw_proto == IPPROTO_ICMP);
636 }
637
638 static inline bool is_icmpv6(const struct flow *flow)
639 {
640     return (flow->dl_type == htons(ETH_TYPE_IPV6)
641             && flow->nw_proto == IPPROTO_ICMPV6);
642 }
643
644 void format_ipv6_addr(char *addr_str, const struct in6_addr *addr);
645 void print_ipv6_addr(struct ds *string, const struct in6_addr *addr);
646 void print_ipv6_masked(struct ds *string, const struct in6_addr *addr,
647                        const struct in6_addr *mask);
648 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
649                                  const struct in6_addr *mask);
650 struct in6_addr ipv6_create_mask(int mask);
651 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
652 bool ipv6_is_cidr(const struct in6_addr *netmask);
653
654 void *eth_compose(struct ofpbuf *, const uint8_t eth_dst[ETH_ADDR_LEN],
655                   const uint8_t eth_src[ETH_ADDR_LEN], uint16_t eth_type,
656                   size_t size);
657 void *snap_compose(struct ofpbuf *, const uint8_t eth_dst[ETH_ADDR_LEN],
658                    const uint8_t eth_src[ETH_ADDR_LEN],
659                    unsigned int oui, uint16_t snap_type, size_t size);
660 void packet_set_ipv4(struct ofpbuf *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
661                      uint8_t ttl);
662 void packet_set_ipv6(struct ofpbuf *, uint8_t proto, const ovs_be32 src[4],
663                      const ovs_be32 dst[4], uint8_t tc,
664                      ovs_be32 fl, uint8_t hlmit);
665 void packet_set_tcp_port(struct ofpbuf *, ovs_be16 src, ovs_be16 dst);
666 void packet_set_udp_port(struct ofpbuf *, ovs_be16 src, ovs_be16 dst);
667 void packet_set_sctp_port(struct ofpbuf *, ovs_be16 src, ovs_be16 dst);
668
669 void packet_format_tcp_flags(struct ds *, uint16_t);
670 const char *packet_tcp_flag_to_string(uint32_t flag);
671
672 #endif /* packets.h */