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