ef98f79351f72abaa8426eed7b1fd5271b33d1f5
[vpp.git] / src / plugins / acl / acl_test.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 /*
16  *------------------------------------------------------------------
17  * acl_test.c - test harness plugin
18  *------------------------------------------------------------------
19  */
20
21 #include <vat/vat.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vppinfra/error.h>
25 #include <vnet/ip/ip.h>
26 #include <arpa/inet.h>
27
28 #define __plugin_msg_base acl_test_main.msg_id_base
29 #include <vlibapi/vat_helper_macros.h>
30
31 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
32
33 /* Declare message IDs */
34 #include <acl/acl_msg_enum.h>
35
36 /* define message structures */
37 #define vl_typedefs
38 #include <acl/acl_all_api_h.h>
39 #undef vl_typedefs
40
41 /* define message structures */
42 #define vl_endianfun
43 #include <acl/acl_all_api_h.h>
44 #undef vl_endianfun
45
46 /* instantiate all the print functions we know about */
47 #define vl_print(handle, ...)
48 #define vl_printfun
49 #include <acl/acl_all_api_h.h>
50 #undef vl_printfun
51
52 /* Get the API version number. */
53 #define vl_api_version(n,v) static u32 api_version=(v);
54 #include <acl/acl_all_api_h.h>
55 #undef vl_api_version
56
57 typedef struct {
58     /* API message ID base */
59     u16 msg_id_base;
60     vat_main_t *vat_main;
61 } acl_test_main_t;
62
63 acl_test_main_t acl_test_main;
64
65 #define foreach_standard_reply_retval_handler   \
66 _(acl_del_reply) \
67 _(acl_interface_add_del_reply) \
68 _(macip_acl_interface_add_del_reply) \
69 _(acl_interface_set_acl_list_reply) \
70 _(acl_interface_set_etype_whitelist_reply) \
71 _(macip_acl_del_reply)
72
73 #define foreach_reply_retval_aclindex_handler  \
74 _(acl_add_replace_reply) \
75 _(macip_acl_add_reply) \
76 _(macip_acl_add_replace_reply)
77
78 #define _(n)                                            \
79     static void vl_api_##n##_t_handler                  \
80     (vl_api_##n##_t * mp)                               \
81     {                                                   \
82         vat_main_t * vam = acl_test_main.vat_main;   \
83         i32 retval = ntohl(mp->retval);                 \
84         if (vam->async_mode) {                          \
85             vam->async_errors += (retval < 0);          \
86         } else {                                        \
87             vam->retval = retval;                       \
88             vam->result_ready = 1;                      \
89         }                                               \
90     }
91 foreach_standard_reply_retval_handler;
92 #undef _
93
94 #define _(n)                                            \
95     static void vl_api_##n##_t_handler                  \
96     (vl_api_##n##_t * mp)                               \
97     {                                                   \
98         vat_main_t * vam = acl_test_main.vat_main;   \
99         i32 retval = ntohl(mp->retval);                 \
100         if (vam->async_mode) {                          \
101             vam->async_errors += (retval < 0);          \
102         } else {                                        \
103             clib_warning("ACL index: %d", ntohl(mp->acl_index)); \
104             vam->retval = retval;                       \
105             vam->result_ready = 1;                      \
106         }                                               \
107     }
108 foreach_reply_retval_aclindex_handler;
109 #undef _
110
111 /* These two ought to be in a library somewhere but they aren't */
112 static uword
113 my_unformat_mac_address (unformat_input_t * input, va_list * args)
114 {
115   u8 *a = va_arg (*args, u8 *);
116   return unformat (input, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3],
117                    &a[4], &a[5]);
118 }
119
120 static u8 *
121 my_format_mac_address (u8 * s, va_list * args)
122 {
123   u8 *a = va_arg (*args, u8 *);
124   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
125                  a[0], a[1], a[2], a[3], a[4], a[5]);
126 }
127
128
129
130 static void vl_api_acl_plugin_get_version_reply_t_handler
131     (vl_api_acl_plugin_get_version_reply_t * mp)
132     {
133         vat_main_t * vam = acl_test_main.vat_main;
134         clib_warning("ACL plugin version: %d.%d", ntohl(mp->major), ntohl(mp->minor));
135         vam->result_ready = 1;
136     }
137
138 static void vl_api_acl_interface_list_details_t_handler
139     (vl_api_acl_interface_list_details_t * mp)
140     {
141         int i;
142         vat_main_t * vam = acl_test_main.vat_main;
143         u8 *out = 0;
144         vl_api_acl_interface_list_details_t_endian(mp);
145         out = format(out, "sw_if_index: %d, count: %d, n_input: %d\n", mp->sw_if_index, mp->count, mp->n_input);
146         out = format(out, "   input ");
147         for(i=0; i<mp->count; i++) {
148           if (i == mp->n_input)
149             out = format(out, "\n  output ");
150           out = format(out, "%d ", ntohl (mp->acls[i]));
151         }
152         out = format(out, "\n");
153         clib_warning("%s", out);
154         vec_free(out);
155         vam->result_ready = 1;
156     }
157
158 static void vl_api_acl_interface_etype_whitelist_details_t_handler
159     (vl_api_acl_interface_etype_whitelist_details_t * mp)
160     {
161         int i;
162         vat_main_t * vam = acl_test_main.vat_main;
163         u8 *out = 0;
164         vl_api_acl_interface_etype_whitelist_details_t_endian(mp);
165         out = format(out, "sw_if_index: %d, count: %d, n_input: %d\n", mp->sw_if_index, mp->count, mp->n_input);
166         out = format(out, "   input ");
167         for(i=0; i<mp->count; i++) {
168           if (i == mp->n_input)
169             out = format(out, "\n  output ");
170           out = format(out, "%04x ", ntohs(mp->whitelist[i]));
171         }
172         out = format(out, "\n");
173         clib_warning("%s", out);
174         vec_free(out);
175         vam->result_ready = 1;
176     }
177
178
179
180 static inline u8 *
181 vl_api_acl_rule_t_pretty_format (u8 *out, vl_api_acl_rule_t * a)
182 {
183   int af = a->is_ipv6 ? AF_INET6 : AF_INET;
184   u8 src[INET6_ADDRSTRLEN];
185   u8 dst[INET6_ADDRSTRLEN];
186   inet_ntop(af, a->src_ip_addr, (void *)src, sizeof(src));
187   inet_ntop(af, a->dst_ip_addr, (void *)dst, sizeof(dst));
188
189   out = format(out, "%s action %d src %s/%d dst %s/%d proto %d sport %d-%d dport %d-%d tcpflags %d mask %d",
190                      a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
191                      src, a->src_ip_prefix_len,
192                      dst, a->dst_ip_prefix_len,
193                      a->proto,
194                      a->srcport_or_icmptype_first, a->srcport_or_icmptype_last,
195                      a->dstport_or_icmpcode_first, a->dstport_or_icmpcode_last,
196                      a->tcp_flags_value, a->tcp_flags_mask);
197   return(out);
198 }
199
200
201
202 static void vl_api_acl_details_t_handler
203     (vl_api_acl_details_t * mp)
204     {
205         int i;
206         vat_main_t * vam = acl_test_main.vat_main;
207         vl_api_acl_details_t_endian(mp);
208         u8 *out = 0;
209         out = format(0, "acl_index: %d, count: %d\n   tag {%s}\n", mp->acl_index, mp->count, mp->tag);
210         for(i=0; i<mp->count; i++) {
211           out = format(out, "   ");
212           out = vl_api_acl_rule_t_pretty_format(out, &mp->r[i]);
213           out = format(out, "%s\n", i<mp->count-1 ? "," : "");
214         }
215         clib_warning("%s", out);
216         vec_free(out);
217         vam->result_ready = 1;
218     }
219
220 static inline u8 *
221 vl_api_macip_acl_rule_t_pretty_format (u8 *out, vl_api_macip_acl_rule_t * a)
222 {
223   int af = a->is_ipv6 ? AF_INET6 : AF_INET;
224   u8 src[INET6_ADDRSTRLEN];
225   inet_ntop(af, a->src_ip_addr, (void *)src, sizeof(src));
226
227   out = format(out, "%s action %d ip %s/%d mac %U mask %U",
228                      a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
229                      src, a->src_ip_prefix_len,
230                      my_format_mac_address, a->src_mac,
231                      my_format_mac_address, a->src_mac_mask);
232   return(out);
233 }
234
235
236 static void vl_api_macip_acl_details_t_handler
237     (vl_api_macip_acl_details_t * mp)
238     {
239         int i;
240         vat_main_t * vam = acl_test_main.vat_main;
241         vl_api_macip_acl_details_t_endian(mp);
242         u8 *out = format(0,"MACIP acl_index: %d, count: %d\n   tag {%s}\n", mp->acl_index, mp->count, mp->tag);
243         for(i=0; i<mp->count; i++) {
244           out = format(out, "   ");
245           out = vl_api_macip_acl_rule_t_pretty_format(out, &mp->r[i]);
246           out = format(out, "%s\n", i<mp->count-1 ? "," : "");
247         }
248         clib_warning("%s", out);
249         vec_free(out);
250         vam->result_ready = 1;
251     }
252
253 static void vl_api_macip_acl_interface_get_reply_t_handler
254     (vl_api_macip_acl_interface_get_reply_t * mp)
255     {
256         int i;
257         vat_main_t * vam = acl_test_main.vat_main;
258         u8 *out = format(0, "sw_if_index with MACIP ACL count: %d\n", ntohl(mp->count));
259         for(i=0; i<ntohl(mp->count); i++) {
260           out = format(out, "  macip_acl_interface_add_del sw_if_index %d add acl %d\n", i, ntohl(mp->acls[i]));
261         }
262         out = format(out, "\n");
263         clib_warning("%s", out);
264         vec_free(out);
265         vam->result_ready = 1;
266     }
267
268 static void vl_api_acl_plugin_control_ping_reply_t_handler
269   (vl_api_acl_plugin_control_ping_reply_t * mp)
270 {
271   vat_main_t *vam = &vat_main;
272   i32 retval = ntohl (mp->retval);
273   if (vam->async_mode)
274     {
275       vam->async_errors += (retval < 0);
276     }
277   else
278     {
279       vam->retval = retval;
280       vam->result_ready = 1;
281     }
282 }
283
284
285 /*
286  * Table of message reply handlers, must include boilerplate handlers
287  * we just generated
288  */
289 #define foreach_vpe_api_reply_msg                                       \
290 _(ACL_ADD_REPLACE_REPLY, acl_add_replace_reply) \
291 _(ACL_DEL_REPLY, acl_del_reply) \
292 _(ACL_INTERFACE_ADD_DEL_REPLY, acl_interface_add_del_reply)  \
293 _(ACL_INTERFACE_SET_ACL_LIST_REPLY, acl_interface_set_acl_list_reply) \
294 _(ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY, acl_interface_set_etype_whitelist_reply) \
295 _(ACL_INTERFACE_ETYPE_WHITELIST_DETAILS, acl_interface_etype_whitelist_details)  \
296 _(ACL_INTERFACE_LIST_DETAILS, acl_interface_list_details)  \
297 _(ACL_DETAILS, acl_details)  \
298 _(MACIP_ACL_ADD_REPLY, macip_acl_add_reply) \
299 _(MACIP_ACL_ADD_REPLACE_REPLY, macip_acl_add_replace_reply) \
300 _(MACIP_ACL_DEL_REPLY, macip_acl_del_reply) \
301 _(MACIP_ACL_DETAILS, macip_acl_details)  \
302 _(MACIP_ACL_INTERFACE_ADD_DEL_REPLY, macip_acl_interface_add_del_reply)  \
303 _(MACIP_ACL_INTERFACE_GET_REPLY, macip_acl_interface_get_reply)  \
304 _(ACL_PLUGIN_CONTROL_PING_REPLY, acl_plugin_control_ping_reply) \
305 _(ACL_PLUGIN_GET_VERSION_REPLY, acl_plugin_get_version_reply)
306
307 static int api_acl_plugin_get_version (vat_main_t * vam)
308 {
309     acl_test_main_t * sm = &acl_test_main;
310     vl_api_acl_plugin_get_version_t * mp;
311     u32 msg_size = sizeof(*mp);
312     int ret;
313
314     vam->result_ready = 0;
315     mp = vl_msg_api_alloc_as_if_client(msg_size);
316     clib_memset (mp, 0, msg_size);
317     mp->_vl_msg_id = ntohs (VL_API_ACL_PLUGIN_GET_VERSION + sm->msg_id_base);
318     mp->client_index = vam->my_client_index;
319
320     /* send it... */
321     S(mp);
322
323     /* Wait for a reply... */
324     W (ret);
325     return ret;
326 }
327
328 static int api_macip_acl_interface_get (vat_main_t * vam)
329 {
330     acl_test_main_t * sm = &acl_test_main;
331     vl_api_acl_plugin_get_version_t * mp;
332     u32 msg_size = sizeof(*mp);
333     int ret;
334
335     vam->result_ready = 0;
336     mp = vl_msg_api_alloc_as_if_client(msg_size);
337     clib_memset (mp, 0, msg_size);
338     mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_INTERFACE_GET + sm->msg_id_base);
339     mp->client_index = vam->my_client_index;
340
341     /* send it... */
342     S(mp);
343
344     /* Wait for a reply... */
345     W (ret);
346     return ret;
347 }
348
349 #define vec_validate_acl_rules(v, idx) \
350   do {                                 \
351     if (vec_len(v) < idx+1) {  \
352       vec_validate(v, idx); \
353       v[idx].is_permit = 0x1; \
354       v[idx].srcport_or_icmptype_last = 0xffff; \
355       v[idx].dstport_or_icmpcode_last = 0xffff; \
356     } \
357   } while (0)
358
359
360 static int api_acl_add_replace (vat_main_t * vam)
361 {
362     acl_test_main_t * sm = &acl_test_main;
363     unformat_input_t * i = vam->input;
364     vl_api_acl_add_replace_t * mp;
365     u32 acl_index = ~0;
366     u32 msg_size = sizeof (*mp); /* without the rules */
367
368     vl_api_acl_rule_t *rules = 0;
369     int rule_idx = 0;
370     int n_rules = 0;
371     int n_rules_override = -1;
372     u32 proto = 0;
373     u32 port1 = 0;
374     u32 port2 = 0;
375     u32 action = 0;
376     u32 tcpflags, tcpmask;
377     u32 src_prefix_length = 0, dst_prefix_length = 0;
378     ip4_address_t src_v4address, dst_v4address;
379     ip6_address_t src_v6address, dst_v6address;
380     u8 *tag = 0;
381     int ret;
382
383     if (!unformat (i, "%d", &acl_index)) {
384         /* Just assume -1 */
385     }
386
387     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
388     {
389         if (unformat (i, "ipv6"))
390           {
391             vec_validate_acl_rules(rules, rule_idx);
392             rules[rule_idx].is_ipv6 = 1;
393           }
394         else if (unformat (i, "ipv4"))
395           {
396             vec_validate_acl_rules(rules, rule_idx);
397             rules[rule_idx].is_ipv6 = 0;
398           }
399         else if (unformat (i, "permit+reflect"))
400           {
401             vec_validate_acl_rules(rules, rule_idx);
402             rules[rule_idx].is_permit = 2;
403           }
404         else if (unformat (i, "permit"))
405           {
406             vec_validate_acl_rules(rules, rule_idx);
407             rules[rule_idx].is_permit = 1;
408           }
409         else if (unformat (i, "deny"))
410           {
411             vec_validate_acl_rules(rules, rule_idx);
412             rules[rule_idx].is_permit = 0;
413           }
414         else if (unformat (i, "count %d", &n_rules_override))
415           {
416             /* we will use this later */
417           }
418         else if (unformat (i, "action %d", &action))
419           {
420             vec_validate_acl_rules(rules, rule_idx);
421             rules[rule_idx].is_permit = action;
422           }
423         else if (unformat (i, "src %U/%d",
424          unformat_ip4_address, &src_v4address, &src_prefix_length))
425           {
426             vec_validate_acl_rules(rules, rule_idx);
427             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
428             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
429             rules[rule_idx].is_ipv6 = 0;
430           }
431         else if (unformat (i, "src %U/%d",
432          unformat_ip6_address, &src_v6address, &src_prefix_length))
433           {
434             vec_validate_acl_rules(rules, rule_idx);
435             memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
436             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
437             rules[rule_idx].is_ipv6 = 1;
438           }
439         else if (unformat (i, "dst %U/%d",
440          unformat_ip4_address, &dst_v4address, &dst_prefix_length))
441           {
442             vec_validate_acl_rules(rules, rule_idx);
443             memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
444             rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
445             rules[rule_idx].is_ipv6 = 0;
446           }
447         else if (unformat (i, "dst %U/%d",
448          unformat_ip6_address, &dst_v6address, &dst_prefix_length))
449           {
450             vec_validate_acl_rules(rules, rule_idx);
451             memcpy (rules[rule_idx].dst_ip_addr, &dst_v6address, 16);
452             rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
453             rules[rule_idx].is_ipv6 = 1;
454           }
455         else if (unformat (i, "sport %d-%d", &port1, &port2))
456           {
457             vec_validate_acl_rules(rules, rule_idx);
458             rules[rule_idx].srcport_or_icmptype_first = htons(port1);
459             rules[rule_idx].srcport_or_icmptype_last = htons(port2);
460           }
461         else if (unformat (i, "sport %d", &port1))
462           {
463             vec_validate_acl_rules(rules, rule_idx);
464             rules[rule_idx].srcport_or_icmptype_first = htons(port1);
465             rules[rule_idx].srcport_or_icmptype_last = htons(port1);
466           }
467         else if (unformat (i, "dport %d-%d", &port1, &port2))
468           {
469             vec_validate_acl_rules(rules, rule_idx);
470             rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
471             rules[rule_idx].dstport_or_icmpcode_last = htons(port2);
472           }
473         else if (unformat (i, "dport %d", &port1))
474           {
475             vec_validate_acl_rules(rules, rule_idx);
476             rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
477             rules[rule_idx].dstport_or_icmpcode_last = htons(port1);
478           }
479         else if (unformat (i, "tcpflags %d %d", &tcpflags, &tcpmask))
480           {
481             vec_validate_acl_rules(rules, rule_idx);
482             rules[rule_idx].tcp_flags_value = tcpflags;
483             rules[rule_idx].tcp_flags_mask = tcpmask;
484           }
485         else if (unformat (i, "tcpflags %d mask %d", &tcpflags, &tcpmask))
486           {
487             vec_validate_acl_rules(rules, rule_idx);
488             rules[rule_idx].tcp_flags_value = tcpflags;
489             rules[rule_idx].tcp_flags_mask = tcpmask;
490           }
491         else if (unformat (i, "proto %d", &proto))
492           {
493             vec_validate_acl_rules(rules, rule_idx);
494             rules[rule_idx].proto = proto;
495           }
496         else if (unformat (i, "tag %s", &tag))
497           {
498           }
499         else if (unformat (i, ","))
500           {
501             rule_idx++;
502             vec_validate_acl_rules(rules, rule_idx);
503           }
504         else
505     break;
506     }
507
508     /* Construct the API message */
509     vam->result_ready = 0;
510
511     if(rules)
512       n_rules = vec_len(rules);
513     else
514       n_rules = 0;
515
516     if (n_rules_override >= 0)
517       n_rules = n_rules_override;
518
519     msg_size += n_rules*sizeof(rules[0]);
520
521     mp = vl_msg_api_alloc_as_if_client(msg_size);
522     clib_memset (mp, 0, msg_size);
523     mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
524     mp->client_index = vam->my_client_index;
525     if ((n_rules > 0) && rules)
526       clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
527     if (tag)
528       {
529         if (vec_len(tag) >= sizeof(mp->tag))
530           {
531             tag[sizeof(mp->tag)-1] = 0;
532             _vec_len(tag) = sizeof(mp->tag);
533           }
534         clib_memcpy(mp->tag, tag, vec_len(tag));
535         vec_free(tag);
536       }
537     mp->acl_index = ntohl(acl_index);
538     mp->count = htonl(n_rules);
539
540     /* send it... */
541     S(mp);
542
543     /* Wait for a reply... */
544     W (ret);
545     return ret;
546 }
547
548
549 /*
550  * Read the series of ACL entries from file in the following format:
551  *
552
553 @0.0.0.0/1      131.179.121.0/24        0 : 65535       0 : 65535       0x00/0x00       0x0000/0x0000
554 @128.0.0.0/1    85.54.226.0/23  0 : 65535       0 : 65535       0x00/0x00       0x0000/0x0000
555 @128.0.0.0/1    85.54.48.0/23   0 : 65535       0 : 65535       0x00/0x00       0x0000/0x0000
556 @128.0.0.0/1    31.237.44.0/23  0 : 65535       0 : 65535       0x00/0x00       0x0000/0x0000
557 @0.0.0.0/1      255.84.184.0/23 0 : 65535       0 : 65535       0x00/0x00       0x0000/0x0000
558 @132.92.0.0/16  0.0.0.0/0       0 : 65535       0 : 65535       0x01/0xFF       0x0000/0x0000
559
560  *
561  */
562
563 static int
564 api_acl_add_replace_from_file (vat_main_t * vam)
565 {
566     int ret = -1;
567     unformat_input_t * input = vam->input;
568     acl_test_main_t * sm = &acl_test_main;
569     vl_api_acl_add_replace_t * mp;
570     u32 acl_index = ~0;
571     u32 msg_size = sizeof (*mp); /* without the rules */
572
573     vl_api_acl_rule_t *rules = 0;
574     int rule_idx = -1;
575     int n_rules = 0;
576     int is_permit = 0;
577     int append_default_permit = 0;
578     u32 tcpflags = 0, tcpmask = 0;
579     ip4_address_t src_v4address, dst_v4address;
580     int fd = -1;
581
582     char *file_name = NULL;
583     unformat_input_t file_input;
584
585     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
586       {
587         if (unformat (input, "filename %s", &file_name))
588           {
589             /* we will use this later */
590           }
591         else if (unformat (input, "acl-index %d", &acl_index))
592           {
593             /* we will try to replace an existing ACL */
594           }
595         else if (unformat (input, "permit+reflect"))
596           {
597             is_permit = 2;
598           }
599         else if (unformat (input, "permit"))
600           {
601             is_permit = 1;
602           }
603         else if (unformat (input, "append-default-permit"))
604           {
605             append_default_permit = 1;
606           }
607         else
608           break;
609       }
610
611     fd = open(file_name, O_RDONLY);
612     if (fd < 0)
613       {
614         clib_warning("Could not open file '%s'");
615         goto done;
616       }
617
618     /* input from file */
619     input =  &file_input;
620     unformat_init_clib_file(input, fd);
621
622     unsigned sport_low, sport_high, dport_low, dport_high;
623     unsigned proto, protomask;
624     u32 src_prefix_length, dst_prefix_length;
625     u32 unused1, unused2;
626
627     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
628       {
629             if (!unformat(input, "@%U/%d\t%U/%d\t%d : %d\t%d : %d\t0x%x/0x%x\t0x%x/0x%x",
630                                  unformat_ip4_address, &src_v4address, &src_prefix_length,
631                                  unformat_ip4_address, &dst_v4address, &dst_prefix_length,
632                                  &sport_low, &sport_high, &dport_low, &dport_high, &proto, &protomask, &unused1, &unused2)) {
633               clib_warning("Error parsing");
634               break;
635             }
636
637             rule_idx++;
638             vec_validate_acl_rules(rules, rule_idx);
639
640             rules[rule_idx].is_ipv6 = 0;
641             rules[rule_idx].is_permit = is_permit;
642             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
643             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
644             memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
645             rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
646             rules[rule_idx].srcport_or_icmptype_first = htons(sport_low);
647             rules[rule_idx].srcport_or_icmptype_last = htons(sport_high);
648             rules[rule_idx].dstport_or_icmpcode_first = htons(dport_low);
649             rules[rule_idx].dstport_or_icmpcode_last = htons(dport_high);
650             rules[rule_idx].tcp_flags_value = tcpflags;
651             rules[rule_idx].tcp_flags_mask = tcpmask;
652             rules[rule_idx].proto = proto;
653
654       }
655
656     if (append_default_permit) {
657         rule_idx++;
658         vec_validate_acl_rules(rules, rule_idx);
659
660         rules[rule_idx].is_ipv6 = 0;
661         rules[rule_idx].is_permit = is_permit == 2 ? 2 : 1;
662
663         src_v4address.data[0]=0;
664         src_v4address.data[1]=0;
665         src_v4address.data[2]=0;
666         src_v4address.data[3]=0;
667         memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
668         rules[rule_idx].src_ip_prefix_len = 0;
669
670         dst_v4address.data[0]=0;
671         dst_v4address.data[1]=0;
672         dst_v4address.data[2]=0;
673         dst_v4address.data[3]=0;
674         memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
675         rules[rule_idx].dst_ip_prefix_len = 0;
676
677         rules[rule_idx].srcport_or_icmptype_first = htons(0);
678         rules[rule_idx].srcport_or_icmptype_last = htons(65535);
679         rules[rule_idx].dstport_or_icmpcode_first = htons(0);
680         rules[rule_idx].dstport_or_icmpcode_last = htons(65535);
681         rules[rule_idx].tcp_flags_value = 0;
682         rules[rule_idx].tcp_flags_mask = 0;
683         rules[rule_idx].proto = 0;
684     }
685
686     /* Construct the API message */
687
688     vam->result_ready = 0;
689
690     n_rules = vec_len(rules);
691
692     msg_size += n_rules*sizeof(rules[0]);
693
694     mp = vl_msg_api_alloc_as_if_client(msg_size);
695     clib_memset (mp, 0, msg_size);
696     mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
697     mp->client_index = vam->my_client_index;
698     if (n_rules > 0)
699       clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
700     mp->acl_index = ntohl(acl_index);
701     mp->count = htonl(n_rules);
702
703     /* send it... */
704     S(mp);
705
706     /* Wait for a reply... */
707     W (ret);
708 done:
709     if (fd > 0)
710       close (fd);
711     vec_free(file_name);
712
713     return ret;
714 }
715
716
717 static int api_acl_del (vat_main_t * vam)
718 {
719     unformat_input_t * i = vam->input;
720     vl_api_acl_del_t * mp;
721     u32 acl_index = ~0;
722     int ret;
723
724     if (!unformat (i, "%d", &acl_index)) {
725       errmsg ("missing acl index\n");
726       return -99;
727     }
728
729     /* Construct the API message */
730     M(ACL_DEL, mp);
731     mp->acl_index = ntohl(acl_index);
732
733     /* send it... */
734     S(mp);
735
736     /* Wait for a reply... */
737     W (ret);
738     return ret;
739 }
740
741 static int api_macip_acl_del (vat_main_t * vam)
742 {
743     unformat_input_t * i = vam->input;
744     vl_api_acl_del_t * mp;
745     u32 acl_index = ~0;
746     int ret;
747
748     if (!unformat (i, "%d", &acl_index)) {
749       errmsg ("missing acl index\n");
750       return -99;
751     }
752
753     /* Construct the API message */
754     M(MACIP_ACL_DEL, mp);
755     mp->acl_index = ntohl(acl_index);
756
757     /* send it... */
758     S(mp);
759
760     /* Wait for a reply... */
761     W (ret);
762     return ret;
763 }
764
765 static int api_acl_interface_add_del (vat_main_t * vam)
766 {
767     unformat_input_t * i = vam->input;
768     vl_api_acl_interface_add_del_t * mp;
769     u32 sw_if_index = ~0;
770     u32 acl_index = ~0;
771     u8 is_input = 0;
772     u8 is_add = 0;
773     int ret;
774
775 //    acl_interface_add_del <intfc> | sw_if_index <if-idx> acl_index <acl-idx> [out] [del]
776
777     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
778     {
779         if (unformat (i, "%d", &acl_index))
780     ;
781         else
782     break;
783     }
784
785
786     /* Parse args required to build the message */
787     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
788         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
789             ;
790         else if (unformat (i, "sw_if_index %d", &sw_if_index))
791             ;
792         else if (unformat (i, "add"))
793             is_add = 1;
794         else if (unformat (i, "del"))
795             is_add = 0;
796         else if (unformat (i, "acl %d", &acl_index))
797             ;
798         else if (unformat (i, "input"))
799             is_input = 1;
800         else if (unformat (i, "output"))
801             is_input = 0;
802         else
803             break;
804     }
805
806     if (sw_if_index == ~0) {
807         errmsg ("missing interface name / explicit sw_if_index number \n");
808         return -99;
809     }
810
811     if (acl_index == ~0) {
812         errmsg ("missing ACL index\n");
813         return -99;
814     }
815
816
817
818     /* Construct the API message */
819     M(ACL_INTERFACE_ADD_DEL, mp);
820     mp->acl_index = ntohl(acl_index);
821     mp->sw_if_index = ntohl(sw_if_index);
822     mp->is_add = is_add;
823     mp->is_input = is_input;
824
825     /* send it... */
826     S(mp);
827
828     /* Wait for a reply... */
829     W (ret);
830     return ret;
831 }
832
833 static int api_macip_acl_interface_add_del (vat_main_t * vam)
834 {
835     unformat_input_t * i = vam->input;
836     vl_api_macip_acl_interface_add_del_t * mp;
837     u32 sw_if_index = ~0;
838     u32 acl_index = ~0;
839     u8 is_add = 0;
840     int ret;
841
842     /* Parse args required to build the message */
843     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
844         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
845             ;
846         else if (unformat (i, "sw_if_index %d", &sw_if_index))
847             ;
848         else if (unformat (i, "add"))
849             is_add = 1;
850         else if (unformat (i, "del"))
851             is_add = 0;
852         else if (unformat (i, "acl %d", &acl_index))
853             ;
854         else
855             break;
856     }
857
858     if (sw_if_index == ~0) {
859         errmsg ("missing interface name / explicit sw_if_index number \n");
860         return -99;
861     }
862
863     if (acl_index == ~0) {
864         errmsg ("missing ACL index\n");
865         return -99;
866     }
867
868
869
870     /* Construct the API message */
871     M(MACIP_ACL_INTERFACE_ADD_DEL, mp);
872     mp->acl_index = ntohl(acl_index);
873     mp->sw_if_index = ntohl(sw_if_index);
874     mp->is_add = is_add;
875
876     /* send it... */
877     S(mp);
878
879     /* Wait for a reply... */
880     W (ret);
881     return ret;
882 }
883
884 static int api_acl_interface_set_acl_list (vat_main_t * vam)
885 {
886     unformat_input_t * i = vam->input;
887     vl_api_acl_interface_set_acl_list_t * mp;
888     u32 sw_if_index = ~0;
889     u32 acl_index = ~0;
890     u32 *inacls = 0;
891     u32 *outacls = 0;
892     u8 is_input = 0;
893     int ret;
894
895 //  acl_interface_set_acl_list <intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]
896
897     /* Parse args required to build the message */
898     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
899         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
900             ;
901         else if (unformat (i, "sw_if_index %d", &sw_if_index))
902             ;
903         else if (unformat (i, "%d", &acl_index))
904           {
905             if(is_input)
906               vec_add1(inacls, htonl(acl_index));
907             else
908               vec_add1(outacls, htonl(acl_index));
909           }
910         else if (unformat (i, "acl %d", &acl_index))
911             ;
912         else if (unformat (i, "input"))
913             is_input = 1;
914         else if (unformat (i, "output"))
915             is_input = 0;
916         else
917             break;
918     }
919
920     if (sw_if_index == ~0) {
921         errmsg ("missing interface name / explicit sw_if_index number \n");
922         return -99;
923     }
924
925     /* Construct the API message */
926     M2(ACL_INTERFACE_SET_ACL_LIST, mp, sizeof(u32) * (vec_len(inacls) + vec_len(outacls)));
927     mp->sw_if_index = ntohl(sw_if_index);
928     mp->n_input = vec_len(inacls);
929     mp->count = vec_len(inacls) + vec_len(outacls);
930     vec_append(inacls, outacls);
931     if (vec_len(inacls) > 0)
932       clib_memcpy(mp->acls, inacls, vec_len(inacls)*sizeof(u32));
933
934     /* send it... */
935     S(mp);
936
937     /* Wait for a reply... */
938     W (ret);
939     return ret;
940 }
941
942 static int api_acl_interface_set_etype_whitelist (vat_main_t * vam)
943 {
944     unformat_input_t * i = vam->input;
945     vl_api_acl_interface_set_etype_whitelist_t * mp;
946     u32 sw_if_index = ~0;
947     u32 ethertype = ~0;
948     u16 *etypes_in = 0;
949     u16 *etypes_out = 0;
950     u8 is_input = 1;
951     int ret;
952
953 //  acl_interface_set_etype_whitelist <intfc> | sw_if_index <if-idx> input [ethertype list] output [ethertype list]
954
955     /* Parse args required to build the message */
956     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
957         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
958             ;
959         else if (unformat (i, "sw_if_index %d", &sw_if_index))
960             ;
961         else if (unformat (i, "%x", &ethertype))
962           {
963             ethertype = ethertype & 0xffff;
964             if(is_input)
965               vec_add1(etypes_in, htons(ethertype));
966             else
967               vec_add1(etypes_out, htons(ethertype));
968           }
969         else if (unformat (i, "input"))
970             is_input = 1;
971         else if (unformat (i, "output"))
972             is_input = 0;
973         else
974             break;
975     }
976
977     if (sw_if_index == ~0) {
978         errmsg ("missing interface name / explicit sw_if_index number \n");
979         return -99;
980     }
981
982     /* Construct the API message */
983     M2(ACL_INTERFACE_SET_ETYPE_WHITELIST, mp, sizeof(u32) * (vec_len(etypes_in) + vec_len(etypes_out)));
984     mp->sw_if_index = ntohl(sw_if_index);
985     mp->n_input = vec_len(etypes_in);
986     mp->count = vec_len(etypes_in) + vec_len(etypes_out);
987     vec_append(etypes_in, etypes_out);
988     if (vec_len(etypes_in) > 0)
989       clib_memcpy(mp->whitelist, etypes_in, vec_len(etypes_in)*sizeof(etypes_in[0]));
990
991     /* send it... */
992     S(mp);
993
994     /* Wait for a reply... */
995     W (ret);
996     return ret;
997 }
998
999 static void
1000 api_acl_send_control_ping(vat_main_t *vam)
1001 {
1002   vl_api_acl_plugin_control_ping_t *mp_ping;
1003
1004   M(ACL_PLUGIN_CONTROL_PING, mp_ping);
1005   S(mp_ping);
1006 }
1007
1008
1009 static int api_acl_interface_list_dump (vat_main_t * vam)
1010 {
1011     unformat_input_t * i = vam->input;
1012     u32 sw_if_index = ~0;
1013     vl_api_acl_interface_list_dump_t * mp;
1014     int ret;
1015
1016     /* Parse args required to build the message */
1017     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1018         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
1019             ;
1020         else if (unformat (i, "sw_if_index %d", &sw_if_index))
1021             ;
1022         else
1023             break;
1024     }
1025
1026     /* Construct the API message */
1027     M(ACL_INTERFACE_LIST_DUMP, mp);
1028     mp->sw_if_index = ntohl (sw_if_index);
1029
1030     /* send it... */
1031     S(mp);
1032
1033     /* Use control ping for synchronization */
1034     api_acl_send_control_ping(vam);
1035
1036     /* Wait for a reply... */
1037     W (ret);
1038     return ret;
1039 }
1040
1041 static int api_acl_dump (vat_main_t * vam)
1042 {
1043     unformat_input_t * i = vam->input;
1044     u32 acl_index = ~0;
1045     vl_api_acl_dump_t * mp;
1046     int ret;
1047
1048     /* Parse args required to build the message */
1049     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1050         if (unformat (i, "%d", &acl_index))
1051             ;
1052         else
1053             break;
1054     }
1055
1056     /* Construct the API message */
1057     M(ACL_DUMP, mp);
1058     mp->acl_index = ntohl (acl_index);
1059
1060     /* send it... */
1061     S(mp);
1062
1063     /* Use control ping for synchronization */
1064     api_acl_send_control_ping(vam);
1065
1066     /* Wait for a reply... */
1067     W (ret);
1068     return ret;
1069 }
1070
1071 static int api_macip_acl_dump (vat_main_t * vam)
1072 {
1073     unformat_input_t * i = vam->input;
1074     u32 acl_index = ~0;
1075     vl_api_acl_dump_t * mp;
1076     int ret;
1077
1078     /* Parse args required to build the message */
1079     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1080         if (unformat (i, "%d", &acl_index))
1081             ;
1082         else
1083             break;
1084     }
1085
1086     /* Construct the API message */
1087     M(MACIP_ACL_DUMP, mp);
1088     mp->acl_index = ntohl (acl_index);
1089
1090     /* send it... */
1091     S(mp);
1092
1093     /* Use control ping for synchronization */
1094     api_acl_send_control_ping(vam);
1095
1096     /* Wait for a reply... */
1097     W (ret);
1098     return ret;
1099 }
1100
1101 static int api_acl_interface_etype_whitelist_dump (vat_main_t * vam)
1102 {
1103     unformat_input_t * i = vam->input;
1104     u32 sw_if_index = ~0;
1105     vl_api_acl_interface_etype_whitelist_dump_t * mp;
1106     int ret;
1107
1108     /* Parse args required to build the message */
1109     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1110         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
1111             ;
1112         else if (unformat (i, "sw_if_index %d", &sw_if_index))
1113             ;
1114         else
1115             break;
1116     }
1117
1118     /* Construct the API message */
1119     M(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, mp);
1120     mp->sw_if_index = ntohl (sw_if_index);
1121
1122     /* send it... */
1123     S(mp);
1124
1125     /* Use control ping for synchronization */
1126     api_acl_send_control_ping(vam);
1127
1128     /* Wait for a reply... */
1129     W (ret);
1130     return ret;
1131 }
1132
1133
1134 #define vec_validate_macip_acl_rules(v, idx) \
1135   do {                                 \
1136     if (vec_len(v) < idx+1) {  \
1137       vec_validate(v, idx); \
1138       v[idx].is_permit = 0x1; \
1139     } \
1140   } while (0)
1141
1142
1143 static int api_macip_acl_add (vat_main_t * vam)
1144 {
1145     acl_test_main_t * sm = &acl_test_main;
1146     unformat_input_t * i = vam->input;
1147     vl_api_macip_acl_add_t * mp;
1148     u32 msg_size = sizeof (*mp); /* without the rules */
1149
1150     vl_api_macip_acl_rule_t *rules = 0;
1151     int rule_idx = 0;
1152     int n_rules = 0;
1153     int n_rules_override = -1;
1154     u32 src_prefix_length = 0;
1155     u32 action = 0;
1156     ip4_address_t src_v4address;
1157     ip6_address_t src_v6address;
1158     u8 src_mac[6];
1159     u8 *tag = 0;
1160     u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1161     int ret;
1162
1163     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1164     {
1165         if (unformat (i, "ipv6"))
1166           {
1167             vec_validate_macip_acl_rules(rules, rule_idx);
1168             rules[rule_idx].is_ipv6 = 1;
1169           }
1170         else if (unformat (i, "ipv4"))
1171           {
1172             vec_validate_macip_acl_rules(rules, rule_idx);
1173             rules[rule_idx].is_ipv6 = 0;
1174           }
1175         else if (unformat (i, "permit"))
1176           {
1177             vec_validate_macip_acl_rules(rules, rule_idx);
1178             rules[rule_idx].is_permit = 1;
1179           }
1180         else if (unformat (i, "deny"))
1181           {
1182             vec_validate_macip_acl_rules(rules, rule_idx);
1183             rules[rule_idx].is_permit = 0;
1184           }
1185         else if (unformat (i, "count %d", &n_rules_override))
1186           {
1187             /* we will use this later */
1188           }
1189         else if (unformat (i, "action %d", &action))
1190           {
1191             vec_validate_macip_acl_rules(rules, rule_idx);
1192             rules[rule_idx].is_permit = action;
1193           }
1194         else if (unformat (i, "ip %U/%d",
1195          unformat_ip4_address, &src_v4address, &src_prefix_length) ||
1196                  unformat (i, "ip %U",
1197          unformat_ip4_address, &src_v4address))
1198           {
1199             if (src_prefix_length == 0)
1200               src_prefix_length = 32;
1201             vec_validate_macip_acl_rules(rules, rule_idx);
1202             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
1203             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1204             rules[rule_idx].is_ipv6 = 0;
1205           }
1206         else if (unformat (i, "src"))
1207           {
1208             /* Everything in MACIP is "source" but allow this verbosity */
1209           }
1210         else if (unformat (i, "ip %U/%d",
1211          unformat_ip6_address, &src_v6address, &src_prefix_length) ||
1212                  unformat (i, "ip %U",
1213          unformat_ip6_address, &src_v6address))
1214           {
1215             if (src_prefix_length == 0)
1216               src_prefix_length = 128;
1217             vec_validate_macip_acl_rules(rules, rule_idx);
1218             memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
1219             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1220             rules[rule_idx].is_ipv6 = 1;
1221           }
1222         else if (unformat (i, "mac %U",
1223          my_unformat_mac_address, &src_mac))
1224           {
1225             vec_validate_macip_acl_rules(rules, rule_idx);
1226             memcpy (rules[rule_idx].src_mac, &src_mac, 6);
1227             memcpy (rules[rule_idx].src_mac_mask, &mac_mask_all_1, 6);
1228           }
1229         else if (unformat (i, "mask %U",
1230          my_unformat_mac_address, &src_mac))
1231           {
1232             vec_validate_macip_acl_rules(rules, rule_idx);
1233             memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
1234           }
1235         else if (unformat (i, "tag %s", &tag))
1236           {
1237           }
1238         else if (unformat (i, ","))
1239           {
1240             rule_idx++;
1241             vec_validate_macip_acl_rules(rules, rule_idx);
1242           }
1243         else
1244     break;
1245     }
1246
1247     /* Construct the API message */
1248     vam->result_ready = 0;
1249
1250     if(rules)
1251       n_rules = vec_len(rules);
1252
1253     if (n_rules_override >= 0)
1254       n_rules = n_rules_override;
1255
1256     msg_size += n_rules*sizeof(rules[0]);
1257
1258     mp = vl_msg_api_alloc_as_if_client(msg_size);
1259     clib_memset (mp, 0, msg_size);
1260     mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD + sm->msg_id_base);
1261     mp->client_index = vam->my_client_index;
1262     if ((n_rules > 0) && rules)
1263       clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
1264     if (tag)
1265       {
1266         if (vec_len(tag) >= sizeof(mp->tag))
1267           {
1268             tag[sizeof(mp->tag)-1] = 0;
1269             _vec_len(tag) = sizeof(mp->tag);
1270           }
1271         clib_memcpy(mp->tag, tag, vec_len(tag));
1272         vec_free(tag);
1273       }
1274
1275     mp->count = htonl(n_rules);
1276
1277     /* send it... */
1278     S(mp);
1279
1280     /* Wait for a reply... */
1281     W (ret);
1282     return ret;
1283 }
1284
1285 static int api_macip_acl_add_replace (vat_main_t * vam)
1286 {
1287     acl_test_main_t * sm = &acl_test_main;
1288     unformat_input_t * i = vam->input;
1289     vl_api_macip_acl_add_replace_t * mp;
1290     u32 acl_index = ~0;
1291     u32 msg_size = sizeof (*mp); /* without the rules */
1292
1293     vl_api_macip_acl_rule_t *rules = 0;
1294     int rule_idx = 0;
1295     int n_rules = 0;
1296     int n_rules_override = -1;
1297     u32 src_prefix_length = 0;
1298     u32 action = 0;
1299     ip4_address_t src_v4address;
1300     ip6_address_t src_v6address;
1301     u8 src_mac[6];
1302     u8 *tag = 0;
1303     u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1304     int ret;
1305
1306     if (!unformat (i, "%d", &acl_index)) {
1307         /* Just assume -1 */
1308     }
1309
1310     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1311     {
1312         if (unformat (i, "ipv6"))
1313           {
1314             vec_validate_macip_acl_rules(rules, rule_idx);
1315             rules[rule_idx].is_ipv6 = 1;
1316           }
1317         else if (unformat (i, "ipv4"))
1318           {
1319             vec_validate_macip_acl_rules(rules, rule_idx);
1320             rules[rule_idx].is_ipv6 = 0;
1321           }
1322         else if (unformat (i, "permit"))
1323           {
1324             vec_validate_macip_acl_rules(rules, rule_idx);
1325             rules[rule_idx].is_permit = 1;
1326           }
1327         else if (unformat (i, "deny"))
1328           {
1329             vec_validate_macip_acl_rules(rules, rule_idx);
1330             rules[rule_idx].is_permit = 0;
1331           }
1332         else if (unformat (i, "count %d", &n_rules_override))
1333           {
1334             /* we will use this later */
1335           }
1336         else if (unformat (i, "action %d", &action))
1337           {
1338             vec_validate_macip_acl_rules(rules, rule_idx);
1339             rules[rule_idx].is_permit = action;
1340           }
1341         else if (unformat (i, "ip %U/%d",
1342          unformat_ip4_address, &src_v4address, &src_prefix_length) ||
1343                  unformat (i, "ip %U",
1344          unformat_ip4_address, &src_v4address))
1345           {
1346             if (src_prefix_length == 0)
1347               src_prefix_length = 32;
1348             vec_validate_macip_acl_rules(rules, rule_idx);
1349             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
1350             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1351             rules[rule_idx].is_ipv6 = 0;
1352           }
1353         else if (unformat (i, "src"))
1354           {
1355             /* Everything in MACIP is "source" but allow this verbosity */
1356           }
1357         else if (unformat (i, "ip %U/%d",
1358          unformat_ip6_address, &src_v6address, &src_prefix_length) ||
1359                  unformat (i, "ip %U",
1360          unformat_ip6_address, &src_v6address))
1361           {
1362             if (src_prefix_length == 0)
1363               src_prefix_length = 128;
1364             vec_validate_macip_acl_rules(rules, rule_idx);
1365             memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
1366             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1367             rules[rule_idx].is_ipv6 = 1;
1368           }
1369         else if (unformat (i, "mac %U",
1370          my_unformat_mac_address, &src_mac))
1371           {
1372             vec_validate_macip_acl_rules(rules, rule_idx);
1373             memcpy (rules[rule_idx].src_mac, &src_mac, 6);
1374             memcpy (rules[rule_idx].src_mac_mask, &mac_mask_all_1, 6);
1375           }
1376         else if (unformat (i, "mask %U",
1377          my_unformat_mac_address, &src_mac))
1378           {
1379             vec_validate_macip_acl_rules(rules, rule_idx);
1380             memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
1381           }
1382         else if (unformat (i, "tag %s", &tag))
1383           {
1384           }
1385         else if (unformat (i, ","))
1386           {
1387             rule_idx++;
1388             vec_validate_macip_acl_rules(rules, rule_idx);
1389           }
1390         else
1391     break;
1392     }
1393
1394     if (!rules)
1395       {
1396       errmsg ("rule/s required\n");
1397       return -99;
1398       }
1399     /* Construct the API message */
1400     vam->result_ready = 0;
1401
1402     if(rules)
1403       n_rules = vec_len(rules);
1404
1405     if (n_rules_override >= 0)
1406       n_rules = n_rules_override;
1407
1408     msg_size += n_rules*sizeof(rules[0]);
1409
1410     mp = vl_msg_api_alloc_as_if_client(msg_size);
1411     clib_memset (mp, 0, msg_size);
1412     mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD_REPLACE + sm->msg_id_base);
1413     mp->client_index = vam->my_client_index;
1414     if ((n_rules > 0) && rules)
1415       clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
1416     if (tag)
1417       {
1418         if (vec_len(tag) >= sizeof(mp->tag))
1419           {
1420             tag[sizeof(mp->tag)-1] = 0;
1421             _vec_len(tag) = sizeof(mp->tag);
1422           }
1423         clib_memcpy(mp->tag, tag, vec_len(tag));
1424         vec_free(tag);
1425       }
1426
1427     mp->acl_index = ntohl(acl_index);
1428     mp->count = htonl(n_rules);
1429
1430     /* send it... */
1431     S(mp);
1432
1433     /* Wait for a reply... */
1434     W (ret);
1435     return ret;
1436 }
1437
1438 /*
1439  * List of messages that the api test plugin sends,
1440  * and that the data plane plugin processes
1441  */
1442 #define foreach_vpe_api_msg \
1443 _(acl_plugin_get_version, "") \
1444 _(acl_add_replace, "<acl-idx> [<ipv4|ipv6> <permit|permit+reflect|deny|action N> [src IP/plen] [dst IP/plen] [sport X-Y] [dport X-Y] [proto P] [tcpflags FL MASK], ... , ...") \
1445 _(acl_add_replace_from_file, "filename <file> [permit] [append-default-permit]") \
1446 _(acl_del, "<acl-idx>") \
1447 _(acl_dump, "[<acl-idx>]") \
1448 _(acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] [input|output] acl <acl-idx>") \
1449 _(acl_interface_set_acl_list, "<intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]") \
1450 _(acl_interface_set_etype_whitelist, "<intfc> | sw_if_index <if-idx> input [ethertype list] output [ethertype list]") \
1451 _(acl_interface_etype_whitelist_dump, "[<intfc> | sw_if_index <if-idx>]") \
1452 _(acl_interface_list_dump, "[<intfc> | sw_if_index <if-idx>]") \
1453 _(macip_acl_add, "...") \
1454 _(macip_acl_add_replace, "<acl-idx> [<ipv4|ipv6> <permit|deny|action N> [count <count>] [src] ip <ipaddress/[plen]> mac <mac> mask <mac_mask>, ... , ...") \
1455 _(macip_acl_del, "<acl-idx>")\
1456 _(macip_acl_dump, "[<acl-idx>]") \
1457 _(macip_acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] acl <acl-idx>") \
1458 _(macip_acl_interface_get, "")
1459
1460
1461 static
1462 void acl_vat_api_hookup (vat_main_t *vam)
1463 {
1464     acl_test_main_t * sm = &acl_test_main;
1465     /* Hook up handlers for replies from the data plane plug-in */
1466 #define _(N,n)                                                  \
1467     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
1468                            #n,                                  \
1469                            vl_api_##n##_t_handler,              \
1470                            vl_noop_handler,                     \
1471                            vl_api_##n##_t_endian,               \
1472                            vl_api_##n##_t_print,                \
1473                            sizeof(vl_api_##n##_t), 1);
1474     foreach_vpe_api_reply_msg;
1475 #undef _
1476
1477     /* API messages we can send */
1478 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
1479     foreach_vpe_api_msg;
1480 #undef _
1481
1482     /* Help strings */
1483 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
1484     foreach_vpe_api_msg;
1485 #undef _
1486 }
1487
1488 clib_error_t * vat_plugin_register (vat_main_t *vam)
1489 {
1490   acl_test_main_t * sm = &acl_test_main;
1491   u8 * name;
1492
1493   sm->vat_main = vam;
1494
1495   name = format (0, "acl_%08x%c", api_version, 0);
1496   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
1497
1498   if (sm->msg_id_base != (u16) ~0)
1499     acl_vat_api_hookup (vam);
1500
1501   vec_free(name);
1502
1503   return 0;
1504 }