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