userspace: Define and use struct eth_addr.
[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 "random.h"
29 #include "hash.h"
30 #include "tun-metadata.h"
31 #include "util.h"
32
33 struct dp_packet;
34 struct ds;
35
36 /* Tunnel information used in flow key and metadata. */
37 struct flow_tnl {
38     ovs_be32 ip_dst;
39     ovs_be32 ip_src;
40     ovs_be64 tun_id;
41     uint16_t flags;
42     uint8_t ip_tos;
43     uint8_t ip_ttl;
44     ovs_be16 tp_src;
45     ovs_be16 tp_dst;
46     ovs_be16 gbp_id;
47     uint8_t  gbp_flags;
48     uint8_t  pad1[5];        /* Pad to 64 bits. */
49     struct tun_metadata metadata;
50 };
51
52 /* Some flags are exposed through OpenFlow while others are used only
53  * internally. */
54
55 /* Public flags */
56 #define FLOW_TNL_F_OAM (1 << 0)
57
58 #define FLOW_TNL_PUB_F_MASK ((1 << 1) - 1)
59
60 /* Private flags */
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)
64
65 #define FLOW_TNL_F_MASK ((1 << 4) - 1)
66
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. */
69
70 /* Tunnel information is in userspace datapath format. */
71 #define FLOW_TNL_F_UDPIF (1 << 4)
72
73 /* Returns an offset to 'src' covering all the meaningful fields in 'src'. */
74 static inline size_t
75 flow_tnl_size(const struct flow_tnl *src)
76 {
77     if (!src->ip_dst) {
78         /* Covers ip_dst only. */
79         return offsetof(struct flow_tnl, ip_src);
80     }
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;
85     }
86     if (!src->metadata.present.map) {
87         /* No TLVs, opts is irrelevant. */
88         return offsetof(struct flow_tnl, metadata.opts);
89     }
90     /* Have decoded TLVs, opts is relevant. */
91     return sizeof *src;
92 }
93
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. */
97 static inline void
98 flow_tnl_copy__(struct flow_tnl *dst, const struct flow_tnl *src)
99 {
100     memcpy(dst, src, flow_tnl_size(src));
101 }
102
103 static inline bool
104 flow_tnl_equal(const struct flow_tnl *a, const struct flow_tnl *b)
105 {
106     size_t a_size = flow_tnl_size(a);
107
108     return a_size == flow_tnl_size(b) && !memcmp(a, b, a_size);
109 }
110
111 /* Unfortunately, a "struct flow" sometimes has to handle OpenFlow port
112  * numbers and other times datapath (dpif) port numbers.  This union allows
113  * access to both. */
114 union flow_in_port {
115     odp_port_t odp_port;
116     ofp_port_t ofp_port;
117 };
118
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
125                                    action. */
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. */
132 };
133
134 static inline void
135 pkt_metadata_init(struct pkt_metadata *md, odp_port_t port)
136 {
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
139      * looked at. */
140     memset(md, 0, offsetof(struct pkt_metadata, tunnel));
141     md->tunnel.ip_dst = 0;
142
143     md->in_port.odp_port = port;
144 }
145
146 bool dpid_from_string(const char *s, uint64_t *dpidp);
147
148 #define ETH_ADDR_LEN           6
149
150 static const struct eth_addr eth_addr_broadcast OVS_UNUSED
151     = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
152
153 static const struct eth_addr eth_addr_exact OVS_UNUSED
154     = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
155
156 static const struct eth_addr eth_addr_zero OVS_UNUSED
157     = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } };
158
159 static const struct eth_addr eth_addr_stp OVS_UNUSED
160     = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 } } };
161
162 static const struct eth_addr eth_addr_lacp OVS_UNUSED
163     = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 } } };
164
165 static const struct eth_addr eth_addr_bfd OVS_UNUSED
166     = { { { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 } } };
167
168 static inline bool eth_addr_is_broadcast(const struct eth_addr a)
169 {
170     return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
171 }
172
173 static inline bool eth_addr_is_multicast(const struct eth_addr a)
174 {
175     return a.ea[0] & 1;
176 }
177
178 static inline bool eth_addr_is_local(const struct eth_addr a)
179 {
180     /* Local if it is either a locally administered address or a Nicira random
181      * address. */
182     return a.ea[0] & 2
183         || (a.be16[0] == htons(0x0023)
184             && (a.be16[1] & htons(0xff80)) == htons(0x2080));
185 }
186 static inline bool eth_addr_is_zero(const struct eth_addr a)
187 {
188     return !(a.be16[0] | a.be16[1] | a.be16[2]);
189 }
190
191 static inline int eth_mask_is_exact(const struct eth_addr a)
192 {
193     return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
194 }
195
196 static inline int eth_addr_compare_3way(const struct eth_addr a,
197                                         const struct eth_addr b)
198 {
199     return memcmp(&a, &b, sizeof a);
200 }
201
202 static inline bool eth_addr_equals(const struct eth_addr a,
203                                    const struct eth_addr b)
204 {
205     return !eth_addr_compare_3way(a, b);
206 }
207
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)
211 {
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]));
215 }
216
217 static inline uint64_t eth_addr_to_uint64(const struct eth_addr ea)
218 {
219     return (((uint64_t) ntohs(ea.be16[0]) << 32)
220             | ((uint64_t) ntohs(ea.be16[1]) << 16)
221             | ntohs(ea.be16[2]));
222 }
223
224 static inline uint64_t eth_addr_vlan_to_uint64(const struct eth_addr ea,
225                                                uint16_t vlan)
226 {
227     return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
228 }
229
230 static inline void eth_addr_from_uint64(uint64_t x, struct eth_addr *ea)
231 {
232     ea->be16[0] = htons(x >> 32);
233     ea->be16[1] = htons(x >> 16);
234     ea->be16[2] = htons(x);
235 }
236
237 static inline struct eth_addr eth_addr_invert(const struct eth_addr src)
238 {
239     struct eth_addr dst;
240
241     for (int i = 0; i < ARRAY_SIZE(src.be16); i++) {
242         dst.be16[i] = ~src.be16[i];
243     }
244
245     return dst;
246 }
247
248 static inline void eth_addr_mark_random(struct eth_addr *ea)
249 {
250     ea->ea[0] &= ~1;                /* Unicast. */
251     ea->ea[0] |= 2;                 /* Private. */
252 }
253
254 static inline void eth_addr_random(struct eth_addr *ea)
255 {
256     random_bytes((uint8_t *)ea, sizeof *ea);
257     eth_addr_mark_random(ea);
258 }
259
260 static inline void eth_addr_nicira_random(struct eth_addr *ea)
261 {
262     eth_addr_random(ea);
263
264     /* Set the OUI to the Nicira one. */
265     ea->ea[0] = 0x00;
266     ea->ea[1] = 0x23;
267     ea->ea[2] = 0x20;
268
269     /* Set the top bit to indicate random Nicira address. */
270     ea->ea[3] |= 0x80;
271 }
272 static inline uint32_t hash_mac(const struct eth_addr ea,
273                                 const uint16_t vlan, const uint32_t basis)
274 {
275     return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
276 }
277
278 bool eth_addr_is_reserved(const struct eth_addr);
279 bool eth_addr_from_string(const char *, struct eth_addr *);
280
281 void compose_rarp(struct dp_packet *, const struct eth_addr);
282
283 void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci);
284 void eth_pop_vlan(struct dp_packet *);
285
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);
289
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);
293
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,
299                              ovs_be32 label);
300
301 /* Example:
302  *
303  * struct eth_addr mac;
304  *    [...]
305  * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
306  *
307  */
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]
312
313 /* Example:
314  *
315  * char *string = "1 00:11:22:33:44:55 2";
316  * struct eth_addr mac;
317  * int a, b;
318  *
319  * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
320  *              &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
321  *     ...
322  * }
323  */
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]
327
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
339
340 static inline bool eth_type_mpls(ovs_be16 eth_type)
341 {
342     return eth_type == htons(ETH_TYPE_MPLS) ||
343         eth_type == htons(ETH_TYPE_MPLS_MCAST);
344 }
345
346 static inline bool eth_type_vlan(ovs_be16 eth_type)
347 {
348     return eth_type == htons(ETH_TYPE_VLAN_8021Q) ||
349         eth_type == htons(ETH_TYPE_VLAN_8021AD);
350 }
351
352
353 /* Minimum value for an Ethernet type.  Values below this are IEEE 802.2 frame
354  * lengths. */
355 #define ETH_TYPE_MIN           0x600
356
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)
363 OVS_PACKED(
364 struct eth_header {
365     struct eth_addr eth_dst;
366     struct eth_addr eth_src;
367     ovs_be16 eth_type;
368 });
369 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
370
371 #define LLC_DSAP_SNAP 0xaa
372 #define LLC_SSAP_SNAP 0xaa
373 #define LLC_CNTL_SNAP 3
374
375 #define LLC_HEADER_LEN 3
376 OVS_PACKED(
377 struct llc_header {
378     uint8_t llc_dsap;
379     uint8_t llc_ssap;
380     uint8_t llc_cntl;
381 });
382 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
383
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
388
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
392 OVS_PACKED(
393 struct snap_header {
394     uint8_t snap_org[3];
395     ovs_be16 snap_type;
396 });
397 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
398
399 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
400 OVS_PACKED(
401 struct llc_snap_header {
402     struct llc_header llc;
403     struct snap_header snap;
404 });
405 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
406
407 #define VLAN_VID_MASK 0x0fff
408 #define VLAN_VID_SHIFT 0
409
410 #define VLAN_PCP_MASK 0xe000
411 #define VLAN_PCP_SHIFT 13
412
413 #define VLAN_CFI 0x1000
414 #define VLAN_CFI_SHIFT 12
415
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)
420 {
421     return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
422 }
423
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. */
426 static inline int
427 vlan_tci_to_pcp(ovs_be16 vlan_tci)
428 {
429     return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
430 }
431
432 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
433  * returns the Canonical Format Indicator (CFI). */
434 static inline int
435 vlan_tci_to_cfi(ovs_be16 vlan_tci)
436 {
437     return (vlan_tci & htons(VLAN_CFI)) != 0;
438 }
439
440 #define VLAN_HEADER_LEN 4
441 struct vlan_header {
442     ovs_be16 vlan_tci;          /* Lowest 12 bits are VLAN ID. */
443     ovs_be16 vlan_next_type;
444 };
445 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
446
447 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
448 OVS_PACKED(
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;
455 });
456 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
457
458 /* MPLS related definitions */
459 #define MPLS_TTL_MASK       0x000000ff
460 #define MPLS_TTL_SHIFT      0
461
462 #define MPLS_BOS_MASK       0x00000100
463 #define MPLS_BOS_SHIFT      8
464
465 #define MPLS_TC_MASK        0x00000e00
466 #define MPLS_TC_SHIFT       9
467
468 #define MPLS_LABEL_MASK     0xfffff000
469 #define MPLS_LABEL_SHIFT    12
470
471 #define MPLS_HLEN           4
472
473 struct mpls_hdr {
474     ovs_16aligned_be32 mpls_lse;
475 };
476 BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
477
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)
482 {
483     return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
484 }
485
486 /* Given a mpls label stack entry in network byte order
487  * return mpls tc */
488 static inline uint8_t
489 mpls_lse_to_tc(ovs_be32 mpls_lse)
490 {
491     return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
492 }
493
494 /* Given a mpls label stack entry in network byte order
495  * return mpls ttl */
496 static inline uint8_t
497 mpls_lse_to_ttl(ovs_be32 mpls_lse)
498 {
499     return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
500 }
501
502 /* Set TTL in mpls lse. */
503 static inline void
504 flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
505 {
506     *mpls_lse &= ~htonl(MPLS_TTL_MASK);
507     *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
508 }
509
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)
514 {
515     return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
516 }
517
518 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
519 #define IP_ARGS(ip)                             \
520     ntohl(ip) >> 24,                            \
521     (ntohl(ip) >> 16) & 0xff,                   \
522     (ntohl(ip) >> 8) & 0xff,                    \
523     ntohl(ip) & 0xff
524
525 /* Example:
526  *
527  * char *string = "1 33.44.55.66 2";
528  * ovs_be32 ip;
529  * int a, b;
530  *
531  * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
532  *     ...
533  * }
534  */
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],                               \
540         &((uint8_t *) ip)[3]
541
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. */
544 static inline bool
545 ip_is_cidr(ovs_be32 netmask)
546 {
547     uint32_t x = ~ntohl(netmask);
548     return !(x & (x + 1));
549 }
550 static inline bool
551 ip_is_multicast(ovs_be32 ip)
552 {
553     return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
554 }
555 static inline bool
556 ip_is_local_multicast(ovs_be32 ip)
557 {
558     return (ip & htonl(0xffffff00)) == htonl(0xe0000000);
559 }
560 int ip_count_cidr_bits(ovs_be32 netmask);
561 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
562
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))
566
567 #ifndef IPPROTO_SCTP
568 #define IPPROTO_SCTP 132
569 #endif
570
571 /* TOS fields. */
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
578
579 #define IP_VERSION 4
580
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))
586
587 #define IP_HEADER_LEN 20
588 struct ip_header {
589     uint8_t ip_ihl_ver;
590     uint8_t ip_tos;
591     ovs_be16 ip_tot_len;
592     ovs_be16 ip_id;
593     ovs_be16 ip_frag_off;
594     uint8_t ip_ttl;
595     uint8_t ip_proto;
596     ovs_be16 ip_csum;
597     ovs_16aligned_be32 ip_src;
598     ovs_16aligned_be32 ip_dst;
599 };
600
601 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
602
603 #define ICMP_HEADER_LEN 8
604 struct icmp_header {
605     uint8_t icmp_type;
606     uint8_t icmp_code;
607     ovs_be16 icmp_csum;
608     union {
609         struct {
610             ovs_be16 id;
611             ovs_be16 seq;
612         } echo;
613         struct {
614             ovs_be16 empty;
615             ovs_be16 mtu;
616         } frag;
617         ovs_16aligned_be32 gateway;
618     } icmp_fields;
619 };
620 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
621
622 #define IGMP_HEADER_LEN 8
623 struct igmp_header {
624     uint8_t igmp_type;
625     uint8_t igmp_code;
626     ovs_be16 igmp_csum;
627     ovs_16aligned_be32 group;
628 };
629 BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header));
630
631 #define IGMPV3_HEADER_LEN 8
632 struct igmpv3_header {
633     uint8_t type;
634     uint8_t rsvr1;
635     ovs_be16 csum;
636     ovs_be16 rsvr2;
637     ovs_be16 ngrp;
638 };
639 BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header));
640
641 #define IGMPV3_RECORD_LEN 8
642 struct igmpv3_record {
643     uint8_t type;
644     uint8_t aux_len;
645     ovs_be16 nsrcs;
646     ovs_16aligned_be32 maddr;
647 };
648 BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record));
649
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 */
655
656 /*
657  * IGMPv3 and MLDv2 use the same codes.
658  */
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
665
666 #define SCTP_HEADER_LEN 12
667 struct sctp_header {
668     ovs_be16 sctp_src;
669     ovs_be16 sctp_dst;
670     ovs_16aligned_be32 sctp_vtag;
671     ovs_16aligned_be32 sctp_csum;
672 };
673 BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
674
675 #define UDP_HEADER_LEN 8
676 struct udp_header {
677     ovs_be16 udp_src;
678     ovs_be16 udp_dst;
679     ovs_be16 udp_len;
680     ovs_be16 udp_csum;
681 };
682 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
683
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
692 #define TCP_NS  0x100
693
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)
698
699 #define TCP_HEADER_LEN 20
700 struct tcp_header {
701     ovs_be16 tcp_src;
702     ovs_be16 tcp_dst;
703     ovs_16aligned_be32 tcp_seq;
704     ovs_16aligned_be32 tcp_ack;
705     ovs_be16 tcp_ctl;
706     ovs_be16 tcp_winsz;
707     ovs_be16 tcp_csum;
708     ovs_be16 tcp_urg;
709 };
710 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
711
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
717
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. */
726
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. */
732 };
733 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
734
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 {
738     ovs_be16 be16[8];
739     ovs_16aligned_be32 be32[4];
740 };
741
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 {
745     union {
746         struct ovs_16aligned_ip6_hdrctl {
747             ovs_16aligned_be32 ip6_un1_flow;
748             ovs_be16 ip6_un1_plen;
749             uint8_t ip6_un1_nxt;
750             uint8_t ip6_un1_hlim;
751         } ip6_un1;
752         uint8_t ip6_un2_vfc;
753     } ip6_ctlun;
754     union ovs_16aligned_in6_addr ip6_src;
755     union ovs_16aligned_in6_addr ip6_dst;
756 };
757
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 {
761     uint8_t ip6f_nxt;
762     uint8_t ip6f_reserved;
763     ovs_be16 ip6f_offlg;
764     ovs_16aligned_be32 ip6f_ident;
765 };
766
767 #define ICMP6_HEADER_LEN 4
768 struct icmp6_header {
769     uint8_t icmp6_type;
770     uint8_t icmp6_code;
771     ovs_be16 icmp6_cksum;
772 };
773 BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header));
774
775 /* Neighbor Discovery option field.
776  * ND options are always a multiple of 8 bytes in size. */
777 #define ND_OPT_LEN 8
778 struct ovs_nd_opt {
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 */
782 };
783 BUILD_ASSERT_DECL(ND_OPT_LEN == sizeof(struct ovs_nd_opt));
784
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
788 struct ovs_nd_msg {
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];
793 };
794 BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg));
795
796 /*
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
799  * other one.
800  */
801 #define MLD_HEADER_LEN 8
802 struct mld_header {
803     uint8_t type;
804     uint8_t code;
805     ovs_be16 csum;
806     ovs_be16 mrd;
807     ovs_be16 ngrp;
808 };
809 BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header));
810
811 #define MLD2_RECORD_LEN 20
812 struct mld2_record {
813     uint8_t type;
814     uint8_t aux_len;
815     ovs_be16 nsrcs;
816     union ovs_16aligned_in6_addr maddr;
817 };
818 BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record));
819
820 #define MLD_QUERY 130
821 #define MLD_REPORT 131
822 #define MLD_DONE 132
823 #define MLD2_REPORT 143
824
825 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
826 #define IPV6_LABEL_MASK 0x000fffff
827
828 /* Example:
829  *
830  * char *string = "1 ::1 2";
831  * char ipv6_s[IPV6_SCAN_LEN + 1];
832  * struct in6_addr ipv6;
833  *
834  * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
835  *     && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
836  *     ...
837  * }
838  */
839 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
840 #define IPV6_SCAN_LEN 46
841
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 } } }
845
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 } } }
849
850 static inline bool ipv6_addr_equals(const struct in6_addr *a,
851                                     const struct in6_addr *b)
852 {
853 #ifdef IN6_ARE_ADDR_EQUAL
854     return IN6_ARE_ADDR_EQUAL(a, b);
855 #else
856     return !memcmp(a, b, sizeof(*a));
857 #endif
858 }
859
860 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
861     return ipv6_addr_equals(mask, &in6addr_any);
862 }
863
864 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
865     return ipv6_addr_equals(mask, &in6addr_exact);
866 }
867
868 static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) {
869     return ipv6_addr_equals(addr, &in6addr_all_hosts);
870 }
871
872 static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
873 {
874     return dl_type == htons(ETH_TYPE_IP)
875         || dl_type == htons(ETH_TYPE_IPV6);
876 }
877
878 /* Tunnel header */
879
880 /* GRE protocol header */
881 struct gre_base_hdr {
882     ovs_be16 flags;
883     ovs_be16 protocol;
884 };
885
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
894
895 /* VXLAN protocol header */
896 struct vxlanhdr {
897     ovs_16aligned_be32 vx_flags;
898     ovs_16aligned_be32 vx_vni;
899 };
900
901 #define VXLAN_FLAGS 0x08000000  /* struct vxlanhdr.vx_flags required value. */
902
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);
913
914 void *eth_compose(struct dp_packet *, const struct eth_addr eth_dst,
915                   const struct eth_addr eth_src, uint16_t eth_type,
916                   size_t size);
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,
921                      uint8_t ttl);
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);
930
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 *);
938
939 #endif /* packets.h */