2 * Copyright (c) 2018 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief NAT66 implementation
20 #include <nat/nat66.h>
21 #include <vnet/fib/fib_table.h>
22 #include <vnet/ip/reass/ip6_sv_reass.h>
24 nat66_main_t nat66_main;
28 /* Hook up input features */
29 VNET_FEATURE_INIT (nat66_in2out, static) = {
30 .arc_name = "ip6-unicast",
31 .node_name = "nat66-in2out",
32 .runs_before = VNET_FEATURES ("ip6-lookup"),
33 .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
35 VNET_FEATURE_INIT (nat66_out2in, static) = {
36 .arc_name = "ip6-unicast",
37 .node_name = "nat66-out2in",
38 .runs_before = VNET_FEATURES ("ip6-lookup"),
39 .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
46 nat66_init (vlib_main_t * vm)
48 nat66_main_t *nm = &nat66_main;
50 u32 static_mapping_buckets = 1024;
51 uword static_mapping_memory_size = 64 << 20;
53 node = vlib_get_node_by_name (vm, (u8 *) "nat66-in2out");
54 nm->in2out_node_index = node->index;
56 node = vlib_get_node_by_name (vm, (u8 *) "nat66-out2in");
57 nm->out2in_node_index = node->index;
59 clib_bihash_init_24_8 (&nm->sm_l, "nat66-static-map-by-local",
60 static_mapping_buckets, static_mapping_memory_size);
61 clib_bihash_init_24_8 (&nm->sm_e, "nat66-static-map-by-external",
62 static_mapping_buckets, static_mapping_memory_size);
64 nm->session_counters.name = "session counters";
68 nat66_interface_add_del (u32 sw_if_index, u8 is_inside, u8 is_add)
70 nat66_main_t *nm = &nat66_main;
71 snat_interface_t *interface = 0, *i;
72 const char *feature_name;
75 pool_foreach (i, nm->interfaces,
77 if (i->sw_if_index == sw_if_index)
88 return VNET_API_ERROR_VALUE_EXIST;
90 pool_get (nm->interfaces, interface);
91 interface->sw_if_index = sw_if_index;
93 is_inside ? NAT_INTERFACE_FLAG_IS_INSIDE :
94 NAT_INTERFACE_FLAG_IS_OUTSIDE;
99 return VNET_API_ERROR_NO_SUCH_ENTRY;
101 pool_put (nm->interfaces, interface);
104 feature_name = is_inside ? "nat66-in2out" : "nat66-out2in";
105 int rv = ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
108 return vnet_feature_enable_disable ("ip6-unicast", feature_name,
109 sw_if_index, is_add, 0, 0);
113 nat66_interfaces_walk (nat66_interface_walk_fn_t fn, void *ctx)
115 nat66_main_t *nm = &nat66_main;
116 snat_interface_t *i = 0;
119 pool_foreach (i, nm->interfaces,
127 nat66_static_mapping_t *
128 nat66_static_mapping_get (ip6_address_t * addr, u32 fib_index, u8 is_local)
130 nat66_main_t *nm = &nat66_main;
131 nat66_static_mapping_t *sm = 0;
132 nat66_sm_key_t sm_key;
133 clib_bihash_kv_24_8_t kv, value;
135 sm_key.addr.as_u64[0] = addr->as_u64[0];
136 sm_key.addr.as_u64[1] = addr->as_u64[1];
137 sm_key.fib_index = fib_index;
140 kv.key[0] = sm_key.as_u64[0];
141 kv.key[1] = sm_key.as_u64[1];
142 kv.key[2] = sm_key.as_u64[2];
144 if (!clib_bihash_search_24_8
145 (is_local ? &nm->sm_l : &nm->sm_e, &kv, &value))
146 sm = pool_elt_at_index (nm->sm, value.value);
152 nat66_static_mapping_add_del (ip6_address_t * l_addr, ip6_address_t * e_addr,
153 u32 vrf_id, u8 is_add)
155 nat66_main_t *nm = &nat66_main;
157 nat66_static_mapping_t *sm = 0;
158 nat66_sm_key_t sm_key;
159 clib_bihash_kv_24_8_t kv, value;
160 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP6, vrf_id);
162 sm_key.addr.as_u64[0] = l_addr->as_u64[0];
163 sm_key.addr.as_u64[1] = l_addr->as_u64[1];
164 sm_key.fib_index = fib_index;
166 kv.key[0] = sm_key.as_u64[0];
167 kv.key[1] = sm_key.as_u64[1];
168 kv.key[2] = sm_key.as_u64[2];
170 if (!clib_bihash_search_24_8 (&nm->sm_l, &kv, &value))
171 sm = pool_elt_at_index (nm->sm, value.value);
176 return VNET_API_ERROR_VALUE_EXIST;
178 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
180 pool_get (nm->sm, sm);
181 clib_memset (sm, 0, sizeof (*sm));
182 sm->l_addr.as_u64[0] = l_addr->as_u64[0];
183 sm->l_addr.as_u64[1] = l_addr->as_u64[1];
184 sm->e_addr.as_u64[0] = e_addr->as_u64[0];
185 sm->e_addr.as_u64[1] = e_addr->as_u64[1];
186 sm->fib_index = fib_index;
188 sm_key.fib_index = fib_index;
189 kv.key[0] = sm_key.as_u64[0];
190 kv.key[1] = sm_key.as_u64[1];
191 kv.key[2] = sm_key.as_u64[2];
192 kv.value = sm - nm->sm;
193 if (clib_bihash_add_del_24_8 (&nm->sm_l, &kv, 1))
194 nat_elog_warn ("nat66-static-map-by-local add key failed");
195 sm_key.addr.as_u64[0] = e_addr->as_u64[0];
196 sm_key.addr.as_u64[1] = e_addr->as_u64[1];
197 sm_key.fib_index = 0;
198 kv.key[0] = sm_key.as_u64[0];
199 kv.key[1] = sm_key.as_u64[1];
200 kv.key[2] = sm_key.as_u64[2];
201 if (clib_bihash_add_del_24_8 (&nm->sm_e, &kv, 1))
202 nat_elog_warn ("nat66-static-map-by-external add key failed");
204 vlib_validate_combined_counter (&nm->session_counters, kv.value);
205 vlib_zero_combined_counter (&nm->session_counters, kv.value);
210 return VNET_API_ERROR_NO_SUCH_ENTRY;
212 kv.value = sm - nm->sm;
213 if (clib_bihash_add_del_24_8 (&nm->sm_l, &kv, 0))
214 nat_elog_warn ("nat66-static-map-by-local delete key failed");
215 sm_key.addr.as_u64[0] = e_addr->as_u64[0];
216 sm_key.addr.as_u64[1] = e_addr->as_u64[1];
217 sm_key.fib_index = 0;
218 kv.key[0] = sm_key.as_u64[0];
219 kv.key[1] = sm_key.as_u64[1];
220 kv.key[2] = sm_key.as_u64[2];
221 if (clib_bihash_add_del_24_8 (&nm->sm_e, &kv, 0))
222 nat_elog_warn ("nat66-static-map-by-external delete key failed");
223 fib_table_unlock (sm->fib_index, FIB_PROTOCOL_IP6, nat_fib_src_hi);
224 pool_put (nm->sm, sm);
231 nat66_static_mappings_walk (nat66_static_mapping_walk_fn_t fn, void *ctx)
233 nat66_main_t *nm = &nat66_main;
234 nat66_static_mapping_t *sm = 0;
237 pool_foreach (sm, nm->sm,
246 * fd.io coding-style-patch-verification: ON
249 * eval: (c-set-style "gnu")