21ac8257a231fc151b0a3516ed686db185ba2789
[vpp.git] / src / plugins / cnat / cnat_snat.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vnet/ip/ip.h>
17 #include <cnat/cnat_snat.h>
18
19 static void
20 cnat_compute_prefix_lengths_in_search_order (cnat_snat_pfx_table_t *
21                                              table, ip_address_family_t af)
22 {
23   int i;
24   vec_reset_length (table->meta[af].prefix_lengths_in_search_order);
25   /* Note: bitmap reversed so this is in fact a longest prefix match */
26   /* *INDENT-OFF* */
27   clib_bitmap_foreach (i, table->meta[af].non_empty_dst_address_length_bitmap,
28     ({
29       int dst_address_length = 128 - i;
30       vec_add1 (table->meta[af].prefix_lengths_in_search_order, dst_address_length);
31     }));
32   /* *INDENT-ON* */
33 }
34
35 int
36 cnat_add_snat_prefix (ip_prefix_t * pfx)
37 {
38   /* All packets destined to this prefix won't be source-NAT-ed */
39   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
40   clib_bihash_kv_24_8_t kv;
41   ip6_address_t *mask;
42   u64 af = ip_prefix_version (pfx);;
43
44   mask = &table->ip_masks[pfx->len];
45   if (AF_IP4 == af)
46     {
47       kv.key[0] = (u64) ip_prefix_v4 (pfx).as_u32 & mask->as_u64[0];
48       kv.key[1] = 0;
49     }
50   else
51     {
52       kv.key[0] = ip_prefix_v6 (pfx).as_u64[0] & mask->as_u64[0];
53       kv.key[1] = ip_prefix_v6 (pfx).as_u64[1] & mask->as_u64[1];
54     }
55   kv.key[2] = ((u64) af << 32) | pfx->len;
56   clib_bihash_add_del_24_8 (&table->ip_hash, &kv, 1 /* is_add */ );
57
58   table->meta[af].dst_address_length_refcounts[pfx->len]++;
59   table->meta[af].non_empty_dst_address_length_bitmap =
60     clib_bitmap_set (table->meta[af].non_empty_dst_address_length_bitmap,
61                      128 - pfx->len, 1);
62   cnat_compute_prefix_lengths_in_search_order (table, af);
63   return 0;
64 }
65
66 int
67 cnat_del_snat_prefix (ip_prefix_t * pfx)
68 {
69   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
70   clib_bihash_kv_24_8_t kv, val;
71   ip6_address_t *mask;
72   u64 af = ip_prefix_version (pfx);;
73
74   mask = &table->ip_masks[pfx->len];
75   if (AF_IP4 == af)
76     {
77       kv.key[0] = (u64) ip_prefix_v4 (pfx).as_u32 & mask->as_u64[0];
78       kv.key[1] = 0;
79     }
80   else
81     {
82       kv.key[0] = ip_prefix_v6 (pfx).as_u64[0] & mask->as_u64[0];
83       kv.key[1] = ip_prefix_v6 (pfx).as_u64[1] & mask->as_u64[1];
84     }
85   kv.key[2] = ((u64) af << 32) | pfx->len;
86
87   if (clib_bihash_search_24_8 (&table->ip_hash, &kv, &val))
88     {
89       return 1;
90     }
91   clib_bihash_add_del_24_8 (&table->ip_hash, &kv, 0 /* is_add */ );
92   /* refcount accounting */
93   ASSERT (table->meta[af].dst_address_length_refcounts[pfx->len] > 0);
94   if (--table->meta[af].dst_address_length_refcounts[pfx->len] == 0)
95     {
96       table->meta[af].non_empty_dst_address_length_bitmap =
97         clib_bitmap_set (table->meta[af].non_empty_dst_address_length_bitmap,
98                          128 - pfx->len, 0);
99       cnat_compute_prefix_lengths_in_search_order (table, af);
100     }
101   return 0;
102 }
103
104 int
105 cnat_search_snat_prefix (ip46_address_t * addr, ip_address_family_t af)
106 {
107   /* Returns 0 if addr matches any of the listed prefixes */
108   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
109   clib_bihash_kv_24_8_t kv, val;
110   int i, n_p, rv;
111   n_p = vec_len (table->meta[af].prefix_lengths_in_search_order);
112   if (AF_IP4 == af)
113     {
114       kv.key[0] = addr->ip4.as_u32;
115       kv.key[1] = 0;
116     }
117   else
118     {
119       kv.key[0] = addr->as_u64[0];
120       kv.key[1] = addr->as_u64[1];
121     }
122
123   /*
124    * start search from a mask length same length or shorter.
125    * we don't want matches longer than the mask passed
126    */
127   i = 0;
128   for (; i < n_p; i++)
129     {
130       int dst_address_length =
131         table->meta[af].prefix_lengths_in_search_order[i];
132       ip6_address_t *mask = &table->ip_masks[dst_address_length];
133
134       ASSERT (dst_address_length >= 0 && dst_address_length <= 128);
135       /* As lengths are decreasing, masks are increasingly specific. */
136       kv.key[0] &= mask->as_u64[0];
137       kv.key[1] &= mask->as_u64[1];
138       kv.key[2] = ((u64) af << 32) | dst_address_length;
139       rv = clib_bihash_search_inline_2_24_8 (&table->ip_hash, &kv, &val);
140       if (rv == 0)
141         return 0;
142     }
143   return -1;
144 }
145
146 u8 *
147 format_cnat_snat_prefix (u8 * s, va_list * args)
148 {
149   clib_bihash_kv_24_8_t *kv = va_arg (*args, clib_bihash_kv_24_8_t *);
150   CLIB_UNUSED (int verbose) = va_arg (*args, int);
151   u32 af = kv->key[2] >> 32;
152   u32 len = kv->key[2] & 0xffffffff;
153   if (AF_IP4 == af)
154     s = format (s, "%U/%d", format_ip4_address, &kv->key[0], len);
155   else
156     s = format (s, "%U/%d", format_ip6_address, &kv->key[0], len);
157   return (s);
158 }
159
160 static clib_error_t *
161 cnat_set_snat (vlib_main_t * vm,
162                unformat_input_t * input, vlib_cli_command_t * cmd)
163 {
164   unformat_input_t _line_input, *line_input = &_line_input;
165   clib_error_t *e = 0;
166   ip_address_t addr;
167
168   cnat_lazy_init ();
169
170   /* Get a line of input. */
171   if (!unformat_user (input, unformat_line_input, line_input))
172     return 0;
173
174   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
175     {
176       if (unformat (line_input, "%U", unformat_ip_address, &addr))
177         {
178           if (ip_addr_version (&addr) == AF_IP4)
179             clib_memcpy (&cnat_main.snat_ip4, &ip_addr_v4 (&addr),
180                          sizeof (ip4_address_t));
181           else
182             clib_memcpy (&cnat_main.snat_ip6, &ip_addr_v6 (&addr),
183                          sizeof (ip6_address_t));
184         }
185       else
186         {
187           e = clib_error_return (0, "unknown input '%U'",
188                                  format_unformat_error, input);
189           goto done;
190         }
191     }
192
193 done:
194   unformat_free (line_input);
195
196   return (e);
197 }
198
199 /* *INDENT-OFF* */
200 VLIB_CLI_COMMAND (cnat_set_snat_command, static) =
201 {
202   .path = "cnat snat with",
203   .short_help = "cnat snat with [<ip4-address>][<ip6-address>]",
204   .function = cnat_set_snat,
205 };
206 /* *INDENT-ON* */
207
208 static clib_error_t *
209 cnat_snat_exclude (vlib_main_t * vm,
210                    unformat_input_t * input, vlib_cli_command_t * cmd)
211 {
212   ip_prefix_t pfx;
213   u8 is_add = 1;
214   int rv;
215
216   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
217     {
218       if (unformat (input, "%U", unformat_ip_prefix, &pfx))
219         ;
220       else if (unformat (input, "del"))
221         is_add = 0;
222       else
223         return (clib_error_return (0, "unknown input '%U'",
224                                    format_unformat_error, input));
225     }
226
227   if (is_add)
228     rv = cnat_add_snat_prefix (&pfx);
229   else
230     rv = cnat_del_snat_prefix (&pfx);
231
232   if (rv)
233     {
234       return (clib_error_return (0, "error %d", rv, input));
235     }
236
237   return (NULL);
238 }
239
240 /* *INDENT-OFF* */
241 VLIB_CLI_COMMAND (cnat_snat_exclude_command, static) =
242 {
243   .path = "cnat snat exclude",
244   .short_help = "cnat snat exclude [ip]",
245   .function = cnat_snat_exclude,
246 };
247 /* *INDENT-ON* */
248
249 static clib_error_t *
250 cnat_show_snat (vlib_main_t * vm,
251                 unformat_input_t * input, vlib_cli_command_t * cmd)
252 {
253   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
254   vlib_cli_output (vm, "Source NAT\nip4: %U\nip6: %U\n",
255                    format_ip4_address, &cnat_main.snat_ip4,
256                    format_ip6_address, &cnat_main.snat_ip6);
257   vlib_cli_output (vm, "Prefixes:\n%U\n",
258                    format_bihash_24_8, &table->ip_hash, 1);
259   return (NULL);
260 }
261
262 /* *INDENT-OFF* */
263 VLIB_CLI_COMMAND (cnat_show_snat_command, static) =
264 {
265   .path = "show cnat snat",
266   .short_help = "show cnat snat",
267   .function = cnat_show_snat,
268 };
269 /* *INDENT-ON* */
270
271 static clib_error_t *
272 cnat_snat_init (vlib_main_t * vm)
273 {
274   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
275   cnat_main_t *cm = &cnat_main;
276   int i;
277   for (i = 0; i < ARRAY_LEN (table->ip_masks); i++)
278     {
279       u32 j, i0, i1;
280
281       i0 = i / 32;
282       i1 = i % 32;
283
284       for (j = 0; j < i0; j++)
285         table->ip_masks[i].as_u32[j] = ~0;
286
287       if (i1)
288         table->ip_masks[i].as_u32[i0] =
289           clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
290     }
291   clib_bihash_init_24_8 (&table->ip_hash, "snat prefixes",
292                          cm->snat_hash_buckets, cm->snat_hash_memory);
293   clib_bihash_set_kvp_format_fn_24_8 (&table->ip_hash,
294                                       format_cnat_snat_prefix);
295
296   return (NULL);
297 }
298
299 VLIB_INIT_FUNCTION (cnat_snat_init);
300
301
302 /*
303  * fd.io coding-style-patch-verification: ON
304  *
305  * Local Variables:
306  * eval: (c-set-style "gnu")
307  * End:
308  */