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