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)); |
| 454 | } |
| 455 | u32 keep_data = |
| 456 | vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end; |
| 457 | while (1) |
| 458 | { |
| 459 | ++buf_cnt; |
| 460 | if (trim_front) |
| 461 | { |
| 462 | if (trim_front > tmp->current_length) |
| 463 | { |
| 464 | /* drop whole buffer */ |
| 465 | vec_add1 (*vec_drop_compress, tmp_bi); |
| 466 | ++dropped_cnt; |
| 467 | trim_front -= tmp->current_length; |
| 468 | ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 469 | tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 470 | tmp_bi = tmp->next_buffer; |
| 471 | tmp = vlib_get_buffer (vm, tmp_bi); |
| 472 | continue; |
| 473 | } |
| 474 | else |
| 475 | { |
| 476 | vlib_buffer_advance (tmp, trim_front); |
| 477 | trim_front = 0; |
| 478 | } |
| 479 | } |
| 480 | if (keep_data) |
| 481 | { |
| 482 | if (last_b) |
| 483 | { |
| 484 | last_b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 485 | last_b->next_buffer = tmp_bi; |
| 486 | } |
| 487 | last_b = tmp; |
| 488 | if (keep_data <= tmp->current_length) |
| 489 | { |
| 490 | tmp->current_length = keep_data; |
| 491 | keep_data = 0; |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | keep_data -= tmp->current_length; |
| 496 | ASSERT (tmp->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 497 | } |
| 498 | total_length += tmp->current_length; |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | vec_add1 (*vec_drop_compress, tmp_bi); |
| 503 | ASSERT (reass->first_bi != tmp_bi); |
| 504 | ++dropped_cnt; |
| 505 | } |
| 506 | if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 507 | { |
| 508 | tmp_bi = tmp->next_buffer; |
| 509 | tmp = vlib_get_buffer (vm, tmp->next_buffer); |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | sub_chain_bi = |
| 517 | vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip. |
| 518 | reass.next_range_bi; |
| 519 | } |
| 520 | while (~0 != sub_chain_bi); |
| 521 | last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 522 | vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi); |
| 523 | ASSERT (total_length >= first_b->current_length); |
| 524 | total_length -= first_b->current_length; |
| 525 | first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 526 | first_b->total_length_not_including_first_buffer = total_length; |
| 527 | // drop fragment header |
| 528 | vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b); |
| 529 | ip6_header_t *ip = vlib_buffer_get_current (first_b); |
| 530 | u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset; |
| 531 | ip6_ext_header_t *prev_hdr; |
| 532 | ip6_ext_header_find_t (ip, prev_hdr, frag_hdr, |
| 533 | IP_PROTOCOL_IPV6_FRAGMENTATION); |
| 534 | if (prev_hdr) |
| 535 | { |
| 536 | prev_hdr->next_hdr = frag_hdr->next_hdr; |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | ip->protocol = frag_hdr->next_hdr; |
| 541 | } |
| 542 | ASSERT ((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset); |
| 543 | memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr), |
| 544 | first_b->current_length - ip6_frag_hdr_offset - |
| 545 | sizeof (ip6_frag_hdr_t)); |
| 546 | first_b->current_length -= sizeof (*frag_hdr); |
| 547 | ip->payload_length = |
| 548 | clib_host_to_net_u16 (total_length + first_b->current_length - |
| 549 | sizeof (*ip)); |
| 550 | ip6_reass_free (rm, reass); |
| 551 | vlib_buffer_chain_compress (vm, first_b, vec_drop_compress); |
| 552 | if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED)) |
| 553 | { |
| 554 | ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0); |
| 555 | #if 0 |
| 556 | // following code does a hexdump of packet fragments to stdout ... |
| 557 | do |
| 558 | { |
| 559 | u32 bi = reass->first_bi; |
| 560 | u8 *s = NULL; |
| 561 | while (~0 != bi) |
| 562 | { |
| 563 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 564 | s = format (s, "%u: %U\n", bi, format_hexdump, |
| 565 | vlib_buffer_get_current (b), b->current_length); |
| 566 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 567 | { |
| 568 | bi = b->next_buffer; |
| 569 | } |
| 570 | else |
| 571 | { |
| 572 | break; |
| 573 | } |
| 574 | } |
| 575 | printf ("%.*s\n", vec_len (s), s); |
| 576 | fflush (stdout); |
| 577 | vec_free (s); |
| 578 | } |
| 579 | while (0); |
| 580 | #endif |
| 581 | } |
| 582 | reass = NULL; |
| 583 | } |
| 584 | |
| 585 | static u32 |
| 586 | ip6_reass_get_buffer_chain_length (vlib_main_t * vm, vlib_buffer_t * b) |
| 587 | { |
| 588 | u32 len = 0; |
| 589 | while (b) |
| 590 | { |
| 591 | ++len; |
| 592 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 593 | { |
| 594 | b = vlib_get_buffer (vm, b->next_buffer); |
| 595 | } |
| 596 | else |
| 597 | { |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | return len; |
| 602 | } |
| 603 | |
| 604 | static void |
| 605 | ip6_reass_insert_range_in_chain (vlib_main_t * vm, |
| 606 | ip6_reass_main_t * rm, |
| 607 | ip6_reass_t * reass, |
| 608 | u32 prev_range_bi, u32 new_next_bi) |
| 609 | { |
| 610 | |
| 611 | vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi); |
| 612 | vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b); |
| 613 | if (~0 != prev_range_bi) |
| 614 | { |
| 615 | vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi); |
| 616 | vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b); |
| 617 | new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi; |
| 618 | prev_vnb->ip.reass.next_range_bi = new_next_bi; |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | if (~0 != reass->first_bi) |
| 623 | { |
| 624 | new_next_vnb->ip.reass.next_range_bi = reass->first_bi; |
| 625 | } |
| 626 | reass->first_bi = new_next_bi; |
| 627 | } |
| 628 | reass->data_len += ip6_reass_buffer_get_data_len (new_next_b); |
| 629 | rm->buffers_n += ip6_reass_get_buffer_chain_length (vm, new_next_b); |
| 630 | } |
| 631 | |
| 632 | void |
| 633 | ip6_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 634 | ip6_reass_main_t * rm, ip6_reass_t * reass, u32 * bi0, |
| 635 | u32 * next0, vlib_error_t * error0, |
| 636 | ip6_frag_hdr_t * frag_hdr, u32 ** vec_drop_overlap, |
| 637 | u32 ** vec_drop_compress, u32 next_input, u32 next_drop, |
| 638 | u32 next_icmp_error) |
| 639 | { |
| 640 | int consumed = 0; |
| 641 | vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0); |
| 642 | vnet_buffer_opaque_t *fvnb = vnet_buffer (fb); |
| 643 | fvnb->ip.reass.ip6_frag_hdr_offset = |
| 644 | (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb); |
| 645 | ip6_header_t *fip = vlib_buffer_get_current (fb); |
| 646 | ASSERT (fb->current_length > sizeof (*fip)); |
| 647 | ASSERT (fvnb->ip.reass.ip6_frag_hdr_offset > 0 && |
| 648 | fvnb->ip.reass.ip6_frag_hdr_offset < fb->current_length); |
| 649 | u32 fragment_first = fvnb->ip.reass.fragment_first = |
| 650 | ip6_frag_hdr_offset_bytes (frag_hdr); |
| 651 | u32 fragment_length = |
| 652 | vlib_buffer_length_in_chain (vm, fb) - |
| 653 | (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr)); |
| 654 | u32 fragment_last = fvnb->ip.reass.fragment_last = |
| 655 | fragment_first + fragment_length - 1; |
| 656 | int more_fragments = ip6_frag_hdr_more (frag_hdr); |
| 657 | u32 candidate_range_bi = reass->first_bi; |
| 658 | u32 prev_range_bi = ~0; |
| 659 | fvnb->ip.reass.range_first = fragment_first; |
| 660 | fvnb->ip.reass.range_last = fragment_last; |
| 661 | fvnb->ip.reass.next_range_bi = ~0; |
| 662 | if (more_fragments && 0 != fragment_length % 8) |
| 663 | { |
| 664 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 665 | { |
| 666 | ip6_reass_add_trace (vm, node, rm, reass, *bi0, |
| 667 | ICMP_ERROR_FL_NOT_MULT_8, 0); |
| 668 | } |
| 669 | *next0 = next_icmp_error; |
| 670 | icmp6_error_set_vnet_buffer (fb, ICMP6_parameter_problem, |
| 671 | ICMP6_parameter_problem_erroneous_header_field, |
| 672 | (u8 *) & fip->payload_length - (u8 *) fip); |
| 673 | return; |
| 674 | } |
| 675 | if (fragment_first + fragment_length > 65535) |
| 676 | { |
| 677 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 678 | { |
| 679 | ip6_reass_add_trace (vm, node, rm, reass, *bi0, |
| 680 | ICMP_ERROR_FL_TOO_BIG, 0); |
| 681 | } |
| 682 | *next0 = next_icmp_error; |
| 683 | ip6_header_t *ip0 = vlib_buffer_get_current (fb); |
| 684 | icmp6_error_set_vnet_buffer (fb, ICMP6_parameter_problem, |
| 685 | ICMP6_parameter_problem_erroneous_header_field, |
| 686 | (u8 *) & frag_hdr->fragment_offset_and_more |
| 687 | - (u8 *) ip0); |
| 688 | return; |
| 689 | } |
| 690 | if (!more_fragments) |
| 691 | { |
| 692 | reass->last_packet_octet = fragment_last; |
| 693 | } |
| 694 | if (~0 == reass->first_bi) |
| 695 | { |
| 696 | // starting a new reassembly |
| 697 | ip6_reass_insert_range_in_chain (vm, rm, reass, prev_range_bi, *bi0); |
| 698 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 699 | { |
| 700 | ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0); |
| 701 | } |
| 702 | *bi0 = ~0; |
| 703 | return; |
| 704 | } |
| 705 | fvnb->ip.reass.estimated_mtu = |
| 706 | clib_min (clib_net_to_host_u16 (fip->payload_length), |
| 707 | fvnb->ip.reass.estimated_mtu); |
| 708 | while (~0 != candidate_range_bi) |
| 709 | { |
| 710 | vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi); |
| 711 | vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b); |
| 712 | if (fragment_first > candidate_vnb->ip.reass.range_last) |
| 713 | { |
| 714 | // this fragments starts after candidate range |
| 715 | prev_range_bi = candidate_range_bi; |
| 716 | candidate_range_bi = candidate_vnb->ip.reass.next_range_bi; |
| 717 | if (candidate_vnb->ip.reass.range_last < fragment_last && |
| 718 | ~0 == candidate_range_bi) |
| 719 | { |
| 720 | // special case - this fragment falls beyond all known ranges |
| 721 | ip6_reass_insert_range_in_chain (vm, rm, reass, prev_range_bi, |
| 722 | *bi0); |
| 723 | consumed = 1; |
| 724 | break; |
| 725 | } |
| 726 | continue; |
| 727 | } |
| 728 | if (fragment_last < candidate_vnb->ip.reass.range_first) |
| 729 | { |
| 730 | // this fragment ends before candidate range without any overlap |
| 731 | ip6_reass_insert_range_in_chain (vm, rm, reass, prev_range_bi, |
| 732 | *bi0); |
| 733 | consumed = 1; |
| 734 | } |
| 735 | else if (fragment_first == candidate_vnb->ip.reass.range_first && |
| 736 | fragment_last == candidate_vnb->ip.reass.range_last) |
| 737 | { |
| 738 | // duplicate fragment - ignore |
| 739 | } |
| 740 | else |
| 741 | { |
| 742 | // overlapping fragment - not allowed by RFC 8200 |
| 743 | ip6_reass_drop_all (vm, rm, reass, vec_drop_overlap); |
| 744 | ip6_reass_free (rm, reass); |
| 745 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 746 | { |
| 747 | ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP, |
| 748 | 0); |
| 749 | } |
| 750 | *next0 = next_drop; |
| 751 | *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT; |
| 752 | } |
| 753 | break; |
| 754 | } |
| 755 | if (consumed) |
| 756 | { |
| 757 | if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED)) |
| 758 | { |
| 759 | ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0); |
| 760 | } |
| 761 | } |
| 762 | if (~0 != reass->last_packet_octet && |
| 763 | reass->data_len == reass->last_packet_octet + 1) |
| 764 | { |
| 765 | ip6_reass_finalize (vm, node, rm, reass, bi0, next0, error0, next_input, |
| 766 | vec_drop_compress); |
| 767 | } |
| 768 | else |
| 769 | { |
| 770 | if (consumed) |
| 771 | { |
| 772 | *bi0 = ~0; |
| 773 | } |
| 774 | else |
| 775 | { |
| 776 | *next0 = next_drop; |
| 777 | *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | always_inline uword |
| 783 | ip6_reassembly (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 784 | vlib_frame_t * frame) |
| 785 | { |
| 786 | u32 *from = vlib_frame_vector_args (frame); |
| 787 | u32 n_left_from, n_left_to_next, *to_next, next_index; |
| 788 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 789 | |
| 790 | n_left_from = frame->n_vectors; |
| 791 | next_index = node->cached_next_index; |
| 792 | static u32 *vec_timeout = NULL; // indexes of buffers which timed out |
| 793 | static u32 *vec_drop_overlap = NULL; // indexes of buffers dropped due to overlap |
| 794 | static u32 *vec_drop_compress = NULL; // indexes of buffers dropped due to buffer compression |
| 795 | while (n_left_from > 0 || vec_len (vec_timeout) > 0 || |
| 796 | vec_len (vec_drop_overlap) > 0) |
| 797 | { |
| 798 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 799 | |
| 800 | while (vec_len (vec_timeout) > 0 && n_left_to_next > 0) |
| 801 | { |
| 802 | u32 bi = vec_pop (vec_timeout); |
| 803 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 804 | b->error = node->errors[IP6_ERROR_REASS_TIMEOUT]; |
| 805 | to_next[0] = bi; |
| 806 | to_next += 1; |
| 807 | n_left_to_next -= 1; |
| 808 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 809 | n_left_to_next, bi, |
| 810 | IP6_REASSEMBLY_NEXT_DROP); |
| 811 | ASSERT (rm->buffers_n > 0); |
| 812 | --rm->buffers_n; |
| 813 | } |
| 814 | |
| 815 | while (vec_len (vec_drop_overlap) > 0 && n_left_to_next > 0) |
| 816 | { |
| 817 | u32 bi = vec_pop (vec_drop_overlap); |
| 818 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 819 | b->error = node->errors[IP6_ERROR_REASS_OVERLAPPING_FRAGMENT]; |
| 820 | to_next[0] = bi; |
| 821 | to_next += 1; |
| 822 | n_left_to_next -= 1; |
| 823 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 824 | n_left_to_next, bi, |
| 825 | IP6_REASSEMBLY_NEXT_DROP); |
| 826 | ASSERT (rm->buffers_n > 0); |
| 827 | --rm->buffers_n; |
| 828 | } |
| 829 | |
| 830 | while (vec_len (vec_drop_compress) > 0 && n_left_to_next > 0) |
| 831 | { |
| 832 | u32 bi = vec_pop (vec_drop_compress); |
| 833 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 834 | b->error = node->errors[IP6_ERROR_NONE]; |
| 835 | to_next[0] = bi; |
| 836 | to_next += 1; |
| 837 | n_left_to_next -= 1; |
| 838 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 839 | n_left_to_next, bi, |
| 840 | IP6_REASSEMBLY_NEXT_DROP); |
| 841 | ASSERT (rm->buffers_n > 0); |
| 842 | --rm->buffers_n; |
| 843 | } |
| 844 | |
| 845 | while (n_left_from > 0 && n_left_to_next > 0) |
| 846 | { |
| 847 | u32 bi0; |
| 848 | vlib_buffer_t *b0; |
| 849 | u32 next0; //, error0; |
| 850 | |
| 851 | bi0 = from[0]; |
| 852 | b0 = vlib_get_buffer (vm, bi0); |
| 853 | |
| 854 | ip6_header_t *ip0 = vlib_buffer_get_current (b0); |
| 855 | ip6_frag_hdr_t *frag_hdr; |
| 856 | ip6_ext_header_t *prev_hdr; |
| 857 | ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr, |
| 858 | IP_PROTOCOL_IPV6_FRAGMENTATION); |
| 859 | if (0 == ip6_frag_hdr_offset (frag_hdr)) |
| 860 | { |
| 861 | // first fragment - verify upper-layer is present |
| 862 | ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr; |
| 863 | while (ip6_ext_hdr (tmp->next_hdr)) |
| 864 | { |
| 865 | tmp = ip6_ext_next_header (tmp); |
| 866 | } |
| 867 | if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr) |
| 868 | { |
| 869 | icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem, |
| 870 | ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, |
| 871 | 0); |
| 872 | b0->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER]; |
| 873 | |
| 874 | to_next[0] = bi0; |
| 875 | to_next += 1; |
| 876 | n_left_to_next -= 1; |
| 877 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 878 | to_next, n_left_to_next, |
| 879 | bi0, |
| 880 | IP6_REASSEMBLY_NEXT_ICMP_ERROR); |
| 881 | goto next; |
| 882 | } |
| 883 | } |
| 884 | vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset = |
| 885 | (u8 *) frag_hdr - (u8 *) ip0; |
| 886 | |
| 887 | ip6_reass_key_t k; |
| 888 | k.src.as_u64[0] = ip0->src_address.as_u64[0]; |
| 889 | k.src.as_u64[1] = ip0->src_address.as_u64[1]; |
| 890 | k.dst.as_u64[0] = ip0->dst_address.as_u64[0]; |
| 891 | k.dst.as_u64[1] = ip0->dst_address.as_u64[1]; |
| 892 | k.xx_id = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 893 | k.frag_id = frag_hdr->identification; |
| 894 | k.proto = ip0->protocol; |
| 895 | k.unused = 0; |
| 896 | u32 icmp_bi = ~0; |
| 897 | ip6_reass_t *reass = |
| 898 | ip6_reass_find_or_create (vm, node, rm, &k, &icmp_bi, |
| 899 | &vec_timeout); |
| 900 | |
| 901 | u32 error0 = IP6_ERROR_NONE; |
| 902 | if (reass) |
| 903 | { |
| 904 | ip6_reass_update (vm, node, rm, reass, &bi0, &next0, &error0, |
| 905 | frag_hdr, &vec_drop_overlap, |
| 906 | &vec_drop_compress, IP6_REASSEMBLY_NEXT_INPUT, |
| 907 | IP6_REASSEMBLY_NEXT_DROP, |
| 908 | IP6_REASSEMBLY_NEXT_ICMP_ERROR); |
| 909 | } |
| 910 | else |
| 911 | { |
| 912 | next0 = IP6_REASSEMBLY_NEXT_DROP; |
| 913 | error0 = IP6_ERROR_REASS_LIMIT_REACHED; |
| 914 | } |
| 915 | |
| 916 | b0->error = node->errors[error0]; |
| 917 | |
| 918 | if (~0 != bi0) |
| 919 | { |
| 920 | to_next[0] = bi0; |
| 921 | to_next += 1; |
| 922 | n_left_to_next -= 1; |
| 923 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 924 | n_left_to_next, bi0, next0); |
| 925 | } |
| 926 | |
| 927 | if (~0 != icmp_bi) |
| 928 | { |
| 929 | next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR; |
| 930 | to_next[0] = icmp_bi; |
| 931 | to_next += 1; |
| 932 | n_left_to_next -= 1; |
| 933 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 934 | n_left_to_next, icmp_bi, |
| 935 | next0); |
| 936 | } |
| 937 | next: |
| 938 | from += 1; |
| 939 | n_left_from -= 1; |
| 940 | } |
| 941 | |
| 942 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 943 | } |
| 944 | |
| 945 | return frame->n_vectors; |
| 946 | } |
| 947 | |
| 948 | static char *ip6_reassembly_error_strings[] = { |
| 949 | #define _(sym, string) string, |
| 950 | foreach_ip6_error |
| 951 | #undef _ |
| 952 | }; |
| 953 | |
| 954 | /* *INDENT-OFF* */ |
| 955 | VLIB_REGISTER_NODE (ip6_reass_node, static) = { |
| 956 | .function = ip6_reassembly, |
| 957 | .name = "ip6-reassembly", |
| 958 | .vector_size = sizeof (u32), |
| 959 | .format_trace = format_ip6_reass_trace, |
| 960 | .n_errors = ARRAY_LEN (ip6_reassembly_error_strings), |
| 961 | .error_strings = ip6_reassembly_error_strings, |
| 962 | .n_next_nodes = IP6_REASSEMBLY_N_NEXT, |
| 963 | .next_nodes = |
| 964 | { |
| 965 | [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input", |
| 966 | [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop", |
| 967 | [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error", |
| 968 | }, |
| 969 | }; |
| 970 | /* *INDENT-ON* */ |
| 971 | |
| 972 | VLIB_NODE_FUNCTION_MULTIARCH (ip6_reass_node, ip6_reassembly) |
| 973 | static u32 ip6_reass_get_nbuckets () |
| 974 | { |
| 975 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 976 | u32 nbuckets; |
| 977 | u8 i; |
| 978 | |
| 979 | nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR); |
| 980 | |
| 981 | for (i = 0; i < 31; i++) |
| 982 | if ((1 << i) >= nbuckets) |
| 983 | break; |
| 984 | nbuckets = 1 << i; |
| 985 | |
| 986 | return nbuckets; |
| 987 | } |
| 988 | |
| 989 | typedef enum |
| 990 | { |
| 991 | IP6_EVENT_CONFIG_CHANGED = 1, |
| 992 | } ip6_reass_event_t; |
| 993 | |
| 994 | typedef struct |
| 995 | { |
| 996 | int failure; |
| 997 | clib_bihash_48_8_t *new_hash; |
| 998 | } ip6_rehash_cb_ctx; |
| 999 | |
| 1000 | void |
| 1001 | ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx) |
| 1002 | { |
| 1003 | ip6_rehash_cb_ctx *ctx = _ctx; |
| 1004 | if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1)) |
| 1005 | { |
| 1006 | ctx->failure = 1; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | vnet_api_error_t |
| 1011 | ip6_reass_set (u32 timeout_ms, u32 max_reassemblies, |
| 1012 | u32 expire_walk_interval_ms) |
| 1013 | { |
| 1014 | u32 old_nbuckets = ip6_reass_get_nbuckets (); |
| 1015 | ip6_reass_main.timeout_ms = timeout_ms; |
| 1016 | ip6_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC; |
| 1017 | ip6_reass_main.max_reass_n = max_reassemblies; |
| 1018 | ip6_reass_main.expire_walk_interval_ms = expire_walk_interval_ms; |
| 1019 | vlib_process_signal_event (ip6_reass_main.vlib_main, |
| 1020 | ip6_reass_main.ip6_reass_expire_node_idx, |
| 1021 | IP6_EVENT_CONFIG_CHANGED, 0); |
| 1022 | u32 new_nbuckets = ip6_reass_get_nbuckets (); |
| 1023 | if (ip6_reass_main.max_reass_n > 0 && new_nbuckets > 1 && |
| 1024 | new_nbuckets != old_nbuckets) |
| 1025 | { |
| 1026 | clib_bihash_48_8_t new_hash; |
| 1027 | memset (&new_hash, 0, sizeof (new_hash)); |
| 1028 | ip6_rehash_cb_ctx ctx; |
| 1029 | ctx.failure = 0; |
| 1030 | ctx.new_hash = &new_hash; |
| 1031 | clib_bihash_init_48_8 (&new_hash, "ip6-reass", new_nbuckets, |
| 1032 | new_nbuckets * 1024); |
| 1033 | clib_bihash_foreach_key_value_pair_48_8 (&ip6_reass_main.hash, |
| 1034 | ip6_rehash_cb, &ctx); |
| 1035 | if (ctx.failure) |
| 1036 | { |
| 1037 | clib_bihash_free_48_8 (&new_hash); |
| 1038 | return -1; |
| 1039 | } |
| 1040 | else |
| 1041 | { |
| 1042 | clib_bihash_free_48_8 (&ip6_reass_main.hash); |
| 1043 | clib_memcpy (&ip6_reass_main.hash, &new_hash, |
| 1044 | sizeof (ip6_reass_main.hash)); |
| 1045 | } |
| 1046 | } |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | vnet_api_error_t |
| 1051 | ip6_reass_get (u32 * timeout_ms, u32 * max_reassemblies, |
| 1052 | u32 * expire_walk_interval_ms) |
| 1053 | { |
| 1054 | *timeout_ms = ip6_reass_main.timeout_ms; |
| 1055 | *max_reassemblies = ip6_reass_main.max_reass_n; |
| 1056 | *expire_walk_interval_ms = ip6_reass_main.expire_walk_interval_ms; |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | clib_error_t * |
| 1061 | ip6_reass_init_function (vlib_main_t * vm) |
| 1062 | { |
| 1063 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1064 | clib_error_t *error = 0; |
| 1065 | u32 nbuckets; |
| 1066 | |
| 1067 | rm->vlib_main = vm; |
| 1068 | rm->vnet_main = vnet_get_main (); |
| 1069 | |
| 1070 | rm->reass_n = 0; |
| 1071 | pool_alloc (rm->pool, rm->max_reass_n); |
| 1072 | ip6_reass_set (IP6_REASS_TIMEOUT_DEFAULT_MS, |
| 1073 | IP6_REASS_MAX_REASSEMBLIES_DEAFULT, |
| 1074 | IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS); |
| 1075 | |
| 1076 | nbuckets = ip6_reass_get_nbuckets (); |
| 1077 | clib_bihash_init_48_8 (&rm->hash, "ip6-reass", nbuckets, nbuckets * 1024); |
| 1078 | |
| 1079 | vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop"); |
| 1080 | ASSERT (node); |
| 1081 | rm->ip6_drop_idx = node->index; |
| 1082 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-reassembly-expire-walk"); |
| 1083 | ASSERT (node); |
| 1084 | rm->ip6_reass_expire_node_idx = node->index; |
| 1085 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error"); |
| 1086 | ASSERT (node); |
| 1087 | rm->ip6_icmp_error_idx = node->index; |
| 1088 | |
| 1089 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 1090 | return error; |
| 1091 | ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION, |
| 1092 | ip6_reass_node.index); |
| 1093 | return error; |
| 1094 | } |
| 1095 | |
| 1096 | VLIB_INIT_FUNCTION (ip6_reass_init_function); |
| 1097 | |
| 1098 | static uword |
| 1099 | ip6_reass_walk_expired (vlib_main_t * vm, |
| 1100 | vlib_node_runtime_t * node, vlib_frame_t * f) |
| 1101 | { |
| 1102 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1103 | uword event_type, *event_data = 0; |
| 1104 | |
| 1105 | while (true) |
| 1106 | { |
| 1107 | vlib_process_wait_for_event_or_clock (vm, |
| 1108 | (f64) rm->expire_walk_interval_ms |
| 1109 | / (f64) MSEC_PER_SEC); |
| 1110 | event_type = vlib_process_get_events (vm, &event_data); |
| 1111 | |
| 1112 | switch (event_type) |
| 1113 | { |
| 1114 | case ~0: /* no events => timeout */ |
| 1115 | /* nothing to do here */ |
| 1116 | break; |
| 1117 | case IP6_EVENT_CONFIG_CHANGED: |
| 1118 | break; |
| 1119 | default: |
| 1120 | clib_warning ("BUG: event type 0x%wx", event_type); |
| 1121 | break; |
| 1122 | } |
| 1123 | f64 now = vlib_time_now (vm); |
| 1124 | |
| 1125 | ip6_reass_t *reass; |
| 1126 | u32 *vec_timeout = NULL; |
| 1127 | int *pool_indexes_to_free = NULL; |
| 1128 | |
| 1129 | int index; |
| 1130 | /* *INDENT-OFF* */ |
| 1131 | pool_foreach_index (index, rm->pool, ({ |
| 1132 | reass = pool_elt_at_index (rm->pool, index); |
| 1133 | if (now > reass->last_heard + rm->timeout) |
| 1134 | { |
| 1135 | vec_add1 (pool_indexes_to_free, index); |
| 1136 | } |
| 1137 | })); |
| 1138 | /* *INDENT-ON* */ |
| 1139 | int *i; |
| 1140 | u32 *vec_icmp_bi = NULL; |
| 1141 | /* *INDENT-OFF* */ |
| 1142 | vec_foreach (i, pool_indexes_to_free) |
| 1143 | { |
| 1144 | ip6_reass_t *reass = pool_elt_at_index (rm->pool, i[0]); |
| 1145 | u32 icmp_bi = ~0; |
| 1146 | ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi, &vec_timeout); |
| 1147 | if (~0 != icmp_bi) |
| 1148 | { |
| 1149 | vec_add1 (vec_icmp_bi, icmp_bi); |
| 1150 | } |
| 1151 | ip6_reass_free (rm, reass); |
| 1152 | } |
| 1153 | /* *INDENT-ON* */ |
| 1154 | |
| 1155 | while (vec_len (vec_timeout) > 0) |
| 1156 | { |
| 1157 | vlib_frame_t *f = vlib_get_frame_to_node (vm, rm->ip6_drop_idx); |
| 1158 | u32 *to_next = vlib_frame_vector_args (f); |
| 1159 | u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors; |
| 1160 | u32 n_trace = 0; |
| 1161 | while (vec_len (vec_timeout) > 0 && n_left_to_next > 0) |
| 1162 | { |
| 1163 | u32 bi = vec_pop (vec_timeout); |
| 1164 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1165 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 1166 | { |
| 1167 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, |
| 1168 | b->trace_index)) |
| 1169 | { |
| 1170 | /* the trace is gone, don't trace this buffer anymore */ |
| 1171 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 1172 | } |
| 1173 | else |
| 1174 | { |
| 1175 | ++n_trace; |
| 1176 | } |
| 1177 | } |
| 1178 | b->error = node->errors[IP6_ERROR_REASS_TIMEOUT]; |
| 1179 | to_next[0] = bi; |
| 1180 | ++f->n_vectors; |
| 1181 | to_next += 1; |
| 1182 | n_left_to_next -= 1; |
| 1183 | ASSERT (rm->buffers_n > 0); |
| 1184 | --rm->buffers_n; |
| 1185 | } |
| 1186 | if (PREDICT_FALSE (n_trace > 0)) |
| 1187 | { |
| 1188 | f->flags |= VLIB_FRAME_TRACE; |
| 1189 | } |
| 1190 | vlib_put_frame_to_node (vm, rm->ip6_drop_idx, f); |
| 1191 | } |
| 1192 | |
| 1193 | while (vec_len (vec_icmp_bi) > 0) |
| 1194 | { |
| 1195 | vlib_frame_t *f = |
| 1196 | vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx); |
| 1197 | u32 *to_next = vlib_frame_vector_args (f); |
| 1198 | u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors; |
| 1199 | u32 n_trace = 0; |
| 1200 | while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0) |
| 1201 | { |
| 1202 | u32 bi = vec_pop (vec_icmp_bi); |
| 1203 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1204 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 1205 | { |
| 1206 | if (pool_is_free_index (vm->trace_main.trace_buffer_pool, |
| 1207 | b->trace_index)) |
| 1208 | { |
| 1209 | /* the trace is gone, don't trace this buffer anymore */ |
| 1210 | b->flags &= ~VLIB_BUFFER_IS_TRACED; |
| 1211 | } |
| 1212 | else |
| 1213 | { |
| 1214 | ++n_trace; |
| 1215 | } |
| 1216 | } |
| 1217 | b->error = node->errors[IP6_ERROR_REASS_TIMEOUT]; |
| 1218 | to_next[0] = bi; |
| 1219 | ++f->n_vectors; |
| 1220 | to_next += 1; |
| 1221 | n_left_to_next -= 1; |
| 1222 | ASSERT (rm->buffers_n > 0); |
| 1223 | --rm->buffers_n; |
| 1224 | } |
| 1225 | if (PREDICT_FALSE (n_trace > 0)) |
| 1226 | { |
| 1227 | f->flags |= VLIB_FRAME_TRACE; |
| 1228 | } |
| 1229 | vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f); |
| 1230 | } |
| 1231 | |
| 1232 | vec_free (pool_indexes_to_free); |
| 1233 | vec_free (vec_timeout); |
| 1234 | vec_free (vec_icmp_bi); |
| 1235 | if (event_data) |
| 1236 | { |
| 1237 | _vec_len (event_data) = 0; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | static vlib_node_registration_t ip6_reass_expire_node; |
| 1245 | |
| 1246 | /* *INDENT-OFF* */ |
| 1247 | VLIB_REGISTER_NODE (ip6_reass_expire_node, static) = { |
| 1248 | .function = ip6_reass_walk_expired, |
| 1249 | .format_trace = format_ip6_reass_trace, |
| 1250 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1251 | .name = "ip6-reassembly-expire-walk", |
| 1252 | |
| 1253 | .n_errors = ARRAY_LEN (ip6_reassembly_error_strings), |
| 1254 | .error_strings = ip6_reassembly_error_strings, |
| 1255 | |
| 1256 | }; |
| 1257 | /* *INDENT-ON* */ |
| 1258 | |
| 1259 | static u8 * |
| 1260 | format_ip6_reass_key (u8 * s, va_list * args) |
| 1261 | { |
| 1262 | ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *); |
| 1263 | s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u", |
| 1264 | key->xx_id, format_ip6_address, &key->src, format_ip6_address, |
| 1265 | &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto); |
| 1266 | return s; |
| 1267 | } |
| 1268 | |
| 1269 | static u8 * |
| 1270 | format_ip6_reass (u8 * s, va_list * args) |
| 1271 | { |
| 1272 | vlib_main_t *vm = va_arg (*args, vlib_main_t *); |
| 1273 | ip6_reass_t *reass = va_arg (*args, ip6_reass_t *); |
| 1274 | |
| 1275 | s = format (s, "ID: %u, key: %U\n first_bi: %u, data_len: %u, " |
| 1276 | "last_packet_octet: %u, trace_op_counter: %u\n", |
| 1277 | reass->id, format_ip6_reass_key, &reass->key, reass->first_bi, |
| 1278 | reass->data_len, reass->last_packet_octet, |
| 1279 | reass->trace_op_counter); |
| 1280 | u32 bi = reass->first_bi; |
| 1281 | u32 counter = 0; |
| 1282 | while (~0 != bi) |
| 1283 | { |
| 1284 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 1285 | vnet_buffer_opaque_t *vnb = vnet_buffer (b); |
| 1286 | s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, " |
| 1287 | "fragment[%u, %u]\n", |
| 1288 | counter, vnb->ip.reass.range_first, |
| 1289 | vnb->ip.reass.range_last, bi, |
| 1290 | ip6_reass_buffer_get_data_offset_no_check (b), |
| 1291 | ip6_reass_buffer_get_data_len_no_check (b), |
| 1292 | vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last); |
| 1293 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1294 | { |
| 1295 | bi = b->next_buffer; |
| 1296 | } |
| 1297 | else |
| 1298 | { |
| 1299 | bi = ~0; |
| 1300 | } |
| 1301 | } |
| 1302 | return s; |
| 1303 | } |
| 1304 | |
| 1305 | static clib_error_t * |
| 1306 | show_ip6_reass (vlib_main_t * vm, unformat_input_t * input, |
| 1307 | CLIB_UNUSED (vlib_cli_command_t * lmd)) |
| 1308 | { |
| 1309 | ip6_reass_main_t *rm = &ip6_reass_main; |
| 1310 | |
| 1311 | vlib_cli_output (vm, "---------------------"); |
| 1312 | vlib_cli_output (vm, "IP6 reassembly status"); |
| 1313 | vlib_cli_output (vm, "---------------------"); |
| 1314 | if (unformat (input, "details")) |
| 1315 | { |
| 1316 | ip6_reass_t *reass; |
| 1317 | /* *INDENT-OFF* */ |
| 1318 | pool_foreach (reass, rm->pool, { |
| 1319 | vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass); |
| 1320 | }); |
| 1321 | /* *INDENT-ON* */ |
| 1322 | } |
| 1323 | vlib_cli_output (vm, "---------------------"); |
| 1324 | vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n", rm->reass_n); |
| 1325 | vlib_cli_output (vm, |
| 1326 | "Maximum configured concurrent IP6 reassemblies: %lu\n", |
| 1327 | (long unsigned) rm->max_reass_n); |
| 1328 | vlib_cli_output (vm, "Buffers in use: %lu\n", |
| 1329 | (long unsigned) rm->buffers_n); |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | /* *INDENT-OFF* */ |
| 1334 | VLIB_CLI_COMMAND (show_ip6_reassembly_cmd, static) = { |
| 1335 | .path = "show ip6-reassembly", |
| 1336 | .short_help = "show ip6-reassembly [details]", |
| 1337 | .function = show_ip6_reass, |
| 1338 | }; |
| 1339 | /* *INDENT-ON* */ |
| 1340 | |
| 1341 | /* |
| 1342 | * fd.io coding-style-patch-verification: ON |
| 1343 | * |
| 1344 | * Local Variables: |
| 1345 | * eval: (c-set-style "gnu") |
| 1346 | * End: |
| 1347 | */ |