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