NAT64: free port when dynamic BIB deleted (VPP-1107)
[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 static void nat64_free_out_addr_and_port (struct nat64_db_s *db,
245                                           ip4_address_t * addr, u16 port,
246                                           u8 protocol);
247
248 void
249 nat64_set_hash (u32 bib_buckets, u32 bib_memory_size, u32 st_buckets,
250                 u32 st_memory_size)
251 {
252   nat64_main_t *nm = &nat64_main;
253   nat64_db_t *db;
254
255   nm->bib_buckets = bib_buckets;
256   nm->bib_memory_size = bib_memory_size;
257   nm->st_buckets = st_buckets;
258   nm->st_memory_size = st_memory_size;
259
260   /* *INDENT-OFF* */
261   vec_foreach (db, nm->db)
262     {
263       if (nat64_db_init (db, bib_buckets, bib_memory_size, st_buckets,
264                          st_memory_size, nat64_free_out_addr_and_port))
265         clib_warning ("NAT64 DB init failed");
266     }
267   /* *INDENT-ON* */
268 }
269
270 int
271 nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add)
272 {
273   nat64_main_t *nm = &nat64_main;
274   snat_address_t *a = 0;
275   snat_interface_t *interface;
276   int i;
277   nat64_db_t *db;
278   vlib_thread_main_t *tm = vlib_get_thread_main ();
279
280   /* Check if address already exists */
281   for (i = 0; i < vec_len (nm->addr_pool); i++)
282     {
283       if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
284         {
285           a = nm->addr_pool + i;
286           break;
287         }
288     }
289
290   if (is_add)
291     {
292       if (a)
293         return VNET_API_ERROR_VALUE_EXIST;
294
295       vec_add2 (nm->addr_pool, a, 1);
296       a->addr = *addr;
297       a->fib_index = ~0;
298       if (vrf_id != ~0)
299         a->fib_index =
300           fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
301                                              FIB_SOURCE_PLUGIN_HI);
302 #define _(N, id, n, s) \
303       clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535); \
304       a->busy_##n##_ports = 0; \
305       vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
306       foreach_snat_protocol
307 #undef _
308     }
309   else
310     {
311       if (!a)
312         return VNET_API_ERROR_NO_SUCH_ENTRY;
313
314       if (a->fib_index != ~0)
315         fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6,
316                           FIB_SOURCE_PLUGIN_HI);
317       /* Delete sessions using address */
318         /* *INDENT-OFF* */
319         vec_foreach (db, nm->db)
320           nat64_db_free_out_addr (db, &a->addr);
321 #define _(N, id, n, s) \
322       clib_bitmap_free (a->busy_##n##_port_bitmap);
323       foreach_snat_protocol
324 #undef _
325         /* *INDENT-ON* */
326       vec_del1 (nm->addr_pool, i);
327     }
328
329   /* Add/del external address to FIB */
330   /* *INDENT-OFF* */
331   pool_foreach (interface, nm->interfaces,
332   ({
333     if (nat_interface_is_inside(interface))
334       continue;
335
336     snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
337     break;
338   }));
339   /* *INDENT-ON* */
340
341   return 0;
342 }
343
344 void
345 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
346 {
347   nat64_main_t *nm = &nat64_main;
348   snat_address_t *a = 0;
349
350   /* *INDENT-OFF* */
351   vec_foreach (a, nm->addr_pool)
352     {
353       if (fn (a, ctx))
354         break;
355     };
356   /* *INDENT-ON* */
357 }
358
359 int
360 nat64_add_interface_address (u32 sw_if_index, int is_add)
361 {
362   nat64_main_t *nm = &nat64_main;
363   ip4_main_t *ip4_main = nm->ip4_main;
364   ip4_address_t *first_int_addr;
365   int i;
366
367   first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
368
369   for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
370     {
371       if (nm->auto_add_sw_if_indices[i] == sw_if_index)
372         {
373           if (is_add)
374             return VNET_API_ERROR_VALUE_EXIST;
375           else
376             {
377               /* if have address remove it */
378               if (first_int_addr)
379                 (void) nat64_add_del_pool_addr (first_int_addr, ~0, 0);
380
381               vec_del1 (nm->auto_add_sw_if_indices, i);
382               return 0;
383             }
384         }
385     }
386
387   if (!is_add)
388     return VNET_API_ERROR_NO_SUCH_ENTRY;
389
390   /* add to the auto-address list */
391   vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
392
393   /* If the address is already bound - or static - add it now */
394   if (first_int_addr)
395     (void) nat64_add_del_pool_addr (first_int_addr, ~0, 1);
396
397   return 0;
398 }
399
400 int
401 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
402 {
403   nat64_main_t *nm = &nat64_main;
404   snat_interface_t *interface = 0, *i;
405   snat_address_t *ap;
406   const char *feature_name, *arc_name;
407
408   /* Check if interface already exists */
409   /* *INDENT-OFF* */
410   pool_foreach (i, nm->interfaces,
411   ({
412     if (i->sw_if_index == sw_if_index)
413       {
414         interface = i;
415         break;
416       }
417   }));
418   /* *INDENT-ON* */
419
420   if (is_add)
421     {
422       if (interface)
423         goto set_flags;
424
425       pool_get (nm->interfaces, interface);
426       interface->sw_if_index = sw_if_index;
427       interface->flags = 0;
428     set_flags:
429       if (is_inside)
430         interface->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
431       else
432         interface->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
433     }
434   else
435     {
436       if (!interface)
437         return VNET_API_ERROR_NO_SUCH_ENTRY;
438
439       if ((nat_interface_is_inside (interface)
440            && nat_interface_is_outside (interface)))
441         interface->flags &=
442           is_inside ? ~NAT_INTERFACE_FLAG_IS_INSIDE :
443           ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
444       else
445         pool_put (nm->interfaces, interface);
446     }
447
448   if (!is_inside)
449     {
450       /* *INDENT-OFF* */
451       vec_foreach (ap, nm->addr_pool)
452         snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
453       /* *INDENT-ON* */
454     }
455
456   if (nm->sm->num_workers > 1)
457     {
458       feature_name =
459         is_inside ? "nat64-in2out-handoff" : "nat64-out2in-handoff";
460       if (nm->fq_in2out_index == ~0)
461         nm->fq_in2out_index =
462           vlib_frame_queue_main_init (nat64_in2out_node.index, 0);
463       if (nm->fq_out2in_index == ~0)
464         nm->fq_out2in_index =
465           vlib_frame_queue_main_init (nat64_out2in_node.index, 0);
466     }
467   else
468     feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
469
470   arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
471
472   return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
473                                       is_add, 0, 0);
474 }
475
476 void
477 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
478 {
479   nat64_main_t *nm = &nat64_main;
480   snat_interface_t *i = 0;
481
482   /* *INDENT-OFF* */
483   pool_foreach (i, nm->interfaces,
484   ({
485     if (fn (i, ctx))
486       break;
487   }));
488   /* *INDENT-ON* */
489 }
490
491 int
492 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
493                                ip4_address_t * addr, u16 * port,
494                                u32 thread_index)
495 {
496   nat64_main_t *nm = &nat64_main;
497   snat_main_t *sm = nm->sm;
498   snat_session_key_t k;
499   u32 ai;
500   int rv;
501
502   k.protocol = proto;
503
504   rv =
505     sm->alloc_addr_and_port (nm->addr_pool, fib_index, thread_index, &k, &ai,
506                              sm->port_per_thread, thread_index);
507
508   if (!rv)
509     {
510       *port = k.port;
511       addr->as_u32 = k.addr.as_u32;
512     }
513
514   return rv;
515 }
516
517 static void
518 nat64_free_out_addr_and_port (struct nat64_db_s *db, ip4_address_t * addr,
519                               u16 port, u8 protocol)
520 {
521   nat64_main_t *nm = &nat64_main;
522   int i;
523   snat_address_t *a;
524   u32 thread_index = db - nm->db;
525   snat_protocol_t proto = ip_proto_to_snat_proto (protocol);
526   u16 port_host_byte_order = clib_net_to_host_u16 (port);
527
528   for (i = 0; i < vec_len (nm->addr_pool); i++)
529     {
530       a = nm->addr_pool + i;
531       if (addr->as_u32 != a->addr.as_u32)
532         continue;
533       switch (proto)
534         {
535 #define _(N, j, n, s) \
536         case SNAT_PROTOCOL_##N: \
537           ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
538                   port_host_byte_order) == 1); \
539           clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
540           a->busy_##n##_ports--; \
541           a->busy_##n##_ports_per_thread[thread_index]--; \
542           break;
543           foreach_snat_protocol
544 #undef _
545         default:
546           clib_warning ("unknown protocol");
547           return;
548         }
549       break;
550     }
551 }
552
553 /**
554  * @brief Add/delete static BIB entry in worker thread.
555  */
556 static uword
557 nat64_static_bib_worker_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
558                             vlib_frame_t * f)
559 {
560   nat64_main_t *nm = &nat64_main;
561   u32 thread_index = vlib_get_thread_index ();
562   nat64_db_t *db = &nm->db[thread_index];
563   nat64_static_bib_to_update_t *static_bib;
564   nat64_db_bib_entry_t *bibe;
565   ip46_address_t addr;
566
567   /* *INDENT-OFF* */
568   pool_foreach (static_bib, nm->static_bibs,
569   ({
570     if ((static_bib->thread_index != thread_index) || (static_bib->done))
571       continue;
572
573     if (static_bib->is_add)
574       (void) nat64_db_bib_entry_create (db, &static_bib->in_addr,
575                                         &static_bib->out_addr,
576                                         static_bib->in_port,
577                                         static_bib->out_port,
578                                         static_bib->fib_index,
579                                         static_bib->proto, 1);
580     else
581       {
582         addr.as_u64[0] = static_bib->in_addr.as_u64[0];
583         addr.as_u64[1] = static_bib->in_addr.as_u64[1];
584         bibe = nat64_db_bib_entry_find (db, &addr, static_bib->in_port,
585                                         static_bib->proto,
586                                         static_bib->fib_index, 1);
587         if (bibe)
588           nat64_db_bib_entry_free (db, bibe);
589       }
590
591       static_bib->done = 1;
592   }));
593   /* *INDENT-ON* */
594
595   return 0;
596 }
597
598 static vlib_node_registration_t nat64_static_bib_worker_node;
599
600 /* *INDENT-OFF* */
601 VLIB_REGISTER_NODE (nat64_static_bib_worker_node, static) = {
602     .function = nat64_static_bib_worker_fn,
603     .type = VLIB_NODE_TYPE_INPUT,
604     .state = VLIB_NODE_STATE_INTERRUPT,
605     .name = "nat64-static-bib-worker",
606 };
607 /* *INDENT-ON* */
608
609 int
610 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
611                                 ip4_address_t * out_addr, u16 in_port,
612                                 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
613 {
614   nat64_main_t *nm = &nat64_main;
615   nat64_db_bib_entry_t *bibe;
616   u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
617                                                      FIB_SOURCE_PLUGIN_HI);
618   snat_protocol_t p = ip_proto_to_snat_proto (proto);
619   ip46_address_t addr;
620   int i;
621   snat_address_t *a;
622   u32 thread_index = 0;
623   nat64_db_t *db;
624   nat64_static_bib_to_update_t *static_bib;
625   vlib_main_t *worker_vm;
626   u32 *to_be_free = 0, *index;
627
628   if (nm->sm->num_workers > 1)
629     {
630       thread_index = nat64_get_worker_in2out (in_addr);
631       db = &nm->db[thread_index];
632     }
633   else
634     db = &nm->db[nm->sm->num_workers];
635
636   addr.as_u64[0] = in_addr->as_u64[0];
637   addr.as_u64[1] = in_addr->as_u64[1];
638   bibe =
639     nat64_db_bib_entry_find (db, &addr, clib_host_to_net_u16 (in_port),
640                              proto, fib_index, 1);
641
642   if (is_add)
643     {
644       if (bibe)
645         return VNET_API_ERROR_VALUE_EXIST;
646
647       /* outside port must be assigned to same thread as internall address */
648       if ((out_port > 1024) && (nm->sm->num_workers > 1))
649         {
650           if (thread_index != ((out_port - 1024) / nm->sm->port_per_thread))
651             return VNET_API_ERROR_INVALID_VALUE_2;
652         }
653
654       for (i = 0; i < vec_len (nm->addr_pool); i++)
655         {
656           a = nm->addr_pool + i;
657           if (out_addr->as_u32 != a->addr.as_u32)
658             continue;
659           switch (p)
660             {
661 #define _(N, j, n, s) \
662             case SNAT_PROTOCOL_##N: \
663               if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
664                                             out_port)) \
665                 return VNET_API_ERROR_INVALID_VALUE; \
666               clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
667                                         out_port, 1); \
668               if (out_port > 1024) \
669                 { \
670                   a->busy_##n##_ports++; \
671                   a->busy_##n##_ports_per_thread[thread_index]++; \
672                 } \
673               break;
674               foreach_snat_protocol
675 #undef _
676             default:
677               memset (&addr, 0, sizeof (addr));
678               addr.ip4.as_u32 = out_addr->as_u32;
679               if (nat64_db_bib_entry_find (db, &addr, 0, proto, fib_index, 0))
680                 return VNET_API_ERROR_INVALID_VALUE;
681             }
682           break;
683         }
684       if (!nm->sm->num_workers)
685         {
686           bibe =
687             nat64_db_bib_entry_create (db, in_addr, out_addr,
688                                        clib_host_to_net_u16 (in_port),
689                                        clib_host_to_net_u16 (out_port),
690                                        fib_index, proto, 1);
691           if (!bibe)
692             return VNET_API_ERROR_UNSPECIFIED;
693         }
694     }
695   else
696     {
697       if (!bibe)
698         return VNET_API_ERROR_NO_SUCH_ENTRY;
699
700       if (!nm->sm->num_workers)
701         nat64_db_bib_entry_free (db, bibe);
702     }
703
704   if (nm->sm->num_workers)
705     {
706       /* *INDENT-OFF* */
707       pool_foreach (static_bib, nm->static_bibs,
708       ({
709         if (static_bib->done)
710           vec_add1 (to_be_free, static_bib - nm->static_bibs);
711       }));
712       vec_foreach (index, to_be_free)
713         pool_put_index (nm->static_bibs, index[0]);
714       /* *INDENT-ON* */
715       vec_free (to_be_free);
716       pool_get (nm->static_bibs, static_bib);
717       static_bib->in_addr.as_u64[0] = in_addr->as_u64[0];
718       static_bib->in_addr.as_u64[1] = in_addr->as_u64[1];
719       static_bib->in_port = clib_host_to_net_u16 (in_port);
720       static_bib->out_addr.as_u32 = out_addr->as_u32;
721       static_bib->out_port = clib_host_to_net_u16 (out_port);
722       static_bib->fib_index = fib_index;
723       static_bib->proto = proto;
724       static_bib->is_add = is_add;
725       static_bib->thread_index = thread_index;
726       static_bib->done = 0;
727       worker_vm = vlib_mains[thread_index];
728       if (worker_vm)
729         vlib_node_set_interrupt_pending (worker_vm,
730                                          nat64_static_bib_worker_node.index);
731       else
732         return VNET_API_ERROR_UNSPECIFIED;
733     }
734
735   return 0;
736 }
737
738 int
739 nat64_set_udp_timeout (u32 timeout)
740 {
741   nat64_main_t *nm = &nat64_main;
742
743   if (timeout == 0)
744     nm->udp_timeout = SNAT_UDP_TIMEOUT;
745   else if (timeout < SNAT_UDP_TIMEOUT_MIN)
746     return VNET_API_ERROR_INVALID_VALUE;
747   else
748     nm->udp_timeout = timeout;
749
750   return 0;
751 }
752
753 u32
754 nat64_get_udp_timeout (void)
755 {
756   nat64_main_t *nm = &nat64_main;
757
758   return nm->udp_timeout;
759 }
760
761 int
762 nat64_set_icmp_timeout (u32 timeout)
763 {
764   nat64_main_t *nm = &nat64_main;
765
766   if (timeout == 0)
767     nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
768   else
769     nm->icmp_timeout = timeout;
770
771   return 0;
772 }
773
774 u32
775 nat64_get_icmp_timeout (void)
776 {
777   nat64_main_t *nm = &nat64_main;
778
779   return nm->icmp_timeout;
780 }
781
782 int
783 nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn)
784 {
785   nat64_main_t *nm = &nat64_main;
786
787   if (trans == 0)
788     nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
789   else
790     nm->tcp_trans_timeout = trans;
791
792   if (est == 0)
793     nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
794   else
795     nm->tcp_est_timeout = est;
796
797   if (incoming_syn == 0)
798     nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
799   else
800     nm->tcp_incoming_syn_timeout = incoming_syn;
801
802   return 0;
803 }
804
805 u32
806 nat64_get_tcp_trans_timeout (void)
807 {
808   nat64_main_t *nm = &nat64_main;
809
810   return nm->tcp_trans_timeout;
811 }
812
813 u32
814 nat64_get_tcp_est_timeout (void)
815 {
816   nat64_main_t *nm = &nat64_main;
817
818   return nm->tcp_est_timeout;
819 }
820
821 u32
822 nat64_get_tcp_incoming_syn_timeout (void)
823 {
824   nat64_main_t *nm = &nat64_main;
825
826   return nm->tcp_incoming_syn_timeout;
827 }
828
829 void
830 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
831 {
832   nat64_main_t *nm = &nat64_main;
833   u32 now = (u32) vlib_time_now (vm);
834
835   switch (ip_proto_to_snat_proto (ste->proto))
836     {
837     case SNAT_PROTOCOL_ICMP:
838       ste->expire = now + nm->icmp_timeout;
839       return;
840     case SNAT_PROTOCOL_TCP:
841       {
842         switch (ste->tcp_state)
843           {
844           case NAT64_TCP_STATE_V4_INIT:
845           case NAT64_TCP_STATE_V6_INIT:
846           case NAT64_TCP_STATE_V4_FIN_RCV:
847           case NAT64_TCP_STATE_V6_FIN_RCV:
848           case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
849           case NAT64_TCP_STATE_TRANS:
850             ste->expire = now + nm->tcp_trans_timeout;
851             return;
852           case NAT64_TCP_STATE_ESTABLISHED:
853             ste->expire = now + nm->tcp_est_timeout;
854             return;
855           default:
856             return;
857           }
858       }
859     case SNAT_PROTOCOL_UDP:
860       ste->expire = now + nm->udp_timeout;
861       return;
862     default:
863       ste->expire = now + nm->udp_timeout;
864       return;
865     }
866 }
867
868 void
869 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
870                              u8 is_ip6)
871 {
872   switch (ste->tcp_state)
873     {
874     case NAT64_TCP_STATE_CLOSED:
875       {
876         if (tcp->flags & TCP_FLAG_SYN)
877           {
878             if (is_ip6)
879               ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
880             else
881               ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
882           }
883         return;
884       }
885     case NAT64_TCP_STATE_V4_INIT:
886       {
887         if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
888           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
889         return;
890       }
891     case NAT64_TCP_STATE_V6_INIT:
892       {
893         if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
894           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
895         return;
896       }
897     case NAT64_TCP_STATE_ESTABLISHED:
898       {
899         if (tcp->flags & TCP_FLAG_FIN)
900           {
901             if (is_ip6)
902               ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
903             else
904               ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
905           }
906         else if (tcp->flags & TCP_FLAG_RST)
907           {
908             ste->tcp_state = NAT64_TCP_STATE_TRANS;
909           }
910         return;
911       }
912     case NAT64_TCP_STATE_V4_FIN_RCV:
913       {
914         if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
915           ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
916         return;
917       }
918     case NAT64_TCP_STATE_V6_FIN_RCV:
919       {
920         if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
921           ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
922         return;
923       }
924     case NAT64_TCP_STATE_TRANS:
925       {
926         if (!(tcp->flags & TCP_FLAG_RST))
927           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
928         return;
929       }
930     default:
931       return;
932     }
933 }
934
935 int
936 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
937 {
938   nat64_main_t *nm = &nat64_main;
939   nat64_prefix_t *p = 0;
940   int i;
941
942   /* Verify prefix length */
943   if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
944       && plen != 96)
945     return VNET_API_ERROR_INVALID_VALUE;
946
947   /* Check if tenant already have prefix */
948   for (i = 0; i < vec_len (nm->pref64); i++)
949     {
950       if (nm->pref64[i].vrf_id == vrf_id)
951         {
952           p = nm->pref64 + i;
953           break;
954         }
955     }
956
957   if (is_add)
958     {
959       if (!p)
960         {
961           vec_add2 (nm->pref64, p, 1);
962           p->fib_index =
963             fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
964                                                FIB_SOURCE_PLUGIN_HI);
965           p->vrf_id = vrf_id;
966         }
967
968       p->prefix.as_u64[0] = prefix->as_u64[0];
969       p->prefix.as_u64[1] = prefix->as_u64[1];
970       p->plen = plen;
971     }
972   else
973     {
974       if (!p)
975         return VNET_API_ERROR_NO_SUCH_ENTRY;
976
977       vec_del1 (nm->pref64, i);
978     }
979
980   return 0;
981 }
982
983 void
984 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
985 {
986   nat64_main_t *nm = &nat64_main;
987   nat64_prefix_t *p = 0;
988
989   /* *INDENT-OFF* */
990   vec_foreach (p, nm->pref64)
991     {
992       if (fn (p, ctx))
993         break;
994     };
995   /* *INDENT-ON* */
996 }
997
998 void
999 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1000 {
1001   nat64_main_t *nm = &nat64_main;
1002   nat64_prefix_t *p, *gp = 0, *prefix = 0;
1003
1004   /* *INDENT-OFF* */
1005   vec_foreach (p, nm->pref64)
1006     {
1007       if (p->fib_index == fib_index)
1008         {
1009           prefix = p;
1010           break;
1011         }
1012
1013       if (p->fib_index == 0)
1014         gp = p;
1015     };
1016   /* *INDENT-ON* */
1017
1018   if (!prefix)
1019     prefix = gp;
1020
1021   if (prefix)
1022     {
1023       clib_memcpy (ip6, &p->prefix, sizeof (ip6_address_t));
1024       switch (p->plen)
1025         {
1026         case 32:
1027           ip6->as_u32[1] = ip4->as_u32;
1028           break;
1029         case 40:
1030           ip6->as_u8[5] = ip4->as_u8[0];
1031           ip6->as_u8[6] = ip4->as_u8[1];
1032           ip6->as_u8[7] = ip4->as_u8[2];
1033           ip6->as_u8[9] = ip4->as_u8[3];
1034           break;
1035         case 48:
1036           ip6->as_u8[6] = ip4->as_u8[0];
1037           ip6->as_u8[7] = ip4->as_u8[1];
1038           ip6->as_u8[9] = ip4->as_u8[2];
1039           ip6->as_u8[10] = ip4->as_u8[3];
1040           break;
1041         case 56:
1042           ip6->as_u8[7] = ip4->as_u8[0];
1043           ip6->as_u8[9] = ip4->as_u8[1];
1044           ip6->as_u8[10] = ip4->as_u8[2];
1045           ip6->as_u8[11] = ip4->as_u8[3];
1046           break;
1047         case 64:
1048           ip6->as_u8[9] = ip4->as_u8[0];
1049           ip6->as_u8[10] = ip4->as_u8[1];
1050           ip6->as_u8[11] = ip4->as_u8[2];
1051           ip6->as_u8[12] = ip4->as_u8[3];
1052           break;
1053         case 96:
1054           ip6->as_u32[3] = ip4->as_u32;
1055           break;
1056         default:
1057           clib_warning ("invalid prefix length");
1058           break;
1059         }
1060     }
1061   else
1062     {
1063       clib_memcpy (ip6, well_known_prefix, sizeof (ip6_address_t));
1064       ip6->as_u32[3] = ip4->as_u32;
1065     }
1066 }
1067
1068 void
1069 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1070 {
1071   nat64_main_t *nm = &nat64_main;
1072   nat64_prefix_t *p, *gp = 0;
1073   u8 plen = 0;
1074
1075   /* *INDENT-OFF* */
1076   vec_foreach (p, nm->pref64)
1077     {
1078       if (p->fib_index == fib_index)
1079         {
1080           plen = p->plen;
1081           break;
1082         }
1083
1084       if (p->vrf_id == 0)
1085         gp = p;
1086     };
1087   /* *INDENT-ON* */
1088
1089   if (!plen)
1090     {
1091       if (gp)
1092         plen = gp->plen;
1093       else
1094         plen = 96;
1095     }
1096
1097   switch (plen)
1098     {
1099     case 32:
1100       ip4->as_u32 = ip6->as_u32[1];
1101       break;
1102     case 40:
1103       ip4->as_u8[0] = ip6->as_u8[5];
1104       ip4->as_u8[1] = ip6->as_u8[6];
1105       ip4->as_u8[2] = ip6->as_u8[7];
1106       ip4->as_u8[3] = ip6->as_u8[9];
1107       break;
1108     case 48:
1109       ip4->as_u8[0] = ip6->as_u8[6];
1110       ip4->as_u8[1] = ip6->as_u8[7];
1111       ip4->as_u8[2] = ip6->as_u8[9];
1112       ip4->as_u8[3] = ip6->as_u8[10];
1113       break;
1114     case 56:
1115       ip4->as_u8[0] = ip6->as_u8[7];
1116       ip4->as_u8[1] = ip6->as_u8[9];
1117       ip4->as_u8[2] = ip6->as_u8[10];
1118       ip4->as_u8[3] = ip6->as_u8[11];
1119       break;
1120     case 64:
1121       ip4->as_u8[0] = ip6->as_u8[9];
1122       ip4->as_u8[1] = ip6->as_u8[10];
1123       ip4->as_u8[2] = ip6->as_u8[11];
1124       ip4->as_u8[3] = ip6->as_u8[12];
1125       break;
1126     case 96:
1127       ip4->as_u32 = ip6->as_u32[3];
1128       break;
1129     default:
1130       clib_warning ("invalid prefix length");
1131       break;
1132     }
1133 }
1134
1135 /**
1136  * @brief Per worker process checking expire time for NAT64 sessions.
1137  */
1138 static uword
1139 nat64_expire_worker_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1140                              vlib_frame_t * f)
1141 {
1142   nat64_main_t *nm = &nat64_main;
1143   u32 thread_index = vlib_get_thread_index ();
1144   nat64_db_t *db = &nm->db[thread_index];
1145   u32 now = (u32) vlib_time_now (vm);
1146
1147   nad64_db_st_free_expired (db, now);
1148
1149   return 0;
1150 }
1151
1152 static vlib_node_registration_t nat64_expire_worker_walk_node;
1153
1154 /* *INDENT-OFF* */
1155 VLIB_REGISTER_NODE (nat64_expire_worker_walk_node, static) = {
1156     .function = nat64_expire_worker_walk_fn,
1157     .type = VLIB_NODE_TYPE_INPUT,
1158     .state = VLIB_NODE_STATE_INTERRUPT,
1159     .name = "nat64-expire-worker-walk",
1160 };
1161 /* *INDENT-ON* */
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   vlib_main_t **worker_vms = 0, *worker_vm;
1171   int i;
1172
1173   if (vec_len (vlib_mains) == 0)
1174     vec_add1 (worker_vms, vm);
1175   else
1176     {
1177       for (i = 0; i < vec_len (vlib_mains); i++)
1178         {
1179           worker_vm = vlib_mains[i];
1180           if (worker_vm)
1181             vec_add1 (worker_vms, worker_vm);
1182         }
1183     }
1184
1185   while (1)
1186     {
1187       vlib_process_wait_for_event_or_clock (vm, 10.0);
1188       vlib_process_get_events (vm, NULL);
1189       for (i = 0; i < vec_len (worker_vms); i++)
1190         {
1191           worker_vm = worker_vms[i];
1192           vlib_node_set_interrupt_pending (worker_vm,
1193                                            nat64_expire_worker_walk_node.index);
1194         }
1195     }
1196
1197   return 0;
1198 }
1199
1200 static vlib_node_registration_t nat64_expire_walk_node;
1201
1202 /* *INDENT-OFF* */
1203 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
1204     .function = nat64_expire_walk_fn,
1205     .type = VLIB_NODE_TYPE_PROCESS,
1206     .name = "nat64-expire-walk",
1207 };
1208 /* *INDENT-ON* */
1209
1210 /*
1211  * fd.io coding-style-patch-verification: ON
1212  *
1213  * Local Variables:
1214  * eval: (c-set-style "gnu")
1215  * End:
1216  */