File: iph.h

package info (click to toggle)
dpdk 25.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 127,892 kB
  • sloc: ansic: 2,358,479; python: 16,426; sh: 4,474; makefile: 1,713; awk: 70
file content (279 lines) | stat: -rw-r--r-- 7,607 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Intel Corporation
 */

#ifndef _IPH_H_
#define _IPH_H_

#include <rte_ip.h>

/**
 * @file iph.h
 * Contains functions/structures/macros to manipulate IPv4/IPv6 headers
 * used internally by ipsec library.
 */

/*
 * Move preceding (L3) headers down to remove ESP header and IV.
 */
static inline void
remove_esph(char *np, char *op, uint32_t hlen)
{
	uint32_t i;

	for (i = hlen; i-- != 0; np[i] = op[i])
		;
}

/*
 * Move preceding (L3) headers up to free space for ESP header and IV.
 */
static inline void
insert_esph(char *np, char *op, uint32_t hlen)
{
	uint32_t i;

	for (i = 0; i != hlen; i++)
		np[i] = op[i];
}

/* update original ip header fields for transport case */
static inline int
update_trs_l3hdr(const struct rte_ipsec_sa *sa, void *p, uint32_t plen,
		uint32_t l2len, uint32_t l3len, uint8_t proto)
{
	int32_t rc;

	/* IPv4 */
	if ((sa->type & RTE_IPSEC_SATP_IPV_MASK) == RTE_IPSEC_SATP_IPV4) {
		struct rte_ipv4_hdr *v4h;

		v4h = p;
		rc = v4h->next_proto_id;
		v4h->next_proto_id = proto;
		v4h->total_length = rte_cpu_to_be_16(plen - l2len);
	/* IPv6 */
	} else {
		struct rte_ipv6_hdr *v6h;
		uint8_t *p_nh;

		v6h = p;

		/* basic IPv6 header with no extensions */
		if (l3len == sizeof(struct rte_ipv6_hdr))
			p_nh = &v6h->proto;

		/* IPv6 with extensions */
		else {
			size_t ext_len;
			int nh;
			uint8_t *pd, *plimit;

			/* locate last extension within l3len bytes */
			pd = (uint8_t *)p;
			plimit = pd + l3len;
			ext_len = sizeof(struct rte_ipv6_hdr);
			nh = v6h->proto;
			while (pd + ext_len < plimit) {
				pd += ext_len;
				nh = rte_ipv6_get_next_ext(pd, nh, &ext_len);
				if (unlikely(nh < 0))
					return -EINVAL;
			}

			/* invalid l3len - extension exceeds header length */
			if (unlikely(pd + ext_len != plimit))
				return -EINVAL;

			/* save last extension offset */
			p_nh = pd;
		}

		/* update header type; return original value */
		rc = *p_nh;
		*p_nh = proto;

		/* fix packet length */
		v6h->payload_len = rte_cpu_to_be_16(plen - l2len -
				sizeof(*v6h));
	}

	return rc;
}

/*
 * Inline functions to get and set ipv6 packet header traffic class (TC) field.
 */
static inline uint8_t
get_ipv6_tc(rte_be32_t vtc_flow)
{
	uint32_t v;

	v = rte_be_to_cpu_32(vtc_flow);
	return v >> RTE_IPV6_HDR_TC_SHIFT;
}

static inline rte_be32_t
set_ipv6_tc(rte_be32_t vtc_flow, uint32_t tos)
{
	uint32_t v;

	v = rte_cpu_to_be_32(tos << RTE_IPV6_HDR_TC_SHIFT);
	vtc_flow &= ~rte_cpu_to_be_32(RTE_IPV6_HDR_TC_MASK);

	return (v | vtc_flow);
}

/**
 * Update type-of-service/traffic-class field of outbound tunnel packet.
 *
 * @param ref_h: reference header, for outbound it is inner header, otherwise
 *   outer header.
 * @param update_h: header to be updated tos/tc field, for outbound it is outer
 *   header, otherwise inner header.
 * @param tos_mask: type-of-service mask stored in sa.
 * @param is_outh_ipv4: 1 if outer header is ipv4, 0 if it is ipv6.
 * @param is_inner_ipv4: 1 if inner header is ipv4, 0 if it is ipv6.
 */
static inline void
update_outb_tun_tos(const void *ref_h, void *update_h, uint32_t tos_mask,
		uint8_t is_outh_ipv4, uint8_t is_inh_ipv4)
{
	uint8_t idx = ((is_outh_ipv4 << 1) | is_inh_ipv4);
	struct rte_ipv4_hdr *v4out_h;
	struct rte_ipv6_hdr *v6out_h;
	uint32_t itp, otp;

	switch (idx) {
	case 0: /*outh ipv6, inh ipv6 */
		v6out_h = update_h;
		otp = get_ipv6_tc(v6out_h->vtc_flow) & ~tos_mask;
		itp = get_ipv6_tc(((const struct rte_ipv6_hdr *)ref_h)->
				vtc_flow) & tos_mask;
		v6out_h->vtc_flow = set_ipv6_tc(v6out_h->vtc_flow, otp | itp);
		break;
	case 1: /*outh ipv6, inh ipv4 */
		v6out_h = update_h;
		otp = get_ipv6_tc(v6out_h->vtc_flow) & ~tos_mask;
		itp = ((const struct rte_ipv4_hdr *)ref_h)->type_of_service &
				tos_mask;
		v6out_h->vtc_flow = set_ipv6_tc(v6out_h->vtc_flow, otp | itp);
		break;
	case 2: /*outh ipv4, inh ipv6 */
		v4out_h = update_h;
		otp = v4out_h->type_of_service & ~tos_mask;
		itp = get_ipv6_tc(((const struct rte_ipv6_hdr *)ref_h)->
				vtc_flow) & tos_mask;
		v4out_h->type_of_service = (otp | itp);
		break;
	case 3: /* outh ipv4, inh ipv4 */
		v4out_h = update_h;
		otp = v4out_h->type_of_service & ~tos_mask;
		itp = ((const struct rte_ipv4_hdr *)ref_h)->type_of_service &
				tos_mask;
		v4out_h->type_of_service = (otp | itp);
		break;
	}
}

