xref: /dpdk/lib/ip_frag/rte_ipv6_reassembly.c (revision 89b5642d0d45c22c0ceab57efe3fab3b49ff4324)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stddef.h>
6 
7 #include <rte_memcpy.h>
8 
9 #include "ip_frag_common.h"
10 
11 /**
12  * @file
13  * IPv6 reassemble
14  *
15  * Implementation of IPv6 reassembly.
16  */
17 
18 static inline void
19 ip_frag_memmove(char *dst, char *src, int len)
20 {
21 	int i;
22 
23 	/* go backwards to make sure we don't overwrite anything important */
24 	for (i = len - 1; i >= 0; i--)
25 		dst[i] = src[i];
26 }
27 
28 /*
29  * Reassemble fragments into one packet.
30  */
31 struct rte_mbuf *
32 ipv6_frag_reassemble(struct ip_frag_pkt *fp)
33 {
34 	struct rte_ipv6_hdr *ip_hdr;
35 	struct rte_ipv6_fragment_ext *frag_hdr;
36 	struct rte_mbuf *m, *prev;
37 	uint32_t i, n, ofs, first_len;
38 	uint32_t last_len, move_len, payload_len;
39 	uint32_t curr_idx = 0;
40 
41 	first_len = fp->frags[IP_FIRST_FRAG_IDX].len;
42 	n = fp->last_idx - 1;
43 
44 	/*start from the last fragment. */
45 	m = fp->frags[IP_LAST_FRAG_IDX].mb;
46 	ofs = fp->frags[IP_LAST_FRAG_IDX].ofs;
47 	last_len = fp->frags[IP_LAST_FRAG_IDX].len;
48 	curr_idx = IP_LAST_FRAG_IDX;
49 
50 	payload_len = ofs + last_len;
51 
52 	while (ofs != first_len) {
53 
54 		prev = m;
55 
56 		for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) {
57 
58 			/* previous fragment found. */
59 			if (fp->frags[i].ofs + fp->frags[i].len == ofs) {
60 
61 				RTE_ASSERT(curr_idx != i);
62 
63 				/* adjust start of the last fragment data. */
64 				rte_pktmbuf_adj(m,
65 					(uint16_t)(m->l2_len + m->l3_len));
66 				rte_pktmbuf_chain(fp->frags[i].mb, m);
67 
68 				/* this mbuf should not be accessed directly */
69 				fp->frags[curr_idx].mb = NULL;
70 				curr_idx = i;
71 
72 				/* update our last fragment and offset. */
73 				m = fp->frags[i].mb;
74 				ofs = fp->frags[i].ofs;
75 			}
76 		}
77 
78 		/* error - hole in the packet. */
79 		if (m == prev) {
80 			return NULL;
81 		}
82 	}
83 
84 	/* chain with the first fragment. */
85 	rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
86 	rte_pktmbuf_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m);
87 	fp->frags[curr_idx].mb = NULL;
88 	m = fp->frags[IP_FIRST_FRAG_IDX].mb;
89 	fp->frags[IP_FIRST_FRAG_IDX].mb = NULL;
90 
91 	/* update ipv6 header for the reassembled datagram */
92 	ip_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *, m->l2_len);
93 
94 	ip_hdr->payload_len = rte_cpu_to_be_16(payload_len);
95 
96 	/*
97 	 * remove fragmentation header. note that per RFC2460, we need to update
98 	 * the last non-fragmentable header with the "next header" field to contain
99 	 * type of the first fragmentable header, but we currently don't support
100 	 * other headers, so we assume there are no other headers and thus update
101 	 * the main IPv6 header instead.
102 	 */
103 	move_len = m->l2_len + m->l3_len - sizeof(*frag_hdr);
104 	frag_hdr = (struct rte_ipv6_fragment_ext *) (ip_hdr + 1);
105 	ip_hdr->proto = frag_hdr->next_header;
106 
107 	ip_frag_memmove(rte_pktmbuf_mtod_offset(m, char *, sizeof(*frag_hdr)),
108 			rte_pktmbuf_mtod(m, char*), move_len);
109 
110 	rte_pktmbuf_adj(m, sizeof(*frag_hdr));
111 
112 	return m;
113 }
114 
115 /*
116  * Process new mbuf with fragment of IPV6 datagram.
117  * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
118  * @param tbl
119  *   Table where to lookup/add the fragmented packet.
120  * @param mb
121  *   Incoming mbuf with IPV6 fragment.
122  * @param tms
123  *   Fragment arrival timestamp.
124  * @param ip_hdr
125  *   Pointer to the IPV6 header.
126  * @param frag_hdr
127  *   Pointer to the IPV6 fragment extension header.
128  * @return
129  *   Pointer to mbuf for reassembled packet, or NULL if:
130  *   - an error occurred.
131  *   - not all fragments of the packet are collected yet.
132  */
133 #define MORE_FRAGS(x) (((x) & 0x100) >> 8)
134 #define FRAG_OFFSET(x) (rte_cpu_to_be_16(x) >> 3)
135 struct rte_mbuf *
136 rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
137 	struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms,
138 	struct rte_ipv6_hdr *ip_hdr, struct rte_ipv6_fragment_ext *frag_hdr)
139 {
140 	struct ip_frag_pkt *fp;
141 	struct ip_frag_key key;
142 	uint16_t ip_ofs;
143 	int32_t ip_len;
144 	int32_t trim;
145 
146 	rte_memcpy(&key.src_dst[0], &ip_hdr->src_addr, 16);
147 	rte_memcpy(&key.src_dst[2], &ip_hdr->dst_addr, 16);
148 
149 	key.id = frag_hdr->id;
150 	key.key_len = IPV6_KEYLEN;
151 
152 	ip_ofs = FRAG_OFFSET(frag_hdr->frag_data) * 8;
153 
154 	/*
155 	 * as per RFC2460, payload length contains all extension headers
156 	 * as well.
157 	 * since we don't support anything but frag headers,
158 	 * this is what we remove from the payload len.
159 	 */
160 	ip_len = rte_be_to_cpu_16(ip_hdr->payload_len) - sizeof(*frag_hdr);
161 	trim = mb->pkt_len - (ip_len + mb->l3_len + mb->l2_len);
162 
163 	IP_FRAG_LOG(DEBUG, "%s:%d:\n"
164 		"mbuf: %p, tms: %" PRIu64
165 		", key: <" IPv6_KEY_BYTES_FMT ", %#x>, "
166 		"ofs: %u, len: %d, padding: %d, flags: %#x\n"
167 		"tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, "
168 		"max_entries: %u, use_entries: %u\n\n",
169 		__func__, __LINE__,
170 		mb, tms, IPv6_KEY_BYTES(key.src_dst), key.id, ip_ofs, ip_len,
171 		trim, RTE_IPV6_GET_MF(frag_hdr->frag_data),
172 		tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries,
173 		tbl->use_entries);
174 
175 	/* check that fragment length is greater then zero. */
176 	if (ip_len <= 0) {
177 		IP_FRAG_MBUF2DR(dr, mb);
178 		return NULL;
179 	}
180 
181 	if (unlikely(trim > 0))
182 		rte_pktmbuf_trim(mb, trim);
183 
184 	/* try to find/add entry into the fragment's table. */
185 	fp = ip_frag_find(tbl, dr, &key, tms);
186 	if (fp == NULL) {
187 		IP_FRAG_MBUF2DR(dr, mb);
188 		return NULL;
189 	}
190 
191 	IP_FRAG_LOG(DEBUG, "%s:%d:\n"
192 		"tbl: %p, max_entries: %u, use_entries: %u\n"
193 		"ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64
194 		", total_size: %u, frag_size: %u, last_idx: %u\n\n",
195 		__func__, __LINE__,
196 		tbl, tbl->max_entries, tbl->use_entries,
197 		fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id, fp->start,
198 		fp->total_size, fp->frag_size, fp->last_idx);
199 
200 
201 	/* process the fragmented packet. */
202 	mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len,
203 			MORE_FRAGS(frag_hdr->frag_data));
204 	ip_frag_inuse(tbl, fp);
205 
206 	IP_FRAG_LOG(DEBUG, "%s:%d:\n"
207 		"mbuf: %p\n"
208 		"tbl: %p, max_entries: %u, use_entries: %u\n"
209 		"ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64
210 		", total_size: %u, frag_size: %u, last_idx: %u\n\n",
211 		__func__, __LINE__, mb,
212 		tbl, tbl->max_entries, tbl->use_entries,
213 		fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id, fp->start,
214 		fp->total_size, fp->frag_size, fp->last_idx);
215 
216 	return mb;
217 }
218