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