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