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