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