Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include <stddef.h>
6 : :
7 : : #include <eal_export.h>
8 : : #include <rte_debug.h>
9 : :
10 : : #include "ip_frag_common.h"
11 : :
12 : : /*
13 : : * Reassemble fragments into one packet.
14 : : */
15 : : struct rte_mbuf *
16 : 227 : ipv4_frag_reassemble(struct ip_frag_pkt *fp)
17 : : {
18 : : struct rte_ipv4_hdr *ip_hdr;
19 : : struct rte_mbuf *m, *prev;
20 : : uint32_t i, n, ofs, first_len;
21 : : uint32_t curr_idx = 0;
22 : :
23 : 227 : first_len = fp->frags[IP_FIRST_FRAG_IDX].len;
24 : 227 : n = fp->last_idx - 1;
25 : :
26 : : /*start from the last fragment. */
27 : 227 : m = fp->frags[IP_LAST_FRAG_IDX].mb;
28 : 227 : ofs = fp->frags[IP_LAST_FRAG_IDX].ofs;
29 : : curr_idx = IP_LAST_FRAG_IDX;
30 : :
31 [ + + ]: 654 : while (ofs != first_len) {
32 : :
33 : : prev = m;
34 : :
35 [ + + ]: 1940 : for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) {
36 : :
37 : : /* previous fragment found. */
38 [ + + ]: 1513 : if(fp->frags[i].ofs + fp->frags[i].len == ofs) {
39 : :
40 : : RTE_ASSERT(curr_idx != i);
41 : :
42 : : /* adjust start of the last fragment data. */
43 : : rte_pktmbuf_adj(m,
44 [ + - ]: 681 : (uint16_t)(m->l2_len + m->l3_len));
45 [ + - ]: 681 : rte_pktmbuf_chain(fp->frags[i].mb, m);
46 : :
47 : : /* this mbuf should not be accessed directly */
48 : 681 : fp->frags[curr_idx].mb = NULL;
49 : : curr_idx = i;
50 : :
51 : : /* update our last fragment and offset. */
52 : 681 : m = fp->frags[i].mb;
53 : 681 : ofs = fp->frags[i].ofs;
54 : : }
55 : : }
56 : :
57 : : /* error - hole in the packet. */
58 [ + - ]: 427 : if (m == prev) {
59 : : return NULL;
60 : : }
61 : : }
62 : :
63 : : /* chain with the first fragment. */
64 [ + - ]: 227 : rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
65 [ + - ]: 227 : rte_pktmbuf_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m);
66 : 227 : fp->frags[curr_idx].mb = NULL;
67 : 227 : m = fp->frags[IP_FIRST_FRAG_IDX].mb;
68 : 227 : fp->frags[IP_FIRST_FRAG_IDX].mb = NULL;
69 : :
70 : : /* update ipv4 header for the reassembled packet */
71 : 227 : ip_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *, m->l2_len);
72 : :
73 [ - + ]: 227 : ip_hdr->total_length = rte_cpu_to_be_16((uint16_t)(fp->total_size +
74 : : m->l3_len));
75 : 227 : ip_hdr->fragment_offset = (uint16_t)(ip_hdr->fragment_offset &
76 : : rte_cpu_to_be_16(RTE_IPV4_HDR_DF_FLAG));
77 : 227 : ip_hdr->hdr_checksum = 0;
78 : :
79 : 227 : return m;
80 : : }
81 : :
82 : : /*
83 : : * Process new mbuf with fragment of IPV4 packet.
84 : : * Incoming mbuf should have it's l2_len/l3_len fields setup correctly.
85 : : * @param tbl
86 : : * Table where to lookup/add the fragmented packet.
87 : : * @param mb
88 : : * Incoming mbuf with IPV4 fragment.
89 : : * @param tms
90 : : * Fragment arrival timestamp.
91 : : * @param ip_hdr
92 : : * Pointer to the IPV4 header inside the fragment.
93 : : * @return
94 : : * Pointer to mbuf for reassembled packet, or NULL if:
95 : : * - an error occurred.
96 : : * - not all fragments of the packet are collected yet.
97 : : */
98 : : RTE_EXPORT_SYMBOL(rte_ipv4_frag_reassemble_packet)
99 : : struct rte_mbuf *
100 : 1151 : rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
101 : : struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms,
102 : : struct rte_ipv4_hdr *ip_hdr)
103 : : {
104 : : struct ip_frag_pkt *fp;
105 : : struct ip_frag_key key;
106 : : uint16_t flag_offset, ip_ofs, ip_flag;
107 : : int32_t ip_len;
108 : : int32_t trim;
109 : :
110 [ - + ]: 1151 : flag_offset = rte_be_to_cpu_16(ip_hdr->fragment_offset);
111 : 1151 : ip_ofs = (uint16_t)(flag_offset & RTE_IPV4_HDR_OFFSET_MASK);
112 : 1151 : ip_flag = (uint16_t)(flag_offset & RTE_IPV4_HDR_MF_FLAG);
113 : :
114 : : /*
115 : : * RFC 791 requires using: source, destination, identifier field and protocol
116 : : */
117 : :
118 : : /* use first 8 bytes only */
119 : : memcpy(&key.src_dst[0], &ip_hdr->src_addr, 8);
120 : :
121 : : /* packet_id is 16 bits and proto id is 8 bits */
122 : 1151 : key.id = ((uint32_t) ip_hdr->next_proto_id << 16) | ip_hdr->packet_id;
123 : 1151 : key.key_len = IPV4_KEYLEN;
124 : :
125 : 1151 : ip_ofs *= RTE_IPV4_HDR_OFFSET_UNITS;
126 [ - + ]: 2302 : ip_len = rte_be_to_cpu_16(ip_hdr->total_length) - mb->l3_len;
127 : 1151 : trim = mb->pkt_len - (ip_len + mb->l3_len + mb->l2_len);
128 : :
129 : : IP_FRAG_LOG(DEBUG, "%s:%d:\n"
130 : : "mbuf: %p, tms: %" PRIu64 ", key: <%" PRIx64 ", %#x>"
131 : : "ofs: %u, len: %d, padding: %d, flags: %#x\n"
132 : : "tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, "
133 : : "max_entries: %u, use_entries: %u\n\n",
134 : : __func__, __LINE__,
135 : : mb, tms, key.src_dst[0], key.id, ip_ofs, ip_len, trim, ip_flag,
136 : : tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries,
137 : : tbl->use_entries);
138 : :
139 : : /*
140 : : * Drop fragments with no payload, and any fragment whose end would
141 : : * make the reassembled datagram exceed the maximum IPv4 size. The
142 : : * total_length field is 16 bits, so otherwise it is silently
143 : : * truncated while the mbuf still holds the full length.
144 : : */
145 [ + + + + ]: 1151 : if (ip_len <= 0 || ip_ofs + ip_len + mb->l3_len > UINT16_MAX) {
146 : 2 : IP_FRAG_MBUF2DR(dr, mb);
147 : 2 : return NULL;
148 : : }
149 : :
150 [ - + ]: 1149 : if (unlikely(trim > 0))
151 : 0 : rte_pktmbuf_trim(mb, trim);
152 : :
153 : : /* try to find/add entry into the fragment's table. */
154 [ - + ]: 1149 : if ((fp = ip_frag_find(tbl, dr, &key, tms)) == NULL) {
155 : 0 : IP_FRAG_MBUF2DR(dr, mb);
156 : 0 : return NULL;
157 : : }
158 : :
159 : : IP_FRAG_LOG(DEBUG, "%s:%d:\n"
160 : : "tbl: %p, max_entries: %u, use_entries: %u\n"
161 : : "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64
162 : : ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
163 : : __func__, __LINE__,
164 : : tbl, tbl->max_entries, tbl->use_entries,
165 : : fp, fp->key.src_dst[0], fp->key.id, fp->start,
166 : : fp->total_size, fp->frag_size, fp->last_idx);
167 : :
168 : :
169 : : /* process the fragmented packet. */
170 : 1149 : mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len, ip_flag);
171 : : ip_frag_inuse(tbl, fp);
172 : :
173 : : IP_FRAG_LOG(DEBUG, "%s:%d:\n"
174 : : "mbuf: %p\n"
175 : : "tbl: %p, max_entries: %u, use_entries: %u\n"
176 : : "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64
177 : : ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
178 : : __func__, __LINE__, mb,
179 : : tbl, tbl->max_entries, tbl->use_entries,
180 : : fp, fp->key.src_dst[0], fp->key.id, fp->start,
181 : : fp->total_size, fp->frag_size, fp->last_idx);
182 : :
183 : : return mb;
184 : : }
|