blob: fd5bc6fa0bad8418ee1a23412e3d1eedf76c450e [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];
Vijayabhaskar Katamreddy64fb9952018-11-28 12:05:23 -080064
65 /* Copy QoS Bits */
66 if (PREDICT_TRUE (from->flags & VNET_BUFFER_F_QOS_DATA_VALID))
67 {
68 vnet_buffer2 (to)->qos = vnet_buffer2 (from)->qos;
69 to->flags |= VNET_BUFFER_F_QOS_DATA_VALID;
70 }
Ole Troan282093f2018-09-19 12:38:51 +020071}
72
73static vlib_buffer_t *
74frag_buffer_alloc (vlib_buffer_t * org_b, u32 * bi)
75{
76 vlib_main_t *vm = vlib_get_main ();
77 if (vlib_buffer_alloc (vm, bi, 1) != 1)
78 return 0;
79
80 vlib_buffer_t *b = vlib_get_buffer (vm, *bi);
Ole Troan282093f2018-09-19 12:38:51 +020081 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
82 vlib_buffer_copy_trace_flag (vm, org_b, *bi);
83
84 return b;
85}
86
Ole Troan4146c652018-08-08 22:23:19 +020087/*
88 * Limitation: Does follow buffer chains in the packet to fragment,
89 * but does not generate buffer chains. I.e. a fragment is always
90 * contained with in a single buffer and limited to the max buffer
91 * size.
92 */
Vijayabhaskar Katamreddye95d1a12018-06-28 11:08:29 -070093void
Ole Troan4146c652018-08-08 22:23:19 +020094ip4_frag_do_fragment (vlib_main_t * vm, u32 from_bi, u32 ** buffer,
Dave Barachd7cb1b52016-12-09 09:52:16 -050095 ip_frag_error_t * error)
Ed Warnickecb9cada2015-12-08 15:45:58 -070096{
Ole Troan4146c652018-08-08 22:23:19 +020097 vlib_buffer_t *from_b;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 ip4_header_t *ip4;
Ole Troan282093f2018-09-19 12:38:51 +020099 u16 mtu, len, max, rem, ip_frag_id, ip_frag_offset;
Ole Troan4146c652018-08-08 22:23:19 +0200100 u8 *org_from_packet, more;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
Ole Troan4146c652018-08-08 22:23:19 +0200102 from_b = vlib_get_buffer (vm, from_bi);
Ole Troan4146c652018-08-08 22:23:19 +0200103 mtu = vnet_buffer (from_b)->ip_frag.mtu;
104 org_from_packet = vlib_buffer_get_current (from_b);
Ole Troan282093f2018-09-19 12:38:51 +0200105 ip4 = (ip4_header_t *) vlib_buffer_get_current (from_b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
Ole Troan4146c652018-08-08 22:23:19 +0200107 rem = clib_net_to_host_u16 (ip4->length) - sizeof (ip4_header_t);
Juraj Sloboda68b7cb82018-10-16 12:18:21 +0200108 max =
Damjan Marion8934a042019-02-09 23:29:26 +0100109 (clib_min (mtu, vlib_buffer_get_default_data_size (vm)) -
Damjan Marion5de3fec2019-02-06 14:22:32 +0100110 sizeof (ip4_header_t)) & ~0x7;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
Ole Troan4146c652018-08-08 22:23:19 +0200112 if (rem >
Ole Troan282093f2018-09-19 12:38:51 +0200113 (vlib_buffer_length_in_chain (vm, from_b) - sizeof (ip4_header_t)))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500114 {
115 *error = IP_FRAG_ERROR_MALFORMED;
116 return;
117 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Ole Troan4146c652018-08-08 22:23:19 +0200119 if (mtu < sizeof (ip4_header_t))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500120 {
121 *error = IP_FRAG_ERROR_CANT_FRAGMENT_HEADER;
122 return;
123 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
125 if (ip4->flags_and_fragment_offset &
Dave Barachd7cb1b52016-12-09 09:52:16 -0500126 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT))
127 {
128 *error = IP_FRAG_ERROR_DONT_FRAGMENT_SET;
129 return;
130 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
Dave Barachd7cb1b52016-12-09 09:52:16 -0500132 if (ip4_is_fragment (ip4))
133 {
134 ip_frag_id = ip4->fragment_id;
135 ip_frag_offset = ip4_get_fragment_offset (ip4);
136 more =
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800137 !(!(ip4->flags_and_fragment_offset &
138 clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS)));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500139 }
140 else
141 {
142 ip_frag_id = (++running_fragment_id);
143 ip_frag_offset = 0;
144 more = 0;
145 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
Ole Troan4146c652018-08-08 22:23:19 +0200147 u8 *from_data = (void *) (ip4 + 1);
148 vlib_buffer_t *org_from_b = from_b;
Ole Troan282093f2018-09-19 12:38:51 +0200149 u16 fo = 0;
150 u16 left_in_from_buffer = from_b->current_length - sizeof (ip4_header_t);
151 u16 ptr = 0;
Ole Troan4146c652018-08-08 22:23:19 +0200152
153 /* Do the actual fragmentation */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500154 while (rem)
155 {
Ole Troan4146c652018-08-08 22:23:19 +0200156 u32 to_bi;
157 vlib_buffer_t *to_b;
158 ip4_header_t *to_ip4;
159 u8 *to_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
Juraj Sloboda68b7cb82018-10-16 12:18:21 +0200161 len = (rem > max ? max : rem);
Ole Troan4146c652018-08-08 22:23:19 +0200162 if (len != rem) /* Last fragment does not need to divisible by 8 */
163 len &= ~0x7;
Ole Troan282093f2018-09-19 12:38:51 +0200164 if ((to_b = frag_buffer_alloc (org_from_b, &to_bi)) == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500165 {
Ole Troan4146c652018-08-08 22:23:19 +0200166 *error = IP_FRAG_ERROR_MEMORY;
Ole Troan4146c652018-08-08 22:23:19 +0200167 return;
168 }
169 vec_add1 (*buffer, to_bi);
Ole Troan282093f2018-09-19 12:38:51 +0200170 frag_set_sw_if_index (to_b, org_from_b);
Ole Troan4146c652018-08-08 22:23:19 +0200171
Ole Troan282093f2018-09-19 12:38:51 +0200172 /* Copy ip4 header */
Dave Barach178cf492018-11-13 16:34:13 -0500173 clib_memcpy_fast (to_b->data, org_from_packet, sizeof (ip4_header_t));
Ole Troan282093f2018-09-19 12:38:51 +0200174 to_ip4 = vlib_buffer_get_current (to_b);
Ole Troan4146c652018-08-08 22:23:19 +0200175 to_data = (void *) (to_ip4 + 1);
176
Ole Troan7eb9d962018-08-10 14:39:48 +0200177 /* Spin through from buffers filling up the to buffer */
Ole Troan282093f2018-09-19 12:38:51 +0200178 u16 left_in_to_buffer = len, to_ptr = 0;
Ole Troan7eb9d962018-08-10 14:39:48 +0200179 while (1)
Ole Troan4146c652018-08-08 22:23:19 +0200180 {
Ole Troan282093f2018-09-19 12:38:51 +0200181 u16 bytes_to_copy;
182
Ole Troan7eb9d962018-08-10 14:39:48 +0200183 /* Figure out how many bytes we can safely copy */
184 bytes_to_copy = left_in_to_buffer <= left_in_from_buffer ?
185 left_in_to_buffer : left_in_from_buffer;
Dave Barach178cf492018-11-13 16:34:13 -0500186 clib_memcpy_fast (to_data + to_ptr, from_data + ptr, bytes_to_copy);
Ole Troan7eb9d962018-08-10 14:39:48 +0200187 left_in_to_buffer -= bytes_to_copy;
188 ptr += bytes_to_copy;
189 left_in_from_buffer -= bytes_to_copy;
190 if (left_in_to_buffer == 0)
191 break;
Ole Troan4146c652018-08-08 22:23:19 +0200192
Ole Troan282093f2018-09-19 12:38:51 +0200193 ASSERT (left_in_from_buffer <= 0);
Ole Troan4146c652018-08-08 22:23:19 +0200194 /* Move buffer */
195 if (!(from_b->flags & VLIB_BUFFER_NEXT_PRESENT))
196 {
197 *error = IP_FRAG_ERROR_MALFORMED;
198 return;
199 }
200 from_b = vlib_get_buffer (vm, from_b->next_buffer);
201 from_data = (u8 *) vlib_buffer_get_current (from_b);
Ole Troan7eb9d962018-08-10 14:39:48 +0200202 ptr = 0;
203 left_in_from_buffer = from_b->current_length;
204 to_ptr += bytes_to_copy;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500205 }
Ole Troan7eb9d962018-08-10 14:39:48 +0200206
Ole Troan282093f2018-09-19 12:38:51 +0200207 to_b->current_length = len + sizeof (ip4_header_t);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500208
Ole Troan4146c652018-08-08 22:23:19 +0200209 to_ip4->fragment_id = ip_frag_id;
210 to_ip4->flags_and_fragment_offset =
211 clib_host_to_net_u16 ((fo >> 3) + ip_frag_offset);
212 to_ip4->flags_and_fragment_offset |=
Dave Barachd7cb1b52016-12-09 09:52:16 -0500213 clib_host_to_net_u16 (((len != rem) || more) << 13);
Ole Troan4146c652018-08-08 22:23:19 +0200214 to_ip4->length = clib_host_to_net_u16 (len + sizeof (ip4_header_t));
215 to_ip4->checksum = ip4_header_checksum (to_ip4);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500216
Ole Troan4146c652018-08-08 22:23:19 +0200217 if (vnet_buffer (org_from_b)->ip_frag.flags & IP_FRAG_FLAG_IP4_HEADER)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500218 {
Ole Troan4146c652018-08-08 22:23:19 +0200219 /* Encapsulating ipv4 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500220 ip4_header_t *encap_header4 =
Ole Troan4146c652018-08-08 22:23:19 +0200221 (ip4_header_t *) vlib_buffer_get_current (to_b);
222 encap_header4->length = clib_host_to_net_u16 (to_b->current_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500223 encap_header4->checksum = ip4_header_checksum (encap_header4);
224 }
Ole Troan4146c652018-08-08 22:23:19 +0200225 else if (vnet_buffer (org_from_b)->
226 ip_frag.flags & IP_FRAG_FLAG_IP6_HEADER)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500227 {
Ole Troan4146c652018-08-08 22:23:19 +0200228 /* Encapsulating ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500229 ip6_header_t *encap_header6 =
Ole Troan4146c652018-08-08 22:23:19 +0200230 (ip6_header_t *) vlib_buffer_get_current (to_b);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500231 encap_header6->payload_length =
Ole Troan4146c652018-08-08 22:23:19 +0200232 clib_host_to_net_u16 (to_b->current_length -
Dave Barachd7cb1b52016-12-09 09:52:16 -0500233 sizeof (*encap_header6));
234 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500235 rem -= len;
Ole Troan4146c652018-08-08 22:23:19 +0200236 fo += len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238}
239
Ole Troan9fb87552016-01-13 22:30:43 +0100240void
Ole Troan282093f2018-09-19 12:38:51 +0200241ip_frag_set_vnet_buffer (vlib_buffer_t * b, u16 mtu, u8 next_index, u8 flags)
Ole Troan9fb87552016-01-13 22:30:43 +0100242{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500243 vnet_buffer (b)->ip_frag.mtu = mtu;
244 vnet_buffer (b)->ip_frag.next_index = next_index;
245 vnet_buffer (b)->ip_frag.flags = flags;
Ole Troan9fb87552016-01-13 22:30:43 +0100246}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Ole Troan282093f2018-09-19 12:38:51 +0200248
249static inline uword
250frag_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
251 vlib_frame_t * frame, u32 node_index, bool is_ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252{
253 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Ole Troan282093f2018-09-19 12:38:51 +0200254 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 from = vlib_frame_vector_args (frame);
256 n_left_from = frame->n_vectors;
257 next_index = node->cached_next_index;
258 u32 frag_sent = 0, small_packets = 0;
259 u32 *buffer = 0;
260
Dave Barachd7cb1b52016-12-09 09:52:16 -0500261 while (n_left_from > 0)
262 {
263 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
Dave Barachd7cb1b52016-12-09 09:52:16 -0500265 while (n_left_from > 0 && n_left_to_next > 0)
266 {
267 u32 pi0, *frag_from, frag_left;
268 vlib_buffer_t *p0;
269 ip_frag_error_t error0;
Ole Troan282093f2018-09-19 12:38:51 +0200270 int next0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
Ole Troan282093f2018-09-19 12:38:51 +0200272 /*
273 * Note: The packet is not enqueued now. It is instead put
274 * in a vector where other fragments will be put as well.
275 */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500276 pi0 = from[0];
277 from += 1;
278 n_left_from -= 1;
279 error0 = IP_FRAG_ERROR_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
Dave Barachd7cb1b52016-12-09 09:52:16 -0500281 p0 = vlib_get_buffer (vm, pi0);
Ole Troan282093f2018-09-19 12:38:51 +0200282 if (is_ip6)
283 ip6_frag_do_fragment (vm, pi0, &buffer, &error0);
284 else
285 ip4_frag_do_fragment (vm, pi0, &buffer, &error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Dave Barachd7cb1b52016-12-09 09:52:16 -0500287 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
288 {
289 ip_frag_trace_t *tr =
290 vlib_add_trace (vm, node, p0, sizeof (*tr));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500291 tr->mtu = vnet_buffer (p0)->ip_frag.mtu;
Ole Troan282093f2018-09-19 12:38:51 +0200292 tr->ipv6 = is_ip6 ? 1 : 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500293 tr->n_fragments = vec_len (buffer);
294 tr->next = vnet_buffer (p0)->ip_frag.next_index;
295 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296
Ole Troan282093f2018-09-19 12:38:51 +0200297 if (!is_ip6 && error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500298 {
299 icmp4_error_set_vnet_buffer (p0, ICMP4_destination_unreachable,
300 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
301 vnet_buffer (p0)->ip_frag.mtu);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500302 next0 = IP4_FRAG_NEXT_ICMP_ERROR;
303 }
304 else
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800305 {
Ole Troan282093f2018-09-19 12:38:51 +0200306 if (is_ip6)
307 next0 =
308 (error0 ==
309 IP_FRAG_ERROR_NONE) ? vnet_buffer (p0)->
310 ip_frag.next_index : IP6_FRAG_NEXT_DROP;
311 else
312 next0 =
313 (error0 ==
314 IP_FRAG_ERROR_NONE) ? vnet_buffer (p0)->
315 ip_frag.next_index : IP4_FRAG_NEXT_DROP;
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800316 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
Dave Barachd7cb1b52016-12-09 09:52:16 -0500318 if (error0 == IP_FRAG_ERROR_NONE)
319 {
Ole Troan7eb9d962018-08-10 14:39:48 +0200320 /* Free original buffer chain */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500321 frag_sent += vec_len (buffer);
322 small_packets += (vec_len (buffer) == 1);
Ole Troan282093f2018-09-19 12:38:51 +0200323 vlib_buffer_free_one (vm, pi0); /* Free original packet */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500324 }
325 else
Ole Troan282093f2018-09-19 12:38:51 +0200326 {
327 vlib_error_count (vm, node_index, error0, 1);
328 vec_add1 (buffer, pi0); /* Get rid of the original buffer */
329 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
Ole Troan282093f2018-09-19 12:38:51 +0200331 /* Send fragments that were added in the frame */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500332 frag_from = buffer;
333 frag_left = vec_len (buffer);
334
335 while (frag_left > 0)
336 {
337 while (frag_left > 0 && n_left_to_next > 0)
338 {
339 u32 i;
340 i = to_next[0] = frag_from[0];
341 frag_from += 1;
342 frag_left -= 1;
343 to_next += 1;
344 n_left_to_next -= 1;
345
346 vlib_get_buffer (vm, i)->error = error_node->errors[error0];
347 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
348 to_next, n_left_to_next, i,
349 next0);
350 }
351 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
352 vlib_get_next_frame (vm, node, next_index, to_next,
353 n_left_to_next);
354 }
355 vec_reset_length (buffer);
356 }
357 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500359 vec_free (buffer);
360
Ole Troan282093f2018-09-19 12:38:51 +0200361 vlib_node_increment_counter (vm, node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500362 IP_FRAG_ERROR_FRAGMENT_SENT, frag_sent);
Ole Troan282093f2018-09-19 12:38:51 +0200363 vlib_node_increment_counter (vm, node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500364 IP_FRAG_ERROR_SMALL_PACKET, small_packets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
366 return frame->n_vectors;
367}
368
Dave Barachd7cb1b52016-12-09 09:52:16 -0500369
Ole Troan282093f2018-09-19 12:38:51 +0200370
371static uword
372ip4_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500373{
Ole Troan282093f2018-09-19 12:38:51 +0200374 return frag_node_inline (vm, node, frame, ip4_frag_node.index,
375 0 /* is_ip6 */ );
Dave Barachd7cb1b52016-12-09 09:52:16 -0500376}
377
378static uword
379ip6_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
380{
Ole Troan282093f2018-09-19 12:38:51 +0200381 return frag_node_inline (vm, node, frame, ip6_frag_node.index,
382 1 /* is_ip6 */ );
383}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500384
Ole Troan282093f2018-09-19 12:38:51 +0200385/*
386 * Fragments the packet given in from_bi. Fragments are returned in the buffer vector.
387 * Caller must ensure the original packet is freed.
388 */
389void
390ip6_frag_do_fragment (vlib_main_t * vm, u32 from_bi, u32 ** buffer,
391 ip_frag_error_t * error)
392{
393 vlib_buffer_t *from_b;
394 ip6_header_t *ip6;
395 u16 mtu, len, max, rem, ip_frag_id;
396
397 from_b = vlib_get_buffer (vm, from_bi);
398 mtu = vnet_buffer (from_b)->ip_frag.mtu;
399 ip6 = (ip6_header_t *) vlib_buffer_get_current (from_b);
400
401 rem = clib_net_to_host_u16 (ip6->payload_length);
402 max = (mtu - sizeof (ip6_header_t) - sizeof (ip6_frag_hdr_t)) & ~0x7; // TODO: Is max correct??
403
404 if (rem >
405 (vlib_buffer_length_in_chain (vm, from_b) - sizeof (ip6_header_t)))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500406 {
Ole Troan282093f2018-09-19 12:38:51 +0200407 *error = IP_FRAG_ERROR_MALFORMED;
408 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500409 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500410
Ole Troan282093f2018-09-19 12:38:51 +0200411 /* TODO: Look through header chain for fragmentation header */
412 if (ip6->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
413 {
414 *error = IP_FRAG_ERROR_MALFORMED;
415 return;
416 }
417
418 u8 *from_data = (void *) (ip6 + 1);
419 vlib_buffer_t *org_from_b = from_b;
420 u16 fo = 0;
421 u16 left_in_from_buffer = from_b->current_length - sizeof (ip6_header_t);
422 u16 ptr = 0;
423
424 ip_frag_id = ++running_fragment_id; // Fix
425
426 /* Do the actual fragmentation */
427 while (rem)
428 {
429 u32 to_bi;
430 vlib_buffer_t *to_b;
431 ip6_header_t *to_ip6;
432 ip6_frag_hdr_t *to_frag_hdr;
433 u8 *to_data;
434
435 len =
436 (rem >
437 (mtu - sizeof (ip6_header_t) - sizeof (ip6_frag_hdr_t)) ? max : rem);
438 if (len != rem) /* Last fragment does not need to divisible by 8 */
439 len &= ~0x7;
440 if ((to_b = frag_buffer_alloc (org_from_b, &to_bi)) == 0)
441 {
442 *error = IP_FRAG_ERROR_MEMORY;
443 return;
444 }
445 vec_add1 (*buffer, to_bi);
446 frag_set_sw_if_index (to_b, org_from_b);
447
448 /* Copy ip6 header */
Dave Barach178cf492018-11-13 16:34:13 -0500449 clib_memcpy_fast (to_b->data, ip6, sizeof (ip6_header_t));
Ole Troan282093f2018-09-19 12:38:51 +0200450 to_ip6 = vlib_buffer_get_current (to_b);
451 to_frag_hdr = (ip6_frag_hdr_t *) (to_ip6 + 1);
452 to_data = (void *) (to_frag_hdr + 1);
453
454 /* Spin through from buffers filling up the to buffer */
455 u16 left_in_to_buffer = len, to_ptr = 0;
456 while (1)
457 {
458 u16 bytes_to_copy;
459
460 /* Figure out how many bytes we can safely copy */
461 bytes_to_copy = left_in_to_buffer <= left_in_from_buffer ?
462 left_in_to_buffer : left_in_from_buffer;
Dave Barach178cf492018-11-13 16:34:13 -0500463 clib_memcpy_fast (to_data + to_ptr, from_data + ptr, bytes_to_copy);
Ole Troan282093f2018-09-19 12:38:51 +0200464 left_in_to_buffer -= bytes_to_copy;
465 ptr += bytes_to_copy;
466 left_in_from_buffer -= bytes_to_copy;
467 if (left_in_to_buffer == 0)
468 break;
469
470 ASSERT (left_in_from_buffer <= 0);
471 /* Move buffer */
472 if (!(from_b->flags & VLIB_BUFFER_NEXT_PRESENT))
473 {
474 *error = IP_FRAG_ERROR_MALFORMED;
475 return;
476 }
477 from_b = vlib_get_buffer (vm, from_b->next_buffer);
478 from_data = (u8 *) vlib_buffer_get_current (from_b);
479 ptr = 0;
480 left_in_from_buffer = from_b->current_length;
481 to_ptr += bytes_to_copy;
482 }
483
484 to_b->current_length =
485 len + sizeof (ip6_header_t) + sizeof (ip6_frag_hdr_t);
486 to_ip6->payload_length =
487 clib_host_to_net_u16 (len + sizeof (ip6_frag_hdr_t));
488 to_ip6->protocol = IP_PROTOCOL_IPV6_FRAGMENTATION;
489 to_frag_hdr->fragment_offset_and_more =
490 ip6_frag_hdr_offset_and_more ((fo >> 3), len != rem);
491 to_frag_hdr->identification = ip_frag_id;
492 to_frag_hdr->next_hdr = ip6->protocol;
493 to_frag_hdr->rsv = 0;
494
495 rem -= len;
496 fo += len;
497 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500498}
499
500static char *ip4_frag_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501#define _(sym,string) string,
502 foreach_ip_frag_error
503#undef _
504};
505
Dave Barachd7cb1b52016-12-09 09:52:16 -0500506/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507VLIB_REGISTER_NODE (ip4_frag_node) = {
508 .function = ip4_frag,
509 .name = IP4_FRAG_NODE_NAME,
510 .vector_size = sizeof (u32),
511 .format_trace = format_ip_frag_trace,
512 .type = VLIB_NODE_TYPE_INTERNAL,
513
514 .n_errors = IP_FRAG_N_ERROR,
515 .error_strings = ip4_frag_error_strings,
516
517 .n_next_nodes = IP4_FRAG_N_NEXT,
518 .next_nodes = {
Ole Troanb3655e52018-08-16 22:08:49 +0200519 [IP4_FRAG_NEXT_IP4_REWRITE] = "ip4-rewrite",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 [IP4_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup",
521 [IP4_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup",
Ole Troan9fb87552016-01-13 22:30:43 +0100522 [IP4_FRAG_NEXT_ICMP_ERROR] = "ip4-icmp-error",
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800523 [IP4_FRAG_NEXT_DROP] = "ip4-drop"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 },
525};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500526/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
Dave Barachd7cb1b52016-12-09 09:52:16 -0500528/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529VLIB_REGISTER_NODE (ip6_frag_node) = {
530 .function = ip6_frag,
531 .name = IP6_FRAG_NODE_NAME,
532 .vector_size = sizeof (u32),
533 .format_trace = format_ip_frag_trace,
534 .type = VLIB_NODE_TYPE_INTERNAL,
535
536 .n_errors = IP_FRAG_N_ERROR,
537 .error_strings = ip4_frag_error_strings,
538
539 .n_next_nodes = IP6_FRAG_N_NEXT,
540 .next_nodes = {
Ole Troanb3655e52018-08-16 22:08:49 +0200541 [IP6_FRAG_NEXT_IP6_REWRITE] = "ip6-rewrite",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 [IP6_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup",
543 [IP6_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup",
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800544 [IP6_FRAG_NEXT_DROP] = "ip6-drop"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 },
546};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500547/* *INDENT-ON* */
548
549/*
550 * fd.io coding-style-patch-verification: ON
551 *
552 * Local Variables:
553 * eval: (c-set-style "gnu")
554 * End:
555 */