net: add IPv6 header extension parsing function
[dpdk.git] / lib / librte_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 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <netinet/ip.h>
22
23 #include <rte_byteorder.h>
24 #include <rte_mbuf.h>
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 /**
31  * IPv4 Header
32  */
33 struct rte_ipv4_hdr {
34         uint8_t  version_ihl;           /**< version and header length */
35         uint8_t  type_of_service;       /**< type of service */
36         uint16_t total_length;          /**< length of packet */
37         uint16_t packet_id;             /**< packet ID */
38         uint16_t fragment_offset;       /**< fragmentation offset */
39         uint8_t  time_to_live;          /**< time to live */
40         uint8_t  next_proto_id;         /**< protocol ID */
41         uint16_t hdr_checksum;          /**< header checksum */
42         uint32_t src_addr;              /**< source address */
43         uint32_t dst_addr;              /**< destination address */
44 } __attribute__((__packed__));
45
46 /** Create IPv4 address */
47 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
48                                            (((b) & 0xff) << 16) | \
49                                            (((c) & 0xff) << 8)  | \
50                                            ((d) & 0xff))
51
52 /** Maximal IPv4 packet length (including a header) */
53 #define RTE_IPV4_MAX_PKT_LEN        65535
54
55 /** Internet header length mask for version_ihl field */
56 #define RTE_IPV4_HDR_IHL_MASK   (0x0f)
57 /**
58  * Internet header length field multiplier (IHL field specifies overall header
59  * length in number of 4-byte words)
60  */
61 #define RTE_IPV4_IHL_MULTIPLIER (4)
62
63 /* Type of Service fields */
64 #define RTE_IPV4_HDR_DSCP_MASK  (0xfc)
65 #define RTE_IPV4_HDR_ECN_MASK   (0x03)
66
67 /* Fragment Offset * Flags. */
68 #define RTE_IPV4_HDR_DF_SHIFT   14
69 #define RTE_IPV4_HDR_MF_SHIFT   13
70 #define RTE_IPV4_HDR_FO_SHIFT   3
71
72 #define RTE_IPV4_HDR_DF_FLAG    (1 << RTE_IPV4_HDR_DF_SHIFT)
73 #define RTE_IPV4_HDR_MF_FLAG    (1 << RTE_IPV4_HDR_MF_SHIFT)
74
75 #define RTE_IPV4_HDR_OFFSET_MASK        ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
76
77 #define RTE_IPV4_HDR_OFFSET_UNITS       8
78
79 /*
80  * IPv4 address types
81  */
82 #define RTE_IPV4_ANY              ((uint32_t)0x00000000) /**< 0.0.0.0 */
83 #define RTE_IPV4_LOOPBACK         ((uint32_t)0x7f000001) /**< 127.0.0.1 */
84 #define RTE_IPV4_BROADCAST        ((uint32_t)0xe0000000) /**< 224.0.0.0 */
85 #define RTE_IPV4_ALLHOSTS_GROUP   ((uint32_t)0xe0000001) /**< 224.0.0.1 */
86 #define RTE_IPV4_ALLRTRS_GROUP    ((uint32_t)0xe0000002) /**< 224.0.0.2 */
87 #define RTE_IPV4_MAX_LOCAL_GROUP  ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
88
89 /*
90  * IPv4 Multicast-related macros
91  */
92 #define RTE_IPV4_MIN_MCAST \
93         RTE_IPV4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
94 #define RTE_IPV4_MAX_MCAST \
95         RTE_IPV4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
96
97 #define RTE_IS_IPV4_MCAST(x) \
98         ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
99         /**< check if IPv4 address is multicast */
100
101 /* IPv4 default fields values */
102 #define RTE_IPV4_MIN_IHL    (0x5)
103 #define RTE_IPV4_VHL_DEF    (IPVERSION | RTE_IPV4_MIN_IHL)
104
105 /**
106  * @internal Calculate a sum of all words in the buffer.
107  * Helper routine for the rte_raw_cksum().
108  *
109  * @param buf
110  *   Pointer to the buffer.
111  * @param len
112  *   Length of the buffer.
113  * @param sum
114  *   Initial value of the sum.
115  * @return
116  *   sum += Sum of all words in the buffer.
117  */
118 static inline uint32_t
119 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
120 {
121         /* workaround gcc strict-aliasing warning */
122         uintptr_t ptr = (uintptr_t)buf;
123         typedef uint16_t __attribute__((__may_alias__)) u16_p;
124         const u16_p *u16_buf = (const u16_p *)ptr;
125
126         while (len >= (sizeof(*u16_buf) * 4)) {
127                 sum += u16_buf[0];
128                 sum += u16_buf[1];
129                 sum += u16_buf[2];
130                 sum += u16_buf[3];
131                 len -= sizeof(*u16_buf) * 4;
132                 u16_buf += 4;
133         }
134         while (len >= sizeof(*u16_buf)) {
135                 sum += *u16_buf;
136                 len -= sizeof(*u16_buf);
137                 u16_buf += 1;
138         }
139
140         /* if length is in odd bytes */
141         if (len == 1)
142                 sum += *((const uint8_t *)u16_buf);
143
144         return sum;
145 }
146
147 /**
148  * @internal Reduce a sum to the non-complemented checksum.
149  * Helper routine for the rte_raw_cksum().
150  *
151  * @param sum
152  *   Value of the sum.
153  * @return
154  *   The non-complemented checksum.
155  */
156 static inline uint16_t
157 __rte_raw_cksum_reduce(uint32_t sum)
158 {
159         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
160         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
161         return (uint16_t)sum;
162 }
163
164 /**
165  * Process the non-complemented checksum of a buffer.
166  *
167  * @param buf
168  *   Pointer to the buffer.
169  * @param len
170  *   Length of the buffer.
171  * @return
172  *   The non-complemented checksum.
173  */
174 static inline uint16_t
175 rte_raw_cksum(const void *buf, size_t len)
176 {
177         uint32_t sum;
178
179         sum = __rte_raw_cksum(buf, len, 0);
180         return __rte_raw_cksum_reduce(sum);
181 }
182
183 /**
184  * Compute the raw (non complemented) checksum of a packet.
185  *
186  * @param m
187  *   The pointer to the mbuf.
188  * @param off
189  *   The offset in bytes to start the checksum.
190  * @param len
191  *   The length in bytes of the data to checksum.
192  * @param cksum
193  *   A pointer to the checksum, filled on success.
194  * @return
195  *   0 on success, -1 on error (bad length or offset).
196  */
197 static inline int
198 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
199         uint16_t *cksum)
200 {
201         const struct rte_mbuf *seg;
202         const char *buf;
203         uint32_t sum, tmp;
204         uint32_t seglen, done;
205
206         /* easy case: all data in the first segment */
207         if (off + len <= rte_pktmbuf_data_len(m)) {
208                 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
209                                 const char *, off), len);
210                 return 0;
211         }
212
213         if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
214                 return -1; /* invalid params, return a dummy value */
215
216         /* else browse the segment to find offset */
217         seglen = 0;
218         for (seg = m; seg != NULL; seg = seg->next) {
219                 seglen = rte_pktmbuf_data_len(seg);
220                 if (off < seglen)
221                         break;
222                 off -= seglen;
223         }
224         seglen -= off;
225         buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
226         if (seglen >= len) {
227                 /* all in one segment */
228                 *cksum = rte_raw_cksum(buf, len);
229                 return 0;
230         }
231
232         /* hard case: process checksum of several segments */
233         sum = 0;
234         done = 0;
235         for (;;) {
236                 tmp = __rte_raw_cksum(buf, seglen, 0);
237                 if (done & 1)
238                         tmp = rte_bswap16((uint16_t)tmp);
239                 sum += tmp;
240                 done += seglen;
241                 if (done == len)
242                         break;
243                 seg = seg->next;
244                 buf = rte_pktmbuf_mtod(seg, const char *);
245                 seglen = rte_pktmbuf_data_len(seg);
246                 if (seglen > len - done)
247                         seglen = len - done;
248         }
249
250         *cksum = __rte_raw_cksum_reduce(sum);
251         return 0;
252 }
253
254 /**
255  * Process the IPv4 checksum of an IPv4 header.
256  *
257  * The checksum field must be set to 0 by the caller.
258  *
259  * @param ipv4_hdr
260  *   The pointer to the contiguous IPv4 header.
261  * @return
262  *   The complemented checksum to set in the IP packet.
263  */
264 static inline uint16_t
265 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
266 {
267         uint16_t cksum;
268         cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
269         return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
270 }
271
272 /**
273  * Process the pseudo-header checksum of an IPv4 header.
274  *
275  * The checksum field must be set to 0 by the caller.
276  *
277  * Depending on the ol_flags, the pseudo-header checksum expected by the
278  * drivers is not the same. For instance, when TSO is enabled, the IP
279  * payload length must not be included in the packet.
280  *
281  * When ol_flags is 0, it computes the standard pseudo-header checksum.
282  *
283  * @param ipv4_hdr
284  *   The pointer to the contiguous IPv4 header.
285  * @param ol_flags
286  *   The ol_flags of the associated mbuf.
287  * @return
288  *   The non-complemented checksum to set in the L4 header.
289  */
290 static inline uint16_t
291 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
292 {
293         struct ipv4_psd_header {
294                 uint32_t src_addr; /* IP address of source host. */
295                 uint32_t dst_addr; /* IP address of destination host. */
296                 uint8_t  zero;     /* zero. */
297                 uint8_t  proto;    /* L4 protocol type. */
298                 uint16_t len;      /* L4 length. */
299         } psd_hdr;
300
301         psd_hdr.src_addr = ipv4_hdr->src_addr;
302         psd_hdr.dst_addr = ipv4_hdr->dst_addr;
303         psd_hdr.zero = 0;
304         psd_hdr.proto = ipv4_hdr->next_proto_id;
305         if (ol_flags & PKT_TX_TCP_SEG) {
306                 psd_hdr.len = 0;
307         } else {
308                 psd_hdr.len = rte_cpu_to_be_16(
309                         (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
310                                 - sizeof(struct rte_ipv4_hdr)));
311         }
312         return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
313 }
314
315 /**
316  * Process the IPv4 UDP or TCP checksum.
317  *
318  * The IPv4 header should not contains options. The IP and layer 4
319  * checksum must be set to 0 in the packet by the caller.
320  *
321  * @param ipv4_hdr
322  *   The pointer to the contiguous IPv4 header.
323  * @param l4_hdr
324  *   The pointer to the beginning of the L4 header.
325  * @return
326  *   The complemented checksum to set in the IP packet
327  *   or 0 on error
328  */
329 static inline uint16_t
330 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
331 {
332         uint32_t cksum;
333         uint32_t l3_len, l4_len;
334
335         l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
336         if (l3_len < sizeof(struct rte_ipv4_hdr))
337                 return 0;
338
339         l4_len = l3_len - sizeof(struct rte_ipv4_hdr);
340
341         cksum = rte_raw_cksum(l4_hdr, l4_len);
342         cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
343
344         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
345         cksum = (~cksum) & 0xffff;
346         if (cksum == 0)
347                 cksum = 0xffff;
348
349         return (uint16_t)cksum;
350 }
351
352 /**
353  * IPv6 Header
354  */
355 struct rte_ipv6_hdr {
356         uint32_t vtc_flow;     /**< IP version, traffic class & flow label. */
357         uint16_t payload_len;  /**< IP packet length - includes sizeof(ip_header). */
358         uint8_t  proto;        /**< Protocol, next header. */
359         uint8_t  hop_limits;   /**< Hop limits. */
360         uint8_t  src_addr[16]; /**< IP address of source host. */
361         uint8_t  dst_addr[16]; /**< IP address of destination host(s). */
362 } __attribute__((__packed__));
363
364 /* IPv6 vtc_flow: IPv / TC / flow_label */
365 #define RTE_IPV6_HDR_FL_SHIFT 0
366 #define RTE_IPV6_HDR_TC_SHIFT 20
367 #define RTE_IPV6_HDR_FL_MASK    ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
368 #define RTE_IPV6_HDR_TC_MASK    (0xff << RTE_IPV6_HDR_TC_SHIFT)
369 #define RTE_IPV6_HDR_DSCP_MASK  (0xfc << RTE_IPV6_HDR_TC_SHIFT)
370 #define RTE_IPV6_HDR_ECN_MASK   (0x03 << RTE_IPV6_HDR_TC_SHIFT)
371
372 /**
373  * Process the pseudo-header checksum of an IPv6 header.
374  *
375  * Depending on the ol_flags, the pseudo-header checksum expected by the
376  * drivers is not the same. For instance, when TSO is enabled, the IPv6
377  * payload length must not be included in the packet.
378  *
379  * When ol_flags is 0, it computes the standard pseudo-header checksum.
380  *
381  * @param ipv6_hdr
382  *   The pointer to the contiguous IPv6 header.
383  * @param ol_flags
384  *   The ol_flags of the associated mbuf.
385  * @return
386  *   The non-complemented checksum to set in the L4 header.
387  */
388 static inline uint16_t
389 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
390 {
391         uint32_t sum;
392         struct {
393                 uint32_t len;   /* L4 length. */
394                 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
395         } psd_hdr;
396
397         psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
398         if (ol_flags & PKT_TX_TCP_SEG) {
399                 psd_hdr.len = 0;
400         } else {
401                 psd_hdr.len = ipv6_hdr->payload_len;
402         }
403
404         sum = __rte_raw_cksum(ipv6_hdr->src_addr,
405                 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
406                 0);
407         sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
408         return __rte_raw_cksum_reduce(sum);
409 }
410
411 /**
412  * Process the IPv6 UDP or TCP checksum.
413  *
414  * The IPv4 header should not contains options. The layer 4 checksum
415  * must be set to 0 in the packet by the caller.
416  *
417  * @param ipv6_hdr
418  *   The pointer to the contiguous IPv6 header.
419  * @param l4_hdr
420  *   The pointer to the beginning of the L4 header.
421  * @return
422  *   The complemented checksum to set in the IP packet.
423  */
424 static inline uint16_t
425 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
426 {
427         uint32_t cksum;
428         uint32_t l4_len;
429
430         l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
431
432         cksum = rte_raw_cksum(l4_hdr, l4_len);
433         cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
434
435         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
436         cksum = (~cksum) & 0xffff;
437         if (cksum == 0)
438                 cksum = 0xffff;
439
440         return (uint16_t)cksum;
441 }
442
443 /* IPv6 fragmentation header size */
444 #define RTE_IPV6_FRAG_HDR_SIZE 8
445
446 /**
447  * Parse next IPv6 header extension
448  *
449  * This function checks if proto number is an IPv6 extensions and parses its
450  * data if so, providing information on next header and extension length.
451  *
452  * @param p
453  *   Pointer to an extension raw data.
454  * @param proto
455  *   Protocol number extracted from the "next header" field from
456  *   the IPv6 header or the previous extension.
457  * @param ext_len
458  *   Extension data length.
459  * @return
460  *   next protocol number if proto is an IPv6 extension, -EINVAL otherwise
461  */
462 __rte_experimental
463 static inline int
464 rte_ipv6_get_next_ext(uint8_t *p, int proto, size_t *ext_len)
465 {
466         int next_proto;
467
468         switch (proto) {
469         case IPPROTO_AH:
470                 next_proto = *p++;
471                 *ext_len = (*p + 2) * sizeof(uint32_t);
472                 break;
473
474         case IPPROTO_HOPOPTS:
475         case IPPROTO_ROUTING:
476         case IPPROTO_DSTOPTS:
477                 next_proto = *p++;
478                 *ext_len = (*p + 1) * sizeof(uint64_t);
479                 break;
480
481         case IPPROTO_FRAGMENT:
482                 next_proto = *p;
483                 *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
484                 break;
485
486         default:
487                 return -EINVAL;
488         }
489
490         return next_proto;
491 }
492
493 #ifdef __cplusplus
494 }
495 #endif
496
497 #endif /* _RTE_IP_H_ */