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