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