NAT: disable CLI/API not supported in active mode (VPP-1234)
[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   int rv = 0;
728   snat_protocol_t proto;
729   u8 *tag = 0;
730
731   if (sm->deterministic)
732     {
733       rv = VNET_API_ERROR_UNSUPPORTED;
734       goto send_reply;
735     }
736
737   memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
738   memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
739   if (mp->addr_only == 0)
740     {
741       local_port = clib_net_to_host_u16 (mp->local_port);
742       external_port = clib_net_to_host_u16 (mp->external_port);
743     }
744   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
745   external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
746   proto = ip_proto_to_snat_proto (mp->protocol);
747   mp->tag[sizeof (mp->tag) - 1] = 0;
748   tag = format (0, "%s", mp->tag);
749   vec_terminate_c_string (tag);
750
751   rv = snat_add_static_mapping (local_addr, external_addr, local_port,
752                                 external_port, vrf_id, mp->addr_only,
753                                 external_sw_if_index, proto, mp->is_add,
754                                 mp->twice_nat, mp->out2in_only, tag);
755
756   vec_free (tag);
757
758 send_reply:
759   REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
760 }
761
762 static void *vl_api_nat44_add_del_static_mapping_t_print
763   (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
764 {
765   u8 *s;
766
767   s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
768   s = format (s, "protocol %d local_addr %U external_addr %U ",
769               mp->protocol,
770               format_ip4_address, mp->local_ip_address,
771               format_ip4_address, mp->external_ip_address);
772
773   if (mp->addr_only == 0)
774     s = format (s, "local_port %d external_port %d ",
775                 clib_net_to_host_u16 (mp->local_port),
776                 clib_net_to_host_u16 (mp->external_port));
777
778   s = format (s, "twice_nat %d out2in_only %d ",
779               mp->twice_nat, mp->out2in_only);
780
781   if (mp->vrf_id != ~0)
782     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
783
784   if (mp->external_sw_if_index != ~0)
785     s = format (s, "external_sw_if_index %d",
786                 clib_net_to_host_u32 (mp->external_sw_if_index));
787   FINISH;
788 }
789
790 static void
791 send_nat44_static_mapping_details (snat_static_mapping_t * m,
792                                    vl_api_registration_t * reg, u32 context)
793 {
794   vl_api_nat44_static_mapping_details_t *rmp;
795   snat_main_t *sm = &snat_main;
796
797   rmp = vl_msg_api_alloc (sizeof (*rmp));
798   memset (rmp, 0, sizeof (*rmp));
799   rmp->_vl_msg_id =
800     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
801   rmp->addr_only = m->addr_only;
802   clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
803   clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
804   rmp->external_sw_if_index = ~0;
805   rmp->vrf_id = htonl (m->vrf_id);
806   rmp->context = context;
807   rmp->twice_nat = m->twice_nat;
808   rmp->out2in_only = m->out2in_only;
809   if (m->addr_only == 0)
810     {
811       rmp->protocol = snat_proto_to_ip_proto (m->proto);
812       rmp->external_port = htons (m->external_port);
813       rmp->local_port = htons (m->local_port);
814     }
815   if (m->tag)
816     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
817
818   vl_api_send_msg (reg, (u8 *) rmp);
819 }
820
821 static void
822 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
823                                        vl_api_registration_t * reg,
824                                        u32 context)
825 {
826   vl_api_nat44_static_mapping_details_t *rmp;
827   snat_main_t *sm = &snat_main;
828
829   rmp = vl_msg_api_alloc (sizeof (*rmp));
830   memset (rmp, 0, sizeof (*rmp));
831   rmp->_vl_msg_id =
832     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
833   rmp->addr_only = m->addr_only;
834   clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
835   rmp->external_sw_if_index = htonl (m->sw_if_index);
836   rmp->vrf_id = htonl (m->vrf_id);
837   rmp->context = context;
838   rmp->twice_nat = m->twice_nat;
839   if (m->addr_only == 0)
840     {
841       rmp->protocol = snat_proto_to_ip_proto (m->proto);
842       rmp->external_port = htons (m->e_port);
843       rmp->local_port = htons (m->l_port);
844     }
845   if (m->tag)
846     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
847
848   vl_api_send_msg (reg, (u8 *) rmp);
849 }
850
851 static void
852 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
853                                             * mp)
854 {
855   vl_api_registration_t *reg;
856   snat_main_t *sm = &snat_main;
857   snat_static_mapping_t *m;
858   snat_static_map_resolve_t *rp;
859   int j;
860
861   if (sm->deterministic)
862     return;
863
864   reg = vl_api_client_index_to_registration (mp->client_index);
865   if (!reg)
866     return;
867
868   /* *INDENT-OFF* */
869   pool_foreach (m, sm->static_mappings,
870   ({
871       if (!vec_len(m->locals) && (m->local_addr.as_u32 != m->external_addr.as_u32))
872         send_nat44_static_mapping_details (m, reg, mp->context);
873   }));
874   /* *INDENT-ON* */
875
876   for (j = 0; j < vec_len (sm->to_resolve); j++)
877     {
878       rp = sm->to_resolve + j;
879       if (rp->l_addr.as_u32 != 0)
880         send_nat44_static_map_resolve_details (rp, reg, mp->context);
881     }
882 }
883
884 static void *
885 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
886                                           mp, void *handle)
887 {
888   u8 *s;
889
890   s = format (0, "SCRIPT: nat44_static_mapping_dump ");
891
892   FINISH;
893 }
894
895 static void
896   vl_api_nat44_add_del_identity_mapping_t_handler
897   (vl_api_nat44_add_del_identity_mapping_t * mp)
898 {
899   snat_main_t *sm = &snat_main;
900   vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
901   ip4_address_t addr;
902   u16 port = 0;
903   u32 vrf_id, sw_if_index;
904   int rv = 0;
905   snat_protocol_t proto = ~0;
906   u8 *tag = 0;
907
908   if (sm->deterministic)
909     {
910       rv = VNET_API_ERROR_UNSUPPORTED;
911       goto send_reply;
912     }
913
914   if (mp->addr_only == 0)
915     {
916       port = clib_net_to_host_u16 (mp->port);
917       proto = ip_proto_to_snat_proto (mp->protocol);
918     }
919   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
920   sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
921   if (sw_if_index != ~0)
922     addr.as_u32 = 0;
923   else
924     memcpy (&addr.as_u8, mp->ip_address, 4);
925   mp->tag[sizeof (mp->tag) - 1] = 0;
926   tag = format (0, "%s", mp->tag);
927   vec_terminate_c_string (tag);
928
929   rv =
930     snat_add_static_mapping (addr, addr, port, port, vrf_id, mp->addr_only,
931                              sw_if_index, proto, mp->is_add, 0, 0, tag);
932
933   vec_free (tag);
934
935 send_reply:
936   REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
937 }
938
939 static void *vl_api_nat44_add_del_identity_mapping_t_print
940   (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
941 {
942   u8 *s;
943
944   s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
945   if (mp->sw_if_index != ~0)
946     s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
947   else
948     s = format (s, "addr %U", format_ip4_address, mp->ip_address);
949
950   if (mp->addr_only == 0)
951     s =
952       format (s, "protocol %d port %d", mp->protocol,
953               clib_net_to_host_u16 (mp->port));
954
955   if (mp->vrf_id != ~0)
956     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
957
958   FINISH;
959 }
960
961 static void
962 send_nat44_identity_mapping_details (snat_static_mapping_t * m,
963                                      vl_api_registration_t * reg, u32 context)
964 {
965   vl_api_nat44_identity_mapping_details_t *rmp;
966   snat_main_t *sm = &snat_main;
967
968   rmp = vl_msg_api_alloc (sizeof (*rmp));
969   memset (rmp, 0, sizeof (*rmp));
970   rmp->_vl_msg_id =
971     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
972   rmp->addr_only = m->addr_only;
973   clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
974   rmp->port = htons (m->local_port);
975   rmp->sw_if_index = ~0;
976   rmp->vrf_id = htonl (m->vrf_id);
977   rmp->protocol = snat_proto_to_ip_proto (m->proto);
978   rmp->context = context;
979   if (m->tag)
980     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
981
982   vl_api_send_msg (reg, (u8 *) rmp);
983 }
984
985 static void
986 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
987                                          vl_api_registration_t * reg,
988                                          u32 context)
989 {
990   vl_api_nat44_identity_mapping_details_t *rmp;
991   snat_main_t *sm = &snat_main;
992
993   rmp = vl_msg_api_alloc (sizeof (*rmp));
994   memset (rmp, 0, sizeof (*rmp));
995   rmp->_vl_msg_id =
996     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
997   rmp->addr_only = m->addr_only;
998   rmp->port = htons (m->l_port);
999   rmp->sw_if_index = htonl (m->sw_if_index);
1000   rmp->vrf_id = htonl (m->vrf_id);
1001   rmp->protocol = snat_proto_to_ip_proto (m->proto);
1002   rmp->context = context;
1003   if (m->tag)
1004     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1005
1006   vl_api_send_msg (reg, (u8 *) rmp);
1007 }
1008
1009 static void
1010   vl_api_nat44_identity_mapping_dump_t_handler
1011   (vl_api_nat44_identity_mapping_dump_t * mp)
1012 {
1013   vl_api_registration_t *reg;
1014   snat_main_t *sm = &snat_main;
1015   snat_static_mapping_t *m;
1016   snat_static_map_resolve_t *rp;
1017   int j;
1018
1019   if (sm->deterministic)
1020     return;
1021
1022   reg = vl_api_client_index_to_registration (mp->client_index);
1023   if (!reg)
1024     return;
1025
1026   /* *INDENT-OFF* */
1027   pool_foreach (m, sm->static_mappings,
1028   ({
1029       if (!vec_len(m->locals) && (m->local_addr.as_u32 == m->external_addr.as_u32))
1030         send_nat44_identity_mapping_details (m, reg, mp->context);
1031   }));
1032   /* *INDENT-ON* */
1033
1034   for (j = 0; j < vec_len (sm->to_resolve); j++)
1035     {
1036       rp = sm->to_resolve + j;
1037       if (rp->l_addr.as_u32 == 0)
1038         send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1039     }
1040 }
1041
1042 static void *vl_api_nat44_identity_mapping_dump_t_print
1043   (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1044 {
1045   u8 *s;
1046
1047   s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1048
1049   FINISH;
1050 }
1051
1052 static void
1053   vl_api_nat44_add_del_interface_addr_t_handler
1054   (vl_api_nat44_add_del_interface_addr_t * mp)
1055 {
1056   snat_main_t *sm = &snat_main;
1057   vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1058   u8 is_del = mp->is_add == 0;
1059   u32 sw_if_index = ntohl (mp->sw_if_index);
1060   int rv = 0;
1061
1062   if (sm->deterministic)
1063     {
1064       rv = VNET_API_ERROR_UNSUPPORTED;
1065       goto send_reply;
1066     }
1067
1068   VALIDATE_SW_IF_INDEX (mp);
1069
1070   rv = snat_add_interface_address (sm, sw_if_index, is_del, mp->twice_nat);
1071
1072   BAD_SW_IF_INDEX_LABEL;
1073 send_reply:
1074   REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1075 }
1076
1077 static void *vl_api_nat44_add_del_interface_addr_t_print
1078   (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1079 {
1080   u8 *s;
1081
1082   s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1083   s = format (s, "sw_if_index %d twice_nat %d %s",
1084               clib_host_to_net_u32 (mp->sw_if_index),
1085               mp->twice_nat, mp->is_add ? "" : "del");
1086
1087   FINISH;
1088 }
1089
1090 static void
1091 send_nat44_interface_addr_details (u32 sw_if_index,
1092                                    vl_api_registration_t * reg, u32 context,
1093                                    u8 twice_nat)
1094 {
1095   vl_api_nat44_interface_addr_details_t *rmp;
1096   snat_main_t *sm = &snat_main;
1097
1098   rmp = vl_msg_api_alloc (sizeof (*rmp));
1099   memset (rmp, 0, sizeof (*rmp));
1100   rmp->_vl_msg_id =
1101     ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1102   rmp->sw_if_index = ntohl (sw_if_index);
1103   rmp->twice_nat = twice_nat;
1104   rmp->context = context;
1105
1106   vl_api_send_msg (reg, (u8 *) rmp);
1107 }
1108
1109 static void
1110 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1111                                             * mp)
1112 {
1113   vl_api_registration_t *reg;
1114   snat_main_t *sm = &snat_main;
1115   u32 *i;
1116
1117   if (sm->deterministic)
1118     return;
1119
1120   reg = vl_api_client_index_to_registration (mp->client_index);
1121   if (!reg)
1122     return;
1123
1124   /* *INDENT-OFF* */
1125   vec_foreach (i, sm->auto_add_sw_if_indices)
1126     send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1127   vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1128     send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1129   /* *INDENT-ON* */
1130 }
1131
1132 static void *
1133 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1134                                           mp, void *handle)
1135 {
1136   u8 *s;
1137
1138   s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1139
1140   FINISH;
1141 }
1142
1143 static void
1144 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1145                          u32 context)
1146 {
1147   vl_api_nat44_user_details_t *rmp;
1148   snat_main_t *sm = &snat_main;
1149   fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1150
1151   rmp = vl_msg_api_alloc (sizeof (*rmp));
1152   memset (rmp, 0, sizeof (*rmp));
1153   rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1154
1155   rmp->vrf_id = ntohl (fib->ft_table_id);
1156
1157   clib_memcpy (rmp->ip_address, &(u->addr), 4);
1158   rmp->nsessions = ntohl (u->nsessions);
1159   rmp->nstaticsessions = ntohl (u->nstaticsessions);
1160   rmp->context = context;
1161
1162   vl_api_send_msg (reg, (u8 *) rmp);
1163 }
1164
1165 static void
1166 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1167 {
1168   vl_api_registration_t *reg;
1169   snat_main_t *sm = &snat_main;
1170   snat_main_per_thread_data_t *tsm;
1171   snat_user_t *u;
1172
1173   if (sm->deterministic)
1174     return;
1175
1176   reg = vl_api_client_index_to_registration (mp->client_index);
1177   if (!reg)
1178     return;
1179
1180   /* *INDENT-OFF* */
1181   vec_foreach (tsm, sm->per_thread_data)
1182     vec_foreach (u, tsm->users)
1183       send_nat44_user_details (u, reg, mp->context);
1184   /* *INDENT-ON* */
1185 }
1186
1187 static void *
1188 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1189 {
1190   u8 *s;
1191
1192   s = format (0, "SCRIPT: nat44_user_dump ");
1193
1194   FINISH;
1195 }
1196
1197 static void
1198 send_nat44_user_session_details (snat_session_t * s,
1199                                  vl_api_registration_t * reg, u32 context)
1200 {
1201   vl_api_nat44_user_session_details_t *rmp;
1202   snat_main_t *sm = &snat_main;
1203
1204   rmp = vl_msg_api_alloc (sizeof (*rmp));
1205   memset (rmp, 0, sizeof (*rmp));
1206   rmp->_vl_msg_id =
1207     ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1208   clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1209   clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1210   rmp->is_static = s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING ? 1 : 0;
1211   rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1212   rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1213   rmp->total_pkts = ntohl (s->total_pkts);
1214   rmp->context = context;
1215   if (snat_is_unk_proto_session (s))
1216     {
1217       rmp->outside_port = 0;
1218       rmp->inside_port = 0;
1219       rmp->protocol = ntohs (s->in2out.port);
1220     }
1221   else
1222     {
1223       rmp->outside_port = s->out2in.port;
1224       rmp->inside_port = s->in2out.port;
1225       rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
1226     }
1227
1228   vl_api_send_msg (reg, (u8 *) rmp);
1229 }
1230
1231 static void
1232 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1233                                           mp)
1234 {
1235   vl_api_registration_t *reg;
1236   snat_main_t *sm = &snat_main;
1237   snat_main_per_thread_data_t *tsm;
1238   snat_session_t *s;
1239   clib_bihash_kv_8_8_t key, value;
1240   snat_user_key_t ukey;
1241   snat_user_t *u;
1242   u32 session_index, head_index, elt_index;
1243   dlist_elt_t *head, *elt;
1244   ip4_header_t ip;
1245
1246   if (sm->deterministic)
1247     return;
1248
1249   reg = vl_api_client_index_to_registration (mp->client_index);
1250   if (!reg)
1251     return;
1252
1253   clib_memcpy (&ukey.addr, mp->ip_address, 4);
1254   ip.src_address.as_u32 = ukey.addr.as_u32;
1255   ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1256   key.key = ukey.as_u64;
1257   if (sm->num_workers > 1)
1258     tsm =
1259       vec_elt_at_index (sm->per_thread_data,
1260                         sm->worker_in2out_cb (&ip, ukey.fib_index));
1261   else
1262     tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1263   if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1264     return;
1265   u = pool_elt_at_index (tsm->users, value.value);
1266   if (!u->nsessions && !u->nstaticsessions)
1267     return;
1268
1269   head_index = u->sessions_per_user_list_head_index;
1270   head = pool_elt_at_index (tsm->list_pool, head_index);
1271   elt_index = head->next;
1272   elt = pool_elt_at_index (tsm->list_pool, elt_index);
1273   session_index = elt->value;
1274   while (session_index != ~0)
1275     {
1276       s = pool_elt_at_index (tsm->sessions, session_index);
1277
1278       send_nat44_user_session_details (s, reg, mp->context);
1279
1280       elt_index = elt->next;
1281       elt = pool_elt_at_index (tsm->list_pool, elt_index);
1282       session_index = elt->value;
1283     }
1284 }
1285
1286 static void *
1287 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1288                                         void *handle)
1289 {
1290   u8 *s;
1291
1292   s = format (0, "SCRIPT: nat44_user_session_dump ");
1293   s = format (s, "ip_address %U vrf_id %d\n",
1294               format_ip4_address, mp->ip_address,
1295               clib_net_to_host_u32 (mp->vrf_id));
1296
1297   FINISH;
1298 }
1299
1300 static nat44_lb_addr_port_t *
1301 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1302                              u8 addr_port_pair_num)
1303 {
1304   u8 i;
1305   nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1306   vl_api_nat44_lb_addr_port_t *ap;
1307
1308   for (i = 0; i < addr_port_pair_num; i++)
1309     {
1310       ap = &addr_port_pairs[i];
1311       memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1312       clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1313       lb_addr_port.port = clib_net_to_host_u16 (ap->port);
1314       lb_addr_port.probability = ap->probability;
1315       vec_add1 (lb_addr_port_pairs, lb_addr_port);
1316     }
1317
1318   return lb_addr_port_pairs;
1319 }
1320
1321 static void
1322   vl_api_nat44_add_del_lb_static_mapping_t_handler
1323   (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1324 {
1325   snat_main_t *sm = &snat_main;
1326   vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1327   int rv = 0;
1328   nat44_lb_addr_port_t *locals = 0;
1329   ip4_address_t e_addr;
1330   snat_protocol_t proto;
1331   u8 *tag = 0;
1332
1333   if (sm->deterministic)
1334     {
1335       rv = VNET_API_ERROR_UNSUPPORTED;
1336       goto send_reply;
1337     }
1338
1339   locals = unformat_nat44_lb_addr_port (mp->locals, mp->local_num);
1340   clib_memcpy (&e_addr, mp->external_addr, 4);
1341   proto = ip_proto_to_snat_proto (mp->protocol);
1342   mp->tag[sizeof (mp->tag) - 1] = 0;
1343   tag = format (0, "%s", mp->tag);
1344   vec_terminate_c_string (tag);
1345
1346   rv =
1347     nat44_add_del_lb_static_mapping (e_addr,
1348                                      clib_net_to_host_u16 (mp->external_port),
1349                                      proto, clib_net_to_host_u32 (mp->vrf_id),
1350                                      locals, mp->is_add, mp->twice_nat,
1351                                      mp->out2in_only, tag);
1352
1353   vec_free (locals);
1354   vec_free (tag);
1355
1356 send_reply:
1357   REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1358 }
1359
1360 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1361   (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1362 {
1363   u8 *s;
1364
1365   s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1366   s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1367               mp->is_add, mp->twice_nat, mp->out2in_only);
1368
1369   FINISH;
1370 }
1371
1372 static void
1373 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1374                                       vl_api_registration_t * reg,
1375                                       u32 context)
1376 {
1377   vl_api_nat44_lb_static_mapping_details_t *rmp;
1378   snat_main_t *sm = &snat_main;
1379   nat44_lb_addr_port_t *ap;
1380   vl_api_nat44_lb_addr_port_t *locals;
1381
1382   rmp =
1383     vl_msg_api_alloc (sizeof (*rmp) +
1384                       (vec_len (m->locals) * sizeof (nat44_lb_addr_port_t)));
1385   memset (rmp, 0, sizeof (*rmp));
1386   rmp->_vl_msg_id =
1387     ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1388
1389   clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1390   rmp->external_port = ntohs (m->external_port);
1391   rmp->protocol = snat_proto_to_ip_proto (m->proto);
1392   rmp->vrf_id = ntohl (m->vrf_id);
1393   rmp->context = context;
1394   rmp->twice_nat = m->twice_nat;
1395   rmp->out2in_only = m->out2in_only;
1396   if (m->tag)
1397     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1398
1399   locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
1400   vec_foreach (ap, m->locals)
1401   {
1402     clib_memcpy (locals->addr, &(ap->addr), 4);
1403     locals->port = htons (ap->port);
1404     locals->probability = ap->probability;
1405     locals++;
1406     rmp->local_num++;
1407   }
1408
1409   vl_api_send_msg (reg, (u8 *) rmp);
1410 }
1411
1412 static void
1413   vl_api_nat44_lb_static_mapping_dump_t_handler
1414   (vl_api_nat44_lb_static_mapping_dump_t * mp)
1415 {
1416   vl_api_registration_t *reg;
1417   snat_main_t *sm = &snat_main;
1418   snat_static_mapping_t *m;
1419
1420   if (sm->deterministic)
1421     return;
1422
1423   reg = vl_api_client_index_to_registration (mp->client_index);
1424   if (!reg)
1425     return;
1426
1427   /* *INDENT-OFF* */
1428   pool_foreach (m, sm->static_mappings,
1429   ({
1430       if (vec_len(m->locals))
1431         send_nat44_lb_static_mapping_details (m, reg, mp->context);
1432   }));
1433   /* *INDENT-ON* */
1434 }
1435
1436 static void *vl_api_nat44_lb_static_mapping_dump_t_print
1437   (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
1438 {
1439   u8 *s;
1440
1441   s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
1442
1443   FINISH;
1444 }
1445
1446 static void
1447 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
1448 {
1449   snat_main_t *sm = &snat_main;
1450   vl_api_nat44_del_session_reply_t *rmp;
1451   ip4_address_t addr;
1452   u16 port;
1453   u32 vrf_id;
1454   int rv = 0;
1455   snat_protocol_t proto;
1456
1457   if (sm->deterministic)
1458     {
1459       rv = VNET_API_ERROR_UNSUPPORTED;
1460       goto send_reply;
1461     }
1462
1463   memcpy (&addr.as_u8, mp->address, 4);
1464   port = clib_net_to_host_u16 (mp->port);
1465   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1466   proto = ip_proto_to_snat_proto (mp->protocol);
1467
1468   rv = nat44_del_session (sm, &addr, port, proto, vrf_id, mp->is_in);
1469
1470 send_reply:
1471   REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
1472 }
1473
1474 static void *
1475 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
1476                                   void *handle)
1477 {
1478   u8 *s;
1479
1480   s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1481   s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
1482               format_ip4_address, mp->address,
1483               clib_net_to_host_u16 (mp->port),
1484               mp->protocol, clib_net_to_host_u32 (mp->vrf_id), mp->is_in);
1485
1486   FINISH;
1487 }
1488
1489 static void
1490   vl_api_nat44_forwarding_enable_disable_t_handler
1491   (vl_api_nat44_forwarding_enable_disable_t * mp)
1492 {
1493   snat_main_t *sm = &snat_main;
1494   vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
1495   int rv = 0;
1496
1497   sm->forwarding_enabled = mp->enable != 0;
1498
1499   REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
1500 }
1501
1502 static void *vl_api_nat44_forwarding_enable_disable_t_print
1503   (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
1504 {
1505   u8 *s;
1506
1507   s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
1508   s = format (s, "enable %d", mp->enable != 0);
1509
1510   FINISH;
1511 }
1512
1513 static void
1514   vl_api_nat44_forwarding_is_enabled_t_handler
1515   (vl_api_nat44_forwarding_is_enabled_t * mp)
1516 {
1517   vl_api_registration_t *reg;
1518   snat_main_t *sm = &snat_main;
1519   vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
1520
1521   reg = vl_api_client_index_to_registration (mp->client_index);
1522   if (!reg)
1523     return;
1524
1525   rmp = vl_msg_api_alloc (sizeof (*rmp));
1526   memset (rmp, 0, sizeof (*rmp));
1527   rmp->_vl_msg_id =
1528     ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
1529   rmp->context = mp->context;
1530
1531   rmp->enabled = sm->forwarding_enabled;
1532
1533   vl_api_send_msg (reg, (u8 *) rmp);
1534 }
1535
1536 static void *vl_api_nat44_forwarding_is_enabled_t_print
1537   (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
1538 {
1539   u8 *s;
1540
1541   s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
1542
1543   FINISH;
1544 }
1545
1546 /*******************************/
1547 /*** Deterministic NAT (CGN) ***/
1548 /*******************************/
1549
1550 static void
1551 vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
1552 {
1553   snat_main_t *sm = &snat_main;
1554   vl_api_nat_det_add_del_map_reply_t *rmp;
1555   int rv = 0;
1556   ip4_address_t in_addr, out_addr;
1557
1558   if (!sm->deterministic)
1559     {
1560       rv = VNET_API_ERROR_UNSUPPORTED;
1561       goto send_reply;
1562     }
1563
1564   if (!mp->is_nat44)
1565     {
1566       rv = VNET_API_ERROR_UNIMPLEMENTED;
1567       goto send_reply;
1568     }
1569
1570   clib_memcpy (&in_addr, mp->in_addr, 4);
1571   clib_memcpy (&out_addr, mp->out_addr, 4);
1572   rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
1573                          mp->out_plen, mp->is_add);
1574
1575 send_reply:
1576   REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
1577 }
1578
1579 static void *
1580 vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
1581                                     void *handle)
1582 {
1583   u8 *s;
1584
1585   s = format (0, "SCRIPT: nat_det_add_del_map ");
1586   s = format (s, "inside address %U/%d outside address %U/%d\n",
1587               format_ip4_address, mp->in_addr, mp->in_plen,
1588               format_ip4_address, mp->out_addr, mp->out_plen);
1589
1590   FINISH;
1591 }
1592
1593 static void
1594 vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
1595 {
1596   snat_main_t *sm = &snat_main;
1597   vl_api_nat_det_forward_reply_t *rmp;
1598   int rv = 0;
1599   u16 lo_port = 0, hi_port = 0;
1600   snat_det_map_t *dm;
1601   ip4_address_t in_addr, out_addr;
1602
1603   if (!sm->deterministic)
1604     {
1605       rv = VNET_API_ERROR_UNSUPPORTED;
1606       REPLY_MACRO (VL_API_NAT_DET_FORWARD_REPLY);
1607       return;
1608     }
1609
1610   if (!mp->is_nat44)
1611     {
1612       out_addr.as_u32 = 0;
1613       rv = VNET_API_ERROR_UNIMPLEMENTED;
1614       goto send_reply;
1615     }
1616
1617   out_addr.as_u32 = 0;
1618   clib_memcpy (&in_addr, mp->in_addr, 4);
1619   dm = snat_det_map_by_user (sm, &in_addr);
1620   if (!dm)
1621     {
1622       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1623       goto send_reply;
1624     }
1625
1626   snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
1627   hi_port = lo_port + dm->ports_per_host - 1;
1628
1629 send_reply:
1630   /* *INDENT-OFF* */
1631   REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
1632   ({
1633     rmp->out_port_lo = ntohs (lo_port);
1634     rmp->out_port_hi = ntohs (hi_port);
1635     clib_memcpy (rmp->out_addr, &out_addr, 4);
1636   }))
1637   /* *INDENT-ON* */
1638 }
1639
1640 static void *
1641 vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
1642 {
1643   u8 *s;
1644
1645   s = format (0, "SCRIPT: nat_det_forward");
1646   s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
1647
1648   FINISH;
1649 }
1650
1651 static void
1652 vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
1653 {
1654   snat_main_t *sm = &snat_main;
1655   vl_api_nat_det_reverse_reply_t *rmp;
1656   int rv = 0;
1657   ip4_address_t out_addr, in_addr;
1658   snat_det_map_t *dm;
1659
1660   if (!sm->deterministic)
1661     {
1662       rv = VNET_API_ERROR_UNSUPPORTED;
1663       REPLY_MACRO (VL_API_NAT_DET_REVERSE_REPLY);
1664       return;
1665     }
1666
1667   in_addr.as_u32 = 0;
1668   clib_memcpy (&out_addr, mp->out_addr, 4);
1669   dm = snat_det_map_by_out (sm, &out_addr);
1670   if (!dm)
1671     {
1672       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1673       goto send_reply;
1674     }
1675
1676   snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
1677
1678 send_reply:
1679   /* *INDENT-OFF* */
1680   REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
1681   ({
1682     rmp->is_nat44 = 1;
1683     memset (rmp->in_addr, 0, 16);
1684     clib_memcpy (rmp->in_addr, &in_addr, 4);
1685   }))
1686   /* *INDENT-ON* */
1687 }
1688
1689 static void *
1690 vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
1691 {
1692   u8 *s;
1693
1694   s = format (0, "SCRIPT: nat_det_reverse");
1695   s = format (s, "outside ip address %U outside port %d",
1696               format_ip4_address, mp->out_addr, ntohs (mp->out_port));
1697
1698   FINISH;
1699 }
1700
1701 static void
1702 sent_nat_det_map_details (snat_det_map_t * m, vl_api_registration_t * reg,
1703                           u32 context)
1704 {
1705   vl_api_nat_det_map_details_t *rmp;
1706   snat_main_t *sm = &snat_main;
1707
1708   rmp = vl_msg_api_alloc (sizeof (*rmp));
1709   memset (rmp, 0, sizeof (*rmp));
1710   rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
1711   rmp->is_nat44 = 1;
1712   clib_memcpy (rmp->in_addr, &m->in_addr, 4);
1713   rmp->in_plen = m->in_plen;
1714   clib_memcpy (rmp->out_addr, &m->out_addr, 4);
1715   rmp->out_plen = m->out_plen;
1716   rmp->sharing_ratio = htonl (m->sharing_ratio);
1717   rmp->ports_per_host = htons (m->ports_per_host);
1718   rmp->ses_num = htonl (m->ses_num);
1719   rmp->context = context;
1720
1721   vl_api_send_msg (reg, (u8 *) rmp);
1722 }
1723
1724 static void
1725 vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
1726 {
1727   vl_api_registration_t *reg;
1728   snat_main_t *sm = &snat_main;
1729   snat_det_map_t *m;
1730
1731   if (!sm->deterministic)
1732     return;
1733
1734   reg = vl_api_client_index_to_registration (mp->client_index);
1735   if (!reg)
1736     return;
1737
1738   /* *INDENT-OFF* */
1739   vec_foreach(m, sm->det_maps)
1740     sent_nat_det_map_details(m, reg, mp->context);
1741   /* *INDENT-ON* */
1742 }
1743
1744 static void *
1745 vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
1746 {
1747   u8 *s;
1748
1749   s = format (0, "SCRIPT: nat_det_map_dump ");
1750
1751   FINISH;
1752 }
1753
1754 static void
1755 vl_api_nat_det_set_timeouts_t_handler (vl_api_nat_det_set_timeouts_t * mp)
1756 {
1757   snat_main_t *sm = &snat_main;
1758   vl_api_nat_det_set_timeouts_reply_t *rmp;
1759   int rv = 0;
1760
1761   if (!sm->deterministic)
1762     {
1763       rv = VNET_API_ERROR_UNSUPPORTED;
1764       goto send_reply;
1765     }
1766
1767   sm->udp_timeout = ntohl (mp->udp);
1768   sm->tcp_established_timeout = ntohl (mp->tcp_established);
1769   sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
1770   sm->icmp_timeout = ntohl (mp->icmp);
1771
1772 send_reply:
1773   REPLY_MACRO (VL_API_NAT_DET_SET_TIMEOUTS_REPLY);
1774 }
1775
1776 static void *
1777 vl_api_nat_det_set_timeouts_t_print (vl_api_nat_det_set_timeouts_t * mp,
1778                                      void *handle)
1779 {
1780   u8 *s;
1781
1782   s = format (0, "SCRIPT: nat_det_set_timeouts ");
1783   s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
1784               ntohl (mp->udp),
1785               ntohl (mp->tcp_established),
1786               ntohl (mp->tcp_transitory), ntohl (mp->icmp));
1787
1788   FINISH;
1789 }
1790
1791 static void
1792 vl_api_nat_det_get_timeouts_t_handler (vl_api_nat_det_get_timeouts_t * mp)
1793 {
1794   snat_main_t *sm = &snat_main;
1795   vl_api_nat_det_get_timeouts_reply_t *rmp;
1796   int rv = 0;
1797
1798   if (!sm->deterministic)
1799     {
1800       rv = VNET_API_ERROR_UNSUPPORTED;
1801       REPLY_MACRO (VL_API_NAT_DET_GET_TIMEOUTS_REPLY);
1802       return;
1803     }
1804
1805   /* *INDENT-OFF* */
1806   REPLY_MACRO2 (VL_API_NAT_DET_GET_TIMEOUTS_REPLY,
1807   ({
1808     rmp->udp = htonl (sm->udp_timeout);
1809     rmp->tcp_established = htonl (sm->tcp_established_timeout);
1810     rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
1811     rmp->icmp = htonl (sm->icmp_timeout);
1812   }))
1813   /* *INDENT-ON* */
1814 }
1815
1816 static void *
1817 vl_api_nat_det_get_timeouts_t_print (vl_api_nat_det_get_timeouts_t * mp,
1818                                      void *handle)
1819 {
1820   u8 *s;
1821
1822   s = format (0, "SCRIPT: nat_det_get_timeouts");
1823
1824   FINISH;
1825 }
1826
1827 static void
1828 vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
1829                                             * mp)
1830 {
1831   snat_main_t *sm = &snat_main;
1832   vl_api_nat_det_close_session_out_reply_t *rmp;
1833   ip4_address_t out_addr, ext_addr, in_addr;
1834   snat_det_out_key_t key;
1835   snat_det_map_t *dm;
1836   snat_det_session_t *ses;
1837   int rv = 0;
1838
1839   if (!sm->deterministic)
1840     {
1841       rv = VNET_API_ERROR_UNSUPPORTED;
1842       goto send_reply;
1843     }
1844
1845   clib_memcpy (&out_addr, mp->out_addr, 4);
1846   clib_memcpy (&ext_addr, mp->ext_addr, 4);
1847
1848   dm = snat_det_map_by_out (sm, &out_addr);
1849   if (!dm)
1850     {
1851       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1852       goto send_reply;
1853     }
1854   snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
1855   key.ext_host_addr = ext_addr;
1856   key.ext_host_port = mp->ext_port;
1857   key.out_port = mp->out_port;
1858   ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
1859   if (!ses)
1860     {
1861       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1862       goto send_reply;
1863     }
1864   snat_det_ses_close (dm, ses);
1865
1866 send_reply:
1867   REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
1868 }
1869
1870 static void *
1871 vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
1872                                           mp, void *handle)
1873 {
1874   u8 *s;
1875
1876   s = format (0, "SCRIPT: nat_det_close_session_out ");
1877   s = format (s, "out_addr %U out_port %d "
1878               "ext_addr %U ext_port %d\n",
1879               format_ip4_address, mp->out_addr, ntohs (mp->out_port),
1880               format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
1881
1882   FINISH;
1883 }
1884
1885 static void
1886 vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
1887                                            mp)
1888 {
1889   snat_main_t *sm = &snat_main;
1890   vl_api_nat_det_close_session_in_reply_t *rmp;
1891   ip4_address_t in_addr, ext_addr;
1892   snat_det_out_key_t key;
1893   snat_det_map_t *dm;
1894   snat_det_session_t *ses;
1895   int rv = 0;
1896
1897   if (!sm->deterministic)
1898     {
1899       rv = VNET_API_ERROR_UNSUPPORTED;
1900       goto send_reply;
1901     }
1902
1903   if (!mp->is_nat44)
1904     {
1905       rv = VNET_API_ERROR_UNIMPLEMENTED;
1906       goto send_reply;
1907     }
1908
1909   clib_memcpy (&in_addr, mp->in_addr, 4);
1910   clib_memcpy (&ext_addr, mp->ext_addr, 4);
1911
1912   dm = snat_det_map_by_user (sm, &in_addr);
1913   if (!dm)
1914     {
1915       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1916       goto send_reply;
1917     }
1918   key.ext_host_addr = ext_addr;
1919   key.ext_host_port = mp->ext_port;
1920   ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
1921   if (!ses)
1922     {
1923       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1924       goto send_reply;
1925     }
1926   snat_det_ses_close (dm, ses);
1927
1928 send_reply:
1929   REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
1930 }
1931
1932 static void *
1933 vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
1934                                          mp, void *handle)
1935 {
1936   u8 *s;
1937   s = format (0, "SCRIPT: nat_det_close_session_in ");
1938   s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
1939               format_ip4_address, mp->in_addr, ntohs (mp->in_port),
1940               format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
1941
1942   FINISH;
1943 }
1944
1945 static void
1946 send_nat_det_session_details (snat_det_session_t * s,
1947                               vl_api_registration_t * reg, u32 context)
1948 {
1949   vl_api_nat_det_session_details_t *rmp;
1950   snat_main_t *sm = &snat_main;
1951
1952   rmp = vl_msg_api_alloc (sizeof (*rmp));
1953   memset (rmp, 0, sizeof (*rmp));
1954   rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
1955   rmp->in_port = s->in_port;
1956   clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
1957   rmp->ext_port = s->out.ext_host_port;
1958   rmp->out_port = s->out.out_port;
1959   rmp->state = s->state;
1960   rmp->expire = ntohl (s->expire);
1961   rmp->context = context;
1962
1963   vl_api_send_msg (reg, (u8 *) rmp);
1964 }
1965
1966 static void
1967 vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
1968 {
1969   vl_api_registration_t *reg;
1970   snat_main_t *sm = &snat_main;
1971   ip4_address_t user_addr;
1972   snat_det_map_t *dm;
1973   snat_det_session_t *s, empty_ses;
1974   u16 i;
1975
1976   if (!sm->deterministic)
1977     return;
1978
1979   reg = vl_api_client_index_to_registration (mp->client_index);
1980   if (!reg)
1981     return;
1982   if (!mp->is_nat44)
1983     return;
1984
1985   memset (&empty_ses, 0, sizeof (empty_ses));
1986   clib_memcpy (&user_addr, mp->user_addr, 4);
1987   dm = snat_det_map_by_user (sm, &user_addr);
1988   if (!dm)
1989     return;
1990
1991   s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
1992   for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
1993     {
1994       if (s->out.as_u64)
1995         send_nat_det_session_details (s, reg, mp->context);
1996       s++;
1997     }
1998 }
1999
2000 static void *
2001 vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
2002                                      void *handle)
2003 {
2004   u8 *s;
2005
2006   s = format (0, "SCRIPT: nat_det_session_dump ");
2007   s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
2008
2009   FINISH;
2010 }
2011
2012 /*************/
2013 /*** NAT64 ***/
2014 /*************/
2015
2016 static void
2017   vl_api_nat64_add_del_pool_addr_range_t_handler
2018   (vl_api_nat64_add_del_pool_addr_range_t * mp)
2019 {
2020   vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
2021   snat_main_t *sm = &snat_main;
2022   int rv = 0;
2023   ip4_address_t this_addr;
2024   u32 start_host_order, end_host_order;
2025   u32 vrf_id;
2026   int i, count;
2027   u32 *tmp;
2028
2029   tmp = (u32 *) mp->start_addr;
2030   start_host_order = clib_host_to_net_u32 (tmp[0]);
2031   tmp = (u32 *) mp->end_addr;
2032   end_host_order = clib_host_to_net_u32 (tmp[0]);
2033
2034   count = (end_host_order - start_host_order) + 1;
2035
2036   vrf_id = clib_host_to_net_u32 (mp->vrf_id);
2037
2038   memcpy (&this_addr.as_u8, mp->start_addr, 4);
2039
2040   for (i = 0; i < count; i++)
2041     {
2042       if ((rv = nat64_add_del_pool_addr (&this_addr, vrf_id, mp->is_add)))
2043         goto send_reply;
2044
2045       increment_v4_address (&this_addr);
2046     }
2047
2048 send_reply:
2049   REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2050 }
2051
2052 static void *vl_api_nat64_add_del_pool_addr_range_t_print
2053   (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
2054 {
2055   u8 *s;
2056
2057   s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
2058   s = format (s, "%U - %U vrf_id %u %s\n",
2059               format_ip4_address, mp->start_addr,
2060               format_ip4_address, mp->end_addr,
2061               ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2062
2063   FINISH;
2064 }
2065
2066 typedef struct nat64_api_walk_ctx_t_
2067 {
2068   vl_api_registration_t *reg;
2069   u32 context;
2070   nat64_db_t *db;
2071 } nat64_api_walk_ctx_t;
2072
2073 static int
2074 nat64_api_pool_walk (snat_address_t * a, void *arg)
2075 {
2076   vl_api_nat64_pool_addr_details_t *rmp;
2077   snat_main_t *sm = &snat_main;
2078   nat64_api_walk_ctx_t *ctx = arg;
2079
2080   rmp = vl_msg_api_alloc (sizeof (*rmp));
2081   memset (rmp, 0, sizeof (*rmp));
2082   rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
2083   clib_memcpy (rmp->address, &(a->addr), 4);
2084   if (a->fib_index != ~0)
2085     {
2086       fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
2087       if (!fib)
2088         return -1;
2089       rmp->vrf_id = ntohl (fib->ft_table_id);
2090     }
2091   else
2092     rmp->vrf_id = ~0;
2093   rmp->context = ctx->context;
2094
2095   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2096
2097   return 0;
2098 }
2099
2100 static void
2101 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
2102 {
2103   vl_api_registration_t *reg;
2104
2105   reg = vl_api_client_index_to_registration (mp->client_index);
2106   if (!reg)
2107     return;
2108
2109   nat64_api_walk_ctx_t ctx = {
2110     .reg = reg,
2111     .context = mp->context,
2112   };
2113
2114   nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
2115 }
2116
2117 static void *
2118 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
2119                                      void *handle)
2120 {
2121   u8 *s;
2122
2123   s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
2124
2125   FINISH;
2126 }
2127
2128 static void
2129 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
2130                                           mp)
2131 {
2132   snat_main_t *sm = &snat_main;
2133   vl_api_nat64_add_del_interface_reply_t *rmp;
2134   int rv = 0;
2135
2136   VALIDATE_SW_IF_INDEX (mp);
2137
2138   rv =
2139     nat64_add_del_interface (ntohl (mp->sw_if_index), mp->is_inside,
2140                              mp->is_add);
2141
2142   BAD_SW_IF_INDEX_LABEL;
2143
2144   REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
2145 }
2146
2147 static void *
2148 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
2149                                         void *handle)
2150 {
2151   u8 *s;
2152
2153   s = format (0, "SCRIPT: nat64_add_del_interface ");
2154   s = format (s, "sw_if_index %d %s %s",
2155               clib_host_to_net_u32 (mp->sw_if_index),
2156               mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
2157
2158   FINISH;
2159 }
2160
2161 static int
2162 nat64_api_interface_walk (snat_interface_t * i, void *arg)
2163 {
2164   vl_api_nat64_interface_details_t *rmp;
2165   snat_main_t *sm = &snat_main;
2166   nat64_api_walk_ctx_t *ctx = arg;
2167
2168   rmp = vl_msg_api_alloc (sizeof (*rmp));
2169   memset (rmp, 0, sizeof (*rmp));
2170   rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2171   rmp->sw_if_index = ntohl (i->sw_if_index);
2172   rmp->is_inside = (nat_interface_is_inside (i)
2173                     && nat_interface_is_outside (i)) ? 2 :
2174     nat_interface_is_inside (i);
2175   rmp->context = ctx->context;
2176
2177   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2178
2179   return 0;
2180 }
2181
2182 static void
2183 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2184 {
2185   vl_api_registration_t *reg;
2186
2187   reg = vl_api_client_index_to_registration (mp->client_index);
2188   if (!reg)
2189     return;
2190
2191   nat64_api_walk_ctx_t ctx = {
2192     .reg = reg,
2193     .context = mp->context,
2194   };
2195
2196   nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2197 }
2198
2199 static void *
2200 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2201                                      void *handle)
2202 {
2203   u8 *s;
2204
2205   s = format (0, "SCRIPT: snat_interface_dump ");
2206
2207   FINISH;
2208 }
2209
2210 static void
2211   vl_api_nat64_add_del_static_bib_t_handler
2212   (vl_api_nat64_add_del_static_bib_t * mp)
2213 {
2214   snat_main_t *sm = &snat_main;
2215   vl_api_nat64_add_del_static_bib_reply_t *rmp;
2216   ip6_address_t in_addr;
2217   ip4_address_t out_addr;
2218   int rv = 0;
2219
2220   memcpy (&in_addr.as_u8, mp->i_addr, 16);
2221   memcpy (&out_addr.as_u8, mp->o_addr, 4);
2222
2223   rv =
2224     nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2225                                     clib_net_to_host_u16 (mp->i_port),
2226                                     clib_net_to_host_u16 (mp->o_port),
2227                                     mp->proto,
2228                                     clib_net_to_host_u32 (mp->vrf_id),
2229                                     mp->is_add);
2230
2231   REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2232 }
2233
2234 static void *vl_api_nat64_add_del_static_bib_t_print
2235   (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2236 {
2237   u8 *s;
2238
2239   s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2240   s = format (s, "protocol %d i_addr %U o_addr %U ",
2241               mp->proto,
2242               format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2243
2244   if (mp->vrf_id != ~0)
2245     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2246
2247   FINISH;
2248 }
2249
2250 static int
2251 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2252 {
2253   vl_api_nat64_bib_details_t *rmp;
2254   snat_main_t *sm = &snat_main;
2255   nat64_api_walk_ctx_t *ctx = arg;
2256   fib_table_t *fib;
2257
2258   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2259   if (!fib)
2260     return -1;
2261
2262   rmp = vl_msg_api_alloc (sizeof (*rmp));
2263   memset (rmp, 0, sizeof (*rmp));
2264   rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2265   rmp->context = ctx->context;
2266   clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2267   clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2268   rmp->i_port = bibe->in_port;
2269   rmp->o_port = bibe->out_port;
2270   rmp->vrf_id = ntohl (fib->ft_table_id);
2271   rmp->proto = bibe->proto;
2272   rmp->is_static = bibe->is_static;
2273   rmp->ses_num = ntohl (bibe->ses_num);
2274
2275   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2276
2277   return 0;
2278 }
2279
2280 static void
2281 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2282 {
2283   vl_api_registration_t *reg;
2284   nat64_main_t *nm = &nat64_main;
2285   nat64_db_t *db;
2286
2287   reg = vl_api_client_index_to_registration (mp->client_index);
2288   if (!reg)
2289     return;
2290
2291   nat64_api_walk_ctx_t ctx = {
2292     .reg = reg,
2293     .context = mp->context,
2294   };
2295
2296   /* *INDENT-OFF* */
2297   vec_foreach (db, nm->db)
2298     nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2299   /* *INDENT-ON* */
2300 }
2301
2302 static void *
2303 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2304 {
2305   u8 *s;
2306
2307   s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2308
2309   FINISH;
2310 }
2311
2312 static void
2313 vl_api_nat64_set_timeouts_t_handler (vl_api_nat64_set_timeouts_t * mp)
2314 {
2315   snat_main_t *sm = &snat_main;
2316   vl_api_nat64_set_timeouts_reply_t *rmp;
2317   int rv = 0;
2318
2319   rv = nat64_set_icmp_timeout (ntohl (mp->icmp));
2320   if (rv)
2321     goto send_reply;
2322   rv = nat64_set_udp_timeout (ntohl (mp->udp));
2323   if (rv)
2324     goto send_reply;
2325   rv =
2326     nat64_set_tcp_timeouts (ntohl (mp->tcp_trans), ntohl (mp->tcp_est),
2327                             ntohl (mp->tcp_incoming_syn));
2328
2329 send_reply:
2330   REPLY_MACRO (VL_API_NAT64_SET_TIMEOUTS_REPLY);
2331 }
2332
2333 static void *vl_api_nat64_set_timeouts_t_print
2334   (vl_api_nat64_set_timeouts_t * mp, void *handle)
2335 {
2336   u8 *s;
2337
2338   s = format (0, "SCRIPT: nat64_set_timeouts ");
2339   s =
2340     format (s,
2341             "udp %d icmp %d, tcp_trans %d, tcp_est %d, tcp_incoming_syn %d\n",
2342             ntohl (mp->udp), ntohl (mp->icmp), ntohl (mp->tcp_trans),
2343             ntohl (mp->tcp_est), ntohl (mp->tcp_incoming_syn));
2344
2345   FINISH;
2346 }
2347
2348 static void
2349 vl_api_nat64_get_timeouts_t_handler (vl_api_nat64_get_timeouts_t * mp)
2350 {
2351   snat_main_t *sm = &snat_main;
2352   vl_api_nat64_get_timeouts_reply_t *rmp;
2353   int rv = 0;
2354
2355   /* *INDENT-OFF* */
2356   REPLY_MACRO2 (VL_API_NAT64_GET_TIMEOUTS_REPLY,
2357   ({
2358     rmp->udp = htonl (nat64_get_udp_timeout());
2359     rmp->icmp = htonl (nat64_get_icmp_timeout());
2360     rmp->tcp_trans = htonl (nat64_get_tcp_trans_timeout());
2361     rmp->tcp_est = htonl (nat64_get_tcp_est_timeout());
2362     rmp->tcp_incoming_syn = htonl (nat64_get_tcp_incoming_syn_timeout());
2363   }))
2364   /* *INDENT-ON* */
2365 }
2366
2367 static void *vl_api_nat64_get_timeouts_t_print
2368   (vl_api_nat64_get_timeouts_t * mp, void *handle)
2369 {
2370   u8 *s;
2371
2372   s = format (0, "SCRIPT: nat64_get_timeouts");
2373
2374   FINISH;
2375 }
2376
2377 static int
2378 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2379 {
2380   vl_api_nat64_st_details_t *rmp;
2381   snat_main_t *sm = &snat_main;
2382   nat64_api_walk_ctx_t *ctx = arg;
2383   nat64_db_bib_entry_t *bibe;
2384   fib_table_t *fib;
2385
2386   bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2387   if (!bibe)
2388     return -1;
2389
2390   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2391   if (!fib)
2392     return -1;
2393
2394   rmp = vl_msg_api_alloc (sizeof (*rmp));
2395   memset (rmp, 0, sizeof (*rmp));
2396   rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2397   rmp->context = ctx->context;
2398   clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2399   clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2400   rmp->il_port = bibe->in_port;
2401   rmp->ol_port = bibe->out_port;
2402   clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2403   clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2404   rmp->il_port = ste->r_port;
2405   rmp->vrf_id = ntohl (fib->ft_table_id);
2406   rmp->proto = ste->proto;
2407
2408   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2409
2410   return 0;
2411 }
2412
2413 static void
2414 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2415 {
2416   vl_api_registration_t *reg;
2417   nat64_main_t *nm = &nat64_main;
2418   nat64_db_t *db;
2419
2420   reg = vl_api_client_index_to_registration (mp->client_index);
2421   if (!reg)
2422     return;
2423
2424   nat64_api_walk_ctx_t ctx = {
2425     .reg = reg,
2426     .context = mp->context,
2427   };
2428
2429   /* *INDENT-OFF* */
2430   vec_foreach (db, nm->db)
2431     {
2432       ctx.db = db;
2433       nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
2434     }
2435   /* *INDENT-ON* */
2436 }
2437
2438 static void *
2439 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
2440 {
2441   u8 *s;
2442
2443   s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
2444
2445   FINISH;
2446 }
2447
2448 static void
2449 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
2450 {
2451   vl_api_nat64_add_del_prefix_reply_t *rmp;
2452   snat_main_t *sm = &snat_main;
2453   ip6_address_t prefix;
2454   int rv = 0;
2455
2456   memcpy (&prefix.as_u8, mp->prefix, 16);
2457
2458   rv =
2459     nat64_add_del_prefix (&prefix, mp->prefix_len,
2460                           clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
2461   REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
2462 }
2463
2464 static void *
2465 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
2466                                      void *handle)
2467 {
2468   u8 *s;
2469
2470   s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
2471               format_ip6_address, mp->prefix, mp->prefix_len,
2472               ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2473
2474   FINISH;
2475 }
2476
2477 static int
2478 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
2479 {
2480   vl_api_nat64_prefix_details_t *rmp;
2481   snat_main_t *sm = &snat_main;
2482   nat64_api_walk_ctx_t *ctx = arg;
2483
2484   rmp = vl_msg_api_alloc (sizeof (*rmp));
2485   memset (rmp, 0, sizeof (*rmp));
2486   rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
2487   clib_memcpy (rmp->prefix, &(p->prefix), 16);
2488   rmp->prefix_len = p->plen;
2489   rmp->vrf_id = ntohl (p->vrf_id);
2490   rmp->context = ctx->context;
2491
2492   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2493
2494   return 0;
2495 }
2496
2497 static void
2498 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
2499 {
2500   vl_api_registration_t *reg;
2501
2502   reg = vl_api_client_index_to_registration (mp->client_index);
2503   if (!reg)
2504     return;
2505
2506   nat64_api_walk_ctx_t ctx = {
2507     .reg = reg,
2508     .context = mp->context,
2509   };
2510
2511   nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
2512 }
2513
2514 static void *
2515 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
2516                                   void *handle)
2517 {
2518   u8 *s;
2519
2520   s = format (0, "SCRIPT: nat64_prefix_dump\n");
2521
2522   FINISH;
2523 }
2524
2525 static void
2526   vl_api_nat64_add_del_interface_addr_t_handler
2527   (vl_api_nat64_add_del_interface_addr_t * mp)
2528 {
2529   snat_main_t *sm = &snat_main;
2530   vl_api_nat64_add_del_interface_addr_reply_t *rmp;
2531   u32 sw_if_index = ntohl (mp->sw_if_index);
2532   int rv = 0;
2533
2534   VALIDATE_SW_IF_INDEX (mp);
2535
2536   rv = nat64_add_interface_address (sw_if_index, mp->is_add);
2537
2538   BAD_SW_IF_INDEX_LABEL;
2539
2540   REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
2541 }
2542
2543 static void *vl_api_nat64_add_del_interface_addr_t_print
2544   (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
2545 {
2546   u8 *s;
2547
2548   s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
2549   s = format (s, "sw_if_index %d %s",
2550               clib_host_to_net_u32 (mp->sw_if_index),
2551               mp->is_add ? "" : "del");
2552
2553   FINISH;
2554 }
2555
2556 /***************/
2557 /*** DS-Lite ***/
2558 /***************/
2559
2560 static void
2561 vl_api_dslite_set_aftr_addr_t_handler (vl_api_dslite_set_aftr_addr_t * mp)
2562 {
2563   vl_api_dslite_set_aftr_addr_reply_t *rmp;
2564   snat_main_t *sm = &snat_main;
2565   dslite_main_t *dm = &dslite_main;
2566   int rv = 0;
2567   ip6_address_t ip6_addr;
2568   ip4_address_t ip4_addr;
2569
2570   memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
2571   memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
2572
2573   rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
2574   if (rv == 0)
2575     rv = dslite_set_aftr_ip4_addr (dm, &ip4_addr);
2576
2577   REPLY_MACRO (VL_API_DSLITE_SET_AFTR_ADDR_REPLY);
2578 }
2579
2580 static void *
2581 vl_api_dslite_set_aftr_addr_t_print (vl_api_dslite_set_aftr_addr_t * mp,
2582                                      void *handle)
2583 {
2584   u8 *s;
2585
2586   s = format (0, "SCRIPT: dslite_set_aftr_addr ");
2587   s = format (s, "ip6_addr %U ip4_addr %U\n",
2588               format_ip6_address, mp->ip6_addr,
2589               format_ip4_address, mp->ip4_addr);
2590
2591   FINISH;
2592 }
2593
2594 static void
2595 vl_api_dslite_get_aftr_addr_t_handler (vl_api_dslite_get_aftr_addr_t * mp)
2596 {
2597   snat_main_t *sm = &snat_main;
2598   vl_api_dslite_get_aftr_addr_reply_t *rmp;
2599   dslite_main_t *dm = &dslite_main;
2600   int rv = 0;
2601
2602   /* *INDENT-OFF* */
2603   REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
2604   ({
2605     memcpy (rmp->ip4_addr, &dm->aftr_ip4_addr.as_u8, 4);
2606     memcpy (rmp->ip6_addr, &dm->aftr_ip6_addr.as_u8, 16);
2607   }))
2608   /* *INDENT-ON* */
2609 }
2610
2611 static void *
2612 vl_api_dslite_get_aftr_addr_t_print (vl_api_dslite_get_aftr_addr_t * mp,
2613                                      void *handle)
2614 {
2615   u8 *s;
2616
2617   s = format (0, "SCRIPT: dslite_get_aftr_addr");
2618
2619   FINISH;
2620 }
2621
2622 static void
2623 vl_api_dslite_set_b4_addr_t_handler (vl_api_dslite_set_b4_addr_t * mp)
2624 {
2625   vl_api_dslite_set_b4_addr_reply_t *rmp;
2626   snat_main_t *sm = &snat_main;
2627   dslite_main_t *dm = &dslite_main;
2628   int rv = 0;
2629   ip6_address_t ip6_addr;
2630   ip4_address_t ip4_addr;
2631
2632   memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
2633   memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
2634
2635   rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
2636   if (rv == 0)
2637     rv = dslite_set_b4_ip4_addr (dm, &ip4_addr);
2638
2639   REPLY_MACRO (VL_API_DSLITE_SET_B4_ADDR_REPLY);
2640 }
2641
2642 static void *
2643 vl_api_dslite_set_b4_addr_t_print (vl_api_dslite_set_b4_addr_t * mp,
2644                                    void *handle)
2645 {
2646   u8 *s;
2647
2648   s = format (0, "SCRIPT: dslite_set_b4_addr ");
2649   s = format (s, "ip6_addr %U ip4_addr %U\n",
2650               format_ip6_address, mp->ip6_addr,
2651               format_ip6_address, mp->ip4_addr);
2652
2653   FINISH;
2654 }
2655
2656 static void
2657 vl_api_dslite_get_b4_addr_t_handler (vl_api_dslite_get_b4_addr_t * mp)
2658 {
2659   snat_main_t *sm = &snat_main;
2660   vl_api_dslite_get_b4_addr_reply_t *rmp;
2661   dslite_main_t *dm = &dslite_main;
2662   int rv = 0;
2663
2664   /* *INDENT-OFF* */
2665   REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
2666   ({
2667     memcpy (rmp->ip4_addr, &dm->b4_ip4_addr.as_u8, 4);
2668     memcpy (rmp->ip6_addr, &dm->b4_ip6_addr.as_u8, 16);
2669   }))
2670   /* *INDENT-ON* */
2671 }
2672
2673 static void *
2674 vl_api_dslite_get_b4_addr_t_print (vl_api_dslite_get_b4_addr_t * mp,
2675                                    void *handle)
2676 {
2677   u8 *s;
2678
2679   s = format (0, "SCRIPT: dslite_get_b4_addr");
2680
2681   FINISH;
2682 }
2683
2684 static void
2685   vl_api_dslite_add_del_pool_addr_range_t_handler
2686   (vl_api_dslite_add_del_pool_addr_range_t * mp)
2687 {
2688   vl_api_dslite_add_del_pool_addr_range_reply_t *rmp;
2689   snat_main_t *sm = &snat_main;
2690   dslite_main_t *dm = &dslite_main;
2691   int rv = 0;
2692   ip4_address_t this_addr;
2693   u32 start_host_order, end_host_order;
2694   int i, count;
2695   u32 *tmp;
2696
2697   tmp = (u32 *) mp->start_addr;
2698   start_host_order = clib_host_to_net_u32 (tmp[0]);
2699   tmp = (u32 *) mp->end_addr;
2700   end_host_order = clib_host_to_net_u32 (tmp[0]);
2701
2702   count = (end_host_order - start_host_order) + 1;
2703   memcpy (&this_addr.as_u8, mp->start_addr, 4);
2704
2705   for (i = 0; i < count; i++)
2706     {
2707       if ((rv = dslite_add_del_pool_addr (dm, &this_addr, mp->is_add)))
2708         goto send_reply;
2709
2710       increment_v4_address (&this_addr);
2711     }
2712
2713 send_reply:
2714   REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2715 }
2716
2717 static void
2718 send_dslite_address_details (snat_address_t * ap,
2719                              vl_api_registration_t * reg, u32 context)
2720 {
2721   vl_api_dslite_address_details_t *rmp;
2722   snat_main_t *sm = &snat_main;
2723
2724   rmp = vl_msg_api_alloc (sizeof (*rmp));
2725
2726   memset (rmp, 0, sizeof (*rmp));
2727
2728   rmp->_vl_msg_id = ntohs (VL_API_DSLITE_ADDRESS_DETAILS + sm->msg_id_base);
2729   clib_memcpy (rmp->ip_address, &(ap->addr), 4);
2730   rmp->context = context;
2731
2732   vl_api_send_msg (reg, (u8 *) rmp);
2733 }
2734
2735 static void
2736 vl_api_dslite_address_dump_t_handler (vl_api_dslite_address_dump_t * mp)
2737 {
2738   vl_api_registration_t *reg;
2739   dslite_main_t *dm = &dslite_main;
2740   snat_address_t *ap;
2741
2742   reg = vl_api_client_index_to_registration (mp->client_index);
2743   if (!reg)
2744     return;
2745
2746   /* *INDENT-OFF* */
2747   vec_foreach (ap, dm->addr_pool)
2748     {
2749       send_dslite_address_details (ap, reg, mp->context);
2750     }
2751   /* *INDENT-ON* */
2752 }
2753
2754 static void *
2755 vl_api_dslite_address_dump_t_print (vl_api_dslite_address_dump_t * mp,
2756                                     void *handle)
2757 {
2758   u8 *s;
2759
2760   s = format (0, "SCRIPT: dslite_address_dump ");
2761
2762   FINISH;
2763 }
2764
2765 static void *vl_api_dslite_add_del_pool_addr_range_t_print
2766   (vl_api_dslite_add_del_pool_addr_range_t * mp, void *handle)
2767 {
2768   u8 *s;
2769
2770   s = format (0, "SCRIPT: dslite_add_del_pool_addr_range ");
2771   s = format (s, "%U - %U\n",
2772               format_ip4_address, mp->start_addr,
2773               format_ip4_address, mp->end_addr);
2774
2775   FINISH;
2776 }
2777
2778
2779 /*************/
2780 /*** NAT66 ***/
2781 /*************/
2782
2783 static void
2784 vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
2785                                           mp)
2786 {
2787   snat_main_t *sm = &snat_main;
2788   vl_api_nat66_add_del_interface_reply_t *rmp;
2789   int rv = 0;
2790
2791   VALIDATE_SW_IF_INDEX (mp);
2792
2793   rv =
2794     nat66_interface_add_del (ntohl (mp->sw_if_index), mp->is_inside,
2795                              mp->is_add);
2796
2797   BAD_SW_IF_INDEX_LABEL;
2798
2799   REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
2800 }
2801
2802 static void *
2803 vl_api_nat66_add_del_interface_t_print (vl_api_nat66_add_del_interface_t * mp,
2804                                         void *handle)
2805 {
2806   u8 *s;
2807
2808   s = format (0, "SCRIPT: nat66_add_del_interface ");
2809   s = format (s, "sw_if_index %d %s %s",
2810               clib_host_to_net_u32 (mp->sw_if_index),
2811               mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
2812
2813   FINISH;
2814 }
2815
2816 static void
2817   vl_api_nat66_add_del_static_mapping_t_handler
2818   (vl_api_nat66_add_del_static_mapping_t * mp)
2819 {
2820   snat_main_t *sm = &snat_main;
2821   vl_api_nat66_add_del_static_mapping_reply_t *rmp;
2822   ip6_address_t l_addr, e_addr;
2823   int rv = 0;
2824
2825   memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
2826   memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
2827
2828   rv =
2829     nat66_static_mapping_add_del (&l_addr, &e_addr,
2830                                   clib_net_to_host_u32 (mp->vrf_id),
2831                                   mp->is_add);
2832
2833   REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
2834 }
2835
2836 static void *vl_api_nat66_add_del_static_mapping_t_print
2837   (vl_api_nat66_add_del_static_mapping_t * mp, void *handle)
2838 {
2839   u8 *s;
2840
2841   s = format (0, "SCRIPT: nat66_add_del_static_mapping ");
2842   s = format (s, "local_ip_address %U external_ip_address %U vrf_id %d %s",
2843               format_ip6_address, mp->local_ip_address,
2844               format_ip6_address, mp->external_ip_address,
2845               clib_net_to_host_u32 (mp->vrf_id), mp->is_add ? "" : "del");
2846
2847   FINISH;
2848 }
2849
2850 typedef struct nat66_api_walk_ctx_t_
2851 {
2852   svm_queue_t *q;
2853   u32 context;
2854 } nat66_api_walk_ctx_t;
2855
2856 static int
2857 nat66_api_interface_walk (snat_interface_t * i, void *arg)
2858 {
2859   vl_api_nat66_interface_details_t *rmp;
2860   snat_main_t *sm = &snat_main;
2861   nat66_api_walk_ctx_t *ctx = arg;
2862
2863   rmp = vl_msg_api_alloc (sizeof (*rmp));
2864   memset (rmp, 0, sizeof (*rmp));
2865   rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + sm->msg_id_base);
2866   rmp->sw_if_index = ntohl (i->sw_if_index);
2867   rmp->is_inside = nat_interface_is_inside (i);
2868   rmp->context = ctx->context;
2869
2870   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
2871
2872   return 0;
2873 }
2874
2875 static void
2876 vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
2877 {
2878   svm_queue_t *q;
2879
2880   q = vl_api_client_index_to_input_queue (mp->client_index);
2881   if (q == 0)
2882     return;
2883
2884   nat66_api_walk_ctx_t ctx = {
2885     .q = q,
2886     .context = mp->context,
2887   };
2888
2889   nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
2890 }
2891
2892 static void *
2893 vl_api_nat66_interface_dump_t_print (vl_api_nat66_interface_dump_t * mp,
2894                                      void *handle)
2895 {
2896   u8 *s;
2897
2898   s = format (0, "SCRIPT: nat66_interface_dump ");
2899
2900   FINISH;
2901 }
2902
2903 static int
2904 nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
2905 {
2906   vl_api_nat66_static_mapping_details_t *rmp;
2907   nat66_main_t *nm = &nat66_main;
2908   snat_main_t *sm = &snat_main;
2909   nat66_api_walk_ctx_t *ctx = arg;
2910   fib_table_t *fib;
2911   vlib_counter_t vc;
2912
2913   fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
2914   if (!fib)
2915     return -1;
2916
2917   vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
2918
2919   rmp = vl_msg_api_alloc (sizeof (*rmp));
2920   memset (rmp, 0, sizeof (*rmp));
2921   rmp->_vl_msg_id =
2922     ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + sm->msg_id_base);
2923   clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
2924   clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
2925   rmp->vrf_id = ntohl (fib->ft_table_id);
2926   rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
2927   rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
2928   rmp->context = ctx->context;
2929
2930   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
2931
2932   return 0;
2933 }
2934
2935 static void
2936 vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
2937                                             * mp)
2938 {
2939   svm_queue_t *q;
2940
2941   q = vl_api_client_index_to_input_queue (mp->client_index);
2942   if (q == 0)
2943     return;
2944
2945   nat66_api_walk_ctx_t ctx = {
2946     .q = q,
2947     .context = mp->context,
2948   };
2949
2950   nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
2951 }
2952
2953 static void *
2954 vl_api_nat66_static_mapping_dump_t_print (vl_api_nat66_static_mapping_dump_t *
2955                                           mp, void *handle)
2956 {
2957   u8 *s;
2958
2959   s = format (0, "SCRIPT: nat66_static_mapping_dump ");
2960
2961   FINISH;
2962 }
2963
2964
2965 /* List of message types that this plugin understands */
2966 #define foreach_snat_plugin_api_msg                                     \
2967 _(NAT_CONTROL_PING, nat_control_ping)                                   \
2968 _(NAT_SHOW_CONFIG, nat_show_config)                                     \
2969 _(NAT_SET_WORKERS, nat_set_workers)                                     \
2970 _(NAT_WORKER_DUMP, nat_worker_dump)                                     \
2971 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable)                   \
2972 _(NAT_SET_REASS, nat_set_reass)                                         \
2973 _(NAT_GET_REASS, nat_get_reass)                                         \
2974 _(NAT_REASS_DUMP, nat_reass_dump)                                       \
2975 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range)             \
2976 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature)     \
2977 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping)           \
2978 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping)       \
2979 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump)                 \
2980 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump)             \
2981 _(NAT44_ADDRESS_DUMP, nat44_address_dump)                               \
2982 _(NAT44_INTERFACE_DUMP, nat44_interface_dump)                           \
2983 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr)           \
2984 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump)                 \
2985 _(NAT44_USER_DUMP, nat44_user_dump)                                     \
2986 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump)                     \
2987 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE,                               \
2988   nat44_interface_add_del_output_feature)                               \
2989 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP,                                  \
2990   nat44_interface_output_feature_dump)                                  \
2991 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping)     \
2992 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump)           \
2993 _(NAT44_DEL_SESSION, nat44_del_session)                                 \
2994 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable)     \
2995 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled)             \
2996 _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map)                             \
2997 _(NAT_DET_FORWARD, nat_det_forward)                                     \
2998 _(NAT_DET_REVERSE, nat_det_reverse)                                     \
2999 _(NAT_DET_MAP_DUMP, nat_det_map_dump)                                   \
3000 _(NAT_DET_SET_TIMEOUTS, nat_det_set_timeouts)                           \
3001 _(NAT_DET_GET_TIMEOUTS, nat_det_get_timeouts)                           \
3002 _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out)                 \
3003 _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in)                   \
3004 _(NAT_DET_SESSION_DUMP, nat_det_session_dump)                           \
3005 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range)         \
3006 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump)                           \
3007 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface)                     \
3008 _(NAT64_INTERFACE_DUMP, nat64_interface_dump)                           \
3009 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib)                   \
3010 _(NAT64_BIB_DUMP, nat64_bib_dump)                                       \
3011 _(NAT64_SET_TIMEOUTS, nat64_set_timeouts)                               \
3012 _(NAT64_GET_TIMEOUTS, nat64_get_timeouts)                               \
3013 _(NAT64_ST_DUMP, nat64_st_dump)                                         \
3014 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix)                           \
3015 _(NAT64_PREFIX_DUMP, nat64_prefix_dump)                                 \
3016 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr)           \
3017 _(DSLITE_ADD_DEL_POOL_ADDR_RANGE, dslite_add_del_pool_addr_range)       \
3018 _(DSLITE_ADDRESS_DUMP, dslite_address_dump)                             \
3019 _(DSLITE_SET_AFTR_ADDR, dslite_set_aftr_addr)                           \
3020 _(DSLITE_GET_AFTR_ADDR, dslite_get_aftr_addr)                           \
3021 _(DSLITE_SET_B4_ADDR, dslite_set_b4_addr)                               \
3022 _(DSLITE_GET_B4_ADDR, dslite_get_b4_addr)                               \
3023 _(NAT66_ADD_DEL_INTERFACE, nat66_add_del_interface)                     \
3024 _(NAT66_INTERFACE_DUMP, nat66_interface_dump)                           \
3025 _(NAT66_ADD_DEL_STATIC_MAPPING, nat66_add_del_static_mapping)           \
3026 _(NAT66_STATIC_MAPPING_DUMP, nat66_static_mapping_dump)
3027
3028 /* Set up the API message handling tables */
3029 static clib_error_t *
3030 snat_plugin_api_hookup (vlib_main_t * vm)
3031 {
3032   snat_main_t *sm __attribute__ ((unused)) = &snat_main;
3033 #define _(N,n)                                                  \
3034     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
3035                            #n,                                  \
3036                            vl_api_##n##_t_handler,              \
3037                            vl_noop_handler,                     \
3038                            vl_api_##n##_t_endian,               \
3039                            vl_api_##n##_t_print,                \
3040                            sizeof(vl_api_##n##_t), 1);
3041   foreach_snat_plugin_api_msg;
3042 #undef _
3043
3044   return 0;
3045 }
3046
3047 #define vl_msg_name_crc_list
3048 #include <nat/nat_all_api_h.h>
3049 #undef vl_msg_name_crc_list
3050
3051 static void
3052 setup_message_id_table (snat_main_t * sm, api_main_t * am)
3053 {
3054 #define _(id,n,crc) \
3055   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
3056   foreach_vl_msg_name_crc_nat;
3057 #undef _
3058 }
3059
3060 static void
3061 plugin_custom_dump_configure (snat_main_t * sm)
3062 {
3063 #define _(n,f) sm->api_main->msg_print_handlers \
3064   [VL_API_##n + sm->msg_id_base]                \
3065     = (void *) vl_api_##f##_t_print;
3066   foreach_snat_plugin_api_msg;
3067 #undef _
3068 }
3069
3070 clib_error_t *
3071 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
3072 {
3073   u8 *name;
3074   clib_error_t *error = 0;
3075
3076   name = format (0, "nat_%08x%c", api_version, 0);
3077
3078   /* Ask for a correctly-sized block of API message decode slots */
3079   sm->msg_id_base =
3080     vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
3081
3082   error = snat_plugin_api_hookup (vm);
3083
3084   /* Add our API messages to the global name_crc hash table */
3085   setup_message_id_table (sm, sm->api_main);
3086
3087   plugin_custom_dump_configure (sm);
3088
3089   vec_free (name);
3090
3091   return error;
3092 }
3093
3094 /*
3095  * fd.io coding-style-patch-verification: ON
3096  *
3097  * Local Variables:
3098  * eval: (c-set-style "gnu")
3099  * End:
3100  */