d0efb00184302a0beb798979bc3dcd425a6c4fc9
[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/dslite.h>
25 #include <nat/nat_reass.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28
29 #include <nat/nat_msg_enum.h>
30 #include <vnet/fib/fib_table.h>
31
32 #define vl_api_nat44_lb_addr_port_t_endian vl_noop_handler
33 #define vl_api_nat44_add_del_lb_static_mapping_t_endian vl_noop_handler
34 #define vl_api_nat44_nat44_lb_static_mapping_details_t_endian vl_noop_handler
35
36 /* define message structures */
37 #define vl_typedefs
38 #include <nat/nat_all_api_h.h>
39 #undef vl_typedefs
40
41 /* define generated endian-swappers */
42 #define vl_endianfun
43 #include <nat/nat_all_api_h.h>
44 #undef vl_endianfun
45
46 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47
48 #define REPLY_MSG_ID_BASE sm->msg_id_base
49 #include <vlibapi/api_helper_macros.h>
50
51 /* Get the API version number */
52 #define vl_api_version(n,v) static u32 api_version=(v);
53 #include <nat/nat_all_api_h.h>
54 #undef vl_api_version
55
56 /* Macro to finish up custom dump fns */
57 #define FINISH                                  \
58     vec_add1 (s, 0);                            \
59     vl_print (handle, (char *)s);               \
60     vec_free (s);                               \
61     return handle;
62
63
64 /******************************/
65 /*** Common NAT plugin APIs ***/
66 /******************************/
67
68 static void
69 vl_api_nat_control_ping_t_handler (vl_api_nat_control_ping_t * mp)
70 {
71   vl_api_nat_control_ping_reply_t *rmp;
72   snat_main_t *sm = &snat_main;
73   int rv = 0;
74
75   /* *INDENT-OFF* */
76   REPLY_MACRO2 (VL_API_NAT_CONTROL_PING_REPLY,
77   ({
78     rmp->vpe_pid = ntohl (getpid ());
79   }));
80   /* *INDENT-ON* */
81 }
82
83 static void *
84 vl_api_nat_control_ping_t_print (vl_api_nat_control_ping_t * mp, void *handle)
85 {
86   u8 *s;
87
88   s = format (0, "SCRIPT: nat_control_ping ");
89
90   FINISH;
91 }
92
93 static void
94 vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp)
95 {
96   vl_api_nat_show_config_reply_t *rmp;
97   snat_main_t *sm = &snat_main;
98   int rv = 0;
99
100   /* *INDENT-OFF* */
101   REPLY_MACRO2 (VL_API_NAT_SHOW_CONFIG_REPLY,
102   ({
103     rmp->translation_buckets = htonl (sm->translation_buckets);
104     rmp->translation_memory_size = htonl (sm->translation_memory_size);
105     rmp->user_buckets = htonl (sm->user_buckets);
106     rmp->user_memory_size = htonl (sm->user_memory_size);
107     rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
108     rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
109     rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
110     rmp->static_mapping_only = sm->static_mapping_only;
111     rmp->static_mapping_connection_tracking =
112       sm->static_mapping_connection_tracking;
113     rmp->deterministic = sm->deterministic;
114   }));
115   /* *INDENT-ON* */
116 }
117
118 static void *
119 vl_api_nat_show_config_t_print (vl_api_nat_show_config_t * mp, void *handle)
120 {
121   u8 *s;
122
123   s = format (0, "SCRIPT: nat_show_config ");
124
125   FINISH;
126 }
127
128 static void
129 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
130 {
131   snat_main_t *sm = &snat_main;
132   vl_api_nat_set_workers_reply_t *rmp;
133   int rv = 0;
134   uword *bitmap = 0;
135   u64 mask = clib_net_to_host_u64 (mp->worker_mask);
136
137   if (sm->num_workers < 2)
138     {
139       rv = VNET_API_ERROR_FEATURE_DISABLED;
140       goto send_reply;
141     }
142
143   bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
144   rv = snat_set_workers (bitmap);
145   clib_bitmap_free (bitmap);
146
147 send_reply:
148   REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
149 }
150
151 static void *
152 vl_api_nat_set_workers_t_print (vl_api_nat_set_workers_t * mp, void *handle)
153 {
154   u8 *s;
155   uword *bitmap = 0;
156   u8 first = 1;
157   int i;
158   u64 mask = clib_net_to_host_u64 (mp->worker_mask);
159
160   s = format (0, "SCRIPT: nat_set_workers ");
161   bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
162   /* *INDENT-OFF* */
163   clib_bitmap_foreach (i, bitmap,
164     ({
165       if (first)
166         s = format (s, "%d", i);
167       else
168         s = format (s, ",%d", i);
169       first = 0;
170     }));
171   /* *INDENT-ON* */
172   clib_bitmap_free (bitmap);
173   FINISH;
174 }
175
176 static void
177 send_nat_worker_details (u32 worker_index, unix_shared_memory_queue_t * q,
178                          u32 context)
179 {
180   vl_api_nat_worker_details_t *rmp;
181   snat_main_t *sm = &snat_main;
182   vlib_worker_thread_t *w =
183     vlib_worker_threads + worker_index + sm->first_worker_index;
184
185   rmp = vl_msg_api_alloc (sizeof (*rmp));
186   memset (rmp, 0, sizeof (*rmp));
187   rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
188   rmp->context = context;
189   rmp->worker_index = htonl (worker_index);
190   rmp->lcore_id = htonl (w->lcore_id);
191   strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
192
193   vl_msg_api_send_shmem (q, (u8 *) & rmp);
194 }
195
196 static void
197 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
198 {
199   unix_shared_memory_queue_t *q;
200   snat_main_t *sm = &snat_main;
201   u32 *worker_index;
202
203   q = vl_api_client_index_to_input_queue (mp->client_index);
204   if (q == 0)
205     return;
206
207   /* *INDENT-OFF* */
208   vec_foreach (worker_index, sm->workers)
209     send_nat_worker_details(*worker_index, q, mp->context);
210   /* *INDENT-ON* */
211 }
212
213 static void *
214 vl_api_nat_worker_dump_t_print (vl_api_nat_worker_dump_t * mp, void *handle)
215 {
216   u8 *s;
217
218   s = format (0, "SCRIPT: nat_worker_dump ");
219
220   FINISH;
221 }
222
223 static void
224 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
225                                            mp)
226 {
227   snat_main_t *sm = &snat_main;
228   vl_api_nat_ipfix_enable_disable_reply_t *rmp;
229   int rv = 0;
230
231   rv = snat_ipfix_logging_enable_disable (mp->enable,
232                                           clib_host_to_net_u32
233                                           (mp->domain_id),
234                                           clib_host_to_net_u16
235                                           (mp->src_port));
236
237   REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
238 }
239
240 static void *
241 vl_api_nat_ipfix_enable_disable_t_print (vl_api_nat_ipfix_enable_disable_t *
242                                          mp, void *handle)
243 {
244   u8 *s;
245
246   s = format (0, "SCRIPT: nat_ipfix_enable_disable ");
247   if (mp->domain_id)
248     s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
249   if (mp->src_port)
250     s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
251   if (!mp->enable)
252     s = format (s, "disable ");
253
254   FINISH;
255 }
256
257 static void
258 vl_api_nat_set_reass_t_handler (vl_api_nat_set_reass_t * mp)
259 {
260   snat_main_t *sm = &snat_main;
261   vl_api_nat_set_reass_reply_t *rmp;
262   int rv = 0;
263
264   rv =
265     nat_reass_set (ntohl (mp->timeout), ntohs (mp->max_reass), mp->max_frag,
266                    mp->drop_frag, mp->is_ip6);
267
268   REPLY_MACRO (VL_API_NAT_SET_REASS_REPLY);
269 }
270
271 static void *
272 vl_api_nat_set_reass_t_print (vl_api_nat_set_reass_t * mp, void *handle)
273 {
274   u8 *s;
275
276   s = format (0, "SCRIPT: nat_set_reass ");
277   s = format (s, "timeout %d max_reass %d max_frag %d drop_frag %d is_ip6 %d",
278               clib_host_to_net_u32 (mp->timeout),
279               clib_host_to_net_u16 (mp->max_reass),
280               mp->max_frag, mp->drop_frag, mp->is_ip6);
281
282   FINISH;
283 }
284
285 static void
286 vl_api_nat_get_reass_t_handler (vl_api_nat_get_reass_t * mp)
287 {
288   snat_main_t *sm = &snat_main;
289   vl_api_nat_get_reass_reply_t *rmp;
290   int rv = 0;
291
292   /* *INDENT-OFF* */
293   REPLY_MACRO2 (VL_API_NAT_GET_REASS_REPLY,
294   ({
295     rmp->ip4_timeout = htonl (nat_reass_get_timeout(0));
296     rmp->ip4_max_reass = htons (nat_reass_get_max_reass(0));
297     rmp->ip4_max_frag = nat_reass_get_max_frag(0);
298     rmp->ip4_drop_frag = nat_reass_is_drop_frag(0);
299     rmp->ip6_timeout = htonl (nat_reass_get_timeout(1));
300     rmp->ip6_max_reass = htons (nat_reass_get_max_reass(1));
301     rmp->ip6_max_frag = nat_reass_get_max_frag(1);
302     rmp->ip6_drop_frag = nat_reass_is_drop_frag(1);
303   }))
304   /* *INDENT-ON* */
305 }
306
307 static void *
308 vl_api_nat_get_reass_t_print (vl_api_nat_get_reass_t * mp, void *handle)
309 {
310   u8 *s;
311
312   s = format (0, "SCRIPT: nat_get_reass");
313
314   FINISH;
315 }
316
317 typedef struct nat_api_walk_ctx_t_
318 {
319   unix_shared_memory_queue_t *q;
320   u32 context;
321 } nat_api_walk_ctx_t;
322
323 static int
324 nat_ip4_reass_walk_api (nat_reass_ip4_t * reass, void *arg)
325 {
326   vl_api_nat_reass_details_t *rmp;
327   snat_main_t *sm = &snat_main;
328   nat_api_walk_ctx_t *ctx = arg;
329
330   rmp = vl_msg_api_alloc (sizeof (*rmp));
331   memset (rmp, 0, sizeof (*rmp));
332   rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
333   rmp->context = ctx->context;
334   clib_memcpy (rmp->src_addr, &(reass->key.src), 4);
335   clib_memcpy (rmp->dst_addr, &(reass->key.dst), 4);
336   rmp->proto = reass->key.proto;
337   rmp->frag_id = ntohl (reass->key.frag_id);
338   rmp->frag_n = reass->frag_n;
339   rmp->is_ip4 = 1;
340
341   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
342
343   return 0;
344 }
345
346 static int
347 nat_ip6_reass_walk_api (nat_reass_ip6_t * reass, void *arg)
348 {
349   vl_api_nat_reass_details_t *rmp;
350   snat_main_t *sm = &snat_main;
351   nat_api_walk_ctx_t *ctx = arg;
352
353   rmp = vl_msg_api_alloc (sizeof (*rmp));
354   memset (rmp, 0, sizeof (*rmp));
355   rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
356   rmp->context = ctx->context;
357   clib_memcpy (rmp->src_addr, &(reass->key.src), 16);
358   clib_memcpy (rmp->dst_addr, &(reass->key.dst), 16);
359   rmp->proto = reass->key.proto;
360   rmp->frag_id = ntohl (reass->key.frag_id);
361   rmp->frag_n = reass->frag_n;
362   rmp->is_ip4 = 0;
363
364   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
365
366   return 0;
367 }
368
369 static void
370 vl_api_nat_reass_dump_t_handler (vl_api_nat_reass_dump_t * mp)
371 {
372   unix_shared_memory_queue_t *q;
373
374   q = vl_api_client_index_to_input_queue (mp->client_index);
375   if (q == 0)
376     return;
377
378   nat_api_walk_ctx_t ctx = {
379     .q = q,
380     .context = mp->context,
381   };
382
383   nat_ip4_reass_walk (nat_ip4_reass_walk_api, &ctx);
384   nat_ip6_reass_walk (nat_ip6_reass_walk_api, &ctx);
385 }
386
387 static void *
388 vl_api_nat_reass_dump_t_print (vl_api_nat_reass_dump_t * mp, void *handle)
389 {
390   u8 *s;
391
392   s = format (0, "SCRIPT: nat_reass_dump");
393
394   FINISH;
395 }
396
397 /*************/
398 /*** NAT44 ***/
399 /*************/
400 static void
401   vl_api_nat44_add_del_address_range_t_handler
402   (vl_api_nat44_add_del_address_range_t * mp)
403 {
404   snat_main_t *sm = &snat_main;
405   vl_api_nat44_add_del_address_range_reply_t *rmp;
406   ip4_address_t this_addr;
407   u32 start_host_order, end_host_order;
408   u32 vrf_id;
409   int i, count;
410   int rv = 0;
411   u32 *tmp;
412
413   if (sm->static_mapping_only)
414     {
415       rv = VNET_API_ERROR_FEATURE_DISABLED;
416       goto send_reply;
417     }
418
419   tmp = (u32 *) mp->first_ip_address;
420   start_host_order = clib_host_to_net_u32 (tmp[0]);
421   tmp = (u32 *) mp->last_ip_address;
422   end_host_order = clib_host_to_net_u32 (tmp[0]);
423
424   count = (end_host_order - start_host_order) + 1;
425
426   vrf_id = clib_host_to_net_u32 (mp->vrf_id);
427
428   if (count > 1024)
429     clib_warning ("%U - %U, %d addresses...",
430                   format_ip4_address, mp->first_ip_address,
431                   format_ip4_address, mp->last_ip_address, count);
432
433   memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
434
435   for (i = 0; i < count; i++)
436     {
437       if (mp->is_add)
438         snat_add_address (sm, &this_addr, vrf_id, mp->twice_nat);
439       else
440         rv = snat_del_address (sm, this_addr, 0, mp->twice_nat);
441
442       if (rv)
443         goto send_reply;
444
445       if (sm->out2in_dpo)
446         nat44_add_del_address_dpo (this_addr, mp->is_add);
447
448       increment_v4_address (&this_addr);
449     }
450
451 send_reply:
452   REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
453 }
454
455 static void *vl_api_nat44_add_del_address_range_t_print
456   (vl_api_nat44_add_del_address_range_t * mp, void *handle)
457 {
458   u8 *s;
459
460   s = format (0, "SCRIPT: nat44_add_address_range ");
461   s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
462   if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
463     {
464       s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
465     }
466   s = format (s, "twice_nat %d ", mp->twice_nat);
467   FINISH;
468 }
469
470 static void
471 send_nat44_address_details (snat_address_t * a,
472                             unix_shared_memory_queue_t * q, u32 context,
473                             u8 twice_nat)
474 {
475   vl_api_nat44_address_details_t *rmp;
476   snat_main_t *sm = &snat_main;
477
478   rmp = vl_msg_api_alloc (sizeof (*rmp));
479   memset (rmp, 0, sizeof (*rmp));
480   rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
481   clib_memcpy (rmp->ip_address, &(a->addr), 4);
482   if (a->fib_index != ~0)
483     {
484       fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
485       rmp->vrf_id = ntohl (fib->ft_table_id);
486     }
487   else
488     rmp->vrf_id = ~0;
489   rmp->twice_nat = twice_nat;
490   rmp->context = context;
491
492   vl_msg_api_send_shmem (q, (u8 *) & rmp);
493 }
494
495 static void
496 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
497 {
498   unix_shared_memory_queue_t *q;
499   snat_main_t *sm = &snat_main;
500   snat_address_t *a;
501
502   q = vl_api_client_index_to_input_queue (mp->client_index);
503   if (q == 0)
504     return;
505
506   /* *INDENT-OFF* */
507   vec_foreach (a, sm->addresses)
508     send_nat44_address_details (a, q, mp->context, 0);
509   vec_foreach (a, sm->twice_nat_addresses)
510     send_nat44_address_details (a, q, mp->context, 1);
511   /* *INDENT-ON* */
512 }
513
514 static void *
515 vl_api_nat44_address_dump_t_print (vl_api_nat44_address_dump_t * mp,
516                                    void *handle)
517 {
518   u8 *s;
519
520   s = format (0, "SCRIPT: nat44_address_dump ");
521
522   FINISH;
523 }
524
525 static void
526   vl_api_nat44_interface_add_del_feature_t_handler
527   (vl_api_nat44_interface_add_del_feature_t * mp)
528 {
529   snat_main_t *sm = &snat_main;
530   vl_api_nat44_interface_add_del_feature_reply_t *rmp;
531   u8 is_del = mp->is_add == 0;
532   u32 sw_if_index = ntohl (mp->sw_if_index);
533   int rv = 0;
534
535   VALIDATE_SW_IF_INDEX (mp);
536
537   rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
538
539   BAD_SW_IF_INDEX_LABEL;
540
541   REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
542 }
543
544 static void *vl_api_nat44_interface_add_del_feature_t_print
545   (vl_api_nat44_interface_add_del_feature_t * mp, void *handle)
546 {
547   u8 *s;
548
549   s = format (0, "SCRIPT: nat44_interface_add_del_feature ");
550   s = format (s, "sw_if_index %d %s %s",
551               clib_host_to_net_u32 (mp->sw_if_index),
552               mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
553
554   FINISH;
555 }
556
557 static void
558 send_nat44_interface_details (snat_interface_t * i,
559                               unix_shared_memory_queue_t * q, u32 context)
560 {
561   vl_api_nat44_interface_details_t *rmp;
562   snat_main_t *sm = &snat_main;
563
564   rmp = vl_msg_api_alloc (sizeof (*rmp));
565   memset (rmp, 0, sizeof (*rmp));
566   rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
567   rmp->sw_if_index = ntohl (i->sw_if_index);
568   rmp->is_inside = (nat_interface_is_inside (i)
569                     && nat_interface_is_outside (i)) ? 2 :
570     nat_interface_is_inside (i);
571   rmp->context = context;
572
573   vl_msg_api_send_shmem (q, (u8 *) & rmp);
574 }
575
576 static void
577 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
578 {
579   unix_shared_memory_queue_t *q;
580   snat_main_t *sm = &snat_main;
581   snat_interface_t *i;
582
583   q = vl_api_client_index_to_input_queue (mp->client_index);
584   if (q == 0)
585     return;
586
587   /* *INDENT-OFF* */
588   pool_foreach (i, sm->interfaces,
589   ({
590     send_nat44_interface_details(i, q, mp->context);
591   }));
592   /* *INDENT-ON* */
593 }
594
595 static void *
596 vl_api_nat44_interface_dump_t_print (vl_api_nat44_interface_dump_t * mp,
597                                      void *handle)
598 {
599   u8 *s;
600
601   s = format (0, "SCRIPT: nat44_interface_dump ");
602
603   FINISH;
604 }
605
606 static void
607   vl_api_nat44_interface_add_del_output_feature_t_handler
608   (vl_api_nat44_interface_add_del_output_feature_t * mp)
609 {
610   snat_main_t *sm = &snat_main;
611   vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
612   u8 is_del = mp->is_add == 0;
613   u32 sw_if_index = ntohl (mp->sw_if_index);
614   int rv = 0;
615
616   VALIDATE_SW_IF_INDEX (mp);
617
618   rv = snat_interface_add_del_output_feature (sw_if_index, mp->is_inside,
619                                               is_del);
620
621   BAD_SW_IF_INDEX_LABEL;
622
623   REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
624 }
625
626 static void *vl_api_nat44_interface_add_del_output_feature_t_print
627   (vl_api_nat44_interface_add_del_output_feature_t * mp, void *handle)
628 {
629   u8 *s;
630
631   s = format (0, "SCRIPT: nat44_interface_add_del_output_feature ");
632   s = format (s, "sw_if_index %d %s %s",
633               clib_host_to_net_u32 (mp->sw_if_index),
634               mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
635
636   FINISH;
637 }
638
639 static void
640 send_nat44_interface_output_feature_details (snat_interface_t * i,
641                                              unix_shared_memory_queue_t * q,
642                                              u32 context)
643 {
644   vl_api_nat44_interface_output_feature_details_t *rmp;
645   snat_main_t *sm = &snat_main;
646
647   rmp = vl_msg_api_alloc (sizeof (*rmp));
648   memset (rmp, 0, sizeof (*rmp));
649   rmp->_vl_msg_id =
650     ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
651   rmp->sw_if_index = ntohl (i->sw_if_index);
652   rmp->context = context;
653   rmp->is_inside = nat_interface_is_inside (i);
654
655   vl_msg_api_send_shmem (q, (u8 *) & rmp);
656 }
657
658 static void
659   vl_api_nat44_interface_output_feature_dump_t_handler
660   (vl_api_nat44_interface_output_feature_dump_t * mp)
661 {
662   unix_shared_memory_queue_t *q;
663   snat_main_t *sm = &snat_main;
664   snat_interface_t *i;
665
666   q = vl_api_client_index_to_input_queue (mp->client_index);
667   if (q == 0)
668     return;
669
670   /* *INDENT-OFF* */
671   pool_foreach (i, sm->output_feature_interfaces,
672   ({
673     send_nat44_interface_output_feature_details(i, q, mp->context);
674   }));
675   /* *INDENT-ON* */
676 }
677
678 static void *vl_api_nat44_interface_output_feature_dump_t_print
679   (vl_api_nat44_interface_output_feature_dump_t * mp, void *handle)
680 {
681   u8 *s;
682
683   s = format (0, "SCRIPT: nat44_interface_output_feature_dump ");
684
685   FINISH;
686 }
687
688 static void
689   vl_api_nat44_add_del_static_mapping_t_handler
690   (vl_api_nat44_add_del_static_mapping_t * mp)
691 {
692   snat_main_t *sm = &snat_main;
693   vl_api_nat44_add_del_static_mapping_reply_t *rmp;
694   ip4_address_t local_addr, external_addr;
695   u16 local_port = 0, external_port = 0;
696   u32 vrf_id, external_sw_if_index;
697   int rv = 0;
698   snat_protocol_t proto;
699
700   memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
701   memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
702   if (mp->addr_only == 0)
703     {
704       local_port = clib_net_to_host_u16 (mp->local_port);
705       external_port = clib_net_to_host_u16 (mp->external_port);
706     }
707   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
708   external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
709   proto = ip_proto_to_snat_proto (mp->protocol);
710
711   rv = snat_add_static_mapping (local_addr, external_addr, local_port,
712                                 external_port, vrf_id, mp->addr_only,
713                                 external_sw_if_index, proto, mp->is_add,
714                                 mp->twice_nat);
715
716   REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
717 }
718
719 static void *vl_api_nat44_add_del_static_mapping_t_print
720   (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
721 {
722   u8 *s;
723
724   s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
725   s = format (s, "protocol %d local_addr %U external_addr %U ",
726               mp->protocol,
727               format_ip4_address, mp->local_ip_address,
728               format_ip4_address, mp->external_ip_address);
729
730   if (mp->addr_only == 0)
731     s = format (s, "local_port %d external_port %d ",
732                 clib_net_to_host_u16 (mp->local_port),
733                 clib_net_to_host_u16 (mp->external_port));
734
735   s = format (s, "twice_nat %d ", mp->twice_nat);
736
737   if (mp->vrf_id != ~0)
738     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
739
740   if (mp->external_sw_if_index != ~0)
741     s = format (s, "external_sw_if_index %d",
742                 clib_net_to_host_u32 (mp->external_sw_if_index));
743   FINISH;
744 }
745
746 static void
747 send_nat44_static_mapping_details (snat_static_mapping_t * m,
748                                    unix_shared_memory_queue_t * q,
749                                    u32 context)
750 {
751   vl_api_nat44_static_mapping_details_t *rmp;
752   snat_main_t *sm = &snat_main;
753
754   rmp = vl_msg_api_alloc (sizeof (*rmp));
755   memset (rmp, 0, sizeof (*rmp));
756   rmp->_vl_msg_id =
757     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
758   rmp->addr_only = m->addr_only;
759   clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
760   clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
761   rmp->local_port = htons (m->local_port);
762   rmp->external_port = htons (m->external_port);
763   rmp->external_sw_if_index = ~0;
764   rmp->vrf_id = htonl (m->vrf_id);
765   rmp->protocol = snat_proto_to_ip_proto (m->proto);
766   rmp->context = context;
767   rmp->twice_nat = m->twice_nat;
768
769   vl_msg_api_send_shmem (q, (u8 *) & rmp);
770 }
771
772 static void
773 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
774                                        unix_shared_memory_queue_t * q,
775                                        u32 context)
776 {
777   vl_api_nat44_static_mapping_details_t *rmp;
778   snat_main_t *sm = &snat_main;
779
780   rmp = vl_msg_api_alloc (sizeof (*rmp));
781   memset (rmp, 0, sizeof (*rmp));
782   rmp->_vl_msg_id =
783     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
784   rmp->addr_only = m->addr_only;
785   clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
786   rmp->local_port = htons (m->l_port);
787   rmp->external_port = htons (m->e_port);
788   rmp->external_sw_if_index = htonl (m->sw_if_index);
789   rmp->vrf_id = htonl (m->vrf_id);
790   rmp->protocol = snat_proto_to_ip_proto (m->proto);
791   rmp->context = context;
792   rmp->twice_nat = m->twice_nat;
793
794   vl_msg_api_send_shmem (q, (u8 *) & rmp);
795 }
796
797 static void
798 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
799                                             * mp)
800 {
801   unix_shared_memory_queue_t *q;
802   snat_main_t *sm = &snat_main;
803   snat_static_mapping_t *m;
804   snat_static_map_resolve_t *rp;
805   int j;
806
807   q = vl_api_client_index_to_input_queue (mp->client_index);
808   if (q == 0)
809     return;
810
811   /* *INDENT-OFF* */
812   pool_foreach (m, sm->static_mappings,
813   ({
814       if (!vec_len(m->locals) && (m->local_addr.as_u32 != m->external_addr.as_u32))
815         send_nat44_static_mapping_details (m, q, mp->context);
816   }));
817   /* *INDENT-ON* */
818
819   for (j = 0; j < vec_len (sm->to_resolve); j++)
820     {
821       rp = sm->to_resolve + j;
822       if (rp->l_addr.as_u32 != 0)
823         send_nat44_static_map_resolve_details (rp, q, mp->context);
824     }
825 }
826
827 static void *
828 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
829                                           mp, void *handle)
830 {
831   u8 *s;
832
833   s = format (0, "SCRIPT: nat44_static_mapping_dump ");
834
835   FINISH;
836 }
837
838 static void
839   vl_api_nat44_add_del_identity_mapping_t_handler
840   (vl_api_nat44_add_del_identity_mapping_t * mp)
841 {
842   snat_main_t *sm = &snat_main;
843   vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
844   ip4_address_t addr;
845   u16 port = 0;
846   u32 vrf_id, sw_if_index;
847   int rv = 0;
848   snat_protocol_t proto = ~0;
849
850   if (mp->addr_only == 0)
851     {
852       port = clib_net_to_host_u16 (mp->port);
853       proto = ip_proto_to_snat_proto (mp->protocol);
854     }
855   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
856   sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
857   if (sw_if_index != ~0)
858     addr.as_u32 = 0;
859   else
860     memcpy (&addr.as_u8, mp->ip_address, 4);
861
862
863   rv =
864     snat_add_static_mapping (addr, addr, port, port, vrf_id, mp->addr_only,
865                              sw_if_index, proto, mp->is_add, 0);
866
867   REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
868 }
869
870 static void *vl_api_nat44_add_del_identity_mapping_t_print
871   (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
872 {
873   u8 *s;
874
875   s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
876   if (mp->sw_if_index != ~0)
877     s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
878   else
879     s = format (s, "addr %U", format_ip4_address, mp->ip_address);
880
881   if (mp->addr_only == 0)
882     s =
883       format (s, "protocol %d port %d", mp->protocol,
884               clib_net_to_host_u16 (mp->port));
885
886   if (mp->vrf_id != ~0)
887     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
888
889   FINISH;
890 }
891
892 static void
893 send_nat44_identity_mapping_details (snat_static_mapping_t * m,
894                                      unix_shared_memory_queue_t * q,
895                                      u32 context)
896 {
897   vl_api_nat44_identity_mapping_details_t *rmp;
898   snat_main_t *sm = &snat_main;
899
900   rmp = vl_msg_api_alloc (sizeof (*rmp));
901   memset (rmp, 0, sizeof (*rmp));
902   rmp->_vl_msg_id =
903     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
904   rmp->addr_only = m->addr_only;
905   clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
906   rmp->port = htons (m->local_port);
907   rmp->sw_if_index = ~0;
908   rmp->vrf_id = htonl (m->vrf_id);
909   rmp->protocol = snat_proto_to_ip_proto (m->proto);
910   rmp->context = context;
911
912   vl_msg_api_send_shmem (q, (u8 *) & rmp);
913 }
914
915 static void
916 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
917                                          unix_shared_memory_queue_t * q,
918                                          u32 context)
919 {
920   vl_api_nat44_identity_mapping_details_t *rmp;
921   snat_main_t *sm = &snat_main;
922
923   rmp = vl_msg_api_alloc (sizeof (*rmp));
924   memset (rmp, 0, sizeof (*rmp));
925   rmp->_vl_msg_id =
926     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
927   rmp->addr_only = m->addr_only;
928   rmp->port = htons (m->l_port);
929   rmp->sw_if_index = htonl (m->sw_if_index);
930   rmp->vrf_id = htonl (m->vrf_id);
931   rmp->protocol = snat_proto_to_ip_proto (m->proto);
932   rmp->context = context;
933
934   vl_msg_api_send_shmem (q, (u8 *) & rmp);
935 }
936
937 static void
938   vl_api_nat44_identity_mapping_dump_t_handler
939   (vl_api_nat44_identity_mapping_dump_t * mp)
940 {
941   unix_shared_memory_queue_t *q;
942   snat_main_t *sm = &snat_main;
943   snat_static_mapping_t *m;
944   snat_static_map_resolve_t *rp;
945   int j;
946
947   q = vl_api_client_index_to_input_queue (mp->client_index);
948   if (q == 0)
949     return;
950
951   /* *INDENT-OFF* */
952   pool_foreach (m, sm->static_mappings,
953   ({
954       if (!vec_len(m->locals) && (m->local_addr.as_u32 == m->external_addr.as_u32))
955         send_nat44_identity_mapping_details (m, q, mp->context);
956   }));
957   /* *INDENT-ON* */
958
959   for (j = 0; j < vec_len (sm->to_resolve); j++)
960     {
961       rp = sm->to_resolve + j;
962       if (rp->l_addr.as_u32 == 0)
963         send_nat44_identity_map_resolve_details (rp, q, mp->context);
964     }
965 }
966
967 static void *vl_api_nat44_identity_mapping_dump_t_print
968   (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
969 {
970   u8 *s;
971
972   s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
973
974   FINISH;
975 }
976
977 static void
978   vl_api_nat44_add_del_interface_addr_t_handler
979   (vl_api_nat44_add_del_interface_addr_t * mp)
980 {
981   snat_main_t *sm = &snat_main;
982   vl_api_nat44_add_del_interface_addr_reply_t *rmp;
983   u8 is_del = mp->is_add == 0;
984   u32 sw_if_index = ntohl (mp->sw_if_index);
985   int rv = 0;
986
987   VALIDATE_SW_IF_INDEX (mp);
988
989   rv = snat_add_interface_address (sm, sw_if_index, is_del, mp->twice_nat);
990
991   BAD_SW_IF_INDEX_LABEL;
992
993   REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
994 }
995
996 static void *vl_api_nat44_add_del_interface_addr_t_print
997   (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
998 {
999   u8 *s;
1000
1001   s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1002   s = format (s, "sw_if_index %d twice_nat %d %s",
1003               clib_host_to_net_u32 (mp->sw_if_index),
1004               mp->twice_nat, mp->is_add ? "" : "del");
1005
1006   FINISH;
1007 }
1008
1009 static void
1010 send_nat44_interface_addr_details (u32 sw_if_index,
1011                                    unix_shared_memory_queue_t * q,
1012                                    u32 context, u8 twice_nat)
1013 {
1014   vl_api_nat44_interface_addr_details_t *rmp;
1015   snat_main_t *sm = &snat_main;
1016
1017   rmp = vl_msg_api_alloc (sizeof (*rmp));
1018   memset (rmp, 0, sizeof (*rmp));
1019   rmp->_vl_msg_id =
1020     ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1021   rmp->sw_if_index = ntohl (sw_if_index);
1022   rmp->twice_nat = twice_nat;
1023   rmp->context = context;
1024
1025   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1026 }
1027
1028 static void
1029 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1030                                             * mp)
1031 {
1032   unix_shared_memory_queue_t *q;
1033   snat_main_t *sm = &snat_main;
1034   u32 *i;
1035
1036   q = vl_api_client_index_to_input_queue (mp->client_index);
1037   if (q == 0)
1038     return;
1039
1040   /* *INDENT-OFF* */
1041   vec_foreach (i, sm->auto_add_sw_if_indices)
1042     send_nat44_interface_addr_details(*i, q, mp->context, 0);
1043   vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1044     send_nat44_interface_addr_details(*i, q, mp->context, 1);
1045   /* *INDENT-ON* */
1046 }
1047
1048 static void *
1049 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1050                                           mp, void *handle)
1051 {
1052   u8 *s;
1053
1054   s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1055
1056   FINISH;
1057 }
1058
1059 static void
1060 send_nat44_user_details (snat_user_t * u, unix_shared_memory_queue_t * q,
1061                          u32 context)
1062 {
1063   vl_api_nat44_user_details_t *rmp;
1064   snat_main_t *sm = &snat_main;
1065   fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1066
1067   rmp = vl_msg_api_alloc (sizeof (*rmp));
1068   memset (rmp, 0, sizeof (*rmp));
1069   rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1070
1071   rmp->vrf_id = ntohl (fib->ft_table_id);
1072
1073   clib_memcpy (rmp->ip_address, &(u->addr), 4);
1074   rmp->nsessions = ntohl (u->nsessions);
1075   rmp->nstaticsessions = ntohl (u->nstaticsessions);
1076   rmp->context = context;
1077
1078   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1079 }
1080
1081 static void
1082 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1083 {
1084   unix_shared_memory_queue_t *q;
1085   snat_main_t *sm = &snat_main;
1086   snat_main_per_thread_data_t *tsm;
1087   snat_user_t *u;
1088
1089   q = vl_api_client_index_to_input_queue (mp->client_index);
1090   if (q == 0)
1091     return;
1092
1093   /* *INDENT-OFF* */
1094   vec_foreach (tsm, sm->per_thread_data)
1095     vec_foreach (u, tsm->users)
1096       send_nat44_user_details (u, q, mp->context);
1097   /* *INDENT-ON* */
1098 }
1099
1100 static void *
1101 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1102 {
1103   u8 *s;
1104
1105   s = format (0, "SCRIPT: nat44_user_dump ");
1106
1107   FINISH;
1108 }
1109
1110 static void
1111 send_nat44_user_session_details (snat_session_t * s,
1112                                  unix_shared_memory_queue_t * q, u32 context)
1113 {
1114   vl_api_nat44_user_session_details_t *rmp;
1115   snat_main_t *sm = &snat_main;
1116
1117   rmp = vl_msg_api_alloc (sizeof (*rmp));
1118   memset (rmp, 0, sizeof (*rmp));
1119   rmp->_vl_msg_id =
1120     ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1121   clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1122   clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1123   rmp->is_static = s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING ? 1 : 0;
1124   rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1125   rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1126   rmp->total_pkts = ntohl (s->total_pkts);
1127   rmp->context = context;
1128   if (snat_is_unk_proto_session (s))
1129     {
1130       rmp->outside_port = 0;
1131       rmp->inside_port = 0;
1132       rmp->protocol = ntohs (s->in2out.port);
1133     }
1134   else
1135     {
1136       rmp->outside_port = s->out2in.port;
1137       rmp->inside_port = s->in2out.port;
1138       rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
1139     }
1140
1141   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1142 }
1143
1144 static void
1145 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1146                                           mp)
1147 {
1148   unix_shared_memory_queue_t *q;
1149   snat_main_t *sm = &snat_main;
1150   snat_main_per_thread_data_t *tsm;
1151   snat_session_t *s;
1152   clib_bihash_kv_8_8_t key, value;
1153   snat_user_key_t ukey;
1154   snat_user_t *u;
1155   u32 session_index, head_index, elt_index;
1156   dlist_elt_t *head, *elt;
1157   ip4_header_t ip;
1158
1159   q = vl_api_client_index_to_input_queue (mp->client_index);
1160   if (q == 0)
1161     return;
1162
1163   clib_memcpy (&ukey.addr, mp->ip_address, 4);
1164   ip.src_address.as_u32 = ukey.addr.as_u32;
1165   ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1166   key.key = ukey.as_u64;
1167   if (sm->num_workers)
1168     tsm =
1169       vec_elt_at_index (sm->per_thread_data,
1170                         sm->worker_in2out_cb (&ip, ukey.fib_index));
1171   else
1172     tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1173   if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1174     return;
1175   u = pool_elt_at_index (tsm->users, value.value);
1176   if (!u->nsessions && !u->nstaticsessions)
1177     return;
1178
1179   head_index = u->sessions_per_user_list_head_index;
1180   head = pool_elt_at_index (tsm->list_pool, head_index);
1181   elt_index = head->next;
1182   elt = pool_elt_at_index (tsm->list_pool, elt_index);
1183   session_index = elt->value;
1184   while (session_index != ~0)
1185     {
1186       s = pool_elt_at_index (tsm->sessions, session_index);
1187
1188       send_nat44_user_session_details (s, q, mp->context);
1189
1190       elt_index = elt->next;
1191       elt = pool_elt_at_index (tsm->list_pool, elt_index);
1192       session_index = elt->value;
1193     }
1194 }
1195
1196 static void *
1197 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1198                                         void *handle)
1199 {
1200   u8 *s;
1201
1202   s = format (0, "SCRIPT: nat44_user_session_dump ");
1203   s = format (s, "ip_address %U vrf_id %d\n",
1204               format_ip4_address, mp->ip_address,
1205               clib_net_to_host_u32 (mp->vrf_id));
1206
1207   FINISH;
1208 }
1209
1210 static nat44_lb_addr_port_t *
1211 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1212                              u8 addr_port_pair_num)
1213 {
1214   u8 i;
1215   nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1216   vl_api_nat44_lb_addr_port_t *ap;
1217
1218   for (i = 0; i < addr_port_pair_num; i++)
1219     {
1220       ap = &addr_port_pairs[i];
1221       memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1222       clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1223       lb_addr_port.port = clib_net_to_host_u16 (ap->port);
1224       lb_addr_port.probability = ap->probability;
1225       vec_add1 (lb_addr_port_pairs, lb_addr_port);
1226     }
1227
1228   return lb_addr_port_pairs;
1229 }
1230
1231 static void
1232   vl_api_nat44_add_del_lb_static_mapping_t_handler
1233   (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1234 {
1235   snat_main_t *sm = &snat_main;
1236   vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1237   int rv = 0;
1238   nat44_lb_addr_port_t *locals = 0;
1239   ip4_address_t e_addr;
1240   snat_protocol_t proto;
1241
1242   locals = unformat_nat44_lb_addr_port (mp->locals, mp->local_num);
1243   clib_memcpy (&e_addr, mp->external_addr, 4);
1244   proto = ip_proto_to_snat_proto (mp->protocol);
1245
1246   rv =
1247     nat44_add_del_lb_static_mapping (e_addr,
1248                                      clib_net_to_host_u16 (mp->external_port),
1249                                      proto, clib_net_to_host_u32 (mp->vrf_id),
1250                                      locals, mp->is_add, mp->twice_nat);
1251
1252   vec_free (locals);
1253
1254   REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1255 }
1256
1257 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1258   (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1259 {
1260   u8 *s;
1261
1262   s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1263   s = format (s, "is_add %d twice_nat %d", mp->is_add, mp->twice_nat);
1264
1265   FINISH;
1266 }
1267
1268 static void
1269 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1270                                       unix_shared_memory_queue_t * q,
1271                                       u32 context)
1272 {
1273   vl_api_nat44_lb_static_mapping_details_t *rmp;
1274   snat_main_t *sm = &snat_main;
1275   nat44_lb_addr_port_t *ap;
1276   vl_api_nat44_lb_addr_port_t *locals;
1277
1278   rmp =
1279     vl_msg_api_alloc (sizeof (*rmp) +
1280                       (vec_len (m->locals) * sizeof (nat44_lb_addr_port_t)));
1281   memset (rmp, 0, sizeof (*rmp));
1282   rmp->_vl_msg_id =
1283     ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1284
1285   clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1286   rmp->external_port = ntohs (m->external_port);
1287   rmp->protocol = snat_proto_to_ip_proto (m->proto);
1288   rmp->vrf_id = ntohl (m->vrf_id);
1289   rmp->context = context;
1290   rmp->twice_nat = m->twice_nat;
1291
1292   locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
1293   vec_foreach (ap, m->locals)
1294   {
1295     clib_memcpy (locals->addr, &(ap->addr), 4);
1296     locals->port = htons (ap->port);
1297     locals->probability = ap->probability;
1298     locals++;
1299     rmp->local_num++;
1300   }
1301
1302   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1303 }
1304
1305 static void
1306   vl_api_nat44_lb_static_mapping_dump_t_handler
1307   (vl_api_nat44_lb_static_mapping_dump_t * mp)
1308 {
1309   unix_shared_memory_queue_t *q;
1310   snat_main_t *sm = &snat_main;
1311   snat_static_mapping_t *m;
1312
1313   q = vl_api_client_index_to_input_queue (mp->client_index);
1314   if (q == 0)
1315     return;
1316
1317   /* *INDENT-OFF* */
1318   pool_foreach (m, sm->static_mappings,
1319   ({
1320       if (vec_len(m->locals))
1321         send_nat44_lb_static_mapping_details (m, q, mp->context);
1322   }));
1323   /* *INDENT-ON* */
1324 }
1325
1326 static void *vl_api_nat44_lb_static_mapping_dump_t_print
1327   (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
1328 {
1329   u8 *s;
1330
1331   s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
1332
1333   FINISH;
1334 }
1335
1336 static void
1337 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
1338 {
1339   snat_main_t *sm = &snat_main;
1340   vl_api_nat44_del_session_reply_t *rmp;
1341   ip4_address_t addr;
1342   u16 port;
1343   u32 vrf_id;
1344   int rv = 0;
1345   snat_protocol_t proto;
1346
1347   memcpy (&addr.as_u8, mp->address, 4);
1348   port = clib_net_to_host_u16 (mp->port);
1349   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1350   proto = ip_proto_to_snat_proto (mp->protocol);
1351
1352   rv = nat44_del_session (sm, &addr, port, proto, vrf_id, mp->is_in);
1353
1354   REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
1355 }
1356
1357 static void *
1358 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
1359                                   void *handle)
1360 {
1361   u8 *s;
1362
1363   s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1364   s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
1365               format_ip4_address, mp->address,
1366               clib_net_to_host_u16 (mp->port),
1367               mp->protocol, clib_net_to_host_u32 (mp->vrf_id), mp->is_in);
1368
1369   FINISH;
1370 }
1371
1372 static void
1373   vl_api_nat44_forwarding_enable_disable_t_handler
1374   (vl_api_nat44_forwarding_enable_disable_t * mp)
1375 {
1376   snat_main_t *sm = &snat_main;
1377   vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
1378   int rv = 0;
1379
1380   sm->forwarding_enabled = mp->enable != 0;
1381
1382   REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
1383 }
1384
1385 static void *vl_api_nat44_forwarding_enable_disable_t_print
1386   (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
1387 {
1388   u8 *s;
1389
1390   s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
1391   s = format (s, "enable %d", mp->enable != 0);
1392
1393   FINISH;
1394 }
1395
1396 static void
1397   vl_api_nat44_forwarding_is_enabled_t_handler
1398   (vl_api_nat44_forwarding_is_enabled_t * mp)
1399 {
1400   unix_shared_memory_queue_t *q;
1401   snat_main_t *sm = &snat_main;
1402   vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
1403
1404   q = vl_api_client_index_to_input_queue (mp->client_index);
1405   if (q == 0)
1406     return;
1407
1408   rmp = vl_msg_api_alloc (sizeof (*rmp));
1409   memset (rmp, 0, sizeof (*rmp));
1410   rmp->_vl_msg_id =
1411     ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
1412   rmp->context = mp->context;
1413
1414   rmp->enabled = sm->forwarding_enabled;
1415
1416   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1417 }
1418
1419 static void *vl_api_nat44_forwarding_is_enabled_t_print
1420   (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
1421 {
1422   u8 *s;
1423
1424   s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
1425
1426   FINISH;
1427 }
1428
1429 /*******************************/
1430 /*** Deterministic NAT (CGN) ***/
1431 /*******************************/
1432
1433 static void
1434 vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
1435 {
1436   snat_main_t *sm = &snat_main;
1437   vl_api_nat_det_add_del_map_reply_t *rmp;
1438   int rv = 0;
1439   ip4_address_t in_addr, out_addr;
1440
1441   if (!mp->is_nat44)
1442     {
1443       rv = VNET_API_ERROR_UNIMPLEMENTED;
1444       goto send_reply;
1445     }
1446
1447   clib_memcpy (&in_addr, mp->in_addr, 4);
1448   clib_memcpy (&out_addr, mp->out_addr, 4);
1449   rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
1450                          mp->out_plen, mp->is_add);
1451
1452 send_reply:
1453   REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
1454 }
1455
1456 static void *
1457 vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
1458                                     void *handle)
1459 {
1460   u8 *s;
1461
1462   s = format (0, "SCRIPT: nat_det_add_del_map ");
1463   s = format (s, "inside address %U/%d outside address %U/%d\n",
1464               format_ip4_address, mp->in_addr, mp->in_plen,
1465               format_ip4_address, mp->out_addr, mp->out_plen);
1466
1467   FINISH;
1468 }
1469
1470 static void
1471 vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
1472 {
1473   snat_main_t *sm = &snat_main;
1474   vl_api_nat_det_forward_reply_t *rmp;
1475   int rv = 0;
1476   u16 lo_port = 0, hi_port = 0;
1477   snat_det_map_t *dm;
1478   ip4_address_t in_addr, out_addr;
1479
1480   if (!mp->is_nat44)
1481     {
1482       out_addr.as_u32 = 0;
1483       rv = VNET_API_ERROR_UNIMPLEMENTED;
1484       goto send_reply;
1485     }
1486
1487   out_addr.as_u32 = 0;
1488   clib_memcpy (&in_addr, mp->in_addr, 4);
1489   dm = snat_det_map_by_user (sm, &in_addr);
1490   if (!dm)
1491     {
1492       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1493       goto send_reply;
1494     }
1495
1496   snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
1497   hi_port = lo_port + dm->ports_per_host - 1;
1498
1499 send_reply:
1500   /* *INDENT-OFF* */
1501   REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
1502   ({
1503     rmp->out_port_lo = ntohs (lo_port);
1504     rmp->out_port_hi = ntohs (hi_port);
1505     clib_memcpy (rmp->out_addr, &out_addr, 4);
1506   }))
1507   /* *INDENT-ON* */
1508 }
1509
1510 static void *
1511 vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
1512 {
1513   u8 *s;
1514
1515   s = format (0, "SCRIPT: nat_det_forward");
1516   s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
1517
1518   FINISH;
1519 }
1520
1521 static void
1522 vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
1523 {
1524   snat_main_t *sm = &snat_main;
1525   vl_api_nat_det_reverse_reply_t *rmp;
1526   int rv = 0;
1527   ip4_address_t out_addr, in_addr;
1528   snat_det_map_t *dm;
1529
1530   in_addr.as_u32 = 0;
1531   clib_memcpy (&out_addr, mp->out_addr, 4);
1532   dm = snat_det_map_by_out (sm, &out_addr);
1533   if (!dm)
1534     {
1535       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1536       goto send_reply;
1537     }
1538
1539   snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
1540
1541 send_reply:
1542   /* *INDENT-OFF* */
1543   REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
1544   ({
1545     rmp->is_nat44 = 1;
1546     memset (rmp->in_addr, 0, 16);
1547     clib_memcpy (rmp->in_addr, &in_addr, 4);
1548   }))
1549   /* *INDENT-ON* */
1550 }
1551
1552 static void *
1553 vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
1554 {
1555   u8 *s;
1556
1557   s = format (0, "SCRIPT: nat_det_reverse");
1558   s = format (s, "outside ip address %U outside port %d",
1559               format_ip4_address, mp->out_addr, ntohs (mp->out_port));
1560
1561   FINISH;
1562 }
1563
1564 static void
1565 sent_nat_det_map_details (snat_det_map_t * m, unix_shared_memory_queue_t * q,
1566                           u32 context)
1567 {
1568   vl_api_nat_det_map_details_t *rmp;
1569   snat_main_t *sm = &snat_main;
1570
1571   rmp = vl_msg_api_alloc (sizeof (*rmp));
1572   memset (rmp, 0, sizeof (*rmp));
1573   rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
1574   rmp->is_nat44 = 1;
1575   clib_memcpy (rmp->in_addr, &m->in_addr, 4);
1576   rmp->in_plen = m->in_plen;
1577   clib_memcpy (rmp->out_addr, &m->out_addr, 4);
1578   rmp->out_plen = m->out_plen;
1579   rmp->sharing_ratio = htonl (m->sharing_ratio);
1580   rmp->ports_per_host = htons (m->ports_per_host);
1581   rmp->ses_num = htonl (m->ses_num);
1582   rmp->context = context;
1583
1584   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1585 }
1586
1587 static void
1588 vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
1589 {
1590   unix_shared_memory_queue_t *q;
1591   snat_main_t *sm = &snat_main;
1592   snat_det_map_t *m;
1593
1594   q = vl_api_client_index_to_input_queue (mp->client_index);
1595   if (q == 0)
1596     return;
1597
1598   /* *INDENT-OFF* */
1599   vec_foreach(m, sm->det_maps)
1600     sent_nat_det_map_details(m, q, mp->context);
1601   /* *INDENT-ON* */
1602 }
1603
1604 static void *
1605 vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
1606 {
1607   u8 *s;
1608
1609   s = format (0, "SCRIPT: nat_det_map_dump ");
1610
1611   FINISH;
1612 }
1613
1614 static void
1615 vl_api_nat_det_set_timeouts_t_handler (vl_api_nat_det_set_timeouts_t * mp)
1616 {
1617   snat_main_t *sm = &snat_main;
1618   vl_api_nat_det_set_timeouts_reply_t *rmp;
1619   int rv = 0;
1620
1621   sm->udp_timeout = ntohl (mp->udp);
1622   sm->tcp_established_timeout = ntohl (mp->tcp_established);
1623   sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
1624   sm->icmp_timeout = ntohl (mp->icmp);
1625
1626   REPLY_MACRO (VL_API_NAT_DET_SET_TIMEOUTS_REPLY);
1627 }
1628
1629 static void *
1630 vl_api_nat_det_set_timeouts_t_print (vl_api_nat_det_set_timeouts_t * mp,
1631                                      void *handle)
1632 {
1633   u8 *s;
1634
1635   s = format (0, "SCRIPT: nat_det_set_timeouts ");
1636   s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
1637               ntohl (mp->udp),
1638               ntohl (mp->tcp_established),
1639               ntohl (mp->tcp_transitory), ntohl (mp->icmp));
1640
1641   FINISH;
1642 }
1643
1644 static void
1645 vl_api_nat_det_get_timeouts_t_handler (vl_api_nat_det_get_timeouts_t * mp)
1646 {
1647   snat_main_t *sm = &snat_main;
1648   vl_api_nat_det_get_timeouts_reply_t *rmp;
1649   int rv = 0;
1650
1651   /* *INDENT-OFF* */
1652   REPLY_MACRO2 (VL_API_NAT_DET_GET_TIMEOUTS_REPLY,
1653   ({
1654     rmp->udp = htonl (sm->udp_timeout);
1655     rmp->tcp_established = htonl (sm->tcp_established_timeout);
1656     rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
1657     rmp->icmp = htonl (sm->icmp_timeout);
1658   }))
1659   /* *INDENT-ON* */
1660 }
1661
1662 static void *
1663 vl_api_nat_det_get_timeouts_t_print (vl_api_nat_det_get_timeouts_t * mp,
1664                                      void *handle)
1665 {
1666   u8 *s;
1667
1668   s = format (0, "SCRIPT: nat_det_get_timeouts");
1669
1670   FINISH;
1671 }
1672
1673 static void
1674 vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
1675                                             * mp)
1676 {
1677   snat_main_t *sm = &snat_main;
1678   vl_api_nat_det_close_session_out_reply_t *rmp;
1679   ip4_address_t out_addr, ext_addr, in_addr;
1680   snat_det_out_key_t key;
1681   snat_det_map_t *dm;
1682   snat_det_session_t *ses;
1683   int rv = 0;
1684
1685   clib_memcpy (&out_addr, mp->out_addr, 4);
1686   clib_memcpy (&ext_addr, mp->ext_addr, 4);
1687
1688   dm = snat_det_map_by_out (sm, &out_addr);
1689   if (!dm)
1690     {
1691       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1692       goto send_reply;
1693     }
1694   snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
1695   key.ext_host_addr = ext_addr;
1696   key.ext_host_port = mp->ext_port;
1697   key.out_port = mp->out_port;
1698   ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
1699   if (!ses)
1700     {
1701       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1702       goto send_reply;
1703     }
1704   snat_det_ses_close (dm, ses);
1705
1706 send_reply:
1707   REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
1708 }
1709
1710 static void *
1711 vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
1712                                           mp, void *handle)
1713 {
1714   u8 *s;
1715
1716   s = format (0, "SCRIPT: nat_det_close_session_out ");
1717   s = format (s, "out_addr %U out_port %d "
1718               "ext_addr %U ext_port %d\n",
1719               format_ip4_address, mp->out_addr, ntohs (mp->out_port),
1720               format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
1721
1722   FINISH;
1723 }
1724
1725 static void
1726 vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
1727                                            mp)
1728 {
1729   snat_main_t *sm = &snat_main;
1730   vl_api_nat_det_close_session_in_reply_t *rmp;
1731   ip4_address_t in_addr, ext_addr;
1732   snat_det_out_key_t key;
1733   snat_det_map_t *dm;
1734   snat_det_session_t *ses;
1735   int rv = 0;
1736
1737   if (!mp->is_nat44)
1738     {
1739       rv = VNET_API_ERROR_UNIMPLEMENTED;
1740       goto send_reply;
1741     }
1742
1743   clib_memcpy (&in_addr, mp->in_addr, 4);
1744   clib_memcpy (&ext_addr, mp->ext_addr, 4);
1745
1746   dm = snat_det_map_by_user (sm, &in_addr);
1747   if (!dm)
1748     {
1749       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1750       goto send_reply;
1751     }
1752   key.ext_host_addr = ext_addr;
1753   key.ext_host_port = mp->ext_port;
1754   ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
1755   if (!ses)
1756     {
1757       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1758       goto send_reply;
1759     }
1760   snat_det_ses_close (dm, ses);
1761
1762 send_reply:
1763   REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
1764 }
1765
1766 static void *
1767 vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
1768                                          mp, void *handle)
1769 {
1770   u8 *s;
1771   s = format (0, "SCRIPT: nat_det_close_session_in ");
1772   s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
1773               format_ip4_address, mp->in_addr, ntohs (mp->in_port),
1774               format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
1775
1776   FINISH;
1777 }
1778
1779 static void
1780 send_nat_det_session_details (snat_det_session_t * s,
1781                               unix_shared_memory_queue_t * q, u32 context)
1782 {
1783   vl_api_nat_det_session_details_t *rmp;
1784   snat_main_t *sm = &snat_main;
1785
1786   rmp = vl_msg_api_alloc (sizeof (*rmp));
1787   memset (rmp, 0, sizeof (*rmp));
1788   rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
1789   rmp->in_port = s->in_port;
1790   clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
1791   rmp->ext_port = s->out.ext_host_port;
1792   rmp->out_port = s->out.out_port;
1793   rmp->state = s->state;
1794   rmp->expire = ntohl (s->expire);
1795   rmp->context = context;
1796
1797   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1798 }
1799
1800 static void
1801 vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
1802 {
1803   unix_shared_memory_queue_t *q;
1804   snat_main_t *sm = &snat_main;
1805   ip4_address_t user_addr;
1806   snat_det_map_t *dm;
1807   snat_det_session_t *s, empty_ses;
1808   u16 i;
1809
1810   q = vl_api_client_index_to_input_queue (mp->client_index);
1811   if (q == 0)
1812     return;
1813   if (!mp->is_nat44)
1814     return;
1815
1816   memset (&empty_ses, 0, sizeof (empty_ses));
1817   clib_memcpy (&user_addr, mp->user_addr, 4);
1818   dm = snat_det_map_by_user (sm, &user_addr);
1819   if (!dm)
1820     return;
1821
1822   s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
1823   for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
1824     {
1825       if (s->out.as_u64)
1826         send_nat_det_session_details (s, q, mp->context);
1827       s++;
1828     }
1829 }
1830
1831 static void *
1832 vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
1833                                      void *handle)
1834 {
1835   u8 *s;
1836
1837   s = format (0, "SCRIPT: nat_det_session_dump ");
1838   s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
1839
1840   FINISH;
1841 }
1842
1843 /*************/
1844 /*** NAT64 ***/
1845 /*************/
1846
1847 static void
1848   vl_api_nat64_add_del_pool_addr_range_t_handler
1849   (vl_api_nat64_add_del_pool_addr_range_t * mp)
1850 {
1851   vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
1852   snat_main_t *sm = &snat_main;
1853   int rv = 0;
1854   ip4_address_t this_addr;
1855   u32 start_host_order, end_host_order;
1856   u32 vrf_id;
1857   int i, count;
1858   u32 *tmp;
1859
1860   tmp = (u32 *) mp->start_addr;
1861   start_host_order = clib_host_to_net_u32 (tmp[0]);
1862   tmp = (u32 *) mp->end_addr;
1863   end_host_order = clib_host_to_net_u32 (tmp[0]);
1864
1865   count = (end_host_order - start_host_order) + 1;
1866
1867   vrf_id = clib_host_to_net_u32 (mp->vrf_id);
1868
1869   memcpy (&this_addr.as_u8, mp->start_addr, 4);
1870
1871   for (i = 0; i < count; i++)
1872     {
1873       if ((rv = nat64_add_del_pool_addr (&this_addr, vrf_id, mp->is_add)))
1874         goto send_reply;
1875
1876       increment_v4_address (&this_addr);
1877     }
1878
1879 send_reply:
1880   REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
1881 }
1882
1883 static void *vl_api_nat64_add_del_pool_addr_range_t_print
1884   (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
1885 {
1886   u8 *s;
1887
1888   s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
1889   s = format (s, "%U - %U vrf_id %u %s\n",
1890               format_ip4_address, mp->start_addr,
1891               format_ip4_address, mp->end_addr,
1892               ntohl (mp->vrf_id), mp->is_add ? "" : "del");
1893
1894   FINISH;
1895 }
1896
1897 typedef struct nat64_api_walk_ctx_t_
1898 {
1899   unix_shared_memory_queue_t *q;
1900   u32 context;
1901   nat64_db_t *db;
1902 } nat64_api_walk_ctx_t;
1903
1904 static int
1905 nat64_api_pool_walk (snat_address_t * a, void *arg)
1906 {
1907   vl_api_nat64_pool_addr_details_t *rmp;
1908   snat_main_t *sm = &snat_main;
1909   nat64_api_walk_ctx_t *ctx = arg;
1910
1911   rmp = vl_msg_api_alloc (sizeof (*rmp));
1912   memset (rmp, 0, sizeof (*rmp));
1913   rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
1914   clib_memcpy (rmp->address, &(a->addr), 4);
1915   if (a->fib_index != ~0)
1916     {
1917       fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
1918       if (!fib)
1919         return -1;
1920       rmp->vrf_id = ntohl (fib->ft_table_id);
1921     }
1922   else
1923     rmp->vrf_id = ~0;
1924   rmp->context = ctx->context;
1925
1926   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
1927
1928   return 0;
1929 }
1930
1931 static void
1932 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
1933 {
1934   unix_shared_memory_queue_t *q;
1935
1936   q = vl_api_client_index_to_input_queue (mp->client_index);
1937   if (q == 0)
1938     return;
1939
1940   nat64_api_walk_ctx_t ctx = {
1941     .q = q,
1942     .context = mp->context,
1943   };
1944
1945   nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
1946 }
1947
1948 static void *
1949 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
1950                                      void *handle)
1951 {
1952   u8 *s;
1953
1954   s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
1955
1956   FINISH;
1957 }
1958
1959 static void
1960 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
1961                                           mp)
1962 {
1963   snat_main_t *sm = &snat_main;
1964   vl_api_nat64_add_del_interface_reply_t *rmp;
1965   int rv = 0;
1966
1967   VALIDATE_SW_IF_INDEX (mp);
1968
1969   rv =
1970     nat64_add_del_interface (ntohl (mp->sw_if_index), mp->is_inside,
1971                              mp->is_add);
1972
1973   BAD_SW_IF_INDEX_LABEL;
1974
1975   REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
1976 }
1977
1978 static void *
1979 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
1980                                         void *handle)
1981 {
1982   u8 *s;
1983
1984   s = format (0, "SCRIPT: nat64_add_del_interface ");
1985   s = format (s, "sw_if_index %d %s %s",
1986               clib_host_to_net_u32 (mp->sw_if_index),
1987               mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
1988
1989   FINISH;
1990 }
1991
1992 static int
1993 nat64_api_interface_walk (snat_interface_t * i, void *arg)
1994 {
1995   vl_api_nat64_interface_details_t *rmp;
1996   snat_main_t *sm = &snat_main;
1997   nat64_api_walk_ctx_t *ctx = arg;
1998
1999   rmp = vl_msg_api_alloc (sizeof (*rmp));
2000   memset (rmp, 0, sizeof (*rmp));
2001   rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2002   rmp->sw_if_index = ntohl (i->sw_if_index);
2003   rmp->is_inside = (nat_interface_is_inside (i)
2004                     && nat_interface_is_outside (i)) ? 2 :
2005     nat_interface_is_inside (i);
2006   rmp->context = ctx->context;
2007
2008   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
2009
2010   return 0;
2011 }
2012
2013 static void
2014 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2015 {
2016   unix_shared_memory_queue_t *q;
2017
2018   q = vl_api_client_index_to_input_queue (mp->client_index);
2019   if (q == 0)
2020     return;
2021
2022   nat64_api_walk_ctx_t ctx = {
2023     .q = q,
2024     .context = mp->context,
2025   };
2026
2027   nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2028 }
2029
2030 static void *
2031 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2032                                      void *handle)
2033 {
2034   u8 *s;
2035
2036   s = format (0, "SCRIPT: snat_interface_dump ");
2037
2038   FINISH;
2039 }
2040
2041 static void
2042   vl_api_nat64_add_del_static_bib_t_handler
2043   (vl_api_nat64_add_del_static_bib_t * mp)
2044 {
2045   snat_main_t *sm = &snat_main;
2046   vl_api_nat64_add_del_static_bib_reply_t *rmp;
2047   ip6_address_t in_addr;
2048   ip4_address_t out_addr;
2049   int rv = 0;
2050
2051   memcpy (&in_addr.as_u8, mp->i_addr, 16);
2052   memcpy (&out_addr.as_u8, mp->o_addr, 4);
2053
2054   rv =
2055     nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2056                                     clib_net_to_host_u16 (mp->i_port),
2057                                     clib_net_to_host_u16 (mp->o_port),
2058                                     mp->proto,
2059                                     clib_net_to_host_u32 (mp->vrf_id),
2060                                     mp->is_add);
2061
2062   REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2063 }
2064
2065 static void *vl_api_nat64_add_del_static_bib_t_print
2066   (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2067 {
2068   u8 *s;
2069
2070   s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2071   s = format (s, "protocol %d i_addr %U o_addr %U ",
2072               mp->proto,
2073               format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2074
2075   if (mp->vrf_id != ~0)
2076     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2077
2078   FINISH;
2079 }
2080
2081 static int
2082 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2083 {
2084   vl_api_nat64_bib_details_t *rmp;
2085   snat_main_t *sm = &snat_main;
2086   nat64_api_walk_ctx_t *ctx = arg;
2087   fib_table_t *fib;
2088
2089   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2090   if (!fib)
2091     return -1;
2092
2093   rmp = vl_msg_api_alloc (sizeof (*rmp));
2094   memset (rmp, 0, sizeof (*rmp));
2095   rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2096   rmp->context = ctx->context;
2097   clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2098   clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2099   rmp->i_port = bibe->in_port;
2100   rmp->o_port = bibe->out_port;
2101   rmp->vrf_id = ntohl (fib->ft_table_id);
2102   rmp->proto = bibe->proto;
2103   rmp->is_static = bibe->is_static;
2104   rmp->ses_num = ntohl (bibe->ses_num);
2105
2106   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
2107
2108   return 0;
2109 }
2110
2111 static void
2112 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2113 {
2114   unix_shared_memory_queue_t *q;
2115   nat64_main_t *nm = &nat64_main;
2116   nat64_db_t *db;
2117
2118   q = vl_api_client_index_to_input_queue (mp->client_index);
2119   if (q == 0)
2120     return;
2121
2122   nat64_api_walk_ctx_t ctx = {
2123     .q = q,
2124     .context = mp->context,
2125   };
2126
2127   /* *INDENT-OFF* */
2128   vec_foreach (db, nm->db)
2129     nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2130   /* *INDENT-ON* */
2131 }
2132
2133 static void *
2134 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2135 {
2136   u8 *s;
2137
2138   s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2139
2140   FINISH;
2141 }
2142
2143 static void
2144 vl_api_nat64_set_timeouts_t_handler (vl_api_nat64_set_timeouts_t * mp)
2145 {
2146   snat_main_t *sm = &snat_main;
2147   vl_api_nat64_set_timeouts_reply_t *rmp;
2148   int rv = 0;
2149
2150   rv = nat64_set_icmp_timeout (ntohl (mp->icmp));
2151   if (rv)
2152     goto send_reply;
2153   rv = nat64_set_udp_timeout (ntohl (mp->udp));
2154   if (rv)
2155     goto send_reply;
2156   rv =
2157     nat64_set_tcp_timeouts (ntohl (mp->tcp_trans), ntohl (mp->tcp_est),
2158                             ntohl (mp->tcp_incoming_syn));
2159
2160 send_reply:
2161   REPLY_MACRO (VL_API_NAT64_SET_TIMEOUTS_REPLY);
2162 }
2163
2164 static void *vl_api_nat64_set_timeouts_t_print
2165   (vl_api_nat64_set_timeouts_t * mp, void *handle)
2166 {
2167   u8 *s;
2168
2169   s = format (0, "SCRIPT: nat64_set_timeouts ");
2170   s =
2171     format (s,
2172             "udp %d icmp %d, tcp_trans %d, tcp_est %d, tcp_incoming_syn %d\n",
2173             ntohl (mp->udp), ntohl (mp->icmp), ntohl (mp->tcp_trans),
2174             ntohl (mp->tcp_est), ntohl (mp->tcp_incoming_syn));
2175
2176   FINISH;
2177 }
2178
2179 static void
2180 vl_api_nat64_get_timeouts_t_handler (vl_api_nat64_get_timeouts_t * mp)
2181 {
2182   snat_main_t *sm = &snat_main;
2183   vl_api_nat64_get_timeouts_reply_t *rmp;
2184   int rv = 0;
2185
2186   /* *INDENT-OFF* */
2187   REPLY_MACRO2 (VL_API_NAT64_GET_TIMEOUTS_REPLY,
2188   ({
2189     rmp->udp = htonl (nat64_get_udp_timeout());
2190     rmp->icmp = htonl (nat64_get_icmp_timeout());
2191     rmp->tcp_trans = htonl (nat64_get_tcp_trans_timeout());
2192     rmp->tcp_est = htonl (nat64_get_tcp_est_timeout());
2193     rmp->tcp_incoming_syn = htonl (nat64_get_tcp_incoming_syn_timeout());
2194   }))
2195   /* *INDENT-ON* */
2196 }
2197
2198 static void *vl_api_nat64_get_timeouts_t_print
2199   (vl_api_nat64_get_timeouts_t * mp, void *handle)
2200 {
2201   u8 *s;
2202
2203   s = format (0, "SCRIPT: nat64_get_timeouts");
2204
2205   FINISH;
2206 }
2207
2208 static int
2209 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2210 {
2211   vl_api_nat64_st_details_t *rmp;
2212   snat_main_t *sm = &snat_main;
2213   nat64_api_walk_ctx_t *ctx = arg;
2214   nat64_db_bib_entry_t *bibe;
2215   fib_table_t *fib;
2216
2217   bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2218   if (!bibe)
2219     return -1;
2220
2221   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2222   if (!fib)
2223     return -1;
2224
2225   rmp = vl_msg_api_alloc (sizeof (*rmp));
2226   memset (rmp, 0, sizeof (*rmp));
2227   rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2228   rmp->context = ctx->context;
2229   clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2230   clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2231   rmp->il_port = bibe->in_port;
2232   rmp->ol_port = bibe->out_port;
2233   clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2234   clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2235   rmp->il_port = ste->r_port;
2236   rmp->vrf_id = ntohl (fib->ft_table_id);
2237   rmp->proto = ste->proto;
2238
2239   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
2240
2241   return 0;
2242 }
2243
2244 static void
2245 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2246 {
2247   unix_shared_memory_queue_t *q;
2248   nat64_main_t *nm = &nat64_main;
2249   nat64_db_t *db;
2250
2251   q = vl_api_client_index_to_input_queue (mp->client_index);
2252   if (q == 0)
2253     return;
2254
2255   nat64_api_walk_ctx_t ctx = {
2256     .q = q,
2257     .context = mp->context,
2258   };
2259
2260   /* *INDENT-OFF* */
2261   vec_foreach (db, nm->db)
2262     {
2263       ctx.db = db;
2264       nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
2265     }
2266   /* *INDENT-ON* */
2267 }
2268
2269 static void *
2270 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
2271 {
2272   u8 *s;
2273
2274   s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
2275
2276   FINISH;
2277 }
2278
2279 static void
2280 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
2281 {
2282   vl_api_nat64_add_del_prefix_reply_t *rmp;
2283   snat_main_t *sm = &snat_main;
2284   ip6_address_t prefix;
2285   int rv = 0;
2286
2287   memcpy (&prefix.as_u8, mp->prefix, 16);
2288
2289   rv =
2290     nat64_add_del_prefix (&prefix, mp->prefix_len,
2291                           clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
2292   REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
2293 }
2294
2295 static void *
2296 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
2297                                      void *handle)
2298 {
2299   u8 *s;
2300
2301   s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
2302               format_ip6_address, mp->prefix, mp->prefix_len,
2303               ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2304
2305   FINISH;
2306 }
2307
2308 static int
2309 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
2310 {
2311   vl_api_nat64_prefix_details_t *rmp;
2312   snat_main_t *sm = &snat_main;
2313   nat64_api_walk_ctx_t *ctx = arg;
2314
2315   rmp = vl_msg_api_alloc (sizeof (*rmp));
2316   memset (rmp, 0, sizeof (*rmp));
2317   rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
2318   clib_memcpy (rmp->prefix, &(p->prefix), 16);
2319   rmp->prefix_len = p->plen;
2320   rmp->vrf_id = ntohl (p->vrf_id);
2321   rmp->context = ctx->context;
2322
2323   vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
2324
2325   return 0;
2326 }
2327
2328 static void
2329 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
2330 {
2331   unix_shared_memory_queue_t *q;
2332
2333   q = vl_api_client_index_to_input_queue (mp->client_index);
2334   if (q == 0)
2335     return;
2336
2337   nat64_api_walk_ctx_t ctx = {
2338     .q = q,
2339     .context = mp->context,
2340   };
2341
2342   nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
2343 }
2344
2345 static void *
2346 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
2347                                   void *handle)
2348 {
2349   u8 *s;
2350
2351   s = format (0, "SCRIPT: nat64_prefix_dump\n");
2352
2353   FINISH;
2354 }
2355
2356 static void
2357   vl_api_nat64_add_del_interface_addr_t_handler
2358   (vl_api_nat64_add_del_interface_addr_t * mp)
2359 {
2360   snat_main_t *sm = &snat_main;
2361   vl_api_nat64_add_del_interface_addr_reply_t *rmp;
2362   u32 sw_if_index = ntohl (mp->sw_if_index);
2363   int rv = 0;
2364
2365   VALIDATE_SW_IF_INDEX (mp);
2366
2367   rv = nat64_add_interface_address (sw_if_index, mp->is_add);
2368
2369   BAD_SW_IF_INDEX_LABEL;
2370
2371   REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
2372 }
2373
2374 static void *vl_api_nat64_add_del_interface_addr_t_print
2375   (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
2376 {
2377   u8 *s;
2378
2379   s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
2380   s = format (s, "sw_if_index %d %s",
2381               clib_host_to_net_u32 (mp->sw_if_index),
2382               mp->is_add ? "" : "del");
2383
2384   FINISH;
2385 }
2386
2387 /***************/
2388 /*** DS-Lite ***/
2389 /***************/
2390
2391 static void
2392 vl_api_dslite_set_aftr_addr_t_handler (vl_api_dslite_set_aftr_addr_t * mp)
2393 {
2394   vl_api_dslite_set_aftr_addr_reply_t *rmp;
2395   snat_main_t *sm = &snat_main;
2396   dslite_main_t *dm = &dslite_main;
2397   int rv = 0;
2398   ip6_address_t ip6_addr;
2399
2400   memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
2401
2402   rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
2403
2404   REPLY_MACRO (VL_API_DSLITE_SET_AFTR_ADDR_REPLY);
2405 }
2406
2407 static void *
2408 vl_api_dslite_set_aftr_addr_t_print (vl_api_dslite_set_aftr_addr_t * mp,
2409                                      void *handle)
2410 {
2411   u8 *s;
2412
2413   s = format (0, "SCRIPT: dslite_set_aftr_addr ");
2414   s = format (s, "ip6_addr %U ip4_addr %U\n",
2415               format_ip6_address, mp->ip6_addr,
2416               format_ip4_address, mp->ip4_addr);
2417
2418   FINISH;
2419 }
2420
2421 static void
2422   vl_api_dslite_add_del_pool_addr_range_t_handler
2423   (vl_api_dslite_add_del_pool_addr_range_t * mp)
2424 {
2425   vl_api_dslite_add_del_pool_addr_range_reply_t *rmp;
2426   snat_main_t *sm = &snat_main;
2427   dslite_main_t *dm = &dslite_main;
2428   int rv = 0;
2429   ip4_address_t this_addr;
2430   u32 start_host_order, end_host_order;
2431   int i, count;
2432   u32 *tmp;
2433
2434   tmp = (u32 *) mp->start_addr;
2435   start_host_order = clib_host_to_net_u32 (tmp[0]);
2436   tmp = (u32 *) mp->end_addr;
2437   end_host_order = clib_host_to_net_u32 (tmp[0]);
2438
2439   count = (end_host_order - start_host_order) + 1;
2440   memcpy (&this_addr.as_u8, mp->start_addr, 4);
2441
2442   for (i = 0; i < count; i++)
2443     {
2444       if ((rv = dslite_add_del_pool_addr (dm, &this_addr, mp->is_add)))
2445         goto send_reply;
2446
2447       increment_v4_address (&this_addr);
2448     }
2449
2450 send_reply:
2451   REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2452 }
2453
2454 static void *vl_api_dslite_add_del_pool_addr_range_t_print
2455   (vl_api_dslite_add_del_pool_addr_range_t * mp, void *handle)
2456 {
2457   u8 *s;
2458
2459   s = format (0, "SCRIPT: dslite_add_del_pool_addr_range ");
2460   s = format (s, "%U - %U\n",
2461               format_ip4_address, mp->start_addr,
2462               format_ip4_address, mp->end_addr);
2463
2464   FINISH;
2465 }
2466
2467
2468 /* List of message types that this plugin understands */
2469 #define foreach_snat_plugin_api_msg                                     \
2470 _(NAT_CONTROL_PING, nat_control_ping)                                   \
2471 _(NAT_SHOW_CONFIG, nat_show_config)                                     \
2472 _(NAT_SET_WORKERS, nat_set_workers)                                     \
2473 _(NAT_WORKER_DUMP, nat_worker_dump)                                     \
2474 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable)                   \
2475 _(NAT_SET_REASS, nat_set_reass)                                         \
2476 _(NAT_GET_REASS, nat_get_reass)                                         \
2477 _(NAT_REASS_DUMP, nat_reass_dump)                                       \
2478 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range)             \
2479 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature)     \
2480 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping)           \
2481 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping)       \
2482 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump)                 \
2483 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump)             \
2484 _(NAT44_ADDRESS_DUMP, nat44_address_dump)                               \
2485 _(NAT44_INTERFACE_DUMP, nat44_interface_dump)                           \
2486 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr)           \
2487 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump)                 \
2488 _(NAT44_USER_DUMP, nat44_user_dump)                                     \
2489 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump)                     \
2490 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE,                               \
2491   nat44_interface_add_del_output_feature)                               \
2492 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP,                                  \
2493   nat44_interface_output_feature_dump)                                  \
2494 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping)     \
2495 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump)           \
2496 _(NAT44_DEL_SESSION, nat44_del_session)                                 \
2497 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable)     \
2498 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled)             \
2499 _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map)                             \
2500 _(NAT_DET_FORWARD, nat_det_forward)                                     \
2501 _(NAT_DET_REVERSE, nat_det_reverse)                                     \
2502 _(NAT_DET_MAP_DUMP, nat_det_map_dump)                                   \
2503 _(NAT_DET_SET_TIMEOUTS, nat_det_set_timeouts)                           \
2504 _(NAT_DET_GET_TIMEOUTS, nat_det_get_timeouts)                           \
2505 _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out)                 \
2506 _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in)                   \
2507 _(NAT_DET_SESSION_DUMP, nat_det_session_dump)                           \
2508 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range)         \
2509 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump)                           \
2510 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface)                     \
2511 _(NAT64_INTERFACE_DUMP, nat64_interface_dump)                           \
2512 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib)                   \
2513 _(NAT64_BIB_DUMP, nat64_bib_dump)                                       \
2514 _(NAT64_SET_TIMEOUTS, nat64_set_timeouts)                               \
2515 _(NAT64_GET_TIMEOUTS, nat64_get_timeouts)                               \
2516 _(NAT64_ST_DUMP, nat64_st_dump)                                         \
2517 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix)                           \
2518 _(NAT64_PREFIX_DUMP, nat64_prefix_dump)                                 \
2519 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr)           \
2520 _(DSLITE_ADD_DEL_POOL_ADDR_RANGE, dslite_add_del_pool_addr_range)       \
2521 _(DSLITE_SET_AFTR_ADDR, dslite_set_aftr_addr)
2522
2523 /* Set up the API message handling tables */
2524 static clib_error_t *
2525 snat_plugin_api_hookup (vlib_main_t * vm)
2526 {
2527   snat_main_t *sm __attribute__ ((unused)) = &snat_main;
2528 #define _(N,n)                                                  \
2529     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
2530                            #n,                                  \
2531                            vl_api_##n##_t_handler,              \
2532                            vl_noop_handler,                     \
2533                            vl_api_##n##_t_endian,               \
2534                            vl_api_##n##_t_print,                \
2535                            sizeof(vl_api_##n##_t), 1);
2536   foreach_snat_plugin_api_msg;
2537 #undef _
2538
2539   return 0;
2540 }
2541
2542 #define vl_msg_name_crc_list
2543 #include <nat/nat_all_api_h.h>
2544 #undef vl_msg_name_crc_list
2545
2546 static void
2547 setup_message_id_table (snat_main_t * sm, api_main_t * am)
2548 {
2549 #define _(id,n,crc) \
2550   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
2551   foreach_vl_msg_name_crc_nat;
2552 #undef _
2553 }
2554
2555 static void
2556 plugin_custom_dump_configure (snat_main_t * sm)
2557 {
2558 #define _(n,f) sm->api_main->msg_print_handlers \
2559   [VL_API_##n + sm->msg_id_base]                \
2560     = (void *) vl_api_##f##_t_print;
2561   foreach_snat_plugin_api_msg;
2562 #undef _
2563 }
2564
2565 clib_error_t *
2566 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
2567 {
2568   u8 *name;
2569   clib_error_t *error = 0;
2570
2571   name = format (0, "nat_%08x%c", api_version, 0);
2572
2573   /* Ask for a correctly-sized block of API message decode slots */
2574   sm->msg_id_base =
2575     vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
2576
2577   error = snat_plugin_api_hookup (vm);
2578
2579   /* Add our API messages to the global name_crc hash table */
2580   setup_message_id_table (sm, sm->api_main);
2581
2582   plugin_custom_dump_configure (sm);
2583
2584   vec_free (name);
2585
2586   return error;
2587 }
2588
2589 /*
2590  * fd.io coding-style-patch-verification: ON
2591  *
2592  * Local Variables:
2593  * eval: (c-set-style "gnu")
2594  * End:
2595  */