Imported Upstream version 16.04
[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(const 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
70         first_len = fp->frags[IP_FIRST_FRAG_IDX].len;
71         n = fp->last_idx - 1;
72
73         /*start from the last fragment. */
74         m = fp->frags[IP_LAST_FRAG_IDX].mb;
75         ofs = fp->frags[IP_LAST_FRAG_IDX].ofs;
76         last_len = fp->frags[IP_LAST_FRAG_IDX].len;
77
78         payload_len = ofs + last_len;
79
80         while (ofs != first_len) {
81
82                 prev = m;
83
84                 for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) {
85
86                         /* previous fragment found. */
87                         if (fp->frags[i].ofs + fp->frags[i].len == ofs) {
88
89                                 /* adjust start of the last fragment data. */
90                                 rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
91                                 rte_pktmbuf_chain(fp->frags[i].mb, m);
92
93                                 /* update our last fragment and offset. */
94                                 m = fp->frags[i].mb;
95                                 ofs = fp->frags[i].ofs;
96                         }
97                 }
98
99                 /* error - hole in the packet. */
100                 if (m == prev) {
101                         return NULL;
102                 }
103         }
104
105         /* chain with the first fragment. */
106         rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
107         rte_pktmbuf_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m);
108         m = fp->frags[IP_FIRST_FRAG_IDX].mb;
109
110         /* update mbuf fields for reassembled packet. */
111         m->ol_flags |= PKT_TX_IP_CKSUM;
112
113         /* update ipv6 header for the reassembled datagram */
114         ip_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *, m->l2_len);
115
116         ip_hdr->payload_len = rte_cpu_to_be_16(payload_len);
117
118         /*
119          * remove fragmentation header. note that per RFC2460, we need to update
120          * the last non-fragmentable header with the "next header" field to contain
121          * type of the first fragmentable header, but we currently don't support
122          * other headers, so we assume there are no other headers and thus update
123          * the main IPv6 header instead.
124          */
125         move_len = m->l2_len + m->l3_len - sizeof(*frag_hdr);
126         frag_hdr = (struct ipv6_extension_fragment *) (ip_hdr + 1);
127         ip_hdr->proto = frag_hdr->next_header;
128
129         ip_frag_memmove(rte_pktmbuf_mtod_offset(m, char *, sizeof(*frag_hdr)),
130                         rte_pktmbuf_mtod(m, char*), move_len);
131
132         rte_pktmbuf_adj(m, sizeof(*frag_hdr));
133
134         return m;
135 }
136
137 /*
138  * Process new mbuf with fragment of IPV6 datagram.
139  * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
140  * @param tbl
141  *   Table where to lookup/add the fragmented packet.
142  * @param mb
143  *   Incoming mbuf with IPV6 fragment.
144  * @param tms
145  *   Fragment arrival timestamp.
146  * @param ip_hdr
147  *   Pointer to the IPV6 header.
148  * @param frag_hdr
149  *   Pointer to the IPV6 fragment extension header.
150  * @return
151  *   Pointer to mbuf for reassembled packet, or NULL if:
152  *   - an error occured.
153  *   - not all fragments of the packet are collected yet.
154  */
155 #define MORE_FRAGS(x) (((x) & 0x100) >> 8)
156 #define FRAG_OFFSET(x) (rte_cpu_to_be_16(x) >> 3)
157 struct rte_mbuf *
158 rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
159                 struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms,
160                 struct ipv6_hdr *ip_hdr, struct ipv6_extension_fragment *frag_hdr)
161 {
162         struct ip_frag_pkt *fp;
163         struct ip_frag_key key;
164         uint16_t ip_len, ip_ofs;
165
166         rte_memcpy(&key.src_dst[0], ip_hdr->src_addr, 16);
167         rte_memcpy(&key.src_dst[2], ip_hdr->dst_addr, 16);
168
169         key.id = frag_hdr->id;
170         key.key_len = IPV6_KEYLEN;
171
172         ip_ofs = FRAG_OFFSET(frag_hdr->frag_data) * 8;
173
174         /*
175          * as per RFC2460, payload length contains all extension headers as well.
176          * since we don't support anything but frag headers, this is what we remove
177          * from the payload len.
178          */
179         ip_len = rte_be_to_cpu_16(ip_hdr->payload_len) - sizeof(*frag_hdr);
180
181         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
182                 "mbuf: %p, tms: %" PRIu64
183                 ", key: <" IPv6_KEY_BYTES_FMT ", %#x>, ofs: %u, len: %u, flags: %#x\n"
184                 "tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, "
185                 "max_entries: %u, use_entries: %u\n\n",
186                 __func__, __LINE__,
187                 mb, tms, IPv6_KEY_BYTES(key.src_dst), key.id, ip_ofs, ip_len,
188                 RTE_IPV6_GET_MF(frag_hdr->frag_data),
189                 tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries,
190                 tbl->use_entries);
191
192         /* try to find/add entry into the fragment's table. */
193         fp = ip_frag_find(tbl, dr, &key, tms);
194         if (fp == NULL) {
195                 IP_FRAG_MBUF2DR(dr, mb);
196                 return NULL;
197         }
198
199         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
200                 "tbl: %p, max_entries: %u, use_entries: %u\n"
201                 "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64
202                 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
203                 __func__, __LINE__,
204                 tbl, tbl->max_entries, tbl->use_entries,
205                 fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id, fp->start,
206                 fp->total_size, fp->frag_size, fp->last_idx);
207
208
209         /* process the fragmented packet. */
210         mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len,
211                         MORE_FRAGS(frag_hdr->frag_data));
212         ip_frag_inuse(tbl, fp);
213
214         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
215                 "mbuf: %p\n"
216                 "tbl: %p, max_entries: %u, use_entries: %u\n"
217                 "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64
218                 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
219                 __func__, __LINE__, mb,
220                 tbl, tbl->max_entries, tbl->use_entries,
221                 fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id, fp->start,
222                 fp->total_size, fp->frag_size, fp->last_idx);
223
224         return mb;
225 }