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