blob: de0998779a847f98d2a26f820a22dd5b5278da32 [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);
81 vlib_buffer_free_list_t *fl =
82 vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
83 vlib_buffer_init_for_free_list (b, fl);
84 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
85 vlib_buffer_copy_trace_flag (vm, org_b, *bi);
86
87 return b;
88}
89
Ole Troan4146c652018-08-08 22:23:19 +020090/*
91 * Limitation: Does follow buffer chains in the packet to fragment,
92 * but does not generate buffer chains. I.e. a fragment is always
93 * contained with in a single buffer and limited to the max buffer
94 * size.
95 */
Vijayabhaskar Katamreddye95d1a12018-06-28 11:08:29 -070096void
Ole Troan4146c652018-08-08 22:23:19 +020097ip4_frag_do_fragment (vlib_main_t * vm, u32 from_bi, u32 ** buffer,
Dave Barachd7cb1b52016-12-09 09:52:16 -050098 ip_frag_error_t * error)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099{
Ole Troan4146c652018-08-08 22:23:19 +0200100 vlib_buffer_t *from_b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 ip4_header_t *ip4;
Ole Troan282093f2018-09-19 12:38:51 +0200102 u16 mtu, len, max, rem, ip_frag_id, ip_frag_offset;
Ole Troan4146c652018-08-08 22:23:19 +0200103 u8 *org_from_packet, more;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
Ole Troan4146c652018-08-08 22:23:19 +0200105 from_b = vlib_get_buffer (vm, from_bi);
Ole Troan4146c652018-08-08 22:23:19 +0200106 mtu = vnet_buffer (from_b)->ip_frag.mtu;
107 org_from_packet = vlib_buffer_get_current (from_b);
Ole Troan282093f2018-09-19 12:38:51 +0200108 ip4 = (ip4_header_t *) vlib_buffer_get_current (from_b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Ole Troan4146c652018-08-08 22:23:19 +0200110 rem = clib_net_to_host_u16 (ip4->length) - sizeof (ip4_header_t);
Juraj Sloboda68b7cb82018-10-16 12:18:21 +0200111 max =
112 (clib_min (mtu, VLIB_BUFFER_DATA_SIZE) - sizeof (ip4_header_t)) & ~0x7;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
Ole Troan4146c652018-08-08 22:23:19 +0200114 if (rem >
Ole Troan282093f2018-09-19 12:38:51 +0200115 (vlib_buffer_length_in_chain (vm, from_b) - sizeof (ip4_header_t)))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500116 {
117 *error = IP_FRAG_ERROR_MALFORMED;
118 return;
119 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
Ole Troan4146c652018-08-08 22:23:19 +0200121 if (mtu < sizeof (ip4_header_t))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500122 {
123 *error = IP_FRAG_ERROR_CANT_FRAGMENT_HEADER;
124 return;
125 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
127 if (ip4->flags_and_fragment_offset &
Dave Barachd7cb1b52016-12-09 09:52:16 -0500128 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT))
129 {
130 *error = IP_FRAG_ERROR_DONT_FRAGMENT_SET;
131 return;
132 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133
Dave Barachd7cb1b52016-12-09 09:52:16 -0500134 if (ip4_is_fragment (ip4))
135 {
136 ip_frag_id = ip4->fragment_id;
137 ip_frag_offset = ip4_get_fragment_offset (ip4);
138 more =
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800139 !(!(ip4->flags_and_fragment_offset &
140 clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS)));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500141 }
142 else
143 {
144 ip_frag_id = (++running_fragment_id);
145 ip_frag_offset = 0;
146 more = 0;
147 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148
Ole Troan4146c652018-08-08 22:23:19 +0200149 u8 *from_data = (void *) (ip4 + 1);
150 vlib_buffer_t *org_from_b = from_b;
Ole Troan282093f2018-09-19 12:38:51 +0200151 u16 fo = 0;
152 u16 left_in_from_buffer = from_b->current_length - sizeof (ip4_header_t);
153 u16 ptr = 0;
Ole Troan4146c652018-08-08 22:23:19 +0200154
155 /* Do the actual fragmentation */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500156 while (rem)
157 {
Ole Troan4146c652018-08-08 22:23:19 +0200158 u32 to_bi;
159 vlib_buffer_t *to_b;
160 ip4_header_t *to_ip4;
161 u8 *to_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Juraj Sloboda68b7cb82018-10-16 12:18:21 +0200163 len = (rem > max ? max : rem);
Ole Troan4146c652018-08-08 22:23:19 +0200164 if (len != rem) /* Last fragment does not need to divisible by 8 */
165 len &= ~0x7;
Ole Troan282093f2018-09-19 12:38:51 +0200166 if ((to_b = frag_buffer_alloc (org_from_b, &to_bi)) == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500167 {
Ole Troan4146c652018-08-08 22:23:19 +0200168 *error = IP_FRAG_ERROR_MEMORY;
Ole Troan4146c652018-08-08 22:23:19 +0200169 return;
170 }
171 vec_add1 (*buffer, to_bi);
Ole Troan282093f2018-09-19 12:38:51 +0200172 frag_set_sw_if_index (to_b, org_from_b);
Ole Troan4146c652018-08-08 22:23:19 +0200173
Ole Troan282093f2018-09-19 12:38:51 +0200174 /* Copy ip4 header */
Dave Barach178cf492018-11-13 16:34:13 -0500175 clib_memcpy_fast (to_b->data, org_from_packet, sizeof (ip4_header_t));
Ole Troan282093f2018-09-19 12:38:51 +0200176 to_ip4 = vlib_buffer_get_current (to_b);
Ole Troan4146c652018-08-08 22:23:19 +0200177 to_data = (void *) (to_ip4 + 1);
178
Ole Troan7eb9d962018-08-10 14:39:48 +0200179 /* Spin through from buffers filling up the to buffer */
Ole Troan282093f2018-09-19 12:38:51 +0200180 u16 left_in_to_buffer = len, to_ptr = 0;
Ole Troan7eb9d962018-08-10 14:39:48 +0200181 while (1)
Ole Troan4146c652018-08-08 22:23:19 +0200182 {
Ole Troan282093f2018-09-19 12:38:51 +0200183 u16 bytes_to_copy;
184
Ole Troan7eb9d962018-08-10 14:39:48 +0200185 /* Figure out how many bytes we can safely copy */
186 bytes_to_copy = left_in_to_buffer <= left_in_from_buffer ?
187 left_in_to_buffer : left_in_from_buffer;
Dave Barach178cf492018-11-13 16:34:13 -0500188 clib_memcpy_fast (to_data + to_ptr, from_data + ptr, bytes_to_copy);
Ole Troan7eb9d962018-08-10 14:39:48 +0200189 left_in_to_buffer -= bytes_to_copy;
190 ptr += bytes_to_copy;
191 left_in_from_buffer -= bytes_to_copy;
192 if (left_in_to_buffer == 0)
193 break;
Ole Troan4146c652018-08-08 22:23:19 +0200194
Ole Troan282093f2018-09-19 12:38:51 +0200195 ASSERT (left_in_from_buffer <= 0);
Ole Troan4146c652018-08-08 22:23:19 +0200196 /* Move buffer */
197 if (!(from_b->flags & VLIB_BUFFER_NEXT_PRESENT))
198 {
199 *error = IP_FRAG_ERROR_MALFORMED;
200 return;
201 }
202 from_b = vlib_get_buffer (vm, from_b->next_buffer);
203 from_data = (u8 *) vlib_buffer_get_current (from_b);
Ole Troan7eb9d962018-08-10 14:39:48 +0200204 ptr = 0;
205 left_in_from_buffer = from_b->current_length;
206 to_ptr += bytes_to_copy;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500207 }
Ole Troan7eb9d962018-08-10 14:39:48 +0200208
Ole Troan282093f2018-09-19 12:38:51 +0200209 to_b->current_length = len + sizeof (ip4_header_t);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500210
Ole Troan4146c652018-08-08 22:23:19 +0200211 to_ip4->fragment_id = ip_frag_id;
212 to_ip4->flags_and_fragment_offset =
213 clib_host_to_net_u16 ((fo >> 3) + ip_frag_offset);
214 to_ip4->flags_and_fragment_offset |=
Dave Barachd7cb1b52016-12-09 09:52:16 -0500215 clib_host_to_net_u16 (((len != rem) || more) << 13);
Ole Troan4146c652018-08-08 22:23:19 +0200216 to_ip4->length = clib_host_to_net_u16 (len + sizeof (ip4_header_t));
217 to_ip4->checksum = ip4_header_checksum (to_ip4);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500218
Ole Troan4146c652018-08-08 22:23:19 +0200219 if (vnet_buffer (org_from_b)->ip_frag.flags & IP_FRAG_FLAG_IP4_HEADER)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500220 {
Ole Troan4146c652018-08-08 22:23:19 +0200221 /* Encapsulating ipv4 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500222 ip4_header_t *encap_header4 =
Ole Troan4146c652018-08-08 22:23:19 +0200223 (ip4_header_t *) vlib_buffer_get_current (to_b);
224 encap_header4->length = clib_host_to_net_u16 (to_b->current_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500225 encap_header4->checksum = ip4_header_checksum (encap_header4);
226 }
Ole Troan4146c652018-08-08 22:23:19 +0200227 else if (vnet_buffer (org_from_b)->
228 ip_frag.flags & IP_FRAG_FLAG_IP6_HEADER)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500229 {
Ole Troan4146c652018-08-08 22:23:19 +0200230 /* Encapsulating ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500231 ip6_header_t *encap_header6 =
Ole Troan4146c652018-08-08 22:23:19 +0200232 (ip6_header_t *) vlib_buffer_get_current (to_b);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500233 encap_header6->payload_length =
Ole Troan4146c652018-08-08 22:23:19 +0200234 clib_host_to_net_u16 (to_b->current_length -
Dave Barachd7cb1b52016-12-09 09:52:16 -0500235 sizeof (*encap_header6));
236 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500237 rem -= len;
Ole Troan4146c652018-08-08 22:23:19 +0200238 fo += len;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240}
241
Ole Troan9fb87552016-01-13 22:30:43 +0100242void
Ole Troan282093f2018-09-19 12:38:51 +0200243ip_frag_set_vnet_buffer (vlib_buffer_t * b, u16 mtu, u8 next_index, u8 flags)
Ole Troan9fb87552016-01-13 22:30:43 +0100244{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500245 vnet_buffer (b)->ip_frag.mtu = mtu;
246 vnet_buffer (b)->ip_frag.next_index = next_index;
247 vnet_buffer (b)->ip_frag.flags = flags;
Ole Troan9fb87552016-01-13 22:30:43 +0100248}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
Ole Troan282093f2018-09-19 12:38:51 +0200250
251static inline uword
252frag_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
253 vlib_frame_t * frame, u32 node_index, bool is_ip6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254{
255 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Ole Troan282093f2018-09-19 12:38:51 +0200256 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257 from = vlib_frame_vector_args (frame);
258 n_left_from = frame->n_vectors;
259 next_index = node->cached_next_index;
260 u32 frag_sent = 0, small_packets = 0;
261 u32 *buffer = 0;
262
Dave Barachd7cb1b52016-12-09 09:52:16 -0500263 while (n_left_from > 0)
264 {
265 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266
Dave Barachd7cb1b52016-12-09 09:52:16 -0500267 while (n_left_from > 0 && n_left_to_next > 0)
268 {
269 u32 pi0, *frag_from, frag_left;
270 vlib_buffer_t *p0;
271 ip_frag_error_t error0;
Ole Troan282093f2018-09-19 12:38:51 +0200272 int next0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273
Ole Troan282093f2018-09-19 12:38:51 +0200274 /*
275 * Note: The packet is not enqueued now. It is instead put
276 * in a vector where other fragments will be put as well.
277 */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500278 pi0 = from[0];
279 from += 1;
280 n_left_from -= 1;
281 error0 = IP_FRAG_ERROR_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
Dave Barachd7cb1b52016-12-09 09:52:16 -0500283 p0 = vlib_get_buffer (vm, pi0);
Ole Troan282093f2018-09-19 12:38:51 +0200284 if (is_ip6)
285 ip6_frag_do_fragment (vm, pi0, &buffer, &error0);
286 else
287 ip4_frag_do_fragment (vm, pi0, &buffer, &error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Dave Barachd7cb1b52016-12-09 09:52:16 -0500289 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
290 {
291 ip_frag_trace_t *tr =
292 vlib_add_trace (vm, node, p0, sizeof (*tr));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500293 tr->mtu = vnet_buffer (p0)->ip_frag.mtu;
Ole Troan282093f2018-09-19 12:38:51 +0200294 tr->ipv6 = is_ip6 ? 1 : 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500295 tr->n_fragments = vec_len (buffer);
296 tr->next = vnet_buffer (p0)->ip_frag.next_index;
297 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298
Ole Troan282093f2018-09-19 12:38:51 +0200299 if (!is_ip6 && error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500300 {
301 icmp4_error_set_vnet_buffer (p0, ICMP4_destination_unreachable,
302 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
303 vnet_buffer (p0)->ip_frag.mtu);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500304 next0 = IP4_FRAG_NEXT_ICMP_ERROR;
305 }
306 else
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800307 {
Ole Troan282093f2018-09-19 12:38:51 +0200308 if (is_ip6)
309 next0 =
310 (error0 ==
311 IP_FRAG_ERROR_NONE) ? vnet_buffer (p0)->
312 ip_frag.next_index : IP6_FRAG_NEXT_DROP;
313 else
314 next0 =
315 (error0 ==
316 IP_FRAG_ERROR_NONE) ? vnet_buffer (p0)->
317 ip_frag.next_index : IP4_FRAG_NEXT_DROP;
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800318 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
Dave Barachd7cb1b52016-12-09 09:52:16 -0500320 if (error0 == IP_FRAG_ERROR_NONE)
321 {
Ole Troan7eb9d962018-08-10 14:39:48 +0200322 /* Free original buffer chain */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500323 frag_sent += vec_len (buffer);
324 small_packets += (vec_len (buffer) == 1);
Ole Troan282093f2018-09-19 12:38:51 +0200325 vlib_buffer_free_one (vm, pi0); /* Free original packet */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500326 }
327 else
Ole Troan282093f2018-09-19 12:38:51 +0200328 {
329 vlib_error_count (vm, node_index, error0, 1);
330 vec_add1 (buffer, pi0); /* Get rid of the original buffer */
331 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
Ole Troan282093f2018-09-19 12:38:51 +0200333 /* Send fragments that were added in the frame */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500334 frag_from = buffer;
335 frag_left = vec_len (buffer);
336
337 while (frag_left > 0)
338 {
339 while (frag_left > 0 && n_left_to_next > 0)
340 {
341 u32 i;
342 i = to_next[0] = frag_from[0];
343 frag_from += 1;
344 frag_left -= 1;
345 to_next += 1;
346 n_left_to_next -= 1;
347
348 vlib_get_buffer (vm, i)->error = error_node->errors[error0];
349 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
350 to_next, n_left_to_next, i,
351 next0);
352 }
353 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
354 vlib_get_next_frame (vm, node, next_index, to_next,
355 n_left_to_next);
356 }
357 vec_reset_length (buffer);
358 }
359 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500361 vec_free (buffer);
362
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_FRAGMENT_SENT, frag_sent);
Ole Troan282093f2018-09-19 12:38:51 +0200365 vlib_node_increment_counter (vm, node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500366 IP_FRAG_ERROR_SMALL_PACKET, small_packets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367
368 return frame->n_vectors;
369}
370
Dave Barachd7cb1b52016-12-09 09:52:16 -0500371
Ole Troan282093f2018-09-19 12:38:51 +0200372
373static uword
374ip4_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500375{
Ole Troan282093f2018-09-19 12:38:51 +0200376 return frag_node_inline (vm, node, frame, ip4_frag_node.index,
377 0 /* is_ip6 */ );
Dave Barachd7cb1b52016-12-09 09:52:16 -0500378}
379
380static uword
381ip6_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
382{
Ole Troan282093f2018-09-19 12:38:51 +0200383 return frag_node_inline (vm, node, frame, ip6_frag_node.index,
384 1 /* is_ip6 */ );
385}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500386
Ole Troan282093f2018-09-19 12:38:51 +0200387/*
388 * Fragments the packet given in from_bi. Fragments are returned in the buffer vector.
389 * Caller must ensure the original packet is freed.
390 */
391void
392ip6_frag_do_fragment (vlib_main_t * vm, u32 from_bi, u32 ** buffer,
393 ip_frag_error_t * error)
394{
395 vlib_buffer_t *from_b;
396 ip6_header_t *ip6;
397 u16 mtu, len, max, rem, ip_frag_id;
398
399 from_b = vlib_get_buffer (vm, from_bi);
400 mtu = vnet_buffer (from_b)->ip_frag.mtu;
401 ip6 = (ip6_header_t *) vlib_buffer_get_current (from_b);
402
403 rem = clib_net_to_host_u16 (ip6->payload_length);
404 max = (mtu - sizeof (ip6_header_t) - sizeof (ip6_frag_hdr_t)) & ~0x7; // TODO: Is max correct??
405
406 if (rem >
407 (vlib_buffer_length_in_chain (vm, from_b) - sizeof (ip6_header_t)))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500408 {
Ole Troan282093f2018-09-19 12:38:51 +0200409 *error = IP_FRAG_ERROR_MALFORMED;
410 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500411 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500412
Ole Troan282093f2018-09-19 12:38:51 +0200413 /* TODO: Look through header chain for fragmentation header */
414 if (ip6->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
415 {
416 *error = IP_FRAG_ERROR_MALFORMED;
417 return;
418 }
419
420 u8 *from_data = (void *) (ip6 + 1);
421 vlib_buffer_t *org_from_b = from_b;
422 u16 fo = 0;
423 u16 left_in_from_buffer = from_b->current_length - sizeof (ip6_header_t);
424 u16 ptr = 0;
425
426 ip_frag_id = ++running_fragment_id; // Fix
427
428 /* Do the actual fragmentation */
429 while (rem)
430 {
431 u32 to_bi;
432 vlib_buffer_t *to_b;
433 ip6_header_t *to_ip6;
434 ip6_frag_hdr_t *to_frag_hdr;
435 u8 *to_data;
436
437 len =
438 (rem >
439 (mtu - sizeof (ip6_header_t) - sizeof (ip6_frag_hdr_t)) ? max : rem);
440 if (len != rem) /* Last fragment does not need to divisible by 8 */
441 len &= ~0x7;
442 if ((to_b = frag_buffer_alloc (org_from_b, &to_bi)) == 0)
443 {
444 *error = IP_FRAG_ERROR_MEMORY;
445 return;
446 }
447 vec_add1 (*buffer, to_bi);
448 frag_set_sw_if_index (to_b, org_from_b);
449
450 /* Copy ip6 header */
Dave Barach178cf492018-11-13 16:34:13 -0500451 clib_memcpy_fast (to_b->data, ip6, sizeof (ip6_header_t));
Ole Troan282093f2018-09-19 12:38:51 +0200452 to_ip6 = vlib_buffer_get_current (to_b);
453 to_frag_hdr = (ip6_frag_hdr_t *) (to_ip6 + 1);
454 to_data = (void *) (to_frag_hdr + 1);
455
456 /* Spin through from buffers filling up the to buffer */
457 u16 left_in_to_buffer = len, to_ptr = 0;
458 while (1)
459 {
460 u16 bytes_to_copy;
461
462 /* Figure out how many bytes we can safely copy */
463 bytes_to_copy = left_in_to_buffer <= left_in_from_buffer ?
464 left_in_to_buffer : left_in_from_buffer;
Dave Barach178cf492018-11-13 16:34:13 -0500465 clib_memcpy_fast (to_data + to_ptr, from_data + ptr, bytes_to_copy);
Ole Troan282093f2018-09-19 12:38:51 +0200466 left_in_to_buffer -= bytes_to_copy;
467 ptr += bytes_to_copy;
468 left_in_from_buffer -= bytes_to_copy;
469 if (left_in_to_buffer == 0)
470 break;
471
472 ASSERT (left_in_from_buffer <= 0);
473 /* Move buffer */
474 if (!(from_b->flags & VLIB_BUFFER_NEXT_PRESENT))
475 {
476 *error = IP_FRAG_ERROR_MALFORMED;
477 return;
478 }
479 from_b = vlib_get_buffer (vm, from_b->next_buffer);
480 from_data = (u8 *) vlib_buffer_get_current (from_b);
481 ptr = 0;
482 left_in_from_buffer = from_b->current_length;
483 to_ptr += bytes_to_copy;
484 }
485
486 to_b->current_length =
487 len + sizeof (ip6_header_t) + sizeof (ip6_frag_hdr_t);
488 to_ip6->payload_length =
489 clib_host_to_net_u16 (len + sizeof (ip6_frag_hdr_t));
490 to_ip6->protocol = IP_PROTOCOL_IPV6_FRAGMENTATION;
491 to_frag_hdr->fragment_offset_and_more =
492 ip6_frag_hdr_offset_and_more ((fo >> 3), len != rem);
493 to_frag_hdr->identification = ip_frag_id;
494 to_frag_hdr->next_hdr = ip6->protocol;
495 to_frag_hdr->rsv = 0;
496
497 rem -= len;
498 fo += len;
499 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500500}
501
502static char *ip4_frag_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503#define _(sym,string) string,
504 foreach_ip_frag_error
505#undef _
506};
507
Dave Barachd7cb1b52016-12-09 09:52:16 -0500508/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509VLIB_REGISTER_NODE (ip4_frag_node) = {
510 .function = ip4_frag,
511 .name = IP4_FRAG_NODE_NAME,
512 .vector_size = sizeof (u32),
513 .format_trace = format_ip_frag_trace,
514 .type = VLIB_NODE_TYPE_INTERNAL,
515
516 .n_errors = IP_FRAG_N_ERROR,
517 .error_strings = ip4_frag_error_strings,
518
519 .n_next_nodes = IP4_FRAG_N_NEXT,
520 .next_nodes = {
Ole Troanb3655e52018-08-16 22:08:49 +0200521 [IP4_FRAG_NEXT_IP4_REWRITE] = "ip4-rewrite",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522 [IP4_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup",
523 [IP4_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup",
Ole Troan9fb87552016-01-13 22:30:43 +0100524 [IP4_FRAG_NEXT_ICMP_ERROR] = "ip4-icmp-error",
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800525 [IP4_FRAG_NEXT_DROP] = "ip4-drop"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526 },
527};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500528/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
Dave Barachd7cb1b52016-12-09 09:52:16 -0500530/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531VLIB_REGISTER_NODE (ip6_frag_node) = {
532 .function = ip6_frag,
533 .name = IP6_FRAG_NODE_NAME,
534 .vector_size = sizeof (u32),
535 .format_trace = format_ip_frag_trace,
536 .type = VLIB_NODE_TYPE_INTERNAL,
537
538 .n_errors = IP_FRAG_N_ERROR,
539 .error_strings = ip4_frag_error_strings,
540
541 .n_next_nodes = IP6_FRAG_N_NEXT,
542 .next_nodes = {
Ole Troanb3655e52018-08-16 22:08:49 +0200543 [IP6_FRAG_NEXT_IP6_REWRITE] = "ip6-rewrite",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 [IP6_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup",
545 [IP6_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup",
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800546 [IP6_FRAG_NEXT_DROP] = "ip6-drop"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 },
548};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500549/* *INDENT-ON* */
550
551/*
552 * fd.io coding-style-patch-verification: ON
553 *
554 * Local Variables:
555 * eval: (c-set-style "gnu")
556 * End:
557 */