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