1 /*---------------------------------------------------------------------------
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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 *---------------------------------------------------------------------------
17 * IPv4 Fragmentation Node
24 #include <vnet/ip/ip.h>
37 format_ip_frag_trace (u8 * s, va_list * args)
39 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
40 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
41 ip_frag_trace_t *t = va_arg (*args, ip_frag_trace_t *);
42 s = format (s, "IPv%s offset: %u mtu: %u fragments: %u",
43 t->ipv6 ? "6" : "4", t->header_offset, t->mtu, t->n_fragments);
47 static u32 running_fragment_id;
50 ip4_frag_do_fragment (vlib_main_t * vm, u32 pi, u32 ** buffer,
51 ip_frag_error_t * error)
55 u16 mtu, ptr, len, max, rem, offset, ip_frag_id, ip_frag_offset;
58 vec_add1 (*buffer, pi);
59 p = vlib_get_buffer (vm, pi);
60 offset = vnet_buffer (p)->ip_frag.header_offset;
61 mtu = vnet_buffer (p)->ip_frag.mtu;
62 packet = (u8 *) vlib_buffer_get_current (p);
63 ip4 = (ip4_header_t *) (packet + offset);
65 rem = clib_net_to_host_u16 (ip4->length) - sizeof (*ip4);
67 max = (mtu - sizeof (*ip4) - vnet_buffer (p)->ip_frag.header_offset) & ~0x7;
69 if (rem < (p->current_length - offset - sizeof (*ip4)))
71 *error = IP_FRAG_ERROR_MALFORMED;
75 if (mtu < sizeof (*ip4))
77 *error = IP_FRAG_ERROR_CANT_FRAGMENT_HEADER;
81 if (ip4->flags_and_fragment_offset &
82 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT))
84 *error = IP_FRAG_ERROR_DONT_FRAGMENT_SET;
88 if (ip4_is_fragment (ip4))
90 ip_frag_id = ip4->fragment_id;
91 ip_frag_offset = ip4_get_fragment_offset (ip4);
93 ! !(ip4->flags_and_fragment_offset &
94 clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS));
98 ip_frag_id = (++running_fragment_id);
103 //Do the actual fragmentation
112 (mtu - sizeof (*ip4) -
113 vnet_buffer (p)->ip_frag.header_offset)) ? max : rem;
119 fip4 = (ip4_header_t *) (vlib_buffer_get_current (b) + offset);
123 if (!vlib_buffer_alloc (vm, &bi, 1))
125 *error = IP_FRAG_ERROR_MEMORY;
128 vec_add1 (*buffer, bi);
129 b = vlib_get_buffer (vm, bi);
130 vnet_buffer (b)->sw_if_index[VLIB_RX] =
131 vnet_buffer (p)->sw_if_index[VLIB_RX];
132 vnet_buffer (b)->sw_if_index[VLIB_TX] =
133 vnet_buffer (p)->sw_if_index[VLIB_TX];
134 fip4 = (ip4_header_t *) (vlib_buffer_get_current (b) + offset);
136 //Copy offset and ip4 header
137 clib_memcpy (b->data, packet, offset + sizeof (*ip4));
139 clib_memcpy (((u8 *) (fip4)) + sizeof (*fip4),
140 packet + offset + sizeof (*fip4) + ptr, len);
142 b->current_length = offset + len + sizeof (*fip4);
144 fip4->fragment_id = ip_frag_id;
145 fip4->flags_and_fragment_offset =
146 clib_host_to_net_u16 ((ptr >> 3) + ip_frag_offset);
147 fip4->flags_and_fragment_offset |=
148 clib_host_to_net_u16 (((len != rem) || more) << 13);
149 // ((len0 != rem0) || more0) << 13 is optimization for
150 // ((len0 != rem0) || more0) ? IP4_HEADER_FLAG_MORE_FRAGMENTS : 0
151 fip4->length = clib_host_to_net_u16 (len + sizeof (*fip4));
152 fip4->checksum = ip4_header_checksum (fip4);
154 if (vnet_buffer (p)->ip_frag.flags & IP_FRAG_FLAG_IP4_HEADER)
156 //Encapsulating ipv4 header
157 ip4_header_t *encap_header4 =
158 (ip4_header_t *) vlib_buffer_get_current (b);
159 encap_header4->length = clib_host_to_net_u16 (b->current_length);
160 encap_header4->checksum = ip4_header_checksum (encap_header4);
162 else if (vnet_buffer (p)->ip_frag.flags & IP_FRAG_FLAG_IP6_HEADER)
164 //Encapsulating ipv6 header
165 ip6_header_t *encap_header6 =
166 (ip6_header_t *) vlib_buffer_get_current (b);
167 encap_header6->payload_length =
168 clib_host_to_net_u16 (b->current_length -
169 sizeof (*encap_header6));
178 ip_frag_set_vnet_buffer (vlib_buffer_t * b, u16 offset, u16 mtu,
179 u8 next_index, u8 flags)
181 vnet_buffer (b)->ip_frag.header_offset = offset;
182 vnet_buffer (b)->ip_frag.mtu = mtu;
183 vnet_buffer (b)->ip_frag.next_index = next_index;
184 vnet_buffer (b)->ip_frag.flags = flags;
188 ip4_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
190 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
191 vlib_node_runtime_t *error_node =
192 vlib_node_get_runtime (vm, ip4_frag_node.index);
193 from = vlib_frame_vector_args (frame);
194 n_left_from = frame->n_vectors;
195 next_index = node->cached_next_index;
196 u32 frag_sent = 0, small_packets = 0;
199 while (n_left_from > 0)
201 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
203 while (n_left_from > 0 && n_left_to_next > 0)
205 u32 pi0, *frag_from, frag_left;
207 ip_frag_error_t error0;
208 ip4_frag_next_t next0;
210 //Note: The packet is not enqueued now.
211 //It is instead put in a vector where other fragments
212 //will be put as well.
216 error0 = IP_FRAG_ERROR_NONE;
218 p0 = vlib_get_buffer (vm, pi0);
219 ip4_frag_do_fragment (vm, pi0, &buffer, &error0);
221 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
223 ip_frag_trace_t *tr =
224 vlib_add_trace (vm, node, p0, sizeof (*tr));
225 tr->header_offset = vnet_buffer (p0)->ip_frag.header_offset;
226 tr->mtu = vnet_buffer (p0)->ip_frag.mtu;
228 tr->n_fragments = vec_len (buffer);
229 tr->next = vnet_buffer (p0)->ip_frag.next_index;
232 if (error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET)
234 icmp4_error_set_vnet_buffer (p0, ICMP4_destination_unreachable,
235 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
236 vnet_buffer (p0)->ip_frag.mtu);
237 vlib_buffer_advance (p0,
238 vnet_buffer (p0)->ip_frag.header_offset);
239 next0 = IP4_FRAG_NEXT_ICMP_ERROR;
244 IP_FRAG_ERROR_NONE) ? vnet_buffer (p0)->
245 ip_frag.next_index : IP4_FRAG_NEXT_DROP;
247 if (error0 == IP_FRAG_ERROR_NONE)
249 frag_sent += vec_len (buffer);
250 small_packets += (vec_len (buffer) == 1);
253 vlib_error_count (vm, ip4_frag_node.index, error0, 1);
255 //Send fragments that were added in the frame
257 frag_left = vec_len (buffer);
259 while (frag_left > 0)
261 while (frag_left > 0 && n_left_to_next > 0)
264 i = to_next[0] = frag_from[0];
270 vlib_get_buffer (vm, i)->error = error_node->errors[error0];
271 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
272 to_next, n_left_to_next, i,
275 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
276 vlib_get_next_frame (vm, node, next_index, to_next,
279 vec_reset_length (buffer);
281 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
285 vlib_node_increment_counter (vm, ip4_frag_node.index,
286 IP_FRAG_ERROR_FRAGMENT_SENT, frag_sent);
287 vlib_node_increment_counter (vm, ip4_frag_node.index,
288 IP_FRAG_ERROR_SMALL_PACKET, small_packets);
290 return frame->n_vectors;
295 ip6_frag_do_fragment (vlib_main_t * vm, u32 pi, u32 ** buffer,
296 ip_frag_error_t * error)
299 ip6_header_t *ip6_hdr;
300 ip6_frag_hdr_t *frag_hdr;
301 u8 *payload, *next_header;
303 p = vlib_get_buffer (vm, pi);
305 //Parsing the IPv6 headers
307 vlib_buffer_get_current (p) + vnet_buffer (p)->ip_frag.header_offset;
308 payload = (u8 *) (ip6_hdr + 1);
309 next_header = &ip6_hdr->protocol;
310 if (*next_header == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS)
312 next_header = payload;
313 payload += payload[1] * 8;
316 if (*next_header == IP_PROTOCOL_IP6_DESTINATION_OPTIONS)
318 next_header = payload;
319 payload += payload[1] * 8;
322 if (*next_header == IP_PROTOCOL_IPV6_ROUTE)
324 next_header = payload;
325 payload += payload[1] * 8;
329 (payload >= (u8 *) vlib_buffer_get_current (p) + p->current_length))
331 //A malicious packet could set an extension header with a too big size
332 //and make us modify another vlib_buffer
333 *error = IP_FRAG_ERROR_MALFORMED;
339 if (*next_header == IP_PROTOCOL_IPV6_FRAGMENTATION)
341 //The fragmentation header is already there
342 frag_hdr = (ip6_frag_hdr_t *) payload;
343 has_more = ip6_frag_hdr_more (frag_hdr);
344 initial_offset = ip6_frag_hdr_offset (frag_hdr);
348 //Insert a fragmentation header in the packet
349 u8 nh = *next_header;
350 *next_header = IP_PROTOCOL_IPV6_FRAGMENTATION;
351 vlib_buffer_advance (p, -sizeof (*frag_hdr));
352 u8 *start = vlib_buffer_get_current (p);
353 memmove (start, start + sizeof (*frag_hdr),
354 payload - (start + sizeof (*frag_hdr)));
355 frag_hdr = (ip6_frag_hdr_t *) (payload - sizeof (*frag_hdr));
356 frag_hdr->identification = ++running_fragment_id;
357 frag_hdr->next_hdr = nh;
362 payload = (u8 *) (frag_hdr + 1);
364 u16 headers_len = payload - (u8 *) vlib_buffer_get_current (p);
365 u16 max_payload = vnet_buffer (p)->ip_frag.mtu - headers_len;
366 u16 rem = p->current_length - headers_len;
371 *error = IP_FRAG_ERROR_CANT_FRAGMENT_HEADER;
379 u16 len = (rem > max_payload) ? (max_payload & ~0x7) : rem;
384 if (!vlib_buffer_alloc (vm, &bi, 1))
386 *error = IP_FRAG_ERROR_MEMORY;
389 b = vlib_get_buffer (vm, bi);
390 vnet_buffer (b)->sw_if_index[VLIB_RX] =
391 vnet_buffer (p)->sw_if_index[VLIB_RX];
392 vnet_buffer (b)->sw_if_index[VLIB_TX] =
393 vnet_buffer (p)->sw_if_index[VLIB_TX];
394 clib_memcpy (vlib_buffer_get_current (b),
395 vlib_buffer_get_current (p), headers_len);
396 clib_memcpy (vlib_buffer_get_current (b) + headers_len,
399 vlib_buffer_get_current (b) + headers_len - sizeof (*frag_hdr);
404 b = vlib_get_buffer (vm, bi);
405 //frag_hdr already set here
409 vlib_buffer_get_current (b) + vnet_buffer (p)->ip_frag.header_offset;
410 frag_hdr->fragment_offset_and_more =
411 ip6_frag_hdr_offset_and_more (initial_offset + (ptr >> 3),
413 b->current_length = headers_len + len;
414 ip6_hdr->payload_length =
415 clib_host_to_net_u16 (b->current_length -
416 vnet_buffer (p)->ip_frag.header_offset -
419 if (vnet_buffer (p)->ip_frag.flags & IP_FRAG_FLAG_IP4_HEADER)
421 //Encapsulating ipv4 header
422 ip4_header_t *encap_header4 =
423 (ip4_header_t *) vlib_buffer_get_current (b);
424 encap_header4->length = clib_host_to_net_u16 (b->current_length);
425 encap_header4->checksum = ip4_header_checksum (encap_header4);
427 else if (vnet_buffer (p)->ip_frag.flags & IP_FRAG_FLAG_IP6_HEADER)
429 //Encapsulating ipv6 header
430 ip6_header_t *encap_header6 =
431 (ip6_header_t *) vlib_buffer_get_current (b);
432 encap_header6->payload_length =
433 clib_host_to_net_u16 (b->current_length -
434 sizeof (*encap_header6));
437 vec_add1 (*buffer, bi);
444 ip6_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
446 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
447 vlib_node_runtime_t *error_node =
448 vlib_node_get_runtime (vm, ip6_frag_node.index);
449 from = vlib_frame_vector_args (frame);
450 n_left_from = frame->n_vectors;
451 next_index = node->cached_next_index;
452 u32 frag_sent = 0, small_packets = 0;
455 while (n_left_from > 0)
457 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
459 while (n_left_from > 0 && n_left_to_next > 0)
461 u32 pi0, *frag_from, frag_left;
463 ip_frag_error_t error0;
464 ip6_frag_next_t next0;
469 error0 = IP_FRAG_ERROR_NONE;
471 p0 = vlib_get_buffer (vm, pi0);
472 ip6_frag_do_fragment (vm, pi0, &buffer, &error0);
474 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
476 ip_frag_trace_t *tr =
477 vlib_add_trace (vm, node, p0, sizeof (*tr));
478 tr->header_offset = vnet_buffer (p0)->ip_frag.header_offset;
479 tr->mtu = vnet_buffer (p0)->ip_frag.mtu;
481 tr->n_fragments = vec_len (buffer);
482 tr->next = vnet_buffer (p0)->ip_frag.next_index;
487 IP_FRAG_ERROR_NONE) ? vnet_buffer (p0)->
488 ip_frag.next_index : IP6_FRAG_NEXT_DROP;
489 frag_sent += vec_len (buffer);
490 small_packets += (vec_len (buffer) == 1);
492 //Send fragments that were added in the frame
494 frag_left = vec_len (buffer);
495 while (frag_left > 0)
497 while (frag_left > 0 && n_left_to_next > 0)
500 i = to_next[0] = frag_from[0];
506 vlib_get_buffer (vm, i)->error = error_node->errors[error0];
507 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
508 to_next, n_left_to_next, i,
511 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
512 vlib_get_next_frame (vm, node, next_index, to_next,
515 vec_reset_length (buffer);
517 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
520 vlib_node_increment_counter (vm, ip6_frag_node.index,
521 IP_FRAG_ERROR_FRAGMENT_SENT, frag_sent);
522 vlib_node_increment_counter (vm, ip6_frag_node.index,
523 IP_FRAG_ERROR_SMALL_PACKET, small_packets);
525 return frame->n_vectors;
528 static char *ip4_frag_error_strings[] = {
529 #define _(sym,string) string,
530 foreach_ip_frag_error
535 VLIB_REGISTER_NODE (ip4_frag_node) = {
536 .function = ip4_frag,
537 .name = IP4_FRAG_NODE_NAME,
538 .vector_size = sizeof (u32),
539 .format_trace = format_ip_frag_trace,
540 .type = VLIB_NODE_TYPE_INTERNAL,
542 .n_errors = IP_FRAG_N_ERROR,
543 .error_strings = ip4_frag_error_strings,
545 .n_next_nodes = IP4_FRAG_N_NEXT,
547 [IP4_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup",
548 [IP4_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup",
549 [IP4_FRAG_NEXT_ICMP_ERROR] = "ip4-icmp-error",
550 [IP4_FRAG_NEXT_DROP] = "error-drop"
556 VLIB_REGISTER_NODE (ip6_frag_node) = {
557 .function = ip6_frag,
558 .name = IP6_FRAG_NODE_NAME,
559 .vector_size = sizeof (u32),
560 .format_trace = format_ip_frag_trace,
561 .type = VLIB_NODE_TYPE_INTERNAL,
563 .n_errors = IP_FRAG_N_ERROR,
564 .error_strings = ip4_frag_error_strings,
566 .n_next_nodes = IP6_FRAG_N_NEXT,
568 [IP6_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup",
569 [IP6_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup",
570 [IP6_FRAG_NEXT_DROP] = "error-drop"
576 * fd.io coding-style-patch-verification: ON
579 * eval: (c-set-style "gnu")