Fix some issue of comment and help for nat feature
[vpp.git] / src / plugins / nat / nat.c
1 /*
2  * snat.c - simple nat plugin
3  *
4  * Copyright (c) 2016 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/plugin/plugin.h>
22 #include <nat/nat.h>
23 #include <nat/nat_ipfix_logging.h>
24 #include <nat/nat_det.h>
25 #include <nat/nat64.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/fib/ip4_fib.h>
28
29 #include <vpp/app/version.h>
30
31 snat_main_t snat_main;
32
33
34 /* Hook up input features */
35 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
36   .arc_name = "ip4-unicast",
37   .node_name = "nat44-in2out",
38   .runs_before = VNET_FEATURES ("nat44-out2in"),
39 };
40 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
41   .arc_name = "ip4-unicast",
42   .node_name = "nat44-out2in",
43   .runs_before = VNET_FEATURES ("ip4-lookup"),
44 };
45 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
46   .arc_name = "ip4-unicast",
47   .node_name = "nat44-det-in2out",
48   .runs_before = VNET_FEATURES ("nat44-det-out2in"),
49 };
50 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
51   .arc_name = "ip4-unicast",
52   .node_name = "nat44-det-out2in",
53   .runs_before = VNET_FEATURES ("ip4-lookup"),
54 };
55 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
56   .arc_name = "ip4-unicast",
57   .node_name = "nat44-in2out-worker-handoff",
58   .runs_before = VNET_FEATURES ("nat44-out2in-worker-handoff"),
59 };
60 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
61   .arc_name = "ip4-unicast",
62   .node_name = "nat44-out2in-worker-handoff",
63   .runs_before = VNET_FEATURES ("ip4-lookup"),
64 };
65 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
66   .arc_name = "ip4-unicast",
67   .node_name = "nat44-in2out-fast",
68   .runs_before = VNET_FEATURES ("nat44-out2in-fast"),
69 };
70 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
71   .arc_name = "ip4-unicast",
72   .node_name = "nat44-out2in-fast",
73   .runs_before = VNET_FEATURES ("ip4-lookup"),
74 };
75 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
76   .arc_name = "ip4-unicast",
77   .node_name = "nat44-hairpin-dst",
78   .runs_before = VNET_FEATURES ("ip4-lookup"),
79 };
80
81 /* Hook up output features */
82 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
83   .arc_name = "ip4-output",
84   .node_name = "nat44-in2out-output",
85   .runs_before = VNET_FEATURES ("interface-output"),
86 };
87 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
88   .arc_name = "ip4-output",
89   .node_name = "nat44-in2out-output-worker-handoff",
90   .runs_before = VNET_FEATURES ("interface-output"),
91 };
92 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
93   .arc_name = "ip4-output",
94   .node_name = "nat44-hairpin-src",
95   .runs_before = VNET_FEATURES ("interface-output"),
96 };
97
98
99 /* *INDENT-OFF* */
100 VLIB_PLUGIN_REGISTER () = {
101     .version = VPP_BUILD_VER,
102     .description = "Network Address Translation",
103 };
104 /* *INDENT-ON* */
105
106 /**
107  * @brief Add/del NAT address to FIB.
108  *
109  * Add the external NAT address to the FIB as receive entries. This ensures
110  * that VPP will reply to ARP for this address and we don't need to enable
111  * proxy ARP on the outside interface.
112  *
113  * @param addr IPv4 address.
114  * @param plen address prefix length
115  * @param sw_if_index Interface.
116  * @param is_add If 0 delete, otherwise add.
117  */
118 void
119 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
120                           int is_add)
121 {
122   fib_prefix_t prefix = {
123     .fp_len = p_len,
124     .fp_proto = FIB_PROTOCOL_IP4,
125     .fp_addr = {
126         .ip4.as_u32 = addr->as_u32,
127     },
128   };
129   u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
130
131   if (is_add)
132     fib_table_entry_update_one_path(fib_index,
133                                     &prefix,
134                                     FIB_SOURCE_PLUGIN_HI,
135                                     (FIB_ENTRY_FLAG_CONNECTED |
136                                      FIB_ENTRY_FLAG_LOCAL |
137                                      FIB_ENTRY_FLAG_EXCLUSIVE),
138                                     DPO_PROTO_IP4,
139                                     NULL,
140                                     sw_if_index,
141                                     ~0,
142                                     1,
143                                     NULL,
144                                     FIB_ROUTE_PATH_FLAG_NONE);
145   else
146     fib_table_entry_delete(fib_index,
147                            &prefix,
148                            FIB_SOURCE_PLUGIN_HI);
149 }
150
151 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
152 {
153   snat_address_t * ap;
154   snat_interface_t *i;
155
156   if (vrf_id != ~0)
157     sm->vrf_mode = 1;
158
159   /* Check if address already exists */
160   vec_foreach (ap, sm->addresses)
161     {
162       if (ap->addr.as_u32 == addr->as_u32)
163         return;
164     }
165
166   vec_add2 (sm->addresses, ap, 1);
167   ap->addr = *addr;
168   if (vrf_id != ~0)
169     ap->fib_index =
170       fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id);
171   else
172     ap->fib_index = ~0;
173 #define _(N, i, n, s) \
174   clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535);
175   foreach_snat_protocol
176 #undef _
177
178   /* Add external address to FIB */
179   pool_foreach (i, sm->interfaces,
180   ({
181     if (i->is_inside)
182       continue;
183
184     snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
185     break;
186   }));
187   pool_foreach (i, sm->output_feature_interfaces,
188   ({
189     if (i->is_inside)
190       continue;
191
192     snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
193     break;
194   }));
195 }
196
197 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
198                                                    ip4_address_t addr)
199 {
200   snat_static_mapping_t *m;
201   pool_foreach (m, sm->static_mappings,
202   ({
203       if (m->external_addr.as_u32 == addr.as_u32)
204         return 1;
205   }));
206
207   return 0;
208 }
209
210 void increment_v4_address (ip4_address_t * a)
211 {
212   u32 v;
213
214   v = clib_net_to_host_u32(a->as_u32) + 1;
215   a->as_u32 = clib_host_to_net_u32(v);
216 }
217
218 static void
219 snat_add_static_mapping_when_resolved (snat_main_t * sm,
220                                        ip4_address_t l_addr,
221                                        u16 l_port,
222                                        u32 sw_if_index,
223                                        u16 e_port,
224                                        u32 vrf_id,
225                                        snat_protocol_t proto,
226                                        int addr_only,
227                                        int is_add)
228 {
229   snat_static_map_resolve_t *rp;
230
231   vec_add2 (sm->to_resolve, rp, 1);
232   rp->l_addr.as_u32 = l_addr.as_u32;
233   rp->l_port = l_port;
234   rp->sw_if_index = sw_if_index;
235   rp->e_port = e_port;
236   rp->vrf_id = vrf_id;
237   rp->proto = proto;
238   rp->addr_only = addr_only;
239   rp->is_add = is_add;
240 }
241
242 /**
243  * @brief Add static mapping.
244  *
245  * Create static mapping between local addr+port and external addr+port.
246  *
247  * @param l_addr Local IPv4 address.
248  * @param e_addr External IPv4 address.
249  * @param l_port Local port number.
250  * @param e_port External port number.
251  * @param vrf_id VRF ID.
252  * @param addr_only If 0 address port and pair mapping, otherwise address only.
253  * @param sw_if_index External port instead of specific IP address.
254  * @param is_add If 0 delete static mapping, otherwise add.
255  *
256  * @returns
257  */
258 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
259                             u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
260                             u32 sw_if_index, snat_protocol_t proto, int is_add)
261 {
262   snat_main_t * sm = &snat_main;
263   snat_static_mapping_t *m;
264   snat_session_key_t m_key;
265   clib_bihash_kv_8_8_t kv, value;
266   snat_address_t *a = 0;
267   u32 fib_index = ~0;
268   uword * p;
269   snat_interface_t *interface;
270   int i;
271
272   /* If the external address is a specific interface address */
273   if (sw_if_index != ~0)
274     {
275       ip4_address_t * first_int_addr;
276
277       /* Might be already set... */
278       first_int_addr = ip4_interface_first_address
279         (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
280
281       /* DHCP resolution required? */
282       if (first_int_addr == 0)
283         {
284           snat_add_static_mapping_when_resolved
285             (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
286              addr_only,  is_add);
287           return 0;
288         }
289         else
290           e_addr.as_u32 = first_int_addr->as_u32;
291     }
292
293   m_key.addr = e_addr;
294   m_key.port = addr_only ? 0 : e_port;
295   m_key.protocol = addr_only ? 0 : proto;
296   m_key.fib_index = sm->outside_fib_index;
297   kv.key = m_key.as_u64;
298   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
299     m = 0;
300   else
301     m = pool_elt_at_index (sm->static_mappings, value.value);
302
303   if (is_add)
304     {
305       if (m)
306         return VNET_API_ERROR_VALUE_EXIST;
307
308       /* Convert VRF id to FIB index */
309       if (vrf_id != ~0)
310         {
311           p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
312           if (!p)
313             return VNET_API_ERROR_NO_SUCH_FIB;
314           fib_index = p[0];
315         }
316       /* If not specified use inside VRF id from SNAT plugin startup config */
317       else
318         {
319           fib_index = sm->inside_fib_index;
320           vrf_id = sm->inside_vrf_id;
321         }
322
323       /* Find external address in allocated addresses and reserve port for
324          address and port pair mapping when dynamic translations enabled */
325       if (!addr_only && !(sm->static_mapping_only))
326         {
327           for (i = 0; i < vec_len (sm->addresses); i++)
328             {
329               if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
330                 {
331                   a = sm->addresses + i;
332                   /* External port must be unused */
333                   switch (proto)
334                     {
335 #define _(N, j, n, s) \
336                     case SNAT_PROTOCOL_##N: \
337                       if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
338                         return VNET_API_ERROR_INVALID_VALUE; \
339                       clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
340                       if (e_port > 1024) \
341                         a->busy_##n##_ports++; \
342                       break;
343                       foreach_snat_protocol
344 #undef _
345                     default:
346                       clib_warning("unknown_protocol");
347                       return VNET_API_ERROR_INVALID_VALUE_2;
348                     }
349                   break;
350                 }
351             }
352           /* External address must be allocated */
353           if (!a)
354             return VNET_API_ERROR_NO_SUCH_ENTRY;
355         }
356
357       pool_get (sm->static_mappings, m);
358       memset (m, 0, sizeof (*m));
359       m->local_addr = l_addr;
360       m->external_addr = e_addr;
361       m->addr_only = addr_only;
362       m->vrf_id = vrf_id;
363       m->fib_index = fib_index;
364       if (!addr_only)
365         {
366           m->local_port = l_port;
367           m->external_port = e_port;
368           m->proto = proto;
369         }
370
371       m_key.addr = m->local_addr;
372       m_key.port = m->local_port;
373       m_key.protocol = m->proto;
374       m_key.fib_index = m->fib_index;
375       kv.key = m_key.as_u64;
376       kv.value = m - sm->static_mappings;
377       clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
378
379       m_key.addr = m->external_addr;
380       m_key.port = m->external_port;
381       m_key.fib_index = sm->outside_fib_index;
382       kv.key = m_key.as_u64;
383       kv.value = m - sm->static_mappings;
384       clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
385
386       /* Assign worker */
387       if (sm->workers)
388         {
389           snat_user_key_t w_key0;
390           snat_worker_key_t w_key1;
391
392           w_key0.addr = m->local_addr;
393           w_key0.fib_index = m->fib_index;
394           kv.key = w_key0.as_u64;
395
396           if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
397             {
398               kv.value = sm->first_worker_index +
399                 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
400
401               clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
402             }
403           else
404             {
405               kv.value = value.value;
406             }
407
408           w_key1.addr = m->external_addr;
409           w_key1.port = clib_host_to_net_u16 (m->external_port);
410           w_key1.fib_index = sm->outside_fib_index;
411           kv.key = w_key1.as_u64;
412           clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
413         }
414     }
415   else
416     {
417       if (!m)
418         return VNET_API_ERROR_NO_SUCH_ENTRY;
419
420       /* Free external address port */
421       if (!addr_only && !(sm->static_mapping_only))
422         {
423           for (i = 0; i < vec_len (sm->addresses); i++)
424             {
425               if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
426                 {
427                   a = sm->addresses + i;
428                   switch (proto)
429                     {
430 #define _(N, j, n, s) \
431                     case SNAT_PROTOCOL_##N: \
432                       clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
433                       if (e_port > 1024) \
434                         a->busy_##n##_ports--; \
435                       break;
436                       foreach_snat_protocol
437 #undef _
438                     default:
439                       clib_warning("unknown_protocol");
440                       return VNET_API_ERROR_INVALID_VALUE_2;
441                     }
442                   break;
443                 }
444             }
445         }
446
447       m_key.addr = m->local_addr;
448       m_key.port = m->local_port;
449       m_key.protocol = m->proto;
450       m_key.fib_index = m->fib_index;
451       kv.key = m_key.as_u64;
452       clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
453
454       m_key.addr = m->external_addr;
455       m_key.port = m->external_port;
456       m_key.fib_index = sm->outside_fib_index;
457       kv.key = m_key.as_u64;
458       clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
459
460       /* Delete session(s) for static mapping if exist */
461       if (!(sm->static_mapping_only) ||
462           (sm->static_mapping_only && sm->static_mapping_connection_tracking))
463         {
464           snat_user_key_t u_key;
465           snat_user_t *u;
466           dlist_elt_t * head, * elt;
467           u32 elt_index, head_index, del_elt_index;
468           u32 ses_index;
469           u64 user_index;
470           snat_session_t * s;
471           snat_main_per_thread_data_t *tsm;
472
473           u_key.addr = m->local_addr;
474           u_key.fib_index = m->fib_index;
475           kv.key = u_key.as_u64;
476           if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
477             {
478               user_index = value.value;
479               if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
480                 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
481               else
482                 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
483               u = pool_elt_at_index (tsm->users, user_index);
484               if (u->nstaticsessions)
485                 {
486                   head_index = u->sessions_per_user_list_head_index;
487                   head = pool_elt_at_index (tsm->list_pool, head_index);
488                   elt_index = head->next;
489                   elt = pool_elt_at_index (tsm->list_pool, elt_index);
490                   ses_index = elt->value;
491                   while (ses_index != ~0)
492                     {
493                       s =  pool_elt_at_index (tsm->sessions, ses_index);
494                       del_elt_index = elt_index;
495                       elt_index = elt->next;
496                       elt = pool_elt_at_index (tsm->list_pool, elt_index);
497                       ses_index = elt->value;
498
499                       if (!addr_only)
500                         {
501                           if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
502                               (clib_net_to_host_u16 (s->out2in.port) != e_port))
503                             continue;
504                         }
505
506                       if (snat_is_unk_proto_session (s))
507                         {
508                           clib_bihash_kv_16_8_t up_kv;
509                           snat_unk_proto_ses_key_t up_key;
510                           up_key.l_addr = s->in2out.addr;
511                           up_key.r_addr = s->ext_host_addr;
512                           up_key.fib_index = s->in2out.fib_index;
513                           up_key.proto = s->in2out.port;
514                           up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
515                           up_kv.key[0] = up_key.as_u64[0];
516                           up_kv.key[1] = up_key.as_u64[1];
517                           if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
518                                                         &up_kv, 0))
519                             clib_warning ("in2out key del failed");
520
521                           up_key.l_addr = s->out2in.addr;
522                           up_key.fib_index = s->out2in.fib_index;
523                           up_kv.key[0] = up_key.as_u64[0];
524                           up_kv.key[1] = up_key.as_u64[1];
525                           if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
526                                                         &up_kv, 0))
527                             clib_warning ("out2in key del failed");
528
529                           goto delete;
530                         }
531                       /* log NAT event */
532                       snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
533                                                           s->out2in.addr.as_u32,
534                                                           s->in2out.protocol,
535                                                           s->in2out.port,
536                                                           s->out2in.port,
537                                                           s->in2out.fib_index);
538
539                       value.key = s->in2out.as_u64;
540                       if (clib_bihash_add_del_8_8 (&sm->in2out, &value, 0))
541                         clib_warning ("in2out key del failed");
542                       value.key = s->out2in.as_u64;
543                       if (clib_bihash_add_del_8_8 (&sm->out2in, &value, 0))
544                         clib_warning ("out2in key del failed");
545 delete:
546                       pool_put (tsm->sessions, s);
547
548                       clib_dlist_remove (tsm->list_pool, del_elt_index);
549                       pool_put_index (tsm->list_pool, del_elt_index);
550                       u->nstaticsessions--;
551
552                       if (!addr_only)
553                         break;
554                     }
555                   if (addr_only)
556                     {
557                       pool_put (tsm->users, u);
558                       clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
559                     }
560                 }
561             }
562         }
563
564       /* Delete static mapping from pool */
565       pool_put (sm->static_mappings, m);
566     }
567
568   if (!addr_only)
569     return 0;
570
571   /* Add/delete external address to FIB */
572   pool_foreach (interface, sm->interfaces,
573   ({
574     if (interface->is_inside)
575       continue;
576
577     snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
578     break;
579   }));
580   pool_foreach (interface, sm->output_feature_interfaces,
581   ({
582     if (interface->is_inside)
583       continue;
584
585     snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
586     break;
587   }));
588
589   return 0;
590 }
591
592 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
593 {
594   snat_address_t *a = 0;
595   snat_session_t *ses;
596   u32 *ses_to_be_removed = 0, *ses_index;
597   clib_bihash_kv_8_8_t kv, value;
598   snat_user_key_t user_key;
599   snat_user_t *u;
600   snat_main_per_thread_data_t *tsm;
601   snat_static_mapping_t *m;
602   snat_interface_t *interface;
603   int i;
604
605   /* Find SNAT address */
606   for (i=0; i < vec_len (sm->addresses); i++)
607     {
608       if (sm->addresses[i].addr.as_u32 == addr.as_u32)
609         {
610           a = sm->addresses + i;
611           break;
612         }
613     }
614   if (!a)
615     return VNET_API_ERROR_NO_SUCH_ENTRY;
616
617   if (delete_sm)
618     {
619       pool_foreach (m, sm->static_mappings,
620       ({
621           if (m->external_addr.as_u32 == addr.as_u32)
622             (void) snat_add_static_mapping (m->local_addr, m->external_addr,
623                                             m->local_port, m->external_port,
624                                             m->vrf_id, m->addr_only, ~0,
625                                             m->proto, 0);
626       }));
627     }
628   else
629     {
630       /* Check if address is used in some static mapping */
631       if (is_snat_address_used_in_static_mapping(sm, addr))
632         {
633           clib_warning ("address used in static mapping");
634           return VNET_API_ERROR_UNSPECIFIED;
635         }
636     }
637
638   if (a->fib_index != ~0)
639     fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4);
640
641   /* Delete sessions using address */
642   if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
643     {
644       vec_foreach (tsm, sm->per_thread_data)
645         {
646           pool_foreach (ses, tsm->sessions, ({
647             if (ses->out2in.addr.as_u32 == addr.as_u32)
648               {
649                 if (snat_is_unk_proto_session (ses))
650                   {
651                     clib_bihash_kv_16_8_t up_kv;
652                     snat_unk_proto_ses_key_t up_key;
653                     up_key.l_addr = ses->in2out.addr;
654                     up_key.r_addr = ses->ext_host_addr;
655                     up_key.fib_index = ses->in2out.fib_index;
656                     up_key.proto = ses->in2out.port;
657                     up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
658                     up_kv.key[0] = up_key.as_u64[0];
659                     up_kv.key[1] = up_key.as_u64[1];
660                     if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
661                                                   &up_kv, 0))
662                       clib_warning ("in2out key del failed");
663
664                     up_key.l_addr = ses->out2in.addr;
665                     up_key.fib_index = ses->out2in.fib_index;
666                     up_kv.key[0] = up_key.as_u64[0];
667                     up_kv.key[1] = up_key.as_u64[1];
668                     if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
669                                                   &up_kv, 0))
670                       clib_warning ("out2in key del failed");
671                   }
672                 else
673                   {
674                     /* log NAT event */
675                     snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
676                                                         ses->out2in.addr.as_u32,
677                                                         ses->in2out.protocol,
678                                                         ses->in2out.port,
679                                                         ses->out2in.port,
680                                                         ses->in2out.fib_index);
681                     kv.key = ses->in2out.as_u64;
682                     clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
683                     kv.key = ses->out2in.as_u64;
684                     clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
685                   }
686                 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
687                 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
688                 user_key.addr = ses->in2out.addr;
689                 user_key.fib_index = ses->in2out.fib_index;
690                 kv.key = user_key.as_u64;
691                 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
692                   {
693                     u = pool_elt_at_index (tsm->users, value.value);
694                     u->nsessions--;
695                   }
696               }
697           }));
698
699           vec_foreach (ses_index, ses_to_be_removed)
700             pool_put_index (tsm->sessions, ses_index[0]);
701
702           vec_free (ses_to_be_removed);
703        }
704     }
705
706   vec_del1 (sm->addresses, i);
707
708   /* Delete external address from FIB */
709   pool_foreach (interface, sm->interfaces,
710   ({
711     if (interface->is_inside)
712       continue;
713
714     snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
715     break;
716   }));
717   pool_foreach (interface, sm->output_feature_interfaces,
718   ({
719     if (interface->is_inside)
720       continue;
721
722     snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
723     break;
724   }));
725
726   return 0;
727 }
728
729 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
730 {
731   snat_main_t *sm = &snat_main;
732   snat_interface_t *i;
733   const char * feature_name;
734   snat_address_t * ap;
735   snat_static_mapping_t * m;
736   snat_det_map_t * dm;
737
738   if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
739     feature_name = is_inside ?  "nat44-in2out-fast" : "nat44-out2in-fast";
740   else
741     {
742       if (sm->num_workers > 1 && !sm->deterministic)
743         feature_name = is_inside ?  "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
744       else if (sm->deterministic)
745         feature_name = is_inside ?  "nat44-det-in2out" : "nat44-det-out2in";
746       else
747         feature_name = is_inside ?  "nat44-in2out" : "nat44-out2in";
748     }
749
750   vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
751                                !is_del, 0, 0);
752
753   if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
754     sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
755
756   if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
757     sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
758
759   pool_foreach (i, sm->interfaces,
760   ({
761     if (i->sw_if_index == sw_if_index)
762       {
763         if (is_del)
764           pool_put (sm->interfaces, i);
765         else
766           return VNET_API_ERROR_VALUE_EXIST;
767
768         goto fib;
769       }
770   }));
771
772   if (is_del)
773     return VNET_API_ERROR_NO_SUCH_ENTRY;
774
775   pool_get (sm->interfaces, i);
776   i->sw_if_index = sw_if_index;
777   i->is_inside = is_inside;
778
779   /* Add/delete external addresses to FIB */
780 fib:
781   if (is_inside)
782     return 0;
783
784   vec_foreach (ap, sm->addresses)
785     snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
786
787   pool_foreach (m, sm->static_mappings,
788   ({
789     if (!(m->addr_only))
790       continue;
791
792     snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
793   }));
794
795   pool_foreach (dm, sm->det_maps,
796   ({
797     snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
798   }));
799
800   return 0;
801 }
802
803 int snat_interface_add_del_output_feature (u32 sw_if_index,
804                                            u8 is_inside,
805                                            int is_del)
806 {
807   snat_main_t *sm = &snat_main;
808   snat_interface_t *i;
809   snat_address_t * ap;
810   snat_static_mapping_t * m;
811
812   if (sm->deterministic ||
813       (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
814     return VNET_API_ERROR_UNSUPPORTED;
815
816   if (is_inside)
817     {
818       vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
819                                    sw_if_index, !is_del, 0, 0);
820       vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
821                                    sw_if_index, !is_del, 0, 0);
822       goto fq;
823     }
824
825   if (sm->num_workers > 1)
826     {
827       vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in-worker-handoff",
828                                    sw_if_index, !is_del, 0, 0);
829       vnet_feature_enable_disable ("ip4-output",
830                                    "nat44-in2out-output-worker-handoff",
831                                    sw_if_index, !is_del, 0, 0);
832     }
833   else
834     {
835       vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in", sw_if_index,
836                                    !is_del, 0, 0);
837       vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
838                                    sw_if_index, !is_del, 0, 0);
839     }
840
841 fq:
842   if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
843     sm->fq_in2out_output_index =
844       vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
845
846   if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
847     sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
848
849   pool_foreach (i, sm->output_feature_interfaces,
850   ({
851     if (i->sw_if_index == sw_if_index)
852       {
853         if (is_del)
854           pool_put (sm->output_feature_interfaces, i);
855         else
856           return VNET_API_ERROR_VALUE_EXIST;
857
858         goto fib;
859       }
860   }));
861
862   if (is_del)
863     return VNET_API_ERROR_NO_SUCH_ENTRY;
864
865   pool_get (sm->output_feature_interfaces, i);
866   i->sw_if_index = sw_if_index;
867   i->is_inside = is_inside;
868
869   /* Add/delete external addresses to FIB */
870 fib:
871   if (is_inside)
872     return 0;
873
874   vec_foreach (ap, sm->addresses)
875     snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
876
877   pool_foreach (m, sm->static_mappings,
878   ({
879     if (!(m->addr_only))
880       continue;
881
882     snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
883   }));
884
885   return 0;
886 }
887
888 int snat_set_workers (uword * bitmap)
889 {
890   snat_main_t *sm = &snat_main;
891   int i, j = 0;
892
893   if (sm->num_workers < 2)
894     return VNET_API_ERROR_FEATURE_DISABLED;
895
896   if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
897     return VNET_API_ERROR_INVALID_WORKER;
898
899   vec_free (sm->workers);
900   clib_bitmap_foreach (i, bitmap,
901     ({
902       vec_add1(sm->workers, i);
903       sm->per_thread_data[i].snat_thread_index = j;
904       j++;
905     }));
906
907   sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
908   sm->num_snat_thread = _vec_len (sm->workers);
909
910   return 0;
911 }
912
913
914 static void
915 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
916                                        uword opaque,
917                                        u32 sw_if_index,
918                                        ip4_address_t * address,
919                                        u32 address_length,
920                                        u32 if_address_index,
921                                        u32 is_delete);
922
923 static clib_error_t * snat_init (vlib_main_t * vm)
924 {
925   snat_main_t * sm = &snat_main;
926   clib_error_t * error = 0;
927   ip4_main_t * im = &ip4_main;
928   ip_lookup_main_t * lm = &im->lookup_main;
929   uword *p;
930   vlib_thread_registration_t *tr;
931   vlib_thread_main_t *tm = vlib_get_thread_main ();
932   uword *bitmap = 0;
933   u32 i;
934   ip4_add_del_interface_address_callback_t cb4;
935
936   sm->vlib_main = vm;
937   sm->vnet_main = vnet_get_main();
938   sm->ip4_main = im;
939   sm->ip4_lookup_main = lm;
940   sm->api_main = &api_main;
941   sm->first_worker_index = 0;
942   sm->next_worker = 0;
943   sm->num_workers = 0;
944   sm->num_snat_thread = 1;
945   sm->workers = 0;
946   sm->port_per_thread = 0xffff - 1024;
947   sm->fq_in2out_index = ~0;
948   sm->fq_out2in_index = ~0;
949   sm->udp_timeout = SNAT_UDP_TIMEOUT;
950   sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
951   sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
952   sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
953
954   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
955   if (p)
956     {
957       tr = (vlib_thread_registration_t *) p[0];
958       if (tr)
959         {
960           sm->num_workers = tr->count;
961           sm->first_worker_index = tr->first_index;
962         }
963     }
964
965   vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
966
967   /* Use all available workers by default */
968   if (sm->num_workers > 1)
969     {
970       for (i=0; i < sm->num_workers; i++)
971         bitmap = clib_bitmap_set (bitmap, i, 1);
972       snat_set_workers(bitmap);
973       clib_bitmap_free (bitmap);
974     }
975   else
976     {
977       sm->per_thread_data[0].snat_thread_index = 0;
978     }
979
980   error = snat_api_init(vm, sm);
981   if (error)
982     return error;
983
984   /* Set up the interface address add/del callback */
985   cb4.function = snat_ip4_add_del_interface_address_cb;
986   cb4.function_opaque = 0;
987
988   vec_add1 (im->add_del_interface_address_callbacks, cb4);
989
990   /* Init IPFIX logging */
991   snat_ipfix_logging_init(vm);
992
993   error = nat64_init(vm);
994
995   return error;
996 }
997
998 VLIB_INIT_FUNCTION (snat_init);
999
1000 void snat_free_outside_address_and_port (snat_main_t * sm,
1001                                          snat_session_key_t * k,
1002                                          u32 address_index)
1003 {
1004   snat_address_t *a;
1005   u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1006
1007   ASSERT (address_index < vec_len (sm->addresses));
1008
1009   a = sm->addresses + address_index;
1010
1011   switch (k->protocol)
1012     {
1013 #define _(N, i, n, s) \
1014     case SNAT_PROTOCOL_##N: \
1015       ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1016         port_host_byte_order) == 1); \
1017       clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1018         port_host_byte_order, 0); \
1019       a->busy_##n##_ports--; \
1020       break;
1021       foreach_snat_protocol
1022 #undef _
1023     default:
1024       clib_warning("unknown_protocol");
1025       return;
1026     }
1027 }
1028
1029 /**
1030  * @brief Match NAT44 static mapping.
1031  *
1032  * @param sm          NAT main.
1033  * @param match       Address and port to match.
1034  * @param mapping     External or local address and port of the matched mapping.
1035  * @param by_external If 0 match by local address otherwise match by external
1036  *                    address.
1037  * @param is_addr_only If matched mapping is address only
1038  *
1039  * @returns 0 if match found otherwise 1.
1040  */
1041 int snat_static_mapping_match (snat_main_t * sm,
1042                                snat_session_key_t match,
1043                                snat_session_key_t * mapping,
1044                                u8 by_external,
1045                                u8 *is_addr_only)
1046 {
1047   clib_bihash_kv_8_8_t kv, value;
1048   snat_static_mapping_t *m;
1049   snat_session_key_t m_key;
1050   clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1051
1052   if (by_external)
1053     mapping_hash = &sm->static_mapping_by_external;
1054
1055   m_key.addr = match.addr;
1056   m_key.port = clib_net_to_host_u16 (match.port);
1057   m_key.protocol = match.protocol;
1058   m_key.fib_index = match.fib_index;
1059
1060   kv.key = m_key.as_u64;
1061
1062   if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1063     {
1064       /* Try address only mapping */
1065       m_key.port = 0;
1066       m_key.protocol = 0;
1067       kv.key = m_key.as_u64;
1068       if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1069         return 1;
1070     }
1071
1072   m = pool_elt_at_index (sm->static_mappings, value.value);
1073
1074   if (by_external)
1075     {
1076       mapping->addr = m->local_addr;
1077       /* Address only mapping doesn't change port */
1078       mapping->port = m->addr_only ? match.port
1079         : clib_host_to_net_u16 (m->local_port);
1080       mapping->fib_index = m->fib_index;
1081     }
1082   else
1083     {
1084       mapping->addr = m->external_addr;
1085       /* Address only mapping doesn't change port */
1086       mapping->port = m->addr_only ? match.port
1087         : clib_host_to_net_u16 (m->external_port);
1088       mapping->fib_index = sm->outside_fib_index;
1089     }
1090
1091   if (PREDICT_FALSE(is_addr_only != 0))
1092     *is_addr_only = m->addr_only;
1093
1094   return 0;
1095 }
1096
1097 static_always_inline u16
1098 snat_random_port (snat_main_t * sm, u16 min, u16 max)
1099 {
1100   return min + random_u32 (&sm->random_seed) /
1101     (random_u32_max() / (max - min + 1) + 1);
1102 }
1103
1104 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1105                                          u32 fib_index,
1106                                          u32 thread_index,
1107                                          snat_session_key_t * k,
1108                                          u32 * address_indexp)
1109 {
1110   int i;
1111   snat_address_t *a;
1112   u32 portnum;
1113
1114   for (i = 0; i < vec_len (sm->addresses); i++)
1115     {
1116       a = sm->addresses + i;
1117       if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
1118         continue;
1119       switch (k->protocol)
1120         {
1121 #define _(N, j, n, s) \
1122         case SNAT_PROTOCOL_##N: \
1123           if (a->busy_##n##_ports < (sm->port_per_thread * sm->num_snat_thread)) \
1124             { \
1125               while (1) \
1126                 { \
1127                   portnum = (sm->port_per_thread * \
1128                     sm->per_thread_data[thread_index].snat_thread_index) + \
1129                     snat_random_port(sm, 0, sm->port_per_thread) + 1024; \
1130                   if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1131                     continue; \
1132                   clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1133                   a->busy_##n##_ports++; \
1134                   k->addr = a->addr; \
1135                   k->port = clib_host_to_net_u16(portnum); \
1136                   *address_indexp = i; \
1137                   return 0; \
1138                 } \
1139             } \
1140           break;
1141           foreach_snat_protocol
1142 #undef _
1143         default:
1144           clib_warning("unknown protocol");
1145           return 1;
1146         }
1147
1148     }
1149   /* Totally out of translations to use... */
1150   snat_ipfix_logging_addresses_exhausted(0);
1151   return 1;
1152 }
1153
1154
1155 static clib_error_t *
1156 add_address_command_fn (vlib_main_t * vm,
1157                         unformat_input_t * input,
1158                         vlib_cli_command_t * cmd)
1159 {
1160   unformat_input_t _line_input, *line_input = &_line_input;
1161   snat_main_t * sm = &snat_main;
1162   ip4_address_t start_addr, end_addr, this_addr;
1163   u32 start_host_order, end_host_order;
1164   u32 vrf_id = ~0;
1165   int i, count;
1166   int is_add = 1;
1167   int rv = 0;
1168   clib_error_t *error = 0;
1169
1170   /* Get a line of input. */
1171   if (!unformat_user (input, unformat_line_input, line_input))
1172     return 0;
1173
1174   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1175     {
1176       if (unformat (line_input, "%U - %U",
1177                     unformat_ip4_address, &start_addr,
1178                     unformat_ip4_address, &end_addr))
1179         ;
1180       else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
1181         ;
1182       else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1183         end_addr = start_addr;
1184       else if (unformat (line_input, "del"))
1185         is_add = 0;
1186       else
1187         {
1188           error = clib_error_return (0, "unknown input '%U'",
1189             format_unformat_error, line_input);
1190           goto done;
1191         }
1192      }
1193
1194   if (sm->static_mapping_only)
1195     {
1196       error = clib_error_return (0, "static mapping only mode");
1197       goto done;
1198     }
1199
1200   start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1201   end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1202
1203   if (end_host_order < start_host_order)
1204     {
1205       error = clib_error_return (0, "end address less than start address");
1206       goto done;
1207     }
1208
1209   count = (end_host_order - start_host_order) + 1;
1210
1211   if (count > 1024)
1212     clib_warning ("%U - %U, %d addresses...",
1213                   format_ip4_address, &start_addr,
1214                   format_ip4_address, &end_addr,
1215                   count);
1216
1217   this_addr = start_addr;
1218
1219   for (i = 0; i < count; i++)
1220     {
1221       if (is_add)
1222         snat_add_address (sm, &this_addr, vrf_id);
1223       else
1224         rv = snat_del_address (sm, this_addr, 0);
1225
1226       switch (rv)
1227         {
1228         case VNET_API_ERROR_NO_SUCH_ENTRY:
1229           error = clib_error_return (0, "S-NAT address not exist.");
1230           goto done;
1231         case VNET_API_ERROR_UNSPECIFIED:
1232           error = clib_error_return (0, "S-NAT address used in static mapping.");
1233           goto done;
1234         default:
1235           break;
1236         }
1237
1238       increment_v4_address (&this_addr);
1239     }
1240
1241 done:
1242   unformat_free (line_input);
1243
1244   return error;
1245 }
1246
1247 VLIB_CLI_COMMAND (add_address_command, static) = {
1248   .path = "nat44 add address",
1249   .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
1250                 "[tenant-vrf <vrf-id>] [del]",
1251   .function = add_address_command_fn,
1252 };
1253
1254 static clib_error_t *
1255 snat_feature_command_fn (vlib_main_t * vm,
1256                           unformat_input_t * input,
1257                           vlib_cli_command_t * cmd)
1258 {
1259   unformat_input_t _line_input, *line_input = &_line_input;
1260   vnet_main_t * vnm = vnet_get_main();
1261   clib_error_t * error = 0;
1262   u32 sw_if_index;
1263   u32 * inside_sw_if_indices = 0;
1264   u32 * outside_sw_if_indices = 0;
1265   u8 is_output_feature = 0;
1266   int is_del = 0;
1267   int i;
1268
1269   sw_if_index = ~0;
1270
1271   /* Get a line of input. */
1272   if (!unformat_user (input, unformat_line_input, line_input))
1273     return 0;
1274
1275   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1276     {
1277       if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1278                     vnm, &sw_if_index))
1279         vec_add1 (inside_sw_if_indices, sw_if_index);
1280       else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1281                          vnm, &sw_if_index))
1282         vec_add1 (outside_sw_if_indices, sw_if_index);
1283       else if (unformat (line_input, "output-feature"))
1284         is_output_feature = 1;
1285       else if (unformat (line_input, "del"))
1286         is_del = 1;
1287       else
1288         {
1289           error = clib_error_return (0, "unknown input '%U'",
1290             format_unformat_error, line_input);
1291           goto done;
1292         }
1293     }
1294
1295   if (vec_len (inside_sw_if_indices))
1296     {
1297       for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1298         {
1299           sw_if_index = inside_sw_if_indices[i];
1300           if (is_output_feature)
1301             {
1302               if (snat_interface_add_del_output_feature (sw_if_index, 1, is_del))
1303                 {
1304                   error = clib_error_return (0, "%s %U failed",
1305                                              is_del ? "del" : "add",
1306                                              format_vnet_sw_interface_name, vnm,
1307                                              vnet_get_sw_interface (vnm,
1308                                                                     sw_if_index));
1309                   goto done;
1310                 }
1311             }
1312           else
1313             {
1314               if (snat_interface_add_del (sw_if_index, 1, is_del))
1315                 {
1316                   error = clib_error_return (0, "%s %U failed",
1317                                              is_del ? "del" : "add",
1318                                              format_vnet_sw_interface_name, vnm,
1319                                              vnet_get_sw_interface (vnm,
1320                                                                     sw_if_index));
1321                   goto done;
1322                 }
1323             }
1324         }
1325     }
1326
1327   if (vec_len (outside_sw_if_indices))
1328     {
1329       for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1330         {
1331           sw_if_index = outside_sw_if_indices[i];
1332           if (is_output_feature)
1333             {
1334               if (snat_interface_add_del_output_feature (sw_if_index, 0, is_del))
1335                 {
1336                   error = clib_error_return (0, "%s %U failed",
1337                                              is_del ? "del" : "add",
1338                                              format_vnet_sw_interface_name, vnm,
1339                                              vnet_get_sw_interface (vnm,
1340                                                                     sw_if_index));
1341                   goto done;
1342                 }
1343             }
1344           else
1345             {
1346               if (snat_interface_add_del (sw_if_index, 0, is_del))
1347                 {
1348                   error = clib_error_return (0, "%s %U failed",
1349                                              is_del ? "del" : "add",
1350                                              format_vnet_sw_interface_name, vnm,
1351                                              vnet_get_sw_interface (vnm,
1352                                                                     sw_if_index));
1353                   goto done;
1354                 }
1355             }
1356         }
1357     }
1358
1359 done:
1360   unformat_free (line_input);
1361   vec_free (inside_sw_if_indices);
1362   vec_free (outside_sw_if_indices);
1363
1364   return error;
1365 }
1366
1367 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1368   .path = "set interface nat44",
1369   .function = snat_feature_command_fn,
1370   .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
1371                 "[del]",
1372 };
1373
1374 uword
1375 unformat_snat_protocol (unformat_input_t * input, va_list * args)
1376 {
1377   u32 *r = va_arg (*args, u32 *);
1378
1379   if (0);
1380 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
1381   foreach_snat_protocol
1382 #undef _
1383   else
1384     return 0;
1385   return 1;
1386 }
1387
1388 u8 *
1389 format_snat_protocol (u8 * s, va_list * args)
1390 {
1391   u32 i = va_arg (*args, u32);
1392   u8 *t = 0;
1393
1394   switch (i)
1395     {
1396 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
1397       foreach_snat_protocol
1398 #undef _
1399     default:
1400       s = format (s, "unknown");
1401       return s;
1402     }
1403   s = format (s, "%s", t);
1404   return s;
1405 }
1406
1407 static clib_error_t *
1408 add_static_mapping_command_fn (vlib_main_t * vm,
1409                                unformat_input_t * input,
1410                                vlib_cli_command_t * cmd)
1411 {
1412   unformat_input_t _line_input, *line_input = &_line_input;
1413   clib_error_t * error = 0;
1414   ip4_address_t l_addr, e_addr;
1415   u32 l_port = 0, e_port = 0, vrf_id = ~0;
1416   int is_add = 1;
1417   int addr_only = 1;
1418   u32 sw_if_index = ~0;
1419   vnet_main_t * vnm = vnet_get_main();
1420   int rv;
1421   snat_protocol_t proto;
1422   u8 proto_set = 0;
1423
1424   /* Get a line of input. */
1425   if (!unformat_user (input, unformat_line_input, line_input))
1426     return 0;
1427
1428   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1429     {
1430       if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1431                     &l_port))
1432         addr_only = 0;
1433       else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1434         ;
1435       else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1436                          &e_addr, &e_port))
1437         addr_only = 0;
1438       else if (unformat (line_input, "external %U", unformat_ip4_address,
1439                          &e_addr))
1440         ;
1441       else if (unformat (line_input, "external %U %u",
1442                          unformat_vnet_sw_interface, vnm, &sw_if_index,
1443                          &e_port))
1444         addr_only = 0;
1445
1446       else if (unformat (line_input, "external %U",
1447                          unformat_vnet_sw_interface, vnm, &sw_if_index))
1448         ;
1449       else if (unformat (line_input, "vrf %u", &vrf_id))
1450         ;
1451       else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
1452         proto_set = 1;
1453       else if (unformat (line_input, "del"))
1454         is_add = 0;
1455       else
1456         {
1457           error = clib_error_return (0, "unknown input: '%U'",
1458             format_unformat_error, line_input);
1459           goto done;
1460         }
1461     }
1462
1463   if (!addr_only && !proto_set)
1464     {
1465       error = clib_error_return (0, "missing protocol");
1466       goto done;
1467     }
1468
1469   rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1470                                vrf_id, addr_only, sw_if_index, proto, is_add);
1471
1472   switch (rv)
1473     {
1474     case VNET_API_ERROR_INVALID_VALUE:
1475       error = clib_error_return (0, "External port already in use.");
1476       goto done;
1477     case VNET_API_ERROR_NO_SUCH_ENTRY:
1478       if (is_add)
1479         error = clib_error_return (0, "External addres must be allocated.");
1480       else
1481         error = clib_error_return (0, "Mapping not exist.");
1482       goto done;
1483     case VNET_API_ERROR_NO_SUCH_FIB:
1484       error = clib_error_return (0, "No such VRF id.");
1485       goto done;
1486     case VNET_API_ERROR_VALUE_EXIST:
1487       error = clib_error_return (0, "Mapping already exist.");
1488       goto done;
1489     default:
1490       break;
1491     }
1492
1493 done:
1494   unformat_free (line_input);
1495
1496   return error;
1497 }
1498
1499 /*?
1500  * @cliexpar
1501  * @cliexstart{snat add static mapping}
1502  * Static mapping allows hosts on the external network to initiate connection
1503  * to to the local network host.
1504  * To create static mapping between local host address 10.0.0.3 port 6303 and
1505  * external address 4.4.4.4 port 3606 for TCP protocol use:
1506  *  vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
1507  * If not runnig "static mapping only" NAT plugin mode use before:
1508  *  vpp# nat44 add address 4.4.4.4
1509  * To create static mapping between local and external address use:
1510  *  vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
1511  * @cliexend
1512 ?*/
1513 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1514   .path = "nat44 add static mapping",
1515   .function = add_static_mapping_command_fn,
1516   .short_help =
1517     "nat44 add static mapping tcp|udp|icmp local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1518 };
1519
1520 static clib_error_t *
1521 set_workers_command_fn (vlib_main_t * vm,
1522                         unformat_input_t * input,
1523                         vlib_cli_command_t * cmd)
1524 {
1525   unformat_input_t _line_input, *line_input = &_line_input;
1526   uword *bitmap = 0;
1527   int rv = 0;
1528   clib_error_t *error = 0;
1529
1530   /* Get a line of input. */
1531   if (!unformat_user (input, unformat_line_input, line_input))
1532     return 0;
1533
1534   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1535     {
1536       if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1537         ;
1538       else
1539         {
1540           error = clib_error_return (0, "unknown input '%U'",
1541             format_unformat_error, line_input);
1542           goto done;
1543         }
1544      }
1545
1546   if (bitmap == 0)
1547     {
1548       error = clib_error_return (0, "List of workers must be specified.");
1549       goto done;
1550     }
1551
1552   rv = snat_set_workers(bitmap);
1553
1554   clib_bitmap_free (bitmap);
1555
1556   switch (rv)
1557     {
1558     case VNET_API_ERROR_INVALID_WORKER:
1559       error = clib_error_return (0, "Invalid worker(s).");
1560       goto done;
1561     case VNET_API_ERROR_FEATURE_DISABLED:
1562       error = clib_error_return (0,
1563         "Supported only if 2 or more workes available.");
1564       goto done;
1565     default:
1566       break;
1567     }
1568
1569 done:
1570   unformat_free (line_input);
1571
1572   return error;
1573 }
1574
1575 /*?
1576  * @cliexpar
1577  * @cliexstart{set snat workers}
1578  * Set NAT workers if 2 or more workers available, use:
1579  *  vpp# set snat workers 0-2,5
1580  * @cliexend
1581 ?*/
1582 VLIB_CLI_COMMAND (set_workers_command, static) = {
1583   .path = "set nat workers",
1584   .function = set_workers_command_fn,
1585   .short_help =
1586     "set nat workers <workers-list>",
1587 };
1588
1589 static clib_error_t *
1590 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1591                                               unformat_input_t * input,
1592                                               vlib_cli_command_t * cmd)
1593 {
1594   unformat_input_t _line_input, *line_input = &_line_input;
1595   u32 domain_id = 0;
1596   u32 src_port = 0;
1597   u8 enable = 1;
1598   int rv = 0;
1599   clib_error_t *error = 0;
1600
1601   /* Get a line of input. */
1602   if (!unformat_user (input, unformat_line_input, line_input))
1603     return 0;
1604
1605   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1606     {
1607       if (unformat (line_input, "domain %d", &domain_id))
1608         ;
1609       else if (unformat (line_input, "src-port %d", &src_port))
1610         ;
1611       else if (unformat (line_input, "disable"))
1612         enable = 0;
1613       else
1614         {
1615           error = clib_error_return (0, "unknown input '%U'",
1616             format_unformat_error, line_input);
1617           goto done;
1618         }
1619      }
1620
1621   rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1622
1623   if (rv)
1624     {
1625       error = clib_error_return (0, "ipfix logging enable failed");
1626       goto done;
1627     }
1628
1629 done:
1630   unformat_free (line_input);
1631
1632   return error;
1633 }
1634
1635 /*?
1636  * @cliexpar
1637  * @cliexstart{snat ipfix logging}
1638  * To enable NAT IPFIX logging use:
1639  *  vpp# nat ipfix logging
1640  * To set IPFIX exporter use:
1641  *  vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1642  * @cliexend
1643 ?*/
1644 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1645   .path = "nat ipfix logging",
1646   .function = snat_ipfix_logging_enable_disable_command_fn,
1647   .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1648 };
1649
1650 static u32
1651 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1652 {
1653   snat_main_t *sm = &snat_main;
1654   snat_user_key_t key0;
1655   clib_bihash_kv_8_8_t kv0, value0;
1656   u32 next_worker_index = 0;
1657
1658   key0.addr = ip0->src_address;
1659   key0.fib_index = rx_fib_index0;
1660
1661   kv0.key = key0.as_u64;
1662
1663   /* Ever heard of of the "user" before? */
1664   if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
1665     {
1666       /* No, assign next available worker (RR) */
1667       next_worker_index = sm->first_worker_index;
1668       if (vec_len (sm->workers))
1669         {
1670           next_worker_index +=
1671             sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1672         }
1673
1674       /* add non-traslated packets worker lookup */
1675       kv0.value = next_worker_index;
1676       clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
1677     }
1678   else
1679     next_worker_index = value0.value;
1680
1681   return next_worker_index;
1682 }
1683
1684 static u32
1685 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1686 {
1687   snat_main_t *sm = &snat_main;
1688   snat_worker_key_t key0;
1689   clib_bihash_kv_8_8_t kv0, value0;
1690   udp_header_t * udp0;
1691   u32 next_worker_index = 0;
1692
1693   udp0 = ip4_next_header (ip0);
1694
1695   key0.addr = ip0->dst_address;
1696   key0.port = udp0->dst_port;
1697   key0.fib_index = rx_fib_index0;
1698
1699   if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
1700     {
1701       icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
1702       icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
1703       key0.port = echo0->identifier;
1704     }
1705
1706   kv0.key = key0.as_u64;
1707
1708   /* Ever heard of of the "user" before? */
1709   if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1710     {
1711       key0.port = 0;
1712       kv0.key = key0.as_u64;
1713
1714       if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1715         {
1716           /* No, assign next available worker (RR) */
1717           next_worker_index = sm->first_worker_index;
1718           if (vec_len (sm->workers))
1719             {
1720               next_worker_index +=
1721                 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1722             }
1723         }
1724       else
1725         {
1726           /* Static mapping without port */
1727           next_worker_index = value0.value;
1728         }
1729
1730       /* Add to translated packets worker lookup */
1731       kv0.value = next_worker_index;
1732       clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
1733     }
1734   else
1735     next_worker_index = value0.value;
1736
1737   return next_worker_index;
1738 }
1739
1740 static clib_error_t *
1741 snat_config (vlib_main_t * vm, unformat_input_t * input)
1742 {
1743   snat_main_t * sm = &snat_main;
1744   u32 translation_buckets = 1024;
1745   u32 translation_memory_size = 128<<20;
1746   u32 user_buckets = 128;
1747   u32 user_memory_size = 64<<20;
1748   u32 max_translations_per_user = 100;
1749   u32 outside_vrf_id = 0;
1750   u32 inside_vrf_id = 0;
1751   u32 static_mapping_buckets = 1024;
1752   u32 static_mapping_memory_size = 64<<20;
1753   u8 static_mapping_only = 0;
1754   u8 static_mapping_connection_tracking = 0;
1755
1756   sm->deterministic = 0;
1757
1758   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1759     {
1760       if (unformat (input, "translation hash buckets %d", &translation_buckets))
1761         ;
1762       else if (unformat (input, "translation hash memory %d",
1763                          &translation_memory_size));
1764       else if (unformat (input, "user hash buckets %d", &user_buckets))
1765         ;
1766       else if (unformat (input, "user hash memory %d",
1767                          &user_memory_size))
1768         ;
1769       else if (unformat (input, "max translations per user %d",
1770                          &max_translations_per_user))
1771         ;
1772       else if (unformat (input, "outside VRF id %d",
1773                          &outside_vrf_id))
1774         ;
1775       else if (unformat (input, "inside VRF id %d",
1776                          &inside_vrf_id))
1777         ;
1778       else if (unformat (input, "static mapping only"))
1779         {
1780           static_mapping_only = 1;
1781           if (unformat (input, "connection tracking"))
1782             static_mapping_connection_tracking = 1;
1783         }
1784       else if (unformat (input, "deterministic"))
1785         sm->deterministic = 1;
1786       else
1787         return clib_error_return (0, "unknown input '%U'",
1788                                   format_unformat_error, input);
1789     }
1790
1791   /* for show commands, etc. */
1792   sm->translation_buckets = translation_buckets;
1793   sm->translation_memory_size = translation_memory_size;
1794   sm->user_buckets = user_buckets;
1795   sm->user_memory_size = user_memory_size;
1796   sm->max_translations_per_user = max_translations_per_user;
1797   sm->outside_vrf_id = outside_vrf_id;
1798   sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1799                                                              outside_vrf_id);
1800   sm->inside_vrf_id = inside_vrf_id;
1801   sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1802                                                             inside_vrf_id);
1803   sm->static_mapping_only = static_mapping_only;
1804   sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1805
1806   if (sm->deterministic)
1807     {
1808       sm->in2out_node_index = snat_det_in2out_node.index;
1809       sm->in2out_output_node_index = ~0;
1810       sm->out2in_node_index = snat_det_out2in_node.index;
1811       sm->icmp_match_in2out_cb = icmp_match_in2out_det;
1812       sm->icmp_match_out2in_cb = icmp_match_out2in_det;
1813     }
1814   else
1815     {
1816       sm->worker_in2out_cb = snat_get_worker_in2out_cb;
1817       sm->worker_out2in_cb = snat_get_worker_out2in_cb;
1818       sm->in2out_node_index = snat_in2out_node.index;
1819       sm->in2out_output_node_index = snat_in2out_output_node.index;
1820       sm->out2in_node_index = snat_out2in_node.index;
1821       if (!static_mapping_only ||
1822           (static_mapping_only && static_mapping_connection_tracking))
1823         {
1824           sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
1825           sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
1826
1827           clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
1828                                 user_memory_size);
1829
1830           clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
1831                                 user_memory_size);
1832
1833           clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1834                                 translation_memory_size);
1835
1836           clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1837                                 translation_memory_size);
1838
1839           clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1840                                 user_memory_size);
1841
1842           clib_bihash_init_16_8 (&sm->in2out_unk_proto, "in2out-unk-proto",
1843                                  translation_buckets, translation_memory_size);
1844
1845           clib_bihash_init_16_8 (&sm->out2in_unk_proto, "out2in-unk-proto",
1846                                  translation_buckets, translation_memory_size);
1847         }
1848       else
1849         {
1850           sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
1851           sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
1852         }
1853       clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1854                             "static_mapping_by_local", static_mapping_buckets,
1855                             static_mapping_memory_size);
1856
1857       clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1858                             "static_mapping_by_external", static_mapping_buckets,
1859                             static_mapping_memory_size);
1860     }
1861
1862   return 0;
1863 }
1864
1865 VLIB_CONFIG_FUNCTION (snat_config, "nat");
1866
1867 u8 * format_snat_session_state (u8 * s, va_list * args)
1868 {
1869   u32 i = va_arg (*args, u32);
1870   u8 *t = 0;
1871
1872   switch (i)
1873     {
1874 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
1875     foreach_snat_session_state
1876 #undef _
1877     default:
1878       t = format (t, "unknown");
1879     }
1880   s = format (s, "%s", t);
1881   return s;
1882 }
1883
1884 u8 * format_snat_key (u8 * s, va_list * args)
1885 {
1886   snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1887   char * protocol_string = "unknown";
1888   static char *protocol_strings[] = {
1889       "UDP",
1890       "TCP",
1891       "ICMP",
1892   };
1893
1894   if (key->protocol < ARRAY_LEN(protocol_strings))
1895       protocol_string = protocol_strings[key->protocol];
1896
1897   s = format (s, "%U proto %s port %d fib %d",
1898               format_ip4_address, &key->addr, protocol_string,
1899               clib_net_to_host_u16 (key->port), key->fib_index);
1900   return s;
1901 }
1902
1903 u8 * format_snat_session (u8 * s, va_list * args)
1904 {
1905   snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1906   snat_session_t * sess = va_arg (*args, snat_session_t *);
1907
1908   if (snat_is_unk_proto_session (sess))
1909     {
1910       s = format (s, "  i2o %U proto %u fib %u\n",
1911                   format_ip4_address, &sess->in2out.addr, sess->in2out.port,
1912                   sess->in2out.fib_index);
1913       s = format (s, "    o2i %U proto %u fib %u\n",
1914                   format_ip4_address, &sess->out2in.addr, sess->out2in.port,
1915                   sess->out2in.fib_index);
1916     }
1917   else
1918     {
1919       s = format (s, "  i2o %U\n", format_snat_key, &sess->in2out);
1920       s = format (s, "    o2i %U\n", format_snat_key, &sess->out2in);
1921     }
1922   s = format (s, "       last heard %.2f\n", sess->last_heard);
1923   s = format (s, "       total pkts %d, total bytes %lld\n",
1924               sess->total_pkts, sess->total_bytes);
1925   if (snat_is_session_static (sess))
1926     s = format (s, "       static translation\n");
1927   else
1928     s = format (s, "       dynamic translation\n");
1929
1930   return s;
1931 }
1932
1933 u8 * format_snat_user (u8 * s, va_list * args)
1934 {
1935   snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
1936   snat_user_t * u = va_arg (*args, snat_user_t *);
1937   int verbose = va_arg (*args, int);
1938   dlist_elt_t * head, * elt;
1939   u32 elt_index, head_index;
1940   u32 session_index;
1941   snat_session_t * sess;
1942
1943   s = format (s, "%U: %d dynamic translations, %d static translations\n",
1944               format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
1945
1946   if (verbose == 0)
1947     return s;
1948
1949   if (u->nsessions || u->nstaticsessions)
1950     {
1951       head_index = u->sessions_per_user_list_head_index;
1952       head = pool_elt_at_index (sm->list_pool, head_index);
1953
1954       elt_index = head->next;
1955       elt = pool_elt_at_index (sm->list_pool, elt_index);
1956       session_index = elt->value;
1957
1958       while (session_index != ~0)
1959         {
1960           sess = pool_elt_at_index (sm->sessions, session_index);
1961
1962           s = format (s, "  %U\n", format_snat_session, sm, sess);
1963
1964           elt_index = elt->next;
1965           elt = pool_elt_at_index (sm->list_pool, elt_index);
1966           session_index = elt->value;
1967         }
1968     }
1969
1970   return s;
1971 }
1972
1973 u8 * format_snat_static_mapping (u8 * s, va_list * args)
1974 {
1975   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
1976
1977   if (m->addr_only)
1978       s = format (s, "local %U external %U vrf %d",
1979                   format_ip4_address, &m->local_addr,
1980                   format_ip4_address, &m->external_addr,
1981                   m->vrf_id);
1982   else
1983       s = format (s, "%U local %U:%d external %U:%d vrf %d",
1984                   format_snat_protocol, m->proto,
1985                   format_ip4_address, &m->local_addr, m->local_port,
1986                   format_ip4_address, &m->external_addr, m->external_port,
1987                   m->vrf_id);
1988
1989   return s;
1990 }
1991
1992 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
1993 {
1994   snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
1995   vnet_main_t *vnm = vnet_get_main();
1996
1997   if (m->addr_only)
1998       s = format (s, "local %U external %U vrf %d",
1999                   format_ip4_address, &m->l_addr,
2000                   format_vnet_sw_interface_name, vnm,
2001                   vnet_get_sw_interface (vnm, m->sw_if_index),
2002                   m->vrf_id);
2003   else
2004       s = format (s, "%U local %U:%d external %U:%d vrf %d",
2005                   format_snat_protocol, m->proto,
2006                   format_ip4_address, &m->l_addr, m->l_port,
2007                   format_vnet_sw_interface_name, vnm,
2008                   vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
2009                   m->vrf_id);
2010
2011   return s;
2012 }
2013
2014 u8 * format_det_map_ses (u8 * s, va_list * args)
2015 {
2016   snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2017   ip4_address_t in_addr, out_addr;
2018   u32 in_offset, out_offset;
2019   snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2020   u32 * i = va_arg (*args, u32 *);
2021
2022   u32 user_index = *i / SNAT_DET_SES_PER_USER;
2023   in_addr.as_u32 = clib_host_to_net_u32 (
2024     clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2025   in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2026     clib_net_to_host_u32(det_map->in_addr.as_u32);
2027   out_offset = in_offset / det_map->sharing_ratio;
2028   out_addr.as_u32 = clib_host_to_net_u32(
2029     clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2030   s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2031               format_ip4_address, &in_addr,
2032               clib_net_to_host_u16 (ses->in_port),
2033               format_ip4_address, &out_addr,
2034               clib_net_to_host_u16 (ses->out.out_port),
2035               format_ip4_address, &ses->out.ext_host_addr,
2036               clib_net_to_host_u16 (ses->out.ext_host_port),
2037               format_snat_session_state, ses->state,
2038               ses->expire);
2039
2040   return s;
2041 }
2042
2043 static clib_error_t *
2044 show_snat_command_fn (vlib_main_t * vm,
2045                  unformat_input_t * input,
2046                  vlib_cli_command_t * cmd)
2047 {
2048   int verbose = 0;
2049   snat_main_t * sm = &snat_main;
2050   snat_user_t * u;
2051   snat_static_mapping_t *m;
2052   snat_interface_t *i;
2053   snat_address_t * ap;
2054   vnet_main_t *vnm = vnet_get_main();
2055   snat_main_per_thread_data_t *tsm;
2056   u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2057   uword j = 0;
2058   snat_static_map_resolve_t *rp;
2059   snat_det_map_t * dm;
2060   snat_det_session_t * ses;
2061
2062   if (unformat (input, "detail"))
2063     verbose = 1;
2064   else if (unformat (input, "verbose"))
2065     verbose = 2;
2066
2067   if (sm->static_mapping_only)
2068     {
2069       if (sm->static_mapping_connection_tracking)
2070         vlib_cli_output (vm, "NAT plugin mode: static mapping only connection "
2071                          "tracking");
2072       else
2073         vlib_cli_output (vm, "NAT plugin mode: static mapping only");
2074     }
2075   else if (sm->deterministic)
2076     {
2077       vlib_cli_output (vm, "NAT plugin mode: deterministic mapping");
2078     }
2079   else
2080     {
2081       vlib_cli_output (vm, "NAT plugin mode: dynamic translations enabled");
2082     }
2083
2084   if (verbose > 0)
2085     {
2086       pool_foreach (i, sm->interfaces,
2087       ({
2088         vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2089                          vnet_get_sw_interface (vnm, i->sw_if_index),
2090                          i->is_inside ? "in" : "out");
2091       }));
2092
2093       pool_foreach (i, sm->output_feature_interfaces,
2094       ({
2095         vlib_cli_output (vm, "%U output-feature %s",
2096                          format_vnet_sw_interface_name, vnm,
2097                          vnet_get_sw_interface (vnm, i->sw_if_index),
2098                          i->is_inside ? "in" : "out");
2099       }));
2100
2101       if (vec_len (sm->auto_add_sw_if_indices))
2102         {
2103           vlib_cli_output (vm, "NAT44 pool addresses interfaces:");
2104           vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2105             {
2106               vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2107                                vnet_get_sw_interface (vnm, *sw_if_index));
2108             }
2109         }
2110
2111       vec_foreach (ap, sm->addresses)
2112         {
2113           vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2114           if (ap->fib_index != ~0)
2115               vlib_cli_output (vm, "  tenant VRF: %u",
2116                                ip4_fib_get(ap->fib_index)->table_id);
2117           else
2118             vlib_cli_output (vm, "  tenant VRF independent");
2119 #define _(N, i, n, s) \
2120           vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
2121           foreach_snat_protocol
2122 #undef _
2123         }
2124     }
2125
2126   if (sm->num_workers > 1)
2127     {
2128       vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2129       if (verbose > 0)
2130         {
2131           vec_foreach (worker, sm->workers)
2132             {
2133               vlib_worker_thread_t *w =
2134                 vlib_worker_threads + *worker + sm->first_worker_index;
2135               vlib_cli_output (vm, "  %s", w->name);
2136             }
2137         }
2138     }
2139
2140   if (sm->deterministic)
2141     {
2142       vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
2143       vlib_cli_output (vm, "tcp-established timeout: %dsec",
2144                        sm->tcp_established_timeout);
2145       vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
2146                        sm->tcp_transitory_timeout);
2147       vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
2148       vlib_cli_output (vm, "%d deterministic mappings",
2149                        pool_elts (sm->det_maps));
2150       if (verbose > 0)
2151         {
2152           pool_foreach (dm, sm->det_maps,
2153           ({
2154             vlib_cli_output (vm, "in %U/%d out %U/%d\n",
2155                              format_ip4_address, &dm->in_addr, dm->in_plen,
2156                              format_ip4_address, &dm->out_addr, dm->out_plen);
2157             vlib_cli_output (vm, " outside address sharing ratio: %d\n",
2158                              dm->sharing_ratio);
2159             vlib_cli_output (vm, " number of ports per inside host: %d\n",
2160                              dm->ports_per_host);
2161             vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
2162             if (verbose > 1)
2163               {
2164                 vec_foreach_index (j, dm->sessions)
2165                   {
2166                     ses = vec_elt_at_index (dm->sessions, j);
2167                     if (ses->in_port)
2168                       vlib_cli_output (vm, "  %U", format_det_map_ses, dm, ses,
2169                                        &j);
2170                   }
2171               }
2172           }));
2173         }
2174     }
2175   else
2176     {
2177       if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2178         {
2179           vlib_cli_output (vm, "%d static mappings",
2180                            pool_elts (sm->static_mappings));
2181
2182           if (verbose > 0)
2183             {
2184               pool_foreach (m, sm->static_mappings,
2185               ({
2186                 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2187               }));
2188             }
2189         }
2190       else
2191         {
2192           vec_foreach (tsm, sm->per_thread_data)
2193             {
2194               users_num += pool_elts (tsm->users);
2195               sessions_num += pool_elts (tsm->sessions);
2196             }
2197
2198           vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2199                            " %d static mappings",
2200                            users_num,
2201                            vec_len (sm->addresses),
2202                            sessions_num,
2203                            pool_elts (sm->static_mappings));
2204
2205           if (verbose > 0)
2206             {
2207               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2208                                verbose - 1);
2209               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2210                                verbose - 1);
2211               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2212                                verbose - 1);
2213               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2214                                verbose - 1);
2215               vec_foreach_index (j, sm->per_thread_data)
2216                 {
2217                   tsm = vec_elt_at_index (sm->per_thread_data, j);
2218
2219                   if (pool_elts (tsm->users) == 0)
2220                     continue;
2221
2222                   vlib_worker_thread_t *w = vlib_worker_threads + j;
2223                   vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2224                                    w->lcore_id);
2225                   vlib_cli_output (vm, "  %d list pool elements",
2226                                    pool_elts (tsm->list_pool));
2227
2228                   pool_foreach (u, tsm->users,
2229                   ({
2230                     vlib_cli_output (vm, "  %U", format_snat_user, tsm, u,
2231                                      verbose - 1);
2232                   }));
2233                 }
2234
2235               if (pool_elts (sm->static_mappings))
2236                 {
2237                   vlib_cli_output (vm, "static mappings:");
2238                   pool_foreach (m, sm->static_mappings,
2239                   ({
2240                     vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2241                   }));
2242                   for (j = 0; j < vec_len (sm->to_resolve); j++)
2243                     {
2244                       rp = sm->to_resolve + j;
2245                       vlib_cli_output (vm, "%U",
2246                                        format_snat_static_map_to_resolve, rp);
2247                     }
2248                 }
2249             }
2250         }
2251     }
2252   return 0;
2253 }
2254
2255 VLIB_CLI_COMMAND (show_snat_command, static) = {
2256     .path = "show nat44",
2257     .short_help = "show nat44",
2258     .function = show_snat_command_fn,
2259 };
2260
2261
2262 static void
2263 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2264                                        uword opaque,
2265                                        u32 sw_if_index,
2266                                        ip4_address_t * address,
2267                                        u32 address_length,
2268                                        u32 if_address_index,
2269                                        u32 is_delete)
2270 {
2271   snat_main_t *sm = &snat_main;
2272   snat_static_map_resolve_t *rp;
2273   u32 *indices_to_delete = 0;
2274   int i, j;
2275   int rv;
2276
2277   for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2278     {
2279       if (sw_if_index == sm->auto_add_sw_if_indices[i])
2280         {
2281           if (!is_delete)
2282             {
2283               /* Don't trip over lease renewal, static config */
2284               for (j = 0; j < vec_len(sm->addresses); j++)
2285                 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2286                   return;
2287
2288               snat_add_address (sm, address, ~0);
2289               /* Scan static map resolution vector */
2290               for (j = 0; j < vec_len (sm->to_resolve); j++)
2291                 {
2292                   rp = sm->to_resolve + j;
2293                   /* On this interface? */
2294                   if (rp->sw_if_index == sw_if_index)
2295                     {
2296                       /* Add the static mapping */
2297                       rv = snat_add_static_mapping (rp->l_addr,
2298                                                     address[0],
2299                                                     rp->l_port,
2300                                                     rp->e_port,
2301                                                     rp->vrf_id,
2302                                                     rp->addr_only,
2303                                                     ~0 /* sw_if_index */,
2304                                                     rp->proto,
2305                                                     rp->is_add);
2306                       if (rv)
2307                         clib_warning ("snat_add_static_mapping returned %d", 
2308                                       rv);
2309                       vec_add1 (indices_to_delete, j);
2310                     }
2311                 }
2312               /* If we resolved any of the outstanding static mappings */
2313               if (vec_len(indices_to_delete))
2314                 {
2315                   /* Delete them */
2316                   for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2317                     vec_delete(sm->to_resolve, 1, j);
2318                   vec_free(indices_to_delete);
2319                 }
2320               return;
2321             }
2322           else
2323             {
2324               (void) snat_del_address(sm, address[0], 1);
2325               return;
2326             }
2327         }
2328     }
2329 }
2330
2331
2332 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del)
2333 {
2334   ip4_main_t * ip4_main = sm->ip4_main;
2335   ip4_address_t * first_int_addr;
2336   snat_static_map_resolve_t *rp;
2337   u32 *indices_to_delete = 0;
2338   int i, j;
2339
2340   first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2341                                                 0 /* just want the address*/);
2342
2343   for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2344     {
2345       if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2346         {
2347           if (is_del)
2348             {
2349               /* if have address remove it */
2350               if (first_int_addr)
2351                   (void) snat_del_address (sm, first_int_addr[0], 1);
2352               else
2353                 {
2354                   for (j = 0; j < vec_len (sm->to_resolve); j++)
2355                     {
2356                       rp = sm->to_resolve + j;
2357                       if (rp->sw_if_index == sw_if_index)
2358                         vec_add1 (indices_to_delete, j);
2359                     }
2360                   if (vec_len(indices_to_delete))
2361                     {
2362                       for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2363                         vec_del1(sm->to_resolve, j);
2364                       vec_free(indices_to_delete);
2365                     }
2366                 }
2367               vec_del1(sm->auto_add_sw_if_indices, i);
2368             }
2369           else
2370             return VNET_API_ERROR_VALUE_EXIST;
2371
2372           return 0;
2373         }
2374     }
2375
2376   if (is_del)
2377     return VNET_API_ERROR_NO_SUCH_ENTRY;
2378
2379   /* add to the auto-address list */
2380   vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2381
2382   /* If the address is already bound - or static - add it now */
2383   if (first_int_addr)
2384       snat_add_address (sm, first_int_addr, ~0);
2385
2386   return 0;
2387 }
2388
2389 static clib_error_t *
2390 snat_add_interface_address_command_fn (vlib_main_t * vm,
2391                                        unformat_input_t * input,
2392                                        vlib_cli_command_t * cmd)
2393 {
2394   snat_main_t *sm = &snat_main;
2395   unformat_input_t _line_input, *line_input = &_line_input;
2396   u32 sw_if_index;
2397   int rv;
2398   int is_del = 0;
2399   clib_error_t *error = 0;
2400
2401   /* Get a line of input. */
2402   if (!unformat_user (input, unformat_line_input, line_input))
2403     return 0;
2404
2405   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2406     {
2407       if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2408                     sm->vnet_main, &sw_if_index))
2409         ;
2410       else if (unformat (line_input, "del"))
2411         is_del = 1;
2412       else
2413         {
2414           error = clib_error_return (0, "unknown input '%U'",
2415                                      format_unformat_error, line_input);
2416           goto done;
2417         }
2418     }
2419
2420   rv = snat_add_interface_address (sm, sw_if_index, is_del);
2421
2422   switch (rv)
2423     {
2424     case 0:
2425       break;
2426
2427     default:
2428       error = clib_error_return (0, "snat_add_interface_address returned %d",
2429                                  rv);
2430       goto done;
2431     }
2432
2433 done:
2434   unformat_free (line_input);
2435
2436   return error;
2437 }
2438
2439 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2440     .path = "nat44 add interface address",
2441     .short_help = "nat44 add interface address <interface> [del]",
2442     .function = snat_add_interface_address_command_fn,
2443 };
2444
2445 static clib_error_t *
2446 snat_det_map_command_fn (vlib_main_t * vm,
2447                          unformat_input_t * input,
2448                          vlib_cli_command_t * cmd)
2449 {
2450   snat_main_t *sm = &snat_main;
2451   unformat_input_t _line_input, *line_input = &_line_input;
2452   ip4_address_t in_addr, out_addr;
2453   u32 in_plen, out_plen;
2454   int is_add = 1, rv;
2455   clib_error_t *error = 0;
2456
2457   /* Get a line of input. */
2458   if (!unformat_user (input, unformat_line_input, line_input))
2459     return 0;
2460
2461   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2462     {
2463       if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
2464         ;
2465       else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
2466         ;
2467       else if (unformat (line_input, "del"))
2468         is_add = 0;
2469       else
2470         {
2471           error = clib_error_return (0, "unknown input '%U'",
2472                                      format_unformat_error, line_input);
2473           goto done;
2474         }
2475     }
2476
2477   unformat_free (line_input);
2478
2479   rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
2480                         is_add);
2481
2482   if (rv)
2483     {
2484       error = clib_error_return (0, "snat_det_add_map return %d", rv);
2485       goto done;
2486     }
2487
2488 done:
2489   unformat_free (line_input);
2490
2491   return error;
2492 }
2493
2494 /*?
2495  * @cliexpar
2496  * @cliexstart{snat deterministic add}
2497  * Create bijective mapping of inside address to outside address and port range
2498  * pairs, with the purpose of enabling deterministic NAT to reduce logging in
2499  * CGN deployments.
2500  * To create deterministic mapping between inside network 10.0.0.0/18 and
2501  * outside network 1.1.1.0/30 use:
2502  * # vpp# nat44 deterministic add in 10.0.0.0/18 out 1.1.1.0/30
2503  * @cliexend
2504 ?*/
2505 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
2506     .path = "nat44 deterministic add",
2507     .short_help = "nat44 deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
2508     .function = snat_det_map_command_fn,
2509 };
2510
2511 static clib_error_t *
2512 snat_det_forward_command_fn (vlib_main_t * vm,
2513                              unformat_input_t * input,
2514                              vlib_cli_command_t * cmd)
2515 {
2516   snat_main_t *sm = &snat_main;
2517   unformat_input_t _line_input, *line_input = &_line_input;
2518   ip4_address_t in_addr, out_addr;
2519   u16 lo_port;
2520   snat_det_map_t * dm;
2521   clib_error_t *error = 0;
2522
2523   /* Get a line of input. */
2524   if (!unformat_user (input, unformat_line_input, line_input))
2525     return 0;
2526
2527   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2528     {
2529       if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
2530         ;
2531       else
2532         {
2533           error = clib_error_return (0, "unknown input '%U'",
2534                                      format_unformat_error, line_input);
2535           goto done;
2536         }
2537     }
2538
2539   unformat_free (line_input);
2540
2541   dm = snat_det_map_by_user(sm, &in_addr);
2542   if (!dm)
2543     vlib_cli_output (vm, "no match");
2544   else
2545     {
2546       snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2547       vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
2548                        lo_port, lo_port + dm->ports_per_host - 1);
2549     }
2550
2551 done:
2552   unformat_free (line_input);
2553
2554   return error;
2555 }
2556
2557 /*?
2558  * @cliexpar
2559  * @cliexstart{snat deterministic forward}
2560  * Return outside address and port range from inside address for deterministic
2561  * NAT.
2562  * To obtain outside address and port of inside host use:
2563  *  vpp# nat44 deterministic forward 10.0.0.2
2564  *  1.1.1.0:<1054-1068>
2565  * @cliexend
2566 ?*/
2567 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
2568     .path = "nat44 deterministic forward",
2569     .short_help = "nat44 deterministic forward <addr>",
2570     .function = snat_det_forward_command_fn,
2571 };
2572
2573 static clib_error_t *
2574 snat_det_reverse_command_fn (vlib_main_t * vm,
2575                              unformat_input_t * input,
2576                              vlib_cli_command_t * cmd)
2577 {
2578   snat_main_t *sm = &snat_main;
2579   unformat_input_t _line_input, *line_input = &_line_input;
2580   ip4_address_t in_addr, out_addr;
2581   u32 out_port;
2582   snat_det_map_t * dm;
2583   clib_error_t *error = 0;
2584
2585   /* Get a line of input. */
2586   if (!unformat_user (input, unformat_line_input, line_input))
2587     return 0;
2588
2589   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2590     {
2591       if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
2592         ;
2593       else
2594         {
2595           error =  clib_error_return (0, "unknown input '%U'",
2596                                       format_unformat_error, line_input);
2597         }
2598     }
2599
2600   unformat_free (line_input);
2601
2602   if (out_port < 1024 || out_port > 65535)
2603     {
2604       error = clib_error_return (0, "wrong port, must be <1024-65535>");
2605       goto done;
2606     }
2607
2608   dm = snat_det_map_by_out(sm, &out_addr);
2609   if (!dm)
2610     vlib_cli_output (vm, "no match");
2611   else
2612     {
2613       snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
2614       vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
2615     }
2616
2617 done:
2618   unformat_free (line_input);
2619
2620   return error;
2621 }
2622
2623 /*?
2624  * @cliexpar
2625  * @cliexstart{snat deterministic reverse}
2626  * Return inside address from outside address and port for deterministic NAT.
2627  * To obtain inside host address from outside address and port use:
2628  *  #vpp nat44 deterministic reverse 1.1.1.1:1276
2629  *  10.0.16.16
2630  * @cliexend
2631 ?*/
2632 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
2633     .path = "nat44 deterministic reverse",
2634     .short_help = "nat44 deterministic reverse <addr>:<port>",
2635     .function = snat_det_reverse_command_fn,
2636 };
2637
2638 static clib_error_t *
2639 set_timeout_command_fn (vlib_main_t * vm,
2640                         unformat_input_t * input,
2641                         vlib_cli_command_t * cmd)
2642 {
2643   snat_main_t *sm = &snat_main;
2644   unformat_input_t _line_input, *line_input = &_line_input;
2645   clib_error_t *error = 0;
2646
2647   /* Get a line of input. */
2648   if (!unformat_user (input, unformat_line_input, line_input))
2649     return 0;
2650
2651   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2652     {
2653       if (unformat (line_input, "udp %u", &sm->udp_timeout))
2654         ;
2655       else if (unformat (line_input, "tcp-established %u",
2656                &sm->tcp_established_timeout))
2657         ;
2658       else if (unformat (line_input, "tcp-transitory %u",
2659                &sm->tcp_transitory_timeout))
2660         ;
2661       else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
2662         ;
2663       else if (unformat (line_input, "reset"))
2664         {
2665           sm->udp_timeout = SNAT_UDP_TIMEOUT;
2666           sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2667           sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2668           sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2669         }
2670       else
2671         {
2672           error = clib_error_return (0, "unknown input '%U'",
2673                                      format_unformat_error, line_input);
2674           goto done;
2675         }
2676     }
2677
2678   unformat_free (line_input);
2679
2680 done:
2681   unformat_free (line_input);
2682
2683   return error;
2684 }
2685
2686 /*?
2687  * @cliexpar
2688  * @cliexstart{set snat deterministic timeout}
2689  * Set values of timeouts for deterministic NAT (in seconds), use:
2690  *  vpp# set nat44 deterministic timeout udp 120 tcp-established 7500
2691  *  tcp-transitory 250 icmp 90
2692  * To reset default values use:
2693  *  vpp# set nat44 deterministic timeout reset
2694  * @cliexend
2695 ?*/
2696 VLIB_CLI_COMMAND (set_timeout_command, static) = {
2697   .path = "set nat44 deterministic timeout",
2698   .function = set_timeout_command_fn,
2699   .short_help =
2700     "set nat44 deterministic timeout [udp <sec> | tcp-established <sec> "
2701     "tcp-transitory <sec> | icmp <sec> | reset]",
2702 };
2703
2704 static clib_error_t *
2705 snat_det_close_session_out_fn (vlib_main_t *vm,
2706                                unformat_input_t * input,
2707                                vlib_cli_command_t * cmd)
2708 {
2709   snat_main_t *sm = &snat_main;
2710   unformat_input_t _line_input, *line_input = &_line_input;
2711   ip4_address_t out_addr, ext_addr, in_addr;
2712   u16 out_port, ext_port;
2713   snat_det_map_t * dm;
2714   snat_det_session_t * ses;
2715   snat_det_out_key_t key;
2716   clib_error_t *error = 0;
2717
2718   /* Get a line of input. */
2719   if (!unformat_user (input, unformat_line_input, line_input))
2720     return 0;
2721
2722   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2723     {
2724       if (unformat (line_input, "%U:%d %U:%d",
2725                     unformat_ip4_address, &out_addr, &out_port,
2726                     unformat_ip4_address, &ext_addr, &ext_port))
2727         ;
2728       else
2729         {
2730           error = clib_error_return (0, "unknown input '%U'",
2731                                      format_unformat_error, line_input);
2732           goto done;
2733         }
2734     }
2735
2736   unformat_free (line_input);
2737
2738   dm = snat_det_map_by_out(sm, &out_addr);
2739   if (!dm)
2740     vlib_cli_output (vm, "no match");
2741   else
2742     {
2743       snat_det_reverse(dm, &ext_addr, out_port, &in_addr);
2744       key.ext_host_addr = out_addr;
2745       key.ext_host_port = ntohs(ext_port);
2746       key.out_port = ntohs(out_port);
2747       ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
2748       if (!ses)
2749         vlib_cli_output (vm, "no match");
2750       else
2751        snat_det_ses_close(dm, ses);
2752     }
2753
2754 done:
2755   unformat_free (line_input);
2756
2757   return error;
2758 }
2759
2760 /*?
2761  * @cliexpar
2762  * @cliexstart{snat deterministic close session out}
2763  * Close session using outside ip address and port
2764  * and external ip address and port, use:
2765  *  vpp# nat44 deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
2766  * @cliexend
2767 ?*/
2768 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
2769   .path = "nat44 deterministic close session out",
2770   .short_help = "nat44 deterministic close session out "
2771                 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
2772   .function = snat_det_close_session_out_fn,
2773 };
2774
2775 static clib_error_t *
2776 snat_det_close_session_in_fn (vlib_main_t *vm,
2777                               unformat_input_t * input,
2778                               vlib_cli_command_t * cmd)
2779 {
2780   snat_main_t *sm = &snat_main;
2781   unformat_input_t _line_input, *line_input = &_line_input;
2782   ip4_address_t in_addr, ext_addr;
2783   u16 in_port, ext_port;
2784   snat_det_map_t * dm;
2785   snat_det_session_t * ses;
2786   snat_det_out_key_t key;
2787   clib_error_t *error = 0;
2788
2789   /* Get a line of input. */
2790   if (!unformat_user (input, unformat_line_input, line_input))
2791     return 0;
2792
2793   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2794     {
2795       if (unformat (line_input, "%U:%d %U:%d",
2796                     unformat_ip4_address, &in_addr, &in_port,
2797                     unformat_ip4_address, &ext_addr, &ext_port))
2798         ;
2799       else
2800         {
2801           error = clib_error_return (0, "unknown input '%U'",
2802                                      format_unformat_error, line_input);
2803           goto done;
2804         }
2805     }
2806
2807   unformat_free (line_input);
2808
2809   dm = snat_det_map_by_user (sm, &in_addr);
2810   if (!dm)
2811     vlib_cli_output (vm, "no match");
2812   else
2813     {
2814       key.ext_host_addr = ext_addr;
2815       key.ext_host_port = ntohs (ext_port);
2816       ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs(in_port), key);
2817       if (!ses)
2818         vlib_cli_output (vm, "no match");
2819       else
2820         snat_det_ses_close(dm, ses);
2821     }
2822
2823 done:
2824   unformat_free(line_input);
2825
2826   return error;
2827 }
2828
2829 /*?
2830  * @cliexpar
2831  * @cliexstart{snat deterministic close_session_in}
2832  * Close session using inside ip address and port
2833  * and external ip address and port, use:
2834  *  vpp# nat44 deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
2835  * @cliexend
2836 ?*/
2837 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
2838   .path = "nat44 deterministic close session in",
2839   .short_help = "nat44 deterministic close session in "
2840                 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
2841   .function = snat_det_close_session_in_fn,
2842 };