37 #include <vpp/app/version.h> 44 .arc_name =
"ip4-unicast",
45 .node_name =
"nat-pre-in2out",
49 .arc_name =
"ip4-unicast",
50 .node_name =
"nat-pre-out2in",
52 "ip4-dhcp-client-detect"),
55 .arc_name =
"ip4-unicast",
56 .node_name =
"nat44-in2out-worker-handoff",
60 .arc_name =
"ip4-unicast",
61 .node_name =
"nat44-out2in-worker-handoff",
63 "ip4-dhcp-client-detect"),
66 .arc_name =
"ip4-unicast",
67 .node_name =
"nat44-in2out",
71 .arc_name =
"ip4-unicast",
72 .node_name =
"nat44-out2in",
74 "ip4-dhcp-client-detect"),
77 .arc_name =
"ip4-unicast",
78 .node_name =
"nat44-classify",
82 .arc_name =
"ip4-unicast",
83 .node_name =
"nat44-det-in2out",
87 .arc_name =
"ip4-unicast",
88 .node_name =
"nat44-det-out2in",
90 "ip4-dhcp-client-detect"),
93 .arc_name =
"ip4-unicast",
94 .node_name =
"nat44-det-classify",
98 .arc_name =
"ip4-unicast",
99 .node_name =
"nat44-ed-in2out",
103 .arc_name =
"ip4-unicast",
104 .node_name =
"nat44-ed-out2in",
106 "ip4-dhcp-client-detect"),
109 .arc_name =
"ip4-unicast",
110 .node_name =
"nat44-ed-classify",
114 .arc_name =
"ip4-unicast",
115 .node_name =
"nat44-handoff-classify",
119 .arc_name =
"ip4-unicast",
120 .node_name =
"nat44-in2out-fast",
124 .arc_name =
"ip4-unicast",
125 .node_name =
"nat44-out2in-fast",
127 "ip4-dhcp-client-detect"),
130 .arc_name =
"ip4-unicast",
131 .node_name =
"nat44-hairpin-dst",
135 .arc_name =
"ip4-unicast",
136 .node_name =
"nat44-ed-hairpin-dst",
142 .arc_name =
"ip4-output",
143 .node_name =
"nat44-in2out-output",
147 .arc_name =
"ip4-output",
148 .node_name =
"nat44-in2out-output-worker-handoff",
152 .arc_name =
"ip4-output",
153 .node_name =
"nat44-hairpin-src",
157 .arc_name =
"ip4-output",
158 .node_name =
"nat44-ed-in2out-output",
162 .arc_name =
"ip4-output",
163 .node_name =
"nat44-ed-hairpin-src",
170 .arc_name =
"ip4-local",
171 .node_name =
"nat44-hairpinning",
176 .arc_name =
"ip4-local",
177 .node_name =
"nat44-ed-hairpinning",
183 .version = VPP_BUILD_VER,
184 .description =
"Network Address Translation (NAT)",
203 ed_key.
proto = s->in2out.port;
210 ed_key.
l_port = s->in2out.port;
211 ed_key.
r_port = s->ext_host_port;
213 ed_key.
l_addr = s->in2out.addr;
214 ed_key.
r_addr = s->ext_host_addr;
218 if (clib_bihash_add_del_16_8 (&tsm->
in2out_ed, &ed_kv, 0))
228 s->in2out.protocol, s->out2in.port);
229 ed_key.
l_addr = s->out2in.addr;
230 ed_key.
r_addr = s->ext_host_addr;
234 ed_key.
proto = s->in2out.port;
241 ed_key.
l_port = s->out2in.port;
242 ed_key.
r_port = s->ext_host_port;
246 if (clib_bihash_add_del_16_8 (&tsm->
out2in_ed, &ed_kv, 0))
248 ed_key.
l_addr = s->in2out.addr;
251 ed_key.
l_port = s->in2out.port;
254 ed_key.
r_addr = s->ext_host_nat_addr;
255 ed_key.
r_port = s->ext_host_nat_port;
259 if (clib_bihash_add_del_16_8 (&tsm->
in2out_ed, &ed_kv, 0))
264 &s->in2out.addr, s->in2out.port,
265 &s->ext_host_nat_addr, s->ext_host_nat_port,
266 &s->out2in.addr, s->out2in.port,
267 &s->ext_host_addr, s->ext_host_port,
272 kv.
key = s->in2out.as_u64;
273 if (clib_bihash_add_del_8_8 (&tsm->
in2out, &kv, 0))
275 kv.
key = s->out2in.as_u64;
276 if (clib_bihash_add_del_8_8 (&tsm->
out2in, &kv, 0))
281 &s->in2out.addr, s->in2out.port,
282 &s->out2in.addr, s->out2in.port,
293 s->in2out.addr.as_u32,
294 s->out2in.addr.as_u32,
298 s->in2out.fib_index);
300 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
301 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
309 key.
port = s->ext_host_nat_port;
310 key.
addr.
as_u32 = s->ext_host_nat_addr.as_u32;
337 if (clib_bihash_search_8_8 (&tsm->
user_hash, &kv, &value))
355 if (clib_bihash_add_del_8_8 (&tsm->
user_hash, &kv, 1))
371 u32 thread_index,
f64 now)
375 u32 oldest_per_user_translation_list_index, session_index;
382 oldest_per_user_translation_list_index =
386 ASSERT (oldest_per_user_translation_list_index != ~0);
391 oldest_per_user_translation_list_index);
393 oldest_per_user_translation_list_elt =
395 oldest_per_user_translation_list_index);
398 session_index = oldest_per_user_translation_list_elt->
value;
411 s->ext_host_addr.as_u32 = 0;
412 s->ext_host_port = 0;
413 s->ext_host_nat_addr.as_u32 = 0;
414 s->ext_host_nat_port = 0;
424 per_user_translation_list_elt - tsm->
list_pool);
427 s->per_user_index = per_user_translation_list_elt - tsm->
list_pool;
431 s->per_user_list_head_index,
432 per_user_translation_list_elt - tsm->
list_pool);
434 s->user_index = u - tsm->
users;
439 s->ha_last_refreshed = now;
450 dlist_elt_t *per_user_translation_list_elt, *oldest_elt;
452 u64 sess_timeout_time;
463 if (now >= sess_timeout_time)
476 s->ext_host_addr.as_u32 = 0;
477 s->ext_host_port = 0;
478 s->ext_host_nat_addr.as_u32 = 0;
479 s->ext_host_nat_port = 0;
488 nat_elog_addr (SNAT_LOG_WARNING,
"[warn] max translations per user",
503 per_user_translation_list_elt - tsm->
list_pool);
506 s->per_user_index = per_user_translation_list_elt - tsm->
list_pool;
510 s->per_user_list_head_index,
511 per_user_translation_list_elt - tsm->
list_pool);
518 s->ha_last_refreshed = now;
531 .ip4.as_u32 = addr->
as_u32,
560 return VNET_API_ERROR_FEATURE_DISABLED;
567 return VNET_API_ERROR_VALUE_EXIST;
583 #define _(N, i, n, s) \ 584 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \ 585 ap->busy_##n##_ports = 0; \ 586 ap->busy_##n##_ports_per_thread = 0;\ 587 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0); 597 if (nat_interface_is_inside(i) || sm->out2in_dpo)
600 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
605 if (nat_interface_is_inside(i) || sm->out2in_dpo)
608 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
623 if (is_addr_only_static_mapping (m) ||
624 is_out2in_only_static_mapping (m) ||
625 is_identity_static_mapping (m))
627 if (m->external_addr.as_u32 == addr.as_u32)
640 v = clib_net_to_host_u32 (a->
as_u32) + 1;
641 a->
as_u32 = clib_host_to_net_u32 (v);
652 int addr_only,
int is_add,
u8 * tag,
653 int twice_nat,
int out2in_only,
689 u16 l_port,
u16 e_port,
u32 vrf_id,
int addr_only,
706 u32 elt_index, head_index;
716 if (twice_nat || out2in_only)
717 return VNET_API_ERROR_FEATURE_DISABLED;
721 if (sw_if_index != ~0)
751 return VNET_API_ERROR_VALUE_EXIST;
754 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
755 addr_only, is_add, tag, twice_nat, out2in_only, identity_nat);
758 if (first_int_addr == 0)
773 return VNET_API_ERROR_NO_SUCH_ENTRY;
790 m_key.
port = addr_only ? 0 : e_port;
808 if (local->vrf_id == vrf_id)
809 return VNET_API_ERROR_VALUE_EXIST;
827 return VNET_API_ERROR_VALUE_EXIST;
830 if (twice_nat && addr_only)
831 return VNET_API_ERROR_UNSUPPORTED;
846 if (!(out2in_only || identity_nat))
849 m_key.
port = addr_only ? 0 : l_port;
853 if (!clib_bihash_search_8_8
855 return VNET_API_ERROR_VALUE_EXIST;
870 #define _(N, j, n, s) \ 871 case SNAT_PROTOCOL_##N: \ 872 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \ 873 return VNET_API_ERROR_INVALID_VALUE; \ 874 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \ 877 a->busy_##n##_ports++; \ 878 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \ 885 return VNET_API_ERROR_INVALID_VALUE_2;
893 if (sw_if_index != ~0)
910 return VNET_API_ERROR_NO_SUCH_ENTRY;
976 if (!clib_bihash_search_8_8 (&tsm->
user_hash, &kv, &value))
978 user_index = value.
value;
984 elt_index = head->
next;
986 ses_index = elt->
value;
987 while (ses_index != ~0)
991 ses_index = elt->
value;
997 && (clib_net_to_host_u16 (s->in2out.port) !=
1016 if (sw_if_index != ~0)
1019 return VNET_API_ERROR_NO_SUCH_ENTRY;
1030 if (local->vrf_id == vrf_id)
1031 find = local - m->locals;
1035 return VNET_API_ERROR_NO_SUCH_ENTRY;
1054 #define _(N, j, n, s) \ 1055 case SNAT_PROTOCOL_##N: \ 1056 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \ 1057 if (e_port > 1024) \ 1059 a->busy_##n##_ports--; \ 1060 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \ 1067 return VNET_API_ERROR_INVALID_VALUE_2;
1094 if (!clib_bihash_search_8_8 (&tsm->
user_hash, &kv, &value))
1096 user_index = value.
value;
1102 elt_index = head->
next;
1104 ses_index = elt->
value;
1105 while (ses_index != ~0)
1109 ses_index = elt->
value;
1113 if ((s->out2in.addr.as_u32 != e_addr.
as_u32) ||
1114 (clib_net_to_host_u16 (s->out2in.port) !=
1159 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1162 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1167 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1170 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1183 u8 * tag,
u32 affinity)
1192 u32 elt_index, head_index, ses_index;
1201 return VNET_API_ERROR_FEATURE_DISABLED;
1203 m_key.
addr = e_addr;
1204 m_key.
port = e_port;
1216 return VNET_API_ERROR_VALUE_EXIST;
1219 return VNET_API_ERROR_INVALID_VALUE;
1233 #define _(N, j, n, s) \ 1234 case SNAT_PROTOCOL_##N: \ 1235 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \ 1236 return VNET_API_ERROR_INVALID_VALUE; \ 1237 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \ 1238 if (e_port > 1024) \ 1240 a->busy_##n##_ports++; \ 1241 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \ 1248 return VNET_API_ERROR_INVALID_VALUE_2;
1255 return VNET_API_ERROR_NO_SUCH_ENTRY;
1284 nat_elog_err (
"static_mapping_by_external key add failed");
1285 return VNET_API_ERROR_UNSPECIFIED;
1289 for (i = 0; i <
vec_len (locals); i++)
1304 locals[
i].
prefix = (i == 0) ? locals[i].probability :
1305 (locals[i - 1].
prefix + locals[i].probability);
1334 return VNET_API_ERROR_NO_SUCH_ENTRY;
1337 return VNET_API_ERROR_INVALID_VALUE;
1349 #define _(N, j, n, s) \ 1350 case SNAT_PROTOCOL_##N: \ 1351 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \ 1352 if (e_port > 1024) \ 1354 a->busy_##n##_ports--; \ 1355 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \ 1362 return VNET_API_ERROR_INVALID_VALUE_2;
1376 nat_elog_err (
"static_mapping_by_external key del failed");
1377 return VNET_API_ERROR_UNSPECIFIED;
1383 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1384 FIB_SOURCE_PLUGIN_LOW);
1385 m_key.addr = local->addr;
1388 m_key.port = local->port;
1389 m_key.fib_index = local->fib_index;
1390 kv.key = m_key.as_u64;
1391 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1393 nat_elog_err (
"static_mapping_by_local key del failed");
1394 return VNET_API_ERROR_UNSPECIFIED;
1401 .src_address = local->addr,
1413 if (!clib_bihash_search_8_8 (&tsm->
user_hash, &kv, &value))
1420 elt_index = head->
next;
1422 ses_index = elt->
value;
1423 while (ses_index != ~0)
1427 ses_index = elt->
value;
1432 if ((s->in2out.addr.as_u32 != local->
addr.
as_u32) ||
1433 (clib_net_to_host_u16 (s->in2out.port) != local->
port))
1459 u8 probability,
u8 is_add)
1471 u32 elt_index, head_index, ses_index, *locals = 0;
1476 return VNET_API_ERROR_FEATURE_DISABLED;
1478 m_key.
addr = e_addr;
1479 m_key.
port = e_port;
1487 return VNET_API_ERROR_NO_SUCH_ENTRY;
1490 return VNET_API_ERROR_INVALID_VALUE;
1495 if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1496 (local->vrf_id == vrf_id))
1498 match_local = local;
1507 return VNET_API_ERROR_VALUE_EXIST;
1511 local->addr.as_u32 = l_addr.as_u32;
1512 local->port = l_port;
1513 local->probability = probability;
1514 local->vrf_id = vrf_id;
1521 m_key.addr = l_addr;
1522 m_key.port = l_port;
1523 m_key.fib_index = local->fib_index;
1524 kv.key = m_key.as_u64;
1525 kv.value = m - sm->static_mappings;
1526 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1))
1527 nat_elog_err (
"static_mapping_by_local key add failed");
1533 return VNET_API_ERROR_NO_SUCH_ENTRY;
1536 return VNET_API_ERROR_UNSPECIFIED;
1543 m_key.addr = l_addr;
1544 m_key.port = l_port;
1545 m_key.fib_index = match_local->fib_index;
1546 kv.key = m_key.as_u64;
1547 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0))
1548 nat_elog_err (
"static_mapping_by_local key del failed");
1551 if (sm->num_workers > 1)
1557 sm->worker_in2out_cb (&ip, m->fib_index,
1564 u_key.addr = match_local->addr;
1565 u_key.fib_index = match_local->fib_index;
1566 kv.key = u_key.as_u64;
1567 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &
value))
1570 if (u->nstaticsessions)
1572 head_index = u->sessions_per_user_list_head_index;
1574 elt_index = head->next;
1576 ses_index = elt->value;
1577 while (ses_index != ~0)
1581 ses_index = elt->value;
1586 if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1587 (clib_net_to_host_u16 (s->in2out.port) !=
1605 vec_add1 (locals, local - m->locals);
1606 if (sm->num_workers > 1)
1609 ip.src_address.as_u32 = local->addr.as_u32,
1610 bitmap = clib_bitmap_set (bitmap,
1611 sm->worker_in2out_cb (&ip, local->fib_index, 0),
1620 local->prefix = local->probability;
1625 local->prefix = local->probability + prev_local->prefix;
1629 if (sm->num_workers > 1)
1644 snat_session_t *ses;
1645 u32 *ses_to_be_removed = 0, *ses_index;
1654 for (i = 0; i <
vec_len (addresses); i++)
1663 return VNET_API_ERROR_NO_SUCH_ENTRY;
1670 if (m->external_addr.as_u32 == addr.as_u32)
1671 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1672 m->local_port, m->external_port,
1673 m->vrf_id, is_addr_only_static_mapping(m), ~0,
1674 m->proto, 0, m->twice_nat,
1675 is_out2in_only_static_mapping(m), m->tag, is_identity_static_mapping(m));
1685 return VNET_API_ERROR_UNSPECIFIED;
1693 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1699 if (ses->out2in.addr.as_u32 == addr.as_u32)
1701 nat_free_session_data (sm, ses, tsm - sm->per_thread_data, 0);
1702 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1717 #define _(N, i, n, s) \ 1718 clib_bitmap_free (a->busy_##n##_port_bitmap); \ 1719 vec_free (a->busy_##n##_ports_per_thread); 1734 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1737 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1740 pool_foreach (interface, sm->output_feature_interfaces,
1742 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1745 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1758 const char *feature_name, *del_feature_name;
1767 return VNET_API_ERROR_UNSUPPORTED;
1772 if (i->sw_if_index == sw_if_index)
1773 return VNET_API_ERROR_VALUE_EXIST;
1778 feature_name = is_inside ?
"nat44-in2out-fast" :
"nat44-out2in-fast";
1783 is_inside ?
"nat44-in2out-worker-handoff" :
1784 "nat44-out2in-worker-handoff";
1786 feature_name = is_inside ?
"nat44-det-in2out" :
"nat44-det-out2in";
1789 feature_name = is_inside ?
"nat-pre-in2out" :
"nat-pre-out2in";
1792 feature_name = is_inside ?
"nat44-in2out" :
"nat44-out2in";
1808 if (outside_fib->
fib_index == fib_index)
1833 if (i->sw_if_index == sw_if_index)
1837 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1840 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1842 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1844 if (sm->num_workers > 1 && !sm->deterministic)
1846 del_feature_name =
"nat44-handoff-classify";
1847 feature_name = !is_inside ?
"nat44-in2out-worker-handoff" :
1848 "nat44-out2in-worker-handoff";
1850 else if (sm->deterministic)
1852 del_feature_name =
"nat44-det-classify";
1853 feature_name = !is_inside ?
"nat44-det-in2out" :
1856 else if (sm->endpoint_dependent)
1858 del_feature_name =
"nat44-ed-classify";
1859 feature_name = !is_inside ?
"nat-pre-in2out" :
1864 del_feature_name =
"nat44-classify";
1865 feature_name = !is_inside ?
"nat44-in2out" :
"nat44-out2in";
1868 vnet_feature_enable_disable (
"ip4-unicast", del_feature_name,
1869 sw_if_index, 0, 0, 0);
1870 vnet_feature_enable_disable (
"ip4-unicast", feature_name,
1871 sw_if_index, 1, 0, 0);
1874 if (sm->endpoint_dependent)
1875 vnet_feature_enable_disable (
"ip4-local",
1876 "nat44-ed-hairpinning",
1877 sw_if_index, 1, 0, 0);
1878 else if (!sm->deterministic)
1879 vnet_feature_enable_disable (
"ip4-local",
1880 "nat44-hairpinning",
1881 sw_if_index, 1, 0, 0);
1886 vnet_feature_enable_disable (
"ip4-unicast", feature_name,
1887 sw_if_index, 0, 0, 0);
1888 pool_put (sm->interfaces, i);
1891 if (sm->endpoint_dependent)
1892 vnet_feature_enable_disable (
"ip4-local",
1893 "nat44-ed-hairpinning",
1894 sw_if_index, 0, 0, 0);
1895 else if (!sm->deterministic)
1896 vnet_feature_enable_disable (
"ip4-local",
1897 "nat44-hairpinning",
1898 sw_if_index, 0, 0, 0);
1904 if ((nat_interface_is_inside(i) && is_inside) ||
1905 (nat_interface_is_outside(i) && !is_inside))
1908 if (sm->num_workers > 1 && !sm->deterministic)
1910 del_feature_name = !is_inside ?
"nat44-in2out-worker-handoff" :
1911 "nat44-out2in-worker-handoff";
1912 feature_name =
"nat44-handoff-classify";
1914 else if (sm->deterministic)
1916 del_feature_name = !is_inside ?
"nat44-det-in2out" :
1918 feature_name =
"nat44-det-classify";
1920 else if (sm->endpoint_dependent)
1922 del_feature_name = !is_inside ?
"nat-pre-in2out" :
1925 feature_name =
"nat44-ed-classify";
1929 del_feature_name = !is_inside ?
"nat44-in2out" :
"nat44-out2in";
1930 feature_name =
"nat44-classify";
1933 vnet_feature_enable_disable (
"ip4-unicast", del_feature_name,
1934 sw_if_index, 0, 0, 0);
1935 vnet_feature_enable_disable (
"ip4-unicast", feature_name,
1936 sw_if_index, 1, 0, 0);
1939 if (sm->endpoint_dependent)
1940 vnet_feature_enable_disable (
"ip4-local",
"nat44-ed-hairpinning",
1941 sw_if_index, 0, 0, 0);
1942 else if (!sm->deterministic)
1943 vnet_feature_enable_disable (
"ip4-local",
"nat44-hairpinning",
1944 sw_if_index, 0, 0, 0);
1955 return VNET_API_ERROR_NO_SUCH_ENTRY;
1963 if (is_inside && !sm->out2in_dpo)
1965 if (sm->endpoint_dependent)
1968 else if (!sm->deterministic)
1990 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1993 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1998 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
2007 u8 is_inside,
int is_del)
2020 return VNET_API_ERROR_UNSUPPORTED;
2025 if (i->sw_if_index == sw_if_index)
2026 return VNET_API_ERROR_VALUE_EXIST;
2035 if (outside_fib->
fib_index == fib_index)
2063 sw_if_index, !is_del, 0, 0);
2065 sw_if_index, !is_del, 0, 0);
2070 sw_if_index, !is_del, 0, 0);
2072 sw_if_index, !is_del, 0, 0);
2080 "nat44-out2in-worker-handoff",
2081 sw_if_index, !is_del, 0, 0);
2083 "nat44-in2out-output-worker-handoff",
2084 sw_if_index, !is_del, 0, 0);
2091 sw_if_index, !is_del, 0, 0);
2093 sw_if_index, !is_del, 0, 0);
2098 sw_if_index, !is_del, 0, 0);
2100 sw_if_index, !is_del, 0, 0);
2116 if (i->sw_if_index == sw_if_index)
2119 pool_put (sm->output_feature_interfaces, i);
2121 return VNET_API_ERROR_VALUE_EXIST;
2129 return VNET_API_ERROR_NO_SUCH_ENTRY;
2131 pool_get (sm->output_feature_interfaces,
i);
2150 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2153 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2167 return VNET_API_ERROR_FEATURE_DISABLED;
2170 return VNET_API_ERROR_INVALID_WORKER;
2199 if (new_fib_index == old_fib_index)
2208 if (i->sw_if_index == sw_if_index)
2210 if (!(nat_interface_is_outside (i)))
2218 if (i->sw_if_index == sw_if_index)
2220 if (!(nat_interface_is_outside (i)))
2232 if (outside_fib->fib_index == old_fib_index)
2234 outside_fib->refcount--;
2235 if (!outside_fib->refcount)
2236 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2243 if (outside_fib->fib_index == new_fib_index)
2245 outside_fib->refcount++;
2253 vec_add2 (sm->outside_fibs, outside_fib, 1);
2254 outside_fib->refcount = 1;
2255 outside_fib->fib_index = new_fib_index;
2273 u32 if_address_index,
u32 is_delete);
2281 u32 if_address_index,
u32 is_delete);
2288 u16 port_per_thread,
u32 snat_thread_index);
2479 u16 port_host_byte_order = clib_net_to_host_u16 (k->
port);
2481 for (address_index = 0; address_index <
vec_len (addresses);
2488 ASSERT (address_index < vec_len (addresses));
2490 a = addresses + address_index;
2494 #define _(N, i, n, s) \ 2495 case SNAT_PROTOCOL_##N: \ 2496 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \ 2497 port_host_byte_order) == 1); \ 2498 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \ 2499 port_host_byte_order, 0); \ 2500 a->busy_##n##_ports--; \ 2501 a->busy_##n##_ports_per_thread[thread_index]--; \ 2517 u16 port_host_byte_order = clib_net_to_host_u16 (k->
port);
2519 for (address_index = 0; address_index <
vec_len (addresses);
2525 a = addresses + address_index;
2528 #define _(N, j, n, s) \ 2529 case SNAT_PROTOCOL_##N: \ 2530 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, port_host_byte_order)) \ 2531 return VNET_API_ERROR_INSTANCE_IN_USE; \ 2532 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port_host_byte_order, 1); \ 2533 a->busy_##n##_ports_per_thread[thread_index]++; \ 2534 a->busy_##n##_ports++; \ 2544 return VNET_API_ERROR_NO_SUCH_ENTRY;
2555 u8 * is_identity_nat)
2561 u32 rand,
lo = 0,
hi, mid, *tmp = 0,
i;
2573 m_key.
port = clib_net_to_host_u16 (match.
port);
2578 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2584 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2604 mapping->
port = clib_host_to_net_u16 (local->
port);
2615 local = pool_elt_at_index (m->locals, i);
2618 .src_address = local->addr,
2644 mid = ((hi -
lo) >> 1) +
lo;
2646 (rand > local->
prefix) ? (lo = mid + 1) : (hi = mid);
2649 if (!(local->
prefix >= rand))
2652 mapping->
port = clib_host_to_net_u16 (local->
port);
2678 mapping->addr = m->external_addr;
2681 : clib_host_to_net_u16 (m->external_port);
2682 mapping->fib_index = sm->outside_fib_index;
2690 *twice_nat = m->twice_nat;
2711 u16 port_per_thread,
2712 u32 snat_thread_index)
2717 port_per_thread, snat_thread_index);
2725 u16 port_per_thread,
u32 snat_thread_index)
2731 for (i = 0; i <
vec_len (addresses); i++)
2736 #define _(N, j, n, s) \ 2737 case SNAT_PROTOCOL_##N: \ 2738 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \ 2740 if (a->fib_index == fib_index) \ 2744 portnum = (port_per_thread * \ 2745 snat_thread_index) + \ 2746 snat_random_port(1, port_per_thread) + 1024; \ 2747 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \ 2749 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \ 2750 a->busy_##n##_ports_per_thread[thread_index]++; \ 2751 a->busy_##n##_ports++; \ 2752 k->addr = a->addr; \ 2753 k->port = clib_host_to_net_u16(portnum); \ 2757 else if (a->fib_index == ~0) \ 2777 #define _(N, j, n, s) \ 2778 case SNAT_PROTOCOL_##N: \ 2781 portnum = (port_per_thread * \ 2782 snat_thread_index) + \ 2783 snat_random_port(1, port_per_thread) + 1024; \ 2784 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \ 2786 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \ 2787 a->busy_##n##_ports_per_thread[thread_index]++; \ 2788 a->busy_##n##_ports++; \ 2789 k->addr = a->addr; \ 2790 k->port = clib_host_to_net_u16(portnum); \ 2812 u16 port_per_thread,
u32 snat_thread_index)
2816 u16 m, ports, portnum,
A, j;
2825 #define _(N, i, n, s) \ 2826 case SNAT_PROTOCOL_##N: \ 2827 if (a->busy_##n##_ports < ports) \ 2831 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \ 2832 j = snat_random_port(0, pow2_mask(m)); \ 2833 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \ 2834 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \ 2836 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \ 2837 a->busy_##n##_ports++; \ 2838 k->addr = a->addr; \ 2839 k->port = clib_host_to_net_u16 (portnum); \ 2862 u16 port_per_thread,
u32 snat_thread_index)
2875 #define _(N, i, n, s) \ 2876 case SNAT_PROTOCOL_##N: \ 2877 if (a->busy_##n##_ports < ports) \ 2881 portnum = snat_random_port(sm->start_port, sm->end_port); \ 2882 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \ 2884 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \ 2885 a->busy_##n##_ports++; \ 2886 k->addr = a->addr; \ 2887 k->port = clib_host_to_net_u16 (portnum); \ 2912 .fp_addr.ip4.as_u32 = addr.
as_u32,
2949 s =
format (s,
"%U static-mapping-index %llu",
2979 format (s,
"local %U:%d remote %U:%d proto %U fib %d session-index %llu",
2992 u32 next_worker_index = 0;
3000 next_worker_index += sm->
workers[hash & (_vec_len (sm->
workers) - 1)];
3004 return next_worker_index;
3018 u32 next_worker_index = 0;
3028 if (!clib_bihash_search_8_8
3045 nat_reass_ip4_t *reass;
3049 if (reass && (reass->thread_index != (
u32) ~ 0))
3050 return reass->thread_index;
3063 m_key.
port = clib_net_to_host_u16 (port);
3067 if (!clib_bihash_search_8_8
3071 reass->thread_index = m->
workers[0];
3072 return reass->thread_index;
3076 reass->thread_index +=
3077 sm->
workers[(clib_net_to_host_u16 (port) - 1024) /
3079 return reass->thread_index;
3095 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3106 case SNAT_PROTOCOL_ICMP:
3107 icmp = (icmp46_header_t *) l4_header;
3111 case SNAT_PROTOCOL_UDP:
3112 case SNAT_PROTOCOL_TCP:
3124 m_key.addr = ip0->dst_address;
3125 m_key.port = clib_net_to_host_u16 (
port);
3126 m_key.protocol =
proto;
3127 m_key.fib_index = rx_fib_index0;
3128 kv.key = m_key.as_u64;
3129 if (!clib_bihash_search_8_8
3130 (&sm->static_mapping_by_external, &kv, &
value))
3133 return m->workers[0];
3138 next_worker_index = sm->first_worker_index;
3139 next_worker_index +=
3140 sm->workers[(clib_net_to_host_u16 (
port) - 1024) / sm->port_per_thread];
3141 return next_worker_index;
3210 "HANDOFF IN2OUT-OUTPUT-FEATURE (session)",
3211 next_worker_index, fib_index,
3215 return next_worker_index;
3225 next_worker_index += sm->
workers[hash & (_vec_len (sm->
workers) - 1)];
3232 next_worker_index, rx_fib_index,
3239 next_worker_index, rx_fib_index,
3244 return next_worker_index;
3264 if (
PREDICT_TRUE (proto == SNAT_PROTOCOL_UDP || proto == SNAT_PROTOCOL_TCP))
3279 next_worker_index, rx_fib_index,
3282 return next_worker_index;
3287 else if (proto == SNAT_PROTOCOL_ICMP)
3307 next_worker_index, rx_fib_index,
3310 return next_worker_index;
3321 if (!clib_bihash_search_8_8
3325 next_worker_index = m->
workers[0];
3343 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3354 case SNAT_PROTOCOL_ICMP:
3355 icmp = (icmp46_header_t *) l4_header;
3359 case SNAT_PROTOCOL_UDP:
3360 case SNAT_PROTOCOL_TCP:
3374 clib_net_to_host_u16 (
port));
3375 if (!clib_bihash_search_8_8
3376 (&sm->static_mapping_by_external, &kv, &
value))
3381 next_worker_index = m->workers[0];
3390 m->workers[hash & (_vec_len (m->workers) - 1)];
3392 next_worker_index = m->workers[hash % _vec_len (m->workers)];
3398 next_worker_index = sm->first_worker_index;
3399 next_worker_index +=
3400 sm->workers[(clib_net_to_host_u16 (
port) - 1024) / sm->port_per_thread];
3406 return next_worker_index;
3429 .ip4.as_u32 = eh_addr->
as_u32,
3436 key.
port = out_port;
3454 s->last_heard = now;
3456 s->ext_host_addr.as_u32 = eh_addr->
as_u32;
3457 s->ext_host_port = eh_port;
3487 if (clib_bihash_add_del_8_8 (&tsm->
out2in, &kv, 1))
3495 if (clib_bihash_add_del_8_8 (&tsm->
in2out, &kv, 1))
3514 (sm->
workers[(clib_net_to_host_u16 (out_port) -
3521 key.
port = out_port;
3525 if (clib_bihash_search_8_8 (&tsm->
out2in, &kv, &value))
3536 u32 total_pkts,
u64 total_bytes,
u32 thread_index)
3547 key.
port = out_port;
3551 if (clib_bihash_search_8_8 (&tsm->
out2in, &kv, &value))
3555 s->total_pkts = total_pkts;
3556 s->total_bytes = total_bytes;
3579 .ip4.as_u32 = eh_addr->
as_u32,
3586 key.
port = out_port;
3597 key.
port = ehn_port;
3613 s->last_heard = now;
3615 s->ext_host_nat_addr.as_u32 = s->ext_host_addr.as_u32 = eh_addr->
as_u32;
3616 s->ext_host_nat_port = s->ext_host_port = eh_port;
3619 s->ext_host_nat_addr.as_u32 = ehn_addr->
as_u32;
3620 s->ext_host_nat_port = ehn_port;
3649 key.
port = out_port;
3658 make_ed_kv (&kv, in_addr, &s->ext_host_nat_addr,
3660 s->ext_host_nat_port);
3661 if (clib_bihash_add_del_16_8 (&tsm->
in2out_ed, &kv, 1))
3665 s->out2in.fib_index, out_port, eh_port);
3666 if (clib_bihash_add_del_16_8 (&tsm->
out2in_ed, &kv, 1))
3685 (sm->
workers[(clib_net_to_host_u16 (out_port) -
3699 if (clib_bihash_search_16_8 (&tsm->
out2in_ed, &kv, &value))
3710 u32 fib_index,
u32 total_pkts,
u64 total_bytes,
3729 if (clib_bihash_search_16_8 (&tsm->
out2in_ed, &kv, &value))
3733 s->total_pkts = total_pkts;
3734 s->total_bytes = total_bytes;
3742 u32 translation_buckets = 1024;
3743 u32 translation_memory_size = 128 << 20;
3744 u32 user_buckets = 128;
3745 u32 user_memory_size = 64 << 20;
3746 u32 max_translations_per_user = 100;
3747 u32 outside_vrf_id = 0;
3748 u32 outside_ip6_vrf_id = 0;
3749 u32 inside_vrf_id = 0;
3750 u32 static_mapping_buckets = 1024;
3751 u32 static_mapping_memory_size = 64 << 20;
3752 u32 nat64_bib_buckets = 1024;
3753 u32 nat64_bib_memory_size = 128 << 20;
3754 u32 nat64_st_buckets = 2048;
3755 u32 nat64_st_memory_size = 256 << 20;
3756 u8 static_mapping_only = 0;
3757 u8 static_mapping_connection_tracking = 0;
3768 (input,
"translation hash buckets %d", &translation_buckets))
3770 else if (
unformat (input,
"translation hash memory %d",
3771 &translation_memory_size));
3772 else if (
unformat (input,
"user hash buckets %d", &user_buckets))
3774 else if (
unformat (input,
"user hash memory %d", &user_memory_size))
3776 else if (
unformat (input,
"max translations per user %d",
3777 &max_translations_per_user))
3779 else if (
unformat (input,
"outside VRF id %d", &outside_vrf_id))
3781 else if (
unformat (input,
"outside ip6 VRF id %d", &outside_ip6_vrf_id))
3783 else if (
unformat (input,
"inside VRF id %d", &inside_vrf_id))
3785 else if (
unformat (input,
"static mapping only"))
3787 static_mapping_only = 1;
3788 if (
unformat (input,
"connection tracking"))
3789 static_mapping_connection_tracking = 1;
3791 else if (
unformat (input,
"deterministic"))
3793 else if (
unformat (input,
"nat64 bib hash buckets %d",
3794 &nat64_bib_buckets))
3796 else if (
unformat (input,
"nat64 bib hash memory %d",
3797 &nat64_bib_memory_size))
3800 if (
unformat (input,
"nat64 st hash buckets %d", &nat64_st_buckets))
3802 else if (
unformat (input,
"nat64 st hash memory %d",
3803 &nat64_st_memory_size))
3805 else if (
unformat (input,
"out2in dpo"))
3807 else if (
unformat (input,
"dslite ce"))
3809 else if (
unformat (input,
"endpoint-dependent"))
3818 "deterministic and endpoint-dependent modes are mutually exclusive");
3822 "static mapping only mode available only for simple nat");
3826 "out2in dpo mode available only for simple nat");
3851 nat64_set_hash (nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
3852 nat64_st_memory_size);
3901 if (!static_mapping_only ||
3902 (static_mapping_only && static_mapping_connection_tracking))
3909 clib_bihash_init_16_8 (&tsm->
in2out_ed,
"in2out-ed",
3910 translation_buckets,
3911 translation_memory_size);
3912 clib_bihash_set_kvp_format_fn_16_8 (&tsm->
in2out_ed,
3915 clib_bihash_init_16_8 (&tsm->
out2in_ed,
"out2in-ed",
3916 translation_buckets,
3917 translation_memory_size);
3918 clib_bihash_set_kvp_format_fn_16_8 (&tsm->
out2in_ed,
3923 clib_bihash_init_8_8 (&tsm->
in2out,
"in2out",
3924 translation_buckets,
3925 translation_memory_size);
3926 clib_bihash_set_kvp_format_fn_8_8 (&tsm->
in2out,
3929 clib_bihash_init_8_8 (&tsm->
out2in,
"out2in",
3930 translation_buckets,
3931 translation_memory_size);
3932 clib_bihash_set_kvp_format_fn_8_8 (&tsm->
out2in,
3936 clib_bihash_init_8_8 (&tsm->
user_hash,
"users", user_buckets,
3938 clib_bihash_set_kvp_format_fn_8_8 (&tsm->
user_hash,
3950 "static_mapping_by_local", static_mapping_buckets,
3951 static_mapping_memory_size);
3956 "static_mapping_by_external",
3957 static_mapping_buckets,
3958 static_mapping_memory_size);
3974 u32 if_address_index,
u32 is_delete)
4042 u32 if_address_index,
u32 is_delete)
4072 for (j = 0; j <
vec_len (addresses); j++)
4073 if (addresses[j].
addr.as_u32 == address->
as_u32)
4125 u32 *indices_to_delete = 0;
4127 u32 *auto_add_sw_if_indices =
4134 for (i = 0; i <
vec_len (auto_add_sw_if_indices); i++)
4136 if (auto_add_sw_if_indices[i] == sw_if_index)
4151 if (vec_len (indices_to_delete))
4153 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
4164 return VNET_API_ERROR_VALUE_EXIST;
4171 return VNET_API_ERROR_NO_SUCH_ENTRY;
4196 clib_bihash_8_8_t *t;
4199 return VNET_API_ERROR_UNSUPPORTED;
4210 key.
port = clib_host_to_net_u16 (port);
4214 t = is_in ? &tsm->in2out : &tsm->out2in;
4215 if (!clib_bihash_search_8_8 (t, &kv, &value))
4218 return VNET_API_ERROR_UNSPECIFIED;
4226 return VNET_API_ERROR_NO_SUCH_ENTRY;
4232 u32 vrf_id,
int is_in)
4235 clib_bihash_16_8_t *t;
4243 return VNET_API_ERROR_FEATURE_DISABLED;
4253 t = is_in ? &tsm->in2out_ed : &tsm->out2in_ed;
4256 key.
l_port = clib_host_to_net_u16 (port);
4257 key.
r_port = clib_host_to_net_u16 (eh_port);
4262 if (clib_bihash_search_16_8 (t, &kv, &value))
4263 return VNET_API_ERROR_NO_SUCH_ENTRY;
4266 return VNET_API_ERROR_UNSPECIFIED;
4314 .name =
"nat-default",
4315 .vector_size =
sizeof (
u32),
ip4_address_t external_addr
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
clib_error_t * snat_api_init(vlib_main_t *vm, snat_main_t *sm)
void dslite_set_ce(dslite_main_t *dm, u8 set)
fib_protocol_t fp_proto
protocol type
static void snat_update_outside_fib(u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
nat_outside_fib_t * outside_fibs
void snat_ipfix_logging_max_entries_per_user(u32 thread_index, u32 limit, u32 src_ip)
Generate maximum entries per user exceeded event.
#define snat_is_session_static(s)
Check if SNAT session is created from static mapping.
u32 sessions_per_user_list_head_index
vlib_node_registration_t nat_default_node
(constructor) VLIB_REGISTER_NODE (nat_default_node)
vlib_node_registration_t nat44_ed_in2out_output_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_in2out_output_node)
#define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst)
ip4_table_bind_function_t * function
int nat_affinity_create_and_lock(ip4_address_t client_addr, ip4_address_t service_addr, u8 proto, u16 service_port, u8 backend_index, u32 sticky_time, u32 affinity_per_service_list_head_index)
Create affinity record and take reference counting lock.
int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm, u8 twice_nat)
Delete external address from NAT44 pool.
u32 hairpin_dst_node_index
void dslite_init(vlib_main_t *vm)
ip4_add_del_interface_address_callback_t * add_del_interface_address_callbacks
Functions to call when interface address changes.
u32 ed_hairpinning_node_index
static void clib_dlist_init(dlist_elt_t *pool, u32 index)
vnet_main_t * vnet_get_main(void)
#define nat_elog_info(nat_elog_str)
static void snat_add_static_mapping_when_resolved(snat_main_t *sm, ip4_address_t l_addr, u16 l_port, u32 sw_if_index, u16 e_port, u32 vrf_id, snat_protocol_t proto, int addr_only, int is_add, u8 *tag, int twice_nat, int out2in_only, int identity_nat)
static u32 nat44_session_get_timeout(snat_main_t *sm, snat_session_t *s)
u32 fq_in2out_output_index
u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation and create session if needed...
clib_error_t * nat_reass_init(vlib_main_t *vm)
Initialize NAT virtual fragmentation reassembly.
#define nat_elog_notice(nat_elog_str)
static int nat_alloc_addr_and_port_range(snat_address_t *addresses, u32 fib_index, u32 thread_index, snat_session_key_t *k, u16 port_per_thread, u32 snat_thread_index)
#define SNAT_TCP_ESTABLISHED_TIMEOUT
#define is_ed_session(s)
Check if NAT session is endpoint dependent.
static_always_inline u8 icmp_is_error_message(icmp46_header_t *icmp)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
ip4_address_t * ip4_interface_first_address(ip4_main_t *im, u32 sw_if_index, ip_interface_address_t **result_ia)
static f64 vlib_time_now(vlib_main_t *vm)
u32 fib_table_get_index_for_sw_if_index(fib_protocol_t proto, u32 sw_if_index)
Get the index of the FIB bound to the interface.
static void make_sm_kv(clib_bihash_kv_8_8_t *kv, ip4_address_t *addr, u8 proto, u32 fib_index, u16 port)
u32 vlib_frame_queue_main_init(u32 node_index, u32 frame_queue_nelts)
nat_reass_ip4_t * nat_ip4_reass_find(ip4_address_t src, ip4_address_t dst, u16 frag_id, u8 proto)
Find reassembly.
void nat_free_session_data(snat_main_t *sm, snat_session_t *s, u32 thread_index, u8 is_ha)
Free NAT44 session data (lookup keys, external addrres port)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
u32 ed_in2out_reass_node_index
void nat_ha_sref_ed_cb(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 total_pkts, u64 total_bytes, u32 thread_index)
u32 ed_out2in_slowpath_node_index
u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation and create session if needed...
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
#define NAT_INTERFACE_FLAG_IS_OUTSIDE
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. ...
u8 * format_user_kvp(u8 *s, va_list *args)
void nat_syslog_nat44_sdel(u32 ssubix, u32 sfibix, ip4_address_t *isaddr, u16 isport, ip4_address_t *idaddr, u16 idport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *xdaddr, u16 xdport, snat_protocol_t proto, u8 is_twicenat)
ip_lookup_main_t lookup_main
#define nat_elog_warn(nat_elog_str)
vlib_node_registration_t snat_det_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_det_out2in_node)
void nat64_set_hash(u32 bib_buckets, u32 bib_memory_size, u32 st_buckets, u32 st_memory_size)
Set NAT64 hash tables configuration.
int nat44_del_ed_session(snat_main_t *sm, ip4_address_t *addr, u16 port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 vrf_id, int is_in)
Delete NAT44 endpoint-dependent session.
format_function_t format_snat_key
void snat_add_del_addr_to_fib(ip4_address_t *addr, u8 p_len, u32 sw_if_index, int is_add)
Add/del NAT address to FIB.
#define VLIB_NODE_FN(node)
#define nat_elog_addr(_level, _str, _addr)
nat_alloc_out_addr_and_port_function_t * alloc_addr_and_port
static void snat_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)
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
#define snat_is_unk_proto_session(s)
Check if SNAT session for unknown protocol.
static int nat_alloc_addr_and_port_mape(snat_address_t *addresses, u32 fib_index, u32 thread_index, snat_session_key_t *k, u16 port_per_thread, u32 snat_thread_index)
static int nat_alloc_addr_and_port_default(snat_address_t *addresses, u32 fib_index, u32 thread_index, snat_session_key_t *k, u16 port_per_thread, u32 snat_thread_index)
u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation.
int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del)
Enable/disable NAT44 feature on the interface.
u32 icmp_match_out2in_ed(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
VNET_FEATURE_INIT(nat_pre_in2out, static)
nat44_lb_addr_port_t * locals
static u32 snat_get_worker_out2in_cb(ip4_header_t *ip0, u32 rx_fib_index0, u8 is_output)
clib_bihash_8_8_t user_hash
static int ip4_is_fragment(const ip4_header_t *i)
int nat44_add_del_lb_static_mapping(ip4_address_t e_addr, u16 e_port, snat_protocol_t proto, nat44_lb_addr_port_t *locals, u8 is_add, twice_nat_type_t twice_nat, u8 out2in_only, u8 *tag, u32 affinity)
Add/delete static mapping with load-balancing (multiple backends)
snat_session_t * nat_session_alloc_or_recycle(snat_main_t *sm, snat_user_t *u, u32 thread_index, f64 now)
Allocate new NAT session or recycle last used.
#define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY
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.
u32 max_translations_per_user
u32 in2out_reass_node_index
void nat_ha_sadd_cb(ip4_address_t *in_addr, u16 in_port, ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, ip4_address_t *ehn_addr, u16 ehn_port, u8 proto, u32 fib_index, u16 flags, u32 thread_index)
u32 in2out_output_node_index
u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index)
#define static_always_inline
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
static_always_inline int get_icmp_o2i_ed_key(ip4_header_t *ip0, nat_ed_ses_key_t *p_key0)
vl_api_interface_index_t sw_if_index
#define VLIB_INIT_FUNCTION(x)
vlib_node_registration_t snat_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_node)
void fib_table_entry_special_remove(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source)
Remove a 'special' entry from the FIB.
u8 * format_static_mapping_kvp(u8 *s, va_list *args)
void nat_ha_sadd_ed_cb(ip4_address_t *in_addr, u16 in_port, ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, ip4_address_t *ehn_addr, u16 ehn_port, u8 proto, u32 fib_index, u16 flags, u32 thread_index)
u32 det_in2out_node_index
int snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id, u8 twice_nat)
Add external address to NAT44 pool.
u8 * format_ed_session_kvp(u8 *s, va_list *args)
A high priority source a plugin can use.
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Aggregate type for a prefix.
#define clib_error_return(e, args...)
#define is_fwd_bypass_session(s)
Check if NAT session is forwarding bypass.
static u32 snat_get_worker_in2out_cb(ip4_header_t *ip0, u32 rx_fib_index0, u8 is_output)
void snat_ipfix_logging_init(vlib_main_t *vm)
Initialize NAT plugin IPFIX logging.
static void * ip4_next_header(ip4_header_t *i)
void snat_ipfix_logging_addresses_exhausted(u32 thread_index, u32 pool_id)
Generate NAT addresses exhausted event.
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
void nat_ha_sdel_ed_cb(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 ti)
fib_node_index_t fib_table_lookup(u32 fib_index, const fib_prefix_t *prefix)
Perfom a longest prefix match in the non-forwarding table.
static void nat_ip4_add_del_addr_only_sm_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)
void nat_affinity_unlock(ip4_address_t client_addr, ip4_address_t service_addr, u8 proto, u16 service_port)
Release a reference counting lock for affinity.
int snat_static_mapping_match(snat_main_t *sm, snat_session_key_t match, snat_session_key_t *mapping, u8 by_external, u8 *is_addr_only, twice_nat_type_t *twice_nat, lb_nat_type_t *lb, ip4_address_t *ext_host_addr, u8 *is_identity_nat)
Match NAT44 static mapping.
void snat_free_outside_address_and_port(snat_address_t *addresses, u32 thread_index, snat_session_key_t *k)
Free outside address and port pair.
u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation and create session if needed...
static void nat44_delete_session(snat_main_t *sm, snat_session_t *ses, u32 thread_index)
char * name
The counter collection's name.
vl_api_fib_path_type_t type
twice_nat_type_t twice_nat
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
u32 * auto_add_sw_if_indices_twice_nat
void nat66_init(vlib_main_t *vm)
vlib_node_registration_t nat44_ed_out2in_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_out2in_node)
int snat_alloc_outside_address_and_port(snat_address_t *addresses, u32 fib_index, u32 thread_index, snat_session_key_t *k, u16 port_per_thread, u32 snat_thread_index)
Alloc outside address and port.
snat_user_t * nat_user_get_or_create(snat_main_t *sm, ip4_address_t *addr, u32 fib_index, u32 thread_index)
Find or create NAT user.
#define clib_bitmap_foreach(i, ai, body)
Macro to iterate across set bits in a bitmap.
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
void nat_dpo_module_init(void)
u32 handoff_in2out_output_index
nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg
clib_bihash_16_8_t out2in_ed
static uword clib_bitmap_last_set(uword *ai)
Return the higest numbered set bit in a bitmap.
void nat_ha_sdel_cb(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 ti)
void nat_ha_sref_cb(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 total_pkts, u64 total_bytes, u32 thread_index)
static void clib_dlist_addtail(dlist_elt_t *pool, u32 head_index, u32 new_index)
u32 pre_in2out_node_index
snat_session_t * nat_ed_session_alloc(snat_main_t *sm, snat_user_t *u, u32 thread_index, f64 now)
Allocate NAT endpoint-dependent session.
snat_static_mapping_t * static_mappings
#define pool_put(P, E)
Free an object E in pool P.
#define vec_dup(V)
Return copy of vector (no header, no alignment)
#define VLIB_CONFIG_FUNCTION(x, n,...)
#define vec_del1(v, i)
Delete the element at index I.
u32 hairpin_src_node_index
clib_bihash_8_8_t static_mapping_by_external
u32 in2out_slowpath_output_node_index
static void vlib_set_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 value)
Set a simple counter.
void nat_set_alloc_addr_and_port_default(void)
Set address and port assignment algorithm to default/standard.
void fib_table_unlock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Take a reference counting lock on the table.
#define is_lb_session(s)
Check if NAT session is load-balancing.
u32 fib_entry_get_resolving_interface(fib_node_index_t entry_index)
snat_interface_t * output_feature_interfaces
u32 pre_out2in_node_index
u32 ed_hairpin_src_node_index
#define pool_free(p)
Free a pool.
void snat_ipfix_logging_nat44_ses_delete(u32 thread_index, u32 src_ip, u32 nat_src_ip, snat_protocol_t snat_proto, u16 src_port, u16 nat_src_port, u32 vrf_id)
Generate NAT44 session delete event.
vlib_node_registration_t snat_det_in2out_node
(constructor) VLIB_REGISTER_NODE (snat_det_in2out_node)
vlib_node_registration_t nat_pre_in2out_node
(constructor) VLIB_REGISTER_NODE (nat_pre_in2out_node)
#define is_affinity_sessions(s)
Check if NAT session has affinity record.
#define VLIB_REGISTER_NODE(x,...)
u32 ed_out2in_reass_node_index
static u8 snat_proto_to_ip_proto(snat_protocol_t snat_proto)
#define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT
u32 nat_affinity_get_per_service_list_head_index(void)
Get new affinity per service list head index.
static void clib_dlist_addhead(dlist_elt_t *pool, u32 head_index, u32 new_index)
static u32 random_u32_max(void)
Maximum value returned by random_u32()
static_always_inline uword vlib_get_thread_index(void)
u8 nat_reass_is_drop_frag(u8 is_ip6)
Get status of virtual fragmentation reassembly.
void nat44_add_del_address_dpo(ip4_address_t addr, u8 is_add)
Add/delete external address to FIB DPO (out2in DPO mode)
vlib_node_registration_t snat_in2out_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_node)
u8 static_mapping_connection_tracking
snat_get_worker_function_t * worker_in2out_cb
#define vec_free(V)
Free vector's memory (no header).
void fib_table_entry_delete(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source)
Delete a FIB entry.
ip4_add_del_interface_address_function_t * function
deterministic NAT definitions
int nat_affinity_find_and_lock(ip4_address_t client_addr, ip4_address_t service_addr, u8 proto, u16 service_port, u8 *backend_index)
Find service backend index for client-IP and take a reference counting lock.
u32 ed_in2out_slowpath_node_index
int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside, int is_del)
Enable/disable NAT44 output feature on the interface (postrouting NAT)
#define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
dslite_main_t dslite_main
static void snat_ip4_table_bind(ip4_main_t *im, uword opaque, u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
u8 * format_session_kvp(u8 *s, va_list *args)
u32 fib_node_index_t
A typedef of a node index.
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
8 octet key, 8 octet key value pair
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
u32 ed_hairpin_dst_node_index
static void make_ed_kv(clib_bihash_kv_16_8_t *kv, ip4_address_t *l_addr, ip4_address_t *r_addr, u8 proto, u32 fib_index, u16 l_port, u16 r_port)
u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation and create session if needed...
u32 tcp_transitory_timeout
int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, u16 l_port, u16 e_port, u32 vrf_id, int addr_only, u32 sw_if_index, snat_protocol_t proto, int is_add, twice_nat_type_t twice_nat, u8 out2in_only, u8 *tag, u8 identity_nat)
Add/delete NAT44 static mapping.
void fib_table_lock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Release a reference counting lock on the table.
u32 * auto_add_sw_if_indices
u32 in2out_fast_node_index
fib_node_index_t fib_table_entry_special_dpo_add(u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, const dpo_id_t *dpo)
Add a 'special' entry to the FIB that links to the DPO passed A special entry is an entry that the FI...
void vlib_validate_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
validate a simple counter
static_always_inline u16 snat_random_port(u16 min, u16 max)
#define NAT_INTERFACE_FLAG_IS_INSIDE
#define is_addr_only_static_mapping(sm)
Check if NAT static mapping is address only (1:1NAT).
#define is_identity_static_mapping(sm)
Check if NAT static mapping is identity NAT.
u32 det_out2in_node_index
snat_get_worker_function_t * worker_out2in_cb
clib_error_t * nat_affinity_init(vlib_main_t *vm)
Initialize NAT client-IP based affinity.
u32 in2out_slowpath_node_index
snat_icmp_match_function_t * icmp_match_out2in_cb
vlib_log_class_t log_class
#define SNAT_SESSION_FLAG_TWICE_NAT
void nat_set_alloc_addr_and_port_range(u16 start_port, u16 end_port)
Set address and port assignment algorithm for port range.
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.
#define clib_bitmap_free(v)
Free a bitmap.
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.
uword * thread_registrations_by_name
vlib_node_registration_t nat44_ed_in2out_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_in2out_node)
int nat44_del_session(snat_main_t *sm, ip4_address_t *addr, u16 port, snat_protocol_t proto, u32 vrf_id, int is_in)
Delete NAT44 session.
snat_address_t * twice_nat_addresses
#define VNET_FEATURES(...)
void nat_syslog_nat44_apmdel(u32 ssubix, u32 sfibix, ip4_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, snat_protocol_t proto)
static clib_error_t * snat_init(vlib_main_t *vm)
void nat_ha_sdel(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 thread_index)
Create session delete HA event.
static uword is_pow2(uword x)
u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation.
ip4_table_bind_callback_t * table_bind_callbacks
Functions to call when interface to table biding changes.
clib_error_t * nat64_init(vlib_main_t *vm)
Initialize NAT64.
u32 out2in_reass_node_index
NAT64 global declarations.
#define is_lb_static_mapping(sm)
Check if NAT static mapping is load-balancing.
void increment_v4_address(ip4_address_t *a)
Increment IPv4 address.
char * stat_segment_name
Name in stat segment directory.
vlib_simple_counter_main_t total_users
static int ip4_is_first_fragment(const ip4_header_t *i)
#define nat_elog_err(nat_elog_str)
static u32 ip_proto_to_snat_proto(u8 ip_proto)
#define FIB_NODE_INDEX_INVALID
format_function_t format_static_mapping_key
static void user_session_increment(snat_main_t *sm, snat_user_t *u, u8 is_static)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u32 hairpinning_node_index
#define is_out2in_only_static_mapping(sm)
Check if NAT static mapping match only out2in direction.
snat_main_per_thread_data_t * per_thread_data
static u32 nat44_ed_get_worker_out2in_cb(ip4_header_t *ip, u32 rx_fib_index, u8 is_output)
void nat_affinity_flush_service(u32 affinity_per_service_list_head_index)
Flush all service affinity data.
#define DPO_INVALID
An initialiser for DPOs declared on the stack.
u32 affinity_per_service_list_head_index
snat_address_t * addresses
void nat_dpo_create(dpo_proto_t dproto, u32 aftr_index, dpo_id_t *dpo)
int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del, u8 twice_nat)
Add/delete NAT44 pool address from specific interfce.
u32 out2in_fast_node_index
#define hash_get_mem(h, key)
#define SNAT_ICMP_TIMEOUT
snat_static_map_resolve_t * to_resolve
static u32 random_u32(u32 *seed)
32-bit random number generator
#define SNAT_SESSION_FLAG_STATIC_MAPPING
ip4_main_t ip4_main
Global ip4 main structure.
static vlib_thread_main_t * vlib_get_thread_main()
static u32 get_thread_idx_by_port(u16 e_port)
u32 translation_memory_size
void dpo_reset(dpo_id_t *dpo)
reset a DPO ID The DPO will be unlocked.
#define vec_foreach(var, vec)
Vector iterator.
vlib_node_registration_t snat_in2out_output_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_output_node)
int snat_set_workers(uword *bitmap)
Set NAT plugin workers.
u32 icmp_match_in2out_ed(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b, ip4_header_t *ip, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
#define is_twice_nat_session(s)
Check if NAT session is twice NAT.
clib_bihash_16_8_t in2out_ed
static clib_error_t * snat_config(vlib_main_t *vm, unformat_input_t *input)
static u32 nat44_ed_get_worker_in2out_cb(ip4_header_t *ip, u32 rx_fib_index, u8 is_output)
#define pool_foreach_index(i, v, body)
Iterate pool by index.
NAT plugin virtual fragmentation reassembly.
void nat_set_alloc_addr_and_port_mape(u16 psid, u16 psid_offset, u16 psid_length)
Set address and port assignment algorithm for MAP-E CE.
NAT66 global declarations.
NAT plugin client-IP based session affinity for load-balancing.
vlib_simple_counter_main_t total_sessions
#define SNAT_TCP_TRANSITORY_TIMEOUT
ip_lookup_main_t * ip4_lookup_main
#define NAT_STATIC_MAPPING_FLAG_LB
static int is_snat_address_used_in_static_mapping(snat_main_t *sm, ip4_address_t addr)
#define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY
snat_session_t * sessions
A low (below routing) priority source a plugin can use.
snat_icmp_match_function_t * icmp_match_in2out_cb
clib_bihash_8_8_t static_mapping_by_local
int nat44_lb_static_mapping_add_del_local(ip4_address_t e_addr, u16 e_port, ip4_address_t l_addr, u16 l_port, snat_protocol_t proto, u32 vrf_id, u8 probability, u8 is_add)
snat_interface_t * interfaces
void nat_ha_init(vlib_main_t *vm, nat_ha_sadd_cb_t sadd_cb, nat_ha_sdel_cb_t sdel_cb, nat_ha_sref_cb_t sref_cb)
Initialize NAT HA.
vl_api_fib_path_nh_proto_t proto
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)
nat_reass_ip4_t * nat_ip4_reass_create(ip4_address_t src, ip4_address_t dst, u16 frag_id, u8 proto)
Create reassembly.
vlib_node_registration_t nat_pre_out2in_node
(constructor) VLIB_REGISTER_NODE (nat_pre_out2in_node)
static u32 clib_dlist_remove_head(dlist_elt_t *pool, u32 head_index)
static int nat_set_outside_address_and_port(snat_address_t *addresses, u32 thread_index, snat_session_key_t *k)
u32 tcp_established_timeout
static uword pool_elts(void *v)
Number of active elements in a pool.