Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 1982, 1986, 1990, 1993
3 : : * The Regents of the University of California.
4 : : * Copyright(c) 2010-2014 Intel Corporation.
5 : : * Copyright(c) 2014 6WIND S.A.
6 : : * All rights reserved.
7 : : */
8 : :
9 : : #ifndef _RTE_IP6_H_
10 : : #define _RTE_IP6_H_
11 : :
12 : : /**
13 : : * @file
14 : : *
15 : : * IPv6-related defines
16 : : */
17 : :
18 : : #include <stdint.h>
19 : : #include <string.h>
20 : :
21 : : #ifdef RTE_EXEC_ENV_WINDOWS
22 : : #include <ws2tcpip.h>
23 : : #else
24 : : #include <sys/socket.h>
25 : : #include <sys/types.h>
26 : : #include <netinet/in.h>
27 : : #include <arpa/inet.h>
28 : : #include <netinet/ip6.h>
29 : : #endif
30 : :
31 : : #include <rte_byteorder.h>
32 : : #include <rte_cksum.h>
33 : : #include <rte_ether.h>
34 : : #include <rte_mbuf.h>
35 : :
36 : : #ifdef __cplusplus
37 : : extern "C" {
38 : : #endif
39 : :
40 : : /**
41 : : * Maximum IPv6 address size in bytes.
42 : : */
43 : : #define RTE_IPV6_ADDR_SIZE 16
44 : :
45 : : /**
46 : : * Maximum IPv6 address size in bits.
47 : : */
48 : : #define RTE_IPV6_MAX_DEPTH (RTE_IPV6_ADDR_SIZE * CHAR_BIT)
49 : :
50 : : /**
51 : : * IPv6 Address
52 : : */
53 : : struct rte_ipv6_addr {
54 : : uint8_t a[RTE_IPV6_ADDR_SIZE];
55 : : };
56 : :
57 : : /**
58 : : * Check if two IPv6 Addresses are equal.
59 : : *
60 : : * @param a
61 : : * The first address.
62 : : * @param b
63 : : * The second address.
64 : : * @return
65 : : * true if both addresses are identical.
66 : : */
67 : : static inline bool
68 : : rte_ipv6_addr_eq(const struct rte_ipv6_addr *a, const struct rte_ipv6_addr *b)
69 : : {
70 [ + + + + : 188146 : return memcmp(a, b, sizeof(*a)) == 0;
+ + - + -
+ - + ]
71 : : }
72 : :
73 : : /**
74 : : * Mask an IPv6 address using the specified depth.
75 : : *
76 : : * Leave untouched one bit per unit in the depth variable and set the rest to 0.
77 : : *
78 : : * @param[in] ip
79 : : * The address to mask.
80 : : * @param[out] depth
81 : : * All bits starting from this bit number will be set to zero.
82 : : */
83 : : static inline void
84 : : rte_ipv6_addr_mask(struct rte_ipv6_addr *ip, uint8_t depth)
85 : : {
86 [ + + + - : 26673 : if (depth < RTE_IPV6_MAX_DEPTH) {
+ + + + ]
87 : 25408 : unsigned int d = depth / CHAR_BIT;
88 : 25408 : uint8_t mask = ~(UINT8_MAX >> (depth % CHAR_BIT));
89 [ - + ]: 25409 : ip->a[d] &= mask;
90 : 25408 : d++;
91 [ + + + + : 258582 : while (d < sizeof(*ip))
+ + + + ]
92 : 233170 : ip->a[d++] = 0;
93 : : }
94 : : }
95 : :
96 : : /**
97 : : * Check if two IPv6 addresses belong to the same network prefix.
98 : : *
99 : : * @param a
100 : : * The first address or network.
101 : : * @param b
102 : : * The second address or network.
103 : : * @param depth
104 : : * The network prefix length.
105 : : * @return
106 : : * @c true if the first @p depth bits of both addresses are identical.
107 : : */
108 : : static inline bool
109 : 2313924 : rte_ipv6_addr_eq_prefix(const struct rte_ipv6_addr *a, const struct rte_ipv6_addr *b, uint8_t depth)
110 : : {
111 [ + + ]: 2313924 : if (depth < RTE_IPV6_MAX_DEPTH) {
112 : 2312922 : unsigned int d = depth / CHAR_BIT;
113 : 2312922 : uint8_t mask = ~(UINT8_MAX >> (depth % CHAR_BIT));
114 : :
115 [ + + ]: 2312922 : if ((a->a[d] ^ b->a[d]) & mask)
116 : : return false;
117 : :
118 : 2297867 : return memcmp(a, b, d) == 0;
119 : : }
120 : 1002 : return rte_ipv6_addr_eq(a, b);
121 : : }
122 : :
123 : : /**
124 : : * Get the depth of a given IPv6 address mask.
125 : : *
126 : : * @param mask
127 : : * The address mask.
128 : : * @return
129 : : * The number of consecutive bits set to 1 starting from the beginning of the mask.
130 : : */
131 : : static inline uint8_t
132 : : rte_ipv6_mask_depth(const struct rte_ipv6_addr *mask)
133 : : {
134 : : uint8_t depth = 0;
135 : :
136 [ + - + - : 57 : for (unsigned int i = 0; i < RTE_DIM(mask->a); i++) {
+ - + + +
- ]
137 : 56 : uint8_t m = mask->a[i];
138 [ + + + + : 56 : if (m == 0xff) {
+ + + - +
+ ]
139 : 52 : depth += 8;
140 : : } else {
141 [ + + + + : 25 : while (m & 0x80) {
+ + + + -
- + + ]
142 : 20 : m <<= 1;
143 : 20 : depth++;
144 : : }
145 : : break;
146 : : }
147 : : }
148 : :
149 : : return depth;
150 : : }
151 : :
152 : : /**
153 : : * Split a literal 16 bit unsigned integer into two bytes separated by a comma
154 : : * according to the platform endianness.
155 : : *
156 : : * @param x
157 : : * A @c uint16_t literal value.
158 : : * @return
159 : : * Two @c uint8_t literals separated by a coma.
160 : : */
161 : : #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
162 : : #define RTE_IPV6_U16_SPLIT(x) \
163 : : (uint8_t)((uint16_t)(x) & UINT16_C(0xff)), \
164 : : (uint8_t)(((uint16_t)(x) >> 8) & UINT16_C(0xff))
165 : : #else
166 : : #define RTE_IPV6_U16_SPLIT(x) \
167 : : (uint8_t)(((uint16_t)(x) >> 8) & UINT16_C(0xff)), \
168 : : (uint8_t)((uint16_t)(x) & UINT16_C(0xff))
169 : : #endif
170 : :
171 : : /**
172 : : * Shorthand to define a literal IPv6 address based on 16bit unsigned integers.
173 : : *
174 : : * @param a,b,c,d,e,f,g,h
175 : : * @c uint8_t literals that will be passed to RTE_IPV6_U16_SPLIT(x).
176 : : * @return
177 : : * A literal @ref rte_ipv6_addr value suitable for static initialization.
178 : : */
179 : : #define RTE_IPV6(a, b, c, d, e, f, g, h) \
180 : : {{ \
181 : : RTE_IPV6_U16_SPLIT(a), \
182 : : RTE_IPV6_U16_SPLIT(b), \
183 : : RTE_IPV6_U16_SPLIT(c), \
184 : : RTE_IPV6_U16_SPLIT(d), \
185 : : RTE_IPV6_U16_SPLIT(e), \
186 : : RTE_IPV6_U16_SPLIT(f), \
187 : : RTE_IPV6_U16_SPLIT(g), \
188 : : RTE_IPV6_U16_SPLIT(h) \
189 : : }}
190 : :
191 : : /**
192 : : * printf() format element for @ref rte_ipv6_addr structures.
193 : : * To be used along with RTE_IPV6_ADDR_SPLIT(ip).
194 : : */
195 : : #define RTE_IPV6_ADDR_FMT \
196 : : "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
197 : :
198 : : /**
199 : : * For use with #RTE_IPV6_ADDR_FMT. E.g.:
200 : : *
201 : : * @code
202 : : * printf(RTE_IPV6_ADDR_FMT "\n", RTE_IPV6_ADDR_SPLIT(&ip));
203 : : * @endcode
204 : : *
205 : : * @param ip
206 : : * A struct rte_ipv6_addr pointer.
207 : : * @return
208 : : * A set of 16 @c uint8_t values separated by comas for use in printf().
209 : : */
210 : : #define RTE_IPV6_ADDR_SPLIT(ip) \
211 : : ((uint8_t)(ip)->a[0]), \
212 : : ((uint8_t)(ip)->a[1]), \
213 : : ((uint8_t)(ip)->a[2]), \
214 : : ((uint8_t)(ip)->a[3]), \
215 : : ((uint8_t)(ip)->a[4]), \
216 : : ((uint8_t)(ip)->a[5]), \
217 : : ((uint8_t)(ip)->a[6]), \
218 : : ((uint8_t)(ip)->a[7]), \
219 : : ((uint8_t)(ip)->a[8]), \
220 : : ((uint8_t)(ip)->a[9]), \
221 : : ((uint8_t)(ip)->a[10]), \
222 : : ((uint8_t)(ip)->a[11]), \
223 : : ((uint8_t)(ip)->a[12]), \
224 : : ((uint8_t)(ip)->a[13]), \
225 : : ((uint8_t)(ip)->a[14]), \
226 : : ((uint8_t)(ip)->a[15])
227 : :
228 : : /** Full IPv6 mask. NB: this is not a valid/routable IPv6 address. */
229 : : #define RTE_IPV6_MASK_FULL \
230 : : RTE_IPV6(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff)
231 : :
232 : : /** Unspecified IPv6 address as defined in RFC 4291, section 2.5.2. */
233 : : #define RTE_IPV6_ADDR_UNSPEC RTE_IPV6(0, 0, 0, 0, 0, 0, 0, 0)
234 : :
235 : : /**
236 : : * Check if an IPv6 address is unspecified as defined in RFC 4291, section 2.5.2.
237 : : *
238 : : * @param ip
239 : : * The address to check.
240 : : * @return
241 : : * @c true if the address is the unspecified address (all zeroes).
242 : : */
243 : : static inline bool
244 : : rte_ipv6_addr_is_unspec(const struct rte_ipv6_addr *ip)
245 : : {
246 [ + + - + : 1089 : const struct rte_ipv6_addr unspec = RTE_IPV6_ADDR_UNSPEC;
- + - + -
+ - + -
+ ]
247 : : return rte_ipv6_addr_eq(ip, &unspec);
248 : : }
249 : :
250 : : /** Loopback IPv6 address as defined in RFC 4291, section 2.5.3. */
251 : : #define RTE_IPV6_ADDR_LOOPBACK RTE_IPV6(0, 0, 0, 0, 0, 0, 0, 1)
252 : :
253 : : /**
254 : : * Check if an IPv6 address is the loopback address as defined in RFC 4291,
255 : : * section 2.5.3.
256 : : *
257 : : * @param ip
258 : : * The address to check.
259 : : * @return
260 : : * @c true if the address is the loopback address (all zeroes except the last bit).
261 : : */
262 : : static inline bool
263 : : rte_ipv6_addr_is_loopback(const struct rte_ipv6_addr *ip)
264 : : {
265 [ - + - + : 7 : struct rte_ipv6_addr loopback = RTE_IPV6_ADDR_LOOPBACK;
- + - + -
+ - + -
+ ]
266 : : return rte_ipv6_addr_eq(ip, &loopback);
267 : : }
268 : :
269 : : /**
270 : : * Check if an IPv6 address is link-local as defined in RFC 4291, section 2.5.6.
271 : : *
272 : : * @param ip
273 : : * The address to check.
274 : : * @return
275 : : * @c true if the address is a link-local address.
276 : : */
277 : : static inline bool
278 : : rte_ipv6_addr_is_linklocal(const struct rte_ipv6_addr *ip)
279 : : {
280 : : return ip->a[0] == 0xfe && (ip->a[1] & 0xc0) == 0x80;
281 : : }
282 : :
283 : : /**
284 : : * Check if an IPv6 address is site-local as defined in RFC 4291, section 2.5.7.
285 : : *
286 : : * @param ip
287 : : * The address to check.
288 : : * @return
289 : : * @c true if the address is a site-local address.
290 : : */
291 : : static inline bool
292 : : rte_ipv6_addr_is_sitelocal(const struct rte_ipv6_addr *ip)
293 : : {
294 : : return ip->a[0] == 0xfe && (ip->a[1] & 0xc0) == 0xc0;
295 : : }
296 : :
297 : : /**
298 : : * Check if an IPv6 address is an IPv4-compatible address as defined in RFC 4291,
299 : : * section 2.5.5.1.
300 : : *
301 : : * @param ip
302 : : * The address to check.
303 : : * @return
304 : : * @c true if the address is an IPv4-compatible address.
305 : : */
306 : : static inline bool
307 : 7 : rte_ipv6_addr_is_v4compat(const struct rte_ipv6_addr *ip)
308 : : {
309 : 7 : const struct rte_ipv6_addr unspec = RTE_IPV6_ADDR_UNSPEC;
310 [ + + + + ]: 7 : return rte_ipv6_addr_eq_prefix(ip, &unspec, 96) && !rte_ipv6_addr_is_loopback(ip);
311 : : }
312 : :
313 : : #define RTE_IPV6_ADDR_PREFIX_V4MAPPED RTE_IPV6(0, 0, 0, 0, 0, 0xffff, 0, 0)
314 : :
315 : : /**
316 : : * Check if an IPv6 address is an IPv4-mapped address as defined in RFC 4291,
317 : : * section 2.5.5.2.
318 : : *
319 : : * @param ip
320 : : * The address to check.
321 : : * @return
322 : : * @c true if the address is an IPv4-mapped address.
323 : : */
324 : : static inline bool
325 : : rte_ipv6_addr_is_v4mapped(const struct rte_ipv6_addr *ip)
326 : : {
327 : 7 : const struct rte_ipv6_addr prefix = RTE_IPV6_ADDR_PREFIX_V4MAPPED;
328 [ - + - + : 7 : return rte_ipv6_addr_eq_prefix(ip, &prefix, 96);
- + - + -
+ - + -
+ ]
329 : : }
330 : :
331 : : /**
332 : : * Check if an IPv6 address is multicast as defined in RFC 4291, section 2.7.
333 : : *
334 : : * @param ip
335 : : * The address to check.
336 : : * @return
337 : : * @c true if the address is multicast.
338 : : */
339 : : static inline bool
340 : : rte_ipv6_addr_is_mcast(const struct rte_ipv6_addr *ip)
341 : : {
342 : : return ip->a[0] == 0xff;
343 : : }
344 : :
345 : : /**
346 : : * IPv6 multicast scope values as defined in RFC 4291, section 2.7.
347 : : */
348 : : enum rte_ipv6_mc_scope {
349 : : /** Invalid multicast scope. */
350 : : RTE_IPV6_MC_SCOPE_NONE = 0x00,
351 : : /** Interface-local multicast scope. */
352 : : RTE_IPV6_MC_SCOPE_IFACELOCAL = 0x01,
353 : : /** Link-local multicast scope. */
354 : : RTE_IPV6_MC_SCOPE_LINKLOCAL = 0x02,
355 : : /** Site-local multicast scope. */
356 : : RTE_IPV6_MC_SCOPE_SITELOCAL = 0x05,
357 : : /** Organizational-local multicast scope. */
358 : : RTE_IPV6_MC_SCOPE_ORGLOCAL = 0x08,
359 : : /** Global multicast scope. */
360 : : RTE_IPV6_MC_SCOPE_GLOBAL = 0x0e,
361 : : };
362 : :
363 : : /**
364 : : * Extract the IPv6 multicast scope value as defined in RFC 4291, section 2.7.
365 : : *
366 : : * @param ip
367 : : * The address from which to get the multicast scope.
368 : : * @return
369 : : * The multicast scope of the address, or #RTE_IPV6_MC_SCOPE_NONE if the
370 : : * address is not multicast.
371 : : */
372 : : static inline enum rte_ipv6_mc_scope
373 : : rte_ipv6_mc_scope(const struct rte_ipv6_addr *ip)
374 : : {
375 : : if (!rte_ipv6_addr_is_mcast(ip))
376 : : return RTE_IPV6_MC_SCOPE_NONE;
377 : : return (enum rte_ipv6_mc_scope)(ip->a[1] & 0x0f);
378 : : }
379 : :
380 : : /** @name Well known multicast addresses */
381 : : /**@{*/
382 : : /** Interface-local all-nodes multicast address as defined in RFC 4291, section 2.7.1. */
383 : : #define RTE_IPV6_ADDR_ALLNODES_IFACE_LOCAL RTE_IPV6(0xff01, 0, 0, 0, 0, 0, 0, 1)
384 : : /** Link-local all-nodes multicast address as defined in RFC 4291, section 2.7.1. */
385 : : #define RTE_IPV6_ADDR_ALLNODES_LINK_LOCAL RTE_IPV6(0xff02, 0, 0, 0, 0, 0, 0, 1)
386 : : /** Interface-local all-routers multicast address as defined in RFC 4291, section 2.7.1. */
387 : : #define RTE_IPV6_ADDR_ALLROUTERS_IFACE_LOCAL RTE_IPV6(0xff01, 0, 0, 0, 0, 0, 0, 2)
388 : : /** Link-local all-routers multicast address as defined in RFC 4291, section 2.7.1. */
389 : : #define RTE_IPV6_ADDR_ALLROUTERS_LINK_LOCAL RTE_IPV6(0xff02, 0, 0, 0, 0, 0, 0, 2)
390 : : /** Site-local all-routers multicast address as defined in RFC 4291, section 2.7.1. */
391 : : #define RTE_IPV6_ADDR_ALLROUTERS_SITE_LOCAL RTE_IPV6(0xff05, 0, 0, 0, 0, 0, 0, 2)
392 : : /**@}*/
393 : :
394 : : /*
395 : : * Generate a link-local IPv6 address from an Ethernet address as specified in
396 : : * RFC 4291, section 2.5.1.
397 : : *
398 : : * @param[out] ip
399 : : * The link-local IPv6 address to generate.
400 : : * @param[in] mac
401 : : * An Ethernet address.
402 : : */
403 : : static inline void
404 : 1 : rte_ipv6_llocal_from_ethernet(struct rte_ipv6_addr *ip, const struct rte_ether_addr *mac)
405 : : {
406 : 1 : ip->a[0] = 0xfe;
407 : 1 : ip->a[1] = 0x80;
408 : 1 : memset(&ip->a[2], 0, 6);
409 : : /*
410 : : * The "u" bit (universal/local bit in IEEE EUI-64 terminology)
411 : : * must be inverted for IPv6 link local address.
412 : : * 0 means local scope, 1 means universal scope.
413 : : */
414 : 1 : ip->a[8] = mac->addr_bytes[0] ^ RTE_ETHER_LOCAL_ADMIN_ADDR;
415 : 1 : ip->a[9] = mac->addr_bytes[1];
416 : 1 : ip->a[10] = mac->addr_bytes[2];
417 : 1 : ip->a[11] = 0xff;
418 : 1 : ip->a[12] = 0xfe;
419 : 1 : ip->a[13] = mac->addr_bytes[3];
420 : 1 : ip->a[14] = mac->addr_bytes[4];
421 : 1 : ip->a[15] = mac->addr_bytes[5];
422 : 1 : }
423 : :
424 : : /**
425 : : * Convert a unicast or anycast IPv6 address to a solicited-node multicast
426 : : * address as defined in RFC 4291, section 2.7.1.
427 : : *
428 : : * @param[out] sol
429 : : * The IPv6 solicited-node multicast address to generate.
430 : : * @param[in] ip
431 : : * A unicast or anycast address.
432 : : */
433 : : static inline void
434 : : rte_ipv6_solnode_from_addr(struct rte_ipv6_addr *sol, const struct rte_ipv6_addr *ip)
435 : : {
436 : 1 : sol->a[0] = 0xff;
437 [ - + ]: 1 : sol->a[1] = 0x02;
438 : : memset(&sol->a[2], 0, 9);
439 : 1 : sol->a[11] = 0x01;
440 : 1 : sol->a[12] = 0xff;
441 : 2 : sol->a[13] = ip->a[13];
442 : 2 : sol->a[14] = ip->a[14];
443 [ - + - + ]: 1 : sol->a[15] = ip->a[15];
444 : : }
445 : :
446 : : /**
447 : : * Generate a multicast Ethernet address from a multicast IPv6 address as defined
448 : : * in RFC 2464, section 7.
449 : : *
450 : : * @param[out] mac
451 : : * The multicast Ethernet address to generate.
452 : : * @param[in] ip
453 : : * A multicast IPv6 address.
454 : : */
455 : : static inline void
456 : : rte_ether_mcast_from_ipv6(struct rte_ether_addr *mac, const struct rte_ipv6_addr *ip)
457 : : {
458 : : mac->addr_bytes[0] = 0x33;
459 : : mac->addr_bytes[1] = 0x33;
460 : : mac->addr_bytes[2] = ip->a[12];
461 : : mac->addr_bytes[3] = ip->a[13];
462 : : mac->addr_bytes[4] = ip->a[14];
463 : : mac->addr_bytes[5] = ip->a[15];
464 : : }
465 : :
466 : : /**
467 : : * IPv6 Header
468 : : */
469 : : struct __rte_aligned(2) __rte_packed_begin rte_ipv6_hdr {
470 : : rte_be32_t vtc_flow; /**< IP version, traffic class & flow label. */
471 : : rte_be16_t payload_len; /**< IP payload size, including ext. headers */
472 : : uint8_t proto; /**< Protocol, next header. */
473 : : uint8_t hop_limits; /**< Hop limits. */
474 : : struct rte_ipv6_addr src_addr; /**< IP address of source host. */
475 : : struct rte_ipv6_addr dst_addr; /**< IP address of destination host(s). */
476 : : } __rte_packed_end;
477 : :
478 : : /**
479 : : * Check that the IPv6 header version field is valid according to RFC 8200 section 3.
480 : : *
481 : : * @param ip
482 : : * The IPv6 header.
483 : : * @return
484 : : * @c 0 if the version field is valid. @c -EINVAL otherwise.
485 : : */
486 : : static inline int rte_ipv6_check_version(const struct rte_ipv6_hdr *ip)
487 : : {
488 : : uint8_t version = ((const uint8_t *)ip)[0];
489 : : if ((version & 0xf0) != 0x60)
490 : : return -EINVAL;
491 : : return 0;
492 : : }
493 : :
494 : : /* IPv6 routing extension type definition. */
495 : : #define RTE_IPV6_SRCRT_TYPE_4 4
496 : :
497 : : /**
498 : : * IPv6 Routing Extension Header
499 : : */
500 : : struct __rte_aligned(2) __rte_packed_begin rte_ipv6_routing_ext {
501 : : uint8_t next_hdr; /**< Protocol, next header. */
502 : : uint8_t hdr_len; /**< Header length. */
503 : : uint8_t type; /**< Extension header type. */
504 : : uint8_t segments_left; /**< Valid segments number. */
505 : : __extension__
506 : : union {
507 : : rte_be32_t flags; /**< Packet control data per type. */
508 : : struct {
509 : : uint8_t last_entry; /**< The last_entry field of SRH */
510 : : uint8_t flag; /**< Packet flag. */
511 : : rte_be16_t tag; /**< Packet tag. */
512 : : };
513 : : };
514 : : /* Next are 128-bit IPv6 address fields to describe segments. */
515 : : } __rte_packed_end;
516 : :
517 : : /* IPv6 vtc_flow: IPv / TC / flow_label */
518 : : #define RTE_IPV6_HDR_FL_SHIFT 0
519 : : #define RTE_IPV6_HDR_TC_SHIFT 20
520 : : #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
521 : : #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
522 : : #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
523 : : #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
524 : : #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK
525 : :
526 : : #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */
527 : :
528 : : /**
529 : : * Process the pseudo-header checksum of an IPv6 header.
530 : : *
531 : : * Depending on the ol_flags, the pseudo-header checksum expected by the
532 : : * drivers is not the same. For instance, when TSO is enabled, the IPv6
533 : : * payload length must not be included in the packet.
534 : : *
535 : : * When ol_flags is 0, it computes the standard pseudo-header checksum.
536 : : *
537 : : * @param ipv6_hdr
538 : : * The pointer to the contiguous IPv6 header.
539 : : * @param ol_flags
540 : : * The ol_flags of the associated mbuf.
541 : : * @return
542 : : * The non-complemented checksum to set in the L4 header.
543 : : */
544 : : static inline uint16_t
545 : 3 : rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
546 : : {
547 : 0 : uint32_t sum;
548 : 0 : struct {
549 : : rte_be32_t len; /* L4 length. */
550 : : rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
551 : : } psd_hdr;
552 : :
553 : 3 : psd_hdr.proto = (uint32_t)ipv6_hdr->proto << 24;
554 [ - + ]: 3 : if (ol_flags & (RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_UDP_SEG))
555 : 0 : psd_hdr.len = 0;
556 : : else
557 : 3 : psd_hdr.len = ipv6_hdr->payload_len;
558 : :
559 [ # # ]: 3 : sum = __rte_raw_cksum(&ipv6_hdr->src_addr,
560 : : sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
561 : : 0);
562 : 0 : sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
563 : 3 : return __rte_raw_cksum_reduce(sum);
564 : : }
565 : :
566 : : /**
567 : : * @internal Calculate the non-complemented IPv6 L4 checksum
568 : : */
569 : : static inline uint16_t
570 : 3 : __rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
571 : : {
572 : : uint32_t cksum;
573 : : uint32_t l4_len;
574 : :
575 [ - + ]: 6 : l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
576 : :
577 : 3 : cksum = rte_raw_cksum(l4_hdr, l4_len);
578 : 3 : cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
579 : :
580 : 3 : cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
581 : :
582 : 3 : return (uint16_t)cksum;
583 : : }
584 : :
585 : : /**
586 : : * Process the IPv6 UDP or TCP checksum.
587 : : *
588 : : * The IPv6 header must not be followed by extension headers. The layer 4
589 : : * checksum must be set to 0 in the L4 header by the caller.
590 : : *
591 : : * @param ipv6_hdr
592 : : * The pointer to the contiguous IPv6 header.
593 : : * @param l4_hdr
594 : : * The pointer to the beginning of the L4 header.
595 : : * @return
596 : : * The complemented checksum to set in the L4 header.
597 : : */
598 : : static inline uint16_t
599 : : rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
600 : : {
601 : 1 : uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
602 : :
603 : 1 : cksum = ~cksum;
604 : :
605 : : /*
606 : : * Per RFC 768: If the computed checksum is zero for UDP,
607 : : * it is transmitted as all ones
608 : : * (the equivalent in one's complement arithmetic).
609 : : */
610 [ - - - - : 1 : if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
- + - - ]
611 : : cksum = 0xffff;
612 : :
613 : : return cksum;
614 : : }
615 : :
616 : : /**
617 : : * @internal Calculate the non-complemented IPv6 L4 checksum of a packet
618 : : */
619 : : static inline uint16_t
620 : 0 : __rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf *m,
621 : : const struct rte_ipv6_hdr *ipv6_hdr,
622 : : uint16_t l4_off)
623 : : {
624 : : uint16_t raw_cksum;
625 : : uint32_t cksum;
626 : :
627 [ # # ]: 0 : if (unlikely(l4_off > m->pkt_len))
628 : : return 0; /* invalid params, return a dummy value */
629 : :
630 [ # # # # ]: 0 : if (rte_raw_cksum_mbuf(m, l4_off, rte_be_to_cpu_16(ipv6_hdr->payload_len), &raw_cksum))
631 : : return 0;
632 : :
633 : 0 : cksum = raw_cksum + rte_ipv6_phdr_cksum(ipv6_hdr, 0);
634 : :
635 : 0 : cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
636 : :
637 : 0 : return (uint16_t)cksum;
638 : : }
639 : :
640 : : /**
641 : : * Process the IPv6 UDP or TCP checksum of a packet.
642 : : *
643 : : * The IPv6 header must not be followed by extension headers. The layer 4
644 : : * checksum must be set to 0 in the L4 header by the caller.
645 : : *
646 : : * @param m
647 : : * The pointer to the mbuf.
648 : : * @param ipv6_hdr
649 : : * The pointer to the contiguous IPv6 header.
650 : : * @param l4_off
651 : : * The offset in bytes to start L4 checksum.
652 : : * @return
653 : : * The complemented checksum to set in the L4 header.
654 : : */
655 : : static inline uint16_t
656 : : rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf *m,
657 : : const struct rte_ipv6_hdr *ipv6_hdr, uint16_t l4_off)
658 : : {
659 : 0 : uint16_t cksum = __rte_ipv6_udptcp_cksum_mbuf(m, ipv6_hdr, l4_off);
660 : :
661 : 0 : cksum = ~cksum;
662 : :
663 : : /*
664 : : * Per RFC 768: If the computed checksum is zero for UDP,
665 : : * it is transmitted as all ones
666 : : * (the equivalent in one's complement arithmetic).
667 : : */
668 [ # # # # ]: 0 : if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
669 : : cksum = 0xffff;
670 : :
671 : : return cksum;
672 : : }
673 : :
674 : : /**
675 : : * Validate the IPv6 UDP or TCP checksum.
676 : : *
677 : : * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0:
678 : : * this is either invalid or means no checksum in some situations. See 8.1
679 : : * (Upper-Layer Checksums) in RFC 8200.
680 : : *
681 : : * @param ipv6_hdr
682 : : * The pointer to the contiguous IPv6 header.
683 : : * @param l4_hdr
684 : : * The pointer to the beginning of the L4 header.
685 : : * @return
686 : : * Return 0 if the checksum is correct, else -1.
687 : : */
688 : : static inline int
689 : : rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,
690 : : const void *l4_hdr)
691 : : {
692 : 2 : uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
693 : :
694 [ - + - - : 2 : if (cksum != 0xffff)
+ - ]
695 : 0 : return -1;
696 : :
697 : : return 0;
698 : : }
699 : :
700 : : /**
701 : : * Validate the IPv6 UDP or TCP checksum of a packet.
702 : : *
703 : : * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0:
704 : : * this is either invalid or means no checksum in some situations. See 8.1
705 : : * (Upper-Layer Checksums) in RFC 8200.
706 : : *
707 : : * @param m
708 : : * The pointer to the mbuf.
709 : : * @param ipv6_hdr
710 : : * The pointer to the contiguous IPv6 header.
711 : : * @param l4_off
712 : : * The offset in bytes to start L4 checksum.
713 : : * @return
714 : : * Return 0 if the checksum is correct, else -1.
715 : : */
716 : : static inline int
717 : : rte_ipv6_udptcp_cksum_mbuf_verify(const struct rte_mbuf *m,
718 : : const struct rte_ipv6_hdr *ipv6_hdr,
719 : : uint16_t l4_off)
720 : : {
721 : 0 : uint16_t cksum = __rte_ipv6_udptcp_cksum_mbuf(m, ipv6_hdr, l4_off);
722 : :
723 [ # # ]: 0 : if (cksum != 0xffff)
724 : : return -1;
725 : :
726 : : return 0;
727 : : }
728 : :
729 : : /** IPv6 fragment extension header. */
730 : : #define RTE_IPV6_EHDR_MF_SHIFT 0
731 : : #define RTE_IPV6_EHDR_MF_MASK 1
732 : : #define RTE_IPV6_EHDR_FO_SHIFT 3
733 : : #define RTE_IPV6_EHDR_FO_MASK (~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
734 : : #define RTE_IPV6_EHDR_FO_ALIGN (1 << RTE_IPV6_EHDR_FO_SHIFT)
735 : :
736 : : #define RTE_IPV6_FRAG_USED_MASK (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
737 : :
738 : : #define RTE_IPV6_GET_MF(x) ((x) & RTE_IPV6_EHDR_MF_MASK)
739 : : #define RTE_IPV6_GET_FO(x) ((x) >> RTE_IPV6_EHDR_FO_SHIFT)
740 : :
741 : : #define RTE_IPV6_SET_FRAG_DATA(fo, mf) \
742 : : (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
743 : :
744 : : struct __rte_aligned(2) __rte_packed_begin rte_ipv6_fragment_ext {
745 : : uint8_t next_header; /**< Next header type */
746 : : uint8_t reserved; /**< Reserved */
747 : : rte_be16_t frag_data; /**< All fragmentation data */
748 : : rte_be32_t id; /**< Packet ID */
749 : : } __rte_packed_end;
750 : :
751 : : /* IPv6 fragment extension header size */
752 : : #define RTE_IPV6_FRAG_HDR_SIZE sizeof(struct rte_ipv6_fragment_ext)
753 : :
754 : : /**
755 : : * Parse next IPv6 header extension
756 : : *
757 : : * This function checks if proto number is an IPv6 extensions and parses its
758 : : * data if so, providing information on next header and extension length.
759 : : *
760 : : * @param p
761 : : * Pointer to an extension raw data.
762 : : * @param proto
763 : : * Protocol number extracted from the "next header" field from
764 : : * the IPv6 header or the previous extension.
765 : : * @param ext_len
766 : : * Extension data length.
767 : : * @return
768 : : * next protocol number if proto is an IPv6 extension, -EINVAL otherwise
769 : : */
770 : : static inline int
771 : 0 : rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
772 : : {
773 : : int next_proto;
774 : :
775 [ # # # # ]: 0 : switch (proto) {
776 : 0 : case IPPROTO_AH:
777 : 0 : next_proto = *p++;
778 : 0 : *ext_len = (*p + 2) * sizeof(uint32_t);
779 : 0 : break;
780 : :
781 : 0 : case IPPROTO_HOPOPTS:
782 : : case IPPROTO_ROUTING:
783 : : case IPPROTO_DSTOPTS:
784 : 0 : next_proto = *p++;
785 : 0 : *ext_len = (*p + 1) * sizeof(uint64_t);
786 : 0 : break;
787 : :
788 : 0 : case IPPROTO_FRAGMENT:
789 : 0 : next_proto = *p;
790 : 0 : *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
791 : 0 : break;
792 : :
793 : : default:
794 : : return -EINVAL;
795 : : }
796 : :
797 : : return next_proto;
798 : : }
799 : :
800 : : #ifdef __cplusplus
801 : : }
802 : : #endif
803 : :
804 : : #endif /* _RTE_IP6_H_ */
|