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