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