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