FD.io VPP  v21.01.1
Vector Packet Processing
nat64.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 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 #include <vppinfra/crc32.h>
17 #include <vnet/fib/ip4_fib.h>
18 
21 #include <vnet/plugin/plugin.h>
22 #include <vpp/app/version.h>
23 
24 #include <nat/lib/ipfix_logging.h>
25 #include <nat/nat64/nat64.h>
26 
28 
29 /* *INDENT-OFF* */
30 /* Hook up input features */
31 VNET_FEATURE_INIT (nat64_in2out, static) = {
32  .arc_name = "ip6-unicast",
33  .node_name = "nat64-in2out",
34  .runs_before = VNET_FEATURES ("ip6-lookup"),
35  .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
36 };
37 VNET_FEATURE_INIT (nat64_out2in, static) = {
38  .arc_name = "ip4-unicast",
39  .node_name = "nat64-out2in",
40  .runs_before = VNET_FEATURES ("ip4-lookup"),
41  .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
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  .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
48 };
49 VNET_FEATURE_INIT (nat64_out2in_handoff, static) = {
50  .arc_name = "ip4-unicast",
51  .node_name = "nat64-out2in-handoff",
52  .runs_before = VNET_FEATURES ("ip4-lookup"),
53  .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
54 };
56  .version = VPP_BUILD_VER,
57  .description = "NAT64",
58 };
59 static u8 well_known_prefix[] = {
60  0x00, 0x64, 0xff, 0x9b,
61  0x00, 0x00, 0x00, 0x00,
62  0x00, 0x00, 0x00, 0x00,
63  0x00, 0x00, 0x00, 0x00
64 };
65 /* *INDENT-ON* */
66 
67 #define nat_elog_str(_str) \
68 do \
69  { \
70  ELOG_TYPE_DECLARE (e) = \
71  { \
72  .format = "nat-msg " _str, \
73  .format_args = "", \
74  }; \
75  ELOG_DATA (&vlib_global_main.elog_main, e); \
76  } while (0);
77 
78 static void
82  u32 address_length,
83  u32 if_address_index, u32 is_delete)
84 {
85  nat64_main_t *nm = &nat64_main;
86  int i, j;
87 
88  if (plugin_enabled () == 0)
89  return;
90 
91  for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
92  {
93  if (sw_if_index == nm->auto_add_sw_if_indices[i])
94  {
95  if (!is_delete)
96  {
97  /* Don't trip over lease renewal, static config */
98  for (j = 0; j < vec_len (nm->addr_pool); j++)
99  if (nm->addr_pool[j].addr.as_u32 == address->as_u32)
100  return;
101 
103  address, ~0, 1);
104  return;
105  }
106  else
107  {
109  address, ~0, 0);
110  return;
111  }
112  }
113  }
114 }
115 
116 u32
117 nat64_get_worker_in2out (ip6_address_t * addr)
118 {
119  nat64_main_t *nm = &nat64_main;
120  u32 next_worker_index = nm->first_worker_index;
121  u32 hash;
122 
123 #ifdef clib_crc32c_uses_intrinsics
124  hash = clib_crc32c ((u8 *) addr->as_u32, 16);
125 #else
126  u64 tmp = addr->as_u64[0] ^ addr->as_u64[1];
127  hash = clib_xxhash (tmp);
128 #endif
129 
130  if (PREDICT_TRUE (is_pow2 (_vec_len (nm->workers))))
131  next_worker_index += nm->workers[hash & (_vec_len (nm->workers) - 1)];
132  else
133  next_worker_index += nm->workers[hash % _vec_len (nm->workers)];
134 
135  return next_worker_index;
136 }
137 
138 u32
140 {
141  nat64_main_t *nm = &nat64_main;
142  udp_header_t *udp;
143  u16 port;
144  u32 proto;
145 
146  proto = ip_proto_to_nat_proto (ip->protocol);
147  udp = ip4_next_header (ip);
148  port = udp->dst_port;
149 
150  /* unknown protocol */
151  if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
152  {
153  nat64_db_t *db;
154  ip46_address_t daddr;
155  nat64_db_bib_entry_t *bibe;
156 
157  clib_memset (&daddr, 0, sizeof (daddr));
158  daddr.ip4.as_u32 = ip->dst_address.as_u32;
159 
160  /* *INDENT-OFF* */
161  vec_foreach (db, nm->db)
162  {
163  bibe = nat64_db_bib_entry_find (db, &daddr, 0, ip->protocol, 0, 0);
164  if (bibe)
165  return (u32) (db - nm->db);
166  }
167  /* *INDENT-ON* */
168  return vlib_get_thread_index ();
169  }
170 
171  /* ICMP */
172  if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
173  {
174  icmp46_header_t *icmp = (icmp46_header_t *) udp;
175  icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
177  (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
178  port = vnet_buffer (b)->ip.reass.l4_src_port;
179  else
180  {
181  /* if error message, then it's not fragmented and we can access it */
182  ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
183  proto = ip_proto_to_nat_proto (inner_ip->protocol);
184  void *l4_header = ip4_next_header (inner_ip);
185  switch (proto)
186  {
187  case NAT_PROTOCOL_ICMP:
188  icmp = (icmp46_header_t *) l4_header;
189  echo = (icmp_echo_header_t *) (icmp + 1);
190  port = echo->identifier;
191  break;
192  case NAT_PROTOCOL_UDP:
193  case NAT_PROTOCOL_TCP:
194  port = ((tcp_udp_header_t *) l4_header)->src_port;
195  break;
196  default:
197  return vlib_get_thread_index ();
198  }
199  }
200  }
201 
202  /* worker by outside port (TCP/UDP) */
203  port = clib_net_to_host_u16 (port);
204  if (port > 1024)
205  return nm->first_worker_index + ((port - 1024) / nm->port_per_thread);
206 
207  return vlib_get_thread_index ();
208 }
209 
210 clib_error_t *
212 {
213  nat64_main_t *nm = &nat64_main;
216  vlib_node_t *node;
217 
218  clib_memset (nm, 0, sizeof (*nm));
219 
220  nm->ip4_main = &ip4_main;
221  nm->log_class = vlib_log_register_class ("nat64", 0);
222 
223  nm->port_per_thread = 0xffff - 1024;
224 
225  nm->fq_in2out_index = ~0;
226  nm->fq_out2in_index = ~0;
227 
228  node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
229  nm->error_node_index = node->index;
230  node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out");
231  nm->in2out_node_index = node->index;
232  node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out-slowpath");
233  nm->in2out_slowpath_node_index = node->index;
234  node = vlib_get_node_by_name (vm, (u8 *) "nat64-out2in");
235  nm->out2in_node_index = node->index;
236 
237  node = vlib_get_node_by_name (vm, (u8 *) "nat64-expire-worker-walk");
239 
240  nm->fib_src_hi = fib_source_allocate ("nat64-hi",
243  nm->fib_src_low = fib_source_allocate ("nat64-low",
246 
247  // set protocol timeouts to defaults
249 
250  /* Set up the interface address add/del callback */
252  cb4.function_opaque = 0;
254 
255  /* Init counters */
256  nm->total_bibs.name = "total-bibs";
257  nm->total_bibs.stat_segment_name = "/nat64/total-bibs";
260  nm->total_sessions.name = "total-sessions";
261  nm->total_sessions.stat_segment_name = "/nat64/total-sessions";
264 
265  uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
266  if (p)
267  {
269  tr = (vlib_thread_registration_t *) p[0];
270  if (tr)
271  {
272  nm->num_workers = tr->count;
274  }
275  }
276 
277  if (nm->num_workers > 1)
278  {
279  int i;
280  uword *bitmap = 0;
281 
282  for (i = 0; i < nm->num_workers; i++)
283  bitmap = clib_bitmap_set (bitmap, i, 1);
284 
285  /* *INDENT-OFF* */
286  clib_bitmap_foreach (i, bitmap)
287  {
288  vec_add1(nm->workers, i);
289  }
290  /* *INDENT-ON* */
291 
292  clib_bitmap_free (bitmap);
293 
294  nm->port_per_thread = (0xffff - 1024) / _vec_len (nm->workers);
295  }
296 
297  /* Init IPFIX logging */
299 
300 #define _(x) \
301  nm->counters.in2out.x.name = #x; \
302  nm->counters.in2out.x.stat_segment_name = "/nat64/in2out/" #x; \
303  nm->counters.out2in.x.name = #x; \
304  nm->counters.out2in.x.stat_segment_name = "/nat64/out2in/" #x;
306 #undef _
307  return nat64_api_hookup (vm);
308 }
309 
311 
312 static void nat64_free_out_addr_and_port (struct nat64_db_s *db,
313  ip4_address_t * addr, u16 port,
314  u8 protocol);
315 
316 int
318 {
320  nat64_main_t *nm = &nat64_main;
321  nat64_db_t *db;
322  int rv = 0;
323 
324  vec_validate (nm->db, tm->n_vlib_mains - 1);
325 
326  /* *INDENT-OFF* */
327  vec_foreach (db, nm->db)
328  {
330  {
331  nat64_log_err ("NAT64 DB init failed");
332  rv = 1;
333  }
334  }
335  /* *INDENT-ON* */
336 
337  return rv;
338 }
339 
340 int
342 {
343  nat64_main_t *nm = &nat64_main;
344  nat64_db_t *db;
345  int rv = 0;
346 
347  /* *INDENT-OFF* */
348  vec_foreach (db, nm->db)
349  {
350  if (nat64_db_free (db))
351  {
352  nat64_log_err ("NAT64 DB free failed");
353  rv = 1;
354  }
355  }
356  /* *INDENT-ON* */
357 
358  vec_free (nm->db);
359 
360  return rv;
361 }
362 
363 int
365  ip4_address_t * addr, u32 vrf_id, u8 is_add)
366 {
367  nat64_main_t *nm = &nat64_main;
368  nat64_address_t *a = 0;
369  nat64_interface_t *interface;
370  int i;
371  nat64_db_t *db;
373 
374  /* Check if address already exists */
375  for (i = 0; i < vec_len (nm->addr_pool); i++)
376  {
377  if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
378  {
379  a = nm->addr_pool + i;
380  break;
381  }
382  }
383 
384  if (is_add)
385  {
386  if (a)
387  return VNET_API_ERROR_VALUE_EXIST;
388 
389  vec_add2 (nm->addr_pool, a, 1);
390  a->addr = *addr;
391  a->fib_index = ~0;
392  if (vrf_id != ~0)
393  a->fib_index =
395  nm->fib_src_hi);
396 #define _(N, id, n, s) \
397  clib_memset (a->busy_##n##_port_refcounts, 0, sizeof(a->busy_##n##_port_refcounts)); \
398  a->busy_##n##_ports = 0; \
399  vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
401 #undef _
402  }
403  else
404  {
405  if (!a)
406  return VNET_API_ERROR_NO_SUCH_ENTRY;
407 
408  if (a->fib_index != ~0)
410  /* Delete sessions using address */
411  /* *INDENT-OFF* */
412  vec_foreach (db, nm->db)
413  {
414  nat64_db_free_out_addr (thread_index, db, &a->addr);
415  vlib_set_simple_counter (&nm->total_bibs, db - nm->db, 0,
416  db->bib.bib_entries_num);
417  vlib_set_simple_counter (&nm->total_sessions, db - nm->db, 0,
418  db->st.st_entries_num);
419  }
420  /* *INDENT-ON* */
421  vec_del1 (nm->addr_pool, i);
422  }
423 
424  /* Add/del external address to FIB */
425  /* *INDENT-OFF* */
426  pool_foreach (interface, nm->interfaces)
427  {
428  if (nat64_interface_is_inside(interface))
429  continue;
430 
431  nat64_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
432  break;
433  }
434  /* *INDENT-ON* */
435 
436  return 0;
437 }
438 
439 void
441 {
442  nat64_main_t *nm = &nat64_main;
443  nat64_address_t *a = 0;
444 
445  /* *INDENT-OFF* */
446  vec_foreach (a, nm->addr_pool)
447  {
448  if (fn (a, ctx))
449  break;
450  };
451  /* *INDENT-ON* */
452 }
453 
454 int
456 {
457  nat64_main_t *nm = &nat64_main;
459  ip4_address_t *first_int_addr;
460  int i;
461 
462  first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
463 
464  for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
465  {
466  if (nm->auto_add_sw_if_indices[i] == sw_if_index)
467  {
468  if (is_add)
469  return VNET_API_ERROR_VALUE_EXIST;
470  else
471  {
472  /* if have address remove it */
473  if (first_int_addr)
475  first_int_addr, ~0, 0);
477  return 0;
478  }
479  }
480  }
481 
482  if (!is_add)
483  return VNET_API_ERROR_NO_SUCH_ENTRY;
484 
485  /* add to the auto-address list */
486  vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
487 
488  /* If the address is already bound - or static - add it now */
489  if (first_int_addr)
491  first_int_addr, ~0, 1);
492 
493  return 0;
494 }
495 
496 static void
498 {
499 #define _(x) \
500  vlib_validate_simple_counter (&nm->counters.in2out.x, sw_if_index); \
501  vlib_zero_simple_counter (&nm->counters.in2out.x, sw_if_index); \
502  vlib_validate_simple_counter (&nm->counters.out2in.x, sw_if_index); \
503  vlib_zero_simple_counter (&nm->counters.out2in.x, sw_if_index);
505 #undef _
506 }
507 
508 void
510  int is_add)
511 {
512  nat64_main_t *nm = &nat64_main;
513  fib_prefix_t prefix = {
514  .fp_len = p_len,
515  .fp_proto = FIB_PROTOCOL_IP4,
516  .fp_addr = {
517  .ip4.as_u32 = addr->as_u32,
518  },
519  };
520  u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
521 
522  if (is_add)
524  &prefix,
525  nm->fib_src_low,
530  NULL,
531  sw_if_index,
532  ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
533  else
534  fib_table_entry_delete (fib_index, &prefix, nm->fib_src_low);
535 }
536 
537 int
539 {
541  nat64_main_t *nm = &nat64_main;
542  nat64_interface_t *interface = 0, *i;
543  nat64_address_t *ap;
544  const char *feature_name, *arc_name;
545 
546  // TODO: is enabled ? we can't signal if it is not
547 
548  /* Check if interface already exists */
549  /* *INDENT-OFF* */
550  pool_foreach (i, nm->interfaces)
551  {
552  if (i->sw_if_index == sw_if_index)
553  {
554  interface = i;
555  break;
556  }
557  }
558  /* *INDENT-ON* */
559 
560  if (is_add)
561  {
562  if (interface)
563  goto set_flags;
564 
565  pool_get (nm->interfaces, interface);
566  interface->sw_if_index = sw_if_index;
567  interface->flags = 0;
568  nat64_validate_counters (nm, sw_if_index);
569  set_flags:
570  if (is_inside)
571  interface->flags |= NAT64_INTERFACE_FLAG_IS_INSIDE;
572  else
573  interface->flags |= NAT64_INTERFACE_FLAG_IS_OUTSIDE;
574 
575  nm->total_enabled_count++;
579 
580  }
581  else
582  {
583  if (!interface)
584  return VNET_API_ERROR_NO_SUCH_ENTRY;
585 
586  if ((nat64_interface_is_inside (interface)
587  && nat64_interface_is_outside (interface)))
588  interface->flags &=
589  is_inside ? ~NAT64_INTERFACE_FLAG_IS_INSIDE :
591  else
592  pool_put (nm->interfaces, interface);
593 
594  nm->total_enabled_count--;
595  }
596 
597  if (!is_inside)
598  {
599  /* *INDENT-OFF* */
600  vec_foreach (ap, nm->addr_pool)
601  nat64_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, is_add);
602  /* *INDENT-ON* */
603  }
604 
605  if (nm->num_workers > 1)
606  {
607  feature_name =
608  is_inside ? "nat64-in2out-handoff" : "nat64-out2in-handoff";
609  if (nm->fq_in2out_index == ~0)
610  nm->fq_in2out_index =
612  if (nm->fq_out2in_index == ~0)
613  nm->fq_out2in_index =
615  }
616  else
617  feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
618 
619  arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
620 
621  if (is_inside)
622  {
623  int rv = ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
624  if (rv)
625  return rv;
626  }
627  else
628  {
629  int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
630  if (rv)
631  return rv;
632  }
633 
634  return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
635  is_add, 0, 0);
636 }
637 
638 void
640 {
641  nat64_main_t *nm = &nat64_main;
642  nat64_interface_t *i = 0;
643 
644  /* *INDENT-OFF* */
645  pool_foreach (i, nm->interfaces)
646  {
647  if (fn (i, ctx))
648  break;
649  }
650  /* *INDENT-ON* */
651 }
652 
653 // TODO: plugin independent
656 {
657  nat64_main_t *nm = &nat64_main;
658  u32 rwide;
659  u16 r;
660 
661  rwide = random_u32 (&nm->random_seed);
662  r = rwide & 0xFFFF;
663  if (r >= min && r <= max)
664  return r;
665 
666  return min + (rwide % (max - min + 1));
667 }
668 
671  u32 fib_index,
672  u32 thread_index,
674  ip4_address_t * addr,
675  u16 * port,
676  u16 port_per_thread, u32 nat_thread_index)
677 {
678  int i;
679  nat64_address_t *a, *ga = 0;
680  u32 portnum;
681 
682  for (i = 0; i < vec_len (addresses); i++)
683  {
684  a = addresses + i;
685  switch (proto)
686  {
687 #define _(N, j, n, s) \
688  case NAT_PROTOCOL_##N: \
689  if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
690  { \
691  if (a->fib_index == fib_index) \
692  { \
693  while (1) \
694  { \
695  portnum = (port_per_thread * \
696  nat_thread_index) + \
697  nat64_random_port(0, port_per_thread - 1) + 1024; \
698  if (a->busy_##n##_port_refcounts[portnum]) \
699  continue; \
700  --a->busy_##n##_port_refcounts[portnum]; \
701  a->busy_##n##_ports_per_thread[thread_index]++; \
702  a->busy_##n##_ports++; \
703  *addr = a->addr; \
704  *port = clib_host_to_net_u16(portnum); \
705  return 0; \
706  } \
707  } \
708  else if (a->fib_index == ~0) \
709  { \
710  ga = a; \
711  } \
712  } \
713  break;
715 #undef _
716  default:
717  return 1;
718  }
719 
720  }
721 
722  if (ga)
723  {
724  a = ga;
725  switch (proto)
726  {
727 #define _(N, j, n, s) \
728  case NAT_PROTOCOL_##N: \
729  while (1) \
730  { \
731  portnum = (port_per_thread * \
732  nat_thread_index) + \
733  nat64_random_port(0, port_per_thread - 1) + 1024; \
734  if (a->busy_##n##_port_refcounts[portnum]) \
735  continue; \
736  ++a->busy_##n##_port_refcounts[portnum]; \
737  a->busy_##n##_ports_per_thread[thread_index]++; \
738  a->busy_##n##_ports++; \
739  *addr = a->addr; \
740  *port = clib_host_to_net_u16(portnum); \
741  return 0; \
742  }
743  break;
745 #undef _
746  default:
747  return 1;
748  }
749  }
750 
751  /* Totally out of translations to use... */
752  nat_ipfix_logging_addresses_exhausted (thread_index, 0);
753  return 1;
754 }
755 
756 int
758  ip4_address_t * addr, u16 * port,
759  u32 thread_index)
760 {
761  nat64_main_t *nm = &nat64_main;
762  u32 worker_index = 0;
763  int rv;
764 
765  if (nm->num_workers > 1)
766  worker_index = thread_index - nm->first_worker_index;
767 
768  rv = nat64_alloc_addr_and_port_default (nm->addr_pool, fib_index,
769  thread_index,
770  proto, addr, port,
771  nm->port_per_thread, worker_index);
772 
773  return rv;
774 }
775 
776 static void
778  u16 port, u8 protocol)
779 {
780  nat64_main_t *nm = &nat64_main;
781  u32 thread_index = db - nm->db;
783  u16 port_host_byte_order = clib_net_to_host_u16 (port);
785  int i;
786 
787  for (i = 0; i < vec_len (nm->addr_pool); i++)
788  {
789  a = nm->addr_pool + i;
790  if (addr->as_u32 != a->addr.as_u32)
791  continue;
792  switch (proto)
793  {
794 #define _(N, j, n, s) \
795  case NAT_PROTOCOL_##N: \
796  ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
797  --a->busy_##n##_port_refcounts[port_host_byte_order]; \
798  a->busy_##n##_ports--; \
799  a->busy_##n##_ports_per_thread[thread_index]--; \
800  break;
802 #undef _
803  default:
804  nat_elog_str ("unknown protocol");
805  return;
806  }
807  break;
808  }
809 }
810 
811 /**
812  * @brief Add/delete static BIB entry in worker thread.
813  */
814 static uword
816  vlib_frame_t * f)
817 {
818  nat64_main_t *nm = &nat64_main;
819  u32 thread_index = vm->thread_index;
820  nat64_db_t *db = &nm->db[thread_index];
821  nat64_static_bib_to_update_t *static_bib;
822  nat64_db_bib_entry_t *bibe;
823  ip46_address_t addr;
824 
825  /* *INDENT-OFF* */
826  pool_foreach (static_bib, nm->static_bibs)
827  {
828  if ((static_bib->thread_index != thread_index) || (static_bib->done))
829  continue;
830 
831  if (static_bib->is_add)
832  {
833  (void) nat64_db_bib_entry_create (thread_index, db,
834  &static_bib->in_addr,
835  &static_bib->out_addr,
836  static_bib->in_port,
837  static_bib->out_port,
838  static_bib->fib_index,
839  static_bib->proto, 1);
840  vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
841  db->bib.bib_entries_num);
842  }
843  else
844  {
845  addr.as_u64[0] = static_bib->in_addr.as_u64[0];
846  addr.as_u64[1] = static_bib->in_addr.as_u64[1];
847  bibe = nat64_db_bib_entry_find (db, &addr, static_bib->in_port,
848  static_bib->proto,
849  static_bib->fib_index, 1);
850  if (bibe)
851  {
852  nat64_db_bib_entry_free (thread_index, db, bibe);
853  vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
854  db->bib.bib_entries_num);
855  vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
856  db->st.st_entries_num);
857  }
858  }
859 
860  static_bib->done = 1;
861  }
862  /* *INDENT-ON* */
863 
864  return 0;
865 }
866 
868 
869 /* *INDENT-OFF* */
871  .function = nat64_static_bib_worker_fn,
872  .type = VLIB_NODE_TYPE_INPUT,
873  .state = VLIB_NODE_STATE_INTERRUPT,
874  .name = "nat64-static-bib-worker",
875 };
876 /* *INDENT-ON* */
877 
878 int
879 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
880  ip4_address_t * out_addr, u16 in_port,
881  u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
882 {
883  nat64_main_t *nm = &nat64_main;
884  nat64_db_bib_entry_t *bibe;
886  nm->fib_src_hi);
888  ip46_address_t addr;
889  int i;
891  u32 thread_index = 0;
892  nat64_db_t *db;
893  nat64_static_bib_to_update_t *static_bib;
894  vlib_main_t *worker_vm;
895  u32 *to_be_free = 0, *index;
896 
897  if (nm->num_workers > 1)
898  {
899  thread_index = nat64_get_worker_in2out (in_addr);
900  db = &nm->db[thread_index];
901  }
902  else
903  db = &nm->db[nm->num_workers];
904 
905  addr.as_u64[0] = in_addr->as_u64[0];
906  addr.as_u64[1] = in_addr->as_u64[1];
907  bibe =
908  nat64_db_bib_entry_find (db, &addr, clib_host_to_net_u16 (in_port),
909  proto, fib_index, 1);
910 
911  if (is_add)
912  {
913  if (bibe)
914  return VNET_API_ERROR_VALUE_EXIST;
915 
916  /* outside port must be assigned to same thread as internall address */
917  if ((out_port > 1024) && (nm->num_workers > 1))
918  {
919  if (thread_index != ((out_port - 1024) / nm->port_per_thread))
920  return VNET_API_ERROR_INVALID_VALUE_2;
921  }
922 
923  for (i = 0; i < vec_len (nm->addr_pool); i++)
924  {
925  a = nm->addr_pool + i;
926  if (out_addr->as_u32 != a->addr.as_u32)
927  continue;
928  switch (p)
929  {
930 #define _(N, j, n, s) \
931  case NAT_PROTOCOL_##N: \
932  if (a->busy_##n##_port_refcounts[out_port]) \
933  return VNET_API_ERROR_INVALID_VALUE; \
934  ++a->busy_##n##_port_refcounts[out_port]; \
935  if (out_port > 1024) \
936  { \
937  a->busy_##n##_ports++; \
938  a->busy_##n##_ports_per_thread[thread_index]++; \
939  } \
940  break;
942 #undef _
943  default:
944  clib_memset (&addr, 0, sizeof (addr));
945  addr.ip4.as_u32 = out_addr->as_u32;
946  if (nat64_db_bib_entry_find (db, &addr, 0, proto, fib_index, 0))
947  return VNET_API_ERROR_INVALID_VALUE;
948  }
949  break;
950  }
951  if (!nm->num_workers)
952  {
953  bibe =
954  nat64_db_bib_entry_create (thread_index, db, in_addr, out_addr,
955  clib_host_to_net_u16 (in_port),
956  clib_host_to_net_u16 (out_port),
957  fib_index, proto, 1);
958  if (!bibe)
959  return VNET_API_ERROR_UNSPECIFIED;
960 
961  vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
962  db->bib.bib_entries_num);
963  }
964  }
965  else
966  {
967  if (!bibe)
968  return VNET_API_ERROR_NO_SUCH_ENTRY;
969 
970  if (!nm->num_workers)
971  {
972  nat64_db_bib_entry_free (thread_index, db, bibe);
973  vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
974  db->bib.bib_entries_num);
975  }
976  }
977 
978  if (nm->num_workers)
979  {
980  /* *INDENT-OFF* */
981  pool_foreach (static_bib, nm->static_bibs)
982  {
983  if (static_bib->done)
984  vec_add1 (to_be_free, static_bib - nm->static_bibs);
985  }
986  vec_foreach (index, to_be_free)
987  pool_put_index (nm->static_bibs, index[0]);
988  /* *INDENT-ON* */
989  vec_free (to_be_free);
990  pool_get (nm->static_bibs, static_bib);
991  static_bib->in_addr.as_u64[0] = in_addr->as_u64[0];
992  static_bib->in_addr.as_u64[1] = in_addr->as_u64[1];
993  static_bib->in_port = clib_host_to_net_u16 (in_port);
994  static_bib->out_addr.as_u32 = out_addr->as_u32;
995  static_bib->out_port = clib_host_to_net_u16 (out_port);
996  static_bib->fib_index = fib_index;
997  static_bib->proto = proto;
998  static_bib->is_add = is_add;
999  static_bib->thread_index = thread_index;
1000  static_bib->done = 0;
1001  worker_vm = vlib_mains[thread_index];
1002  if (worker_vm)
1005  else
1006  return VNET_API_ERROR_UNSPECIFIED;
1007  }
1008 
1009  return 0;
1010 }
1011 
1012 int
1014 {
1015  nat64_main_t *nm = &nat64_main;
1016 
1017  if (timeout == 0)
1019  else
1020  nm->udp_timeout = timeout;
1021 
1022  return 0;
1023 }
1024 
1025 u32
1027 {
1028  nat64_main_t *nm = &nat64_main;
1029 
1030  return nm->udp_timeout;
1031 }
1032 
1033 int
1035 {
1036  nat64_main_t *nm = &nat64_main;
1037 
1038  if (timeout == 0)
1040  else
1041  nm->icmp_timeout = timeout;
1042 
1043  return 0;
1044 }
1045 
1046 void
1048 {
1049  nat64_main_t *nm = &nat64_main;
1050 
1055 }
1056 
1057 u32
1059 {
1060  nat64_main_t *nm = &nat64_main;
1061 
1062  return nm->icmp_timeout;
1063 }
1064 
1065 int
1067 {
1068  nat64_main_t *nm = &nat64_main;
1069 
1070  if (trans == 0)
1072  else
1073  nm->tcp_trans_timeout = trans;
1074 
1075  if (est == 0)
1077  else
1078  nm->tcp_est_timeout = est;
1079 
1080  return 0;
1081 }
1082 
1083 u32
1085 {
1086  nat64_main_t *nm = &nat64_main;
1087 
1088  return nm->tcp_trans_timeout;
1089 }
1090 
1091 u32
1093 {
1094  nat64_main_t *nm = &nat64_main;
1095 
1096  return nm->tcp_est_timeout;
1097 }
1098 
1099 void
1100 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
1101 {
1102  nat64_main_t *nm = &nat64_main;
1103  u32 now = (u32) vlib_time_now (vm);
1104 
1105  switch (ip_proto_to_nat_proto (ste->proto))
1106  {
1107  case NAT_PROTOCOL_ICMP:
1108  ste->expire = now + nm->icmp_timeout;
1109  return;
1110  case NAT_PROTOCOL_TCP:
1111  {
1112  switch (ste->tcp_state)
1113  {
1114  case NAT64_TCP_STATE_V4_INIT:
1115  case NAT64_TCP_STATE_V6_INIT:
1116  case NAT64_TCP_STATE_V4_FIN_RCV:
1117  case NAT64_TCP_STATE_V6_FIN_RCV:
1118  case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
1119  case NAT64_TCP_STATE_TRANS:
1120  ste->expire = now + nm->tcp_trans_timeout;
1121  return;
1122  case NAT64_TCP_STATE_ESTABLISHED:
1123  ste->expire = now + nm->tcp_est_timeout;
1124  return;
1125  default:
1126  return;
1127  }
1128  }
1129  case NAT_PROTOCOL_UDP:
1130  ste->expire = now + nm->udp_timeout;
1131  return;
1132  default:
1133  ste->expire = now + nm->udp_timeout;
1134  return;
1135  }
1136 }
1137 
1138 void
1139 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
1140  u8 is_ip6)
1141 {
1142  switch (ste->tcp_state)
1143  {
1144  case NAT64_TCP_STATE_CLOSED:
1145  {
1146  if (tcp->flags & TCP_FLAG_SYN)
1147  {
1148  if (is_ip6)
1149  ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
1150  else
1151  ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
1152  }
1153  return;
1154  }
1155  case NAT64_TCP_STATE_V4_INIT:
1156  {
1157  if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
1158  ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
1159  return;
1160  }
1161  case NAT64_TCP_STATE_V6_INIT:
1162  {
1163  if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
1164  ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
1165  return;
1166  }
1167  case NAT64_TCP_STATE_ESTABLISHED:
1168  {
1169  if (tcp->flags & TCP_FLAG_FIN)
1170  {
1171  if (is_ip6)
1172  ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
1173  else
1174  ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
1175  }
1176  else if (tcp->flags & TCP_FLAG_RST)
1177  {
1178  ste->tcp_state = NAT64_TCP_STATE_TRANS;
1179  }
1180  return;
1181  }
1182  case NAT64_TCP_STATE_V4_FIN_RCV:
1183  {
1184  if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
1185  ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
1186  return;
1187  }
1188  case NAT64_TCP_STATE_V6_FIN_RCV:
1189  {
1190  if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
1191  ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
1192  return;
1193  }
1194  case NAT64_TCP_STATE_TRANS:
1195  {
1196  if (!(tcp->flags & TCP_FLAG_RST))
1197  ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
1198  return;
1199  }
1200  default:
1201  return;
1202  }
1203 }
1204 
1205 int
1206 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
1207 {
1208  nat64_main_t *nm = &nat64_main;
1209  nat64_prefix_t *p = 0;
1210  int i;
1211 
1212  /* Verify prefix length */
1213  if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
1214  && plen != 96)
1215  return VNET_API_ERROR_INVALID_VALUE;
1216 
1217  /* Check if tenant already have prefix */
1218  for (i = 0; i < vec_len (nm->pref64); i++)
1219  {
1220  if (nm->pref64[i].vrf_id == vrf_id)
1221  {
1222  p = nm->pref64 + i;
1223  break;
1224  }
1225  }
1226 
1227  if (is_add)
1228  {
1229  if (!p)
1230  {
1231  vec_add2 (nm->pref64, p, 1);
1232  p->fib_index =
1234  nm->fib_src_hi);
1235  p->vrf_id = vrf_id;
1236  }
1237 
1238  p->prefix.as_u64[0] = prefix->as_u64[0];
1239  p->prefix.as_u64[1] = prefix->as_u64[1];
1240  p->plen = plen;
1241  }
1242  else
1243  {
1244  if (!p)
1245  return VNET_API_ERROR_NO_SUCH_ENTRY;
1246 
1247  // TODO: missing fib_table_unlock ?
1248 
1249  vec_del1 (nm->pref64, i);
1250  }
1251 
1252  return 0;
1253 }
1254 
1255 void
1257 {
1258  nat64_main_t *nm = &nat64_main;
1259  nat64_prefix_t *p = 0;
1260 
1261  /* *INDENT-OFF* */
1262  vec_foreach (p, nm->pref64)
1263  {
1264  if (fn (p, ctx))
1265  break;
1266  };
1267  /* *INDENT-ON* */
1268 }
1269 
1270 void
1271 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1272 {
1273  nat64_main_t *nm = &nat64_main;
1274  nat64_prefix_t *p, *gp = 0, *prefix = 0;
1275 
1276  /* *INDENT-OFF* */
1277  vec_foreach (p, nm->pref64)
1278  {
1279  if (p->fib_index == fib_index)
1280  {
1281  prefix = p;
1282  break;
1283  }
1284 
1285  if (p->fib_index == 0)
1286  gp = p;
1287  };
1288  /* *INDENT-ON* */
1289 
1290  if (!prefix)
1291  prefix = gp;
1292 
1293  if (prefix)
1294  {
1295  clib_memcpy_fast (ip6, &p->prefix, sizeof (ip6_address_t));
1296  switch (p->plen)
1297  {
1298  case 32:
1299  ip6->as_u32[1] = ip4->as_u32;
1300  break;
1301  case 40:
1302  ip6->as_u8[5] = ip4->as_u8[0];
1303  ip6->as_u8[6] = ip4->as_u8[1];
1304  ip6->as_u8[7] = ip4->as_u8[2];
1305  ip6->as_u8[9] = ip4->as_u8[3];
1306  break;
1307  case 48:
1308  ip6->as_u8[6] = ip4->as_u8[0];
1309  ip6->as_u8[7] = ip4->as_u8[1];
1310  ip6->as_u8[9] = ip4->as_u8[2];
1311  ip6->as_u8[10] = ip4->as_u8[3];
1312  break;
1313  case 56:
1314  ip6->as_u8[7] = ip4->as_u8[0];
1315  ip6->as_u8[9] = ip4->as_u8[1];
1316  ip6->as_u8[10] = ip4->as_u8[2];
1317  ip6->as_u8[11] = ip4->as_u8[3];
1318  break;
1319  case 64:
1320  ip6->as_u8[9] = ip4->as_u8[0];
1321  ip6->as_u8[10] = ip4->as_u8[1];
1322  ip6->as_u8[11] = ip4->as_u8[2];
1323  ip6->as_u8[12] = ip4->as_u8[3];
1324  break;
1325  case 96:
1326  ip6->as_u32[3] = ip4->as_u32;
1327  break;
1328  default:
1329  nat_elog_str ("invalid prefix length");
1330  break;
1331  }
1332  }
1333  else
1334  {
1335  clib_memcpy_fast (ip6, well_known_prefix, sizeof (ip6_address_t));
1336  ip6->as_u32[3] = ip4->as_u32;
1337  }
1338 }
1339 
1340 void
1341 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1342 {
1343  nat64_main_t *nm = &nat64_main;
1344  nat64_prefix_t *p, *gp = 0;
1345  u8 plen = 0;
1346 
1347  /* *INDENT-OFF* */
1348  vec_foreach (p, nm->pref64)
1349  {
1350  if (p->fib_index == fib_index)
1351  {
1352  plen = p->plen;
1353  break;
1354  }
1355 
1356  if (p->vrf_id == 0)
1357  gp = p;
1358  };
1359  /* *INDENT-ON* */
1360 
1361  if (!plen)
1362  {
1363  if (gp)
1364  plen = gp->plen;
1365  else
1366  plen = 96;
1367  }
1368 
1369  switch (plen)
1370  {
1371  case 32:
1372  ip4->as_u32 = ip6->as_u32[1];
1373  break;
1374  case 40:
1375  ip4->as_u8[0] = ip6->as_u8[5];
1376  ip4->as_u8[1] = ip6->as_u8[6];
1377  ip4->as_u8[2] = ip6->as_u8[7];
1378  ip4->as_u8[3] = ip6->as_u8[9];
1379  break;
1380  case 48:
1381  ip4->as_u8[0] = ip6->as_u8[6];
1382  ip4->as_u8[1] = ip6->as_u8[7];
1383  ip4->as_u8[2] = ip6->as_u8[9];
1384  ip4->as_u8[3] = ip6->as_u8[10];
1385  break;
1386  case 56:
1387  ip4->as_u8[0] = ip6->as_u8[7];
1388  ip4->as_u8[1] = ip6->as_u8[9];
1389  ip4->as_u8[2] = ip6->as_u8[10];
1390  ip4->as_u8[3] = ip6->as_u8[11];
1391  break;
1392  case 64:
1393  ip4->as_u8[0] = ip6->as_u8[9];
1394  ip4->as_u8[1] = ip6->as_u8[10];
1395  ip4->as_u8[2] = ip6->as_u8[11];
1396  ip4->as_u8[3] = ip6->as_u8[12];
1397  break;
1398  case 96:
1399  ip4->as_u32 = ip6->as_u32[3];
1400  break;
1401  default:
1402  nat_elog_str ("invalid prefix length");
1403  break;
1404  }
1405 }
1406 
1407 /**
1408  * @brief Per worker process checking expire time for NAT64 sessions.
1409  */
1410 static uword
1412  vlib_frame_t * f)
1413 {
1414  nat64_main_t *nm = &nat64_main;
1415  u32 thread_index = vm->thread_index;
1416  nat64_db_t *db;
1417  u32 now;
1418 
1419  // TODO: barier sync on plugin enabled
1420  if (plugin_enabled () == 0)
1421  return 0;
1422 
1423  db = &nm->db[thread_index];
1424  now = (u32) vlib_time_now (vm);
1425 
1426  nad64_db_st_free_expired (thread_index, db, now);
1427  vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
1428  db->bib.bib_entries_num);
1429  vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1430  db->st.st_entries_num);
1431  return 0;
1432 }
1433 
1434 /* *INDENT-OFF* */
1436  .function = nat64_expire_worker_walk_fn,
1437  .type = VLIB_NODE_TYPE_INPUT,
1438  .state = VLIB_NODE_STATE_INTERRUPT,
1439  .name = "nat64-expire-worker-walk",
1440 };
1441 /* *INDENT-ON* */
1442 
1443 /**
1444  * @brief Centralized process to drive per worker expire walk.
1445  */
1446 static uword
1448  vlib_frame_t * f)
1449 {
1450  nat64_main_t *nm = &nat64_main;
1451  vlib_main_t **worker_vms = 0, *worker_vm;
1452  int i;
1453  uword event_type, *event_data = 0;
1454 
1455  if (vec_len (vlib_mains) == 0)
1456  vec_add1 (worker_vms, vm);
1457  else
1458  {
1459  for (i = 0; i < vec_len (vlib_mains); i++)
1460  {
1461  worker_vm = vlib_mains[i];
1462  if (worker_vm)
1463  vec_add1 (worker_vms, worker_vm);
1464  }
1465  }
1466 
1467  while (1)
1468  {
1469  if (nm->total_enabled_count)
1470  {
1472  event_type = vlib_process_get_events (vm, &event_data);
1473  }
1474  else
1475  {
1477  event_type = vlib_process_get_events (vm, &event_data);
1478  }
1479 
1480  switch (event_type)
1481  {
1482  case ~0:
1483  break;
1485  break;
1486  default:
1487  nat64_log_err ("unknown event %u", event_type);
1488  break;
1489  }
1490 
1491  for (i = 0; i < vec_len (worker_vms); i++)
1492  {
1493  worker_vm = worker_vms[i];
1496  }
1497  }
1498 
1499  return 0;
1500 }
1501 
1502 void
1504 {
1505  nat64_main_t *nm = &nat64_main;
1506 
1507  if (nm->expire_walk_node_index)
1508  return;
1510  "nat64-expire-walk",
1512  16 /* stack_bytes */ );
1513 }
1514 
1515 int
1517 {
1518  nat64_main_t *nm = &nat64_main;
1519 
1520  if (plugin_enabled () == 1)
1521  {
1522  nat64_log_err ("plugin already enabled!");
1523  return 1;
1524  }
1525 
1526  if (!c.bib_buckets)
1527  c.bib_buckets = 1024;
1528 
1529  if (!c.bib_memory_size)
1530  c.bib_memory_size = 128 << 20;
1531 
1532  if (!c.st_buckets)
1533  c.st_buckets = 2048;
1534 
1535  if (!c.st_memory_size)
1536  c.st_memory_size = 256 << 20;
1537 
1538  nm->config = c;
1539 
1540  if (nat64_init_hash (c))
1541  {
1542  nat64_log_err ("initializing hashes failed!");
1543  return 1;
1544  }
1545 
1547 
1548  nm->enabled = 1;
1549  return 0;
1550 }
1551 
1552 int
1554 {
1555  nat64_main_t *nm = &nat64_main;
1556  vnet_main_t *vnm = vnet_get_main ();
1557  int rv = 0;
1558 
1559  nat64_address_t *a;
1560  nat64_interface_t *i, *interfaces = 0;
1561 
1562  if (plugin_enabled () == 0)
1563  {
1564  nat64_log_err ("plugin already disabled!");
1565  return 1;
1566  }
1567  nm->enabled = 0;
1568 
1569  /* *INDENT-OFF* */
1570  pool_foreach (i, nm->interfaces)
1571  {
1572  vec_add1 (interfaces, *i);
1573  }
1574  /* *INDENT-ON* */
1575  vec_foreach (i, interfaces)
1576  {
1577  rv = nat64_interface_add_del (i->sw_if_index, i->flags, 0);
1578  if (rv)
1579  {
1580  nat64_log_err ("%U %s interface del failed",
1583  "inside" : "outside");
1584  }
1585  }
1586  vec_free (interfaces);
1587  pool_free (nm->interfaces);
1588 
1590 
1591  if (nat64_free_hash ())
1592  {
1593  rv = 1;
1594  nat64_log_err ("freeing hashes failed!");
1595  }
1596 
1597  // TODO: based on nat64_add_del_prefix fib_table_unlock is not called
1598  vec_free (nm->pref64);
1599 
1600  if (vec_len (nm->addr_pool))
1601  {
1602  vec_foreach (a, nm->addr_pool)
1603  {
1604  if (a->fib_index != ~0)
1606  }
1607  vec_free (nm->addr_pool);
1608  }
1609  return rv;
1610 }
1611 
1612 uword
1613 unformat_nat_protocol (unformat_input_t * input, va_list * args)
1614 {
1615  u32 *r = va_arg (*args, u32 *);
1616 
1617  if (0);
1618 #define _(N, i, n, s) else if (unformat (input, s)) *r = NAT_PROTOCOL_##N;
1620 #undef _
1621  else
1622  return 0;
1623  return 1;
1624 }
1625 
1626 u8 *
1627 format_nat_protocol (u8 * s, va_list * args)
1628 {
1629  u32 i = va_arg (*args, u32);
1630  u8 *t = 0;
1631 
1632  switch (i)
1633  {
1634 #define _(N, j, n, str) case NAT_PROTOCOL_##N: t = (u8 *) str; break;
1636 #undef _
1637  default:
1638  s = format (s, "unknown");
1639  return s;
1640  }
1641  s = format (s, "%s", t);
1642  return s;
1643 }
1644 
1645 /*
1646  * fd.io coding-style-patch-verification: ON
1647  *
1648  * Local Variables:
1649  * eval: (c-set-style "gnu")
1650  * End:
1651  */
u32 nat64_get_icmp_timeout(void)
Get ICMP session timeout.
Definition: nat64.c:1058
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:338
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
void nat64_db_free_out_addr(u32 thread_index, nat64_db_t *db, ip4_address_t *out_addr)
Free sessions using specific outside address.
Definition: nat64_db.c:699
nat64_db_t * db
BIB and session DB per thread.
Definition: nat64.h:137
int nat64_alloc_out_addr_and_port(u32 fib_index, nat_protocol_t proto, ip4_address_t *addr, u16 *port, u32 thread_index)
Alloce IPv4 address and port pair from NAT64 pool.
Definition: nat64.c:757
u8 * format_nat_protocol(u8 *s, va_list *args)
Definition: nat64.c:1627
nat64_address_t * addr_pool
Address pool vector.
Definition: nat64.h:128
u32 icmp_timeout
Definition: nat64.h:154
u32 out2in_node_index
Definition: nat64.h:177
int nat64_set_udp_timeout(u32 timeout)
Set UDP session timeout.
Definition: nat64.c:1013
ip4_add_del_interface_address_callback_t * add_del_interface_address_callbacks
Functions to call when interface address changes.
Definition: ip4.h:141
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:751
a
Definition: bitmap.h:544
int ip4_sv_reass_enable_disable_with_refcnt(u32 sw_if_index, int is_enable)
static_always_inline int nat64_alloc_addr_and_port_default(nat64_address_t *addresses, u32 fib_index, u32 thread_index, nat_protocol_t proto, ip4_address_t *addr, u16 *port, u16 port_per_thread, u32 nat_thread_index)
Definition: nat64.c:670
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:656
int nat64_db_free(nat64_db_t *db)
Free NAT64 DB.
Definition: nat64_db.c:49
ip6_address_t prefix
Definition: nat64.h:73
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
void nat_ipfix_logging_init(vlib_main_t *vm)
Initialize NAT plugin IPFIX logging.
void nat_ipfix_logging_addresses_exhausted(u32 thread_index, u32 pool_id)
Generate NAT addresses exhausted event.
#define TCP_FLAG_SYN
Definition: fa_node.h:13
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:527
uword unformat_nat_protocol(unformat_input_t *input, va_list *args)
Definition: nat64.c:1613
void nat64_extract_ip4(ip6_address_t *ip6, ip4_address_t *ip4, u32 fib_index)
Extract IPv4 address from the IPv4-embedded IPv6 addresses.
Definition: nat64.c:1341
#define PREDICT_TRUE(x)
Definition: clib.h:122
unsigned long u64
Definition: types.h:89
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
static void vlib_node_set_interrupt_pending(vlib_main_t *vm, u32 node_index)
Definition: node_funcs.h:255
#define NAT_ICMP_TIMEOUT
Definition: lib.h:74
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 index
Definition: node.h:280
void nat64_create_expire_walk_process()
Definition: nat64.c:1503
ip4_address_t * ip4_interface_first_address(ip4_main_t *im, u32 sw_if_index, ip_interface_address_t **result_ia)
Definition: ip4_forward.c:281
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:334
#define clib_bitmap_foreach(i, ai)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
void nat64_reset_timeouts()
Definition: nat64.c:1047
u32 first_worker_index
Definition: nat64.h:211
u32 st_buckets
Definition: nat64_db.h:29
u32 vlib_frame_queue_main_init(u32 node_index, u32 frame_queue_nelts)
Definition: threads.c:1880
u32 thread_index
Definition: main.h:250
add paths without path extensions
Definition: fib_source.h:205
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
nat64_db_bib_entry_t * nat64_db_bib_entry_find(nat64_db_t *db, ip46_address_t *addr, u16 port, u8 proto, u32 fib_index, u8 is_ip6)
Find NAT64 BIB entry.
Definition: nat64_db.c:225
static u64 clib_xxhash(u64 key)
Definition: xxhash.h:58
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:630
#define nat64_interface_is_inside(i)
Check if NAT64 interface is inside.
Definition: nat64.h:483
u32 vrf_id
Definition: nat44.api:1029
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
clib_error_t * nat64_api_hookup(vlib_main_t *vm)
Definition: nat64_api.c:445
#define NAT_TCP_TRANSITORY_TIMEOUT
Definition: lib.h:72
vlib_main_t * vm
Definition: in2out_ed.c:1580
u32 tcp_trans_timeout
Definition: nat64.h:155
nat_protocol_t
Definition: lib.h:63
u32 in2out_node_index
Definition: nat64.h:174
vl_api_prefix_t prefix
Definition: ip.api:144
int(* nat64_pool_addr_walk_fn_t)(nat64_address_t *addr, void *ctx)
Call back function when walking addresses in NAT64 pool, non-zero return value stop walk...
Definition: nat64.h:238
int nat64_add_interface_address(u32 sw_if_index, int is_add)
NAT64 pool address from specific (DHCP addressed) interface.
Definition: nat64.c:455
nat64_db_bib_t bib
Definition: nat64_db.h:144
u32 st_memory_size
Definition: nat64_db.h:30
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:251
struct _tcp_header tcp_header_t
vhost_vring_addr_t addr
Definition: vhost_user.h:111
u32 * auto_add_sw_if_indices
sw_if_indices whose interface addresses should be auto-added
Definition: nat64.h:131
u32 num_workers
Definition: nat64.h:210
vlib_main_t ** vlib_mains
Definition: buffer.c:332
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
u32 st_entries_num
Definition: nat64_db.h:129
fib_node_index_t fib_table_entry_update_one_path(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, dpo_proto_t next_hop_proto, const ip46_address_t *next_hop, u32 next_hop_sw_if_index, u32 next_hop_fib_index, u32 next_hop_weight, fib_mpls_label_t *next_hop_labels, fib_route_path_flags_t path_flags)
Update the entry to have just one path.
Definition: fib_table.c:814
static void nat64_ip4_add_del_interface_address_cb(ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address, u32 address_length, u32 if_address_index, u32 is_delete)
Definition: nat64.c:79
static_always_inline u8 plugin_enabled()
Definition: det44.h:222
u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index)
Definition: ip4_fib.c:227
u32 nat64_get_worker_in2out(ip6_address_t *addr)
Get worker thread index for NAT64 in2out.
Definition: nat64.c:117
#define static_always_inline
Definition: clib.h:109
static nat_protocol_t ip_proto_to_nat_proto(u8 ip_proto)
Common NAT inline functions.
Definition: inlines.h:24
#define NAT64_INTERFACE_FLAG_IS_OUTSIDE
Definition: nat64.h:477
vlib_log_class_t log_class
Definition: nat64.h:122
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:579
fib_source_t fib_src_hi
Definition: nat64.h:206
vl_api_ip6_address_t ip6
Definition: one.api:424
ip4_address_t dst_address
Definition: ip4_packet.h:125
u32 total_enabled_count
Definition: nat64.h:159
description fragment has unexpected format
Definition: map.api:433
int nat64_set_icmp_timeout(u32 timeout)
Set ICMP session timeout.
Definition: nat64.c:1034
Aggregate type for a prefix.
Definition: fib_types.h:202
static_always_inline u8 icmp_type_is_error_message(u8 icmp_type)
Definition: inlines.h:55
const cJSON *const b
Definition: cJSON.h:255
u32 tcp_est_timeout
Definition: nat64.h:156
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:196
unsigned int u32
Definition: types.h:88
int nat64_init_hash(nat64_config_t c)
Definition: nat64.c:317
vlib_node_registration_t nat64_in2out_node
(constructor) VLIB_REGISTER_NODE (nat64_in2out_node)
void nat64_tcp_session_set_state(nat64_db_st_entry_t *ste, tcp_header_t *tcp, u8 is_ip6)
Set NAT64 TCP session state.
Definition: nat64.c:1139
u32 expire_worker_walk_node_index
Definition: nat64.h:165
bool is_ip6
Definition: ip.api:43
fib_source_t fib_source_allocate(const char *name, fib_source_priority_t prio, fib_source_behaviour_t bh)
Definition: fib_source.c:118
u32 in2out_slowpath_node_index
Definition: nat64.h:175
static_always_inline u16 nat64_random_port(u16 min, u16 max)
Definition: nat64.c:655
char * name
The counter collection&#39;s name.
Definition: counter.h:64
u32 fib_index
Definition: nat64.h:76
u32 udp_timeout
values of various timeouts
Definition: nat64.h:153
Definition: fib_entry.h:117
ip4_address_t out_addr
Definition: nat64.h:83
Definition: fib_entry.h:116
nat64_prefix_t * pref64
Pref64 vector.
Definition: nat64.h:134
ip6_address_t in_addr
Definition: nat64.h:81
u32 fib_index
Definition: nat64.h:95
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1015
#define FIB_SOURCE_PRIORITY_HI
Some priority values that plugins might use when they are not to concerned where in the list they&#39;ll ...
Definition: fib_source.h:273
void nat64_session_reset_timeout(nat64_db_st_entry_t *ste, vlib_main_t *vm)
Reset NAT64 session timeout.
Definition: nat64.c:1100
void nad64_db_st_free_expired(u32 thread_index, nat64_db_t *db, u32 now)
Free expired session entries in session tables.
Definition: nat64_db.c:665
IPv6 shallow virtual reassembly.
#define nat64_log_err(...)
Definition: nat64.h:510
vl_api_ip_proto_t proto
Definition: acl_types.api:51
IPv4 shallow virtual reassembly.
nat64_config_t config
Definition: nat64.h:116
icmp
Definition: map.api:387
long ctx[MAX_CONNS]
Definition: main.c:144
u32 random_seed
Definition: nat64.h:201
u32 nat64_get_udp_timeout(void)
Get UDP session timeout.
Definition: nat64.c:1026
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
u32 vlib_process_create(vlib_main_t *vm, char *name, vlib_node_function_t *f, u32 log2_n_stack_bytes)
Create a vlib process.
Definition: node.c:726
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:301
nat64_db_st_t st
Definition: nat64_db.h:145
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:875
vl_api_ip4_address_t ip4
Definition: one.api:376
static uword nat64_expire_walk_fn(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Centralized process to drive per worker expire walk.
Definition: nat64.c:1447
#define TCP_FLAG_FIN
Definition: fa_node.h:12
ip4_address_t addr
Definition: nat64.h:94
static void vlib_set_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 value)
Set a simple counter.
Definition: counter.h:113
u32 * workers
Definition: nat64.h:212
void fib_table_unlock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Take a reference counting lock on the table.
Definition: fib_table.c:1300
void nat64_compose_ip6(ip6_address_t *ip6, ip4_address_t *ip4, u32 fib_index)
Compose IPv4-embedded IPv6 addresses.
Definition: nat64.c:1271
#define TCP_FLAG_RST
Definition: fa_node.h:14
vlib_simple_counter_main_t total_sessions
Definition: nat64.h:169
u32 expire_walk_node_index
Definition: nat64.h:162
static u8 well_known_prefix[]
Definition: nat64.c:59
#define pool_free(p)
Free a pool.
Definition: pool.h:440
u32 nat64_get_tcp_est_timeout(void)
Get TCP established timeout.
Definition: nat64.c:1092
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:170
u32 bib_buckets
Definition: nat64_db.h:27
vlib_node_registration_t nat64_out2in_node
(constructor) VLIB_REGISTER_NODE (nat64_out2in_node)
Definition: nat64_out2in.c:643
svmdb_client_t * c
void nat64_pool_addr_walk(nat64_pool_addr_walk_fn_t fn, void *ctx)
Walk NAT64 pool.
Definition: nat64.c:440
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:219
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
void fib_table_entry_delete(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source)
Delete a FIB entry.
Definition: fib_table.c:895
ip4_add_del_interface_address_function_t * function
Definition: ip4.h:75
int nat64_add_del_prefix(ip6_address_t *prefix, u8 plen, u32 vrf_id, u8 is_add)
Add/delete NAT64 prefix.
Definition: nat64.c:1206
nat64_main_t nat64_main
Definition: nat64.c:27
u32 fq_out2in_index
Definition: nat64.h:141
nat64_db_bib_entry_t * nat64_db_bib_entry_create(u32 thread_index, nat64_db_t *db, ip6_address_t *in_addr, ip4_address_t *out_addr, u16 in_port, u16 out_port, u32 fib_index, u8 proto, u8 is_static)
Create new NAT64 BIB entry.
Definition: nat64_db.c:72
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1580
u32 vrf_id
Definition: nat64.h:75
void vlib_validate_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
validate a simple counter
Definition: counter.c:79
#define nat64_interface_is_outside(i)
Check if NAT64 interface is outside.
Definition: nat64.h:489
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:330
u32 nat64_get_worker_out2in(vlib_buffer_t *b, ip4_header_t *ip)
Get worker thread index for NAT64 out2in.
Definition: nat64.c:139
manual_print typedef address
Definition: ip_types.api:96
u32 fq_in2out_index
Worker handoff.
Definition: nat64.h:140
nat64_static_bib_to_update_t * static_bibs
Pool of static BIB entries to be added/deleted in worker threads.
Definition: nat64.h:144
IPv4 main type.
Definition: ip4.h:107
static void nat64_free_out_addr_and_port(struct nat64_db_s *db, ip4_address_t *addr, u16 port, u8 protocol)
Definition: nat64.c:777
#define foreach_nat_counter
Definition: lib.h:42
u32 fib_table_find_or_create_and_lock(fib_protocol_t proto, u32 table_id, fib_source_t src)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1165
#define clib_bitmap_free(v)
Free a bitmap.
Definition: bitmap.h:92
static void vlib_zero_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
Clear a simple counter Clears the set of per-thread u16 counters, and the u64 counter.
Definition: counter.h:158
u32 bib_memory_size
Definition: nat64_db.h:28
uword * thread_registrations_by_name
Definition: threads.h:295
static uword nat64_expire_worker_walk_fn(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Per worker process checking expire time for NAT64 sessions.
Definition: nat64.c:1411
u32 bib_entries_num
Definition: nat64_db.h:79
int nat64_add_del_pool_addr(u32 thread_index, ip4_address_t *addr, u32 vrf_id, u8 is_add)
Add/delete address to NAT64 pool.
Definition: nat64.c:364
int nat64_free_hash()
Definition: nat64.c:341
#define FIB_SOURCE_PRIORITY_LOW
Definition: fib_source.h:274
static vlib_node_registration_t nat64_expire_worker_walk_node
(constructor) VLIB_REGISTER_NODE (nat64_expire_worker_walk_node)
Definition: nat64.c:1435
int ip6_sv_reass_enable_disable_with_refcnt(u32 sw_if_index, int is_enable)
#define VNET_FEATURES(...)
Definition: feature.h:470
void nat64_prefix_walk(nat64_prefix_walk_fn_t fn, void *ctx)
Walk NAT64 prefixes.
Definition: nat64.c:1256
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static uword is_pow2(uword x)
Definition: clib.h:253
void nat64_interfaces_walk(nat64_interface_walk_fn_t fn, void *ctx)
Walk NAT64 interfaces.
Definition: nat64.c:639
clib_error_t * nat64_init(vlib_main_t *vm)
Initialize NAT64.
Definition: nat64.c:211
struct _vlib_node_registration vlib_node_registration_t
#define NAT_UDP_TIMEOUT
Definition: lib.h:71
int nat64_set_tcp_timeouts(u32 trans, u32 est)
Set TCP session timeouts.
Definition: nat64.c:1066
ip4_main_t * ip4_main
Definition: nat64.h:195
char * stat_segment_name
Name in stat segment directory.
Definition: counter.h:65
u32 enabled
Definition: nat64.h:114
vl_api_address_t ip
Definition: l2.api:501
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define NAT_TCP_ESTABLISHED_TIMEOUT
Definition: lib.h:73
Definition: fib_entry.h:113
VLIB buffer representation.
Definition: buffer.h:102
#define nat_elog_str(_str)
Definition: nat64.c:67
u64 uword
Definition: types.h:112
static uword nat64_static_bib_worker_fn(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Add/delete static BIB entry in worker thread.
Definition: nat64.c:815
#define NAT64_INTERFACE_FLAG_IS_INSIDE
Definition: nat64.h:476
int nat64_plugin_disable()
Definition: nat64.c:1553
u32 error_node_index
node index
Definition: nat64.h:172
u32 index
Definition: flow_types.api:221
int nat64_interface_add_del(u32 sw_if_index, u8 is_inside, u8 is_add)
Enable/disable NAT64 feature on the interface.
Definition: nat64.c:538
u16 port
Definition: lb_types.api:73
u16 port_per_thread
Definition: nat64.h:213
#define hash_get_mem(h, key)
Definition: hash.h:269
void nat64_db_bib_entry_free(u32 thread_index, nat64_db_t *db, nat64_db_bib_entry_t *bibe)
Free NAT64 BIB entry.
Definition: nat64_db.c:147
#define vnet_buffer(b)
Definition: buffer.h:417
int nat64_plugin_enable(nat64_config_t c)
Definition: nat64.c:1516
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1105
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
VLIB_PLUGIN_REGISTER()
#define vec_foreach(var, vec)
Vector iterator.
int nat64_db_init(nat64_db_t *db, nat64_config_t c, nat64_db_free_addr_port_function_t free_addr_port_cb)
Initialize NAT64 DB.
Definition: nat64_db.c:23
static vlib_node_registration_t nat64_static_bib_worker_node
(constructor) VLIB_REGISTER_NODE (nat64_static_bib_worker_node)
Definition: nat64.c:867
nat64_interface_t * interfaces
Interface pool.
Definition: nat64.h:125
void nat64_add_del_addr_to_fib(ip4_address_t *addr, u8 p_len, u32 sw_if_index, int is_add)
Definition: nat64.c:509
VNET_FEATURE_INIT(nat64_in2out, static)
int nat64_add_del_static_bib_entry(ip6_address_t *in_addr, ip4_address_t *out_addr, u16 in_port, u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
Add/delete static NAT64 BIB entry.
Definition: nat64.c:879
fib_source_t fib_src_low
Definition: nat64.h:207
int(* nat64_prefix_walk_fn_t)(nat64_prefix_t *pref64, void *ctx)
Call back function when walking addresses in NAT64 prefixes, non-zero return value stop walk...
Definition: nat64.h:417
static void nat64_validate_counters(nat64_main_t *nm, u32 sw_if_index)
Definition: nat64.c:497
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
u32 nat64_get_tcp_trans_timeout(void)
Get TCP transitory timeout.
Definition: nat64.c:1084
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:303
int(* nat64_interface_walk_fn_t)(nat64_interface_t *i, void *ctx)
Call back function when walking interfaces with NAT64 feature, non-zero return value stop walk...
Definition: nat64.h:273
vlib_simple_counter_main_t total_bibs
Definition: nat64.h:168