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