/**
 * Update type-of-service/traffic-class field of inbound tunnel packet.
 *
 * @param ref_h: reference header, for outbound it is inner header, otherwise
 *   outer header.
 * @param update_h: header to be updated tos/tc field, for outbound it is outer
 *   header, otherwise inner header.
 * @param is_outh_ipv4: 1 if outer header is ipv4, 0 if it is ipv6.
 * @param is_inner_ipv4: 1 if inner header is ipv4, 0 if it is ipv6.
 */
static inline void
update_inb_tun_tos(const void *ref_h, void *update_h,
		uint8_t is_outh_ipv4, uint8_t is_inh_ipv4)
{
	uint8_t idx = ((is_outh_ipv4 << 1) | is_inh_ipv4);
	struct rte_ipv4_hdr *v4in_h;
	struct rte_ipv6_hdr *v6in_h;
	uint8_t ecn_v4out, ecn_v4in;
	uint32_t ecn_v6out, ecn_v6in;

	switch (idx) {
	case 0: /* outh ipv6, inh ipv6 */
		v6in_h = update_h;
		ecn_v6out = ((const struct rte_ipv6_hdr *)ref_h)->vtc_flow &
				rte_cpu_to_be_32(RTE_IPV6_HDR_ECN_MASK);
		ecn_v6in = v6in_h->vtc_flow &
				rte_cpu_to_be_32(RTE_IPV6_HDR_ECN_MASK);
		if ((ecn_v6out == rte_cpu_to_be_32(RTE_IPV6_HDR_ECN_CE)) &&
				(ecn_v6in != 0))
			v6in_h->vtc_flow |=
					rte_cpu_to_be_32(RTE_IPV6_HDR_ECN_CE);
		break;
	case 1: /* outh ipv6, inh ipv4 */
		v4in_h = update_h;
		ecn_v6out = ((const struct rte_ipv6_hdr *)ref_h)->vtc_flow &
				rte_cpu_to_be_32(RTE_IPV6_HDR_ECN_MASK);
		ecn_v4in = v4in_h->type_of_service & RTE_IPV4_HDR_ECN_MASK;
		if ((ecn_v6out == rte_cpu_to_be_32(RTE_IPV6_HDR_ECN_CE)) &&
				(ecn_v4in != 0))
			v4in_h->type_of_service |= RTE_IPV4_HDR_ECN_CE;
		break;
	case 2: /* outh ipv4, inh ipv6 */
		v6in_h = update_h;
		ecn_v4out = ((const struct rte_ipv4_hdr *)ref_h)->
				type_of_service & RTE_IPV4_HDR_ECN_MASK;
		ecn_v6in = v6in_h->vtc_flow &
				rte_cpu_to_be_32(RTE_IPV6_HDR_ECN_MASK);
		if (ecn_v4out == RTE_IPV4_HDR_ECN_CE && ecn_v6in != 0)
			v6in_h->vtc_flow |=
					rte_cpu_to_be_32(RTE_IPV6_HDR_ECN_CE);
		break;
	case 3: /* outh ipv4, inh ipv4 */
		v4in_h = update_h;
		ecn_v4out = ((const struct rte_ipv4_hdr *)ref_h)->
				type_of_service & RTE_IPV4_HDR_ECN_MASK;
		ecn_v4in = v4in_h->type_of_service & RTE_IPV4_HDR_ECN_MASK;
		if (ecn_v4out == RTE_IPV4_HDR_ECN_CE && ecn_v4in != 0)
			v4in_h->type_of_service |= RTE_IPV4_HDR_ECN_CE;
		break;
	}
}

/* update original and new ip header fields for tunnel case */
static inline void
update_tun_outb_l3hdr(const struct rte_ipsec_sa *sa, void *outh,
		const void *inh, uint32_t plen, uint32_t l2len, rte_be16_t pid)
{
	struct rte_ipv4_hdr *v4h;
	struct rte_ipv6_hdr *v6h;
	uint8_t is_outh_ipv4;

	if (sa->type & RTE_IPSEC_SATP_MODE_TUNLV4) {
		is_outh_ipv4 = 1;
		v4h = outh;
		v4h->packet_id = pid;
		v4h->total_length = rte_cpu_to_be_16(plen - l2len);
	} else {
		is_outh_ipv4 = 0;
		v6h = outh;
		v6h->payload_len = rte_cpu_to_be_16(plen - l2len -
				sizeof(*v6h));
	}

	if (sa->type & TUN_HDR_MSK)
		update_outb_tun_tos(inh, outh, sa->tos_mask, is_outh_ipv4,
				((sa->type & RTE_IPSEC_SATP_IPV_MASK) ==
					RTE_IPSEC_SATP_IPV4));
}

static inline void
update_tun_inb_l3hdr(const struct rte_ipsec_sa *sa, const void *outh,
		void *inh)
{
	if (sa->type & TUN_HDR_MSK)
		update_inb_tun_tos(outh, inh,
				((sa->type & RTE_IPSEC_SATP_MODE_TUNLV4) != 0),
				((sa->type & RTE_IPSEC_SATP_IPV_MASK) ==
						RTE_IPSEC_SATP_IPV4));
}

#endif /* _IPH_H_ */