blob: 6efb8834c4b642bea1b172ea45e8d5a59f2f34cf [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*---------------------------------------------------------------------------
2 * Copyright (c) 2009-2014 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *---------------------------------------------------------------------------
15 */
16/*
17 * IPv4 Fragmentation Node
18 *
19 *
20 */
21
22#include "ip_frag.h"
23
24#include <vnet/ip/ip.h>
25
26
Dave Barachd7cb1b52016-12-09 09:52:16 -050027typedef struct
28{
Ed Warnickecb9cada2015-12-08 15:45:58 -070029 u8 ipv6;
Ed Warnickecb9cada2015-12-08 15:45:58 -070030 u16 mtu;
31 u8 next;
32 u16 n_fragments;
33} ip_frag_trace_t;
34
Dave Barachd7cb1b52016-12-09 09:52:16 -050035static u8 *
36format_ip_frag_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070037{
38 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
39 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -050040 ip_frag_trace_t *t = va_arg (*args, ip_frag_trace_t *);
Ole Troan282093f2018-09-19 12:38:51 +020041 s = format (s, "IPv%s mtu: %u fragments: %u",
42 t->ipv6 ? "6" : "4", t->mtu, t->n_fragments);
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 return s;
44}
45
46static u32 running_fragment_id;
47
Ole Troan282093f2018-09-19 12:38:51 +020048static void
49frag_set_sw_if_index (vlib_buffer_t * to, vlib_buffer_t * from)
50{
51 vnet_buffer (to)->sw_if_index[VLIB_RX] =
52 vnet_buffer (from)->sw_if_index[VLIB_RX];
53 vnet_buffer (to)->sw_if_index[VLIB_TX] =
54 vnet_buffer (from)->sw_if_index[VLIB_TX];
55
56 /* Copy adj_index in case DPO based node is sending for the
57 * fragmentation, the packet would be sent back to the proper
58 * DPO next node and Index
59 */
60 vnet_buffer (to)->ip.adj_index[VLIB_RX] =
61 vnet_buffer (from)->ip.adj_index[VLIB_RX];
62 vnet_buffer (to)->ip.adj_index[VLIB_TX] =
63 vnet_buffer (from)->ip.adj_index[VLIB_TX];
64}
65
66static vlib_buffer_t *
67frag_buffer_alloc (vlib_buffer_t * org_b, u32 * bi)
68{
69 vlib_main_t *vm = vlib_get_main ();
70 if (vlib_buffer_alloc (vm, bi, 1) != 1)
71 return 0;
72
73 vlib_buffer_t *b = vlib_get_buffer (vm, *bi);
74 vlib_buffer_free_list_t *fl =
75 vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
76 vlib_buffer_init_for_free_list (b, fl);
77 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
78 vlib_buffer_copy_trace_flag (vm, org_b, *bi);
79
80 return b;
81}
82
Ole Troan4146c652018-08-08 22:23:19 +020083/*
84 * Limitation: Does follow buffer chains in the packet to fragment,
85 * but does not generate buffer chains. I.e. a fragment is always
86 * contained with in a single buffer and limited to the max buffer
87 * size.
88 */
Vijayabhaskar Katamreddye95d1a12018-06-28 11:08:29 -070089void
Ole Troan4146c652018-08-08 22:23:19 +020090ip4_frag_do_fragment (vlib_main_t * vm, u32 from_bi, u32 ** buffer,
Dave Barachd7cb1b52016-12-09 09:52:16 -050091 ip_frag_error_t * error)
Ed Warnickecb9cada2015-12-08 15:45:58 -070092{
Ole Troan4146c652018-08-08 22:23:19 +020093 vlib_buffer_t *from_b;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 ip4_header_t *ip4;
Ole Troan282093f2018-09-19 12:38:51 +020095 u16 mtu, len, max, rem, ip_frag_id, ip_frag_offset;
Ole Troan4146c652018-08-08 22:23:19 +020096 u8 *org_from_packet, more;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Ole Troan4146c652018-08-08 22:23:19 +020098 from_b = vlib_get_buffer (vm, from_bi);
Ole Troan4146c652018-08-08 22:23:19 +020099 mtu = vnet_buffer (from_b)->ip_frag.mtu;
100 org_from_packet = vlib_buffer_get_current (from_b);
Ole Troan282093f2018-09-19 12:38:51 +0200101 ip4 = (ip4_header_t *) vlib_buffer_get_current (from_b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Ole Troan4146c652018-08-08 22:23:19 +0200103 rem = clib_net_to_host_u16 (ip4->length) - sizeof (ip4_header_t);
Juraj Sloboda68b7cb82018-10-16 12:18:21 +0200104 max =
105 (clib_min (mtu, VLIB_BUFFER_DATA_SIZE) - sizeof (ip4_header_t)) & ~0x7;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
Ole Troan4146c652018-08-08 22:23:19 +0200107 if (rem >
Ole Troan282093f2018-09-19 12:38:51 +0200108 (vlib_buffer_length_in_chain (vm, from_b) - sizeof (ip4_header_t)))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500109 {
110 *error = IP_FRAG_ERROR_MALFORMED;
111 return;
112 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
Ole Troan4146c652018-08-08 22:23:19 +0200114 if (mtu < sizeof (ip4_header_t))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500115 {
116 *error = IP_FRAG_ERROR_CANT_FRAGMENT_HEADER;
117 return;
118 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119
120 if (ip4->flags_and_fragment_offset &
Dave Barachd7cb1b52016-12-09 09:52:16 -0500121 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT))
122 {
123 *error = IP_FRAG_ERROR_DONT_FRAGMENT_SET;
124 return;
125 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
Dave Barachd7cb1b52016-12-09 09:52:16 -0500127 if (ip4_is_fragment (ip4))
128 {
129 ip_frag_id = ip4->fragment_id;
130 ip_frag_offset = ip4_get_fragment_offset (ip4);
131 more =
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800132 !(!(ip4->flags_and_fragment_offset &
133 clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS)));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500134 }
135 else
136 {
137 ip_frag_id = (++running_fragment_id);
138 ip_frag_offset = 0;
139 more = 0;
140 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
Ole Troan4146c652018-08-08 22:23:19 +0200142 u8 *from_data = (void *) (ip4 + 1);
143 vlib_buffer_t *org_from_b = from_b;
Ole Troan282093f2018-09-19 12:38:51 +0200144 u16 fo = 0;
145 u16 left_in_from_buffer = from_b->current_length - sizeof (ip4_header_t);
146 u16 ptr = 0;
Ole Troan4146c652018-08-08 22:23:19 +0200147
148 /* Do the actual fragmentation */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500149 while (rem)
150 {
Ole Troan4146c652018-08-08 22:23:19 +0200151 u32 to_bi;
152 vlib_buffer_t *to_b;
153 ip4_header_t *to_ip4;
154 u8 *to_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
Juraj Sloboda68b7cb82018-10-16 12:18:21 +0200156 len = (rem > max ? max : rem);
Ole Troan4146c652018-08-08 22:23:19 +0200157 if (len != rem) /* Last fragment does not need to divisible by 8 */
158 len &= ~0x7;
Ole Troan282093f2018-09-19 12:38:51 +0200159 if ((to_b = frag_buffer_alloc (org_from_b, &to_bi)) == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500160 {
Ole Troan4146c652018-08-08 22:23:19 +0200161 *error = IP_FRAG_ERROR_MEMORY;
Ole Troan4146c652018-08-08 22:23:19 +0200162 return;
163 }
164 vec_add1 (*buffer, to_bi);
Ole Troan282093f2018-09-19 12:38:51 +0200165 frag_set_sw_if_index (to_b, org_from_b);
Ole Troan4146c652018-08-08 22:23:19 +0200166
Ole Troan282093f2018-09-19 12:38:51 +0200167 /* Copy ip4 header */
Dave Barach178cf492018-11-13 16:34:13 -0500168 clib_memcpy_fast (to_b->data, org_from_packet, sizeof (ip4_header_t));
Ole Troan282093f2018-09-19 12:38:51 +0200169 to_ip4 = vlib_buffer_get_current (to_b);
Ole Troan4146c652018-08-08 22:23:19 +0200170 to_data = (void *) (to_ip4 + 1);
171
Ole Troan7eb9d962018-08-10 14:39:48 +0200172 /* Spin through from buffers filling up the to buffer */
Ole Troan282093f2018-09-19 12:38:51 +0200173 u16 left_in_to_buffer = len, to_ptr = 0;
Ole Troan7eb9d962018-08-10 14:39:48 +0200174 while (1)
Ole Troan4146c652018-08-08 22:23:19 +0200175 {
Ole Troan282093f2018-09-19 12:38:51 +0200176 u16 bytes_to_copy;
177
Ole Troan7eb9d962018-08-10 14:39:48 +0200178 /* Figure out how many bytes we can safely copy */
179 bytes_to_copy = left_in_to_buffer <= left_in_from_buffer ?
180 left_in_to_buffer : left_in_from_buffer;
Dave Barach178cf492018-11-13 16:34:13 -0500181 clib_memcpy_fast (to_data + to_ptr, from_data + ptr, bytes_to_copy);
Ole Troan7eb9d962018-08-10 14:39:48 +0200182 left_in_to_buffer -= bytes_to_copy;
183 ptr += bytes_to_copy;
184 left_in_from_buffer -= bytes_to_copy;
185 if (left_in_to_buffer == 0)
186 break;
Ole Troan4146c652018-08-08 22:23:19 +0200187
Ole Troan282093f2018-09-19 12:38:51 +0200188 ASSERT (left_in_from_buffer <= 0);
Ole Troan4146c652018-08-08 22:23:19 +0200189 /* Move buffer */
190 if (!(from_b->flags & VLIB_BUFFER_NEXT_PRESENT))
191 {
192 *error = IP_FRAG_ERROR_MALFORMED;
193 return;
194 }
195 from_b = vlib_get_buffer (vm, from_b->next_buffer);
196 from_data = (u8 *) vlib_buffer_get_current (from_b);
Ole Troan7eb9d962018-08-10 14:39:48 +0200197 ptr = 0;
198 left_in_from_buffer = from_b->current_length;
199 to_ptr += bytes_to_copy;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500200 }
Ole Troan7eb9d962018-08-10 14:39:48 +0200201
Ole Troan282093f2018-09-19 12:38:51 +0200202 to_b->current_length = len + sizeof (ip4_header_t);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500203
Ole Troan4146c652018-08-08 22:23:19 +0200204 to_ip4->fragment_id = ip_frag_id;
205 to_ip4->flags_and_fragment_offset =
206 clib_host_to_net_u16 ((fo >> 3) + ip_frag_offset);
207 to_ip4->flags_and_fragment_offset |=
Dave Barachd7cb1b52016-12-09 09:52:16 -0500208 clib_host_to_net_u16 (((len != rem) || more) << 13);
Ole Troan4146c652018-08-08 22:23:19 +0200209 to_ip4->length = clib_host_to_net_u16 (len + sizeof (ip4_header_t));
210 to_ip4->checksum = ip4_header_checksum (to_ip4);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500211
Ole Troan4146c652018-08-08 22:23:19 +0200212 if (vnet_buffer (org_from_b)->ip_frag.flags & IP_FRAG_FLAG_IP4_HEADER)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500213 {
Ole Troan4146c652018-08-08 22:23:19 +0200214 /* Encapsulating ipv4 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500215 ip4_header_t *encap_header4 =
Ole Troan4146c652018-08-08 22:23:19 +0200216 (ip4_header_t *) vlib_buffer_get_current (to_b);
217 encap_header4->length = clib_host_to_net_u16 (to_b->current_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500218 encap_header4->checksum = ip4_header_checksum (encap_header4);
219 }
Ole Troan4146c652018-08-08 22:23:19 +0200220 else if (vnet_buffer (org_from_b)->
221 ip_frag.flags & IP_FRAG_FLAG_IP6_HEADER)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500222 {
Ole Troan4146c652018-08-08 22:23:19 +0200223 /* Encapsulating ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500224 ip6_header_t *encap_header6 =
Ole Troan4146c652018-08-08 22:23:19 +0200225 (ip6_header_t *) vlib_buffer_get_current (to_b);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500226 encap_header6->payload_length =
Ole Troan4146c652018-08-08 22:23:19 +0200227 clib_host_to_net_u16 (to_b->current_length -
Dave Barachd7cb1b52016-12-09 09:52:16 -0500228 sizeof (*encap_header6));
229 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500230 rem -= len;
Ole Troan4146c652018-08-08 22:23:19 +0200231 fo += len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233}
234
Ole Troan9fb87552016-01-13 22:30:43 +0100235void
Ole Troan282093f2018-09-19 12:38:51 +0200236ip_frag_set_vnet_buffer (vlib_buffer_t * b, u16 mtu, u8 next_index, u8 flags)
Ole Troan9fb87552016-01-13 22:30:43 +0100237{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500238 vnet_buffer (b)->ip_frag.mtu = mtu;
239 vnet_buffer (b)->ip_frag.next_index = next_index;
240 vnet_buffer (b)->ip_frag.flags = flags;
Ole Troan9fb87552016-01-13 22:30:43 +0100241}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Ole Troan282093f2018-09-19 12:38:51 +0200243
244static inline uword
245frag_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
246 vlib_frame_t * frame, u32 node_index, bool is_ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247{
248 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Ole Troan282093f2018-09-19 12:38:51 +0200249 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250 from = vlib_frame_vector_args (frame);
251 n_left_from = frame->n_vectors;
252 next_index = node->cached_next_index;
253 u32 frag_sent = 0, small_packets = 0;
254 u32 *buffer = 0;
255
Dave Barachd7cb1b52016-12-09 09:52:16 -0500256 while (n_left_from > 0)
257 {
258 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
Dave Barachd7cb1b52016-12-09 09:52:16 -0500260 while (n_left_from > 0 && n_left_to_next > 0)
261 {
262 u32 pi0, *frag_from, frag_left;
263 vlib_buffer_t *p0;
264 ip_frag_error_t error0;
Ole Troan282093f2018-09-19 12:38:51 +0200265 int next0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266
Ole Troan282093f2018-09-19 12:38:51 +0200267 /*
268 * Note: The packet is not enqueued now. It is instead put
269 * in a vector where other fragments will be put as well.
270 */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500271 pi0 = from[0];
272 from += 1;
273 n_left_from -= 1;
274 error0 = IP_FRAG_ERROR_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275
Dave Barachd7cb1b52016-12-09 09:52:16 -0500276 p0 = vlib_get_buffer (vm, pi0);
Ole Troan282093f2018-09-19 12:38:51 +0200277 if (is_ip6)
278 ip6_frag_do_fragment (vm, pi0, &buffer, &error0);
279 else
280 ip4_frag_do_fragment (vm, pi0, &buffer, &error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
Dave Barachd7cb1b52016-12-09 09:52:16 -0500282 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
283 {
284 ip_frag_trace_t *tr =
285 vlib_add_trace (vm, node, p0, sizeof (*tr));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500286 tr->mtu = vnet_buffer (p0)->ip_frag.mtu;
Ole Troan282093f2018-09-19 12:38:51 +0200287 tr->ipv6 = is_ip6 ? 1 : 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500288 tr->n_fragments = vec_len (buffer);
289 tr->next = vnet_buffer (p0)->ip_frag.next_index;
290 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291
Ole Troan282093f2018-09-19 12:38:51 +0200292 if (!is_ip6 && error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500293 {
294 icmp4_error_set_vnet_buffer (p0, ICMP4_destination_unreachable,
295 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
296 vnet_buffer (p0)->ip_frag.mtu);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500297 next0 = IP4_FRAG_NEXT_ICMP_ERROR;
298 }
299 else
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800300 {
Ole Troan282093f2018-09-19 12:38:51 +0200301 if (is_ip6)
302 next0 =
303 (error0 ==
304 IP_FRAG_ERROR_NONE) ? vnet_buffer (p0)->
305 ip_frag.next_index : IP6_FRAG_NEXT_DROP;
306 else
307 next0 =
308 (error0 ==
309 IP_FRAG_ERROR_NONE) ? vnet_buffer (p0)->
310 ip_frag.next_index : IP4_FRAG_NEXT_DROP;
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800311 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
Dave Barachd7cb1b52016-12-09 09:52:16 -0500313 if (error0 == IP_FRAG_ERROR_NONE)
314 {
Ole Troan7eb9d962018-08-10 14:39:48 +0200315 /* Free original buffer chain */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500316 frag_sent += vec_len (buffer);
317 small_packets += (vec_len (buffer) == 1);
Ole Troan282093f2018-09-19 12:38:51 +0200318 vlib_buffer_free_one (vm, pi0); /* Free original packet */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500319 }
320 else
Ole Troan282093f2018-09-19 12:38:51 +0200321 {
322 vlib_error_count (vm, node_index, error0, 1);
323 vec_add1 (buffer, pi0); /* Get rid of the original buffer */
324 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325
Ole Troan282093f2018-09-19 12:38:51 +0200326 /* Send fragments that were added in the frame */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500327 frag_from = buffer;
328 frag_left = vec_len (buffer);
329
330 while (frag_left > 0)
331 {
332 while (frag_left > 0 && n_left_to_next > 0)
333 {
334 u32 i;
335 i = to_next[0] = frag_from[0];
336 frag_from += 1;
337 frag_left -= 1;
338 to_next += 1;
339 n_left_to_next -= 1;
340
341 vlib_get_buffer (vm, i)->error = error_node->errors[error0];
342 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
343 to_next, n_left_to_next, i,
344 next0);
345 }
346 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
347 vlib_get_next_frame (vm, node, next_index, to_next,
348 n_left_to_next);
349 }
350 vec_reset_length (buffer);
351 }
352 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500354 vec_free (buffer);
355
Ole Troan282093f2018-09-19 12:38:51 +0200356 vlib_node_increment_counter (vm, node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500357 IP_FRAG_ERROR_FRAGMENT_SENT, frag_sent);
Ole Troan282093f2018-09-19 12:38:51 +0200358 vlib_node_increment_counter (vm, node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500359 IP_FRAG_ERROR_SMALL_PACKET, small_packets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360
361 return frame->n_vectors;
362}
363
Dave Barachd7cb1b52016-12-09 09:52:16 -0500364
Ole Troan282093f2018-09-19 12:38:51 +0200365
366static uword
367ip4_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500368{
Ole Troan282093f2018-09-19 12:38:51 +0200369 return frag_node_inline (vm, node, frame, ip4_frag_node.index,
370 0 /* is_ip6 */ );
Dave Barachd7cb1b52016-12-09 09:52:16 -0500371}
372
373static uword
374ip6_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
375{
Ole Troan282093f2018-09-19 12:38:51 +0200376 return frag_node_inline (vm, node, frame, ip6_frag_node.index,
377 1 /* is_ip6 */ );
378}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500379
Ole Troan282093f2018-09-19 12:38:51 +0200380/*
381 * Fragments the packet given in from_bi. Fragments are returned in the buffer vector.
382 * Caller must ensure the original packet is freed.
383 */
384void
385ip6_frag_do_fragment (vlib_main_t * vm, u32 from_bi, u32 ** buffer,
386 ip_frag_error_t * error)
387{
388 vlib_buffer_t *from_b;
389 ip6_header_t *ip6;
390 u16 mtu, len, max, rem, ip_frag_id;
391
392 from_b = vlib_get_buffer (vm, from_bi);
393 mtu = vnet_buffer (from_b)->ip_frag.mtu;
394 ip6 = (ip6_header_t *) vlib_buffer_get_current (from_b);
395
396 rem = clib_net_to_host_u16 (ip6->payload_length);
397 max = (mtu - sizeof (ip6_header_t) - sizeof (ip6_frag_hdr_t)) & ~0x7; // TODO: Is max correct??
398
399 if (rem >
400 (vlib_buffer_length_in_chain (vm, from_b) - sizeof (ip6_header_t)))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500401 {
Ole Troan282093f2018-09-19 12:38:51 +0200402 *error = IP_FRAG_ERROR_MALFORMED;
403 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500404 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500405
Ole Troan282093f2018-09-19 12:38:51 +0200406 /* TODO: Look through header chain for fragmentation header */
407 if (ip6->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
408 {
409 *error = IP_FRAG_ERROR_MALFORMED;
410 return;
411 }
412
413 u8 *from_data = (void *) (ip6 + 1);
414 vlib_buffer_t *org_from_b = from_b;
415 u16 fo = 0;
416 u16 left_in_from_buffer = from_b->current_length - sizeof (ip6_header_t);
417 u16 ptr = 0;
418
419 ip_frag_id = ++running_fragment_id; // Fix
420
421 /* Do the actual fragmentation */
422 while (rem)
423 {
424 u32 to_bi;
425 vlib_buffer_t *to_b;
426 ip6_header_t *to_ip6;
427 ip6_frag_hdr_t *to_frag_hdr;
428 u8 *to_data;
429
430 len =
431 (rem >
432 (mtu - sizeof (ip6_header_t) - sizeof (ip6_frag_hdr_t)) ? max : rem);
433 if (len != rem) /* Last fragment does not need to divisible by 8 */
434 len &= ~0x7;
435 if ((to_b = frag_buffer_alloc (org_from_b, &to_bi)) == 0)
436 {
437 *error = IP_FRAG_ERROR_MEMORY;
438 return;
439 }
440 vec_add1 (*buffer, to_bi);
441 frag_set_sw_if_index (to_b, org_from_b);
442
443 /* Copy ip6 header */
Dave Barach178cf492018-11-13 16:34:13 -0500444 clib_memcpy_fast (to_b->data, ip6, sizeof (ip6_header_t));
Ole Troan282093f2018-09-19 12:38:51 +0200445 to_ip6 = vlib_buffer_get_current (to_b);
446 to_frag_hdr = (ip6_frag_hdr_t *) (to_ip6 + 1);
447 to_data = (void *) (to_frag_hdr + 1);
448
449 /* Spin through from buffers filling up the to buffer */
450 u16 left_in_to_buffer = len, to_ptr = 0;
451 while (1)
452 {
453 u16 bytes_to_copy;
454
455 /* Figure out how many bytes we can safely copy */
456 bytes_to_copy = left_in_to_buffer <= left_in_from_buffer ?
457 left_in_to_buffer : left_in_from_buffer;
Dave Barach178cf492018-11-13 16:34:13 -0500458 clib_memcpy_fast (to_data + to_ptr, from_data + ptr, bytes_to_copy);
Ole Troan282093f2018-09-19 12:38:51 +0200459 left_in_to_buffer -= bytes_to_copy;
460 ptr += bytes_to_copy;
461 left_in_from_buffer -= bytes_to_copy;
462 if (left_in_to_buffer == 0)
463 break;
464
465 ASSERT (left_in_from_buffer <= 0);
466 /* Move buffer */
467 if (!(from_b->flags & VLIB_BUFFER_NEXT_PRESENT))
468 {
469 *error = IP_FRAG_ERROR_MALFORMED;
470 return;
471 }
472 from_b = vlib_get_buffer (vm, from_b->next_buffer);
473 from_data = (u8 *) vlib_buffer_get_current (from_b);
474 ptr = 0;
475 left_in_from_buffer = from_b->current_length;
476 to_ptr += bytes_to_copy;
477 }
478
479 to_b->current_length =
480 len + sizeof (ip6_header_t) + sizeof (ip6_frag_hdr_t);
481 to_ip6->payload_length =
482 clib_host_to_net_u16 (len + sizeof (ip6_frag_hdr_t));
483 to_ip6->protocol = IP_PROTOCOL_IPV6_FRAGMENTATION;
484 to_frag_hdr->fragment_offset_and_more =
485 ip6_frag_hdr_offset_and_more ((fo >> 3), len != rem);
486 to_frag_hdr->identification = ip_frag_id;
487 to_frag_hdr->next_hdr = ip6->protocol;
488 to_frag_hdr->rsv = 0;
489
490 rem -= len;
491 fo += len;
492 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500493}
494
495static char *ip4_frag_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496#define _(sym,string) string,
497 foreach_ip_frag_error
498#undef _
499};
500
Dave Barachd7cb1b52016-12-09 09:52:16 -0500501/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502VLIB_REGISTER_NODE (ip4_frag_node) = {
503 .function = ip4_frag,
504 .name = IP4_FRAG_NODE_NAME,
505 .vector_size = sizeof (u32),
506 .format_trace = format_ip_frag_trace,
507 .type = VLIB_NODE_TYPE_INTERNAL,
508
509 .n_errors = IP_FRAG_N_ERROR,
510 .error_strings = ip4_frag_error_strings,
511
512 .n_next_nodes = IP4_FRAG_N_NEXT,
513 .next_nodes = {
Ole Troanb3655e52018-08-16 22:08:49 +0200514 [IP4_FRAG_NEXT_IP4_REWRITE] = "ip4-rewrite",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 [IP4_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup",
516 [IP4_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup",
Ole Troan9fb87552016-01-13 22:30:43 +0100517 [IP4_FRAG_NEXT_ICMP_ERROR] = "ip4-icmp-error",
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800518 [IP4_FRAG_NEXT_DROP] = "ip4-drop"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 },
520};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500521/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522
Dave Barachd7cb1b52016-12-09 09:52:16 -0500523/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524VLIB_REGISTER_NODE (ip6_frag_node) = {
525 .function = ip6_frag,
526 .name = IP6_FRAG_NODE_NAME,
527 .vector_size = sizeof (u32),
528 .format_trace = format_ip_frag_trace,
529 .type = VLIB_NODE_TYPE_INTERNAL,
530
531 .n_errors = IP_FRAG_N_ERROR,
532 .error_strings = ip4_frag_error_strings,
533
534 .n_next_nodes = IP6_FRAG_N_NEXT,
535 .next_nodes = {
Ole Troanb3655e52018-08-16 22:08:49 +0200536 [IP6_FRAG_NEXT_IP6_REWRITE] = "ip6-rewrite",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537 [IP6_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup",
538 [IP6_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup",
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800539 [IP6_FRAG_NEXT_DROP] = "ip6-drop"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 },
541};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500542/* *INDENT-ON* */
543
544/*
545 * fd.io coding-style-patch-verification: ON
546 *
547 * Local Variables:
548 * eval: (c-set-style "gnu")
549 * End:
550 */