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 */
27 clib_bitmap_foreach (i, table->meta[af].non_empty_dst_address_length_bitmap)
29 int dst_address_length = 128 - i;
30 vec_add1 (table->meta[af].prefix_lengths_in_search_order, dst_address_length);
35 cnat_add_snat_prefix (ip_prefix_t * pfx)
37 /* All packets destined to this prefix won't be source-NAT-ed */
38 cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
39 clib_bihash_kv_24_8_t kv;
41 u64 af = ip_prefix_version (pfx);;
43 mask = &table->ip_masks[pfx->len];
46 kv.key[0] = (u64) ip_prefix_v4 (pfx).as_u32 & mask->as_u64[0];
51 kv.key[0] = ip_prefix_v6 (pfx).as_u64[0] & mask->as_u64[0];
52 kv.key[1] = ip_prefix_v6 (pfx).as_u64[1] & mask->as_u64[1];
54 kv.key[2] = ((u64) af << 32) | pfx->len;
55 clib_bihash_add_del_24_8 (&table->ip_hash, &kv, 1 /* is_add */ );
57 table->meta[af].dst_address_length_refcounts[pfx->len]++;
58 table->meta[af].non_empty_dst_address_length_bitmap =
59 clib_bitmap_set (table->meta[af].non_empty_dst_address_length_bitmap,
61 cnat_compute_prefix_lengths_in_search_order (table, af);
66 cnat_del_snat_prefix (ip_prefix_t * pfx)
68 cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
69 clib_bihash_kv_24_8_t kv, val;
71 u64 af = ip_prefix_version (pfx);;
73 mask = &table->ip_masks[pfx->len];
76 kv.key[0] = (u64) ip_prefix_v4 (pfx).as_u32 & mask->as_u64[0];
81 kv.key[0] = ip_prefix_v6 (pfx).as_u64[0] & mask->as_u64[0];
82 kv.key[1] = ip_prefix_v6 (pfx).as_u64[1] & mask->as_u64[1];
84 kv.key[2] = ((u64) af << 32) | pfx->len;
86 if (clib_bihash_search_24_8 (&table->ip_hash, &kv, &val))
90 clib_bihash_add_del_24_8 (&table->ip_hash, &kv, 0 /* is_add */ );
91 /* refcount accounting */
92 ASSERT (table->meta[af].dst_address_length_refcounts[pfx->len] > 0);
93 if (--table->meta[af].dst_address_length_refcounts[pfx->len] == 0)
95 table->meta[af].non_empty_dst_address_length_bitmap =
96 clib_bitmap_set (table->meta[af].non_empty_dst_address_length_bitmap,
98 cnat_compute_prefix_lengths_in_search_order (table, af);
104 cnat_search_snat_prefix (ip46_address_t * addr, ip_address_family_t af)
106 /* Returns 0 if addr matches any of the listed prefixes */
107 cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
108 clib_bihash_kv_24_8_t kv, val;
110 n_p = vec_len (table->meta[af].prefix_lengths_in_search_order);
113 kv.key[0] = addr->ip4.as_u32;
118 kv.key[0] = addr->as_u64[0];
119 kv.key[1] = addr->as_u64[1];
123 * start search from a mask length same length or shorter.
124 * we don't want matches longer than the mask passed
129 int dst_address_length =
130 table->meta[af].prefix_lengths_in_search_order[i];
131 ip6_address_t *mask = &table->ip_masks[dst_address_length];
133 ASSERT (dst_address_length >= 0 && dst_address_length <= 128);
134 /* As lengths are decreasing, masks are increasingly specific. */
135 kv.key[0] &= mask->as_u64[0];
136 kv.key[1] &= mask->as_u64[1];
137 kv.key[2] = ((u64) af << 32) | dst_address_length;
138 rv = clib_bihash_search_inline_2_24_8 (&table->ip_hash, &kv, &val);
146 format_cnat_snat_prefix (u8 * s, va_list * args)
148 clib_bihash_kv_24_8_t *kv = va_arg (*args, clib_bihash_kv_24_8_t *);
149 CLIB_UNUSED (int verbose) = va_arg (*args, int);
150 u32 af = kv->key[2] >> 32;
151 u32 len = kv->key[2] & 0xffffffff;
153 s = format (s, "%U/%d", format_ip4_address, &kv->key[0], len);
155 s = format (s, "%U/%d", format_ip6_address, &kv->key[0], len);
160 cnat_set_snat (ip4_address_t * ip4, ip6_address_t * ip6, u32 sw_if_index)
164 cnat_translation_unwatch_addr (INDEX_INVALID, CNAT_RESOLV_ADDR_SNAT);
166 ip_address_set (&cnat_main.snat_ip4.ce_ip, ip4, AF_IP4);
167 ip_address_set (&cnat_main.snat_ip6.ce_ip, ip6, AF_IP6);
168 cnat_main.snat_ip4.ce_sw_if_index = sw_if_index;
169 cnat_main.snat_ip6.ce_sw_if_index = sw_if_index;
171 cnat_resolve_ep (&cnat_main.snat_ip4);
172 cnat_resolve_ep (&cnat_main.snat_ip6);
173 cnat_translation_watch_addr (INDEX_INVALID, 0, &cnat_main.snat_ip4,
174 CNAT_RESOLV_ADDR_SNAT);
175 cnat_translation_watch_addr (INDEX_INVALID, 0, &cnat_main.snat_ip6,
176 CNAT_RESOLV_ADDR_SNAT);
179 static clib_error_t *
180 cnat_set_snat_cli (vlib_main_t * vm,
181 unformat_input_t * input, vlib_cli_command_t * cmd)
183 unformat_input_t _line_input, *line_input = &_line_input;
184 vnet_main_t *vnm = vnet_get_main ();
185 ip4_address_t ip4 = { {0} };
186 ip6_address_t ip6 = { {0} };
188 u32 sw_if_index = INDEX_INVALID;
192 /* Get a line of input. */
193 if (!unformat_user (input, unformat_line_input, line_input))
196 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
198 if (unformat_user (line_input, unformat_ip4_address, &ip4))
200 else if (unformat_user (line_input, unformat_ip6_address, &ip6))
202 else if (unformat_user (line_input, unformat_vnet_sw_interface,
207 e = clib_error_return (0, "unknown input '%U'",
208 format_unformat_error, input);
213 cnat_set_snat (&ip4, &ip6, sw_if_index);
216 unformat_free (line_input);
221 VLIB_CLI_COMMAND (cnat_set_snat_command, static) =
223 .path = "cnat snat with",
224 .short_help = "cnat snat with [<ip4-address>][<ip6-address>][sw_if_index]",
225 .function = cnat_set_snat_cli,
228 static clib_error_t *
229 cnat_snat_exclude (vlib_main_t * vm,
230 unformat_input_t * input, vlib_cli_command_t * cmd)
236 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
238 if (unformat (input, "%U", unformat_ip_prefix, &pfx))
240 else if (unformat (input, "del"))
243 return (clib_error_return (0, "unknown input '%U'",
244 format_unformat_error, input));
248 rv = cnat_add_snat_prefix (&pfx);
250 rv = cnat_del_snat_prefix (&pfx);
254 return (clib_error_return (0, "error %d", rv, input));
260 VLIB_CLI_COMMAND (cnat_snat_exclude_command, static) =
262 .path = "cnat snat exclude",
263 .short_help = "cnat snat exclude [ip]",
264 .function = cnat_snat_exclude,
267 static clib_error_t *
268 cnat_show_snat (vlib_main_t * vm,
269 unformat_input_t * input, vlib_cli_command_t * cmd)
271 cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
272 vlib_cli_output (vm, "Source NAT\nip4: %U\nip6: %U\n",
273 format_cnat_endpoint, &cnat_main.snat_ip4,
274 format_cnat_endpoint, &cnat_main.snat_ip6);
275 vlib_cli_output (vm, "Prefixes:\n%U\n",
276 format_bihash_24_8, &table->ip_hash, 1);
280 VLIB_CLI_COMMAND (cnat_show_snat_command, static) =
282 .path = "show cnat snat",
283 .short_help = "show cnat snat",
284 .function = cnat_show_snat,
287 static clib_error_t *
288 cnat_snat_init (vlib_main_t * vm)
290 cnat_main_t *cm = &cnat_main;
291 cnat_snat_pfx_table_t *table = &cm->snat_pfx_table;
293 for (i = 0; i < ARRAY_LEN (table->ip_masks); i++)
300 for (j = 0; j < i0; j++)
301 table->ip_masks[i].as_u32[j] = ~0;
304 table->ip_masks[i].as_u32[i0] =
305 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
307 clib_bihash_init_24_8 (&table->ip_hash, "snat prefixes",
308 cm->snat_hash_buckets, cm->snat_hash_memory);
309 clib_bihash_set_kvp_format_fn_24_8 (&table->ip_hash,
310 format_cnat_snat_prefix);
315 VLIB_INIT_FUNCTION (cnat_snat_init);
319 * fd.io coding-style-patch-verification: ON
322 * eval: (c-set-style "gnu")