Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include <stddef.h>
6 : :
7 : : #include <eal_export.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 [ + + ]: 9225 : for (i = len - 1; i >= 0; i--)
25 : 9000 : dst[i] = src[i];
26 : : }
27 : :
28 : : /*
29 : : * Reassemble fragments into one packet.
30 : : */
31 : : struct rte_mbuf *
32 : 225 : 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 : 225 : first_len = fp->frags[IP_FIRST_FRAG_IDX].len;
42 : 225 : n = fp->last_idx - 1;
43 : :
44 : : /*start from the last fragment. */
45 : 225 : m = fp->frags[IP_LAST_FRAG_IDX].mb;
46 : 225 : ofs = fp->frags[IP_LAST_FRAG_IDX].ofs;
47 : 225 : last_len = fp->frags[IP_LAST_FRAG_IDX].len;
48 : : curr_idx = IP_LAST_FRAG_IDX;
49 : :
50 : 225 : payload_len = ofs + last_len;
51 : :
52 [ + + ]: 650 : while (ofs != first_len) {
53 : :
54 : : prev = m;
55 : :
56 [ + + ]: 1930 : for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) {
57 : :
58 : : /* previous fragment found. */
59 [ + + ]: 1505 : 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 [ + - ]: 673 : (uint16_t)(m->l2_len + m->l3_len));
66 [ + - ]: 673 : rte_pktmbuf_chain(fp->frags[i].mb, m);
67 : :
68 : : /* this mbuf should not be accessed directly */
69 : 673 : fp->frags[curr_idx].mb = NULL;
70 : : curr_idx = i;
71 : :
72 : : /* update our last fragment and offset. */
73 : 673 : m = fp->frags[i].mb;
74 : 673 : ofs = fp->frags[i].ofs;
75 : : }
76 : : }
77 : :
78 : : /* error - hole in the packet. */
79 [ + - ]: 425 : if (m == prev) {
80 : : return NULL;
81 : : }
82 : : }
83 : :
84 : : /* chain with the first fragment. */
85 [ + - ]: 225 : rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
86 [ + - ]: 225 : rte_pktmbuf_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m);
87 : 225 : fp->frags[curr_idx].mb = NULL;
88 : 225 : m = fp->frags[IP_FIRST_FRAG_IDX].mb;
89 : 225 : fp->frags[IP_FIRST_FRAG_IDX].mb = NULL;
90 : :
91 : : /* update ipv6 header for the reassembled datagram */
92 : 225 : ip_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *, m->l2_len);
93 : :
94 [ - + ]: 225 : 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 : 225 : move_len = m->l2_len + m->l3_len - sizeof(*frag_hdr);
104 : : frag_hdr = (struct rte_ipv6_fragment_ext *) (ip_hdr + 1);
105 : 225 : ip_hdr->proto = frag_hdr->next_header;
106 : :
107 : 225 : 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 : : RTE_EXPORT_SYMBOL(rte_ipv6_frag_reassemble_packet)
136 : : struct rte_mbuf *
137 : 1131 : rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
138 : : struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms,
139 : : struct rte_ipv6_hdr *ip_hdr, struct rte_ipv6_fragment_ext *frag_hdr)
140 : : {
141 : : struct ip_frag_pkt *fp;
142 : : struct ip_frag_key key;
143 : : uint16_t ip_ofs;
144 : : int32_t ip_len;
145 : : int32_t trim;
146 : :
147 : : memcpy(&key.src_dst[0], &ip_hdr->src_addr, 16);
148 : : memcpy(&key.src_dst[2], &ip_hdr->dst_addr, 16);
149 : :
150 : 1131 : key.id = frag_hdr->id;
151 : 1131 : key.key_len = IPV6_KEYLEN;
152 : :
153 [ - + ]: 2262 : ip_ofs = FRAG_OFFSET(frag_hdr->frag_data) * 8;
154 : :
155 : : /*
156 : : * as per RFC2460, payload length contains all extension headers
157 : : * as well.
158 : : * since we don't support anything but frag headers,
159 : : * this is what we remove from the payload len.
160 : : */
161 [ - + ]: 2262 : ip_len = rte_be_to_cpu_16(ip_hdr->payload_len) - sizeof(*frag_hdr);
162 : 1131 : trim = mb->pkt_len - (ip_len + mb->l3_len + mb->l2_len);
163 : :
164 : : IP_FRAG_LOG(DEBUG, "%s:%d:\n"
165 : : "mbuf: %p, tms: %" PRIu64
166 : : ", key: <" IPv6_KEY_BYTES_FMT ", %#x>, "
167 : : "ofs: %u, len: %d, padding: %d, flags: %#x\n"
168 : : "tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, "
169 : : "max_entries: %u, use_entries: %u\n\n",
170 : : __func__, __LINE__,
171 : : mb, tms, IPv6_KEY_BYTES(key.src_dst), key.id, ip_ofs, ip_len,
172 : : trim, RTE_IPV6_GET_MF(frag_hdr->frag_data),
173 : : tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries,
174 : : tbl->use_entries);
175 : :
176 : : /*
177 : : * Drop fragments with no payload, and any fragment whose end would
178 : : * make the reassembled payload exceed 65535 bytes. The payload_len
179 : : * field is 16 bits, so otherwise it is silently truncated while the
180 : : * mbuf still holds the full length.
181 : : */
182 [ + - + + ]: 1131 : if (ip_len <= 0 || ip_ofs + ip_len > UINT16_MAX) {
183 : 1 : IP_FRAG_MBUF2DR(dr, mb);
184 : 1 : return NULL;
185 : : }
186 : :
187 : : /*
188 : : * Only a fragment header directly following the IPv6 header is supported.
189 : : * Per-fragment (unfragmentable) extension headers placed
190 : : * before the fragment header are not handled: ipv6_frag_reassemble()
191 : : * patches the IPv6 header's next-header field and removes the fragment
192 : : * header assuming it sits immediately after the IPv6 header, so such a
193 : : * fragment would be reassembled into a corrupt datagram. Drop it.
194 : : *
195 : : * Extension headers after the fragment header (destination options,
196 : : * AH, ESP, upper-layer) are part of the fragmentable payload and are
197 : : * reassembled as opaque bytes, so they are not affected. The test uses
198 : : * the fragment header's position rather than l3_len so that callers
199 : : * which include later headers in l3_len are not rejected.
200 : : */
201 [ + + ]: 1130 : if ((uintptr_t)frag_hdr != (uintptr_t)(ip_hdr + 1)) {
202 : : IP_FRAG_LOG(DEBUG,
203 : : "%s:%d: drop fragment with header before frag header, offset %zu\n",
204 : : __func__, __LINE__, (uintptr_t)frag_hdr - (uintptr_t)ip_hdr);
205 : 1 : IP_FRAG_MBUF2DR(dr, mb);
206 : 1 : return NULL;
207 : : }
208 : :
209 [ - + ]: 1129 : if (unlikely(trim > 0))
210 : 0 : rte_pktmbuf_trim(mb, trim);
211 : :
212 : : /* try to find/add entry into the fragment's table. */
213 : 1129 : fp = ip_frag_find(tbl, dr, &key, tms);
214 [ - + ]: 1129 : if (fp == NULL) {
215 : 0 : IP_FRAG_MBUF2DR(dr, mb);
216 : 0 : return NULL;
217 : : }
218 : :
219 : : IP_FRAG_LOG(DEBUG, "%s:%d:\n"
220 : : "tbl: %p, max_entries: %u, use_entries: %u\n"
221 : : "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64
222 : : ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
223 : : __func__, __LINE__,
224 : : tbl, tbl->max_entries, tbl->use_entries,
225 : : fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id, fp->start,
226 : : fp->total_size, fp->frag_size, fp->last_idx);
227 : :
228 : :
229 : : /* process the fragmented packet. */
230 : 1129 : mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len,
231 : 1129 : MORE_FRAGS(frag_hdr->frag_data));
232 : : ip_frag_inuse(tbl, fp);
233 : :
234 : : IP_FRAG_LOG(DEBUG, "%s:%d:\n"
235 : : "mbuf: %p\n"
236 : : "tbl: %p, max_entries: %u, use_entries: %u\n"
237 : : "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64
238 : : ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
239 : : __func__, __LINE__, mb,
240 : : tbl, tbl->max_entries, tbl->use_entries,
241 : : fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id, fp->start,
242 : : fp->total_size, fp->frag_size, fp->last_idx);
243 : :
244 : : return mb;
245 : : }
|