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