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