nat: ipfix logging separation & refactor
[vpp.git] / src / plugins / nat / nat_api.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /**
17  * @file
18  * @brief NAT plugin API implementation
19  */
20
21 #include <nat/nat.h>
22 #include <nat/nat_inlines.h>
23 #include <nat/nat44/inlines.h>
24 #include <nat/lib/nat_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/lib/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   int rv = 0;
100
101   /* *INDENT-OFF* */
102   REPLY_MACRO2 (VL_API_NAT_SHOW_CONFIG_REPLY,
103   ({
104     rmp->translation_buckets = htonl (sm->translation_buckets);
105     rmp->translation_memory_size = clib_host_to_net_u32 (
106                     sm->translation_memory_size > 0xffffffffULL
107                     ? 0xffffffffUL
108                     : (u32)sm->translation_memory_size);
109     rmp->user_buckets = htonl (sm->user_buckets);
110     rmp->user_memory_size = clib_host_to_net_u64 (sm->user_memory_size);
111     rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
112     rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
113     rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
114     rmp->static_mapping_only = sm->static_mapping_only;
115     rmp->static_mapping_connection_tracking =
116       sm->static_mapping_connection_tracking;
117     rmp->endpoint_dependent = sm->endpoint_dependent;
118     rmp->out2in_dpo = sm->out2in_dpo;
119     // these are obsolete
120     rmp->dslite_ce = 0;
121     rmp->deterministic = 0;
122     rmp->nat64_bib_buckets = 0;
123     rmp->nat64_bib_memory_size = 0;
124     rmp->nat64_st_buckets = 0;
125     rmp->nat64_st_memory_size = 0;
126   }));
127   /* *INDENT-ON* */
128 }
129
130 static void *
131 vl_api_nat_show_config_t_print (vl_api_nat_show_config_t * mp, void *handle)
132 {
133   u8 *s;
134
135   s = format (0, "SCRIPT: nat_show_config ");
136
137   FINISH;
138 }
139
140 static void
141 vl_api_nat_show_config_2_t_handler (vl_api_nat_show_config_2_t * mp)
142 {
143   vl_api_nat_show_config_2_reply_t *rmp;
144   snat_main_t *sm = &snat_main;
145   int rv = 0;
146
147   /* *INDENT-OFF* */
148   REPLY_MACRO2 (VL_API_NAT_SHOW_CONFIG_2_REPLY,
149   ({
150     rmp->translation_buckets = htonl (sm->translation_buckets);
151     rmp->translation_memory_size = clib_host_to_net_u64 (sm->translation_memory_size);
152     rmp->user_buckets = htonl (sm->user_buckets);
153     rmp->user_memory_size = clib_host_to_net_u64 (sm->user_memory_size);
154     rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
155     rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
156     rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
157     rmp->static_mapping_only = sm->static_mapping_only;
158     rmp->static_mapping_connection_tracking =
159       sm->static_mapping_connection_tracking;
160     rmp->endpoint_dependent = sm->endpoint_dependent;
161     rmp->out2in_dpo = sm->out2in_dpo;
162     rmp->max_translations_per_thread = clib_net_to_host_u32(sm->max_translations_per_thread);
163     rmp->max_users_per_thread = clib_net_to_host_u32(sm->max_users_per_thread);
164     // these are obsolete
165     rmp->dslite_ce = 0;
166     rmp->deterministic = 0;
167     rmp->nat64_bib_buckets = 0;
168     rmp->nat64_bib_memory_size = 0;
169     rmp->nat64_st_buckets = 0;
170     rmp->nat64_st_memory_size = 0;
171   }));
172   /* *INDENT-ON* */
173 }
174
175 static void *
176 vl_api_nat_show_config_2_t_print (vl_api_nat_show_config_t * mp, void *handle)
177 {
178   u8 *s;
179
180   s = format (0, "SCRIPT: nat_show_config_2 ");
181
182   FINISH;
183 }
184
185 static void
186 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
187 {
188   snat_main_t *sm = &snat_main;
189   vl_api_nat_set_workers_reply_t *rmp;
190   int rv = 0;
191   uword *bitmap = 0;
192   u64 mask;
193
194   mask = clib_net_to_host_u64 (mp->worker_mask);
195
196   if (sm->num_workers < 2)
197     {
198       rv = VNET_API_ERROR_FEATURE_DISABLED;
199       goto send_reply;
200     }
201
202   bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
203   rv = snat_set_workers (bitmap);
204   clib_bitmap_free (bitmap);
205
206 send_reply:
207   REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
208 }
209
210 static void *
211 vl_api_nat_set_workers_t_print (vl_api_nat_set_workers_t * mp, void *handle)
212 {
213   u8 *s;
214   uword *bitmap = 0;
215   u8 first = 1;
216   int i;
217   u64 mask = clib_net_to_host_u64 (mp->worker_mask);
218
219   s = format (0, "SCRIPT: nat_set_workers ");
220   bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
221   /* *INDENT-OFF* */
222   clib_bitmap_foreach (i, bitmap,
223     ({
224       if (first)
225         s = format (s, "%d", i);
226       else
227         s = format (s, ",%d", i);
228       first = 0;
229     }));
230   /* *INDENT-ON* */
231   clib_bitmap_free (bitmap);
232   FINISH;
233 }
234
235 static void
236 send_nat_worker_details (u32 worker_index, vl_api_registration_t * reg,
237                          u32 context)
238 {
239   vl_api_nat_worker_details_t *rmp;
240   snat_main_t *sm = &snat_main;
241   vlib_worker_thread_t *w =
242     vlib_worker_threads + worker_index + sm->first_worker_index;
243
244   rmp = vl_msg_api_alloc (sizeof (*rmp));
245   clib_memset (rmp, 0, sizeof (*rmp));
246   rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
247   rmp->context = context;
248   rmp->worker_index = htonl (worker_index);
249   rmp->lcore_id = htonl (w->cpu_id);
250   strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
251
252   vl_api_send_msg (reg, (u8 *) rmp);
253 }
254
255 static void
256 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
257 {
258   vl_api_registration_t *reg;
259   snat_main_t *sm = &snat_main;
260   u32 *worker_index;
261
262   reg = vl_api_client_index_to_registration (mp->client_index);
263   if (!reg)
264     return;
265
266   /* *INDENT-OFF* */
267   vec_foreach (worker_index, sm->workers)
268     send_nat_worker_details(*worker_index, reg, mp->context);
269   /* *INDENT-ON* */
270 }
271
272 static void *
273 vl_api_nat_worker_dump_t_print (vl_api_nat_worker_dump_t * mp, void *handle)
274 {
275   u8 *s;
276
277   s = format (0, "SCRIPT: nat_worker_dump ");
278
279   FINISH;
280 }
281
282 static void
283 vl_api_nat44_set_session_limit_t_handler (vl_api_nat44_set_session_limit_t *
284                                           mp)
285 {
286   snat_main_t *sm = &snat_main;
287   vl_api_nat44_set_session_limit_reply_t *rmp;
288   int rv = 0;
289
290   rv = nat44_set_session_limit
291     (ntohl (mp->session_limit), ntohl (mp->vrf_id));
292
293   REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
294 }
295
296 static void *
297 vl_api_nat44_set_session_limit_t_print (vl_api_nat44_set_session_limit_t *
298                                         mp, void *handle)
299 {
300   u8 *s;
301
302   s = format (0, "SCRIPT: nat44_set_session_limit ");
303   s = format (s, "session_limit %d", ntohl (mp->session_limit));
304   s = format (s, "vrf_id %d", ntohl (mp->vrf_id));
305
306   FINISH;
307 }
308
309 static void
310 vl_api_nat_set_log_level_t_handler (vl_api_nat_set_log_level_t * mp)
311 {
312   snat_main_t *sm = &snat_main;
313   vl_api_nat_set_log_level_reply_t *rmp;
314   int rv = 0;
315
316   if (sm->log_level > NAT_LOG_DEBUG)
317     rv = VNET_API_ERROR_UNSUPPORTED;
318   else
319     sm->log_level = mp->log_level;
320
321   REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
322 }
323
324 static void *
325 vl_api_nat_set_log_level_t_print (vl_api_nat_set_log_level_t *
326                                   mp, void *handle)
327 {
328   u8 *s;
329
330   s = format (0, "SCRIPT: nat_set_log_level ");
331   s = format (s, "log_level %d", mp->log_level);
332
333   FINISH;
334 }
335
336 static void
337 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
338                                            mp)
339 {
340   snat_main_t *sm = &snat_main;
341   vl_api_nat_ipfix_enable_disable_reply_t *rmp;
342   int rv = 0;
343
344   rv = nat_ipfix_logging_enable_disable (mp->enable,
345                                          clib_host_to_net_u32
346                                          (mp->domain_id),
347                                          clib_host_to_net_u16 (mp->src_port));
348
349   REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
350 }
351
352 static void *
353 vl_api_nat_ipfix_enable_disable_t_print (vl_api_nat_ipfix_enable_disable_t *
354                                          mp, void *handle)
355 {
356   u8 *s;
357
358   s = format (0, "SCRIPT: nat_ipfix_enable_disable ");
359   if (mp->domain_id)
360     s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
361   if (mp->src_port)
362     s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
363   if (!mp->enable)
364     s = format (s, "disable ");
365
366   FINISH;
367 }
368
369 static void
370 vl_api_nat_set_timeouts_t_handler (vl_api_nat_set_timeouts_t * mp)
371 {
372   snat_main_t *sm = &snat_main;
373   vl_api_nat_set_timeouts_reply_t *rmp;
374   int rv = 0;
375
376   sm->udp_timeout = ntohl (mp->udp);
377   sm->tcp_established_timeout = ntohl (mp->tcp_established);
378   sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
379   sm->icmp_timeout = ntohl (mp->icmp);
380
381   REPLY_MACRO (VL_API_NAT_SET_TIMEOUTS_REPLY);
382 }
383
384 static void *
385 vl_api_nat_set_timeouts_t_print (vl_api_nat_set_timeouts_t * mp, void *handle)
386 {
387   u8 *s;
388
389   s = format (0, "SCRIPT: nat_set_timeouts ");
390   s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
391               ntohl (mp->udp),
392               ntohl (mp->tcp_established),
393               ntohl (mp->tcp_transitory), ntohl (mp->icmp));
394
395   FINISH;
396 }
397
398 static void
399 vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp)
400 {
401   snat_main_t *sm = &snat_main;
402   vl_api_nat_get_timeouts_reply_t *rmp;
403   int rv = 0;
404
405   /* *INDENT-OFF* */
406   REPLY_MACRO2 (VL_API_NAT_GET_TIMEOUTS_REPLY,
407   ({
408     rmp->udp = htonl (sm->udp_timeout);
409     rmp->tcp_established = htonl (sm->tcp_established_timeout);
410     rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
411     rmp->icmp = htonl (sm->icmp_timeout);
412   }))
413   /* *INDENT-ON* */
414 }
415
416 static void *
417 vl_api_nat_get_timeouts_t_print (vl_api_nat_get_timeouts_t * mp, void *handle)
418 {
419   u8 *s;
420
421   s = format (0, "SCRIPT: nat_get_timeouts");
422
423   FINISH;
424 }
425
426 static void
427   vl_api_nat_set_addr_and_port_alloc_alg_t_handler
428   (vl_api_nat_set_addr_and_port_alloc_alg_t * mp)
429 {
430   snat_main_t *sm = &snat_main;
431   vl_api_nat_set_addr_and_port_alloc_alg_reply_t *rmp;
432   int rv = 0;
433   u16 port_start, port_end;
434
435   switch (mp->alg)
436     {
437     case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
438       nat_set_alloc_addr_and_port_default ();
439       break;
440     case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
441       nat_set_alloc_addr_and_port_mape (ntohs (mp->psid), mp->psid_offset,
442                                         mp->psid_length);
443       break;
444     case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
445       port_start = ntohs (mp->start_port);
446       port_end = ntohs (mp->end_port);
447       if (port_end <= port_start)
448         {
449           rv = VNET_API_ERROR_INVALID_VALUE;
450           goto send_reply;
451         }
452       nat_set_alloc_addr_and_port_range (port_start, port_end);
453       break;
454     default:
455       rv = VNET_API_ERROR_INVALID_VALUE;
456       break;
457     }
458
459 send_reply:
460   REPLY_MACRO (VL_API_NAT_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
461 }
462
463 static void *vl_api_nat_set_addr_and_port_alloc_alg_t_print
464   (vl_api_nat_set_addr_and_port_alloc_alg_t * mp, void *handle)
465 {
466   u8 *s;
467
468   s = format (0, "SCRIPT: nat_set_addr_and_port_alloc_alg ");
469   s = format (s, "alg %d psid_offset %d psid_length %d psid %d start_port %d "
470               "end_port %d\n",
471               ntohl (mp->alg), ntohl (mp->psid_offset),
472               ntohl (mp->psid_length), ntohs (mp->psid),
473               ntohs (mp->start_port), ntohs (mp->end_port));
474
475   FINISH;
476 }
477
478 static void
479   vl_api_nat_get_addr_and_port_alloc_alg_t_handler
480   (vl_api_nat_get_addr_and_port_alloc_alg_t * mp)
481 {
482   snat_main_t *sm = &snat_main;
483   vl_api_nat_get_addr_and_port_alloc_alg_reply_t *rmp;
484   int rv = 0;
485
486   /* *INDENT-OFF* */
487   REPLY_MACRO2 (VL_API_NAT_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY,
488   ({
489     rmp->alg = sm->addr_and_port_alloc_alg;
490     rmp->psid_offset = sm->psid_offset;
491     rmp->psid_length = sm->psid_length;
492     rmp->psid = htons (sm->psid);
493     rmp->start_port = htons (sm->start_port);
494     rmp->end_port = htons (sm->end_port);
495   }))
496   /* *INDENT-ON* */
497 }
498
499 static void *vl_api_nat_get_addr_and_port_alloc_alg_t_print
500   (vl_api_nat_get_addr_and_port_alloc_alg_t * mp, void *handle)
501 {
502   u8 *s;
503
504   s = format (0, "SCRIPT: nat_get_addr_and_port_alloc_alg");
505
506   FINISH;
507 }
508
509 static void
510 vl_api_nat_set_mss_clamping_t_handler (vl_api_nat_set_mss_clamping_t * mp)
511 {
512   snat_main_t *sm = &snat_main;
513   vl_api_nat_set_mss_clamping_reply_t *rmp;
514   int rv = 0;
515
516   if (mp->enable)
517     sm->mss_clamping = ntohs (mp->mss_value);
518   else
519     sm->mss_clamping = 0;
520
521   REPLY_MACRO (VL_API_NAT_SET_MSS_CLAMPING_REPLY);
522 }
523
524 static void *
525 vl_api_nat_set_mss_clamping_t_print (vl_api_nat_set_mss_clamping_t * mp,
526                                      void *handle)
527 {
528   u8 *s;
529
530   s = format (0, "SCRIPT: nat_set_mss_clamping enable %d mss_value %d\n",
531               mp->enable, ntohs (mp->mss_value));
532
533   FINISH;
534 }
535
536 static void
537 vl_api_nat_get_mss_clamping_t_handler (vl_api_nat_get_mss_clamping_t * mp)
538 {
539   snat_main_t *sm = &snat_main;
540   vl_api_nat_get_mss_clamping_reply_t *rmp;
541   int rv = 0;
542
543   /* *INDENT-OFF* */
544   REPLY_MACRO2 (VL_API_NAT_GET_MSS_CLAMPING_REPLY,
545   ({
546     rmp->enable = sm->mss_clamping ? 1 : 0;
547     rmp->mss_value = htons (sm->mss_clamping);
548   }))
549   /* *INDENT-ON* */
550 }
551
552 static void *
553 vl_api_nat_get_mss_clamping_t_print (vl_api_nat_get_mss_clamping_t * mp,
554                                      void *handle)
555 {
556   u8 *s;
557
558   s = format (0, "SCRIPT: nat_get_mss_clamping");
559
560   FINISH;
561 }
562
563 static void
564 vl_api_nat_ha_set_listener_t_handler (vl_api_nat_ha_set_listener_t * mp)
565 {
566   snat_main_t *sm = &snat_main;
567   vl_api_nat_ha_set_listener_reply_t *rmp;
568   ip4_address_t addr;
569   int rv;
570
571   memcpy (&addr, &mp->ip_address, sizeof (addr));
572   rv =
573     nat_ha_set_listener (&addr, clib_net_to_host_u16 (mp->port),
574                          clib_net_to_host_u32 (mp->path_mtu));
575
576   REPLY_MACRO (VL_API_NAT_HA_SET_LISTENER_REPLY);
577 }
578
579 static void *
580 vl_api_nat_ha_set_listener_t_print (vl_api_nat_ha_set_listener_t * mp,
581                                     void *handle)
582 {
583   u8 *s;
584
585   s = format (0, "SCRIPT: nat_ha_set_listener ");
586   s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
587   s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
588   s = format (s, "path_mtu %d", clib_net_to_host_u32 (mp->path_mtu));
589
590   FINISH;
591 }
592
593 static void
594 vl_api_nat_ha_get_listener_t_handler (vl_api_nat_ha_get_listener_t * mp)
595 {
596   snat_main_t *sm = &snat_main;
597   vl_api_nat_ha_get_listener_reply_t *rmp;
598   int rv = 0;
599   ip4_address_t addr;
600   u16 port;
601   u32 path_mtu;
602
603   nat_ha_get_listener (&addr, &port, &path_mtu);
604
605   /* *INDENT-OFF* */
606   REPLY_MACRO2 (VL_API_NAT_HA_GET_LISTENER_REPLY,
607   ({
608     clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
609     rmp->port = clib_host_to_net_u16 (port);
610     rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
611   }))
612   /* *INDENT-ON* */
613 }
614
615 static void *
616 vl_api_nat_ha_get_listener_t_print (vl_api_nat_ha_get_listener_t * mp,
617                                     void *handle)
618 {
619   u8 *s;
620
621   s = format (0, "SCRIPT: nat_ha_get_listener");
622
623   FINISH;
624 }
625
626 static void
627 vl_api_nat_ha_set_failover_t_handler (vl_api_nat_ha_set_failover_t * mp)
628 {
629   snat_main_t *sm = &snat_main;
630   vl_api_nat_ha_set_failover_reply_t *rmp;
631   ip4_address_t addr;
632   int rv;
633
634   memcpy (&addr, &mp->ip_address, sizeof (addr));
635   rv =
636     nat_ha_set_failover (&addr, clib_net_to_host_u16 (mp->port),
637                          clib_net_to_host_u32 (mp->session_refresh_interval));
638
639   REPLY_MACRO (VL_API_NAT_HA_SET_FAILOVER_REPLY);
640 }
641
642 static void *
643 vl_api_nat_ha_set_failover_t_print (vl_api_nat_ha_set_failover_t * mp,
644                                     void *handle)
645 {
646   u8 *s;
647
648   s = format (0, "SCRIPT: nat_ha_set_failover ");
649   s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
650   s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
651
652   FINISH;
653 }
654
655 static void
656 vl_api_nat_ha_get_failover_t_handler (vl_api_nat_ha_get_failover_t * mp)
657 {
658   snat_main_t *sm = &snat_main;
659   vl_api_nat_ha_get_failover_reply_t *rmp;
660   int rv = 0;
661   ip4_address_t addr;
662   u16 port;
663   u32 session_refresh_interval;
664
665   nat_ha_get_failover (&addr, &port, &session_refresh_interval);
666
667   /* *INDENT-OFF* */
668   REPLY_MACRO2 (VL_API_NAT_HA_GET_FAILOVER_REPLY,
669   ({
670     clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
671     rmp->port = clib_host_to_net_u16 (port);
672     rmp->session_refresh_interval = clib_host_to_net_u32 (session_refresh_interval);
673   }))
674   /* *INDENT-ON* */
675 }
676
677 static void *
678 vl_api_nat_ha_get_failover_t_print (vl_api_nat_ha_get_failover_t * mp,
679                                     void *handle)
680 {
681   u8 *s;
682
683   s = format (0, "SCRIPT: nat_ha_get_failover");
684
685   FINISH;
686 }
687
688 static void
689 vl_api_nat_ha_flush_t_handler (vl_api_nat_ha_flush_t * mp)
690 {
691   snat_main_t *sm = &snat_main;
692   vl_api_nat_ha_flush_reply_t *rmp;
693   int rv = 0;
694
695   nat_ha_flush (0);
696
697   REPLY_MACRO (VL_API_NAT_HA_FLUSH_REPLY);
698 }
699
700 static void *
701 vl_api_nat_ha_flush_t_print (vl_api_nat_ha_flush_t * mp, void *handle)
702 {
703   u8 *s;
704
705   s = format (0, "SCRIPT: nat_ha_flush ");
706
707   FINISH;
708 }
709
710 static void
711 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
712 {
713   snat_main_t *sm = &snat_main;
714   vl_api_registration_t *reg;
715   vl_api_nat_ha_resync_completed_event_t *mp;
716
717   reg = vl_api_client_index_to_registration (client_index);
718   if (!reg)
719     return;
720
721   mp = vl_msg_api_alloc (sizeof (*mp));
722   clib_memset (mp, 0, sizeof (*mp));
723   mp->client_index = client_index;
724   mp->pid = pid;
725   mp->missed_count = clib_host_to_net_u32 (missed_count);
726   mp->_vl_msg_id =
727     ntohs (VL_API_NAT_HA_RESYNC_COMPLETED_EVENT + sm->msg_id_base);
728
729   vl_api_send_msg (reg, (u8 *) mp);
730 }
731
732 static void
733 vl_api_nat_ha_resync_t_handler (vl_api_nat_ha_resync_t * mp)
734 {
735   snat_main_t *sm = &snat_main;
736   vl_api_nat_ha_resync_reply_t *rmp;
737   int rv;
738
739   rv =
740     nat_ha_resync (mp->client_index, mp->pid,
741                    mp->want_resync_event ? nat_ha_resync_completed_event_cb :
742                    NULL);
743
744   REPLY_MACRO (VL_API_NAT_HA_RESYNC_REPLY);
745 }
746
747 static void *
748 vl_api_nat_ha_resync_t_print (vl_api_nat_ha_resync_t * mp, void *handle)
749 {
750   u8 *s;
751
752   s = format (0, "SCRIPT: nat_ha_resync ");
753   s =
754     format (s, "want_resync_event %d pid %d", mp->want_resync_event,
755             clib_host_to_net_u32 (mp->pid));
756
757   FINISH;
758 }
759
760 /*************/
761 /*** NAT44 ***/
762 /*************/
763 static void
764 vl_api_nat44_del_user_t_handler (vl_api_nat44_del_user_t * mp)
765 {
766   snat_main_t *sm = &snat_main;
767   vl_api_nat44_del_user_reply_t *rmp;
768   ip4_address_t addr;
769   int rv;
770   memcpy (&addr.as_u8, mp->ip_address, 4);
771   rv = nat44_user_del (&addr, ntohl (mp->fib_index));
772   REPLY_MACRO (VL_API_NAT44_DEL_USER_REPLY);
773 }
774
775 static void *vl_api_nat44_del_user_t_print
776   (vl_api_nat44_del_user_t * mp, void *handle)
777 {
778   u8 *s;
779   s = format (0, "SCRIPT: nat44_del_user ");
780   s = format (s, "ip_address %U fib_index %U ",
781               format_ip4_address, mp->ip_address, ntohl (mp->fib_index));
782   FINISH;
783 }
784
785 static void
786   vl_api_nat44_add_del_address_range_t_handler
787   (vl_api_nat44_add_del_address_range_t * mp)
788 {
789   snat_main_t *sm = &snat_main;
790   vl_api_nat44_add_del_address_range_reply_t *rmp;
791   ip4_address_t this_addr;
792   u8 is_add, twice_nat;
793   u32 start_host_order, end_host_order;
794   u32 vrf_id;
795   int i, count;
796   int rv = 0;
797   u32 *tmp;
798
799   if (sm->static_mapping_only)
800     {
801       rv = VNET_API_ERROR_FEATURE_DISABLED;
802       goto send_reply;
803     }
804
805   is_add = mp->is_add;
806   twice_nat = mp->flags & NAT_API_IS_TWICE_NAT;
807
808   tmp = (u32 *) mp->first_ip_address;
809   start_host_order = clib_host_to_net_u32 (tmp[0]);
810   tmp = (u32 *) mp->last_ip_address;
811   end_host_order = clib_host_to_net_u32 (tmp[0]);
812
813   count = (end_host_order - start_host_order) + 1;
814
815   vrf_id = clib_host_to_net_u32 (mp->vrf_id);
816
817   if (count > 1024)
818     nat_log_info ("%U - %U, %d addresses...",
819                   format_ip4_address, mp->first_ip_address,
820                   format_ip4_address, mp->last_ip_address, count);
821
822   memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
823
824   for (i = 0; i < count; i++)
825     {
826       if (is_add)
827         rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
828       else
829         rv = snat_del_address (sm, this_addr, 0, twice_nat);
830
831       if (rv)
832         goto send_reply;
833
834       if (sm->out2in_dpo)
835         nat44_add_del_address_dpo (this_addr, is_add);
836
837       increment_v4_address (&this_addr);
838     }
839
840 send_reply:
841   REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
842 }
843
844 static void *vl_api_nat44_add_del_address_range_t_print
845   (vl_api_nat44_add_del_address_range_t * mp, void *handle)
846 {
847   u8 *s;
848
849   s = format (0, "SCRIPT: nat44_add_address_range ");
850   s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
851   if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
852     {
853       s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
854     }
855   s = format (s, "twice_nat %d ", mp->flags & NAT_API_IS_TWICE_NAT);
856   FINISH;
857 }
858
859 static void
860 send_nat44_address_details (snat_address_t * a,
861                             vl_api_registration_t * reg, u32 context,
862                             u8 twice_nat)
863 {
864   vl_api_nat44_address_details_t *rmp;
865   snat_main_t *sm = &snat_main;
866
867   rmp = vl_msg_api_alloc (sizeof (*rmp));
868   clib_memset (rmp, 0, sizeof (*rmp));
869   rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
870   clib_memcpy (rmp->ip_address, &(a->addr), 4);
871   if (a->fib_index != ~0)
872     {
873       fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
874       rmp->vrf_id = ntohl (fib->ft_table_id);
875     }
876   else
877     rmp->vrf_id = ~0;
878   if (twice_nat)
879     rmp->flags |= NAT_API_IS_TWICE_NAT;
880   rmp->context = context;
881
882   vl_api_send_msg (reg, (u8 *) rmp);
883 }
884
885 static void
886 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
887 {
888   vl_api_registration_t *reg;
889   snat_main_t *sm = &snat_main;
890   snat_address_t *a;
891
892   reg = vl_api_client_index_to_registration (mp->client_index);
893   if (!reg)
894     return;
895
896   /* *INDENT-OFF* */
897   vec_foreach (a, sm->addresses)
898     send_nat44_address_details (a, reg, mp->context, 0);
899   vec_foreach (a, sm->twice_nat_addresses)
900     send_nat44_address_details (a, reg, mp->context, 1);
901   /* *INDENT-ON* */
902 }
903
904 static void *
905 vl_api_nat44_address_dump_t_print (vl_api_nat44_address_dump_t * mp,
906                                    void *handle)
907 {
908   u8 *s;
909
910   s = format (0, "SCRIPT: nat44_address_dump ");
911
912   FINISH;
913 }
914
915 static void
916   vl_api_nat44_interface_add_del_feature_t_handler
917   (vl_api_nat44_interface_add_del_feature_t * mp)
918 {
919   snat_main_t *sm = &snat_main;
920   vl_api_nat44_interface_add_del_feature_reply_t *rmp;
921   u32 sw_if_index = ntohl (mp->sw_if_index);
922   u8 is_del;
923   int rv = 0;
924
925   is_del = !mp->is_add;
926
927   VALIDATE_SW_IF_INDEX (mp);
928
929   rv =
930     snat_interface_add_del (sw_if_index, mp->flags & NAT_API_IS_INSIDE,
931                             is_del);
932
933   BAD_SW_IF_INDEX_LABEL;
934
935   REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
936 }
937
938 static void *vl_api_nat44_interface_add_del_feature_t_print
939   (vl_api_nat44_interface_add_del_feature_t * mp, void *handle)
940 {
941   u8 *s;
942
943   s = format (0, "SCRIPT: nat44_interface_add_del_feature ");
944   s = format (s, "sw_if_index %d %s %s",
945               clib_host_to_net_u32 (mp->sw_if_index),
946               mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
947               mp->is_add ? "" : "del");
948
949   FINISH;
950 }
951
952 static void
953 send_nat44_interface_details (snat_interface_t * i,
954                               vl_api_registration_t * reg, u32 context)
955 {
956   vl_api_nat44_interface_details_t *rmp;
957   snat_main_t *sm = &snat_main;
958
959   rmp = vl_msg_api_alloc (sizeof (*rmp));
960   clib_memset (rmp, 0, sizeof (*rmp));
961   rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
962   rmp->sw_if_index = ntohl (i->sw_if_index);
963
964   if (nat_interface_is_inside (i))
965     rmp->flags |= NAT_API_IS_INSIDE;
966   if (nat_interface_is_outside (i))
967     rmp->flags |= NAT_API_IS_OUTSIDE;
968
969   rmp->context = context;
970
971   vl_api_send_msg (reg, (u8 *) rmp);
972 }
973
974 static void
975 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
976 {
977   vl_api_registration_t *reg;
978   snat_main_t *sm = &snat_main;
979   snat_interface_t *i;
980
981   reg = vl_api_client_index_to_registration (mp->client_index);
982   if (!reg)
983     return;
984
985   /* *INDENT-OFF* */
986   pool_foreach (i, sm->interfaces,
987   ({
988     send_nat44_interface_details(i, reg, mp->context);
989   }));
990   /* *INDENT-ON* */
991 }
992
993 static void *
994 vl_api_nat44_interface_dump_t_print (vl_api_nat44_interface_dump_t * mp,
995                                      void *handle)
996 {
997   u8 *s;
998
999   s = format (0, "SCRIPT: nat44_interface_dump ");
1000
1001   FINISH;
1002 }
1003
1004 static void
1005   vl_api_nat44_interface_add_del_output_feature_t_handler
1006   (vl_api_nat44_interface_add_del_output_feature_t * mp)
1007 {
1008   snat_main_t *sm = &snat_main;
1009   vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
1010   u32 sw_if_index = ntohl (mp->sw_if_index);
1011   int rv = 0;
1012
1013   VALIDATE_SW_IF_INDEX (mp);
1014
1015   rv = snat_interface_add_del_output_feature (sw_if_index,
1016                                               mp->flags & NAT_API_IS_INSIDE,
1017                                               !mp->is_add);
1018
1019   BAD_SW_IF_INDEX_LABEL;
1020   REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
1021 }
1022
1023 static void *vl_api_nat44_interface_add_del_output_feature_t_print
1024   (vl_api_nat44_interface_add_del_output_feature_t * mp, void *handle)
1025 {
1026   u8 *s;
1027
1028   s = format (0, "SCRIPT: nat44_interface_add_del_output_feature ");
1029   s = format (s, "sw_if_index %d %s %s",
1030               clib_host_to_net_u32 (mp->sw_if_index),
1031               mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
1032               mp->is_add ? "" : "del");
1033
1034   FINISH;
1035 }
1036
1037 static void
1038 send_nat44_interface_output_feature_details (snat_interface_t * i,
1039                                              vl_api_registration_t * reg,
1040                                              u32 context)
1041 {
1042   vl_api_nat44_interface_output_feature_details_t *rmp;
1043   snat_main_t *sm = &snat_main;
1044
1045   rmp = vl_msg_api_alloc (sizeof (*rmp));
1046   clib_memset (rmp, 0, sizeof (*rmp));
1047   rmp->_vl_msg_id =
1048     ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
1049   rmp->sw_if_index = ntohl (i->sw_if_index);
1050   rmp->context = context;
1051
1052   if (nat_interface_is_inside (i))
1053     rmp->flags |= NAT_API_IS_INSIDE;
1054
1055   vl_api_send_msg (reg, (u8 *) rmp);
1056 }
1057
1058 static void
1059   vl_api_nat44_interface_output_feature_dump_t_handler
1060   (vl_api_nat44_interface_output_feature_dump_t * mp)
1061 {
1062   vl_api_registration_t *reg;
1063   snat_main_t *sm = &snat_main;
1064   snat_interface_t *i;
1065
1066   reg = vl_api_client_index_to_registration (mp->client_index);
1067   if (!reg)
1068     return;
1069
1070   /* *INDENT-OFF* */
1071   pool_foreach (i, sm->output_feature_interfaces,
1072   ({
1073     send_nat44_interface_output_feature_details(i, reg, mp->context);
1074   }));
1075   /* *INDENT-ON* */
1076 }
1077
1078 static void *vl_api_nat44_interface_output_feature_dump_t_print
1079   (vl_api_nat44_interface_output_feature_dump_t * mp, void *handle)
1080 {
1081   u8 *s;
1082
1083   s = format (0, "SCRIPT: nat44_interface_output_feature_dump ");
1084
1085   FINISH;
1086 }
1087
1088 static void
1089   vl_api_nat44_add_del_static_mapping_t_handler
1090   (vl_api_nat44_add_del_static_mapping_t * mp)
1091 {
1092   snat_main_t *sm = &snat_main;
1093   vl_api_nat44_add_del_static_mapping_reply_t *rmp;
1094   ip4_address_t local_addr, external_addr, pool_addr = { 0 };
1095   u16 local_port = 0, external_port = 0;
1096   u32 vrf_id, external_sw_if_index;
1097   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1098   int rv = 0;
1099   nat_protocol_t proto;
1100   u8 *tag = 0;
1101
1102   memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1103   memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1104
1105   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1106     {
1107       local_port = mp->local_port;
1108       external_port = mp->external_port;
1109     }
1110
1111   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1112   external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1113   proto = ip_proto_to_nat_proto (mp->protocol);
1114
1115   if (mp->flags & NAT_API_IS_TWICE_NAT)
1116     twice_nat = TWICE_NAT;
1117   else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1118     twice_nat = TWICE_NAT_SELF;
1119   mp->tag[sizeof (mp->tag) - 1] = 0;
1120   tag = format (0, "%s", mp->tag);
1121   vec_terminate_c_string (tag);
1122
1123   rv = snat_add_static_mapping (local_addr, external_addr, local_port,
1124                                 external_port, vrf_id,
1125                                 mp->flags & NAT_API_IS_ADDR_ONLY,
1126                                 external_sw_if_index, proto,
1127                                 mp->is_add, twice_nat,
1128                                 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0,
1129                                 pool_addr, 0);
1130   vec_free (tag);
1131
1132   REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
1133 }
1134
1135 static void
1136   vl_api_nat44_add_del_static_mapping_v2_t_handler
1137   (vl_api_nat44_add_del_static_mapping_v2_t * mp)
1138 {
1139   snat_main_t *sm = &snat_main;
1140   vl_api_nat44_add_del_static_mapping_v2_reply_t *rmp;
1141   ip4_address_t local_addr, external_addr, pool_addr;
1142   u16 local_port = 0, external_port = 0;
1143   u32 vrf_id, external_sw_if_index;
1144   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1145   int rv = 0;
1146   nat_protocol_t proto;
1147   u8 *tag = 0;
1148
1149   memcpy (&pool_addr.as_u8, mp->pool_ip_address, 4);
1150   memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1151   memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1152
1153   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1154     {
1155       local_port = mp->local_port;
1156       external_port = mp->external_port;
1157     }
1158
1159   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1160   external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1161   proto = ip_proto_to_nat_proto (mp->protocol);
1162
1163   if (mp->flags & NAT_API_IS_TWICE_NAT)
1164     twice_nat = TWICE_NAT;
1165   else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1166     twice_nat = TWICE_NAT_SELF;
1167   mp->tag[sizeof (mp->tag) - 1] = 0;
1168   tag = format (0, "%s", mp->tag);
1169   vec_terminate_c_string (tag);
1170
1171   rv = snat_add_static_mapping (local_addr, external_addr, local_port,
1172                                 external_port, vrf_id,
1173                                 mp->flags & NAT_API_IS_ADDR_ONLY,
1174                                 external_sw_if_index, proto,
1175                                 mp->is_add, twice_nat,
1176                                 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0,
1177                                 pool_addr, mp->match_pool);
1178   vec_free (tag);
1179
1180   REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_V2_REPLY);
1181 }
1182
1183 static void *vl_api_nat44_add_del_static_mapping_t_print
1184   (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
1185 {
1186   u8 *s;
1187
1188   s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1189   s = format (s, "protocol %d local_addr %U external_addr %U ",
1190               mp->protocol,
1191               format_ip4_address, mp->local_ip_address,
1192               format_ip4_address, mp->external_ip_address);
1193
1194   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1195     s = format (s, "local_port %d external_port %d ",
1196                 clib_net_to_host_u16 (mp->local_port),
1197                 clib_net_to_host_u16 (mp->external_port));
1198
1199   s = format (s, "twice_nat %d out2in_only %d ",
1200               mp->flags & NAT_API_IS_TWICE_NAT,
1201               mp->flags & NAT_API_IS_OUT2IN_ONLY);
1202
1203   if (mp->vrf_id != ~0)
1204     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1205
1206   if (mp->external_sw_if_index != ~0)
1207     s = format (s, "external_sw_if_index %d",
1208                 clib_net_to_host_u32 (mp->external_sw_if_index));
1209   FINISH;
1210 }
1211
1212 static void *vl_api_nat44_add_del_static_mapping_v2_t_print
1213   (vl_api_nat44_add_del_static_mapping_v2_t * mp, void *handle)
1214 {
1215   u8 *s;
1216
1217   s = format (0, "SCRIPT: nat44_add_del_static_mapping_v2 ");
1218   s = format (s, "protocol %d local_addr %U external_addr %U ",
1219               mp->protocol,
1220               format_ip4_address, mp->local_ip_address,
1221               format_ip4_address, mp->external_ip_address);
1222
1223   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1224     s = format (s, "local_port %d external_port %d ",
1225                 clib_net_to_host_u16 (mp->local_port),
1226                 clib_net_to_host_u16 (mp->external_port));
1227
1228   s = format (s, "twice_nat %d out2in_only %d ",
1229               mp->flags & NAT_API_IS_TWICE_NAT,
1230               mp->flags & NAT_API_IS_OUT2IN_ONLY);
1231
1232   if (mp->vrf_id != ~0)
1233     s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1234
1235   if (mp->external_sw_if_index != ~0)
1236     s = format (s, "external_sw_if_index %d",
1237                 clib_net_to_host_u32 (mp->external_sw_if_index));
1238   if (mp->match_pool)
1239     s = format (s, "match pool address %U",
1240                 format_ip4_address, mp->pool_ip_address);
1241
1242   FINISH;
1243 }
1244
1245 static void
1246 send_nat44_static_mapping_details (snat_static_mapping_t * m,
1247                                    vl_api_registration_t * reg, u32 context)
1248 {
1249   vl_api_nat44_static_mapping_details_t *rmp;
1250   snat_main_t *sm = &snat_main;
1251   u32 len = sizeof (*rmp);
1252
1253   rmp = vl_msg_api_alloc (len);
1254   clib_memset (rmp, 0, len);
1255   rmp->_vl_msg_id =
1256     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1257
1258   clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1259   clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1260   rmp->external_sw_if_index = ~0;
1261   rmp->vrf_id = htonl (m->vrf_id);
1262   rmp->context = context;
1263
1264   if (m->twice_nat == TWICE_NAT)
1265     rmp->flags |= NAT_API_IS_TWICE_NAT;
1266   else if (m->twice_nat == TWICE_NAT_SELF)
1267     rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1268
1269   if (is_out2in_only_static_mapping (m))
1270     rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1271
1272   if (is_addr_only_static_mapping (m))
1273     {
1274       rmp->flags |= NAT_API_IS_ADDR_ONLY;
1275     }
1276   else
1277     {
1278       rmp->protocol = nat_proto_to_ip_proto (m->proto);
1279       rmp->external_port = m->external_port;
1280       rmp->local_port = m->local_port;
1281     }
1282
1283   if (m->tag)
1284     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1285
1286   vl_api_send_msg (reg, (u8 *) rmp);
1287 }
1288
1289 static void
1290 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
1291                                        vl_api_registration_t * reg,
1292                                        u32 context)
1293 {
1294   vl_api_nat44_static_mapping_details_t *rmp;
1295   snat_main_t *sm = &snat_main;
1296
1297   rmp = vl_msg_api_alloc (sizeof (*rmp));
1298   clib_memset (rmp, 0, sizeof (*rmp));
1299   rmp->_vl_msg_id =
1300     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1301   clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1302   rmp->external_sw_if_index = htonl (m->sw_if_index);
1303   rmp->vrf_id = htonl (m->vrf_id);
1304   rmp->context = context;
1305
1306   if (m->twice_nat)
1307     rmp->flags |= NAT_API_IS_TWICE_NAT;
1308
1309   if (m->addr_only)
1310     {
1311       rmp->flags |= NAT_API_IS_ADDR_ONLY;
1312     }
1313   else
1314     {
1315       rmp->protocol = nat_proto_to_ip_proto (m->proto);
1316       rmp->external_port = m->e_port;
1317       rmp->local_port = m->l_port;
1318     }
1319   if (m->tag)
1320     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1321
1322   vl_api_send_msg (reg, (u8 *) rmp);
1323 }
1324
1325 static void
1326 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
1327                                             * mp)
1328 {
1329   vl_api_registration_t *reg;
1330   snat_main_t *sm = &snat_main;
1331   snat_static_mapping_t *m;
1332   snat_static_map_resolve_t *rp;
1333   int j;
1334
1335   reg = vl_api_client_index_to_registration (mp->client_index);
1336   if (!reg)
1337     return;
1338
1339   /* *INDENT-OFF* */
1340   pool_foreach (m, sm->static_mappings,
1341   ({
1342       if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1343         send_nat44_static_mapping_details (m, reg, mp->context);
1344   }));
1345   /* *INDENT-ON* */
1346
1347   for (j = 0; j < vec_len (sm->to_resolve); j++)
1348     {
1349       rp = sm->to_resolve + j;
1350       if (!rp->identity_nat)
1351         send_nat44_static_map_resolve_details (rp, reg, mp->context);
1352     }
1353 }
1354
1355 static void *
1356 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
1357                                           mp, void *handle)
1358 {
1359   u8 *s;
1360
1361   s = format (0, "SCRIPT: nat44_static_mapping_dump ");
1362
1363   FINISH;
1364 }
1365
1366 static void
1367   vl_api_nat44_add_del_identity_mapping_t_handler
1368   (vl_api_nat44_add_del_identity_mapping_t * mp)
1369 {
1370   snat_main_t *sm = &snat_main;
1371   vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
1372   ip4_address_t addr, pool_addr = { 0 };
1373   u16 port = 0;
1374   u32 vrf_id, sw_if_index;
1375   int rv = 0;
1376   nat_protocol_t proto = NAT_PROTOCOL_OTHER;
1377   u8 *tag = 0;
1378
1379   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1380     {
1381       port = mp->port;
1382       proto = ip_proto_to_nat_proto (mp->protocol);
1383     }
1384   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1385   sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1386   if (sw_if_index != ~0)
1387     addr.as_u32 = 0;
1388   else
1389     memcpy (&addr.as_u8, mp->ip_address, 4);
1390   mp->tag[sizeof (mp->tag) - 1] = 0;
1391   tag = format (0, "%s", mp->tag);
1392   vec_terminate_c_string (tag);
1393
1394   rv =
1395     snat_add_static_mapping (addr, addr, port, port, vrf_id,
1396                              mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1397                              proto, mp->is_add, 0, 0, tag, 1, pool_addr, 0);
1398   vec_free (tag);
1399
1400   REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1401 }
1402
1403 static void *vl_api_nat44_add_del_identity_mapping_t_print
1404   (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
1405 {
1406   u8 *s;
1407
1408   s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
1409   if (mp->sw_if_index != ~0)
1410     s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
1411   else
1412     s = format (s, "addr %U", format_ip4_address, mp->ip_address);
1413
1414   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1415     s =
1416       format (s, " protocol %d port %d", mp->protocol,
1417               clib_net_to_host_u16 (mp->port));
1418
1419   if (mp->vrf_id != ~0)
1420     s = format (s, " vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1421
1422   FINISH;
1423 }
1424
1425 static void
1426 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1427                                      vl_api_registration_t * reg, u32 context)
1428 {
1429   vl_api_nat44_identity_mapping_details_t *rmp;
1430   snat_main_t *sm = &snat_main;
1431   nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1432
1433   rmp = vl_msg_api_alloc (sizeof (*rmp));
1434   clib_memset (rmp, 0, sizeof (*rmp));
1435   rmp->_vl_msg_id =
1436     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1437
1438   if (is_addr_only_static_mapping (m))
1439     rmp->flags |= NAT_API_IS_ADDR_ONLY;
1440
1441   clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1442   rmp->port = m->local_port;
1443   rmp->sw_if_index = ~0;
1444   rmp->vrf_id = htonl (local->vrf_id);
1445   rmp->protocol = nat_proto_to_ip_proto (m->proto);
1446   rmp->context = context;
1447   if (m->tag)
1448     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1449
1450   vl_api_send_msg (reg, (u8 *) rmp);
1451 }
1452
1453 static void
1454 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1455                                          vl_api_registration_t * reg,
1456                                          u32 context)
1457 {
1458   vl_api_nat44_identity_mapping_details_t *rmp;
1459   snat_main_t *sm = &snat_main;
1460
1461   rmp = vl_msg_api_alloc (sizeof (*rmp));
1462   clib_memset (rmp, 0, sizeof (*rmp));
1463   rmp->_vl_msg_id =
1464     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1465
1466   if (m->addr_only)
1467     rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1468
1469   rmp->port = m->l_port;
1470   rmp->sw_if_index = htonl (m->sw_if_index);
1471   rmp->vrf_id = htonl (m->vrf_id);
1472   rmp->protocol = nat_proto_to_ip_proto (m->proto);
1473   rmp->context = context;
1474   if (m->tag)
1475     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1476
1477   vl_api_send_msg (reg, (u8 *) rmp);
1478 }
1479
1480 static void
1481   vl_api_nat44_identity_mapping_dump_t_handler
1482   (vl_api_nat44_identity_mapping_dump_t * mp)
1483 {
1484   vl_api_registration_t *reg;
1485   snat_main_t *sm = &snat_main;
1486   snat_static_mapping_t *m;
1487   snat_static_map_resolve_t *rp;
1488   int j;
1489
1490   reg = vl_api_client_index_to_registration (mp->client_index);
1491   if (!reg)
1492     return;
1493
1494   /* *INDENT-OFF* */
1495   pool_foreach (m, sm->static_mappings,
1496   ({
1497       if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1498         {
1499           pool_foreach_index (j, m->locals,
1500           ({
1501             send_nat44_identity_mapping_details (m, j, reg, mp->context);
1502           }));
1503         }
1504   }));
1505   /* *INDENT-ON* */
1506
1507   for (j = 0; j < vec_len (sm->to_resolve); j++)
1508     {
1509       rp = sm->to_resolve + j;
1510       if (rp->identity_nat)
1511         send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1512     }
1513 }
1514
1515 static void *vl_api_nat44_identity_mapping_dump_t_print
1516   (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1517 {
1518   u8 *s;
1519
1520   s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1521
1522   FINISH;
1523 }
1524
1525 static void
1526   vl_api_nat44_add_del_interface_addr_t_handler
1527   (vl_api_nat44_add_del_interface_addr_t * mp)
1528 {
1529   snat_main_t *sm = &snat_main;
1530   vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1531   u32 sw_if_index = ntohl (mp->sw_if_index);
1532   int rv = 0;
1533   u8 is_del;
1534
1535   is_del = !mp->is_add;
1536
1537   VALIDATE_SW_IF_INDEX (mp);
1538
1539   rv = snat_add_interface_address (sm, sw_if_index, is_del,
1540                                    mp->flags & NAT_API_IS_TWICE_NAT);
1541
1542   BAD_SW_IF_INDEX_LABEL;
1543   REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1544 }
1545
1546 static void *vl_api_nat44_add_del_interface_addr_t_print
1547   (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1548 {
1549   u8 *s;
1550
1551   s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1552   s = format (s, "sw_if_index %d twice_nat %d %s",
1553               clib_host_to_net_u32 (mp->sw_if_index),
1554               mp->flags & NAT_API_IS_TWICE_NAT, mp->is_add ? "" : "del");
1555
1556   FINISH;
1557 }
1558
1559 static void
1560 send_nat44_interface_addr_details (u32 sw_if_index,
1561                                    vl_api_registration_t * reg, u32 context,
1562                                    u8 twice_nat)
1563 {
1564   vl_api_nat44_interface_addr_details_t *rmp;
1565   snat_main_t *sm = &snat_main;
1566
1567   rmp = vl_msg_api_alloc (sizeof (*rmp));
1568   clib_memset (rmp, 0, sizeof (*rmp));
1569   rmp->_vl_msg_id =
1570     ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1571   rmp->sw_if_index = ntohl (sw_if_index);
1572
1573   if (twice_nat)
1574     rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1575   rmp->context = context;
1576
1577   vl_api_send_msg (reg, (u8 *) rmp);
1578 }
1579
1580 static void
1581 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1582                                             * mp)
1583 {
1584   vl_api_registration_t *reg;
1585   snat_main_t *sm = &snat_main;
1586   u32 *i;
1587
1588   reg = vl_api_client_index_to_registration (mp->client_index);
1589   if (!reg)
1590     return;
1591
1592   /* *INDENT-OFF* */
1593   vec_foreach (i, sm->auto_add_sw_if_indices)
1594     send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1595   vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1596     send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1597   /* *INDENT-ON* */
1598 }
1599
1600 static void *
1601 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1602                                           mp, void *handle)
1603 {
1604   u8 *s;
1605
1606   s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1607
1608   FINISH;
1609 }
1610
1611 static void
1612 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1613                          u32 context)
1614 {
1615   vl_api_nat44_user_details_t *rmp;
1616   snat_main_t *sm = &snat_main;
1617   ip4_main_t *im = &ip4_main;
1618
1619   rmp = vl_msg_api_alloc (sizeof (*rmp));
1620   clib_memset (rmp, 0, sizeof (*rmp));
1621   rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1622
1623   if (!pool_is_free_index (im->fibs, u->fib_index))
1624     {
1625       fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1626       rmp->vrf_id = ntohl (fib->ft_table_id);
1627     }
1628
1629   clib_memcpy (rmp->ip_address, &(u->addr), 4);
1630   rmp->nsessions = ntohl (u->nsessions);
1631   rmp->nstaticsessions = ntohl (u->nstaticsessions);
1632   rmp->context = context;
1633
1634   vl_api_send_msg (reg, (u8 *) rmp);
1635 }
1636
1637 static void
1638 nat_ed_user_create_helper (snat_main_per_thread_data_t * tsm,
1639                            snat_session_t * s)
1640 {
1641   snat_user_key_t k;
1642   k.addr = s->in2out.addr;
1643   k.fib_index = s->in2out.fib_index;
1644   clib_bihash_kv_8_8_t key, value;
1645   key.key = k.as_u64;
1646   snat_user_t *u;
1647   if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1648     {
1649       pool_get (tsm->users, u);
1650       u->addr = k.addr;
1651       u->fib_index = k.fib_index;
1652       u->nsessions = 0;
1653       u->nstaticsessions = 0;
1654       key.value = u - tsm->users;
1655       clib_bihash_add_del_8_8 (&tsm->user_hash, &key, 1);
1656     }
1657   else
1658     {
1659       u = pool_elt_at_index (tsm->users, value.value);
1660     }
1661   if (snat_is_session_static (s))
1662     {
1663       ++u->nstaticsessions;
1664     }
1665   else
1666     {
1667       ++u->nsessions;
1668     }
1669 }
1670
1671 static void
1672 nat_ed_users_create (snat_main_per_thread_data_t * tsm)
1673 {
1674   snat_session_t *s;
1675   /* *INDENT-OFF* */
1676   pool_foreach (s, tsm->sessions, { nat_ed_user_create_helper (tsm, s); });
1677   /* *INDENT-ON* */
1678 }
1679
1680 static void
1681 nat_ed_users_destroy (snat_main_per_thread_data_t * tsm)
1682 {
1683   snat_user_t *u;
1684   /* *INDENT-OFF* */
1685   pool_flush (u, tsm->users, { });
1686   /* *INDENT-ON* */
1687   clib_bihash_free_8_8 (&tsm->user_hash);
1688   clib_bihash_init_8_8 (&tsm->user_hash, "users", snat_main.user_buckets,
1689                         snat_main.user_memory_size);
1690   clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
1691 }
1692
1693 static void
1694 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1695 {
1696   vl_api_registration_t *reg;
1697   snat_main_t *sm = &snat_main;
1698   snat_main_per_thread_data_t *tsm;
1699   snat_user_t *u;
1700
1701   reg = vl_api_client_index_to_registration (mp->client_index);
1702   if (!reg)
1703     return;
1704
1705   /* *INDENT-OFF* */
1706   vec_foreach (tsm, sm->per_thread_data)
1707     {
1708       if (sm->endpoint_dependent)
1709         {
1710           nat_ed_users_create (tsm);
1711         }
1712       pool_foreach (u, tsm->users,
1713       ({
1714         send_nat44_user_details (u, reg, mp->context);
1715       }));
1716       if (sm->endpoint_dependent)
1717         {
1718           nat_ed_users_destroy (tsm);
1719         }
1720     }
1721   /* *INDENT-ON* */
1722 }
1723
1724 static void *
1725 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1726 {
1727   u8 *s;
1728
1729   s = format (0, "SCRIPT: nat44_user_dump ");
1730
1731   FINISH;
1732 }
1733
1734 static void
1735 send_nat44_user_session_details (snat_session_t * s,
1736                                  vl_api_registration_t * reg, u32 context)
1737 {
1738   vl_api_nat44_user_session_details_t *rmp;
1739   snat_main_t *sm = &snat_main;
1740
1741   rmp = vl_msg_api_alloc (sizeof (*rmp));
1742   clib_memset (rmp, 0, sizeof (*rmp));
1743   rmp->_vl_msg_id =
1744     ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1745   clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1746   clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1747
1748   if (snat_is_session_static (s))
1749     rmp->flags |= NAT_API_IS_STATIC;
1750
1751   if (is_twice_nat_session (s))
1752     rmp->flags |= NAT_API_IS_TWICE_NAT;
1753
1754   if (is_ed_session (s) || is_fwd_bypass_session (s))
1755     rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1756
1757   rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1758   rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1759   rmp->total_pkts = ntohl (s->total_pkts);
1760   rmp->context = context;
1761   if (snat_is_unk_proto_session (s))
1762     {
1763       rmp->outside_port = 0;
1764       rmp->inside_port = 0;
1765       rmp->protocol = ntohs (s->in2out.port);
1766     }
1767   else
1768     {
1769       rmp->outside_port = s->out2in.port;
1770       rmp->inside_port = s->in2out.port;
1771       rmp->protocol = ntohs (nat_proto_to_ip_proto (s->nat_proto));
1772     }
1773   if (is_ed_session (s) || is_fwd_bypass_session (s))
1774     {
1775       clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1776       rmp->ext_host_port = s->ext_host_port;
1777       if (is_twice_nat_session (s))
1778         {
1779           clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1780           rmp->ext_host_nat_port = s->ext_host_nat_port;
1781         }
1782     }
1783
1784   vl_api_send_msg (reg, (u8 *) rmp);
1785 }
1786
1787 static void
1788 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1789                                           mp)
1790 {
1791   vl_api_registration_t *reg;
1792   snat_main_t *sm = &snat_main;
1793   snat_main_per_thread_data_t *tsm;
1794   snat_session_t *s;
1795   clib_bihash_kv_8_8_t key, value;
1796   snat_user_key_t ukey;
1797   snat_user_t *u;
1798   u32 session_index, head_index, elt_index;
1799   dlist_elt_t *head, *elt;
1800   ip4_header_t ip;
1801
1802   reg = vl_api_client_index_to_registration (mp->client_index);
1803   if (!reg)
1804     return;
1805
1806   clib_memcpy (&ukey.addr, mp->ip_address, 4);
1807   ip.src_address.as_u32 = ukey.addr.as_u32;
1808   ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1809   key.key = ukey.as_u64;
1810   if (sm->num_workers > 1)
1811     tsm =
1812       vec_elt_at_index (sm->per_thread_data,
1813                         sm->worker_in2out_cb (&ip, ukey.fib_index, 0));
1814   else
1815     tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1816   if (!sm->endpoint_dependent)
1817     {
1818       if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1819         return;
1820       u = pool_elt_at_index (tsm->users, value.value);
1821       if (!u->nsessions && !u->nstaticsessions)
1822         return;
1823
1824       head_index = u->sessions_per_user_list_head_index;
1825       head = pool_elt_at_index (tsm->list_pool, head_index);
1826       elt_index = head->next;
1827       elt = pool_elt_at_index (tsm->list_pool, elt_index);
1828       session_index = elt->value;
1829       while (session_index != ~0)
1830         {
1831           s = pool_elt_at_index (tsm->sessions, session_index);
1832
1833           send_nat44_user_session_details (s, reg, mp->context);
1834
1835           elt_index = elt->next;
1836           elt = pool_elt_at_index (tsm->list_pool, elt_index);
1837           session_index = elt->value;
1838         }
1839     }
1840   else
1841     {
1842       /* *INDENT-OFF* */
1843       pool_foreach (s, tsm->sessions, {
1844         if (s->in2out.addr.as_u32 == ukey.addr.as_u32)
1845           {
1846             send_nat44_user_session_details (s, reg, mp->context);
1847           }
1848       });
1849       /* *INDENT-ON* */
1850     }
1851 }
1852
1853 static void *
1854 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1855                                         void *handle)
1856 {
1857   u8 *s;
1858
1859   s = format (0, "SCRIPT: nat44_user_session_dump ");
1860   s = format (s, "ip_address %U vrf_id %d\n",
1861               format_ip4_address, mp->ip_address,
1862               clib_net_to_host_u32 (mp->vrf_id));
1863
1864   FINISH;
1865 }
1866
1867 static nat44_lb_addr_port_t *
1868 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1869                              u32 addr_port_pair_num)
1870 {
1871   u8 i;
1872   nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1873   vl_api_nat44_lb_addr_port_t *ap;
1874
1875   for (i = 0; i < addr_port_pair_num; i++)
1876     {
1877       ap = &addr_port_pairs[i];
1878       clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1879       clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1880       lb_addr_port.port = ap->port;
1881       lb_addr_port.probability = ap->probability;
1882       lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1883       vec_add1 (lb_addr_port_pairs, lb_addr_port);
1884     }
1885
1886   return lb_addr_port_pairs;
1887 }
1888
1889 static void
1890   vl_api_nat44_add_del_lb_static_mapping_t_handler
1891   (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1892 {
1893   snat_main_t *sm = &snat_main;
1894   vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1895   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1896   int rv = 0;
1897   nat44_lb_addr_port_t *locals = 0;
1898   ip4_address_t e_addr;
1899   nat_protocol_t proto;
1900   u8 *tag = 0;
1901
1902   if (!sm->endpoint_dependent)
1903     {
1904       rv = VNET_API_ERROR_UNSUPPORTED;
1905       goto send_reply;
1906     }
1907
1908   locals =
1909     unformat_nat44_lb_addr_port (mp->locals,
1910                                  clib_net_to_host_u32 (mp->local_num));
1911   clib_memcpy (&e_addr, mp->external_addr, 4);
1912   proto = ip_proto_to_nat_proto (mp->protocol);
1913
1914   if (mp->flags & NAT_API_IS_TWICE_NAT)
1915     twice_nat = TWICE_NAT;
1916   else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1917     twice_nat = TWICE_NAT_SELF;
1918   mp->tag[sizeof (mp->tag) - 1] = 0;
1919   tag = format (0, "%s", mp->tag);
1920   vec_terminate_c_string (tag);
1921
1922   rv =
1923     nat44_add_del_lb_static_mapping (e_addr,
1924                                      mp->external_port,
1925                                      proto, locals, mp->is_add,
1926                                      twice_nat,
1927                                      mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1928                                      clib_net_to_host_u32 (mp->affinity));
1929
1930   vec_free (locals);
1931   vec_free (tag);
1932
1933 send_reply:
1934   REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1935 }
1936
1937 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1938   (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1939 {
1940   u8 *s;
1941
1942   s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1943   s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1944               mp->is_add,
1945               mp->flags & NAT_API_IS_TWICE_NAT,
1946               mp->flags & NAT_API_IS_OUT2IN_ONLY);
1947
1948   FINISH;
1949 }
1950
1951 static void
1952   vl_api_nat44_lb_static_mapping_add_del_local_t_handler
1953   (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
1954 {
1955   snat_main_t *sm = &snat_main;
1956   vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
1957   int rv = 0;
1958   ip4_address_t e_addr, l_addr;
1959   nat_protocol_t proto;
1960
1961   if (!sm->endpoint_dependent)
1962     {
1963       rv = VNET_API_ERROR_UNSUPPORTED;
1964       goto send_reply;
1965     }
1966
1967   clib_memcpy (&e_addr, mp->external_addr, 4);
1968   clib_memcpy (&l_addr, mp->local.addr, 4);
1969   proto = ip_proto_to_nat_proto (mp->protocol);
1970
1971   rv =
1972     nat44_lb_static_mapping_add_del_local (e_addr,
1973                                            clib_net_to_host_u16
1974                                            (mp->external_port), l_addr,
1975                                            clib_net_to_host_u16 (mp->
1976                                                                  local.port),
1977                                            proto,
1978                                            clib_net_to_host_u32 (mp->
1979                                                                  local.vrf_id),
1980                                            mp->local.probability, mp->is_add);
1981
1982 send_reply:
1983   REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
1984 }
1985
1986 static void *vl_api_nat44_lb_static_mapping_add_del_local_t_print
1987   (vl_api_nat44_lb_static_mapping_add_del_local_t * mp, void *handle)
1988 {
1989   u8 *s;
1990
1991   s = format (0, "SCRIPT: nat44_lb_static_mapping_add_del_local ");
1992   s = format (s, "is_add %d", mp->is_add);
1993
1994   FINISH;
1995 }
1996
1997 static void
1998 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1999                                       vl_api_registration_t * reg,
2000                                       u32 context)
2001 {
2002   vl_api_nat44_lb_static_mapping_details_t *rmp;
2003   snat_main_t *sm = &snat_main;
2004   nat44_lb_addr_port_t *ap;
2005   vl_api_nat44_lb_addr_port_t *locals;
2006   u32 local_num = 0;
2007
2008   rmp =
2009     vl_msg_api_alloc (sizeof (*rmp) +
2010                       (pool_elts (m->locals) *
2011                        sizeof (nat44_lb_addr_port_t)));
2012   clib_memset (rmp, 0, sizeof (*rmp));
2013   rmp->_vl_msg_id =
2014     ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
2015
2016   clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
2017   rmp->external_port = m->external_port;
2018   rmp->protocol = nat_proto_to_ip_proto (m->proto);
2019   rmp->context = context;
2020
2021   if (m->twice_nat == TWICE_NAT)
2022     rmp->flags |= NAT_API_IS_TWICE_NAT;
2023   else if (m->twice_nat == TWICE_NAT_SELF)
2024     rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
2025   if (is_out2in_only_static_mapping (m))
2026     rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
2027   if (m->tag)
2028     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
2029
2030   locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
2031   /* *INDENT-OFF* */
2032   pool_foreach (ap, m->locals,
2033   ({
2034     clib_memcpy (locals->addr, &(ap->addr), 4);
2035     locals->port = ap->port;
2036     locals->probability = ap->probability;
2037     locals->vrf_id = ntohl (ap->vrf_id);
2038     locals++;
2039     local_num++;
2040   }));
2041   /* *INDENT-ON* */
2042   rmp->local_num = ntohl (local_num);
2043
2044   vl_api_send_msg (reg, (u8 *) rmp);
2045 }
2046
2047 static void
2048   vl_api_nat44_lb_static_mapping_dump_t_handler
2049   (vl_api_nat44_lb_static_mapping_dump_t * mp)
2050 {
2051   vl_api_registration_t *reg;
2052   snat_main_t *sm = &snat_main;
2053   snat_static_mapping_t *m;
2054
2055   if (!sm->endpoint_dependent)
2056     return;
2057
2058   reg = vl_api_client_index_to_registration (mp->client_index);
2059   if (!reg)
2060     return;
2061
2062   /* *INDENT-OFF* */
2063   pool_foreach (m, sm->static_mappings,
2064   ({
2065       if (is_lb_static_mapping(m))
2066         send_nat44_lb_static_mapping_details (m, reg, mp->context);
2067   }));
2068   /* *INDENT-ON* */
2069 }
2070
2071 static void *vl_api_nat44_lb_static_mapping_dump_t_print
2072   (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
2073 {
2074   u8 *s;
2075
2076   s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
2077
2078   FINISH;
2079 }
2080
2081 static void
2082 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
2083 {
2084   snat_main_t *sm = &snat_main;
2085   vl_api_nat44_del_session_reply_t *rmp;
2086   ip4_address_t addr, eh_addr;
2087   u16 port, eh_port;
2088   u32 vrf_id;
2089   int rv = 0;
2090   u8 is_in;
2091   nat_protocol_t proto;
2092
2093   memcpy (&addr.as_u8, mp->address, 4);
2094   port = mp->port;
2095   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
2096   proto = ip_proto_to_nat_proto (mp->protocol);
2097   memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
2098   eh_port = mp->ext_host_port;
2099
2100   is_in = mp->flags & NAT_API_IS_INSIDE;
2101
2102   if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2103     rv =
2104       nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
2105                             vrf_id, is_in);
2106   else
2107     rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
2108
2109   REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
2110 }
2111
2112 static void *
2113 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
2114                                   void *handle)
2115 {
2116   u8 *s;
2117
2118   s = format (0, "SCRIPT: nat44_add_del_session ");
2119   s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
2120               format_ip4_address, mp->address,
2121               clib_net_to_host_u16 (mp->port),
2122               mp->protocol, clib_net_to_host_u32 (mp->vrf_id),
2123               mp->flags & NAT_API_IS_INSIDE);
2124   if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2125     s = format (s, "ext_host_address %U ext_host_port %d",
2126                 format_ip4_address, mp->ext_host_address,
2127                 clib_net_to_host_u16 (mp->ext_host_port));
2128
2129   FINISH;
2130 }
2131
2132 static void
2133   vl_api_nat44_forwarding_enable_disable_t_handler
2134   (vl_api_nat44_forwarding_enable_disable_t * mp)
2135 {
2136   snat_main_t *sm = &snat_main;
2137   vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
2138   int rv = 0;
2139   u32 *ses_to_be_removed = 0, *ses_index;
2140   snat_main_per_thread_data_t *tsm;
2141   snat_session_t *s;
2142
2143   sm->forwarding_enabled = mp->enable != 0;
2144
2145   if (mp->enable == 0)
2146     {
2147       /* *INDENT-OFF* */
2148       vec_foreach (tsm, sm->per_thread_data)
2149       {
2150         pool_foreach (s, tsm->sessions,
2151         ({
2152           if (is_fwd_bypass_session(s))
2153             {
2154               vec_add1 (ses_to_be_removed, s - tsm->sessions);
2155             }
2156         }));
2157         if(sm->endpoint_dependent){
2158             vec_foreach (ses_index, ses_to_be_removed)
2159               {
2160                 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
2161                 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2162                 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
2163               }
2164         }else{
2165             vec_foreach (ses_index, ses_to_be_removed)
2166               {
2167                 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
2168                 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2169                 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
2170               }
2171         }
2172         vec_free (ses_to_be_removed);
2173       }
2174       /* *INDENT-ON* */
2175     }
2176
2177   REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
2178 }
2179
2180 static void *vl_api_nat44_forwarding_enable_disable_t_print
2181   (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
2182 {
2183   u8 *s;
2184
2185   s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
2186   s = format (s, "enable %d", mp->enable != 0);
2187
2188   FINISH;
2189 }
2190
2191 static void
2192   vl_api_nat44_forwarding_is_enabled_t_handler
2193   (vl_api_nat44_forwarding_is_enabled_t * mp)
2194 {
2195   vl_api_registration_t *reg;
2196   snat_main_t *sm = &snat_main;
2197   vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
2198
2199   reg = vl_api_client_index_to_registration (mp->client_index);
2200   if (!reg)
2201     return;
2202
2203   rmp = vl_msg_api_alloc (sizeof (*rmp));
2204   clib_memset (rmp, 0, sizeof (*rmp));
2205   rmp->_vl_msg_id =
2206     ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
2207   rmp->context = mp->context;
2208
2209   rmp->enabled = sm->forwarding_enabled;
2210
2211   vl_api_send_msg (reg, (u8 *) rmp);
2212 }
2213
2214 static void *vl_api_nat44_forwarding_is_enabled_t_print
2215   (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
2216 {
2217   u8 *s;
2218
2219   s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
2220
2221   FINISH;
2222 }
2223
2224 /* List of message types that this plugin understands */
2225 #define foreach_snat_plugin_api_msg                                     \
2226 _(NAT_CONTROL_PING, nat_control_ping)                                   \
2227 _(NAT_SHOW_CONFIG, nat_show_config)                                     \
2228 _(NAT_SHOW_CONFIG_2, nat_show_config_2)                                 \
2229 _(NAT_SET_WORKERS, nat_set_workers)                                     \
2230 _(NAT_WORKER_DUMP, nat_worker_dump)                                     \
2231 _(NAT44_DEL_USER, nat44_del_user)                                       \
2232 _(NAT44_SET_SESSION_LIMIT, nat44_set_session_limit)                     \
2233 _(NAT_SET_LOG_LEVEL, nat_set_log_level)                                 \
2234 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable)                   \
2235 _(NAT_SET_TIMEOUTS, nat_set_timeouts)                                   \
2236 _(NAT_GET_TIMEOUTS, nat_get_timeouts)                                   \
2237 _(NAT_SET_ADDR_AND_PORT_ALLOC_ALG, nat_set_addr_and_port_alloc_alg)     \
2238 _(NAT_GET_ADDR_AND_PORT_ALLOC_ALG, nat_get_addr_and_port_alloc_alg)     \
2239 _(NAT_SET_MSS_CLAMPING, nat_set_mss_clamping)                           \
2240 _(NAT_GET_MSS_CLAMPING, nat_get_mss_clamping)                           \
2241 _(NAT_HA_SET_LISTENER, nat_ha_set_listener)                             \
2242 _(NAT_HA_SET_FAILOVER, nat_ha_set_failover)                             \
2243 _(NAT_HA_GET_LISTENER, nat_ha_get_listener)                             \
2244 _(NAT_HA_GET_FAILOVER, nat_ha_get_failover)                             \
2245 _(NAT_HA_FLUSH, nat_ha_flush)                                           \
2246 _(NAT_HA_RESYNC, nat_ha_resync)                                         \
2247 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range)             \
2248 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature)     \
2249 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping)           \
2250 _(NAT44_ADD_DEL_STATIC_MAPPING_V2, nat44_add_del_static_mapping_v2)     \
2251 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping)       \
2252 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump)                 \
2253 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump)             \
2254 _(NAT44_ADDRESS_DUMP, nat44_address_dump)                               \
2255 _(NAT44_INTERFACE_DUMP, nat44_interface_dump)                           \
2256 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr)           \
2257 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump)                 \
2258 _(NAT44_USER_DUMP, nat44_user_dump)                                     \
2259 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump)                     \
2260 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE,                               \
2261   nat44_interface_add_del_output_feature)                               \
2262 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP,                                  \
2263   nat44_interface_output_feature_dump)                                  \
2264 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping)     \
2265 _(NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL,                                \
2266   nat44_lb_static_mapping_add_del_local)                                \
2267 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump)           \
2268 _(NAT44_DEL_SESSION, nat44_del_session)                                 \
2269 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable)     \
2270 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled)
2271
2272 /* Set up the API message handling tables */
2273 static clib_error_t *
2274 snat_plugin_api_hookup (vlib_main_t * vm)
2275 {
2276   snat_main_t *sm __attribute__ ((unused)) = &snat_main;
2277 #define _(N,n)                                                  \
2278     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
2279                            #n,                                  \
2280                            vl_api_##n##_t_handler,              \
2281                            vl_noop_handler,                     \
2282                            vl_api_##n##_t_endian,               \
2283                            vl_api_##n##_t_print,                \
2284                            sizeof(vl_api_##n##_t), 1);
2285   foreach_snat_plugin_api_msg;
2286 #undef _
2287
2288   return 0;
2289 }
2290
2291 #define vl_msg_name_crc_list
2292 #include <nat/nat_all_api_h.h>
2293 #undef vl_msg_name_crc_list
2294
2295 static void
2296 setup_message_id_table (snat_main_t * sm, api_main_t * am)
2297 {
2298 #define _(id,n,crc) \
2299   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
2300   foreach_vl_msg_name_crc_nat;
2301 #undef _
2302 }
2303
2304 static void
2305 plugin_custom_dump_configure (snat_main_t * sm)
2306 {
2307 #define _(n,f) sm->api_main->msg_print_handlers \
2308   [VL_API_##n + sm->msg_id_base]                \
2309     = (void *) vl_api_##f##_t_print;
2310   foreach_snat_plugin_api_msg;
2311 #undef _
2312 }
2313
2314 clib_error_t *
2315 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
2316 {
2317   u8 *name;
2318   clib_error_t *error = 0;
2319
2320   name = format (0, "nat_%08x%c", api_version, 0);
2321
2322   /* Ask for a correctly-sized block of API message decode slots */
2323   sm->msg_id_base =
2324     vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
2325
2326   error = snat_plugin_api_hookup (vm);
2327
2328   /* Add our API messages to the global name_crc hash table */
2329   setup_message_id_table (sm, sm->api_main);
2330
2331   plugin_custom_dump_configure (sm);
2332
2333   vec_free (name);
2334
2335   return error;
2336 }
2337
2338 /*
2339  * fd.io coding-style-patch-verification: ON
2340  *
2341  * Local Variables:
2342  * eval: (c-set-style "gnu")
2343  * End:
2344  */