nat: static mappings in flow hash
[vpp.git] / src / plugins / nat / nat44-ed / nat44_ed.c
1 /*
2  * snat.c - simple nat plugin
3  *
4  * Copyright (c) 2016 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vpp/app/version.h>
19
20 #include <vnet/vnet.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ip/ip4.h>
23 #include <vnet/ip/ip_table.h>
24 #include <vnet/ip/reass/ip4_sv_reass.h>
25 #include <vnet/fib/fib_table.h>
26 #include <vnet/fib/ip4_fib.h>
27 #include <vnet/plugin/plugin.h>
28 #include <vppinfra/bihash_16_8.h>
29
30 #include <nat/lib/log.h>
31 #include <nat/lib/nat_inlines.h>
32 #include <nat/lib/ipfix_logging.h>
33 #include <vnet/syslog/syslog.h>
34 #include <nat/lib/nat_syslog_constants.h>
35 #include <nat/lib/nat_syslog.h>
36
37 #include <nat/nat44-ed/nat44_ed.h>
38 #include <nat/nat44-ed/nat44_ed_affinity.h>
39 #include <nat/nat44-ed/nat44_ed_inlines.h>
40
41 #include <vpp/stats/stat_segment.h>
42
43 snat_main_t snat_main;
44
45 static_always_inline void nat_validate_interface_counters (snat_main_t *sm,
46                                                            u32 sw_if_index);
47
48 #define skip_if_disabled()                                                    \
49   do                                                                          \
50     {                                                                         \
51       snat_main_t *sm = &snat_main;                                           \
52       if (PREDICT_FALSE (!sm->enabled))                                       \
53         return;                                                               \
54     }                                                                         \
55   while (0)
56
57 #define fail_if_enabled()                                                     \
58   do                                                                          \
59     {                                                                         \
60       snat_main_t *sm = &snat_main;                                           \
61       if (PREDICT_FALSE (sm->enabled))                                        \
62         {                                                                     \
63           nat_log_err ("plugin enabled");                                     \
64           return 1;                                                           \
65         }                                                                     \
66     }                                                                         \
67   while (0)
68
69 #define fail_if_disabled()                                                    \
70   do                                                                          \
71     {                                                                         \
72       snat_main_t *sm = &snat_main;                                           \
73       if (PREDICT_FALSE (!sm->enabled))                                       \
74         {                                                                     \
75           nat_log_err ("plugin disabled");                                    \
76           return 1;                                                           \
77         }                                                                     \
78     }                                                                         \
79   while (0)
80
81 /* Hook up input features */
82 VNET_FEATURE_INIT (nat_pre_in2out, static) = {
83   .arc_name = "ip4-unicast",
84   .node_name = "nat-pre-in2out",
85   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
86                                "ip4-sv-reassembly-feature"),
87 };
88 VNET_FEATURE_INIT (nat_pre_out2in, static) = {
89   .arc_name = "ip4-unicast",
90   .node_name = "nat-pre-out2in",
91   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
92                                "ip4-dhcp-client-detect",
93                                "ip4-sv-reassembly-feature"),
94 };
95 VNET_FEATURE_INIT (snat_in2out_worker_handoff, static) = {
96   .arc_name = "ip4-unicast",
97   .node_name = "nat44-in2out-worker-handoff",
98   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
99 };
100 VNET_FEATURE_INIT (snat_out2in_worker_handoff, static) = {
101   .arc_name = "ip4-unicast",
102   .node_name = "nat44-out2in-worker-handoff",
103   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
104                                "ip4-dhcp-client-detect"),
105 };
106 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
107   .arc_name = "ip4-unicast",
108   .node_name = "nat44-in2out",
109   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
110 };
111 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
112   .arc_name = "ip4-unicast",
113   .node_name = "nat44-out2in",
114   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
115                                "ip4-dhcp-client-detect"),
116 };
117 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
118   .arc_name = "ip4-unicast",
119   .node_name = "nat44-ed-in2out",
120   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
121 };
122 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
123   .arc_name = "ip4-unicast",
124   .node_name = "nat44-ed-out2in",
125   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
126                                "ip4-dhcp-client-detect"),
127 };
128 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
129   .arc_name = "ip4-unicast",
130   .node_name = "nat44-ed-classify",
131   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
132 };
133 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
134   .arc_name = "ip4-unicast",
135   .node_name = "nat44-handoff-classify",
136   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
137 };
138 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
139   .arc_name = "ip4-unicast",
140   .node_name = "nat44-in2out-fast",
141   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
142 };
143 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
144   .arc_name = "ip4-unicast",
145   .node_name = "nat44-out2in-fast",
146   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
147                                "ip4-dhcp-client-detect"),
148 };
149
150 /* Hook up output features */
151 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
152   .arc_name = "ip4-output",
153   .node_name = "nat44-in2out-output",
154   .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
155   .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
156 };
157 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
158   .arc_name = "ip4-output",
159   .node_name = "nat44-in2out-output-worker-handoff",
160   .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
161   .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
162 };
163 VNET_FEATURE_INIT (nat_pre_in2out_output, static) = {
164   .arc_name = "ip4-output",
165   .node_name = "nat-pre-in2out-output",
166   .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
167   .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
168 };
169 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
170   .arc_name = "ip4-output",
171   .node_name = "nat44-ed-in2out-output",
172   .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
173   .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
174 };
175
176 VLIB_PLUGIN_REGISTER () = {
177     .version = VPP_BUILD_VER,
178     .description = "Network Address Translation (NAT)",
179 };
180
181 static void nat44_ed_db_init (u32 translations, u32 translation_buckets);
182
183 static void nat44_ed_db_free ();
184
185 u32 nat_calc_bihash_buckets (u32 n_elts);
186
187 u8 *
188 format_ed_session_kvp (u8 * s, va_list * args)
189 {
190   clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
191
192   u8 proto;
193   u16 r_port, l_port;
194   ip4_address_t l_addr, r_addr;
195   u32 fib_index;
196
197   split_ed_kv (v, &l_addr, &r_addr, &proto, &fib_index, &l_port, &r_port);
198   s = format (s,
199               "local %U:%d remote %U:%d proto %U fib %d thread-index %u "
200               "session-index %u",
201               format_ip4_address, &l_addr, clib_net_to_host_u16 (l_port),
202               format_ip4_address, &r_addr, clib_net_to_host_u16 (r_port),
203               format_ip_protocol, proto, fib_index,
204               ed_value_get_thread_index (v), ed_value_get_session_index (v));
205
206   return s;
207 }
208
209 static_always_inline int
210 nat44_ed_sm_i2o_add (snat_main_t *sm, snat_static_mapping_t *m,
211                      ip4_address_t addr, u16 port, u32 fib_index, u8 proto)
212 {
213   ASSERT (!pool_is_free (sm->static_mappings, m));
214   clib_bihash_kv_16_8_t kv;
215   nat44_ed_sm_init_i2o_kv (&kv, addr.as_u32, port, fib_index, proto,
216                            m - sm->static_mappings);
217   return clib_bihash_add_del_16_8 (&sm->flow_hash, &kv, 1 /*is_add*/);
218 }
219
220 static_always_inline int
221 nat44_ed_sm_i2o_del (snat_main_t *sm, ip4_address_t addr, u16 port,
222                      u32 fib_index, u8 proto)
223 {
224   clib_bihash_kv_16_8_t kv;
225   nat44_ed_sm_init_i2o_k (&kv, addr.as_u32, port, fib_index, proto);
226   return clib_bihash_add_del_16_8 (&sm->flow_hash, &kv, 0 /*is_add*/);
227 }
228
229 static_always_inline int
230 nat44_ed_sm_o2i_add (snat_main_t *sm, snat_static_mapping_t *m,
231                      ip4_address_t addr, u16 port, u32 fib_index, u8 proto)
232 {
233   ASSERT (!pool_is_free (sm->static_mappings, m));
234   clib_bihash_kv_16_8_t kv;
235   nat44_ed_sm_init_o2i_kv (&kv, addr.as_u32, port, fib_index, proto,
236                            m - sm->static_mappings);
237   return clib_bihash_add_del_16_8 (&sm->flow_hash, &kv, 1 /*is_add*/);
238 }
239
240 static_always_inline int
241 nat44_ed_sm_o2i_del (snat_main_t *sm, ip4_address_t addr, u16 port,
242                      u32 fib_index, u8 proto)
243 {
244   clib_bihash_kv_16_8_t kv;
245   nat44_ed_sm_init_o2i_k (&kv, addr.as_u32, port, fib_index, proto);
246   return clib_bihash_add_del_16_8 (&sm->flow_hash, &kv, 0 /*is_add*/);
247 }
248
249 void
250 nat44_ed_free_session_data (snat_main_t *sm, snat_session_t *s,
251                             u32 thread_index, u8 is_ha)
252 {
253   per_vrf_sessions_unregister_session (s, thread_index);
254
255   if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 0))
256     nat_elog_warn (sm, "flow hash del failed");
257
258   if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 0))
259     nat_elog_warn (sm, "flow hash del failed");
260
261   if (na44_ed_is_fwd_bypass_session (s))
262     {
263       return;
264     }
265
266   if (nat44_ed_is_affinity_session (s))
267     nat_affinity_unlock (s->ext_host_addr, s->out2in.addr, s->proto,
268                          s->out2in.port);
269
270   if (!is_ha)
271     nat_syslog_nat44_sdel (0, s->in2out.fib_index, &s->in2out.addr,
272                            s->in2out.port, &s->ext_host_nat_addr,
273                            s->ext_host_nat_port, &s->out2in.addr,
274                            s->out2in.port, &s->ext_host_addr, s->ext_host_port,
275                            s->proto, nat44_ed_is_twice_nat_session (s));
276
277   if (!is_ha)
278     {
279       /* log NAT event */
280       nat_ipfix_logging_nat44_ses_delete (
281         thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32, s->proto,
282         s->in2out.port, s->out2in.port, s->in2out.fib_index);
283     }
284 }
285
286 static int
287 is_snat_address_used_in_static_mapping (snat_main_t *sm, ip4_address_t addr)
288 {
289   snat_static_mapping_t *m;
290   pool_foreach (m, sm->static_mappings)
291     {
292       if (is_sm_addr_only (m->flags) || is_sm_out2in_only (m->flags) ||
293           is_sm_identity_nat (m->flags))
294         {
295           continue;
296         }
297       if (m->external_addr.as_u32 == addr.as_u32)
298         {
299           return 1;
300         }
301     }
302   return 0;
303 }
304
305 static void
306 nat44_ed_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len, u32 sw_if_index,
307                               int is_add)
308 {
309   // Add the external NAT address to the FIB as receive entries. This ensures
310   // that VPP will reply to ARP for this address and we don't need to enable
311   // proxy ARP on the outside interface.
312
313   snat_main_t *sm = &snat_main;
314   fib_prefix_t prefix = {
315     .fp_len = p_len,
316     .fp_proto = FIB_PROTOCOL_IP4,
317     .fp_addr = {
318                 .ip4.as_u32 = addr->as_u32,
319                 },
320   };
321   u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
322
323   if (is_add)
324     {
325       fib_table_entry_update_one_path (fib_index, &prefix, sm->fib_src_low,
326                                        (FIB_ENTRY_FLAG_CONNECTED |
327                                         FIB_ENTRY_FLAG_LOCAL |
328                                         FIB_ENTRY_FLAG_EXCLUSIVE),
329                                        DPO_PROTO_IP4, NULL, sw_if_index, ~0, 1,
330                                        NULL, FIB_ROUTE_PATH_FLAG_NONE);
331     }
332   else
333     {
334       fib_table_entry_delete (fib_index, &prefix, sm->fib_src_low);
335     }
336 }
337
338 static void
339 nat44_ed_add_del_addr_to_fib_foreach_out_if (ip4_address_t *addr, u8 is_add)
340 {
341   snat_main_t *sm = &snat_main;
342   snat_interface_t *i;
343
344   pool_foreach (i, sm->interfaces)
345     {
346       if (nat44_ed_is_interface_outside (i))
347         {
348           nat44_ed_add_del_addr_to_fib (addr, 32, i->sw_if_index, is_add);
349         }
350     }
351   pool_foreach (i, sm->output_feature_interfaces)
352     {
353       if (nat44_ed_is_interface_outside (i))
354         {
355           nat44_ed_add_del_addr_to_fib (addr, 32, i->sw_if_index, is_add);
356         }
357     }
358 }
359
360 static_always_inline void
361 nat44_ed_add_del_addr_to_fib_foreach_addr (u32 sw_if_index, u8 is_add)
362 {
363   snat_main_t *sm = &snat_main;
364   snat_address_t *ap;
365
366   vec_foreach (ap, sm->addresses)
367     {
368       nat44_ed_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, is_add);
369     }
370 }
371
372 static_always_inline void
373 nat44_ed_add_del_addr_to_fib_foreach_addr_only_sm (u32 sw_if_index, u8 is_add)
374 {
375   snat_main_t *sm = &snat_main;
376   snat_static_mapping_t *m;
377
378   pool_foreach (m, sm->static_mappings)
379     {
380       if (is_sm_addr_only (m->flags) && !is_sm_identity_nat (m->flags))
381         {
382           nat44_ed_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index,
383                                         is_add);
384         }
385     }
386 }
387
388 int
389 nat44_ed_add_address (ip4_address_t *addr, u32 vrf_id, u8 twice_nat)
390 {
391   snat_main_t *sm = &snat_main;
392   snat_address_t *ap, *addresses;
393
394   addresses = twice_nat ? sm->twice_nat_addresses : sm->addresses;
395
396   if (!sm->enabled)
397     {
398       return VNET_API_ERROR_UNSUPPORTED;
399     }
400
401   // check if address already exists
402   vec_foreach (ap, addresses)
403     {
404       if (ap->addr.as_u32 == addr->as_u32)
405         {
406           nat_log_err ("address exist");
407           return VNET_API_ERROR_VALUE_EXIST;
408         }
409     }
410
411   if (twice_nat)
412     {
413       vec_add2 (sm->twice_nat_addresses, ap, 1);
414     }
415   else
416     {
417       vec_add2 (sm->addresses, ap, 1);
418     }
419
420   ap->fib_index = ~0;
421   ap->addr = *addr;
422
423   if (vrf_id != ~0)
424     {
425       ap->fib_index = fib_table_find_or_create_and_lock (
426         FIB_PROTOCOL_IP4, vrf_id, sm->fib_src_low);
427     }
428
429     if (!twice_nat)
430   {
431     // if we don't have enabled interface we don't add address
432     // to fib
433     nat44_ed_add_del_addr_to_fib_foreach_out_if (addr, 1);
434   }
435   return 0;
436 }
437
438 int
439 nat44_ed_del_address (ip4_address_t addr, u8 delete_sm, u8 twice_nat)
440 {
441   snat_main_t *sm = &snat_main;
442   snat_address_t *a = 0, *addresses;
443   snat_session_t *ses;
444   u32 *ses_to_be_removed = 0, *ses_index;
445   snat_main_per_thread_data_t *tsm;
446   snat_static_mapping_t *m;
447   int j;
448
449   addresses = twice_nat ? sm->twice_nat_addresses : sm->addresses;
450
451   for (j = 0; j < vec_len (addresses); j++)
452     {
453       if (addresses[j].addr.as_u32 == addr.as_u32)
454         {
455           a = addresses + j;
456           break;
457         }
458     }
459   if (!a)
460     {
461       nat_log_err ("no such address");
462       return VNET_API_ERROR_NO_SUCH_ENTRY;
463     }
464
465   if (delete_sm)
466     {
467       pool_foreach (m, sm->static_mappings)
468         {
469           if (m->external_addr.as_u32 == addr.as_u32)
470             {
471               nat44_ed_del_static_mapping (m->local_addr, m->external_addr,
472                                            m->local_port, m->external_port,
473                                            ip_proto_to_nat_proto (m->proto),
474                                            m->vrf_id, ~0, m->flags);
475             }
476         }
477     }
478   else
479     {
480       // TODO: why ?
481       // check if address is used in some static mapping
482       if (is_snat_address_used_in_static_mapping (sm, addr))
483         {
484           nat_log_err ("address used in static mapping");
485           return VNET_API_ERROR_UNSPECIFIED;
486         }
487     }
488
489   if (a->fib_index != ~0)
490     {
491       fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
492     }
493
494   // delete sessions using address
495   vec_foreach (tsm, sm->per_thread_data)
496     {
497       pool_foreach (ses, tsm->sessions)
498         {
499           if (ses->out2in.addr.as_u32 == addr.as_u32)
500             {
501               nat44_ed_free_session_data (sm, ses, tsm - sm->per_thread_data,
502                                           0);
503               vec_add1 (ses_to_be_removed, ses - tsm->sessions);
504             }
505         }
506       vec_foreach (ses_index, ses_to_be_removed)
507         {
508           ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
509           nat_ed_session_delete (sm, ses, tsm - sm->per_thread_data, 1);
510         }
511       vec_free (ses_to_be_removed);
512     }
513
514     if (!twice_nat)
515   {
516     vec_del1 (sm->addresses, j);
517     nat44_ed_add_del_addr_to_fib_foreach_out_if (&addr, 0);
518   }
519   else { vec_del1 (sm->twice_nat_addresses, j); }
520   return 0;
521 }
522
523 u32
524 get_thread_idx_by_port (u16 e_port)
525 {
526   snat_main_t *sm = &snat_main;
527   u32 thread_idx = sm->num_workers;
528   if (sm->num_workers > 1)
529     {
530       thread_idx =
531         sm->first_worker_index +
532         sm->workers[(e_port - 1024) / sm->port_per_thread];
533     }
534   return thread_idx;
535 }
536
537 void
538 nat_ed_static_mapping_del_sessions (snat_main_t * sm,
539                                     snat_main_per_thread_data_t * tsm,
540                                     ip4_address_t l_addr,
541                                     u16 l_port,
542                                     u8 protocol,
543                                     u32 fib_index, int addr_only,
544                                     ip4_address_t e_addr, u16 e_port)
545 {
546   snat_session_t *s;
547   u32 *indexes_to_free = NULL;
548   pool_foreach (s, tsm->sessions) {
549     if (s->in2out.fib_index != fib_index ||
550         s->in2out.addr.as_u32 != l_addr.as_u32)
551       {
552         continue;
553       }
554     if (!addr_only)
555       {
556         if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
557             s->out2in.port != e_port || s->in2out.port != l_port ||
558             s->proto != protocol)
559           continue;
560       }
561
562     if (nat44_ed_is_lb_session (s))
563       continue;
564     if (!nat44_ed_is_session_static (s))
565       continue;
566     nat44_ed_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
567     vec_add1 (indexes_to_free, s - tsm->sessions);
568     if (!addr_only)
569       break;
570   }
571   u32 *ses_index;
572   vec_foreach (ses_index, indexes_to_free)
573   {
574     s = pool_elt_at_index (tsm->sessions, *ses_index);
575     nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
576   }
577   vec_free (indexes_to_free);
578 }
579
580 static_always_inline snat_static_mapping_t *
581 nat44_ed_sm_lookup (snat_main_t *sm, clib_bihash_kv_16_8_t *kv)
582 {
583   clib_bihash_kv_16_8_t v;
584   int rc = clib_bihash_search_16_8 (&sm->flow_hash, kv, &v);
585   if (!rc)
586     {
587       ASSERT (0 == ed_value_get_thread_index (&v));
588       return pool_elt_at_index (sm->static_mappings,
589                                 ed_value_get_session_index (&v));
590     }
591   return NULL;
592 }
593
594 snat_static_mapping_t *
595 nat44_ed_sm_o2i_lookup (snat_main_t *sm, ip4_address_t addr, u16 port,
596                         u32 fib_index, u8 proto)
597 {
598   clib_bihash_kv_16_8_t kv;
599   nat44_ed_sm_init_o2i_k (&kv, addr.as_u32, port, fib_index, proto);
600   return nat44_ed_sm_lookup (sm, &kv);
601 }
602
603 snat_static_mapping_t *
604 nat44_ed_sm_i2o_lookup (snat_main_t *sm, ip4_address_t addr, u16 port,
605                         u32 fib_index, u8 proto)
606 {
607   clib_bihash_kv_16_8_t kv;
608   nat44_ed_sm_init_i2o_k (&kv, addr.as_u32, port, fib_index, proto);
609   return nat44_ed_sm_lookup (sm, &kv);
610 }
611
612 void
613 nat44_ed_add_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
614                              ip_protocol_t proto, u32 vrf_id, u32 sw_if_index,
615                              u32 flags, ip4_address_t pool_addr, u8 *tag)
616 {
617   snat_static_map_resolve_t *rp;
618   snat_main_t *sm = &snat_main;
619
620   vec_add2 (sm->to_resolve, rp, 1);
621   rp->l_addr.as_u32 = l_addr.as_u32;
622   rp->l_port = l_port;
623   rp->e_port = e_port;
624   rp->sw_if_index = sw_if_index;
625   rp->vrf_id = vrf_id;
626   rp->proto = proto;
627   rp->flags = flags;
628   rp->pool_addr = pool_addr;
629   rp->tag = vec_dup (tag);
630 }
631
632 int
633 nat44_ed_get_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
634                              ip_protocol_t proto, u32 vrf_id, u32 sw_if_index,
635                              u32 flags, int *out)
636 {
637   snat_static_map_resolve_t *rp;
638   snat_main_t *sm = &snat_main;
639   int i;
640
641   for (i = 0; i < vec_len (sm->to_resolve); i++)
642     {
643       rp = sm->to_resolve + i;
644
645       if (rp->sw_if_index == sw_if_index && rp->vrf_id == vrf_id)
646         {
647           if (is_sm_identity_nat (rp->flags) && is_sm_identity_nat (flags))
648             {
649               if (!(is_sm_addr_only (rp->flags) && is_sm_addr_only (flags)))
650                 {
651                   if (rp->e_port != e_port || rp->proto != proto)
652                     {
653                       continue;
654                     }
655                 }
656             }
657           else if (rp->l_addr.as_u32 == l_addr.as_u32)
658             {
659               if (!(is_sm_addr_only (rp->flags) && is_sm_addr_only (flags)))
660                 {
661                   if (rp->l_port != l_port || rp->e_port != e_port ||
662                       rp->proto != proto)
663                     {
664                       continue;
665                     }
666                 }
667             }
668           else
669             {
670               continue;
671             }
672           if (out)
673             {
674               *out = i;
675             }
676           return 0;
677         }
678     }
679   return 1;
680 }
681
682 int
683 nat44_ed_del_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
684                              ip_protocol_t proto, u32 vrf_id, u32 sw_if_index,
685                              u32 flags)
686 {
687   snat_main_t *sm = &snat_main;
688   int i;
689   if (!nat44_ed_get_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
690                                     sw_if_index, flags, &i))
691     {
692       vec_del1 (sm->to_resolve, i);
693       return 0;
694     }
695   return 1;
696 }
697
698 static_always_inline int
699 nat44_ed_validate_sm_input (u32 flags)
700 {
701   // identity nat can be initiated only from inside interface
702   if (is_sm_identity_nat (flags) && is_sm_out2in_only (flags))
703     {
704       return VNET_API_ERROR_UNSUPPORTED;
705     }
706
707   if (is_sm_twice_nat (flags) || is_sm_self_twice_nat (flags))
708     {
709       if (is_sm_addr_only (flags) || is_sm_identity_nat (flags))
710         {
711           return VNET_API_ERROR_UNSUPPORTED;
712         }
713     }
714   return 0;
715 }
716
717 snat_address_t *
718 nat44_ed_addr_lookup (snat_main_t *sm, u32 addr)
719 {
720   for (int i = 0; i < vec_len (sm->addresses); ++i)
721     {
722       if (sm->addresses[i].addr.as_u32 == addr)
723         return &sm->addresses[i];
724     }
725   return NULL;
726 }
727
728 int
729 nat44_ed_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
730                              u16 l_port, u16 e_port, ip_protocol_t proto,
731                              u32 vrf_id, u32 sw_if_index, u32 flags,
732                              ip4_address_t pool_addr, u8 *tag)
733 {
734   snat_main_t *sm = &snat_main;
735   nat44_lb_addr_port_t *local;
736   snat_static_mapping_t *m;
737   u32 fib_index = ~0;
738   int rv;
739
740   if (!sm->enabled)
741     {
742       return VNET_API_ERROR_UNSUPPORTED;
743     }
744
745   rv = nat44_ed_validate_sm_input (flags);
746   if (rv != 0)
747     {
748       return rv;
749     }
750
751   if (is_sm_addr_only (flags))
752     {
753       e_port = l_port = proto = 0;
754     }
755
756   if (is_sm_switch_address (flags))
757     {
758       // this mapping is interface bound
759       ip4_address_t *first_int_addr;
760
761       // check if this record isn't registered for resolve
762       if (!nat44_ed_get_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
763                                         sw_if_index, flags, 0))
764         {
765           return VNET_API_ERROR_VALUE_EXIST;
766         }
767       // register record for resolve
768       nat44_ed_add_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
769                                    sw_if_index, flags, pool_addr, tag);
770
771       first_int_addr =
772         ip4_interface_first_address (sm->ip4_main, sw_if_index, 0);
773       if (!first_int_addr)
774         {
775           // dhcp resolution required
776           return 0;
777         }
778
779       e_addr.as_u32 = first_int_addr->as_u32;
780     }
781
782   if (is_sm_identity_nat (flags))
783     {
784       l_port = e_port;
785       l_addr.as_u32 = e_addr.as_u32;
786     }
787
788   m = nat44_ed_sm_o2i_lookup (sm, e_addr, e_port, 0, proto);
789   if (m)
790     {
791       if (!is_sm_identity_nat (m->flags))
792         {
793           return VNET_API_ERROR_VALUE_EXIST;
794         }
795
796       // case:
797       // adding local identity nat record for different vrf table
798       pool_foreach (local, m->locals)
799         {
800           if (local->vrf_id == vrf_id)
801             {
802               return VNET_API_ERROR_VALUE_EXIST;
803             }
804         }
805
806       pool_get (m->locals, local);
807
808       local->vrf_id = vrf_id;
809       local->fib_index = fib_table_find_or_create_and_lock (
810         FIB_PROTOCOL_IP4, vrf_id, sm->fib_src_low);
811
812       nat44_ed_sm_i2o_add (sm, m, m->local_addr, m->local_port,
813                            local->fib_index, m->proto);
814
815       return 0;
816     }
817
818   if (vrf_id != ~0)
819     {
820       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
821                                                      sm->fib_src_low);
822     }
823   else
824     {
825       // fallback to default vrf
826       vrf_id = sm->inside_vrf_id;
827       fib_index = sm->inside_fib_index;
828       fib_table_lock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
829     }
830
831   // test if local mapping record doesn't exist
832   // identity nat supports multiple records in local mapping
833   if (!(is_sm_out2in_only (flags) || is_sm_identity_nat (flags)))
834     {
835       if (nat44_ed_sm_i2o_lookup (sm, l_addr, l_port, fib_index, proto))
836         {
837           return VNET_API_ERROR_VALUE_EXIST;
838         }
839     }
840
841   if (!(is_sm_out2in_only (flags) || is_sm_addr_only (flags) ||
842         sm->static_mapping_only))
843     {
844       if (!nat44_ed_addr_lookup (sm, e_addr.as_u32))
845         {
846           // remove resolve record
847           if (is_sm_switch_address (flags) && !is_sm_identity_nat (flags))
848             {
849               nat44_ed_del_resolve_record (l_addr, l_port, e_port, proto,
850                                            vrf_id, sw_if_index, flags);
851             }
852           return VNET_API_ERROR_NO_SUCH_ENTRY;
853         }
854     }
855
856   pool_get (sm->static_mappings, m);
857   clib_memset (m, 0, sizeof (*m));
858
859   m->flags = flags;
860   m->local_addr = l_addr;
861   m->external_addr = e_addr;
862
863   m->tag = vec_dup (tag);
864
865   if (is_sm_exact_address (flags) && is_sm_twice_nat (flags))
866     {
867       m->pool_addr = pool_addr;
868     }
869
870   if (!is_sm_addr_only (flags))
871     {
872       m->local_port = l_port;
873       m->external_port = e_port;
874       m->proto = proto;
875     }
876
877   if (is_sm_identity_nat (flags))
878     {
879       pool_get (m->locals, local);
880
881       local->vrf_id = vrf_id;
882       local->fib_index = fib_index;
883     }
884   else
885     {
886       m->vrf_id = vrf_id;
887       m->fib_index = fib_index;
888
889       // not identity && addr only
890       if (is_sm_addr_only (flags))
891         {
892           nat44_ed_add_del_addr_to_fib_foreach_out_if (&e_addr, 1);
893         }
894     }
895
896   if (!is_sm_out2in_only (flags))
897     {
898       nat44_ed_sm_i2o_add (sm, m, m->local_addr, m->local_port, fib_index,
899                            m->proto);
900     }
901
902   nat44_ed_sm_o2i_add (sm, m, m->external_addr, m->external_port, 0, m->proto);
903
904   if (sm->num_workers > 1)
905     {
906       // store worker index for this record
907       ip4_header_t ip = {
908         .src_address = m->local_addr,
909       };
910       u32 worker_index;
911       worker_index =
912         nat44_ed_get_in2out_worker_index (0, &ip, m->fib_index, 0);
913       vec_add1 (m->workers, worker_index);
914     }
915
916   return 0;
917 }
918
919 int
920 nat44_ed_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
921                              u16 l_port, u16 e_port, ip_protocol_t proto,
922                              u32 vrf_id, u32 sw_if_index, u32 flags)
923 {
924   snat_main_per_thread_data_t *tsm;
925   snat_main_t *sm = &snat_main;
926
927   nat44_lb_addr_port_t *local;
928   snat_static_mapping_t *m;
929   u32 fib_index = ~0;
930   int rv;
931
932   if (!sm->enabled)
933     {
934       return VNET_API_ERROR_UNSUPPORTED;
935     }
936
937   rv = nat44_ed_validate_sm_input (flags);
938   if (rv != 0)
939     {
940       return rv;
941     }
942
943   if (is_sm_addr_only (flags))
944     {
945       e_port = l_port = proto = 0;
946     }
947
948   if (is_sm_switch_address (flags))
949     {
950       // this mapping is interface bound
951       ip4_address_t *first_int_addr;
952
953       // delete record registered for resolve
954       if (nat44_ed_del_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
955                                        sw_if_index, flags))
956         {
957           return VNET_API_ERROR_NO_SUCH_ENTRY;
958         }
959
960       first_int_addr =
961         ip4_interface_first_address (sm->ip4_main, sw_if_index, 0);
962       if (!first_int_addr)
963         {
964           // dhcp resolution required
965           return 0;
966         }
967
968       e_addr.as_u32 = first_int_addr->as_u32;
969     }
970
971   if (is_sm_identity_nat (flags))
972     {
973       l_port = e_port;
974       l_addr.as_u32 = e_addr.as_u32;
975     }
976
977   // fib index 0
978   m = nat44_ed_sm_o2i_lookup (sm, e_addr, e_port, 0, proto);
979
980   if (!m)
981     {
982       if (is_sm_switch_address (flags))
983         {
984           return 0;
985         }
986       return VNET_API_ERROR_NO_SUCH_ENTRY;
987     }
988
989   if (is_sm_identity_nat (flags))
990     {
991       u8 failure = 1;
992
993       if (!is_sm_switch_address (flags))
994         {
995           vrf_id = sm->inside_vrf_id;
996         }
997
998       pool_foreach (local, m->locals)
999         {
1000           if (local->vrf_id == vrf_id)
1001             {
1002               local = pool_elt_at_index (m->locals, local - m->locals);
1003               fib_index = local->fib_index;
1004               pool_put (m->locals, local);
1005               failure = 0;
1006             }
1007         }
1008
1009       if (failure)
1010         {
1011           return VNET_API_ERROR_NO_SUCH_ENTRY;
1012         }
1013     }
1014   else
1015     {
1016       fib_index = m->fib_index;
1017     }
1018
1019   if (!(is_sm_out2in_only (flags) || is_sm_addr_only (flags) ||
1020         sm->static_mapping_only))
1021     {
1022       if (!nat44_ed_addr_lookup (sm, e_addr.as_u32))
1023         {
1024           return VNET_API_ERROR_INVALID_VALUE;
1025         }
1026     }
1027
1028   if (!is_sm_out2in_only (flags))
1029     {
1030       nat44_ed_sm_i2o_del (sm, l_addr, l_port, fib_index, proto);
1031     }
1032
1033   if (!sm->static_mapping_only || sm->static_mapping_connection_tracking)
1034     {
1035       // delete sessions for static mapping
1036       if (sm->num_workers > 1)
1037         tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1038       else
1039         tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1040
1041       nat_ed_static_mapping_del_sessions (
1042         sm, tsm, m->local_addr, m->local_port, m->proto, fib_index,
1043         is_sm_addr_only (flags), e_addr, e_port);
1044     }
1045
1046   fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
1047
1048   if (!pool_elts (m->locals))
1049     {
1050       // this is last record remove all required stuff
1051       // fib_index 0
1052       nat44_ed_sm_o2i_del (sm, e_addr, e_port, 0, proto);
1053
1054       vec_free (m->tag);
1055       vec_free (m->workers);
1056       pool_put (sm->static_mappings, m);
1057
1058       if (is_sm_addr_only (flags) && !is_sm_identity_nat (flags))
1059         {
1060           nat44_ed_add_del_addr_to_fib_foreach_out_if (&e_addr, 0);
1061         }
1062     }
1063
1064   return 0;
1065 }
1066
1067 int
1068 nat44_ed_add_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1069                                 ip_protocol_t proto,
1070                                 nat44_lb_addr_port_t *locals, u32 flags,
1071                                 u8 *tag, u32 affinity)
1072 {
1073   snat_main_t *sm = &snat_main;
1074   snat_static_mapping_t *m;
1075   snat_address_t *a = 0;
1076
1077   nat44_lb_addr_port_t *local;
1078   uword *bitmap = 0;
1079   int rc = 0;
1080
1081   int i;
1082
1083   if (!sm->enabled)
1084     {
1085       return VNET_API_ERROR_UNSUPPORTED;
1086     }
1087
1088   m = nat44_ed_sm_o2i_lookup (sm, e_addr, e_port, 0, proto);
1089
1090   if (m)
1091     {
1092       return VNET_API_ERROR_VALUE_EXIST;
1093     }
1094
1095   if (vec_len (locals) < 2)
1096     {
1097       return VNET_API_ERROR_INVALID_VALUE;
1098     }
1099
1100   if (!(sm->static_mapping_only || is_sm_out2in_only (flags)))
1101     {
1102       /* Find external address in allocated addresses and reserve port for
1103          address and port pair mapping when dynamic translations enabled */
1104       for (i = 0; i < vec_len (sm->addresses); i++)
1105         {
1106           if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1107             {
1108               /* External port must be unused */
1109               a = sm->addresses + i;
1110               if (nat44_ed_sm_o2i_lookup (sm, a->addr, e_port, 0, proto))
1111                 {
1112                   return VNET_API_ERROR_VALUE_EXIST;
1113                 }
1114               break;
1115             }
1116         }
1117       // external address must be allocated
1118       if (!a)
1119         {
1120           return VNET_API_ERROR_NO_SUCH_ENTRY;
1121         }
1122     }
1123
1124   pool_get (sm->static_mappings, m);
1125   clib_memset (m, 0, sizeof (*m));
1126   m->tag = vec_dup (tag);
1127   m->external_addr = e_addr;
1128   m->external_port = e_port;
1129   m->affinity = affinity;
1130   m->proto = proto;
1131
1132   m->flags = flags;
1133   m->flags |= NAT_SM_FLAG_LB;
1134
1135   if (affinity)
1136     m->affinity_per_service_list_head_index =
1137       nat_affinity_get_per_service_list_head_index ();
1138   else
1139     m->affinity_per_service_list_head_index = ~0;
1140
1141   if (nat44_ed_sm_o2i_add (sm, m, m->external_addr, m->external_port, 0,
1142                            m->proto))
1143     {
1144       nat_elog_err (sm, "sm o2i key add failed");
1145       return VNET_API_ERROR_UNSPECIFIED;
1146     }
1147
1148   for (i = 0; i < vec_len (locals); i++)
1149     {
1150       locals[i].fib_index = fib_table_find_or_create_and_lock (
1151         FIB_PROTOCOL_IP4, locals[i].vrf_id, sm->fib_src_low);
1152       if (!is_sm_out2in_only (flags))
1153         {
1154           if (nat44_ed_sm_o2i_add (sm, m, e_addr, e_port, 0, proto))
1155             {
1156               nat_elog_err (sm, "sm o2i key add failed");
1157               rc = VNET_API_ERROR_UNSPECIFIED;
1158               // here we continue with add operation so that it can be safely
1159               // reversed in delete path - otherwise we'd have to track what
1160               // we've done and deal with partial cleanups and since bihash
1161               // adds are (extremely improbable) the only points of failure,
1162               // it's easier to just do it this way
1163             }
1164         }
1165       locals[i].prefix = (i == 0) ?
1166                            locals[i].probability :
1167                            (locals[i - 1].prefix + locals[i].probability);
1168       pool_get (m->locals, local);
1169       *local = locals[i];
1170       if (sm->num_workers > 1)
1171         {
1172           ip4_header_t ip = {
1173             .src_address = locals[i].addr,
1174           };
1175           bitmap = clib_bitmap_set (
1176             bitmap, nat44_ed_get_in2out_worker_index (0, &ip, m->fib_index, 0),
1177             1);
1178         }
1179     }
1180
1181   /* Assign workers */
1182   if (sm->num_workers > 1)
1183     {
1184       clib_bitmap_foreach (i, bitmap)
1185         {
1186           vec_add1 (m->workers, i);
1187         }
1188     }
1189
1190   return rc;
1191 }
1192
1193 int
1194 nat44_ed_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1195                                 ip_protocol_t proto, u32 flags)
1196 {
1197   snat_main_t *sm = &snat_main;
1198   snat_static_mapping_t *m;
1199
1200   nat44_lb_addr_port_t *local;
1201   snat_main_per_thread_data_t *tsm;
1202   snat_session_t *s;
1203
1204   if (!sm->enabled)
1205     {
1206       return VNET_API_ERROR_UNSUPPORTED;
1207     }
1208
1209   m = nat44_ed_sm_o2i_lookup (sm, e_addr, e_port, 0, proto);
1210   if (!m)
1211     return VNET_API_ERROR_NO_SUCH_ENTRY;
1212
1213   if (!is_sm_lb (m->flags))
1214     return VNET_API_ERROR_INVALID_VALUE;
1215
1216   if (nat44_ed_sm_o2i_del (sm, m->external_addr, m->external_port, 0,
1217                            m->proto))
1218     {
1219       nat_elog_err (sm, "sm o2i key del failed");
1220       return VNET_API_ERROR_UNSPECIFIED;
1221     }
1222
1223   pool_foreach (local, m->locals)
1224     {
1225       fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
1226       if (!is_sm_out2in_only (flags))
1227         {
1228           if (nat44_ed_sm_i2o_del (sm, local->addr, local->port,
1229                                    local->fib_index, m->proto))
1230             {
1231               nat_elog_err (sm, "sm i2o key del failed");
1232               return VNET_API_ERROR_UNSPECIFIED;
1233             }
1234         }
1235
1236       if (sm->num_workers > 1)
1237         {
1238           ip4_header_t ip = {
1239             .src_address = local->addr,
1240           };
1241           tsm = vec_elt_at_index (
1242             sm->per_thread_data,
1243             nat44_ed_get_in2out_worker_index (0, &ip, m->fib_index, 0));
1244         }
1245       else
1246         tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1247
1248       /* Delete sessions */
1249       pool_foreach (s, tsm->sessions)
1250         {
1251           if (!(nat44_ed_is_lb_session (s)))
1252             continue;
1253
1254           if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1255               s->in2out.port != local->port)
1256             continue;
1257
1258           nat44_ed_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1259           nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1260         }
1261     }
1262
1263   if (m->affinity)
1264     {
1265       nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1266     }
1267
1268   pool_free (m->locals);
1269   vec_free (m->tag);
1270   vec_free (m->workers);
1271   pool_put (sm->static_mappings, m);
1272
1273   return 0;
1274 }
1275
1276 int
1277 nat44_ed_add_del_lb_static_mapping_local (ip4_address_t e_addr, u16 e_port,
1278                                           ip4_address_t l_addr, u16 l_port,
1279                                           ip_protocol_t proto, u32 vrf_id,
1280                                           u8 probability, u8 is_add)
1281 {
1282   snat_main_t *sm = &snat_main;
1283   snat_static_mapping_t *m = 0;
1284   nat44_lb_addr_port_t *local, *prev_local, *match_local = 0;
1285   snat_main_per_thread_data_t *tsm;
1286   snat_session_t *s;
1287   u32 *locals = 0;
1288   uword *bitmap = 0;
1289   int i;
1290
1291   if (!sm->enabled)
1292     {
1293       return VNET_API_ERROR_UNSUPPORTED;
1294     }
1295
1296   m = nat44_ed_sm_o2i_lookup (sm, e_addr, e_port, 0, proto);
1297
1298   if (!m)
1299     {
1300       return VNET_API_ERROR_NO_SUCH_ENTRY;
1301     }
1302
1303   if (!is_sm_lb (m->flags))
1304     {
1305       return VNET_API_ERROR_INVALID_VALUE;
1306     }
1307
1308   pool_foreach (local, m->locals)
1309    {
1310     if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1311         (local->vrf_id == vrf_id))
1312       {
1313         match_local = local;
1314         break;
1315       }
1316   }
1317
1318   if (is_add)
1319     {
1320       if (match_local)
1321         {
1322           return VNET_API_ERROR_VALUE_EXIST;
1323         }
1324
1325       pool_get (m->locals, local);
1326       clib_memset (local, 0, sizeof (*local));
1327       local->addr.as_u32 = l_addr.as_u32;
1328       local->port = l_port;
1329       local->probability = probability;
1330       local->vrf_id = vrf_id;
1331       local->fib_index =
1332         fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1333                                            sm->fib_src_low);
1334
1335       if (!is_sm_out2in_only (m->flags))
1336         {
1337           if (nat44_ed_sm_i2o_add (sm, m, l_addr, l_port, local->fib_index,
1338                                    proto))
1339             {
1340               nat_elog_err (sm, "sm i2o key add failed");
1341               pool_put (m->locals, local);
1342               return VNET_API_ERROR_UNSPECIFIED;
1343             }
1344         }
1345     }
1346   else
1347     {
1348       if (!match_local)
1349         return VNET_API_ERROR_NO_SUCH_ENTRY;
1350
1351       if (pool_elts (m->locals) < 3)
1352         return VNET_API_ERROR_UNSPECIFIED;
1353
1354       fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4,
1355                         sm->fib_src_low);
1356
1357       if (!is_sm_out2in_only (m->flags))
1358         {
1359           if (nat44_ed_sm_i2o_del (sm, l_addr, l_port, match_local->fib_index,
1360                                    proto))
1361             nat_elog_err (sm, "sm i2o key del failed");
1362         }
1363
1364       if (sm->num_workers > 1)
1365         {
1366           ip4_header_t ip = {
1367             .src_address = local->addr,
1368           };
1369           tsm = vec_elt_at_index (
1370             sm->per_thread_data,
1371             nat44_ed_get_in2out_worker_index (0, &ip, m->fib_index, 0));
1372         }
1373       else
1374         tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1375
1376       /* Delete sessions */
1377       pool_foreach (s, tsm->sessions) {
1378           if (!(nat44_ed_is_lb_session (s)))
1379             continue;
1380
1381           if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1382               s->in2out.port != match_local->port)
1383             continue;
1384
1385           nat44_ed_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1386           nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1387       }
1388
1389       pool_put (m->locals, match_local);
1390     }
1391
1392   vec_free (m->workers);
1393
1394   pool_foreach (local, m->locals)
1395    {
1396     vec_add1 (locals, local - m->locals);
1397     if (sm->num_workers > 1)
1398       {
1399         ip4_header_t ip;
1400         ip.src_address.as_u32 = local->addr.as_u32,
1401         bitmap = clib_bitmap_set (
1402           bitmap,
1403           nat44_ed_get_in2out_worker_index (0, &ip, local->fib_index, 0), 1);
1404       }
1405   }
1406
1407   ASSERT (vec_len (locals) > 1);
1408
1409   local = pool_elt_at_index (m->locals, locals[0]);
1410   local->prefix = local->probability;
1411   for (i = 1; i < vec_len (locals); i++)
1412     {
1413       local = pool_elt_at_index (m->locals, locals[i]);
1414       prev_local = pool_elt_at_index (m->locals, locals[i - 1]);
1415       local->prefix = local->probability + prev_local->prefix;
1416     }
1417
1418   /* Assign workers */
1419   if (sm->num_workers > 1)
1420     {
1421       clib_bitmap_foreach (i, bitmap)  { vec_add1(m->workers, i); }
1422     }
1423
1424   return 0;
1425 }
1426
1427 void
1428 expire_per_vrf_sessions (u32 fib_index)
1429 {
1430   per_vrf_sessions_t *per_vrf_sessions;
1431   snat_main_per_thread_data_t *tsm;
1432   snat_main_t *sm = &snat_main;
1433
1434   vec_foreach (tsm, sm->per_thread_data)
1435     {
1436       vec_foreach (per_vrf_sessions, tsm->per_vrf_sessions_vec)
1437         {
1438           if ((per_vrf_sessions->rx_fib_index == fib_index) ||
1439               (per_vrf_sessions->tx_fib_index == fib_index))
1440             {
1441               per_vrf_sessions->expired = 1;
1442             }
1443         }
1444     }
1445 }
1446
1447 void
1448 update_per_vrf_sessions_vec (u32 fib_index, int is_del)
1449 {
1450   snat_main_t *sm = &snat_main;
1451   nat_fib_t *fib;
1452
1453   // we don't care if it is outside/inside fib
1454   // we just care about their ref_count
1455   // if it reaches 0 sessions should expire
1456   // because the fib isn't valid for NAT anymore
1457
1458   vec_foreach (fib, sm->fibs)
1459   {
1460     if (fib->fib_index == fib_index)
1461       {
1462         if (is_del)
1463           {
1464             fib->ref_count--;
1465             if (!fib->ref_count)
1466               {
1467                 vec_del1 (sm->fibs, fib - sm->fibs);
1468                 expire_per_vrf_sessions (fib_index);
1469               }
1470             return;
1471           }
1472         else
1473           fib->ref_count++;
1474       }
1475   }
1476   if (!is_del)
1477     {
1478       vec_add2 (sm->fibs, fib, 1);
1479       fib->ref_count = 1;
1480       fib->fib_index = fib_index;
1481     }
1482 }
1483
1484 static_always_inline nat_outside_fib_t *
1485 nat44_ed_get_outside_fib (nat_outside_fib_t *outside_fibs, u32 fib_index)
1486 {
1487   nat_outside_fib_t *f;
1488   vec_foreach (f, outside_fibs)
1489     {
1490       if (f->fib_index == fib_index)
1491         {
1492           return f;
1493         }
1494     }
1495   return 0;
1496 }
1497
1498 static_always_inline snat_interface_t *
1499 nat44_ed_get_interface (snat_interface_t *interfaces, u32 sw_if_index)
1500 {
1501   snat_interface_t *i;
1502   pool_foreach (i, interfaces)
1503     {
1504       if (i->sw_if_index == sw_if_index)
1505         {
1506           return i;
1507         }
1508     }
1509   return 0;
1510 }
1511
1512 int
1513 nat44_ed_add_interface (u32 sw_if_index, u8 is_inside)
1514 {
1515   const char *del_feature_name, *feature_name;
1516   snat_main_t *sm = &snat_main;
1517
1518   nat_outside_fib_t *outside_fib;
1519   snat_interface_t *i;
1520   u32 fib_index;
1521   int rv;
1522
1523   if (!sm->enabled)
1524     {
1525       nat_log_err ("nat44 is disabled");
1526       return VNET_API_ERROR_UNSUPPORTED;
1527     }
1528
1529   if (nat44_ed_get_interface (sm->output_feature_interfaces, sw_if_index))
1530     {
1531       nat_log_err ("error interface already configured");
1532       return VNET_API_ERROR_VALUE_EXIST;
1533     }
1534
1535   i = nat44_ed_get_interface (sm->interfaces, sw_if_index);
1536   if (i)
1537     {
1538       if ((nat44_ed_is_interface_inside (i) && is_inside) ||
1539           (nat44_ed_is_interface_outside (i) && !is_inside))
1540         {
1541           return 0;
1542         }
1543       if (sm->num_workers > 1)
1544         {
1545           del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1546                                           "nat44-out2in-worker-handoff";
1547           feature_name = "nat44-handoff-classify";
1548         }
1549       else
1550         {
1551           del_feature_name = !is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1552
1553           feature_name = "nat44-ed-classify";
1554         }
1555
1556       rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
1557       if (rv)
1558         return rv;
1559       vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1560                                    sw_if_index, 0, 0, 0);
1561       vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1,
1562                                    0, 0);
1563     }
1564   else
1565     {
1566       if (sm->num_workers > 1)
1567         {
1568           feature_name = is_inside ? "nat44-in2out-worker-handoff" :
1569                                      "nat44-out2in-worker-handoff";
1570         }
1571       else
1572         {
1573           feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1574         }
1575
1576       nat_validate_interface_counters (sm, sw_if_index);
1577       rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
1578       if (rv)
1579         return rv;
1580       vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1,
1581                                    0, 0);
1582
1583       pool_get (sm->interfaces, i);
1584       i->sw_if_index = sw_if_index;
1585       i->flags = 0;
1586     }
1587
1588   fib_index =
1589     fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
1590
1591   update_per_vrf_sessions_vec (fib_index, 0 /*is_del*/);
1592
1593   if (!is_inside)
1594     {
1595       i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1596
1597       outside_fib = nat44_ed_get_outside_fib (sm->outside_fibs, fib_index);
1598       if (outside_fib)
1599         {
1600           outside_fib->refcount++;
1601         }
1602       else
1603         {
1604           vec_add2 (sm->outside_fibs, outside_fib, 1);
1605           outside_fib->fib_index = fib_index;
1606           outside_fib->refcount = 1;
1607         }
1608
1609       nat44_ed_add_del_addr_to_fib_foreach_addr (sw_if_index, 1);
1610       nat44_ed_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 1);
1611     }
1612   else
1613     {
1614       i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1615     }
1616
1617   return 0;
1618 }
1619
1620 int
1621 nat44_ed_del_interface (u32 sw_if_index, u8 is_inside)
1622 {
1623   const char *del_feature_name, *feature_name;
1624   snat_main_t *sm = &snat_main;
1625
1626   nat_outside_fib_t *outside_fib;
1627   snat_interface_t *i;
1628   u32 fib_index;
1629   int rv;
1630
1631   if (!sm->enabled)
1632     {
1633       nat_log_err ("nat44 is disabled");
1634       return VNET_API_ERROR_UNSUPPORTED;
1635     }
1636
1637   i = nat44_ed_get_interface (sm->interfaces, sw_if_index);
1638   if (i == 0)
1639     {
1640       nat_log_err ("error interface couldn't be found");
1641       return VNET_API_ERROR_NO_SUCH_ENTRY;
1642     }
1643
1644   if (nat44_ed_is_interface_inside (i) && nat44_ed_is_interface_outside (i))
1645     {
1646       if (sm->num_workers > 1)
1647         {
1648           del_feature_name = "nat44-handoff-classify";
1649           feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1650                                       "nat44-out2in-worker-handoff";
1651         }
1652       else
1653         {
1654           del_feature_name = "nat44-ed-classify";
1655           feature_name = !is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1656         }
1657
1658       rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1659       if (rv)
1660         {
1661           return rv;
1662         }
1663       vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1664                                    sw_if_index, 0, 0, 0);
1665       vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1,
1666                                    0, 0);
1667
1668       if (is_inside)
1669         {
1670           i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1671         }
1672       else
1673         {
1674           i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1675         }
1676     }
1677   else
1678     {
1679       if (sm->num_workers > 1)
1680         {
1681           feature_name = is_inside ? "nat44-in2out-worker-handoff" :
1682                                      "nat44-out2in-worker-handoff";
1683         }
1684       else
1685         {
1686           feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1687         }
1688
1689       rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1690       if (rv)
1691         {
1692           return rv;
1693         }
1694       vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 0,
1695                                    0, 0);
1696
1697       // remove interface
1698       pool_put (sm->interfaces, i);
1699     }
1700
1701   fib_index =
1702     fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
1703
1704   update_per_vrf_sessions_vec (fib_index, 1 /*is_del*/);
1705
1706   if (!is_inside)
1707     {
1708       outside_fib = nat44_ed_get_outside_fib (sm->outside_fibs, fib_index);
1709       if (outside_fib)
1710         {
1711           outside_fib->refcount--;
1712           if (!outside_fib->refcount)
1713             {
1714               vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1715             }
1716         }
1717
1718       nat44_ed_add_del_addr_to_fib_foreach_addr (sw_if_index, 0);
1719       nat44_ed_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 0);
1720     }
1721
1722   return 0;
1723 }
1724
1725 int
1726 nat44_ed_add_output_interface (u32 sw_if_index)
1727 {
1728   snat_main_t *sm = &snat_main;
1729
1730   nat_outside_fib_t *outside_fib;
1731   snat_interface_t *i;
1732   u32 fib_index;
1733   int rv;
1734
1735   if (!sm->enabled)
1736     {
1737       nat_log_err ("nat44 is disabled");
1738       return VNET_API_ERROR_UNSUPPORTED;
1739     }
1740
1741   if (nat44_ed_get_interface (sm->interfaces, sw_if_index))
1742     {
1743       nat_log_err ("error interface already configured");
1744       return VNET_API_ERROR_VALUE_EXIST;
1745     }
1746
1747   if (nat44_ed_get_interface (sm->output_feature_interfaces, sw_if_index))
1748     {
1749       nat_log_err ("error interface already configured");
1750       return VNET_API_ERROR_VALUE_EXIST;
1751     }
1752
1753   if (sm->num_workers > 1)
1754     {
1755       rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
1756       if (rv)
1757         {
1758           return rv;
1759         }
1760
1761       rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 1);
1762       if (rv)
1763         {
1764           return rv;
1765         }
1766
1767       vnet_feature_enable_disable (
1768         "ip4-unicast", "nat44-out2in-worker-handoff", sw_if_index, 1, 0, 0);
1769       vnet_feature_enable_disable ("ip4-output",
1770                                    "nat44-in2out-output-worker-handoff",
1771                                    sw_if_index, 1, 0, 0);
1772     }
1773   else
1774     {
1775       rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
1776       if (rv)
1777         {
1778           return rv;
1779         }
1780
1781       rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 1);
1782       if (rv)
1783         {
1784           return rv;
1785         }
1786
1787       vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
1788                                    sw_if_index, 1, 0, 0);
1789       vnet_feature_enable_disable ("ip4-output", "nat-pre-in2out-output",
1790                                    sw_if_index, 1, 0, 0);
1791     }
1792
1793   nat_validate_interface_counters (sm, sw_if_index);
1794
1795   pool_get (sm->output_feature_interfaces, i);
1796   i->sw_if_index = sw_if_index;
1797   i->flags = 0;
1798   i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1799   i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1800
1801   fib_index =
1802     fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
1803   update_per_vrf_sessions_vec (fib_index, 0 /*is_del*/);
1804
1805   outside_fib = nat44_ed_get_outside_fib (sm->outside_fibs, fib_index);
1806   if (outside_fib)
1807     {
1808       outside_fib->refcount++;
1809     }
1810   else
1811     {
1812       vec_add2 (sm->outside_fibs, outside_fib, 1);
1813       outside_fib->fib_index = fib_index;
1814       outside_fib->refcount = 1;
1815     }
1816
1817   nat44_ed_add_del_addr_to_fib_foreach_addr (sw_if_index, 1);
1818   nat44_ed_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 1);
1819
1820   return 0;
1821 }
1822
1823 int
1824 nat44_ed_del_output_interface (u32 sw_if_index)
1825 {
1826   snat_main_t *sm = &snat_main;
1827
1828   nat_outside_fib_t *outside_fib;
1829   snat_interface_t *i;
1830   u32 fib_index;
1831   int rv;
1832
1833   if (!sm->enabled)
1834     {
1835       nat_log_err ("nat44 is disabled");
1836       return VNET_API_ERROR_UNSUPPORTED;
1837     }
1838
1839   i = nat44_ed_get_interface (sm->output_feature_interfaces, sw_if_index);
1840   if (!i)
1841     {
1842       nat_log_err ("error interface couldn't be found");
1843       return VNET_API_ERROR_NO_SUCH_ENTRY;
1844     }
1845
1846   if (sm->num_workers > 1)
1847     {
1848       rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1849       if (rv)
1850         {
1851           return rv;
1852         }
1853
1854       rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 0);
1855       if (rv)
1856         {
1857           return rv;
1858         }
1859
1860       vnet_feature_enable_disable (
1861         "ip4-unicast", "nat44-out2in-worker-handoff", sw_if_index, 0, 0, 0);
1862       vnet_feature_enable_disable ("ip4-output",
1863                                    "nat44-in2out-output-worker-handoff",
1864                                    sw_if_index, 0, 0, 0);
1865     }
1866   else
1867     {
1868       rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1869       if (rv)
1870         {
1871           return rv;
1872         }
1873
1874       rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 0);
1875       if (rv)
1876         {
1877           return rv;
1878         }
1879
1880       vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
1881                                    sw_if_index, 0, 0, 0);
1882       vnet_feature_enable_disable ("ip4-output", "nat-pre-in2out-output",
1883                                    sw_if_index, 0, 0, 0);
1884     }
1885
1886   // remove interface
1887   pool_put (sm->output_feature_interfaces, i);
1888
1889   fib_index =
1890     fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
1891   update_per_vrf_sessions_vec (fib_index, 1 /*is_del*/);
1892
1893   outside_fib = nat44_ed_get_outside_fib (sm->outside_fibs, fib_index);
1894   if (outside_fib)
1895     {
1896       outside_fib->refcount--;
1897       if (!outside_fib->refcount)
1898         {
1899           vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1900         }
1901     }
1902
1903   nat44_ed_add_del_addr_to_fib_foreach_addr (sw_if_index, 0);
1904   nat44_ed_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 0);
1905
1906   return 0;
1907 }
1908
1909 int
1910 snat_set_workers (uword * bitmap)
1911 {
1912   snat_main_t *sm = &snat_main;
1913   int i, j = 0;
1914
1915   if (sm->num_workers < 2)
1916     return VNET_API_ERROR_FEATURE_DISABLED;
1917
1918   if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1919     return VNET_API_ERROR_INVALID_WORKER;
1920
1921   vec_free (sm->workers);
1922   clib_bitmap_foreach (i, bitmap)
1923     {
1924       vec_add1(sm->workers, i);
1925       sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1926       sm->per_thread_data[sm->first_worker_index + i].thread_index = i;
1927       j++;
1928     }
1929
1930   sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1931
1932   return 0;
1933 }
1934
1935 int
1936 nat44_ed_set_frame_queue_nelts (u32 frame_queue_nelts)
1937 {
1938   fail_if_enabled ();
1939   snat_main_t *sm = &snat_main;
1940   sm->frame_queue_nelts = frame_queue_nelts;
1941   return 0;
1942 }
1943
1944 static void
1945 nat44_ed_update_outside_fib_cb (ip4_main_t *im, uword opaque, u32 sw_if_index,
1946                                 u32 new_fib_index, u32 old_fib_index)
1947 {
1948   snat_main_t *sm = &snat_main;
1949   nat_outside_fib_t *outside_fib;
1950   snat_interface_t *i;
1951   u8 is_add = 1;
1952   u8 match = 0;
1953
1954   if (!sm->enabled || (new_fib_index == old_fib_index)
1955       || (!vec_len (sm->outside_fibs)))
1956     {
1957       return;
1958     }
1959
1960   pool_foreach (i, sm->interfaces)
1961      {
1962       if (i->sw_if_index == sw_if_index)
1963         {
1964           if (!(nat44_ed_is_interface_outside (i)))
1965             return;
1966           match = 1;
1967         }
1968     }
1969
1970   pool_foreach (i, sm->output_feature_interfaces)
1971      {
1972       if (i->sw_if_index == sw_if_index)
1973         {
1974           if (!(nat44_ed_is_interface_outside (i)))
1975             return;
1976           match = 1;
1977         }
1978     }
1979
1980   if (!match)
1981     return;
1982
1983   vec_foreach (outside_fib, sm->outside_fibs)
1984   {
1985     if (outside_fib->fib_index == old_fib_index)
1986       {
1987         outside_fib->refcount--;
1988         if (!outside_fib->refcount)
1989           vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1990         break;
1991       }
1992   }
1993
1994   vec_foreach (outside_fib, sm->outside_fibs)
1995   {
1996     if (outside_fib->fib_index == new_fib_index)
1997       {
1998         outside_fib->refcount++;
1999         is_add = 0;
2000         break;
2001       }
2002   }
2003
2004   if (is_add)
2005     {
2006       vec_add2 (sm->outside_fibs, outside_fib, 1);
2007       outside_fib->refcount = 1;
2008       outside_fib->fib_index = new_fib_index;
2009     }
2010 }
2011
2012 static void nat44_ed_update_outside_fib_cb (ip4_main_t *im, uword opaque,
2013                                             u32 sw_if_index, u32 new_fib_index,
2014                                             u32 old_fib_index);
2015
2016 static void nat44_ed_add_del_interface_address_cb (
2017   ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
2018   u32 address_length, u32 if_address_index, u32 is_delete);
2019
2020 static void nat44_ed_add_del_static_mapping_addr_only_cb (
2021   ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
2022   u32 address_length, u32 if_address_index, u32 is_delete);
2023
2024 void
2025 test_key_calc_split ()
2026 {
2027   ip4_address_t l_addr;
2028   l_addr.as_u8[0] = 1;
2029   l_addr.as_u8[1] = 1;
2030   l_addr.as_u8[2] = 1;
2031   l_addr.as_u8[3] = 1;
2032   ip4_address_t r_addr;
2033   r_addr.as_u8[0] = 2;
2034   r_addr.as_u8[1] = 2;
2035   r_addr.as_u8[2] = 2;
2036   r_addr.as_u8[3] = 2;
2037   u16 l_port = 40001;
2038   u16 r_port = 40301;
2039   u8 proto = 9;
2040   u32 fib_index = 9000001;
2041   u32 thread_index = 3000000001;
2042   u32 session_index = 3000000221;
2043   clib_bihash_kv_16_8_t kv;
2044   init_ed_kv (&kv, l_addr.as_u32, l_port, r_addr.as_u32, r_port, fib_index,
2045               proto, thread_index, session_index);
2046   ip4_address_t l_addr2;
2047   ip4_address_t r_addr2;
2048   clib_memset (&l_addr2, 0, sizeof (l_addr2));
2049   clib_memset (&r_addr2, 0, sizeof (r_addr2));
2050   u16 l_port2 = 0;
2051   u16 r_port2 = 0;
2052   u8 proto2 = 0;
2053   u32 fib_index2 = 0;
2054   split_ed_kv (&kv, &l_addr2, &r_addr2, &proto2, &fib_index2, &l_port2,
2055                &r_port2);
2056   ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2057   ASSERT (r_addr.as_u32 == r_addr2.as_u32);
2058   ASSERT (l_port == l_port2);
2059   ASSERT (r_port == r_port2);
2060   ASSERT (proto == proto2);
2061   ASSERT (fib_index == fib_index2);
2062   ASSERT (thread_index == ed_value_get_thread_index (&kv));
2063   ASSERT (session_index == ed_value_get_session_index (&kv));
2064 }
2065
2066 static clib_error_t *
2067 nat_ip_table_add_del (vnet_main_t * vnm, u32 table_id, u32 is_add)
2068 {
2069   u32 fib_index;
2070   if (!is_add)
2071     {
2072       fib_index = ip4_fib_index_from_table_id (table_id);
2073       if (fib_index != ~0)
2074         {
2075           expire_per_vrf_sessions (fib_index);
2076         }
2077     }
2078   return 0;
2079 }
2080
2081 VNET_IP_TABLE_ADD_DEL_FUNCTION (nat_ip_table_add_del);
2082
2083 void
2084 nat44_set_node_indexes (snat_main_t * sm, vlib_main_t * vm)
2085 {
2086   vlib_node_t *node;
2087
2088   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in");
2089   sm->out2in_node_index = node->index;
2090
2091   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out");
2092   sm->in2out_node_index = node->index;
2093
2094   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-output");
2095   sm->in2out_output_node_index = node->index;
2096 }
2097
2098 #define nat_validate_simple_counter(c, i)                                     \
2099   do                                                                          \
2100     {                                                                         \
2101       vlib_validate_simple_counter (&c, i);                                   \
2102       vlib_zero_simple_counter (&c, i);                                       \
2103     }                                                                         \
2104   while (0);
2105
2106 #define nat_init_simple_counter(c, n, sn)                                     \
2107   do                                                                          \
2108     {                                                                         \
2109       c.name = n;                                                             \
2110       c.stat_segment_name = sn;                                               \
2111       nat_validate_simple_counter (c, 0);                                     \
2112     }                                                                         \
2113   while (0);
2114
2115 static_always_inline void
2116 nat_validate_interface_counters (snat_main_t *sm, u32 sw_if_index)
2117 {
2118 #define _(x)                                                                  \
2119   nat_validate_simple_counter (sm->counters.fastpath.in2out.x, sw_if_index);  \
2120   nat_validate_simple_counter (sm->counters.fastpath.out2in.x, sw_if_index);  \
2121   nat_validate_simple_counter (sm->counters.slowpath.in2out.x, sw_if_index);  \
2122   nat_validate_simple_counter (sm->counters.slowpath.out2in.x, sw_if_index);
2123   foreach_nat_counter;
2124 #undef _
2125   nat_validate_simple_counter (sm->counters.hairpinning, sw_if_index);
2126 }
2127
2128 static clib_error_t *
2129 nat_init (vlib_main_t * vm)
2130 {
2131   snat_main_t *sm = &snat_main;
2132   vlib_thread_main_t *tm = vlib_get_thread_main ();
2133   vlib_thread_registration_t *tr;
2134   ip4_add_del_interface_address_callback_t cbi = { 0 };
2135   ip4_table_bind_callback_t cbt = { 0 };
2136   u32 i, num_threads = 0;
2137   uword *p, *bitmap = 0;
2138
2139   clib_memset (sm, 0, sizeof (*sm));
2140
2141   // required
2142   sm->vnet_main = vnet_get_main ();
2143   // convenience
2144   sm->ip4_main = &ip4_main;
2145   sm->api_main = vlibapi_get_main ();
2146   sm->ip4_lookup_main = &ip4_main.lookup_main;
2147
2148   // frame queue indices used for handoff
2149   sm->fq_out2in_index = ~0;
2150   sm->fq_in2out_index = ~0;
2151   sm->fq_in2out_output_index = ~0;
2152
2153   sm->log_level = NAT_LOG_ERROR;
2154
2155   nat44_set_node_indexes (sm, vm);
2156
2157   sm->log_class = vlib_log_register_class ("nat", 0);
2158   nat_ipfix_logging_init (vm);
2159
2160   nat_init_simple_counter (sm->total_sessions, "total-sessions",
2161                            "/nat44-ed/total-sessions");
2162   sm->max_cfg_sessions_gauge = stat_segment_new_entry (
2163     (u8 *) "/nat44-ed/max-cfg-sessions", STAT_DIR_TYPE_SCALAR_INDEX);
2164
2165 #define _(x)                                                                  \
2166   nat_init_simple_counter (sm->counters.fastpath.in2out.x, #x,                \
2167                            "/nat44-ed/in2out/fastpath/" #x);                  \
2168   nat_init_simple_counter (sm->counters.fastpath.out2in.x, #x,                \
2169                            "/nat44-ed/out2in/fastpath/" #x);                  \
2170   nat_init_simple_counter (sm->counters.slowpath.in2out.x, #x,                \
2171                            "/nat44-ed/in2out/slowpath/" #x);                  \
2172   nat_init_simple_counter (sm->counters.slowpath.out2in.x, #x,                \
2173                            "/nat44-ed/out2in/slowpath/" #x);
2174   foreach_nat_counter;
2175 #undef _
2176   nat_init_simple_counter (sm->counters.hairpinning, "hairpinning",
2177                            "/nat44-ed/hairpinning");
2178
2179   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2180   if (p)
2181     {
2182       tr = (vlib_thread_registration_t *) p[0];
2183       if (tr)
2184         {
2185           sm->num_workers = tr->count;
2186           sm->first_worker_index = tr->first_index;
2187         }
2188     }
2189   num_threads = tm->n_vlib_mains - 1;
2190   sm->port_per_thread = 0xffff - 1024;
2191   vec_validate (sm->per_thread_data, num_threads);
2192
2193   /* Use all available workers by default */
2194   if (sm->num_workers > 1)
2195     {
2196       for (i = 0; i < sm->num_workers; i++)
2197         bitmap = clib_bitmap_set (bitmap, i, 1);
2198       snat_set_workers (bitmap);
2199       clib_bitmap_free (bitmap);
2200     }
2201   else
2202     {
2203       sm->per_thread_data[0].snat_thread_index = 0;
2204     }
2205
2206   /* callbacks to call when interface address changes. */
2207   cbi.function = nat44_ed_add_del_interface_address_cb;
2208   vec_add1 (sm->ip4_main->add_del_interface_address_callbacks, cbi);
2209   cbi.function = nat44_ed_add_del_static_mapping_addr_only_cb;
2210   vec_add1 (sm->ip4_main->add_del_interface_address_callbacks, cbi);
2211
2212   /* callbacks to call when interface to table biding changes */
2213   cbt.function = nat44_ed_update_outside_fib_cb;
2214   vec_add1 (sm->ip4_main->table_bind_callbacks, cbt);
2215
2216   sm->fib_src_low =
2217     fib_source_allocate ("nat-low", FIB_SOURCE_PRIORITY_LOW,
2218                          FIB_SOURCE_BH_SIMPLE);
2219   sm->fib_src_hi =
2220     fib_source_allocate ("nat-hi", FIB_SOURCE_PRIORITY_HI,
2221                          FIB_SOURCE_BH_SIMPLE);
2222
2223   nat_affinity_init (vm);
2224   test_key_calc_split ();
2225
2226   return nat44_api_hookup (vm);
2227 }
2228
2229 VLIB_INIT_FUNCTION (nat_init);
2230
2231 int
2232 nat44_plugin_enable (nat44_config_t c)
2233 {
2234   snat_main_t *sm = &snat_main;
2235
2236   fail_if_enabled ();
2237
2238   if (c.static_mapping_only && !c.connection_tracking)
2239     {
2240       nat_log_err ("unsupported combination of configuration");
2241       return 1;
2242     }
2243
2244   sm->static_mapping_only = c.static_mapping_only;
2245   sm->static_mapping_connection_tracking = c.connection_tracking;
2246
2247   sm->forwarding_enabled = 0;
2248   sm->mss_clamping = 0;
2249   sm->pat = (!c.static_mapping_only ||
2250              (c.static_mapping_only && c.connection_tracking));
2251
2252   if (!c.sessions)
2253     c.sessions = 63 * 1024;
2254
2255   sm->max_translations_per_thread = c.sessions;
2256   stat_segment_set_state_counter (sm->max_cfg_sessions_gauge,
2257                                   sm->max_translations_per_thread);
2258   sm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
2259
2260   vec_add1 (sm->max_translations_per_fib, sm->max_translations_per_thread);
2261
2262   sm->inside_vrf_id = c.inside_vrf;
2263   sm->inside_fib_index =
2264     fib_table_find_or_create_and_lock
2265     (FIB_PROTOCOL_IP4, c.inside_vrf, sm->fib_src_hi);
2266
2267   sm->outside_vrf_id = c.outside_vrf;
2268   sm->outside_fib_index = fib_table_find_or_create_and_lock (
2269     FIB_PROTOCOL_IP4, c.outside_vrf, sm->fib_src_hi);
2270
2271   nat44_ed_db_init (sm->max_translations_per_thread, sm->translation_buckets);
2272
2273   nat_affinity_enable ();
2274
2275   nat_reset_timeouts (&sm->timeouts);
2276
2277   vlib_zero_simple_counter (&sm->total_sessions, 0);
2278
2279   if (!sm->frame_queue_nelts)
2280     {
2281       sm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT;
2282     }
2283
2284   if (sm->num_workers > 1)
2285     {
2286       if (sm->fq_in2out_index == ~0)
2287         {
2288           sm->fq_in2out_index = vlib_frame_queue_main_init (
2289             sm->in2out_node_index, sm->frame_queue_nelts);
2290         }
2291       if (sm->fq_out2in_index == ~0)
2292         {
2293           sm->fq_out2in_index = vlib_frame_queue_main_init (
2294             sm->out2in_node_index, sm->frame_queue_nelts);
2295         }
2296       if (sm->fq_in2out_output_index == ~0)
2297         {
2298           sm->fq_in2out_output_index = vlib_frame_queue_main_init (
2299             sm->in2out_output_node_index, sm->frame_queue_nelts);
2300         }
2301     }
2302
2303   sm->enabled = 1;
2304   sm->rconfig = c;
2305
2306   return 0;
2307 }
2308
2309 void
2310 nat44_addresses_free (snat_address_t ** addresses)
2311 {
2312   vec_free (*addresses);
2313   *addresses = 0;
2314 }
2315
2316 int
2317 nat44_plugin_disable ()
2318 {
2319   snat_main_t *sm = &snat_main;
2320   snat_interface_t *i, *pool;
2321   int error = 0;
2322
2323   fail_if_disabled ();
2324
2325   pool = pool_dup (sm->interfaces);
2326   pool_foreach (i, pool)
2327     {
2328       if (nat44_ed_is_interface_inside (i))
2329         {
2330           error = nat44_ed_del_interface (i->sw_if_index, 1);
2331         }
2332       if (nat44_ed_is_interface_outside (i))
2333         {
2334           error = nat44_ed_del_interface (i->sw_if_index, 0);
2335         }
2336       if (error)
2337         {
2338           nat_log_err ("error occurred while removing interface %u",
2339                        i->sw_if_index);
2340         }
2341     }
2342   pool_free (sm->interfaces);
2343   pool_free (pool);
2344   sm->interfaces = 0;
2345
2346   pool = pool_dup (sm->output_feature_interfaces);
2347   pool_foreach (i, pool)
2348     {
2349       error = nat44_ed_del_output_interface (i->sw_if_index);
2350       if (error)
2351         {
2352           nat_log_err ("error occurred while removing interface %u",
2353                        i->sw_if_index);
2354         }
2355     }
2356   pool_free (sm->output_feature_interfaces);
2357   pool_free (pool);
2358   sm->output_feature_interfaces = 0;
2359
2360   vec_free (sm->max_translations_per_fib);
2361
2362   nat44_ed_db_free ();
2363
2364   nat44_addresses_free (&sm->addresses);
2365   nat44_addresses_free (&sm->twice_nat_addresses);
2366
2367   vec_free (sm->to_resolve);
2368   vec_free (sm->auto_add_sw_if_indices);
2369   vec_free (sm->auto_add_sw_if_indices_twice_nat);
2370
2371   sm->to_resolve = 0;
2372   sm->auto_add_sw_if_indices = 0;
2373   sm->auto_add_sw_if_indices_twice_nat = 0;
2374
2375   sm->forwarding_enabled = 0;
2376
2377   sm->enabled = 0;
2378   clib_memset (&sm->rconfig, 0, sizeof (sm->rconfig));
2379
2380   return 0;
2381 }
2382
2383 void
2384 nat44_ed_forwarding_enable_disable (u8 is_enable)
2385 {
2386   snat_main_per_thread_data_t *tsm;
2387   snat_main_t *sm = &snat_main;
2388   snat_session_t *s;
2389
2390   u32 *ses_to_be_removed = 0, *ses_index;
2391
2392   sm->forwarding_enabled = is_enable != 0;
2393
2394   if (!sm->enabled || is_enable)
2395     {
2396       return;
2397     }
2398
2399   vec_foreach (tsm, sm->per_thread_data)
2400     {
2401       pool_foreach (s, tsm->sessions)
2402         {
2403           if (na44_ed_is_fwd_bypass_session (s))
2404             {
2405               vec_add1 (ses_to_be_removed, s - tsm->sessions);
2406             }
2407         }
2408       vec_foreach (ses_index, ses_to_be_removed)
2409         {
2410           s = pool_elt_at_index (tsm->sessions, ses_index[0]);
2411           nat44_ed_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2412           nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
2413         }
2414
2415       vec_free (ses_to_be_removed);
2416     }
2417 }
2418
2419 static_always_inline snat_static_mapping_t *
2420 nat44_ed_sm_match (snat_main_t *sm, ip4_address_t match_addr, u16 match_port,
2421                    u32 match_fib_index, ip_protocol_t match_protocol,
2422                    int by_external)
2423 {
2424   snat_static_mapping_t *m;
2425   if (!by_external)
2426     {
2427       m = nat44_ed_sm_i2o_lookup (sm, match_addr, match_port, match_fib_index,
2428                                   match_protocol);
2429       if (m)
2430         return m;
2431
2432       /* Try address only mapping */
2433       m = nat44_ed_sm_i2o_lookup (sm, match_addr, 0, match_fib_index, 0);
2434       if (m)
2435         return m;
2436
2437       if (sm->inside_fib_index != match_fib_index)
2438         {
2439           m = nat44_ed_sm_i2o_lookup (sm, match_addr, match_port,
2440                                       sm->inside_fib_index, match_protocol);
2441           if (m)
2442             return m;
2443
2444           /* Try address only mapping */
2445           m = nat44_ed_sm_i2o_lookup (sm, match_addr, 0, sm->inside_fib_index,
2446                                       0);
2447           if (m)
2448             return m;
2449         }
2450       if (sm->outside_fib_index != match_fib_index)
2451         {
2452           m = nat44_ed_sm_i2o_lookup (sm, match_addr, match_port,
2453                                       sm->outside_fib_index, match_protocol);
2454           if (m)
2455             return m;
2456
2457           /* Try address only mapping */
2458           m = nat44_ed_sm_i2o_lookup (sm, match_addr, 0, sm->outside_fib_index,
2459                                       0);
2460           if (m)
2461             return m;
2462         }
2463     }
2464   else
2465     {
2466       m =
2467         nat44_ed_sm_o2i_lookup (sm, match_addr, match_port, 0, match_protocol);
2468       if (m)
2469         return m;
2470
2471       /* Try address only mapping */
2472       m = nat44_ed_sm_o2i_lookup (sm, match_addr, 0, 0, 0);
2473       if (m)
2474         return m;
2475     }
2476   return 0;
2477 }
2478
2479 int
2480 snat_static_mapping_match (vlib_main_t *vm, snat_main_t *sm,
2481                            ip4_address_t match_addr, u16 match_port,
2482                            u32 match_fib_index, ip_protocol_t match_protocol,
2483                            ip4_address_t *mapping_addr, u16 *mapping_port,
2484                            u32 *mapping_fib_index, int by_external,
2485                            u8 *is_addr_only, twice_nat_type_t *twice_nat,
2486                            lb_nat_type_t *lb, ip4_address_t *ext_host_addr,
2487                            u8 *is_identity_nat, snat_static_mapping_t **out)
2488 {
2489   snat_static_mapping_t *m;
2490   u32 rand, lo = 0, hi, mid, *tmp = 0, i;
2491   nat44_lb_addr_port_t *local;
2492   u8 backend_index;
2493
2494   m = nat44_ed_sm_match (sm, match_addr, match_port, match_fib_index,
2495                          match_protocol, by_external);
2496   if (!m)
2497     {
2498       return 1;
2499     }
2500
2501   if (by_external)
2502     {
2503       if (is_sm_lb (m->flags))
2504         {
2505           if (PREDICT_FALSE (lb != 0))
2506             *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2507           if (m->affinity && !nat_affinity_find_and_lock (
2508                                vm, ext_host_addr[0], match_addr,
2509                                match_protocol, match_port, &backend_index))
2510             {
2511               local = pool_elt_at_index (m->locals, backend_index);
2512               *mapping_addr = local->addr;
2513               *mapping_port = local->port;
2514               *mapping_fib_index = local->fib_index;
2515               goto end;
2516             }
2517           // pick locals matching this worker
2518           if (PREDICT_FALSE (sm->num_workers > 1))
2519             {
2520               u32 thread_index = vlib_get_thread_index ();
2521               pool_foreach_index (i, m->locals)
2522                {
2523                 local = pool_elt_at_index (m->locals, i);
2524
2525                 ip4_header_t ip = {
2526                   .src_address = local->addr,
2527                 };
2528
2529                 if (nat44_ed_get_in2out_worker_index (0, &ip, m->fib_index,
2530                                                       0) == thread_index)
2531                   {
2532                     vec_add1 (tmp, i);
2533                   }
2534                }
2535               ASSERT (vec_len (tmp) != 0);
2536             }
2537           else
2538             {
2539               pool_foreach_index (i, m->locals)
2540                {
2541                 vec_add1 (tmp, i);
2542               }
2543             }
2544           hi = vec_len (tmp) - 1;
2545           local = pool_elt_at_index (m->locals, tmp[hi]);
2546           rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
2547           while (lo < hi)
2548             {
2549               mid = ((hi - lo) >> 1) + lo;
2550               local = pool_elt_at_index (m->locals, tmp[mid]);
2551               (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
2552             }
2553           local = pool_elt_at_index (m->locals, tmp[lo]);
2554           if (!(local->prefix >= rand))
2555             return 1;
2556           *mapping_addr = local->addr;
2557           *mapping_port = local->port;
2558           *mapping_fib_index = local->fib_index;
2559           if (m->affinity)
2560             {
2561               if (nat_affinity_create_and_lock (ext_host_addr[0], match_addr,
2562                                                 match_protocol, match_port,
2563                                                 tmp[lo], m->affinity,
2564                                                 m->affinity_per_service_list_head_index))
2565                 nat_elog_info (sm, "create affinity record failed");
2566             }
2567           vec_free (tmp);
2568         }
2569       else
2570         {
2571           if (PREDICT_FALSE (lb != 0))
2572             *lb = NO_LB_NAT;
2573           *mapping_fib_index = m->fib_index;
2574           *mapping_addr = m->local_addr;
2575           /* Address only mapping doesn't change port */
2576           *mapping_port =
2577             is_sm_addr_only (m->flags) ? match_port : m->local_port;
2578         }
2579     }
2580   else
2581     {
2582       *mapping_addr = m->external_addr;
2583       /* Address only mapping doesn't change port */
2584       *mapping_port =
2585         is_sm_addr_only (m->flags) ? match_port : m->external_port;
2586       *mapping_fib_index = sm->outside_fib_index;
2587     }
2588
2589 end:
2590   if (PREDICT_FALSE (is_addr_only != 0))
2591     *is_addr_only = is_sm_addr_only (m->flags);
2592
2593   if (PREDICT_FALSE (twice_nat != 0))
2594     {
2595       *twice_nat = TWICE_NAT_DISABLED;
2596
2597       if (is_sm_twice_nat (m->flags))
2598         {
2599           *twice_nat = TWICE_NAT;
2600         }
2601       else if (is_sm_self_twice_nat (m->flags))
2602         {
2603           *twice_nat = TWICE_NAT_SELF;
2604         }
2605     }
2606
2607   if (PREDICT_FALSE (is_identity_nat != 0))
2608     *is_identity_nat = is_sm_identity_nat (m->flags);
2609
2610   if (out != 0)
2611     *out = m;
2612
2613   return 0;
2614 }
2615
2616 u32
2617 nat44_ed_get_in2out_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
2618                                   u32 rx_fib_index, u8 is_output)
2619 {
2620   snat_main_t *sm = &snat_main;
2621   u32 next_worker_index = sm->first_worker_index;
2622   u32 hash;
2623
2624   clib_bihash_kv_16_8_t kv16, value16;
2625
2626   u32 fib_index = rx_fib_index;
2627   if (b)
2628     {
2629       if (PREDICT_FALSE (is_output))
2630         {
2631           fib_index = sm->outside_fib_index;
2632           nat_outside_fib_t *outside_fib;
2633           fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
2634           fib_prefix_t pfx = {
2635                   .fp_proto = FIB_PROTOCOL_IP4,
2636                   .fp_len = 32,
2637                   .fp_addr = {
2638                           .ip4.as_u32 = ip->dst_address.as_u32,
2639                   } ,
2640           };
2641
2642           switch (vec_len (sm->outside_fibs))
2643             {
2644             case 0:
2645               fib_index = sm->outside_fib_index;
2646               break;
2647             case 1:
2648               fib_index = sm->outside_fibs[0].fib_index;
2649               break;
2650             default:
2651               vec_foreach (outside_fib, sm->outside_fibs)
2652                 {
2653                   fei = fib_table_lookup (outside_fib->fib_index, &pfx);
2654                   if (FIB_NODE_INDEX_INVALID != fei)
2655                     {
2656                       if (fib_entry_get_resolving_interface (fei) != ~0)
2657                         {
2658                           fib_index = outside_fib->fib_index;
2659                           break;
2660                         }
2661                     }
2662                 }
2663               break;
2664             }
2665         }
2666
2667       if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
2668         {
2669           ip4_address_t lookup_saddr, lookup_daddr;
2670           u16 lookup_sport, lookup_dport;
2671           u8 lookup_protocol;
2672
2673           if (!nat_get_icmp_session_lookup_values (
2674                 b, ip, &lookup_saddr, &lookup_sport, &lookup_daddr,
2675                 &lookup_dport, &lookup_protocol))
2676             {
2677               init_ed_k (&kv16, lookup_saddr.as_u32, lookup_sport,
2678                          lookup_daddr.as_u32, lookup_dport, rx_fib_index,
2679                          lookup_protocol);
2680               if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16))
2681                 {
2682                   next_worker_index = ed_value_get_thread_index (&value16);
2683                   vnet_buffer2 (b)->nat.cached_session_index =
2684                     ed_value_get_session_index (&value16);
2685                   goto out;
2686                 }
2687             }
2688         }
2689
2690       init_ed_k (&kv16, ip->src_address.as_u32,
2691                  vnet_buffer (b)->ip.reass.l4_src_port, ip->dst_address.as_u32,
2692                  vnet_buffer (b)->ip.reass.l4_dst_port, fib_index,
2693                  ip->protocol);
2694
2695       if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16))
2696         {
2697           next_worker_index = ed_value_get_thread_index (&value16);
2698           vnet_buffer2 (b)->nat.cached_session_index =
2699             ed_value_get_session_index (&value16);
2700           goto out;
2701         }
2702
2703       // dst NAT
2704       init_ed_k (&kv16, ip->dst_address.as_u32,
2705                  vnet_buffer (b)->ip.reass.l4_dst_port, ip->src_address.as_u32,
2706                  vnet_buffer (b)->ip.reass.l4_src_port, rx_fib_index,
2707                  ip->protocol);
2708       if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16))
2709         {
2710           next_worker_index = ed_value_get_thread_index (&value16);
2711           vnet_buffer2 (b)->nat.cached_dst_nat_session_index =
2712             ed_value_get_session_index (&value16);
2713           goto out;
2714         }
2715     }
2716
2717   hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
2718     (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
2719
2720   if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2721     next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2722   else
2723     next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2724
2725 out:
2726   if (PREDICT_TRUE (!is_output))
2727     {
2728       nat_elog_debug_handoff (sm, "HANDOFF IN2OUT", next_worker_index,
2729                               rx_fib_index,
2730                               clib_net_to_host_u32 (ip->src_address.as_u32),
2731                               clib_net_to_host_u32 (ip->dst_address.as_u32));
2732     }
2733   else
2734     {
2735       nat_elog_debug_handoff (sm, "HANDOFF IN2OUT-OUTPUT-FEATURE",
2736                               next_worker_index, rx_fib_index,
2737                               clib_net_to_host_u32 (ip->src_address.as_u32),
2738                               clib_net_to_host_u32 (ip->dst_address.as_u32));
2739     }
2740
2741   return next_worker_index;
2742 }
2743
2744 u32
2745 nat44_ed_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
2746                                   u32 rx_fib_index, u8 is_output)
2747 {
2748   snat_main_t *sm = &snat_main;
2749   clib_bihash_kv_16_8_t kv16, value16;
2750
2751   u8 proto, next_worker_index = 0;
2752   u16 port;
2753   snat_static_mapping_t *m;
2754   u32 hash;
2755
2756   proto = ip->protocol;
2757
2758   if (PREDICT_FALSE (IP_PROTOCOL_ICMP == proto))
2759     {
2760       ip4_address_t lookup_saddr, lookup_daddr;
2761       u16 lookup_sport, lookup_dport;
2762       u8 lookup_protocol;
2763       if (!nat_get_icmp_session_lookup_values (
2764             b, ip, &lookup_saddr, &lookup_sport, &lookup_daddr, &lookup_dport,
2765             &lookup_protocol))
2766         {
2767           init_ed_k (&kv16, lookup_saddr.as_u32, lookup_sport,
2768                      lookup_daddr.as_u32, lookup_dport, rx_fib_index,
2769                      lookup_protocol);
2770           if (PREDICT_TRUE (
2771                 !clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16)))
2772             {
2773               next_worker_index = ed_value_get_thread_index (&value16);
2774               nat_elog_debug_handoff (
2775                 sm, "HANDOFF OUT2IN (session)", next_worker_index,
2776                 rx_fib_index, clib_net_to_host_u32 (ip->src_address.as_u32),
2777                 clib_net_to_host_u32 (ip->dst_address.as_u32));
2778               return next_worker_index;
2779             }
2780         }
2781     }
2782
2783   init_ed_k (&kv16, ip->src_address.as_u32,
2784              vnet_buffer (b)->ip.reass.l4_src_port, ip->dst_address.as_u32,
2785              vnet_buffer (b)->ip.reass.l4_dst_port, rx_fib_index,
2786              ip->protocol);
2787
2788   if (PREDICT_TRUE (
2789         !clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16)))
2790     {
2791       vnet_buffer2 (b)->nat.cached_session_index =
2792         ed_value_get_session_index (&value16);
2793       next_worker_index = ed_value_get_thread_index (&value16);
2794       nat_elog_debug_handoff (sm, "HANDOFF OUT2IN (session)",
2795                               next_worker_index, rx_fib_index,
2796                               clib_net_to_host_u32 (ip->src_address.as_u32),
2797                               clib_net_to_host_u32 (ip->dst_address.as_u32));
2798       return next_worker_index;
2799     }
2800
2801   /* first try static mappings without port */
2802   if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2803     {
2804       m = nat44_ed_sm_o2i_lookup (sm, ip->dst_address, 0, 0, proto);
2805       if (m)
2806         {
2807           {
2808             next_worker_index = m->workers[0];
2809             goto done;
2810           }
2811         }
2812     }
2813
2814   /* unknown protocol */
2815   if (PREDICT_FALSE (nat44_ed_is_unk_proto (proto)))
2816     {
2817       /* use current thread */
2818       next_worker_index = vlib_get_thread_index ();
2819       goto done;
2820     }
2821
2822   port = vnet_buffer (b)->ip.reass.l4_dst_port;
2823
2824   if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
2825     {
2826       udp_header_t *udp = ip4_next_header (ip);
2827       icmp46_header_t *icmp = (icmp46_header_t *) udp;
2828       icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
2829       if (!icmp_type_is_error_message
2830           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
2831         port = vnet_buffer (b)->ip.reass.l4_src_port;
2832       else
2833         {
2834           /* if error message, then it's not fragmented and we can access it */
2835           ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
2836           proto = inner_ip->protocol;
2837           void *l4_header = ip4_next_header (inner_ip);
2838           switch (proto)
2839             {
2840             case IP_PROTOCOL_ICMP:
2841               icmp = (icmp46_header_t *) l4_header;
2842               echo = (icmp_echo_header_t *) (icmp + 1);
2843               port = echo->identifier;
2844               break;
2845             case IP_PROTOCOL_UDP:
2846               /* breakthrough */
2847             case IP_PROTOCOL_TCP:
2848               port = ((tcp_udp_header_t *) l4_header)->src_port;
2849               break;
2850             default:
2851               next_worker_index = vlib_get_thread_index ();
2852               goto done;
2853             }
2854         }
2855     }
2856
2857   /* try static mappings with port */
2858   if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2859     {
2860       m = nat44_ed_sm_o2i_lookup (sm, ip->dst_address, port, 0, proto);
2861       if (m)
2862         {
2863           if (!is_sm_lb (m->flags))
2864             {
2865               next_worker_index = m->workers[0];
2866               goto done;
2867             }
2868
2869           hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
2870             (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
2871
2872           if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
2873             next_worker_index =
2874               m->workers[hash & (_vec_len (m->workers) - 1)];
2875           else
2876             next_worker_index = m->workers[hash % _vec_len (m->workers)];
2877           goto done;
2878         }
2879     }
2880
2881   /* worker by outside port */
2882   next_worker_index = sm->first_worker_index;
2883   next_worker_index +=
2884     sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2885
2886 done:
2887   nat_elog_debug_handoff (sm, "HANDOFF OUT2IN", next_worker_index,
2888                           rx_fib_index,
2889                           clib_net_to_host_u32 (ip->src_address.as_u32),
2890                           clib_net_to_host_u32 (ip->dst_address.as_u32));
2891   return next_worker_index;
2892 }
2893
2894 u32
2895 nat44_get_max_session_limit ()
2896 {
2897   snat_main_t *sm = &snat_main;
2898   u32 max_limit = 0, len = 0;
2899
2900   for (; len < vec_len (sm->max_translations_per_fib); len++)
2901     {
2902       if (max_limit < sm->max_translations_per_fib[len])
2903         max_limit = sm->max_translations_per_fib[len];
2904     }
2905   return max_limit;
2906 }
2907
2908 int
2909 nat44_set_session_limit (u32 session_limit, u32 vrf_id)
2910 {
2911   snat_main_t *sm = &snat_main;
2912   u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
2913   u32 len = vec_len (sm->max_translations_per_fib);
2914
2915   if (len <= fib_index)
2916     {
2917       vec_validate (sm->max_translations_per_fib, fib_index + 1);
2918
2919       for (; len < vec_len (sm->max_translations_per_fib); len++)
2920         sm->max_translations_per_fib[len] = sm->max_translations_per_thread;
2921     }
2922
2923   sm->max_translations_per_fib[fib_index] = session_limit;
2924   return 0;
2925 }
2926
2927 int
2928 nat44_update_session_limit (u32 session_limit, u32 vrf_id)
2929 {
2930   snat_main_t *sm = &snat_main;
2931
2932   if (nat44_set_session_limit (session_limit, vrf_id))
2933     return 1;
2934   sm->max_translations_per_thread = nat44_get_max_session_limit ();
2935
2936   stat_segment_set_state_counter (sm->max_cfg_sessions_gauge,
2937                                   sm->max_translations_per_thread);
2938
2939   sm->translation_buckets =
2940     nat_calc_bihash_buckets (sm->max_translations_per_thread);
2941
2942   nat44_ed_sessions_clear ();
2943   return 0;
2944 }
2945
2946 static void
2947 nat44_ed_worker_db_init (snat_main_per_thread_data_t *tsm, u32 translations,
2948                          u32 translation_buckets)
2949 {
2950   dlist_elt_t *head;
2951
2952   pool_alloc (tsm->sessions, translations);
2953   pool_alloc (tsm->lru_pool, translations);
2954
2955   pool_get (tsm->lru_pool, head);
2956   tsm->tcp_trans_lru_head_index = head - tsm->lru_pool;
2957   clib_dlist_init (tsm->lru_pool, tsm->tcp_trans_lru_head_index);
2958
2959   pool_get (tsm->lru_pool, head);
2960   tsm->tcp_estab_lru_head_index = head - tsm->lru_pool;
2961   clib_dlist_init (tsm->lru_pool, tsm->tcp_estab_lru_head_index);
2962
2963   pool_get (tsm->lru_pool, head);
2964   tsm->udp_lru_head_index = head - tsm->lru_pool;
2965   clib_dlist_init (tsm->lru_pool, tsm->udp_lru_head_index);
2966
2967   pool_get (tsm->lru_pool, head);
2968   tsm->icmp_lru_head_index = head - tsm->lru_pool;
2969   clib_dlist_init (tsm->lru_pool, tsm->icmp_lru_head_index);
2970
2971   pool_get (tsm->lru_pool, head);
2972   tsm->unk_proto_lru_head_index = head - tsm->lru_pool;
2973   clib_dlist_init (tsm->lru_pool, tsm->unk_proto_lru_head_index);
2974 }
2975
2976 static void
2977 reinit_ed_flow_hash ()
2978 {
2979   snat_main_t *sm = &snat_main;
2980   // we expect 2 flows per session, so multiply translation_buckets by 2
2981   clib_bihash_init_16_8 (
2982     &sm->flow_hash, "ed-flow-hash",
2983     clib_max (1, sm->num_workers) * 2 * sm->translation_buckets, 0);
2984   clib_bihash_set_kvp_format_fn_16_8 (&sm->flow_hash, format_ed_session_kvp);
2985 }
2986
2987 static void
2988 nat44_ed_db_init (u32 translations, u32 translation_buckets)
2989 {
2990   snat_main_t *sm = &snat_main;
2991   snat_main_per_thread_data_t *tsm;
2992
2993   reinit_ed_flow_hash ();
2994
2995   if (sm->pat)
2996     {
2997       vec_foreach (tsm, sm->per_thread_data)
2998         {
2999           nat44_ed_worker_db_init (tsm, sm->max_translations_per_thread,
3000                                    sm->translation_buckets);
3001         }
3002     }
3003 }
3004
3005 static void
3006 nat44_ed_worker_db_free (snat_main_per_thread_data_t *tsm)
3007 {
3008   pool_free (tsm->lru_pool);
3009   pool_free (tsm->sessions);
3010   vec_free (tsm->per_vrf_sessions_vec);
3011 }
3012
3013 static void
3014 nat44_ed_db_free ()
3015 {
3016   snat_main_t *sm = &snat_main;
3017   snat_main_per_thread_data_t *tsm;
3018
3019   pool_free (sm->static_mappings);
3020   clib_bihash_free_16_8 (&sm->flow_hash);
3021
3022   if (sm->pat)
3023     {
3024       vec_foreach (tsm, sm->per_thread_data)
3025         {
3026           nat44_ed_worker_db_free (tsm);
3027         }
3028     }
3029 }
3030
3031 void
3032 nat44_ed_sessions_clear ()
3033 {
3034   snat_main_t *sm = &snat_main;
3035   snat_main_per_thread_data_t *tsm;
3036
3037   reinit_ed_flow_hash ();
3038
3039   if (sm->pat)
3040     {
3041       vec_foreach (tsm, sm->per_thread_data)
3042         {
3043
3044           nat44_ed_worker_db_free (tsm);
3045           nat44_ed_worker_db_init (tsm, sm->max_translations_per_thread,
3046                                    sm->translation_buckets);
3047         }
3048     }
3049   vlib_zero_simple_counter (&sm->total_sessions, 0);
3050 }
3051
3052 static void
3053 nat44_ed_add_del_static_mapping_addr_only_cb (
3054   ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
3055   u32 address_length, u32 if_address_index, u32 is_delete)
3056 {
3057   snat_static_map_resolve_t *rp;
3058   snat_main_t *sm = &snat_main;
3059   snat_static_mapping_t *m;
3060   int i, rv = 0, match = 0;
3061
3062   if (!sm->enabled)
3063     {
3064       return;
3065     }
3066
3067   // find first addr_only resolve record by sw_if_index
3068   for (i = 0; i < vec_len (sm->to_resolve); i++)
3069     {
3070       rp = sm->to_resolve + i;
3071       if (rp->addr_only && rp->sw_if_index == sw_if_index)
3072         {
3073           match = 1;
3074           break;
3075         }
3076     }
3077   if (!match)
3078     {
3079       return;
3080     }
3081
3082   m = nat44_ed_sm_o2i_lookup (sm, *address, rp->addr_only ? 0 : rp->e_port, 0,
3083                               rp->proto);
3084
3085   if (is_delete)
3086     {
3087       if (m)
3088         {
3089           rv = nat44_ed_del_static_mapping (rp->l_addr, address[0], rp->l_port,
3090                                             rp->e_port, rp->proto, rp->vrf_id,
3091                                             ~0, rp->flags);
3092         }
3093     }
3094   else
3095     {
3096       if (!m)
3097         {
3098           rv = nat44_ed_add_static_mapping (
3099             rp->l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
3100             rp->vrf_id, ~0, rp->flags, rp->pool_addr, rp->tag);
3101         }
3102       // else: don't trip over lease renewal, static config
3103     }
3104   if (rv)
3105     {
3106       nat_elog_notice_X1 (sm, "nat44_ed_del_static_mapping returned %d", "i4",
3107                           rv);
3108     }
3109 }
3110
3111 static_always_inline int
3112 is_sw_if_index_reg_for_auto_resolve (u32 *sw_if_indices, u32 sw_if_index)
3113 {
3114   u32 *i;
3115   vec_foreach (i, sw_if_indices)
3116     {
3117       if (*i == sw_if_index)
3118         {
3119           return 1;
3120         }
3121     }
3122   return 0;
3123 }
3124
3125 static void
3126 nat44_ed_add_del_interface_address_cb (ip4_main_t *im, uword opaque,
3127                                        u32 sw_if_index, ip4_address_t *address,
3128                                        u32 address_length,
3129                                        u32 if_address_index, u32 is_delete)
3130 {
3131   snat_main_t *sm = &snat_main;
3132   snat_static_map_resolve_t *rp;
3133   snat_address_t *addresses = sm->addresses;
3134   u8 twice_nat = 0;
3135   int rv, i;
3136
3137   if (!sm->enabled)
3138     {
3139       return;
3140     }
3141
3142   if (!is_sw_if_index_reg_for_auto_resolve (sm->auto_add_sw_if_indices,
3143                                             sw_if_index))
3144     {
3145       if (!is_sw_if_index_reg_for_auto_resolve (
3146             sm->auto_add_sw_if_indices_twice_nat, sw_if_index))
3147         {
3148           return;
3149         }
3150       else
3151         {
3152           addresses = sm->twice_nat_addresses;
3153           twice_nat = 1;
3154         }
3155     }
3156
3157   if (!is_delete)
3158     {
3159       // don't trip over lease renewal, static config
3160       for (i = 0; i < vec_len (addresses); i++)
3161         {
3162           if (addresses[i].addr.as_u32 == address->as_u32)
3163             {
3164               return;
3165             }
3166         }
3167
3168       (void) nat44_ed_add_address (address, ~0, twice_nat);
3169
3170       // scan static mapping switch address resolution record vector
3171       for (i = 0; i < vec_len (sm->to_resolve); i++)
3172         {
3173           rp = sm->to_resolve + i;
3174           if (rp->addr_only)
3175             {
3176               continue;
3177             }
3178           if (rp->sw_if_index == sw_if_index)
3179             {
3180               rv = nat44_ed_add_static_mapping (
3181                 rp->l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
3182                 rp->vrf_id, sw_if_index, rp->flags, rp->pool_addr, rp->tag);
3183               if (rv)
3184                 {
3185                   nat_elog_notice_X1 (sm, "add_static_mapping returned %d",
3186                                       "i4", rv);
3187                 }
3188             }
3189         }
3190     }
3191   else
3192     {
3193       // remove all static mapping records
3194       (void) nat44_ed_del_address (address[0], 1, twice_nat);
3195     }
3196 }
3197
3198 int
3199 nat44_ed_add_interface_address (u32 sw_if_index, u8 twice_nat)
3200 {
3201   snat_main_t *sm = &snat_main;
3202   ip4_main_t *ip4_main = sm->ip4_main;
3203   ip4_address_t *first_int_addr;
3204   u32 *auto_add_sw_if_indices = twice_nat ?
3205                                   sm->auto_add_sw_if_indices_twice_nat :
3206                                   sm->auto_add_sw_if_indices;
3207   int i;
3208
3209   for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3210     {
3211       if (auto_add_sw_if_indices[i] == sw_if_index)
3212         {
3213           return VNET_API_ERROR_VALUE_EXIST;
3214         }
3215     }
3216
3217   // add to the auto-address list
3218   if (twice_nat)
3219     {
3220       vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3221     }
3222   else
3223     {
3224       vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
3225     }
3226
3227   // if the address is already bound - or static - add it now
3228   first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
3229   if (first_int_addr)
3230     {
3231       (void) nat44_ed_add_address (first_int_addr, ~0, twice_nat);
3232     }
3233
3234   return 0;
3235 }
3236
3237 int
3238 nat44_ed_del_interface_address (u32 sw_if_index, u8 twice_nat)
3239 {
3240   snat_main_t *sm = &snat_main;
3241   ip4_main_t *ip4_main = sm->ip4_main;
3242   ip4_address_t *first_int_addr;
3243   snat_static_map_resolve_t *rp;
3244   u32 *indices_to_delete = 0;
3245   int i, j;
3246   u32 *auto_add_sw_if_indices;
3247
3248   if (!sm->enabled)
3249     {
3250       return VNET_API_ERROR_UNSUPPORTED;
3251     }
3252
3253   auto_add_sw_if_indices = twice_nat ? sm->auto_add_sw_if_indices_twice_nat :
3254                                        sm->auto_add_sw_if_indices;
3255
3256   for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3257     {
3258       if (auto_add_sw_if_indices[i] == sw_if_index)
3259         {
3260           first_int_addr =
3261             ip4_interface_first_address (ip4_main, sw_if_index, 0);
3262           if (first_int_addr)
3263             {
3264               // remove all static mapping records
3265               (void) nat44_ed_del_address (first_int_addr[0], 1, twice_nat);
3266             }
3267           else
3268             {
3269               for (j = 0; j < vec_len (sm->to_resolve); j++)
3270                 {
3271                   rp = sm->to_resolve + j;
3272                   if (rp->sw_if_index == sw_if_index)
3273                     {
3274                       vec_add1 (indices_to_delete, j);
3275                     }
3276                 }
3277               if (vec_len (indices_to_delete))
3278                 {
3279                   for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
3280                     {
3281                       vec_del1 (sm->to_resolve, j);
3282                     }
3283                   vec_free (indices_to_delete);
3284                 }
3285             }
3286
3287           if (twice_nat)
3288             {
3289               vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
3290             }
3291           else
3292             {
3293               vec_del1 (sm->auto_add_sw_if_indices, i);
3294             }
3295           return 0;
3296         }
3297     }
3298   return VNET_API_ERROR_NO_SUCH_ENTRY;
3299 }
3300
3301 int
3302 nat44_ed_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
3303                       ip4_address_t *eh_addr, u16 eh_port, u8 proto,
3304                       u32 vrf_id, int is_in)
3305 {
3306   ip4_header_t ip;
3307   clib_bihash_kv_16_8_t kv, value;
3308   u32 fib_index;
3309   snat_session_t *s;
3310   snat_main_per_thread_data_t *tsm;
3311
3312   if (!sm->enabled)
3313     {
3314       return VNET_API_ERROR_UNSUPPORTED;
3315     }
3316
3317   fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3318   ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3319   if (sm->num_workers > 1)
3320     tsm = vec_elt_at_index (
3321       sm->per_thread_data,
3322       nat44_ed_get_in2out_worker_index (0, &ip, fib_index, 0));
3323   else
3324     tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3325
3326   init_ed_k (&kv, addr->as_u32, port, eh_addr->as_u32, eh_port, fib_index,
3327              proto);
3328   if (clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
3329     {
3330       return VNET_API_ERROR_NO_SUCH_ENTRY;
3331     }
3332
3333   if (pool_is_free_index (tsm->sessions, ed_value_get_session_index (&value)))
3334     return VNET_API_ERROR_UNSPECIFIED;
3335   s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
3336   nat44_ed_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
3337   nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
3338   return 0;
3339 }
3340
3341 VLIB_NODE_FN (nat_default_node) (vlib_main_t * vm,
3342                                  vlib_node_runtime_t * node,
3343                                  vlib_frame_t * frame)
3344 {
3345   return 0;
3346 }
3347
3348 VLIB_REGISTER_NODE (nat_default_node) = {
3349   .name = "nat-default",
3350   .vector_size = sizeof (u32),
3351   .format_trace = 0,
3352   .type = VLIB_NODE_TYPE_INTERNAL,
3353   .n_errors = 0,
3354   .n_next_nodes = NAT_N_NEXT,
3355   .next_nodes = {
3356     [NAT_NEXT_DROP] = "error-drop",
3357     [NAT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3358     [NAT_NEXT_IN2OUT_ED_FAST_PATH] = "nat44-ed-in2out",
3359     [NAT_NEXT_IN2OUT_ED_SLOW_PATH] = "nat44-ed-in2out-slowpath",
3360     [NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH] = "nat44-ed-in2out-output",
3361     [NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
3362     [NAT_NEXT_OUT2IN_ED_FAST_PATH] = "nat44-ed-out2in",
3363     [NAT_NEXT_OUT2IN_ED_SLOW_PATH] = "nat44-ed-out2in-slowpath",
3364     [NAT_NEXT_IN2OUT_CLASSIFY] = "nat44-in2out-worker-handoff",
3365     [NAT_NEXT_OUT2IN_CLASSIFY] = "nat44-out2in-worker-handoff",
3366   },
3367 };
3368
3369 void
3370 nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f)
3371 {
3372   f->l3_csum_delta = 0;
3373   f->l4_csum_delta = 0;
3374   if (f->ops & NAT_FLOW_OP_SADDR_REWRITE &&
3375       f->rewrite.saddr.as_u32 != f->match.saddr.as_u32)
3376     {
3377       f->l3_csum_delta =
3378         ip_csum_add_even (f->l3_csum_delta, f->rewrite.saddr.as_u32);
3379       f->l3_csum_delta =
3380         ip_csum_sub_even (f->l3_csum_delta, f->match.saddr.as_u32);
3381     }
3382   else
3383     {
3384       f->rewrite.saddr.as_u32 = f->match.saddr.as_u32;
3385     }
3386   if (f->ops & NAT_FLOW_OP_DADDR_REWRITE &&
3387       f->rewrite.daddr.as_u32 != f->match.daddr.as_u32)
3388     {
3389       f->l3_csum_delta =
3390         ip_csum_add_even (f->l3_csum_delta, f->rewrite.daddr.as_u32);
3391       f->l3_csum_delta =
3392         ip_csum_sub_even (f->l3_csum_delta, f->match.daddr.as_u32);
3393     }
3394   else
3395     {
3396       f->rewrite.daddr.as_u32 = f->match.daddr.as_u32;
3397     }
3398   if (f->ops & NAT_FLOW_OP_SPORT_REWRITE && f->rewrite.sport != f->match.sport)
3399     {
3400       f->l4_csum_delta = ip_csum_add_even (f->l4_csum_delta, f->rewrite.sport);
3401       f->l4_csum_delta = ip_csum_sub_even (f->l4_csum_delta, f->match.sport);
3402     }
3403   else
3404     {
3405       f->rewrite.sport = f->match.sport;
3406     }
3407   if (f->ops & NAT_FLOW_OP_DPORT_REWRITE && f->rewrite.dport != f->match.dport)
3408     {
3409       f->l4_csum_delta = ip_csum_add_even (f->l4_csum_delta, f->rewrite.dport);
3410       f->l4_csum_delta = ip_csum_sub_even (f->l4_csum_delta, f->match.dport);
3411     }
3412   else
3413     {
3414       f->rewrite.dport = f->match.dport;
3415     }
3416   if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE &&
3417       f->rewrite.icmp_id != f->match.sport)
3418     {
3419       f->l4_csum_delta =
3420         ip_csum_add_even (f->l4_csum_delta, f->rewrite.icmp_id);
3421       f->l4_csum_delta = ip_csum_sub_even (f->l4_csum_delta, f->match.sport);
3422     }
3423   else
3424     {
3425       f->rewrite.icmp_id = f->match.sport;
3426     }
3427   if (f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
3428     {
3429     }
3430   else
3431     {
3432       f->rewrite.fib_index = f->match.fib_index;
3433     }
3434 }
3435
3436 static_always_inline int
3437 nat_6t_flow_icmp_translate (vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b,
3438                             ip4_header_t *ip, nat_6t_flow_t *f);
3439
3440 static_always_inline void
3441 nat_6t_flow_ip4_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
3442                            nat_6t_flow_t *f, ip_protocol_t proto,
3443                            int is_icmp_inner_ip4, int skip_saddr_rewrite)
3444 {
3445   udp_header_t *udp = ip4_next_header (ip);
3446   tcp_header_t *tcp = (tcp_header_t *) udp;
3447
3448   if ((IP_PROTOCOL_TCP == proto || IP_PROTOCOL_UDP == proto) &&
3449       !vnet_buffer (b)->ip.reass.is_non_first_fragment)
3450     {
3451       if (!is_icmp_inner_ip4)
3452         { // regular case
3453           ip->src_address = f->rewrite.saddr;
3454           ip->dst_address = f->rewrite.daddr;
3455           udp->src_port = f->rewrite.sport;
3456           udp->dst_port = f->rewrite.dport;
3457         }
3458       else
3459         { // icmp inner ip4 - reversed saddr/daddr
3460           ip->src_address = f->rewrite.daddr;
3461           ip->dst_address = f->rewrite.saddr;
3462           udp->src_port = f->rewrite.dport;
3463           udp->dst_port = f->rewrite.sport;
3464         }
3465
3466       if (IP_PROTOCOL_TCP == proto)
3467         {
3468           ip_csum_t tcp_sum = tcp->checksum;
3469           tcp_sum = ip_csum_sub_even (tcp_sum, f->l3_csum_delta);
3470           tcp_sum = ip_csum_sub_even (tcp_sum, f->l4_csum_delta);
3471           mss_clamping (sm->mss_clamping, tcp, &tcp_sum);
3472           tcp->checksum = ip_csum_fold (tcp_sum);
3473         }
3474       else if (IP_PROTOCOL_UDP == proto && udp->checksum)
3475         {
3476           ip_csum_t udp_sum = udp->checksum;
3477           udp_sum = ip_csum_sub_even (udp_sum, f->l3_csum_delta);
3478           udp_sum = ip_csum_sub_even (udp_sum, f->l4_csum_delta);
3479           udp->checksum = ip_csum_fold (udp_sum);
3480         }
3481     }
3482   else
3483     {
3484       if (!is_icmp_inner_ip4)
3485         { // regular case
3486           if (!skip_saddr_rewrite)
3487             {
3488               ip->src_address = f->rewrite.saddr;
3489             }
3490           ip->dst_address = f->rewrite.daddr;
3491         }
3492       else
3493         { // icmp inner ip4 - reversed saddr/daddr
3494           ip->src_address = f->rewrite.daddr;
3495           ip->dst_address = f->rewrite.saddr;
3496         }
3497     }
3498
3499   if (skip_saddr_rewrite)
3500     {
3501       ip->checksum = ip4_header_checksum (ip);
3502     }
3503   else
3504     {
3505       ip_csum_t ip_sum = ip->checksum;
3506       ip_sum = ip_csum_sub_even (ip_sum, f->l3_csum_delta);
3507       ip->checksum = ip_csum_fold (ip_sum);
3508     }
3509   if (0xffff == ip->checksum)
3510     ip->checksum = 0;
3511   ASSERT (ip4_header_checksum_is_valid (ip));
3512 }
3513
3514 static_always_inline int
3515 it_fits (vlib_main_t *vm, vlib_buffer_t *b, void *object, size_t size)
3516 {
3517   int result = ((u8 *) object + size <=
3518                 (u8 *) vlib_buffer_get_current (b) + b->current_length) &&
3519                vlib_object_within_buffer_data (vm, b, object, size);
3520   return result;
3521 }
3522
3523 static_always_inline int
3524 nat_6t_flow_icmp_translate (vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b,
3525                             ip4_header_t *ip, nat_6t_flow_t *f)
3526 {
3527   if (IP_PROTOCOL_ICMP != ip->protocol)
3528     return NAT_ED_TRNSL_ERR_TRANSLATION_FAILED;
3529
3530   icmp46_header_t *icmp = ip4_next_header (ip);
3531   icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3532
3533   if ((!vnet_buffer (b)->ip.reass.is_non_first_fragment))
3534     {
3535       if (!it_fits (vm, b, icmp, sizeof (*icmp)))
3536         {
3537           return NAT_ED_TRNSL_ERR_PACKET_TRUNCATED;
3538         }
3539
3540       if (!icmp_type_is_error_message (icmp->type))
3541         {
3542           if ((f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE) &&
3543               (f->rewrite.icmp_id != echo->identifier))
3544             {
3545               ip_csum_t sum = icmp->checksum;
3546               sum = ip_csum_update (sum, echo->identifier, f->rewrite.icmp_id,
3547                                     icmp_echo_header_t,
3548                                     identifier /* changed member */);
3549               echo->identifier = f->rewrite.icmp_id;
3550               icmp->checksum = ip_csum_fold (sum);
3551             }
3552         }
3553       else
3554         {
3555           ip_csum_t sum = ip_incremental_checksum (
3556             0, icmp,
3557             clib_net_to_host_u16 (ip->length) - ip4_header_bytes (ip));
3558           sum = (u16) ~ip_csum_fold (sum);
3559           if (sum != 0)
3560             {
3561               return NAT_ED_TRNSL_ERR_INVALID_CSUM;
3562             }
3563
3564           // errors are not fragmented
3565           ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3566
3567           if (!ip4_header_checksum_is_valid (inner_ip))
3568             {
3569               return NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT;
3570             }
3571
3572           ip_protocol_t inner_proto = inner_ip->protocol;
3573
3574           ip_csum_t old_icmp_sum = icmp->checksum;
3575           ip_csum_t old_inner_ip_sum = inner_ip->checksum;
3576           ip_csum_t old_udp_sum;
3577           ip_csum_t old_tcp_sum;
3578           ip_csum_t new_icmp_sum;
3579           udp_header_t *udp;
3580           tcp_header_t *tcp;
3581
3582           switch (inner_proto)
3583             {
3584             case IP_PROTOCOL_UDP:
3585               udp = (udp_header_t *) (inner_ip + 1);
3586               if (!it_fits (vm, b, udp, sizeof (*udp)))
3587                 {
3588                   return NAT_ED_TRNSL_ERR_PACKET_TRUNCATED;
3589                 }
3590               old_udp_sum = udp->checksum;
3591               nat_6t_flow_ip4_translate (sm, b, inner_ip, f, inner_proto,
3592                                          1 /* is_icmp_inner_ip4 */,
3593                                          0 /* skip_saddr_rewrite */);
3594               new_icmp_sum = ip_csum_sub_even (old_icmp_sum, f->l3_csum_delta);
3595               new_icmp_sum = ip_csum_sub_even (new_icmp_sum, f->l4_csum_delta);
3596               new_icmp_sum =
3597                 ip_csum_update (new_icmp_sum, old_inner_ip_sum,
3598                                 inner_ip->checksum, ip4_header_t, checksum);
3599               new_icmp_sum =
3600                 ip_csum_update (new_icmp_sum, old_udp_sum, udp->checksum,
3601                                 udp_header_t, checksum);
3602               new_icmp_sum = ip_csum_fold (new_icmp_sum);
3603               icmp->checksum = new_icmp_sum;
3604               break;
3605             case IP_PROTOCOL_TCP:
3606               tcp = (tcp_header_t *) (inner_ip + 1);
3607               if (!it_fits (vm, b, tcp, sizeof (*tcp)))
3608                 {
3609                   return NAT_ED_TRNSL_ERR_PACKET_TRUNCATED;
3610                 }
3611               old_tcp_sum = tcp->checksum;
3612               nat_6t_flow_ip4_translate (sm, b, inner_ip, f, inner_proto,
3613                                          1 /* is_icmp_inner_ip4 */,
3614                                          0 /* skip_saddr_rewrite */);
3615               new_icmp_sum = ip_csum_sub_even (old_icmp_sum, f->l3_csum_delta);
3616               new_icmp_sum = ip_csum_sub_even (new_icmp_sum, f->l4_csum_delta);
3617               new_icmp_sum =
3618                 ip_csum_update (new_icmp_sum, old_inner_ip_sum,
3619                                 inner_ip->checksum, ip4_header_t, checksum);
3620               new_icmp_sum =
3621                 ip_csum_update (new_icmp_sum, old_tcp_sum, tcp->checksum,
3622                                 tcp_header_t, checksum);
3623               new_icmp_sum = ip_csum_fold (new_icmp_sum);
3624               icmp->checksum = new_icmp_sum;
3625               break;
3626             case IP_PROTOCOL_ICMP:
3627               if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE)
3628                 {
3629                   icmp46_header_t *inner_icmp = ip4_next_header (inner_ip);
3630                   if (!it_fits (vm, b, inner_icmp, sizeof (*inner_icmp)))
3631                     {
3632                       return NAT_ED_TRNSL_ERR_PACKET_TRUNCATED;
3633                     }
3634                   icmp_echo_header_t *inner_echo =
3635                     (icmp_echo_header_t *) (inner_icmp + 1);
3636                   if (f->rewrite.icmp_id != inner_echo->identifier)
3637                     {
3638                       ip_csum_t sum = icmp->checksum;
3639                       sum = ip_csum_update (
3640                         sum, inner_echo->identifier, f->rewrite.icmp_id,
3641                         icmp_echo_header_t, identifier /* changed member */);
3642                       icmp->checksum = ip_csum_fold (sum);
3643                       ip_csum_t inner_sum = inner_icmp->checksum;
3644                       inner_sum = ip_csum_update (
3645                         sum, inner_echo->identifier, f->rewrite.icmp_id,
3646                         icmp_echo_header_t, identifier /* changed member */);
3647                       inner_icmp->checksum = ip_csum_fold (inner_sum);
3648                       inner_echo->identifier = f->rewrite.icmp_id;
3649                     }
3650                 }
3651               break;
3652             default:
3653               clib_warning ("unexpected NAT protocol value `%d'", inner_proto);
3654               return NAT_ED_TRNSL_ERR_TRANSLATION_FAILED;
3655             }
3656         }
3657     }
3658
3659   return NAT_ED_TRNSL_ERR_SUCCESS;
3660 }
3661
3662 static_always_inline nat_translation_error_e
3663 nat_6t_flow_buf_translate (vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b,
3664                            ip4_header_t *ip, nat_6t_flow_t *f,
3665                            ip_protocol_t proto, int is_output_feature,
3666                            int is_i2o)
3667 {
3668   if (!is_output_feature && f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
3669     {
3670       vnet_buffer (b)->sw_if_index[VLIB_TX] = f->rewrite.fib_index;
3671     }
3672
3673   if (IP_PROTOCOL_ICMP == proto)
3674     {
3675       if (ip->src_address.as_u32 != f->rewrite.saddr.as_u32)
3676         {
3677           // packet is returned from a router, not from destination
3678           // skip source address rewrite if in o2i path
3679           nat_6t_flow_ip4_translate (sm, b, ip, f, proto,
3680                                      0 /* is_icmp_inner_ip4 */,
3681                                      !is_i2o /* skip_saddr_rewrite */);
3682         }
3683       else
3684         {
3685           nat_6t_flow_ip4_translate (sm, b, ip, f, proto,
3686                                      0 /* is_icmp_inner_ip4 */,
3687                                      0 /* skip_saddr_rewrite */);
3688         }
3689       return nat_6t_flow_icmp_translate (vm, sm, b, ip, f);
3690     }
3691
3692   nat_6t_flow_ip4_translate (sm, b, ip, f, proto, 0 /* is_icmp_inner_ip4 */,
3693                              0 /* skip_saddr_rewrite */);
3694
3695   return NAT_ED_TRNSL_ERR_SUCCESS;
3696 }
3697
3698 nat_translation_error_e
3699 nat_6t_flow_buf_translate_i2o (vlib_main_t *vm, snat_main_t *sm,
3700                                vlib_buffer_t *b, ip4_header_t *ip,
3701                                nat_6t_flow_t *f, ip_protocol_t proto,
3702                                int is_output_feature)
3703 {
3704   return nat_6t_flow_buf_translate (vm, sm, b, ip, f, proto, is_output_feature,
3705                                     1 /* is_i2o */);
3706 }
3707
3708 nat_translation_error_e
3709 nat_6t_flow_buf_translate_o2i (vlib_main_t *vm, snat_main_t *sm,
3710                                vlib_buffer_t *b, ip4_header_t *ip,
3711                                nat_6t_flow_t *f, ip_protocol_t proto,
3712                                int is_output_feature)
3713 {
3714   return nat_6t_flow_buf_translate (vm, sm, b, ip, f, proto, is_output_feature,
3715                                     0 /* is_i2o */);
3716 }
3717
3718 u8 *
3719 format_nat_6t (u8 *s, va_list *args)
3720 {
3721   nat_6t_t *t = va_arg (*args, nat_6t_t *);
3722
3723   s = format (s, "saddr %U sport %u daddr %U dport %u proto %U fib_idx %u",
3724               format_ip4_address, t->saddr.as_u8,
3725               clib_net_to_host_u16 (t->sport), format_ip4_address,
3726               t->daddr.as_u8, clib_net_to_host_u16 (t->dport),
3727               format_ip_protocol, t->proto, t->fib_index);
3728   return s;
3729 }
3730
3731 u8 *
3732 format_nat_ed_translation_error (u8 *s, va_list *args)
3733 {
3734   nat_translation_error_e e = va_arg (*args, nat_translation_error_e);
3735
3736   switch (e)
3737     {
3738     case NAT_ED_TRNSL_ERR_SUCCESS:
3739       s = format (s, "success");
3740       break;
3741     case NAT_ED_TRNSL_ERR_TRANSLATION_FAILED:
3742       s = format (s, "translation-failed");
3743       break;
3744     case NAT_ED_TRNSL_ERR_FLOW_MISMATCH:
3745       s = format (s, "flow-mismatch");
3746       break;
3747     case NAT_ED_TRNSL_ERR_PACKET_TRUNCATED:
3748       s = format (s, "packet-truncated");
3749       break;
3750     case NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT:
3751       s = format (s, "inner-ip-corrupted");
3752       break;
3753     case NAT_ED_TRNSL_ERR_INVALID_CSUM:
3754       s = format (s, "invalid-checksum");
3755       break;
3756     }
3757   return s;
3758 }
3759
3760 u8 *
3761 format_nat_6t_flow (u8 *s, va_list *args)
3762 {
3763   nat_6t_flow_t *f = va_arg (*args, nat_6t_flow_t *);
3764
3765   s = format (s, "match: %U ", format_nat_6t, &f->match);
3766   int r = 0;
3767   if (f->ops & NAT_FLOW_OP_SADDR_REWRITE)
3768     {
3769       s = format (s, "rewrite: saddr %U ", format_ip4_address,
3770                   f->rewrite.saddr.as_u8);
3771       r = 1;
3772     }
3773   if (f->ops & NAT_FLOW_OP_SPORT_REWRITE)
3774     {
3775       if (!r)
3776         {
3777           s = format (s, "rewrite: ");
3778           r = 1;
3779         }
3780       s = format (s, "sport %u ", clib_net_to_host_u16 (f->rewrite.sport));
3781     }
3782   if (f->ops & NAT_FLOW_OP_DADDR_REWRITE)
3783     {
3784       if (!r)
3785         {
3786           s = format (s, "rewrite: ");
3787           r = 1;
3788         }
3789       s = format (s, "daddr %U ", format_ip4_address, f->rewrite.daddr.as_u8);
3790     }
3791   if (f->ops & NAT_FLOW_OP_DPORT_REWRITE)
3792     {
3793       if (!r)
3794         {
3795           s = format (s, "rewrite: ");
3796           r = 1;
3797         }
3798       s = format (s, "dport %u ", clib_net_to_host_u16 (f->rewrite.dport));
3799     }
3800   if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE)
3801     {
3802       if (!r)
3803         {
3804           s = format (s, "rewrite: ");
3805           r = 1;
3806         }
3807       s = format (s, "icmp-id %u ", clib_net_to_host_u16 (f->rewrite.icmp_id));
3808     }
3809   if (f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
3810     {
3811       if (!r)
3812         {
3813           s = format (s, "rewrite: ");
3814           r = 1;
3815         }
3816       s = format (s, "txfib %u ", f->rewrite.fib_index);
3817     }
3818   return s;
3819 }
3820
3821 static inline void
3822 nat_syslog_nat44_sess (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
3823                        u16 isport, ip4_address_t *xsaddr, u16 xsport,
3824                        ip4_address_t *idaddr, u16 idport,
3825                        ip4_address_t *xdaddr, u16 xdport, u8 proto, u8 is_add,
3826                        u8 is_twicenat)
3827 {
3828   syslog_msg_t syslog_msg;
3829   fib_table_t *fib;
3830
3831   if (!syslog_is_enabled ())
3832     return;
3833
3834   if (syslog_severity_filter_block (SADD_SDEL_SEVERITY))
3835     return;
3836
3837   fib = fib_table_get (sfibix, FIB_PROTOCOL_IP4);
3838
3839   syslog_msg_init (&syslog_msg, NAT_FACILITY, SADD_SDEL_SEVERITY, NAT_APPNAME,
3840                    is_add ? SADD_MSGID : SDEL_MSGID);
3841
3842   syslog_msg_sd_init (&syslog_msg, NSESS_SDID);
3843   syslog_msg_add_sd_param (&syslog_msg, SSUBIX_SDPARAM_NAME, "%d", ssubix);
3844   syslog_msg_add_sd_param (&syslog_msg, SVLAN_SDPARAM_NAME, "%d",
3845                            fib->ft_table_id);
3846   syslog_msg_add_sd_param (&syslog_msg, IATYP_SDPARAM_NAME, IATYP_IPV4);
3847   syslog_msg_add_sd_param (&syslog_msg, ISADDR_SDPARAM_NAME, "%U",
3848                            format_ip4_address, isaddr);
3849   syslog_msg_add_sd_param (&syslog_msg, ISPORT_SDPARAM_NAME, "%d",
3850                            clib_net_to_host_u16 (isport));
3851   syslog_msg_add_sd_param (&syslog_msg, XATYP_SDPARAM_NAME, IATYP_IPV4);
3852   syslog_msg_add_sd_param (&syslog_msg, XSADDR_SDPARAM_NAME, "%U",
3853                            format_ip4_address, xsaddr);
3854   syslog_msg_add_sd_param (&syslog_msg, XSPORT_SDPARAM_NAME, "%d",
3855                            clib_net_to_host_u16 (xsport));
3856   syslog_msg_add_sd_param (&syslog_msg, PROTO_SDPARAM_NAME, "%d", proto);
3857   syslog_msg_add_sd_param (&syslog_msg, XDADDR_SDPARAM_NAME, "%U",
3858                            format_ip4_address, xdaddr);
3859   syslog_msg_add_sd_param (&syslog_msg, XDPORT_SDPARAM_NAME, "%d",
3860                            clib_net_to_host_u16 (xdport));
3861   if (is_twicenat)
3862     {
3863       syslog_msg_add_sd_param (&syslog_msg, IDADDR_SDPARAM_NAME, "%U",
3864                                format_ip4_address, idaddr);
3865       syslog_msg_add_sd_param (&syslog_msg, IDPORT_SDPARAM_NAME, "%d",
3866                                clib_net_to_host_u16 (idport));
3867     }
3868
3869   syslog_msg_send (&syslog_msg);
3870 }
3871
3872 void
3873 nat_syslog_nat44_sadd (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
3874                        u16 isport, ip4_address_t *idaddr, u16 idport,
3875                        ip4_address_t *xsaddr, u16 xsport,
3876                        ip4_address_t *xdaddr, u16 xdport, u8 proto,
3877                        u8 is_twicenat)
3878 {
3879   nat_syslog_nat44_sess (ssubix, sfibix, isaddr, isport, xsaddr, xsport,
3880                          idaddr, idport, xdaddr, xdport, proto, 1,
3881                          is_twicenat);
3882 }
3883
3884 void
3885 nat_syslog_nat44_sdel (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
3886                        u16 isport, ip4_address_t *idaddr, u16 idport,
3887                        ip4_address_t *xsaddr, u16 xsport,
3888                        ip4_address_t *xdaddr, u16 xdport, u8 proto,
3889                        u8 is_twicenat)
3890 {
3891   nat_syslog_nat44_sess (ssubix, sfibix, isaddr, isport, xsaddr, xsport,
3892                          idaddr, idport, xdaddr, xdport, proto, 0,
3893                          is_twicenat);
3894 }
3895
3896 /*
3897  * fd.io coding-style-patch-verification: ON
3898  *
3899  * Local Variables:
3900  * eval: (c-set-style "gnu")
3901  * End:
3902  */