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