NAT: fix bin API dump of static mappings.
[vpp.git] / src / plugins / nat / nat_api.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /**
17  * @file
18  * @brief NAT plugin API implementation
19  */
20
21 #include <nat/nat.h>
22 #include <nat/nat_det.h>
23 #include <nat/nat64.h>
24 #include <nat/nat66.h>
25 #include <nat/dslite.h>
26 #include <nat/nat_reass.h>
27 #include <vlibapi/api.h>
28 #include <vlibmemory/api.h>
29
30 #include <nat/nat_msg_enum.h>
31 #include <vnet/fib/fib_table.h>
32
33 #define vl_api_nat44_lb_addr_port_t_endian vl_noop_handler
34 #define vl_api_nat44_add_del_lb_static_mapping_t_endian vl_noop_handler
35 #define vl_api_nat44_nat44_lb_static_mapping_details_t_endian vl_noop_handler
36
37 /* define message structures */
38 #define vl_typedefs
39 #include <nat/nat_all_api_h.h>
40 #undef vl_typedefs
41
42 /* define generated endian-swappers */
43 #define vl_endianfun
44 #include <nat/nat_all_api_h.h>
45 #undef vl_endianfun
46
47 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
48
49 #define REPLY_MSG_ID_BASE sm->msg_id_base
50 #include <vlibapi/api_helper_macros.h>
51
52 /* Get the API version number */
53 #define vl_api_version(n,v) static u32 api_version=(v);
54 #include <nat/nat_all_api_h.h>
55 #undef vl_api_version
56
57 /* Macro to finish up custom dump fns */
58 #define FINISH                                  \
59     vec_add1 (s, 0);                            \
60     vl_print (handle, (char *)s);               \
61     vec_free (s);                               \
62     return handle;
63
64
65 /******************************/
66 /*** Common NAT plugin APIs ***/
67 /******************************/
68
69 static void
70 vl_api_nat_control_ping_t_handler (vl_api_nat_control_ping_t * mp)
71 {
72   vl_api_nat_control_ping_reply_t *rmp;
73   snat_main_t *sm = &snat_main;
74   int rv = 0;
75
76   /* *INDENT-OFF* */
77   REPLY_MACRO2 (VL_API_NAT_CONTROL_PING_REPLY,
78   ({
79     rmp->vpe_pid = ntohl (getpid ());
80   }));
81   /* *INDENT-ON* */
82 }
83
84 static void *
85 vl_api_nat_control_ping_t_print (vl_api_nat_control_ping_t * mp, void *handle)
86 {
87   u8 *s;
88
89   s = format (0, "SCRIPT: nat_control_ping ");
90
91   FINISH;
92 }
93
94 static void
95 vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp)
96 {
97   vl_api_nat_show_config_reply_t *rmp;
98   snat_main_t *sm = &snat_main;
99   int rv = 0;
100
101   /* *INDENT-OFF* */
102   REPLY_MACRO2 (VL_API_NAT_SHOW_CONFIG_REPLY,
103   ({
104     rmp->translation_buckets = htonl (sm->translation_buckets);
105     rmp->translation_memory_size = htonl (sm->translation_memory_size);
106     rmp->user_buckets = htonl (sm->user_buckets);
107     rmp->user_memory_size = htonl (sm->user_memory_size);
108     rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
109     rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
110     rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
111     rmp->static_mapping_only = sm->static_mapping_only;
112     rmp->static_mapping_connection_tracking =
113       sm->static_mapping_connection_tracking;
114     rmp->deterministic = sm->deterministic;
115   }));
116   /* *INDENT-ON* */
117 }
118
119 static void *
120 vl_api_nat_show_config_t_print (vl_api_nat_show_config_t * mp, void *handle)
121 {
122   u8 *s;
123
124   s = format (0, "SCRIPT: nat_show_config ");
125
126   FINISH;
127 }
128
129 static void
130 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
131 {
132   snat_main_t *sm = &snat_main;
133   vl_api_nat_set_workers_reply_t *rmp;
134   int rv = 0;
135   uword *bitmap = 0;
136   u64 mask;
137
138   if (sm->deterministic)
139     {
140       rv = VNET_API_ERROR_UNSUPPORTED;
141       goto send_reply;
142     }
143
144   mask = clib_net_to_host_u64 (mp->worker_mask);
145
146   if (sm->num_workers < 2)
147     {
148       rv = VNET_API_ERROR_FEATURE_DISABLED;
149       goto send_reply;
150     }
151
152   bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
153   rv = snat_set_workers (bitmap);
154   clib_bitmap_free (bitmap);
155
156 send_reply:
157   REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
158 }
159
160 static void *
161 vl_api_nat_set_workers_t_print (vl_api_nat_set_workers_t * mp, void *handle)
162 {
163   u8 *s;
164   uword *bitmap = 0;
165   u8 first = 1;
166   int i;
167   u64 mask = clib_net_to_host_u64 (mp->worker_mask);
168
169   s = format (0, "SCRIPT: nat_set_workers ");
170   bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
171   /* *INDENT-OFF* */
172   clib_bitmap_foreach (i, bitmap,
173     ({
174       if (first)
175         s = format (s, "%d", i);
176       else
177         s = format (s, ",%d", i);
178       first = 0;
179     }));
180   /* *INDENT-ON* */
181   clib_bitmap_free (bitmap);
182   FINISH;
183 }
184
185 static void
186 send_nat_worker_details (u32 worker_index, vl_api_registration_t * reg,
187                          u32 context)
188 {
189   vl_api_nat_worker_details_t *rmp;
190   snat_main_t *sm = &snat_main;
191   vlib_worker_thread_t *w =
192     vlib_worker_threads + worker_index + sm->first_worker_index;
193
194   rmp = vl_msg_api_alloc (sizeof (*rmp));
195   memset (rmp, 0, sizeof (*rmp));
196   rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
197   rmp->context = context;
198   rmp->worker_index = htonl (worker_index);
199   rmp->lcore_id = htonl (w->lcore_id);
200   strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
201
202   vl_api_send_msg (reg, (u8 *) rmp);
203 }
204
205 static void
206 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
207 {
208   vl_api_registration_t *reg;
209   snat_main_t *sm = &snat_main;
210   u32 *worker_index;
211
212   if (sm->deterministic)
213     return;
214
215   reg = vl_api_client_index_to_registration (mp->client_index);
216   if (!reg)
217     return;
218
219   /* *INDENT-OFF* */
220   vec_foreach (worker_index, sm->workers)
221     send_nat_worker_details(*worker_index, reg, mp->context);
222   /* *INDENT-ON* */
223 }
224
225 static void *
226 vl_api_nat_worker_dump_t_print (vl_api_nat_worker_dump_t * mp, void *handle)
227 {
228   u8 *s;
229
230   s = format (0, "SCRIPT: nat_worker_dump ");
231
232   FINISH;
233 }
234
235 static void
236 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
237                                            mp)
238 {
239   snat_main_t *sm = &snat_main;
240   vl_api_nat_ipfix_enable_disable_reply_t *rmp;
241   int rv = 0;
242
243   rv = snat_ipfix_logging_enable_disable (mp->enable,
244                                           clib_host_to_net_u32
245                                           (mp->domain_id),
246                                           clib_host_to_net_u16
247                                           (mp->src_port));
248
249   REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
250 }
251
252 static void *
253 vl_api_nat_ipfix_enable_disable_t_print (vl_api_nat_ipfix_enable_disable_t *
254                                          mp, void *handle)
255 {
256   u8 *s;
257
258   s = format (0, "SCRIPT: nat_ipfix_enable_disable ");
259   if (mp->domain_id)
260     s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
261   if (mp->src_port)
262     s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
263   if (!mp->enable)
264     s = format (s, "disable ");
265
266   FINISH;
267 }
268
269 static void
270 vl_api_nat_set_reass_t_handler (vl_api_nat_set_reass_t * mp)
271 {
272   snat_main_t *sm = &snat_main;
273   vl_api_nat_set_reass_reply_t *rmp;
274   int rv = 0;
275
276   rv =
277     nat_reass_set (ntohl (mp->timeout), ntohs (mp->max_reass), mp->max_frag,
278                    mp->drop_frag, mp->is_ip6);
279
280   REPLY_MACRO (VL_API_NAT_SET_REASS_REPLY);
281 }
282
283 static void *
284 vl_api_nat_set_reass_t_print (vl_api_nat_set_reass_t * mp, void *handle)
285 {
286   u8 *s;
287
288   s = format (0, "SCRIPT: nat_set_reass ");
289   s = format (s, "timeout %d max_reass %d max_frag %d drop_frag %d is_ip6 %d",
290               clib_host_to_net_u32 (mp->timeout),
291               clib_host_to_net_u16 (mp->max_reass),
292               mp->max_frag, mp->drop_frag, mp->is_ip6);
293
294   FINISH;
295 }
296
297 static void
298 vl_api_nat_get_reass_t_handler (vl_api_nat_get_reass_t * mp)
299 {
300   snat_main_t *sm = &snat_main;
301   vl_api_nat_get_reass_reply_t *rmp;
302   int rv = 0;
303
304   /* *INDENT-OFF* */
305   REPLY_MACRO2 (VL_API_NAT_GET_REASS_REPLY,
306   ({
307     rmp->ip4_timeout = htonl (nat_reass_get_timeout(0));
308     rmp->ip4_max_reass = htons (nat_reass_get_max_reass(0));
309     rmp->ip4_max_frag = nat_reass_get_max_frag(0);
310     rmp->ip4_drop_frag = nat_reass_is_drop_frag(0);
311     rmp->ip6_timeout = htonl (nat_reass_get_timeout(1));
312     rmp->ip6_max_reass = htons (nat_reass_get_max_reass(1));
313     rmp->ip6_max_frag = nat_reass_get_max_frag(1);
314     rmp->ip6_drop_frag = nat_reass_is_drop_frag(1);
315   }))
316   /* *INDENT-ON* */
317 }
318
319 static void *
320 vl_api_nat_get_reass_t_print (vl_api_nat_get_reass_t * mp, void *handle)
321 {
322   u8 *s;
323
324   s = format (0, "SCRIPT: nat_get_reass");
325
326   FINISH;
327 }
328
329 typedef struct nat_api_walk_ctx_t_
330 {
331   vl_api_registration_t *reg;
332   u32 context;
333 } nat_api_walk_ctx_t;
334
335 static int
336 nat_ip4_reass_walk_api (nat_reass_ip4_t * reass, void *arg)
337 {
338   vl_api_nat_reass_details_t *rmp;
339   snat_main_t *sm = &snat_main;
340   nat_api_walk_ctx_t *ctx = arg;
341
342   rmp = vl_msg_api_alloc (sizeof (*rmp));
343   memset (rmp, 0, sizeof (*rmp));
344   rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
345   rmp->context = ctx->context;
346   clib_memcpy (rmp->src_addr, &(reass->key.src), 4);
347   clib_memcpy (rmp->dst_addr, &(reass->key.dst), 4);
348   rmp->proto = reass->key.proto;
349   rmp->frag_id = ntohl (reass->key.frag_id);
350   rmp->frag_n = reass->frag_n;
351   rmp->is_ip4 = 1;
352
353   vl_api_send_msg (ctx->reg, (u8 *) rmp);
354
355   return 0;
356 }
357
358 static int
359 nat_ip6_reass_walk_api (nat_reass_ip6_t * reass, void *arg)
360 {
361   vl_api_nat_reass_details_t *rmp;
362   snat_main_t *sm = &snat_main;
363   nat_api_walk_ctx_t *ctx = arg;
364
365   rmp = vl_msg_api_alloc (sizeof (*rmp));
366   memset (rmp, 0, sizeof (*rmp));
367   rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
368   rmp->context = ctx->context;
369   clib_memcpy (rmp->src_addr, &(reass->key.src), 16);
370   clib_memcpy (rmp->dst_addr, &(reass->key.dst), 16);
371   rmp->proto = reass->key.proto;
372   rmp->frag_id = ntohl (reass->key.frag_id);
373   rmp->frag_n = reass->frag_n;
374   rmp->is_ip4 = 0;
375
376   vl_api_send_msg (ctx->reg, (u8 *) rmp);
377
378   return 0;
379 }
380
381 static void
382 vl_api_nat_reass_dump_t_handler (vl_api_nat_reass_dump_t * mp)
383 {
384   vl_api_registration_t *reg;
385
386   reg = vl_api_client_index_to_registration (mp->client_index);
387   if (!reg)
388     return;
389
390   nat_api_walk_ctx_t ctx = {
391     .reg = reg,
392     .context = mp->context,
393   };
394
395   nat_ip4_reass_walk (nat_ip4_reass_walk_api, &ctx);
396   nat_ip6_reass_walk (nat_ip6_reass_walk_api, &ctx);
397 }
398
399 static void *
400 vl_api_nat_reass_dump_t_print (vl_api_nat_reass_dump_t * mp, void *handle)
401 {
402   u8 *s;
403
404   s = format (0, "SCRIPT: nat_reass_dump");
405
406   FINISH;
407 }
408
409 /*************/
410 /*** NAT44 ***/
411 /*************/
412 static void
413   vl_api_nat44_add_del_address_range_t_handler
414   (vl_api_nat44_add_del_address_range_t * mp)
415 {
416   snat_main_t *sm = &snat_main;
417   vl_api_nat44_add_del_address_range_reply_t *rmp;
418   ip4_address_t this_addr;
419   u32 start_host_order, end_host_order;
420   u32 vrf_id;
421   int i, count;
422   int rv = 0;
423   u32 *tmp;
424
425   if (sm->deterministic)
426     {
427       rv = VNET_API_ERROR_UNSUPPORTED;
428       goto send_reply;
429     }
430
431   if (sm->static_mapping_only)
432     {
433       rv = VNET_API_ERROR_FEATURE_DISABLED;
434       goto send_reply;
435     }
436
437   tmp = (u32 *) mp->first_ip_address;
438   start_host_order = clib_host_to_net_u32 (tmp[0]);
439   tmp = (u32 *) mp->last_ip_address;
440   end_host_order = clib_host_to_net_u32 (tmp[0]);
441
442   count = (end_host_order - start_host_order) + 1;
443
444   vrf_id = clib_host_to_net_u32 (mp->vrf_id);
445
446   if (count > 1024)
447     clib_warning ("%U - %U, %d addresses...",
448                   format_ip4_address, mp->first_ip_address,
449                   format_ip4_address, mp->last_ip_address, count);
450
451   memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
452
453   for (i = 0; i < count; i++)
454     {
455       if (mp->is_add)
456         snat_add_address (sm, &this_addr, vrf_id, mp->twice_nat);
457       else
458         rv = snat_del_address (sm, this_addr, 0, mp->twice_nat);
459
460       if (rv)
461         goto send_reply;
462
463       if (sm->out2in_dpo)
464         nat44_add_del_address_dpo (this_addr, mp->is_add);
465
466       increment_v4_address (&this_addr);
467     }
468
469 send_reply:
470   REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
471 }
472
473 static void *vl_api_nat44_add_del_address_range_t_print
474   (vl_api_nat44_add_del_address_range_t * mp, void *handle)
475 {
476   u8 *s;
477
478   s = format (0, "SCRIPT: nat44_add_address_range ");
479   s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
480   if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
481     {
482       s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
483     }
484   s = format (s, "twice_nat %d ", mp->twice_nat);
485   FINISH;
486 }
487
488 static void
489 send_nat44_address_details (snat_address_t * a,
490                             vl_api_registration_t * reg, u32 context,
491                             u8 twice_nat)
492 {
493   vl_api_nat44_address_details_t *rmp;
494   snat_main_t *sm = &snat_main;
495
496   rmp = vl_msg_api_alloc (sizeof (*rmp));
497   memset (rmp, 0, sizeof (*rmp));
498   rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
499   clib_memcpy (rmp->ip_address, &(a->addr), 4);
500   if (a->fib_index != ~0)
501     {
502       fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
503       rmp->vrf_id = ntohl (fib->ft_table_id);
504     }
505   else
506     rmp->vrf_id = ~0;
507   rmp->twice_nat = twice_nat;
508   rmp->context = context;
509
510   vl_api_send_msg (reg, (u8 *) rmp);
511 }
512
513 static void
514 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
515 {
516   vl_api_registration_t *reg;
517   snat_main_t *sm = &snat_main;
518   snat_address_t *a;
519
520   if (sm->deterministic)
521     return;
522
523   reg = vl_api_client_index_to_registration (mp->client_index);
524   if (!reg)
525     return;
526
527   /* *INDENT-OFF* */
528   vec_foreach (a, sm->addresses)
529     send_nat44_address_details (a, reg, mp->context, 0);
530   vec_foreach (a, sm->twice_nat_addresses)
531     send_nat44_address_details (a, reg, mp->context, 1);
532   /* *INDENT-ON* */
533 }
534
535 static void *
536 vl_api_nat44_address_dump_t_print (vl_api_nat44_address_dump_t * mp,
537                                    void *handle)
538 {
539   u8 *s;
540
541   s = format (0, "SCRIPT: nat44_address_dump ");
542
543   FINISH;
544 }
545
546 static void
547   vl_api_nat44_interface_add_del_feature_t_handler
548   (vl_api_nat44_interface_add_del_feature_t * mp)
549 {
550   snat_main_t *sm = &snat_main;
551   vl_api_nat44_interface_add_del_feature_reply_t *rmp;
552   u8 is_del = mp->is_add == 0;
553   u32 sw_if_index = ntohl (mp->sw_if_index);
554   int rv = 0;
555
556   VALIDATE_SW_IF_INDEX (mp);
557
558   rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
559
560   BAD_SW_IF_INDEX_LABEL;
561
562   REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
563 }
564
565 static void *vl_api_nat44_interface_add_del_feature_t_print
566   (vl_api_nat44_interface_add_del_feature_t * mp, void *handle)
567 {
568   u8 *s;
569
570   s = format (0, "SCRIPT: nat44_interface_add_del_feature ");
571   s = format (s, "sw_if_index %d %s %s",
572               clib_host_to_net_u32 (mp->sw_if_index),
573               mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
574
575   FINISH;
576 }
577
578 static void
579 send_nat44_interface_details (snat_interface_t * i,
580                               vl_api_registration_t * reg, u32 context)
581 {
582   vl_api_nat44_interface_details_t *rmp;
583   snat_main_t *sm = &snat_main;
584
585   rmp = vl_msg_api_alloc (sizeof (*rmp));
586   memset (rmp, 0, sizeof (*rmp));
587   rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
588   rmp->sw_if_index = ntohl (i->sw_if_index);
589   rmp->is_inside = (nat_interface_is_inside (i)
590                     && nat_interface_is_outside (i)) ? 2 :
591     nat_interface_is_inside (i);
592   rmp->context = context;
593
594   vl_api_send_msg (reg, (u8 *) rmp);
595 }
596
597 static void
598 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
599 {
600   vl_api_registration_t *reg;
601   snat_main_t *sm = &snat_main;
602   snat_interface_t *i;
603
604   reg = vl_api_client_index_to_registration (mp->client_index);
605   if (!reg)
606     return;
607
608   /* *INDENT-OFF* */
609   pool_foreach (i, sm->interfaces,
610   ({
611     send_nat44_interface_details(i, reg, mp->context);
612   }));
613   /* *INDENT-ON* */
614 }
615
616 static void *
617 vl_api_nat44_interface_dump_t_print (vl_api_nat44_interface_dump_t * mp,
618                                      void *handle)
619 {
620   u8 *s;
621
622   s = format (0, "SCRIPT: nat44_interface_dump ");
623
624   FINISH;
625 }
626
627 static void
628   vl_api_nat44_interface_add_del_output_feature_t_handler
629   (vl_api_nat44_interface_add_del_output_feature_t * mp)
630 {
631   snat_main_t *sm = &snat_main;
632   vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
633   u8 is_del = mp->is_add == 0;
634   u32 sw_if_index = ntohl (mp->sw_if_index);
635   int rv = 0;
636
637   if (sm->deterministic)
638     {
639       rv = VNET_API_ERROR_UNSUPPORTED;
640       goto send_reply;
641     }
642
643   VALIDATE_SW_IF_INDEX (mp);
644
645   rv = snat_interface_add_del_output_feature (sw_if_index, mp->is_inside,
646                                               is_del);
647
648   BAD_SW_IF_INDEX_LABEL;
649 send_reply:
650   REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
651 }
652
653 static void *vl_api_nat44_interface_add_del_output_feature_t_print
654   (vl_api_nat44_interface_add_del_output_feature_t * mp, void *handle)
655 {
656   u8 *s;
657
658   s = format (0, "SCRIPT: nat44_interface_add_del_output_feature ");
659   s = format (s, "sw_if_index %d %s %s",
660               clib_host_to_net_u32 (mp->sw_if_index),
661               mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
662
663   FINISH;
664 }
665
666 static void
667 send_nat44_interface_output_feature_details (snat_interface_t * i,
668                                              vl_api_registration_t * reg,
669                                              u32 context)
670 {
671   vl_api_nat44_interface_output_feature_details_t *rmp;
672   snat_main_t *sm = &snat_main;
673
674   rmp = vl_msg_api_alloc (sizeof (*rmp));
675   memset (rmp, 0, sizeof (*rmp));
676   rmp->_vl_msg_id =
677     ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
678   rmp->sw_if_index = ntohl (i->sw_if_index);
679   rmp->context = context;
680   rmp->is_inside = nat_interface_is_inside (i);
681
682   vl_api_send_msg (reg, (u8 *) rmp);
683 }
684
685 static void
686   vl_api_nat44_interface_output_feature_dump_t_handler
687   (vl_api_nat44_interface_output_feature_dump_t * mp)
688 {
689   vl_api_registration_t *reg;
690   snat_main_t *sm = &snat_main;
691   snat_interface_t *i;
692
693   if (sm->deterministic)
694     return;
695
696   reg = vl_api_client_index_to_registration (mp->client_index);
697   if (!reg)
698     return;
699
700   /* *INDENT-OFF* */
701   pool_foreach (i, sm->output_feature_interfaces,
702   ({
703     send_nat44_interface_output_feature_details(i, reg, mp->context);
704   }));
705   /* *INDENT-ON* */
706 }
707
708 static void *vl_api_nat44_interface_output_feature_dump_t_print
709   (vl_api_nat44_interface_output_feature_dump_t * mp, void *handle)
710 {
711   u8 *s;
712
713   s = format (0, "SCRIPT: nat44_interface_output_feature_dump ");
714
715   FINISH;
716 }
717
718 static void
719   vl_api_nat44_add_del_static_mapping_t_handler
720   (vl_api_nat44_add_del_static_mapping_t * mp)
721 {
722   snat_main_t *sm = &snat_main;
723   vl_api_nat44_add_del_static_mapping_reply_t *rmp;
724   ip4_address_t local_addr, external_addr;
725   u16 local_port = 0, external_port = 0;
726   u32 vrf_id, external_sw_if_index;
727   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
728   int rv = 0;
729   snat_protocol_t proto;
730   u8 *tag = 0;
731
732   if (sm->deterministic)
733     {
734       rv = VNET_API_ERROR_UNSUPPORTED;
735       goto send_reply;
736     }
737
738   memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
739   memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
740   if (mp->addr_only == 0)
741     {
742       local_port = clib_net_to_host_u16 (mp->local_port);
743       external_port = clib_net_to_host_u16 (mp->external_port);
744     }
745   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
746   external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
747   proto = ip_proto_to_snat_proto (mp->protocol);
748   if (mp->twice_nat)
749     twice_nat = TWICE_NAT;
750   else if (mp->self_twice_nat)
751     twice_nat = TWICE_NAT_SELF;
752   mp->tag[sizeof (mp->tag) - 1] = 0;
753   tag = format (0, "%s", mp->tag);
754   vec_terminate_c_string (tag);
755
756   rv = snat_add_static_mapping (local_addr, external_addr, local_port,
757                                 external_port, vrf_id, mp->addr_only,
758                                 external_sw_if_index, proto, mp->is_add,
759                                 twice_nat, mp->out2in_only, tag);
760
761   vec_free (tag);
762
763 send_reply:
764   REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
765 }
766
767 static void *vl_api_nat44_add_del_static_mapping_t_print
768   (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
769 {
770   u8 *s;
771
772   s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
773   s = format (s, "protocol %d local_addr %U external_addr %U ",
774               mp->protocol,
775               format_ip4_address, mp->local_ip_address,
776               format_ip4_address, mp->external_ip_address);
777
778   if (mp->addr_only == 0)
779     s = format (s, "local_port %d external_port %d ",
780                 clib_net_to_host_u16 (mp->local_port),
781                 clib_net_to_host_u16 (mp->external_port));
782
783   s = format (s, "twice_nat %d out2in_only %d ",
784               mp->twice_nat, mp->out2in_only);
785
786   if (mp->vrf_id != ~0)
787     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
788
789   if (mp->external_sw_if_index != ~0)
790     s = format (s, "external_sw_if_index %d",
791                 clib_net_to_host_u32 (mp->external_sw_if_index));
792   FINISH;
793 }
794
795 static void
796 send_nat44_static_mapping_details (snat_static_mapping_t * m,
797                                    vl_api_registration_t * reg, u32 context)
798 {
799   vl_api_nat44_static_mapping_details_t *rmp;
800   snat_main_t *sm = &snat_main;
801
802   rmp = vl_msg_api_alloc (sizeof (*rmp));
803   memset (rmp, 0, sizeof (*rmp));
804   rmp->_vl_msg_id =
805     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
806   rmp->addr_only = m->addr_only;
807   clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
808   clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
809   rmp->external_sw_if_index = ~0;
810   rmp->vrf_id = htonl (m->vrf_id);
811   rmp->context = context;
812   if (m->twice_nat == TWICE_NAT)
813     rmp->twice_nat = 1;
814   else if (m->twice_nat == TWICE_NAT_SELF)
815     rmp->self_twice_nat = 1;
816   rmp->out2in_only = m->out2in_only;
817   if (m->addr_only == 0)
818     {
819       rmp->protocol = snat_proto_to_ip_proto (m->proto);
820       rmp->external_port = htons (m->external_port);
821       rmp->local_port = htons (m->local_port);
822     }
823   if (m->tag)
824     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
825
826   vl_api_send_msg (reg, (u8 *) rmp);
827 }
828
829 static void
830 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
831                                        vl_api_registration_t * reg,
832                                        u32 context)
833 {
834   vl_api_nat44_static_mapping_details_t *rmp;
835   snat_main_t *sm = &snat_main;
836
837   rmp = vl_msg_api_alloc (sizeof (*rmp));
838   memset (rmp, 0, sizeof (*rmp));
839   rmp->_vl_msg_id =
840     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
841   rmp->addr_only = m->addr_only;
842   clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
843   rmp->external_sw_if_index = htonl (m->sw_if_index);
844   rmp->vrf_id = htonl (m->vrf_id);
845   rmp->context = context;
846   rmp->twice_nat = m->twice_nat;
847   if (m->addr_only == 0)
848     {
849       rmp->protocol = snat_proto_to_ip_proto (m->proto);
850       rmp->external_port = htons (m->e_port);
851       rmp->local_port = htons (m->l_port);
852     }
853   if (m->tag)
854     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
855
856   vl_api_send_msg (reg, (u8 *) rmp);
857 }
858
859 static void
860 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
861                                             * mp)
862 {
863   vl_api_registration_t *reg;
864   snat_main_t *sm = &snat_main;
865   snat_static_mapping_t *m;
866   snat_static_map_resolve_t *rp;
867   int j;
868
869   if (sm->deterministic)
870     return;
871
872   reg = vl_api_client_index_to_registration (mp->client_index);
873   if (!reg)
874     return;
875
876   /* *INDENT-OFF* */
877   pool_foreach (m, sm->static_mappings,
878   ({
879       if (!vec_len (m->locals) &&
880           ((m->local_port != m->external_port)
881            || (m->local_addr.as_u32 != m->external_addr.as_u32)))
882         send_nat44_static_mapping_details (m, reg, mp->context);
883   }));
884   /* *INDENT-ON* */
885
886   for (j = 0; j < vec_len (sm->to_resolve); j++)
887     {
888       rp = sm->to_resolve + j;
889       if (rp->l_addr.as_u32 != 0)
890         send_nat44_static_map_resolve_details (rp, reg, mp->context);
891     }
892 }
893
894 static void *
895 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
896                                           mp, void *handle)
897 {
898   u8 *s;
899
900   s = format (0, "SCRIPT: nat44_static_mapping_dump ");
901
902   FINISH;
903 }
904
905 static void
906   vl_api_nat44_add_del_identity_mapping_t_handler
907   (vl_api_nat44_add_del_identity_mapping_t * mp)
908 {
909   snat_main_t *sm = &snat_main;
910   vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
911   ip4_address_t addr;
912   u16 port = 0;
913   u32 vrf_id, sw_if_index;
914   int rv = 0;
915   snat_protocol_t proto = ~0;
916   u8 *tag = 0;
917
918   if (sm->deterministic)
919     {
920       rv = VNET_API_ERROR_UNSUPPORTED;
921       goto send_reply;
922     }
923
924   if (mp->addr_only == 0)
925     {
926       port = clib_net_to_host_u16 (mp->port);
927       proto = ip_proto_to_snat_proto (mp->protocol);
928     }
929   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
930   sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
931   if (sw_if_index != ~0)
932     addr.as_u32 = 0;
933   else
934     memcpy (&addr.as_u8, mp->ip_address, 4);
935   mp->tag[sizeof (mp->tag) - 1] = 0;
936   tag = format (0, "%s", mp->tag);
937   vec_terminate_c_string (tag);
938
939   rv =
940     snat_add_static_mapping (addr, addr, port, port, vrf_id, mp->addr_only,
941                              sw_if_index, proto, mp->is_add, 0, 0, tag);
942
943   vec_free (tag);
944
945 send_reply:
946   REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
947 }
948
949 static void *vl_api_nat44_add_del_identity_mapping_t_print
950   (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
951 {
952   u8 *s;
953
954   s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
955   if (mp->sw_if_index != ~0)
956     s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
957   else
958     s = format (s, "addr %U", format_ip4_address, mp->ip_address);
959
960   if (mp->addr_only == 0)
961     s =
962       format (s, "protocol %d port %d", mp->protocol,
963               clib_net_to_host_u16 (mp->port));
964
965   if (mp->vrf_id != ~0)
966     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
967
968   FINISH;
969 }
970
971 static void
972 send_nat44_identity_mapping_details (snat_static_mapping_t * m,
973                                      vl_api_registration_t * reg, u32 context)
974 {
975   vl_api_nat44_identity_mapping_details_t *rmp;
976   snat_main_t *sm = &snat_main;
977
978   rmp = vl_msg_api_alloc (sizeof (*rmp));
979   memset (rmp, 0, sizeof (*rmp));
980   rmp->_vl_msg_id =
981     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
982   rmp->addr_only = m->addr_only;
983   clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
984   rmp->port = htons (m->local_port);
985   rmp->sw_if_index = ~0;
986   rmp->vrf_id = htonl (m->vrf_id);
987   rmp->protocol = snat_proto_to_ip_proto (m->proto);
988   rmp->context = context;
989   if (m->tag)
990     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
991
992   vl_api_send_msg (reg, (u8 *) rmp);
993 }
994
995 static void
996 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
997                                          vl_api_registration_t * reg,
998                                          u32 context)
999 {
1000   vl_api_nat44_identity_mapping_details_t *rmp;
1001   snat_main_t *sm = &snat_main;
1002
1003   rmp = vl_msg_api_alloc (sizeof (*rmp));
1004   memset (rmp, 0, sizeof (*rmp));
1005   rmp->_vl_msg_id =
1006     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1007   rmp->addr_only = m->addr_only;
1008   rmp->port = htons (m->l_port);
1009   rmp->sw_if_index = htonl (m->sw_if_index);
1010   rmp->vrf_id = htonl (m->vrf_id);
1011   rmp->protocol = snat_proto_to_ip_proto (m->proto);
1012   rmp->context = context;
1013   if (m->tag)
1014     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1015
1016   vl_api_send_msg (reg, (u8 *) rmp);
1017 }
1018
1019 static void
1020   vl_api_nat44_identity_mapping_dump_t_handler
1021   (vl_api_nat44_identity_mapping_dump_t * mp)
1022 {
1023   vl_api_registration_t *reg;
1024   snat_main_t *sm = &snat_main;
1025   snat_static_mapping_t *m;
1026   snat_static_map_resolve_t *rp;
1027   int j;
1028
1029   if (sm->deterministic)
1030     return;
1031
1032   reg = vl_api_client_index_to_registration (mp->client_index);
1033   if (!reg)
1034     return;
1035
1036   /* *INDENT-OFF* */
1037   pool_foreach (m, sm->static_mappings,
1038   ({
1039       if (!vec_len (m->locals) && (m->local_port == m->external_port)
1040           && (m->local_addr.as_u32 == m->external_addr.as_u32))
1041         send_nat44_identity_mapping_details (m, reg, mp->context);
1042   }));
1043   /* *INDENT-ON* */
1044
1045   for (j = 0; j < vec_len (sm->to_resolve); j++)
1046     {
1047       rp = sm->to_resolve + j;
1048       if (rp->l_addr.as_u32 == 0)
1049         send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1050     }
1051 }
1052
1053 static void *vl_api_nat44_identity_mapping_dump_t_print
1054   (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1055 {
1056   u8 *s;
1057
1058   s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1059
1060   FINISH;
1061 }
1062
1063 static void
1064   vl_api_nat44_add_del_interface_addr_t_handler
1065   (vl_api_nat44_add_del_interface_addr_t * mp)
1066 {
1067   snat_main_t *sm = &snat_main;
1068   vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1069   u8 is_del = mp->is_add == 0;
1070   u32 sw_if_index = ntohl (mp->sw_if_index);
1071   int rv = 0;
1072
1073   if (sm->deterministic)
1074     {
1075       rv = VNET_API_ERROR_UNSUPPORTED;
1076       goto send_reply;
1077     }
1078
1079   VALIDATE_SW_IF_INDEX (mp);
1080
1081   rv = snat_add_interface_address (sm, sw_if_index, is_del, mp->twice_nat);
1082
1083   BAD_SW_IF_INDEX_LABEL;
1084 send_reply:
1085   REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1086 }
1087
1088 static void *vl_api_nat44_add_del_interface_addr_t_print
1089   (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1090 {
1091   u8 *s;
1092
1093   s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1094   s = format (s, "sw_if_index %d twice_nat %d %s",
1095               clib_host_to_net_u32 (mp->sw_if_index),
1096               mp->twice_nat, mp->is_add ? "" : "del");
1097
1098   FINISH;
1099 }
1100
1101 static void
1102 send_nat44_interface_addr_details (u32 sw_if_index,
1103                                    vl_api_registration_t * reg, u32 context,
1104                                    u8 twice_nat)
1105 {
1106   vl_api_nat44_interface_addr_details_t *rmp;
1107   snat_main_t *sm = &snat_main;
1108
1109   rmp = vl_msg_api_alloc (sizeof (*rmp));
1110   memset (rmp, 0, sizeof (*rmp));
1111   rmp->_vl_msg_id =
1112     ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1113   rmp->sw_if_index = ntohl (sw_if_index);
1114   rmp->twice_nat = twice_nat;
1115   rmp->context = context;
1116
1117   vl_api_send_msg (reg, (u8 *) rmp);
1118 }
1119
1120 static void
1121 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1122                                             * mp)
1123 {
1124   vl_api_registration_t *reg;
1125   snat_main_t *sm = &snat_main;
1126   u32 *i;
1127
1128   if (sm->deterministic)
1129     return;
1130
1131   reg = vl_api_client_index_to_registration (mp->client_index);
1132   if (!reg)
1133     return;
1134
1135   /* *INDENT-OFF* */
1136   vec_foreach (i, sm->auto_add_sw_if_indices)
1137     send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1138   vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1139     send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1140   /* *INDENT-ON* */
1141 }
1142
1143 static void *
1144 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1145                                           mp, void *handle)
1146 {
1147   u8 *s;
1148
1149   s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1150
1151   FINISH;
1152 }
1153
1154 static void
1155 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1156                          u32 context)
1157 {
1158   vl_api_nat44_user_details_t *rmp;
1159   snat_main_t *sm = &snat_main;
1160   fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1161
1162   rmp = vl_msg_api_alloc (sizeof (*rmp));
1163   memset (rmp, 0, sizeof (*rmp));
1164   rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1165
1166   rmp->vrf_id = ntohl (fib->ft_table_id);
1167
1168   clib_memcpy (rmp->ip_address, &(u->addr), 4);
1169   rmp->nsessions = ntohl (u->nsessions);
1170   rmp->nstaticsessions = ntohl (u->nstaticsessions);
1171   rmp->context = context;
1172
1173   vl_api_send_msg (reg, (u8 *) rmp);
1174 }
1175
1176 static void
1177 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1178 {
1179   vl_api_registration_t *reg;
1180   snat_main_t *sm = &snat_main;
1181   snat_main_per_thread_data_t *tsm;
1182   snat_user_t *u;
1183
1184   if (sm->deterministic)
1185     return;
1186
1187   reg = vl_api_client_index_to_registration (mp->client_index);
1188   if (!reg)
1189     return;
1190
1191   /* *INDENT-OFF* */
1192   vec_foreach (tsm, sm->per_thread_data)
1193     vec_foreach (u, tsm->users)
1194       send_nat44_user_details (u, reg, mp->context);
1195   /* *INDENT-ON* */
1196 }
1197
1198 static void *
1199 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1200 {
1201   u8 *s;
1202
1203   s = format (0, "SCRIPT: nat44_user_dump ");
1204
1205   FINISH;
1206 }
1207
1208 static void
1209 send_nat44_user_session_details (snat_session_t * s,
1210                                  vl_api_registration_t * reg, u32 context)
1211 {
1212   vl_api_nat44_user_session_details_t *rmp;
1213   snat_main_t *sm = &snat_main;
1214
1215   rmp = vl_msg_api_alloc (sizeof (*rmp));
1216   memset (rmp, 0, sizeof (*rmp));
1217   rmp->_vl_msg_id =
1218     ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1219   clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1220   clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1221   rmp->is_static = s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING ? 1 : 0;
1222   rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1223   rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1224   rmp->total_pkts = ntohl (s->total_pkts);
1225   rmp->context = context;
1226   if (snat_is_unk_proto_session (s))
1227     {
1228       rmp->outside_port = 0;
1229       rmp->inside_port = 0;
1230       rmp->protocol = ntohs (s->in2out.port);
1231     }
1232   else
1233     {
1234       rmp->outside_port = s->out2in.port;
1235       rmp->inside_port = s->in2out.port;
1236       rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
1237     }
1238
1239   vl_api_send_msg (reg, (u8 *) rmp);
1240 }
1241
1242 static void
1243 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1244                                           mp)
1245 {
1246   vl_api_registration_t *reg;
1247   snat_main_t *sm = &snat_main;
1248   snat_main_per_thread_data_t *tsm;
1249   snat_session_t *s;
1250   clib_bihash_kv_8_8_t key, value;
1251   snat_user_key_t ukey;
1252   snat_user_t *u;
1253   u32 session_index, head_index, elt_index;
1254   dlist_elt_t *head, *elt;
1255   ip4_header_t ip;
1256
1257   if (sm->deterministic)
1258     return;
1259
1260   reg = vl_api_client_index_to_registration (mp->client_index);
1261   if (!reg)
1262     return;
1263
1264   clib_memcpy (&ukey.addr, mp->ip_address, 4);
1265   ip.src_address.as_u32 = ukey.addr.as_u32;
1266   ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1267   key.key = ukey.as_u64;
1268   if (sm->num_workers > 1)
1269     tsm =
1270       vec_elt_at_index (sm->per_thread_data,
1271                         sm->worker_in2out_cb (&ip, ukey.fib_index));
1272   else
1273     tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1274   if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1275     return;
1276   u = pool_elt_at_index (tsm->users, value.value);
1277   if (!u->nsessions && !u->nstaticsessions)
1278     return;
1279
1280   head_index = u->sessions_per_user_list_head_index;
1281   head = pool_elt_at_index (tsm->list_pool, head_index);
1282   elt_index = head->next;
1283   elt = pool_elt_at_index (tsm->list_pool, elt_index);
1284   session_index = elt->value;
1285   while (session_index != ~0)
1286     {
1287       s = pool_elt_at_index (tsm->sessions, session_index);
1288
1289       send_nat44_user_session_details (s, reg, mp->context);
1290
1291       elt_index = elt->next;
1292       elt = pool_elt_at_index (tsm->list_pool, elt_index);
1293       session_index = elt->value;
1294     }
1295 }
1296
1297 static void *
1298 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1299                                         void *handle)
1300 {
1301   u8 *s;
1302
1303   s = format (0, "SCRIPT: nat44_user_session_dump ");
1304   s = format (s, "ip_address %U vrf_id %d\n",
1305               format_ip4_address, mp->ip_address,
1306               clib_net_to_host_u32 (mp->vrf_id));
1307
1308   FINISH;
1309 }
1310
1311 static nat44_lb_addr_port_t *
1312 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1313                              u8 addr_port_pair_num)
1314 {
1315   u8 i;
1316   nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1317   vl_api_nat44_lb_addr_port_t *ap;
1318
1319   for (i = 0; i < addr_port_pair_num; i++)
1320     {
1321       ap = &addr_port_pairs[i];
1322       memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1323       clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1324       lb_addr_port.port = clib_net_to_host_u16 (ap->port);
1325       lb_addr_port.probability = ap->probability;
1326       vec_add1 (lb_addr_port_pairs, lb_addr_port);
1327     }
1328
1329   return lb_addr_port_pairs;
1330 }
1331
1332 static void
1333   vl_api_nat44_add_del_lb_static_mapping_t_handler
1334   (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1335 {
1336   snat_main_t *sm = &snat_main;
1337   vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1338   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1339   int rv = 0;
1340   nat44_lb_addr_port_t *locals = 0;
1341   ip4_address_t e_addr;
1342   snat_protocol_t proto;
1343   u8 *tag = 0;
1344
1345   if (sm->deterministic)
1346     {
1347       rv = VNET_API_ERROR_UNSUPPORTED;
1348       goto send_reply;
1349     }
1350
1351   locals = unformat_nat44_lb_addr_port (mp->locals, mp->local_num);
1352   clib_memcpy (&e_addr, mp->external_addr, 4);
1353   proto = ip_proto_to_snat_proto (mp->protocol);
1354   if (mp->twice_nat)
1355     twice_nat = TWICE_NAT;
1356   else if (mp->self_twice_nat)
1357     twice_nat = TWICE_NAT_SELF;
1358   mp->tag[sizeof (mp->tag) - 1] = 0;
1359   tag = format (0, "%s", mp->tag);
1360   vec_terminate_c_string (tag);
1361
1362   rv =
1363     nat44_add_del_lb_static_mapping (e_addr,
1364                                      clib_net_to_host_u16 (mp->external_port),
1365                                      proto, clib_net_to_host_u32 (mp->vrf_id),
1366                                      locals, mp->is_add, twice_nat,
1367                                      mp->out2in_only, tag);
1368
1369   vec_free (locals);
1370   vec_free (tag);
1371
1372 send_reply:
1373   REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1374 }
1375
1376 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1377   (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1378 {
1379   u8 *s;
1380
1381   s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1382   s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1383               mp->is_add, mp->twice_nat, mp->out2in_only);
1384
1385   FINISH;
1386 }
1387
1388 static void
1389 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1390                                       vl_api_registration_t * reg,
1391                                       u32 context)
1392 {
1393   vl_api_nat44_lb_static_mapping_details_t *rmp;
1394   snat_main_t *sm = &snat_main;
1395   nat44_lb_addr_port_t *ap;
1396   vl_api_nat44_lb_addr_port_t *locals;
1397
1398   rmp =
1399     vl_msg_api_alloc (sizeof (*rmp) +
1400                       (vec_len (m->locals) * sizeof (nat44_lb_addr_port_t)));
1401   memset (rmp, 0, sizeof (*rmp));
1402   rmp->_vl_msg_id =
1403     ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1404
1405   clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1406   rmp->external_port = ntohs (m->external_port);
1407   rmp->protocol = snat_proto_to_ip_proto (m->proto);
1408   rmp->vrf_id = ntohl (m->vrf_id);
1409   rmp->context = context;
1410   if (m->twice_nat == TWICE_NAT)
1411     rmp->twice_nat = 1;
1412   else if (m->twice_nat == TWICE_NAT_SELF)
1413     rmp->self_twice_nat = 1;
1414   rmp->out2in_only = m->out2in_only;
1415   if (m->tag)
1416     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1417
1418   locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
1419   vec_foreach (ap, m->locals)
1420   {
1421     clib_memcpy (locals->addr, &(ap->addr), 4);
1422     locals->port = htons (ap->port);
1423     locals->probability = ap->probability;
1424     locals++;
1425     rmp->local_num++;
1426   }
1427
1428   vl_api_send_msg (reg, (u8 *) rmp);
1429 }
1430
1431 static void
1432   vl_api_nat44_lb_static_mapping_dump_t_handler
1433   (vl_api_nat44_lb_static_mapping_dump_t * mp)
1434 {
1435   vl_api_registration_t *reg;
1436   snat_main_t *sm = &snat_main;
1437   snat_static_mapping_t *m;
1438
1439   if (sm->deterministic)
1440     return;
1441
1442   reg = vl_api_client_index_to_registration (mp->client_index);
1443   if (!reg)
1444     return;
1445
1446   /* *INDENT-OFF* */
1447   pool_foreach (m, sm->static_mappings,
1448   ({
1449       if (vec_len(m->locals))
1450         send_nat44_lb_static_mapping_details (m, reg, mp->context);
1451   }));
1452   /* *INDENT-ON* */
1453 }
1454
1455 static void *vl_api_nat44_lb_static_mapping_dump_t_print
1456   (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
1457 {
1458   u8 *s;
1459
1460   s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
1461
1462   FINISH;
1463 }
1464
1465 static void
1466 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
1467 {
1468   snat_main_t *sm = &snat_main;
1469   vl_api_nat44_del_session_reply_t *rmp;
1470   ip4_address_t addr;
1471   u16 port;
1472   u32 vrf_id;
1473   int rv = 0;
1474   snat_protocol_t proto;
1475
1476   if (sm->deterministic)
1477     {
1478       rv = VNET_API_ERROR_UNSUPPORTED;
1479       goto send_reply;
1480     }
1481
1482   memcpy (&addr.as_u8, mp->address, 4);
1483   port = clib_net_to_host_u16 (mp->port);
1484   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1485   proto = ip_proto_to_snat_proto (mp->protocol);
1486
1487   rv = nat44_del_session (sm, &addr, port, proto, vrf_id, mp->is_in);
1488
1489 send_reply:
1490   REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
1491 }
1492
1493 static void *
1494 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
1495                                   void *handle)
1496 {
1497   u8 *s;
1498
1499   s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1500   s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
1501               format_ip4_address, mp->address,
1502               clib_net_to_host_u16 (mp->port),
1503               mp->protocol, clib_net_to_host_u32 (mp->vrf_id), mp->is_in);
1504
1505   FINISH;
1506 }
1507
1508 static void
1509   vl_api_nat44_forwarding_enable_disable_t_handler
1510   (vl_api_nat44_forwarding_enable_disable_t * mp)
1511 {
1512   snat_main_t *sm = &snat_main;
1513   vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
1514   int rv = 0;
1515
1516   sm->forwarding_enabled = mp->enable != 0;
1517
1518   REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
1519 }
1520
1521 static void *vl_api_nat44_forwarding_enable_disable_t_print
1522   (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
1523 {
1524   u8 *s;
1525
1526   s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
1527   s = format (s, "enable %d", mp->enable != 0);
1528
1529   FINISH;
1530 }
1531
1532 static void
1533   vl_api_nat44_forwarding_is_enabled_t_handler
1534   (vl_api_nat44_forwarding_is_enabled_t * mp)
1535 {
1536   vl_api_registration_t *reg;
1537   snat_main_t *sm = &snat_main;
1538   vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
1539
1540   reg = vl_api_client_index_to_registration (mp->client_index);
1541   if (!reg)
1542     return;
1543
1544   rmp = vl_msg_api_alloc (sizeof (*rmp));
1545   memset (rmp, 0, sizeof (*rmp));
1546   rmp->_vl_msg_id =
1547     ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
1548   rmp->context = mp->context;
1549
1550   rmp->enabled = sm->forwarding_enabled;
1551
1552   vl_api_send_msg (reg, (u8 *) rmp);
1553 }
1554
1555 static void *vl_api_nat44_forwarding_is_enabled_t_print
1556   (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
1557 {
1558   u8 *s;
1559
1560   s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
1561
1562   FINISH;
1563 }
1564
1565 /*******************************/
1566 /*** Deterministic NAT (CGN) ***/
1567 /*******************************/
1568
1569 static void
1570 vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
1571 {
1572   snat_main_t *sm = &snat_main;
1573   vl_api_nat_det_add_del_map_reply_t *rmp;
1574   int rv = 0;
1575   ip4_address_t in_addr, out_addr;
1576
1577   if (!sm->deterministic)
1578     {
1579       rv = VNET_API_ERROR_UNSUPPORTED;
1580       goto send_reply;
1581     }
1582
1583   if (!mp->is_nat44)
1584     {
1585       rv = VNET_API_ERROR_UNIMPLEMENTED;
1586       goto send_reply;
1587     }
1588
1589   clib_memcpy (&in_addr, mp->in_addr, 4);
1590   clib_memcpy (&out_addr, mp->out_addr, 4);
1591   rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
1592                          mp->out_plen, mp->is_add);
1593
1594 send_reply:
1595   REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
1596 }
1597
1598 static void *
1599 vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
1600                                     void *handle)
1601 {
1602   u8 *s;
1603
1604   s = format (0, "SCRIPT: nat_det_add_del_map ");
1605   s = format (s, "inside address %U/%d outside address %U/%d\n",
1606               format_ip4_address, mp->in_addr, mp->in_plen,
1607               format_ip4_address, mp->out_addr, mp->out_plen);
1608
1609   FINISH;
1610 }
1611
1612 static void
1613 vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
1614 {
1615   snat_main_t *sm = &snat_main;
1616   vl_api_nat_det_forward_reply_t *rmp;
1617   int rv = 0;
1618   u16 lo_port = 0, hi_port = 0;
1619   snat_det_map_t *dm;
1620   ip4_address_t in_addr, out_addr;
1621
1622   if (!sm->deterministic)
1623     {
1624       rv = VNET_API_ERROR_UNSUPPORTED;
1625       REPLY_MACRO (VL_API_NAT_DET_FORWARD_REPLY);
1626       return;
1627     }
1628
1629   if (!mp->is_nat44)
1630     {
1631       out_addr.as_u32 = 0;
1632       rv = VNET_API_ERROR_UNIMPLEMENTED;
1633       goto send_reply;
1634     }
1635
1636   out_addr.as_u32 = 0;
1637   clib_memcpy (&in_addr, mp->in_addr, 4);
1638   dm = snat_det_map_by_user (sm, &in_addr);
1639   if (!dm)
1640     {
1641       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1642       goto send_reply;
1643     }
1644
1645   snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
1646   hi_port = lo_port + dm->ports_per_host - 1;
1647
1648 send_reply:
1649   /* *INDENT-OFF* */
1650   REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
1651   ({
1652     rmp->out_port_lo = ntohs (lo_port);
1653     rmp->out_port_hi = ntohs (hi_port);
1654     clib_memcpy (rmp->out_addr, &out_addr, 4);
1655   }))
1656   /* *INDENT-ON* */
1657 }
1658
1659 static void *
1660 vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
1661 {
1662   u8 *s;
1663
1664   s = format (0, "SCRIPT: nat_det_forward");
1665   s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
1666
1667   FINISH;
1668 }
1669
1670 static void
1671 vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
1672 {
1673   snat_main_t *sm = &snat_main;
1674   vl_api_nat_det_reverse_reply_t *rmp;
1675   int rv = 0;
1676   ip4_address_t out_addr, in_addr;
1677   snat_det_map_t *dm;
1678
1679   if (!sm->deterministic)
1680     {
1681       rv = VNET_API_ERROR_UNSUPPORTED;
1682       REPLY_MACRO (VL_API_NAT_DET_REVERSE_REPLY);
1683       return;
1684     }
1685
1686   in_addr.as_u32 = 0;
1687   clib_memcpy (&out_addr, mp->out_addr, 4);
1688   dm = snat_det_map_by_out (sm, &out_addr);
1689   if (!dm)
1690     {
1691       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1692       goto send_reply;
1693     }
1694
1695   snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
1696
1697 send_reply:
1698   /* *INDENT-OFF* */
1699   REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
1700   ({
1701     rmp->is_nat44 = 1;
1702     memset (rmp->in_addr, 0, 16);
1703     clib_memcpy (rmp->in_addr, &in_addr, 4);
1704   }))
1705   /* *INDENT-ON* */
1706 }
1707
1708 static void *
1709 vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
1710 {
1711   u8 *s;
1712
1713   s = format (0, "SCRIPT: nat_det_reverse");
1714   s = format (s, "outside ip address %U outside port %d",
1715               format_ip4_address, mp->out_addr, ntohs (mp->out_port));
1716
1717   FINISH;
1718 }
1719
1720 static void
1721 sent_nat_det_map_details (snat_det_map_t * m, vl_api_registration_t * reg,
1722                           u32 context)
1723 {
1724   vl_api_nat_det_map_details_t *rmp;
1725   snat_main_t *sm = &snat_main;
1726
1727   rmp = vl_msg_api_alloc (sizeof (*rmp));
1728   memset (rmp, 0, sizeof (*rmp));
1729   rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
1730   rmp->is_nat44 = 1;
1731   clib_memcpy (rmp->in_addr, &m->in_addr, 4);
1732   rmp->in_plen = m->in_plen;
1733   clib_memcpy (rmp->out_addr, &m->out_addr, 4);
1734   rmp->out_plen = m->out_plen;
1735   rmp->sharing_ratio = htonl (m->sharing_ratio);
1736   rmp->ports_per_host = htons (m->ports_per_host);
1737   rmp->ses_num = htonl (m->ses_num);
1738   rmp->context = context;
1739
1740   vl_api_send_msg (reg, (u8 *) rmp);
1741 }
1742
1743 static void
1744 vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
1745 {
1746   vl_api_registration_t *reg;
1747   snat_main_t *sm = &snat_main;
1748   snat_det_map_t *m;
1749
1750   if (!sm->deterministic)
1751     return;
1752
1753   reg = vl_api_client_index_to_registration (mp->client_index);
1754   if (!reg)
1755     return;
1756
1757   /* *INDENT-OFF* */
1758   vec_foreach(m, sm->det_maps)
1759     sent_nat_det_map_details(m, reg, mp->context);
1760   /* *INDENT-ON* */
1761 }
1762
1763 static void *
1764 vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
1765 {
1766   u8 *s;
1767
1768   s = format (0, "SCRIPT: nat_det_map_dump ");
1769
1770   FINISH;
1771 }
1772
1773 static void
1774 vl_api_nat_det_set_timeouts_t_handler (vl_api_nat_det_set_timeouts_t * mp)
1775 {
1776   snat_main_t *sm = &snat_main;
1777   vl_api_nat_det_set_timeouts_reply_t *rmp;
1778   int rv = 0;
1779
1780   if (!sm->deterministic)
1781     {
1782       rv = VNET_API_ERROR_UNSUPPORTED;
1783       goto send_reply;
1784     }
1785
1786   sm->udp_timeout = ntohl (mp->udp);
1787   sm->tcp_established_timeout = ntohl (mp->tcp_established);
1788   sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
1789   sm->icmp_timeout = ntohl (mp->icmp);
1790
1791 send_reply:
1792   REPLY_MACRO (VL_API_NAT_DET_SET_TIMEOUTS_REPLY);
1793 }
1794
1795 static void *
1796 vl_api_nat_det_set_timeouts_t_print (vl_api_nat_det_set_timeouts_t * mp,
1797                                      void *handle)
1798 {
1799   u8 *s;
1800
1801   s = format (0, "SCRIPT: nat_det_set_timeouts ");
1802   s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
1803               ntohl (mp->udp),
1804               ntohl (mp->tcp_established),
1805               ntohl (mp->tcp_transitory), ntohl (mp->icmp));
1806
1807   FINISH;
1808 }
1809
1810 static void
1811 vl_api_nat_det_get_timeouts_t_handler (vl_api_nat_det_get_timeouts_t * mp)
1812 {
1813   snat_main_t *sm = &snat_main;
1814   vl_api_nat_det_get_timeouts_reply_t *rmp;
1815   int rv = 0;
1816
1817   if (!sm->deterministic)
1818     {
1819       rv = VNET_API_ERROR_UNSUPPORTED;
1820       REPLY_MACRO (VL_API_NAT_DET_GET_TIMEOUTS_REPLY);
1821       return;
1822     }
1823
1824   /* *INDENT-OFF* */
1825   REPLY_MACRO2 (VL_API_NAT_DET_GET_TIMEOUTS_REPLY,
1826   ({
1827     rmp->udp = htonl (sm->udp_timeout);
1828     rmp->tcp_established = htonl (sm->tcp_established_timeout);
1829     rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
1830     rmp->icmp = htonl (sm->icmp_timeout);
1831   }))
1832   /* *INDENT-ON* */
1833 }
1834
1835 static void *
1836 vl_api_nat_det_get_timeouts_t_print (vl_api_nat_det_get_timeouts_t * mp,
1837                                      void *handle)
1838 {
1839   u8 *s;
1840
1841   s = format (0, "SCRIPT: nat_det_get_timeouts");
1842
1843   FINISH;
1844 }
1845
1846 static void
1847 vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
1848                                             * mp)
1849 {
1850   snat_main_t *sm = &snat_main;
1851   vl_api_nat_det_close_session_out_reply_t *rmp;
1852   ip4_address_t out_addr, ext_addr, in_addr;
1853   snat_det_out_key_t key;
1854   snat_det_map_t *dm;
1855   snat_det_session_t *ses;
1856   int rv = 0;
1857
1858   if (!sm->deterministic)
1859     {
1860       rv = VNET_API_ERROR_UNSUPPORTED;
1861       goto send_reply;
1862     }
1863
1864   clib_memcpy (&out_addr, mp->out_addr, 4);
1865   clib_memcpy (&ext_addr, mp->ext_addr, 4);
1866
1867   dm = snat_det_map_by_out (sm, &out_addr);
1868   if (!dm)
1869     {
1870       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1871       goto send_reply;
1872     }
1873   snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
1874   key.ext_host_addr = ext_addr;
1875   key.ext_host_port = mp->ext_port;
1876   key.out_port = mp->out_port;
1877   ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
1878   if (!ses)
1879     {
1880       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1881       goto send_reply;
1882     }
1883   snat_det_ses_close (dm, ses);
1884
1885 send_reply:
1886   REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
1887 }
1888
1889 static void *
1890 vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
1891                                           mp, void *handle)
1892 {
1893   u8 *s;
1894
1895   s = format (0, "SCRIPT: nat_det_close_session_out ");
1896   s = format (s, "out_addr %U out_port %d "
1897               "ext_addr %U ext_port %d\n",
1898               format_ip4_address, mp->out_addr, ntohs (mp->out_port),
1899               format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
1900
1901   FINISH;
1902 }
1903
1904 static void
1905 vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
1906                                            mp)
1907 {
1908   snat_main_t *sm = &snat_main;
1909   vl_api_nat_det_close_session_in_reply_t *rmp;
1910   ip4_address_t in_addr, ext_addr;
1911   snat_det_out_key_t key;
1912   snat_det_map_t *dm;
1913   snat_det_session_t *ses;
1914   int rv = 0;
1915
1916   if (!sm->deterministic)
1917     {
1918       rv = VNET_API_ERROR_UNSUPPORTED;
1919       goto send_reply;
1920     }
1921
1922   if (!mp->is_nat44)
1923     {
1924       rv = VNET_API_ERROR_UNIMPLEMENTED;
1925       goto send_reply;
1926     }
1927
1928   clib_memcpy (&in_addr, mp->in_addr, 4);
1929   clib_memcpy (&ext_addr, mp->ext_addr, 4);
1930
1931   dm = snat_det_map_by_user (sm, &in_addr);
1932   if (!dm)
1933     {
1934       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1935       goto send_reply;
1936     }
1937   key.ext_host_addr = ext_addr;
1938   key.ext_host_port = mp->ext_port;
1939   ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
1940   if (!ses)
1941     {
1942       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1943       goto send_reply;
1944     }
1945   snat_det_ses_close (dm, ses);
1946
1947 send_reply:
1948   REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
1949 }
1950
1951 static void *
1952 vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
1953                                          mp, void *handle)
1954 {
1955   u8 *s;
1956   s = format (0, "SCRIPT: nat_det_close_session_in ");
1957   s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
1958               format_ip4_address, mp->in_addr, ntohs (mp->in_port),
1959               format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
1960
1961   FINISH;
1962 }
1963
1964 static void
1965 send_nat_det_session_details (snat_det_session_t * s,
1966                               vl_api_registration_t * reg, u32 context)
1967 {
1968   vl_api_nat_det_session_details_t *rmp;
1969   snat_main_t *sm = &snat_main;
1970
1971   rmp = vl_msg_api_alloc (sizeof (*rmp));
1972   memset (rmp, 0, sizeof (*rmp));
1973   rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
1974   rmp->in_port = s->in_port;
1975   clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
1976   rmp->ext_port = s->out.ext_host_port;
1977   rmp->out_port = s->out.out_port;
1978   rmp->state = s->state;
1979   rmp->expire = ntohl (s->expire);
1980   rmp->context = context;
1981
1982   vl_api_send_msg (reg, (u8 *) rmp);
1983 }
1984
1985 static void
1986 vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
1987 {
1988   vl_api_registration_t *reg;
1989   snat_main_t *sm = &snat_main;
1990   ip4_address_t user_addr;
1991   snat_det_map_t *dm;
1992   snat_det_session_t *s, empty_ses;
1993   u16 i;
1994
1995   if (!sm->deterministic)
1996     return;
1997
1998   reg = vl_api_client_index_to_registration (mp->client_index);
1999   if (!reg)
2000     return;
2001   if (!mp->is_nat44)
2002     return;
2003
2004   memset (&empty_ses, 0, sizeof (empty_ses));
2005   clib_memcpy (&user_addr, mp->user_addr, 4);
2006   dm = snat_det_map_by_user (sm, &user_addr);
2007   if (!dm)
2008     return;
2009
2010   s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
2011   for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
2012     {
2013       if (s->out.as_u64)
2014         send_nat_det_session_details (s, reg, mp->context);
2015       s++;
2016     }
2017 }
2018
2019 static void *
2020 vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
2021                                      void *handle)
2022 {
2023   u8 *s;
2024
2025   s = format (0, "SCRIPT: nat_det_session_dump ");
2026   s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
2027
2028   FINISH;
2029 }
2030
2031 /*************/
2032 /*** NAT64 ***/
2033 /*************/
2034
2035 static void
2036   vl_api_nat64_add_del_pool_addr_range_t_handler
2037   (vl_api_nat64_add_del_pool_addr_range_t * mp)
2038 {
2039   vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
2040   snat_main_t *sm = &snat_main;
2041   int rv = 0;
2042   ip4_address_t this_addr;
2043   u32 start_host_order, end_host_order;
2044   u32 vrf_id;
2045   int i, count;
2046   u32 *tmp;
2047
2048   tmp = (u32 *) mp->start_addr;
2049   start_host_order = clib_host_to_net_u32 (tmp[0]);
2050   tmp = (u32 *) mp->end_addr;
2051   end_host_order = clib_host_to_net_u32 (tmp[0]);
2052
2053   count = (end_host_order - start_host_order) + 1;
2054
2055   vrf_id = clib_host_to_net_u32 (mp->vrf_id);
2056
2057   memcpy (&this_addr.as_u8, mp->start_addr, 4);
2058
2059   for (i = 0; i < count; i++)
2060     {
2061       if ((rv = nat64_add_del_pool_addr (&this_addr, vrf_id, mp->is_add)))
2062         goto send_reply;
2063
2064       increment_v4_address (&this_addr);
2065     }
2066
2067 send_reply:
2068   REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2069 }
2070
2071 static void *vl_api_nat64_add_del_pool_addr_range_t_print
2072   (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
2073 {
2074   u8 *s;
2075
2076   s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
2077   s = format (s, "%U - %U vrf_id %u %s\n",
2078               format_ip4_address, mp->start_addr,
2079               format_ip4_address, mp->end_addr,
2080               ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2081
2082   FINISH;
2083 }
2084
2085 typedef struct nat64_api_walk_ctx_t_
2086 {
2087   vl_api_registration_t *reg;
2088   u32 context;
2089   nat64_db_t *db;
2090 } nat64_api_walk_ctx_t;
2091
2092 static int
2093 nat64_api_pool_walk (snat_address_t * a, void *arg)
2094 {
2095   vl_api_nat64_pool_addr_details_t *rmp;
2096   snat_main_t *sm = &snat_main;
2097   nat64_api_walk_ctx_t *ctx = arg;
2098
2099   rmp = vl_msg_api_alloc (sizeof (*rmp));
2100   memset (rmp, 0, sizeof (*rmp));
2101   rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
2102   clib_memcpy (rmp->address, &(a->addr), 4);
2103   if (a->fib_index != ~0)
2104     {
2105       fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
2106       if (!fib)
2107         return -1;
2108       rmp->vrf_id = ntohl (fib->ft_table_id);
2109     }
2110   else
2111     rmp->vrf_id = ~0;
2112   rmp->context = ctx->context;
2113
2114   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2115
2116   return 0;
2117 }
2118
2119 static void
2120 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
2121 {
2122   vl_api_registration_t *reg;
2123
2124   reg = vl_api_client_index_to_registration (mp->client_index);
2125   if (!reg)
2126     return;
2127
2128   nat64_api_walk_ctx_t ctx = {
2129     .reg = reg,
2130     .context = mp->context,
2131   };
2132
2133   nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
2134 }
2135
2136 static void *
2137 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
2138                                      void *handle)
2139 {
2140   u8 *s;
2141
2142   s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
2143
2144   FINISH;
2145 }
2146
2147 static void
2148 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
2149                                           mp)
2150 {
2151   snat_main_t *sm = &snat_main;
2152   vl_api_nat64_add_del_interface_reply_t *rmp;
2153   int rv = 0;
2154
2155   VALIDATE_SW_IF_INDEX (mp);
2156
2157   rv =
2158     nat64_add_del_interface (ntohl (mp->sw_if_index), mp->is_inside,
2159                              mp->is_add);
2160
2161   BAD_SW_IF_INDEX_LABEL;
2162
2163   REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
2164 }
2165
2166 static void *
2167 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
2168                                         void *handle)
2169 {
2170   u8 *s;
2171
2172   s = format (0, "SCRIPT: nat64_add_del_interface ");
2173   s = format (s, "sw_if_index %d %s %s",
2174               clib_host_to_net_u32 (mp->sw_if_index),
2175               mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
2176
2177   FINISH;
2178 }
2179
2180 static int
2181 nat64_api_interface_walk (snat_interface_t * i, void *arg)
2182 {
2183   vl_api_nat64_interface_details_t *rmp;
2184   snat_main_t *sm = &snat_main;
2185   nat64_api_walk_ctx_t *ctx = arg;
2186
2187   rmp = vl_msg_api_alloc (sizeof (*rmp));
2188   memset (rmp, 0, sizeof (*rmp));
2189   rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2190   rmp->sw_if_index = ntohl (i->sw_if_index);
2191   rmp->is_inside = (nat_interface_is_inside (i)
2192                     && nat_interface_is_outside (i)) ? 2 :
2193     nat_interface_is_inside (i);
2194   rmp->context = ctx->context;
2195
2196   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2197
2198   return 0;
2199 }
2200
2201 static void
2202 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2203 {
2204   vl_api_registration_t *reg;
2205
2206   reg = vl_api_client_index_to_registration (mp->client_index);
2207   if (!reg)
2208     return;
2209
2210   nat64_api_walk_ctx_t ctx = {
2211     .reg = reg,
2212     .context = mp->context,
2213   };
2214
2215   nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2216 }
2217
2218 static void *
2219 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2220                                      void *handle)
2221 {
2222   u8 *s;
2223
2224   s = format (0, "SCRIPT: snat_interface_dump ");
2225
2226   FINISH;
2227 }
2228
2229 static void
2230   vl_api_nat64_add_del_static_bib_t_handler
2231   (vl_api_nat64_add_del_static_bib_t * mp)
2232 {
2233   snat_main_t *sm = &snat_main;
2234   vl_api_nat64_add_del_static_bib_reply_t *rmp;
2235   ip6_address_t in_addr;
2236   ip4_address_t out_addr;
2237   int rv = 0;
2238
2239   memcpy (&in_addr.as_u8, mp->i_addr, 16);
2240   memcpy (&out_addr.as_u8, mp->o_addr, 4);
2241
2242   rv =
2243     nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2244                                     clib_net_to_host_u16 (mp->i_port),
2245                                     clib_net_to_host_u16 (mp->o_port),
2246                                     mp->proto,
2247                                     clib_net_to_host_u32 (mp->vrf_id),
2248                                     mp->is_add);
2249
2250   REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2251 }
2252
2253 static void *vl_api_nat64_add_del_static_bib_t_print
2254   (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2255 {
2256   u8 *s;
2257
2258   s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2259   s = format (s, "protocol %d i_addr %U o_addr %U ",
2260               mp->proto,
2261               format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2262
2263   if (mp->vrf_id != ~0)
2264     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2265
2266   FINISH;
2267 }
2268
2269 static int
2270 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2271 {
2272   vl_api_nat64_bib_details_t *rmp;
2273   snat_main_t *sm = &snat_main;
2274   nat64_api_walk_ctx_t *ctx = arg;
2275   fib_table_t *fib;
2276
2277   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2278   if (!fib)
2279     return -1;
2280
2281   rmp = vl_msg_api_alloc (sizeof (*rmp));
2282   memset (rmp, 0, sizeof (*rmp));
2283   rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2284   rmp->context = ctx->context;
2285   clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2286   clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2287   rmp->i_port = bibe->in_port;
2288   rmp->o_port = bibe->out_port;
2289   rmp->vrf_id = ntohl (fib->ft_table_id);
2290   rmp->proto = bibe->proto;
2291   rmp->is_static = bibe->is_static;
2292   rmp->ses_num = ntohl (bibe->ses_num);
2293
2294   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2295
2296   return 0;
2297 }
2298
2299 static void
2300 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2301 {
2302   vl_api_registration_t *reg;
2303   nat64_main_t *nm = &nat64_main;
2304   nat64_db_t *db;
2305
2306   reg = vl_api_client_index_to_registration (mp->client_index);
2307   if (!reg)
2308     return;
2309
2310   nat64_api_walk_ctx_t ctx = {
2311     .reg = reg,
2312     .context = mp->context,
2313   };
2314
2315   /* *INDENT-OFF* */
2316   vec_foreach (db, nm->db)
2317     nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2318   /* *INDENT-ON* */
2319 }
2320
2321 static void *
2322 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2323 {
2324   u8 *s;
2325
2326   s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2327
2328   FINISH;
2329 }
2330
2331 static void
2332 vl_api_nat64_set_timeouts_t_handler (vl_api_nat64_set_timeouts_t * mp)
2333 {
2334   snat_main_t *sm = &snat_main;
2335   vl_api_nat64_set_timeouts_reply_t *rmp;
2336   int rv = 0;
2337
2338   rv = nat64_set_icmp_timeout (ntohl (mp->icmp));
2339   if (rv)
2340     goto send_reply;
2341   rv = nat64_set_udp_timeout (ntohl (mp->udp));
2342   if (rv)
2343     goto send_reply;
2344   rv =
2345     nat64_set_tcp_timeouts (ntohl (mp->tcp_trans), ntohl (mp->tcp_est),
2346                             ntohl (mp->tcp_incoming_syn));
2347
2348 send_reply:
2349   REPLY_MACRO (VL_API_NAT64_SET_TIMEOUTS_REPLY);
2350 }
2351
2352 static void *vl_api_nat64_set_timeouts_t_print
2353   (vl_api_nat64_set_timeouts_t * mp, void *handle)
2354 {
2355   u8 *s;
2356
2357   s = format (0, "SCRIPT: nat64_set_timeouts ");
2358   s =
2359     format (s,
2360             "udp %d icmp %d, tcp_trans %d, tcp_est %d, tcp_incoming_syn %d\n",
2361             ntohl (mp->udp), ntohl (mp->icmp), ntohl (mp->tcp_trans),
2362             ntohl (mp->tcp_est), ntohl (mp->tcp_incoming_syn));
2363
2364   FINISH;
2365 }
2366
2367 static void
2368 vl_api_nat64_get_timeouts_t_handler (vl_api_nat64_get_timeouts_t * mp)
2369 {
2370   snat_main_t *sm = &snat_main;
2371   vl_api_nat64_get_timeouts_reply_t *rmp;
2372   int rv = 0;
2373
2374   /* *INDENT-OFF* */
2375   REPLY_MACRO2 (VL_API_NAT64_GET_TIMEOUTS_REPLY,
2376   ({
2377     rmp->udp = htonl (nat64_get_udp_timeout());
2378     rmp->icmp = htonl (nat64_get_icmp_timeout());
2379     rmp->tcp_trans = htonl (nat64_get_tcp_trans_timeout());
2380     rmp->tcp_est = htonl (nat64_get_tcp_est_timeout());
2381     rmp->tcp_incoming_syn = htonl (nat64_get_tcp_incoming_syn_timeout());
2382   }))
2383   /* *INDENT-ON* */
2384 }
2385
2386 static void *vl_api_nat64_get_timeouts_t_print
2387   (vl_api_nat64_get_timeouts_t * mp, void *handle)
2388 {
2389   u8 *s;
2390
2391   s = format (0, "SCRIPT: nat64_get_timeouts");
2392
2393   FINISH;
2394 }
2395
2396 static int
2397 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2398 {
2399   vl_api_nat64_st_details_t *rmp;
2400   snat_main_t *sm = &snat_main;
2401   nat64_api_walk_ctx_t *ctx = arg;
2402   nat64_db_bib_entry_t *bibe;
2403   fib_table_t *fib;
2404
2405   bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2406   if (!bibe)
2407     return -1;
2408
2409   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2410   if (!fib)
2411     return -1;
2412
2413   rmp = vl_msg_api_alloc (sizeof (*rmp));
2414   memset (rmp, 0, sizeof (*rmp));
2415   rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2416   rmp->context = ctx->context;
2417   clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2418   clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2419   rmp->il_port = bibe->in_port;
2420   rmp->ol_port = bibe->out_port;
2421   clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2422   clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2423   rmp->il_port = ste->r_port;
2424   rmp->vrf_id = ntohl (fib->ft_table_id);
2425   rmp->proto = ste->proto;
2426
2427   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2428
2429   return 0;
2430 }
2431
2432 static void
2433 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2434 {
2435   vl_api_registration_t *reg;
2436   nat64_main_t *nm = &nat64_main;
2437   nat64_db_t *db;
2438
2439   reg = vl_api_client_index_to_registration (mp->client_index);
2440   if (!reg)
2441     return;
2442
2443   nat64_api_walk_ctx_t ctx = {
2444     .reg = reg,
2445     .context = mp->context,
2446   };
2447
2448   /* *INDENT-OFF* */
2449   vec_foreach (db, nm->db)
2450     {
2451       ctx.db = db;
2452       nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
2453     }
2454   /* *INDENT-ON* */
2455 }
2456
2457 static void *
2458 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
2459 {
2460   u8 *s;
2461
2462   s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
2463
2464   FINISH;
2465 }
2466
2467 static void
2468 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
2469 {
2470   vl_api_nat64_add_del_prefix_reply_t *rmp;
2471   snat_main_t *sm = &snat_main;
2472   ip6_address_t prefix;
2473   int rv = 0;
2474
2475   memcpy (&prefix.as_u8, mp->prefix, 16);
2476
2477   rv =
2478     nat64_add_del_prefix (&prefix, mp->prefix_len,
2479                           clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
2480   REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
2481 }
2482
2483 static void *
2484 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
2485                                      void *handle)
2486 {
2487   u8 *s;
2488
2489   s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
2490               format_ip6_address, mp->prefix, mp->prefix_len,
2491               ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2492
2493   FINISH;
2494 }
2495
2496 static int
2497 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
2498 {
2499   vl_api_nat64_prefix_details_t *rmp;
2500   snat_main_t *sm = &snat_main;
2501   nat64_api_walk_ctx_t *ctx = arg;
2502
2503   rmp = vl_msg_api_alloc (sizeof (*rmp));
2504   memset (rmp, 0, sizeof (*rmp));
2505   rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
2506   clib_memcpy (rmp->prefix, &(p->prefix), 16);
2507   rmp->prefix_len = p->plen;
2508   rmp->vrf_id = ntohl (p->vrf_id);
2509   rmp->context = ctx->context;
2510
2511   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2512
2513   return 0;
2514 }
2515
2516 static void
2517 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
2518 {
2519   vl_api_registration_t *reg;
2520
2521   reg = vl_api_client_index_to_registration (mp->client_index);
2522   if (!reg)
2523     return;
2524
2525   nat64_api_walk_ctx_t ctx = {
2526     .reg = reg,
2527     .context = mp->context,
2528   };
2529
2530   nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
2531 }
2532
2533 static void *
2534 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
2535                                   void *handle)
2536 {
2537   u8 *s;
2538
2539   s = format (0, "SCRIPT: nat64_prefix_dump\n");
2540
2541   FINISH;
2542 }
2543
2544 static void
2545   vl_api_nat64_add_del_interface_addr_t_handler
2546   (vl_api_nat64_add_del_interface_addr_t * mp)
2547 {
2548   snat_main_t *sm = &snat_main;
2549   vl_api_nat64_add_del_interface_addr_reply_t *rmp;
2550   u32 sw_if_index = ntohl (mp->sw_if_index);
2551   int rv = 0;
2552
2553   VALIDATE_SW_IF_INDEX (mp);
2554
2555   rv = nat64_add_interface_address (sw_if_index, mp->is_add);
2556
2557   BAD_SW_IF_INDEX_LABEL;
2558
2559   REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
2560 }
2561
2562 static void *vl_api_nat64_add_del_interface_addr_t_print
2563   (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
2564 {
2565   u8 *s;
2566
2567   s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
2568   s = format (s, "sw_if_index %d %s",
2569               clib_host_to_net_u32 (mp->sw_if_index),
2570               mp->is_add ? "" : "del");
2571
2572   FINISH;
2573 }
2574
2575 /***************/
2576 /*** DS-Lite ***/
2577 /***************/
2578
2579 static void
2580 vl_api_dslite_set_aftr_addr_t_handler (vl_api_dslite_set_aftr_addr_t * mp)
2581 {
2582   vl_api_dslite_set_aftr_addr_reply_t *rmp;
2583   snat_main_t *sm = &snat_main;
2584   dslite_main_t *dm = &dslite_main;
2585   int rv = 0;
2586   ip6_address_t ip6_addr;
2587   ip4_address_t ip4_addr;
2588
2589   memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
2590   memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
2591
2592   rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
2593   if (rv == 0)
2594     rv = dslite_set_aftr_ip4_addr (dm, &ip4_addr);
2595
2596   REPLY_MACRO (VL_API_DSLITE_SET_AFTR_ADDR_REPLY);
2597 }
2598
2599 static void *
2600 vl_api_dslite_set_aftr_addr_t_print (vl_api_dslite_set_aftr_addr_t * mp,
2601                                      void *handle)
2602 {
2603   u8 *s;
2604
2605   s = format (0, "SCRIPT: dslite_set_aftr_addr ");
2606   s = format (s, "ip6_addr %U ip4_addr %U\n",
2607               format_ip6_address, mp->ip6_addr,
2608               format_ip4_address, mp->ip4_addr);
2609
2610   FINISH;
2611 }
2612
2613 static void
2614 vl_api_dslite_get_aftr_addr_t_handler (vl_api_dslite_get_aftr_addr_t * mp)
2615 {
2616   snat_main_t *sm = &snat_main;
2617   vl_api_dslite_get_aftr_addr_reply_t *rmp;
2618   dslite_main_t *dm = &dslite_main;
2619   int rv = 0;
2620
2621   /* *INDENT-OFF* */
2622   REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
2623   ({
2624     memcpy (rmp->ip4_addr, &dm->aftr_ip4_addr.as_u8, 4);
2625     memcpy (rmp->ip6_addr, &dm->aftr_ip6_addr.as_u8, 16);
2626   }))
2627   /* *INDENT-ON* */
2628 }
2629
2630 static void *
2631 vl_api_dslite_get_aftr_addr_t_print (vl_api_dslite_get_aftr_addr_t * mp,
2632                                      void *handle)
2633 {
2634   u8 *s;
2635
2636   s = format (0, "SCRIPT: dslite_get_aftr_addr");
2637
2638   FINISH;
2639 }
2640
2641 static void
2642 vl_api_dslite_set_b4_addr_t_handler (vl_api_dslite_set_b4_addr_t * mp)
2643 {
2644   vl_api_dslite_set_b4_addr_reply_t *rmp;
2645   snat_main_t *sm = &snat_main;
2646   dslite_main_t *dm = &dslite_main;
2647   int rv = 0;
2648   ip6_address_t ip6_addr;
2649   ip4_address_t ip4_addr;
2650
2651   memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
2652   memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
2653
2654   rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
2655   if (rv == 0)
2656     rv = dslite_set_b4_ip4_addr (dm, &ip4_addr);
2657
2658   REPLY_MACRO (VL_API_DSLITE_SET_B4_ADDR_REPLY);
2659 }
2660
2661 static void *
2662 vl_api_dslite_set_b4_addr_t_print (vl_api_dslite_set_b4_addr_t * mp,
2663                                    void *handle)
2664 {
2665   u8 *s;
2666
2667   s = format (0, "SCRIPT: dslite_set_b4_addr ");
2668   s = format (s, "ip6_addr %U ip4_addr %U\n",
2669               format_ip6_address, mp->ip6_addr,
2670               format_ip6_address, mp->ip4_addr);
2671
2672   FINISH;
2673 }
2674
2675 static void
2676 vl_api_dslite_get_b4_addr_t_handler (vl_api_dslite_get_b4_addr_t * mp)
2677 {
2678   snat_main_t *sm = &snat_main;
2679   vl_api_dslite_get_b4_addr_reply_t *rmp;
2680   dslite_main_t *dm = &dslite_main;
2681   int rv = 0;
2682
2683   /* *INDENT-OFF* */
2684   REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
2685   ({
2686     memcpy (rmp->ip4_addr, &dm->b4_ip4_addr.as_u8, 4);
2687     memcpy (rmp->ip6_addr, &dm->b4_ip6_addr.as_u8, 16);
2688   }))
2689   /* *INDENT-ON* */
2690 }
2691
2692 static void *
2693 vl_api_dslite_get_b4_addr_t_print (vl_api_dslite_get_b4_addr_t * mp,
2694                                    void *handle)
2695 {
2696   u8 *s;
2697
2698   s = format (0, "SCRIPT: dslite_get_b4_addr");
2699
2700   FINISH;
2701 }
2702
2703 static void
2704   vl_api_dslite_add_del_pool_addr_range_t_handler
2705   (vl_api_dslite_add_del_pool_addr_range_t * mp)
2706 {
2707   vl_api_dslite_add_del_pool_addr_range_reply_t *rmp;
2708   snat_main_t *sm = &snat_main;
2709   dslite_main_t *dm = &dslite_main;
2710   int rv = 0;
2711   ip4_address_t this_addr;
2712   u32 start_host_order, end_host_order;
2713   int i, count;
2714   u32 *tmp;
2715
2716   tmp = (u32 *) mp->start_addr;
2717   start_host_order = clib_host_to_net_u32 (tmp[0]);
2718   tmp = (u32 *) mp->end_addr;
2719   end_host_order = clib_host_to_net_u32 (tmp[0]);
2720
2721   count = (end_host_order - start_host_order) + 1;
2722   memcpy (&this_addr.as_u8, mp->start_addr, 4);
2723
2724   for (i = 0; i < count; i++)
2725     {
2726       if ((rv = dslite_add_del_pool_addr (dm, &this_addr, mp->is_add)))
2727         goto send_reply;
2728
2729       increment_v4_address (&this_addr);
2730     }
2731
2732 send_reply:
2733   REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2734 }
2735
2736 static void
2737 send_dslite_address_details (snat_address_t * ap,
2738                              vl_api_registration_t * reg, u32 context)
2739 {
2740   vl_api_dslite_address_details_t *rmp;
2741   snat_main_t *sm = &snat_main;
2742
2743   rmp = vl_msg_api_alloc (sizeof (*rmp));
2744
2745   memset (rmp, 0, sizeof (*rmp));
2746
2747   rmp->_vl_msg_id = ntohs (VL_API_DSLITE_ADDRESS_DETAILS + sm->msg_id_base);
2748   clib_memcpy (rmp->ip_address, &(ap->addr), 4);
2749   rmp->context = context;
2750
2751   vl_api_send_msg (reg, (u8 *) rmp);
2752 }
2753
2754 static void
2755 vl_api_dslite_address_dump_t_handler (vl_api_dslite_address_dump_t * mp)
2756 {
2757   vl_api_registration_t *reg;
2758   dslite_main_t *dm = &dslite_main;
2759   snat_address_t *ap;
2760
2761   reg = vl_api_client_index_to_registration (mp->client_index);
2762   if (!reg)
2763     return;
2764
2765   /* *INDENT-OFF* */
2766   vec_foreach (ap, dm->addr_pool)
2767     {
2768       send_dslite_address_details (ap, reg, mp->context);
2769     }
2770   /* *INDENT-ON* */
2771 }
2772
2773 static void *
2774 vl_api_dslite_address_dump_t_print (vl_api_dslite_address_dump_t * mp,
2775                                     void *handle)
2776 {
2777   u8 *s;
2778
2779   s = format (0, "SCRIPT: dslite_address_dump ");
2780
2781   FINISH;
2782 }
2783
2784 static void *vl_api_dslite_add_del_pool_addr_range_t_print
2785   (vl_api_dslite_add_del_pool_addr_range_t * mp, void *handle)
2786 {
2787   u8 *s;
2788
2789   s = format (0, "SCRIPT: dslite_add_del_pool_addr_range ");
2790   s = format (s, "%U - %U\n",
2791               format_ip4_address, mp->start_addr,
2792               format_ip4_address, mp->end_addr);
2793
2794   FINISH;
2795 }
2796
2797
2798 /*************/
2799 /*** NAT66 ***/
2800 /*************/
2801
2802 static void
2803 vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
2804                                           mp)
2805 {
2806   snat_main_t *sm = &snat_main;
2807   vl_api_nat66_add_del_interface_reply_t *rmp;
2808   int rv = 0;
2809
2810   VALIDATE_SW_IF_INDEX (mp);
2811
2812   rv =
2813     nat66_interface_add_del (ntohl (mp->sw_if_index), mp->is_inside,
2814                              mp->is_add);
2815
2816   BAD_SW_IF_INDEX_LABEL;
2817
2818   REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
2819 }
2820
2821 static void *
2822 vl_api_nat66_add_del_interface_t_print (vl_api_nat66_add_del_interface_t * mp,
2823                                         void *handle)
2824 {
2825   u8 *s;
2826
2827   s = format (0, "SCRIPT: nat66_add_del_interface ");
2828   s = format (s, "sw_if_index %d %s %s",
2829               clib_host_to_net_u32 (mp->sw_if_index),
2830               mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
2831
2832   FINISH;
2833 }
2834
2835 static void
2836   vl_api_nat66_add_del_static_mapping_t_handler
2837   (vl_api_nat66_add_del_static_mapping_t * mp)
2838 {
2839   snat_main_t *sm = &snat_main;
2840   vl_api_nat66_add_del_static_mapping_reply_t *rmp;
2841   ip6_address_t l_addr, e_addr;
2842   int rv = 0;
2843
2844   memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
2845   memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
2846
2847   rv =
2848     nat66_static_mapping_add_del (&l_addr, &e_addr,
2849                                   clib_net_to_host_u32 (mp->vrf_id),
2850                                   mp->is_add);
2851
2852   REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
2853 }
2854
2855 static void *vl_api_nat66_add_del_static_mapping_t_print
2856   (vl_api_nat66_add_del_static_mapping_t * mp, void *handle)
2857 {
2858   u8 *s;
2859
2860   s = format (0, "SCRIPT: nat66_add_del_static_mapping ");
2861   s = format (s, "local_ip_address %U external_ip_address %U vrf_id %d %s",
2862               format_ip6_address, mp->local_ip_address,
2863               format_ip6_address, mp->external_ip_address,
2864               clib_net_to_host_u32 (mp->vrf_id), mp->is_add ? "" : "del");
2865
2866   FINISH;
2867 }
2868
2869 typedef struct nat66_api_walk_ctx_t_
2870 {
2871   svm_queue_t *q;
2872   u32 context;
2873 } nat66_api_walk_ctx_t;
2874
2875 static int
2876 nat66_api_interface_walk (snat_interface_t * i, void *arg)
2877 {
2878   vl_api_nat66_interface_details_t *rmp;
2879   snat_main_t *sm = &snat_main;
2880   nat66_api_walk_ctx_t *ctx = arg;
2881
2882   rmp = vl_msg_api_alloc (sizeof (*rmp));
2883   memset (rmp, 0, sizeof (*rmp));
2884   rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + sm->msg_id_base);
2885   rmp->sw_if_index = ntohl (i->sw_if_index);
2886   rmp->is_inside = nat_interface_is_inside (i);
2887   rmp->context = ctx->context;
2888
2889   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
2890
2891   return 0;
2892 }
2893
2894 static void
2895 vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
2896 {
2897   svm_queue_t *q;
2898
2899   q = vl_api_client_index_to_input_queue (mp->client_index);
2900   if (q == 0)
2901     return;
2902
2903   nat66_api_walk_ctx_t ctx = {
2904     .q = q,
2905     .context = mp->context,
2906   };
2907
2908   nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
2909 }
2910
2911 static void *
2912 vl_api_nat66_interface_dump_t_print (vl_api_nat66_interface_dump_t * mp,
2913                                      void *handle)
2914 {
2915   u8 *s;
2916
2917   s = format (0, "SCRIPT: nat66_interface_dump ");
2918
2919   FINISH;
2920 }
2921
2922 static int
2923 nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
2924 {
2925   vl_api_nat66_static_mapping_details_t *rmp;
2926   nat66_main_t *nm = &nat66_main;
2927   snat_main_t *sm = &snat_main;
2928   nat66_api_walk_ctx_t *ctx = arg;
2929   fib_table_t *fib;
2930   vlib_counter_t vc;
2931
2932   fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
2933   if (!fib)
2934     return -1;
2935
2936   vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
2937
2938   rmp = vl_msg_api_alloc (sizeof (*rmp));
2939   memset (rmp, 0, sizeof (*rmp));
2940   rmp->_vl_msg_id =
2941     ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + sm->msg_id_base);
2942   clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
2943   clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
2944   rmp->vrf_id = ntohl (fib->ft_table_id);
2945   rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
2946   rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
2947   rmp->context = ctx->context;
2948
2949   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
2950
2951   return 0;
2952 }
2953
2954 static void
2955 vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
2956                                             * mp)
2957 {
2958   svm_queue_t *q;
2959
2960   q = vl_api_client_index_to_input_queue (mp->client_index);
2961   if (q == 0)
2962     return;
2963
2964   nat66_api_walk_ctx_t ctx = {
2965     .q = q,
2966     .context = mp->context,
2967   };
2968
2969   nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
2970 }
2971
2972 static void *
2973 vl_api_nat66_static_mapping_dump_t_print (vl_api_nat66_static_mapping_dump_t *
2974                                           mp, void *handle)
2975 {
2976   u8 *s;
2977
2978   s = format (0, "SCRIPT: nat66_static_mapping_dump ");
2979
2980   FINISH;
2981 }
2982
2983
2984 /* List of message types that this plugin understands */
2985 #define foreach_snat_plugin_api_msg                                     \
2986 _(NAT_CONTROL_PING, nat_control_ping)                                   \
2987 _(NAT_SHOW_CONFIG, nat_show_config)                                     \
2988 _(NAT_SET_WORKERS, nat_set_workers)                                     \
2989 _(NAT_WORKER_DUMP, nat_worker_dump)                                     \
2990 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable)                   \
2991 _(NAT_SET_REASS, nat_set_reass)                                         \
2992 _(NAT_GET_REASS, nat_get_reass)                                         \
2993 _(NAT_REASS_DUMP, nat_reass_dump)                                       \
2994 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range)             \
2995 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature)     \
2996 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping)           \
2997 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping)       \
2998 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump)                 \
2999 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump)             \
3000 _(NAT44_ADDRESS_DUMP, nat44_address_dump)                               \
3001 _(NAT44_INTERFACE_DUMP, nat44_interface_dump)                           \
3002 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr)           \
3003 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump)                 \
3004 _(NAT44_USER_DUMP, nat44_user_dump)                                     \
3005 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump)                     \
3006 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE,                               \
3007   nat44_interface_add_del_output_feature)                               \
3008 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP,                                  \
3009   nat44_interface_output_feature_dump)                                  \
3010 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping)     \
3011 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump)           \
3012 _(NAT44_DEL_SESSION, nat44_del_session)                                 \
3013 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable)     \
3014 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled)             \
3015 _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map)                             \
3016 _(NAT_DET_FORWARD, nat_det_forward)                                     \
3017 _(NAT_DET_REVERSE, nat_det_reverse)                                     \
3018 _(NAT_DET_MAP_DUMP, nat_det_map_dump)                                   \
3019 _(NAT_DET_SET_TIMEOUTS, nat_det_set_timeouts)                           \
3020 _(NAT_DET_GET_TIMEOUTS, nat_det_get_timeouts)                           \
3021 _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out)                 \
3022 _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in)                   \
3023 _(NAT_DET_SESSION_DUMP, nat_det_session_dump)                           \
3024 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range)         \
3025 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump)                           \
3026 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface)                     \
3027 _(NAT64_INTERFACE_DUMP, nat64_interface_dump)                           \
3028 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib)                   \
3029 _(NAT64_BIB_DUMP, nat64_bib_dump)                                       \
3030 _(NAT64_SET_TIMEOUTS, nat64_set_timeouts)                               \
3031 _(NAT64_GET_TIMEOUTS, nat64_get_timeouts)                               \
3032 _(NAT64_ST_DUMP, nat64_st_dump)                                         \
3033 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix)                           \
3034 _(NAT64_PREFIX_DUMP, nat64_prefix_dump)                                 \
3035 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr)           \
3036 _(DSLITE_ADD_DEL_POOL_ADDR_RANGE, dslite_add_del_pool_addr_range)       \
3037 _(DSLITE_ADDRESS_DUMP, dslite_address_dump)                             \
3038 _(DSLITE_SET_AFTR_ADDR, dslite_set_aftr_addr)                           \
3039 _(DSLITE_GET_AFTR_ADDR, dslite_get_aftr_addr)                           \
3040 _(DSLITE_SET_B4_ADDR, dslite_set_b4_addr)                               \
3041 _(DSLITE_GET_B4_ADDR, dslite_get_b4_addr)                               \
3042 _(NAT66_ADD_DEL_INTERFACE, nat66_add_del_interface)                     \
3043 _(NAT66_INTERFACE_DUMP, nat66_interface_dump)                           \
3044 _(NAT66_ADD_DEL_STATIC_MAPPING, nat66_add_del_static_mapping)           \
3045 _(NAT66_STATIC_MAPPING_DUMP, nat66_static_mapping_dump)
3046
3047 /* Set up the API message handling tables */
3048 static clib_error_t *
3049 snat_plugin_api_hookup (vlib_main_t * vm)
3050 {
3051   snat_main_t *sm __attribute__ ((unused)) = &snat_main;
3052 #define _(N,n)                                                  \
3053     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
3054                            #n,                                  \
3055                            vl_api_##n##_t_handler,              \
3056                            vl_noop_handler,                     \
3057                            vl_api_##n##_t_endian,               \
3058                            vl_api_##n##_t_print,                \
3059                            sizeof(vl_api_##n##_t), 1);
3060   foreach_snat_plugin_api_msg;
3061 #undef _
3062
3063   return 0;
3064 }
3065
3066 #define vl_msg_name_crc_list
3067 #include <nat/nat_all_api_h.h>
3068 #undef vl_msg_name_crc_list
3069
3070 static void
3071 setup_message_id_table (snat_main_t * sm, api_main_t * am)
3072 {
3073 #define _(id,n,crc) \
3074   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
3075   foreach_vl_msg_name_crc_nat;
3076 #undef _
3077 }
3078
3079 static void
3080 plugin_custom_dump_configure (snat_main_t * sm)
3081 {
3082 #define _(n,f) sm->api_main->msg_print_handlers \
3083   [VL_API_##n + sm->msg_id_base]                \
3084     = (void *) vl_api_##f##_t_print;
3085   foreach_snat_plugin_api_msg;
3086 #undef _
3087 }
3088
3089 clib_error_t *
3090 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
3091 {
3092   u8 *name;
3093   clib_error_t *error = 0;
3094
3095   name = format (0, "nat_%08x%c", api_version, 0);
3096
3097   /* Ask for a correctly-sized block of API message decode slots */
3098   sm->msg_id_base =
3099     vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
3100
3101   error = snat_plugin_api_hookup (vm);
3102
3103   /* Add our API messages to the global name_crc hash table */
3104   setup_message_id_table (sm, sm->api_main);
3105
3106   plugin_custom_dump_configure (sm);
3107
3108   vec_free (name);
3109
3110   return error;
3111 }
3112
3113 /*
3114  * fd.io coding-style-patch-verification: ON
3115  *
3116  * Local Variables:
3117  * eval: (c-set-style "gnu")
3118  * End:
3119  */