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