nat: det44 plugin fix style and api cleanup
[vpp.git] / src / plugins / nat / nat64.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief NAT64 implementation
18  */
19
20 #include <nat/nat64.h>
21 #include <nat/nat64_db.h>
22 #include <nat/nat_inlines.h>
23 #include <vnet/fib/ip4_fib.h>
24 #include <vppinfra/crc32.h>
25 #include <vnet/ip/reass/ip4_sv_reass.h>
26 #include <vnet/ip/reass/ip6_sv_reass.h>
27
28
29 nat64_main_t nat64_main;
30
31 /* *INDENT-OFF* */
32
33 /* Hook up input features */
34 VNET_FEATURE_INIT (nat64_in2out, static) = {
35   .arc_name = "ip6-unicast",
36   .node_name = "nat64-in2out",
37   .runs_before = VNET_FEATURES ("ip6-lookup"),
38   .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
39 };
40 VNET_FEATURE_INIT (nat64_out2in, static) = {
41   .arc_name = "ip4-unicast",
42   .node_name = "nat64-out2in",
43   .runs_before = VNET_FEATURES ("ip4-lookup"),
44   .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
45 };
46 VNET_FEATURE_INIT (nat64_in2out_handoff, static) = {
47   .arc_name = "ip6-unicast",
48   .node_name = "nat64-in2out-handoff",
49   .runs_before = VNET_FEATURES ("ip6-lookup"),
50   .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
51 };
52 VNET_FEATURE_INIT (nat64_out2in_handoff, static) = {
53   .arc_name = "ip4-unicast",
54   .node_name = "nat64-out2in-handoff",
55   .runs_before = VNET_FEATURES ("ip4-lookup"),
56   .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
57 };
58
59
60 static u8 well_known_prefix[] = {
61   0x00, 0x64, 0xff, 0x9b,
62   0x00, 0x00, 0x00, 0x00,
63   0x00, 0x00, 0x00, 0x00,
64   0x00, 0x00, 0x00, 0x00
65 };
66
67 /* *INDENT-ON* */
68
69 static void
70 nat64_ip4_add_del_interface_address_cb (ip4_main_t * im, uword opaque,
71                                         u32 sw_if_index,
72                                         ip4_address_t * address,
73                                         u32 address_length,
74                                         u32 if_address_index, u32 is_delete)
75 {
76   nat64_main_t *nm = &nat64_main;
77   int i, j;
78
79   for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
80     {
81       if (sw_if_index == nm->auto_add_sw_if_indices[i])
82         {
83           if (!is_delete)
84             {
85               /* Don't trip over lease renewal, static config */
86               for (j = 0; j < vec_len (nm->addr_pool); j++)
87                 if (nm->addr_pool[j].addr.as_u32 == address->as_u32)
88                   return;
89
90               (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
91                                               address, ~0, 1);
92               return;
93             }
94           else
95             {
96               (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
97                                               address, ~0, 0);
98               return;
99             }
100         }
101     }
102 }
103
104 u32
105 nat64_get_worker_in2out (ip6_address_t * addr)
106 {
107   nat64_main_t *nm = &nat64_main;
108   snat_main_t *sm = nm->sm;
109   u32 next_worker_index = nm->sm->first_worker_index;
110   u32 hash;
111
112 #ifdef clib_crc32c_uses_intrinsics
113   hash = clib_crc32c ((u8 *) addr->as_u32, 16);
114 #else
115   u64 tmp = addr->as_u64[0] ^ addr->as_u64[1];
116   hash = clib_xxhash (tmp);
117 #endif
118
119   if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
120     next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
121   else
122     next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
123
124   return next_worker_index;
125 }
126
127 u32
128 nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip)
129 {
130   nat64_main_t *nm = &nat64_main;
131   snat_main_t *sm = nm->sm;
132   udp_header_t *udp;
133   u16 port;
134   u32 proto;
135
136   proto = ip_proto_to_nat_proto (ip->protocol);
137   udp = ip4_next_header (ip);
138   port = udp->dst_port;
139
140   /* unknown protocol */
141   if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
142     {
143       nat64_db_t *db;
144       ip46_address_t daddr;
145       nat64_db_bib_entry_t *bibe;
146
147       clib_memset (&daddr, 0, sizeof (daddr));
148       daddr.ip4.as_u32 = ip->dst_address.as_u32;
149
150       /* *INDENT-OFF* */
151       vec_foreach (db, nm->db)
152         {
153           bibe = nat64_db_bib_entry_find (db, &daddr, 0, ip->protocol, 0, 0);
154           if (bibe)
155             return (u32) (db - nm->db);
156         }
157       /* *INDENT-ON* */
158       return vlib_get_thread_index ();
159     }
160
161   /* ICMP */
162   if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
163     {
164       icmp46_header_t *icmp = (icmp46_header_t *) udp;
165       icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
166       if (!icmp_type_is_error_message
167           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
168         port = vnet_buffer (b)->ip.reass.l4_src_port;
169       else
170         {
171           /* if error message, then it's not fragmented and we can access it */
172           ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
173           proto = ip_proto_to_nat_proto (inner_ip->protocol);
174           void *l4_header = ip4_next_header (inner_ip);
175           switch (proto)
176             {
177             case NAT_PROTOCOL_ICMP:
178               icmp = (icmp46_header_t *) l4_header;
179               echo = (icmp_echo_header_t *) (icmp + 1);
180               port = echo->identifier;
181               break;
182             case NAT_PROTOCOL_UDP:
183             case NAT_PROTOCOL_TCP:
184               port = ((tcp_udp_header_t *) l4_header)->src_port;
185               break;
186             default:
187               return vlib_get_thread_index ();
188             }
189         }
190     }
191
192   /* worker by outside port  (TCP/UDP) */
193   port = clib_net_to_host_u16 (port);
194   if (port > 1024)
195     return nm->sm->first_worker_index + ((port - 1024) / sm->port_per_thread);
196
197   return vlib_get_thread_index ();
198 }
199
200 clib_error_t *
201 nat64_init (vlib_main_t * vm)
202 {
203   nat64_main_t *nm = &nat64_main;
204   vlib_thread_main_t *tm = vlib_get_thread_main ();
205   ip4_add_del_interface_address_callback_t cb4;
206   ip4_main_t *im = &ip4_main;
207   nm->sm = &snat_main;
208   vlib_node_t *node;
209
210   vec_validate (nm->db, tm->n_vlib_mains - 1);
211
212   nm->fq_in2out_index = ~0;
213   nm->fq_out2in_index = ~0;
214
215   node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
216   nm->error_node_index = node->index;
217
218   node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out");
219   nm->in2out_node_index = node->index;
220
221   node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out-slowpath");
222   nm->in2out_slowpath_node_index = node->index;
223
224   node = vlib_get_node_by_name (vm, (u8 *) "nat64-out2in");
225   nm->out2in_node_index = node->index;
226
227   /* set session timeouts to default values */
228   nm->udp_timeout = SNAT_UDP_TIMEOUT;
229   nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
230   nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
231   nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
232
233   nm->total_enabled_count = 0;
234
235   /* Set up the interface address add/del callback */
236   cb4.function = nat64_ip4_add_del_interface_address_cb;
237   cb4.function_opaque = 0;
238   vec_add1 (im->add_del_interface_address_callbacks, cb4);
239   nm->ip4_main = im;
240
241   /* Init counters */
242   nm->total_bibs.name = "total-bibs";
243   nm->total_bibs.stat_segment_name = "/nat64/total-bibs";
244   vlib_validate_simple_counter (&nm->total_bibs, 0);
245   vlib_zero_simple_counter (&nm->total_bibs, 0);
246   nm->total_sessions.name = "total-sessions";
247   nm->total_sessions.stat_segment_name = "/nat64/total-sessions";
248   vlib_validate_simple_counter (&nm->total_sessions, 0);
249   vlib_zero_simple_counter (&nm->total_sessions, 0);
250
251 #define _(x)                                                     \
252   nm->counters.in2out.x.name = #x;                               \
253   nm->counters.in2out.x.stat_segment_name = "/nat64/in2out/" #x; \
254   nm->counters.out2in.x.name = #x;                               \
255   nm->counters.out2in.x.stat_segment_name = "/nat64/out2in/" #x;
256   foreach_nat_counter;
257 #undef _
258   return 0;
259 }
260
261 static void nat64_free_out_addr_and_port (struct nat64_db_s *db,
262                                           ip4_address_t * addr, u16 port,
263                                           u8 protocol);
264
265 void
266 nat64_set_hash (u32 bib_buckets, uword bib_memory_size, u32 st_buckets,
267                 uword st_memory_size)
268 {
269   nat64_main_t *nm = &nat64_main;
270   nat64_db_t *db;
271
272   nm->bib_buckets = bib_buckets;
273   nm->bib_memory_size = bib_memory_size;
274   nm->st_buckets = st_buckets;
275   nm->st_memory_size = st_memory_size;
276
277   /* *INDENT-OFF* */
278   vec_foreach (db, nm->db)
279     {
280       if (nat64_db_init (db, bib_buckets, bib_memory_size, st_buckets,
281                          st_memory_size, nat64_free_out_addr_and_port))
282         nat_elog_err ("NAT64 DB init failed");
283     }
284   /* *INDENT-ON* */
285 }
286
287 int
288 nat64_add_del_pool_addr (u32 thread_index,
289                          ip4_address_t * addr, u32 vrf_id, u8 is_add)
290 {
291   nat64_main_t *nm = &nat64_main;
292   snat_address_t *a = 0;
293   snat_interface_t *interface;
294   int i;
295   nat64_db_t *db;
296   vlib_thread_main_t *tm = vlib_get_thread_main ();
297
298   /* Check if address already exists */
299   for (i = 0; i < vec_len (nm->addr_pool); i++)
300     {
301       if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
302         {
303           a = nm->addr_pool + i;
304           break;
305         }
306     }
307
308   if (is_add)
309     {
310       if (a)
311         return VNET_API_ERROR_VALUE_EXIST;
312
313       vec_add2 (nm->addr_pool, a, 1);
314       a->addr = *addr;
315       a->fib_index = ~0;
316       if (vrf_id != ~0)
317         a->fib_index =
318           fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
319                                              nat_fib_src_hi);
320 #define _(N, id, n, s) \
321       clib_memset (a->busy_##n##_port_refcounts, 0, sizeof(a->busy_##n##_port_refcounts)); \
322       a->busy_##n##_ports = 0; \
323       vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
324       foreach_nat_protocol
325 #undef _
326     }
327   else
328     {
329       if (!a)
330         return VNET_API_ERROR_NO_SUCH_ENTRY;
331
332       if (a->fib_index != ~0)
333         fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6, nat_fib_src_hi);
334       /* Delete sessions using address */
335         /* *INDENT-OFF* */
336         vec_foreach (db, nm->db)
337           {
338             nat64_db_free_out_addr (thread_index, db, &a->addr);
339             vlib_set_simple_counter (&nm->total_bibs, db - nm->db, 0,
340                                      db->bib.bib_entries_num);
341             vlib_set_simple_counter (&nm->total_sessions, db - nm->db, 0,
342                                      db->st.st_entries_num);
343           }
344         /* *INDENT-ON* */
345       vec_del1 (nm->addr_pool, i);
346     }
347
348   /* Add/del external address to FIB */
349   /* *INDENT-OFF* */
350   pool_foreach (interface, nm->interfaces,
351   ({
352     if (nat_interface_is_inside(interface))
353       continue;
354
355     snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
356     break;
357   }));
358   /* *INDENT-ON* */
359
360   return 0;
361 }
362
363 void
364 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
365 {
366   nat64_main_t *nm = &nat64_main;
367   snat_address_t *a = 0;
368
369   /* *INDENT-OFF* */
370   vec_foreach (a, nm->addr_pool)
371     {
372       if (fn (a, ctx))
373         break;
374     };
375   /* *INDENT-ON* */
376 }
377
378 int
379 nat64_add_interface_address (u32 sw_if_index, int is_add)
380 {
381   nat64_main_t *nm = &nat64_main;
382   ip4_main_t *ip4_main = nm->ip4_main;
383   ip4_address_t *first_int_addr;
384   int i;
385
386   first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
387
388   for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
389     {
390       if (nm->auto_add_sw_if_indices[i] == sw_if_index)
391         {
392           if (is_add)
393             return VNET_API_ERROR_VALUE_EXIST;
394           else
395             {
396               /* if have address remove it */
397               if (first_int_addr)
398                 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
399                                                 first_int_addr, ~0, 0);
400               vec_del1 (nm->auto_add_sw_if_indices, i);
401               return 0;
402             }
403         }
404     }
405
406   if (!is_add)
407     return VNET_API_ERROR_NO_SUCH_ENTRY;
408
409   /* add to the auto-address list */
410   vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
411
412   /* If the address is already bound - or static - add it now */
413   if (first_int_addr)
414     (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
415                                     first_int_addr, ~0, 1);
416
417   return 0;
418 }
419
420 static void
421 nat64_validate_counters (nat64_main_t * nm, u32 sw_if_index)
422 {
423 #define _(x)                                                          \
424   vlib_validate_simple_counter (&nm->counters.in2out.x, sw_if_index); \
425   vlib_zero_simple_counter (&nm->counters.in2out.x, sw_if_index);     \
426   vlib_validate_simple_counter (&nm->counters.out2in.x, sw_if_index); \
427   vlib_zero_simple_counter (&nm->counters.out2in.x, sw_if_index);
428   foreach_nat_counter;
429 #undef _
430 }
431
432 int
433 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
434 {
435   vlib_main_t *vm = vlib_get_main ();
436   nat64_main_t *nm = &nat64_main;
437   snat_interface_t *interface = 0, *i;
438   snat_address_t *ap;
439   const char *feature_name, *arc_name;
440
441   /* Check if interface already exists */
442   /* *INDENT-OFF* */
443   pool_foreach (i, nm->interfaces,
444   ({
445     if (i->sw_if_index == sw_if_index)
446       {
447         interface = i;
448         break;
449       }
450   }));
451   /* *INDENT-ON* */
452
453   if (is_add)
454     {
455       if (interface)
456         goto set_flags;
457
458       pool_get (nm->interfaces, interface);
459       interface->sw_if_index = sw_if_index;
460       interface->flags = 0;
461       nat64_validate_counters (nm, sw_if_index);
462     set_flags:
463       if (is_inside)
464         interface->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
465       else
466         interface->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
467
468       nm->total_enabled_count++;
469       vlib_process_signal_event (vm,
470                                  nm->nat64_expire_walk_node_index,
471                                  NAT64_CLEANER_RESCHEDULE, 0);
472
473     }
474   else
475     {
476       if (!interface)
477         return VNET_API_ERROR_NO_SUCH_ENTRY;
478
479       if ((nat_interface_is_inside (interface)
480            && nat_interface_is_outside (interface)))
481         interface->flags &=
482           is_inside ? ~NAT_INTERFACE_FLAG_IS_INSIDE :
483           ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
484       else
485         pool_put (nm->interfaces, interface);
486
487       nm->total_enabled_count--;
488     }
489
490   if (!is_inside)
491     {
492       /* *INDENT-OFF* */
493       vec_foreach (ap, nm->addr_pool)
494         snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
495       /* *INDENT-ON* */
496     }
497
498   if (nm->sm->num_workers > 1)
499     {
500       feature_name =
501         is_inside ? "nat64-in2out-handoff" : "nat64-out2in-handoff";
502       if (nm->fq_in2out_index == ~0)
503         nm->fq_in2out_index =
504           vlib_frame_queue_main_init (nat64_in2out_node.index, 0);
505       if (nm->fq_out2in_index == ~0)
506         nm->fq_out2in_index =
507           vlib_frame_queue_main_init (nat64_out2in_node.index, 0);
508     }
509   else
510     feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
511
512   arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
513
514   if (is_inside)
515     {
516       int rv = ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
517       if (rv)
518         return rv;
519     }
520   else
521     {
522       int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
523       if (rv)
524         return rv;
525     }
526
527   return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
528                                       is_add, 0, 0);
529 }
530
531 void
532 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
533 {
534   nat64_main_t *nm = &nat64_main;
535   snat_interface_t *i = 0;
536
537   /* *INDENT-OFF* */
538   pool_foreach (i, nm->interfaces,
539   ({
540     if (fn (i, ctx))
541       break;
542   }));
543   /* *INDENT-ON* */
544 }
545
546 int
547 nat64_alloc_out_addr_and_port (u32 fib_index, nat_protocol_t proto,
548                                ip4_address_t * addr, u16 * port,
549                                u32 thread_index)
550 {
551   nat64_main_t *nm = &nat64_main;
552   snat_main_t *sm = nm->sm;
553   u32 worker_index = 0;
554   int rv;
555
556   if (sm->num_workers > 1)
557     worker_index = thread_index - sm->first_worker_index;
558
559   rv =
560     sm->alloc_addr_and_port (nm->addr_pool, fib_index, thread_index,
561                              proto, addr, port, sm->port_per_thread,
562                              worker_index);
563
564   return rv;
565 }
566
567 static void
568 nat64_free_out_addr_and_port (struct nat64_db_s *db, ip4_address_t * addr,
569                               u16 port, u8 protocol)
570 {
571   nat64_main_t *nm = &nat64_main;
572   int i;
573   snat_address_t *a;
574   u32 thread_index = db - nm->db;
575   nat_protocol_t proto = ip_proto_to_nat_proto (protocol);
576   u16 port_host_byte_order = clib_net_to_host_u16 (port);
577
578   for (i = 0; i < vec_len (nm->addr_pool); i++)
579     {
580       a = nm->addr_pool + i;
581       if (addr->as_u32 != a->addr.as_u32)
582         continue;
583       switch (proto)
584         {
585 #define _(N, j, n, s) \
586         case NAT_PROTOCOL_##N: \
587           ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
588           --a->busy_##n##_port_refcounts[port_host_byte_order]; \
589           a->busy_##n##_ports--; \
590           a->busy_##n##_ports_per_thread[thread_index]--; \
591           break;
592           foreach_nat_protocol
593 #undef _
594         default:
595           nat_elog_notice ("unknown protocol");
596           return;
597         }
598       break;
599     }
600 }
601
602 /**
603  * @brief Add/delete static BIB entry in worker thread.
604  */
605 static uword
606 nat64_static_bib_worker_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
607                             vlib_frame_t * f)
608 {
609   nat64_main_t *nm = &nat64_main;
610   u32 thread_index = vm->thread_index;
611   nat64_db_t *db = &nm->db[thread_index];
612   nat64_static_bib_to_update_t *static_bib;
613   nat64_db_bib_entry_t *bibe;
614   ip46_address_t addr;
615
616   /* *INDENT-OFF* */
617   pool_foreach (static_bib, nm->static_bibs,
618   ({
619     if ((static_bib->thread_index != thread_index) || (static_bib->done))
620       continue;
621
622     if (static_bib->is_add)
623       {
624           (void) nat64_db_bib_entry_create (thread_index, db,
625                                             &static_bib->in_addr,
626                                             &static_bib->out_addr,
627                                             static_bib->in_port,
628                                             static_bib->out_port,
629                                             static_bib->fib_index,
630                                             static_bib->proto, 1);
631           vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
632                                    db->bib.bib_entries_num);
633       }
634     else
635       {
636         addr.as_u64[0] = static_bib->in_addr.as_u64[0];
637         addr.as_u64[1] = static_bib->in_addr.as_u64[1];
638         bibe = nat64_db_bib_entry_find (db, &addr, static_bib->in_port,
639                                         static_bib->proto,
640                                         static_bib->fib_index, 1);
641         if (bibe)
642           {
643             nat64_db_bib_entry_free (thread_index, db, bibe);
644             vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
645                                      db->bib.bib_entries_num);
646             vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
647                                      db->st.st_entries_num);
648           }
649       }
650
651       static_bib->done = 1;
652   }));
653   /* *INDENT-ON* */
654
655   return 0;
656 }
657
658 static vlib_node_registration_t nat64_static_bib_worker_node;
659
660 /* *INDENT-OFF* */
661 VLIB_REGISTER_NODE (nat64_static_bib_worker_node, static) = {
662     .function = nat64_static_bib_worker_fn,
663     .type = VLIB_NODE_TYPE_INPUT,
664     .state = VLIB_NODE_STATE_INTERRUPT,
665     .name = "nat64-static-bib-worker",
666 };
667 /* *INDENT-ON* */
668
669 int
670 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
671                                 ip4_address_t * out_addr, u16 in_port,
672                                 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
673 {
674   nat64_main_t *nm = &nat64_main;
675   nat64_db_bib_entry_t *bibe;
676   u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
677                                                      nat_fib_src_hi);
678   nat_protocol_t p = ip_proto_to_nat_proto (proto);
679   ip46_address_t addr;
680   int i;
681   snat_address_t *a;
682   u32 thread_index = 0;
683   nat64_db_t *db;
684   nat64_static_bib_to_update_t *static_bib;
685   vlib_main_t *worker_vm;
686   u32 *to_be_free = 0, *index;
687
688   if (nm->sm->num_workers > 1)
689     {
690       thread_index = nat64_get_worker_in2out (in_addr);
691       db = &nm->db[thread_index];
692     }
693   else
694     db = &nm->db[nm->sm->num_workers];
695
696   addr.as_u64[0] = in_addr->as_u64[0];
697   addr.as_u64[1] = in_addr->as_u64[1];
698   bibe =
699     nat64_db_bib_entry_find (db, &addr, clib_host_to_net_u16 (in_port),
700                              proto, fib_index, 1);
701
702   if (is_add)
703     {
704       if (bibe)
705         return VNET_API_ERROR_VALUE_EXIST;
706
707       /* outside port must be assigned to same thread as internall address */
708       if ((out_port > 1024) && (nm->sm->num_workers > 1))
709         {
710           if (thread_index != ((out_port - 1024) / nm->sm->port_per_thread))
711             return VNET_API_ERROR_INVALID_VALUE_2;
712         }
713
714       for (i = 0; i < vec_len (nm->addr_pool); i++)
715         {
716           a = nm->addr_pool + i;
717           if (out_addr->as_u32 != a->addr.as_u32)
718             continue;
719           switch (p)
720             {
721 #define _(N, j, n, s) \
722             case NAT_PROTOCOL_##N: \
723               if (a->busy_##n##_port_refcounts[out_port]) \
724                 return VNET_API_ERROR_INVALID_VALUE; \
725               ++a->busy_##n##_port_refcounts[out_port]; \
726               if (out_port > 1024) \
727                 { \
728                   a->busy_##n##_ports++; \
729                   a->busy_##n##_ports_per_thread[thread_index]++; \
730                 } \
731               break;
732               foreach_nat_protocol
733 #undef _
734             default:
735               clib_memset (&addr, 0, sizeof (addr));
736               addr.ip4.as_u32 = out_addr->as_u32;
737               if (nat64_db_bib_entry_find (db, &addr, 0, proto, fib_index, 0))
738                 return VNET_API_ERROR_INVALID_VALUE;
739             }
740           break;
741         }
742       if (!nm->sm->num_workers)
743         {
744           bibe =
745             nat64_db_bib_entry_create (thread_index, db, in_addr, out_addr,
746                                        clib_host_to_net_u16 (in_port),
747                                        clib_host_to_net_u16 (out_port),
748                                        fib_index, proto, 1);
749           if (!bibe)
750             return VNET_API_ERROR_UNSPECIFIED;
751
752           vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
753                                    db->bib.bib_entries_num);
754         }
755     }
756   else
757     {
758       if (!bibe)
759         return VNET_API_ERROR_NO_SUCH_ENTRY;
760
761       if (!nm->sm->num_workers)
762         {
763           nat64_db_bib_entry_free (thread_index, db, bibe);
764           vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
765                                    db->bib.bib_entries_num);
766         }
767     }
768
769   if (nm->sm->num_workers)
770     {
771       /* *INDENT-OFF* */
772       pool_foreach (static_bib, nm->static_bibs,
773       ({
774         if (static_bib->done)
775           vec_add1 (to_be_free, static_bib - nm->static_bibs);
776       }));
777       vec_foreach (index, to_be_free)
778         pool_put_index (nm->static_bibs, index[0]);
779       /* *INDENT-ON* */
780       vec_free (to_be_free);
781       pool_get (nm->static_bibs, static_bib);
782       static_bib->in_addr.as_u64[0] = in_addr->as_u64[0];
783       static_bib->in_addr.as_u64[1] = in_addr->as_u64[1];
784       static_bib->in_port = clib_host_to_net_u16 (in_port);
785       static_bib->out_addr.as_u32 = out_addr->as_u32;
786       static_bib->out_port = clib_host_to_net_u16 (out_port);
787       static_bib->fib_index = fib_index;
788       static_bib->proto = proto;
789       static_bib->is_add = is_add;
790       static_bib->thread_index = thread_index;
791       static_bib->done = 0;
792       worker_vm = vlib_mains[thread_index];
793       if (worker_vm)
794         vlib_node_set_interrupt_pending (worker_vm,
795                                          nat64_static_bib_worker_node.index);
796       else
797         return VNET_API_ERROR_UNSPECIFIED;
798     }
799
800   return 0;
801 }
802
803 int
804 nat64_set_udp_timeout (u32 timeout)
805 {
806   nat64_main_t *nm = &nat64_main;
807
808   if (timeout == 0)
809     nm->udp_timeout = SNAT_UDP_TIMEOUT;
810   else
811     nm->udp_timeout = timeout;
812
813   return 0;
814 }
815
816 u32
817 nat64_get_udp_timeout (void)
818 {
819   nat64_main_t *nm = &nat64_main;
820
821   return nm->udp_timeout;
822 }
823
824 int
825 nat64_set_icmp_timeout (u32 timeout)
826 {
827   nat64_main_t *nm = &nat64_main;
828
829   if (timeout == 0)
830     nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
831   else
832     nm->icmp_timeout = timeout;
833
834   return 0;
835 }
836
837 u32
838 nat64_get_icmp_timeout (void)
839 {
840   nat64_main_t *nm = &nat64_main;
841
842   return nm->icmp_timeout;
843 }
844
845 int
846 nat64_set_tcp_timeouts (u32 trans, u32 est)
847 {
848   nat64_main_t *nm = &nat64_main;
849
850   if (trans == 0)
851     nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
852   else
853     nm->tcp_trans_timeout = trans;
854
855   if (est == 0)
856     nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
857   else
858     nm->tcp_est_timeout = est;
859
860   return 0;
861 }
862
863 u32
864 nat64_get_tcp_trans_timeout (void)
865 {
866   nat64_main_t *nm = &nat64_main;
867
868   return nm->tcp_trans_timeout;
869 }
870
871 u32
872 nat64_get_tcp_est_timeout (void)
873 {
874   nat64_main_t *nm = &nat64_main;
875
876   return nm->tcp_est_timeout;
877 }
878
879 void
880 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
881 {
882   nat64_main_t *nm = &nat64_main;
883   u32 now = (u32) vlib_time_now (vm);
884
885   switch (ip_proto_to_nat_proto (ste->proto))
886     {
887     case NAT_PROTOCOL_ICMP:
888       ste->expire = now + nm->icmp_timeout;
889       return;
890     case NAT_PROTOCOL_TCP:
891       {
892         switch (ste->tcp_state)
893           {
894           case NAT64_TCP_STATE_V4_INIT:
895           case NAT64_TCP_STATE_V6_INIT:
896           case NAT64_TCP_STATE_V4_FIN_RCV:
897           case NAT64_TCP_STATE_V6_FIN_RCV:
898           case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
899           case NAT64_TCP_STATE_TRANS:
900             ste->expire = now + nm->tcp_trans_timeout;
901             return;
902           case NAT64_TCP_STATE_ESTABLISHED:
903             ste->expire = now + nm->tcp_est_timeout;
904             return;
905           default:
906             return;
907           }
908       }
909     case NAT_PROTOCOL_UDP:
910       ste->expire = now + nm->udp_timeout;
911       return;
912     default:
913       ste->expire = now + nm->udp_timeout;
914       return;
915     }
916 }
917
918 void
919 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
920                              u8 is_ip6)
921 {
922   switch (ste->tcp_state)
923     {
924     case NAT64_TCP_STATE_CLOSED:
925       {
926         if (tcp->flags & TCP_FLAG_SYN)
927           {
928             if (is_ip6)
929               ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
930             else
931               ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
932           }
933         return;
934       }
935     case NAT64_TCP_STATE_V4_INIT:
936       {
937         if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
938           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
939         return;
940       }
941     case NAT64_TCP_STATE_V6_INIT:
942       {
943         if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
944           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
945         return;
946       }
947     case NAT64_TCP_STATE_ESTABLISHED:
948       {
949         if (tcp->flags & TCP_FLAG_FIN)
950           {
951             if (is_ip6)
952               ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
953             else
954               ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
955           }
956         else if (tcp->flags & TCP_FLAG_RST)
957           {
958             ste->tcp_state = NAT64_TCP_STATE_TRANS;
959           }
960         return;
961       }
962     case NAT64_TCP_STATE_V4_FIN_RCV:
963       {
964         if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
965           ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
966         return;
967       }
968     case NAT64_TCP_STATE_V6_FIN_RCV:
969       {
970         if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
971           ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
972         return;
973       }
974     case NAT64_TCP_STATE_TRANS:
975       {
976         if (!(tcp->flags & TCP_FLAG_RST))
977           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
978         return;
979       }
980     default:
981       return;
982     }
983 }
984
985 int
986 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
987 {
988   nat64_main_t *nm = &nat64_main;
989   nat64_prefix_t *p = 0;
990   int i;
991
992   /* Verify prefix length */
993   if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
994       && plen != 96)
995     return VNET_API_ERROR_INVALID_VALUE;
996
997   /* Check if tenant already have prefix */
998   for (i = 0; i < vec_len (nm->pref64); i++)
999     {
1000       if (nm->pref64[i].vrf_id == vrf_id)
1001         {
1002           p = nm->pref64 + i;
1003           break;
1004         }
1005     }
1006
1007   if (is_add)
1008     {
1009       if (!p)
1010         {
1011           vec_add2 (nm->pref64, p, 1);
1012           p->fib_index =
1013             fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
1014                                                nat_fib_src_hi);
1015           p->vrf_id = vrf_id;
1016         }
1017
1018       p->prefix.as_u64[0] = prefix->as_u64[0];
1019       p->prefix.as_u64[1] = prefix->as_u64[1];
1020       p->plen = plen;
1021     }
1022   else
1023     {
1024       if (!p)
1025         return VNET_API_ERROR_NO_SUCH_ENTRY;
1026
1027       vec_del1 (nm->pref64, i);
1028     }
1029
1030   return 0;
1031 }
1032
1033 void
1034 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
1035 {
1036   nat64_main_t *nm = &nat64_main;
1037   nat64_prefix_t *p = 0;
1038
1039   /* *INDENT-OFF* */
1040   vec_foreach (p, nm->pref64)
1041     {
1042       if (fn (p, ctx))
1043         break;
1044     };
1045   /* *INDENT-ON* */
1046 }
1047
1048 void
1049 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1050 {
1051   nat64_main_t *nm = &nat64_main;
1052   nat64_prefix_t *p, *gp = 0, *prefix = 0;
1053
1054   /* *INDENT-OFF* */
1055   vec_foreach (p, nm->pref64)
1056     {
1057       if (p->fib_index == fib_index)
1058         {
1059           prefix = p;
1060           break;
1061         }
1062
1063       if (p->fib_index == 0)
1064         gp = p;
1065     };
1066   /* *INDENT-ON* */
1067
1068   if (!prefix)
1069     prefix = gp;
1070
1071   if (prefix)
1072     {
1073       clib_memcpy_fast (ip6, &p->prefix, sizeof (ip6_address_t));
1074       switch (p->plen)
1075         {
1076         case 32:
1077           ip6->as_u32[1] = ip4->as_u32;
1078           break;
1079         case 40:
1080           ip6->as_u8[5] = ip4->as_u8[0];
1081           ip6->as_u8[6] = ip4->as_u8[1];
1082           ip6->as_u8[7] = ip4->as_u8[2];
1083           ip6->as_u8[9] = ip4->as_u8[3];
1084           break;
1085         case 48:
1086           ip6->as_u8[6] = ip4->as_u8[0];
1087           ip6->as_u8[7] = ip4->as_u8[1];
1088           ip6->as_u8[9] = ip4->as_u8[2];
1089           ip6->as_u8[10] = ip4->as_u8[3];
1090           break;
1091         case 56:
1092           ip6->as_u8[7] = ip4->as_u8[0];
1093           ip6->as_u8[9] = ip4->as_u8[1];
1094           ip6->as_u8[10] = ip4->as_u8[2];
1095           ip6->as_u8[11] = ip4->as_u8[3];
1096           break;
1097         case 64:
1098           ip6->as_u8[9] = ip4->as_u8[0];
1099           ip6->as_u8[10] = ip4->as_u8[1];
1100           ip6->as_u8[11] = ip4->as_u8[2];
1101           ip6->as_u8[12] = ip4->as_u8[3];
1102           break;
1103         case 96:
1104           ip6->as_u32[3] = ip4->as_u32;
1105           break;
1106         default:
1107           nat_elog_notice ("invalid prefix length");
1108           break;
1109         }
1110     }
1111   else
1112     {
1113       clib_memcpy_fast (ip6, well_known_prefix, sizeof (ip6_address_t));
1114       ip6->as_u32[3] = ip4->as_u32;
1115     }
1116 }
1117
1118 void
1119 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1120 {
1121   nat64_main_t *nm = &nat64_main;
1122   nat64_prefix_t *p, *gp = 0;
1123   u8 plen = 0;
1124
1125   /* *INDENT-OFF* */
1126   vec_foreach (p, nm->pref64)
1127     {
1128       if (p->fib_index == fib_index)
1129         {
1130           plen = p->plen;
1131           break;
1132         }
1133
1134       if (p->vrf_id == 0)
1135         gp = p;
1136     };
1137   /* *INDENT-ON* */
1138
1139   if (!plen)
1140     {
1141       if (gp)
1142         plen = gp->plen;
1143       else
1144         plen = 96;
1145     }
1146
1147   switch (plen)
1148     {
1149     case 32:
1150       ip4->as_u32 = ip6->as_u32[1];
1151       break;
1152     case 40:
1153       ip4->as_u8[0] = ip6->as_u8[5];
1154       ip4->as_u8[1] = ip6->as_u8[6];
1155       ip4->as_u8[2] = ip6->as_u8[7];
1156       ip4->as_u8[3] = ip6->as_u8[9];
1157       break;
1158     case 48:
1159       ip4->as_u8[0] = ip6->as_u8[6];
1160       ip4->as_u8[1] = ip6->as_u8[7];
1161       ip4->as_u8[2] = ip6->as_u8[9];
1162       ip4->as_u8[3] = ip6->as_u8[10];
1163       break;
1164     case 56:
1165       ip4->as_u8[0] = ip6->as_u8[7];
1166       ip4->as_u8[1] = ip6->as_u8[9];
1167       ip4->as_u8[2] = ip6->as_u8[10];
1168       ip4->as_u8[3] = ip6->as_u8[11];
1169       break;
1170     case 64:
1171       ip4->as_u8[0] = ip6->as_u8[9];
1172       ip4->as_u8[1] = ip6->as_u8[10];
1173       ip4->as_u8[2] = ip6->as_u8[11];
1174       ip4->as_u8[3] = ip6->as_u8[12];
1175       break;
1176     case 96:
1177       ip4->as_u32 = ip6->as_u32[3];
1178       break;
1179     default:
1180       nat_elog_notice ("invalid prefix length");
1181       break;
1182     }
1183 }
1184
1185 /**
1186  * @brief Per worker process checking expire time for NAT64 sessions.
1187  */
1188 static uword
1189 nat64_expire_worker_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1190                              vlib_frame_t * f)
1191 {
1192   nat64_main_t *nm = &nat64_main;
1193   u32 thread_index = vm->thread_index;
1194   nat64_db_t *db = &nm->db[thread_index];
1195   u32 now = (u32) vlib_time_now (vm);
1196
1197   nad64_db_st_free_expired (thread_index, db, now);
1198   vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
1199                            db->bib.bib_entries_num);
1200   vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1201                            db->st.st_entries_num);
1202
1203   return 0;
1204 }
1205
1206 static vlib_node_registration_t nat64_expire_worker_walk_node;
1207
1208 /* *INDENT-OFF* */
1209 VLIB_REGISTER_NODE (nat64_expire_worker_walk_node, static) = {
1210     .function = nat64_expire_worker_walk_fn,
1211     .type = VLIB_NODE_TYPE_INPUT,
1212     .state = VLIB_NODE_STATE_INTERRUPT,
1213     .name = "nat64-expire-worker-walk",
1214 };
1215 /* *INDENT-ON* */
1216
1217 static vlib_node_registration_t nat64_expire_walk_node;
1218
1219 /**
1220  * @brief Centralized process to drive per worker expire walk.
1221  */
1222 static uword
1223 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1224                       vlib_frame_t * f)
1225 {
1226   nat64_main_t *nm = &nat64_main;
1227   vlib_main_t **worker_vms = 0, *worker_vm;
1228   int i;
1229   uword event_type, *event_data = 0;
1230
1231   nm->nat64_expire_walk_node_index = nat64_expire_walk_node.index;
1232
1233   if (vec_len (vlib_mains) == 0)
1234     vec_add1 (worker_vms, vm);
1235   else
1236     {
1237       for (i = 0; i < vec_len (vlib_mains); i++)
1238         {
1239           worker_vm = vlib_mains[i];
1240           if (worker_vm)
1241             vec_add1 (worker_vms, worker_vm);
1242         }
1243     }
1244
1245   while (1)
1246     {
1247       if (nm->total_enabled_count)
1248         {
1249           vlib_process_wait_for_event_or_clock (vm, 10.0);
1250           event_type = vlib_process_get_events (vm, &event_data);
1251         }
1252       else
1253         {
1254           vlib_process_wait_for_event (vm);
1255           event_type = vlib_process_get_events (vm, &event_data);
1256         }
1257
1258       switch (event_type)
1259         {
1260         case ~0:
1261           break;
1262         case NAT64_CLEANER_RESCHEDULE:
1263           break;
1264         default:
1265           nat_elog_notice_X1 ("unknown event %d", "i4", event_type);
1266           break;
1267         }
1268
1269       for (i = 0; i < vec_len (worker_vms); i++)
1270         {
1271           worker_vm = worker_vms[i];
1272           vlib_node_set_interrupt_pending (worker_vm,
1273                                            nat64_expire_worker_walk_node.index);
1274         }
1275     }
1276
1277   return 0;
1278 }
1279
1280 /* *INDENT-OFF* */
1281 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
1282     .function = nat64_expire_walk_fn,
1283     .type = VLIB_NODE_TYPE_PROCESS,
1284     .name = "nat64-expire-walk",
1285 };
1286 /* *INDENT-ON* */
1287
1288 /*
1289  * fd.io coding-style-patch-verification: ON
1290  *
1291  * Local Variables:
1292  * eval: (c-set-style "gnu")
1293  * End:
1294  */