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