session svm: fix fifo migration
[vpp.git] / src / plugins / nat / nat44_api.c
1 /*
2  * Copyright (c) 2020 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 NAT44 plugin API implementation
19  */
20
21 #include <vnet/ip/ip_types_api.h>
22 #include <vlibmemory/api.h>
23
24 #include <vnet/fib/fib_table.h>
25
26 #include <nat/lib/nat_inlines.h>
27 #include <nat/lib/ipfix_logging.h>
28
29 #include <nat/nat.h>
30
31 #include <nat/nat44.api_enum.h>
32 #include <nat/nat44.api_types.h>
33
34 #include <nat/nat44-ei/nat44_ei_ha.h>
35 #include <nat/nat44-ei/nat44_ei.h>
36 #include <nat/nat_inlines.h>
37
38 #include <nat/nat44/inlines.h>
39 #include <nat/nat44/ed_inlines.h>
40
41 #define REPLY_MSG_ID_BASE sm->msg_id_base
42 #include <vlibapi/api_helper_macros.h>
43
44 static void
45 vl_api_nat_control_ping_t_handler (vl_api_nat_control_ping_t * mp)
46 {
47   vl_api_nat_control_ping_reply_t *rmp;
48   snat_main_t *sm = &snat_main;
49   int rv = 0;
50
51   /* *INDENT-OFF* */
52   REPLY_MACRO2 (VL_API_NAT_CONTROL_PING_REPLY,
53   ({
54     rmp->vpe_pid = ntohl (getpid ());
55   }));
56   /* *INDENT-ON* */
57 }
58
59 static void
60 vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp)
61 {
62   vl_api_nat_show_config_reply_t *rmp;
63   snat_main_t *sm = &snat_main;
64   int rv = 0;
65
66   /* *INDENT-OFF* */
67   REPLY_MACRO2_ZERO (VL_API_NAT_SHOW_CONFIG_REPLY,
68   ({
69     rmp->translation_buckets = htonl (sm->translation_buckets);
70     rmp->user_buckets = htonl (sm->user_buckets);
71     rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
72     rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
73     rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
74     rmp->static_mapping_only = sm->static_mapping_only;
75     rmp->static_mapping_connection_tracking =
76       sm->static_mapping_connection_tracking;
77     rmp->endpoint_dependent = sm->endpoint_dependent;
78     rmp->out2in_dpo = sm->out2in_dpo;
79   }));
80   /* *INDENT-ON* */
81 }
82
83 static void
84 vl_api_nat_show_config_2_t_handler (vl_api_nat_show_config_2_t * mp)
85 {
86   vl_api_nat_show_config_2_reply_t *rmp;
87   snat_main_t *sm = &snat_main;
88   int rv = 0;
89
90   /* *INDENT-OFF* */
91   REPLY_MACRO2_ZERO (VL_API_NAT_SHOW_CONFIG_2_REPLY,
92   ({
93     rmp->translation_buckets = htonl (sm->translation_buckets);
94     rmp->user_buckets = htonl (sm->user_buckets);
95     rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
96     rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
97     rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
98     rmp->static_mapping_only = sm->static_mapping_only;
99     rmp->static_mapping_connection_tracking =
100       sm->static_mapping_connection_tracking;
101     rmp->endpoint_dependent = sm->endpoint_dependent;
102     rmp->out2in_dpo = sm->out2in_dpo;
103     rmp->max_translations_per_thread = clib_net_to_host_u32(sm->max_translations_per_thread);
104     rmp->max_users_per_thread = clib_net_to_host_u32(sm->max_users_per_thread);
105   }));
106   /* *INDENT-ON* */
107 }
108
109 static void
110 vl_api_nat44_show_running_config_t_handler (vl_api_nat44_show_running_config_t
111                                             * mp)
112 {
113   vl_api_nat44_show_running_config_reply_t *rmp;
114   snat_main_t *sm = &snat_main;
115   nat44_config_t *rc = &sm->rconfig;
116   int rv = 0;
117
118   /* *INDENT-OFF* */
119   REPLY_MACRO2_ZERO (VL_API_NAT44_SHOW_RUNNING_CONFIG_REPLY,
120   ({
121     rmp->inside_vrf = htonl (rc->inside_vrf);
122     rmp->outside_vrf = htonl (rc->outside_vrf);
123     rmp->users = htonl (rc->users);
124     rmp->sessions = htonl (rc->sessions);
125     rmp->user_sessions = htonl (rc->user_sessions);
126
127     rmp->user_buckets = htonl (sm->user_buckets);
128     rmp->translation_buckets = htonl (sm->translation_buckets);
129
130     rmp->timeouts.udp = htonl (sm->timeouts.udp);
131     rmp->timeouts.tcp_established = htonl (sm->timeouts.tcp.established);
132     rmp->timeouts.tcp_transitory = htonl (sm->timeouts.tcp.transitory);
133     rmp->timeouts.icmp = htonl (sm->timeouts.icmp);
134
135     rmp->forwarding_enabled = sm->forwarding_enabled == 1;
136     // consider how to split functionality between subplugins
137     rmp->ipfix_logging_enabled = nat_ipfix_logging_enabled ();
138
139     if (rc->endpoint_dependent)
140       rmp->flags |= NAT44_IS_ENDPOINT_DEPENDENT;
141     else
142       rmp->flags |= NAT44_IS_ENDPOINT_INDEPENDENT;
143
144     if (rc->static_mapping_only)
145       rmp->flags |= NAT44_IS_STATIC_MAPPING_ONLY;
146     if (rc->connection_tracking)
147       rmp->flags |= NAT44_IS_CONNECTION_TRACKING;
148     if (rc->out2in_dpo)
149       rmp->flags |= NAT44_IS_OUT2IN_DPO;
150   }));
151   /* *INDENT-ON* */
152 }
153
154 static void
155 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
156 {
157   snat_main_t *sm = &snat_main;
158   vl_api_nat_set_workers_reply_t *rmp;
159   int rv = 0;
160   uword *bitmap = 0;
161   u64 mask;
162
163   mask = clib_net_to_host_u64 (mp->worker_mask);
164
165   if (sm->num_workers < 2)
166     {
167       rv = VNET_API_ERROR_FEATURE_DISABLED;
168       goto send_reply;
169     }
170
171   bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
172   rv = snat_set_workers (bitmap);
173   clib_bitmap_free (bitmap);
174
175 send_reply:
176   REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
177 }
178
179 static void
180 send_nat_worker_details (u32 worker_index, vl_api_registration_t * reg,
181                          u32 context)
182 {
183   vl_api_nat_worker_details_t *rmp;
184   snat_main_t *sm = &snat_main;
185   vlib_worker_thread_t *w =
186     vlib_worker_threads + worker_index + sm->first_worker_index;
187
188   rmp = vl_msg_api_alloc (sizeof (*rmp));
189   clib_memset (rmp, 0, sizeof (*rmp));
190   rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
191   rmp->context = context;
192   rmp->worker_index = htonl (worker_index);
193   rmp->lcore_id = htonl (w->cpu_id);
194   strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
195
196   vl_api_send_msg (reg, (u8 *) rmp);
197 }
198
199 static void
200 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
201 {
202   vl_api_registration_t *reg;
203   snat_main_t *sm = &snat_main;
204   u32 *worker_index;
205
206   reg = vl_api_client_index_to_registration (mp->client_index);
207   if (!reg)
208     return;
209
210   /* *INDENT-OFF* */
211   vec_foreach (worker_index, sm->workers)
212     send_nat_worker_details(*worker_index, reg, mp->context);
213   /* *INDENT-ON* */
214 }
215
216 static void
217 vl_api_nat44_session_cleanup_t_handler (vl_api_nat44_session_cleanup_t * mp)
218 {
219   snat_main_t *sm = &snat_main;
220   vl_api_nat44_session_cleanup_reply_t *rmp;
221   int rv = 0;
222   REPLY_MACRO (VL_API_NAT44_SESSION_CLEANUP_REPLY);
223 }
224
225 static void
226 vl_api_nat44_set_session_limit_t_handler (vl_api_nat44_set_session_limit_t *
227                                           mp)
228 {
229   snat_main_t *sm = &snat_main;
230   vl_api_nat44_set_session_limit_reply_t *rmp;
231   int rv = 0;
232
233   rv = nat44_set_session_limit
234     (ntohl (mp->session_limit), ntohl (mp->vrf_id));
235
236   REPLY_MACRO (VL_API_NAT44_SET_SESSION_LIMIT_REPLY);
237 }
238
239 static void
240 vl_api_nat_set_log_level_t_handler (vl_api_nat_set_log_level_t * mp)
241 {
242   snat_main_t *sm = &snat_main;
243   vl_api_nat_set_log_level_reply_t *rmp;
244   int rv = 0;
245
246   if (sm->log_level > NAT_LOG_DEBUG)
247     rv = VNET_API_ERROR_UNSUPPORTED;
248   else
249     sm->log_level = mp->log_level;
250
251   REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
252 }
253
254 static void
255   vl_api_nat44_plugin_enable_disable_t_handler
256   (vl_api_nat44_plugin_enable_disable_t * mp)
257 {
258   snat_main_t *sm = &snat_main;
259   nat44_config_t c = { 0 };
260   vl_api_nat44_plugin_enable_disable_reply_t *rmp;
261   int rv = 0;
262
263   if (mp->enable)
264     {
265       c.endpoint_dependent = mp->flags & NAT44_API_IS_ENDPOINT_DEPENDENT;
266       c.static_mapping_only = mp->flags & NAT44_API_IS_STATIC_MAPPING_ONLY;
267       c.connection_tracking = mp->flags & NAT44_API_IS_CONNECTION_TRACKING;
268       c.out2in_dpo = mp->flags & NAT44_API_IS_OUT2IN_DPO;
269
270       c.inside_vrf = ntohl (mp->inside_vrf);
271       c.outside_vrf = ntohl (mp->outside_vrf);
272
273       c.users = ntohl (mp->users);
274
275       c.sessions = ntohl (mp->sessions);
276
277       c.user_sessions = ntohl (mp->user_sessions);
278
279       rv = nat44_plugin_enable (c);
280     }
281   else
282     rv = nat44_plugin_disable ();
283
284   REPLY_MACRO (VL_API_NAT44_PLUGIN_ENABLE_DISABLE_REPLY);
285 }
286
287 static void
288 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
289                                            mp)
290 {
291   snat_main_t *sm = &snat_main;
292   vl_api_nat_ipfix_enable_disable_reply_t *rmp;
293   int rv = 0;
294
295   rv = nat_ipfix_logging_enable_disable (mp->enable,
296                                          clib_host_to_net_u32
297                                          (mp->domain_id),
298                                          clib_host_to_net_u16 (mp->src_port));
299
300   REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
301 }
302
303 static void
304 vl_api_nat_set_timeouts_t_handler (vl_api_nat_set_timeouts_t * mp)
305 {
306   snat_main_t *sm = &snat_main;
307   vl_api_nat_set_timeouts_reply_t *rmp;
308   int rv = 0;
309
310   sm->timeouts.udp = ntohl (mp->udp);
311   sm->timeouts.tcp.established = ntohl (mp->tcp_established);
312   sm->timeouts.tcp.transitory = ntohl (mp->tcp_transitory);
313   sm->timeouts.icmp = ntohl (mp->icmp);
314
315   REPLY_MACRO (VL_API_NAT_SET_TIMEOUTS_REPLY);
316 }
317
318 static void
319 vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp)
320 {
321   snat_main_t *sm = &snat_main;
322   vl_api_nat_get_timeouts_reply_t *rmp;
323   int rv = 0;
324
325   /* *INDENT-OFF* */
326   REPLY_MACRO2 (VL_API_NAT_GET_TIMEOUTS_REPLY,
327   ({
328     rmp->udp = htonl (sm->timeouts.udp);
329     rmp->tcp_established = htonl (sm->timeouts.tcp.established);
330     rmp->tcp_transitory = htonl (sm->timeouts.tcp.transitory);
331     rmp->icmp = htonl (sm->timeouts.icmp);
332   }))
333   /* *INDENT-ON* */
334 }
335
336 static void
337   vl_api_nat_set_addr_and_port_alloc_alg_t_handler
338   (vl_api_nat_set_addr_and_port_alloc_alg_t * mp)
339 {
340   snat_main_t *sm = &snat_main;
341   vl_api_nat_set_addr_and_port_alloc_alg_reply_t *rmp;
342   int rv = 0;
343   u16 port_start, port_end;
344
345   if (sm->endpoint_dependent)
346     {
347       rv = VNET_API_ERROR_UNSUPPORTED;
348       goto send_reply;
349     }
350
351   switch (mp->alg)
352     {
353     case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
354       nat44_ei_set_alloc_default ();
355       break;
356     case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
357       nat44_ei_set_alloc_mape (ntohs (mp->psid), mp->psid_offset,
358                                mp->psid_length);
359       break;
360     case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
361       port_start = ntohs (mp->start_port);
362       port_end = ntohs (mp->end_port);
363       if (port_end <= port_start)
364         {
365           rv = VNET_API_ERROR_INVALID_VALUE;
366           goto send_reply;
367         }
368       nat44_ei_set_alloc_range (port_start, port_end);
369       break;
370     default:
371       rv = VNET_API_ERROR_INVALID_VALUE;
372       break;
373     }
374
375 send_reply:
376   REPLY_MACRO (VL_API_NAT_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
377 }
378
379 static void
380   vl_api_nat_get_addr_and_port_alloc_alg_t_handler
381   (vl_api_nat_get_addr_and_port_alloc_alg_t * mp)
382 {
383   snat_main_t *sm = &snat_main;
384   vl_api_nat_get_addr_and_port_alloc_alg_reply_t *rmp;
385   int rv = 0;
386
387   /* *INDENT-OFF* */
388   REPLY_MACRO2 (VL_API_NAT_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY,
389   ({
390     rmp->alg = sm->addr_and_port_alloc_alg;
391     rmp->psid_offset = sm->psid_offset;
392     rmp->psid_length = sm->psid_length;
393     rmp->psid = htons (sm->psid);
394     rmp->start_port = htons (sm->start_port);
395     rmp->end_port = htons (sm->end_port);
396   }))
397   /* *INDENT-ON* */
398 }
399
400 static void
401 vl_api_nat_set_mss_clamping_t_handler (vl_api_nat_set_mss_clamping_t * mp)
402 {
403   snat_main_t *sm = &snat_main;
404   vl_api_nat_set_mss_clamping_reply_t *rmp;
405   int rv = 0;
406
407   if (mp->enable)
408     sm->mss_clamping = ntohs (mp->mss_value);
409   else
410     sm->mss_clamping = 0;
411
412   REPLY_MACRO (VL_API_NAT_SET_MSS_CLAMPING_REPLY);
413 }
414
415 static void
416 vl_api_nat_get_mss_clamping_t_handler (vl_api_nat_get_mss_clamping_t * mp)
417 {
418   snat_main_t *sm = &snat_main;
419   vl_api_nat_get_mss_clamping_reply_t *rmp;
420   int rv = 0;
421
422   /* *INDENT-OFF* */
423   REPLY_MACRO2 (VL_API_NAT_GET_MSS_CLAMPING_REPLY,
424   ({
425     rmp->enable = sm->mss_clamping ? 1 : 0;
426     rmp->mss_value = htons (sm->mss_clamping);
427   }))
428   /* *INDENT-ON* */
429 }
430
431 static void
432 vl_api_nat_ha_set_listener_t_handler (vl_api_nat_ha_set_listener_t * mp)
433 {
434   snat_main_t *sm = &snat_main;
435   vl_api_nat_ha_set_listener_reply_t *rmp;
436   ip4_address_t addr;
437   int rv;
438
439   memcpy (&addr, &mp->ip_address, sizeof (addr));
440   rv =
441     nat_ha_set_listener (&addr, clib_net_to_host_u16 (mp->port),
442                          clib_net_to_host_u32 (mp->path_mtu));
443
444   REPLY_MACRO (VL_API_NAT_HA_SET_LISTENER_REPLY);
445 }
446
447 static void
448 vl_api_nat_ha_get_listener_t_handler (vl_api_nat_ha_get_listener_t * mp)
449 {
450   snat_main_t *sm = &snat_main;
451   vl_api_nat_ha_get_listener_reply_t *rmp;
452   int rv = 0;
453   ip4_address_t addr;
454   u16 port;
455   u32 path_mtu;
456
457   nat_ha_get_listener (&addr, &port, &path_mtu);
458
459   /* *INDENT-OFF* */
460   REPLY_MACRO2 (VL_API_NAT_HA_GET_LISTENER_REPLY,
461   ({
462     clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
463     rmp->port = clib_host_to_net_u16 (port);
464     rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
465   }))
466   /* *INDENT-ON* */
467 }
468
469 static void
470 vl_api_nat_ha_set_failover_t_handler (vl_api_nat_ha_set_failover_t * mp)
471 {
472   snat_main_t *sm = &snat_main;
473   vl_api_nat_ha_set_failover_reply_t *rmp;
474   ip4_address_t addr;
475   int rv;
476
477   memcpy (&addr, &mp->ip_address, sizeof (addr));
478   rv =
479     nat_ha_set_failover (&addr, clib_net_to_host_u16 (mp->port),
480                          clib_net_to_host_u32 (mp->session_refresh_interval));
481
482   REPLY_MACRO (VL_API_NAT_HA_SET_FAILOVER_REPLY);
483 }
484
485 static void
486 vl_api_nat_ha_get_failover_t_handler (vl_api_nat_ha_get_failover_t * mp)
487 {
488   snat_main_t *sm = &snat_main;
489   vl_api_nat_ha_get_failover_reply_t *rmp;
490   int rv = 0;
491   ip4_address_t addr;
492   u16 port;
493   u32 session_refresh_interval;
494
495   nat_ha_get_failover (&addr, &port, &session_refresh_interval);
496
497   /* *INDENT-OFF* */
498   REPLY_MACRO2 (VL_API_NAT_HA_GET_FAILOVER_REPLY,
499   ({
500     clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
501     rmp->port = clib_host_to_net_u16 (port);
502     rmp->session_refresh_interval = clib_host_to_net_u32 (session_refresh_interval);
503   }))
504   /* *INDENT-ON* */
505 }
506
507 static void
508 vl_api_nat_ha_flush_t_handler (vl_api_nat_ha_flush_t * mp)
509 {
510   snat_main_t *sm = &snat_main;
511   vl_api_nat_ha_flush_reply_t *rmp;
512   int rv = 0;
513
514   nat_ha_flush (0);
515
516   REPLY_MACRO (VL_API_NAT_HA_FLUSH_REPLY);
517 }
518
519 static void
520 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
521 {
522   snat_main_t *sm = &snat_main;
523   vl_api_registration_t *reg;
524   vl_api_nat_ha_resync_completed_event_t *mp;
525
526   reg = vl_api_client_index_to_registration (client_index);
527   if (!reg)
528     return;
529
530   mp = vl_msg_api_alloc (sizeof (*mp));
531   clib_memset (mp, 0, sizeof (*mp));
532   mp->client_index = client_index;
533   mp->pid = pid;
534   mp->missed_count = clib_host_to_net_u32 (missed_count);
535   mp->_vl_msg_id =
536     ntohs (VL_API_NAT_HA_RESYNC_COMPLETED_EVENT + sm->msg_id_base);
537
538   vl_api_send_msg (reg, (u8 *) mp);
539 }
540
541 static void
542 vl_api_nat_ha_resync_t_handler (vl_api_nat_ha_resync_t * mp)
543 {
544   snat_main_t *sm = &snat_main;
545   vl_api_nat_ha_resync_reply_t *rmp;
546   int rv;
547
548   rv =
549     nat_ha_resync (mp->client_index, mp->pid,
550                    mp->want_resync_event ? nat_ha_resync_completed_event_cb :
551                    NULL);
552
553   REPLY_MACRO (VL_API_NAT_HA_RESYNC_REPLY);
554 }
555
556 static void
557 vl_api_nat44_del_user_t_handler (vl_api_nat44_del_user_t * mp)
558 {
559   snat_main_t *sm = &snat_main;
560   vl_api_nat44_del_user_reply_t *rmp;
561   ip4_address_t addr;
562   int rv;
563   memcpy (&addr.as_u8, mp->ip_address, 4);
564   rv = nat44_ei_user_del (&addr, ntohl (mp->fib_index));
565   REPLY_MACRO (VL_API_NAT44_DEL_USER_REPLY);
566 }
567
568 static void
569   vl_api_nat44_add_del_address_range_t_handler
570   (vl_api_nat44_add_del_address_range_t * mp)
571 {
572   snat_main_t *sm = &snat_main;
573   vl_api_nat44_add_del_address_range_reply_t *rmp;
574   ip4_address_t this_addr;
575   u8 is_add, twice_nat;
576   u32 start_host_order, end_host_order;
577   u32 vrf_id;
578   int i, count;
579   int rv = 0;
580   u32 *tmp;
581
582   if (sm->static_mapping_only)
583     {
584       rv = VNET_API_ERROR_FEATURE_DISABLED;
585       goto send_reply;
586     }
587
588   is_add = mp->is_add;
589   twice_nat = mp->flags & NAT_API_IS_TWICE_NAT;
590
591   tmp = (u32 *) mp->first_ip_address;
592   start_host_order = clib_host_to_net_u32 (tmp[0]);
593   tmp = (u32 *) mp->last_ip_address;
594   end_host_order = clib_host_to_net_u32 (tmp[0]);
595
596   count = (end_host_order - start_host_order) + 1;
597
598   vrf_id = clib_host_to_net_u32 (mp->vrf_id);
599
600   if (count > 1024)
601     nat_log_info ("%U - %U, %d addresses...",
602                   format_ip4_address, mp->first_ip_address,
603                   format_ip4_address, mp->last_ip_address, count);
604
605   memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
606
607   for (i = 0; i < count; i++)
608     {
609       if (is_add)
610         rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
611       else
612         rv = snat_del_address (sm, this_addr, 0, twice_nat);
613
614       if (rv)
615         goto send_reply;
616
617       if (sm->out2in_dpo)
618         nat44_add_del_address_dpo (this_addr, is_add);
619
620       increment_v4_address (&this_addr);
621     }
622
623 send_reply:
624   REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
625 }
626
627 static void
628 send_nat44_address_details (snat_address_t * a,
629                             vl_api_registration_t * reg, u32 context,
630                             u8 twice_nat)
631 {
632   vl_api_nat44_address_details_t *rmp;
633   snat_main_t *sm = &snat_main;
634
635   rmp = vl_msg_api_alloc (sizeof (*rmp));
636   clib_memset (rmp, 0, sizeof (*rmp));
637   rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
638   clib_memcpy (rmp->ip_address, &(a->addr), 4);
639   if (a->fib_index != ~0)
640     {
641       fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
642       rmp->vrf_id = ntohl (fib->ft_table_id);
643     }
644   else
645     rmp->vrf_id = ~0;
646   if (twice_nat)
647     rmp->flags |= NAT_API_IS_TWICE_NAT;
648   rmp->context = context;
649
650   vl_api_send_msg (reg, (u8 *) rmp);
651 }
652
653 static void
654 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
655 {
656   vl_api_registration_t *reg;
657   snat_main_t *sm = &snat_main;
658   snat_address_t *a;
659
660   reg = vl_api_client_index_to_registration (mp->client_index);
661   if (!reg)
662     return;
663
664   /* *INDENT-OFF* */
665   vec_foreach (a, sm->addresses)
666     send_nat44_address_details (a, reg, mp->context, 0);
667   vec_foreach (a, sm->twice_nat_addresses)
668     send_nat44_address_details (a, reg, mp->context, 1);
669   /* *INDENT-ON* */
670 }
671
672 static void
673   vl_api_nat44_interface_add_del_feature_t_handler
674   (vl_api_nat44_interface_add_del_feature_t * mp)
675 {
676   snat_main_t *sm = &snat_main;
677   vl_api_nat44_interface_add_del_feature_reply_t *rmp;
678   u32 sw_if_index = ntohl (mp->sw_if_index);
679   u8 is_del;
680   int rv = 0;
681
682   is_del = !mp->is_add;
683
684   VALIDATE_SW_IF_INDEX (mp);
685
686   rv =
687     snat_interface_add_del (sw_if_index, mp->flags & NAT_API_IS_INSIDE,
688                             is_del);
689
690   BAD_SW_IF_INDEX_LABEL;
691
692   REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
693 }
694
695 static void
696 send_nat44_interface_details (snat_interface_t * i,
697                               vl_api_registration_t * reg, u32 context)
698 {
699   vl_api_nat44_interface_details_t *rmp;
700   snat_main_t *sm = &snat_main;
701
702   rmp = vl_msg_api_alloc (sizeof (*rmp));
703   clib_memset (rmp, 0, sizeof (*rmp));
704   rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
705   rmp->sw_if_index = ntohl (i->sw_if_index);
706
707   if (nat_interface_is_inside (i))
708     rmp->flags |= NAT_API_IS_INSIDE;
709   if (nat_interface_is_outside (i))
710     rmp->flags |= NAT_API_IS_OUTSIDE;
711
712   rmp->context = context;
713
714   vl_api_send_msg (reg, (u8 *) rmp);
715 }
716
717 static void
718 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
719 {
720   vl_api_registration_t *reg;
721   snat_main_t *sm = &snat_main;
722   snat_interface_t *i;
723
724   reg = vl_api_client_index_to_registration (mp->client_index);
725   if (!reg)
726     return;
727
728   /* *INDENT-OFF* */
729   pool_foreach (i, sm->interfaces)
730    {
731     send_nat44_interface_details(i, reg, mp->context);
732   }
733   /* *INDENT-ON* */
734 }
735
736 static void
737   vl_api_nat44_interface_add_del_output_feature_t_handler
738   (vl_api_nat44_interface_add_del_output_feature_t * mp)
739 {
740   snat_main_t *sm = &snat_main;
741   vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
742   u32 sw_if_index = ntohl (mp->sw_if_index);
743   int rv = 0;
744
745   VALIDATE_SW_IF_INDEX (mp);
746
747   rv = snat_interface_add_del_output_feature (sw_if_index,
748                                               mp->flags & NAT_API_IS_INSIDE,
749                                               !mp->is_add);
750
751   BAD_SW_IF_INDEX_LABEL;
752   REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
753 }
754
755 static void
756 send_nat44_interface_output_feature_details (snat_interface_t * i,
757                                              vl_api_registration_t * reg,
758                                              u32 context)
759 {
760   vl_api_nat44_interface_output_feature_details_t *rmp;
761   snat_main_t *sm = &snat_main;
762
763   rmp = vl_msg_api_alloc (sizeof (*rmp));
764   clib_memset (rmp, 0, sizeof (*rmp));
765   rmp->_vl_msg_id =
766     ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
767   rmp->sw_if_index = ntohl (i->sw_if_index);
768   rmp->context = context;
769
770   if (nat_interface_is_inside (i))
771     rmp->flags |= NAT_API_IS_INSIDE;
772
773   vl_api_send_msg (reg, (u8 *) rmp);
774 }
775
776 static void
777   vl_api_nat44_interface_output_feature_dump_t_handler
778   (vl_api_nat44_interface_output_feature_dump_t * mp)
779 {
780   vl_api_registration_t *reg;
781   snat_main_t *sm = &snat_main;
782   snat_interface_t *i;
783
784   reg = vl_api_client_index_to_registration (mp->client_index);
785   if (!reg)
786     return;
787
788   /* *INDENT-OFF* */
789   pool_foreach (i, sm->output_feature_interfaces)
790    {
791     send_nat44_interface_output_feature_details(i, reg, mp->context);
792   }
793   /* *INDENT-ON* */
794 }
795
796 static void
797   vl_api_nat44_add_del_static_mapping_t_handler
798   (vl_api_nat44_add_del_static_mapping_t * mp)
799 {
800   snat_main_t *sm = &snat_main;
801   vl_api_nat44_add_del_static_mapping_reply_t *rmp;
802   ip4_address_t local_addr, external_addr, pool_addr = { 0 };
803   u16 local_port = 0, external_port = 0;
804   u32 vrf_id, external_sw_if_index;
805   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
806   int rv = 0;
807   nat_protocol_t proto;
808   u8 *tag = 0;
809
810   memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
811   memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
812
813   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
814     {
815       local_port = mp->local_port;
816       external_port = mp->external_port;
817     }
818
819   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
820   external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
821   proto = ip_proto_to_nat_proto (mp->protocol);
822
823   if (mp->flags & NAT_API_IS_TWICE_NAT)
824     twice_nat = TWICE_NAT;
825   else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
826     twice_nat = TWICE_NAT_SELF;
827   mp->tag[sizeof (mp->tag) - 1] = 0;
828   tag = format (0, "%s", mp->tag);
829   vec_terminate_c_string (tag);
830
831   rv = snat_add_static_mapping (local_addr, external_addr, local_port,
832                                 external_port, vrf_id,
833                                 mp->flags & NAT_API_IS_ADDR_ONLY,
834                                 external_sw_if_index, proto,
835                                 mp->is_add, twice_nat,
836                                 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0,
837                                 pool_addr, 0);
838   vec_free (tag);
839
840   REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
841 }
842
843 static void
844   vl_api_nat44_add_del_static_mapping_v2_t_handler
845   (vl_api_nat44_add_del_static_mapping_v2_t * mp)
846 {
847   snat_main_t *sm = &snat_main;
848   vl_api_nat44_add_del_static_mapping_v2_reply_t *rmp;
849   ip4_address_t local_addr, external_addr, pool_addr;
850   u16 local_port = 0, external_port = 0;
851   u32 vrf_id, external_sw_if_index;
852   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
853   int rv = 0;
854   nat_protocol_t proto;
855   u8 *tag = 0;
856
857   memcpy (&pool_addr.as_u8, mp->pool_ip_address, 4);
858   memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
859   memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
860
861   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
862     {
863       local_port = mp->local_port;
864       external_port = mp->external_port;
865     }
866
867   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
868   external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
869   proto = ip_proto_to_nat_proto (mp->protocol);
870
871   if (mp->flags & NAT_API_IS_TWICE_NAT)
872     twice_nat = TWICE_NAT;
873   else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
874     twice_nat = TWICE_NAT_SELF;
875   mp->tag[sizeof (mp->tag) - 1] = 0;
876   tag = format (0, "%s", mp->tag);
877   vec_terminate_c_string (tag);
878
879   rv = snat_add_static_mapping (local_addr, external_addr, local_port,
880                                 external_port, vrf_id,
881                                 mp->flags & NAT_API_IS_ADDR_ONLY,
882                                 external_sw_if_index, proto,
883                                 mp->is_add, twice_nat,
884                                 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0,
885                                 pool_addr, mp->match_pool);
886   vec_free (tag);
887
888   REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_V2_REPLY);
889 }
890
891 static void
892 send_nat44_static_mapping_details (snat_static_mapping_t * m,
893                                    vl_api_registration_t * reg, u32 context)
894 {
895   vl_api_nat44_static_mapping_details_t *rmp;
896   snat_main_t *sm = &snat_main;
897   u32 len = sizeof (*rmp);
898
899   rmp = vl_msg_api_alloc (len);
900   clib_memset (rmp, 0, len);
901   rmp->_vl_msg_id =
902     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
903
904   clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
905   clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
906   rmp->external_sw_if_index = ~0;
907   rmp->vrf_id = htonl (m->vrf_id);
908   rmp->context = context;
909
910   if (m->twice_nat == TWICE_NAT)
911     rmp->flags |= NAT_API_IS_TWICE_NAT;
912   else if (m->twice_nat == TWICE_NAT_SELF)
913     rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
914
915   if (is_out2in_only_static_mapping (m))
916     rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
917
918   if (is_addr_only_static_mapping (m))
919     {
920       rmp->flags |= NAT_API_IS_ADDR_ONLY;
921     }
922   else
923     {
924       rmp->protocol = nat_proto_to_ip_proto (m->proto);
925       rmp->external_port = m->external_port;
926       rmp->local_port = m->local_port;
927     }
928
929   if (m->tag)
930     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
931
932   vl_api_send_msg (reg, (u8 *) rmp);
933 }
934
935 static void
936 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
937                                        vl_api_registration_t * reg,
938                                        u32 context)
939 {
940   vl_api_nat44_static_mapping_details_t *rmp;
941   snat_main_t *sm = &snat_main;
942
943   rmp = vl_msg_api_alloc (sizeof (*rmp));
944   clib_memset (rmp, 0, sizeof (*rmp));
945   rmp->_vl_msg_id =
946     ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
947   clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
948   rmp->external_sw_if_index = htonl (m->sw_if_index);
949   rmp->vrf_id = htonl (m->vrf_id);
950   rmp->context = context;
951
952   if (m->twice_nat)
953     rmp->flags |= NAT_API_IS_TWICE_NAT;
954
955   if (m->addr_only)
956     {
957       rmp->flags |= NAT_API_IS_ADDR_ONLY;
958     }
959   else
960     {
961       rmp->protocol = nat_proto_to_ip_proto (m->proto);
962       rmp->external_port = m->e_port;
963       rmp->local_port = m->l_port;
964     }
965   if (m->tag)
966     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
967
968   vl_api_send_msg (reg, (u8 *) rmp);
969 }
970
971 static void
972 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
973                                             * mp)
974 {
975   vl_api_registration_t *reg;
976   snat_main_t *sm = &snat_main;
977   snat_static_mapping_t *m;
978   snat_static_map_resolve_t *rp;
979   int j;
980
981   reg = vl_api_client_index_to_registration (mp->client_index);
982   if (!reg)
983     return;
984
985   /* *INDENT-OFF* */
986   pool_foreach (m, sm->static_mappings)
987    {
988       if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
989         send_nat44_static_mapping_details (m, reg, mp->context);
990   }
991   /* *INDENT-ON* */
992
993   for (j = 0; j < vec_len (sm->to_resolve); j++)
994     {
995       rp = sm->to_resolve + j;
996       if (!rp->identity_nat)
997         send_nat44_static_map_resolve_details (rp, reg, mp->context);
998     }
999 }
1000
1001 static void
1002   vl_api_nat44_add_del_identity_mapping_t_handler
1003   (vl_api_nat44_add_del_identity_mapping_t * mp)
1004 {
1005   snat_main_t *sm = &snat_main;
1006   vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
1007   ip4_address_t addr, pool_addr = { 0 };
1008   u16 port = 0;
1009   u32 vrf_id, sw_if_index;
1010   int rv = 0;
1011   nat_protocol_t proto = NAT_PROTOCOL_OTHER;
1012   u8 *tag = 0;
1013
1014   if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1015     {
1016       port = mp->port;
1017       proto = ip_proto_to_nat_proto (mp->protocol);
1018     }
1019   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1020   sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1021   if (sw_if_index != ~0)
1022     addr.as_u32 = 0;
1023   else
1024     memcpy (&addr.as_u8, mp->ip_address, 4);
1025   mp->tag[sizeof (mp->tag) - 1] = 0;
1026   tag = format (0, "%s", mp->tag);
1027   vec_terminate_c_string (tag);
1028
1029   rv =
1030     snat_add_static_mapping (addr, addr, port, port, vrf_id,
1031                              mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1032                              proto, mp->is_add, 0, 0, tag, 1, pool_addr, 0);
1033   vec_free (tag);
1034
1035   REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1036 }
1037
1038 static void
1039 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1040                                      vl_api_registration_t * reg, u32 context)
1041 {
1042   vl_api_nat44_identity_mapping_details_t *rmp;
1043   snat_main_t *sm = &snat_main;
1044   nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1045
1046   rmp = vl_msg_api_alloc (sizeof (*rmp));
1047   clib_memset (rmp, 0, sizeof (*rmp));
1048   rmp->_vl_msg_id =
1049     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1050
1051   if (is_addr_only_static_mapping (m))
1052     rmp->flags |= NAT_API_IS_ADDR_ONLY;
1053
1054   clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1055   rmp->port = m->local_port;
1056   rmp->sw_if_index = ~0;
1057   rmp->vrf_id = htonl (local->vrf_id);
1058   rmp->protocol = nat_proto_to_ip_proto (m->proto);
1059   rmp->context = context;
1060   if (m->tag)
1061     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1062
1063   vl_api_send_msg (reg, (u8 *) rmp);
1064 }
1065
1066 static void
1067 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1068                                          vl_api_registration_t * reg,
1069                                          u32 context)
1070 {
1071   vl_api_nat44_identity_mapping_details_t *rmp;
1072   snat_main_t *sm = &snat_main;
1073
1074   rmp = vl_msg_api_alloc (sizeof (*rmp));
1075   clib_memset (rmp, 0, sizeof (*rmp));
1076   rmp->_vl_msg_id =
1077     ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1078
1079   if (m->addr_only)
1080     rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1081
1082   rmp->port = m->l_port;
1083   rmp->sw_if_index = htonl (m->sw_if_index);
1084   rmp->vrf_id = htonl (m->vrf_id);
1085   rmp->protocol = nat_proto_to_ip_proto (m->proto);
1086   rmp->context = context;
1087   if (m->tag)
1088     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1089
1090   vl_api_send_msg (reg, (u8 *) rmp);
1091 }
1092
1093 static void
1094   vl_api_nat44_identity_mapping_dump_t_handler
1095   (vl_api_nat44_identity_mapping_dump_t * mp)
1096 {
1097   vl_api_registration_t *reg;
1098   snat_main_t *sm = &snat_main;
1099   snat_static_mapping_t *m;
1100   snat_static_map_resolve_t *rp;
1101   int j;
1102
1103   reg = vl_api_client_index_to_registration (mp->client_index);
1104   if (!reg)
1105     return;
1106
1107   /* *INDENT-OFF* */
1108   pool_foreach (m, sm->static_mappings)
1109    {
1110       if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1111         {
1112           pool_foreach_index (j, m->locals)
1113            {
1114             send_nat44_identity_mapping_details (m, j, reg, mp->context);
1115           }
1116         }
1117   }
1118   /* *INDENT-ON* */
1119
1120   for (j = 0; j < vec_len (sm->to_resolve); j++)
1121     {
1122       rp = sm->to_resolve + j;
1123       if (rp->identity_nat)
1124         send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1125     }
1126 }
1127
1128 static void
1129   vl_api_nat44_add_del_interface_addr_t_handler
1130   (vl_api_nat44_add_del_interface_addr_t * mp)
1131 {
1132   snat_main_t *sm = &snat_main;
1133   vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1134   u32 sw_if_index = ntohl (mp->sw_if_index);
1135   int rv = 0;
1136   u8 is_del;
1137
1138   is_del = !mp->is_add;
1139
1140   VALIDATE_SW_IF_INDEX (mp);
1141
1142   rv = snat_add_interface_address (sm, sw_if_index, is_del,
1143                                    mp->flags & NAT_API_IS_TWICE_NAT);
1144
1145   BAD_SW_IF_INDEX_LABEL;
1146   REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1147 }
1148
1149 static void
1150 send_nat44_interface_addr_details (u32 sw_if_index,
1151                                    vl_api_registration_t * reg, u32 context,
1152                                    u8 twice_nat)
1153 {
1154   vl_api_nat44_interface_addr_details_t *rmp;
1155   snat_main_t *sm = &snat_main;
1156
1157   rmp = vl_msg_api_alloc (sizeof (*rmp));
1158   clib_memset (rmp, 0, sizeof (*rmp));
1159   rmp->_vl_msg_id =
1160     ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1161   rmp->sw_if_index = ntohl (sw_if_index);
1162
1163   if (twice_nat)
1164     rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1165   rmp->context = context;
1166
1167   vl_api_send_msg (reg, (u8 *) rmp);
1168 }
1169
1170 static void
1171 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1172                                             * mp)
1173 {
1174   vl_api_registration_t *reg;
1175   snat_main_t *sm = &snat_main;
1176   u32 *i;
1177
1178   reg = vl_api_client_index_to_registration (mp->client_index);
1179   if (!reg)
1180     return;
1181
1182   /* *INDENT-OFF* */
1183   vec_foreach (i, sm->auto_add_sw_if_indices)
1184     send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1185   vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1186     send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1187   /* *INDENT-ON* */
1188 }
1189
1190 static void
1191 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1192                          u32 context)
1193 {
1194   vl_api_nat44_user_details_t *rmp;
1195   snat_main_t *sm = &snat_main;
1196   ip4_main_t *im = &ip4_main;
1197
1198   rmp = vl_msg_api_alloc (sizeof (*rmp));
1199   clib_memset (rmp, 0, sizeof (*rmp));
1200   rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1201
1202   if (!pool_is_free_index (im->fibs, u->fib_index))
1203     {
1204       fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1205       rmp->vrf_id = ntohl (fib->ft_table_id);
1206     }
1207
1208   clib_memcpy (rmp->ip_address, &(u->addr), 4);
1209   rmp->nsessions = ntohl (u->nsessions);
1210   rmp->nstaticsessions = ntohl (u->nstaticsessions);
1211   rmp->context = context;
1212
1213   vl_api_send_msg (reg, (u8 *) rmp);
1214 }
1215
1216 static void
1217 nat_ed_user_create_helper (snat_main_per_thread_data_t * tsm,
1218                            snat_session_t * s)
1219 {
1220   snat_user_key_t k;
1221   k.addr = s->in2out.addr;
1222   k.fib_index = s->in2out.fib_index;
1223   clib_bihash_kv_8_8_t key, value;
1224   key.key = k.as_u64;
1225   snat_user_t *u;
1226   if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1227     {
1228       pool_get (tsm->users, u);
1229       u->addr = k.addr;
1230       u->fib_index = k.fib_index;
1231       u->nsessions = 0;
1232       u->nstaticsessions = 0;
1233       key.value = u - tsm->users;
1234       clib_bihash_add_del_8_8 (&tsm->user_hash, &key, 1);
1235     }
1236   else
1237     {
1238       u = pool_elt_at_index (tsm->users, value.value);
1239     }
1240   if (snat_is_session_static (s))
1241     {
1242       ++u->nstaticsessions;
1243     }
1244   else
1245     {
1246       ++u->nsessions;
1247     }
1248 }
1249
1250 static void
1251 nat_ed_users_create (snat_main_per_thread_data_t * tsm)
1252 {
1253   snat_session_t *s;
1254   /* *INDENT-OFF* */
1255   pool_foreach (s, tsm->sessions) { nat_ed_user_create_helper (tsm, s); }
1256   /* *INDENT-ON* */
1257 }
1258
1259 static void
1260 nat_ed_users_destroy (snat_main_per_thread_data_t * tsm)
1261 {
1262   snat_user_t *u;
1263   /* *INDENT-OFF* */
1264   pool_flush (u, tsm->users, { });
1265   /* *INDENT-ON* */
1266   clib_bihash_free_8_8 (&tsm->user_hash);
1267   clib_bihash_init_8_8 (&tsm->user_hash, "users", snat_main.user_buckets, 0);
1268   clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
1269 }
1270
1271 static void
1272 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1273 {
1274   vl_api_registration_t *reg;
1275   snat_main_t *sm = &snat_main;
1276   snat_main_per_thread_data_t *tsm;
1277   snat_user_t *u;
1278
1279   reg = vl_api_client_index_to_registration (mp->client_index);
1280   if (!reg)
1281     return;
1282
1283   /* *INDENT-OFF* */
1284   vec_foreach (tsm, sm->per_thread_data)
1285     {
1286       if (sm->endpoint_dependent)
1287         {
1288           nat_ed_users_create (tsm);
1289         }
1290       pool_foreach (u, tsm->users)
1291        {
1292         send_nat44_user_details (u, reg, mp->context);
1293       }
1294       if (sm->endpoint_dependent)
1295         {
1296           nat_ed_users_destroy (tsm);
1297         }
1298     }
1299   /* *INDENT-ON* */
1300 }
1301
1302 static void
1303 send_nat44_user_session_details (snat_session_t * s,
1304                                  vl_api_registration_t * reg, u32 context)
1305 {
1306   vl_api_nat44_user_session_details_t *rmp;
1307   snat_main_t *sm = &snat_main;
1308
1309   rmp = vl_msg_api_alloc (sizeof (*rmp));
1310   clib_memset (rmp, 0, sizeof (*rmp));
1311   rmp->_vl_msg_id =
1312     ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1313   clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1314   clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1315
1316   if (snat_is_session_static (s))
1317     rmp->flags |= NAT_API_IS_STATIC;
1318
1319   if (is_twice_nat_session (s))
1320     rmp->flags |= NAT_API_IS_TWICE_NAT;
1321
1322   if (is_ed_session (s) || is_fwd_bypass_session (s))
1323     rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1324
1325   rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1326   rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1327   rmp->total_pkts = ntohl (s->total_pkts);
1328   rmp->context = context;
1329   if (snat_is_unk_proto_session (s))
1330     {
1331       rmp->outside_port = 0;
1332       rmp->inside_port = 0;
1333       rmp->protocol = ntohs (s->in2out.port);
1334     }
1335   else
1336     {
1337       rmp->outside_port = s->out2in.port;
1338       rmp->inside_port = s->in2out.port;
1339       rmp->protocol = ntohs (nat_proto_to_ip_proto (s->nat_proto));
1340     }
1341   if (is_ed_session (s) || is_fwd_bypass_session (s))
1342     {
1343       clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1344       rmp->ext_host_port = s->ext_host_port;
1345       if (is_twice_nat_session (s))
1346         {
1347           clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1348           rmp->ext_host_nat_port = s->ext_host_nat_port;
1349         }
1350     }
1351
1352   vl_api_send_msg (reg, (u8 *) rmp);
1353 }
1354
1355 static void
1356 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1357                                           mp)
1358 {
1359   vl_api_registration_t *reg;
1360   snat_main_t *sm = &snat_main;
1361   snat_main_per_thread_data_t *tsm;
1362   snat_session_t *s;
1363   clib_bihash_kv_8_8_t key, value;
1364   snat_user_key_t ukey;
1365   snat_user_t *u;
1366   u32 session_index, head_index, elt_index;
1367   dlist_elt_t *head, *elt;
1368   ip4_header_t ip;
1369
1370   reg = vl_api_client_index_to_registration (mp->client_index);
1371   if (!reg)
1372     return;
1373
1374   clib_memcpy (&ukey.addr, mp->ip_address, 4);
1375   ip.src_address.as_u32 = ukey.addr.as_u32;
1376   ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1377   key.key = ukey.as_u64;
1378   if (sm->num_workers > 1)
1379     tsm =
1380       vec_elt_at_index (sm->per_thread_data,
1381                         sm->worker_in2out_cb (&ip, ukey.fib_index, 0));
1382   else
1383     tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1384   if (!sm->endpoint_dependent)
1385     {
1386       if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1387         return;
1388       u = pool_elt_at_index (tsm->users, value.value);
1389       if (!u->nsessions && !u->nstaticsessions)
1390         return;
1391
1392       head_index = u->sessions_per_user_list_head_index;
1393       head = pool_elt_at_index (tsm->list_pool, head_index);
1394       elt_index = head->next;
1395       elt = pool_elt_at_index (tsm->list_pool, elt_index);
1396       session_index = elt->value;
1397       while (session_index != ~0)
1398         {
1399           s = pool_elt_at_index (tsm->sessions, session_index);
1400
1401           send_nat44_user_session_details (s, reg, mp->context);
1402
1403           elt_index = elt->next;
1404           elt = pool_elt_at_index (tsm->list_pool, elt_index);
1405           session_index = elt->value;
1406         }
1407     }
1408   else
1409     {
1410       /* *INDENT-OFF* */
1411       pool_foreach (s, tsm->sessions) {
1412         if (s->in2out.addr.as_u32 == ukey.addr.as_u32)
1413           {
1414             send_nat44_user_session_details (s, reg, mp->context);
1415           }
1416       }
1417       /* *INDENT-ON* */
1418     }
1419 }
1420
1421 static nat44_lb_addr_port_t *
1422 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1423                              u32 addr_port_pair_num)
1424 {
1425   u8 i;
1426   nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1427   vl_api_nat44_lb_addr_port_t *ap;
1428
1429   for (i = 0; i < addr_port_pair_num; i++)
1430     {
1431       ap = &addr_port_pairs[i];
1432       clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1433       clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1434       lb_addr_port.port = ap->port;
1435       lb_addr_port.probability = ap->probability;
1436       lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1437       vec_add1 (lb_addr_port_pairs, lb_addr_port);
1438     }
1439
1440   return lb_addr_port_pairs;
1441 }
1442
1443 static void
1444   vl_api_nat44_add_del_lb_static_mapping_t_handler
1445   (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1446 {
1447   snat_main_t *sm = &snat_main;
1448   vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1449   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1450   int rv = 0;
1451   nat44_lb_addr_port_t *locals = 0;
1452   ip4_address_t e_addr;
1453   nat_protocol_t proto;
1454   u8 *tag = 0;
1455
1456   if (!sm->endpoint_dependent)
1457     {
1458       rv = VNET_API_ERROR_UNSUPPORTED;
1459       goto send_reply;
1460     }
1461
1462   locals =
1463     unformat_nat44_lb_addr_port (mp->locals,
1464                                  clib_net_to_host_u32 (mp->local_num));
1465   clib_memcpy (&e_addr, mp->external_addr, 4);
1466   proto = ip_proto_to_nat_proto (mp->protocol);
1467
1468   if (mp->flags & NAT_API_IS_TWICE_NAT)
1469     twice_nat = TWICE_NAT;
1470   else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1471     twice_nat = TWICE_NAT_SELF;
1472   mp->tag[sizeof (mp->tag) - 1] = 0;
1473   tag = format (0, "%s", mp->tag);
1474   vec_terminate_c_string (tag);
1475
1476   rv =
1477     nat44_add_del_lb_static_mapping (e_addr,
1478                                      mp->external_port,
1479                                      proto, locals, mp->is_add,
1480                                      twice_nat,
1481                                      mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1482                                      clib_net_to_host_u32 (mp->affinity));
1483
1484   vec_free (locals);
1485   vec_free (tag);
1486
1487 send_reply:
1488   REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1489 }
1490
1491 static void
1492   vl_api_nat44_lb_static_mapping_add_del_local_t_handler
1493   (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
1494 {
1495   snat_main_t *sm = &snat_main;
1496   vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
1497   int rv = 0;
1498   ip4_address_t e_addr, l_addr;
1499   nat_protocol_t proto;
1500
1501   if (!sm->endpoint_dependent)
1502     {
1503       rv = VNET_API_ERROR_UNSUPPORTED;
1504       goto send_reply;
1505     }
1506
1507   clib_memcpy (&e_addr, mp->external_addr, 4);
1508   clib_memcpy (&l_addr, mp->local.addr, 4);
1509   proto = ip_proto_to_nat_proto (mp->protocol);
1510
1511   rv =
1512     nat44_lb_static_mapping_add_del_local (e_addr,
1513                                            mp->external_port,
1514                                            l_addr,
1515                                            mp->local.port,
1516                                            proto,
1517                                            clib_net_to_host_u32 (mp->
1518                                                                  local.vrf_id),
1519                                            mp->local.probability, mp->is_add);
1520
1521 send_reply:
1522   REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
1523 }
1524
1525 static void
1526 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1527                                       vl_api_registration_t * reg,
1528                                       u32 context)
1529 {
1530   vl_api_nat44_lb_static_mapping_details_t *rmp;
1531   snat_main_t *sm = &snat_main;
1532   nat44_lb_addr_port_t *ap;
1533   vl_api_nat44_lb_addr_port_t *locals;
1534   u32 local_num = 0;
1535
1536   rmp =
1537     vl_msg_api_alloc (sizeof (*rmp) +
1538                       (pool_elts (m->locals) *
1539                        sizeof (nat44_lb_addr_port_t)));
1540   clib_memset (rmp, 0, sizeof (*rmp));
1541   rmp->_vl_msg_id =
1542     ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1543
1544   clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1545   rmp->external_port = m->external_port;
1546   rmp->protocol = nat_proto_to_ip_proto (m->proto);
1547   rmp->context = context;
1548
1549   if (m->twice_nat == TWICE_NAT)
1550     rmp->flags |= NAT_API_IS_TWICE_NAT;
1551   else if (m->twice_nat == TWICE_NAT_SELF)
1552     rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1553   if (is_out2in_only_static_mapping (m))
1554     rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1555   if (m->tag)
1556     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1557
1558   locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
1559   /* *INDENT-OFF* */
1560   pool_foreach (ap, m->locals)
1561    {
1562     clib_memcpy (locals->addr, &(ap->addr), 4);
1563     locals->port = ap->port;
1564     locals->probability = ap->probability;
1565     locals->vrf_id = ntohl (ap->vrf_id);
1566     locals++;
1567     local_num++;
1568   }
1569   /* *INDENT-ON* */
1570   rmp->local_num = ntohl (local_num);
1571
1572   vl_api_send_msg (reg, (u8 *) rmp);
1573 }
1574
1575 static void
1576   vl_api_nat44_lb_static_mapping_dump_t_handler
1577   (vl_api_nat44_lb_static_mapping_dump_t * mp)
1578 {
1579   vl_api_registration_t *reg;
1580   snat_main_t *sm = &snat_main;
1581   snat_static_mapping_t *m;
1582
1583   if (!sm->endpoint_dependent)
1584     return;
1585
1586   reg = vl_api_client_index_to_registration (mp->client_index);
1587   if (!reg)
1588     return;
1589
1590   /* *INDENT-OFF* */
1591   pool_foreach (m, sm->static_mappings)
1592    {
1593       if (is_lb_static_mapping(m))
1594         send_nat44_lb_static_mapping_details (m, reg, mp->context);
1595   }
1596   /* *INDENT-ON* */
1597 }
1598
1599 static void
1600 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
1601 {
1602   snat_main_t *sm = &snat_main;
1603   vl_api_nat44_del_session_reply_t *rmp;
1604   ip4_address_t addr, eh_addr;
1605   u16 port, eh_port;
1606   u32 vrf_id;
1607   int rv = 0;
1608   u8 is_in;
1609   nat_protocol_t proto;
1610
1611   memcpy (&addr.as_u8, mp->address, 4);
1612   port = mp->port;
1613   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1614   proto = ip_proto_to_nat_proto (mp->protocol);
1615   memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
1616   eh_port = mp->ext_host_port;
1617
1618   is_in = mp->flags & NAT_API_IS_INSIDE;
1619
1620   if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
1621     rv =
1622       nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
1623                             vrf_id, is_in);
1624   else
1625     rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
1626
1627   REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
1628 }
1629
1630 static void
1631   vl_api_nat44_forwarding_enable_disable_t_handler
1632   (vl_api_nat44_forwarding_enable_disable_t * mp)
1633 {
1634   snat_main_t *sm = &snat_main;
1635   vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
1636   int rv = 0;
1637   u32 *ses_to_be_removed = 0, *ses_index;
1638   snat_main_per_thread_data_t *tsm;
1639   snat_session_t *s;
1640
1641   sm->forwarding_enabled = mp->enable != 0;
1642
1643   if (mp->enable == 0)
1644     {
1645       /* *INDENT-OFF* */
1646       vec_foreach (tsm, sm->per_thread_data)
1647       {
1648         pool_foreach (s, tsm->sessions)
1649          {
1650           if (is_fwd_bypass_session(s))
1651             {
1652               vec_add1 (ses_to_be_removed, s - tsm->sessions);
1653             }
1654         }
1655         if(sm->endpoint_dependent){
1656             vec_foreach (ses_index, ses_to_be_removed)
1657               {
1658                 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
1659                 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1660                 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1661               }
1662         }else{
1663             vec_foreach (ses_index, ses_to_be_removed)
1664               {
1665                 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
1666                 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1667                 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1668               }
1669         }
1670         vec_free (ses_to_be_removed);
1671       }
1672       /* *INDENT-ON* */
1673     }
1674
1675   REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
1676 }
1677
1678 static void
1679   vl_api_nat44_forwarding_is_enabled_t_handler
1680   (vl_api_nat44_forwarding_is_enabled_t * mp)
1681 {
1682   vl_api_registration_t *reg;
1683   snat_main_t *sm = &snat_main;
1684   vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
1685
1686   reg = vl_api_client_index_to_registration (mp->client_index);
1687   if (!reg)
1688     return;
1689
1690   rmp = vl_msg_api_alloc (sizeof (*rmp));
1691   clib_memset (rmp, 0, sizeof (*rmp));
1692   rmp->_vl_msg_id =
1693     ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
1694   rmp->context = mp->context;
1695
1696   rmp->enabled = sm->forwarding_enabled;
1697
1698   vl_api_send_msg (reg, (u8 *) rmp);
1699 }
1700
1701 /* API definitions */
1702 #include <vnet/format_fns.h>
1703 #include <nat/nat44.api.c>
1704
1705 /* Set up the API message handling tables */
1706 clib_error_t *
1707 nat44_api_hookup (vlib_main_t * vm)
1708 {
1709   snat_main_t *sm = &snat_main;
1710   sm->msg_id_base = setup_message_id_table ();
1711   return 0;
1712 }
1713
1714 /*
1715  * fd.io coding-style-patch-verification: ON
1716  *
1717  * Local Variables:
1718  * eval: (c-set-style "gnu")
1719  * End:
1720  */