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