]> git.proxmox.com Git - mirror_ovs.git/blob - lib/packets.h
OF support and translation of generic encap and decap
[mirror_ovs.git] / lib / packets.h
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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 <stdint.h>
23 #include <string.h>
24 #include "compiler.h"
25 #include "openvswitch/geneve.h"
26 #include "openvswitch/packets.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 /* Purely internal to OVS userspace. These flags should never be exposed to
39 * the outside world and so aren't included in the flags mask. */
40
41 /* Tunnel information is in userspace datapath format. */
42 #define FLOW_TNL_F_UDPIF (1 << 4)
43
44 static inline bool ipv6_addr_is_set(const struct in6_addr *addr);
45
46 static inline bool
47 flow_tnl_dst_is_set(const struct flow_tnl *tnl)
48 {
49 return tnl->ip_dst || ipv6_addr_is_set(&tnl->ipv6_dst);
50 }
51
52 struct in6_addr flow_tnl_dst(const struct flow_tnl *tnl);
53 struct in6_addr flow_tnl_src(const struct flow_tnl *tnl);
54
55 /* Returns an offset to 'src' covering all the meaningful fields in 'src'. */
56 static inline size_t
57 flow_tnl_size(const struct flow_tnl *src)
58 {
59 if (!flow_tnl_dst_is_set(src)) {
60 /* Covers ip_dst and ipv6_dst only. */
61 return offsetof(struct flow_tnl, ip_src);
62 }
63 if (src->flags & FLOW_TNL_F_UDPIF) {
64 /* Datapath format, cover all options we have. */
65 return offsetof(struct flow_tnl, metadata.opts)
66 + src->metadata.present.len;
67 }
68 if (!src->metadata.present.map) {
69 /* No TLVs, opts is irrelevant. */
70 return offsetof(struct flow_tnl, metadata.opts);
71 }
72 /* Have decoded TLVs, opts is relevant. */
73 return sizeof *src;
74 }
75
76 /* Copy flow_tnl, but avoid copying unused portions of tun_metadata. Unused
77 * data in 'dst' is NOT cleared, so this must not be used in cases where the
78 * uninitialized portion may be hashed over. */
79 static inline void
80 flow_tnl_copy__(struct flow_tnl *dst, const struct flow_tnl *src)
81 {
82 memcpy(dst, src, flow_tnl_size(src));
83 }
84
85 static inline bool
86 flow_tnl_equal(const struct flow_tnl *a, const struct flow_tnl *b)
87 {
88 size_t a_size = flow_tnl_size(a);
89
90 return a_size == flow_tnl_size(b) && !memcmp(a, b, a_size);
91 }
92
93 /* Datapath packet metadata */
94 struct pkt_metadata {
95 uint32_t recirc_id; /* Recirculation id carried with the
96 recirculating packets. 0 for packets
97 received from the wire. */
98 uint32_t dp_hash; /* hash value computed by the recirculation
99 action. */
100 uint32_t skb_priority; /* Packet priority for QoS. */
101 uint32_t pkt_mark; /* Packet mark. */
102 uint8_t ct_state; /* Connection state. */
103 bool ct_orig_tuple_ipv6;
104 uint16_t ct_zone; /* Connection zone. */
105 uint32_t ct_mark; /* Connection mark. */
106 ovs_u128 ct_label; /* Connection label. */
107 union { /* Populated only for non-zero 'ct_state'. */
108 struct ovs_key_ct_tuple_ipv4 ipv4;
109 struct ovs_key_ct_tuple_ipv6 ipv6; /* Used only if */
110 } ct_orig_tuple; /* 'ct_orig_tuple_ipv6' is set */
111 union flow_in_port in_port; /* Input port. */
112 struct flow_tnl tunnel; /* Encapsulating tunnel parameters. Note that
113 * if 'ip_dst' == 0, the rest of the fields may
114 * be uninitialized. */
115 };
116
117 static inline void
118 pkt_metadata_init_tnl(struct pkt_metadata *md)
119 {
120 /* Zero up through the tunnel metadata options. The length and table
121 * are before this and as long as they are empty, the options won't
122 * be looked at. */
123 memset(md, 0, offsetof(struct pkt_metadata, tunnel.metadata.opts));
124 }
125
126 static inline void
127 pkt_metadata_init(struct pkt_metadata *md, odp_port_t port)
128 {
129 /* This is called for every packet in userspace datapath and affects
130 * performance if all the metadata is initialized. Hence, fields should
131 * only be zeroed out when necessary.
132 *
133 * Initialize only till ct_state. Once the ct_state is zeroed out rest
134 * of ct fields will not be looked at unless ct_state != 0.
135 */
136 memset(md, 0, offsetof(struct pkt_metadata, ct_orig_tuple_ipv6));
137
138 /* It can be expensive to zero out all of the tunnel metadata. However,
139 * we can just zero out ip_dst and the rest of the data will never be
140 * looked at. */
141 md->tunnel.ip_dst = 0;
142 md->tunnel.ipv6_dst = in6addr_any;
143 md->in_port.odp_port = port;
144 }
145
146 /* This function prefetches the cachelines touched by pkt_metadata_init()
147 * For performance reasons the two functions should be kept in sync. */
148 static inline void
149 pkt_metadata_prefetch_init(struct pkt_metadata *md)
150 {
151 ovs_prefetch_range(md, offsetof(struct pkt_metadata, tunnel.ip_src));
152 }
153
154 bool dpid_from_string(const char *s, uint64_t *dpidp);
155
156 #define ETH_ADDR_LEN 6
157
158 static const struct eth_addr eth_addr_broadcast OVS_UNUSED
159 = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
160
161 static const struct eth_addr eth_addr_exact OVS_UNUSED
162 = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
163
164 static const struct eth_addr eth_addr_zero OVS_UNUSED
165 = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } };
166 static const struct eth_addr64 eth_addr64_zero OVS_UNUSED
167 = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } };
168
169 static const struct eth_addr eth_addr_stp OVS_UNUSED
170 = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 } } };
171
172 static const struct eth_addr eth_addr_lacp OVS_UNUSED
173 = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 } } };
174
175 static const struct eth_addr eth_addr_bfd OVS_UNUSED
176 = { { { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 } } };
177
178 static inline bool eth_addr_is_broadcast(const struct eth_addr a)
179 {
180 return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
181 }
182
183 static inline bool eth_addr_is_multicast(const struct eth_addr a)
184 {
185 return a.ea[0] & 1;
186 }
187
188 static inline bool eth_addr_is_local(const struct eth_addr a)
189 {
190 /* Local if it is either a locally administered address or a Nicira random
191 * address. */
192 return a.ea[0] & 2
193 || (a.be16[0] == htons(0x0023)
194 && (a.be16[1] & htons(0xff80)) == htons(0x2080));
195 }
196 static inline bool eth_addr_is_zero(const struct eth_addr a)
197 {
198 return !(a.be16[0] | a.be16[1] | a.be16[2]);
199 }
200 static inline bool eth_addr64_is_zero(const struct eth_addr64 a)
201 {
202 return !(a.be16[0] | a.be16[1] | a.be16[2] | a.be16[3]);
203 }
204
205 static inline int eth_mask_is_exact(const struct eth_addr a)
206 {
207 return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
208 }
209
210 static inline int eth_addr_compare_3way(const struct eth_addr a,
211 const struct eth_addr b)
212 {
213 return memcmp(&a, &b, sizeof a);
214 }
215 static inline int eth_addr64_compare_3way(const struct eth_addr64 a,
216 const struct eth_addr64 b)
217 {
218 return memcmp(&a, &b, sizeof a);
219 }
220
221 static inline bool eth_addr_equals(const struct eth_addr a,
222 const struct eth_addr b)
223 {
224 return !eth_addr_compare_3way(a, b);
225 }
226 static inline bool eth_addr64_equals(const struct eth_addr64 a,
227 const struct eth_addr64 b)
228 {
229 return !eth_addr64_compare_3way(a, b);
230 }
231
232 static inline bool eth_addr_equal_except(const struct eth_addr a,
233 const struct eth_addr b,
234 const struct eth_addr mask)
235 {
236 return !(((a.be16[0] ^ b.be16[0]) & mask.be16[0])
237 || ((a.be16[1] ^ b.be16[1]) & mask.be16[1])
238 || ((a.be16[2] ^ b.be16[2]) & mask.be16[2]));
239 }
240
241 static inline uint64_t eth_addr_to_uint64(const struct eth_addr ea)
242 {
243 return (((uint64_t) ntohs(ea.be16[0]) << 32)
244 | ((uint64_t) ntohs(ea.be16[1]) << 16)
245 | ntohs(ea.be16[2]));
246 }
247
248 static inline uint64_t eth_addr_vlan_to_uint64(const struct eth_addr ea,
249 uint16_t vlan)
250 {
251 return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
252 }
253
254 static inline void eth_addr_from_uint64(uint64_t x, struct eth_addr *ea)
255 {
256 ea->be16[0] = htons(x >> 32);
257 ea->be16[1] = htons((x & 0xFFFF0000) >> 16);
258 ea->be16[2] = htons(x & 0xFFFF);
259 }
260
261 static inline struct eth_addr eth_addr_invert(const struct eth_addr src)
262 {
263 struct eth_addr dst;
264
265 for (int i = 0; i < ARRAY_SIZE(src.be16); i++) {
266 dst.be16[i] = ~src.be16[i];
267 }
268
269 return dst;
270 }
271
272 static inline void eth_addr_mark_random(struct eth_addr *ea)
273 {
274 ea->ea[0] &= ~1; /* Unicast. */
275 ea->ea[0] |= 2; /* Private. */
276 }
277
278 static inline void eth_addr_random(struct eth_addr *ea)
279 {
280 random_bytes((uint8_t *)ea, sizeof *ea);
281 eth_addr_mark_random(ea);
282 }
283
284 static inline void eth_addr_nicira_random(struct eth_addr *ea)
285 {
286 eth_addr_random(ea);
287
288 /* Set the OUI to the Nicira one. */
289 ea->ea[0] = 0x00;
290 ea->ea[1] = 0x23;
291 ea->ea[2] = 0x20;
292
293 /* Set the top bit to indicate random Nicira address. */
294 ea->ea[3] |= 0x80;
295 }
296 static inline uint32_t hash_mac(const struct eth_addr ea,
297 const uint16_t vlan, const uint32_t basis)
298 {
299 return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
300 }
301
302 bool eth_addr_is_reserved(const struct eth_addr);
303 bool eth_addr_from_string(const char *, struct eth_addr *);
304
305 void compose_rarp(struct dp_packet *, const struct eth_addr);
306
307 void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci);
308 void eth_pop_vlan(struct dp_packet *);
309
310 const char *eth_from_hex(const char *hex, struct dp_packet **packetp);
311 void eth_format_masked(const struct eth_addr ea,
312 const struct eth_addr *mask, struct ds *s);
313
314 void set_mpls_lse(struct dp_packet *, ovs_be32 label);
315 void push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse);
316 void pop_mpls(struct dp_packet *, ovs_be16 ethtype);
317
318 void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
319 void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
320 void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
321 void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
322 ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
323 ovs_be32 label);
324
325 /* Example:
326 *
327 * struct eth_addr mac;
328 * [...]
329 * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
330 *
331 */
332 #define ETH_ADDR_FMT \
333 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
334 #define ETH_ADDR_ARGS(EA) ETH_ADDR_BYTES_ARGS((EA).ea)
335 #define ETH_ADDR_BYTES_ARGS(EAB) \
336 (EAB)[0], (EAB)[1], (EAB)[2], (EAB)[3], (EAB)[4], (EAB)[5]
337 #define ETH_ADDR_STRLEN 17
338
339 /* Example:
340 *
341 * struct eth_addr64 eui64;
342 * [...]
343 * printf("The EUI-64 address is "ETH_ADDR64_FMT"\n", ETH_ADDR64_ARGS(mac));
344 *
345 */
346 #define ETH_ADDR64_FMT \
347 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":" \
348 "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
349 #define ETH_ADDR64_ARGS(EA) ETH_ADDR64_BYTES_ARGS((EA).ea64)
350 #define ETH_ADDR64_BYTES_ARGS(EAB) \
351 (EAB)[0], (EAB)[1], (EAB)[2], (EAB)[3], \
352 (EAB)[4], (EAB)[5], (EAB)[6], (EAB)[7]
353 #define ETH_ADDR64_STRLEN 23
354
355 /* Example:
356 *
357 * char *string = "1 00:11:22:33:44:55 2";
358 * struct eth_addr mac;
359 * int a, b;
360 *
361 * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
362 * &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
363 * ...
364 * }
365 */
366 #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
367 #define ETH_ADDR_SCAN_ARGS(EA) \
368 &(EA).ea[0], &(EA).ea[1], &(EA).ea[2], &(EA).ea[3], &(EA).ea[4], &(EA).ea[5]
369
370 #define ETH_TYPE_IP 0x0800
371 #define ETH_TYPE_ARP 0x0806
372 #define ETH_TYPE_TEB 0x6558
373 #define ETH_TYPE_VLAN_8021Q 0x8100
374 #define ETH_TYPE_VLAN ETH_TYPE_VLAN_8021Q
375 #define ETH_TYPE_VLAN_8021AD 0x88a8
376 #define ETH_TYPE_IPV6 0x86dd
377 #define ETH_TYPE_LACP 0x8809
378 #define ETH_TYPE_RARP 0x8035
379 #define ETH_TYPE_MPLS 0x8847
380 #define ETH_TYPE_MPLS_MCAST 0x8848
381
382 static inline bool eth_type_mpls(ovs_be16 eth_type)
383 {
384 return eth_type == htons(ETH_TYPE_MPLS) ||
385 eth_type == htons(ETH_TYPE_MPLS_MCAST);
386 }
387
388 static inline bool eth_type_vlan(ovs_be16 eth_type)
389 {
390 return eth_type == htons(ETH_TYPE_VLAN_8021Q) ||
391 eth_type == htons(ETH_TYPE_VLAN_8021AD);
392 }
393
394
395 /* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame
396 * lengths. */
397 #define ETH_TYPE_MIN 0x600
398
399 #define ETH_HEADER_LEN 14
400 #define ETH_PAYLOAD_MIN 46
401 #define ETH_PAYLOAD_MAX 1500
402 #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
403 #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
404 #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
405 struct eth_header {
406 struct eth_addr eth_dst;
407 struct eth_addr eth_src;
408 ovs_be16 eth_type;
409 };
410 BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
411
412 void push_eth(struct dp_packet *packet, const struct eth_addr *dst,
413 const struct eth_addr *src);
414 void pop_eth(struct dp_packet *packet);
415
416 #define LLC_DSAP_SNAP 0xaa
417 #define LLC_SSAP_SNAP 0xaa
418 #define LLC_CNTL_SNAP 3
419
420 #define LLC_HEADER_LEN 3
421 struct llc_header {
422 uint8_t llc_dsap;
423 uint8_t llc_ssap;
424 uint8_t llc_cntl;
425 };
426 BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
427
428 /* LLC field values used for STP frames. */
429 #define STP_LLC_SSAP 0x42
430 #define STP_LLC_DSAP 0x42
431 #define STP_LLC_CNTL 0x03
432
433 #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
434 sizeof(SNAP_ORG_ETHERNET) == 3. */
435 #define SNAP_HEADER_LEN 5
436 OVS_PACKED(
437 struct snap_header {
438 uint8_t snap_org[3];
439 ovs_be16 snap_type;
440 });
441 BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
442
443 #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
444 OVS_PACKED(
445 struct llc_snap_header {
446 struct llc_header llc;
447 struct snap_header snap;
448 });
449 BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
450
451 #define VLAN_VID_MASK 0x0fff
452 #define VLAN_VID_SHIFT 0
453
454 #define VLAN_PCP_MASK 0xe000
455 #define VLAN_PCP_SHIFT 13
456
457 #define VLAN_CFI 0x1000
458 #define VLAN_CFI_SHIFT 12
459
460 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
461 * returns the VLAN ID in host byte order. */
462 static inline uint16_t
463 vlan_tci_to_vid(ovs_be16 vlan_tci)
464 {
465 return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
466 }
467
468 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
469 * returns the priority code point (PCP) in host byte order. */
470 static inline int
471 vlan_tci_to_pcp(ovs_be16 vlan_tci)
472 {
473 return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
474 }
475
476 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
477 * returns the Canonical Format Indicator (CFI). */
478 static inline int
479 vlan_tci_to_cfi(ovs_be16 vlan_tci)
480 {
481 return (vlan_tci & htons(VLAN_CFI)) != 0;
482 }
483
484 #define VLAN_HEADER_LEN 4
485 struct vlan_header {
486 ovs_be16 vlan_tci; /* Lowest 12 bits are VLAN ID. */
487 ovs_be16 vlan_next_type;
488 };
489 BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
490
491 #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
492 struct vlan_eth_header {
493 struct eth_addr veth_dst;
494 struct eth_addr veth_src;
495 ovs_be16 veth_type; /* Always htons(ETH_TYPE_VLAN). */
496 ovs_be16 veth_tci; /* Lowest 12 bits are VLAN ID. */
497 ovs_be16 veth_next_type;
498 };
499 BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
500
501 /* MPLS related definitions */
502 #define MPLS_TTL_MASK 0x000000ff
503 #define MPLS_TTL_SHIFT 0
504
505 #define MPLS_BOS_MASK 0x00000100
506 #define MPLS_BOS_SHIFT 8
507
508 #define MPLS_TC_MASK 0x00000e00
509 #define MPLS_TC_SHIFT 9
510
511 #define MPLS_LABEL_MASK 0xfffff000
512 #define MPLS_LABEL_SHIFT 12
513
514 #define MPLS_HLEN 4
515
516 struct mpls_hdr {
517 ovs_16aligned_be32 mpls_lse;
518 };
519 BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
520
521 /* Given a mpls label stack entry in network byte order
522 * return mpls label in host byte order */
523 static inline uint32_t
524 mpls_lse_to_label(ovs_be32 mpls_lse)
525 {
526 return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
527 }
528
529 /* Given a mpls label stack entry in network byte order
530 * return mpls tc */
531 static inline uint8_t
532 mpls_lse_to_tc(ovs_be32 mpls_lse)
533 {
534 return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
535 }
536
537 /* Given a mpls label stack entry in network byte order
538 * return mpls ttl */
539 static inline uint8_t
540 mpls_lse_to_ttl(ovs_be32 mpls_lse)
541 {
542 return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
543 }
544
545 /* Set TTL in mpls lse. */
546 static inline void
547 flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
548 {
549 *mpls_lse &= ~htonl(MPLS_TTL_MASK);
550 *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
551 }
552
553 /* Given a mpls label stack entry in network byte order
554 * return mpls BoS bit */
555 static inline uint8_t
556 mpls_lse_to_bos(ovs_be32 mpls_lse)
557 {
558 return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
559 }
560
561 #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
562 #define IP_ARGS(ip) \
563 ntohl(ip) >> 24, \
564 (ntohl(ip) >> 16) & 0xff, \
565 (ntohl(ip) >> 8) & 0xff, \
566 ntohl(ip) & 0xff
567
568 /* Example:
569 *
570 * char *string = "1 33.44.55.66 2";
571 * ovs_be32 ip;
572 * int a, b;
573 *
574 * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
575 * ...
576 * }
577 */
578 #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
579 #define IP_SCAN_ARGS(ip) \
580 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
581 &((uint8_t *) ip)[1], \
582 &((uint8_t *) ip)[2], \
583 &((uint8_t *) ip)[3]
584
585 #define IP_PORT_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8":%"SCNu16
586 #define IP_PORT_SCAN_ARGS(ip, port) \
587 ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \
588 &((uint8_t *) ip)[1], \
589 &((uint8_t *) ip)[2], \
590 &((uint8_t *) ip)[3], \
591 ((void) (ovs_be16) *(port), (uint16_t *) port)
592
593 /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
594 * high-order 1-bits and 32-N low-order 0-bits. */
595 static inline bool
596 ip_is_cidr(ovs_be32 netmask)
597 {
598 uint32_t x = ~ntohl(netmask);
599 return !(x & (x + 1));
600 }
601 static inline bool
602 ip_is_multicast(ovs_be32 ip)
603 {
604 return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
605 }
606 static inline bool
607 ip_is_local_multicast(ovs_be32 ip)
608 {
609 return (ip & htonl(0xffffff00)) == htonl(0xe0000000);
610 }
611 int ip_count_cidr_bits(ovs_be32 netmask);
612 void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
613 bool ip_parse(const char *s, ovs_be32 *ip);
614 char *ip_parse_port(const char *s, ovs_be32 *ip, ovs_be16 *port)
615 OVS_WARN_UNUSED_RESULT;
616 char *ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
617 OVS_WARN_UNUSED_RESULT;
618 char *ip_parse_cidr(const char *s, ovs_be32 *ip, unsigned int *plen)
619 OVS_WARN_UNUSED_RESULT;
620 char *ip_parse_masked_len(const char *s, int *n, ovs_be32 *ip, ovs_be32 *mask)
621 OVS_WARN_UNUSED_RESULT;
622 char *ip_parse_cidr_len(const char *s, int *n, ovs_be32 *ip,
623 unsigned int *plen)
624 OVS_WARN_UNUSED_RESULT;
625
626 #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
627 #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
628 #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
629
630 #ifndef IPPROTO_SCTP
631 #define IPPROTO_SCTP 132
632 #endif
633
634 #ifndef IPPROTO_DCCP
635 #define IPPROTO_DCCP 33
636 #endif
637
638 #ifndef IPPROTO_IGMP
639 #define IPPROTO_IGMP 2
640 #endif
641
642 #ifndef IPPROTO_UDPLITE
643 #define IPPROTO_UDPLITE 136
644 #endif
645
646 /* TOS fields. */
647 #define IP_ECN_NOT_ECT 0x0
648 #define IP_ECN_ECT_1 0x01
649 #define IP_ECN_ECT_0 0x02
650 #define IP_ECN_CE 0x03
651 #define IP_ECN_MASK 0x03
652 #define IP_DSCP_MASK 0xfc
653
654 static inline int
655 IP_ECN_is_ce(uint8_t dsfield)
656 {
657 return (dsfield & IP_ECN_MASK) == IP_ECN_CE;
658 }
659
660 #define IP_VERSION 4
661
662 #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */
663 #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
664 #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */
665 #define IP_IS_FRAGMENT(ip_frag_off) \
666 ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
667
668 #define IP_HEADER_LEN 20
669 struct ip_header {
670 uint8_t ip_ihl_ver;
671 uint8_t ip_tos;
672 ovs_be16 ip_tot_len;
673 ovs_be16 ip_id;
674 ovs_be16 ip_frag_off;
675 uint8_t ip_ttl;
676 uint8_t ip_proto;
677 ovs_be16 ip_csum;
678 ovs_16aligned_be32 ip_src;
679 ovs_16aligned_be32 ip_dst;
680 };
681 BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
682
683 /* ICMPv4 types. */
684 #define ICMP4_ECHO_REPLY 0
685 #define ICMP4_DST_UNREACH 3
686 #define ICMP4_SOURCEQUENCH 4
687 #define ICMP4_REDIRECT 5
688 #define ICMP4_ECHO_REQUEST 8
689 #define ICMP4_TIME_EXCEEDED 11
690 #define ICMP4_PARAM_PROB 12
691 #define ICMP4_TIMESTAMP 13
692 #define ICMP4_TIMESTAMPREPLY 14
693 #define ICMP4_INFOREQUEST 15
694 #define ICMP4_INFOREPLY 16
695
696 #define ICMP_HEADER_LEN 8
697 struct icmp_header {
698 uint8_t icmp_type;
699 uint8_t icmp_code;
700 ovs_be16 icmp_csum;
701 union {
702 struct {
703 ovs_be16 id;
704 ovs_be16 seq;
705 } echo;
706 struct {
707 ovs_be16 empty;
708 ovs_be16 mtu;
709 } frag;
710 ovs_16aligned_be32 gateway;
711 } icmp_fields;
712 };
713 BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
714
715 #define IGMP_HEADER_LEN 8
716 struct igmp_header {
717 uint8_t igmp_type;
718 uint8_t igmp_code;
719 ovs_be16 igmp_csum;
720 ovs_16aligned_be32 group;
721 };
722 BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header));
723
724 #define IGMPV3_HEADER_LEN 8
725 struct igmpv3_header {
726 uint8_t type;
727 uint8_t rsvr1;
728 ovs_be16 csum;
729 ovs_be16 rsvr2;
730 ovs_be16 ngrp;
731 };
732 BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header));
733
734 #define IGMPV3_RECORD_LEN 8
735 struct igmpv3_record {
736 uint8_t type;
737 uint8_t aux_len;
738 ovs_be16 nsrcs;
739 ovs_16aligned_be32 maddr;
740 };
741 BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record));
742
743 #define IGMP_HOST_MEMBERSHIP_QUERY 0x11 /* From RFC1112 */
744 #define IGMP_HOST_MEMBERSHIP_REPORT 0x12 /* Ditto */
745 #define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */
746 #define IGMP_HOST_LEAVE_MESSAGE 0x17
747 #define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */
748
749 /*
750 * IGMPv3 and MLDv2 use the same codes.
751 */
752 #define IGMPV3_MODE_IS_INCLUDE 1
753 #define IGMPV3_MODE_IS_EXCLUDE 2
754 #define IGMPV3_CHANGE_TO_INCLUDE_MODE 3
755 #define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4
756 #define IGMPV3_ALLOW_NEW_SOURCES 5
757 #define IGMPV3_BLOCK_OLD_SOURCES 6
758
759 #define SCTP_HEADER_LEN 12
760 struct sctp_header {
761 ovs_be16 sctp_src;
762 ovs_be16 sctp_dst;
763 ovs_16aligned_be32 sctp_vtag;
764 ovs_16aligned_be32 sctp_csum;
765 };
766 BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
767
768 #define UDP_HEADER_LEN 8
769 struct udp_header {
770 ovs_be16 udp_src;
771 ovs_be16 udp_dst;
772 ovs_be16 udp_len;
773 ovs_be16 udp_csum;
774 };
775 BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
776
777 #define TCP_FIN 0x001
778 #define TCP_SYN 0x002
779 #define TCP_RST 0x004
780 #define TCP_PSH 0x008
781 #define TCP_ACK 0x010
782 #define TCP_URG 0x020
783 #define TCP_ECE 0x040
784 #define TCP_CWR 0x080
785 #define TCP_NS 0x100
786
787 #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
788 #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
789 #define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff))
790 #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
791
792 #define TCP_HEADER_LEN 20
793 struct tcp_header {
794 ovs_be16 tcp_src;
795 ovs_be16 tcp_dst;
796 ovs_16aligned_be32 tcp_seq;
797 ovs_16aligned_be32 tcp_ack;
798 ovs_be16 tcp_ctl;
799 ovs_be16 tcp_winsz;
800 ovs_be16 tcp_csum;
801 ovs_be16 tcp_urg;
802 };
803 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
804
805 /* Connection states.
806 *
807 * Names like CS_RELATED are bit values, e.g. 1 << 2.
808 * Names like CS_RELATED_BIT are bit indexes, e.g. 2. */
809 #define CS_STATES \
810 CS_STATE(NEW, 0, "new") \
811 CS_STATE(ESTABLISHED, 1, "est") \
812 CS_STATE(RELATED, 2, "rel") \
813 CS_STATE(REPLY_DIR, 3, "rpl") \
814 CS_STATE(INVALID, 4, "inv") \
815 CS_STATE(TRACKED, 5, "trk") \
816 CS_STATE(SRC_NAT, 6, "snat") \
817 CS_STATE(DST_NAT, 7, "dnat")
818
819 enum {
820 #define CS_STATE(ENUM, INDEX, NAME) \
821 CS_##ENUM = 1 << INDEX, \
822 CS_##ENUM##_BIT = INDEX,
823 CS_STATES
824 #undef CS_STATE
825 };
826
827 /* Undefined connection state bits. */
828 enum {
829 #define CS_STATE(ENUM, INDEX, NAME) +CS_##ENUM
830 CS_SUPPORTED_MASK = CS_STATES
831 #undef CS_STATE
832 };
833 #define CS_UNSUPPORTED_MASK (~(uint32_t)CS_SUPPORTED_MASK)
834
835 #define ARP_HRD_ETHERNET 1
836 #define ARP_PRO_IP 0x0800
837 #define ARP_OP_REQUEST 1
838 #define ARP_OP_REPLY 2
839 #define ARP_OP_RARP 3
840
841 #define ARP_ETH_HEADER_LEN 28
842 struct arp_eth_header {
843 /* Generic members. */
844 ovs_be16 ar_hrd; /* Hardware type. */
845 ovs_be16 ar_pro; /* Protocol type. */
846 uint8_t ar_hln; /* Hardware address length. */
847 uint8_t ar_pln; /* Protocol address length. */
848 ovs_be16 ar_op; /* Opcode. */
849
850 /* Ethernet+IPv4 specific members. */
851 struct eth_addr ar_sha; /* Sender hardware address. */
852 ovs_16aligned_be32 ar_spa; /* Sender protocol address. */
853 struct eth_addr ar_tha; /* Target hardware address. */
854 ovs_16aligned_be32 ar_tpa; /* Target protocol address. */
855 };
856 BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
857
858 #define IPV6_HEADER_LEN 40
859
860 /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
861 * most implementations, this one only requires 16-bit alignment. */
862 union ovs_16aligned_in6_addr {
863 ovs_be16 be16[8];
864 ovs_16aligned_be32 be32[4];
865 };
866
867 /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
868 * one only requires 16-bit alignment. */
869 struct ovs_16aligned_ip6_hdr {
870 union {
871 struct ovs_16aligned_ip6_hdrctl {
872 ovs_16aligned_be32 ip6_un1_flow;
873 ovs_be16 ip6_un1_plen;
874 uint8_t ip6_un1_nxt;
875 uint8_t ip6_un1_hlim;
876 } ip6_un1;
877 uint8_t ip6_un2_vfc;
878 } ip6_ctlun;
879 union ovs_16aligned_in6_addr ip6_src;
880 union ovs_16aligned_in6_addr ip6_dst;
881 };
882
883 /* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
884 * this one only requires 16-bit alignment. */
885 struct ovs_16aligned_ip6_frag {
886 uint8_t ip6f_nxt;
887 uint8_t ip6f_reserved;
888 ovs_be16 ip6f_offlg;
889 ovs_16aligned_be32 ip6f_ident;
890 };
891
892 #define ICMP6_HEADER_LEN 4
893 struct icmp6_header {
894 uint8_t icmp6_type;
895 uint8_t icmp6_code;
896 ovs_be16 icmp6_cksum;
897 };
898 BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header));
899
900 #define ICMP6_ERROR_HEADER_LEN 8
901 struct icmp6_error_header {
902 struct icmp6_header icmp6_base;
903 ovs_be32 icmp6_error_ext;
904 };
905 BUILD_ASSERT_DECL(ICMP6_ERROR_HEADER_LEN == sizeof(struct icmp6_error_header));
906
907 uint32_t packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *);
908 uint16_t packet_csum_upperlayer6(const struct ovs_16aligned_ip6_hdr *,
909 const void *, uint8_t, uint16_t);
910
911 /* Neighbor Discovery option field.
912 * ND options are always a multiple of 8 bytes in size. */
913 #define ND_LLA_OPT_LEN 8
914 struct ovs_nd_lla_opt {
915 uint8_t type; /* One of ND_OPT_*_LINKADDR. */
916 uint8_t len;
917 struct eth_addr mac;
918 };
919 BUILD_ASSERT_DECL(ND_LLA_OPT_LEN == sizeof(struct ovs_nd_lla_opt));
920
921 /* Neighbor Discovery option: Prefix Information. */
922 #define ND_PREFIX_OPT_LEN 32
923 struct ovs_nd_prefix_opt {
924 uint8_t type; /* ND_OPT_PREFIX_INFORMATION. */
925 uint8_t len; /* Always 4. */
926 uint8_t prefix_len;
927 uint8_t la_flags; /* ND_PREFIX_* flags. */
928 ovs_16aligned_be32 valid_lifetime;
929 ovs_16aligned_be32 preferred_lifetime;
930 ovs_16aligned_be32 reserved; /* Always 0. */
931 union ovs_16aligned_in6_addr prefix;
932 };
933 BUILD_ASSERT_DECL(ND_PREFIX_OPT_LEN == sizeof(struct ovs_nd_prefix_opt));
934
935 #define ND_PREFIX_ON_LINK 0x80
936 #define ND_PREFIX_AUTONOMOUS_ADDRESS 0x40
937
938 /* Neighbor Discovery option: MTU. */
939 #define ND_MTU_OPT_LEN 8
940 struct ovs_nd_mtu_opt {
941 uint8_t type; /* ND_OPT_MTU */
942 uint8_t len; /* Always 1. */
943 ovs_be16 reserved; /* Always 0. */
944 ovs_16aligned_be32 mtu;
945 };
946 BUILD_ASSERT_DECL(ND_MTU_OPT_LEN == sizeof(struct ovs_nd_mtu_opt));
947
948 /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit
949 * alignment, this one only requires 16-bit alignment. */
950 #define ND_MSG_LEN 24
951 struct ovs_nd_msg {
952 struct icmp6_header icmph;
953 ovs_16aligned_be32 rso_flags;
954 union ovs_16aligned_in6_addr target;
955 struct ovs_nd_lla_opt options[0];
956 };
957 BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg));
958
959 /* Neighbor Discovery packet flags. */
960 #define ND_RSO_ROUTER 0x80000000
961 #define ND_RSO_SOLICITED 0x40000000
962 #define ND_RSO_OVERRIDE 0x20000000
963
964 #define RA_MSG_LEN 16
965 struct ovs_ra_msg {
966 struct icmp6_header icmph;
967 uint8_t cur_hop_limit;
968 uint8_t mo_flags; /* ND_RA_MANAGED_ADDRESS and ND_RA_OTHER_CONFIG flags. */
969 ovs_be16 router_lifetime;
970 ovs_be32 reachable_time;
971 ovs_be32 retrans_timer;
972 struct ovs_nd_lla_opt options[0];
973 };
974 BUILD_ASSERT_DECL(RA_MSG_LEN == sizeof(struct ovs_ra_msg));
975
976 #define ND_RA_MANAGED_ADDRESS 0x80
977 #define ND_RA_OTHER_CONFIG 0x40
978
979 /*
980 * Use the same struct for MLD and MLD2, naming members as the defined fields in
981 * in the corresponding version of the protocol, though they are reserved in the
982 * other one.
983 */
984 #define MLD_HEADER_LEN 8
985 struct mld_header {
986 uint8_t type;
987 uint8_t code;
988 ovs_be16 csum;
989 ovs_be16 mrd;
990 ovs_be16 ngrp;
991 };
992 BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header));
993
994 #define MLD2_RECORD_LEN 20
995 struct mld2_record {
996 uint8_t type;
997 uint8_t aux_len;
998 ovs_be16 nsrcs;
999 union ovs_16aligned_in6_addr maddr;
1000 };
1001 BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record));
1002
1003 #define MLD_QUERY 130
1004 #define MLD_REPORT 131
1005 #define MLD_DONE 132
1006 #define MLD2_REPORT 143
1007
1008 /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
1009 #define IPV6_LABEL_MASK 0x000fffff
1010
1011 /* Example:
1012 *
1013 * char *string = "1 ::1 2";
1014 * char ipv6_s[IPV6_SCAN_LEN + 1];
1015 * struct in6_addr ipv6;
1016 *
1017 * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
1018 * && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
1019 * ...
1020 * }
1021 */
1022 #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
1023 #define IPV6_SCAN_LEN 46
1024
1025 extern const struct in6_addr in6addr_exact;
1026 #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
1027 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
1028
1029 extern const struct in6_addr in6addr_all_hosts;
1030 #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
1031 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } }
1032
1033 extern const struct in6_addr in6addr_all_routers;
1034 #define IN6ADDR_ALL_ROUTERS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
1035 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02 } } }
1036
1037 static inline bool ipv6_addr_equals(const struct in6_addr *a,
1038 const struct in6_addr *b)
1039 {
1040 #ifdef IN6_ARE_ADDR_EQUAL
1041 return IN6_ARE_ADDR_EQUAL(a, b);
1042 #else
1043 return !memcmp(a, b, sizeof(*a));
1044 #endif
1045 }
1046
1047 /* Checks the IPv6 address in 'mask' for all zeroes. */
1048 static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
1049 return ipv6_addr_equals(mask, &in6addr_any);
1050 }
1051
1052 static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
1053 return ipv6_addr_equals(mask, &in6addr_exact);
1054 }
1055
1056 static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) {
1057 return ipv6_addr_equals(addr, &in6addr_all_hosts);
1058 }
1059
1060 static inline bool ipv6_addr_is_set(const struct in6_addr *addr) {
1061 return !ipv6_addr_equals(addr, &in6addr_any);
1062 }
1063
1064 static inline bool ipv6_addr_is_multicast(const struct in6_addr *ip) {
1065 return ip->s6_addr[0] == 0xff;
1066 }
1067
1068 static inline struct in6_addr
1069 in6_addr_mapped_ipv4(ovs_be32 ip4)
1070 {
1071 struct in6_addr ip6 = { .s6_addr = { [10] = 0xff, [11] = 0xff } };
1072 memcpy(&ip6.s6_addr[12], &ip4, 4);
1073 return ip6;
1074 }
1075
1076 static inline void
1077 in6_addr_set_mapped_ipv4(struct in6_addr *ip6, ovs_be32 ip4)
1078 {
1079 *ip6 = in6_addr_mapped_ipv4(ip4);
1080 }
1081
1082 static inline ovs_be32
1083 in6_addr_get_mapped_ipv4(const struct in6_addr *addr)
1084 {
1085 union ovs_16aligned_in6_addr *taddr = (void *) addr;
1086 if (IN6_IS_ADDR_V4MAPPED(addr)) {
1087 return get_16aligned_be32(&taddr->be32[3]);
1088 } else {
1089 return INADDR_ANY;
1090 }
1091 }
1092
1093 static inline void
1094 in6_addr_solicited_node(struct in6_addr *addr, const struct in6_addr *ip6)
1095 {
1096 union ovs_16aligned_in6_addr *taddr = (void *) addr;
1097 memset(taddr->be16, 0, sizeof(taddr->be16));
1098 taddr->be16[0] = htons(0xff02);
1099 taddr->be16[5] = htons(0x1);
1100 taddr->be16[6] = htons(0xff00);
1101 memcpy(&addr->s6_addr[13], &ip6->s6_addr[13], 3);
1102 }
1103
1104 /*
1105 * Generates ipv6 EUI64 address from the given eth addr
1106 * and prefix and stores it in 'lla'
1107 */
1108 static inline void
1109 in6_generate_eui64(struct eth_addr ea, struct in6_addr *prefix,
1110 struct in6_addr *lla)
1111 {
1112 union ovs_16aligned_in6_addr *taddr = (void *) lla;
1113 union ovs_16aligned_in6_addr *prefix_taddr = (void *) prefix;
1114 taddr->be16[0] = prefix_taddr->be16[0];
1115 taddr->be16[1] = prefix_taddr->be16[1];
1116 taddr->be16[2] = prefix_taddr->be16[2];
1117 taddr->be16[3] = prefix_taddr->be16[3];
1118 taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]);
1119 taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff);
1120 taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]);
1121 taddr->be16[7] = ea.be16[2];
1122 }
1123
1124 /*
1125 * Generates ipv6 link local address from the given eth addr
1126 * with prefix 'fe80::/64' and stores it in 'lla'
1127 */
1128 static inline void
1129 in6_generate_lla(struct eth_addr ea, struct in6_addr *lla)
1130 {
1131 union ovs_16aligned_in6_addr *taddr = (void *) lla;
1132 memset(taddr->be16, 0, sizeof(taddr->be16));
1133 taddr->be16[0] = htons(0xfe80);
1134 taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]);
1135 taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff);
1136 taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]);
1137 taddr->be16[7] = ea.be16[2];
1138 }
1139
1140 /* Returns true if 'addr' is a link local address. Otherwise, false. */
1141 static inline bool
1142 in6_is_lla(struct in6_addr *addr)
1143 {
1144 #ifdef s6_addr32
1145 return addr->s6_addr32[0] == htonl(0xfe800000) && !(addr->s6_addr32[1]);
1146 #else
1147 return addr->s6_addr[0] == 0xfe && addr->s6_addr[1] == 0x80 &&
1148 !(addr->s6_addr[2] | addr->s6_addr[3] | addr->s6_addr[4] |
1149 addr->s6_addr[5] | addr->s6_addr[6] | addr->s6_addr[7]);
1150 #endif
1151 }
1152
1153 static inline void
1154 ipv6_multicast_to_ethernet(struct eth_addr *eth, const struct in6_addr *ip6)
1155 {
1156 eth->ea[0] = 0x33;
1157 eth->ea[1] = 0x33;
1158 eth->ea[2] = ip6->s6_addr[12];
1159 eth->ea[3] = ip6->s6_addr[13];
1160 eth->ea[4] = ip6->s6_addr[14];
1161 eth->ea[5] = ip6->s6_addr[15];
1162 }
1163
1164 static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
1165 {
1166 return dl_type == htons(ETH_TYPE_IP)
1167 || dl_type == htons(ETH_TYPE_IPV6);
1168 }
1169
1170 /* Tunnel header */
1171
1172 /* GRE protocol header */
1173 struct gre_base_hdr {
1174 ovs_be16 flags;
1175 ovs_be16 protocol;
1176 };
1177
1178 #define GRE_CSUM 0x8000
1179 #define GRE_ROUTING 0x4000
1180 #define GRE_KEY 0x2000
1181 #define GRE_SEQ 0x1000
1182 #define GRE_STRICT 0x0800
1183 #define GRE_REC 0x0700
1184 #define GRE_FLAGS 0x00F8
1185 #define GRE_VERSION 0x0007
1186
1187 /* VXLAN protocol header */
1188 struct vxlanhdr {
1189 union {
1190 ovs_16aligned_be32 vx_flags; /* VXLAN flags. */
1191 struct {
1192 uint8_t flags; /* VXLAN GPE flags. */
1193 uint8_t reserved[2]; /* 16 bits reserved. */
1194 uint8_t next_protocol; /* Next Protocol field for VXLAN GPE. */
1195 } vx_gpe;
1196 };
1197 ovs_16aligned_be32 vx_vni;
1198 };
1199 BUILD_ASSERT_DECL(sizeof(struct vxlanhdr) == 8);
1200
1201 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
1202
1203 /*
1204 * VXLAN Generic Protocol Extension (VXLAN_F_GPE):
1205 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1206 * |R|R|Ver|I|P|R|O| Reserved |Next Protocol |
1207 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1208 * | VXLAN Network Identifier (VNI) | Reserved |
1209 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1210 *
1211 * Ver = Version. Indicates VXLAN GPE protocol version.
1212 *
1213 * P = Next Protocol Bit. The P bit is set to indicate that the
1214 * Next Protocol field is present.
1215 *
1216 * O = OAM Flag Bit. The O bit is set to indicate that the packet
1217 * is an OAM packet.
1218 *
1219 * Next Protocol = This 8 bit field indicates the protocol header
1220 * immediately following the VXLAN GPE header.
1221 *
1222 * https://tools.ietf.org/html/draft-ietf-nvo3-vxlan-gpe-01
1223 */
1224
1225 /* Fields in struct vxlanhdr.vx_gpe.flags */
1226 #define VXLAN_GPE_FLAGS_VER 0x30 /* Version. */
1227 #define VLXAN_GPE_FLAGS_P 0x04 /* Next Protocol Bit. */
1228 #define VXLAN_GPE_FLAGS_O 0x01 /* OAM Bit. */
1229
1230 /* VXLAN-GPE header flags. */
1231 #define VXLAN_HF_VER ((1U <<29) | (1U <<28))
1232 #define VXLAN_HF_NP (1U <<26)
1233 #define VXLAN_HF_OAM (1U <<24)
1234
1235 #define VXLAN_GPE_USED_BITS (VXLAN_HF_VER | VXLAN_HF_NP | VXLAN_HF_OAM | \
1236 0xff)
1237
1238 /* VXLAN-GPE header Next Protocol. */
1239 #define VXLAN_GPE_NP_IPV4 0x01
1240 #define VXLAN_GPE_NP_IPV6 0x02
1241 #define VXLAN_GPE_NP_ETHERNET 0x03
1242 #define VXLAN_GPE_NP_NSH 0x04
1243
1244 #define VXLAN_F_GPE 0x4000
1245 #define VXLAN_HF_GPE 0x04000000
1246
1247 /* Input values for PACKET_TYPE macros have to be in host byte order.
1248 * The _BE postfix indicates result is in network byte order. Otherwise result
1249 * is in host byte order. */
1250 #define PACKET_TYPE(NS, NS_TYPE) ((uint32_t) ((NS) << 16 | (NS_TYPE)))
1251 #define PACKET_TYPE_BE(NS, NS_TYPE) (htonl((NS) << 16 | (NS_TYPE)))
1252
1253 /* Returns the host byte ordered namespace of 'packet type'. */
1254 static inline uint16_t
1255 pt_ns(ovs_be32 packet_type)
1256 {
1257 return ntohl(packet_type) >> 16;
1258 }
1259
1260 /* Returns the network byte ordered namespace type of 'packet type'. */
1261 static inline ovs_be16
1262 pt_ns_type_be(ovs_be32 packet_type)
1263 {
1264 return be32_to_be16(packet_type);
1265 }
1266
1267 /* Returns the host byte ordered namespace type of 'packet type'. */
1268 static inline uint16_t
1269 pt_ns_type(ovs_be32 packet_type)
1270 {
1271 return ntohs(pt_ns_type_be(packet_type));
1272 }
1273
1274 /* Well-known packet_type field values. */
1275 enum packet_type {
1276 PT_ETH = PACKET_TYPE(OFPHTN_ONF, 0x0000), /* Default PT: Ethernet */
1277 PT_USE_NEXT_PROTO = PACKET_TYPE(OFPHTN_ONF, 0xfffe), /* Pseudo PT for decap. */
1278 PT_IPV4 = PACKET_TYPE(OFPHTN_ETHERTYPE, ETH_TYPE_IP),
1279 PT_IPV6 = PACKET_TYPE(OFPHTN_ETHERTYPE, ETH_TYPE_IPV6),
1280 PT_MPLS = PACKET_TYPE(OFPHTN_ETHERTYPE, ETH_TYPE_MPLS),
1281 PT_MPLS_MC = PACKET_TYPE(OFPHTN_ETHERTYPE, ETH_TYPE_MPLS_MCAST),
1282 PT_UNKNOWN = PACKET_TYPE(0xffff, 0xffff), /* Unknown packet type. */
1283 };
1284
1285
1286 void ipv6_format_addr(const struct in6_addr *addr, struct ds *);
1287 void ipv6_format_addr_bracket(const struct in6_addr *addr, struct ds *,
1288 bool bracket);
1289 void ipv6_format_mapped(const struct in6_addr *addr, struct ds *);
1290 void ipv6_format_masked(const struct in6_addr *addr,
1291 const struct in6_addr *mask, struct ds *);
1292 const char * ipv6_string_mapped(char *addr_str, const struct in6_addr *addr);
1293 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
1294 const struct in6_addr *mask);
1295 struct in6_addr ipv6_addr_bitxor(const struct in6_addr *a,
1296 const struct in6_addr *b);
1297 bool ipv6_is_zero(const struct in6_addr *a);
1298 struct in6_addr ipv6_create_mask(int mask);
1299 int ipv6_count_cidr_bits(const struct in6_addr *netmask);
1300 bool ipv6_is_cidr(const struct in6_addr *netmask);
1301
1302 bool ipv6_parse(const char *s, struct in6_addr *ip);
1303 char *ipv6_parse_masked(const char *s, struct in6_addr *ipv6,
1304 struct in6_addr *mask);
1305 char *ipv6_parse_cidr(const char *s, struct in6_addr *ip, unsigned int *plen)
1306 OVS_WARN_UNUSED_RESULT;
1307 char *ipv6_parse_masked_len(const char *s, int *n, struct in6_addr *ipv6,
1308 struct in6_addr *mask);
1309 char *ipv6_parse_cidr_len(const char *s, int *n, struct in6_addr *ip,
1310 unsigned int *plen)
1311 OVS_WARN_UNUSED_RESULT;
1312
1313 void *eth_compose(struct dp_packet *, const struct eth_addr eth_dst,
1314 const struct eth_addr eth_src, uint16_t eth_type,
1315 size_t size);
1316 void *snap_compose(struct dp_packet *, const struct eth_addr eth_dst,
1317 const struct eth_addr eth_src,
1318 unsigned int oui, uint16_t snap_type, size_t size);
1319 void packet_set_ipv4(struct dp_packet *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
1320 uint8_t ttl);
1321 void packet_set_ipv4_addr(struct dp_packet *packet, ovs_16aligned_be32 *addr,
1322 ovs_be32 new_addr);
1323 void packet_set_ipv6(struct dp_packet *, const struct in6_addr *src,
1324 const struct in6_addr *dst, uint8_t tc,
1325 ovs_be32 fl, uint8_t hlmit);
1326 void packet_set_ipv6_addr(struct dp_packet *packet, uint8_t proto,
1327 ovs_16aligned_be32 addr[4],
1328 const struct in6_addr *new_addr,
1329 bool recalculate_csum);
1330 void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1331 void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1332 void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
1333 void packet_set_icmp(struct dp_packet *, uint8_t type, uint8_t code);
1334 void packet_set_nd(struct dp_packet *, const struct in6_addr *target,
1335 const struct eth_addr sll, const struct eth_addr tll);
1336
1337 void packet_format_tcp_flags(struct ds *, uint16_t);
1338 const char *packet_tcp_flag_to_string(uint32_t flag);
1339 void compose_arp__(struct dp_packet *);
1340 void compose_arp(struct dp_packet *, uint16_t arp_op,
1341 const struct eth_addr arp_sha,
1342 const struct eth_addr arp_tha, bool broadcast,
1343 ovs_be32 arp_spa, ovs_be32 arp_tpa);
1344 void compose_nd_ns(struct dp_packet *, const struct eth_addr eth_src,
1345 const struct in6_addr *ipv6_src,
1346 const struct in6_addr *ipv6_dst);
1347 void compose_nd_na(struct dp_packet *, const struct eth_addr eth_src,
1348 const struct eth_addr eth_dst,
1349 const struct in6_addr *ipv6_src,
1350 const struct in6_addr *ipv6_dst,
1351 ovs_be32 rso_flags);
1352 void compose_nd_ra(struct dp_packet *,
1353 const struct eth_addr eth_src,
1354 const struct eth_addr eth_dst,
1355 const struct in6_addr *ipv6_src,
1356 const struct in6_addr *ipv6_dst,
1357 uint8_t cur_hop_limit, uint8_t mo_flags,
1358 ovs_be16 router_lt, ovs_be32 reachable_time,
1359 ovs_be32 retrans_timer, ovs_be32 mtu);
1360 void packet_put_ra_prefix_opt(struct dp_packet *,
1361 uint8_t plen, uint8_t la_flags,
1362 ovs_be32 valid_lifetime,
1363 ovs_be32 preferred_lifetime,
1364 const ovs_be128 router_prefix);
1365 uint32_t packet_csum_pseudoheader(const struct ip_header *);
1366 void IP_ECN_set_ce(struct dp_packet *pkt, bool is_ipv6);
1367
1368 #define DNS_HEADER_LEN 12
1369 struct dns_header {
1370 ovs_be16 id;
1371 uint8_t lo_flag; /* QR (1), OPCODE (4), AA (1), TC (1) and RD (1) */
1372 uint8_t hi_flag; /* RA (1), Z (3) and RCODE (4) */
1373 ovs_be16 qdcount; /* Num of entries in the question section. */
1374 ovs_be16 ancount; /* Num of resource records in the answer section. */
1375
1376 /* Num of name server records in the authority record section. */
1377 ovs_be16 nscount;
1378
1379 /* Num of resource records in the additional records section. */
1380 ovs_be16 arcount;
1381 };
1382
1383 BUILD_ASSERT_DECL(DNS_HEADER_LEN == sizeof(struct dns_header));
1384
1385 #define DNS_QUERY_TYPE_A 0x01
1386 #define DNS_QUERY_TYPE_AAAA 0x1c
1387 #define DNS_QUERY_TYPE_ANY 0xff
1388
1389 #define DNS_CLASS_IN 0x01
1390 #define DNS_DEFAULT_RR_TTL 3600
1391
1392 #endif /* packets.h */