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