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]; |
Vijayabhaskar Katamreddy | 64fb995 | 2018-11-28 12:05:23 -0800 | [diff] [blame] | 64 | |
| 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 Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static vlib_buffer_t * |
| 74 | frag_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 Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 90 | /* |
| 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 Katamreddy | e95d1a1 | 2018-06-28 11:08:29 -0700 | [diff] [blame] | 96 | void |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 97 | 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] | 98 | ip_frag_error_t * error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | { |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 100 | vlib_buffer_t *from_b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | ip4_header_t *ip4; |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 102 | u16 mtu, len, max, rem, ip_frag_id, ip_frag_offset; |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 103 | u8 *org_from_packet, more; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 105 | from_b = vlib_get_buffer (vm, from_bi); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 106 | mtu = vnet_buffer (from_b)->ip_frag.mtu; |
| 107 | org_from_packet = vlib_buffer_get_current (from_b); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 108 | ip4 = (ip4_header_t *) vlib_buffer_get_current (from_b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 110 | rem = clib_net_to_host_u16 (ip4->length) - sizeof (ip4_header_t); |
Juraj Sloboda | 68b7cb8 | 2018-10-16 12:18:21 +0200 | [diff] [blame] | 111 | max = |
| 112 | (clib_min (mtu, VLIB_BUFFER_DATA_SIZE) - sizeof (ip4_header_t)) & ~0x7; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 114 | if (rem > |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 115 | (vlib_buffer_length_in_chain (vm, from_b) - sizeof (ip4_header_t))) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 116 | { |
| 117 | *error = IP_FRAG_ERROR_MALFORMED; |
| 118 | return; |
| 119 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 121 | if (mtu < sizeof (ip4_header_t)) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 122 | { |
| 123 | *error = IP_FRAG_ERROR_CANT_FRAGMENT_HEADER; |
| 124 | return; |
| 125 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
| 127 | if (ip4->flags_and_fragment_offset & |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 128 | clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT)) |
| 129 | { |
| 130 | *error = IP_FRAG_ERROR_DONT_FRAGMENT_SET; |
| 131 | return; |
| 132 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 134 | 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 Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 139 | !(!(ip4->flags_and_fragment_offset & |
| 140 | clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS))); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 141 | } |
| 142 | else |
| 143 | { |
| 144 | ip_frag_id = (++running_fragment_id); |
| 145 | ip_frag_offset = 0; |
| 146 | more = 0; |
| 147 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 149 | u8 *from_data = (void *) (ip4 + 1); |
| 150 | vlib_buffer_t *org_from_b = from_b; |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 151 | u16 fo = 0; |
| 152 | u16 left_in_from_buffer = from_b->current_length - sizeof (ip4_header_t); |
| 153 | u16 ptr = 0; |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 154 | |
| 155 | /* Do the actual fragmentation */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 156 | while (rem) |
| 157 | { |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 158 | u32 to_bi; |
| 159 | vlib_buffer_t *to_b; |
| 160 | ip4_header_t *to_ip4; |
| 161 | u8 *to_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | |
Juraj Sloboda | 68b7cb8 | 2018-10-16 12:18:21 +0200 | [diff] [blame] | 163 | len = (rem > max ? max : rem); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 164 | if (len != rem) /* Last fragment does not need to divisible by 8 */ |
| 165 | len &= ~0x7; |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 166 | if ((to_b = frag_buffer_alloc (org_from_b, &to_bi)) == 0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 167 | { |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 168 | *error = IP_FRAG_ERROR_MEMORY; |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | vec_add1 (*buffer, to_bi); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 172 | frag_set_sw_if_index (to_b, org_from_b); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 173 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 174 | /* Copy ip4 header */ |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 175 | clib_memcpy_fast (to_b->data, org_from_packet, sizeof (ip4_header_t)); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 176 | to_ip4 = vlib_buffer_get_current (to_b); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 177 | to_data = (void *) (to_ip4 + 1); |
| 178 | |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 179 | /* Spin through from buffers filling up the to buffer */ |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 180 | u16 left_in_to_buffer = len, to_ptr = 0; |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 181 | while (1) |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 182 | { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 183 | u16 bytes_to_copy; |
| 184 | |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 185 | /* 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 Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 188 | clib_memcpy_fast (to_data + to_ptr, from_data + ptr, bytes_to_copy); |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 189 | 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 Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 194 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 195 | ASSERT (left_in_from_buffer <= 0); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 196 | /* 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 Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 204 | ptr = 0; |
| 205 | left_in_from_buffer = from_b->current_length; |
| 206 | to_ptr += bytes_to_copy; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 207 | } |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 208 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 209 | to_b->current_length = len + sizeof (ip4_header_t); |
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 | 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 Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 215 | clib_host_to_net_u16 (((len != rem) || more) << 13); |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 216 | to_ip4->length = clib_host_to_net_u16 (len + sizeof (ip4_header_t)); |
| 217 | to_ip4->checksum = ip4_header_checksum (to_ip4); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 218 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 219 | 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] | 220 | { |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 221 | /* Encapsulating ipv4 header */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 222 | ip4_header_t *encap_header4 = |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 223 | (ip4_header_t *) vlib_buffer_get_current (to_b); |
| 224 | encap_header4->length = clib_host_to_net_u16 (to_b->current_length); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 225 | encap_header4->checksum = ip4_header_checksum (encap_header4); |
| 226 | } |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 227 | else if (vnet_buffer (org_from_b)-> |
| 228 | ip_frag.flags & IP_FRAG_FLAG_IP6_HEADER) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 229 | { |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 230 | /* Encapsulating ipv6 header */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 231 | ip6_header_t *encap_header6 = |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 232 | (ip6_header_t *) vlib_buffer_get_current (to_b); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 233 | encap_header6->payload_length = |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 234 | clib_host_to_net_u16 (to_b->current_length - |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 235 | sizeof (*encap_header6)); |
| 236 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 237 | rem -= len; |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 238 | fo += len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Ole Troan | 9fb8755 | 2016-01-13 22:30:43 +0100 | [diff] [blame] | 242 | void |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 243 | 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] | 244 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 245 | 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 Troan | 9fb8755 | 2016-01-13 22:30:43 +0100 | [diff] [blame] | 248 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 250 | |
| 251 | static inline uword |
| 252 | frag_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 253 | vlib_frame_t * frame, u32 node_index, bool is_ip6) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | { |
| 255 | 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] | 256 | 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] | 257 | 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 Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 263 | while (n_left_from > 0) |
| 264 | { |
| 265 | 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] | 266 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 267 | 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 Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 272 | int next0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 274 | /* |
| 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 Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 278 | pi0 = from[0]; |
| 279 | from += 1; |
| 280 | n_left_from -= 1; |
| 281 | error0 = IP_FRAG_ERROR_NONE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 283 | p0 = vlib_get_buffer (vm, pi0); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 284 | if (is_ip6) |
| 285 | ip6_frag_do_fragment (vm, pi0, &buffer, &error0); |
| 286 | else |
| 287 | ip4_frag_do_fragment (vm, pi0, &buffer, &error0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 289 | 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 Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 293 | tr->mtu = vnet_buffer (p0)->ip_frag.mtu; |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 294 | tr->ipv6 = is_ip6 ? 1 : 0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 295 | tr->n_fragments = vec_len (buffer); |
| 296 | tr->next = vnet_buffer (p0)->ip_frag.next_index; |
| 297 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 298 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 299 | if (!is_ip6 && error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 300 | { |
| 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 Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 304 | next0 = IP4_FRAG_NEXT_ICMP_ERROR; |
| 305 | } |
| 306 | else |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 307 | { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 308 | 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 Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 318 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 320 | if (error0 == IP_FRAG_ERROR_NONE) |
| 321 | { |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 322 | /* Free original buffer chain */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 323 | frag_sent += vec_len (buffer); |
| 324 | small_packets += (vec_len (buffer) == 1); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 325 | vlib_buffer_free_one (vm, pi0); /* Free original packet */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 326 | } |
| 327 | else |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 328 | { |
| 329 | vlib_error_count (vm, node_index, error0, 1); |
| 330 | vec_add1 (buffer, pi0); /* Get rid of the original buffer */ |
| 331 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 333 | /* Send fragments that were added in the frame */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 334 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 361 | vec_free (buffer); |
| 362 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 363 | vlib_node_increment_counter (vm, node_index, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 364 | IP_FRAG_ERROR_FRAGMENT_SENT, frag_sent); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 365 | vlib_node_increment_counter (vm, node_index, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 366 | IP_FRAG_ERROR_SMALL_PACKET, small_packets); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | |
| 368 | return frame->n_vectors; |
| 369 | } |
| 370 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 371 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 372 | |
| 373 | static uword |
| 374 | 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] | 375 | { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 376 | return frag_node_inline (vm, node, frame, ip4_frag_node.index, |
| 377 | 0 /* is_ip6 */ ); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static uword |
| 381 | ip6_frag (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 382 | { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 383 | return frag_node_inline (vm, node, frame, ip6_frag_node.index, |
| 384 | 1 /* is_ip6 */ ); |
| 385 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 386 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 387 | /* |
| 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 | */ |
| 391 | void |
| 392 | ip6_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 Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 408 | { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 409 | *error = IP_FRAG_ERROR_MALFORMED; |
| 410 | return; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 411 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 412 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 413 | /* 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 Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 451 | clib_memcpy_fast (to_b->data, ip6, sizeof (ip6_header_t)); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 452 | 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 Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 465 | clib_memcpy_fast (to_data + to_ptr, from_data + ptr, bytes_to_copy); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 466 | 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 Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static char *ip4_frag_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | #define _(sym,string) string, |
| 504 | foreach_ip_frag_error |
| 505 | #undef _ |
| 506 | }; |
| 507 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 508 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | VLIB_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 Troan | b3655e5 | 2018-08-16 22:08:49 +0200 | [diff] [blame] | 521 | [IP4_FRAG_NEXT_IP4_REWRITE] = "ip4-rewrite", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | [IP4_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup", |
| 523 | [IP4_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup", |
Ole Troan | 9fb8755 | 2016-01-13 22:30:43 +0100 | [diff] [blame] | 524 | [IP4_FRAG_NEXT_ICMP_ERROR] = "ip4-icmp-error", |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 525 | [IP4_FRAG_NEXT_DROP] = "ip4-drop" |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | }, |
| 527 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 528 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 530 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 531 | VLIB_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 Troan | b3655e5 | 2018-08-16 22:08:49 +0200 | [diff] [blame] | 543 | [IP6_FRAG_NEXT_IP6_REWRITE] = "ip6-rewrite", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 544 | [IP6_FRAG_NEXT_IP4_LOOKUP] = "ip4-lookup", |
| 545 | [IP6_FRAG_NEXT_IP6_LOOKUP] = "ip6-lookup", |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 546 | [IP6_FRAG_NEXT_DROP] = "ip6-drop" |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | }, |
| 548 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 549 | /* *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 | */ |