Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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: |
| 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 Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 27 | typedef struct |
| 28 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | u8 ipv6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | u16 mtu; |
| 31 | u8 next; |
| 32 | u16 n_fragments; |
| 33 | } ip_frag_trace_t; |
| 34 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 35 | static u8 * |
| 36 | format_ip_frag_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | { |
| 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 Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 40 | ip_frag_trace_t *t = va_arg (*args, ip_frag_trace_t *); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 41 | s = format (s, "IPv%s mtu: %u fragments: %u", |
| 42 | t->ipv6 ? "6" : "4", t->mtu, t->n_fragments); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | return s; |
| 44 | } |
| 45 | |
| 46 | static u32 running_fragment_id; |
| 47 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 48 | static void |
| 49 | frag_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 | |
| 66 | static vlib_buffer_t * |
| 67 | frag_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 Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 83 | /* |
| 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 Katamreddy | e95d1a1 | 2018-06-28 11:08:29 -0700 | [diff] [blame] | 89 | void |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 90 | ip4_frag_do_fragment (vlib_main_t * vm, u32 from_bi, u32 ** buffer, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 91 | ip_frag_error_t * error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | { |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 93 | vlib_buffer_t *from_b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | ip4_header_t *ip4; |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 95 | u16 mtu, len, max, rem, ip_frag_id, ip_frag_offset; |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 96 | u8 *org_from_packet, more; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 98 | from_b = vlib_get_buffer (vm, from_bi); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 99 | mtu = vnet_buffer (from_b)->ip_frag.mtu; |
| 100 | org_from_packet = vlib_buffer_get_current (from_b); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 101 | ip4 = (ip4_header_t *) vlib_buffer_get_current (from_b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 103 | rem = clib_net_to_host_u16 (ip4->length) - sizeof (ip4_header_t); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 104 | max = (mtu - sizeof (ip4_header_t)) & ~0x7; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 106 | if (rem > |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 107 | (vlib_buffer_length_in_chain (vm, from_b) - sizeof (ip4_header_t))) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 108 | { |
| 109 | *error = IP_FRAG_ERROR_MALFORMED; |
| 110 | return; |
| 111 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 113 | if (mtu < sizeof (ip4_header_t)) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 114 | { |
| 115 | *error = IP_FRAG_ERROR_CANT_FRAGMENT_HEADER; |
| 116 | return; |
| 117 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
| 119 | if (ip4->flags_and_fragment_offset & |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 120 | clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT)) |
| 121 | { |
| 122 | *error = IP_FRAG_ERROR_DONT_FRAGMENT_SET; |
| 123 | return; |
| 124 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 126 | if (ip4_is_fragment (ip4)) |
| 127 | { |
| 128 | ip_frag_id = ip4->fragment_id; |
| 129 | ip_frag_offset = ip4_get_fragment_offset (ip4); |
| 130 | more = |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 131 | !(!(ip4->flags_and_fragment_offset & |
| 132 | clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS))); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 133 | } |
| 134 | else |
| 135 | { |
| 136 | ip_frag_id = (++running_fragment_id); |
| 137 | ip_frag_offset = 0; |
| 138 | more = 0; |
| 139 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 141 | u8 *from_data = (void *) (ip4 + 1); |
| 142 | vlib_buffer_t *org_from_b = from_b; |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 143 | u16 fo = 0; |
| 144 | u16 left_in_from_buffer = from_b->current_length - sizeof (ip4_header_t); |
| 145 | u16 ptr = 0; |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 146 | |
| 147 | /* Do the actual fragmentation */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 148 | while (rem) |
| 149 | { |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 150 | u32 to_bi; |
| 151 | vlib_buffer_t *to_b; |
| 152 | ip4_header_t *to_ip4; |
| 153 | u8 *to_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 155 | len = (rem > (mtu - sizeof (ip4_header_t)) ? max : rem); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 156 | if (len != rem) /* Last fragment does not need to divisible by 8 */ |
| 157 | len &= ~0x7; |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 158 | if ((to_b = frag_buffer_alloc (org_from_b, &to_bi)) == 0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 159 | { |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 160 | *error = IP_FRAG_ERROR_MEMORY; |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | vec_add1 (*buffer, to_bi); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 164 | frag_set_sw_if_index (to_b, org_from_b); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 165 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 166 | /* Copy ip4 header */ |
| 167 | clib_memcpy (to_b->data, org_from_packet, sizeof (ip4_header_t)); |
| 168 | to_ip4 = vlib_buffer_get_current (to_b); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 169 | to_data = (void *) (to_ip4 + 1); |
| 170 | |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 171 | /* Spin through from buffers filling up the to buffer */ |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 172 | u16 left_in_to_buffer = len, to_ptr = 0; |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 173 | while (1) |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 174 | { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 175 | u16 bytes_to_copy; |
| 176 | |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 177 | /* Figure out how many bytes we can safely copy */ |
| 178 | bytes_to_copy = left_in_to_buffer <= left_in_from_buffer ? |
| 179 | left_in_to_buffer : left_in_from_buffer; |
| 180 | clib_memcpy (to_data + to_ptr, from_data + ptr, bytes_to_copy); |
| 181 | left_in_to_buffer -= bytes_to_copy; |
| 182 | ptr += bytes_to_copy; |
| 183 | left_in_from_buffer -= bytes_to_copy; |
| 184 | if (left_in_to_buffer == 0) |
| 185 | break; |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 186 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 187 | ASSERT (left_in_from_buffer <= 0); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 188 | /* Move buffer */ |
| 189 | if (!(from_b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 190 | { |
| 191 | *error = IP_FRAG_ERROR_MALFORMED; |
| 192 | return; |
| 193 | } |
| 194 | from_b = vlib_get_buffer (vm, from_b->next_buffer); |
| 195 | from_data = (u8 *) vlib_buffer_get_current (from_b); |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 196 | ptr = 0; |
| 197 | left_in_from_buffer = from_b->current_length; |
| 198 | to_ptr += bytes_to_copy; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 199 | } |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 200 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 201 | to_b->current_length = len + sizeof (ip4_header_t); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 202 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 203 | to_ip4->fragment_id = ip_frag_id; |
| 204 | to_ip4->flags_and_fragment_offset = |
| 205 | clib_host_to_net_u16 ((fo >> 3) + ip_frag_offset); |
| 206 | to_ip4->flags_and_fragment_offset |= |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 207 | clib_host_to_net_u16 (((len != rem) || more) << 13); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 208 | to_ip4->length = clib_host_to_net_u16 (len + sizeof (ip4_header_t)); |
| 209 | to_ip4->checksum = ip4_header_checksum (to_ip4); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 210 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 211 | if (vnet_buffer (org_from_b)->ip_frag.flags & IP_FRAG_FLAG_IP4_HEADER) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 212 | { |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 213 | /* Encapsulating ipv4 header */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 214 | ip4_header_t *encap_header4 = |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 215 | (ip4_header_t *) vlib_buffer_get_current (to_b); |
| 216 | encap_header4->length = clib_host_to_net_u16 (to_b->current_length); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 217 | encap_header4->checksum = ip4_header_checksum (encap_header4); |
| 218 | } |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 219 | else if (vnet_buffer (org_from_b)-> |
| 220 | ip_frag.flags & IP_FRAG_FLAG_IP6_HEADER) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 221 | { |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 222 | /* Encapsulating ipv6 header */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 223 | ip6_header_t *encap_header6 = |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 224 | (ip6_header_t *) vlib_buffer_get_current (to_b); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 225 | encap_header6->payload_length = |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 226 | clib_host_to_net_u16 (to_b->current_length - |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 227 | sizeof (*encap_header6)); |
| 228 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 229 | rem -= len; |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 230 | fo += len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Ole Troan | 9fb8755 | 2016-01-13 22:30:43 +0100 | [diff] [blame] | 234 | void |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 235 | ip_frag_set_vnet_buffer (vlib_buffer_t * b, u16 mtu, u8 next_index, u8 flags) |
Ole Troan | 9fb8755 | 2016-01-13 22:30:43 +0100 | [diff] [blame] | 236 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 237 | vnet_buffer (b)->ip_frag.mtu = mtu; |
| 238 | vnet_buffer (b)->ip_frag.next_index = next_index; |
| 239 | vnet_buffer (b)->ip_frag.flags = flags; |
Ole Troan | 9fb8755 | 2016-01-13 22:30:43 +0100 | [diff] [blame] | 240 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 242 | |
| 243 | static inline uword |
| 244 | frag_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 245 | vlib_frame_t * frame, u32 node_index, bool is_ip6) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | { |
| 247 | u32 n_left_from, *from, next_index, *to_next, n_left_to_next; |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 248 | vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | from = vlib_frame_vector_args (frame); |
| 250 | n_left_from = frame->n_vectors; |
| 251 | next_index = node->cached_next_index; |
| 252 | u32 frag_sent = 0, small_packets = 0; |
| 253 | u32 *buffer = 0; |
| 254 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 255 | while (n_left_from > 0) |
| 256 | { |
| 257 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 259 | while (n_left_from > 0 && n_left_to_next > 0) |
| 260 | { |
| 261 | u32 pi0, *frag_from, frag_left; |
| 262 | vlib_buffer_t *p0; |
| 263 | ip_frag_error_t error0; |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 264 | int next0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 266 | /* |
| 267 | * Note: The packet is not enqueued now. It is instead put |
| 268 | * in a vector where other fragments will be put as well. |
| 269 | */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 270 | pi0 = from[0]; |
| 271 | from += 1; |
| 272 | n_left_from -= 1; |
| 273 | error0 = IP_FRAG_ERROR_NONE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 275 | p0 = vlib_get_buffer (vm, pi0); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 276 | if (is_ip6) |
| 277 | ip6_frag_do_fragment (vm, pi0, &buffer, &error0); |
| 278 | else |
| 279 | ip4_frag_do_fragment (vm, pi0, &buffer, &error0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 281 | if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED)) |
| 282 | { |
| 283 | ip_frag_trace_t *tr = |
| 284 | vlib_add_trace (vm, node, p0, sizeof (*tr)); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 285 | tr->mtu = vnet_buffer (p0)->ip_frag.mtu; |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 286 | tr->ipv6 = is_ip6 ? 1 : 0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 287 | tr->n_fragments = vec_len (buffer); |
| 288 | tr->next = vnet_buffer (p0)->ip_frag.next_index; |
| 289 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 290 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 291 | if (!is_ip6 && error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 292 | { |
| 293 | icmp4_error_set_vnet_buffer (p0, ICMP4_destination_unreachable, |
| 294 | ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set, |
| 295 | vnet_buffer (p0)->ip_frag.mtu); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 296 | next0 = IP4_FRAG_NEXT_ICMP_ERROR; |
| 297 | } |
| 298 | else |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 299 | { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 300 | if (is_ip6) |
| 301 | next0 = |
| 302 | (error0 == |
| 303 | IP_FRAG_ERROR_NONE) ? vnet_buffer (p0)-> |
| 304 | ip_frag.next_index : IP6_FRAG_NEXT_DROP; |
| 305 | else |
| 306 | next0 = |
| 307 | (error0 == |
| 308 | IP_FRAG_ERROR_NONE) ? vnet_buffer (p0)-> |
| 309 | ip_frag.next_index : IP4_FRAG_NEXT_DROP; |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 310 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 312 | if (error0 == IP_FRAG_ERROR_NONE) |
| 313 | { |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 314 | /* Free original buffer chain */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 315 | frag_sent += vec_len (buffer); |
| 316 | small_packets += (vec_len (buffer) == 1); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 317 | vlib_buffer_free_one (vm, pi0); /* Free original packet */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 318 | } |
| 319 | else |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 320 | { |
| 321 | vlib_error_count (vm, node_index, error0, 1); |
| 322 | vec_add1 (buffer, pi0); /* Get rid of the original buffer */ |
| 323 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 325 | /* Send fragments that were added in the frame */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 326 | frag_from = buffer; |
| 327 | frag_left = vec_len (buffer); |
| 328 | |
| 329 | while (frag_left > 0) |
| 330 | { |
| 331 | while (frag_left > 0 && n_left_to_next > 0) |
| 332 | { |
| 333 | u32 i; |
| 334 | i = to_next[0] = frag_from[0]; |
| 335 | frag_from += 1; |
| 336 | frag_left -= 1; |
| 337 | to_next += 1; |
| 338 | n_left_to_next -= 1; |
| 339 | |
| 340 | vlib_get_buffer (vm, i)->error = error_node->errors[error0]; |
| 341 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 342 | to_next, n_left_to_next, i, |
| 343 | next0); |
| 344 | } |
| 345 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 346 | vlib_get_next_frame (vm, node, next_index, to_next, |
| 347 | n_left_to_next); |
| 348 | } |
| 349 | vec_reset_length (buffer); |
| 350 | } |
| 351 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 353 | vec_free (buffer); |
| 354 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 355 | vlib_node_increment_counter (vm, node_index, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 356 | IP_FRAG_ERROR_FRAGMENT_SENT, frag_sent); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 357 | vlib_node_increment_counter (vm, node_index, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 358 | IP_FRAG_ERROR_SMALL_PACKET, small_packets); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | |
| 360 | return frame->n_vectors; |
| 361 | } |
| 362 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 363 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 364 | |
| 365 | static uword |
| 366 | ip4_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 367 | { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 368 | return frag_node_inline (vm, node, frame, ip4_frag_node.index, |
| 369 | 0 /* is_ip6 */ ); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static uword |
| 373 | ip6_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 374 | { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 375 | return frag_node_inline (vm, node, frame, ip6_frag_node.index, |
| 376 | 1 /* is_ip6 */ ); |
| 377 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 378 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 379 | /* |
| 380 | * Fragments the packet given in from_bi. Fragments are returned in the buffer vector. |
| 381 | * Caller must ensure the original packet is freed. |
| 382 | */ |
| 383 | void |
| 384 | ip6_frag_do_fragment (vlib_main_t * vm, u32 from_bi, u32 ** buffer, |
| 385 | ip_frag_error_t * error) |
| 386 | { |
| 387 | vlib_buffer_t *from_b; |
| 388 | ip6_header_t *ip6; |
| 389 | u16 mtu, len, max, rem, ip_frag_id; |
| 390 | |
| 391 | from_b = vlib_get_buffer (vm, from_bi); |
| 392 | mtu = vnet_buffer (from_b)->ip_frag.mtu; |
| 393 | ip6 = (ip6_header_t *) vlib_buffer_get_current (from_b); |
| 394 | |
| 395 | rem = clib_net_to_host_u16 (ip6->payload_length); |
| 396 | max = (mtu - sizeof (ip6_header_t) - sizeof (ip6_frag_hdr_t)) & ~0x7; // TODO: Is max correct?? |
| 397 | |
| 398 | if (rem > |
| 399 | (vlib_buffer_length_in_chain (vm, from_b) - sizeof (ip6_header_t))) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 400 | { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 401 | *error = IP_FRAG_ERROR_MALFORMED; |
| 402 | return; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 403 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 404 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 405 | /* TODO: Look through header chain for fragmentation header */ |
| 406 | if (ip6->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION) |
| 407 | { |
| 408 | *error = IP_FRAG_ERROR_MALFORMED; |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | u8 *from_data = (void *) (ip6 + 1); |
| 413 | vlib_buffer_t *org_from_b = from_b; |
| 414 | u16 fo = 0; |
| 415 | u16 left_in_from_buffer = from_b->current_length - sizeof (ip6_header_t); |
| 416 | u16 ptr = 0; |
| 417 | |
| 418 | ip_frag_id = ++running_fragment_id; // Fix |
| 419 | |
| 420 | /* Do the actual fragmentation */ |
| 421 | while (rem) |
| 422 | { |
| 423 | u32 to_bi; |
| 424 | vlib_buffer_t *to_b; |
| 425 | ip6_header_t *to_ip6; |
| 426 | ip6_frag_hdr_t *to_frag_hdr; |
| 427 | u8 *to_data; |
| 428 | |
| 429 | len = |
| 430 | (rem > |
| 431 | (mtu - sizeof (ip6_header_t) - sizeof (ip6_frag_hdr_t)) ? max : rem); |
| 432 | if (len != rem) /* Last fragment does not need to divisible by 8 */ |
| 433 | len &= ~0x7; |
| 434 | if ((to_b = frag_buffer_alloc (org_from_b, &to_bi)) == 0) |
| 435 | { |
| 436 | *error = IP_FRAG_ERROR_MEMORY; |
| 437 | return; |
| 438 | } |
| 439 | vec_add1 (*buffer, to_bi); |
| 440 | frag_set_sw_if_index (to_b, org_from_b); |
| 441 | |
| 442 | /* Copy ip6 header */ |
| 443 | clib_memcpy (to_b->data, ip6, sizeof (ip6_header_t)); |
| 444 | to_ip6 = vlib_buffer_get_current (to_b); |
| 445 | to_frag_hdr = (ip6_frag_hdr_t *) (to_ip6 + 1); |
| 446 | to_data = (void *) (to_frag_hdr + 1); |
| 447 | |
| 448 | /* Spin through from buffers filling up the to buffer */ |
| 449 | u16 left_in_to_buffer = len, to_ptr = 0; |
| 450 | while (1) |
| 451 | { |
| 452 | u16 bytes_to_copy; |
| 453 | |
| 454 | /* Figure out how many bytes we can safely copy */ |
| 455 | bytes_to_copy = left_in_to_buffer <= left_in_from_buffer ? |
| 456 | left_in_to_buffer : left_in_from_buffer; |
| 457 | clib_memcpy (to_data + to_ptr, from_data + ptr, bytes_to_copy); |
| 458 | left_in_to_buffer -= bytes_to_copy; |
| 459 | ptr += bytes_to_copy; |
| 460 | left_in_from_buffer -= bytes_to_copy; |
| 461 | if (left_in_to_buffer == 0) |
| 462 | break; |
| 463 | |
| 464 | ASSERT (left_in_from_buffer <= 0); |
| 465 | /* Move buffer */ |
| 466 | if (!(from_b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 467 | { |
| 468 | *error = IP_FRAG_ERROR_MALFORMED; |
| 469 | return; |
| 470 | } |
| 471 | from_b = vlib_get_buffer (vm, from_b->next_buffer); |
| 472 | from_data = (u8 *) vlib_buffer_get_current (from_b); |
| 473 | ptr = 0; |
| 474 | left_in_from_buffer = from_b->current_length; |
| 475 | to_ptr += bytes_to_copy; |
| 476 | } |
| 477 | |
| 478 | to_b->current_length = |
| 479 | len + sizeof (ip6_header_t) + sizeof (ip6_frag_hdr_t); |
| 480 | to_ip6->payload_length = |
| 481 | clib_host_to_net_u16 (len + sizeof (ip6_frag_hdr_t)); |
| 482 | to_ip6->protocol = IP_PROTOCOL_IPV6_FRAGMENTATION; |
| 483 | to_frag_hdr->fragment_offset_and_more = |
| 484 | ip6_frag_hdr_offset_and_more ((fo >> 3), len != rem); |
| 485 | to_frag_hdr->identification = ip_frag_id; |
| 486 | to_frag_hdr->next_hdr = ip6->protocol; |
| 487 | to_frag_hdr->rsv = 0; |
| 488 | |
| 489 | rem -= len; |
| 490 | fo += len; |
| 491 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static char *ip4_frag_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 495 | #define _(sym,string) string, |
| 496 | foreach_ip_frag_error |
| 497 | #undef _ |
| 498 | }; |
| 499 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 500 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 501 | VLIB_REGISTER_NODE (ip4_frag_node) = { |
| 502 | .function = ip4_frag, |
| 503 | .name = IP4_FRAG_NODE_NAME, |
| 504 | .vector_size = sizeof (u32), |
| 505 | .format_trace = format_ip_frag_trace, |
| 506 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 507 | |
| 508 | .n_errors = IP_FRAG_N_ERROR, |
| 509 | .error_strings = ip4_frag_error_strings, |
| 510 | |
| 511 | .n_next_nodes = IP4_FRAG_N_NEXT, |
| 512 | .next_nodes = { |
Ole Troan | b3655e5 | 2018-08-16 22:08:49 +0200 | [diff] [blame] | 513 | [IP4_FRAG_NEXT_IP4_REWRITE] = "ip4-rewrite", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | [IP4_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup", |
| 515 | [IP4_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup", |
Ole Troan | 9fb8755 | 2016-01-13 22:30:43 +0100 | [diff] [blame] | 516 | [IP4_FRAG_NEXT_ICMP_ERROR] = "ip4-icmp-error", |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 517 | [IP4_FRAG_NEXT_DROP] = "ip4-drop" |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 518 | }, |
| 519 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 520 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 522 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | VLIB_REGISTER_NODE (ip6_frag_node) = { |
| 524 | .function = ip6_frag, |
| 525 | .name = IP6_FRAG_NODE_NAME, |
| 526 | .vector_size = sizeof (u32), |
| 527 | .format_trace = format_ip_frag_trace, |
| 528 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 529 | |
| 530 | .n_errors = IP_FRAG_N_ERROR, |
| 531 | .error_strings = ip4_frag_error_strings, |
| 532 | |
| 533 | .n_next_nodes = IP6_FRAG_N_NEXT, |
| 534 | .next_nodes = { |
Ole Troan | b3655e5 | 2018-08-16 22:08:49 +0200 | [diff] [blame] | 535 | [IP6_FRAG_NEXT_IP6_REWRITE] = "ip6-rewrite", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | [IP6_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup", |
| 537 | [IP6_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup", |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 538 | [IP6_FRAG_NEXT_DROP] = "ip6-drop" |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 539 | }, |
| 540 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 541 | /* *INDENT-ON* */ |
| 542 | |
| 543 | /* |
| 544 | * fd.io coding-style-patch-verification: ON |
| 545 | * |
| 546 | * Local Variables: |
| 547 | * eval: (c-set-style "gnu") |
| 548 | * End: |
| 549 | */ |