nat: fix type in api message
[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;
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   vec_free (tag);
1144
1145   REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
1146 }
1147
1148 static void *vl_api_nat44_add_del_static_mapping_t_print
1149   (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
1150 {
1151   u8 *s;
1152
1153   s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1154   s = format (s, "protocol %d local_addr %U external_addr %U ",
1155               mp->protocol,
1156               format_ip4_address, mp->local_ip_address,
1157               format_ip4_address, mp->external_ip_address);
1158
1159   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1160     s = format (s, "local_port %d external_port %d ",
1161                 clib_net_to_host_u16 (mp->local_port),
1162                 clib_net_to_host_u16 (mp->external_port));
1163
1164   s = format (s, "twice_nat %d out2in_only %d ",
1165               mp->flags & NAT_API_IS_TWICE_NAT,
1166               mp->flags & NAT_API_IS_OUT2IN_ONLY);
1167
1168   if (mp->vrf_id != ~0)
1169     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1170
1171   if (mp->external_sw_if_index != ~0)
1172     s = format (s, "external_sw_if_index %d",
1173                 clib_net_to_host_u32 (mp->external_sw_if_index));
1174   FINISH;
1175 }
1176
1177 static void
1178 send_nat44_static_mapping_details (snat_static_mapping_t * m,
1179                                    vl_api_registration_t * reg, u32 context)
1180 {
1181   vl_api_nat44_static_mapping_details_t *rmp;
1182   snat_main_t *sm = &snat_main;
1183   u32 len = sizeof (*rmp);
1184
1185   rmp = vl_msg_api_alloc (len);
1186   clib_memset (rmp, 0, len);
1187   rmp->_vl_msg_id =
1188     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1189
1190   clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1191   clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1192   rmp->external_sw_if_index = ~0;
1193   rmp->vrf_id = htonl (m->vrf_id);
1194   rmp->context = context;
1195
1196   if (m->twice_nat == TWICE_NAT)
1197     rmp->flags |= NAT_API_IS_TWICE_NAT;
1198   else if (m->twice_nat == TWICE_NAT_SELF)
1199     rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1200
1201   if (is_out2in_only_static_mapping (m))
1202     rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1203
1204   if (is_addr_only_static_mapping (m))
1205     {
1206       rmp->flags |= NAT_API_IS_ADDR_ONLY;
1207     }
1208   else
1209     {
1210       rmp->protocol = nat_proto_to_ip_proto (m->proto);
1211       rmp->external_port = m->external_port;
1212       rmp->local_port = m->local_port;
1213     }
1214
1215   if (m->tag)
1216     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1217
1218   vl_api_send_msg (reg, (u8 *) rmp);
1219 }
1220
1221 static void
1222 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
1223                                        vl_api_registration_t * reg,
1224                                        u32 context)
1225 {
1226   vl_api_nat44_static_mapping_details_t *rmp;
1227   snat_main_t *sm = &snat_main;
1228
1229   rmp = vl_msg_api_alloc (sizeof (*rmp));
1230   clib_memset (rmp, 0, sizeof (*rmp));
1231   rmp->_vl_msg_id =
1232     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1233   clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1234   rmp->external_sw_if_index = htonl (m->sw_if_index);
1235   rmp->vrf_id = htonl (m->vrf_id);
1236   rmp->context = context;
1237
1238   if (m->twice_nat)
1239     rmp->flags |= NAT_API_IS_TWICE_NAT;
1240
1241   if (m->addr_only)
1242     {
1243       rmp->flags |= NAT_API_IS_ADDR_ONLY;
1244     }
1245   else
1246     {
1247       rmp->protocol = nat_proto_to_ip_proto (m->proto);
1248       rmp->external_port = m->e_port;
1249       rmp->local_port = m->l_port;
1250     }
1251   if (m->tag)
1252     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1253
1254   vl_api_send_msg (reg, (u8 *) rmp);
1255 }
1256
1257 static void
1258 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
1259                                             * mp)
1260 {
1261   vl_api_registration_t *reg;
1262   snat_main_t *sm = &snat_main;
1263   snat_static_mapping_t *m;
1264   snat_static_map_resolve_t *rp;
1265   int j;
1266
1267   reg = vl_api_client_index_to_registration (mp->client_index);
1268   if (!reg)
1269     return;
1270
1271   /* *INDENT-OFF* */
1272   pool_foreach (m, sm->static_mappings,
1273   ({
1274       if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1275         send_nat44_static_mapping_details (m, reg, mp->context);
1276   }));
1277   /* *INDENT-ON* */
1278
1279   for (j = 0; j < vec_len (sm->to_resolve); j++)
1280     {
1281       rp = sm->to_resolve + j;
1282       if (!rp->identity_nat)
1283         send_nat44_static_map_resolve_details (rp, reg, mp->context);
1284     }
1285 }
1286
1287 static void *
1288 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
1289                                           mp, void *handle)
1290 {
1291   u8 *s;
1292
1293   s = format (0, "SCRIPT: nat44_static_mapping_dump ");
1294
1295   FINISH;
1296 }
1297
1298 static void
1299   vl_api_nat44_add_del_identity_mapping_t_handler
1300   (vl_api_nat44_add_del_identity_mapping_t * mp)
1301 {
1302   snat_main_t *sm = &snat_main;
1303   vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
1304   ip4_address_t addr;
1305   u16 port = 0;
1306   u32 vrf_id, sw_if_index;
1307   int rv = 0;
1308   nat_protocol_t proto = NAT_PROTOCOL_OTHER;
1309   u8 *tag = 0;
1310
1311   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1312     {
1313       port = mp->port;
1314       proto = ip_proto_to_nat_proto (mp->protocol);
1315     }
1316   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1317   sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1318   if (sw_if_index != ~0)
1319     addr.as_u32 = 0;
1320   else
1321     memcpy (&addr.as_u8, mp->ip_address, 4);
1322   mp->tag[sizeof (mp->tag) - 1] = 0;
1323   tag = format (0, "%s", mp->tag);
1324   vec_terminate_c_string (tag);
1325
1326   rv =
1327     snat_add_static_mapping (addr, addr, port, port, vrf_id,
1328                              mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1329                              proto, mp->is_add, 0, 0, tag, 1);
1330   vec_free (tag);
1331
1332   REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1333 }
1334
1335 static void *vl_api_nat44_add_del_identity_mapping_t_print
1336   (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
1337 {
1338   u8 *s;
1339
1340   s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
1341   if (mp->sw_if_index != ~0)
1342     s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
1343   else
1344     s = format (s, "addr %U", format_ip4_address, mp->ip_address);
1345
1346   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1347     s =
1348       format (s, " protocol %d port %d", mp->protocol,
1349               clib_net_to_host_u16 (mp->port));
1350
1351   if (mp->vrf_id != ~0)
1352     s = format (s, " vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1353
1354   FINISH;
1355 }
1356
1357 static void
1358 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1359                                      vl_api_registration_t * reg, u32 context)
1360 {
1361   vl_api_nat44_identity_mapping_details_t *rmp;
1362   snat_main_t *sm = &snat_main;
1363   nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1364
1365   rmp = vl_msg_api_alloc (sizeof (*rmp));
1366   clib_memset (rmp, 0, sizeof (*rmp));
1367   rmp->_vl_msg_id =
1368     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1369
1370   if (is_addr_only_static_mapping (m))
1371     rmp->flags |= NAT_API_IS_ADDR_ONLY;
1372
1373   clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1374   rmp->port = m->local_port;
1375   rmp->sw_if_index = ~0;
1376   rmp->vrf_id = htonl (local->vrf_id);
1377   rmp->protocol = nat_proto_to_ip_proto (m->proto);
1378   rmp->context = context;
1379   if (m->tag)
1380     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1381
1382   vl_api_send_msg (reg, (u8 *) rmp);
1383 }
1384
1385 static void
1386 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1387                                          vl_api_registration_t * reg,
1388                                          u32 context)
1389 {
1390   vl_api_nat44_identity_mapping_details_t *rmp;
1391   snat_main_t *sm = &snat_main;
1392
1393   rmp = vl_msg_api_alloc (sizeof (*rmp));
1394   clib_memset (rmp, 0, sizeof (*rmp));
1395   rmp->_vl_msg_id =
1396     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1397
1398   if (m->addr_only)
1399     rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1400
1401   rmp->port = m->l_port;
1402   rmp->sw_if_index = htonl (m->sw_if_index);
1403   rmp->vrf_id = htonl (m->vrf_id);
1404   rmp->protocol = nat_proto_to_ip_proto (m->proto);
1405   rmp->context = context;
1406   if (m->tag)
1407     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1408
1409   vl_api_send_msg (reg, (u8 *) rmp);
1410 }
1411
1412 static void
1413   vl_api_nat44_identity_mapping_dump_t_handler
1414   (vl_api_nat44_identity_mapping_dump_t * mp)
1415 {
1416   vl_api_registration_t *reg;
1417   snat_main_t *sm = &snat_main;
1418   snat_static_mapping_t *m;
1419   snat_static_map_resolve_t *rp;
1420   int j;
1421
1422   reg = vl_api_client_index_to_registration (mp->client_index);
1423   if (!reg)
1424     return;
1425
1426   /* *INDENT-OFF* */
1427   pool_foreach (m, sm->static_mappings,
1428   ({
1429       if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1430         {
1431           pool_foreach_index (j, m->locals,
1432           ({
1433             send_nat44_identity_mapping_details (m, j, reg, mp->context);
1434           }));
1435         }
1436   }));
1437   /* *INDENT-ON* */
1438
1439   for (j = 0; j < vec_len (sm->to_resolve); j++)
1440     {
1441       rp = sm->to_resolve + j;
1442       if (rp->identity_nat)
1443         send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1444     }
1445 }
1446
1447 static void *vl_api_nat44_identity_mapping_dump_t_print
1448   (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1449 {
1450   u8 *s;
1451
1452   s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1453
1454   FINISH;
1455 }
1456
1457 static void
1458   vl_api_nat44_add_del_interface_addr_t_handler
1459   (vl_api_nat44_add_del_interface_addr_t * mp)
1460 {
1461   snat_main_t *sm = &snat_main;
1462   vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1463   u32 sw_if_index = ntohl (mp->sw_if_index);
1464   int rv = 0;
1465   u8 is_del;
1466
1467   is_del = !mp->is_add;
1468
1469   VALIDATE_SW_IF_INDEX (mp);
1470
1471   rv = snat_add_interface_address (sm, sw_if_index, is_del,
1472                                    mp->flags & NAT_API_IS_TWICE_NAT);
1473
1474   BAD_SW_IF_INDEX_LABEL;
1475   REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1476 }
1477
1478 static void *vl_api_nat44_add_del_interface_addr_t_print
1479   (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1480 {
1481   u8 *s;
1482
1483   s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1484   s = format (s, "sw_if_index %d twice_nat %d %s",
1485               clib_host_to_net_u32 (mp->sw_if_index),
1486               mp->flags & NAT_API_IS_TWICE_NAT, mp->is_add ? "" : "del");
1487
1488   FINISH;
1489 }
1490
1491 static void
1492 send_nat44_interface_addr_details (u32 sw_if_index,
1493                                    vl_api_registration_t * reg, u32 context,
1494                                    u8 twice_nat)
1495 {
1496   vl_api_nat44_interface_addr_details_t *rmp;
1497   snat_main_t *sm = &snat_main;
1498
1499   rmp = vl_msg_api_alloc (sizeof (*rmp));
1500   clib_memset (rmp, 0, sizeof (*rmp));
1501   rmp->_vl_msg_id =
1502     ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1503   rmp->sw_if_index = ntohl (sw_if_index);
1504
1505   if (twice_nat)
1506     rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1507   rmp->context = context;
1508
1509   vl_api_send_msg (reg, (u8 *) rmp);
1510 }
1511
1512 static void
1513 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1514                                             * mp)
1515 {
1516   vl_api_registration_t *reg;
1517   snat_main_t *sm = &snat_main;
1518   u32 *i;
1519
1520   reg = vl_api_client_index_to_registration (mp->client_index);
1521   if (!reg)
1522     return;
1523
1524   /* *INDENT-OFF* */
1525   vec_foreach (i, sm->auto_add_sw_if_indices)
1526     send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1527   vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1528     send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1529   /* *INDENT-ON* */
1530 }
1531
1532 static void *
1533 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1534                                           mp, void *handle)
1535 {
1536   u8 *s;
1537
1538   s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1539
1540   FINISH;
1541 }
1542
1543 static void
1544 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1545                          u32 context)
1546 {
1547   vl_api_nat44_user_details_t *rmp;
1548   snat_main_t *sm = &snat_main;
1549   ip4_main_t *im = &ip4_main;
1550
1551   rmp = vl_msg_api_alloc (sizeof (*rmp));
1552   clib_memset (rmp, 0, sizeof (*rmp));
1553   rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1554
1555   if (!pool_is_free_index (im->fibs, u->fib_index))
1556     {
1557       fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1558       rmp->vrf_id = ntohl (fib->ft_table_id);
1559     }
1560
1561   clib_memcpy (rmp->ip_address, &(u->addr), 4);
1562   rmp->nsessions = ntohl (u->nsessions);
1563   rmp->nstaticsessions = ntohl (u->nstaticsessions);
1564   rmp->context = context;
1565
1566   vl_api_send_msg (reg, (u8 *) rmp);
1567 }
1568
1569 static void
1570 nat_ed_user_create_helper (snat_main_per_thread_data_t * tsm,
1571                            snat_session_t * s)
1572 {
1573   snat_user_key_t k;
1574   k.addr = s->in2out.addr;
1575   k.fib_index = s->in2out.fib_index;
1576   clib_bihash_kv_8_8_t key, value;
1577   key.key = k.as_u64;
1578   snat_user_t *u;
1579   if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1580     {
1581       pool_get (tsm->users, u);
1582       u->addr = k.addr;
1583       u->fib_index = k.fib_index;
1584       u->nsessions = 0;
1585       u->nstaticsessions = 0;
1586       key.value = u - tsm->users;
1587       clib_bihash_add_del_8_8 (&tsm->user_hash, &key, 1);
1588     }
1589   else
1590     {
1591       u = pool_elt_at_index (tsm->users, value.value);
1592     }
1593   if (snat_is_session_static (s))
1594     {
1595       ++u->nstaticsessions;
1596     }
1597   else
1598     {
1599       ++u->nsessions;
1600     }
1601 }
1602
1603 static void
1604 nat_ed_users_create (snat_main_per_thread_data_t * tsm)
1605 {
1606   snat_session_t *s;
1607   /* *INDENT-OFF* */
1608   pool_foreach (s, tsm->sessions, { nat_ed_user_create_helper (tsm, s); });
1609   /* *INDENT-ON* */
1610 }
1611
1612 static void
1613 nat_ed_users_destroy (snat_main_per_thread_data_t * tsm)
1614 {
1615   snat_user_t *u;
1616   /* *INDENT-OFF* */
1617   pool_flush (u, tsm->users, { });
1618   /* *INDENT-ON* */
1619   clib_bihash_free_8_8 (&tsm->user_hash);
1620   clib_bihash_init_8_8 (&tsm->user_hash, "users", snat_main.user_buckets,
1621                         snat_main.user_memory_size);
1622   clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
1623 }
1624
1625 static void
1626 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1627 {
1628   vl_api_registration_t *reg;
1629   snat_main_t *sm = &snat_main;
1630   snat_main_per_thread_data_t *tsm;
1631   snat_user_t *u;
1632
1633   reg = vl_api_client_index_to_registration (mp->client_index);
1634   if (!reg)
1635     return;
1636
1637   /* *INDENT-OFF* */
1638   vec_foreach (tsm, sm->per_thread_data)
1639     {
1640       if (sm->endpoint_dependent)
1641         {
1642           nat_ed_users_create (tsm);
1643         }
1644       pool_foreach (u, tsm->users,
1645       ({
1646         send_nat44_user_details (u, reg, mp->context);
1647       }));
1648       if (sm->endpoint_dependent)
1649         {
1650           nat_ed_users_destroy (tsm);
1651         }
1652     }
1653   /* *INDENT-ON* */
1654 }
1655
1656 static void *
1657 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1658 {
1659   u8 *s;
1660
1661   s = format (0, "SCRIPT: nat44_user_dump ");
1662
1663   FINISH;
1664 }
1665
1666 static void
1667 send_nat44_user_session_details (snat_session_t * s,
1668                                  vl_api_registration_t * reg, u32 context)
1669 {
1670   vl_api_nat44_user_session_details_t *rmp;
1671   snat_main_t *sm = &snat_main;
1672
1673   rmp = vl_msg_api_alloc (sizeof (*rmp));
1674   clib_memset (rmp, 0, sizeof (*rmp));
1675   rmp->_vl_msg_id =
1676     ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1677   clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1678   clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1679
1680   if (snat_is_session_static (s))
1681     rmp->flags |= NAT_API_IS_STATIC;
1682
1683   if (is_twice_nat_session (s))
1684     rmp->flags |= NAT_API_IS_TWICE_NAT;
1685
1686   if (is_ed_session (s) || is_fwd_bypass_session (s))
1687     rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1688
1689   rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1690   rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1691   rmp->total_pkts = ntohl (s->total_pkts);
1692   rmp->context = context;
1693   if (snat_is_unk_proto_session (s))
1694     {
1695       rmp->outside_port = 0;
1696       rmp->inside_port = 0;
1697       rmp->protocol = ntohs (s->in2out.port);
1698     }
1699   else
1700     {
1701       rmp->outside_port = s->out2in.port;
1702       rmp->inside_port = s->in2out.port;
1703       rmp->protocol = ntohs (nat_proto_to_ip_proto (s->nat_proto));
1704     }
1705   if (is_ed_session (s) || is_fwd_bypass_session (s))
1706     {
1707       clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1708       rmp->ext_host_port = s->ext_host_port;
1709       if (is_twice_nat_session (s))
1710         {
1711           clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1712           rmp->ext_host_nat_port = s->ext_host_nat_port;
1713         }
1714     }
1715
1716   vl_api_send_msg (reg, (u8 *) rmp);
1717 }
1718
1719 static void
1720 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1721                                           mp)
1722 {
1723   vl_api_registration_t *reg;
1724   snat_main_t *sm = &snat_main;
1725   snat_main_per_thread_data_t *tsm;
1726   snat_session_t *s;
1727   clib_bihash_kv_8_8_t key, value;
1728   snat_user_key_t ukey;
1729   snat_user_t *u;
1730   u32 session_index, head_index, elt_index;
1731   dlist_elt_t *head, *elt;
1732   ip4_header_t ip;
1733
1734   reg = vl_api_client_index_to_registration (mp->client_index);
1735   if (!reg)
1736     return;
1737
1738   clib_memcpy (&ukey.addr, mp->ip_address, 4);
1739   ip.src_address.as_u32 = ukey.addr.as_u32;
1740   ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1741   key.key = ukey.as_u64;
1742   if (sm->num_workers > 1)
1743     tsm =
1744       vec_elt_at_index (sm->per_thread_data,
1745                         sm->worker_in2out_cb (&ip, ukey.fib_index, 0));
1746   else
1747     tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1748   if (!sm->endpoint_dependent)
1749     {
1750       if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1751         return;
1752       u = pool_elt_at_index (tsm->users, value.value);
1753       if (!u->nsessions && !u->nstaticsessions)
1754         return;
1755
1756       head_index = u->sessions_per_user_list_head_index;
1757       head = pool_elt_at_index (tsm->list_pool, head_index);
1758       elt_index = head->next;
1759       elt = pool_elt_at_index (tsm->list_pool, elt_index);
1760       session_index = elt->value;
1761       while (session_index != ~0)
1762         {
1763           s = pool_elt_at_index (tsm->sessions, session_index);
1764
1765           send_nat44_user_session_details (s, reg, mp->context);
1766
1767           elt_index = elt->next;
1768           elt = pool_elt_at_index (tsm->list_pool, elt_index);
1769           session_index = elt->value;
1770         }
1771     }
1772   else
1773     {
1774       /* *INDENT-OFF* */
1775       pool_foreach (s, tsm->sessions, {
1776         if (s->in2out.addr.as_u32 == ukey.addr.as_u32)
1777           {
1778             send_nat44_user_session_details (s, reg, mp->context);
1779           }
1780       });
1781       /* *INDENT-ON* */
1782     }
1783 }
1784
1785 static void *
1786 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1787                                         void *handle)
1788 {
1789   u8 *s;
1790
1791   s = format (0, "SCRIPT: nat44_user_session_dump ");
1792   s = format (s, "ip_address %U vrf_id %d\n",
1793               format_ip4_address, mp->ip_address,
1794               clib_net_to_host_u32 (mp->vrf_id));
1795
1796   FINISH;
1797 }
1798
1799 static nat44_lb_addr_port_t *
1800 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1801                              u32 addr_port_pair_num)
1802 {
1803   u8 i;
1804   nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1805   vl_api_nat44_lb_addr_port_t *ap;
1806
1807   for (i = 0; i < addr_port_pair_num; i++)
1808     {
1809       ap = &addr_port_pairs[i];
1810       clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1811       clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1812       lb_addr_port.port = ap->port;
1813       lb_addr_port.probability = ap->probability;
1814       lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1815       vec_add1 (lb_addr_port_pairs, lb_addr_port);
1816     }
1817
1818   return lb_addr_port_pairs;
1819 }
1820
1821 static void
1822   vl_api_nat44_add_del_lb_static_mapping_t_handler
1823   (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1824 {
1825   snat_main_t *sm = &snat_main;
1826   vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1827   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1828   int rv = 0;
1829   nat44_lb_addr_port_t *locals = 0;
1830   ip4_address_t e_addr;
1831   nat_protocol_t proto;
1832   u8 *tag = 0;
1833
1834   if (!sm->endpoint_dependent)
1835     {
1836       rv = VNET_API_ERROR_UNSUPPORTED;
1837       goto send_reply;
1838     }
1839
1840   locals =
1841     unformat_nat44_lb_addr_port (mp->locals,
1842                                  clib_net_to_host_u32 (mp->local_num));
1843   clib_memcpy (&e_addr, mp->external_addr, 4);
1844   proto = ip_proto_to_nat_proto (mp->protocol);
1845
1846   if (mp->flags & NAT_API_IS_TWICE_NAT)
1847     twice_nat = TWICE_NAT;
1848   else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1849     twice_nat = TWICE_NAT_SELF;
1850   mp->tag[sizeof (mp->tag) - 1] = 0;
1851   tag = format (0, "%s", mp->tag);
1852   vec_terminate_c_string (tag);
1853
1854   rv =
1855     nat44_add_del_lb_static_mapping (e_addr,
1856                                      mp->external_port,
1857                                      proto, locals, mp->is_add,
1858                                      twice_nat,
1859                                      mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1860                                      clib_net_to_host_u32 (mp->affinity));
1861
1862   vec_free (locals);
1863   vec_free (tag);
1864
1865 send_reply:
1866   REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1867 }
1868
1869 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1870   (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1871 {
1872   u8 *s;
1873
1874   s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1875   s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1876               mp->is_add,
1877               mp->flags & NAT_API_IS_TWICE_NAT,
1878               mp->flags & NAT_API_IS_OUT2IN_ONLY);
1879
1880   FINISH;
1881 }
1882
1883 static void
1884   vl_api_nat44_lb_static_mapping_add_del_local_t_handler
1885   (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
1886 {
1887   snat_main_t *sm = &snat_main;
1888   vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
1889   int rv = 0;
1890   ip4_address_t e_addr, l_addr;
1891   nat_protocol_t proto;
1892
1893   if (!sm->endpoint_dependent)
1894     {
1895       rv = VNET_API_ERROR_UNSUPPORTED;
1896       goto send_reply;
1897     }
1898
1899   clib_memcpy (&e_addr, mp->external_addr, 4);
1900   clib_memcpy (&l_addr, mp->local.addr, 4);
1901   proto = ip_proto_to_nat_proto (mp->protocol);
1902
1903   rv =
1904     nat44_lb_static_mapping_add_del_local (e_addr,
1905                                            clib_net_to_host_u16
1906                                            (mp->external_port), l_addr,
1907                                            clib_net_to_host_u16 (mp->
1908                                                                  local.port),
1909                                            proto,
1910                                            clib_net_to_host_u32 (mp->
1911                                                                  local.vrf_id),
1912                                            mp->local.probability, mp->is_add);
1913
1914 send_reply:
1915   REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
1916 }
1917
1918 static void *vl_api_nat44_lb_static_mapping_add_del_local_t_print
1919   (vl_api_nat44_lb_static_mapping_add_del_local_t * mp, void *handle)
1920 {
1921   u8 *s;
1922
1923   s = format (0, "SCRIPT: nat44_lb_static_mapping_add_del_local ");
1924   s = format (s, "is_add %d", mp->is_add);
1925
1926   FINISH;
1927 }
1928
1929 static void
1930 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1931                                       vl_api_registration_t * reg,
1932                                       u32 context)
1933 {
1934   vl_api_nat44_lb_static_mapping_details_t *rmp;
1935   snat_main_t *sm = &snat_main;
1936   nat44_lb_addr_port_t *ap;
1937   vl_api_nat44_lb_addr_port_t *locals;
1938   u32 local_num = 0;
1939
1940   rmp =
1941     vl_msg_api_alloc (sizeof (*rmp) +
1942                       (pool_elts (m->locals) *
1943                        sizeof (nat44_lb_addr_port_t)));
1944   clib_memset (rmp, 0, sizeof (*rmp));
1945   rmp->_vl_msg_id =
1946     ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1947
1948   clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1949   rmp->external_port = m->external_port;
1950   rmp->protocol = nat_proto_to_ip_proto (m->proto);
1951   rmp->context = context;
1952
1953   if (m->twice_nat == TWICE_NAT)
1954     rmp->flags |= NAT_API_IS_TWICE_NAT;
1955   else if (m->twice_nat == TWICE_NAT_SELF)
1956     rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1957   if (is_out2in_only_static_mapping (m))
1958     rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1959   if (m->tag)
1960     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1961
1962   locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
1963   /* *INDENT-OFF* */
1964   pool_foreach (ap, m->locals,
1965   ({
1966     clib_memcpy (locals->addr, &(ap->addr), 4);
1967     locals->port = ap->port;
1968     locals->probability = ap->probability;
1969     locals->vrf_id = ntohl (ap->vrf_id);
1970     locals++;
1971     local_num++;
1972   }));
1973   /* *INDENT-ON* */
1974   rmp->local_num = ntohl (local_num);
1975
1976   vl_api_send_msg (reg, (u8 *) rmp);
1977 }
1978
1979 static void
1980   vl_api_nat44_lb_static_mapping_dump_t_handler
1981   (vl_api_nat44_lb_static_mapping_dump_t * mp)
1982 {
1983   vl_api_registration_t *reg;
1984   snat_main_t *sm = &snat_main;
1985   snat_static_mapping_t *m;
1986
1987   if (!sm->endpoint_dependent)
1988     return;
1989
1990   reg = vl_api_client_index_to_registration (mp->client_index);
1991   if (!reg)
1992     return;
1993
1994   /* *INDENT-OFF* */
1995   pool_foreach (m, sm->static_mappings,
1996   ({
1997       if (is_lb_static_mapping(m))
1998         send_nat44_lb_static_mapping_details (m, reg, mp->context);
1999   }));
2000   /* *INDENT-ON* */
2001 }
2002
2003 static void *vl_api_nat44_lb_static_mapping_dump_t_print
2004   (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
2005 {
2006   u8 *s;
2007
2008   s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
2009
2010   FINISH;
2011 }
2012
2013 static void
2014 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
2015 {
2016   snat_main_t *sm = &snat_main;
2017   vl_api_nat44_del_session_reply_t *rmp;
2018   ip4_address_t addr, eh_addr;
2019   u16 port, eh_port;
2020   u32 vrf_id;
2021   int rv = 0;
2022   u8 is_in;
2023   nat_protocol_t proto;
2024
2025   memcpy (&addr.as_u8, mp->address, 4);
2026   port = mp->port;
2027   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
2028   proto = ip_proto_to_nat_proto (mp->protocol);
2029   memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
2030   eh_port = mp->ext_host_port;
2031
2032   is_in = mp->flags & NAT_API_IS_INSIDE;
2033
2034   if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2035     rv =
2036       nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
2037                             vrf_id, is_in);
2038   else
2039     rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
2040
2041   REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
2042 }
2043
2044 static void *
2045 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
2046                                   void *handle)
2047 {
2048   u8 *s;
2049
2050   s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
2051   s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
2052               format_ip4_address, mp->address,
2053               clib_net_to_host_u16 (mp->port),
2054               mp->protocol, clib_net_to_host_u32 (mp->vrf_id),
2055               mp->flags & NAT_API_IS_INSIDE);
2056   if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2057     s = format (s, "ext_host_address %U ext_host_port %d",
2058                 format_ip4_address, mp->ext_host_address,
2059                 clib_net_to_host_u16 (mp->ext_host_port));
2060
2061   FINISH;
2062 }
2063
2064 static void
2065   vl_api_nat44_forwarding_enable_disable_t_handler
2066   (vl_api_nat44_forwarding_enable_disable_t * mp)
2067 {
2068   snat_main_t *sm = &snat_main;
2069   vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
2070   int rv = 0;
2071   u32 *ses_to_be_removed = 0, *ses_index;
2072   snat_main_per_thread_data_t *tsm;
2073   snat_session_t *s;
2074
2075   sm->forwarding_enabled = mp->enable != 0;
2076
2077   if (mp->enable == 0)
2078     {
2079       /* *INDENT-OFF* */
2080       vec_foreach (tsm, sm->per_thread_data)
2081       {
2082         pool_foreach (s, tsm->sessions,
2083         ({
2084           if (is_fwd_bypass_session(s))
2085             {
2086               vec_add1 (ses_to_be_removed, s - tsm->sessions);
2087             }
2088         }));
2089         if(sm->endpoint_dependent){
2090             vec_foreach (ses_index, ses_to_be_removed)
2091               {
2092                 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
2093                 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2094                 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
2095               }
2096         }else{
2097             vec_foreach (ses_index, ses_to_be_removed)
2098               {
2099                 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
2100                 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2101                 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
2102               }
2103         }
2104         vec_free (ses_to_be_removed);
2105       }
2106       /* *INDENT-ON* */
2107     }
2108
2109   REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
2110 }
2111
2112 static void *vl_api_nat44_forwarding_enable_disable_t_print
2113   (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
2114 {
2115   u8 *s;
2116
2117   s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
2118   s = format (s, "enable %d", mp->enable != 0);
2119
2120   FINISH;
2121 }
2122
2123 static void
2124   vl_api_nat44_forwarding_is_enabled_t_handler
2125   (vl_api_nat44_forwarding_is_enabled_t * mp)
2126 {
2127   vl_api_registration_t *reg;
2128   snat_main_t *sm = &snat_main;
2129   vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
2130
2131   reg = vl_api_client_index_to_registration (mp->client_index);
2132   if (!reg)
2133     return;
2134
2135   rmp = vl_msg_api_alloc (sizeof (*rmp));
2136   clib_memset (rmp, 0, sizeof (*rmp));
2137   rmp->_vl_msg_id =
2138     ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
2139   rmp->context = mp->context;
2140
2141   rmp->enabled = sm->forwarding_enabled;
2142
2143   vl_api_send_msg (reg, (u8 *) rmp);
2144 }
2145
2146 static void *vl_api_nat44_forwarding_is_enabled_t_print
2147   (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
2148 {
2149   u8 *s;
2150
2151   s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
2152
2153   FINISH;
2154 }
2155
2156 /*************/
2157 /*** NAT64 ***/
2158 /*************/
2159
2160 static void
2161   vl_api_nat64_add_del_pool_addr_range_t_handler
2162   (vl_api_nat64_add_del_pool_addr_range_t * mp)
2163 {
2164   vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
2165   snat_main_t *sm = &snat_main;
2166   int rv = 0;
2167   ip4_address_t this_addr;
2168   u32 start_host_order, end_host_order;
2169   u32 vrf_id;
2170   int i, count;
2171   u32 *tmp;
2172
2173   tmp = (u32 *) mp->start_addr;
2174   start_host_order = clib_host_to_net_u32 (tmp[0]);
2175   tmp = (u32 *) mp->end_addr;
2176   end_host_order = clib_host_to_net_u32 (tmp[0]);
2177
2178   count = (end_host_order - start_host_order) + 1;
2179
2180   vrf_id = clib_host_to_net_u32 (mp->vrf_id);
2181
2182   memcpy (&this_addr.as_u8, mp->start_addr, 4);
2183
2184   for (i = 0; i < count; i++)
2185     {
2186       if ((rv = nat64_add_del_pool_addr (0, &this_addr, vrf_id, mp->is_add)))
2187         goto send_reply;
2188
2189       increment_v4_address (&this_addr);
2190     }
2191
2192 send_reply:
2193   REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2194 }
2195
2196 static void *vl_api_nat64_add_del_pool_addr_range_t_print
2197   (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
2198 {
2199   u8 *s;
2200
2201   s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
2202   s = format (s, "%U - %U vrf_id %u %s\n",
2203               format_ip4_address, mp->start_addr,
2204               format_ip4_address, mp->end_addr,
2205               ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2206
2207   FINISH;
2208 }
2209
2210 typedef struct nat64_api_walk_ctx_t_
2211 {
2212   vl_api_registration_t *reg;
2213   u32 context;
2214   nat64_db_t *db;
2215 } nat64_api_walk_ctx_t;
2216
2217 static int
2218 nat64_api_pool_walk (snat_address_t * a, void *arg)
2219 {
2220   vl_api_nat64_pool_addr_details_t *rmp;
2221   snat_main_t *sm = &snat_main;
2222   nat64_api_walk_ctx_t *ctx = arg;
2223
2224   rmp = vl_msg_api_alloc (sizeof (*rmp));
2225   clib_memset (rmp, 0, sizeof (*rmp));
2226   rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
2227   clib_memcpy (rmp->address, &(a->addr), 4);
2228   if (a->fib_index != ~0)
2229     {
2230       fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
2231       if (!fib)
2232         return -1;
2233       rmp->vrf_id = ntohl (fib->ft_table_id);
2234     }
2235   else
2236     rmp->vrf_id = ~0;
2237   rmp->context = ctx->context;
2238
2239   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2240
2241   return 0;
2242 }
2243
2244 static void
2245 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
2246 {
2247   vl_api_registration_t *reg;
2248
2249   reg = vl_api_client_index_to_registration (mp->client_index);
2250   if (!reg)
2251     return;
2252
2253   nat64_api_walk_ctx_t ctx = {
2254     .reg = reg,
2255     .context = mp->context,
2256   };
2257
2258   nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
2259 }
2260
2261 static void *
2262 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
2263                                      void *handle)
2264 {
2265   u8 *s;
2266
2267   s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
2268
2269   FINISH;
2270 }
2271
2272 static void
2273 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
2274                                           mp)
2275 {
2276   snat_main_t *sm = &snat_main;
2277   vl_api_nat64_add_del_interface_reply_t *rmp;
2278   int rv = 0;
2279
2280   VALIDATE_SW_IF_INDEX (mp);
2281
2282   rv =
2283     nat64_add_del_interface (ntohl (mp->sw_if_index),
2284                              mp->flags & NAT_API_IS_INSIDE, mp->is_add);
2285
2286   BAD_SW_IF_INDEX_LABEL;
2287
2288   REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
2289 }
2290
2291 static void *
2292 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
2293                                         void *handle)
2294 {
2295   u8 *s;
2296
2297   s = format (0, "SCRIPT: nat64_add_del_interface ");
2298   s = format (s, "sw_if_index %d %s %s",
2299               clib_host_to_net_u32 (mp->sw_if_index),
2300               mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
2301               mp->is_add ? "" : "del");
2302
2303   FINISH;
2304 }
2305
2306 static int
2307 nat64_api_interface_walk (snat_interface_t * i, void *arg)
2308 {
2309   vl_api_nat64_interface_details_t *rmp;
2310   snat_main_t *sm = &snat_main;
2311   nat64_api_walk_ctx_t *ctx = arg;
2312
2313   rmp = vl_msg_api_alloc (sizeof (*rmp));
2314   clib_memset (rmp, 0, sizeof (*rmp));
2315   rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2316   rmp->sw_if_index = ntohl (i->sw_if_index);
2317
2318   if (nat_interface_is_inside (i))
2319     rmp->flags |= NAT_API_IS_INSIDE;
2320   if (nat_interface_is_outside (i))
2321     rmp->flags |= NAT_API_IS_OUTSIDE;
2322
2323   rmp->context = ctx->context;
2324
2325   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2326
2327   return 0;
2328 }
2329
2330 static void
2331 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2332 {
2333   vl_api_registration_t *reg;
2334
2335   reg = vl_api_client_index_to_registration (mp->client_index);
2336   if (!reg)
2337     return;
2338
2339   nat64_api_walk_ctx_t ctx = {
2340     .reg = reg,
2341     .context = mp->context,
2342   };
2343
2344   nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2345 }
2346
2347 static void *
2348 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2349                                      void *handle)
2350 {
2351   u8 *s;
2352
2353   s = format (0, "SCRIPT: snat_interface_dump ");
2354
2355   FINISH;
2356 }
2357
2358 static void
2359   vl_api_nat64_add_del_static_bib_t_handler
2360   (vl_api_nat64_add_del_static_bib_t * mp)
2361 {
2362   snat_main_t *sm = &snat_main;
2363   vl_api_nat64_add_del_static_bib_reply_t *rmp;
2364   ip6_address_t in_addr;
2365   ip4_address_t out_addr;
2366   int rv = 0;
2367
2368   memcpy (&in_addr.as_u8, mp->i_addr, 16);
2369   memcpy (&out_addr.as_u8, mp->o_addr, 4);
2370
2371   rv =
2372     nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2373                                     clib_net_to_host_u16 (mp->i_port),
2374                                     clib_net_to_host_u16 (mp->o_port),
2375                                     mp->proto,
2376                                     clib_net_to_host_u32 (mp->vrf_id),
2377                                     mp->is_add);
2378
2379   REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2380 }
2381
2382 static void *vl_api_nat64_add_del_static_bib_t_print
2383   (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2384 {
2385   u8 *s;
2386
2387   s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2388   s = format (s, "protocol %d i_addr %U o_addr %U ",
2389               mp->proto,
2390               format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2391
2392   if (mp->vrf_id != ~0)
2393     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2394
2395   FINISH;
2396 }
2397
2398 static int
2399 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2400 {
2401   vl_api_nat64_bib_details_t *rmp;
2402   snat_main_t *sm = &snat_main;
2403   nat64_api_walk_ctx_t *ctx = arg;
2404   fib_table_t *fib;
2405
2406   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2407   if (!fib)
2408     return -1;
2409
2410   rmp = vl_msg_api_alloc (sizeof (*rmp));
2411   clib_memset (rmp, 0, sizeof (*rmp));
2412   rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2413   rmp->context = ctx->context;
2414   clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2415   clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2416   rmp->i_port = bibe->in_port;
2417   rmp->o_port = bibe->out_port;
2418   rmp->vrf_id = ntohl (fib->ft_table_id);
2419   rmp->proto = bibe->proto;
2420   if (bibe->is_static)
2421     rmp->flags |= NAT_API_IS_STATIC;
2422   rmp->ses_num = ntohl (bibe->ses_num);
2423
2424   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2425
2426   return 0;
2427 }
2428
2429 static void
2430 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2431 {
2432   vl_api_registration_t *reg;
2433   nat64_main_t *nm = &nat64_main;
2434   nat64_db_t *db;
2435
2436   reg = vl_api_client_index_to_registration (mp->client_index);
2437   if (!reg)
2438     return;
2439
2440   nat64_api_walk_ctx_t ctx = {
2441     .reg = reg,
2442     .context = mp->context,
2443   };
2444
2445   /* *INDENT-OFF* */
2446   vec_foreach (db, nm->db)
2447     nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2448   /* *INDENT-ON* */
2449 }
2450
2451 static void *
2452 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2453 {
2454   u8 *s;
2455
2456   s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2457
2458   FINISH;
2459 }
2460
2461 static int
2462 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2463 {
2464   vl_api_nat64_st_details_t *rmp;
2465   snat_main_t *sm = &snat_main;
2466   nat64_api_walk_ctx_t *ctx = arg;
2467   nat64_db_bib_entry_t *bibe;
2468   fib_table_t *fib;
2469
2470   bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2471   if (!bibe)
2472     return -1;
2473
2474   fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2475   if (!fib)
2476     return -1;
2477
2478   rmp = vl_msg_api_alloc (sizeof (*rmp));
2479   clib_memset (rmp, 0, sizeof (*rmp));
2480   rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2481   rmp->context = ctx->context;
2482   clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2483   clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2484   rmp->il_port = bibe->in_port;
2485   rmp->ol_port = bibe->out_port;
2486   clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2487   clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2488   rmp->il_port = ste->r_port;
2489   rmp->vrf_id = ntohl (fib->ft_table_id);
2490   rmp->proto = ste->proto;
2491
2492   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2493
2494   return 0;
2495 }
2496
2497 static void
2498 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2499 {
2500   vl_api_registration_t *reg;
2501   nat64_main_t *nm = &nat64_main;
2502   nat64_db_t *db;
2503
2504   reg = vl_api_client_index_to_registration (mp->client_index);
2505   if (!reg)
2506     return;
2507
2508   nat64_api_walk_ctx_t ctx = {
2509     .reg = reg,
2510     .context = mp->context,
2511   };
2512
2513   /* *INDENT-OFF* */
2514   vec_foreach (db, nm->db)
2515     {
2516       ctx.db = db;
2517       nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
2518     }
2519   /* *INDENT-ON* */
2520 }
2521
2522 static void *
2523 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
2524 {
2525   u8 *s;
2526
2527   s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
2528
2529   FINISH;
2530 }
2531
2532 static void
2533 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
2534 {
2535   vl_api_nat64_add_del_prefix_reply_t *rmp;
2536   snat_main_t *sm = &snat_main;
2537   ip6_address_t prefix;
2538   int rv = 0;
2539
2540   memcpy (&prefix.as_u8, mp->prefix.address, 16);
2541
2542   rv =
2543     nat64_add_del_prefix (&prefix, mp->prefix.len,
2544                           clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
2545   REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
2546 }
2547
2548 static void *
2549 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
2550                                      void *handle)
2551 {
2552   u8 *s;
2553
2554   s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
2555               format_ip6_address, mp->prefix.address, mp->prefix.len,
2556               ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2557
2558   FINISH;
2559 }
2560
2561 static int
2562 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
2563 {
2564   vl_api_nat64_prefix_details_t *rmp;
2565   snat_main_t *sm = &snat_main;
2566   nat64_api_walk_ctx_t *ctx = arg;
2567
2568   rmp = vl_msg_api_alloc (sizeof (*rmp));
2569   clib_memset (rmp, 0, sizeof (*rmp));
2570   rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
2571   clib_memcpy (rmp->prefix.address, &(p->prefix), 16);
2572   rmp->prefix.len = p->plen;
2573   rmp->vrf_id = ntohl (p->vrf_id);
2574   rmp->context = ctx->context;
2575
2576   vl_api_send_msg (ctx->reg, (u8 *) rmp);
2577
2578   return 0;
2579 }
2580
2581 static void
2582 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
2583 {
2584   vl_api_registration_t *reg;
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   nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
2596 }
2597
2598 static void *
2599 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
2600                                   void *handle)
2601 {
2602   u8 *s;
2603
2604   s = format (0, "SCRIPT: nat64_prefix_dump\n");
2605
2606   FINISH;
2607 }
2608
2609 static void
2610   vl_api_nat64_add_del_interface_addr_t_handler
2611   (vl_api_nat64_add_del_interface_addr_t * mp)
2612 {
2613   snat_main_t *sm = &snat_main;
2614   vl_api_nat64_add_del_interface_addr_reply_t *rmp;
2615   u32 sw_if_index = ntohl (mp->sw_if_index);
2616   int rv = 0;
2617
2618   VALIDATE_SW_IF_INDEX (mp);
2619
2620   rv = nat64_add_interface_address (sw_if_index, mp->is_add);
2621
2622   BAD_SW_IF_INDEX_LABEL;
2623
2624   REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
2625 }
2626
2627 static void *vl_api_nat64_add_del_interface_addr_t_print
2628   (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
2629 {
2630   u8 *s;
2631
2632   s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
2633   s = format (s, "sw_if_index %d %s",
2634               clib_host_to_net_u32 (mp->sw_if_index),
2635               mp->is_add ? "" : "del");
2636
2637   FINISH;
2638 }
2639
2640 /* List of message types that this plugin understands */
2641 #define foreach_snat_plugin_api_msg                                     \
2642 _(NAT_CONTROL_PING, nat_control_ping)                                   \
2643 _(NAT_SHOW_CONFIG, nat_show_config)                                     \
2644 _(NAT_SHOW_CONFIG_2, nat_show_config_2)                                 \
2645 _(NAT_SET_WORKERS, nat_set_workers)                                     \
2646 _(NAT_WORKER_DUMP, nat_worker_dump)                                     \
2647 _(NAT44_DEL_USER, nat44_del_user)                                       \
2648 _(NAT44_SET_SESSION_LIMIT, nat44_set_session_limit)                     \
2649 _(NAT_SET_LOG_LEVEL, nat_set_log_level)                                 \
2650 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable)                   \
2651 _(NAT_SET_TIMEOUTS, nat_set_timeouts)                                   \
2652 _(NAT_GET_TIMEOUTS, nat_get_timeouts)                                   \
2653 _(NAT_SET_ADDR_AND_PORT_ALLOC_ALG, nat_set_addr_and_port_alloc_alg)     \
2654 _(NAT_GET_ADDR_AND_PORT_ALLOC_ALG, nat_get_addr_and_port_alloc_alg)     \
2655 _(NAT_SET_MSS_CLAMPING, nat_set_mss_clamping)                           \
2656 _(NAT_GET_MSS_CLAMPING, nat_get_mss_clamping)                           \
2657 _(NAT_HA_SET_LISTENER, nat_ha_set_listener)                             \
2658 _(NAT_HA_SET_FAILOVER, nat_ha_set_failover)                             \
2659 _(NAT_HA_GET_LISTENER, nat_ha_get_listener)                             \
2660 _(NAT_HA_GET_FAILOVER, nat_ha_get_failover)                             \
2661 _(NAT_HA_FLUSH, nat_ha_flush)                                           \
2662 _(NAT_HA_RESYNC, nat_ha_resync)                                         \
2663 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range)             \
2664 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature)     \
2665 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping)           \
2666 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping)       \
2667 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump)                 \
2668 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump)             \
2669 _(NAT44_ADDRESS_DUMP, nat44_address_dump)                               \
2670 _(NAT44_INTERFACE_DUMP, nat44_interface_dump)                           \
2671 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr)           \
2672 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump)                 \
2673 _(NAT44_USER_DUMP, nat44_user_dump)                                     \
2674 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump)                     \
2675 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE,                               \
2676   nat44_interface_add_del_output_feature)                               \
2677 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP,                                  \
2678   nat44_interface_output_feature_dump)                                  \
2679 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping)     \
2680 _(NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL,                                \
2681   nat44_lb_static_mapping_add_del_local)                                \
2682 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump)           \
2683 _(NAT44_DEL_SESSION, nat44_del_session)                                 \
2684 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable)     \
2685 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled)             \
2686 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range)         \
2687 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump)                           \
2688 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface)                     \
2689 _(NAT64_INTERFACE_DUMP, nat64_interface_dump)                           \
2690 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib)                   \
2691 _(NAT64_BIB_DUMP, nat64_bib_dump)                                       \
2692 _(NAT64_ST_DUMP, nat64_st_dump)                                         \
2693 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix)                           \
2694 _(NAT64_PREFIX_DUMP, nat64_prefix_dump)                                 \
2695 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr)
2696
2697 /* Set up the API message handling tables */
2698 static clib_error_t *
2699 snat_plugin_api_hookup (vlib_main_t * vm)
2700 {
2701   snat_main_t *sm __attribute__ ((unused)) = &snat_main;
2702 #define _(N,n)                                                  \
2703     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
2704                            #n,                                  \
2705                            vl_api_##n##_t_handler,              \
2706                            vl_noop_handler,                     \
2707                            vl_api_##n##_t_endian,               \
2708                            vl_api_##n##_t_print,                \
2709                            sizeof(vl_api_##n##_t), 1);
2710   foreach_snat_plugin_api_msg;
2711 #undef _
2712
2713   return 0;
2714 }
2715
2716 #define vl_msg_name_crc_list
2717 #include <nat/nat_all_api_h.h>
2718 #undef vl_msg_name_crc_list
2719
2720 static void
2721 setup_message_id_table (snat_main_t * sm, api_main_t * am)
2722 {
2723 #define _(id,n,crc) \
2724   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
2725   foreach_vl_msg_name_crc_nat;
2726 #undef _
2727 }
2728
2729 static void
2730 plugin_custom_dump_configure (snat_main_t * sm)
2731 {
2732 #define _(n,f) sm->api_main->msg_print_handlers \
2733   [VL_API_##n + sm->msg_id_base]                \
2734     = (void *) vl_api_##f##_t_print;
2735   foreach_snat_plugin_api_msg;
2736 #undef _
2737 }
2738
2739 clib_error_t *
2740 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
2741 {
2742   u8 *name;
2743   clib_error_t *error = 0;
2744
2745   name = format (0, "nat_%08x%c", api_version, 0);
2746
2747   /* Ask for a correctly-sized block of API message decode slots */
2748   sm->msg_id_base =
2749     vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
2750
2751   error = snat_plugin_api_hookup (vm);
2752
2753   /* Add our API messages to the global name_crc hash table */
2754   setup_message_id_table (sm, sm->api_main);
2755
2756   plugin_custom_dump_configure (sm);
2757
2758   vec_free (name);
2759
2760   return error;
2761 }
2762
2763 /*
2764  * fd.io coding-style-patch-verification: ON
2765  *
2766  * Local Variables:
2767  * eval: (c-set-style "gnu")
2768  * End:
2769  */