2d156ff4373e002338d75e7b93fb5071aac8bb29
[vpp.git] / src / vnet / classify / in_out_acl.c
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/in_out_acl.h>
18
19 in_out_acl_main_t in_out_acl_main;
20
21 static int
22 vnet_in_out_acl_ip_feature_enable (vlib_main_t * vnm,
23                                    in_out_acl_main_t * am,
24                                    u32 sw_if_index,
25                                    in_out_acl_table_id_t tid,
26                                    int feature_enable, int is_output)
27 {
28
29   if (tid == IN_OUT_ACL_TABLE_L2)
30     {
31       if (is_output)
32         l2output_intf_bitmap_enable (sw_if_index, L2OUTPUT_FEAT_ACL,
33                                      feature_enable);
34       else
35         l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ACL,
36                                     feature_enable);
37     }
38   else
39     {                           /* IP[46] */
40       vnet_feature_config_main_t *fcm;
41       u8 arc;
42
43       if (tid == IN_OUT_ACL_TABLE_IP4)
44         {
45           char *arc_name = is_output ? "ip4-output" : "ip4-unicast";
46           vnet_feature_enable_disable (arc_name,
47                                        is_output ? "ip4-outacl" : "ip4-inacl",
48                                        sw_if_index, feature_enable, 0, 0);
49           arc = vnet_get_feature_arc_index (arc_name);
50         }
51       else
52         {
53           char *arc_name = is_output ? "ip6-output" : "ip6-unicast";
54           vnet_feature_enable_disable (arc_name,
55                                        is_output ? "ip6-outacl" : "ip6-inacl",
56                                        sw_if_index, feature_enable, 0, 0);
57           arc = vnet_get_feature_arc_index (arc_name);
58         }
59
60       fcm = vnet_get_feature_arc_config_main (arc);
61       am->vnet_config_main[is_output][tid] = &fcm->config_main;
62     }
63
64   return 0;
65 }
66
67 int
68 vnet_set_in_out_acl_intfc (vlib_main_t * vm, u32 sw_if_index,
69                            u32 ip4_table_index,
70                            u32 ip6_table_index, u32 l2_table_index,
71                            u32 is_add, u32 is_output)
72 {
73   in_out_acl_main_t *am = &in_out_acl_main;
74   vnet_classify_main_t *vcm = am->vnet_classify_main;
75   u32 acl[IN_OUT_ACL_N_TABLES] = { ip4_table_index, ip6_table_index,
76     l2_table_index
77   };
78   u32 ti;
79
80   /* Assume that we've validated sw_if_index in the API layer */
81
82   for (ti = 0; ti < IN_OUT_ACL_N_TABLES; ti++)
83     {
84       if (acl[ti] == ~0)
85         continue;
86
87       if (pool_is_free_index (vcm->tables, acl[ti]))
88         return VNET_API_ERROR_NO_SUCH_TABLE;
89
90       vec_validate_init_empty
91         (am->classify_table_index_by_sw_if_index[is_output][ti], sw_if_index,
92          ~0);
93
94       /* Reject any DEL operation with wrong sw_if_index */
95       if (!is_add &&
96           (acl[ti] !=
97            am->classify_table_index_by_sw_if_index[is_output][ti]
98            [sw_if_index]))
99         {
100           clib_warning
101             ("Non-existent intf_idx=%d with table_index=%d for delete",
102              sw_if_index, acl[ti]);
103           return VNET_API_ERROR_NO_SUCH_TABLE;
104         }
105
106       /* Return ok on ADD operaton if feature is already enabled */
107       if (is_add &&
108           am->classify_table_index_by_sw_if_index[is_output][ti][sw_if_index]
109           != ~0)
110         return 0;
111
112       vnet_in_out_acl_ip_feature_enable (vm, am, sw_if_index, ti, is_add,
113                                          is_output);
114
115       if (is_add)
116         am->classify_table_index_by_sw_if_index[is_output][ti][sw_if_index] =
117           acl[ti];
118       else
119         am->classify_table_index_by_sw_if_index[is_output][ti][sw_if_index] =
120           ~0;
121     }
122
123   return 0;
124 }
125
126 int
127 vnet_set_input_acl_intfc (vlib_main_t * vm, u32 sw_if_index,
128                           u32 ip4_table_index,
129                           u32 ip6_table_index, u32 l2_table_index, u32 is_add)
130 {
131   return vnet_set_in_out_acl_intfc (vm, sw_if_index, ip4_table_index,
132                                     ip6_table_index, l2_table_index, is_add,
133                                     IN_OUT_ACL_INPUT_TABLE_GROUP);
134 }
135
136 int
137 vnet_set_output_acl_intfc (vlib_main_t * vm, u32 sw_if_index,
138                            u32 ip4_table_index,
139                            u32 ip6_table_index, u32 l2_table_index,
140                            u32 is_add)
141 {
142   return vnet_set_in_out_acl_intfc (vm, sw_if_index, ip4_table_index,
143                                     ip6_table_index, l2_table_index, is_add,
144                                     IN_OUT_ACL_OUTPUT_TABLE_GROUP);
145 }
146
147 static clib_error_t *
148 set_in_out_acl_command_fn (vlib_main_t * vm,
149                            unformat_input_t * input, vlib_cli_command_t * cmd,
150                            u32 is_output)
151 {
152   vnet_main_t *vnm = vnet_get_main ();
153   u32 sw_if_index = ~0;
154   u32 ip4_table_index = ~0;
155   u32 ip6_table_index = ~0;
156   u32 l2_table_index = ~0;
157   u32 is_add = 1;
158   u32 idx_cnt = 0;
159   int rv;
160
161   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
162     {
163       if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
164                     vnm, &sw_if_index))
165         ;
166       else if (unformat (input, "ip4-table %d", &ip4_table_index))
167         idx_cnt++;
168       else if (unformat (input, "ip6-table %d", &ip6_table_index))
169         idx_cnt++;
170       else if (unformat (input, "l2-table %d", &l2_table_index))
171         idx_cnt++;
172       else if (unformat (input, "del"))
173         is_add = 0;
174       else
175         break;
176     }
177
178   if (sw_if_index == ~0)
179     return clib_error_return (0, "Interface must be specified.");
180
181   if (!idx_cnt)
182     return clib_error_return (0, "Table index should be specified.");
183
184   if (idx_cnt > 1)
185     return clib_error_return (0, "Only one table index per API is allowed.");
186
187   rv = vnet_set_in_out_acl_intfc (vm, sw_if_index, ip4_table_index,
188                                   ip6_table_index, l2_table_index, is_add,
189                                   is_output);
190
191   switch (rv)
192     {
193     case 0:
194       break;
195
196     case VNET_API_ERROR_NO_MATCHING_INTERFACE:
197       return clib_error_return (0, "No such interface");
198
199     case VNET_API_ERROR_NO_SUCH_ENTRY:
200       return clib_error_return (0, "No such classifier table");
201     }
202   return 0;
203 }
204
205 static clib_error_t *
206 set_input_acl_command_fn (vlib_main_t * vm,
207                           unformat_input_t * input, vlib_cli_command_t * cmd)
208 {
209   return set_in_out_acl_command_fn (vm, input, cmd,
210                                     IN_OUT_ACL_INPUT_TABLE_GROUP);
211 }
212
213 static clib_error_t *
214 set_output_acl_command_fn (vlib_main_t * vm,
215                            unformat_input_t * input, vlib_cli_command_t * cmd)
216 {
217   return set_in_out_acl_command_fn (vm, input, cmd,
218                                     IN_OUT_ACL_OUTPUT_TABLE_GROUP);
219 }
220
221 /*
222  * Configure interface to enable/disble input/output ACL features:
223  * intfc - interface name to be configured as input ACL
224  * Ip4-table <index> [del] - enable/disable IP4 input ACL
225  * Ip6-table <index> [del] - enable/disable IP6 input ACL
226  * l2-table <index> [del] - enable/disable Layer2 input ACL
227  *
228  * Note: Only one table index per API call is allowed.
229  *
230  */
231 /* *INDENT-OFF* */
232 VLIB_CLI_COMMAND (set_input_acl_command, static) = {
233     .path = "set interface input acl",
234     .short_help =
235     "set interface input acl intfc <int> [ip4-table <index>]\n"
236     "  [ip6-table <index>] [l2-table <index>] [del]",
237     .function = set_input_acl_command_fn,
238 };
239 VLIB_CLI_COMMAND (set_output_acl_command, static) = {
240     .path = "set interface output acl",
241     .short_help =
242     "set interface output acl intfc <int> [ip4-table <index>]\n"
243     "  [ip6-table <index>] [l2-table <index>] [del]",
244     .function = set_output_acl_command_fn,
245 };
246 /* *INDENT-ON* */
247
248 clib_error_t *
249 in_out_acl_init (vlib_main_t * vm)
250 {
251   in_out_acl_main_t *am = &in_out_acl_main;
252   clib_error_t *error = 0;
253
254   if ((error = vlib_call_init_function (vm, ip_in_out_acl_init)))
255     return error;
256
257   am->vlib_main = vm;
258   am->vnet_main = vnet_get_main ();
259   am->vnet_classify_main = &vnet_classify_main;
260
261   return 0;
262 }
263
264 VLIB_INIT_FUNCTION (in_out_acl_init);
265
266 uword
267 unformat_acl_type (unformat_input_t * input, va_list * args)
268 {
269   u32 *acl_type = va_arg (*args, u32 *);
270   u32 tid = IN_OUT_ACL_N_TABLES;
271
272   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
273     {
274       if (unformat (input, "ip4"))
275         tid = IN_OUT_ACL_TABLE_IP4;
276       else if (unformat (input, "ip6"))
277         tid = IN_OUT_ACL_TABLE_IP6;
278       else if (unformat (input, "l2"))
279         tid = IN_OUT_ACL_TABLE_L2;
280       else
281         break;
282     }
283
284   *acl_type = tid;
285   return 1;
286 }
287
288 u8 *
289 format_vnet_in_out_acl_info (u8 * s, va_list * va)
290 {
291   in_out_acl_main_t *am = va_arg (*va, in_out_acl_main_t *);
292   int sw_if_idx = va_arg (*va, int);
293   u32 tid = va_arg (*va, u32);
294
295   if (tid == ~0)
296     {
297       s = format (s, "%10s%20s\t\t%s", "Intfc idx", "Classify table",
298                   "Interface name");
299       return s;
300     }
301
302   s = format (s, "%10d%20d\t\t%U", sw_if_idx, tid,
303               format_vnet_sw_if_index_name, am->vnet_main, sw_if_idx);
304
305   return s;
306 }
307
308 static clib_error_t *
309 show_in_out_acl_command_fn (vlib_main_t * vm,
310                             unformat_input_t * input,
311                             vlib_cli_command_t * cmd, u32 is_output)
312 {
313   in_out_acl_main_t *am = &in_out_acl_main;
314   u32 type = IN_OUT_ACL_N_TABLES;
315   int i;
316   u32 *vec_tbl;
317
318   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
319     {
320       if (unformat (input, "type %U", unformat_acl_type, &type))
321         ;
322       else
323         break;
324     }
325
326   if (type == IN_OUT_ACL_N_TABLES)
327     return clib_error_return (0, is_output ? "Invalid output ACL table type."
328                               : "Invalid input ACL table type.");
329
330   vec_tbl = am->classify_table_index_by_sw_if_index[is_output][type];
331
332   if (vec_len (vec_tbl))
333     vlib_cli_output (vm, "%U", format_vnet_in_out_acl_info, am, ~0 /* hdr */ ,
334                      ~0);
335   else
336     vlib_cli_output (vm, is_output ? "No output ACL tables configured"
337                      : "No input ACL tables configured");
338
339   for (i = 0; i < vec_len (vec_tbl); i++)
340     {
341       if (vec_elt (vec_tbl, i) == ~0)
342         continue;
343
344       vlib_cli_output (vm, "%U", format_vnet_in_out_acl_info,
345                        am, i, vec_elt (vec_tbl, i));
346     }
347
348   return 0;
349 }
350
351 static clib_error_t *
352 show_inacl_command_fn (vlib_main_t * vm,
353                        unformat_input_t * input, vlib_cli_command_t * cmd)
354 {
355   return show_in_out_acl_command_fn (vm, input, cmd,
356                                      IN_OUT_ACL_INPUT_TABLE_GROUP);
357 }
358
359 static clib_error_t *
360 show_outacl_command_fn (vlib_main_t * vm,
361                         unformat_input_t * input, vlib_cli_command_t * cmd)
362 {
363   return show_in_out_acl_command_fn (vm, input, cmd,
364                                      IN_OUT_ACL_OUTPUT_TABLE_GROUP);
365 }
366
367 /* *INDENT-OFF* */
368 VLIB_CLI_COMMAND (show_inacl_command, static) = {
369     .path = "show inacl",
370     .short_help = "show inacl type [ip4|ip6|l2]",
371     .function = show_inacl_command_fn,
372 };
373 VLIB_CLI_COMMAND (show_outacl_command, static) = {
374     .path = "show outacl",
375     .short_help = "show outacl type [ip4|ip6|l2]",
376     .function = show_outacl_command_fn,
377 };
378 /* *INDENT-ON* */
379
380 /*
381  * fd.io coding-style-patch-verification: ON
382  *
383  * Local Variables:
384  * eval: (c-set-style "gnu")
385  * End:
386  */