xref: /dpdk/lib/ip_frag/rte_ipv4_fragmentation.c (revision 72206323a5dd3182b13f61b25a64abdddfee595c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stddef.h>
6 #include <errno.h>
7 
8 #include <rte_ether.h>
9 
10 #include "ip_frag_common.h"
11 
12 /* Fragment Offset */
13 #define	RTE_IPV4_HDR_DF_SHIFT			14
14 #define	RTE_IPV4_HDR_MF_SHIFT			13
15 #define	RTE_IPV4_HDR_FO_SHIFT			3
16 
17 #define	IPV4_HDR_DF_MASK			(1 << RTE_IPV4_HDR_DF_SHIFT)
18 #define	IPV4_HDR_MF_MASK			(1 << RTE_IPV4_HDR_MF_SHIFT)
19 
20 #define	IPV4_HDR_FO_ALIGN			(1 << RTE_IPV4_HDR_FO_SHIFT)
21 
22 #define IPV4_HDR_MAX_LEN			60
23 
24 static inline void __fill_ipv4hdr_frag(struct rte_ipv4_hdr *dst,
25 		const struct rte_ipv4_hdr *src, uint16_t header_len,
26 		uint16_t len, uint16_t fofs, uint16_t dofs, uint32_t mf)
27 {
28 	memcpy(dst, src, header_len);
29 	fofs = (uint16_t)(fofs + (dofs >> RTE_IPV4_HDR_FO_SHIFT));
30 	fofs = (uint16_t)(fofs | mf << RTE_IPV4_HDR_MF_SHIFT);
31 	dst->fragment_offset = rte_cpu_to_be_16(fofs);
32 	dst->total_length = rte_cpu_to_be_16(len);
33 	dst->hdr_checksum = 0;
34 }
35 
36 static inline void __free_fragments(struct rte_mbuf *mb[], uint32_t num)
37 {
38 	uint32_t i;
39 	for (i = 0; i != num; i++)
40 		rte_pktmbuf_free(mb[i]);
41 }
42 
43 static inline uint16_t __create_ipopt_frag_hdr(uint8_t *iph,
44 	uint16_t ipopt_len, uint8_t *ipopt_frag_hdr)
45 {
46 	uint16_t len = ipopt_len;
47 	struct rte_ipv4_hdr *iph_opt = (struct rte_ipv4_hdr *)ipopt_frag_hdr;
48 
49 	ipopt_len = 0;
50 	memcpy(ipopt_frag_hdr, iph, sizeof(struct rte_ipv4_hdr));
51 	ipopt_frag_hdr += sizeof(struct rte_ipv4_hdr);
52 
53 	uint8_t *p_opt = iph + sizeof(struct rte_ipv4_hdr);
54 
55 	while (len > 0) {
56 		if (unlikely(*p_opt == RTE_IPV4_HDR_OPT_NOP)) {
57 			len--;
58 			p_opt++;
59 			continue;
60 		} else if (unlikely(*p_opt == RTE_IPV4_HDR_OPT_EOL))
61 			break;
62 
63 		if (unlikely(p_opt[1] < 2 || p_opt[1] > len))
64 			break;
65 
66 		if (RTE_IPV4_HDR_OPT_COPIED(*p_opt)) {
67 			memcpy(ipopt_frag_hdr + ipopt_len,
68 				p_opt, p_opt[1]);
69 			ipopt_len += p_opt[1];
70 		}
71 
72 		len -= p_opt[1];
73 		p_opt += p_opt[1];
74 	}
75 
76 	len = RTE_ALIGN_CEIL(ipopt_len, RTE_IPV4_IHL_MULTIPLIER);
77 	memset(ipopt_frag_hdr + ipopt_len,
78 		RTE_IPV4_HDR_OPT_EOL, len - ipopt_len);
79 	ipopt_len = len;
80 	iph_opt->ihl = (sizeof(struct rte_ipv4_hdr) + ipopt_len) /
81 		RTE_IPV4_IHL_MULTIPLIER;
82 
83 	return ipopt_len;
84 }
85 
86 /**
87  * IPv4 fragmentation.
88  *
89  * This function implements the fragmentation of IPv4 packets.
90  *
91  * @param pkt_in
92  *   The input packet.
93  * @param pkts_out
94  *   Array storing the output fragments.
95  * @param mtu_size
96  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
97  *   datagrams. This value includes the size of the IPv4 header.
98  * @param pool_direct
99  *   MBUF pool used for allocating direct buffers for the output fragments.
100  * @param pool_indirect
101  *   MBUF pool used for allocating indirect buffers for the output fragments.
102  * @return
103  *   Upon successful completion - number of output fragments placed
104  *   in the pkts_out array.
105  *   Otherwise - (-1) * <errno>.
106  */
107 int32_t
108 rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
109 	struct rte_mbuf **pkts_out,
110 	uint16_t nb_pkts_out,
111 	uint16_t mtu_size,
112 	struct rte_mempool *pool_direct,
113 	struct rte_mempool *pool_indirect)
114 {
115 	struct rte_mbuf *in_seg = NULL;
116 	struct rte_ipv4_hdr *in_hdr;
117 	uint32_t out_pkt_pos, in_seg_data_pos;
118 	uint32_t more_in_segs;
119 	uint16_t fragment_offset, flag_offset, frag_size, header_len;
120 	uint16_t frag_bytes_remaining;
121 	uint8_t ipopt_frag_hdr[IPV4_HDR_MAX_LEN];
122 	uint16_t ipopt_len;
123 
124 	/*
125 	 * Formal parameter checking.
126 	 */
127 	if (unlikely(pkt_in == NULL) || unlikely(pkts_out == NULL) ||
128 	    unlikely(nb_pkts_out == 0) ||
129 	    unlikely(pool_direct == NULL) || unlikely(pool_indirect == NULL) ||
130 	    unlikely(mtu_size < RTE_ETHER_MIN_MTU))
131 		return -EINVAL;
132 
133 	in_hdr = rte_pktmbuf_mtod(pkt_in, struct rte_ipv4_hdr *);
134 	header_len = (in_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
135 	    RTE_IPV4_IHL_MULTIPLIER;
136 
137 	/* Check IP header length */
138 	if (unlikely(pkt_in->data_len < header_len) ||
139 	    unlikely(mtu_size < header_len))
140 		return -EINVAL;
141 
142 	/*
143 	 * Ensure the IP payload length of all fragments is aligned to a
144 	 * multiple of 8 bytes as per RFC791 section 2.3.
145 	 */
146 	frag_size = RTE_ALIGN_FLOOR((mtu_size - header_len),
147 				    IPV4_HDR_FO_ALIGN);
148 
149 	flag_offset = rte_cpu_to_be_16(in_hdr->fragment_offset);
150 
151 	/* If Don't Fragment flag is set */
152 	if (unlikely ((flag_offset & IPV4_HDR_DF_MASK) != 0))
153 		return -ENOTSUP;
154 
155 	/* Check that pkts_out is big enough to hold all fragments */
156 	if (unlikely(frag_size * nb_pkts_out <
157 	    (uint16_t)(pkt_in->pkt_len - header_len)))
158 		return -EINVAL;
159 
160 	in_seg = pkt_in;
161 	in_seg_data_pos = header_len;
162 	out_pkt_pos = 0;
163 	fragment_offset = 0;
164 
165 	ipopt_len = header_len - sizeof(struct rte_ipv4_hdr);
166 	if (unlikely(ipopt_len > RTE_IPV4_HDR_OPT_MAX_LEN))
167 		return -EINVAL;
168 
169 	more_in_segs = 1;
170 	while (likely(more_in_segs)) {
171 		struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
172 		uint32_t more_out_segs;
173 		struct rte_ipv4_hdr *out_hdr;
174 
175 		/* Allocate direct buffer */
176 		out_pkt = rte_pktmbuf_alloc(pool_direct);
177 		if (unlikely(out_pkt == NULL)) {
178 			__free_fragments(pkts_out, out_pkt_pos);
179 			return -ENOMEM;
180 		}
181 
182 		/* Reserve space for the IP header that will be built later */
183 		out_pkt->data_len = header_len;
184 		out_pkt->pkt_len = header_len;
185 		frag_bytes_remaining = frag_size;
186 
187 		out_seg_prev = out_pkt;
188 		more_out_segs = 1;
189 		while (likely(more_out_segs && more_in_segs)) {
190 			struct rte_mbuf *out_seg = NULL;
191 			uint32_t len;
192 
193 			/* Allocate indirect buffer */
194 			out_seg = rte_pktmbuf_alloc(pool_indirect);
195 			if (unlikely(out_seg == NULL)) {
196 				rte_pktmbuf_free(out_pkt);
197 				__free_fragments(pkts_out, out_pkt_pos);
198 				return -ENOMEM;
199 			}
200 			out_seg_prev->next = out_seg;
201 			out_seg_prev = out_seg;
202 
203 			/* Prepare indirect buffer */
204 			rte_pktmbuf_attach(out_seg, in_seg);
205 			len = frag_bytes_remaining;
206 			if (len > (in_seg->data_len - in_seg_data_pos)) {
207 				len = in_seg->data_len - in_seg_data_pos;
208 			}
209 			out_seg->data_off = in_seg->data_off + in_seg_data_pos;
210 			out_seg->data_len = (uint16_t)len;
211 			out_pkt->pkt_len = (uint16_t)(len +
212 			    out_pkt->pkt_len);
213 			out_pkt->nb_segs += 1;
214 			in_seg_data_pos += len;
215 			frag_bytes_remaining -= len;
216 
217 			/* Current output packet (i.e. fragment) done ? */
218 			if (unlikely(frag_bytes_remaining == 0))
219 				more_out_segs = 0;
220 
221 			/* Current input segment done ? */
222 			if (unlikely(in_seg_data_pos == in_seg->data_len)) {
223 				in_seg = in_seg->next;
224 				in_seg_data_pos = 0;
225 
226 				if (unlikely(in_seg == NULL))
227 					more_in_segs = 0;
228 			}
229 		}
230 
231 		/* Build the IP header */
232 
233 		out_hdr = rte_pktmbuf_mtod(out_pkt, struct rte_ipv4_hdr *);
234 
235 		__fill_ipv4hdr_frag(out_hdr, in_hdr, header_len,
236 		    (uint16_t)out_pkt->pkt_len,
237 		    flag_offset, fragment_offset, more_in_segs);
238 
239 		if (unlikely((fragment_offset == 0) && (ipopt_len) &&
240 			    ((flag_offset & RTE_IPV4_HDR_OFFSET_MASK) == 0))) {
241 			ipopt_len = __create_ipopt_frag_hdr((uint8_t *)in_hdr,
242 				ipopt_len, ipopt_frag_hdr);
243 			fragment_offset = (uint16_t)(fragment_offset +
244 				out_pkt->pkt_len - header_len);
245 			out_pkt->l3_len = header_len;
246 
247 			header_len = sizeof(struct rte_ipv4_hdr) + ipopt_len;
248 			in_hdr = (struct rte_ipv4_hdr *)ipopt_frag_hdr;
249 		} else {
250 			fragment_offset = (uint16_t)(fragment_offset +
251 				out_pkt->pkt_len - header_len);
252 			out_pkt->l3_len = header_len;
253 		}
254 
255 		/* Write the fragment to the output list */
256 		pkts_out[out_pkt_pos] = out_pkt;
257 		out_pkt_pos ++;
258 	}
259 
260 	return out_pkt_pos;
261 }
262