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