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