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