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