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