Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [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 | #include <vnet/session/mma_16.h> |
| 17 | #include <vnet/session/mma_template.c> |
| 18 | #include <vnet/session/mma_40.h> |
| 19 | #include <vnet/session/mma_template.c> |
| 20 | #include <vnet/session/session_rules_table.h> |
| 21 | #include <vnet/session/transport.h> |
| 22 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 23 | u32 |
| 24 | session_rule_tag_key_index (u32 ri, u8 is_ip4) |
| 25 | { |
| 26 | return ((ri << 1) | is_ip4); |
| 27 | } |
| 28 | |
| 29 | void |
| 30 | session_rule_tag_key_index_parse (u32 rti_key, u32 * ri, u8 * is_ip4) |
| 31 | { |
| 32 | *is_ip4 = rti_key & 1; |
| 33 | *ri = rti_key >> 1; |
| 34 | } |
| 35 | |
| 36 | u8 * |
| 37 | session_rules_table_rule_tag (session_rules_table_t * srt, u32 ri, u8 is_ip4) |
| 38 | { |
| 39 | uword *tip; |
| 40 | session_rule_tag_t *rt; |
| 41 | |
| 42 | tip = |
| 43 | hash_get (srt->tags_by_rules, session_rule_tag_key_index (ri, is_ip4)); |
| 44 | if (tip) |
| 45 | { |
| 46 | rt = pool_elt_at_index (srt->rule_tags, *tip); |
| 47 | return rt->tag; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | session_rules_table_add_del_tag (session_rules_table_t * srt, u8 * tag, |
| 54 | u32 rule_index, u8 is_ip4, u8 is_add) |
| 55 | { |
| 56 | uword *rip, *rtip; |
| 57 | session_rule_tag_t *rt; |
| 58 | u32 rti_key; |
| 59 | |
| 60 | if (tag == 0) |
| 61 | return; |
| 62 | if (is_add) |
| 63 | { |
| 64 | pool_get (srt->rule_tags, rt); |
| 65 | rt->tag = vec_dup (tag); |
| 66 | hash_set_mem (srt->rules_by_tag, rt->tag, rule_index); |
| 67 | rti_key = session_rule_tag_key_index (rule_index, is_ip4); |
| 68 | hash_set (srt->tags_by_rules, rti_key, rt - srt->rule_tags); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | rip = hash_get_mem (srt->rules_by_tag, tag); |
| 73 | if (!rip) |
| 74 | { |
| 75 | clib_warning ("tag has no rule associated"); |
| 76 | return; |
| 77 | } |
| 78 | rti_key = session_rule_tag_key_index (*rip, is_ip4); |
| 79 | rtip = hash_get (srt->tags_by_rules, rti_key); |
| 80 | if (!rtip) |
| 81 | { |
| 82 | clib_warning ("rule has no tag associated"); |
| 83 | return; |
| 84 | } |
| 85 | rt = pool_elt_at_index (srt->rule_tags, *rtip); |
| 86 | ASSERT (rt); |
| 87 | hash_unset_mem (srt->rules_by_tag, tag); |
| 88 | hash_unset (srt->tags_by_rules, rti_key); |
| 89 | pool_put (srt->rule_tags, rt); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | u32 |
| 94 | session_rules_table_rule_for_tag (session_rules_table_t * srt, u8 * tag) |
| 95 | { |
| 96 | uword *rp; |
| 97 | if (tag == 0) |
| 98 | return SESSION_RULES_TABLE_INVALID_INDEX; |
| 99 | rp = hash_get_mem (srt->rules_by_tag, tag); |
| 100 | return (rp == 0 ? SESSION_RULES_TABLE_INVALID_INDEX : *rp); |
| 101 | } |
| 102 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 103 | static void |
| 104 | fib_pref_normalize (fib_prefix_t * pref) |
| 105 | { |
| 106 | if (pref->fp_proto == FIB_PROTOCOL_IP4) |
| 107 | ip4_address_normalize (&pref->fp_addr.ip4, pref->fp_len); |
| 108 | else |
| 109 | ip6_address_normalize (&pref->fp_addr.ip6, pref->fp_len); |
| 110 | } |
| 111 | |
| 112 | u8 * |
| 113 | format_session_rule4 (u8 * s, va_list * args) |
| 114 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 115 | session_rules_table_t *srt = va_arg (*args, session_rules_table_t *); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 116 | mma_rule_16_t *sr = va_arg (*args, mma_rule_16_t *); |
| 117 | session_mask_or_match_4_t *mask, *match; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 118 | mma_rules_table_16_t *srt4; |
| 119 | u8 *tag = 0, *null_tag = format (0, "none"); |
| 120 | u32 ri; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 121 | int i; |
| 122 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 123 | srt4 = &srt->session_rules_tables_16; |
| 124 | ri = mma_rules_table_rule_index_16 (srt4, sr); |
| 125 | tag = session_rules_table_rule_tag (srt, ri, 1); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 126 | match = (session_mask_or_match_4_t *) & sr->match; |
| 127 | mask = (session_mask_or_match_4_t *) & sr->mask; |
| 128 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 129 | s = format (s, "[%d] rule: %U/%d %d %U/%d %d action: %d tag: %v", ri, |
| 130 | format_ip4_address, &match->lcl_ip, |
| 131 | ip4_mask_to_preflen (&mask->lcl_ip), match->lcl_port, |
| 132 | format_ip4_address, &match->rmt_ip, |
| 133 | ip4_mask_to_preflen (&mask->rmt_ip), match->rmt_port, |
| 134 | sr->action_index, tag ? tag : null_tag); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 135 | if (vec_len (sr->next_indices)) |
| 136 | { |
| 137 | s = format (s, "\n children: "); |
| 138 | for (i = 0; i < vec_len (sr->next_indices); i++) |
| 139 | s = format (s, "%d ", sr->next_indices[i]); |
| 140 | } |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 141 | vec_free (null_tag); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 142 | return s; |
| 143 | } |
| 144 | |
| 145 | u8 * |
| 146 | format_session_rule6 (u8 * s, va_list * args) |
| 147 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 148 | session_rules_table_t *srt = va_arg (*args, session_rules_table_t *); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 149 | mma_rule_40_t *sr = va_arg (*args, mma_rule_40_t *); |
| 150 | session_mask_or_match_6_t *mask, *match; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 151 | mma_rules_table_40_t *srt6; |
| 152 | u8 *tag = 0, *null_tag = format (0, "none"); |
| 153 | u32 ri; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 154 | int i; |
| 155 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 156 | srt6 = &srt->session_rules_tables_40; |
| 157 | ri = mma_rules_table_rule_index_40 (srt6, sr); |
| 158 | tag = session_rules_table_rule_tag (srt, ri, 0); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 159 | match = (session_mask_or_match_6_t *) & sr->match; |
| 160 | mask = (session_mask_or_match_6_t *) & sr->mask; |
| 161 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 162 | s = format (s, "[%d] rule: %U/%d %d %U/%d %d action: %d tag: %v", ri, |
| 163 | format_ip6_address, &match->lcl_ip, |
| 164 | ip6_mask_to_preflen (&mask->lcl_ip), match->lcl_port, |
| 165 | format_ip6_address, &match->rmt_ip, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 166 | ip6_mask_to_preflen (&mask->rmt_ip), match->rmt_port, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 167 | sr->action_index, tag ? tag : null_tag); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 168 | if (vec_len (sr->next_indices)) |
| 169 | { |
| 170 | s = format (s, "\n children: "); |
| 171 | for (i = 0; i < vec_len (sr->next_indices); i++) |
| 172 | s = format (s, "%d ", sr->next_indices[i]); |
| 173 | } |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 174 | vec_free (null_tag); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 175 | return s; |
| 176 | } |
| 177 | |
| 178 | void * |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 179 | session_rules_table_get (session_rules_table_t * srt, u8 fib_proto) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 180 | { |
| 181 | if (fib_proto == FIB_PROTOCOL_IP4) |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 182 | return &srt->session_rules_tables_16; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 183 | else if (fib_proto == FIB_PROTOCOL_IP6) |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 184 | return &srt->session_rules_tables_40; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | int |
| 189 | rule_cmp_16 (mma_rule_16_t * rule1, mma_rule_16_t * rule2) |
| 190 | { |
| 191 | session_mask_or_match_4_t *m1, *m2; |
| 192 | |
| 193 | m1 = (session_mask_or_match_4_t *) & rule1->max_match; |
| 194 | m2 = (session_mask_or_match_4_t *) & rule2->max_match; |
| 195 | if (m1->rmt_ip.as_u32 != m2->rmt_ip.as_u32) |
| 196 | return (m1->rmt_ip.as_u32 < m2->rmt_ip.as_u32 ? -1 : 1); |
| 197 | if (m1->lcl_ip.as_u32 != m2->lcl_ip.as_u32) |
| 198 | return (m1->lcl_ip.as_u32 < m2->lcl_ip.as_u32 ? -1 : 1); |
| 199 | if (m1->rmt_port != m2->rmt_port) |
| 200 | return (m1->rmt_port < m2->rmt_port ? -1 : 1); |
| 201 | if (m1->lcl_port != m2->lcl_port) |
| 202 | return (m1->lcl_port < m2->lcl_port ? -1 : 1); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | int |
| 207 | rule_cmp_40 (mma_rule_40_t * rule1, mma_rule_40_t * rule2) |
| 208 | { |
| 209 | session_mask_or_match_6_t *r1, *r2; |
| 210 | r1 = (session_mask_or_match_6_t *) & rule1->max_match; |
| 211 | r2 = (session_mask_or_match_6_t *) & rule2->max_match; |
| 212 | if (r1->rmt_ip.as_u64[0] != r2->rmt_ip.as_u64[0]) |
| 213 | return (r1->rmt_ip.as_u64[0] < r2->rmt_ip.as_u64[0] ? -1 : 1); |
| 214 | if (r1->rmt_ip.as_u64[1] != r2->rmt_ip.as_u64[1]) |
| 215 | return (r1->rmt_ip.as_u64[1] < r2->rmt_ip.as_u64[1] ? -1 : 1); |
| 216 | if (r1->lcl_ip.as_u64[0] != r2->lcl_ip.as_u64[0]) |
| 217 | return (r1->lcl_ip.as_u64[0] < r2->lcl_ip.as_u64[0] ? -1 : 1); |
| 218 | if (r1->lcl_ip.as_u64[1] != r2->lcl_ip.as_u64[1]) |
| 219 | return (r1->lcl_ip.as_u64[1] < r2->lcl_ip.as_u64[1]) ? -1 : 1; |
| 220 | if (r1->rmt_port != r2->rmt_port) |
| 221 | return (r1->rmt_port < r2->rmt_port ? -1 : 1); |
| 222 | if (r1->lcl_port != r2->lcl_port) |
| 223 | return (r1->lcl_port < r2->lcl_port ? -1 : 1); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | void |
| 228 | session_rules_table_init_rule_16 (mma_rule_16_t * rule, |
| 229 | fib_prefix_t * lcl, u16 lcl_port, |
| 230 | fib_prefix_t * rmt, u16 rmt_port) |
| 231 | { |
| 232 | session_mask_or_match_4_t *match, *mask, *max_match; |
| 233 | fib_pref_normalize (lcl); |
| 234 | fib_pref_normalize (rmt); |
| 235 | match = (session_mask_or_match_4_t *) & rule->match; |
| 236 | match->lcl_ip.as_u32 = lcl->fp_addr.ip4.as_u32; |
| 237 | match->rmt_ip.as_u32 = rmt->fp_addr.ip4.as_u32; |
| 238 | match->lcl_port = lcl_port; |
| 239 | match->rmt_port = rmt_port; |
| 240 | mask = (session_mask_or_match_4_t *) & rule->mask; |
| 241 | ip4_preflen_to_mask (lcl->fp_len, &mask->lcl_ip); |
| 242 | ip4_preflen_to_mask (rmt->fp_len, &mask->rmt_ip); |
| 243 | mask->lcl_port = lcl_port == 0 ? 0 : (u16) ~ 0; |
| 244 | mask->rmt_port = rmt_port == 0 ? 0 : (u16) ~ 0; |
| 245 | max_match = (session_mask_or_match_4_t *) & rule->max_match; |
| 246 | ip4_prefix_max_address_host_order (&rmt->fp_addr.ip4, rmt->fp_len, |
| 247 | &max_match->rmt_ip); |
| 248 | ip4_prefix_max_address_host_order (&lcl->fp_addr.ip4, lcl->fp_len, |
| 249 | &max_match->lcl_ip); |
| 250 | max_match->lcl_port = lcl_port == 0 ? (u16) ~ 0 : lcl_port; |
| 251 | max_match->rmt_port = rmt_port == 0 ? (u16) ~ 0 : rmt_port; |
| 252 | } |
| 253 | |
| 254 | void |
| 255 | session_rules_table_init_rule_40 (mma_rule_40_t * rule, |
| 256 | fib_prefix_t * lcl, u16 lcl_port, |
| 257 | fib_prefix_t * rmt, u16 rmt_port) |
| 258 | { |
| 259 | session_mask_or_match_6_t *match, *mask, *max_match; |
| 260 | fib_pref_normalize (lcl); |
| 261 | fib_pref_normalize (rmt); |
| 262 | match = (session_mask_or_match_6_t *) & rule->match; |
| 263 | clib_memcpy (&match->lcl_ip, &lcl->fp_addr.ip6, sizeof (match->lcl_ip)); |
| 264 | clib_memcpy (&match->rmt_ip, &rmt->fp_addr.ip6, sizeof (match->rmt_ip)); |
| 265 | match->lcl_port = lcl_port; |
| 266 | match->rmt_port = rmt_port; |
| 267 | mask = (session_mask_or_match_6_t *) & rule->mask; |
| 268 | ip6_preflen_to_mask (lcl->fp_len, &mask->lcl_ip); |
| 269 | ip6_preflen_to_mask (rmt->fp_len, &mask->rmt_ip); |
| 270 | mask->lcl_port = lcl_port == 0 ? 0 : (u16) ~ 0; |
| 271 | mask->rmt_port = rmt_port == 0 ? 0 : (u16) ~ 0; |
| 272 | max_match = (session_mask_or_match_6_t *) & rule->max_match; |
| 273 | ip6_prefix_max_address_host_order (&rmt->fp_addr.ip6, rmt->fp_len, |
| 274 | &max_match->rmt_ip); |
| 275 | ip6_prefix_max_address_host_order (&lcl->fp_addr.ip6, lcl->fp_len, |
| 276 | &max_match->lcl_ip); |
| 277 | max_match->lcl_port = lcl_port == 0 ? (u16) ~ 0 : lcl_port; |
| 278 | max_match->rmt_port = rmt_port == 0 ? (u16) ~ 0 : rmt_port; |
| 279 | } |
| 280 | |
| 281 | mma_rule_16_t * |
| 282 | session_rules_table_alloc_rule_16 (mma_rules_table_16_t * srt, |
| 283 | fib_prefix_t * lcl, u16 lcl_port, |
| 284 | fib_prefix_t * rmt, u16 rmt_port) |
| 285 | { |
| 286 | mma_rule_16_t *rule = 0; |
| 287 | rule = mma_rules_table_rule_alloc_16 (srt); |
| 288 | session_rules_table_init_rule_16 (rule, lcl, lcl_port, rmt, rmt_port); |
| 289 | return rule; |
| 290 | } |
| 291 | |
| 292 | mma_rule_40_t * |
| 293 | session_rules_table_alloc_rule_40 (mma_rules_table_40_t * srt, |
| 294 | fib_prefix_t * lcl, u16 lcl_port, |
| 295 | fib_prefix_t * rmt, u16 rmt_port) |
| 296 | { |
| 297 | mma_rule_40_t *rule; |
| 298 | rule = mma_rules_table_rule_alloc_40 (srt); |
| 299 | session_rules_table_init_rule_40 (rule, lcl, lcl_port, rmt, rmt_port); |
| 300 | return rule; |
| 301 | } |
| 302 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 303 | /** |
| 304 | * Add/delete session rule |
| 305 | * |
| 306 | * @param srt table where rule should be added |
| 307 | * @param args rule arguments |
| 308 | * |
| 309 | * @return 0 if success, clib_error_t error otherwise |
| 310 | */ |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 311 | clib_error_t * |
| 312 | session_rules_table_add_del (session_rules_table_t * srt, |
| 313 | session_rule_table_add_del_args_t * args) |
| 314 | { |
| 315 | u8 fib_proto = args->rmt.fp_proto; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 316 | u32 ri_from_tag, ri; |
| 317 | int rv; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 318 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 319 | ri_from_tag = session_rules_table_rule_for_tag (srt, args->tag); |
| 320 | if (args->is_add && ri_from_tag != SESSION_RULES_TABLE_INVALID_INDEX) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 321 | return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 322 | "tag exists"); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 323 | |
| 324 | if (fib_proto == FIB_PROTOCOL_IP4) |
| 325 | { |
| 326 | mma_rules_table_16_t *srt4; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 327 | srt4 = &srt->session_rules_tables_16; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 328 | if (args->is_add) |
| 329 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 330 | mma_rule_16_t *rule4; |
| 331 | rule4 = session_rules_table_alloc_rule_16 (srt4, &args->lcl, |
| 332 | args->lcl_port, |
| 333 | &args->rmt, |
| 334 | args->rmt_port); |
| 335 | rule4->action_index = args->action_index; |
| 336 | rv = mma_rules_table_add_rule_16 (srt4, rule4); |
| 337 | if (!rv) |
| 338 | { |
| 339 | ri = mma_rules_table_rule_index_16 (srt4, rule4); |
| 340 | session_rules_table_add_del_tag (srt, args->tag, ri, 1, 1); |
| 341 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 342 | } |
| 343 | else |
| 344 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 345 | mma_rule_16_t *rule; |
| 346 | if (ri_from_tag != SESSION_RULES_TABLE_INVALID_INDEX) |
| 347 | { |
| 348 | rule = mma_rules_table_get_rule_16 (srt4, ri_from_tag); |
| 349 | mma_rules_table_del_rule_16 (srt4, rule, srt4->root_index); |
| 350 | session_rules_table_add_del_tag (srt, args->tag, 0, 1, 0); |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | mma_rule_16_t _rule; |
| 355 | rule = &_rule; |
| 356 | memset (rule, 0, sizeof (*rule)); |
| 357 | session_rules_table_init_rule_16 (rule, &args->lcl, |
| 358 | args->lcl_port, &args->rmt, |
| 359 | args->rmt_port); |
| 360 | mma_rules_table_del_rule_16 (srt4, rule, srt4->root_index); |
| 361 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | else if (fib_proto == FIB_PROTOCOL_IP6) |
| 365 | { |
| 366 | mma_rules_table_40_t *srt6; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 367 | mma_rule_40_t *rule6; |
| 368 | srt6 = &srt->session_rules_tables_40; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 369 | if (args->is_add) |
| 370 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 371 | rule6 = session_rules_table_alloc_rule_40 (srt6, &args->lcl, |
| 372 | args->lcl_port, |
| 373 | &args->rmt, |
| 374 | args->rmt_port); |
| 375 | rule6->action_index = args->action_index; |
| 376 | rv = mma_rules_table_add_rule_40 (srt6, rule6); |
| 377 | if (!rv) |
| 378 | { |
| 379 | ri = mma_rules_table_rule_index_40 (srt6, rule6); |
| 380 | session_rules_table_add_del_tag (srt, args->tag, ri, 0, 1); |
| 381 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 382 | } |
| 383 | else |
| 384 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 385 | mma_rule_40_t *rule; |
| 386 | if (ri_from_tag != SESSION_RULES_TABLE_INVALID_INDEX) |
| 387 | { |
| 388 | rule = mma_rules_table_get_rule_40 (srt6, ri_from_tag); |
| 389 | mma_rules_table_del_rule_40 (srt6, rule, srt6->root_index); |
| 390 | session_rules_table_add_del_tag (srt, args->tag, 0, 0, 0); |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | mma_rule_40_t _rule; |
| 395 | rule = &_rule; |
| 396 | memset (rule, 0, sizeof (*rule)); |
| 397 | session_rules_table_init_rule_40 (rule, &args->lcl, |
| 398 | args->lcl_port, &args->rmt, |
| 399 | args->rmt_port); |
| 400 | mma_rules_table_del_rule_40 (srt6, rule, srt6->root_index); |
| 401 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | else |
| 405 | return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE_2, 0, |
| 406 | "invalid fib proto"); |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | u32 |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 411 | session_rules_table_lookup4 (session_rules_table_t * srt, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 412 | ip4_address_t * lcl_ip, ip4_address_t * rmt_ip, |
| 413 | u16 lcl_port, u16 rmt_port) |
| 414 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 415 | mma_rules_table_16_t *srt4 = &srt->session_rules_tables_16; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 416 | session_mask_or_match_4_t key = { |
| 417 | .lcl_ip.as_u32 = lcl_ip->as_u32, |
| 418 | .rmt_ip.as_u32 = rmt_ip->as_u32, |
| 419 | .lcl_port = lcl_port, |
| 420 | .rmt_port = rmt_port, |
| 421 | }; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 422 | return mma_rules_table_lookup_16 (srt4, (mma_mask_or_match_16_t *) & key, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 423 | srt4->root_index); |
| 424 | } |
| 425 | |
| 426 | u32 |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 427 | session_rules_table_lookup6 (session_rules_table_t * srt, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 428 | ip6_address_t * lcl_ip, ip6_address_t * rmt_ip, |
| 429 | u16 lcl_port, u16 rmt_port) |
| 430 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 431 | mma_rules_table_40_t *srt6 = &srt->session_rules_tables_40; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 432 | session_mask_or_match_6_t key = { |
| 433 | .lcl_port = lcl_port, |
| 434 | .rmt_port = rmt_port, |
| 435 | }; |
Florin Coras | a2e244c | 2017-10-29 10:56:15 -0700 | [diff] [blame] | 436 | clib_memcpy (&key.lcl_ip, lcl_ip, sizeof (*lcl_ip)); |
| 437 | clib_memcpy (&key.rmt_ip, rmt_ip, sizeof (*rmt_ip)); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 438 | return mma_rules_table_lookup_40 (srt6, (mma_mask_or_match_40_t *) & key, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 439 | srt6->root_index); |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | session_rules_table_init (session_rules_table_t * srt) |
| 444 | { |
| 445 | mma_rules_table_16_t *srt4; |
| 446 | mma_rules_table_40_t *srt6; |
| 447 | mma_rule_16_t *rule4; |
| 448 | mma_rule_40_t *rule6; |
| 449 | fib_prefix_t null_prefix; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 450 | |
| 451 | memset (&null_prefix, 0, sizeof (null_prefix)); |
| 452 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 453 | srt4 = &srt->session_rules_tables_16; |
| 454 | rule4 = session_rules_table_alloc_rule_16 (srt4, &null_prefix, 0, |
| 455 | &null_prefix, 0); |
| 456 | rule4->action_index = SESSION_RULES_TABLE_INVALID_INDEX; |
| 457 | srt4->root_index = mma_rules_table_rule_index_16 (srt4, rule4); |
| 458 | srt4->rule_cmp_fn = rule_cmp_16; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 459 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 460 | srt6 = &srt->session_rules_tables_40; |
| 461 | rule6 = session_rules_table_alloc_rule_40 (srt6, &null_prefix, 0, |
| 462 | &null_prefix, 0); |
| 463 | rule6->action_index = SESSION_RULES_TABLE_INVALID_INDEX; |
| 464 | srt6->root_index = mma_rules_table_rule_index_40 (srt6, rule6); |
| 465 | srt6->rule_cmp_fn = rule_cmp_40; |
| 466 | |
| 467 | srt->rules_by_tag = hash_create_vec (0, sizeof (u8), sizeof (uword)); |
| 468 | srt->tags_by_rules = hash_create (0, sizeof (uword)); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void |
| 472 | session_rules_table_show_rule (vlib_main_t * vm, session_rules_table_t * srt, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 473 | ip46_address_t * lcl_ip, u16 lcl_port, |
| 474 | ip46_address_t * rmt_ip, u16 rmt_port, |
| 475 | u8 is_ip4) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 476 | { |
| 477 | mma_rules_table_16_t *srt4; |
| 478 | mma_rules_table_40_t *srt6; |
| 479 | mma_rule_16_t *sr4; |
| 480 | mma_rule_40_t *sr6; |
| 481 | u32 ri; |
| 482 | |
| 483 | if (is_ip4) |
| 484 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 485 | srt4 = session_rules_table_get (srt, FIB_PROTOCOL_IP4); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 486 | session_mask_or_match_4_t key = { |
| 487 | .lcl_ip.as_u32 = lcl_ip->ip4.as_u32, |
| 488 | .rmt_ip.as_u32 = rmt_ip->ip4.as_u32, |
| 489 | .lcl_port = lcl_port, |
| 490 | .rmt_port = rmt_port, |
| 491 | }; |
| 492 | ri = |
| 493 | mma_rules_table_lookup_rule_16 (srt4, |
| 494 | (mma_mask_or_match_16_t *) & key, |
| 495 | srt4->root_index); |
| 496 | sr4 = mma_rules_table_get_rule_16 (srt4, ri); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 497 | vlib_cli_output (vm, "%U", format_session_rule4, srt, sr4); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 498 | } |
| 499 | else |
| 500 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 501 | srt6 = session_rules_table_get (srt, FIB_PROTOCOL_IP6); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 502 | session_mask_or_match_6_t key = { |
| 503 | .lcl_port = lcl_port, |
| 504 | .rmt_port = rmt_port, |
| 505 | }; |
Florin Coras | a2e244c | 2017-10-29 10:56:15 -0700 | [diff] [blame] | 506 | clib_memcpy (&key.lcl_ip, &lcl_ip->ip6, sizeof (lcl_ip->ip6)); |
| 507 | clib_memcpy (&key.rmt_ip, &rmt_ip->ip6, sizeof (rmt_ip->ip6)); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 508 | ri = mma_rules_table_lookup_rule_40 (srt6, |
| 509 | (mma_mask_or_match_40_t *) & key, |
| 510 | srt6->root_index); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 511 | sr6 = mma_rules_table_get_rule_40 (srt6, ri); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 512 | vlib_cli_output (vm, "%U", format_session_rule6, srt, sr6); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | void |
| 517 | session_rules_table_cli_dump (vlib_main_t * vm, session_rules_table_t * srt, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 518 | u8 fib_proto) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 519 | { |
| 520 | if (fib_proto == FIB_PROTOCOL_IP4) |
| 521 | { |
| 522 | mma_rules_table_16_t *srt4; |
| 523 | mma_rule_16_t *sr4; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 524 | srt4 = &srt->session_rules_tables_16; |
| 525 | vlib_cli_output (vm, "IP4 rules"); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 526 | |
| 527 | /* *INDENT-OFF* */ |
| 528 | pool_foreach(sr4, srt4->rules, ({ |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 529 | vlib_cli_output (vm, "%U", format_session_rule4, srt, sr4); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 530 | })); |
| 531 | /* *INDENT-ON* */ |
| 532 | |
| 533 | } |
| 534 | else if (fib_proto == FIB_PROTOCOL_IP6) |
| 535 | { |
| 536 | mma_rules_table_40_t *srt6; |
| 537 | mma_rule_40_t *sr6; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 538 | srt6 = &srt->session_rules_tables_40; |
| 539 | vlib_cli_output (vm, "IP6 rules"); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 540 | |
| 541 | /* *INDENT-OFF* */ |
| 542 | pool_foreach(sr6, srt6->rules, ({ |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame^] | 543 | vlib_cli_output (vm, "%U", format_session_rule6, srt, sr6); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 544 | })); |
| 545 | /* *INDENT-ON* */ |
| 546 | |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * fd.io coding-style-patch-verification: ON |
| 552 | * |
| 553 | * Local Variables: |
| 554 | * eval: (c-set-style "gnu") |
| 555 | * End: |
| 556 | */ |