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