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