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