9fbc1e542d466d6df4bbbea78d7d20022248701f
[vpp.git] / src / plugins / snat / snat.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 <snat/snat.h>
23 #include <snat/snat_ipfix_logging.h>
24 #include <snat/snat_det.h>
25 #include <snat/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 = "snat-in2out",
38   .runs_before = VNET_FEATURES ("snat-out2in"),
39 };
40 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
41   .arc_name = "ip4-unicast",
42   .node_name = "snat-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 = "snat-det-in2out",
48   .runs_before = VNET_FEATURES ("snat-det-out2in"),
49 };
50 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
51   .arc_name = "ip4-unicast",
52   .node_name = "snat-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 = "snat-in2out-worker-handoff",
58   .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
59 };
60 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
61   .arc_name = "ip4-unicast",
62   .node_name = "snat-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 = "snat-in2out-fast",
68   .runs_before = VNET_FEATURES ("snat-out2in-fast"),
69 };
70 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
71   .arc_name = "ip4-unicast",
72   .node_name = "snat-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 = "snat-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 = "snat-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 = "snat-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 = "snat-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                                     FIB_PROTOCOL_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 ?  "snat-in2out-fast" : "snat-out2in-fast";
740   else
741     {
742       if (sm->num_workers > 1 && !sm->deterministic)
743         feature_name = is_inside ?  "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
744       else if (sm->deterministic)
745         feature_name = is_inside ?  "snat-det-in2out" : "snat-det-out2in";
746       else
747         feature_name = is_inside ?  "snat-in2out" : "snat-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", "snat-hairpin-dst",
819                                    sw_if_index, !is_del, 0, 0);
820       vnet_feature_enable_disable ("ip4-output", "snat-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", "snat-out2in-worker-handoff",
828                                    sw_if_index, !is_del, 0, 0);
829       vnet_feature_enable_disable ("ip4-output",
830                                    "snat-in2out-output-worker-handoff",
831                                    sw_if_index, !is_del, 0, 0);
832     }
833   else
834     {
835       vnet_feature_enable_disable ("ip4-unicast", "snat-out2in", sw_if_index,
836                                    !is_del, 0, 0);
837       vnet_feature_enable_disable ("ip4-output", "snat-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 SNAT static mapping.
1031  *
1032  * @param sm          SNAT 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 = "snat add address",
1249   .short_help = "snat add addresses <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 snat",
1369   .function = snat_feature_command_fn,
1370   .short_help = "set interface snat 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     }
1402   s = format (s, "%s", t);
1403   return s;
1404 }
1405
1406 static clib_error_t *
1407 add_static_mapping_command_fn (vlib_main_t * vm,
1408                                unformat_input_t * input,
1409                                vlib_cli_command_t * cmd)
1410 {
1411   unformat_input_t _line_input, *line_input = &_line_input;
1412   clib_error_t * error = 0;
1413   ip4_address_t l_addr, e_addr;
1414   u32 l_port = 0, e_port = 0, vrf_id = ~0;
1415   int is_add = 1;
1416   int addr_only = 1;
1417   u32 sw_if_index = ~0;
1418   vnet_main_t * vnm = vnet_get_main();
1419   int rv;
1420   snat_protocol_t proto;
1421   u8 proto_set = 0;
1422
1423   /* Get a line of input. */
1424   if (!unformat_user (input, unformat_line_input, line_input))
1425     return 0;
1426
1427   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1428     {
1429       if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1430                     &l_port))
1431         addr_only = 0;
1432       else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1433         ;
1434       else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1435                          &e_addr, &e_port))
1436         addr_only = 0;
1437       else if (unformat (line_input, "external %U", unformat_ip4_address,
1438                          &e_addr))
1439         ;
1440       else if (unformat (line_input, "external %U %u",
1441                          unformat_vnet_sw_interface, vnm, &sw_if_index,
1442                          &e_port))
1443         addr_only = 0;
1444
1445       else if (unformat (line_input, "external %U",
1446                          unformat_vnet_sw_interface, vnm, &sw_if_index))
1447         ;
1448       else if (unformat (line_input, "vrf %u", &vrf_id))
1449         ;
1450       else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
1451         proto_set = 1;
1452       else if (unformat (line_input, "del"))
1453         is_add = 0;
1454       else
1455         {
1456           error = clib_error_return (0, "unknown input: '%U'",
1457             format_unformat_error, line_input);
1458           goto done;
1459         }
1460     }
1461
1462   if (!addr_only && !proto_set)
1463     {
1464       error = clib_error_return (0, "missing protocol");
1465       goto done;
1466     }
1467
1468   rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1469                                vrf_id, addr_only, sw_if_index, proto, is_add);
1470
1471   switch (rv)
1472     {
1473     case VNET_API_ERROR_INVALID_VALUE:
1474       error = clib_error_return (0, "External port already in use.");
1475       goto done;
1476     case VNET_API_ERROR_NO_SUCH_ENTRY:
1477       if (is_add)
1478         error = clib_error_return (0, "External addres must be allocated.");
1479       else
1480         error = clib_error_return (0, "Mapping not exist.");
1481       goto done;
1482     case VNET_API_ERROR_NO_SUCH_FIB:
1483       error = clib_error_return (0, "No such VRF id.");
1484       goto done;
1485     case VNET_API_ERROR_VALUE_EXIST:
1486       error = clib_error_return (0, "Mapping already exist.");
1487       goto done;
1488     default:
1489       break;
1490     }
1491
1492 done:
1493   unformat_free (line_input);
1494
1495   return error;
1496 }
1497
1498 /*?
1499  * @cliexpar
1500  * @cliexstart{snat add static mapping}
1501  * Static mapping allows hosts on the external network to initiate connection
1502  * to to the local network host.
1503  * To create static mapping between local host address 10.0.0.3 port 6303 and
1504  * external address 4.4.4.4 port 3606 for TCP protocol use:
1505  *  vpp# snat add static mapping local tcp 10.0.0.3 6303 external 4.4.4.4 3606
1506  * If not runnig "static mapping only" S-NAT plugin mode use before:
1507  *  vpp# snat add address 4.4.4.4
1508  * To create static mapping between local and external address use:
1509  *  vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1510  * @cliexend
1511 ?*/
1512 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1513   .path = "snat add static mapping",
1514   .function = add_static_mapping_command_fn,
1515   .short_help =
1516     "snat add static mapping local tcp|udp|icmp <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1517 };
1518
1519 static clib_error_t *
1520 set_workers_command_fn (vlib_main_t * vm,
1521                         unformat_input_t * input,
1522                         vlib_cli_command_t * cmd)
1523 {
1524   unformat_input_t _line_input, *line_input = &_line_input;
1525   uword *bitmap = 0;
1526   int rv = 0;
1527   clib_error_t *error = 0;
1528
1529   /* Get a line of input. */
1530   if (!unformat_user (input, unformat_line_input, line_input))
1531     return 0;
1532
1533   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1534     {
1535       if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1536         ;
1537       else
1538         {
1539           error = clib_error_return (0, "unknown input '%U'",
1540             format_unformat_error, line_input);
1541           goto done;
1542         }
1543      }
1544
1545   if (bitmap == 0)
1546     {
1547       error = clib_error_return (0, "List of workers must be specified.");
1548       goto done;
1549     }
1550
1551   rv = snat_set_workers(bitmap);
1552
1553   clib_bitmap_free (bitmap);
1554
1555   switch (rv)
1556     {
1557     case VNET_API_ERROR_INVALID_WORKER:
1558       error = clib_error_return (0, "Invalid worker(s).");
1559       goto done;
1560     case VNET_API_ERROR_FEATURE_DISABLED:
1561       error = clib_error_return (0,
1562         "Supported only if 2 or more workes available.");
1563       goto done;
1564     default:
1565       break;
1566     }
1567
1568 done:
1569   unformat_free (line_input);
1570
1571   return error;
1572 }
1573
1574 /*?
1575  * @cliexpar
1576  * @cliexstart{set snat workers}
1577  * Set SNAT workers if 2 or more workers available, use:
1578  *  vpp# set snat workers 0-2,5
1579  * @cliexend
1580 ?*/
1581 VLIB_CLI_COMMAND (set_workers_command, static) = {
1582   .path = "set snat workers",
1583   .function = set_workers_command_fn,
1584   .short_help =
1585     "set snat workers <workers-list>",
1586 };
1587
1588 static clib_error_t *
1589 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1590                                               unformat_input_t * input,
1591                                               vlib_cli_command_t * cmd)
1592 {
1593   unformat_input_t _line_input, *line_input = &_line_input;
1594   u32 domain_id = 0;
1595   u32 src_port = 0;
1596   u8 enable = 1;
1597   int rv = 0;
1598   clib_error_t *error = 0;
1599
1600   /* Get a line of input. */
1601   if (!unformat_user (input, unformat_line_input, line_input))
1602     return 0;
1603
1604   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1605     {
1606       if (unformat (line_input, "domain %d", &domain_id))
1607         ;
1608       else if (unformat (line_input, "src-port %d", &src_port))
1609         ;
1610       else if (unformat (line_input, "disable"))
1611         enable = 0;
1612       else
1613         {
1614           error = clib_error_return (0, "unknown input '%U'",
1615             format_unformat_error, line_input);
1616           goto done;
1617         }
1618      }
1619
1620   rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1621
1622   if (rv)
1623     {
1624       error = clib_error_return (0, "ipfix logging enable failed");
1625       goto done;
1626     }
1627
1628 done:
1629   unformat_free (line_input);
1630
1631   return error;
1632 }
1633
1634 /*?
1635  * @cliexpar
1636  * @cliexstart{snat ipfix logging}
1637  * To enable SNAT IPFIX logging use:
1638  *  vpp# snat ipfix logging
1639  * To set IPFIX exporter use:
1640  *  vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1641  * @cliexend
1642 ?*/
1643 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1644   .path = "snat ipfix logging",
1645   .function = snat_ipfix_logging_enable_disable_command_fn,
1646   .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1647 };
1648
1649 static u32
1650 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1651 {
1652   snat_main_t *sm = &snat_main;
1653   snat_user_key_t key0;
1654   clib_bihash_kv_8_8_t kv0, value0;
1655   u32 next_worker_index = 0;
1656
1657   key0.addr = ip0->src_address;
1658   key0.fib_index = rx_fib_index0;
1659
1660   kv0.key = key0.as_u64;
1661
1662   /* Ever heard of of the "user" before? */
1663   if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
1664     {
1665       /* No, assign next available worker (RR) */
1666       next_worker_index = sm->first_worker_index;
1667       if (vec_len (sm->workers))
1668         {
1669           next_worker_index +=
1670             sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1671         }
1672
1673       /* add non-traslated packets worker lookup */
1674       kv0.value = next_worker_index;
1675       clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
1676     }
1677   else
1678     next_worker_index = value0.value;
1679
1680   return next_worker_index;
1681 }
1682
1683 static u32
1684 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1685 {
1686   snat_main_t *sm = &snat_main;
1687   snat_worker_key_t key0;
1688   clib_bihash_kv_8_8_t kv0, value0;
1689   udp_header_t * udp0;
1690   u32 next_worker_index = 0;
1691
1692   udp0 = ip4_next_header (ip0);
1693
1694   key0.addr = ip0->dst_address;
1695   key0.port = udp0->dst_port;
1696   key0.fib_index = rx_fib_index0;
1697
1698   if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
1699     {
1700       icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
1701       icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
1702       key0.port = echo0->identifier;
1703     }
1704
1705   kv0.key = key0.as_u64;
1706
1707   /* Ever heard of of the "user" before? */
1708   if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1709     {
1710       key0.port = 0;
1711       kv0.key = key0.as_u64;
1712
1713       if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1714         {
1715           /* No, assign next available worker (RR) */
1716           next_worker_index = sm->first_worker_index;
1717           if (vec_len (sm->workers))
1718             {
1719               next_worker_index +=
1720                 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1721             }
1722         }
1723       else
1724         {
1725           /* Static mapping without port */
1726           next_worker_index = value0.value;
1727         }
1728
1729       /* Add to translated packets worker lookup */
1730       kv0.value = next_worker_index;
1731       clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
1732     }
1733   else
1734     next_worker_index = value0.value;
1735
1736   return next_worker_index;
1737 }
1738
1739 static clib_error_t *
1740 snat_config (vlib_main_t * vm, unformat_input_t * input)
1741 {
1742   snat_main_t * sm = &snat_main;
1743   u32 translation_buckets = 1024;
1744   u32 translation_memory_size = 128<<20;
1745   u32 user_buckets = 128;
1746   u32 user_memory_size = 64<<20;
1747   u32 max_translations_per_user = 100;
1748   u32 outside_vrf_id = 0;
1749   u32 inside_vrf_id = 0;
1750   u32 static_mapping_buckets = 1024;
1751   u32 static_mapping_memory_size = 64<<20;
1752   u8 static_mapping_only = 0;
1753   u8 static_mapping_connection_tracking = 0;
1754
1755   sm->deterministic = 0;
1756
1757   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1758     {
1759       if (unformat (input, "translation hash buckets %d", &translation_buckets))
1760         ;
1761       else if (unformat (input, "translation hash memory %d",
1762                          &translation_memory_size));
1763       else if (unformat (input, "user hash buckets %d", &user_buckets))
1764         ;
1765       else if (unformat (input, "user hash memory %d",
1766                          &user_memory_size))
1767         ;
1768       else if (unformat (input, "max translations per user %d",
1769                          &max_translations_per_user))
1770         ;
1771       else if (unformat (input, "outside VRF id %d",
1772                          &outside_vrf_id))
1773         ;
1774       else if (unformat (input, "inside VRF id %d",
1775                          &inside_vrf_id))
1776         ;
1777       else if (unformat (input, "static mapping only"))
1778         {
1779           static_mapping_only = 1;
1780           if (unformat (input, "connection tracking"))
1781             static_mapping_connection_tracking = 1;
1782         }
1783       else if (unformat (input, "deterministic"))
1784         sm->deterministic = 1;
1785       else
1786         return clib_error_return (0, "unknown input '%U'",
1787                                   format_unformat_error, input);
1788     }
1789
1790   /* for show commands, etc. */
1791   sm->translation_buckets = translation_buckets;
1792   sm->translation_memory_size = translation_memory_size;
1793   sm->user_buckets = user_buckets;
1794   sm->user_memory_size = user_memory_size;
1795   sm->max_translations_per_user = max_translations_per_user;
1796   sm->outside_vrf_id = outside_vrf_id;
1797   sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1798                                                              outside_vrf_id);
1799   sm->inside_vrf_id = inside_vrf_id;
1800   sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1801                                                             inside_vrf_id);
1802   sm->static_mapping_only = static_mapping_only;
1803   sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1804
1805   if (sm->deterministic)
1806     {
1807       sm->in2out_node_index = snat_det_in2out_node.index;
1808       sm->in2out_output_node_index = ~0;
1809       sm->out2in_node_index = snat_det_out2in_node.index;
1810       sm->icmp_match_in2out_cb = icmp_match_in2out_det;
1811       sm->icmp_match_out2in_cb = icmp_match_out2in_det;
1812     }
1813   else
1814     {
1815       sm->worker_in2out_cb = snat_get_worker_in2out_cb;
1816       sm->worker_out2in_cb = snat_get_worker_out2in_cb;
1817       sm->in2out_node_index = snat_in2out_node.index;
1818       sm->in2out_output_node_index = snat_in2out_output_node.index;
1819       sm->out2in_node_index = snat_out2in_node.index;
1820       if (!static_mapping_only ||
1821           (static_mapping_only && static_mapping_connection_tracking))
1822         {
1823           sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
1824           sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
1825
1826           clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
1827                                 user_memory_size);
1828
1829           clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
1830                                 user_memory_size);
1831
1832           clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1833                                 translation_memory_size);
1834
1835           clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1836                                 translation_memory_size);
1837
1838           clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1839                                 user_memory_size);
1840
1841           clib_bihash_init_16_8 (&sm->in2out_unk_proto, "in2out-unk-proto",
1842                                  translation_buckets, translation_memory_size);
1843
1844           clib_bihash_init_16_8 (&sm->out2in_unk_proto, "out2in-unk-proto",
1845                                  translation_buckets, translation_memory_size);
1846         }
1847       else
1848         {
1849           sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
1850           sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
1851         }
1852       clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1853                             "static_mapping_by_local", static_mapping_buckets,
1854                             static_mapping_memory_size);
1855
1856       clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1857                             "static_mapping_by_external", static_mapping_buckets,
1858                             static_mapping_memory_size);
1859     }
1860
1861   return 0;
1862 }
1863
1864 VLIB_CONFIG_FUNCTION (snat_config, "snat");
1865
1866 u8 * format_snat_session_state (u8 * s, va_list * args)
1867 {
1868   u32 i = va_arg (*args, u32);
1869   u8 *t = 0;
1870
1871   switch (i)
1872     {
1873 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
1874     foreach_snat_session_state
1875 #undef _
1876     default:
1877       t = format (t, "unknown");
1878     }
1879   s = format (s, "%s", t);
1880   return s;
1881 }
1882
1883 u8 * format_snat_key (u8 * s, va_list * args)
1884 {
1885   snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1886   char * protocol_string = "unknown";
1887   static char *protocol_strings[] = {
1888       "UDP",
1889       "TCP",
1890       "ICMP",
1891   };
1892
1893   if (key->protocol < ARRAY_LEN(protocol_strings))
1894       protocol_string = protocol_strings[key->protocol];
1895
1896   s = format (s, "%U proto %s port %d fib %d",
1897               format_ip4_address, &key->addr, protocol_string,
1898               clib_net_to_host_u16 (key->port), key->fib_index);
1899   return s;
1900 }
1901
1902 u8 * format_snat_session (u8 * s, va_list * args)
1903 {
1904   snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1905   snat_session_t * sess = va_arg (*args, snat_session_t *);
1906
1907   if (snat_is_unk_proto_session (sess))
1908     {
1909       s = format (s, "  i2o %U proto %u fib %u\n",
1910                   format_ip4_address, &sess->in2out.addr, sess->in2out.port,
1911                   sess->in2out.fib_index);
1912       s = format (s, "    o2i %U proto %u fib %u\n",
1913                   format_ip4_address, &sess->out2in.addr, sess->out2in.port,
1914                   sess->out2in.fib_index);
1915     }
1916   else
1917     {
1918       s = format (s, "  i2o %U\n", format_snat_key, &sess->in2out);
1919       s = format (s, "    o2i %U\n", format_snat_key, &sess->out2in);
1920     }
1921   s = format (s, "       last heard %.2f\n", sess->last_heard);
1922   s = format (s, "       total pkts %d, total bytes %lld\n",
1923               sess->total_pkts, sess->total_bytes);
1924   if (snat_is_session_static (sess))
1925     s = format (s, "       static translation\n");
1926   else
1927     s = format (s, "       dynamic translation\n");
1928
1929   return s;
1930 }
1931
1932 u8 * format_snat_user (u8 * s, va_list * args)
1933 {
1934   snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
1935   snat_user_t * u = va_arg (*args, snat_user_t *);
1936   int verbose = va_arg (*args, int);
1937   dlist_elt_t * head, * elt;
1938   u32 elt_index, head_index;
1939   u32 session_index;
1940   snat_session_t * sess;
1941
1942   s = format (s, "%U: %d dynamic translations, %d static translations\n",
1943               format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
1944
1945   if (verbose == 0)
1946     return s;
1947
1948   if (u->nsessions || u->nstaticsessions)
1949     {
1950       head_index = u->sessions_per_user_list_head_index;
1951       head = pool_elt_at_index (sm->list_pool, head_index);
1952
1953       elt_index = head->next;
1954       elt = pool_elt_at_index (sm->list_pool, elt_index);
1955       session_index = elt->value;
1956
1957       while (session_index != ~0)
1958         {
1959           sess = pool_elt_at_index (sm->sessions, session_index);
1960
1961           s = format (s, "  %U\n", format_snat_session, sm, sess);
1962
1963           elt_index = elt->next;
1964           elt = pool_elt_at_index (sm->list_pool, elt_index);
1965           session_index = elt->value;
1966         }
1967     }
1968
1969   return s;
1970 }
1971
1972 u8 * format_snat_static_mapping (u8 * s, va_list * args)
1973 {
1974   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
1975
1976   if (m->addr_only)
1977       s = format (s, "local %U external %U vrf %d",
1978                   format_ip4_address, &m->local_addr,
1979                   format_ip4_address, &m->external_addr,
1980                   m->vrf_id);
1981   else
1982       s = format (s, "%U local %U:%d external %U:%d vrf %d",
1983                   format_snat_protocol, m->proto,
1984                   format_ip4_address, &m->local_addr, m->local_port,
1985                   format_ip4_address, &m->external_addr, m->external_port,
1986                   m->vrf_id);
1987
1988   return s;
1989 }
1990
1991 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
1992 {
1993   snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
1994   vnet_main_t *vnm = vnet_get_main();
1995
1996   if (m->addr_only)
1997       s = format (s, "local %U external %U vrf %d",
1998                   format_ip4_address, &m->l_addr,
1999                   format_vnet_sw_interface_name, vnm,
2000                   vnet_get_sw_interface (vnm, m->sw_if_index),
2001                   m->vrf_id);
2002   else
2003       s = format (s, "%U local %U:%d external %U:%d vrf %d",
2004                   format_snat_protocol, m->proto,
2005                   format_ip4_address, &m->l_addr, m->l_port,
2006                   format_vnet_sw_interface_name, vnm,
2007                   vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
2008                   m->vrf_id);
2009
2010   return s;
2011 }
2012
2013 u8 * format_det_map_ses (u8 * s, va_list * args)
2014 {
2015   snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2016   ip4_address_t in_addr, out_addr;
2017   u32 in_offset, out_offset;
2018   snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2019   u32 * i = va_arg (*args, u32 *);
2020
2021   u32 user_index = *i / SNAT_DET_SES_PER_USER;
2022   in_addr.as_u32 = clib_host_to_net_u32 (
2023     clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2024   in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2025     clib_net_to_host_u32(det_map->in_addr.as_u32);
2026   out_offset = in_offset / det_map->sharing_ratio;
2027   out_addr.as_u32 = clib_host_to_net_u32(
2028     clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2029   s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2030               format_ip4_address, &in_addr,
2031               clib_net_to_host_u16 (ses->in_port),
2032               format_ip4_address, &out_addr,
2033               clib_net_to_host_u16 (ses->out.out_port),
2034               format_ip4_address, &ses->out.ext_host_addr,
2035               clib_net_to_host_u16 (ses->out.ext_host_port),
2036               format_snat_session_state, ses->state,
2037               ses->expire);
2038
2039   return s;
2040 }
2041
2042 static clib_error_t *
2043 show_snat_command_fn (vlib_main_t * vm,
2044                  unformat_input_t * input,
2045                  vlib_cli_command_t * cmd)
2046 {
2047   int verbose = 0;
2048   snat_main_t * sm = &snat_main;
2049   snat_user_t * u;
2050   snat_static_mapping_t *m;
2051   snat_interface_t *i;
2052   snat_address_t * ap;
2053   vnet_main_t *vnm = vnet_get_main();
2054   snat_main_per_thread_data_t *tsm;
2055   u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2056   uword j = 0;
2057   snat_static_map_resolve_t *rp;
2058   snat_det_map_t * dm;
2059   snat_det_session_t * ses;
2060
2061   if (unformat (input, "detail"))
2062     verbose = 1;
2063   else if (unformat (input, "verbose"))
2064     verbose = 2;
2065
2066   if (sm->static_mapping_only)
2067     {
2068       if (sm->static_mapping_connection_tracking)
2069         vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2070                          "tracking");
2071       else
2072         vlib_cli_output (vm, "SNAT mode: static mapping only");
2073     }
2074   else if (sm->deterministic)
2075     {
2076       vlib_cli_output (vm, "SNAT mode: deterministic mapping");
2077     }
2078   else
2079     {
2080       vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2081     }
2082
2083   if (verbose > 0)
2084     {
2085       pool_foreach (i, sm->interfaces,
2086       ({
2087         vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2088                          vnet_get_sw_interface (vnm, i->sw_if_index),
2089                          i->is_inside ? "in" : "out");
2090       }));
2091
2092       pool_foreach (i, sm->output_feature_interfaces,
2093       ({
2094         vlib_cli_output (vm, "%U output-feature %s",
2095                          format_vnet_sw_interface_name, vnm,
2096                          vnet_get_sw_interface (vnm, i->sw_if_index),
2097                          i->is_inside ? "in" : "out");
2098       }));
2099
2100       if (vec_len (sm->auto_add_sw_if_indices))
2101         {
2102           vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2103           vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2104             {
2105               vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2106                                vnet_get_sw_interface (vnm, *sw_if_index));
2107             }
2108         }
2109
2110       vec_foreach (ap, sm->addresses)
2111         {
2112           vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2113           if (ap->fib_index != ~0)
2114               vlib_cli_output (vm, "  tenant VRF: %u",
2115                                ip4_fib_get(ap->fib_index)->table_id);
2116           else
2117             vlib_cli_output (vm, "  tenant VRF independent");
2118 #define _(N, i, n, s) \
2119           vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
2120           foreach_snat_protocol
2121 #undef _
2122         }
2123     }
2124
2125   if (sm->num_workers > 1)
2126     {
2127       vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2128       if (verbose > 0)
2129         {
2130           vec_foreach (worker, sm->workers)
2131             {
2132               vlib_worker_thread_t *w =
2133                 vlib_worker_threads + *worker + sm->first_worker_index;
2134               vlib_cli_output (vm, "  %s", w->name);
2135             }
2136         }
2137     }
2138
2139   if (sm->deterministic)
2140     {
2141       vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
2142       vlib_cli_output (vm, "tcp-established timeout: %dsec",
2143                        sm->tcp_established_timeout);
2144       vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
2145                        sm->tcp_transitory_timeout);
2146       vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
2147       vlib_cli_output (vm, "%d deterministic mappings",
2148                        pool_elts (sm->det_maps));
2149       if (verbose > 0)
2150         {
2151           pool_foreach (dm, sm->det_maps,
2152           ({
2153             vlib_cli_output (vm, "in %U/%d out %U/%d\n",
2154                              format_ip4_address, &dm->in_addr, dm->in_plen,
2155                              format_ip4_address, &dm->out_addr, dm->out_plen);
2156             vlib_cli_output (vm, " outside address sharing ratio: %d\n",
2157                              dm->sharing_ratio);
2158             vlib_cli_output (vm, " number of ports per inside host: %d\n",
2159                              dm->ports_per_host);
2160             vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
2161             if (verbose > 1)
2162               {
2163                 vec_foreach_index (j, dm->sessions)
2164                   {
2165                     ses = vec_elt_at_index (dm->sessions, j);
2166                     if (ses->in_port)
2167                       vlib_cli_output (vm, "  %U", format_det_map_ses, dm, ses,
2168                                        &j);
2169                   }
2170               }
2171           }));
2172         }
2173     }
2174   else
2175     {
2176       if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2177         {
2178           vlib_cli_output (vm, "%d static mappings",
2179                            pool_elts (sm->static_mappings));
2180
2181           if (verbose > 0)
2182             {
2183               pool_foreach (m, sm->static_mappings,
2184               ({
2185                 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2186               }));
2187             }
2188         }
2189       else
2190         {
2191           vec_foreach (tsm, sm->per_thread_data)
2192             {
2193               users_num += pool_elts (tsm->users);
2194               sessions_num += pool_elts (tsm->sessions);
2195             }
2196
2197           vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2198                            " %d static mappings",
2199                            users_num,
2200                            vec_len (sm->addresses),
2201                            sessions_num,
2202                            pool_elts (sm->static_mappings));
2203
2204           if (verbose > 0)
2205             {
2206               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2207                                verbose - 1);
2208               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2209                                verbose - 1);
2210               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2211                                verbose - 1);
2212               vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2213                                verbose - 1);
2214               vec_foreach_index (j, sm->per_thread_data)
2215                 {
2216                   tsm = vec_elt_at_index (sm->per_thread_data, j);
2217
2218                   if (pool_elts (tsm->users) == 0)
2219                     continue;
2220
2221                   vlib_worker_thread_t *w = vlib_worker_threads + j;
2222                   vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2223                                    w->lcore_id);
2224                   vlib_cli_output (vm, "  %d list pool elements",
2225                                    pool_elts (tsm->list_pool));
2226
2227                   pool_foreach (u, tsm->users,
2228                   ({
2229                     vlib_cli_output (vm, "  %U", format_snat_user, tsm, u,
2230                                      verbose - 1);
2231                   }));
2232                 }
2233
2234               if (pool_elts (sm->static_mappings))
2235                 {
2236                   vlib_cli_output (vm, "static mappings:");
2237                   pool_foreach (m, sm->static_mappings,
2238                   ({
2239                     vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2240                   }));
2241                   for (j = 0; j < vec_len (sm->to_resolve); j++)
2242                     {
2243                       rp = sm->to_resolve + j;
2244                       vlib_cli_output (vm, "%U",
2245                                        format_snat_static_map_to_resolve, rp);
2246                     }
2247                 }
2248             }
2249         }
2250     }
2251   return 0;
2252 }
2253
2254 VLIB_CLI_COMMAND (show_snat_command, static) = {
2255     .path = "show snat",
2256     .short_help = "show snat",
2257     .function = show_snat_command_fn,
2258 };
2259
2260
2261 static void
2262 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2263                                        uword opaque,
2264                                        u32 sw_if_index,
2265                                        ip4_address_t * address,
2266                                        u32 address_length,
2267                                        u32 if_address_index,
2268                                        u32 is_delete)
2269 {
2270   snat_main_t *sm = &snat_main;
2271   snat_static_map_resolve_t *rp;
2272   u32 *indices_to_delete = 0;
2273   int i, j;
2274   int rv;
2275
2276   for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2277     {
2278       if (sw_if_index == sm->auto_add_sw_if_indices[i])
2279         {
2280           if (!is_delete)
2281             {
2282               /* Don't trip over lease renewal, static config */
2283               for (j = 0; j < vec_len(sm->addresses); j++)
2284                 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2285                   return;
2286
2287               snat_add_address (sm, address, ~0);
2288               /* Scan static map resolution vector */
2289               for (j = 0; j < vec_len (sm->to_resolve); j++)
2290                 {
2291                   rp = sm->to_resolve + j;
2292                   /* On this interface? */
2293                   if (rp->sw_if_index == sw_if_index)
2294                     {
2295                       /* Add the static mapping */
2296                       rv = snat_add_static_mapping (rp->l_addr,
2297                                                     address[0],
2298                                                     rp->l_port,
2299                                                     rp->e_port,
2300                                                     rp->vrf_id,
2301                                                     rp->addr_only,
2302                                                     ~0 /* sw_if_index */,
2303                                                     rp->proto,
2304                                                     rp->is_add);
2305                       if (rv)
2306                         clib_warning ("snat_add_static_mapping returned %d", 
2307                                       rv);
2308                       vec_add1 (indices_to_delete, j);
2309                     }
2310                 }
2311               /* If we resolved any of the outstanding static mappings */
2312               if (vec_len(indices_to_delete))
2313                 {
2314                   /* Delete them */
2315                   for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2316                     vec_delete(sm->to_resolve, 1, j);
2317                   vec_free(indices_to_delete);
2318                 }
2319               return;
2320             }
2321           else
2322             {
2323               (void) snat_del_address(sm, address[0], 1);
2324               return;
2325             }
2326         }
2327     }
2328 }
2329
2330
2331 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del)
2332 {
2333   ip4_main_t * ip4_main = sm->ip4_main;
2334   ip4_address_t * first_int_addr;
2335   snat_static_map_resolve_t *rp;
2336   u32 *indices_to_delete = 0;
2337   int i, j;
2338
2339   first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2340                                                 0 /* just want the address*/);
2341
2342   for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2343     {
2344       if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2345         {
2346           if (is_del)
2347             {
2348               /* if have address remove it */
2349               if (first_int_addr)
2350                   (void) snat_del_address (sm, first_int_addr[0], 1);
2351               else
2352                 {
2353                   for (j = 0; j < vec_len (sm->to_resolve); j++)
2354                     {
2355                       rp = sm->to_resolve + j;
2356                       if (rp->sw_if_index == sw_if_index)
2357                         vec_add1 (indices_to_delete, j);
2358                     }
2359                   if (vec_len(indices_to_delete))
2360                     {
2361                       for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2362                         vec_del1(sm->to_resolve, j);
2363                       vec_free(indices_to_delete);
2364                     }
2365                 }
2366               vec_del1(sm->auto_add_sw_if_indices, i);
2367             }
2368           else
2369             return VNET_API_ERROR_VALUE_EXIST;
2370
2371           return 0;
2372         }
2373     }
2374   
2375   if (is_del)
2376     return VNET_API_ERROR_NO_SUCH_ENTRY;
2377
2378   /* add to the auto-address list */
2379   vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2380
2381   /* If the address is already bound - or static - add it now */
2382   if (first_int_addr)
2383       snat_add_address (sm, first_int_addr, ~0);
2384
2385   return 0;
2386 }
2387
2388 static clib_error_t *
2389 snat_add_interface_address_command_fn (vlib_main_t * vm,
2390                                        unformat_input_t * input,
2391                                        vlib_cli_command_t * cmd)
2392 {
2393   snat_main_t *sm = &snat_main;
2394   unformat_input_t _line_input, *line_input = &_line_input;
2395   u32 sw_if_index;
2396   int rv;
2397   int is_del = 0;
2398   clib_error_t *error = 0;
2399
2400   /* Get a line of input. */
2401   if (!unformat_user (input, unformat_line_input, line_input))
2402     return 0;
2403
2404   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2405     {
2406       if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2407                     sm->vnet_main, &sw_if_index))
2408         ;
2409       else if (unformat (line_input, "del"))
2410         is_del = 1;
2411       else
2412         {
2413           error = clib_error_return (0, "unknown input '%U'",
2414                                      format_unformat_error, line_input);
2415           goto done;
2416         }
2417     }
2418
2419   rv = snat_add_interface_address (sm, sw_if_index, is_del);
2420
2421   switch (rv)
2422     {
2423     case 0:
2424       break;
2425
2426     default:
2427       error = clib_error_return (0, "snat_add_interface_address returned %d",
2428                                  rv);
2429       goto done;
2430     }
2431
2432 done:
2433   unformat_free (line_input);
2434
2435   return error;
2436 }
2437
2438 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2439     .path = "snat add interface address",
2440     .short_help = "snat add interface address <interface> [del]",
2441     .function = snat_add_interface_address_command_fn,
2442 };
2443
2444 static clib_error_t *
2445 snat_det_map_command_fn (vlib_main_t * vm,
2446                          unformat_input_t * input,
2447                          vlib_cli_command_t * cmd)
2448 {
2449   snat_main_t *sm = &snat_main;
2450   unformat_input_t _line_input, *line_input = &_line_input;
2451   ip4_address_t in_addr, out_addr;
2452   u32 in_plen, out_plen;
2453   int is_add = 1, rv;
2454   clib_error_t *error = 0;
2455
2456   /* Get a line of input. */
2457   if (!unformat_user (input, unformat_line_input, line_input))
2458     return 0;
2459
2460   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2461     {
2462       if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
2463         ;
2464       else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
2465         ;
2466       else if (unformat (line_input, "del"))
2467         is_add = 0;
2468       else
2469         {
2470           error = clib_error_return (0, "unknown input '%U'",
2471                                      format_unformat_error, line_input);
2472           goto done;
2473         }
2474     }
2475
2476   unformat_free (line_input);
2477
2478   rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
2479                         is_add);
2480
2481   if (rv)
2482     {
2483       error = clib_error_return (0, "snat_det_add_map return %d", rv);
2484       goto done;
2485     }
2486
2487 done:
2488   unformat_free (line_input);
2489
2490   return error;
2491 }
2492
2493 /*?
2494  * @cliexpar
2495  * @cliexstart{snat deterministic add}
2496  * Create bijective mapping of inside address to outside address and port range
2497  * pairs, with the purpose of enabling deterministic NAT to reduce logging in
2498  * CGN deployments.
2499  * To create deterministic mapping between inside network 10.0.0.0/18 and
2500  * outside network 1.1.1.0/30 use:
2501  * # vpp# snat deterministic add in 10.0.0.0/18 out 1.1.1.0/30
2502  * @cliexend
2503 ?*/
2504 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
2505     .path = "snat deterministic add",
2506     .short_help = "snat deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
2507     .function = snat_det_map_command_fn,
2508 };
2509
2510 static clib_error_t *
2511 snat_det_forward_command_fn (vlib_main_t * vm,
2512                              unformat_input_t * input,
2513                              vlib_cli_command_t * cmd)
2514 {
2515   snat_main_t *sm = &snat_main;
2516   unformat_input_t _line_input, *line_input = &_line_input;
2517   ip4_address_t in_addr, out_addr;
2518   u16 lo_port;
2519   snat_det_map_t * dm;
2520   clib_error_t *error = 0;
2521
2522   /* Get a line of input. */
2523   if (!unformat_user (input, unformat_line_input, line_input))
2524     return 0;
2525
2526   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2527     {
2528       if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
2529         ;
2530       else
2531         {
2532           error = clib_error_return (0, "unknown input '%U'",
2533                                      format_unformat_error, line_input);
2534           goto done;
2535         }
2536     }
2537
2538   unformat_free (line_input);
2539
2540   dm = snat_det_map_by_user(sm, &in_addr);
2541   if (!dm)
2542     vlib_cli_output (vm, "no match");
2543   else
2544     {
2545       snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2546       vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
2547                        lo_port, lo_port + dm->ports_per_host - 1);
2548     }
2549
2550 done:
2551   unformat_free (line_input);
2552
2553   return error;
2554 }
2555
2556 /*?
2557  * @cliexpar
2558  * @cliexstart{snat deterministic forward}
2559  * Return outside address and port range from inside address for deterministic
2560  * NAT.
2561  * To obtain outside address and port of inside host use:
2562  *  vpp# snat deterministic forward 10.0.0.2
2563  *  1.1.1.0:<1054-1068>
2564  * @cliexend
2565 ?*/
2566 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
2567     .path = "snat deterministic forward",
2568     .short_help = "snat deterministic forward <addr>",
2569     .function = snat_det_forward_command_fn,
2570 };
2571
2572 static clib_error_t *
2573 snat_det_reverse_command_fn (vlib_main_t * vm,
2574                              unformat_input_t * input,
2575                              vlib_cli_command_t * cmd)
2576 {
2577   snat_main_t *sm = &snat_main;
2578   unformat_input_t _line_input, *line_input = &_line_input;
2579   ip4_address_t in_addr, out_addr;
2580   u32 out_port;
2581   snat_det_map_t * dm;
2582   clib_error_t *error = 0;
2583
2584   /* Get a line of input. */
2585   if (!unformat_user (input, unformat_line_input, line_input))
2586     return 0;
2587
2588   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2589     {
2590       if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
2591         ;
2592       else
2593         {
2594           error =  clib_error_return (0, "unknown input '%U'",
2595                                       format_unformat_error, line_input);
2596         }
2597     }
2598
2599   unformat_free (line_input);
2600
2601   if (out_port < 1024 || out_port > 65535)
2602     {
2603       error = clib_error_return (0, "wrong port, must be <1024-65535>");
2604       goto done;
2605     }
2606
2607   dm = snat_det_map_by_out(sm, &out_addr);
2608   if (!dm)
2609     vlib_cli_output (vm, "no match");
2610   else
2611     {
2612       snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
2613       vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
2614     }
2615
2616 done:
2617   unformat_free (line_input);
2618
2619   return error;
2620 }
2621
2622 /*?
2623  * @cliexpar
2624  * @cliexstart{snat deterministic reverse}
2625  * Return inside address from outside address and port for deterministic NAT.
2626  * To obtain inside host address from outside address and port use:
2627  *  #vpp snat deterministic reverse 1.1.1.1:1276
2628  *  10.0.16.16
2629  * @cliexend
2630 ?*/
2631 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
2632     .path = "snat deterministic reverse",
2633     .short_help = "snat deterministic reverse <addr>:<port>",
2634     .function = snat_det_reverse_command_fn,
2635 };
2636
2637 static clib_error_t *
2638 set_timeout_command_fn (vlib_main_t * vm,
2639                         unformat_input_t * input,
2640                         vlib_cli_command_t * cmd)
2641 {
2642   snat_main_t *sm = &snat_main;
2643   unformat_input_t _line_input, *line_input = &_line_input;
2644   clib_error_t *error = 0;
2645
2646   /* Get a line of input. */
2647   if (!unformat_user (input, unformat_line_input, line_input))
2648     return 0;
2649
2650   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2651     {
2652       if (unformat (line_input, "udp %u", &sm->udp_timeout))
2653         ;
2654       else if (unformat (line_input, "tcp-established %u",
2655                &sm->tcp_established_timeout))
2656         ;
2657       else if (unformat (line_input, "tcp-transitory %u",
2658                &sm->tcp_transitory_timeout))
2659         ;
2660       else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
2661         ;
2662       else if (unformat (line_input, "reset"))
2663         {
2664           sm->udp_timeout = SNAT_UDP_TIMEOUT;
2665           sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2666           sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2667           sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2668         }
2669       else
2670         {
2671           error = clib_error_return (0, "unknown input '%U'",
2672                                      format_unformat_error, line_input);
2673           goto done;
2674         }
2675     }
2676
2677   unformat_free (line_input);
2678
2679 done:
2680   unformat_free (line_input);
2681
2682   return error;
2683 }
2684
2685 /*?
2686  * @cliexpar
2687  * @cliexstart{set snat deterministic timeout}
2688  * Set values of timeouts for deterministic NAT (in seconds), use:
2689  *  vpp# set snat deterministic timeout udp 120 tcp-established 7500
2690  *  tcp-transitory 250 icmp 90
2691  * To reset default values use:
2692  *  vpp# set snat deterministic timeout reset
2693  * @cliexend
2694 ?*/
2695 VLIB_CLI_COMMAND (set_timeout_command, static) = {
2696   .path = "set snat deterministic timeout",
2697   .function = set_timeout_command_fn,
2698   .short_help =
2699     "set snat deterministic timeout [udp <sec> | tcp-established <sec> "
2700     "tcp-transitory <sec> | icmp <sec> | reset]",
2701 };
2702
2703 static clib_error_t *
2704 snat_det_close_session_out_fn (vlib_main_t *vm,
2705                                unformat_input_t * input,
2706                                vlib_cli_command_t * cmd)
2707 {
2708   snat_main_t *sm = &snat_main;
2709   unformat_input_t _line_input, *line_input = &_line_input;
2710   ip4_address_t out_addr, ext_addr, in_addr;
2711   u16 out_port, ext_port;
2712   snat_det_map_t * dm;
2713   snat_det_session_t * ses;
2714   snat_det_out_key_t key;
2715   clib_error_t *error = 0;
2716
2717   /* Get a line of input. */
2718   if (!unformat_user (input, unformat_line_input, line_input))
2719     return 0;
2720
2721   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2722     {
2723       if (unformat (line_input, "%U:%d %U:%d",
2724                     unformat_ip4_address, &out_addr, &out_port,
2725                     unformat_ip4_address, &ext_addr, &ext_port))
2726         ;
2727       else
2728         {
2729           error = clib_error_return (0, "unknown input '%U'",
2730                                      format_unformat_error, line_input);
2731           goto done;
2732         }
2733     }
2734
2735   unformat_free (line_input);
2736
2737   dm = snat_det_map_by_out(sm, &out_addr);
2738   if (!dm)
2739     vlib_cli_output (vm, "no match");
2740   else
2741     {
2742       snat_det_reverse(dm, &ext_addr, out_port, &in_addr);
2743       key.ext_host_addr = out_addr;
2744       key.ext_host_port = ntohs(ext_port);
2745       key.out_port = ntohs(out_port);
2746       ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
2747       if (!ses)
2748         vlib_cli_output (vm, "no match");
2749       else
2750        snat_det_ses_close(dm, ses);
2751     }
2752
2753 done:
2754   unformat_free (line_input);
2755
2756   return error;
2757 }
2758
2759 /*?
2760  * @cliexpar
2761  * @cliexstart{snat deterministic close session out}
2762  * Close session using outside ip address and port
2763  * and external ip address and port, use:
2764  *  vpp# snat deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
2765  * @cliexend
2766 ?*/
2767 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
2768   .path = "snat deterministic close session out",
2769   .short_help = "snat deterministic close session out "
2770                 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
2771   .function = snat_det_close_session_out_fn,
2772 };
2773
2774 static clib_error_t *
2775 snat_det_close_session_in_fn (vlib_main_t *vm,
2776                               unformat_input_t * input,
2777                               vlib_cli_command_t * cmd)
2778 {
2779   snat_main_t *sm = &snat_main;
2780   unformat_input_t _line_input, *line_input = &_line_input;
2781   ip4_address_t in_addr, ext_addr;
2782   u16 in_port, ext_port;
2783   snat_det_map_t * dm;
2784   snat_det_session_t * ses;
2785   snat_det_out_key_t key;
2786   clib_error_t *error = 0;
2787
2788   /* Get a line of input. */
2789   if (!unformat_user (input, unformat_line_input, line_input))
2790     return 0;
2791
2792   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2793     {
2794       if (unformat (line_input, "%U:%d %U:%d",
2795                     unformat_ip4_address, &in_addr, &in_port,
2796                     unformat_ip4_address, &ext_addr, &ext_port))
2797         ;
2798       else
2799         {
2800           error = clib_error_return (0, "unknown input '%U'",
2801                                      format_unformat_error, line_input);
2802           goto done;
2803         }
2804     }
2805
2806   unformat_free (line_input);
2807
2808   dm = snat_det_map_by_user (sm, &in_addr);
2809   if (!dm)
2810     vlib_cli_output (vm, "no match");
2811   else
2812     {
2813       key.ext_host_addr = ext_addr;
2814       key.ext_host_port = ntohs (ext_port);
2815       ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs(in_port), key);
2816       if (!ses)
2817         vlib_cli_output (vm, "no match");
2818       else
2819         snat_det_ses_close(dm, ses);
2820     }
2821
2822 done:
2823   unformat_free(line_input);
2824
2825   return error;
2826 }
2827
2828 /*?
2829  * @cliexpar
2830  * @cliexstart{snat deterministic close_session_in}
2831  * Close session using inside ip address and port
2832  * and external ip address and port, use:
2833  *  vpp# snat deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
2834  * @cliexend
2835 ?*/
2836 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
2837   .path = "snat deterministic close session in",
2838   .short_help = "snat deterministic close session in "
2839                 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
2840   .function = snat_det_close_session_in_fn,
2841 };