xref: /dpdk/lib/net/rte_ip.h (revision 592ab76f9f0f41993bebb44da85c37750a93ece9)
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_IP_H_
10 #define _RTE_IP_H_
11 
12 /**
13  * @file
14  *
15  * IP-related defines
16  */
17 
18 #include <stdint.h>
19 
20 #ifdef RTE_EXEC_ENV_WINDOWS
21 #include <ws2tcpip.h>
22 #else
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netinet/ip.h>
28 #include <netinet/ip6.h>
29 #endif
30 
31 #include <rte_byteorder.h>
32 #include <rte_mbuf.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * IPv4 Header
40  */
41 struct rte_ipv4_hdr {
42 	__extension__
43 	union {
44 		uint8_t version_ihl;    /**< version and header length */
45 		struct {
46 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
47 			uint8_t ihl:4;     /**< header length */
48 			uint8_t version:4; /**< version */
49 #elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN
50 			uint8_t version:4; /**< version */
51 			uint8_t ihl:4;     /**< header length */
52 #endif
53 		};
54 	};
55 	uint8_t  type_of_service;	/**< type of service */
56 	rte_be16_t total_length;	/**< length of packet */
57 	rte_be16_t packet_id;		/**< packet ID */
58 	rte_be16_t fragment_offset;	/**< fragmentation offset */
59 	uint8_t  time_to_live;		/**< time to live */
60 	uint8_t  next_proto_id;		/**< protocol ID */
61 	rte_be16_t hdr_checksum;	/**< header checksum */
62 	rte_be32_t src_addr;		/**< source address */
63 	rte_be32_t dst_addr;		/**< destination address */
64 } __rte_packed;
65 
66 /** Create IPv4 address */
67 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
68 					   (((b) & 0xff) << 16) | \
69 					   (((c) & 0xff) << 8)  | \
70 					   ((d) & 0xff))
71 
72 /** Maximal IPv4 packet length (including a header) */
73 #define RTE_IPV4_MAX_PKT_LEN        65535
74 
75 /** Internet header length mask for version_ihl field */
76 #define RTE_IPV4_HDR_IHL_MASK	(0x0f)
77 /**
78  * Internet header length field multiplier (IHL field specifies overall header
79  * length in number of 4-byte words)
80  */
81 #define RTE_IPV4_IHL_MULTIPLIER	(4)
82 
83 /* Type of Service fields */
84 #define RTE_IPV4_HDR_DSCP_MASK	(0xfc)
85 #define RTE_IPV4_HDR_ECN_MASK	(0x03)
86 #define RTE_IPV4_HDR_ECN_CE	RTE_IPV4_HDR_ECN_MASK
87 
88 /* Fragment Offset * Flags. */
89 #define	RTE_IPV4_HDR_DF_SHIFT	14
90 #define	RTE_IPV4_HDR_MF_SHIFT	13
91 #define	RTE_IPV4_HDR_FO_SHIFT	3
92 
93 #define	RTE_IPV4_HDR_DF_FLAG	(1 << RTE_IPV4_HDR_DF_SHIFT)
94 #define	RTE_IPV4_HDR_MF_FLAG	(1 << RTE_IPV4_HDR_MF_SHIFT)
95 
96 #define	RTE_IPV4_HDR_OFFSET_MASK	((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
97 
98 #define	RTE_IPV4_HDR_OFFSET_UNITS	8
99 
100 /* IPv4 options */
101 #define RTE_IPV4_HDR_OPT_EOL       0
102 #define RTE_IPV4_HDR_OPT_NOP       1
103 #define RTE_IPV4_HDR_OPT_COPIED(v) ((v) & 0x80)
104 #define RTE_IPV4_HDR_OPT_MAX_LEN   40
105 
106 /*
107  * IPv4 address types
108  */
109 #define RTE_IPV4_ANY              ((uint32_t)0x00000000) /**< 0.0.0.0 */
110 #define RTE_IPV4_LOOPBACK         ((uint32_t)0x7f000001) /**< 127.0.0.1 */
111 #define RTE_IPV4_BROADCAST        ((uint32_t)0xe0000000) /**< 224.0.0.0 */
112 #define RTE_IPV4_ALLHOSTS_GROUP   ((uint32_t)0xe0000001) /**< 224.0.0.1 */
113 #define RTE_IPV4_ALLRTRS_GROUP    ((uint32_t)0xe0000002) /**< 224.0.0.2 */
114 #define RTE_IPV4_MAX_LOCAL_GROUP  ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
115 
116 /*
117  * IPv4 Multicast-related macros
118  */
119 #define RTE_IPV4_MIN_MCAST \
120 	RTE_IPV4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
121 #define RTE_IPV4_MAX_MCAST \
122 	RTE_IPV4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
123 
124 #define RTE_IS_IPV4_MCAST(x) \
125 	((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
126 	/**< check if IPv4 address is multicast */
127 
128 /* IPv4 default fields values */
129 #define RTE_IPV4_MIN_IHL    (0x5)
130 #define RTE_IPV4_VHL_DEF    ((IPVERSION << 4) | RTE_IPV4_MIN_IHL)
131 
132 /**
133  * Get the length of an IPv4 header.
134  *
135  * @param ipv4_hdr
136  *   Pointer to the IPv4 header.
137  * @return
138  *   The length of the IPv4 header (with options if present) in bytes.
139  */
140 static inline uint8_t
141 rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
142 {
143 	return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
144 		RTE_IPV4_IHL_MULTIPLIER);
145 }
146 
147 /**
148  * @internal Calculate a sum of all words in the buffer.
149  * Helper routine for the rte_raw_cksum().
150  *
151  * @param buf
152  *   Pointer to the buffer.
153  * @param len
154  *   Length of the buffer.
155  * @param sum
156  *   Initial value of the sum.
157  * @return
158  *   sum += Sum of all words in the buffer.
159  */
160 static inline uint32_t
161 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
162 {
163 	/* extend strict-aliasing rules */
164 	typedef uint16_t __attribute__((__may_alias__)) u16_p;
165 	const u16_p *u16_buf = (const u16_p *)buf;
166 	const u16_p *end = u16_buf + len / sizeof(*u16_buf);
167 
168 	for (; u16_buf != end; ++u16_buf)
169 		sum += *u16_buf;
170 
171 	/* if length is odd, keeping it byte order independent */
172 	if (unlikely(len % 2)) {
173 		uint16_t left = 0;
174 		*(unsigned char *)&left = *(const unsigned char *)end;
175 		sum += left;
176 	}
177 
178 	return sum;
179 }
180 
181 /**
182  * @internal Reduce a sum to the non-complemented checksum.
183  * Helper routine for the rte_raw_cksum().
184  *
185  * @param sum
186  *   Value of the sum.
187  * @return
188  *   The non-complemented checksum.
189  */
190 static inline uint16_t
191 __rte_raw_cksum_reduce(uint32_t sum)
192 {
193 	sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
194 	sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
195 	return (uint16_t)sum;
196 }
197 
198 /**
199  * Process the non-complemented checksum of a buffer.
200  *
201  * @param buf
202  *   Pointer to the buffer.
203  * @param len
204  *   Length of the buffer.
205  * @return
206  *   The non-complemented checksum.
207  */
208 static inline uint16_t
209 rte_raw_cksum(const void *buf, size_t len)
210 {
211 	uint32_t sum;
212 
213 	sum = __rte_raw_cksum(buf, len, 0);
214 	return __rte_raw_cksum_reduce(sum);
215 }
216 
217 /**
218  * Compute the raw (non complemented) checksum of a packet.
219  *
220  * @param m
221  *   The pointer to the mbuf.
222  * @param off
223  *   The offset in bytes to start the checksum.
224  * @param len
225  *   The length in bytes of the data to checksum.
226  * @param cksum
227  *   A pointer to the checksum, filled on success.
228  * @return
229  *   0 on success, -1 on error (bad length or offset).
230  */
231 static inline int
232 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
233 	uint16_t *cksum)
234 {
235 	const struct rte_mbuf *seg;
236 	const char *buf;
237 	uint32_t sum, tmp;
238 	uint32_t seglen, done;
239 
240 	/* easy case: all data in the first segment */
241 	if (off + len <= rte_pktmbuf_data_len(m)) {
242 		*cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
243 				const char *, off), len);
244 		return 0;
245 	}
246 
247 	if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
248 		return -1; /* invalid params, return a dummy value */
249 
250 	/* else browse the segment to find offset */
251 	seglen = 0;
252 	for (seg = m; seg != NULL; seg = seg->next) {
253 		seglen = rte_pktmbuf_data_len(seg);
254 		if (off < seglen)
255 			break;
256 		off -= seglen;
257 	}
258 	RTE_ASSERT(seg != NULL);
259 	if (seg == NULL)
260 		return -1;
261 	seglen -= off;
262 	buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
263 	if (seglen >= len) {
264 		/* all in one segment */
265 		*cksum = rte_raw_cksum(buf, len);
266 		return 0;
267 	}
268 
269 	/* hard case: process checksum of several segments */
270 	sum = 0;
271 	done = 0;
272 	for (;;) {
273 		tmp = __rte_raw_cksum(buf, seglen, 0);
274 		if (done & 1)
275 			tmp = rte_bswap16((uint16_t)tmp);
276 		sum += tmp;
277 		done += seglen;
278 		if (done == len)
279 			break;
280 		seg = seg->next;
281 		buf = rte_pktmbuf_mtod(seg, const char *);
282 		seglen = rte_pktmbuf_data_len(seg);
283 		if (seglen > len - done)
284 			seglen = len - done;
285 	}
286 
287 	*cksum = __rte_raw_cksum_reduce(sum);
288 	return 0;
289 }
290 
291 /**
292  * Process the IPv4 checksum of an IPv4 header.
293  *
294  * The checksum field must be set to 0 by the caller.
295  *
296  * @param ipv4_hdr
297  *   The pointer to the contiguous IPv4 header.
298  * @return
299  *   The complemented checksum to set in the IP packet.
300  */
301 static inline uint16_t
302 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
303 {
304 	uint16_t cksum;
305 	cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
306 	return (uint16_t)~cksum;
307 }
308 
309 /**
310  * Process the pseudo-header checksum of an IPv4 header.
311  *
312  * The checksum field must be set to 0 by the caller.
313  *
314  * Depending on the ol_flags, the pseudo-header checksum expected by the
315  * drivers is not the same. For instance, when TSO is enabled, the IP
316  * payload length must not be included in the packet.
317  *
318  * When ol_flags is 0, it computes the standard pseudo-header checksum.
319  *
320  * @param ipv4_hdr
321  *   The pointer to the contiguous IPv4 header.
322  * @param ol_flags
323  *   The ol_flags of the associated mbuf.
324  * @return
325  *   The non-complemented checksum to set in the L4 header.
326  */
327 static inline uint16_t
328 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
329 {
330 	struct ipv4_psd_header {
331 		uint32_t src_addr; /* IP address of source host. */
332 		uint32_t dst_addr; /* IP address of destination host. */
333 		uint8_t  zero;     /* zero. */
334 		uint8_t  proto;    /* L4 protocol type. */
335 		uint16_t len;      /* L4 length. */
336 	} psd_hdr;
337 
338 	uint32_t l3_len;
339 
340 	psd_hdr.src_addr = ipv4_hdr->src_addr;
341 	psd_hdr.dst_addr = ipv4_hdr->dst_addr;
342 	psd_hdr.zero = 0;
343 	psd_hdr.proto = ipv4_hdr->next_proto_id;
344 	if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
345 		psd_hdr.len = 0;
346 	} else {
347 		l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
348 		psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
349 			rte_ipv4_hdr_len(ipv4_hdr)));
350 	}
351 	return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
352 }
353 
354 /**
355  * @internal Calculate the non-complemented IPv4 L4 checksum
356  */
357 static inline uint16_t
358 __rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
359 {
360 	uint32_t cksum;
361 	uint32_t l3_len, l4_len;
362 	uint8_t ip_hdr_len;
363 
364 	ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
365 	l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
366 	if (l3_len < ip_hdr_len)
367 		return 0;
368 
369 	l4_len = l3_len - ip_hdr_len;
370 
371 	cksum = rte_raw_cksum(l4_hdr, l4_len);
372 	cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
373 
374 	cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
375 
376 	return (uint16_t)cksum;
377 }
378 
379 /**
380  * Process the IPv4 UDP or TCP checksum.
381  *
382  * The layer 4 checksum must be set to 0 in the L4 header by the caller.
383  *
384  * @param ipv4_hdr
385  *   The pointer to the contiguous IPv4 header.
386  * @param l4_hdr
387  *   The pointer to the beginning of the L4 header.
388  * @return
389  *   The complemented checksum to set in the L4 header.
390  */
391 static inline uint16_t
392 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
393 {
394 	uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
395 
396 	cksum = ~cksum;
397 
398 	/*
399 	 * Per RFC 768: If the computed checksum is zero for UDP,
400 	 * it is transmitted as all ones
401 	 * (the equivalent in one's complement arithmetic).
402 	 */
403 	if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
404 		cksum = 0xffff;
405 
406 	return cksum;
407 }
408 
409 /**
410  * @internal Calculate the non-complemented IPv4 L4 checksum of a packet
411  */
412 static inline uint16_t
413 __rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m,
414 			     const struct rte_ipv4_hdr *ipv4_hdr,
415 			     uint16_t l4_off)
416 {
417 	uint16_t raw_cksum;
418 	uint32_t cksum;
419 
420 	if (l4_off > m->pkt_len)
421 		return 0;
422 
423 	if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off, &raw_cksum))
424 		return 0;
425 
426 	cksum = raw_cksum + rte_ipv4_phdr_cksum(ipv4_hdr, 0);
427 
428 	cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
429 
430 	return (uint16_t)cksum;
431 }
432 
433 /**
434  * @warning
435  * @b EXPERIMENTAL: this API may change without prior notice.
436  *
437  * Compute the IPv4 UDP/TCP checksum of a packet.
438  *
439  * @param m
440  *   The pointer to the mbuf.
441  * @param ipv4_hdr
442  *   The pointer to the contiguous IPv4 header.
443  * @param l4_off
444  *   The offset in bytes to start L4 checksum.
445  * @return
446  *   The complemented checksum to set in the L4 header.
447  */
448 __rte_experimental
449 static inline uint16_t
450 rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m,
451 			   const struct rte_ipv4_hdr *ipv4_hdr, uint16_t l4_off)
452 {
453 	uint16_t cksum = __rte_ipv4_udptcp_cksum_mbuf(m, ipv4_hdr, l4_off);
454 
455 	cksum = ~cksum;
456 
457 	/*
458 	 * Per RFC 768: If the computed checksum is zero for UDP,
459 	 * it is transmitted as all ones
460 	 * (the equivalent in one's complement arithmetic).
461 	 */
462 	if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
463 		cksum = 0xffff;
464 
465 	return cksum;
466 }
467 
468 /**
469  * Validate the IPv4 UDP or TCP checksum.
470  *
471  * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0
472  * (i.e. no checksum).
473  *
474  * @param ipv4_hdr
475  *   The pointer to the contiguous IPv4 header.
476  * @param l4_hdr
477  *   The pointer to the beginning of the L4 header.
478  * @return
479  *   Return 0 if the checksum is correct, else -1.
480  */
481 __rte_experimental
482 static inline int
483 rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr,
484 			     const void *l4_hdr)
485 {
486 	uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
487 
488 	if (cksum != 0xffff)
489 		return -1;
490 
491 	return 0;
492 }
493 
494 /**
495  * @warning
496  * @b EXPERIMENTAL: this API may change without prior notice.
497  *
498  * Verify the IPv4 UDP/TCP checksum of a packet.
499  *
500  * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0
501  * (i.e. no checksum).
502  *
503  * @param m
504  *   The pointer to the mbuf.
505  * @param ipv4_hdr
506  *   The pointer to the contiguous IPv4 header.
507  * @param l4_off
508  *   The offset in bytes to start L4 checksum.
509  * @return
510  *   Return 0 if the checksum is correct, else -1.
511  */
512 __rte_experimental
513 static inline uint16_t
514 rte_ipv4_udptcp_cksum_mbuf_verify(const struct rte_mbuf *m,
515 				  const struct rte_ipv4_hdr *ipv4_hdr,
516 				  uint16_t l4_off)
517 {
518 	uint16_t cksum = __rte_ipv4_udptcp_cksum_mbuf(m, ipv4_hdr, l4_off);
519 
520 	if (cksum != 0xffff)
521 		return -1;
522 
523 	return 0;
524 }
525 
526 /**
527  * IPv6 Header
528  */
529 struct rte_ipv6_hdr {
530 	rte_be32_t vtc_flow;	/**< IP version, traffic class & flow label. */
531 	rte_be16_t payload_len;	/**< IP payload size, including ext. headers */
532 	uint8_t  proto;		/**< Protocol, next header. */
533 	uint8_t  hop_limits;	/**< Hop limits. */
534 	uint8_t  src_addr[16];	/**< IP address of source host. */
535 	uint8_t  dst_addr[16];	/**< IP address of destination host(s). */
536 } __rte_packed;
537 
538 /* IPv6 vtc_flow: IPv / TC / flow_label */
539 #define RTE_IPV6_HDR_FL_SHIFT 0
540 #define RTE_IPV6_HDR_TC_SHIFT 20
541 #define RTE_IPV6_HDR_FL_MASK	((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
542 #define RTE_IPV6_HDR_TC_MASK	(0xff << RTE_IPV6_HDR_TC_SHIFT)
543 #define RTE_IPV6_HDR_DSCP_MASK	(0xfc << RTE_IPV6_HDR_TC_SHIFT)
544 #define RTE_IPV6_HDR_ECN_MASK	(0x03 << RTE_IPV6_HDR_TC_SHIFT)
545 #define RTE_IPV6_HDR_ECN_CE	RTE_IPV6_HDR_ECN_MASK
546 
547 #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */
548 
549 /**
550  * Process the pseudo-header checksum of an IPv6 header.
551  *
552  * Depending on the ol_flags, the pseudo-header checksum expected by the
553  * drivers is not the same. For instance, when TSO is enabled, the IPv6
554  * payload length must not be included in the packet.
555  *
556  * When ol_flags is 0, it computes the standard pseudo-header checksum.
557  *
558  * @param ipv6_hdr
559  *   The pointer to the contiguous IPv6 header.
560  * @param ol_flags
561  *   The ol_flags of the associated mbuf.
562  * @return
563  *   The non-complemented checksum to set in the L4 header.
564  */
565 static inline uint16_t
566 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
567 {
568 	uint32_t sum;
569 	struct {
570 		rte_be32_t len;   /* L4 length. */
571 		rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
572 	} psd_hdr;
573 
574 	psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
575 	if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
576 		psd_hdr.len = 0;
577 	} else {
578 		psd_hdr.len = ipv6_hdr->payload_len;
579 	}
580 
581 	sum = __rte_raw_cksum(ipv6_hdr->src_addr,
582 		sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
583 		0);
584 	sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
585 	return __rte_raw_cksum_reduce(sum);
586 }
587 
588 /**
589  * @internal Calculate the non-complemented IPv6 L4 checksum
590  */
591 static inline uint16_t
592 __rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
593 {
594 	uint32_t cksum;
595 	uint32_t l4_len;
596 
597 	l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
598 
599 	cksum = rte_raw_cksum(l4_hdr, l4_len);
600 	cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
601 
602 	cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
603 
604 	return (uint16_t)cksum;
605 }
606 
607 /**
608  * Process the IPv6 UDP or TCP checksum.
609  *
610  * The IPv6 header must not be followed by extension headers. The layer 4
611  * checksum must be set to 0 in the L4 header by the caller.
612  *
613  * @param ipv6_hdr
614  *   The pointer to the contiguous IPv6 header.
615  * @param l4_hdr
616  *   The pointer to the beginning of the L4 header.
617  * @return
618  *   The complemented checksum to set in the L4 header.
619  */
620 static inline uint16_t
621 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
622 {
623 	uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
624 
625 	cksum = ~cksum;
626 
627 	/*
628 	 * Per RFC 768: If the computed checksum is zero for UDP,
629 	 * it is transmitted as all ones
630 	 * (the equivalent in one's complement arithmetic).
631 	 */
632 	if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
633 		cksum = 0xffff;
634 
635 	return cksum;
636 }
637 
638 /**
639  * @internal Calculate the non-complemented IPv6 L4 checksum of a packet
640  */
641 static inline uint16_t
642 __rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf *m,
643 			     const struct rte_ipv6_hdr *ipv6_hdr,
644 			     uint16_t l4_off)
645 {
646 	uint16_t raw_cksum;
647 	uint32_t cksum;
648 
649 	if (l4_off > m->pkt_len)
650 		return 0;
651 
652 	if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off, &raw_cksum))
653 		return 0;
654 
655 	cksum = raw_cksum + rte_ipv6_phdr_cksum(ipv6_hdr, 0);
656 
657 	cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
658 
659 	return (uint16_t)cksum;
660 }
661 
662 /**
663  * @warning
664  * @b EXPERIMENTAL: this API may change without prior notice.
665  *
666  * Process the IPv6 UDP or TCP checksum of a packet.
667  *
668  * The IPv6 header must not be followed by extension headers. The layer 4
669  * checksum must be set to 0 in the L4 header by the caller.
670  *
671  * @param m
672  *   The pointer to the mbuf.
673  * @param ipv6_hdr
674  *   The pointer to the contiguous IPv6 header.
675  * @param l4_off
676  *   The offset in bytes to start L4 checksum.
677  * @return
678  *   The complemented checksum to set in the L4 header.
679  */
680 __rte_experimental
681 static inline uint16_t
682 rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf *m,
683 			   const struct rte_ipv6_hdr *ipv6_hdr, uint16_t l4_off)
684 {
685 	uint16_t cksum = __rte_ipv6_udptcp_cksum_mbuf(m, ipv6_hdr, l4_off);
686 
687 	cksum = ~cksum;
688 
689 	/*
690 	 * Per RFC 768: If the computed checksum is zero for UDP,
691 	 * it is transmitted as all ones
692 	 * (the equivalent in one's complement arithmetic).
693 	 */
694 	if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
695 		cksum = 0xffff;
696 
697 	return cksum;
698 }
699 
700 /**
701  * Validate the IPv6 UDP or TCP checksum.
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 ipv6_hdr
708  *   The pointer to the contiguous IPv6 header.
709  * @param l4_hdr
710  *   The pointer to the beginning of the L4 header.
711  * @return
712  *   Return 0 if the checksum is correct, else -1.
713  */
714 __rte_experimental
715 static inline int
716 rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,
717 			     const void *l4_hdr)
718 {
719 	uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
720 
721 	if (cksum != 0xffff)
722 		return -1;
723 
724 	return 0;
725 }
726 
727 /**
728  * @warning
729  * @b EXPERIMENTAL: this API may change without prior notice.
730  *
731  * Validate the IPv6 UDP or TCP checksum of a packet.
732  *
733  * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0:
734  * this is either invalid or means no checksum in some situations. See 8.1
735  * (Upper-Layer Checksums) in RFC 8200.
736  *
737  * @param m
738  *   The pointer to the mbuf.
739  * @param ipv6_hdr
740  *   The pointer to the contiguous IPv6 header.
741  * @param l4_off
742  *   The offset in bytes to start L4 checksum.
743  * @return
744  *   Return 0 if the checksum is correct, else -1.
745  */
746 __rte_experimental
747 static inline int
748 rte_ipv6_udptcp_cksum_mbuf_verify(const struct rte_mbuf *m,
749 				  const struct rte_ipv6_hdr *ipv6_hdr,
750 				  uint16_t l4_off)
751 {
752 	uint16_t cksum = __rte_ipv6_udptcp_cksum_mbuf(m, ipv6_hdr, l4_off);
753 
754 	if (cksum != 0xffff)
755 		return -1;
756 
757 	return 0;
758 }
759 
760 /** IPv6 fragment extension header. */
761 #define	RTE_IPV6_EHDR_MF_SHIFT	0
762 #define	RTE_IPV6_EHDR_MF_MASK	1
763 #define	RTE_IPV6_EHDR_FO_SHIFT	3
764 #define	RTE_IPV6_EHDR_FO_MASK	(~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
765 #define	RTE_IPV6_EHDR_FO_ALIGN	(1 << RTE_IPV6_EHDR_FO_SHIFT)
766 
767 #define RTE_IPV6_FRAG_USED_MASK	(RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
768 
769 #define RTE_IPV6_GET_MF(x)	((x) & RTE_IPV6_EHDR_MF_MASK)
770 #define RTE_IPV6_GET_FO(x)	((x) >> RTE_IPV6_EHDR_FO_SHIFT)
771 
772 #define RTE_IPV6_SET_FRAG_DATA(fo, mf)	\
773 	(((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
774 
775 struct rte_ipv6_fragment_ext {
776 	uint8_t next_header;	/**< Next header type */
777 	uint8_t reserved;	/**< Reserved */
778 	rte_be16_t frag_data;	/**< All fragmentation data */
779 	rte_be32_t id;		/**< Packet ID */
780 } __rte_packed;
781 
782 /* IPv6 fragment extension header size */
783 #define RTE_IPV6_FRAG_HDR_SIZE	sizeof(struct rte_ipv6_fragment_ext)
784 
785 /**
786  * Parse next IPv6 header extension
787  *
788  * This function checks if proto number is an IPv6 extensions and parses its
789  * data if so, providing information on next header and extension length.
790  *
791  * @param p
792  *   Pointer to an extension raw data.
793  * @param proto
794  *   Protocol number extracted from the "next header" field from
795  *   the IPv6 header or the previous extension.
796  * @param ext_len
797  *   Extension data length.
798  * @return
799  *   next protocol number if proto is an IPv6 extension, -EINVAL otherwise
800  */
801 __rte_experimental
802 static inline int
803 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
804 {
805 	int next_proto;
806 
807 	switch (proto) {
808 	case IPPROTO_AH:
809 		next_proto = *p++;
810 		*ext_len = (*p + 2) * sizeof(uint32_t);
811 		break;
812 
813 	case IPPROTO_HOPOPTS:
814 	case IPPROTO_ROUTING:
815 	case IPPROTO_DSTOPTS:
816 		next_proto = *p++;
817 		*ext_len = (*p + 1) * sizeof(uint64_t);
818 		break;
819 
820 	case IPPROTO_FRAGMENT:
821 		next_proto = *p;
822 		*ext_len = RTE_IPV6_FRAG_HDR_SIZE;
823 		break;
824 
825 	default:
826 		return -EINVAL;
827 	}
828 
829 	return next_proto;
830 }
831 
832 #ifdef __cplusplus
833 }
834 #endif
835 
836 #endif /* _RTE_IP_H_ */
837