xref: /dpdk/lib/net/rte_ip.h (revision b53d106d34b5c638f5a2cbdfee0da5bd42d4383f)
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 /*
101  * IPv4 address types
102  */
103 #define RTE_IPV4_ANY              ((uint32_t)0x00000000) /**< 0.0.0.0 */
104 #define RTE_IPV4_LOOPBACK         ((uint32_t)0x7f000001) /**< 127.0.0.1 */
105 #define RTE_IPV4_BROADCAST        ((uint32_t)0xe0000000) /**< 224.0.0.0 */
106 #define RTE_IPV4_ALLHOSTS_GROUP   ((uint32_t)0xe0000001) /**< 224.0.0.1 */
107 #define RTE_IPV4_ALLRTRS_GROUP    ((uint32_t)0xe0000002) /**< 224.0.0.2 */
108 #define RTE_IPV4_MAX_LOCAL_GROUP  ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
109 
110 /*
111  * IPv4 Multicast-related macros
112  */
113 #define RTE_IPV4_MIN_MCAST \
114 	RTE_IPV4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
115 #define RTE_IPV4_MAX_MCAST \
116 	RTE_IPV4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
117 
118 #define RTE_IS_IPV4_MCAST(x) \
119 	((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
120 	/**< check if IPv4 address is multicast */
121 
122 /* IPv4 default fields values */
123 #define RTE_IPV4_MIN_IHL    (0x5)
124 #define RTE_IPV4_VHL_DEF    ((IPVERSION << 4) | RTE_IPV4_MIN_IHL)
125 
126 /**
127  * Get the length of an IPv4 header.
128  *
129  * @param ipv4_hdr
130  *   Pointer to the IPv4 header.
131  * @return
132  *   The length of the IPv4 header (with options if present) in bytes.
133  */
134 static inline uint8_t
135 rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
136 {
137 	return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
138 		RTE_IPV4_IHL_MULTIPLIER);
139 }
140 
141 /**
142  * @internal Calculate a sum of all words in the buffer.
143  * Helper routine for the rte_raw_cksum().
144  *
145  * @param buf
146  *   Pointer to the buffer.
147  * @param len
148  *   Length of the buffer.
149  * @param sum
150  *   Initial value of the sum.
151  * @return
152  *   sum += Sum of all words in the buffer.
153  */
154 static inline uint32_t
155 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
156 {
157 	/* extend strict-aliasing rules */
158 	typedef uint16_t __attribute__((__may_alias__)) u16_p;
159 	const u16_p *u16_buf = (const u16_p *)buf;
160 	const u16_p *end = u16_buf + len / sizeof(*u16_buf);
161 
162 	for (; u16_buf != end; ++u16_buf)
163 		sum += *u16_buf;
164 
165 	/* if length is odd, keeping it byte order independent */
166 	if (unlikely(len % 2)) {
167 		uint16_t left = 0;
168 		*(unsigned char *)&left = *(const unsigned char *)end;
169 		sum += left;
170 	}
171 
172 	return sum;
173 }
174 
175 /**
176  * @internal Reduce a sum to the non-complemented checksum.
177  * Helper routine for the rte_raw_cksum().
178  *
179  * @param sum
180  *   Value of the sum.
181  * @return
182  *   The non-complemented checksum.
183  */
184 static inline uint16_t
185 __rte_raw_cksum_reduce(uint32_t sum)
186 {
187 	sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
188 	sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
189 	return (uint16_t)sum;
190 }
191 
192 /**
193  * Process the non-complemented checksum of a buffer.
194  *
195  * @param buf
196  *   Pointer to the buffer.
197  * @param len
198  *   Length of the buffer.
199  * @return
200  *   The non-complemented checksum.
201  */
202 static inline uint16_t
203 rte_raw_cksum(const void *buf, size_t len)
204 {
205 	uint32_t sum;
206 
207 	sum = __rte_raw_cksum(buf, len, 0);
208 	return __rte_raw_cksum_reduce(sum);
209 }
210 
211 /**
212  * Compute the raw (non complemented) checksum of a packet.
213  *
214  * @param m
215  *   The pointer to the mbuf.
216  * @param off
217  *   The offset in bytes to start the checksum.
218  * @param len
219  *   The length in bytes of the data to checksum.
220  * @param cksum
221  *   A pointer to the checksum, filled on success.
222  * @return
223  *   0 on success, -1 on error (bad length or offset).
224  */
225 static inline int
226 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
227 	uint16_t *cksum)
228 {
229 	const struct rte_mbuf *seg;
230 	const char *buf;
231 	uint32_t sum, tmp;
232 	uint32_t seglen, done;
233 
234 	/* easy case: all data in the first segment */
235 	if (off + len <= rte_pktmbuf_data_len(m)) {
236 		*cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
237 				const char *, off), len);
238 		return 0;
239 	}
240 
241 	if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
242 		return -1; /* invalid params, return a dummy value */
243 
244 	/* else browse the segment to find offset */
245 	seglen = 0;
246 	for (seg = m; seg != NULL; seg = seg->next) {
247 		seglen = rte_pktmbuf_data_len(seg);
248 		if (off < seglen)
249 			break;
250 		off -= seglen;
251 	}
252 	RTE_ASSERT(seg != NULL);
253 	if (seg == NULL)
254 		return -1;
255 	seglen -= off;
256 	buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
257 	if (seglen >= len) {
258 		/* all in one segment */
259 		*cksum = rte_raw_cksum(buf, len);
260 		return 0;
261 	}
262 
263 	/* hard case: process checksum of several segments */
264 	sum = 0;
265 	done = 0;
266 	for (;;) {
267 		tmp = __rte_raw_cksum(buf, seglen, 0);
268 		if (done & 1)
269 			tmp = rte_bswap16((uint16_t)tmp);
270 		sum += tmp;
271 		done += seglen;
272 		if (done == len)
273 			break;
274 		seg = seg->next;
275 		buf = rte_pktmbuf_mtod(seg, const char *);
276 		seglen = rte_pktmbuf_data_len(seg);
277 		if (seglen > len - done)
278 			seglen = len - done;
279 	}
280 
281 	*cksum = __rte_raw_cksum_reduce(sum);
282 	return 0;
283 }
284 
285 /**
286  * Process the IPv4 checksum of an IPv4 header.
287  *
288  * The checksum field must be set to 0 by the caller.
289  *
290  * @param ipv4_hdr
291  *   The pointer to the contiguous IPv4 header.
292  * @return
293  *   The complemented checksum to set in the IP packet.
294  */
295 static inline uint16_t
296 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
297 {
298 	uint16_t cksum;
299 	cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
300 	return (uint16_t)~cksum;
301 }
302 
303 /**
304  * Process the pseudo-header checksum of an IPv4 header.
305  *
306  * The checksum field must be set to 0 by the caller.
307  *
308  * Depending on the ol_flags, the pseudo-header checksum expected by the
309  * drivers is not the same. For instance, when TSO is enabled, the IP
310  * payload length must not be included in the packet.
311  *
312  * When ol_flags is 0, it computes the standard pseudo-header checksum.
313  *
314  * @param ipv4_hdr
315  *   The pointer to the contiguous IPv4 header.
316  * @param ol_flags
317  *   The ol_flags of the associated mbuf.
318  * @return
319  *   The non-complemented checksum to set in the L4 header.
320  */
321 static inline uint16_t
322 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
323 {
324 	struct ipv4_psd_header {
325 		uint32_t src_addr; /* IP address of source host. */
326 		uint32_t dst_addr; /* IP address of destination host. */
327 		uint8_t  zero;     /* zero. */
328 		uint8_t  proto;    /* L4 protocol type. */
329 		uint16_t len;      /* L4 length. */
330 	} psd_hdr;
331 
332 	uint32_t l3_len;
333 
334 	psd_hdr.src_addr = ipv4_hdr->src_addr;
335 	psd_hdr.dst_addr = ipv4_hdr->dst_addr;
336 	psd_hdr.zero = 0;
337 	psd_hdr.proto = ipv4_hdr->next_proto_id;
338 	if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
339 		psd_hdr.len = 0;
340 	} else {
341 		l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
342 		psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
343 			rte_ipv4_hdr_len(ipv4_hdr)));
344 	}
345 	return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
346 }
347 
348 /**
349  * @internal Calculate the non-complemented IPv4 L4 checksum
350  */
351 static inline uint16_t
352 __rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
353 {
354 	uint32_t cksum;
355 	uint32_t l3_len, l4_len;
356 	uint8_t ip_hdr_len;
357 
358 	ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
359 	l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
360 	if (l3_len < ip_hdr_len)
361 		return 0;
362 
363 	l4_len = l3_len - ip_hdr_len;
364 
365 	cksum = rte_raw_cksum(l4_hdr, l4_len);
366 	cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
367 
368 	cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
369 
370 	return (uint16_t)cksum;
371 }
372 
373 /**
374  * Process the IPv4 UDP or TCP checksum.
375  *
376  * The layer 4 checksum must be set to 0 in the L4 header by the caller.
377  *
378  * @param ipv4_hdr
379  *   The pointer to the contiguous IPv4 header.
380  * @param l4_hdr
381  *   The pointer to the beginning of the L4 header.
382  * @return
383  *   The complemented checksum to set in the L4 header.
384  */
385 static inline uint16_t
386 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
387 {
388 	uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
389 
390 	cksum = ~cksum;
391 
392 	/*
393 	 * Per RFC 768: If the computed checksum is zero for UDP,
394 	 * it is transmitted as all ones
395 	 * (the equivalent in one's complement arithmetic).
396 	 */
397 	if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
398 		cksum = 0xffff;
399 
400 	return cksum;
401 }
402 
403 /**
404  * Validate the IPv4 UDP or TCP checksum.
405  *
406  * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0
407  * (i.e. no checksum).
408  *
409  * @param ipv4_hdr
410  *   The pointer to the contiguous IPv4 header.
411  * @param l4_hdr
412  *   The pointer to the beginning of the L4 header.
413  * @return
414  *   Return 0 if the checksum is correct, else -1.
415  */
416 __rte_experimental
417 static inline int
418 rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr,
419 			     const void *l4_hdr)
420 {
421 	uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
422 
423 	if (cksum != 0xffff)
424 		return -1;
425 
426 	return 0;
427 }
428 
429 /**
430  * IPv6 Header
431  */
432 struct rte_ipv6_hdr {
433 	rte_be32_t vtc_flow;	/**< IP version, traffic class & flow label. */
434 	rte_be16_t payload_len;	/**< IP payload size, including ext. headers */
435 	uint8_t  proto;		/**< Protocol, next header. */
436 	uint8_t  hop_limits;	/**< Hop limits. */
437 	uint8_t  src_addr[16];	/**< IP address of source host. */
438 	uint8_t  dst_addr[16];	/**< IP address of destination host(s). */
439 } __rte_packed;
440 
441 /* IPv6 vtc_flow: IPv / TC / flow_label */
442 #define RTE_IPV6_HDR_FL_SHIFT 0
443 #define RTE_IPV6_HDR_TC_SHIFT 20
444 #define RTE_IPV6_HDR_FL_MASK	((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
445 #define RTE_IPV6_HDR_TC_MASK	(0xff << RTE_IPV6_HDR_TC_SHIFT)
446 #define RTE_IPV6_HDR_DSCP_MASK	(0xfc << RTE_IPV6_HDR_TC_SHIFT)
447 #define RTE_IPV6_HDR_ECN_MASK	(0x03 << RTE_IPV6_HDR_TC_SHIFT)
448 #define RTE_IPV6_HDR_ECN_CE	RTE_IPV6_HDR_ECN_MASK
449 
450 #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */
451 
452 /**
453  * Process the pseudo-header checksum of an IPv6 header.
454  *
455  * Depending on the ol_flags, the pseudo-header checksum expected by the
456  * drivers is not the same. For instance, when TSO is enabled, the IPv6
457  * payload length must not be included in the packet.
458  *
459  * When ol_flags is 0, it computes the standard pseudo-header checksum.
460  *
461  * @param ipv6_hdr
462  *   The pointer to the contiguous IPv6 header.
463  * @param ol_flags
464  *   The ol_flags of the associated mbuf.
465  * @return
466  *   The non-complemented checksum to set in the L4 header.
467  */
468 static inline uint16_t
469 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
470 {
471 	uint32_t sum;
472 	struct {
473 		rte_be32_t len;   /* L4 length. */
474 		rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
475 	} psd_hdr;
476 
477 	psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
478 	if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
479 		psd_hdr.len = 0;
480 	} else {
481 		psd_hdr.len = ipv6_hdr->payload_len;
482 	}
483 
484 	sum = __rte_raw_cksum(ipv6_hdr->src_addr,
485 		sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
486 		0);
487 	sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
488 	return __rte_raw_cksum_reduce(sum);
489 }
490 
491 /**
492  * @internal Calculate the non-complemented IPv6 L4 checksum
493  */
494 static inline uint16_t
495 __rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
496 {
497 	uint32_t cksum;
498 	uint32_t l4_len;
499 
500 	l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
501 
502 	cksum = rte_raw_cksum(l4_hdr, l4_len);
503 	cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
504 
505 	cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
506 
507 	return (uint16_t)cksum;
508 }
509 
510 /**
511  * Process the IPv6 UDP or TCP checksum.
512  *
513  * The IPv6 header must not be followed by extension headers. The layer 4
514  * checksum must be set to 0 in the L4 header by the caller.
515  *
516  * @param ipv6_hdr
517  *   The pointer to the contiguous IPv6 header.
518  * @param l4_hdr
519  *   The pointer to the beginning of the L4 header.
520  * @return
521  *   The complemented checksum to set in the L4 header.
522  */
523 static inline uint16_t
524 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
525 {
526 	uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
527 
528 	cksum = ~cksum;
529 
530 	/*
531 	 * Per RFC 768: If the computed checksum is zero for UDP,
532 	 * it is transmitted as all ones
533 	 * (the equivalent in one's complement arithmetic).
534 	 */
535 	if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
536 		cksum = 0xffff;
537 
538 	return cksum;
539 }
540 
541 /**
542  * Validate the IPv6 UDP or TCP checksum.
543  *
544  * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0:
545  * this is either invalid or means no checksum in some situations. See 8.1
546  * (Upper-Layer Checksums) in RFC 8200.
547  *
548  * @param ipv6_hdr
549  *   The pointer to the contiguous IPv6 header.
550  * @param l4_hdr
551  *   The pointer to the beginning of the L4 header.
552  * @return
553  *   Return 0 if the checksum is correct, else -1.
554  */
555 __rte_experimental
556 static inline int
557 rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,
558 			     const void *l4_hdr)
559 {
560 	uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
561 
562 	if (cksum != 0xffff)
563 		return -1;
564 
565 	return 0;
566 }
567 
568 /** IPv6 fragment extension header. */
569 #define	RTE_IPV6_EHDR_MF_SHIFT	0
570 #define	RTE_IPV6_EHDR_MF_MASK	1
571 #define	RTE_IPV6_EHDR_FO_SHIFT	3
572 #define	RTE_IPV6_EHDR_FO_MASK	(~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
573 #define	RTE_IPV6_EHDR_FO_ALIGN	(1 << RTE_IPV6_EHDR_FO_SHIFT)
574 
575 #define RTE_IPV6_FRAG_USED_MASK	(RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
576 
577 #define RTE_IPV6_GET_MF(x)	((x) & RTE_IPV6_EHDR_MF_MASK)
578 #define RTE_IPV6_GET_FO(x)	((x) >> RTE_IPV6_EHDR_FO_SHIFT)
579 
580 #define RTE_IPV6_SET_FRAG_DATA(fo, mf)	\
581 	(((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
582 
583 struct rte_ipv6_fragment_ext {
584 	uint8_t next_header;	/**< Next header type */
585 	uint8_t reserved;	/**< Reserved */
586 	rte_be16_t frag_data;	/**< All fragmentation data */
587 	rte_be32_t id;		/**< Packet ID */
588 } __rte_packed;
589 
590 /* IPv6 fragment extension header size */
591 #define RTE_IPV6_FRAG_HDR_SIZE	sizeof(struct rte_ipv6_fragment_ext)
592 
593 /**
594  * Parse next IPv6 header extension
595  *
596  * This function checks if proto number is an IPv6 extensions and parses its
597  * data if so, providing information on next header and extension length.
598  *
599  * @param p
600  *   Pointer to an extension raw data.
601  * @param proto
602  *   Protocol number extracted from the "next header" field from
603  *   the IPv6 header or the previous extension.
604  * @param ext_len
605  *   Extension data length.
606  * @return
607  *   next protocol number if proto is an IPv6 extension, -EINVAL otherwise
608  */
609 __rte_experimental
610 static inline int
611 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
612 {
613 	int next_proto;
614 
615 	switch (proto) {
616 	case IPPROTO_AH:
617 		next_proto = *p++;
618 		*ext_len = (*p + 2) * sizeof(uint32_t);
619 		break;
620 
621 	case IPPROTO_HOPOPTS:
622 	case IPPROTO_ROUTING:
623 	case IPPROTO_DSTOPTS:
624 		next_proto = *p++;
625 		*ext_len = (*p + 1) * sizeof(uint64_t);
626 		break;
627 
628 	case IPPROTO_FRAGMENT:
629 		next_proto = *p;
630 		*ext_len = RTE_IPV6_FRAG_HDR_SIZE;
631 		break;
632 
633 	default:
634 		return -EINVAL;
635 	}
636 
637 	return next_proto;
638 }
639 
640 #ifdef __cplusplus
641 }
642 #endif
643 
644 #endif /* _RTE_IP_H_ */
645