net: add rte prefix to IP structure
[dpdk.git] / lib / librte_ipsec / iph.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #ifndef _IPH_H_
6 #define _IPH_H_
7
8 /**
9  * @file iph.h
10  * Contains functions/structures/macros to manipulate IPv4/IPv6 headers
11  * used internally by ipsec library.
12  */
13
14 /*
15  * Move preceding (L3) headers down to remove ESP header and IV.
16  */
17 static inline void
18 remove_esph(char *np, char *op, uint32_t hlen)
19 {
20         uint32_t i;
21
22         for (i = hlen; i-- != 0; np[i] = op[i])
23                 ;
24 }
25
26 /*
27  * Move preceding (L3) headers up to free space for ESP header and IV.
28  */
29 static inline void
30 insert_esph(char *np, char *op, uint32_t hlen)
31 {
32         uint32_t i;
33
34         for (i = 0; i != hlen; i++)
35                 np[i] = op[i];
36 }
37
38 /* update original ip header fields for transport case */
39 static inline int
40 update_trs_l3hdr(const struct rte_ipsec_sa *sa, void *p, uint32_t plen,
41                 uint32_t l2len, uint32_t l3len, uint8_t proto)
42 {
43         struct rte_ipv4_hdr *v4h;
44         struct rte_ipv6_hdr *v6h;
45         int32_t rc;
46
47         if ((sa->type & RTE_IPSEC_SATP_IPV_MASK) == RTE_IPSEC_SATP_IPV4) {
48                 v4h = p;
49                 rc = v4h->next_proto_id;
50                 v4h->next_proto_id = proto;
51                 v4h->total_length = rte_cpu_to_be_16(plen - l2len);
52         } else if (l3len == sizeof(*v6h)) {
53                 v6h = p;
54                 rc = v6h->proto;
55                 v6h->proto = proto;
56                 v6h->payload_len = rte_cpu_to_be_16(plen - l2len -
57                                 sizeof(*v6h));
58         /* need to add support for IPv6 with options */
59         } else
60                 rc = -ENOTSUP;
61
62         return rc;
63 }
64
65 /* update original and new ip header fields for tunnel case */
66 static inline void
67 update_tun_l3hdr(const struct rte_ipsec_sa *sa, void *p, uint32_t plen,
68                 uint32_t l2len, rte_be16_t pid)
69 {
70         struct rte_ipv4_hdr *v4h;
71         struct rte_ipv6_hdr *v6h;
72
73         if (sa->type & RTE_IPSEC_SATP_MODE_TUNLV4) {
74                 v4h = p;
75                 v4h->packet_id = pid;
76                 v4h->total_length = rte_cpu_to_be_16(plen - l2len);
77         } else {
78                 v6h = p;
79                 v6h->payload_len = rte_cpu_to_be_16(plen - l2len -
80                                 sizeof(*v6h));
81         }
82 }
83
84 #endif /* _IPH_H_ */