Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | * @file |
| 18 | * @brief IPv6 Reassembly. |
| 19 | * |
| 20 | * This file contains the source code for IPv6 reassembly. |
| 21 | */ |
| 22 | |
| 23 | #include <vppinfra/vec.h> |
| 24 | #include <vnet/vnet.h> |
| 25 | #include <vnet/ip/ip.h> |
| 26 | #include <vppinfra/bihash_48_8.h> |
| 27 | #include <vnet/ip/ip6_reassembly.h> |
| 28 | |
| 29 | #define MSEC_PER_SEC 1000 |
| 30 | #define IP6_REASS_TIMEOUT_DEFAULT_MS 100 |
| 31 | #define IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 32 | #define IP6_REASS_MAX_REASSEMBLIES_DEFAULT 1024 |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 33 | #define IP6_REASS_HT_LOAD_FACTOR (0.75) |
| 34 | |
| 35 | static vlib_node_registration_t ip6_reass_node; |
| 36 | |
| 37 | typedef struct |
| 38 | { |
| 39 | union |
| 40 | { |
| 41 | struct |
| 42 | { |
| 43 | ip6_address_t src; |
| 44 | ip6_address_t dst; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 45 | u32 xx_id; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 46 | u32 frag_id; |
Klement Sekera | 8dcfed5 | 2018-06-28 11:16:15 +0200 | [diff] [blame] | 47 | u8 unused[7]; |
| 48 | u8 proto; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 49 | }; |
| 50 | u64 as_u64[6]; |
| 51 | }; |
| 52 | } ip6_reass_key_t; |
| 53 | |
| 54 | always_inline u32 |
| 55 | ip6_reass_buffer_get_data_offset_no_check (vlib_buffer_t * b) |
| 56 | { |
| 57 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 58 | return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first; |
| 59 | } |
| 60 | |
| 61 | always_inline u32 |
| 62 | ip6_reass_buffer_get_data_offset (vlib_buffer_t * b) |
| 63 | { |
| 64 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 65 | ASSERT (vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first); |
| 66 | return ip6_reass_buffer_get_data_offset_no_check (b); |
| 67 | } |
| 68 | |
| 69 | always_inline u16 |
| 70 | ip6_reass_buffer_get_data_len_no_check (vlib_buffer_t * b) |
| 71 | { |
| 72 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 73 | return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) - |
| 74 | (vnb->ip.reass.fragment_first + ip6_reass_buffer_get_data_offset (b)) + 1; |
| 75 | } |
| 76 | |
| 77 | always_inline u16 |
| 78 | ip6_reass_buffer_get_data_len (vlib_buffer_t * b) |
| 79 | { |
| 80 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 81 | ASSERT (vnb->ip.reass.range_last > vnb->ip.reass.fragment_first); |
| 82 | return ip6_reass_buffer_get_data_len_no_check (b); |
| 83 | } |
| 84 | |
| 85 | typedef struct |
| 86 | { |
| 87 | // hash table key |
| 88 | ip6_reass_key_t key; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 89 | // time when last packet was received |
| 90 | f64 last_heard; |
| 91 | // internal id of this reassembly |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 92 | u64 id; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 93 | // buffer index of first buffer in this reassembly context |
| 94 | u32 first_bi; |
| 95 | // last octet of packet, ~0 until fragment without more_fragments arrives |
| 96 | u32 last_packet_octet; |
| 97 | // length of data collected so far |
| 98 | u32 data_len; |
| 99 | // trace operation counter |
| 100 | u32 trace_op_counter; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 101 | // next index - used by non-feature node |
| 102 | u8 next_index; |
| 103 | // minimum fragment length for this reassembly - used to estimate MTU |
| 104 | u16 min_fragment_length; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 105 | } ip6_reass_t; |
| 106 | |
| 107 | typedef struct |
| 108 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 109 | ip6_reass_t *pool; |
| 110 | u32 reass_n; |
| 111 | u32 buffers_n; |
| 112 | u32 id_counter; |
| 113 | clib_spinlock_t lock; |
| 114 | } ip6_reass_per_thread_t; |
| 115 | |
| 116 | typedef struct |
| 117 | { |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 118 | // IPv6 config |
| 119 | u32 timeout_ms; |
| 120 | f64 timeout; |
| 121 | u32 expire_walk_interval_ms; |
| 122 | u32 max_reass_n; |
| 123 | |
| 124 | // IPv6 runtime |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 125 | clib_bihash_48_8_t hash; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 126 | |
| 127 | // per-thread data |
| 128 | ip6_reass_per_thread_t *per_thread_data; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 129 | |
| 130 | // convenience |
| 131 | vlib_main_t *vlib_main; |
| 132 | vnet_main_t *vnet_main; |
| 133 | |
| 134 | // node index of ip6-drop node |
| 135 | u32 ip6_drop_idx; |
| 136 | u32 ip6_icmp_error_idx; |
| 137 | u32 ip6_reass_expire_node_idx; |
| 138 | |
| 139 | } ip6_reass_main_t; |
| 140 | |
| 141 | ip6_reass_main_t ip6_reass_main; |
| 142 | |
| 143 | typedef enum |
| 144 | { |
| 145 | IP6_REASSEMBLY_NEXT_INPUT, |
| 146 | IP6_REASSEMBLY_NEXT_DROP, |
| 147 | IP6_REASSEMBLY_NEXT_ICMP_ERROR, |
| 148 | IP6_REASSEMBLY_N_NEXT, |
| 149 | } ip6_reass_next_t; |
| 150 | |
| 151 | typedef enum |
| 152 | { |
| 153 | RANGE_NEW, |
| 154 | RANGE_OVERLAP, |
| 155 | ICMP_ERROR_RT_EXCEEDED, |
| 156 | ICMP_ERROR_FL_TOO_BIG, |
| 157 | ICMP_ERROR_FL_NOT_MULT_8, |
| 158 | FINALIZE, |
| 159 | } ip6_reass_trace_operation_e; |
| 160 | |
| 161 | typedef struct |
| 162 | { |
| 163 | u16 range_first; |
| 164 | u16 range_last; |
| 165 | u32 range_bi; |
| 166 | i32 data_offset; |
| 167 | u32 data_len; |
| 168 | u32 first_bi; |
| 169 | } ip6_reass_range_trace_t; |
| 170 | |
| 171 | typedef struct |
| 172 | { |
| 173 | ip6_reass_trace_operation_e action; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 174 | u32 reass_id; |
| 175 | ip6_reass_range_trace_t trace_range; |
| 176 | u32 size_diff; |
| 177 | u32 op_id; |
| 178 | u32 fragment_first; |
| 179 | u32 fragment_last; |
| 180 | u32 total_data_len; |
| 181 | } ip6_reass_trace_t; |
| 182 | |
| 183 | static void |
| 184 | ip6_reass_trace_details (vlib_main_t * vm, u32 bi, |
| 185 | ip6_reass_range_trace_t * trace) |
| 186 | { |
| 187 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 188 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 189 | trace->range_first = vnb->ip.reass.range_first; |
| 190 | trace->range_last = vnb->ip.reass.range_last; |
| 191 | trace->data_offset = ip6_reass_buffer_get_data_offset_no_check (b); |
| 192 | trace->data_len = ip6_reass_buffer_get_data_len_no_check (b); |
| 193 | trace->range_bi = bi; |
| 194 | } |
| 195 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 196 | static u8 * |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 197 | format_ip6_reass_range_trace (u8 * s, va_list * args) |
| 198 | { |
| 199 | ip6_reass_range_trace_t *trace = va_arg (*args, ip6_reass_range_trace_t *); |
| 200 | s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first, |
| 201 | trace->range_last, trace->data_offset, trace->data_len, |
| 202 | trace->range_bi); |
| 203 | return s; |
| 204 | } |
| 205 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 206 | static u8 * |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 207 | format_ip6_reass_trace (u8 * s, va_list * args) |
| 208 | { |
| 209 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 210 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 211 | ip6_reass_trace_t *t = va_arg (*args, ip6_reass_trace_t *); |
| 212 | s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id); |
| 213 | u32 indent = format_get_indent (s); |
| 214 | s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]", |
| 215 | t->trace_range.first_bi, t->total_data_len, t->fragment_first, |
| 216 | t->fragment_last); |
| 217 | switch (t->action) |
| 218 | { |
| 219 | case RANGE_NEW: |
| 220 | s = format (s, "\n%Unew %U", format_white_space, indent, |
| 221 | format_ip6_reass_range_trace, &t->trace_range); |
| 222 | break; |
| 223 | case RANGE_OVERLAP: |
| 224 | s = format (s, "\n%Uoverlap %U", format_white_space, indent, |
| 225 | format_ip6_reass_range_trace, &t->trace_range); |
| 226 | break; |
| 227 | case ICMP_ERROR_FL_TOO_BIG: |
| 228 | s = format (s, "\n%Uicmp-error - frag_len > 65535 %U", |
| 229 | format_white_space, indent, format_ip6_reass_range_trace, |
| 230 | &t->trace_range); |
| 231 | break; |
| 232 | case ICMP_ERROR_FL_NOT_MULT_8: |
| 233 | s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U", |
| 234 | format_white_space, indent, format_ip6_reass_range_trace, |
| 235 | &t->trace_range); |
| 236 | break; |
| 237 | case ICMP_ERROR_RT_EXCEEDED: |
| 238 | s = format (s, "\n%Uicmp-error - reassembly time exceeded", |
| 239 | format_white_space, indent); |
| 240 | break; |
| 241 | case FINALIZE: |
| 242 | s = format (s, "\n%Ufinalize reassembly", format_white_space, indent); |
| 243 | break; |
| 244 | } |
| 245 | return s; |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | ip6_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 250 | ip6_reass_main_t * rm, ip6_reass_t * reass, |
| 251 | u32 bi, ip6_reass_trace_operation_e action, |
| 252 | u32 size_diff) |
| 253 | { |
| 254 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 255 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 256 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, b->trace_index)) |
| 257 | { |
| 258 | // this buffer's trace is gone |
| 259 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 260 | return; |
| 261 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 262 | ip6_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0])); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 263 | t->reass_id = reass->id; |
| 264 | t->action = action; |
| 265 | ip6_reass_trace_details (vm, bi, &t->trace_range); |
| 266 | t->size_diff = size_diff; |
| 267 | t->op_id = reass->trace_op_counter; |
| 268 | ++reass->trace_op_counter; |
| 269 | t->fragment_first = vnb->ip.reass.fragment_first; |
| 270 | t->fragment_last = vnb->ip.reass.fragment_last; |
| 271 | t->trace_range.first_bi = reass->first_bi; |
| 272 | t->total_data_len = reass->data_len; |
| 273 | #if 0 |
| 274 | static u8 *s = NULL; |
| 275 | s = format (s, "%U", format_ip6_reass_trace, NULL, NULL, t); |
| 276 | printf ("%.*s\n", vec_len (s), s); |
| 277 | fflush (stdout); |
| 278 | vec_reset_length (s); |
| 279 | #endif |
| 280 | } |
| 281 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 282 | always_inline void |
| 283 | ip6_reass_free (ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt, |
| 284 | ip6_reass_t * reass) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 285 | { |
| 286 | clib_bihash_kv_48_8_t kv; |
| 287 | kv.key[0] = reass->key.as_u64[0]; |
| 288 | kv.key[1] = reass->key.as_u64[1]; |
| 289 | kv.key[2] = reass->key.as_u64[2]; |
| 290 | kv.key[3] = reass->key.as_u64[3]; |
| 291 | kv.key[4] = reass->key.as_u64[4]; |
| 292 | kv.key[5] = reass->key.as_u64[5]; |
| 293 | clib_bihash_add_del_48_8 (&rm->hash, &kv, 0); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 294 | pool_put (rt->pool, reass); |
| 295 | --rt->reass_n; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 298 | always_inline void |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 299 | ip6_reass_drop_all (vlib_main_t * vm, ip6_reass_main_t * rm, |
| 300 | ip6_reass_t * reass, u32 ** vec_drop_bi) |
| 301 | { |
| 302 | u32 range_bi = reass->first_bi; |
| 303 | vlib_buffer_t *range_b; |
| 304 | vnet_buffer_opaque_t *range_vnb; |
| 305 | while (~0 != range_bi) |
| 306 | { |
| 307 | range_b = vlib_get_buffer (vm, range_bi); |
| 308 | range_vnb = vnet_buffer (range_b); |
| 309 | u32 bi = range_bi; |
| 310 | while (~0 != bi) |
| 311 | { |
| 312 | vec_add1 (*vec_drop_bi, bi); |
| 313 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 314 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 315 | { |
| 316 | bi = b->next_buffer; |
| 317 | b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | bi = ~0; |
| 322 | } |
| 323 | } |
| 324 | range_bi = range_vnb->ip.reass.next_range_bi; |
| 325 | } |
| 326 | } |
| 327 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 328 | always_inline void |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 329 | ip6_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 330 | ip6_reass_main_t * rm, ip6_reass_t * reass, |
| 331 | u32 * icmp_bi, u32 ** vec_timeout) |
| 332 | { |
| 333 | if (~0 == reass->first_bi) |
| 334 | { |
| 335 | return; |
| 336 | } |
| 337 | vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi); |
| 338 | if (0 == vnet_buffer (b)->ip.reass.fragment_first) |
| 339 | { |
| 340 | *icmp_bi = reass->first_bi; |
| 341 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 342 | { |
| 343 | ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, |
| 344 | ICMP_ERROR_RT_EXCEEDED, 0); |
| 345 | } |
| 346 | // fragment with offset zero received - send icmp message back |
| 347 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 348 | { |
| 349 | // separate first buffer from chain and steer it towards icmp node |
| 350 | b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 351 | reass->first_bi = b->next_buffer; |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi; |
| 356 | } |
| 357 | icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded, |
| 358 | ICMP6_time_exceeded_fragment_reassembly_time_exceeded, |
| 359 | 0); |
| 360 | } |
| 361 | ip6_reass_drop_all (vm, rm, reass, vec_timeout); |
| 362 | } |
| 363 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 364 | always_inline ip6_reass_t * |
| 365 | ip6_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 366 | ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt, |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 367 | ip6_reass_key_t * k, u32 * icmp_bi, |
| 368 | u32 ** vec_timeout) |
| 369 | { |
| 370 | ip6_reass_t *reass = NULL; |
| 371 | f64 now = vlib_time_now (rm->vlib_main); |
| 372 | clib_bihash_kv_48_8_t kv, value; |
| 373 | kv.key[0] = k->as_u64[0]; |
| 374 | kv.key[1] = k->as_u64[1]; |
| 375 | kv.key[2] = k->as_u64[2]; |
| 376 | kv.key[3] = k->as_u64[3]; |
| 377 | kv.key[4] = k->as_u64[4]; |
| 378 | kv.key[5] = k->as_u64[5]; |
| 379 | |
| 380 | if (!clib_bihash_search_48_8 (&rm->hash, &kv, &value)) |
| 381 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 382 | reass = pool_elt_at_index (rt->pool, value.value); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 383 | if (now > reass->last_heard + rm->timeout) |
| 384 | { |
| 385 | ip6_reass_on_timeout (vm, node, rm, reass, icmp_bi, vec_timeout); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 386 | ip6_reass_free (rm, rt, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 387 | reass = NULL; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (reass) |
| 392 | { |
| 393 | reass->last_heard = now; |
| 394 | return reass; |
| 395 | } |
| 396 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 397 | if (rt->reass_n >= rm->max_reass_n) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 398 | { |
| 399 | reass = NULL; |
| 400 | return reass; |
| 401 | } |
| 402 | else |
| 403 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 404 | pool_get (rt->pool, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 405 | memset (reass, 0, sizeof (*reass)); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 406 | reass->id = |
| 407 | ((u64) os_get_thread_index () * 1000000000) + rt->id_counter; |
| 408 | ++rt->id_counter; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 409 | reass->first_bi = ~0; |
| 410 | reass->last_packet_octet = ~0; |
| 411 | reass->data_len = 0; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 412 | ++rt->reass_n; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | reass->key.as_u64[0] = kv.key[0] = k->as_u64[0]; |
| 416 | reass->key.as_u64[1] = kv.key[1] = k->as_u64[1]; |
| 417 | reass->key.as_u64[2] = kv.key[2] = k->as_u64[2]; |
| 418 | reass->key.as_u64[3] = kv.key[3] = k->as_u64[3]; |
| 419 | reass->key.as_u64[4] = kv.key[4] = k->as_u64[4]; |
| 420 | reass->key.as_u64[5] = kv.key[5] = k->as_u64[5]; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 421 | kv.value = reass - rt->pool; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 422 | reass->last_heard = now; |
| 423 | |
| 424 | if (clib_bihash_add_del_48_8 (&rm->hash, &kv, 1)) |
| 425 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 426 | ip6_reass_free (rm, rt, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 427 | reass = NULL; |
| 428 | } |
| 429 | |
| 430 | return reass; |
| 431 | } |
| 432 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 433 | always_inline void |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 434 | ip6_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node, |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 435 | ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt, |
| 436 | ip6_reass_t * reass, u32 * bi0, u32 * next0, |
Damjan Marion | aa682a3 | 2018-04-26 22:45:40 +0200 | [diff] [blame] | 437 | u32 * error0, u32 ** vec_drop_compress, bool is_feature) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 438 | { |
| 439 | ASSERT (~0 != reass->first_bi); |
| 440 | *bi0 = reass->first_bi; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 441 | *error0 = IP6_ERROR_NONE; |
| 442 | ip6_frag_hdr_t *frag_hdr; |
| 443 | vlib_buffer_t *last_b = NULL; |
| 444 | u32 sub_chain_bi = reass->first_bi; |
| 445 | u32 total_length = 0; |
| 446 | u32 buf_cnt = 0; |
| 447 | u32 dropped_cnt = 0; |
| 448 | do |
| 449 | { |
| 450 | u32 tmp_bi = sub_chain_bi; |
| 451 | vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi); |
| 452 | u32 data_len = ip6_reass_buffer_get_data_len (tmp); |
| 453 | u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset + |
| 454 | sizeof (*frag_hdr) + ip6_reass_buffer_get_data_offset (tmp); |
| 455 | u32 trim_end = |
| 456 | vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len; |
| 457 | if (tmp_bi == reass->first_bi) |
| 458 | { |
| 459 | /* first buffer - keep ip6 header */ |
| 460 | ASSERT (0 == ip6_reass_buffer_get_data_offset (tmp)); |
| 461 | trim_front = 0; |
| 462 | trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len - |
| 463 | (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset + |
| 464 | sizeof (*frag_hdr)); |
Klement Sekera | 56e6f09 | 2018-02-02 16:17:55 +0100 | [diff] [blame] | 465 | ASSERT (vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 466 | } |
| 467 | u32 keep_data = |
| 468 | vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end; |
| 469 | while (1) |
| 470 | { |
| 471 | ++buf_cnt; |
| 472 | if (trim_front) |
| 473 | { |
| 474 | if (trim_front > tmp->current_length) |
| 475 | { |
| 476 | /* drop whole buffer */ |
| 477 | vec_add1 (*vec_drop_compress, tmp_bi); |
| 478 | ++dropped_cnt; |
| 479 | trim_front -= tmp->current_length; |
| 480 | ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 481 | tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 482 | tmp_bi = tmp->next_buffer; |
| 483 | tmp = vlib_get_buffer (vm, tmp_bi); |
| 484 | continue; |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | vlib_buffer_advance (tmp, trim_front); |
| 489 | trim_front = 0; |
| 490 | } |
| 491 | } |
| 492 | if (keep_data) |
| 493 | { |
| 494 | if (last_b) |
| 495 | { |
| 496 | last_b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 497 | last_b->next_buffer = tmp_bi; |
| 498 | } |
| 499 | last_b = tmp; |
| 500 | if (keep_data <= tmp->current_length) |
| 501 | { |
| 502 | tmp->current_length = keep_data; |
| 503 | keep_data = 0; |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | keep_data -= tmp->current_length; |
| 508 | ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 509 | } |
| 510 | total_length += tmp->current_length; |
| 511 | } |
| 512 | else |
| 513 | { |
| 514 | vec_add1 (*vec_drop_compress, tmp_bi); |
| 515 | ASSERT (reass->first_bi != tmp_bi); |
| 516 | ++dropped_cnt; |
| 517 | } |
| 518 | if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 519 | { |
| 520 | tmp_bi = tmp->next_buffer; |
| 521 | tmp = vlib_get_buffer (vm, tmp->next_buffer); |
| 522 | } |
| 523 | else |
| 524 | { |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | sub_chain_bi = |
| 529 | vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip. |
| 530 | reass.next_range_bi; |
| 531 | } |
| 532 | while (~0 != sub_chain_bi); |
Chris Luke | 30684ac | 2018-03-29 12:56:58 -0700 | [diff] [blame] | 533 | |
| 534 | ASSERT (last_b != NULL); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 535 | last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 536 | vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi); |
| 537 | ASSERT (total_length >= first_b->current_length); |
| 538 | total_length -= first_b->current_length; |
| 539 | first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 540 | first_b->total_length_not_including_first_buffer = total_length; |
| 541 | // drop fragment header |
| 542 | vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b); |
| 543 | ip6_header_t *ip = vlib_buffer_get_current (first_b); |
| 544 | u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset; |
| 545 | ip6_ext_header_t *prev_hdr; |
| 546 | ip6_ext_header_find_t (ip, prev_hdr, frag_hdr, |
| 547 | IP_PROTOCOL_IPV6_FRAGMENTATION); |
| 548 | if (prev_hdr) |
| 549 | { |
| 550 | prev_hdr->next_hdr = frag_hdr->next_hdr; |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | ip->protocol = frag_hdr->next_hdr; |
| 555 | } |
| 556 | ASSERT ((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset); |
| 557 | memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr), |
| 558 | first_b->current_length - ip6_frag_hdr_offset - |
| 559 | sizeof (ip6_frag_hdr_t)); |
| 560 | first_b->current_length -= sizeof (*frag_hdr); |
| 561 | ip->payload_length = |
| 562 | clib_host_to_net_u16 (total_length + first_b->current_length - |
| 563 | sizeof (*ip)); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 564 | vlib_buffer_chain_compress (vm, first_b, vec_drop_compress); |
| 565 | if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED)) |
| 566 | { |
| 567 | ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0); |
| 568 | #if 0 |
| 569 | // following code does a hexdump of packet fragments to stdout ... |
| 570 | do |
| 571 | { |
| 572 | u32 bi = reass->first_bi; |
| 573 | u8 *s = NULL; |
| 574 | while (~0 != bi) |
| 575 | { |
| 576 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 577 | s = format (s, "%u: %U\n", bi, format_hexdump, |
| 578 | vlib_buffer_get_current (b), b->current_length); |
| 579 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 580 | { |
| 581 | bi = b->next_buffer; |
| 582 | } |
| 583 | else |
| 584 | { |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | printf ("%.*s\n", vec_len (s), s); |
| 589 | fflush (stdout); |
| 590 | vec_free (s); |
| 591 | } |
| 592 | while (0); |
| 593 | #endif |
| 594 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 595 | if (is_feature) |
| 596 | { |
| 597 | *next0 = IP6_REASSEMBLY_NEXT_INPUT; |
| 598 | } |
| 599 | else |
| 600 | { |
| 601 | *next0 = reass->next_index; |
| 602 | } |
| 603 | vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length; |
| 604 | ip6_reass_free (rm, rt, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 605 | reass = NULL; |
| 606 | } |
| 607 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 608 | always_inline u32 |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 609 | ip6_reass_get_buffer_chain_length (vlib_main_t * vm, vlib_buffer_t * b) |
| 610 | { |
| 611 | u32 len = 0; |
| 612 | while (b) |
| 613 | { |
| 614 | ++len; |
| 615 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 616 | { |
| 617 | b = vlib_get_buffer (vm, b->next_buffer); |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | break; |
| 622 | } |
| 623 | } |
| 624 | return len; |
| 625 | } |
| 626 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 627 | always_inline void |
| 628 | ip6_reass_insert_range_in_chain (vlib_main_t * vm, ip6_reass_main_t * rm, |
| 629 | ip6_reass_per_thread_t * rt, |
| 630 | ip6_reass_t * reass, u32 prev_range_bi, |
| 631 | u32 new_next_bi) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 632 | { |
| 633 | |
| 634 | vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi); |
| 635 | vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b); |
| 636 | if (~0 != prev_range_bi) |
| 637 | { |
| 638 | vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi); |
| 639 | vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b); |
| 640 | new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi; |
| 641 | prev_vnb->ip.reass.next_range_bi = new_next_bi; |
| 642 | } |
| 643 | else |
| 644 | { |
| 645 | if (~0 != reass->first_bi) |
| 646 | { |
| 647 | new_next_vnb->ip.reass.next_range_bi = reass->first_bi; |
| 648 | } |
| 649 | reass->first_bi = new_next_bi; |
| 650 | } |
| 651 | reass->data_len += ip6_reass_buffer_get_data_len (new_next_b); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 652 | rt->buffers_n += ip6_reass_get_buffer_chain_length (vm, new_next_b); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 653 | } |
| 654 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 655 | always_inline void |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 656 | ip6_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node, |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 657 | ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt, |
| 658 | ip6_reass_t * reass, u32 * bi0, u32 * next0, |
Damjan Marion | aa682a3 | 2018-04-26 22:45:40 +0200 | [diff] [blame] | 659 | u32 * error0, ip6_frag_hdr_t * frag_hdr, |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 660 | u32 ** vec_drop_overlap, u32 ** vec_drop_compress, |
| 661 | bool is_feature) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 662 | { |
| 663 | int consumed = 0; |
| 664 | vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0); |
| 665 | vnet_buffer_opaque_t *fvnb = vnet_buffer (fb); |
Vijayabhaskar Katamreddy | b361076 | 2018-06-29 05:03:40 -0700 | [diff] [blame] | 666 | reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 667 | fvnb->ip.reass.ip6_frag_hdr_offset = |
| 668 | (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb); |
| 669 | ip6_header_t *fip = vlib_buffer_get_current (fb); |
| 670 | ASSERT (fb->current_length > sizeof (*fip)); |
| 671 | ASSERT (fvnb->ip.reass.ip6_frag_hdr_offset > 0 && |
| 672 | fvnb->ip.reass.ip6_frag_hdr_offset < fb->current_length); |
| 673 | u32 fragment_first = fvnb->ip.reass.fragment_first = |
| 674 | ip6_frag_hdr_offset_bytes (frag_hdr); |
| 675 | u32 fragment_length = |
| 676 | vlib_buffer_length_in_chain (vm, fb) - |
| 677 | (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr)); |
| 678 | u32 fragment_last = fvnb->ip.reass.fragment_last = |
| 679 | fragment_first + fragment_length - 1; |
| 680 | int more_fragments = ip6_frag_hdr_more (frag_hdr); |
| 681 | u32 candidate_range_bi = reass->first_bi; |
| 682 | u32 prev_range_bi = ~0; |
| 683 | fvnb->ip.reass.range_first = fragment_first; |
| 684 | fvnb->ip.reass.range_last = fragment_last; |
| 685 | fvnb->ip.reass.next_range_bi = ~0; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 686 | if (!more_fragments) |
| 687 | { |
| 688 | reass->last_packet_octet = fragment_last; |
| 689 | } |
| 690 | if (~0 == reass->first_bi) |
| 691 | { |
| 692 | // starting a new reassembly |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 693 | ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi, |
| 694 | *bi0); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 695 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 696 | { |
| 697 | ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0); |
| 698 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 699 | reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 700 | *bi0 = ~0; |
| 701 | return; |
| 702 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 703 | reass->min_fragment_length = |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 704 | clib_min (clib_net_to_host_u16 (fip->payload_length), |
| 705 | fvnb->ip.reass.estimated_mtu); |
| 706 | while (~0 != candidate_range_bi) |
| 707 | { |
| 708 | vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi); |
| 709 | vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b); |
| 710 | if (fragment_first > candidate_vnb->ip.reass.range_last) |
| 711 | { |
| 712 | // this fragments starts after candidate range |
| 713 | prev_range_bi = candidate_range_bi; |
| 714 | candidate_range_bi = candidate_vnb->ip.reass.next_range_bi; |
| 715 | if (candidate_vnb->ip.reass.range_last < fragment_last && |
| 716 | ~0 == candidate_range_bi) |
| 717 | { |
| 718 | // special case - this fragment falls beyond all known ranges |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 719 | ip6_reass_insert_range_in_chain (vm, rm, rt, reass, |
| 720 | prev_range_bi, *bi0); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 721 | consumed = 1; |
| 722 | break; |
| 723 | } |
| 724 | continue; |
| 725 | } |
| 726 | if (fragment_last < candidate_vnb->ip.reass.range_first) |
| 727 | { |
| 728 | // this fragment ends before candidate range without any overlap |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 729 | ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi, |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 730 | *bi0); |
| 731 | consumed = 1; |
| 732 | } |
| 733 | else if (fragment_first == candidate_vnb->ip.reass.range_first && |
| 734 | fragment_last == candidate_vnb->ip.reass.range_last) |
| 735 | { |
| 736 | // duplicate fragment - ignore |
| 737 | } |
| 738 | else |
| 739 | { |
| 740 | // overlapping fragment - not allowed by RFC 8200 |
| 741 | ip6_reass_drop_all (vm, rm, reass, vec_drop_overlap); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 742 | ip6_reass_free (rm, rt, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 743 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 744 | { |
| 745 | ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP, |
| 746 | 0); |
| 747 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 748 | *next0 = IP6_REASSEMBLY_NEXT_DROP; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 749 | *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT; |
| 750 | } |
| 751 | break; |
| 752 | } |
| 753 | if (consumed) |
| 754 | { |
| 755 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 756 | { |
| 757 | ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0); |
| 758 | } |
| 759 | } |
| 760 | if (~0 != reass->last_packet_octet && |
| 761 | reass->data_len == reass->last_packet_octet + 1) |
| 762 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 763 | ip6_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0, |
| 764 | vec_drop_compress, is_feature); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 765 | } |
| 766 | else |
| 767 | { |
| 768 | if (consumed) |
| 769 | { |
| 770 | *bi0 = ~0; |
| 771 | } |
| 772 | else |
| 773 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 774 | *next0 = IP6_REASSEMBLY_NEXT_DROP; |
| 775 | ; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 776 | *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 781 | always_inline bool |
| 782 | ip6_reass_verify_upper_layer_present (vlib_node_runtime_t * node, |
| 783 | vlib_buffer_t * b, |
| 784 | ip6_frag_hdr_t * frag_hdr) |
| 785 | { |
| 786 | ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr; |
| 787 | while (ip6_ext_hdr (tmp->next_hdr)) |
| 788 | { |
| 789 | tmp = ip6_ext_next_header (tmp); |
| 790 | } |
| 791 | if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr) |
| 792 | { |
| 793 | icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem, |
| 794 | ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, |
| 795 | 0); |
| 796 | b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER]; |
| 797 | |
| 798 | return false; |
| 799 | } |
| 800 | return true; |
| 801 | } |
| 802 | |
| 803 | always_inline bool |
| 804 | ip6_reass_verify_fragment_multiple_8 (vlib_main_t * vm, |
| 805 | vlib_node_runtime_t * node, |
| 806 | vlib_buffer_t * b, |
| 807 | ip6_frag_hdr_t * frag_hdr) |
| 808 | { |
| 809 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 810 | ip6_header_t *ip = vlib_buffer_get_current (b); |
| 811 | int more_fragments = ip6_frag_hdr_more (frag_hdr); |
| 812 | u32 fragment_length = |
| 813 | vlib_buffer_length_in_chain (vm, b) - |
| 814 | (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr)); |
| 815 | if (more_fragments && 0 != fragment_length % 8) |
| 816 | { |
| 817 | icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem, |
| 818 | ICMP6_parameter_problem_erroneous_header_field, |
| 819 | (u8 *) & ip->payload_length - (u8 *) ip); |
| 820 | return false; |
| 821 | } |
| 822 | return true; |
| 823 | } |
| 824 | |
| 825 | always_inline bool |
| 826 | ip6_reass_verify_packet_size_lt_64k (vlib_main_t * vm, |
| 827 | vlib_node_runtime_t * node, |
| 828 | vlib_buffer_t * b, |
| 829 | ip6_frag_hdr_t * frag_hdr) |
| 830 | { |
| 831 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 832 | u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr); |
| 833 | u32 fragment_length = |
| 834 | vlib_buffer_length_in_chain (vm, b) - |
| 835 | (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr)); |
| 836 | if (fragment_first + fragment_length > 65535) |
| 837 | { |
| 838 | ip6_header_t *ip0 = vlib_buffer_get_current (b); |
| 839 | icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem, |
| 840 | ICMP6_parameter_problem_erroneous_header_field, |
| 841 | (u8 *) & frag_hdr->fragment_offset_and_more |
| 842 | - (u8 *) ip0); |
| 843 | return false; |
| 844 | } |
| 845 | return true; |
| 846 | } |
| 847 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 848 | always_inline uword |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 849 | ip6_reassembly_inline (vlib_main_t * vm, |
| 850 | vlib_node_runtime_t * node, |
| 851 | vlib_frame_t * frame, bool is_feature) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 852 | { |
| 853 | u32 *from = vlib_frame_vector_args (frame); |
| 854 | u32 n_left_from, n_left_to_next, *to_next, next_index; |
| 855 | ip6_reass_main_t *rm = &ip6_reass_main; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 856 | ip6_reass_per_thread_t *rt = &rm->per_thread_data[os_get_thread_index ()]; |
| 857 | clib_spinlock_lock (&rt->lock); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 858 | |
| 859 | n_left_from = frame->n_vectors; |
| 860 | next_index = node->cached_next_index; |
| 861 | static u32 *vec_timeout = NULL; // indexes of buffers which timed out |
| 862 | static u32 *vec_drop_overlap = NULL; // indexes of buffers dropped due to overlap |
| 863 | static u32 *vec_drop_compress = NULL; // indexes of buffers dropped due to buffer compression |
| 864 | while (n_left_from > 0 || vec_len (vec_timeout) > 0 || |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 865 | vec_len (vec_drop_overlap) > 0 || vec_len (vec_drop_compress) > 0) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 866 | { |
| 867 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 868 | |
| 869 | while (vec_len (vec_timeout) > 0 && n_left_to_next > 0) |
| 870 | { |
| 871 | u32 bi = vec_pop (vec_timeout); |
| 872 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 873 | b->error = node->errors[IP6_ERROR_REASS_TIMEOUT]; |
| 874 | to_next[0] = bi; |
| 875 | to_next += 1; |
| 876 | n_left_to_next -= 1; |
| 877 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 878 | n_left_to_next, bi, |
| 879 | IP6_REASSEMBLY_NEXT_DROP); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 880 | ASSERT (rt->buffers_n > 0); |
| 881 | --rt->buffers_n; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | while (vec_len (vec_drop_overlap) > 0 && n_left_to_next > 0) |
| 885 | { |
| 886 | u32 bi = vec_pop (vec_drop_overlap); |
| 887 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 888 | b->error = node->errors[IP6_ERROR_REASS_OVERLAPPING_FRAGMENT]; |
| 889 | to_next[0] = bi; |
| 890 | to_next += 1; |
| 891 | n_left_to_next -= 1; |
| 892 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 893 | n_left_to_next, bi, |
| 894 | IP6_REASSEMBLY_NEXT_DROP); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 895 | ASSERT (rt->buffers_n > 0); |
| 896 | --rt->buffers_n; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | while (vec_len (vec_drop_compress) > 0 && n_left_to_next > 0) |
| 900 | { |
| 901 | u32 bi = vec_pop (vec_drop_compress); |
| 902 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 903 | b->error = node->errors[IP6_ERROR_NONE]; |
| 904 | to_next[0] = bi; |
| 905 | to_next += 1; |
| 906 | n_left_to_next -= 1; |
| 907 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 908 | n_left_to_next, bi, |
| 909 | IP6_REASSEMBLY_NEXT_DROP); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 910 | ASSERT (rt->buffers_n > 0); |
| 911 | --rt->buffers_n; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | while (n_left_from > 0 && n_left_to_next > 0) |
| 915 | { |
| 916 | u32 bi0; |
| 917 | vlib_buffer_t *b0; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 918 | u32 next0; |
| 919 | u32 error0 = IP6_ERROR_NONE; |
| 920 | u32 icmp_bi = ~0; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 921 | |
| 922 | bi0 = from[0]; |
| 923 | b0 = vlib_get_buffer (vm, bi0); |
| 924 | |
| 925 | ip6_header_t *ip0 = vlib_buffer_get_current (b0); |
Klement Sekera | 3ecc221 | 2018-03-27 10:34:43 +0200 | [diff] [blame] | 926 | ip6_frag_hdr_t *frag_hdr = NULL; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 927 | ip6_ext_header_t *prev_hdr; |
Klement Sekera | 3ecc221 | 2018-03-27 10:34:43 +0200 | [diff] [blame] | 928 | if (ip6_ext_hdr (ip0->protocol)) |
| 929 | { |
| 930 | ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr, |
| 931 | IP_PROTOCOL_IPV6_FRAGMENTATION); |
| 932 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 933 | if (!frag_hdr) |
| 934 | { |
| 935 | // this is a regular packet - no fragmentation |
| 936 | next0 = IP6_REASSEMBLY_NEXT_INPUT; |
| 937 | goto skip_reass; |
| 938 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 939 | if (0 == ip6_frag_hdr_offset (frag_hdr)) |
| 940 | { |
| 941 | // first fragment - verify upper-layer is present |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 942 | if (!ip6_reass_verify_upper_layer_present (node, b0, frag_hdr)) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 943 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 944 | next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR; |
| 945 | goto skip_reass; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 946 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 947 | } |
| 948 | if (!ip6_reass_verify_fragment_multiple_8 (vm, node, b0, frag_hdr) |
| 949 | || !ip6_reass_verify_packet_size_lt_64k (vm, node, b0, |
| 950 | frag_hdr)) |
| 951 | { |
| 952 | next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR; |
| 953 | goto skip_reass; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 954 | } |
| 955 | vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset = |
| 956 | (u8 *) frag_hdr - (u8 *) ip0; |
| 957 | |
| 958 | ip6_reass_key_t k; |
Klement Sekera | 8dcfed5 | 2018-06-28 11:16:15 +0200 | [diff] [blame] | 959 | k.as_u64[0] = ip0->src_address.as_u64[0]; |
| 960 | k.as_u64[1] = ip0->src_address.as_u64[1]; |
| 961 | k.as_u64[2] = ip0->dst_address.as_u64[0]; |
| 962 | k.as_u64[3] = ip0->dst_address.as_u64[1]; |
| 963 | k.as_u64[4] = |
| 964 | (u64) vnet_buffer (b0)-> |
| 965 | sw_if_index[VLIB_RX] << 32 | frag_hdr->identification; |
| 966 | k.as_u64[5] = ip0->protocol; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 967 | ip6_reass_t *reass = |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 968 | ip6_reass_find_or_create (vm, node, rm, rt, &k, &icmp_bi, |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 969 | &vec_timeout); |
| 970 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 971 | if (reass) |
| 972 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 973 | ip6_reass_update (vm, node, rm, rt, reass, &bi0, &next0, |
| 974 | &error0, frag_hdr, &vec_drop_overlap, |
| 975 | &vec_drop_compress, is_feature); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 976 | } |
| 977 | else |
| 978 | { |
| 979 | next0 = IP6_REASSEMBLY_NEXT_DROP; |
| 980 | error0 = IP6_ERROR_REASS_LIMIT_REACHED; |
| 981 | } |
| 982 | |
| 983 | b0->error = node->errors[error0]; |
| 984 | |
| 985 | if (~0 != bi0) |
| 986 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 987 | skip_reass: |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 988 | to_next[0] = bi0; |
| 989 | to_next += 1; |
| 990 | n_left_to_next -= 1; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 991 | if (is_feature && IP6_ERROR_NONE == error0) |
| 992 | { |
Damjan Marion | 7d98a12 | 2018-07-19 20:42:08 +0200 | [diff] [blame] | 993 | vnet_feature_next (&next0, b0); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 994 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 995 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 996 | n_left_to_next, bi0, next0); |
| 997 | } |
| 998 | |
| 999 | if (~0 != icmp_bi) |
| 1000 | { |
| 1001 | next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR; |
| 1002 | to_next[0] = icmp_bi; |
| 1003 | to_next += 1; |
| 1004 | n_left_to_next -= 1; |
| 1005 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 1006 | n_left_to_next, icmp_bi, |
| 1007 | next0); |
| 1008 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1009 | from += 1; |
| 1010 | n_left_from -= 1; |
| 1011 | } |
| 1012 | |
| 1013 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 1014 | } |
| 1015 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1016 | clib_spinlock_unlock (&rt->lock); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1017 | return frame->n_vectors; |
| 1018 | } |
| 1019 | |
| 1020 | static char *ip6_reassembly_error_strings[] = { |
| 1021 | #define _(sym, string) string, |
| 1022 | foreach_ip6_error |
| 1023 | #undef _ |
| 1024 | }; |
| 1025 | |
Damjan Marion | 2574d9f | 2018-06-26 14:45:49 +0200 | [diff] [blame] | 1026 | static uword |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1027 | ip6_reassembly (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1028 | vlib_frame_t * frame) |
| 1029 | { |
| 1030 | return ip6_reassembly_inline (vm, node, frame, false /* is_feature */ ); |
| 1031 | } |
| 1032 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1033 | /* *INDENT-OFF* */ |
| 1034 | VLIB_REGISTER_NODE (ip6_reass_node, static) = { |
| 1035 | .function = ip6_reassembly, |
| 1036 | .name = "ip6-reassembly", |
| 1037 | .vector_size = sizeof (u32), |
| 1038 | .format_trace = format_ip6_reass_trace, |
| 1039 | .n_errors = ARRAY_LEN (ip6_reassembly_error_strings), |
| 1040 | .error_strings = ip6_reassembly_error_strings, |
| 1041 | .n_next_nodes = IP6_REASSEMBLY_N_NEXT, |
| 1042 | .next_nodes = |
| 1043 | { |
| 1044 | [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input", |
| 1045 | [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop", |
| 1046 | [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error", |
| 1047 | }, |
| 1048 | }; |
| 1049 | /* *INDENT-ON* */ |
| 1050 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1051 | VLIB_NODE_FUNCTION_MULTIARCH (ip6_reass_node, ip6_reassembly); |
| 1052 | |
Damjan Marion | 2574d9f | 2018-06-26 14:45:49 +0200 | [diff] [blame] | 1053 | static uword |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1054 | ip6_reassembly_feature (vlib_main_t * vm, |
| 1055 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 1056 | { |
| 1057 | return ip6_reassembly_inline (vm, node, frame, true /* is_feature */ ); |
| 1058 | } |
| 1059 | |
| 1060 | /* *INDENT-OFF* */ |
| 1061 | VLIB_REGISTER_NODE (ip6_reass_node_feature, static) = { |
| 1062 | .function = ip6_reassembly_feature, |
| 1063 | .name = "ip6-reassembly-feature", |
| 1064 | .vector_size = sizeof (u32), |
| 1065 | .format_trace = format_ip6_reass_trace, |
| 1066 | .n_errors = ARRAY_LEN (ip6_reassembly_error_strings), |
| 1067 | .error_strings = ip6_reassembly_error_strings, |
| 1068 | .n_next_nodes = IP6_REASSEMBLY_N_NEXT, |
| 1069 | .next_nodes = |
| 1070 | { |
| 1071 | [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input", |
| 1072 | [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop", |
| 1073 | [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error", |
| 1074 | }, |
| 1075 | }; |
| 1076 | /* *INDENT-ON* */ |
| 1077 | |
| 1078 | VLIB_NODE_FUNCTION_MULTIARCH (ip6_reass_node_feature, ip6_reassembly_feature); |
| 1079 | |
| 1080 | /* *INDENT-OFF* */ |
| 1081 | VNET_FEATURE_INIT (ip6_reassembly_feature, static) = { |
| 1082 | .arc_name = "ip6-unicast", |
| 1083 | .node_name = "ip6-reassembly-feature", |
| 1084 | .runs_before = VNET_FEATURES ("ip6-lookup"), |
| 1085 | .runs_after = 0, |
| 1086 | }; |
| 1087 | /* *INDENT-ON* */ |
| 1088 | |
| 1089 | static u32 |
| 1090 | ip6_reass_get_nbuckets () |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1091 | { |
| 1092 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1093 | u32 nbuckets; |
| 1094 | u8 i; |
| 1095 | |
| 1096 | nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR); |
| 1097 | |
| 1098 | for (i = 0; i < 31; i++) |
| 1099 | if ((1 << i) >= nbuckets) |
| 1100 | break; |
| 1101 | nbuckets = 1 << i; |
| 1102 | |
| 1103 | return nbuckets; |
| 1104 | } |
| 1105 | |
| 1106 | typedef enum |
| 1107 | { |
| 1108 | IP6_EVENT_CONFIG_CHANGED = 1, |
| 1109 | } ip6_reass_event_t; |
| 1110 | |
| 1111 | typedef struct |
| 1112 | { |
| 1113 | int failure; |
| 1114 | clib_bihash_48_8_t *new_hash; |
| 1115 | } ip6_rehash_cb_ctx; |
| 1116 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1117 | static void |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1118 | ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx) |
| 1119 | { |
| 1120 | ip6_rehash_cb_ctx *ctx = _ctx; |
| 1121 | if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1)) |
| 1122 | { |
| 1123 | ctx->failure = 1; |
| 1124 | } |
| 1125 | } |
| 1126 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1127 | static void |
| 1128 | ip6_reass_set_params (u32 timeout_ms, u32 max_reassemblies, |
| 1129 | u32 expire_walk_interval_ms) |
| 1130 | { |
| 1131 | ip6_reass_main.timeout_ms = timeout_ms; |
| 1132 | ip6_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC; |
| 1133 | ip6_reass_main.max_reass_n = max_reassemblies; |
| 1134 | ip6_reass_main.expire_walk_interval_ms = expire_walk_interval_ms; |
| 1135 | } |
| 1136 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1137 | vnet_api_error_t |
| 1138 | ip6_reass_set (u32 timeout_ms, u32 max_reassemblies, |
| 1139 | u32 expire_walk_interval_ms) |
| 1140 | { |
| 1141 | u32 old_nbuckets = ip6_reass_get_nbuckets (); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1142 | ip6_reass_set_params (timeout_ms, max_reassemblies, |
| 1143 | expire_walk_interval_ms); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1144 | vlib_process_signal_event (ip6_reass_main.vlib_main, |
| 1145 | ip6_reass_main.ip6_reass_expire_node_idx, |
| 1146 | IP6_EVENT_CONFIG_CHANGED, 0); |
| 1147 | u32 new_nbuckets = ip6_reass_get_nbuckets (); |
Klement Sekera | 3ecc221 | 2018-03-27 10:34:43 +0200 | [diff] [blame] | 1148 | if (ip6_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1149 | { |
| 1150 | clib_bihash_48_8_t new_hash; |
| 1151 | memset (&new_hash, 0, sizeof (new_hash)); |
| 1152 | ip6_rehash_cb_ctx ctx; |
| 1153 | ctx.failure = 0; |
| 1154 | ctx.new_hash = &new_hash; |
| 1155 | clib_bihash_init_48_8 (&new_hash, "ip6-reass", new_nbuckets, |
| 1156 | new_nbuckets * 1024); |
| 1157 | clib_bihash_foreach_key_value_pair_48_8 (&ip6_reass_main.hash, |
| 1158 | ip6_rehash_cb, &ctx); |
| 1159 | if (ctx.failure) |
| 1160 | { |
| 1161 | clib_bihash_free_48_8 (&new_hash); |
| 1162 | return -1; |
| 1163 | } |
| 1164 | else |
| 1165 | { |
| 1166 | clib_bihash_free_48_8 (&ip6_reass_main.hash); |
| 1167 | clib_memcpy (&ip6_reass_main.hash, &new_hash, |
| 1168 | sizeof (ip6_reass_main.hash)); |
| 1169 | } |
| 1170 | } |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
| 1174 | vnet_api_error_t |
| 1175 | ip6_reass_get (u32 * timeout_ms, u32 * max_reassemblies, |
| 1176 | u32 * expire_walk_interval_ms) |
| 1177 | { |
| 1178 | *timeout_ms = ip6_reass_main.timeout_ms; |
| 1179 | *max_reassemblies = ip6_reass_main.max_reass_n; |
| 1180 | *expire_walk_interval_ms = ip6_reass_main.expire_walk_interval_ms; |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1184 | static clib_error_t * |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1185 | ip6_reass_init_function (vlib_main_t * vm) |
| 1186 | { |
| 1187 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1188 | clib_error_t *error = 0; |
| 1189 | u32 nbuckets; |
Dave Barach | 1403fcd | 2018-02-05 09:45:43 -0500 | [diff] [blame] | 1190 | vlib_node_t *node; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1191 | |
| 1192 | rm->vlib_main = vm; |
| 1193 | rm->vnet_main = vnet_get_main (); |
| 1194 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1195 | vec_validate (rm->per_thread_data, vlib_num_workers () + 1); |
| 1196 | ip6_reass_per_thread_t *rt; |
| 1197 | vec_foreach (rt, rm->per_thread_data) |
| 1198 | { |
| 1199 | clib_spinlock_init (&rt->lock); |
| 1200 | pool_alloc (rt->pool, rm->max_reass_n); |
| 1201 | } |
Dave Barach | 1403fcd | 2018-02-05 09:45:43 -0500 | [diff] [blame] | 1202 | |
| 1203 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-reassembly-expire-walk"); |
| 1204 | ASSERT (node); |
| 1205 | rm->ip6_reass_expire_node_idx = node->index; |
| 1206 | |
Klement Sekera | 3ecc221 | 2018-03-27 10:34:43 +0200 | [diff] [blame] | 1207 | ip6_reass_set_params (IP6_REASS_TIMEOUT_DEFAULT_MS, |
| 1208 | IP6_REASS_MAX_REASSEMBLIES_DEFAULT, |
| 1209 | IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS); |
| 1210 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1211 | nbuckets = ip6_reass_get_nbuckets (); |
| 1212 | clib_bihash_init_48_8 (&rm->hash, "ip6-reass", nbuckets, nbuckets * 1024); |
| 1213 | |
Dave Barach | 1403fcd | 2018-02-05 09:45:43 -0500 | [diff] [blame] | 1214 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop"); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1215 | ASSERT (node); |
| 1216 | rm->ip6_drop_idx = node->index; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1217 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error"); |
| 1218 | ASSERT (node); |
| 1219 | rm->ip6_icmp_error_idx = node->index; |
| 1220 | |
| 1221 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 1222 | return error; |
| 1223 | ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION, |
| 1224 | ip6_reass_node.index); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1225 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1226 | return error; |
| 1227 | } |
| 1228 | |
| 1229 | VLIB_INIT_FUNCTION (ip6_reass_init_function); |
| 1230 | |
| 1231 | static uword |
| 1232 | ip6_reass_walk_expired (vlib_main_t * vm, |
| 1233 | vlib_node_runtime_t * node, vlib_frame_t * f) |
| 1234 | { |
| 1235 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1236 | uword event_type, *event_data = 0; |
| 1237 | |
| 1238 | while (true) |
| 1239 | { |
| 1240 | vlib_process_wait_for_event_or_clock (vm, |
| 1241 | (f64) rm->expire_walk_interval_ms |
| 1242 | / (f64) MSEC_PER_SEC); |
| 1243 | event_type = vlib_process_get_events (vm, &event_data); |
| 1244 | |
| 1245 | switch (event_type) |
| 1246 | { |
| 1247 | case ~0: /* no events => timeout */ |
| 1248 | /* nothing to do here */ |
| 1249 | break; |
| 1250 | case IP6_EVENT_CONFIG_CHANGED: |
| 1251 | break; |
| 1252 | default: |
| 1253 | clib_warning ("BUG: event type 0x%wx", event_type); |
| 1254 | break; |
| 1255 | } |
| 1256 | f64 now = vlib_time_now (vm); |
| 1257 | |
| 1258 | ip6_reass_t *reass; |
| 1259 | u32 *vec_timeout = NULL; |
| 1260 | int *pool_indexes_to_free = NULL; |
| 1261 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1262 | uword thread_index = 0; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1263 | int index; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1264 | const uword nthreads = os_get_nthreads (); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1265 | u32 *vec_icmp_bi = NULL; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1266 | for (thread_index = 0; thread_index < nthreads; ++thread_index) |
| 1267 | { |
| 1268 | ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index]; |
| 1269 | clib_spinlock_lock (&rt->lock); |
| 1270 | |
| 1271 | vec_reset_length (pool_indexes_to_free); |
| 1272 | /* *INDENT-OFF* */ |
| 1273 | pool_foreach_index (index, rt->pool, ({ |
| 1274 | reass = pool_elt_at_index (rt->pool, index); |
| 1275 | if (now > reass->last_heard + rm->timeout) |
| 1276 | { |
| 1277 | vec_add1 (pool_indexes_to_free, index); |
| 1278 | } |
| 1279 | })); |
| 1280 | /* *INDENT-ON* */ |
| 1281 | int *i; |
| 1282 | /* *INDENT-OFF* */ |
| 1283 | vec_foreach (i, pool_indexes_to_free) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1284 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1285 | ip6_reass_t *reass = pool_elt_at_index (rt->pool, i[0]); |
| 1286 | u32 icmp_bi = ~0; |
| 1287 | u32 before = vec_len (vec_timeout); |
| 1288 | vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi); |
| 1289 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 1290 | { |
| 1291 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, |
| 1292 | b->trace_index)) |
| 1293 | { |
| 1294 | /* the trace is gone, don't trace this buffer anymore */ |
| 1295 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 1296 | } |
| 1297 | } |
| 1298 | ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi, &vec_timeout); |
| 1299 | u32 after = vec_len (vec_timeout); |
| 1300 | ASSERT (rt->buffers_n >= (after - before)); |
| 1301 | rt->buffers_n -= (after - before); |
| 1302 | if (~0 != icmp_bi) |
| 1303 | { |
| 1304 | vec_add1 (vec_icmp_bi, icmp_bi); |
| 1305 | ASSERT (rt->buffers_n > 0); |
| 1306 | --rt->buffers_n; |
| 1307 | } |
| 1308 | ip6_reass_free (rm, rt, reass); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1309 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1310 | /* *INDENT-ON* */ |
| 1311 | |
| 1312 | clib_spinlock_unlock (&rt->lock); |
| 1313 | } |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1314 | |
| 1315 | while (vec_len (vec_timeout) > 0) |
| 1316 | { |
| 1317 | vlib_frame_t *f = vlib_get_frame_to_node (vm, rm->ip6_drop_idx); |
| 1318 | u32 *to_next = vlib_frame_vector_args (f); |
| 1319 | u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1320 | int trace_frame = 0; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1321 | while (vec_len (vec_timeout) > 0 && n_left_to_next > 0) |
| 1322 | { |
| 1323 | u32 bi = vec_pop (vec_timeout); |
| 1324 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1325 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 1326 | { |
| 1327 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, |
| 1328 | b->trace_index)) |
| 1329 | { |
| 1330 | /* the trace is gone, don't trace this buffer anymore */ |
| 1331 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 1332 | } |
| 1333 | else |
| 1334 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1335 | trace_frame = 1; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | b->error = node->errors[IP6_ERROR_REASS_TIMEOUT]; |
| 1339 | to_next[0] = bi; |
| 1340 | ++f->n_vectors; |
| 1341 | to_next += 1; |
| 1342 | n_left_to_next -= 1; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1343 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1344 | f->flags |= (trace_frame * VLIB_FRAME_TRACE); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1345 | vlib_put_frame_to_node (vm, rm->ip6_drop_idx, f); |
| 1346 | } |
| 1347 | |
| 1348 | while (vec_len (vec_icmp_bi) > 0) |
| 1349 | { |
| 1350 | vlib_frame_t *f = |
| 1351 | vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx); |
| 1352 | u32 *to_next = vlib_frame_vector_args (f); |
| 1353 | u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors; |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1354 | int trace_frame = 0; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1355 | while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0) |
| 1356 | { |
| 1357 | u32 bi = vec_pop (vec_icmp_bi); |
| 1358 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1359 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 1360 | { |
| 1361 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, |
| 1362 | b->trace_index)) |
| 1363 | { |
| 1364 | /* the trace is gone, don't trace this buffer anymore */ |
| 1365 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 1366 | } |
| 1367 | else |
| 1368 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1369 | trace_frame = 1; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | b->error = node->errors[IP6_ERROR_REASS_TIMEOUT]; |
| 1373 | to_next[0] = bi; |
| 1374 | ++f->n_vectors; |
| 1375 | to_next += 1; |
| 1376 | n_left_to_next -= 1; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1377 | } |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1378 | f->flags |= (trace_frame * VLIB_FRAME_TRACE); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1379 | vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f); |
| 1380 | } |
| 1381 | |
| 1382 | vec_free (pool_indexes_to_free); |
| 1383 | vec_free (vec_timeout); |
| 1384 | vec_free (vec_icmp_bi); |
| 1385 | if (event_data) |
| 1386 | { |
| 1387 | _vec_len (event_data) = 0; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | static vlib_node_registration_t ip6_reass_expire_node; |
| 1395 | |
| 1396 | /* *INDENT-OFF* */ |
| 1397 | VLIB_REGISTER_NODE (ip6_reass_expire_node, static) = { |
| 1398 | .function = ip6_reass_walk_expired, |
| 1399 | .format_trace = format_ip6_reass_trace, |
| 1400 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1401 | .name = "ip6-reassembly-expire-walk", |
| 1402 | |
| 1403 | .n_errors = ARRAY_LEN (ip6_reassembly_error_strings), |
| 1404 | .error_strings = ip6_reassembly_error_strings, |
| 1405 | |
| 1406 | }; |
| 1407 | /* *INDENT-ON* */ |
| 1408 | |
| 1409 | static u8 * |
| 1410 | format_ip6_reass_key (u8 * s, va_list * args) |
| 1411 | { |
| 1412 | ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *); |
| 1413 | s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u", |
| 1414 | key->xx_id, format_ip6_address, &key->src, format_ip6_address, |
| 1415 | &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto); |
| 1416 | return s; |
| 1417 | } |
| 1418 | |
| 1419 | static u8 * |
| 1420 | format_ip6_reass (u8 * s, va_list * args) |
| 1421 | { |
| 1422 | vlib_main_t *vm = va_arg (*args, vlib_main_t *); |
| 1423 | ip6_reass_t *reass = va_arg (*args, ip6_reass_t *); |
| 1424 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1425 | s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, " |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1426 | "last_packet_octet: %u, trace_op_counter: %u\n", |
| 1427 | reass->id, format_ip6_reass_key, &reass->key, reass->first_bi, |
| 1428 | reass->data_len, reass->last_packet_octet, |
| 1429 | reass->trace_op_counter); |
| 1430 | u32 bi = reass->first_bi; |
| 1431 | u32 counter = 0; |
| 1432 | while (~0 != bi) |
| 1433 | { |
| 1434 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1435 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 1436 | s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, " |
| 1437 | "fragment[%u, %u]\n", |
| 1438 | counter, vnb->ip.reass.range_first, |
| 1439 | vnb->ip.reass.range_last, bi, |
| 1440 | ip6_reass_buffer_get_data_offset_no_check (b), |
| 1441 | ip6_reass_buffer_get_data_len_no_check (b), |
| 1442 | vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last); |
| 1443 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1444 | { |
| 1445 | bi = b->next_buffer; |
| 1446 | } |
| 1447 | else |
| 1448 | { |
| 1449 | bi = ~0; |
| 1450 | } |
| 1451 | } |
| 1452 | return s; |
| 1453 | } |
| 1454 | |
| 1455 | static clib_error_t * |
| 1456 | show_ip6_reass (vlib_main_t * vm, unformat_input_t * input, |
| 1457 | CLIB_UNUSED (vlib_cli_command_t * lmd)) |
| 1458 | { |
| 1459 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1460 | |
| 1461 | vlib_cli_output (vm, "---------------------"); |
| 1462 | vlib_cli_output (vm, "IP6 reassembly status"); |
| 1463 | vlib_cli_output (vm, "---------------------"); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1464 | bool details = false; |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1465 | if (unformat (input, "details")) |
| 1466 | { |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1467 | details = true; |
| 1468 | } |
| 1469 | |
| 1470 | u32 sum_reass_n = 0; |
| 1471 | u64 sum_buffers_n = 0; |
| 1472 | ip6_reass_t *reass; |
| 1473 | uword thread_index; |
| 1474 | const uword nthreads = os_get_nthreads (); |
| 1475 | for (thread_index = 0; thread_index < nthreads; ++thread_index) |
| 1476 | { |
| 1477 | ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index]; |
| 1478 | clib_spinlock_lock (&rt->lock); |
| 1479 | if (details) |
| 1480 | { |
| 1481 | /* *INDENT-OFF* */ |
| 1482 | pool_foreach (reass, rt->pool, { |
| 1483 | vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass); |
| 1484 | }); |
| 1485 | /* *INDENT-ON* */ |
| 1486 | } |
| 1487 | sum_reass_n += rt->reass_n; |
| 1488 | sum_buffers_n += rt->buffers_n; |
| 1489 | clib_spinlock_unlock (&rt->lock); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1490 | } |
| 1491 | vlib_cli_output (vm, "---------------------"); |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1492 | vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n", |
| 1493 | (long unsigned) sum_reass_n); |
| 1494 | vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per " |
| 1495 | "worker-thread: %lu\n", (long unsigned) rm->max_reass_n); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1496 | vlib_cli_output (vm, "Buffers in use: %lu\n", |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1497 | (long unsigned) sum_buffers_n); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | /* *INDENT-OFF* */ |
| 1502 | VLIB_CLI_COMMAND (show_ip6_reassembly_cmd, static) = { |
| 1503 | .path = "show ip6-reassembly", |
| 1504 | .short_help = "show ip6-reassembly [details]", |
| 1505 | .function = show_ip6_reass, |
| 1506 | }; |
| 1507 | /* *INDENT-ON* */ |
| 1508 | |
Klement Sekera | 4c53313 | 2018-02-22 11:41:12 +0100 | [diff] [blame] | 1509 | vnet_api_error_t |
| 1510 | ip6_reass_enable_disable (u32 sw_if_index, u8 enable_disable) |
| 1511 | { |
| 1512 | return vnet_feature_enable_disable ("ip6-unicast", "ip6-reassembly-feature", |
| 1513 | sw_if_index, enable_disable, 0, 0); |
| 1514 | } |
| 1515 | |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1516 | /* |
| 1517 | * fd.io coding-style-patch-verification: ON |
| 1518 | * |
| 1519 | * Local Variables: |
| 1520 | * eval: (c-set-style "gnu") |
| 1521 | * End: |
| 1522 | */ |