Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | #include <vnet/ip/ip.h> |
| 16 | #include <vnet/classify/vnet_classify.h> |
| 17 | #include <vnet/classify/input_acl.h> |
| 18 | |
| 19 | input_acl_main_t input_acl_main; |
| 20 | |
| 21 | static int |
| 22 | vnet_inacl_ip_feature_enable (vlib_main_t * vnm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 23 | input_acl_main_t * am, |
| 24 | u32 sw_if_index, |
| 25 | input_acl_table_id_t tid, int feature_enable) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | { |
| 27 | |
| 28 | if (tid == INPUT_ACL_TABLE_L2) |
| 29 | { |
| 30 | l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ACL, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 31 | feature_enable); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | } |
| 33 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 34 | { /* IP[46] */ |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 35 | vnet_feature_config_main_t *fcm; |
| 36 | u8 arc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | |
| 38 | if (tid == INPUT_ACL_TABLE_IP4) |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 39 | { |
| 40 | vnet_feature_enable_disable ("ip4-unicast", "ip4-inacl", |
| 41 | sw_if_index, feature_enable, 0, 0); |
| 42 | arc = vnet_get_feature_arc_index ("ip4-unicast"); |
| 43 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | else |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 45 | { |
| 46 | vnet_feature_enable_disable ("ip6-unicast", "ip6-inacl", |
| 47 | sw_if_index, feature_enable, 0, 0); |
| 48 | arc = vnet_get_feature_arc_index ("ip6-unicast"); |
| 49 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 51 | fcm = vnet_get_feature_arc_config_main (arc); |
| 52 | am->vnet_config_main[tid] = &fcm->config_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 58 | int |
| 59 | vnet_set_input_acl_intfc (vlib_main_t * vm, u32 sw_if_index, |
| 60 | u32 ip4_table_index, |
| 61 | u32 ip6_table_index, u32 l2_table_index, u32 is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 63 | input_acl_main_t *am = &input_acl_main; |
| 64 | vnet_classify_main_t *vcm = am->vnet_classify_main; |
| 65 | u32 acl[INPUT_ACL_N_TABLES] = { ip4_table_index, ip6_table_index, |
| 66 | l2_table_index |
| 67 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | u32 ti; |
| 69 | |
| 70 | /* Assume that we've validated sw_if_index in the API layer */ |
| 71 | |
| 72 | for (ti = 0; ti < INPUT_ACL_N_TABLES; ti++) |
| 73 | { |
| 74 | if (acl[ti] == ~0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 75 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | |
| 77 | if (pool_is_free_index (vcm->tables, acl[ti])) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 78 | return VNET_API_ERROR_NO_SUCH_TABLE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | |
| 80 | vec_validate_init_empty |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 81 | (am->classify_table_index_by_sw_if_index[ti], sw_if_index, ~0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
| 83 | /* Reject any DEL operation with wrong sw_if_index */ |
| 84 | if (!is_add && |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 85 | (acl[ti] != |
| 86 | am->classify_table_index_by_sw_if_index[ti][sw_if_index])) |
| 87 | { |
| 88 | clib_warning |
| 89 | ("Non-existent intf_idx=%d with table_index=%d for delete", |
| 90 | sw_if_index, acl[ti]); |
| 91 | return VNET_API_ERROR_NO_SUCH_TABLE; |
| 92 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
| 94 | /* Return ok on ADD operaton if feature is already enabled */ |
| 95 | if (is_add && |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 96 | am->classify_table_index_by_sw_if_index[ti][sw_if_index] != ~0) |
| 97 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
| 99 | vnet_inacl_ip_feature_enable (vm, am, sw_if_index, ti, is_add); |
| 100 | |
| 101 | if (is_add) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 102 | am->classify_table_index_by_sw_if_index[ti][sw_if_index] = acl[ti]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 104 | am->classify_table_index_by_sw_if_index[ti][sw_if_index] = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static clib_error_t * |
| 111 | set_input_acl_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 112 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 114 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | u32 sw_if_index = ~0; |
| 116 | u32 ip4_table_index = ~0; |
| 117 | u32 ip6_table_index = ~0; |
| 118 | u32 l2_table_index = ~0; |
| 119 | u32 is_add = 1; |
| 120 | u32 idx_cnt = 0; |
| 121 | int rv; |
| 122 | |
| 123 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 124 | { |
| 125 | if (unformat (input, "intfc %U", unformat_vnet_sw_interface, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 126 | vnm, &sw_if_index)) |
| 127 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | else if (unformat (input, "ip4-table %d", &ip4_table_index)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 129 | idx_cnt++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | else if (unformat (input, "ip6-table %d", &ip6_table_index)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 131 | idx_cnt++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | else if (unformat (input, "l2-table %d", &l2_table_index)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 133 | idx_cnt++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | else if (unformat (input, "del")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 135 | is_add = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 137 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if (sw_if_index == ~0) |
| 141 | return clib_error_return (0, "Interface must be specified."); |
| 142 | |
| 143 | if (!idx_cnt) |
| 144 | return clib_error_return (0, "Table index should be specified."); |
| 145 | |
| 146 | if (idx_cnt > 1) |
| 147 | return clib_error_return (0, "Only one table index per API is allowed."); |
| 148 | |
| 149 | rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 150 | ip6_table_index, l2_table_index, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
| 152 | switch (rv) |
| 153 | { |
| 154 | case 0: |
| 155 | break; |
| 156 | |
| 157 | case VNET_API_ERROR_NO_MATCHING_INTERFACE: |
| 158 | return clib_error_return (0, "No such interface"); |
| 159 | |
| 160 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 161 | return clib_error_return (0, "No such classifier table"); |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Configure interface to enable/disble input ACL feature: |
| 168 | * intfc - interface name to be configured as input ACL |
| 169 | * Ip4-table <index> [del] - enable/disable IP4 input ACL |
| 170 | * Ip6-table <index> [del] - enable/disable IP6 input ACL |
| 171 | * l2-table <index> [del] - enable/disable Layer2 input ACL |
| 172 | * |
| 173 | * Note: Only one table index per API call is allowed. |
| 174 | * |
| 175 | */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 176 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | VLIB_CLI_COMMAND (set_input_acl_command, static) = { |
| 178 | .path = "set interface input acl", |
| 179 | .short_help = |
| 180 | "set interface input acl intfc <int> [ip4-table <index>]\n" |
| 181 | " [ip6-table <index>] [l2-table <index>] [del]", |
| 182 | .function = set_input_acl_command_fn, |
| 183 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 184 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 186 | clib_error_t * |
| 187 | input_acl_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 189 | input_acl_main_t *am = &input_acl_main; |
| 190 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | |
| 192 | if ((error = vlib_call_init_function (vm, ip_inacl_init))) |
| 193 | return error; |
| 194 | |
| 195 | am->vlib_main = vm; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 196 | am->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | am->vnet_classify_main = &vnet_classify_main; |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | VLIB_INIT_FUNCTION (input_acl_init); |
| 203 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 204 | uword |
| 205 | unformat_acl_type (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 207 | u32 *acl_type = va_arg (*args, u32 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | u32 tid = INPUT_ACL_N_TABLES; |
| 209 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 210 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 211 | { |
| 212 | if (unformat (input, "ip4")) |
| 213 | tid = INPUT_ACL_TABLE_IP4; |
| 214 | else if (unformat (input, "ip6")) |
| 215 | tid = INPUT_ACL_TABLE_IP6; |
| 216 | else if (unformat (input, "l2")) |
| 217 | tid = INPUT_ACL_TABLE_L2; |
| 218 | else |
| 219 | break; |
| 220 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | |
| 222 | *acl_type = tid; |
| 223 | return 1; |
| 224 | } |
| 225 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 226 | u8 * |
| 227 | format_vnet_inacl_info (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 229 | input_acl_main_t *am = va_arg (*va, input_acl_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | int sw_if_idx = va_arg (*va, int); |
| 231 | u32 tid = va_arg (*va, u32); |
| 232 | |
| 233 | if (tid == ~0) |
| 234 | { |
| 235 | s = format (s, "%10s%20s\t\t%s", "Intfc idx", "Classify table", |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 236 | "Interface name"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | return s; |
| 238 | } |
| 239 | |
| 240 | s = format (s, "%10d%20d\t\t%U", sw_if_idx, tid, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 241 | format_vnet_sw_if_index_name, am->vnet_main, sw_if_idx); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | |
| 243 | return s; |
| 244 | } |
| 245 | |
| 246 | static clib_error_t * |
| 247 | show_inacl_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 248 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 250 | input_acl_main_t *am = &input_acl_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | u32 type = INPUT_ACL_N_TABLES; |
| 252 | int i; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 253 | u32 *vec_tbl; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | |
| 255 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 256 | { |
| 257 | if (unformat (input, "type %U", unformat_acl_type, &type)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 258 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 260 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | if (type == INPUT_ACL_N_TABLES) |
| 264 | return clib_error_return (0, "Invalid input ACL table type."); |
| 265 | |
| 266 | vec_tbl = am->classify_table_index_by_sw_if_index[type]; |
| 267 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 268 | if (vec_len (vec_tbl)) |
| 269 | vlib_cli_output (vm, "%U", format_vnet_inacl_info, am, ~0 /* hdr */ , ~0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | else |
| 271 | vlib_cli_output (vm, "No input ACL tables configured"); |
| 272 | |
| 273 | for (i = 0; i < vec_len (vec_tbl); i++) |
| 274 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 275 | if (vec_elt (vec_tbl, i) == ~0) |
| 276 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | |
| 278 | vlib_cli_output (vm, "%U", format_vnet_inacl_info, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 279 | am, i, vec_elt (vec_tbl, i)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 285 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 286 | VLIB_CLI_COMMAND (show_inacl_command, static) = { |
| 287 | .path = "show inacl", |
| 288 | .short_help = "show inacl type [ip4|ip6|l2]", |
| 289 | .function = show_inacl_command_fn, |
| 290 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 291 | /* *INDENT-ON* */ |
| 292 | |
| 293 | /* |
| 294 | * fd.io coding-style-patch-verification: ON |
| 295 | * |
| 296 | * Local Variables: |
| 297 | * eval: (c-set-style "gnu") |
| 298 | * End: |
| 299 | */ |