4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <rte_memcpy.h>
38 #include "ip_frag_common.h"
44 * Implementation of IPv6 reassembly.
49 ip_frag_memmove(char *dst, char *src, int len)
53 /* go backwards to make sure we don't overwrite anything important */
54 for (i = len - 1; i >= 0; i--)
59 * Reassemble fragments into one packet.
62 ipv6_frag_reassemble(struct ip_frag_pkt *fp)
64 struct ipv6_hdr *ip_hdr;
65 struct ipv6_extension_fragment *frag_hdr;
66 struct rte_mbuf *m, *prev;
67 uint32_t i, n, ofs, first_len;
68 uint32_t last_len, move_len, payload_len;
69 uint32_t curr_idx = 0;
71 first_len = fp->frags[IP_FIRST_FRAG_IDX].len;
74 /*start from the last fragment. */
75 m = fp->frags[IP_LAST_FRAG_IDX].mb;
76 ofs = fp->frags[IP_LAST_FRAG_IDX].ofs;
77 last_len = fp->frags[IP_LAST_FRAG_IDX].len;
78 curr_idx = IP_LAST_FRAG_IDX;
80 payload_len = ofs + last_len;
82 while (ofs != first_len) {
86 for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) {
88 /* previous fragment found. */
89 if (fp->frags[i].ofs + fp->frags[i].len == ofs) {
91 /* adjust start of the last fragment data. */
92 rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
93 rte_pktmbuf_chain(fp->frags[i].mb, m);
95 /* this mbuf should not be accessed directly */
96 fp->frags[curr_idx].mb = NULL;
99 /* update our last fragment and offset. */
101 ofs = fp->frags[i].ofs;
105 /* error - hole in the packet. */
111 /* chain with the first fragment. */
112 rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
113 rte_pktmbuf_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m);
114 m = fp->frags[IP_FIRST_FRAG_IDX].mb;
116 /* update mbuf fields for reassembled packet. */
117 m->ol_flags |= PKT_TX_IP_CKSUM;
119 /* update ipv6 header for the reassembled datagram */
120 ip_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *, m->l2_len);
122 ip_hdr->payload_len = rte_cpu_to_be_16(payload_len);
125 * remove fragmentation header. note that per RFC2460, we need to update
126 * the last non-fragmentable header with the "next header" field to contain
127 * type of the first fragmentable header, but we currently don't support
128 * other headers, so we assume there are no other headers and thus update
129 * the main IPv6 header instead.
131 move_len = m->l2_len + m->l3_len - sizeof(*frag_hdr);
132 frag_hdr = (struct ipv6_extension_fragment *) (ip_hdr + 1);
133 ip_hdr->proto = frag_hdr->next_header;
135 ip_frag_memmove(rte_pktmbuf_mtod_offset(m, char *, sizeof(*frag_hdr)),
136 rte_pktmbuf_mtod(m, char*), move_len);
138 rte_pktmbuf_adj(m, sizeof(*frag_hdr));
144 * Process new mbuf with fragment of IPV6 datagram.
145 * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
147 * Table where to lookup/add the fragmented packet.
149 * Incoming mbuf with IPV6 fragment.
151 * Fragment arrival timestamp.
153 * Pointer to the IPV6 header.
155 * Pointer to the IPV6 fragment extension header.
157 * Pointer to mbuf for reassembled packet, or NULL if:
158 * - an error occurred.
159 * - not all fragments of the packet are collected yet.
161 #define MORE_FRAGS(x) (((x) & 0x100) >> 8)
162 #define FRAG_OFFSET(x) (rte_cpu_to_be_16(x) >> 3)
164 rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
165 struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms,
166 struct ipv6_hdr *ip_hdr, struct ipv6_extension_fragment *frag_hdr)
168 struct ip_frag_pkt *fp;
169 struct ip_frag_key key;
170 uint16_t ip_len, ip_ofs;
172 rte_memcpy(&key.src_dst[0], ip_hdr->src_addr, 16);
173 rte_memcpy(&key.src_dst[2], ip_hdr->dst_addr, 16);
175 key.id = frag_hdr->id;
176 key.key_len = IPV6_KEYLEN;
178 ip_ofs = FRAG_OFFSET(frag_hdr->frag_data) * 8;
181 * as per RFC2460, payload length contains all extension headers as well.
182 * since we don't support anything but frag headers, this is what we remove
183 * from the payload len.
185 ip_len = rte_be_to_cpu_16(ip_hdr->payload_len) - sizeof(*frag_hdr);
187 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
188 "mbuf: %p, tms: %" PRIu64
189 ", key: <" IPv6_KEY_BYTES_FMT ", %#x>, ofs: %u, len: %u, flags: %#x\n"
190 "tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, "
191 "max_entries: %u, use_entries: %u\n\n",
193 mb, tms, IPv6_KEY_BYTES(key.src_dst), key.id, ip_ofs, ip_len,
194 RTE_IPV6_GET_MF(frag_hdr->frag_data),
195 tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries,
198 /* try to find/add entry into the fragment's table. */
199 fp = ip_frag_find(tbl, dr, &key, tms);
201 IP_FRAG_MBUF2DR(dr, mb);
205 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
206 "tbl: %p, max_entries: %u, use_entries: %u\n"
207 "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64
208 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
210 tbl, tbl->max_entries, tbl->use_entries,
211 fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id, fp->start,
212 fp->total_size, fp->frag_size, fp->last_idx);
215 /* process the fragmented packet. */
216 mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len,
217 MORE_FRAGS(frag_hdr->frag_data));
218 ip_frag_inuse(tbl, fp);
220 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
222 "tbl: %p, max_entries: %u, use_entries: %u\n"
223 "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64
224 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
225 __func__, __LINE__, mb,
226 tbl, tbl->max_entries, tbl->use_entries,
227 fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id, fp->start,
228 fp->total_size, fp->frag_size, fp->last_idx);