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