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