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 if (ip4_sv_reass_enable_disable_with_refcnt(sw_if_index, 1) != 0)
114 return clib_error_return(0, "PNAT SVR enable failed on %u",
118 if (ip4_sv_reass_output_enable_disable_with_refcnt(sw_if_index,
120 return clib_error_return(0, "PNAT SVR enable failed on %u",
124 interface->lookup_mask[attachment] = mask;
125 interface->lookup_mask_fast[attachment] = pnat_mask2fast(mask);
126 interface->enabled[attachment] = true;
129 pnat_mask_t current_mask = interface->lookup_mask[attachment];
130 if (current_mask != mask) {
131 return clib_error_return(0,
132 "PNAT lookup mask must be consistent per "
133 "interface/direction %u",
138 interface->refcount++;
144 * Delete interface object when no rules reference the interface.
146 static int pnat_disable_interface(u32 sw_if_index,
147 pnat_attachment_point_t attachment) {
148 pnat_main_t *pm = &pnat_main;
149 pnat_interface_t *interface = pnat_interface_by_sw_if_index(sw_if_index);
153 if (interface->refcount == 0)
156 if (interface->enabled[attachment] && attachment == PNAT_IP4_INPUT) {
157 if (ip4_sv_reass_enable_disable_with_refcnt(sw_if_index, 0) != 0)
159 if (vnet_feature_enable_disable("ip4-unicast", "pnat-input",
160 sw_if_index, 0, 0, 0) != 0)
163 if (interface->enabled[attachment] && attachment == PNAT_IP4_OUTPUT) {
164 if (ip4_sv_reass_output_enable_disable_with_refcnt(sw_if_index, 0) != 0)
166 if (vnet_feature_enable_disable("ip4-output", "pnat-output",
167 sw_if_index, 0, 0, 0) != 0)
171 interface->lookup_mask[attachment] = 0;
172 interface->enabled[attachment] = false;
174 interface->refcount--;
175 if (interface->refcount == 0) {
176 pm->interface_by_sw_if_index[sw_if_index] = ~0;
177 pool_put(pm->interfaces, interface);
183 * From a 5-tuple (with mask) calculate the key used in the flow cache lookup.
185 static inline void pnat_calc_key_from_5tuple(u32 sw_if_index,
186 pnat_attachment_point_t attachment,
187 pnat_match_tuple_t *match,
188 clib_bihash_kv_16_8_t *kv) {
189 pnat_mask_fast_t mask = pnat_mask2fast(match->mask);
190 ip4_address_t src, dst;
191 clib_memcpy(&src, &match->src, 4);
192 clib_memcpy(&dst, &match->dst, 4);
193 pnat_calc_key(sw_if_index, attachment, src, dst, match->proto,
194 htons(match->sport), htons(match->dport), mask, kv);
198 * Map between the 5-tuple mask and the instruction set of the rewrite node.
200 pnat_instructions_t pnat_instructions_from_mask(pnat_mask_t m) {
201 pnat_instructions_t i = 0;
204 i |= PNAT_INSTR_SOURCE_ADDRESS;
206 i |= PNAT_INSTR_DESTINATION_ADDRESS;
208 i |= PNAT_INSTR_SOURCE_PORT;
210 i |= PNAT_INSTR_DESTINATION_PORT;
211 if (m & PNAT_COPY_BYTE)
212 i |= PNAT_INSTR_COPY_BYTE;
213 if (m & PNAT_CLEAR_BYTE)
214 i |= PNAT_INSTR_CLEAR_BYTE;
219 * "Init" the PNAT datastructures. Called upon first creation of a PNAT rule.
220 * TODO: Make number of buckets configurable.
222 static void pnat_enable(void) {
223 pnat_main_t *pm = &pnat_main;
227 /* Create new flow cache table */
228 clib_bihash_init_16_8(&pm->flowhash, "PNAT flow hash",
229 PNAT_FLOW_HASH_BUCKETS, 0);
233 static void pnat_disable(void) {
234 pnat_main_t *pm = &pnat_main;
238 if (pool_elts(pm->translations))
241 /* Delete flow cache table */
242 clib_bihash_free_16_8(&pm->flowhash);
248 * Ensure that a new rule lookup mask matches what's installed on interface
250 static int pnat_interface_check_mask(u32 sw_if_index,
251 pnat_attachment_point_t attachment,
253 pnat_interface_t *interface = pnat_interface_by_sw_if_index(sw_if_index);
256 if (!interface->enabled[attachment])
258 if (interface->lookup_mask[attachment] != mask)
265 * Add a binding to the binding table.
266 * Returns 0 on success.
268 * -2: Only matches ports for UDP or TCP
271 int pnat_binding_add(pnat_match_tuple_t *match, pnat_rewrite_tuple_t *rewrite,
273 pnat_main_t *pm = &pnat_main;
277 /* If we aren't matching or rewriting, why are we here? */
278 if (match->mask == 0 || rewrite->mask == 0)
281 /* Check if protocol is set if ports are set */
282 if ((match->dport || match->sport) &&
283 (match->proto != IP_API_PROTO_UDP && match->proto != IP_API_PROTO_TCP))
286 /* Create pool entry */
287 pnat_translation_t *t;
288 pool_get_zero(pm->translations, t);
289 memcpy(&t->post_da, &rewrite->dst, 4);
290 memcpy(&t->post_sa, &rewrite->src, 4);
291 t->post_sp = rewrite->sport;
292 t->post_dp = rewrite->dport;
293 t->from_offset = rewrite->from_offset;
294 t->to_offset = rewrite->to_offset;
295 t->clear_offset = rewrite->clear_offset;
296 t->instructions = pnat_instructions_from_mask(rewrite->mask);
298 /* These are only used for show commands and trace */
300 t->rewrite = *rewrite;
302 *index = t - pm->translations;
308 * Looks a match flow in the flow cache, returns the index in the binding table
310 u32 pnat_flow_lookup(u32 sw_if_index, pnat_attachment_point_t attachment,
311 pnat_match_tuple_t *match) {
312 pnat_main_t *pm = &pnat_main;
313 clib_bihash_kv_16_8_t kv, value;
314 pnat_calc_key_from_5tuple(sw_if_index, attachment, match, &kv);
315 if (clib_bihash_search_16_8(&pm->flowhash, &kv, &value) == 0) {
322 * Attach a binding to an interface / direction.
323 * Returns 0 on success.
324 * -1: Binding does not exist
325 * -2: Interface mask does not match
326 * -3: Existing match entry in flow table
327 * -4: Adding flow table entry failed
329 int pnat_binding_attach(u32 sw_if_index, pnat_attachment_point_t attachment,
331 pnat_main_t *pm = &pnat_main;
333 if (!pm->translations ||
334 pool_is_free_index(pm->translations, binding_index))
337 pnat_translation_t *t = pool_elt_at_index(pm->translations, binding_index);
339 if (pnat_interface_check_mask(sw_if_index, attachment, t->match.mask) != 0)
344 /* Verify non-duplicate */
345 clib_bihash_kv_16_8_t kv, value;
346 pnat_calc_key_from_5tuple(sw_if_index, attachment, &t->match, &kv);
347 if (clib_bihash_search_16_8(&pm->flowhash, &kv, &value) == 0) {
351 /* Create flow cache */
352 kv.value = binding_index;
353 if (clib_bihash_add_del_16_8(&pm->flowhash, &kv, 1)) {
354 pool_put(pm->translations, t);
358 /* Register interface */
359 pnat_enable_interface(sw_if_index, attachment, t->match.mask);
364 int pnat_binding_detach(u32 sw_if_index, pnat_attachment_point_t attachment,
366 pnat_main_t *pm = &pnat_main;
368 if (!pm->translations ||
369 pool_is_free_index(pm->translations, binding_index))
372 pnat_translation_t *t = pool_elt_at_index(pm->translations, binding_index);
374 /* Verify non-duplicate */
375 clib_bihash_kv_16_8_t kv;
376 pnat_calc_key_from_5tuple(sw_if_index, attachment, &t->match, &kv);
377 if (clib_bihash_add_del_16_8(&pm->flowhash, &kv, 0)) {
381 /* Deregister interface */
382 pnat_disable_interface(sw_if_index, attachment);
390 * Delete a translation using the index returned from pnat_add_translation.
392 int pnat_binding_del(u32 index) {
393 pnat_main_t *pm = &pnat_main;
395 if (pool_is_free_index(pm->translations, index)) {
396 clib_warning("Binding delete: translation does not exist: %d", index);
400 pnat_translation_t *t = pool_elt_at_index(pm->translations, index);
401 pool_put(pm->translations, t);