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