d27887e80e6d1ea23cde89072eb9d79c0451a0bd
[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/nat_inlines.h>
29 #include <nat/nat44/inlines.h>
30 #include <nat/nat_affinity.h>
31 #include <nat/nat_syslog.h>
32 #include <nat/nat_ha.h>
33 #include <vnet/fib/fib_table.h>
34 #include <vnet/fib/ip4_fib.h>
35 #include <vnet/ip/reass/ip4_sv_reass.h>
36 #include <vppinfra/bihash_16_8.h>
37
38 #include <vpp/app/version.h>
39
40 snat_main_t snat_main;
41
42 fib_source_t nat_fib_src_hi;
43 fib_source_t nat_fib_src_low;
44
45 /* *INDENT-OFF* */
46 /* Hook up input features */
47 VNET_FEATURE_INIT (nat_pre_in2out, static) = {
48   .arc_name = "ip4-unicast",
49   .node_name = "nat-pre-in2out",
50   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
51                                "ip4-sv-reassembly-feature"),
52 };
53 VNET_FEATURE_INIT (nat_pre_out2in, static) = {
54   .arc_name = "ip4-unicast",
55   .node_name = "nat-pre-out2in",
56   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
57                                "ip4-dhcp-client-detect",
58                                "ip4-sv-reassembly-feature"),
59 };
60 VNET_FEATURE_INIT (snat_in2out_worker_handoff, static) = {
61   .arc_name = "ip4-unicast",
62   .node_name = "nat44-in2out-worker-handoff",
63   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
64 };
65 VNET_FEATURE_INIT (snat_out2in_worker_handoff, static) = {
66   .arc_name = "ip4-unicast",
67   .node_name = "nat44-out2in-worker-handoff",
68   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
69                                "ip4-dhcp-client-detect"),
70 };
71 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
72   .arc_name = "ip4-unicast",
73   .node_name = "nat44-in2out",
74   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
75 };
76 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
77   .arc_name = "ip4-unicast",
78   .node_name = "nat44-out2in",
79   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
80                                "ip4-dhcp-client-detect"),
81 };
82 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
83   .arc_name = "ip4-unicast",
84   .node_name = "nat44-classify",
85   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
86 };
87 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
88   .arc_name = "ip4-unicast",
89   .node_name = "nat44-det-in2out",
90   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
91 };
92 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
93   .arc_name = "ip4-unicast",
94   .node_name = "nat44-det-out2in",
95   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
96                                "ip4-dhcp-client-detect"),
97 };
98 VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
99   .arc_name = "ip4-unicast",
100   .node_name = "nat44-det-classify",
101   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
102 };
103 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
104   .arc_name = "ip4-unicast",
105   .node_name = "nat44-ed-in2out",
106   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
107 };
108 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
109   .arc_name = "ip4-unicast",
110   .node_name = "nat44-ed-out2in",
111   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
112                                "ip4-dhcp-client-detect"),
113 };
114 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
115   .arc_name = "ip4-unicast",
116   .node_name = "nat44-ed-classify",
117   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
118 };
119 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
120   .arc_name = "ip4-unicast",
121   .node_name = "nat44-handoff-classify",
122   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
123 };
124 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
125   .arc_name = "ip4-unicast",
126   .node_name = "nat44-in2out-fast",
127   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
128 };
129 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
130   .arc_name = "ip4-unicast",
131   .node_name = "nat44-out2in-fast",
132   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
133                                "ip4-dhcp-client-detect"),
134 };
135 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
136   .arc_name = "ip4-unicast",
137   .node_name = "nat44-hairpin-dst",
138   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
139 };
140 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = {
141   .arc_name = "ip4-unicast",
142   .node_name = "nat44-ed-hairpin-dst",
143   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
144 };
145
146 /* Hook up output features */
147 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
148   .arc_name = "ip4-output",
149   .node_name = "nat44-in2out-output",
150   .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
151 };
152 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
153   .arc_name = "ip4-output",
154   .node_name = "nat44-in2out-output-worker-handoff",
155   .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
156 };
157 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
158   .arc_name = "ip4-output",
159   .node_name = "nat44-hairpin-src",
160   .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
161 };
162 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
163   .arc_name = "ip4-output",
164   .node_name = "nat44-ed-in2out-output",
165   .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
166   .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
167 };
168 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = {
169   .arc_name = "ip4-output",
170   .node_name = "nat44-ed-hairpin-src",
171   .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
172   .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
173 };
174
175 /* Hook up ip4-local features */
176 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
177 {
178   .arc_name = "ip4-local",
179   .node_name = "nat44-hairpinning",
180   .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
181 };
182 VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) =
183 {
184   .arc_name = "ip4-local",
185   .node_name = "nat44-ed-hairpinning",
186   .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
187 };
188
189
190 VLIB_PLUGIN_REGISTER () = {
191     .version = VPP_BUILD_VER,
192     .description = "Network Address Translation (NAT)",
193 };
194 /* *INDENT-ON* */
195
196 void
197 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index,
198                        u8 is_ha)
199 {
200   snat_session_key_t key;
201   clib_bihash_kv_8_8_t kv;
202   nat_ed_ses_key_t ed_key;
203   clib_bihash_kv_16_8_t ed_kv;
204   snat_main_per_thread_data_t *tsm =
205     vec_elt_at_index (sm->per_thread_data, thread_index);
206
207   if (is_fwd_bypass_session (s))
208     {
209       if (snat_is_unk_proto_session (s))
210         {
211           ed_key.proto = s->in2out.port;
212           ed_key.r_port = 0;
213           ed_key.l_port = 0;
214         }
215       else
216         {
217           ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
218           ed_key.l_port = s->in2out.port;
219           ed_key.r_port = s->ext_host_port;
220         }
221       ed_key.l_addr = s->in2out.addr;
222       ed_key.r_addr = s->ext_host_addr;
223       ed_key.fib_index = 0;
224       ed_kv.key[0] = ed_key.as_u64[0];
225       ed_kv.key[1] = ed_key.as_u64[1];
226       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
227         nat_elog_warn ("in2out_ed key del failed");
228       return;
229     }
230
231   /* session lookup tables */
232   if (is_ed_session (s))
233     {
234       if (is_affinity_sessions (s))
235         nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
236                              s->in2out.protocol, s->out2in.port);
237       ed_key.l_addr = s->out2in.addr;
238       ed_key.r_addr = s->ext_host_addr;
239       ed_key.fib_index = s->out2in.fib_index;
240       if (snat_is_unk_proto_session (s))
241         {
242           ed_key.proto = s->in2out.port;
243           ed_key.r_port = 0;
244           ed_key.l_port = 0;
245         }
246       else
247         {
248           ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
249           ed_key.l_port = s->out2in.port;
250           ed_key.r_port = s->ext_host_port;
251         }
252       ed_kv.key[0] = ed_key.as_u64[0];
253       ed_kv.key[1] = ed_key.as_u64[1];
254       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
255         nat_elog_warn ("out2in_ed key del failed");
256       ed_key.l_addr = s->in2out.addr;
257       ed_key.fib_index = s->in2out.fib_index;
258       if (!snat_is_unk_proto_session (s))
259         ed_key.l_port = s->in2out.port;
260       if (is_twice_nat_session (s))
261         {
262           ed_key.r_addr = s->ext_host_nat_addr;
263           ed_key.r_port = s->ext_host_nat_port;
264         }
265       ed_kv.key[0] = ed_key.as_u64[0];
266       ed_kv.key[1] = ed_key.as_u64[1];
267       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
268         nat_elog_warn ("in2out_ed key del failed");
269
270       if (!is_ha)
271         nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
272                                &s->in2out.addr, s->in2out.port,
273                                &s->ext_host_nat_addr, s->ext_host_nat_port,
274                                &s->out2in.addr, s->out2in.port,
275                                &s->ext_host_addr, s->ext_host_port,
276                                s->in2out.protocol, is_twice_nat_session (s));
277     }
278   else
279     {
280       kv.key = s->in2out.as_u64;
281       if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
282         nat_elog_warn ("in2out key del failed");
283       kv.key = s->out2in.as_u64;
284       if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
285         nat_elog_warn ("out2in key del failed");
286
287       if (!is_ha)
288         nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
289                                  &s->in2out.addr, s->in2out.port,
290                                  &s->out2in.addr, s->out2in.port,
291                                  s->in2out.protocol);
292     }
293
294   if (snat_is_unk_proto_session (s))
295     return;
296
297   if (!is_ha)
298     {
299       /* log NAT event */
300       snat_ipfix_logging_nat44_ses_delete (thread_index,
301                                            s->in2out.addr.as_u32,
302                                            s->out2in.addr.as_u32,
303                                            s->in2out.protocol,
304                                            s->in2out.port,
305                                            s->out2in.port,
306                                            s->in2out.fib_index);
307
308       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
309                    s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
310                    thread_index);
311     }
312
313   /* Twice NAT address and port for external host */
314   if (is_twice_nat_session (s))
315     {
316       key.protocol = s->in2out.protocol;
317       key.port = s->ext_host_nat_port;
318       key.addr.as_u32 = s->ext_host_nat_addr.as_u32;
319       snat_free_outside_address_and_port (sm->twice_nat_addresses,
320                                           thread_index, &key);
321     }
322
323   if (snat_is_session_static (s))
324     return;
325
326   snat_free_outside_address_and_port (sm->addresses, thread_index,
327                                       &s->out2in);
328 }
329
330 void
331 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
332                          u32 thread_index, u8 is_ha)
333 {
334   snat_session_key_t key;
335   nat_ed_ses_key_t ed_key;
336   clib_bihash_kv_16_8_t ed_kv;
337   snat_main_per_thread_data_t *tsm =
338     vec_elt_at_index (sm->per_thread_data, thread_index);
339
340   if (is_fwd_bypass_session (s))
341     {
342       if (snat_is_unk_proto_session (s))
343         {
344           ed_key.proto = s->in2out.port;
345           ed_key.r_port = 0;
346           ed_key.l_port = 0;
347         }
348       else
349         {
350           ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
351           ed_key.l_port = s->in2out.port;
352           ed_key.r_port = s->ext_host_port;
353         }
354
355       ed_key.l_addr = s->in2out.addr;
356       ed_key.r_addr = s->ext_host_addr;
357       ed_key.fib_index = 0;
358       ed_kv.key[0] = ed_key.as_u64[0];
359       ed_kv.key[1] = ed_key.as_u64[1];
360
361       if (PREDICT_FALSE
362           (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0)))
363         nat_elog_warn ("in2out_ed key del failed");
364       return;
365     }
366
367   /* session lookup tables */
368   if (is_affinity_sessions (s))
369     nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
370                          s->in2out.protocol, s->out2in.port);
371   ed_key.l_addr = s->out2in.addr;
372   ed_key.r_addr = s->ext_host_addr;
373   ed_key.fib_index = s->out2in.fib_index;
374   if (snat_is_unk_proto_session (s))
375     {
376       ed_key.proto = s->in2out.port;
377       ed_key.r_port = 0;
378       ed_key.l_port = 0;
379     }
380   else
381     {
382       ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
383       ed_key.l_port = s->out2in.port;
384       ed_key.r_port = s->ext_host_port;
385     }
386   ed_kv.key[0] = ed_key.as_u64[0];
387   ed_kv.key[1] = ed_key.as_u64[1];
388
389   if (PREDICT_FALSE (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0)))
390     nat_elog_warn ("out2in_ed key del failed");
391
392   ed_key.l_addr = s->in2out.addr;
393   ed_key.fib_index = s->in2out.fib_index;
394
395   if (!snat_is_unk_proto_session (s))
396     ed_key.l_port = s->in2out.port;
397
398   if (is_twice_nat_session (s))
399     {
400       ed_key.r_addr = s->ext_host_nat_addr;
401       ed_key.r_port = s->ext_host_nat_port;
402     }
403
404   ed_kv.key[0] = ed_key.as_u64[0];
405   ed_kv.key[1] = ed_key.as_u64[1];
406
407   if (PREDICT_FALSE (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0)))
408     nat_elog_warn ("in2out_ed key del failed");
409
410   if (!is_ha)
411     {
412       nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
413                              &s->in2out.addr, s->in2out.port,
414                              &s->ext_host_nat_addr, s->ext_host_nat_port,
415                              &s->out2in.addr, s->out2in.port,
416                              &s->ext_host_addr, s->ext_host_port,
417                              s->in2out.protocol, is_twice_nat_session (s));
418     }
419
420   if (snat_is_unk_proto_session (s))
421     return;
422
423   // is this correct ?
424   if (!is_ha)
425     {
426       snat_ipfix_logging_nat44_ses_delete (thread_index,
427                                            s->in2out.addr.as_u32,
428                                            s->out2in.addr.as_u32,
429                                            s->in2out.protocol,
430                                            s->in2out.port,
431                                            s->out2in.port,
432                                            s->in2out.fib_index);
433       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
434                    s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
435                    thread_index);
436     }
437
438   /* Twice NAT address and port for external host */
439   if (is_twice_nat_session (s))
440     {
441       key.protocol = s->in2out.protocol;
442       key.port = s->ext_host_nat_port;
443       key.addr.as_u32 = s->ext_host_nat_addr.as_u32;
444       snat_free_outside_address_and_port (sm->twice_nat_addresses,
445                                           thread_index, &key);
446     }
447
448   if (snat_is_session_static (s))
449     return;
450
451   // should be called for every dynamic session
452   snat_free_outside_address_and_port (sm->addresses, thread_index,
453                                       &s->out2in);
454 }
455
456
457 snat_user_t *
458 nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr, u32 fib_index,
459                         u32 thread_index)
460 {
461   snat_user_t *u = 0;
462   snat_user_key_t user_key;
463   clib_bihash_kv_8_8_t kv, value;
464   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
465   dlist_elt_t *per_user_list_head_elt;
466
467   user_key.addr.as_u32 = addr->as_u32;
468   user_key.fib_index = fib_index;
469   kv.key = user_key.as_u64;
470
471   /* Ever heard of the "user" = src ip4 address before? */
472   if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
473     {
474       /* no, make a new one */
475       pool_get (tsm->users, u);
476       clib_memset (u, 0, sizeof (*u));
477
478       u->addr.as_u32 = addr->as_u32;
479       u->fib_index = fib_index;
480
481       pool_get (tsm->list_pool, per_user_list_head_elt);
482
483       u->sessions_per_user_list_head_index = per_user_list_head_elt -
484         tsm->list_pool;
485
486       clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
487
488       kv.value = u - tsm->users;
489
490       /* add user */
491       if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
492         {
493           nat_elog_warn ("user_hash key add failed");
494           nat44_delete_user_with_no_session (sm, u, thread_index);
495           return NULL;
496         }
497
498       vlib_set_simple_counter (&sm->total_users, thread_index, 0,
499                                pool_elts (tsm->users));
500     }
501   else
502     {
503       u = pool_elt_at_index (tsm->users, value.value);
504     }
505
506   return u;
507 }
508
509 snat_session_t *
510 nat_session_alloc_or_recycle (snat_main_t * sm, snat_user_t * u,
511                               u32 thread_index, f64 now)
512 {
513   snat_session_t *s;
514   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
515   u32 oldest_per_user_translation_list_index, session_index;
516   dlist_elt_t *oldest_per_user_translation_list_elt;
517   dlist_elt_t *per_user_translation_list_elt;
518
519   /* Over quota? Recycle the least recently used translation */
520   if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
521     {
522       oldest_per_user_translation_list_index =
523         clib_dlist_remove_head (tsm->list_pool,
524                                 u->sessions_per_user_list_head_index);
525
526       ASSERT (oldest_per_user_translation_list_index != ~0);
527
528       /* Add it back to the end of the LRU list */
529       clib_dlist_addtail (tsm->list_pool,
530                           u->sessions_per_user_list_head_index,
531                           oldest_per_user_translation_list_index);
532       /* Get the list element */
533       oldest_per_user_translation_list_elt =
534         pool_elt_at_index (tsm->list_pool,
535                            oldest_per_user_translation_list_index);
536
537       /* Get the session index from the list element */
538       session_index = oldest_per_user_translation_list_elt->value;
539
540       /* Get the session */
541       s = pool_elt_at_index (tsm->sessions, session_index);
542       nat_free_session_data (sm, s, thread_index, 0);
543       if (snat_is_session_static (s))
544         u->nstaticsessions--;
545       else
546         u->nsessions--;
547       s->flags = 0;
548       s->total_bytes = 0;
549       s->total_pkts = 0;
550       s->state = 0;
551       s->ext_host_addr.as_u32 = 0;
552       s->ext_host_port = 0;
553       s->ext_host_nat_addr.as_u32 = 0;
554       s->ext_host_nat_port = 0;
555     }
556   else
557     {
558       pool_get (tsm->sessions, s);
559       clib_memset (s, 0, sizeof (*s));
560
561       /* Create list elts */
562       pool_get (tsm->list_pool, per_user_translation_list_elt);
563       clib_dlist_init (tsm->list_pool,
564                        per_user_translation_list_elt - tsm->list_pool);
565
566       per_user_translation_list_elt->value = s - tsm->sessions;
567       s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
568       s->per_user_list_head_index = u->sessions_per_user_list_head_index;
569
570       clib_dlist_addtail (tsm->list_pool,
571                           s->per_user_list_head_index,
572                           per_user_translation_list_elt - tsm->list_pool);
573
574       dlist_elt_t *global_lru_list_elt;
575       pool_get (tsm->global_lru_pool, global_lru_list_elt);
576       global_lru_list_elt->value = s - tsm->sessions;
577       s->global_lru_index = global_lru_list_elt - tsm->global_lru_pool;
578       clib_dlist_addtail (tsm->global_lru_pool, tsm->global_lru_head_index,
579                           s->global_lru_index);
580       s->last_lru_update = now;
581
582       s->user_index = u - tsm->users;
583       vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
584                                pool_elts (tsm->sessions));
585     }
586
587   s->ha_last_refreshed = now;
588
589   return s;
590 }
591
592 int
593 nat_global_lru_free_one (snat_main_t * sm, int thread_index, f64 now)
594 {
595   snat_session_t *s = NULL;
596   dlist_elt_t *oldest_elt;
597   u64 sess_timeout_time;
598   u32 oldest_index;
599   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
600   oldest_index = clib_dlist_remove_head (tsm->global_lru_pool,
601                                          tsm->global_lru_head_index);
602   if (~0 != oldest_index)
603     {
604       oldest_elt = pool_elt_at_index (tsm->global_lru_pool, oldest_index);
605       s = pool_elt_at_index (tsm->sessions, oldest_elt->value);
606
607       sess_timeout_time =
608         s->last_heard + (f64) nat44_session_get_timeout (sm, s);
609       if (now >= sess_timeout_time
610           || (s->tcp_close_timestamp && now >= s->tcp_close_timestamp))
611         {
612           nat_free_session_data (sm, s, thread_index, 0);
613           nat44_ed_delete_session (sm, s, thread_index, 0);
614           return 1;
615         }
616       else
617         {
618           clib_dlist_addhead (tsm->global_lru_pool,
619                               tsm->global_lru_head_index, oldest_index);
620         }
621     }
622   return 0;
623 }
624
625 snat_session_t *
626 nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u, u32 thread_index,
627                       f64 now)
628 {
629   snat_session_t *s = NULL;
630   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
631
632   dlist_elt_t *oldest_elt;
633   u64 sess_timeout_time;
634   u32 oldest_index;
635
636   // no sessions
637   if (PREDICT_FALSE (!(u->nsessions) && !(u->nstaticsessions)))
638     goto alloc_new;
639
640   // no free sessions
641   if (PREDICT_FALSE
642       ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user))
643     {
644       nat_elog_addr (SNAT_LOG_WARNING, "[warn] max translations per user",
645                      clib_net_to_host_u32 (u->addr.as_u32));
646       snat_ipfix_logging_max_entries_per_user (thread_index,
647                                                sm->max_translations_per_user,
648                                                u->addr.as_u32);
649       return 0;
650     }
651
652   /* first try to reuse an expired session from this ip */
653   oldest_index =
654     clib_dlist_remove_head (tsm->list_pool,
655                             u->sessions_per_user_list_head_index);
656   oldest_elt = pool_elt_at_index (tsm->list_pool, oldest_index);
657   s = pool_elt_at_index (tsm->sessions, oldest_elt->value);
658
659   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
660   if (now >= sess_timeout_time ||
661       (s->tcp_close_timestamp && now >= s->tcp_close_timestamp))
662     {
663       // reuse old session
664       clib_dlist_addtail (tsm->list_pool,
665                           u->sessions_per_user_list_head_index, oldest_index);
666       s = nat44_session_reuse_old (sm, u, s, thread_index, now);
667       s->last_lru_update = now;
668     }
669   else
670     {
671       clib_dlist_addhead (tsm->list_pool,
672                           u->sessions_per_user_list_head_index, oldest_index);
673       s = NULL;
674     }
675
676 alloc_new:
677   if (!s)
678     {
679       nat_global_lru_free_one (sm, thread_index, now);
680       s = nat44_session_alloc_new (tsm, u, now);
681       vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
682                                pool_elts (tsm->sessions));
683     }
684   return s;
685 }
686
687 void
688 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
689                           int is_add)
690 {
691   fib_prefix_t prefix = {
692     .fp_len = p_len,
693     .fp_proto = FIB_PROTOCOL_IP4,
694     .fp_addr = {
695                 .ip4.as_u32 = addr->as_u32,
696                 },
697   };
698   u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
699
700   if (is_add)
701     fib_table_entry_update_one_path (fib_index,
702                                      &prefix,
703                                      nat_fib_src_low,
704                                      (FIB_ENTRY_FLAG_CONNECTED |
705                                       FIB_ENTRY_FLAG_LOCAL |
706                                       FIB_ENTRY_FLAG_EXCLUSIVE),
707                                      DPO_PROTO_IP4,
708                                      NULL,
709                                      sw_if_index,
710                                      ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
711   else
712     fib_table_entry_delete (fib_index, &prefix, nat_fib_src_low);
713 }
714
715 int
716 snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
717                   u8 twice_nat)
718 {
719   snat_address_t *ap;
720   snat_interface_t *i;
721   vlib_thread_main_t *tm = vlib_get_thread_main ();
722
723   if (twice_nat && !sm->endpoint_dependent)
724     return VNET_API_ERROR_FEATURE_DISABLED;
725
726   /* Check if address already exists */
727   /* *INDENT-OFF* */
728   vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
729     {
730       if (ap->addr.as_u32 == addr->as_u32)
731         return VNET_API_ERROR_VALUE_EXIST;
732     }
733   /* *INDENT-ON* */
734
735   if (twice_nat)
736     vec_add2 (sm->twice_nat_addresses, ap, 1);
737   else
738     vec_add2 (sm->addresses, ap, 1);
739
740   ap->addr = *addr;
741   if (vrf_id != ~0)
742     ap->fib_index =
743       fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
744                                          nat_fib_src_low);
745   else
746     ap->fib_index = ~0;
747 #define _(N, i, n, s) \
748   clib_memset(ap->busy_##n##_port_refcounts, 0, sizeof(ap->busy_##n##_port_refcounts));\
749   ap->busy_##n##_ports = 0; \
750   ap->busy_##n##_ports_per_thread = 0;\
751   vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
752   foreach_snat_protocol
753 #undef _
754     if (twice_nat)
755     return 0;
756
757   /* Add external address to FIB */
758   /* *INDENT-OFF* */
759   pool_foreach (i, sm->interfaces,
760   ({
761     if (nat_interface_is_inside(i) || sm->out2in_dpo)
762       continue;
763
764     snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
765     break;
766   }));
767   pool_foreach (i, sm->output_feature_interfaces,
768   ({
769     if (nat_interface_is_inside(i) || sm->out2in_dpo)
770       continue;
771
772     snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
773     break;
774   }));
775   /* *INDENT-ON* */
776
777   return 0;
778 }
779
780 static int
781 is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr)
782 {
783   snat_static_mapping_t *m;
784   /* *INDENT-OFF* */
785   pool_foreach (m, sm->static_mappings,
786   ({
787       if (is_addr_only_static_mapping (m) ||
788           is_out2in_only_static_mapping (m) ||
789           is_identity_static_mapping (m))
790         continue;
791       if (m->external_addr.as_u32 == addr.as_u32)
792         return 1;
793   }));
794   /* *INDENT-ON* */
795
796   return 0;
797 }
798
799 static void
800 snat_add_static_mapping_when_resolved (snat_main_t * sm,
801                                        ip4_address_t l_addr,
802                                        u16 l_port,
803                                        u32 sw_if_index,
804                                        u16 e_port,
805                                        u32 vrf_id,
806                                        snat_protocol_t proto,
807                                        int addr_only, int is_add, u8 * tag,
808                                        int twice_nat, int out2in_only,
809                                        int identity_nat)
810 {
811   snat_static_map_resolve_t *rp;
812
813   vec_add2 (sm->to_resolve, rp, 1);
814   rp->l_addr.as_u32 = l_addr.as_u32;
815   rp->l_port = l_port;
816   rp->sw_if_index = sw_if_index;
817   rp->e_port = e_port;
818   rp->vrf_id = vrf_id;
819   rp->proto = proto;
820   rp->addr_only = addr_only;
821   rp->is_add = is_add;
822   rp->twice_nat = twice_nat;
823   rp->out2in_only = out2in_only;
824   rp->identity_nat = identity_nat;
825   rp->tag = vec_dup (tag);
826 }
827
828 static u32
829 get_thread_idx_by_port (u16 e_port)
830 {
831   snat_main_t *sm = &snat_main;
832   u32 thread_idx = sm->num_workers;
833   if (sm->num_workers > 1)
834     {
835       thread_idx =
836         sm->first_worker_index +
837         sm->workers[(e_port - 1024) / sm->port_per_thread];
838     }
839   return thread_idx;
840 }
841
842 int
843 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
844                          u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
845                          u32 sw_if_index, snat_protocol_t proto, int is_add,
846                          twice_nat_type_t twice_nat, u8 out2in_only, u8 * tag,
847                          u8 identity_nat)
848 {
849   snat_main_t *sm = &snat_main;
850   snat_static_mapping_t *m;
851   snat_session_key_t m_key;
852   clib_bihash_kv_8_8_t kv, value;
853   snat_address_t *a = 0;
854   u32 fib_index = ~0;
855   snat_interface_t *interface;
856   int i;
857   snat_main_per_thread_data_t *tsm;
858   snat_user_key_t u_key;
859   snat_user_t *u;
860   dlist_elt_t *head, *elt;
861   u32 elt_index, head_index;
862   u32 ses_index;
863   u64 user_index;
864   snat_session_t *s;
865   snat_static_map_resolve_t *rp, *rp_match = 0;
866   nat44_lb_addr_port_t *local;
867   u32 find = ~0;
868
869   if (!sm->endpoint_dependent)
870     {
871       if (twice_nat || out2in_only)
872         return VNET_API_ERROR_FEATURE_DISABLED;
873     }
874
875   /* If the external address is a specific interface address */
876   if (sw_if_index != ~0)
877     {
878       ip4_address_t *first_int_addr;
879
880       for (i = 0; i < vec_len (sm->to_resolve); i++)
881         {
882           rp = sm->to_resolve + i;
883           if (rp->sw_if_index != sw_if_index ||
884               rp->l_addr.as_u32 != l_addr.as_u32 ||
885               rp->vrf_id != vrf_id || rp->addr_only != addr_only)
886             continue;
887
888           if (!addr_only)
889             {
890               if ((rp->l_port != l_port && rp->e_port != e_port)
891                   || rp->proto != proto)
892                 continue;
893             }
894
895           rp_match = rp;
896           break;
897         }
898
899       /* Might be already set... */
900       first_int_addr = ip4_interface_first_address
901         (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
902
903       if (is_add)
904         {
905           if (rp_match)
906             return VNET_API_ERROR_VALUE_EXIST;
907
908           snat_add_static_mapping_when_resolved
909             (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
910              addr_only, is_add, tag, twice_nat, out2in_only, identity_nat);
911
912           /* DHCP resolution required? */
913           if (first_int_addr == 0)
914             {
915               return 0;
916             }
917           else
918             {
919               e_addr.as_u32 = first_int_addr->as_u32;
920               /* Identity mapping? */
921               if (l_addr.as_u32 == 0)
922                 l_addr.as_u32 = e_addr.as_u32;
923             }
924         }
925       else
926         {
927           if (!rp_match)
928             return VNET_API_ERROR_NO_SUCH_ENTRY;
929
930           vec_del1 (sm->to_resolve, i);
931
932           if (first_int_addr)
933             {
934               e_addr.as_u32 = first_int_addr->as_u32;
935               /* Identity mapping? */
936               if (l_addr.as_u32 == 0)
937                 l_addr.as_u32 = e_addr.as_u32;
938             }
939           else
940             return 0;
941         }
942     }
943
944   m_key.addr = e_addr;
945   m_key.port = addr_only ? 0 : e_port;
946   m_key.protocol = addr_only ? 0 : proto;
947   m_key.fib_index = 0;
948   kv.key = m_key.as_u64;
949   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
950     m = 0;
951   else
952     m = pool_elt_at_index (sm->static_mappings, value.value);
953
954   if (is_add)
955     {
956       if (m)
957         {
958           if (is_identity_static_mapping (m))
959             {
960               /* *INDENT-OFF* */
961               pool_foreach (local, m->locals,
962               ({
963                 if (local->vrf_id == vrf_id)
964                   return VNET_API_ERROR_VALUE_EXIST;
965               }));
966               /* *INDENT-ON* */
967               pool_get (m->locals, local);
968               local->vrf_id = vrf_id;
969               local->fib_index =
970                 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
971                                                    nat_fib_src_low);
972               m_key.addr = m->local_addr;
973               m_key.port = m->local_port;
974               m_key.protocol = m->proto;
975               m_key.fib_index = local->fib_index;
976               kv.key = m_key.as_u64;
977               kv.value = m - sm->static_mappings;
978               clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
979               return 0;
980             }
981           else
982             return VNET_API_ERROR_VALUE_EXIST;
983         }
984
985       if (twice_nat && addr_only)
986         return VNET_API_ERROR_UNSUPPORTED;
987
988       /* Convert VRF id to FIB index */
989       if (vrf_id != ~0)
990         fib_index =
991           fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
992                                              nat_fib_src_low);
993       /* If not specified use inside VRF id from SNAT plugin startup config */
994       else
995         {
996           fib_index = sm->inside_fib_index;
997           vrf_id = sm->inside_vrf_id;
998           fib_table_lock (fib_index, FIB_PROTOCOL_IP4, nat_fib_src_low);
999         }
1000
1001       if (!(out2in_only || identity_nat))
1002         {
1003           m_key.addr = l_addr;
1004           m_key.port = addr_only ? 0 : l_port;
1005           m_key.protocol = addr_only ? 0 : proto;
1006           m_key.fib_index = fib_index;
1007           kv.key = m_key.as_u64;
1008           if (!clib_bihash_search_8_8
1009               (&sm->static_mapping_by_local, &kv, &value))
1010             return VNET_API_ERROR_VALUE_EXIST;
1011         }
1012
1013       /* Find external address in allocated addresses and reserve port for
1014          address and port pair mapping when dynamic translations enabled */
1015       if (!(addr_only || sm->static_mapping_only || out2in_only))
1016         {
1017           for (i = 0; i < vec_len (sm->addresses); i++)
1018             {
1019               if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1020                 {
1021                   a = sm->addresses + i;
1022                   /* External port must be unused */
1023                   switch (proto)
1024                     {
1025 #define _(N, j, n, s) \
1026                     case SNAT_PROTOCOL_##N: \
1027                       if (a->busy_##n##_port_refcounts[e_port]) \
1028                         return VNET_API_ERROR_INVALID_VALUE; \
1029                       ++a->busy_##n##_port_refcounts[e_port]; \
1030                       if (e_port > 1024) \
1031                         { \
1032                           a->busy_##n##_ports++; \
1033                           a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1034                         } \
1035                       break;
1036                       foreach_snat_protocol
1037 #undef _
1038                     default:
1039                       nat_elog_info ("unknown protocol");
1040                       return VNET_API_ERROR_INVALID_VALUE_2;
1041                     }
1042                   break;
1043                 }
1044             }
1045           /* External address must be allocated */
1046           if (!a && (l_addr.as_u32 != e_addr.as_u32))
1047             {
1048               if (sw_if_index != ~0)
1049                 {
1050                   for (i = 0; i < vec_len (sm->to_resolve); i++)
1051                     {
1052                       rp = sm->to_resolve + i;
1053                       if (rp->addr_only)
1054                         continue;
1055                       if (rp->sw_if_index != sw_if_index &&
1056                           rp->l_addr.as_u32 != l_addr.as_u32 &&
1057                           rp->vrf_id != vrf_id && rp->l_port != l_port &&
1058                           rp->e_port != e_port && rp->proto != proto)
1059                         continue;
1060
1061                       vec_del1 (sm->to_resolve, i);
1062                       break;
1063                     }
1064                 }
1065               return VNET_API_ERROR_NO_SUCH_ENTRY;
1066             }
1067         }
1068
1069       pool_get (sm->static_mappings, m);
1070       clib_memset (m, 0, sizeof (*m));
1071       m->tag = vec_dup (tag);
1072       m->local_addr = l_addr;
1073       m->external_addr = e_addr;
1074       m->twice_nat = twice_nat;
1075       if (out2in_only)
1076         m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1077       if (addr_only)
1078         m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
1079       if (identity_nat)
1080         {
1081           m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
1082           pool_get (m->locals, local);
1083           local->vrf_id = vrf_id;
1084           local->fib_index = fib_index;
1085         }
1086       else
1087         {
1088           m->vrf_id = vrf_id;
1089           m->fib_index = fib_index;
1090         }
1091       if (!addr_only)
1092         {
1093           m->local_port = l_port;
1094           m->external_port = e_port;
1095           m->proto = proto;
1096         }
1097
1098       if (sm->num_workers > 1)
1099         {
1100           ip4_header_t ip = {
1101             .src_address = m->local_addr,
1102           };
1103           vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index, 0));
1104           tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1105         }
1106       else
1107         tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1108
1109       m_key.addr = m->local_addr;
1110       m_key.port = m->local_port;
1111       m_key.protocol = m->proto;
1112       m_key.fib_index = fib_index;
1113       kv.key = m_key.as_u64;
1114       kv.value = m - sm->static_mappings;
1115       if (!out2in_only)
1116         clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1117
1118       m_key.addr = m->external_addr;
1119       m_key.port = m->external_port;
1120       m_key.fib_index = 0;
1121       kv.key = m_key.as_u64;
1122       kv.value = m - sm->static_mappings;
1123       clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
1124
1125       /* Delete dynamic sessions matching local address (+ local port) */
1126       if (!(sm->static_mapping_only))
1127         {
1128           u_key.addr = m->local_addr;
1129           u_key.fib_index = m->fib_index;
1130           kv.key = u_key.as_u64;
1131           if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1132             {
1133               user_index = value.value;
1134               u = pool_elt_at_index (tsm->users, user_index);
1135               if (u->nsessions)
1136                 {
1137                   head_index = u->sessions_per_user_list_head_index;
1138                   head = pool_elt_at_index (tsm->list_pool, head_index);
1139                   elt_index = head->next;
1140                   elt = pool_elt_at_index (tsm->list_pool, elt_index);
1141                   ses_index = elt->value;
1142                   while (ses_index != ~0)
1143                     {
1144                       s = pool_elt_at_index (tsm->sessions, ses_index);
1145                       elt = pool_elt_at_index (tsm->list_pool, elt->next);
1146                       ses_index = elt->value;
1147
1148                       if (snat_is_session_static (s))
1149                         continue;
1150
1151                       if (!addr_only
1152                           && (clib_net_to_host_u16 (s->in2out.port) !=
1153                               m->local_port))
1154                         continue;
1155
1156                       nat_free_session_data (sm, s,
1157                                              tsm - sm->per_thread_data, 0);
1158                       nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1159
1160                       if (!addr_only && !sm->endpoint_dependent)
1161                         break;
1162                     }
1163                 }
1164             }
1165         }
1166     }
1167   else
1168     {
1169       if (!m)
1170         {
1171           if (sw_if_index != ~0)
1172             return 0;
1173           else
1174             return VNET_API_ERROR_NO_SUCH_ENTRY;
1175         }
1176
1177       if (identity_nat)
1178         {
1179           if (vrf_id == ~0)
1180             vrf_id = sm->inside_vrf_id;
1181
1182           /* *INDENT-OFF* */
1183           pool_foreach (local, m->locals,
1184           ({
1185             if (local->vrf_id == vrf_id)
1186               find = local - m->locals;
1187           }));
1188           /* *INDENT-ON* */
1189           if (find == ~0)
1190             return VNET_API_ERROR_NO_SUCH_ENTRY;
1191
1192           local = pool_elt_at_index (m->locals, find);
1193           fib_index = local->fib_index;
1194           pool_put (m->locals, local);
1195         }
1196       else
1197         fib_index = m->fib_index;
1198
1199       /* Free external address port */
1200       if (!(addr_only || sm->static_mapping_only || out2in_only))
1201         {
1202           for (i = 0; i < vec_len (sm->addresses); i++)
1203             {
1204               if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1205                 {
1206                   a = sm->addresses + i;
1207                   switch (proto)
1208                     {
1209 #define _(N, j, n, s) \
1210                     case SNAT_PROTOCOL_##N: \
1211                       --a->busy_##n##_port_refcounts[e_port]; \
1212                       if (e_port > 1024) \
1213                         { \
1214                           a->busy_##n##_ports--; \
1215                           a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1216                         } \
1217                       break;
1218                       foreach_snat_protocol
1219 #undef _
1220                     default:
1221                       nat_elog_info ("unknown protocol");
1222                       return VNET_API_ERROR_INVALID_VALUE_2;
1223                     }
1224                   break;
1225                 }
1226             }
1227         }
1228
1229       if (sm->num_workers > 1)
1230         tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1231       else
1232         tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1233
1234       m_key.addr = m->local_addr;
1235       m_key.port = m->local_port;
1236       m_key.protocol = m->proto;
1237       m_key.fib_index = fib_index;
1238       kv.key = m_key.as_u64;
1239       if (!out2in_only)
1240         clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
1241
1242       /* Delete session(s) for static mapping if exist */
1243       if (!(sm->static_mapping_only) ||
1244           (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1245         {
1246           u_key.addr = m->local_addr;
1247           u_key.fib_index = fib_index;
1248           kv.key = u_key.as_u64;
1249           if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1250             {
1251               user_index = value.value;
1252               u = pool_elt_at_index (tsm->users, user_index);
1253               if (u->nstaticsessions)
1254                 {
1255                   head_index = u->sessions_per_user_list_head_index;
1256                   head = pool_elt_at_index (tsm->list_pool, head_index);
1257                   elt_index = head->next;
1258                   elt = pool_elt_at_index (tsm->list_pool, elt_index);
1259                   ses_index = elt->value;
1260                   while (ses_index != ~0)
1261                     {
1262                       s = pool_elt_at_index (tsm->sessions, ses_index);
1263                       elt = pool_elt_at_index (tsm->list_pool, elt->next);
1264                       ses_index = elt->value;
1265
1266                       if (!addr_only)
1267                         {
1268                           if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1269                               (clib_net_to_host_u16 (s->out2in.port) !=
1270                                e_port))
1271                             continue;
1272                         }
1273
1274                       if (is_lb_session (s))
1275                         continue;
1276
1277                       if (!snat_is_session_static (s))
1278                         continue;
1279
1280                       nat_free_session_data (sm, s,
1281                                              tsm - sm->per_thread_data, 0);
1282                       nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1283
1284                       if (!addr_only && !sm->endpoint_dependent)
1285                         break;
1286                     }
1287                 }
1288             }
1289         }
1290
1291       fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, nat_fib_src_low);
1292       if (pool_elts (m->locals))
1293         return 0;
1294
1295       m_key.addr = m->external_addr;
1296       m_key.port = m->external_port;
1297       m_key.fib_index = 0;
1298       kv.key = m_key.as_u64;
1299       clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
1300
1301       vec_free (m->tag);
1302       vec_free (m->workers);
1303       /* Delete static mapping from pool */
1304       pool_put (sm->static_mappings, m);
1305     }
1306
1307   if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1308     return 0;
1309
1310   /* Add/delete external address to FIB */
1311   /* *INDENT-OFF* */
1312   pool_foreach (interface, sm->interfaces,
1313   ({
1314     if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1315       continue;
1316
1317     snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1318     break;
1319   }));
1320   pool_foreach (interface, sm->output_feature_interfaces,
1321   ({
1322     if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1323       continue;
1324
1325     snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1326     break;
1327   }));
1328   /* *INDENT-ON* */
1329
1330   return 0;
1331 }
1332
1333 int
1334 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1335                                  snat_protocol_t proto,
1336                                  nat44_lb_addr_port_t * locals, u8 is_add,
1337                                  twice_nat_type_t twice_nat, u8 out2in_only,
1338                                  u8 * tag, u32 affinity)
1339 {
1340   snat_main_t *sm = &snat_main;
1341   snat_static_mapping_t *m;
1342   snat_session_key_t m_key;
1343   clib_bihash_kv_8_8_t kv, value;
1344   snat_address_t *a = 0;
1345   int i;
1346   nat44_lb_addr_port_t *local;
1347   u32 elt_index, head_index, ses_index;
1348   snat_main_per_thread_data_t *tsm;
1349   snat_user_key_t u_key;
1350   snat_user_t *u;
1351   snat_session_t *s;
1352   dlist_elt_t *head, *elt;
1353   uword *bitmap = 0;
1354
1355   if (!sm->endpoint_dependent)
1356     return VNET_API_ERROR_FEATURE_DISABLED;
1357
1358   m_key.addr = e_addr;
1359   m_key.port = e_port;
1360   m_key.protocol = proto;
1361   m_key.fib_index = 0;
1362   kv.key = m_key.as_u64;
1363   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1364     m = 0;
1365   else
1366     m = pool_elt_at_index (sm->static_mappings, value.value);
1367
1368   if (is_add)
1369     {
1370       if (m)
1371         return VNET_API_ERROR_VALUE_EXIST;
1372
1373       if (vec_len (locals) < 2)
1374         return VNET_API_ERROR_INVALID_VALUE;
1375
1376       /* Find external address in allocated addresses and reserve port for
1377          address and port pair mapping when dynamic translations enabled */
1378       if (!(sm->static_mapping_only || out2in_only))
1379         {
1380           for (i = 0; i < vec_len (sm->addresses); i++)
1381             {
1382               if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1383                 {
1384                   a = sm->addresses + i;
1385                   /* External port must be unused */
1386                   switch (proto)
1387                     {
1388 #define _(N, j, n, s) \
1389                     case SNAT_PROTOCOL_##N: \
1390                       if (a->busy_##n##_port_refcounts[e_port]) \
1391                         return VNET_API_ERROR_INVALID_VALUE; \
1392                       ++a->busy_##n##_port_refcounts[e_port]; \
1393                       if (e_port > 1024) \
1394                         { \
1395                           a->busy_##n##_ports++; \
1396                           a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1397                         } \
1398                       break;
1399                       foreach_snat_protocol
1400 #undef _
1401                     default:
1402                       nat_elog_info ("unknown protocol");
1403                       return VNET_API_ERROR_INVALID_VALUE_2;
1404                     }
1405                   break;
1406                 }
1407             }
1408           /* External address must be allocated */
1409           if (!a)
1410             return VNET_API_ERROR_NO_SUCH_ENTRY;
1411         }
1412
1413       pool_get (sm->static_mappings, m);
1414       clib_memset (m, 0, sizeof (*m));
1415       m->tag = vec_dup (tag);
1416       m->external_addr = e_addr;
1417       m->external_port = e_port;
1418       m->proto = proto;
1419       m->twice_nat = twice_nat;
1420       m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
1421       if (out2in_only)
1422         m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1423       m->affinity = affinity;
1424
1425       if (affinity)
1426         m->affinity_per_service_list_head_index =
1427           nat_affinity_get_per_service_list_head_index ();
1428       else
1429         m->affinity_per_service_list_head_index = ~0;
1430
1431       m_key.addr = m->external_addr;
1432       m_key.port = m->external_port;
1433       m_key.protocol = m->proto;
1434       m_key.fib_index = 0;
1435       kv.key = m_key.as_u64;
1436       kv.value = m - sm->static_mappings;
1437       if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
1438         {
1439           nat_elog_err ("static_mapping_by_external key add failed");
1440           return VNET_API_ERROR_UNSPECIFIED;
1441         }
1442
1443       m_key.fib_index = m->fib_index;
1444       for (i = 0; i < vec_len (locals); i++)
1445         {
1446           locals[i].fib_index =
1447             fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1448                                                locals[i].vrf_id,
1449                                                nat_fib_src_low);
1450           m_key.addr = locals[i].addr;
1451           m_key.fib_index = locals[i].fib_index;
1452           if (!out2in_only)
1453             {
1454               m_key.port = locals[i].port;
1455               kv.key = m_key.as_u64;
1456               kv.value = m - sm->static_mappings;
1457               clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1458             }
1459           locals[i].prefix = (i == 0) ? locals[i].probability :
1460             (locals[i - 1].prefix + locals[i].probability);
1461           pool_get (m->locals, local);
1462           *local = locals[i];
1463           if (sm->num_workers > 1)
1464             {
1465               ip4_header_t ip = {
1466                 .src_address = locals[i].addr,
1467               };
1468               bitmap =
1469                 clib_bitmap_set (bitmap,
1470                                  sm->worker_in2out_cb (&ip, m->fib_index, 0),
1471                                  1);
1472             }
1473         }
1474
1475       /* Assign workers */
1476       if (sm->num_workers > 1)
1477         {
1478           /* *INDENT-OFF* */
1479           clib_bitmap_foreach (i, bitmap,
1480             ({
1481                vec_add1(m->workers, i);
1482             }));
1483           /* *INDENT-ON* */
1484         }
1485     }
1486   else
1487     {
1488       if (!m)
1489         return VNET_API_ERROR_NO_SUCH_ENTRY;
1490
1491       if (!is_lb_static_mapping (m))
1492         return VNET_API_ERROR_INVALID_VALUE;
1493
1494       /* Free external address port */
1495       if (!(sm->static_mapping_only || out2in_only))
1496         {
1497           for (i = 0; i < vec_len (sm->addresses); i++)
1498             {
1499               if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1500                 {
1501                   a = sm->addresses + i;
1502                   switch (proto)
1503                     {
1504 #define _(N, j, n, s) \
1505                     case SNAT_PROTOCOL_##N: \
1506                       --a->busy_##n##_port_refcounts[e_port]; \
1507                       if (e_port > 1024) \
1508                         { \
1509                           a->busy_##n##_ports--; \
1510                           a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1511                         } \
1512                       break;
1513                       foreach_snat_protocol
1514 #undef _
1515                     default:
1516                       nat_elog_info ("unknown protocol");
1517                       return VNET_API_ERROR_INVALID_VALUE_2;
1518                     }
1519                   break;
1520                 }
1521             }
1522         }
1523
1524       m_key.addr = m->external_addr;
1525       m_key.port = m->external_port;
1526       m_key.protocol = m->proto;
1527       m_key.fib_index = 0;
1528       kv.key = m_key.as_u64;
1529       if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1530         {
1531           nat_elog_err ("static_mapping_by_external key del failed");
1532           return VNET_API_ERROR_UNSPECIFIED;
1533         }
1534
1535       /* *INDENT-OFF* */
1536       pool_foreach (local, m->locals,
1537       ({
1538           fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1539                             nat_fib_src_low);
1540           m_key.addr = local->addr;
1541           if (!out2in_only)
1542             {
1543               m_key.port = local->port;
1544               m_key.fib_index = local->fib_index;
1545               kv.key = m_key.as_u64;
1546               if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1547                 {
1548                   nat_elog_err ("static_mapping_by_local key del failed");
1549                   return VNET_API_ERROR_UNSPECIFIED;
1550                 }
1551             }
1552
1553           if (sm->num_workers > 1)
1554             {
1555               ip4_header_t ip = {
1556                 .src_address = local->addr,
1557               };
1558               tsm = vec_elt_at_index (sm->per_thread_data,
1559                                       sm->worker_in2out_cb (&ip, m->fib_index, 0));
1560             }
1561           else
1562             tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1563
1564           /* Delete sessions */
1565           u_key.addr = local->addr;
1566           u_key.fib_index = local->fib_index;
1567           kv.key = u_key.as_u64;
1568           if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1569             {
1570               u = pool_elt_at_index (tsm->users, value.value);
1571               if (u->nstaticsessions)
1572                 {
1573                   head_index = u->sessions_per_user_list_head_index;
1574                   head = pool_elt_at_index (tsm->list_pool, head_index);
1575                   elt_index = head->next;
1576                   elt = pool_elt_at_index (tsm->list_pool, elt_index);
1577                   ses_index = elt->value;
1578                   while (ses_index != ~0)
1579                     {
1580                       s =  pool_elt_at_index (tsm->sessions, ses_index);
1581                       elt = pool_elt_at_index (tsm->list_pool, elt->next);
1582                       ses_index = elt->value;
1583
1584                       if (!(is_lb_session (s)))
1585                         continue;
1586
1587                       if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1588                           (clib_net_to_host_u16 (s->in2out.port) != local->port))
1589                         continue;
1590
1591                       nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1592                       nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1593                     }
1594                 }
1595             }
1596       }));
1597       /* *INDENT-ON* */
1598       if (m->affinity)
1599         nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1600       pool_free (m->locals);
1601       vec_free (m->tag);
1602       vec_free (m->workers);
1603
1604       pool_put (sm->static_mappings, m);
1605     }
1606
1607   return 0;
1608 }
1609
1610 int
1611 nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1612                                        ip4_address_t l_addr, u16 l_port,
1613                                        snat_protocol_t proto, u32 vrf_id,
1614                                        u8 probability, u8 is_add)
1615 {
1616   snat_main_t *sm = &snat_main;
1617   snat_static_mapping_t *m = 0;
1618   snat_session_key_t m_key;
1619   clib_bihash_kv_8_8_t kv, value;
1620   nat44_lb_addr_port_t *local, *prev_local, *match_local = 0;
1621   snat_main_per_thread_data_t *tsm;
1622   snat_user_key_t u_key;
1623   snat_user_t *u;
1624   snat_session_t *s;
1625   dlist_elt_t *head, *elt;
1626   u32 elt_index, head_index, ses_index, *locals = 0;
1627   uword *bitmap = 0;
1628   int i;
1629
1630   if (!sm->endpoint_dependent)
1631     return VNET_API_ERROR_FEATURE_DISABLED;
1632
1633   m_key.addr = e_addr;
1634   m_key.port = e_port;
1635   m_key.protocol = proto;
1636   m_key.fib_index = 0;
1637   kv.key = m_key.as_u64;
1638   if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1639     m = pool_elt_at_index (sm->static_mappings, value.value);
1640
1641   if (!m)
1642     return VNET_API_ERROR_NO_SUCH_ENTRY;
1643
1644   if (!is_lb_static_mapping (m))
1645     return VNET_API_ERROR_INVALID_VALUE;
1646
1647   /* *INDENT-OFF* */
1648   pool_foreach (local, m->locals,
1649   ({
1650     if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1651         (local->vrf_id == vrf_id))
1652       {
1653         match_local = local;
1654         break;
1655       }
1656   }));
1657   /* *INDENT-ON* */
1658
1659   if (is_add)
1660     {
1661       if (match_local)
1662         return VNET_API_ERROR_VALUE_EXIST;
1663
1664       pool_get (m->locals, local);
1665       clib_memset (local, 0, sizeof (*local));
1666       local->addr.as_u32 = l_addr.as_u32;
1667       local->port = l_port;
1668       local->probability = probability;
1669       local->vrf_id = vrf_id;
1670       local->fib_index =
1671         fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1672                                            nat_fib_src_low);
1673
1674       if (!is_out2in_only_static_mapping (m))
1675         {
1676           m_key.addr = l_addr;
1677           m_key.port = l_port;
1678           m_key.fib_index = local->fib_index;
1679           kv.key = m_key.as_u64;
1680           kv.value = m - sm->static_mappings;
1681           if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1))
1682             nat_elog_err ("static_mapping_by_local key add failed");
1683         }
1684     }
1685   else
1686     {
1687       if (!match_local)
1688         return VNET_API_ERROR_NO_SUCH_ENTRY;
1689
1690       if (pool_elts (m->locals) < 3)
1691         return VNET_API_ERROR_UNSPECIFIED;
1692
1693       fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4,
1694                         nat_fib_src_low);
1695
1696       if (!is_out2in_only_static_mapping (m))
1697         {
1698           m_key.addr = l_addr;
1699           m_key.port = l_port;
1700           m_key.fib_index = match_local->fib_index;
1701           kv.key = m_key.as_u64;
1702           if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0))
1703             nat_elog_err ("static_mapping_by_local key del failed");
1704         }
1705
1706       if (sm->num_workers > 1)
1707         {
1708           ip4_header_t ip = {
1709             .src_address = local->addr,
1710           };
1711           tsm = vec_elt_at_index (sm->per_thread_data,
1712                                   sm->worker_in2out_cb (&ip, m->fib_index,
1713                                                         0));
1714         }
1715       else
1716         tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1717
1718       /* Delete sessions */
1719       u_key.addr = match_local->addr;
1720       u_key.fib_index = match_local->fib_index;
1721       kv.key = u_key.as_u64;
1722       if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1723         {
1724           u = pool_elt_at_index (tsm->users, value.value);
1725           if (u->nstaticsessions)
1726             {
1727               head_index = u->sessions_per_user_list_head_index;
1728               head = pool_elt_at_index (tsm->list_pool, head_index);
1729               elt_index = head->next;
1730               elt = pool_elt_at_index (tsm->list_pool, elt_index);
1731               ses_index = elt->value;
1732               while (ses_index != ~0)
1733                 {
1734                   s = pool_elt_at_index (tsm->sessions, ses_index);
1735                   elt = pool_elt_at_index (tsm->list_pool, elt->next);
1736                   ses_index = elt->value;
1737
1738                   if (!(is_lb_session (s)))
1739                     continue;
1740
1741                   if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1742                       (clib_net_to_host_u16 (s->in2out.port) !=
1743                        match_local->port))
1744                     continue;
1745
1746                   nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1747                   nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1748                 }
1749             }
1750         }
1751
1752       pool_put (m->locals, match_local);
1753     }
1754
1755   vec_free (m->workers);
1756
1757   /* *INDENT-OFF* */
1758   pool_foreach (local, m->locals,
1759   ({
1760     vec_add1 (locals, local - m->locals);
1761     if (sm->num_workers > 1)
1762       {
1763         ip4_header_t ip;
1764         ip.src_address.as_u32 = local->addr.as_u32,
1765         bitmap = clib_bitmap_set (bitmap,
1766                                   sm->worker_in2out_cb (&ip, local->fib_index, 0),
1767                                   1);
1768       }
1769   }));
1770   /* *INDENT-ON* */
1771
1772   ASSERT (vec_len (locals) > 1);
1773
1774   local = pool_elt_at_index (m->locals, locals[0]);
1775   local->prefix = local->probability;
1776   for (i = 1; i < vec_len (locals); i++)
1777     {
1778       local = pool_elt_at_index (m->locals, locals[i]);
1779       prev_local = pool_elt_at_index (m->locals, locals[i - 1]);
1780       local->prefix = local->probability + prev_local->prefix;
1781     }
1782
1783   /* Assign workers */
1784   if (sm->num_workers > 1)
1785     {
1786       /* *INDENT-OFF* */
1787       clib_bitmap_foreach (i, bitmap, ({ vec_add1(m->workers, i); }));
1788       /* *INDENT-ON* */
1789     }
1790
1791   return 0;
1792 }
1793
1794 int
1795 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1796                   u8 twice_nat)
1797 {
1798   snat_address_t *a = 0;
1799   snat_session_t *ses;
1800   u32 *ses_to_be_removed = 0, *ses_index;
1801   snat_main_per_thread_data_t *tsm;
1802   snat_static_mapping_t *m;
1803   snat_interface_t *interface;
1804   int i;
1805   snat_address_t *addresses =
1806     twice_nat ? sm->twice_nat_addresses : sm->addresses;
1807
1808   /* Find SNAT address */
1809   for (i = 0; i < vec_len (addresses); i++)
1810     {
1811       if (addresses[i].addr.as_u32 == addr.as_u32)
1812         {
1813           a = addresses + i;
1814           break;
1815         }
1816     }
1817   if (!a)
1818     return VNET_API_ERROR_NO_SUCH_ENTRY;
1819
1820   if (delete_sm)
1821     {
1822       /* *INDENT-OFF* */
1823       pool_foreach (m, sm->static_mappings,
1824       ({
1825           if (m->external_addr.as_u32 == addr.as_u32)
1826             (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1827                                             m->local_port, m->external_port,
1828                                             m->vrf_id, is_addr_only_static_mapping(m), ~0,
1829                                             m->proto, 0, m->twice_nat,
1830                                             is_out2in_only_static_mapping(m), m->tag, is_identity_static_mapping(m));
1831       }));
1832       /* *INDENT-ON* */
1833     }
1834   else
1835     {
1836       /* Check if address is used in some static mapping */
1837       if (is_snat_address_used_in_static_mapping (sm, addr))
1838         {
1839           nat_elog_notice ("address used in static mapping");
1840           return VNET_API_ERROR_UNSPECIFIED;
1841         }
1842     }
1843
1844   if (a->fib_index != ~0)
1845     fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, nat_fib_src_low);
1846
1847   /* Delete sessions using address */
1848   if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1849     {
1850       /* *INDENT-OFF* */
1851       vec_foreach (tsm, sm->per_thread_data)
1852         {
1853           pool_foreach (ses, tsm->sessions, ({
1854             if (ses->out2in.addr.as_u32 == addr.as_u32)
1855               {
1856                 nat_free_session_data (sm, ses, tsm - sm->per_thread_data, 0);
1857                 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1858               }
1859           }));
1860
1861           vec_foreach (ses_index, ses_to_be_removed)
1862             {
1863               ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1864               nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1865             }
1866
1867           vec_free (ses_to_be_removed);
1868         }
1869       /* *INDENT-ON* */
1870     }
1871
1872 #define _(N, i, n, s) \
1873   vec_free (a->busy_##n##_ports_per_thread);
1874   foreach_snat_protocol
1875 #undef _
1876     if (twice_nat)
1877     {
1878       vec_del1 (sm->twice_nat_addresses, i);
1879       return 0;
1880     }
1881   else
1882     vec_del1 (sm->addresses, i);
1883
1884   /* Delete external address from FIB */
1885   /* *INDENT-OFF* */
1886   pool_foreach (interface, sm->interfaces,
1887   ({
1888     if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1889       continue;
1890
1891     snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1892     break;
1893   }));
1894   pool_foreach (interface, sm->output_feature_interfaces,
1895   ({
1896     if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1897       continue;
1898
1899     snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1900     break;
1901   }));
1902   /* *INDENT-ON* */
1903
1904   return 0;
1905 }
1906
1907 int
1908 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1909 {
1910   snat_main_t *sm = &snat_main;
1911   snat_interface_t *i;
1912   const char *feature_name, *del_feature_name;
1913   snat_address_t *ap;
1914   snat_static_mapping_t *m;
1915   snat_det_map_t *dm;
1916   nat_outside_fib_t *outside_fib;
1917   u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1918                                                        sw_if_index);
1919
1920   if (sm->out2in_dpo && !is_inside)
1921     return VNET_API_ERROR_UNSUPPORTED;
1922
1923   /* *INDENT-OFF* */
1924   pool_foreach (i, sm->output_feature_interfaces,
1925   ({
1926     if (i->sw_if_index == sw_if_index)
1927       return VNET_API_ERROR_VALUE_EXIST;
1928   }));
1929   /* *INDENT-ON* */
1930
1931   if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1932     feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1933   else
1934     {
1935       if (sm->num_workers > 1 && !sm->deterministic)
1936         feature_name =
1937           is_inside ? "nat44-in2out-worker-handoff" :
1938           "nat44-out2in-worker-handoff";
1939       else if (sm->deterministic)
1940         feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1941       else if (sm->endpoint_dependent)
1942         {
1943           feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1944         }
1945       else
1946         feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1947     }
1948
1949   if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1950     sm->fq_in2out_index =
1951       vlib_frame_queue_main_init (sm->handoff_in2out_index, NAT_FQ_NELTS);
1952
1953   if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1954     sm->fq_out2in_index =
1955       vlib_frame_queue_main_init (sm->handoff_out2in_index, NAT_FQ_NELTS);
1956
1957   if (!is_inside)
1958     {
1959       /* *INDENT-OFF* */
1960       vec_foreach (outside_fib, sm->outside_fibs)
1961         {
1962           if (outside_fib->fib_index == fib_index)
1963             {
1964               if (is_del)
1965                 {
1966                   outside_fib->refcount--;
1967                   if (!outside_fib->refcount)
1968                     vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1969                 }
1970               else
1971                 outside_fib->refcount++;
1972               goto feature_set;
1973             }
1974         }
1975       /* *INDENT-ON* */
1976       if (!is_del)
1977         {
1978           vec_add2 (sm->outside_fibs, outside_fib, 1);
1979           outside_fib->refcount = 1;
1980           outside_fib->fib_index = fib_index;
1981         }
1982     }
1983 feature_set:
1984   /* *INDENT-OFF* */
1985   pool_foreach (i, sm->interfaces,
1986   ({
1987     if (i->sw_if_index == sw_if_index)
1988       {
1989         if (is_del)
1990           {
1991             if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1992               {
1993                 if (is_inside)
1994                   i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1995                 else
1996                   i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1997
1998                 if (sm->num_workers > 1 && !sm->deterministic)
1999                   {
2000                     del_feature_name = "nat44-handoff-classify";
2001                     feature_name = !is_inside ?  "nat44-in2out-worker-handoff" :
2002                                                  "nat44-out2in-worker-handoff";
2003                   }
2004                 else if (sm->deterministic)
2005                   {
2006                     del_feature_name = "nat44-det-classify";
2007                     feature_name = !is_inside ?  "nat44-det-in2out" :
2008                                                  "nat44-det-out2in";
2009                   }
2010                 else if (sm->endpoint_dependent)
2011                   {
2012                     del_feature_name = "nat44-ed-classify";
2013                     feature_name = !is_inside ?  "nat-pre-in2out" :
2014                                                  "nat-pre-out2in";
2015                   }
2016                 else
2017                   {
2018                     del_feature_name = "nat44-classify";
2019                     feature_name = !is_inside ?  "nat44-in2out" : "nat44-out2in";
2020                   }
2021
2022                 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
2023                 if (rv)
2024                   return rv;
2025                 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
2026                                              sw_if_index, 0, 0, 0);
2027                 vnet_feature_enable_disable ("ip4-unicast", feature_name,
2028                                              sw_if_index, 1, 0, 0);
2029                 if (!is_inside)
2030                   {
2031                     if (sm->endpoint_dependent)
2032                       vnet_feature_enable_disable ("ip4-local",
2033                                                    "nat44-ed-hairpinning",
2034                                                    sw_if_index, 1, 0, 0);
2035                     else if (!sm->deterministic)
2036                       vnet_feature_enable_disable ("ip4-local",
2037                                                    "nat44-hairpinning",
2038                                                    sw_if_index, 1, 0, 0);
2039                   }
2040               }
2041             else
2042               {
2043                 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
2044                 if (rv)
2045                   return rv;
2046                 vnet_feature_enable_disable ("ip4-unicast", feature_name,
2047                                              sw_if_index, 0, 0, 0);
2048                 pool_put (sm->interfaces, i);
2049                 if (is_inside)
2050                   {
2051                     if (sm->endpoint_dependent)
2052                       vnet_feature_enable_disable ("ip4-local",
2053                                                    "nat44-ed-hairpinning",
2054                                                    sw_if_index, 0, 0, 0);
2055                     else if (!sm->deterministic)
2056                       vnet_feature_enable_disable ("ip4-local",
2057                                                    "nat44-hairpinning",
2058                                                    sw_if_index, 0, 0, 0);
2059                   }
2060               }
2061           }
2062         else
2063           {
2064             if ((nat_interface_is_inside(i) && is_inside) ||
2065                 (nat_interface_is_outside(i) && !is_inside))
2066               return 0;
2067
2068             if (sm->num_workers > 1 && !sm->deterministic)
2069               {
2070                 del_feature_name = !is_inside ?  "nat44-in2out-worker-handoff" :
2071                                                  "nat44-out2in-worker-handoff";
2072                 feature_name = "nat44-handoff-classify";
2073               }
2074             else if (sm->deterministic)
2075               {
2076                 del_feature_name = !is_inside ?  "nat44-det-in2out" :
2077                                                  "nat44-det-out2in";
2078                 feature_name = "nat44-det-classify";
2079               }
2080             else if (sm->endpoint_dependent)
2081               {
2082                 del_feature_name = !is_inside ?  "nat-pre-in2out" :
2083                                                  "nat-pre-out2in";
2084
2085                 feature_name = "nat44-ed-classify";
2086               }
2087             else
2088               {
2089                 del_feature_name = !is_inside ?  "nat44-in2out" : "nat44-out2in";
2090                 feature_name = "nat44-classify";
2091               }
2092
2093             int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
2094             if (rv)
2095               return rv;
2096             vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
2097                                          sw_if_index, 0, 0, 0);
2098             vnet_feature_enable_disable ("ip4-unicast", feature_name,
2099                                          sw_if_index, 1, 0, 0);
2100             if (!is_inside)
2101               {
2102                 if (sm->endpoint_dependent)
2103                   vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
2104                                                sw_if_index, 0, 0, 0);
2105                 else if (!sm->deterministic)
2106                   vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
2107                                                sw_if_index, 0, 0, 0);
2108               }
2109             goto set_flags;
2110           }
2111
2112         goto fib;
2113       }
2114   }));
2115   /* *INDENT-ON* */
2116
2117   if (is_del)
2118     return VNET_API_ERROR_NO_SUCH_ENTRY;
2119
2120   pool_get (sm->interfaces, i);
2121   i->sw_if_index = sw_if_index;
2122   i->flags = 0;
2123   vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
2124                                0);
2125
2126   int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
2127   if (rv)
2128     return rv;
2129
2130   if (is_inside && !sm->out2in_dpo)
2131     {
2132       if (sm->endpoint_dependent)
2133         vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
2134                                      sw_if_index, 1, 0, 0);
2135       else if (!sm->deterministic)
2136         vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
2137                                      sw_if_index, 1, 0, 0);
2138     }
2139
2140 set_flags:
2141   if (is_inside)
2142     {
2143       i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2144       return 0;
2145     }
2146   else
2147     i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2148
2149   /* Add/delete external addresses to FIB */
2150 fib:
2151   /* *INDENT-OFF* */
2152   vec_foreach (ap, sm->addresses)
2153     snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2154
2155   pool_foreach (m, sm->static_mappings,
2156   ({
2157     if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2158       continue;
2159
2160     snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2161   }));
2162
2163   pool_foreach (dm, sm->det_maps,
2164   ({
2165     snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
2166   }));
2167   /* *INDENT-ON* */
2168
2169   return 0;
2170 }
2171
2172 int
2173 snat_interface_add_del_output_feature (u32 sw_if_index,
2174                                        u8 is_inside, int is_del)
2175 {
2176   snat_main_t *sm = &snat_main;
2177   snat_interface_t *i;
2178   snat_address_t *ap;
2179   snat_static_mapping_t *m;
2180   nat_outside_fib_t *outside_fib;
2181   u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2182                                                        sw_if_index);
2183
2184
2185   if (sm->deterministic ||
2186       (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
2187     return VNET_API_ERROR_UNSUPPORTED;
2188
2189   /* *INDENT-OFF* */
2190   pool_foreach (i, sm->interfaces,
2191   ({
2192     if (i->sw_if_index == sw_if_index)
2193       return VNET_API_ERROR_VALUE_EXIST;
2194   }));
2195   /* *INDENT-ON* */
2196
2197   if (!is_inside)
2198     {
2199       /* *INDENT-OFF* */
2200       vec_foreach (outside_fib, sm->outside_fibs)
2201         {
2202           if (outside_fib->fib_index == fib_index)
2203             {
2204               if (is_del)
2205                 {
2206                   outside_fib->refcount--;
2207                   if (!outside_fib->refcount)
2208                     vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2209                 }
2210               else
2211                 outside_fib->refcount++;
2212               goto feature_set;
2213             }
2214         }
2215       /* *INDENT-ON* */
2216       if (!is_del)
2217         {
2218           vec_add2 (sm->outside_fibs, outside_fib, 1);
2219           outside_fib->refcount = 1;
2220           outside_fib->fib_index = fib_index;
2221         }
2222     }
2223
2224 feature_set:
2225   if (is_inside)
2226     {
2227       if (sm->endpoint_dependent)
2228         {
2229           int rv =
2230             ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2231           if (rv)
2232             return rv;
2233           rv =
2234             ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2235                                                             !is_del);
2236           if (rv)
2237             return rv;
2238           vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
2239                                        sw_if_index, !is_del, 0, 0);
2240           vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
2241                                        sw_if_index, !is_del, 0, 0);
2242         }
2243       else
2244         {
2245           int rv =
2246             ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2247           if (rv)
2248             return rv;
2249           rv =
2250             ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2251                                                             !is_del);
2252           if (rv)
2253             return rv;
2254           vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
2255                                        sw_if_index, !is_del, 0, 0);
2256           vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
2257                                        sw_if_index, !is_del, 0, 0);
2258         }
2259       goto fq;
2260     }
2261
2262   if (sm->num_workers > 1)
2263     {
2264       int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2265       if (rv)
2266         return rv;
2267       rv =
2268         ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
2269       if (rv)
2270         return rv;
2271       vnet_feature_enable_disable ("ip4-unicast",
2272                                    "nat44-out2in-worker-handoff",
2273                                    sw_if_index, !is_del, 0, 0);
2274       vnet_feature_enable_disable ("ip4-output",
2275                                    "nat44-in2out-output-worker-handoff",
2276                                    sw_if_index, !is_del, 0, 0);
2277     }
2278   else
2279     {
2280       if (sm->endpoint_dependent)
2281         {
2282           int rv =
2283             ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2284           if (rv)
2285             return rv;
2286           rv =
2287             ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2288                                                             !is_del);
2289           if (rv)
2290             return rv;
2291           vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
2292                                        sw_if_index, !is_del, 0, 0);
2293           vnet_feature_enable_disable ("ip4-output", "nat44-ed-in2out-output",
2294                                        sw_if_index, !is_del, 0, 0);
2295         }
2296       else
2297         {
2298           int rv =
2299             ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2300           if (rv)
2301             return rv;
2302           rv =
2303             ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2304                                                             !is_del);
2305           if (rv)
2306             return rv;
2307           vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
2308                                        sw_if_index, !is_del, 0, 0);
2309           vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
2310                                        sw_if_index, !is_del, 0, 0);
2311         }
2312     }
2313
2314 fq:
2315   if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
2316     sm->fq_in2out_output_index =
2317       vlib_frame_queue_main_init (sm->handoff_in2out_output_index, 0);
2318
2319   if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
2320     sm->fq_out2in_index =
2321       vlib_frame_queue_main_init (sm->handoff_out2in_index, 0);
2322
2323   /* *INDENT-OFF* */
2324   pool_foreach (i, sm->output_feature_interfaces,
2325   ({
2326     if (i->sw_if_index == sw_if_index)
2327       {
2328         if (is_del)
2329           pool_put (sm->output_feature_interfaces, i);
2330         else
2331           return VNET_API_ERROR_VALUE_EXIST;
2332
2333         goto fib;
2334       }
2335   }));
2336   /* *INDENT-ON* */
2337
2338   if (is_del)
2339     return VNET_API_ERROR_NO_SUCH_ENTRY;
2340
2341   pool_get (sm->output_feature_interfaces, i);
2342   i->sw_if_index = sw_if_index;
2343   i->flags = 0;
2344   if (is_inside)
2345     i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2346   else
2347     i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2348
2349   /* Add/delete external addresses to FIB */
2350 fib:
2351   if (is_inside)
2352     return 0;
2353
2354   /* *INDENT-OFF* */
2355   vec_foreach (ap, sm->addresses)
2356     snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2357
2358   pool_foreach (m, sm->static_mappings,
2359   ({
2360     if (!((is_addr_only_static_mapping(m)))  || (m->local_addr.as_u32 == m->external_addr.as_u32))
2361       continue;
2362
2363     snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2364   }));
2365   /* *INDENT-ON* */
2366
2367   return 0;
2368 }
2369
2370 int
2371 snat_set_workers (uword * bitmap)
2372 {
2373   snat_main_t *sm = &snat_main;
2374   int i, j = 0;
2375
2376   if (sm->num_workers < 2)
2377     return VNET_API_ERROR_FEATURE_DISABLED;
2378
2379   if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
2380     return VNET_API_ERROR_INVALID_WORKER;
2381
2382   vec_free (sm->workers);
2383   /* *INDENT-OFF* */
2384   clib_bitmap_foreach (i, bitmap,
2385     ({
2386       vec_add1(sm->workers, i);
2387       sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
2388       sm->per_thread_data[sm->first_worker_index + i].thread_index = i;
2389       j++;
2390     }));
2391   /* *INDENT-ON* */
2392
2393   sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
2394   sm->num_snat_thread = _vec_len (sm->workers);
2395
2396   return 0;
2397 }
2398
2399 static void
2400 snat_update_outside_fib (u32 sw_if_index, u32 new_fib_index,
2401                          u32 old_fib_index)
2402 {
2403   snat_main_t *sm = &snat_main;
2404   nat_outside_fib_t *outside_fib;
2405   snat_interface_t *i;
2406   u8 is_add = 1;
2407   u8 match = 0;
2408
2409   if (new_fib_index == old_fib_index)
2410     return;
2411
2412   if (!vec_len (sm->outside_fibs))
2413     return;
2414
2415   /* *INDENT-OFF* */
2416   pool_foreach (i, sm->interfaces,
2417     ({
2418       if (i->sw_if_index == sw_if_index)
2419         {
2420           if (!(nat_interface_is_outside (i)))
2421             return;
2422           match = 1;
2423         }
2424     }));
2425
2426   pool_foreach (i, sm->output_feature_interfaces,
2427     ({
2428       if (i->sw_if_index == sw_if_index)
2429         {
2430           if (!(nat_interface_is_outside (i)))
2431             return;
2432           match = 1;
2433         }
2434     }));
2435   /* *INDENT-ON* */
2436
2437   if (!match)
2438     return;
2439
2440   vec_foreach (outside_fib, sm->outside_fibs)
2441   {
2442     if (outside_fib->fib_index == old_fib_index)
2443       {
2444         outside_fib->refcount--;
2445         if (!outside_fib->refcount)
2446           vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2447         break;
2448       }
2449   }
2450
2451   vec_foreach (outside_fib, sm->outside_fibs)
2452   {
2453     if (outside_fib->fib_index == new_fib_index)
2454       {
2455         outside_fib->refcount++;
2456         is_add = 0;
2457         break;
2458       }
2459   }
2460
2461   if (is_add)
2462     {
2463       vec_add2 (sm->outside_fibs, outside_fib, 1);
2464       outside_fib->refcount = 1;
2465       outside_fib->fib_index = new_fib_index;
2466     }
2467 }
2468
2469 static void
2470 snat_ip4_table_bind (ip4_main_t * im,
2471                      uword opaque,
2472                      u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
2473 {
2474   snat_update_outside_fib (sw_if_index, new_fib_index, old_fib_index);
2475 }
2476
2477 static void
2478 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2479                                        uword opaque,
2480                                        u32 sw_if_index,
2481                                        ip4_address_t * address,
2482                                        u32 address_length,
2483                                        u32 if_address_index, u32 is_delete);
2484
2485 static void
2486 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2487                                  uword opaque,
2488                                  u32 sw_if_index,
2489                                  ip4_address_t * address,
2490                                  u32 address_length,
2491                                  u32 if_address_index, u32 is_delete);
2492
2493 static int
2494 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2495                                  u32 fib_index,
2496                                  u32 thread_index,
2497                                  snat_session_key_t * k,
2498                                  u16 port_per_thread, u32 snat_thread_index);
2499
2500 static clib_error_t *
2501 snat_init (vlib_main_t * vm)
2502 {
2503   snat_main_t *sm = &snat_main;
2504   clib_error_t *error = 0;
2505   ip4_main_t *im = &ip4_main;
2506   ip_lookup_main_t *lm = &im->lookup_main;
2507   uword *p;
2508   vlib_thread_registration_t *tr;
2509   vlib_thread_main_t *tm = vlib_get_thread_main ();
2510   uword *bitmap = 0;
2511   u32 i;
2512   ip4_add_del_interface_address_callback_t cb4;
2513   vlib_node_t *node;
2514
2515   sm->vlib_main = vm;
2516   sm->vnet_main = vnet_get_main ();
2517   sm->ip4_main = im;
2518   sm->ip4_lookup_main = lm;
2519   sm->api_main = vlibapi_get_main ();
2520   sm->first_worker_index = 0;
2521   sm->num_workers = 0;
2522   sm->num_snat_thread = 1;
2523   sm->workers = 0;
2524   sm->port_per_thread = 0xffff - 1024;
2525   sm->fq_in2out_index = ~0;
2526   sm->fq_in2out_output_index = ~0;
2527   sm->fq_out2in_index = ~0;
2528
2529   sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
2530   sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
2531   sm->forwarding_enabled = 0;
2532   sm->log_class = vlib_log_register_class ("nat", 0);
2533   sm->log_level = SNAT_LOG_ERROR;
2534   sm->mss_clamping = 0;
2535
2536   node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
2537   sm->error_node_index = node->index;
2538
2539   node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2540   sm->pre_in2out_node_index = node->index;
2541   node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2542   sm->pre_out2in_node_index = node->index;
2543
2544   node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2545   sm->pre_in2out_node_index = node->index;
2546
2547   node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2548   sm->pre_out2in_node_index = node->index;
2549
2550   node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out");
2551   sm->in2out_node_index = node->index;
2552   node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output");
2553   sm->in2out_output_node_index = node->index;
2554   node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-fast");
2555   sm->in2out_fast_node_index = node->index;
2556   node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-slowpath");
2557   sm->in2out_slowpath_node_index = node->index;
2558   node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output-slowpath");
2559   sm->in2out_slowpath_output_node_index = node->index;
2560
2561   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out");
2562   sm->ed_in2out_node_index = node->index;
2563   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-slowpath");
2564   sm->ed_in2out_slowpath_node_index = node->index;
2565
2566   node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in");
2567   sm->out2in_node_index = node->index;
2568   node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in-fast");
2569   sm->out2in_fast_node_index = node->index;
2570
2571   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in");
2572   sm->ed_out2in_node_index = node->index;
2573   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in-slowpath");
2574   sm->ed_out2in_slowpath_node_index = node->index;
2575
2576   node = vlib_get_node_by_name (vm, (u8 *) "nat44-det-in2out");
2577   sm->det_in2out_node_index = node->index;
2578   node = vlib_get_node_by_name (vm, (u8 *) "nat44-det-out2in");
2579   sm->det_out2in_node_index = node->index;
2580
2581   node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpinning");
2582   sm->hairpinning_node_index = node->index;
2583   node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-dst");
2584   sm->hairpin_dst_node_index = node->index;
2585   node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-src");
2586   sm->hairpin_src_node_index = node->index;
2587   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpinning");
2588   sm->ed_hairpinning_node_index = node->index;
2589   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-dst");
2590   sm->ed_hairpin_dst_node_index = node->index;
2591   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-src");
2592   sm->ed_hairpin_src_node_index = node->index;
2593
2594   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2595   if (p)
2596     {
2597       tr = (vlib_thread_registration_t *) p[0];
2598       if (tr)
2599         {
2600           sm->num_workers = tr->count;
2601           sm->first_worker_index = tr->first_index;
2602         }
2603     }
2604
2605   vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2606
2607   /* Use all available workers by default */
2608   if (sm->num_workers > 1)
2609     {
2610       for (i = 0; i < sm->num_workers; i++)
2611         bitmap = clib_bitmap_set (bitmap, i, 1);
2612       snat_set_workers (bitmap);
2613       clib_bitmap_free (bitmap);
2614     }
2615   else
2616     {
2617       sm->per_thread_data[0].snat_thread_index = 0;
2618     }
2619
2620   error = snat_api_init (vm, sm);
2621   if (error)
2622     return error;
2623
2624   /* Set up the interface address add/del callback */
2625   cb4.function = snat_ip4_add_del_interface_address_cb;
2626   cb4.function_opaque = 0;
2627
2628   vec_add1 (im->add_del_interface_address_callbacks, cb4);
2629
2630   cb4.function = nat_ip4_add_del_addr_only_sm_cb;
2631   cb4.function_opaque = 0;
2632
2633   vec_add1 (im->add_del_interface_address_callbacks, cb4);
2634
2635   nat_dpo_module_init ();
2636
2637   /* Init counters */
2638   sm->total_users.name = "total-users";
2639   sm->total_users.stat_segment_name = "/nat44/total-users";
2640   vlib_validate_simple_counter (&sm->total_users, 0);
2641   vlib_zero_simple_counter (&sm->total_users, 0);
2642   sm->total_sessions.name = "total-sessions";
2643   sm->total_sessions.stat_segment_name = "/nat44/total-sessions";
2644   vlib_validate_simple_counter (&sm->total_sessions, 0);
2645   vlib_zero_simple_counter (&sm->total_sessions, 0);
2646
2647   /* Init IPFIX logging */
2648   snat_ipfix_logging_init (vm);
2649
2650   /* Init NAT64 */
2651   error = nat64_init (vm);
2652   if (error)
2653     return error;
2654
2655   nat66_init (vm);
2656
2657   ip4_table_bind_callback_t cbt4 = {
2658     .function = snat_ip4_table_bind,
2659   };
2660   vec_add1 (ip4_main.table_bind_callbacks, cbt4);
2661
2662   nat_fib_src_hi = fib_source_allocate ("nat-hi",
2663                                         FIB_SOURCE_PRIORITY_HI,
2664                                         FIB_SOURCE_BH_SIMPLE);
2665   nat_fib_src_low = fib_source_allocate ("nat-low",
2666                                          FIB_SOURCE_PRIORITY_LOW,
2667                                          FIB_SOURCE_BH_SIMPLE);
2668
2669   return error;
2670 }
2671
2672 VLIB_INIT_FUNCTION (snat_init);
2673
2674 void
2675 snat_free_outside_address_and_port (snat_address_t * addresses,
2676                                     u32 thread_index, snat_session_key_t * k)
2677 {
2678   snat_address_t *a;
2679   u32 address_index;
2680   u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
2681
2682   for (address_index = 0; address_index < vec_len (addresses);
2683        address_index++)
2684     {
2685       if (addresses[address_index].addr.as_u32 == k->addr.as_u32)
2686         break;
2687     }
2688
2689   ASSERT (address_index < vec_len (addresses));
2690
2691   a = addresses + address_index;
2692
2693   switch (k->protocol)
2694     {
2695 #define _(N, i, n, s) \
2696     case SNAT_PROTOCOL_##N: \
2697       ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
2698       --a->busy_##n##_port_refcounts[port_host_byte_order]; \
2699       a->busy_##n##_ports--; \
2700       a->busy_##n##_ports_per_thread[thread_index]--; \
2701       break;
2702       foreach_snat_protocol
2703 #undef _
2704     default:
2705       nat_elog_info ("unknown protocol");
2706       return;
2707     }
2708 }
2709
2710 static int
2711 nat_set_outside_address_and_port (snat_address_t * addresses,
2712                                   u32 thread_index, snat_session_key_t * k)
2713 {
2714   snat_address_t *a = 0;
2715   u32 address_index;
2716   u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
2717
2718   for (address_index = 0; address_index < vec_len (addresses);
2719        address_index++)
2720     {
2721       if (addresses[address_index].addr.as_u32 != k->addr.as_u32)
2722         continue;
2723
2724       a = addresses + address_index;
2725       switch (k->protocol)
2726         {
2727 #define _(N, j, n, s) \
2728         case SNAT_PROTOCOL_##N: \
2729           if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
2730             return VNET_API_ERROR_INSTANCE_IN_USE; \
2731           ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
2732           a->busy_##n##_ports_per_thread[thread_index]++; \
2733           a->busy_##n##_ports++; \
2734           return 0;
2735           foreach_snat_protocol
2736 #undef _
2737         default:
2738           nat_elog_info ("unknown protocol");
2739           return 1;
2740         }
2741     }
2742
2743   return VNET_API_ERROR_NO_SUCH_ENTRY;
2744 }
2745
2746 int
2747 snat_static_mapping_match (snat_main_t * sm,
2748                            snat_session_key_t match,
2749                            snat_session_key_t * mapping,
2750                            u8 by_external,
2751                            u8 * is_addr_only,
2752                            twice_nat_type_t * twice_nat,
2753                            lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
2754                            u8 * is_identity_nat)
2755 {
2756   clib_bihash_kv_8_8_t kv, value;
2757   snat_static_mapping_t *m;
2758   snat_session_key_t m_key;
2759   clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
2760   u32 rand, lo = 0, hi, mid, *tmp = 0, i;
2761   u8 backend_index;
2762   nat44_lb_addr_port_t *local;
2763
2764   m_key.fib_index = match.fib_index;
2765   if (by_external)
2766     {
2767       mapping_hash = &sm->static_mapping_by_external;
2768       m_key.fib_index = 0;
2769     }
2770
2771   m_key.addr = match.addr;
2772   m_key.port = clib_net_to_host_u16 (match.port);
2773   m_key.protocol = match.protocol;
2774
2775   kv.key = m_key.as_u64;
2776
2777   if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2778     {
2779       /* Try address only mapping */
2780       m_key.port = 0;
2781       m_key.protocol = 0;
2782       kv.key = m_key.as_u64;
2783       if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2784         return 1;
2785     }
2786
2787   m = pool_elt_at_index (sm->static_mappings, value.value);
2788
2789   if (by_external)
2790     {
2791       if (is_lb_static_mapping (m))
2792         {
2793           if (PREDICT_FALSE (lb != 0))
2794             *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2795           if (m->affinity && !nat_affinity_find_and_lock (ext_host_addr[0],
2796                                                           match.addr,
2797                                                           match.protocol,
2798                                                           match.port,
2799                                                           &backend_index))
2800             {
2801               local = pool_elt_at_index (m->locals, backend_index);
2802               mapping->addr = local->addr;
2803               mapping->port = clib_host_to_net_u16 (local->port);
2804               mapping->fib_index = local->fib_index;
2805               goto end;
2806             }
2807           // pick locals matching this worker
2808           if (PREDICT_FALSE (sm->num_workers > 1))
2809             {
2810               u32 thread_index = vlib_get_thread_index ();
2811               /* *INDENT-OFF* */
2812               pool_foreach_index (i, m->locals,
2813               ({
2814                 local = pool_elt_at_index (m->locals, i);
2815
2816                 ip4_header_t ip = {
2817                   .src_address = local->addr,
2818                 };
2819
2820                 if (sm->worker_in2out_cb (&ip, m->fib_index, 0) ==
2821                     thread_index)
2822                   {
2823                     vec_add1 (tmp, i);
2824                   }
2825               }));
2826               /* *INDENT-ON* */
2827               ASSERT (vec_len (tmp) != 0);
2828             }
2829           else
2830             {
2831               /* *INDENT-OFF* */
2832               pool_foreach_index (i, m->locals,
2833               ({
2834                 vec_add1 (tmp, i);
2835               }));
2836               /* *INDENT-ON* */
2837             }
2838           hi = vec_len (tmp) - 1;
2839           local = pool_elt_at_index (m->locals, tmp[hi]);
2840           rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
2841           while (lo < hi)
2842             {
2843               mid = ((hi - lo) >> 1) + lo;
2844               local = pool_elt_at_index (m->locals, tmp[mid]);
2845               (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
2846             }
2847           local = pool_elt_at_index (m->locals, tmp[lo]);
2848           if (!(local->prefix >= rand))
2849             return 1;
2850           mapping->addr = local->addr;
2851           mapping->port = clib_host_to_net_u16 (local->port);
2852           mapping->fib_index = local->fib_index;
2853           if (m->affinity)
2854             {
2855               if (nat_affinity_create_and_lock (ext_host_addr[0], match.addr,
2856                                                 match.protocol, match.port,
2857                                                 tmp[lo], m->affinity,
2858                                                 m->affinity_per_service_list_head_index))
2859                 nat_elog_info ("create affinity record failed");
2860             }
2861           vec_free (tmp);
2862         }
2863       else
2864         {
2865           if (PREDICT_FALSE (lb != 0))
2866             *lb = NO_LB_NAT;
2867           mapping->fib_index = m->fib_index;
2868           mapping->addr = m->local_addr;
2869           /* Address only mapping doesn't change port */
2870           mapping->port = is_addr_only_static_mapping (m) ? match.port
2871             : clib_host_to_net_u16 (m->local_port);
2872         }
2873       mapping->protocol = m->proto;
2874     }
2875   else
2876     {
2877       mapping->addr = m->external_addr;
2878       /* Address only mapping doesn't change port */
2879       mapping->port = is_addr_only_static_mapping (m) ? match.port
2880         : clib_host_to_net_u16 (m->external_port);
2881       mapping->fib_index = sm->outside_fib_index;
2882     }
2883
2884 end:
2885   if (PREDICT_FALSE (is_addr_only != 0))
2886     *is_addr_only = is_addr_only_static_mapping (m);
2887
2888   if (PREDICT_FALSE (twice_nat != 0))
2889     *twice_nat = m->twice_nat;
2890
2891   if (PREDICT_FALSE (is_identity_nat != 0))
2892     *is_identity_nat = is_identity_static_mapping (m);
2893
2894   return 0;
2895 }
2896
2897 static_always_inline u16
2898 snat_random_port (u16 min, u16 max)
2899 {
2900   snat_main_t *sm = &snat_main;
2901   return min + random_u32 (&sm->random_seed) /
2902     (random_u32_max () / (max - min + 1) + 1);
2903 }
2904
2905 int
2906 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2907                                      u32 fib_index,
2908                                      u32 thread_index,
2909                                      snat_session_key_t * k,
2910                                      u16 port_per_thread,
2911                                      u32 snat_thread_index)
2912 {
2913   snat_main_t *sm = &snat_main;
2914
2915   return sm->alloc_addr_and_port (addresses, fib_index, thread_index, k,
2916                                   port_per_thread, snat_thread_index);
2917 }
2918
2919 static int
2920 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2921                                  u32 fib_index,
2922                                  u32 thread_index,
2923                                  snat_session_key_t * k,
2924                                  u16 port_per_thread, u32 snat_thread_index)
2925 {
2926   int i;
2927   snat_address_t *a, *ga = 0;
2928   u32 portnum;
2929
2930   for (i = 0; i < vec_len (addresses); i++)
2931     {
2932       a = addresses + i;
2933       switch (k->protocol)
2934         {
2935 #define _(N, j, n, s) \
2936         case SNAT_PROTOCOL_##N: \
2937           if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
2938             { \
2939               if (a->fib_index == fib_index) \
2940                 { \
2941                   while (1) \
2942                     { \
2943                       portnum = (port_per_thread * \
2944                         snat_thread_index) + \
2945                         snat_random_port(1, port_per_thread) + 1024; \
2946                       if (a->busy_##n##_port_refcounts[portnum]) \
2947                         continue; \
2948                       --a->busy_##n##_port_refcounts[portnum]; \
2949                       a->busy_##n##_ports_per_thread[thread_index]++; \
2950                       a->busy_##n##_ports++; \
2951                       k->addr = a->addr; \
2952                       k->port = clib_host_to_net_u16(portnum); \
2953                       return 0; \
2954                     } \
2955                 } \
2956               else if (a->fib_index == ~0) \
2957                 { \
2958                   ga = a; \
2959                 } \
2960             } \
2961           break;
2962           foreach_snat_protocol
2963 #undef _
2964         default:
2965           nat_elog_info ("unknown protocol");
2966           return 1;
2967         }
2968
2969     }
2970
2971   if (ga)
2972     {
2973       a = ga;
2974       switch (k->protocol)
2975         {
2976 #define _(N, j, n, s) \
2977         case SNAT_PROTOCOL_##N: \
2978           while (1) \
2979             { \
2980               portnum = (port_per_thread * \
2981                 snat_thread_index) + \
2982                 snat_random_port(1, port_per_thread) + 1024; \
2983               if (a->busy_##n##_port_refcounts[portnum]) \
2984                 continue; \
2985               ++a->busy_##n##_port_refcounts[portnum]; \
2986               a->busy_##n##_ports_per_thread[thread_index]++; \
2987               a->busy_##n##_ports++; \
2988               k->addr = a->addr; \
2989               k->port = clib_host_to_net_u16(portnum); \
2990               return 0; \
2991             }
2992           break;
2993           foreach_snat_protocol
2994 #undef _
2995         default:
2996           nat_elog_info ("unknown protocol");
2997           return 1;
2998         }
2999     }
3000
3001   /* Totally out of translations to use... */
3002   snat_ipfix_logging_addresses_exhausted (thread_index, 0);
3003   return 1;
3004 }
3005
3006 static int
3007 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
3008                               u32 fib_index,
3009                               u32 thread_index,
3010                               snat_session_key_t * k,
3011                               u16 port_per_thread, u32 snat_thread_index)
3012 {
3013   snat_main_t *sm = &snat_main;
3014   snat_address_t *a = addresses;
3015   u16 m, ports, portnum, A, j;
3016   m = 16 - (sm->psid_offset + sm->psid_length);
3017   ports = (1 << (16 - sm->psid_length)) - (1 << m);
3018
3019   if (!vec_len (addresses))
3020     goto exhausted;
3021
3022   switch (k->protocol)
3023     {
3024 #define _(N, i, n, s) \
3025     case SNAT_PROTOCOL_##N: \
3026       if (a->busy_##n##_ports < ports) \
3027         { \
3028           while (1) \
3029             { \
3030               A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
3031               j = snat_random_port(0, pow2_mask(m)); \
3032               portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
3033               if (a->busy_##n##_port_refcounts[portnum]) \
3034                 continue; \
3035               ++a->busy_##n##_port_refcounts[portnum]; \
3036               a->busy_##n##_ports++; \
3037               k->addr = a->addr; \
3038               k->port = clib_host_to_net_u16 (portnum); \
3039               return 0; \
3040             } \
3041         } \
3042       break;
3043       foreach_snat_protocol
3044 #undef _
3045     default:
3046       nat_elog_info ("unknown protocol");
3047       return 1;
3048     }
3049
3050 exhausted:
3051   /* Totally out of translations to use... */
3052   snat_ipfix_logging_addresses_exhausted (thread_index, 0);
3053   return 1;
3054 }
3055
3056 static int
3057 nat_alloc_addr_and_port_range (snat_address_t * addresses,
3058                                u32 fib_index,
3059                                u32 thread_index,
3060                                snat_session_key_t * k,
3061                                u16 port_per_thread, u32 snat_thread_index)
3062 {
3063   snat_main_t *sm = &snat_main;
3064   snat_address_t *a = addresses;
3065   u16 portnum, ports;
3066
3067   ports = sm->end_port - sm->start_port + 1;
3068
3069   if (!vec_len (addresses))
3070     goto exhausted;
3071
3072   switch (k->protocol)
3073     {
3074 #define _(N, i, n, s) \
3075     case SNAT_PROTOCOL_##N: \
3076       if (a->busy_##n##_ports < ports) \
3077         { \
3078           while (1) \
3079             { \
3080               portnum = snat_random_port(sm->start_port, sm->end_port); \
3081               if (a->busy_##n##_port_refcounts[portnum]) \
3082                 continue; \
3083               ++a->busy_##n##_port_refcounts[portnum]; \
3084               a->busy_##n##_ports++; \
3085               k->addr = a->addr; \
3086               k->port = clib_host_to_net_u16 (portnum); \
3087               return 0; \
3088             } \
3089         } \
3090       break;
3091       foreach_snat_protocol
3092 #undef _
3093     default:
3094       nat_elog_info ("unknown protocol");
3095       return 1;
3096     }
3097
3098 exhausted:
3099   /* Totally out of translations to use... */
3100   snat_ipfix_logging_addresses_exhausted (thread_index, 0);
3101   return 1;
3102 }
3103
3104 void
3105 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
3106 {
3107   dpo_id_t dpo_v4 = DPO_INVALID;
3108   fib_prefix_t pfx = {
3109     .fp_proto = FIB_PROTOCOL_IP4,
3110     .fp_len = 32,
3111     .fp_addr.ip4.as_u32 = addr.as_u32,
3112   };
3113
3114   if (is_add)
3115     {
3116       nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
3117       fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
3118                                        FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
3119       dpo_reset (&dpo_v4);
3120     }
3121   else
3122     {
3123       fib_table_entry_special_remove (0, &pfx, nat_fib_src_hi);
3124     }
3125 }
3126
3127 u8 *
3128 format_session_kvp (u8 * s, va_list * args)
3129 {
3130   clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
3131   snat_session_key_t k;
3132
3133   k.as_u64 = v->key;
3134
3135   s = format (s, "%U session-index %llu", format_snat_key, &k, v->value);
3136
3137   return s;
3138 }
3139
3140 u8 *
3141 format_static_mapping_kvp (u8 * s, va_list * args)
3142 {
3143   clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
3144   snat_session_key_t k;
3145
3146   k.as_u64 = v->key;
3147
3148   s = format (s, "%U static-mapping-index %llu",
3149               format_static_mapping_key, &k, v->value);
3150
3151   return s;
3152 }
3153
3154 u8 *
3155 format_user_kvp (u8 * s, va_list * args)
3156 {
3157   clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
3158   snat_user_key_t k;
3159
3160   k.as_u64 = v->key;
3161
3162   s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
3163               k.fib_index, v->value);
3164
3165   return s;
3166 }
3167
3168 u8 *
3169 format_ed_session_kvp (u8 * s, va_list * args)
3170 {
3171   clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
3172   nat_ed_ses_key_t k;
3173
3174   k.as_u64[0] = v->key[0];
3175   k.as_u64[1] = v->key[1];
3176
3177   s =
3178     format (s, "local %U:%d remote %U:%d proto %U fib %d session-index %llu",
3179             format_ip4_address, &k.l_addr, clib_net_to_host_u16 (k.l_port),
3180             format_ip4_address, &k.r_addr, clib_net_to_host_u16 (k.r_port),
3181             format_ip_protocol, k.proto, k.fib_index, v->value);
3182
3183   return s;
3184 }
3185
3186 static u32
3187 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0,
3188                            u8 is_output)
3189 {
3190   snat_main_t *sm = &snat_main;
3191   u32 next_worker_index = 0;
3192   u32 hash;
3193
3194   next_worker_index = sm->first_worker_index;
3195   hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
3196     (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
3197
3198   if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3199     next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3200   else
3201     next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3202
3203   return next_worker_index;
3204 }
3205
3206 static u32
3207 snat_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip0,
3208                            u32 rx_fib_index0, u8 is_output)
3209 {
3210   snat_main_t *sm = &snat_main;
3211   udp_header_t *udp;
3212   u16 port;
3213   snat_session_key_t m_key;
3214   clib_bihash_kv_8_8_t kv, value;
3215   snat_static_mapping_t *m;
3216   u32 proto;
3217   u32 next_worker_index = 0;
3218
3219   /* first try static mappings without port */
3220   if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3221     {
3222       m_key.addr = ip0->dst_address;
3223       m_key.port = 0;
3224       m_key.protocol = 0;
3225       m_key.fib_index = rx_fib_index0;
3226       kv.key = m_key.as_u64;
3227       if (!clib_bihash_search_8_8
3228           (&sm->static_mapping_by_external, &kv, &value))
3229         {
3230           m = pool_elt_at_index (sm->static_mappings, value.value);
3231           return m->workers[0];
3232         }
3233     }
3234
3235   proto = ip_proto_to_snat_proto (ip0->protocol);
3236   udp = ip4_next_header (ip0);
3237   port = udp->dst_port;
3238
3239   /* unknown protocol */
3240   if (PREDICT_FALSE (proto == ~0))
3241     {
3242       /* use current thread */
3243       return vlib_get_thread_index ();
3244     }
3245
3246   if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
3247     {
3248       icmp46_header_t *icmp = (icmp46_header_t *) udp;
3249       icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3250       if (!icmp_type_is_error_message
3251           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3252         port = vnet_buffer (b)->ip.reass.l4_src_port;
3253       else
3254         {
3255           /* if error message, then it's not fragmented and we can access it */
3256           ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3257           proto = ip_proto_to_snat_proto (inner_ip->protocol);
3258           void *l4_header = ip4_next_header (inner_ip);
3259           switch (proto)
3260             {
3261             case SNAT_PROTOCOL_ICMP:
3262               icmp = (icmp46_header_t *) l4_header;
3263               echo = (icmp_echo_header_t *) (icmp + 1);
3264               port = echo->identifier;
3265               break;
3266             case SNAT_PROTOCOL_UDP:
3267             case SNAT_PROTOCOL_TCP:
3268               port = ((tcp_udp_header_t *) l4_header)->src_port;
3269               break;
3270             default:
3271               return vlib_get_thread_index ();
3272             }
3273         }
3274     }
3275
3276   /* try static mappings with port */
3277   if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3278     {
3279       m_key.addr = ip0->dst_address;
3280       m_key.port = clib_net_to_host_u16 (port);
3281       m_key.protocol = proto;
3282       m_key.fib_index = rx_fib_index0;
3283       kv.key = m_key.as_u64;
3284       if (!clib_bihash_search_8_8
3285           (&sm->static_mapping_by_external, &kv, &value))
3286         {
3287           m = pool_elt_at_index (sm->static_mappings, value.value);
3288           return m->workers[0];
3289         }
3290     }
3291
3292   /* worker by outside port */
3293   next_worker_index = sm->first_worker_index;
3294   next_worker_index +=
3295     sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3296   return next_worker_index;
3297 }
3298
3299 static u32
3300 nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
3301                                u8 is_output)
3302 {
3303   snat_main_t *sm = &snat_main;
3304   u32 next_worker_index = sm->first_worker_index;
3305   u32 hash;
3306
3307   clib_bihash_kv_16_8_t kv16, value16;
3308   snat_main_per_thread_data_t *tsm;
3309   udp_header_t *udp;
3310
3311   if (PREDICT_FALSE (is_output))
3312     {
3313       u32 fib_index = sm->outside_fib_index;
3314       nat_outside_fib_t *outside_fib;
3315       fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3316       fib_prefix_t pfx = {
3317         .fp_proto = FIB_PROTOCOL_IP4,
3318         .fp_len = 32,
3319         .fp_addr = {
3320                     .ip4.as_u32 = ip->dst_address.as_u32,
3321                     }
3322         ,
3323       };
3324
3325       udp = ip4_next_header (ip);
3326
3327       switch (vec_len (sm->outside_fibs))
3328         {
3329         case 0:
3330           fib_index = sm->outside_fib_index;
3331           break;
3332         case 1:
3333           fib_index = sm->outside_fibs[0].fib_index;
3334           break;
3335         default:
3336             /* *INDENT-OFF* */
3337             vec_foreach (outside_fib, sm->outside_fibs)
3338               {
3339                 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3340                 if (FIB_NODE_INDEX_INVALID != fei)
3341                   {
3342                     if (fib_entry_get_resolving_interface (fei) != ~0)
3343                       {
3344                         fib_index = outside_fib->fib_index;
3345                         break;
3346                       }
3347                   }
3348               }
3349             /* *INDENT-ON* */
3350           break;
3351         }
3352
3353       make_ed_kv (&kv16, &ip->src_address, &ip->dst_address,
3354                   ip->protocol, fib_index, udp->src_port, udp->dst_port);
3355
3356       /* *INDENT-OFF* */
3357       vec_foreach (tsm, sm->per_thread_data)
3358         {
3359           if (PREDICT_TRUE (!clib_bihash_search_16_8 (&tsm->out2in_ed,
3360                                                       &kv16, &value16)))
3361             {
3362               next_worker_index += tsm->thread_index;
3363
3364               nat_elog_debug_handoff (
3365                 "HANDOFF IN2OUT-OUTPUT-FEATURE (session)",
3366                 next_worker_index, fib_index,
3367                 clib_net_to_host_u32 (ip->src_address.as_u32),
3368                 clib_net_to_host_u32 (ip->dst_address.as_u32));
3369
3370               return next_worker_index;
3371             }
3372         }
3373       /* *INDENT-ON* */
3374     }
3375
3376   hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3377     (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3378
3379   if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3380     next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3381   else
3382     next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3383
3384   if (PREDICT_TRUE (!is_output))
3385     {
3386       nat_elog_debug_handoff ("HANDOFF IN2OUT",
3387                               next_worker_index, rx_fib_index,
3388                               clib_net_to_host_u32 (ip->src_address.as_u32),
3389                               clib_net_to_host_u32 (ip->dst_address.as_u32));
3390     }
3391   else
3392     {
3393       nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE",
3394                               next_worker_index, rx_fib_index,
3395                               clib_net_to_host_u32 (ip->src_address.as_u32),
3396                               clib_net_to_host_u32 (ip->dst_address.as_u32));
3397     }
3398
3399   return next_worker_index;
3400 }
3401
3402 static u32
3403 nat44_ed_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip,
3404                                u32 rx_fib_index, u8 is_output)
3405 {
3406   snat_main_t *sm = &snat_main;
3407   clib_bihash_kv_8_8_t kv, value;
3408   clib_bihash_kv_16_8_t kv16, value16;
3409   snat_main_per_thread_data_t *tsm;
3410
3411   u32 proto, next_worker_index = 0;
3412   udp_header_t *udp;
3413   u16 port;
3414   snat_static_mapping_t *m;
3415   u32 hash;
3416
3417   proto = ip_proto_to_snat_proto (ip->protocol);
3418
3419   if (PREDICT_TRUE (proto == SNAT_PROTOCOL_UDP || proto == SNAT_PROTOCOL_TCP))
3420     {
3421       udp = ip4_next_header (ip);
3422
3423       make_ed_kv (&kv16, &ip->dst_address, &ip->src_address,
3424                   ip->protocol, rx_fib_index, udp->dst_port, udp->src_port);
3425
3426       /* *INDENT-OFF* */
3427       vec_foreach (tsm, sm->per_thread_data)
3428         {
3429           if (PREDICT_TRUE (!clib_bihash_search_16_8 (&tsm->out2in_ed,
3430                                                       &kv16, &value16)))
3431             {
3432               next_worker_index = sm->first_worker_index + tsm->thread_index;
3433               nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3434                           next_worker_index, rx_fib_index,
3435                           clib_net_to_host_u32 (ip->src_address.as_u32),
3436                           clib_net_to_host_u32 (ip->dst_address.as_u32));
3437               return next_worker_index;
3438             }
3439           }
3440         /* *INDENT-ON* */
3441     }
3442   else if (proto == SNAT_PROTOCOL_ICMP)
3443     {
3444       nat_ed_ses_key_t key;
3445
3446       if (!get_icmp_o2i_ed_key (b, ip, &key))
3447         {
3448
3449           key.fib_index = rx_fib_index;
3450           kv16.key[0] = key.as_u64[0];
3451           kv16.key[1] = key.as_u64[1];
3452
3453           /* *INDENT-OFF* */
3454           vec_foreach (tsm, sm->per_thread_data)
3455             {
3456               if (PREDICT_TRUE (!clib_bihash_search_16_8 (&tsm->out2in_ed,
3457                                                           &kv16, &value16)))
3458                 {
3459                   next_worker_index = sm->first_worker_index +
3460                                       tsm->thread_index;
3461                   nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3462                               next_worker_index, rx_fib_index,
3463                               clib_net_to_host_u32 (ip->src_address.as_u32),
3464                               clib_net_to_host_u32 (ip->dst_address.as_u32));
3465                   return next_worker_index;
3466                 }
3467             }
3468           /* *INDENT-ON* */
3469         }
3470     }
3471
3472   /* first try static mappings without port */
3473   if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3474     {
3475       make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
3476       if (!clib_bihash_search_8_8
3477           (&sm->static_mapping_by_external, &kv, &value))
3478         {
3479           m = pool_elt_at_index (sm->static_mappings, value.value);
3480           next_worker_index = m->workers[0];
3481           goto done;
3482         }
3483     }
3484
3485   /* unknown protocol */
3486   if (PREDICT_FALSE (proto == ~0))
3487     {
3488       /* use current thread */
3489       next_worker_index = vlib_get_thread_index ();
3490       goto done;
3491     }
3492
3493   udp = ip4_next_header (ip);
3494   port = udp->dst_port;
3495
3496   if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
3497     {
3498       icmp46_header_t *icmp = (icmp46_header_t *) udp;
3499       icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3500       if (!icmp_type_is_error_message
3501           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3502         port = vnet_buffer (b)->ip.reass.l4_src_port;
3503       else
3504         {
3505           /* if error message, then it's not fragmented and we can access it */
3506           ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3507           proto = ip_proto_to_snat_proto (inner_ip->protocol);
3508           void *l4_header = ip4_next_header (inner_ip);
3509           switch (proto)
3510             {
3511             case SNAT_PROTOCOL_ICMP:
3512               icmp = (icmp46_header_t *) l4_header;
3513               echo = (icmp_echo_header_t *) (icmp + 1);
3514               port = echo->identifier;
3515               break;
3516             case SNAT_PROTOCOL_UDP:
3517             case SNAT_PROTOCOL_TCP:
3518               port = ((tcp_udp_header_t *) l4_header)->src_port;
3519               break;
3520             default:
3521               next_worker_index = vlib_get_thread_index ();
3522               goto done;
3523             }
3524         }
3525     }
3526
3527   /* try static mappings with port */
3528   if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3529     {
3530       make_sm_kv (&kv, &ip->dst_address, proto, 0,
3531                   clib_net_to_host_u16 (port));
3532       if (!clib_bihash_search_8_8
3533           (&sm->static_mapping_by_external, &kv, &value))
3534         {
3535           m = pool_elt_at_index (sm->static_mappings, value.value);
3536           if (!is_lb_static_mapping (m))
3537             {
3538               next_worker_index = m->workers[0];
3539               goto done;
3540             }
3541
3542           hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3543             (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3544
3545           if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
3546             next_worker_index =
3547               m->workers[hash & (_vec_len (m->workers) - 1)];
3548           else
3549             next_worker_index = m->workers[hash % _vec_len (m->workers)];
3550           goto done;
3551         }
3552     }
3553
3554   /* worker by outside port */
3555   next_worker_index = sm->first_worker_index;
3556   next_worker_index +=
3557     sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3558
3559 done:
3560   nat_elog_debug_handoff ("HANDOFF OUT2IN", next_worker_index, rx_fib_index,
3561                           clib_net_to_host_u32 (ip->src_address.as_u32),
3562                           clib_net_to_host_u32 (ip->dst_address.as_u32));
3563   return next_worker_index;
3564 }
3565
3566 void
3567 nat_ha_sadd_cb (ip4_address_t * in_addr, u16 in_port,
3568                 ip4_address_t * out_addr, u16 out_port,
3569                 ip4_address_t * eh_addr, u16 eh_port,
3570                 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
3571                 u32 fib_index, u16 flags, u32 thread_index)
3572 {
3573   snat_main_t *sm = &snat_main;
3574   snat_session_key_t key;
3575   snat_user_t *u;
3576   snat_session_t *s;
3577   clib_bihash_kv_8_8_t kv;
3578   f64 now = vlib_time_now (sm->vlib_main);
3579   nat_outside_fib_t *outside_fib;
3580   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3581   snat_main_per_thread_data_t *tsm;
3582   fib_prefix_t pfx = {
3583     .fp_proto = FIB_PROTOCOL_IP4,
3584     .fp_len = 32,
3585     .fp_addr = {
3586                 .ip4.as_u32 = eh_addr->as_u32,
3587                 },
3588   };
3589
3590   tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3591
3592   key.addr.as_u32 = out_addr->as_u32;
3593   key.port = out_port;
3594   key.protocol = proto;
3595
3596   if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
3597     {
3598       if (nat_set_outside_address_and_port
3599           (sm->addresses, thread_index, &key))
3600         return;
3601     }
3602
3603   u = nat_user_get_or_create (sm, in_addr, fib_index, thread_index);
3604   if (!u)
3605     return;
3606
3607   s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
3608   if (!s)
3609     return;
3610
3611   s->last_heard = now;
3612   s->flags = flags;
3613   s->ext_host_addr.as_u32 = eh_addr->as_u32;
3614   s->ext_host_port = eh_port;
3615   user_session_increment (sm, u, snat_is_session_static (s));
3616   switch (vec_len (sm->outside_fibs))
3617     {
3618     case 0:
3619       key.fib_index = sm->outside_fib_index;
3620       break;
3621     case 1:
3622       key.fib_index = sm->outside_fibs[0].fib_index;
3623       break;
3624     default:
3625       /* *INDENT-OFF* */
3626       vec_foreach (outside_fib, sm->outside_fibs)
3627         {
3628           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3629           if (FIB_NODE_INDEX_INVALID != fei)
3630             {
3631               if (fib_entry_get_resolving_interface (fei) != ~0)
3632                 {
3633                   key.fib_index = outside_fib->fib_index;
3634                   break;
3635                 }
3636             }
3637         }
3638       /* *INDENT-ON* */
3639       break;
3640     }
3641   s->out2in = key;
3642   kv.key = key.as_u64;
3643   kv.value = s - tsm->sessions;
3644   if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 1))
3645     nat_elog_warn ("out2in key add failed");
3646
3647   key.addr.as_u32 = in_addr->as_u32;
3648   key.port = in_port;
3649   key.fib_index = fib_index;
3650   s->in2out = key;
3651   kv.key = key.as_u64;
3652   if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 1))
3653     nat_elog_warn ("in2out key add failed");
3654 }
3655
3656 void
3657 nat_ha_sdel_cb (ip4_address_t * out_addr, u16 out_port,
3658                 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
3659                 u32 ti)
3660 {
3661   snat_main_t *sm = &snat_main;
3662   snat_session_key_t key;
3663   clib_bihash_kv_8_8_t kv, value;
3664   u32 thread_index;
3665   snat_session_t *s;
3666   snat_main_per_thread_data_t *tsm;
3667
3668   if (sm->num_workers > 1)
3669     thread_index =
3670       sm->first_worker_index +
3671       (sm->workers[(clib_net_to_host_u16 (out_port) -
3672                     1024) / sm->port_per_thread]);
3673   else
3674     thread_index = sm->num_workers;
3675   tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3676
3677   key.addr.as_u32 = out_addr->as_u32;
3678   key.port = out_port;
3679   key.protocol = proto;
3680   key.fib_index = fib_index;
3681   kv.key = key.as_u64;
3682   if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
3683     return;
3684
3685   s = pool_elt_at_index (tsm->sessions, value.value);
3686   nat_free_session_data (sm, s, thread_index, 1);
3687   nat44_delete_session (sm, s, thread_index);
3688 }
3689
3690 void
3691 nat_ha_sref_cb (ip4_address_t * out_addr, u16 out_port,
3692                 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
3693                 u32 total_pkts, u64 total_bytes, u32 thread_index)
3694 {
3695   snat_main_t *sm = &snat_main;
3696   snat_session_key_t key;
3697   clib_bihash_kv_8_8_t kv, value;
3698   snat_session_t *s;
3699   snat_main_per_thread_data_t *tsm;
3700
3701   tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3702
3703   key.addr.as_u32 = out_addr->as_u32;
3704   key.port = out_port;
3705   key.protocol = proto;
3706   key.fib_index = fib_index;
3707   kv.key = key.as_u64;
3708   if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
3709     return;
3710
3711   s = pool_elt_at_index (tsm->sessions, value.value);
3712   s->total_pkts = total_pkts;
3713   s->total_bytes = total_bytes;
3714 }
3715
3716 void
3717 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
3718                    ip4_address_t * out_addr, u16 out_port,
3719                    ip4_address_t * eh_addr, u16 eh_port,
3720                    ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
3721                    u32 fib_index, u16 flags, u32 thread_index)
3722 {
3723   snat_main_t *sm = &snat_main;
3724   snat_session_key_t key;
3725   snat_user_t *u;
3726   snat_session_t *s;
3727   clib_bihash_kv_16_8_t kv;
3728   f64 now = vlib_time_now (sm->vlib_main);
3729   nat_outside_fib_t *outside_fib;
3730   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3731   snat_main_per_thread_data_t *tsm;
3732   fib_prefix_t pfx = {
3733     .fp_proto = FIB_PROTOCOL_IP4,
3734     .fp_len = 32,
3735     .fp_addr = {
3736                 .ip4.as_u32 = eh_addr->as_u32,
3737                 },
3738   };
3739
3740   tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3741
3742   key.addr.as_u32 = out_addr->as_u32;
3743   key.port = out_port;
3744   key.protocol = proto;
3745
3746   if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
3747     {
3748       if (nat_set_outside_address_and_port
3749           (sm->addresses, thread_index, &key))
3750         return;
3751     }
3752
3753   key.addr.as_u32 = ehn_addr->as_u32;
3754   key.port = ehn_port;
3755   if (flags & SNAT_SESSION_FLAG_TWICE_NAT)
3756     {
3757       if (nat_set_outside_address_and_port
3758           (sm->twice_nat_addresses, thread_index, &key))
3759         return;
3760     }
3761
3762   u = nat_user_get_or_create (sm, in_addr, fib_index, thread_index);
3763   if (!u)
3764     return;
3765
3766   s = nat_ed_session_alloc (sm, u, thread_index, now);
3767   if (!s)
3768     return;
3769
3770   s->last_heard = now;
3771   s->flags = flags;
3772   s->ext_host_nat_addr.as_u32 = s->ext_host_addr.as_u32 = eh_addr->as_u32;
3773   s->ext_host_nat_port = s->ext_host_port = eh_port;
3774   if (is_twice_nat_session (s))
3775     {
3776       s->ext_host_nat_addr.as_u32 = ehn_addr->as_u32;
3777       s->ext_host_nat_port = ehn_port;
3778     }
3779   user_session_increment (sm, u, snat_is_session_static (s));
3780   switch (vec_len (sm->outside_fibs))
3781     {
3782     case 0:
3783       key.fib_index = sm->outside_fib_index;
3784       break;
3785     case 1:
3786       key.fib_index = sm->outside_fibs[0].fib_index;
3787       break;
3788     default:
3789       /* *INDENT-OFF* */
3790       vec_foreach (outside_fib, sm->outside_fibs)
3791         {
3792           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3793           if (FIB_NODE_INDEX_INVALID != fei)
3794             {
3795               if (fib_entry_get_resolving_interface (fei) != ~0)
3796                 {
3797                   key.fib_index = outside_fib->fib_index;
3798                   break;
3799                 }
3800             }
3801         }
3802       /* *INDENT-ON* */
3803       break;
3804     }
3805   key.addr.as_u32 = out_addr->as_u32;
3806   key.port = out_port;
3807   s->out2in = key;
3808   kv.value = s - tsm->sessions;
3809
3810   key.addr.as_u32 = in_addr->as_u32;
3811   key.port = in_port;
3812   key.fib_index = fib_index;
3813   s->in2out = key;
3814
3815   make_ed_kv (&kv, in_addr, &s->ext_host_nat_addr,
3816               snat_proto_to_ip_proto (proto), fib_index, in_port,
3817               s->ext_host_nat_port);
3818   if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
3819     nat_elog_warn ("in2out key add failed");
3820
3821   make_ed_kv (&kv, out_addr, eh_addr, snat_proto_to_ip_proto (proto),
3822               s->out2in.fib_index, out_port, eh_port);
3823   if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 1))
3824     nat_elog_warn ("out2in key add failed");
3825 }
3826
3827 void
3828 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
3829                    ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3830                    u32 fib_index, u32 ti)
3831 {
3832   snat_main_t *sm = &snat_main;
3833   nat_ed_ses_key_t key;
3834   clib_bihash_kv_16_8_t kv, value;
3835   u32 thread_index;
3836   snat_session_t *s;
3837   snat_main_per_thread_data_t *tsm;
3838
3839   if (sm->num_workers > 1)
3840     thread_index =
3841       sm->first_worker_index +
3842       (sm->workers[(clib_net_to_host_u16 (out_port) -
3843                     1024) / sm->port_per_thread]);
3844   else
3845     thread_index = sm->num_workers;
3846   tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3847
3848   key.l_addr.as_u32 = out_addr->as_u32;
3849   key.l_port = out_port;
3850   key.r_addr.as_u32 = eh_addr->as_u32;
3851   key.r_port = eh_port;
3852   key.proto = proto;
3853   key.fib_index = fib_index;
3854   kv.key[0] = key.as_u64[0];
3855   kv.key[1] = key.as_u64[1];
3856   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
3857     return;
3858
3859   s = pool_elt_at_index (tsm->sessions, value.value);
3860   nat_free_session_data (sm, s, thread_index, 1);
3861   nat44_delete_session (sm, s, thread_index);
3862 }
3863
3864 void
3865 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
3866                    ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3867                    u32 fib_index, u32 total_pkts, u64 total_bytes,
3868                    u32 thread_index)
3869 {
3870   snat_main_t *sm = &snat_main;
3871   nat_ed_ses_key_t key;
3872   clib_bihash_kv_16_8_t kv, value;
3873   snat_session_t *s;
3874   snat_main_per_thread_data_t *tsm;
3875
3876   tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3877
3878   key.l_addr.as_u32 = out_addr->as_u32;
3879   key.l_port = out_port;
3880   key.r_addr.as_u32 = eh_addr->as_u32;
3881   key.r_port = eh_port;
3882   key.proto = proto;
3883   key.fib_index = fib_index;
3884   kv.key[0] = key.as_u64[0];
3885   kv.key[1] = key.as_u64[1];
3886   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
3887     return;
3888
3889   s = pool_elt_at_index (tsm->sessions, value.value);
3890   s->total_pkts = total_pkts;
3891   s->total_bytes = total_bytes;
3892 }
3893
3894 static clib_error_t *
3895 snat_config (vlib_main_t * vm, unformat_input_t * input)
3896 {
3897   snat_main_t *sm = &snat_main;
3898   nat66_main_t *nm = &nat66_main;
3899   //dslite_main_t *dm = &dslite_main;
3900   snat_main_per_thread_data_t *tsm;
3901
3902   u32 static_mapping_buckets = 1024;
3903   uword static_mapping_memory_size = 64 << 20;
3904
3905   u32 nat64_bib_buckets = 1024;
3906   u32 nat64_bib_memory_size = 128 << 20;
3907
3908   u32 nat64_st_buckets = 2048;
3909   uword nat64_st_memory_size = 256 << 20;
3910
3911   u32 user_buckets = 128;
3912   uword user_memory_size = 64 << 20;
3913   u32 translation_buckets = 1024;
3914   uword translation_memory_size = 128 << 20;
3915
3916   u32 max_translations_per_user = ~0;
3917
3918   u32 outside_vrf_id = 0;
3919   u32 outside_ip6_vrf_id = 0;
3920   u32 inside_vrf_id = 0;
3921   u8 static_mapping_only = 0;
3922   u8 static_mapping_connection_tracking = 0;
3923
3924   // configurable timeouts
3925   u32 udp_timeout = SNAT_UDP_TIMEOUT;
3926   u32 icmp_timeout = SNAT_ICMP_TIMEOUT;
3927   u32 tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
3928   u32 tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
3929
3930   sm->deterministic = 0;
3931   sm->out2in_dpo = 0;
3932   sm->endpoint_dependent = 0;
3933
3934   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3935     {
3936       if (unformat
3937           (input, "translation hash buckets %d", &translation_buckets))
3938         ;
3939       else if (unformat (input, "udp timeout %d", &udp_timeout))
3940         ;
3941       else if (unformat (input, "icmp timeout %d", &icmp_timeout))
3942         ;
3943       else if (unformat (input, "tcp transitory timeout %d",
3944                          &tcp_transitory_timeout));
3945       else if (unformat (input, "tcp established timeout %d",
3946                          &tcp_established_timeout));
3947       else if (unformat (input, "translation hash memory %d",
3948                          &translation_memory_size));
3949       else if (unformat (input, "user hash buckets %d", &user_buckets))
3950         ;
3951       else if (unformat (input, "user hash memory %d", &user_memory_size))
3952         ;
3953       else if (unformat (input, "max translations per user %d",
3954                          &max_translations_per_user))
3955         ;
3956       else if (unformat (input, "outside VRF id %d", &outside_vrf_id))
3957         ;
3958       else if (unformat (input, "outside ip6 VRF id %d", &outside_ip6_vrf_id))
3959         ;
3960       else if (unformat (input, "inside VRF id %d", &inside_vrf_id))
3961         ;
3962       else if (unformat (input, "static mapping only"))
3963         {
3964           static_mapping_only = 1;
3965           if (unformat (input, "connection tracking"))
3966             static_mapping_connection_tracking = 1;
3967         }
3968       else if (unformat (input, "deterministic"))
3969         sm->deterministic = 1;
3970       else if (unformat (input, "nat64 bib hash buckets %d",
3971                          &nat64_bib_buckets))
3972         ;
3973       else if (unformat (input, "nat64 bib hash memory %d",
3974                          &nat64_bib_memory_size))
3975         ;
3976       else
3977         if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
3978         ;
3979       else if (unformat (input, "nat64 st hash memory %d",
3980                          &nat64_st_memory_size))
3981         ;
3982       else if (unformat (input, "out2in dpo"))
3983         sm->out2in_dpo = 1;
3984       //else if (unformat (input, "dslite ce"))
3985       //dslite_set_ce (dm, 1);
3986       else if (unformat (input, "endpoint-dependent"))
3987         sm->endpoint_dependent = 1;
3988       else
3989         return clib_error_return (0, "unknown input '%U'",
3990                                   format_unformat_error, input);
3991     }
3992
3993   if (sm->deterministic && sm->endpoint_dependent)
3994     return clib_error_return (0,
3995                               "deterministic and endpoint-dependent modes are mutually exclusive");
3996
3997   if (static_mapping_only && (sm->deterministic || sm->endpoint_dependent))
3998     return clib_error_return (0,
3999                               "static mapping only mode available only for simple nat");
4000
4001   if (sm->out2in_dpo && (sm->deterministic || sm->endpoint_dependent))
4002     return clib_error_return (0,
4003                               "out2in dpo mode available only for simple nat");
4004
4005   /* optionally configurable timeouts for testing purposes */
4006   sm->udp_timeout = udp_timeout;
4007   sm->tcp_transitory_timeout = tcp_transitory_timeout;
4008   sm->tcp_established_timeout = tcp_established_timeout;
4009   sm->icmp_timeout = icmp_timeout;
4010
4011   sm->user_buckets = user_buckets;
4012   sm->user_memory_size = user_memory_size;
4013
4014   sm->translation_buckets = translation_buckets;
4015   sm->translation_memory_size = translation_memory_size;
4016
4017   /* do not exceed load factor 10 */
4018   sm->max_translations = 10 * translation_buckets;
4019   sm->max_translations_per_user = max_translations_per_user == ~0 ?
4020     sm->max_translations : max_translations_per_user;
4021
4022   sm->outside_vrf_id = outside_vrf_id;
4023   sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
4024                                                              outside_vrf_id,
4025                                                              nat_fib_src_hi);
4026   nm->outside_vrf_id = outside_ip6_vrf_id;
4027   nm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
4028                                                              outside_ip6_vrf_id,
4029                                                              nat_fib_src_hi);
4030   sm->inside_vrf_id = inside_vrf_id;
4031   sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
4032                                                             inside_vrf_id,
4033                                                             nat_fib_src_hi);
4034   sm->static_mapping_only = static_mapping_only;
4035   sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
4036
4037   nat64_set_hash (nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
4038                   nat64_st_memory_size);
4039
4040   if (sm->deterministic)
4041     {
4042       sm->in2out_node_index = snat_det_in2out_node.index;
4043       sm->in2out_output_node_index = ~0;
4044       sm->out2in_node_index = snat_det_out2in_node.index;
4045       sm->icmp_match_in2out_cb = icmp_match_in2out_det;
4046       sm->icmp_match_out2in_cb = icmp_match_out2in_det;
4047     }
4048   else
4049     {
4050       if (sm->endpoint_dependent)
4051         {
4052           sm->worker_in2out_cb = nat44_ed_get_worker_in2out_cb;
4053           sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
4054
4055           sm->handoff_out2in_index = nat_pre_out2in_node.index;
4056           sm->handoff_in2out_index = nat_pre_in2out_node.index;
4057           sm->handoff_in2out_output_index = nat44_ed_in2out_output_node.index;
4058
4059           sm->in2out_node_index = nat44_ed_in2out_node.index;
4060           sm->in2out_output_node_index = nat44_ed_in2out_output_node.index;
4061           sm->out2in_node_index = nat44_ed_out2in_node.index;
4062
4063           sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
4064           sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
4065           nat_affinity_init (vm);
4066           nat_ha_init (vm, nat_ha_sadd_ed_cb, nat_ha_sdel_ed_cb,
4067                        nat_ha_sref_ed_cb);
4068         }
4069       else
4070         {
4071           sm->worker_in2out_cb = snat_get_worker_in2out_cb;
4072           sm->worker_out2in_cb = snat_get_worker_out2in_cb;
4073
4074           sm->handoff_out2in_index = snat_out2in_node.index;
4075           sm->handoff_in2out_index = snat_in2out_node.index;
4076           sm->handoff_in2out_output_index = snat_in2out_output_node.index;
4077
4078           sm->in2out_node_index = snat_in2out_node.index;
4079           sm->in2out_output_node_index = snat_in2out_output_node.index;
4080           sm->out2in_node_index = snat_out2in_node.index;
4081           sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
4082           sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
4083           nat_ha_init (vm, nat_ha_sadd_cb, nat_ha_sdel_cb, nat_ha_sref_cb);
4084         }
4085       if (!static_mapping_only ||
4086           (static_mapping_only && static_mapping_connection_tracking))
4087         {
4088           /* *INDENT-OFF* */
4089           vec_foreach (tsm, sm->per_thread_data)
4090             {
4091               pool_alloc (tsm->sessions, sm->max_translations);
4092               pool_alloc (tsm->list_pool, sm->max_translations);
4093               pool_alloc (tsm->global_lru_pool, sm->max_translations);
4094
4095               dlist_elt_t *head;
4096               pool_get (tsm->global_lru_pool, head);
4097               tsm->global_lru_head_index = head - tsm->global_lru_pool;
4098               clib_dlist_init (tsm->global_lru_pool,
4099                                tsm->global_lru_head_index);
4100
4101               if (sm->endpoint_dependent)
4102                 {
4103                   clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
4104                                          translation_buckets,
4105                                          translation_memory_size);
4106                   clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
4107                                                       format_ed_session_kvp);
4108
4109                   clib_bihash_init_16_8 (&tsm->out2in_ed, "out2in-ed",
4110                                          translation_buckets,
4111                                          translation_memory_size);
4112                   clib_bihash_set_kvp_format_fn_16_8 (&tsm->out2in_ed,
4113                                                       format_ed_session_kvp);
4114                 }
4115               else
4116                 {
4117                   clib_bihash_init_8_8 (&tsm->in2out, "in2out",
4118                                         translation_buckets,
4119                                         translation_memory_size);
4120                   clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out,
4121                                                      format_session_kvp);
4122
4123                   clib_bihash_init_8_8 (&tsm->out2in, "out2in",
4124                                         translation_buckets,
4125                                         translation_memory_size);
4126                   clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in,
4127                                                      format_session_kvp);
4128                 }
4129
4130               clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
4131                                     user_memory_size);
4132               clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash,
4133                                                  format_user_kvp);
4134             }
4135           /* *INDENT-ON* */
4136
4137         }
4138       else
4139         {
4140           sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
4141           sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
4142         }
4143       clib_bihash_init_8_8 (&sm->static_mapping_by_local,
4144                             "static_mapping_by_local", static_mapping_buckets,
4145                             static_mapping_memory_size);
4146       clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
4147                                          format_static_mapping_kvp);
4148
4149       clib_bihash_init_8_8 (&sm->static_mapping_by_external,
4150                             "static_mapping_by_external",
4151                             static_mapping_buckets,
4152                             static_mapping_memory_size);
4153       clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
4154                                          format_static_mapping_kvp);
4155     }
4156
4157   return 0;
4158 }
4159
4160 VLIB_CONFIG_FUNCTION (snat_config, "nat");
4161
4162 static void
4163 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
4164                                  uword opaque,
4165                                  u32 sw_if_index,
4166                                  ip4_address_t * address,
4167                                  u32 address_length,
4168                                  u32 if_address_index, u32 is_delete)
4169 {
4170   snat_main_t *sm = &snat_main;
4171   snat_static_map_resolve_t *rp;
4172   snat_static_mapping_t *m;
4173   snat_session_key_t m_key;
4174   clib_bihash_kv_8_8_t kv, value;
4175   int i, rv;
4176   ip4_address_t l_addr;
4177
4178   for (i = 0; i < vec_len (sm->to_resolve); i++)
4179     {
4180       rp = sm->to_resolve + i;
4181       if (rp->addr_only == 0)
4182         continue;
4183       if (rp->sw_if_index == sw_if_index)
4184         goto match;
4185     }
4186
4187   return;
4188
4189 match:
4190   m_key.addr.as_u32 = address->as_u32;
4191   m_key.port = rp->addr_only ? 0 : rp->e_port;
4192   m_key.protocol = rp->addr_only ? 0 : rp->proto;
4193   m_key.fib_index = sm->outside_fib_index;
4194   kv.key = m_key.as_u64;
4195   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
4196     m = 0;
4197   else
4198     m = pool_elt_at_index (sm->static_mappings, value.value);
4199
4200   if (!is_delete)
4201     {
4202       /* Don't trip over lease renewal, static config */
4203       if (m)
4204         return;
4205     }
4206   else
4207     {
4208       if (!m)
4209         return;
4210     }
4211
4212   /* Indetity mapping? */
4213   if (rp->l_addr.as_u32 == 0)
4214     l_addr.as_u32 = address[0].as_u32;
4215   else
4216     l_addr.as_u32 = rp->l_addr.as_u32;
4217   /* Add the static mapping */
4218   rv = snat_add_static_mapping (l_addr,
4219                                 address[0],
4220                                 rp->l_port,
4221                                 rp->e_port,
4222                                 rp->vrf_id,
4223                                 rp->addr_only, ~0 /* sw_if_index */ ,
4224                                 rp->proto, !is_delete, rp->twice_nat,
4225                                 rp->out2in_only, rp->tag, rp->identity_nat);
4226   if (rv)
4227     nat_elog_notice_X1 ("snat_add_static_mapping returned %d", "i4", rv);
4228 }
4229
4230 static void
4231 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
4232                                        uword opaque,
4233                                        u32 sw_if_index,
4234                                        ip4_address_t * address,
4235                                        u32 address_length,
4236                                        u32 if_address_index, u32 is_delete)
4237 {
4238   snat_main_t *sm = &snat_main;
4239   snat_static_map_resolve_t *rp;
4240   ip4_address_t l_addr;
4241   int i, j;
4242   int rv;
4243   u8 twice_nat = 0;
4244   snat_address_t *addresses = sm->addresses;
4245
4246   for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
4247     {
4248       if (sw_if_index == sm->auto_add_sw_if_indices[i])
4249         goto match;
4250     }
4251
4252   for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
4253     {
4254       twice_nat = 1;
4255       addresses = sm->twice_nat_addresses;
4256       if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
4257         goto match;
4258     }
4259
4260   return;
4261
4262 match:
4263   if (!is_delete)
4264     {
4265       /* Don't trip over lease renewal, static config */
4266       for (j = 0; j < vec_len (addresses); j++)
4267         if (addresses[j].addr.as_u32 == address->as_u32)
4268           return;
4269
4270       (void) snat_add_address (sm, address, ~0, twice_nat);
4271       /* Scan static map resolution vector */
4272       for (j = 0; j < vec_len (sm->to_resolve); j++)
4273         {
4274           rp = sm->to_resolve + j;
4275           if (rp->addr_only)
4276             continue;
4277           /* On this interface? */
4278           if (rp->sw_if_index == sw_if_index)
4279             {
4280               /* Indetity mapping? */
4281               if (rp->l_addr.as_u32 == 0)
4282                 l_addr.as_u32 = address[0].as_u32;
4283               else
4284                 l_addr.as_u32 = rp->l_addr.as_u32;
4285               /* Add the static mapping */
4286               rv = snat_add_static_mapping (l_addr,
4287                                             address[0],
4288                                             rp->l_port,
4289                                             rp->e_port,
4290                                             rp->vrf_id,
4291                                             rp->addr_only,
4292                                             ~0 /* sw_if_index */ ,
4293                                             rp->proto,
4294                                             rp->is_add, rp->twice_nat,
4295                                             rp->out2in_only, rp->tag,
4296                                             rp->identity_nat);
4297               if (rv)
4298                 nat_elog_notice_X1 ("snat_add_static_mapping returned %d",
4299                                     "i4", rv);
4300             }
4301         }
4302       return;
4303     }
4304   else
4305     {
4306       (void) snat_del_address (sm, address[0], 1, twice_nat);
4307       return;
4308     }
4309 }
4310
4311
4312 int
4313 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
4314                             u8 twice_nat)
4315 {
4316   ip4_main_t *ip4_main = sm->ip4_main;
4317   ip4_address_t *first_int_addr;
4318   snat_static_map_resolve_t *rp;
4319   u32 *indices_to_delete = 0;
4320   int i, j;
4321   u32 *auto_add_sw_if_indices =
4322     twice_nat ? sm->
4323     auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
4324
4325   first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0        /* just want the address */
4326     );
4327
4328   for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
4329     {
4330       if (auto_add_sw_if_indices[i] == sw_if_index)
4331         {
4332           if (is_del)
4333             {
4334               /* if have address remove it */
4335               if (first_int_addr)
4336                 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
4337               else
4338                 {
4339                   for (j = 0; j < vec_len (sm->to_resolve); j++)
4340                     {
4341                       rp = sm->to_resolve + j;
4342                       if (rp->sw_if_index == sw_if_index)
4343                         vec_add1 (indices_to_delete, j);
4344                     }
4345                   if (vec_len (indices_to_delete))
4346                     {
4347                       for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
4348                         vec_del1 (sm->to_resolve, j);
4349                       vec_free (indices_to_delete);
4350                     }
4351                 }
4352               if (twice_nat)
4353                 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
4354               else
4355                 vec_del1 (sm->auto_add_sw_if_indices, i);
4356             }
4357           else
4358             return VNET_API_ERROR_VALUE_EXIST;
4359
4360           return 0;
4361         }
4362     }
4363
4364   if (is_del)
4365     return VNET_API_ERROR_NO_SUCH_ENTRY;
4366
4367   /* add to the auto-address list */
4368   if (twice_nat)
4369     vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
4370   else
4371     vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
4372
4373   /* If the address is already bound - or static - add it now */
4374   if (first_int_addr)
4375     (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
4376
4377   return 0;
4378 }
4379
4380 int
4381 nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
4382                    snat_protocol_t proto, u32 vrf_id, int is_in)
4383 {
4384   snat_main_per_thread_data_t *tsm;
4385   clib_bihash_kv_8_8_t kv, value;
4386   ip4_header_t ip;
4387   u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4388   snat_session_key_t key;
4389   snat_session_t *s;
4390   clib_bihash_8_8_t *t;
4391
4392   if (sm->endpoint_dependent)
4393     return VNET_API_ERROR_UNSUPPORTED;
4394
4395   ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
4396   if (sm->num_workers > 1)
4397     tsm =
4398       vec_elt_at_index (sm->per_thread_data,
4399                         sm->worker_in2out_cb (&ip, fib_index, 0));
4400   else
4401     tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
4402
4403   key.addr.as_u32 = addr->as_u32;
4404   key.port = clib_host_to_net_u16 (port);
4405   key.protocol = proto;
4406   key.fib_index = fib_index;
4407   kv.key = key.as_u64;
4408   t = is_in ? &tsm->in2out : &tsm->out2in;
4409   if (!clib_bihash_search_8_8 (t, &kv, &value))
4410     {
4411       if (pool_is_free_index (tsm->sessions, value.value))
4412         return VNET_API_ERROR_UNSPECIFIED;
4413
4414       s = pool_elt_at_index (tsm->sessions, value.value);
4415       nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
4416       nat44_delete_session (sm, s, tsm - sm->per_thread_data);
4417       return 0;
4418     }
4419
4420   return VNET_API_ERROR_NO_SUCH_ENTRY;
4421 }
4422
4423 int
4424 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
4425                       ip4_address_t * eh_addr, u16 eh_port, u8 proto,
4426                       u32 vrf_id, int is_in)
4427 {
4428   ip4_header_t ip;
4429   clib_bihash_16_8_t *t;
4430   nat_ed_ses_key_t key;
4431   clib_bihash_kv_16_8_t kv, value;
4432   u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4433   snat_session_t *s;
4434   snat_main_per_thread_data_t *tsm;
4435
4436   if (!sm->endpoint_dependent)
4437     return VNET_API_ERROR_FEATURE_DISABLED;
4438
4439   ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
4440   if (sm->num_workers > 1)
4441     tsm =
4442       vec_elt_at_index (sm->per_thread_data,
4443                         sm->worker_in2out_cb (&ip, fib_index, 0));
4444   else
4445     tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
4446
4447   t = is_in ? &tsm->in2out_ed : &tsm->out2in_ed;
4448   key.l_addr.as_u32 = addr->as_u32;
4449   key.r_addr.as_u32 = eh_addr->as_u32;
4450   key.l_port = clib_host_to_net_u16 (port);
4451   key.r_port = clib_host_to_net_u16 (eh_port);
4452   key.proto = proto;
4453   key.fib_index = fib_index;
4454   kv.key[0] = key.as_u64[0];
4455   kv.key[1] = key.as_u64[1];
4456   if (clib_bihash_search_16_8 (t, &kv, &value))
4457     return VNET_API_ERROR_NO_SUCH_ENTRY;
4458
4459   if (pool_is_free_index (tsm->sessions, value.value))
4460     return VNET_API_ERROR_UNSPECIFIED;
4461   s = pool_elt_at_index (tsm->sessions, value.value);
4462   nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
4463   nat44_delete_session (sm, s, tsm - sm->per_thread_data);
4464   return 0;
4465 }
4466
4467 void
4468 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
4469 {
4470   snat_main_t *sm = &snat_main;
4471
4472   sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE;
4473   sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
4474   sm->psid = psid;
4475   sm->psid_offset = psid_offset;
4476   sm->psid_length = psid_length;
4477 }
4478
4479 void
4480 nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port)
4481 {
4482   snat_main_t *sm = &snat_main;
4483
4484   sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE;
4485   sm->alloc_addr_and_port = nat_alloc_addr_and_port_range;
4486   sm->start_port = start_port;
4487   sm->end_port = end_port;
4488 }
4489
4490 void
4491 nat_set_alloc_addr_and_port_default (void)
4492 {
4493   snat_main_t *sm = &snat_main;
4494
4495   sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
4496   sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
4497 }
4498
4499 VLIB_NODE_FN (nat_default_node) (vlib_main_t * vm,
4500                                  vlib_node_runtime_t * node,
4501                                  vlib_frame_t * frame)
4502 {
4503   return 0;
4504 }
4505
4506 /* *INDENT-OFF* */
4507 VLIB_REGISTER_NODE (nat_default_node) = {
4508   .name = "nat-default",
4509   .vector_size = sizeof (u32),
4510   .format_trace = 0,
4511   .type = VLIB_NODE_TYPE_INTERNAL,
4512   .n_errors = 0,
4513   .n_next_nodes = NAT_N_NEXT,
4514   .next_nodes = {
4515     [NAT_NEXT_DROP] = "error-drop",
4516     [NAT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
4517     [NAT_NEXT_IN2OUT_PRE] = "nat-pre-in2out",
4518     [NAT_NEXT_OUT2IN_PRE] = "nat-pre-out2in",
4519     [NAT_NEXT_IN2OUT_ED_FAST_PATH] = "nat44-ed-in2out",
4520     [NAT_NEXT_IN2OUT_ED_SLOW_PATH] = "nat44-ed-in2out-slowpath",
4521     [NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
4522     [NAT_NEXT_OUT2IN_ED_FAST_PATH] = "nat44-ed-out2in",
4523     [NAT_NEXT_OUT2IN_ED_SLOW_PATH] = "nat44-ed-out2in-slowpath",
4524   },
4525 };
4526 /* *INDENT-ON* */
4527
4528 /*
4529  * fd.io coding-style-patch-verification: ON
4530  *
4531  * Local Variables:
4532  * eval: (c-set-style "gnu")
4533  * End:
4534  */