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