2 * Copyright (c) 2021 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 #include <arpa/inet.h>
19 #include <vlib/vlib.h>
20 #include <vnet/feature/feature.h>
21 #include <vnet/fib/fib_table.h>
22 #include <vnet/ip/format.h>
23 #include <vnet/ip/ip4.h>
24 #include <vnet/ip/ip4_packet.h>
25 #include <vnet/ip/reass/ip4_sv_reass.h>
26 #include <vppinfra/clib_error.h>
29 * This is the main control plane part of the PNAT (Policy 1:1 NAT) feature.
32 pnat_main_t pnat_main;
35 * Do a lookup in the interface vector (interface_by_sw_if_index)
36 * and return pool entry.
38 pnat_interface_t *pnat_interface_by_sw_if_index(u32 sw_if_index) {
39 pnat_main_t *pm = &pnat_main;
41 if (!pm->interface_by_sw_if_index ||
42 sw_if_index > (vec_len(pm->interface_by_sw_if_index) - 1))
44 u32 index = pm->interface_by_sw_if_index[sw_if_index];
47 if (pool_is_free_index(pm->interfaces, index))
49 return pool_elt_at_index(pm->interfaces, index);
52 static pnat_mask_fast_t pnat_mask2fast(pnat_mask_t lookup_mask) {
53 pnat_mask_fast_t m = {0};
55 if (lookup_mask & PNAT_SA)
56 m.as_u64[0] = 0xffffffff00000000;
57 if (lookup_mask & PNAT_DA)
58 m.as_u64[0] |= 0x00000000ffffffff;
59 m.as_u64[1] = 0xffffffff00000000;
60 if (lookup_mask & PNAT_SPORT)
61 m.as_u64[1] |= 0x00000000ffff0000;
62 if (lookup_mask & PNAT_DPORT)
63 m.as_u64[1] |= 0x000000000000ffff;
68 * Create new PNAT interface object and register the pnat feature in the
69 * corresponding feature chain.
70 * Also enable shallow virtual reassembly, to ensure that we have
71 * L4 ports available for all packets we receive.
73 static clib_error_t *pnat_enable_interface(u32 sw_if_index,
74 pnat_attachment_point_t attachment,
76 pnat_main_t *pm = &pnat_main;
77 pnat_interface_t *interface = pnat_interface_by_sw_if_index(sw_if_index);
80 pool_get_zero(pm->interfaces, interface);
81 interface->sw_if_index = sw_if_index;
82 vec_validate_init_empty(pm->interface_by_sw_if_index, sw_if_index, ~0);
83 pm->interface_by_sw_if_index[sw_if_index] = interface - pm->interfaces;
91 nodename = "pnat-input";
92 arcname = "ip4-unicast";
97 nodename = "pnat-output";
98 arcname = "ip4-output";
101 return clib_error_return(0, "Unknown attachment point %u %u",
102 sw_if_index, attachment);
105 if (!interface->enabled[attachment]) {
106 if (vnet_feature_enable_disable(arcname, nodename, sw_if_index, 1, 0,
108 return clib_error_return(0, "PNAT feature enable failed on %u",
112 /* TODO: Make shallow virtual reassembly configurable */
113 ip4_sv_reass_enable_disable_with_refcnt(sw_if_index, 1);
115 ip4_sv_reass_output_enable_disable_with_refcnt(sw_if_index, 1);
118 interface->lookup_mask[attachment] = mask;
119 interface->lookup_mask_fast[attachment] = pnat_mask2fast(mask);
120 interface->enabled[attachment] = true;
123 pnat_mask_t current_mask = interface->lookup_mask[attachment];
124 if (current_mask != mask) {
125 return clib_error_return(0,
126 "PNAT lookup mask must be consistent per "
127 "interface/direction %u",
132 interface->refcount++;
138 * Delete interface object when no rules reference the interface.
140 static int pnat_disable_interface(u32 sw_if_index,
141 pnat_attachment_point_t attachment) {
142 pnat_main_t *pm = &pnat_main;
143 pnat_interface_t *interface = pnat_interface_by_sw_if_index(sw_if_index);
147 if (interface->refcount == 0)
150 if (interface->enabled[attachment] && attachment == PNAT_IP4_INPUT) {
151 if (ip4_sv_reass_enable_disable_with_refcnt(sw_if_index, 0) != 0)
153 if (vnet_feature_enable_disable("ip4-unicast", "pnat-input",
154 sw_if_index, 0, 0, 0) != 0)
157 if (interface->enabled[attachment] && attachment == PNAT_IP4_OUTPUT) {
158 if (ip4_sv_reass_output_enable_disable_with_refcnt(sw_if_index, 0) != 0)
160 if (vnet_feature_enable_disable("ip4-output", "pnat-output",
161 sw_if_index, 0, 0, 0) != 0)
165 interface->lookup_mask[attachment] = 0;
166 interface->enabled[attachment] = false;
168 interface->refcount--;
169 if (interface->refcount == 0) {
170 pm->interface_by_sw_if_index[sw_if_index] = ~0;
171 pool_put(pm->interfaces, interface);
177 * From a 5-tuple (with mask) calculate the key used in the flow cache lookup.
179 static inline void pnat_calc_key_from_5tuple(u32 sw_if_index,
180 pnat_attachment_point_t attachment,
181 pnat_5tuple_t *match,
182 clib_bihash_kv_16_8_t *kv) {
183 pnat_mask_fast_t mask = pnat_mask2fast(match->mask);
184 ip4_address_t src, dst;
185 clib_memcpy(&src, &match->src, 4);
186 clib_memcpy(&dst, &match->dst, 4);
187 pnat_calc_key(sw_if_index, attachment, src, dst, match->proto,
188 htons(match->sport), htons(match->dport), mask, kv);
192 * Map between the 5-tuple mask and the instruction set of the rewrite node.
194 pnat_instructions_t pnat_instructions_from_mask(pnat_mask_t m) {
195 pnat_instructions_t i = 0;
198 i |= PNAT_INSTR_SOURCE_ADDRESS;
200 i |= PNAT_INSTR_DESTINATION_ADDRESS;
202 i |= PNAT_INSTR_SOURCE_PORT;
204 i |= PNAT_INSTR_DESTINATION_PORT;
209 * "Init" the PNAT datastructures. Called upon first creation of a PNAT rule.
210 * TODO: Make number of buckets configurable.
212 static void pnat_enable(void) {
213 pnat_main_t *pm = &pnat_main;
217 /* Create new flow cache table */
218 clib_bihash_init_16_8(&pm->flowhash, "PNAT flow hash",
219 PNAT_FLOW_HASH_BUCKETS, 0);
223 static void pnat_disable(void) {
224 pnat_main_t *pm = &pnat_main;
228 if (pool_elts(pm->translations))
231 /* Delete flow cache table */
232 clib_bihash_free_16_8(&pm->flowhash);
238 * Ensure that a new rule lookup mask matches what's installed on interface
240 static int pnat_interface_check_mask(u32 sw_if_index,
241 pnat_attachment_point_t attachment,
243 pnat_interface_t *interface = pnat_interface_by_sw_if_index(sw_if_index);
246 if (!interface->enabled[attachment])
248 if (interface->lookup_mask[attachment] != mask)
254 int pnat_binding_add(pnat_5tuple_t *match, pnat_5tuple_t *rewrite, u32 *index) {
255 pnat_main_t *pm = &pnat_main;
259 /* If we aren't matching or rewriting, why are we here? */
260 if (match->mask == 0 || rewrite->mask == 0)
263 /* Check if protocol is set if ports are set */
264 if ((match->dport || match->sport) &&
265 (match->proto != IP_API_PROTO_UDP && match->proto != IP_API_PROTO_TCP))
268 /* Create pool entry */
269 pnat_translation_t *t;
270 pool_get_zero(pm->translations, t);
271 memcpy(&t->post_da, &rewrite->dst, 4);
272 memcpy(&t->post_sa, &rewrite->src, 4);
273 t->post_sp = rewrite->sport;
274 t->post_dp = rewrite->dport;
275 t->instructions = pnat_instructions_from_mask(rewrite->mask);
277 /* These are only used for show commands and trace */
280 /* Rewrite of protocol is not supported, ignore. */
281 t->rewrite = *rewrite;
282 t->rewrite.proto = 0;
284 *index = t - pm->translations;
288 u32 pnat_flow_lookup(u32 sw_if_index, pnat_attachment_point_t attachment,
289 pnat_5tuple_t *match) {
290 pnat_main_t *pm = &pnat_main;
291 clib_bihash_kv_16_8_t kv, value;
292 pnat_calc_key_from_5tuple(sw_if_index, attachment, match, &kv);
293 if (clib_bihash_search_16_8(&pm->flowhash, &kv, &value) == 0) {
299 int pnat_binding_attach(u32 sw_if_index, pnat_attachment_point_t attachment,
301 pnat_main_t *pm = &pnat_main;
303 if (!pm->translations ||
304 pool_is_free_index(pm->translations, binding_index))
307 pnat_translation_t *t = pool_elt_at_index(pm->translations, binding_index);
309 if (pnat_interface_check_mask(sw_if_index, attachment, t->match.mask) != 0)
314 /* Verify non-duplicate */
315 clib_bihash_kv_16_8_t kv, value;
316 pnat_calc_key_from_5tuple(sw_if_index, attachment, &t->match, &kv);
317 if (clib_bihash_search_16_8(&pm->flowhash, &kv, &value) == 0) {
321 /* Create flow cache */
322 kv.value = binding_index;
323 if (clib_bihash_add_del_16_8(&pm->flowhash, &kv, 1)) {
324 pool_put(pm->translations, t);
328 /* Register interface */
329 pnat_enable_interface(sw_if_index, attachment, t->match.mask);
334 int pnat_binding_detach(u32 sw_if_index, pnat_attachment_point_t attachment,
336 pnat_main_t *pm = &pnat_main;
338 if (!pm->translations ||
339 pool_is_free_index(pm->translations, binding_index))
342 pnat_translation_t *t = pool_elt_at_index(pm->translations, binding_index);
344 /* Verify non-duplicate */
345 clib_bihash_kv_16_8_t kv;
346 pnat_calc_key_from_5tuple(sw_if_index, attachment, &t->match, &kv);
347 if (clib_bihash_add_del_16_8(&pm->flowhash, &kv, 0)) {
351 /* Deregister interface */
352 pnat_disable_interface(sw_if_index, attachment);
360 * Delete a translation using the index returned from pnat_add_translation.
362 int pnat_binding_del(u32 index) {
363 pnat_main_t *pm = &pnat_main;
365 if (pool_is_free_index(pm->translations, index)) {
366 clib_warning("Binding delete: translation does not exist: %d", index);
370 pnat_translation_t *t = pool_elt_at_index(pm->translations, index);
371 pool_put(pm->translations, t);