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