ovn-northd: Allow lport 'addresses' to store multiple ips in each set
[cascardo/ovs.git] / lib / packets.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 "geneve.h"
27 #include "openvswitch/types.h"
28 #include "odp-netlink.h"
29 #include "random.h"
30 #include "hash.h"
31 #include "tun-metadata.h"
32 #include "unaligned.h"
33 #include "util.h"
34
35 struct dp_packet;
36 struct ds;
37
38 /* Tunnel information used in flow key and metadata. */
39 struct flow_tnl {
40     ovs_be32 ip_dst;
41     struct in6_addr ipv6_dst;
42     ovs_be32 ip_src;
43     struct in6_addr ipv6_src;
44     ovs_be64 tun_id;
45     uint16_t flags;
46     uint8_t ip_tos;
47     uint8_t ip_ttl;
48     ovs_be16 tp_src;
49     ovs_be16 tp_dst;
50     ovs_be16 gbp_id;
51     uint8_t  gbp_flags;
52     uint8_t  pad1[5];        /* Pad to 64 bits. */
53     struct tun_metadata metadata;
54 };
55
56 /* Some flags are exposed through OpenFlow while others are used only
57  * internally. */
58
59 /* Public flags */
60 #define FLOW_TNL_F_OAM (1 << 0)
61
62 #define FLOW_TNL_PUB_F_MASK ((1 << 1) - 1)
63
64 /* Private flags */
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)
68
69 #define FLOW_TNL_F_MASK ((1 << 4) - 1)
70
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. */
73
74 /* Tunnel information is in userspace datapath format. */
75 #define FLOW_TNL_F_UDPIF (1 << 4)
76
77 static inline bool ipv6_addr_is_set(const struct in6_addr *addr);
78
79 static inline bool
80 flow_tnl_dst_is_set(const struct flow_tnl *tnl)
81 {
82     return tnl->ip_dst || ipv6_addr_is_set(&tnl->ipv6_dst);
83 }
84
85 struct in6_addr flow_tnl_dst(const struct flow_tnl *tnl);
86 struct in6_addr flow_tnl_src(const struct flow_tnl *tnl);
87
88 /* Returns an offset to 'src' covering all the meaningful fields in 'src'. */
89 static inline size_t
90 flow_tnl_size(const struct flow_tnl *src)
91 {
92     if (!flow_tnl_dst_is_set(src)) {
93         /* Covers ip_dst and ipv6_dst only. */
94         return offsetof(struct flow_tnl, ip_src);
95     }
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;
100     }
101     if (!src->metadata.present.map) {
102         /* No TLVs, opts is irrelevant. */
103         return offsetof(struct flow_tnl, metadata.opts);
104     }
105     /* Have decoded TLVs, opts is relevant. */
106     return sizeof *src;
107 }
108
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. */
112 static inline void
113 flow_tnl_copy__(struct flow_tnl *dst, const struct flow_tnl *src)
114 {
115     memcpy(dst, src, flow_tnl_size(src));
116 }
117
118 static inline bool
119 flow_tnl_equal(const struct flow_tnl *a, const struct flow_tnl *b)
120 {
121     size_t a_size = flow_tnl_size(a);
122
123     return a_size == flow_tnl_size(b) && !memcmp(a, b, a_size);
124 }
125
126 /* Unfortunately, a "struct flow" sometimes has to handle OpenFlow port
127  * numbers and other times datapath (dpif) port numbers.  This union allows
128  * access to both. */
129 union flow_in_port {
130     odp_port_t odp_port;
131     ofp_port_t ofp_port;
132 };
133
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
140                                    action. */
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. */
151 };
152
153 static inline void
154 pkt_metadata_init(struct pkt_metadata *md, odp_port_t port)
155 {
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
158      * looked at. */
159     memset(md, 0, offsetof(struct pkt_metadata, in_port));
160     md->tunnel.ip_dst = 0;
161     md->tunnel.ipv6_dst = in6addr_any;
162
163     md->in_port.odp_port = port;
164 }
165
166 /* This function prefetches the cachelines touched by pkt_metadata_init()
167  * For performance reasons the two functions should be kept in sync. */
168 static inline void
169 pkt_metadata_prefetch_init(struct pkt_metadata *md)
170 {
171     ovs_prefetch_range(md, offsetof(struct pkt_metadata, tunnel.ip_src));
172 }
173
174 bool dpid_from_string(const char *s, uint64_t *dpidp);
175
176 #define ETH_ADDR_LEN           6
177
178 static const struct eth_addr eth_addr_broadcast OVS_UNUSED
179     = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
180
181 static const struct eth_addr eth_addr_exact OVS_UNUSED
182     = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
183
184 static const struct eth_addr eth_addr_zero OVS_UNUSED
185     = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } };
186
187 static const struct eth_addr eth_addr_stp OVS_UNUSED
188     = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 } } };
189
190 static const struct eth_addr eth_addr_lacp OVS_UNUSED
191     = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 } } };
192
193 static const struct eth_addr eth_addr_bfd OVS_UNUSED
194     = { { { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 } } };
195
196 static inline bool eth_addr_is_broadcast(const struct eth_addr a)
197 {
198     return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
199 }
200
201 static inline bool eth_addr_is_multicast(const struct eth_addr a)
202 {
203     return a.ea[0] & 1;
204 }
205
206 static inline bool eth_addr_is_local(const struct eth_addr a)
207 {
208     /* Local if it is either a locally administered address or a Nicira random
209      * address. */
210     return a.ea[0] & 2
211         || (a.be16[0] == htons(0x0023)
212             && (a.be16[1] & htons(0xff80)) == htons(0x2080));
213 }
214 static inline bool eth_addr_is_zero(const struct eth_addr a)
215 {
216     return !(a.be16[0] | a.be16[1] | a.be16[2]);
217 }
218
219 static inline int eth_mask_is_exact(const struct eth_addr a)
220 {
221     return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
222 }
223
224 static inline int eth_addr_compare_3way(const struct eth_addr a,
225                                         const struct eth_addr b)
226 {
227     return memcmp(&a, &b, sizeof a);
228 }
229
230 static inline bool eth_addr_equals(const struct eth_addr a,
231                                    const struct eth_addr b)
232 {
233     return !eth_addr_compare_3way(a, b);
234 }
235
236 static inline bool eth_addr_equal_except(const struct eth_addr a,
237                                          const struct eth_addr b,
238                                          const struct eth_addr mask)
239 {
240     return !(((a.be16[0] ^ b.be16[0]) & mask.be16[0])
241              || ((a.be16[1] ^ b.be16[1]) & mask.be16[1])
242              || ((a.be16[2] ^ b.be16[2]) & mask.be16[2]));
243 }
244
245 static inline uint64_t eth_addr_to_uint64(const struct eth_addr ea)
246 {
247     return (((uint64_t) ntohs(ea.be16[0]) << 32)
248             | ((uint64_t) ntohs(ea.be16[1]) << 16)
249             | ntohs(ea.be16[2]));
250 }
251
252 static inline uint64_t eth_addr_vlan_to_uint64(const struct eth_addr ea,
253                                                uint16_t vlan)
254 {
255     return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
256 }
257
258 static inline void eth_addr_from_uint64(uint64_t x, struct eth_addr *ea)
259 {
260     ea->be16[0] = htons(x >> 32);
261     ea->be16[1] = htons((x & 0xFFFF0000) >> 16);
262     ea->be16[2] = htons(x & 0xFFFF);
263 }
264
265 static inline struct eth_addr eth_addr_invert(const struct eth_addr src)
266 {
267     struct eth_addr dst;
268
269     for (int i = 0; i < ARRAY_SIZE(src.be16); i++) {
270         dst.be16[i] = ~src.be16[i];
271     }
272
273     return dst;
274 }
275
276 static inline void eth_addr_mark_random(struct eth_addr *ea)
277 {
278     ea->ea[0] &= ~1;                /* Unicast. */
279     ea->ea[0] |= 2;                 /* Private. */
280 }
281
282 static inline void eth_addr_random(struct eth_addr *ea)
283 {
284     random_bytes((uint8_t *)ea, sizeof *ea);
285     eth_addr_mark_random(ea);
286 }
287
288 static inline void eth_addr_nicira_random(struct eth_addr *ea)
289 {
290     eth_addr_random(ea);
291
292     /* Set the OUI to the Nicira one. */
293     ea->ea[0] = 0x00;
294     ea->ea[1] = 0x23;
295     ea->ea[2] = 0x20;
296
297     /* Set the top bit to indicate random Nicira address. */
298     ea->ea[3] |= 0x80;
299 }
300 static inline uint32_t hash_mac(const struct eth_addr ea,
301                                 const uint16_t vlan, const uint32_t basis)
302 {
303     return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
304 }
305
306 bool eth_addr_is_reserved(const struct eth_addr);
307 bool eth_addr_from_string(const char *, struct eth_addr *);
308
309 void compose_rarp(struct dp_packet *, const struct eth_addr);
310
311 void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci);
312 void eth_pop_vlan(struct dp_packet *);
313
314 const char *eth_from_hex(const char *hex, struct dp_packet **packetp);
315 void eth_format_masked(const struct eth_addr ea,
316                        const struct eth_addr *mask, struct ds *s);
317
318 void set_mpls_lse(struct dp_packet *, ovs_be32 label);
319 void push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse);
320 void pop_mpls(struct dp_packet *, ovs_be16 ethtype);
321
322 void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
323 void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
324 void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
325 void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
326 ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
327                              ovs_be32 label);
328
329 /* Example:
330  *
331  * struct eth_addr mac;
332  *    [...]
333  * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
334  *
335  */
336 #define ETH_ADDR_FMT                                                    \
337     "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
338 #define ETH_ADDR_ARGS(EA) ETH_ADDR_BYTES_ARGS((EA).ea)
339 #define ETH_ADDR_BYTES_ARGS(EAB) \
340          (EAB)[0], (EAB)[1], (EAB)[2], (EAB)[3], (EAB)[4], (EAB)[5]
341 #define ETH_ADDR_STRLEN 17
342
343 /* Example:
344  *
345  * char *string = "1 00:11:22:33:44:55 2";
346  * struct eth_addr mac;
347  * int a, b;
348  *
349  * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
350  *              &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
351  *     ...
352  * }
353  */
354 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
355 #define ETH_ADDR_SCAN_ARGS(EA) \
356     &(EA).ea[0], &(EA).ea[1], &(EA).ea[2], &(EA).ea[3], &(EA).ea[4], &(EA).ea[5]
357
358 #define ETH_TYPE_IP            0x0800
359 #define ETH_TYPE_ARP           0x0806
360 #define ETH_TYPE_TEB           0x6558
361 #define ETH_TYPE_VLAN_8021Q    0x8100
362 #define ETH_TYPE_VLAN          ETH_TYPE_VLAN_8021Q
363 #define ETH_TYPE_VLAN_8021AD   0x88a8
364 #define ETH_TYPE_IPV6          0x86dd
365 #define ETH_TYPE_LACP          0x8809
366 #define ETH_TYPE_RARP          0x8035
367 #define ETH_TYPE_MPLS          0x8847
368 #define ETH_TYPE_MPLS_MCAST    0x8848
369
370 static inline bool eth_type_mpls(ovs_be16 eth_type)
371 {
372     return eth_type == htons(ETH_TYPE_MPLS) ||
373         eth_type == htons(ETH_TYPE_MPLS_MCAST);
374 }
375
376 static inline bool eth_type_vlan(ovs_be16 eth_type)
377 {
378     return eth_type == htons(ETH_TYPE_VLAN_8021Q) ||
379         eth_type == htons(ETH_TYPE_VLAN_8021AD);
380 }
381
382
383 /* Minimum value for an Ethernet type.  Values below this are IEEE 802.2 frame
384  * lengths. */
385 #define ETH_TYPE_MIN           0x600
386
387 #define ETH_HEADER_LEN 14
388 #define ETH_PAYLOAD_MIN 46
389 #define ETH_PAYLOAD_MAX 1500
390 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
391 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
392 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
393 OVS_PACKED(
394 struct eth_header {
395     struct eth_addr eth_dst;
396     struct eth_addr eth_src;
397     ovs_be16 eth_type;
398 });
399 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
400
401 #define LLC_DSAP_SNAP 0xaa
402 #define LLC_SSAP_SNAP 0xaa
403 #define LLC_CNTL_SNAP 3
404
405 #define LLC_HEADER_LEN 3
406 OVS_PACKED(
407 struct llc_header {
408     uint8_t llc_dsap;
409     uint8_t llc_ssap;
410     uint8_t llc_cntl;
411 });
412 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
413
414 /* LLC field values used for STP frames. */
415 #define STP_LLC_SSAP 0x42
416 #define STP_LLC_DSAP 0x42
417 #define STP_LLC_CNTL 0x03
418
419 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
420                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
421 #define SNAP_HEADER_LEN 5
422 OVS_PACKED(
423 struct snap_header {
424     uint8_t snap_org[3];
425     ovs_be16 snap_type;
426 });
427 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
428
429 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
430 OVS_PACKED(
431 struct llc_snap_header {
432     struct llc_header llc;
433     struct snap_header snap;
434 });
435 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
436
437 #define VLAN_VID_MASK 0x0fff
438 #define VLAN_VID_SHIFT 0
439
440 #define VLAN_PCP_MASK 0xe000
441 #define VLAN_PCP_SHIFT 13
442
443 #define VLAN_CFI 0x1000
444 #define VLAN_CFI_SHIFT 12
445
446 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
447  * returns the VLAN ID in host byte order. */
448 static inline uint16_t
449 vlan_tci_to_vid(ovs_be16 vlan_tci)
450 {
451     return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
452 }
453
454 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
455  * returns the priority code point (PCP) in host byte order. */
456 static inline int
457 vlan_tci_to_pcp(ovs_be16 vlan_tci)
458 {
459     return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
460 }
461
462 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
463  * returns the Canonical Format Indicator (CFI). */
464 static inline int
465 vlan_tci_to_cfi(ovs_be16 vlan_tci)
466 {
467     return (vlan_tci & htons(VLAN_CFI)) != 0;
468 }
469
470 #define VLAN_HEADER_LEN 4
471 struct vlan_header {
472     ovs_be16 vlan_tci;          /* Lowest 12 bits are VLAN ID. */
473     ovs_be16 vlan_next_type;
474 };
475 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
476
477 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
478 OVS_PACKED(
479 struct vlan_eth_header {
480     struct eth_addr veth_dst;
481     struct eth_addr veth_src;
482     ovs_be16 veth_type;         /* Always htons(ETH_TYPE_VLAN). */
483     ovs_be16 veth_tci;          /* Lowest 12 bits are VLAN ID. */
484     ovs_be16 veth_next_type;
485 });
486 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
487
488 /* MPLS related definitions */
489 #define MPLS_TTL_MASK       0x000000ff
490 #define MPLS_TTL_SHIFT      0
491
492 #define MPLS_BOS_MASK       0x00000100
493 #define MPLS_BOS_SHIFT      8
494
495 #define MPLS_TC_MASK        0x00000e00
496 #define MPLS_TC_SHIFT       9
497
498 #define MPLS_LABEL_MASK     0xfffff000
499 #define MPLS_LABEL_SHIFT    12
500
501 #define MPLS_HLEN           4
502
503 struct mpls_hdr {
504     ovs_16aligned_be32 mpls_lse;
505 };
506 BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
507
508 /* Given a mpls label stack entry in network byte order
509  * return mpls label in host byte order */
510 static inline uint32_t
511 mpls_lse_to_label(ovs_be32 mpls_lse)
512 {
513     return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
514 }
515
516 /* Given a mpls label stack entry in network byte order
517  * return mpls tc */
518 static inline uint8_t
519 mpls_lse_to_tc(ovs_be32 mpls_lse)
520 {
521     return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
522 }
523
524 /* Given a mpls label stack entry in network byte order
525  * return mpls ttl */
526 static inline uint8_t
527 mpls_lse_to_ttl(ovs_be32 mpls_lse)
528 {
529     return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
530 }
531
532 /* Set TTL in mpls lse. */
533 static inline void
534 flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
535 {
536     *mpls_lse &= ~htonl(MPLS_TTL_MASK);
537     *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
538 }
539
540 /* Given a mpls label stack entry in network byte order
541  * return mpls BoS bit  */
542 static inline uint8_t
543 mpls_lse_to_bos(ovs_be32 mpls_lse)
544 {
545     return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
546 }
547
548 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
549 #define IP_ARGS(ip)                             \
550     ntohl(ip) >> 24,                            \
551     (ntohl(ip) >> 16) & 0xff,                   \
552     (ntohl(ip) >> 8) & 0xff,                    \
553     ntohl(ip) & 0xff
554
555 /* Example:
556  *
557  * char *string = "1 33.44.55.66 2";
558  * ovs_be32 ip;
559  * int a, b;
560  *
561  * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
562  *     ...
563  * }
564  */
565 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
566 #define IP_SCAN_ARGS(ip)                                    \
567         ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]),    \
568         &((uint8_t *) ip)[1],                               \
569         &((uint8_t *) ip)[2],                               \
570         &((uint8_t *) ip)[3]
571
572 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
573  * high-order 1-bits and 32-N low-order 0-bits. */
574 static inline bool
575 ip_is_cidr(ovs_be32 netmask)
576 {
577     uint32_t x = ~ntohl(netmask);
578     return !(x & (x + 1));
579 }
580 static inline bool
581 ip_is_multicast(ovs_be32 ip)
582 {
583     return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
584 }
585 static inline bool
586 ip_is_local_multicast(ovs_be32 ip)
587 {
588     return (ip & htonl(0xffffff00)) == htonl(0xe0000000);
589 }
590 int ip_count_cidr_bits(ovs_be32 netmask);
591 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
592 bool ip_parse(const char *s, ovs_be32 *ip);
593 char *ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
594     OVS_WARN_UNUSED_RESULT;
595 char *ip_parse_cidr(const char *s, ovs_be32 *ip, unsigned int *plen)
596     OVS_WARN_UNUSED_RESULT;
597 char *ip_parse_masked_len(const char *s, int *n, ovs_be32 *ip, ovs_be32 *mask)
598     OVS_WARN_UNUSED_RESULT;
599 char *ip_parse_cidr_len(const char *s, int *n, ovs_be32 *ip,
600                         unsigned int *plen)
601     OVS_WARN_UNUSED_RESULT;
602
603 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
604 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
605 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
606
607 #ifndef IPPROTO_SCTP
608 #define IPPROTO_SCTP 132
609 #endif
610
611 /* TOS fields. */
612 #define IP_ECN_NOT_ECT 0x0
613 #define IP_ECN_ECT_1 0x01
614 #define IP_ECN_ECT_0 0x02
615 #define IP_ECN_CE 0x03
616 #define IP_ECN_MASK 0x03
617 #define IP_DSCP_MASK 0xfc
618
619 #define IP_VERSION 4
620
621 #define IP_DONT_FRAGMENT  0x4000 /* Don't fragment. */
622 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
623 #define IP_FRAG_OFF_MASK  0x1fff /* Fragment offset. */
624 #define IP_IS_FRAGMENT(ip_frag_off) \
625         ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
626
627 #define IP_HEADER_LEN 20
628 struct ip_header {
629     uint8_t ip_ihl_ver;
630     uint8_t ip_tos;
631     ovs_be16 ip_tot_len;
632     ovs_be16 ip_id;
633     ovs_be16 ip_frag_off;
634     uint8_t ip_ttl;
635     uint8_t ip_proto;
636     ovs_be16 ip_csum;
637     ovs_16aligned_be32 ip_src;
638     ovs_16aligned_be32 ip_dst;
639 };
640
641 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
642
643 #define ICMP_HEADER_LEN 8
644 struct icmp_header {
645     uint8_t icmp_type;
646     uint8_t icmp_code;
647     ovs_be16 icmp_csum;
648     union {
649         struct {
650             ovs_be16 id;
651             ovs_be16 seq;
652         } echo;
653         struct {
654             ovs_be16 empty;
655             ovs_be16 mtu;
656         } frag;
657         ovs_16aligned_be32 gateway;
658     } icmp_fields;
659 };
660 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
661
662 #define IGMP_HEADER_LEN 8
663 struct igmp_header {
664     uint8_t igmp_type;
665     uint8_t igmp_code;
666     ovs_be16 igmp_csum;
667     ovs_16aligned_be32 group;
668 };
669 BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header));
670
671 #define IGMPV3_HEADER_LEN 8
672 struct igmpv3_header {
673     uint8_t type;
674     uint8_t rsvr1;
675     ovs_be16 csum;
676     ovs_be16 rsvr2;
677     ovs_be16 ngrp;
678 };
679 BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header));
680
681 #define IGMPV3_RECORD_LEN 8
682 struct igmpv3_record {
683     uint8_t type;
684     uint8_t aux_len;
685     ovs_be16 nsrcs;
686     ovs_16aligned_be32 maddr;
687 };
688 BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record));
689
690 #define IGMP_HOST_MEMBERSHIP_QUERY    0x11 /* From RFC1112 */
691 #define IGMP_HOST_MEMBERSHIP_REPORT   0x12 /* Ditto */
692 #define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */
693 #define IGMP_HOST_LEAVE_MESSAGE       0x17
694 #define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */
695
696 /*
697  * IGMPv3 and MLDv2 use the same codes.
698  */
699 #define IGMPV3_MODE_IS_INCLUDE 1
700 #define IGMPV3_MODE_IS_EXCLUDE 2
701 #define IGMPV3_CHANGE_TO_INCLUDE_MODE 3
702 #define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4
703 #define IGMPV3_ALLOW_NEW_SOURCES 5
704 #define IGMPV3_BLOCK_OLD_SOURCES 6
705
706 #define SCTP_HEADER_LEN 12
707 struct sctp_header {
708     ovs_be16 sctp_src;
709     ovs_be16 sctp_dst;
710     ovs_16aligned_be32 sctp_vtag;
711     ovs_16aligned_be32 sctp_csum;
712 };
713 BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
714
715 #define UDP_HEADER_LEN 8
716 struct udp_header {
717     ovs_be16 udp_src;
718     ovs_be16 udp_dst;
719     ovs_be16 udp_len;
720     ovs_be16 udp_csum;
721 };
722 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
723
724 #define TCP_FIN 0x001
725 #define TCP_SYN 0x002
726 #define TCP_RST 0x004
727 #define TCP_PSH 0x008
728 #define TCP_ACK 0x010
729 #define TCP_URG 0x020
730 #define TCP_ECE 0x040
731 #define TCP_CWR 0x080
732 #define TCP_NS  0x100
733
734 #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
735 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
736 #define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff))
737 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
738
739 #define TCP_HEADER_LEN 20
740 struct tcp_header {
741     ovs_be16 tcp_src;
742     ovs_be16 tcp_dst;
743     ovs_16aligned_be32 tcp_seq;
744     ovs_16aligned_be32 tcp_ack;
745     ovs_be16 tcp_ctl;
746     ovs_be16 tcp_winsz;
747     ovs_be16 tcp_csum;
748     ovs_be16 tcp_urg;
749 };
750 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
751
752 /* Connection states */
753 enum {
754     CS_NEW_BIT =         0,
755     CS_ESTABLISHED_BIT = 1,
756     CS_RELATED_BIT =     2,
757     CS_REPLY_DIR_BIT =   3,
758     CS_INVALID_BIT =     4,
759     CS_TRACKED_BIT =     5,
760     CS_SRC_NAT_BIT =     6,
761     CS_DST_NAT_BIT =     7,
762 };
763
764 enum {
765     CS_NEW =         (1 << CS_NEW_BIT),
766     CS_ESTABLISHED = (1 << CS_ESTABLISHED_BIT),
767     CS_RELATED =     (1 << CS_RELATED_BIT),
768     CS_REPLY_DIR =   (1 << CS_REPLY_DIR_BIT),
769     CS_INVALID =     (1 << CS_INVALID_BIT),
770     CS_TRACKED =     (1 << CS_TRACKED_BIT),
771     CS_SRC_NAT =     (1 << CS_SRC_NAT_BIT),
772     CS_DST_NAT =     (1 << CS_DST_NAT_BIT),
773 };
774
775 /* Undefined connection state bits. */
776 #define CS_SUPPORTED_MASK    (CS_NEW | CS_ESTABLISHED | CS_RELATED \
777                               | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
778                               | CS_SRC_NAT | CS_DST_NAT)
779 #define CS_UNSUPPORTED_MASK  (~(uint32_t)CS_SUPPORTED_MASK)
780
781 #define ARP_HRD_ETHERNET 1
782 #define ARP_PRO_IP 0x0800
783 #define ARP_OP_REQUEST 1
784 #define ARP_OP_REPLY 2
785 #define ARP_OP_RARP 3
786
787 #define ARP_ETH_HEADER_LEN 28
788 struct arp_eth_header {
789     /* Generic members. */
790     ovs_be16 ar_hrd;           /* Hardware type. */
791     ovs_be16 ar_pro;           /* Protocol type. */
792     uint8_t ar_hln;            /* Hardware address length. */
793     uint8_t ar_pln;            /* Protocol address length. */
794     ovs_be16 ar_op;            /* Opcode. */
795
796     /* Ethernet+IPv4 specific members. */
797     struct eth_addr ar_sha;     /* Sender hardware address. */
798     ovs_16aligned_be32 ar_spa;  /* Sender protocol address. */
799     struct eth_addr ar_tha;     /* Target hardware address. */
800     ovs_16aligned_be32 ar_tpa;  /* Target protocol address. */
801 };
802 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
803
804 #define IPV6_HEADER_LEN 40
805
806 /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
807  * most implementations, this one only requires 16-bit alignment. */
808 union ovs_16aligned_in6_addr {
809     ovs_be16 be16[8];
810     ovs_16aligned_be32 be32[4];
811 };
812
813 /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
814  * one only requires 16-bit alignment. */
815 struct ovs_16aligned_ip6_hdr {
816     union {
817         struct ovs_16aligned_ip6_hdrctl {
818             ovs_16aligned_be32 ip6_un1_flow;
819             ovs_be16 ip6_un1_plen;
820             uint8_t ip6_un1_nxt;
821             uint8_t ip6_un1_hlim;
822         } ip6_un1;
823         uint8_t ip6_un2_vfc;
824     } ip6_ctlun;
825     union ovs_16aligned_in6_addr ip6_src;
826     union ovs_16aligned_in6_addr ip6_dst;
827 };
828
829 /* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
830  * this one only requires 16-bit alignment. */
831 struct ovs_16aligned_ip6_frag {
832     uint8_t ip6f_nxt;
833     uint8_t ip6f_reserved;
834     ovs_be16 ip6f_offlg;
835     ovs_16aligned_be32 ip6f_ident;
836 };
837
838 #define ICMP6_HEADER_LEN 4
839 struct icmp6_header {
840     uint8_t icmp6_type;
841     uint8_t icmp6_code;
842     ovs_be16 icmp6_cksum;
843 };
844 BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header));
845
846 uint32_t packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *);
847
848 /* Neighbor Discovery option field.
849  * ND options are always a multiple of 8 bytes in size. */
850 #define ND_OPT_LEN 8
851 struct ovs_nd_opt {
852     uint8_t  nd_opt_type;      /* Values defined in icmp6.h */
853     uint8_t  nd_opt_len;       /* in units of 8 octets (the size of this struct) */
854     struct eth_addr nd_opt_mac;   /* Ethernet address in the case of SLL or TLL options */
855 };
856 BUILD_ASSERT_DECL(ND_OPT_LEN == sizeof(struct ovs_nd_opt));
857
858 /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit
859  * alignment, this one only requires 16-bit alignment. */
860 #define ND_MSG_LEN 24
861 struct ovs_nd_msg {
862     struct icmp6_header icmph;
863     ovs_16aligned_be32 rco_flags;
864     union ovs_16aligned_in6_addr target;
865     struct ovs_nd_opt options[0];
866 };
867 BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg));
868
869 /*
870  * Use the same struct for MLD and MLD2, naming members as the defined fields in
871  * in the corresponding version of the protocol, though they are reserved in the
872  * other one.
873  */
874 #define MLD_HEADER_LEN 8
875 struct mld_header {
876     uint8_t type;
877     uint8_t code;
878     ovs_be16 csum;
879     ovs_be16 mrd;
880     ovs_be16 ngrp;
881 };
882 BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header));
883
884 #define MLD2_RECORD_LEN 20
885 struct mld2_record {
886     uint8_t type;
887     uint8_t aux_len;
888     ovs_be16 nsrcs;
889     union ovs_16aligned_in6_addr maddr;
890 };
891 BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record));
892
893 #define MLD_QUERY 130
894 #define MLD_REPORT 131
895 #define MLD_DONE 132
896 #define MLD2_REPORT 143
897
898 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
899 #define IPV6_LABEL_MASK 0x000fffff
900
901 /* Example:
902  *
903  * char *string = "1 ::1 2";
904  * char ipv6_s[IPV6_SCAN_LEN + 1];
905  * struct in6_addr ipv6;
906  *
907  * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
908  *     && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
909  *     ...
910  * }
911  */
912 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
913 #define IPV6_SCAN_LEN 46
914
915 extern const struct in6_addr in6addr_exact;
916 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
917                                  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
918
919 extern const struct in6_addr in6addr_all_hosts;
920 #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
921                                      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } }
922
923 static inline bool ipv6_addr_equals(const struct in6_addr *a,
924                                     const struct in6_addr *b)
925 {
926 #ifdef IN6_ARE_ADDR_EQUAL
927     return IN6_ARE_ADDR_EQUAL(a, b);
928 #else
929     return !memcmp(a, b, sizeof(*a));
930 #endif
931 }
932
933 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
934     return ipv6_addr_equals(mask, &in6addr_any);
935 }
936
937 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
938     return ipv6_addr_equals(mask, &in6addr_exact);
939 }
940
941 static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) {
942     return ipv6_addr_equals(addr, &in6addr_all_hosts);
943 }
944
945 static inline bool ipv6_addr_is_set(const struct in6_addr *addr) {
946     return !ipv6_addr_equals(addr, &in6addr_any);
947 }
948
949 static inline bool ipv6_addr_is_multicast(const struct in6_addr *ip) {
950     return ip->s6_addr[0] == 0xff;
951 }
952
953 static inline struct in6_addr
954 in6_addr_mapped_ipv4(ovs_be32 ip4)
955 {
956     struct in6_addr ip6 = { .s6_addr = { [10] = 0xff, [11] = 0xff } };
957     memcpy(&ip6.s6_addr[12], &ip4, 4);
958     return ip6;
959 }
960
961 static inline void
962 in6_addr_set_mapped_ipv4(struct in6_addr *ip6, ovs_be32 ip4)
963 {
964     *ip6 = in6_addr_mapped_ipv4(ip4);
965 }
966
967 static inline ovs_be32
968 in6_addr_get_mapped_ipv4(const struct in6_addr *addr)
969 {
970     union ovs_16aligned_in6_addr *taddr = (void *) addr;
971     if (IN6_IS_ADDR_V4MAPPED(addr)) {
972         return get_16aligned_be32(&taddr->be32[3]);
973     } else {
974         return INADDR_ANY;
975     }
976 }
977
978 static inline void
979 in6_addr_solicited_node(struct in6_addr *addr, const struct in6_addr *ip6)
980 {
981     union ovs_16aligned_in6_addr *taddr = (void *) addr;
982     memset(taddr->be16, 0, sizeof(taddr->be16));
983     taddr->be16[0] = htons(0xff02);
984     taddr->be16[5] = htons(0x1);
985     taddr->be16[6] = htons(0xff00);
986     memcpy(&addr->s6_addr[13], &ip6->s6_addr[13], 3);
987 }
988
989 static inline void
990 ipv6_multicast_to_ethernet(struct eth_addr *eth, const struct in6_addr *ip6)
991 {
992     eth->ea[0] = 0x33;
993     eth->ea[1] = 0x33;
994     eth->ea[2] = ip6->s6_addr[12];
995     eth->ea[3] = ip6->s6_addr[13];
996     eth->ea[4] = ip6->s6_addr[14];
997     eth->ea[5] = ip6->s6_addr[15];
998 }
999
1000 static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
1001 {
1002     return dl_type == htons(ETH_TYPE_IP)
1003         || dl_type == htons(ETH_TYPE_IPV6);
1004 }
1005
1006 /* Tunnel header */
1007
1008 /* GRE protocol header */
1009 struct gre_base_hdr {
1010     ovs_be16 flags;
1011     ovs_be16 protocol;
1012 };
1013
1014 #define GRE_CSUM        0x8000
1015 #define GRE_ROUTING     0x4000
1016 #define GRE_KEY         0x2000
1017 #define GRE_SEQ         0x1000
1018 #define GRE_STRICT      0x0800
1019 #define GRE_REC         0x0700
1020 #define GRE_FLAGS       0x00F8
1021 #define GRE_VERSION     0x0007
1022
1023 /* VXLAN protocol header */
1024 struct vxlanhdr {
1025     ovs_16aligned_be32 vx_flags;
1026     ovs_16aligned_be32 vx_vni;
1027 };
1028
1029 #define VXLAN_FLAGS 0x08000000  /* struct vxlanhdr.vx_flags required value. */
1030
1031 void ipv6_format_addr(const struct in6_addr *addr, struct ds *);
1032 void ipv6_format_addr_bracket(const struct in6_addr *addr, struct ds *,
1033                               bool bracket);
1034 void ipv6_format_mapped(const struct in6_addr *addr, struct ds *);
1035 void ipv6_format_masked(const struct in6_addr *addr,
1036                         const struct in6_addr *mask, struct ds *);
1037 const char * ipv6_string_mapped(char *addr_str, const struct in6_addr *addr);
1038 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
1039                                  const struct in6_addr *mask);
1040 struct in6_addr ipv6_create_mask(int mask);
1041 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
1042 bool ipv6_is_cidr(const struct in6_addr *netmask);
1043
1044 bool ipv6_parse(const char *s, struct in6_addr *ip);
1045 char *ipv6_parse_masked(const char *s, struct in6_addr *ipv6,
1046                         struct in6_addr *mask);
1047 char *ipv6_parse_cidr(const char *s, struct in6_addr *ip, unsigned int *plen)
1048     OVS_WARN_UNUSED_RESULT;
1049 char *ipv6_parse_masked_len(const char *s, int *n, struct in6_addr *ipv6,
1050                             struct in6_addr *mask);
1051 char *ipv6_parse_cidr_len(const char *s, int *n, struct in6_addr *ip,
1052                           unsigned int *plen)
1053     OVS_WARN_UNUSED_RESULT;
1054
1055 void *eth_compose(struct dp_packet *, const struct eth_addr eth_dst,
1056                   const struct eth_addr eth_src, uint16_t eth_type,
1057                   size_t size);
1058 void *snap_compose(struct dp_packet *, const struct eth_addr eth_dst,
1059                    const struct eth_addr eth_src,
1060                    unsigned int oui, uint16_t snap_type, size_t size);
1061 void packet_set_ipv4(struct dp_packet *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
1062                      uint8_t ttl);
1063 void packet_set_ipv6(struct dp_packet *, uint8_t proto, const ovs_be32 src[4],
1064                      const ovs_be32 dst[4], uint8_t tc,
1065                      ovs_be32 fl, uint8_t hlmit);
1066 void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1067 void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1068 void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1069 void packet_set_icmp(struct dp_packet *, uint8_t type, uint8_t code);
1070 void packet_set_nd(struct dp_packet *, const ovs_be32 target[4],
1071                    const struct eth_addr sll, const struct eth_addr tll);
1072
1073 void packet_format_tcp_flags(struct ds *, uint16_t);
1074 const char *packet_tcp_flag_to_string(uint32_t flag);
1075 void compose_arp(struct dp_packet *, uint16_t arp_op,
1076                  const struct eth_addr arp_sha,
1077                  const struct eth_addr arp_tha, bool broadcast,
1078                  ovs_be32 arp_spa, ovs_be32 arp_tpa);
1079 void compose_nd(struct dp_packet *, const struct eth_addr eth_src,
1080                 struct in6_addr *, struct in6_addr *);
1081 uint32_t packet_csum_pseudoheader(const struct ip_header *);
1082
1083 #endif /* packets.h */