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:
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.
16 #include <vnet/ip/ip.h>
17 #include <cnat/cnat_snat.h>
18 #include <cnat/cnat_translation.h>
21 cnat_compute_prefix_lengths_in_search_order (cnat_snat_pfx_table_t *
22 table, ip_address_family_t af)
25 vec_reset_length (table->meta[af].prefix_lengths_in_search_order);
26 /* Note: bitmap reversed so this is in fact a longest prefix match */
28 clib_bitmap_foreach (i, table->meta[af].non_empty_dst_address_length_bitmap)
30 int dst_address_length = 128 - i;
31 vec_add1 (table->meta[af].prefix_lengths_in_search_order, dst_address_length);
37 cnat_add_snat_prefix (ip_prefix_t * pfx)
39 /* All packets destined to this prefix won't be source-NAT-ed */
40 cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
41 clib_bihash_kv_24_8_t kv;
43 u64 af = ip_prefix_version (pfx);;
45 mask = &table->ip_masks[pfx->len];
48 kv.key[0] = (u64) ip_prefix_v4 (pfx).as_u32 & mask->as_u64[0];
53 kv.key[0] = ip_prefix_v6 (pfx).as_u64[0] & mask->as_u64[0];
54 kv.key[1] = ip_prefix_v6 (pfx).as_u64[1] & mask->as_u64[1];
56 kv.key[2] = ((u64) af << 32) | pfx->len;
57 clib_bihash_add_del_24_8 (&table->ip_hash, &kv, 1 /* is_add */ );
59 table->meta[af].dst_address_length_refcounts[pfx->len]++;
60 table->meta[af].non_empty_dst_address_length_bitmap =
61 clib_bitmap_set (table->meta[af].non_empty_dst_address_length_bitmap,
63 cnat_compute_prefix_lengths_in_search_order (table, af);
68 cnat_del_snat_prefix (ip_prefix_t * pfx)
70 cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
71 clib_bihash_kv_24_8_t kv, val;
73 u64 af = ip_prefix_version (pfx);;
75 mask = &table->ip_masks[pfx->len];
78 kv.key[0] = (u64) ip_prefix_v4 (pfx).as_u32 & mask->as_u64[0];
83 kv.key[0] = ip_prefix_v6 (pfx).as_u64[0] & mask->as_u64[0];
84 kv.key[1] = ip_prefix_v6 (pfx).as_u64[1] & mask->as_u64[1];
86 kv.key[2] = ((u64) af << 32) | pfx->len;
88 if (clib_bihash_search_24_8 (&table->ip_hash, &kv, &val))
92 clib_bihash_add_del_24_8 (&table->ip_hash, &kv, 0 /* is_add */ );
93 /* refcount accounting */
94 ASSERT (table->meta[af].dst_address_length_refcounts[pfx->len] > 0);
95 if (--table->meta[af].dst_address_length_refcounts[pfx->len] == 0)
97 table->meta[af].non_empty_dst_address_length_bitmap =
98 clib_bitmap_set (table->meta[af].non_empty_dst_address_length_bitmap,
100 cnat_compute_prefix_lengths_in_search_order (table, af);
106 cnat_search_snat_prefix (ip46_address_t * addr, ip_address_family_t af)
108 /* Returns 0 if addr matches any of the listed prefixes */
109 cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
110 clib_bihash_kv_24_8_t kv, val;
112 n_p = vec_len (table->meta[af].prefix_lengths_in_search_order);
115 kv.key[0] = addr->ip4.as_u32;
120 kv.key[0] = addr->as_u64[0];
121 kv.key[1] = addr->as_u64[1];
125 * start search from a mask length same length or shorter.
126 * we don't want matches longer than the mask passed
131 int dst_address_length =
132 table->meta[af].prefix_lengths_in_search_order[i];
133 ip6_address_t *mask = &table->ip_masks[dst_address_length];
135 ASSERT (dst_address_length >= 0 && dst_address_length <= 128);
136 /* As lengths are decreasing, masks are increasingly specific. */
137 kv.key[0] &= mask->as_u64[0];
138 kv.key[1] &= mask->as_u64[1];
139 kv.key[2] = ((u64) af << 32) | dst_address_length;
140 rv = clib_bihash_search_inline_2_24_8 (&table->ip_hash, &kv, &val);
148 format_cnat_snat_prefix (u8 * s, va_list * args)
150 clib_bihash_kv_24_8_t *kv = va_arg (*args, clib_bihash_kv_24_8_t *);
151 CLIB_UNUSED (int verbose) = va_arg (*args, int);
152 u32 af = kv->key[2] >> 32;
153 u32 len = kv->key[2] & 0xffffffff;
155 s = format (s, "%U/%d", format_ip4_address, &kv->key[0], len);
157 s = format (s, "%U/%d", format_ip6_address, &kv->key[0], len);
162 cnat_set_snat (ip4_address_t * ip4, ip6_address_t * ip6, u32 sw_if_index)
166 cnat_translation_unwatch_addr (INDEX_INVALID, CNAT_RESOLV_ADDR_SNAT);
168 ip_address_set (&cnat_main.snat_ip4.ce_ip, ip4, AF_IP4);
169 ip_address_set (&cnat_main.snat_ip6.ce_ip, ip6, AF_IP6);
170 cnat_main.snat_ip4.ce_sw_if_index = sw_if_index;
171 cnat_main.snat_ip6.ce_sw_if_index = sw_if_index;
173 cnat_resolve_ep (&cnat_main.snat_ip4);
174 cnat_resolve_ep (&cnat_main.snat_ip6);
175 cnat_translation_watch_addr (INDEX_INVALID, 0, &cnat_main.snat_ip4,
176 CNAT_RESOLV_ADDR_SNAT);
177 cnat_translation_watch_addr (INDEX_INVALID, 0, &cnat_main.snat_ip6,
178 CNAT_RESOLV_ADDR_SNAT);
181 static clib_error_t *
182 cnat_set_snat_cli (vlib_main_t * vm,
183 unformat_input_t * input, vlib_cli_command_t * cmd)
185 unformat_input_t _line_input, *line_input = &_line_input;
186 vnet_main_t *vnm = vnet_get_main ();
187 ip4_address_t ip4 = { {0} };
188 ip6_address_t ip6 = { {0} };
190 u32 sw_if_index = INDEX_INVALID;
194 /* Get a line of input. */
195 if (!unformat_user (input, unformat_line_input, line_input))
198 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
200 if (unformat_user (line_input, unformat_ip4_address, &ip4))
202 else if (unformat_user (line_input, unformat_ip6_address, &ip6))
204 else if (unformat_user (line_input, unformat_vnet_sw_interface,
209 e = clib_error_return (0, "unknown input '%U'",
210 format_unformat_error, input);
215 cnat_set_snat (&ip4, &ip6, sw_if_index);
218 unformat_free (line_input);
224 VLIB_CLI_COMMAND (cnat_set_snat_command, static) =
226 .path = "cnat snat with",
227 .short_help = "cnat snat with [<ip4-address>][<ip6-address>][sw_if_index]",
228 .function = cnat_set_snat_cli,
232 static clib_error_t *
233 cnat_snat_exclude (vlib_main_t * vm,
234 unformat_input_t * input, vlib_cli_command_t * cmd)
240 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
242 if (unformat (input, "%U", unformat_ip_prefix, &pfx))
244 else if (unformat (input, "del"))
247 return (clib_error_return (0, "unknown input '%U'",
248 format_unformat_error, input));
252 rv = cnat_add_snat_prefix (&pfx);
254 rv = cnat_del_snat_prefix (&pfx);
258 return (clib_error_return (0, "error %d", rv, input));
265 VLIB_CLI_COMMAND (cnat_snat_exclude_command, static) =
267 .path = "cnat snat exclude",
268 .short_help = "cnat snat exclude [ip]",
269 .function = cnat_snat_exclude,
273 static clib_error_t *
274 cnat_show_snat (vlib_main_t * vm,
275 unformat_input_t * input, vlib_cli_command_t * cmd)
277 cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
278 vlib_cli_output (vm, "Source NAT\nip4: %U\nip6: %U\n",
279 format_cnat_endpoint, &cnat_main.snat_ip4,
280 format_cnat_endpoint, &cnat_main.snat_ip6);
281 vlib_cli_output (vm, "Prefixes:\n%U\n",
282 format_bihash_24_8, &table->ip_hash, 1);
287 VLIB_CLI_COMMAND (cnat_show_snat_command, static) =
289 .path = "show cnat snat",
290 .short_help = "show cnat snat",
291 .function = cnat_show_snat,
295 static clib_error_t *
296 cnat_snat_init (vlib_main_t * vm)
298 cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
299 cnat_main_t *cm = &cnat_main;
301 for (i = 0; i < ARRAY_LEN (table->ip_masks); i++)
308 for (j = 0; j < i0; j++)
309 table->ip_masks[i].as_u32[j] = ~0;
312 table->ip_masks[i].as_u32[i0] =
313 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
315 clib_bihash_init_24_8 (&table->ip_hash, "snat prefixes",
316 cm->snat_hash_buckets, cm->snat_hash_memory);
317 clib_bihash_set_kvp_format_fn_24_8 (&table->ip_hash,
318 format_cnat_snat_prefix);
323 VLIB_INIT_FUNCTION (cnat_snat_init);
327 * fd.io coding-style-patch-verification: ON
330 * eval: (c-set-style "gnu")