cnat: Fix snat with dhcp
[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 #include <cnat/cnat_translation.h>
19
20 cnat_snat_policy_main_t cnat_snat_policy_main;
21
22 void
23 cnat_set_snat_policy (cnat_snat_policy_t fp)
24 {
25   cnat_snat_policy_main.snat_policy = fp;
26 }
27
28 static clib_error_t *
29 cnat_snat_policy_cmd (vlib_main_t *vm, unformat_input_t *input,
30                       vlib_cli_command_t *cmd)
31 {
32   cnat_snat_policy_t fp = NULL;
33
34   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
35     {
36       if (unformat (input, "none"))
37         ;
38       else
39         return clib_error_return (0, "unknown input '%U'",
40                                   format_unformat_error, input);
41     }
42
43   cnat_set_snat_policy (fp);
44   return NULL;
45 }
46
47 VLIB_CLI_COMMAND (cnat_snat_policy_command, static) = {
48   .path = "cnat set snat policy",
49   .short_help = "cnat set snat policy {none,k8s}",
50   .function = cnat_snat_policy_cmd,
51 };
52
53 static clib_error_t *
54 show_cnat_snat_policy_cmd (vlib_main_t *vm, unformat_input_t *input,
55                            vlib_cli_command_t *cmd)
56 {
57   u8 *s = format (NULL, "snat policy: ");
58   if (cnat_snat_policy_main.snat_policy == NULL)
59     s = format (s, "none");
60   else
61     s = format (s, "unknown (%x)", cnat_snat_policy_main.snat_policy);
62
63   vlib_cli_output (vm, (char *) s);
64   return NULL;
65 }
66
67 VLIB_CLI_COMMAND (show_cnat_snat_policy_command, static) = {
68   .path = "show cnat snat policy",
69   .short_help = "show cnat snat policy",
70   .function = show_cnat_snat_policy_cmd,
71 };
72
73 static void
74 cnat_compute_prefix_lengths_in_search_order (cnat_snat_pfx_table_t *
75                                              table, ip_address_family_t af)
76 {
77   int i;
78   vec_reset_length (table->meta[af].prefix_lengths_in_search_order);
79   /* Note: bitmap reversed so this is in fact a longest prefix match */
80   clib_bitmap_foreach (i, table->meta[af].non_empty_dst_address_length_bitmap)
81      {
82       int dst_address_length = 128 - i;
83       vec_add1 (table->meta[af].prefix_lengths_in_search_order, dst_address_length);
84     }
85 }
86
87 int
88 cnat_add_snat_prefix (ip_prefix_t * pfx)
89 {
90   /* All packets destined to this prefix won't be source-NAT-ed */
91   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
92   clib_bihash_kv_24_8_t kv;
93   ip6_address_t *mask;
94   u64 af = ip_prefix_version (pfx);;
95
96   mask = &table->ip_masks[pfx->len];
97   if (AF_IP4 == af)
98     {
99       kv.key[0] = (u64) ip_prefix_v4 (pfx).as_u32 & mask->as_u64[0];
100       kv.key[1] = 0;
101     }
102   else
103     {
104       kv.key[0] = ip_prefix_v6 (pfx).as_u64[0] & mask->as_u64[0];
105       kv.key[1] = ip_prefix_v6 (pfx).as_u64[1] & mask->as_u64[1];
106     }
107   kv.key[2] = ((u64) af << 32) | pfx->len;
108   clib_bihash_add_del_24_8 (&table->ip_hash, &kv, 1 /* is_add */ );
109
110   table->meta[af].dst_address_length_refcounts[pfx->len]++;
111   table->meta[af].non_empty_dst_address_length_bitmap =
112     clib_bitmap_set (table->meta[af].non_empty_dst_address_length_bitmap,
113                      128 - pfx->len, 1);
114   cnat_compute_prefix_lengths_in_search_order (table, af);
115   return 0;
116 }
117
118 int
119 cnat_del_snat_prefix (ip_prefix_t * pfx)
120 {
121   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
122   clib_bihash_kv_24_8_t kv, val;
123   ip6_address_t *mask;
124   u64 af = ip_prefix_version (pfx);;
125
126   mask = &table->ip_masks[pfx->len];
127   if (AF_IP4 == af)
128     {
129       kv.key[0] = (u64) ip_prefix_v4 (pfx).as_u32 & mask->as_u64[0];
130       kv.key[1] = 0;
131     }
132   else
133     {
134       kv.key[0] = ip_prefix_v6 (pfx).as_u64[0] & mask->as_u64[0];
135       kv.key[1] = ip_prefix_v6 (pfx).as_u64[1] & mask->as_u64[1];
136     }
137   kv.key[2] = ((u64) af << 32) | pfx->len;
138
139   if (clib_bihash_search_24_8 (&table->ip_hash, &kv, &val))
140     {
141       return 1;
142     }
143   clib_bihash_add_del_24_8 (&table->ip_hash, &kv, 0 /* is_add */ );
144   /* refcount accounting */
145   ASSERT (table->meta[af].dst_address_length_refcounts[pfx->len] > 0);
146   if (--table->meta[af].dst_address_length_refcounts[pfx->len] == 0)
147     {
148       table->meta[af].non_empty_dst_address_length_bitmap =
149         clib_bitmap_set (table->meta[af].non_empty_dst_address_length_bitmap,
150                          128 - pfx->len, 0);
151       cnat_compute_prefix_lengths_in_search_order (table, af);
152     }
153   return 0;
154 }
155
156 int
157 cnat_search_snat_prefix (ip46_address_t * addr, ip_address_family_t af)
158 {
159   /* Returns 0 if addr matches any of the listed prefixes */
160   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
161   clib_bihash_kv_24_8_t kv, val;
162   int i, n_p, rv;
163   n_p = vec_len (table->meta[af].prefix_lengths_in_search_order);
164   if (AF_IP4 == af)
165     {
166       kv.key[0] = addr->ip4.as_u32;
167       kv.key[1] = 0;
168     }
169   else
170     {
171       kv.key[0] = addr->as_u64[0];
172       kv.key[1] = addr->as_u64[1];
173     }
174
175   /*
176    * start search from a mask length same length or shorter.
177    * we don't want matches longer than the mask passed
178    */
179   i = 0;
180   for (; i < n_p; i++)
181     {
182       int dst_address_length =
183         table->meta[af].prefix_lengths_in_search_order[i];
184       ip6_address_t *mask = &table->ip_masks[dst_address_length];
185
186       ASSERT (dst_address_length >= 0 && dst_address_length <= 128);
187       /* As lengths are decreasing, masks are increasingly specific. */
188       kv.key[0] &= mask->as_u64[0];
189       kv.key[1] &= mask->as_u64[1];
190       kv.key[2] = ((u64) af << 32) | dst_address_length;
191       rv = clib_bihash_search_inline_2_24_8 (&table->ip_hash, &kv, &val);
192       if (rv == 0)
193         return 0;
194     }
195   return -1;
196 }
197
198 u8 *
199 format_cnat_snat_prefix (u8 * s, va_list * args)
200 {
201   clib_bihash_kv_24_8_t *kv = va_arg (*args, clib_bihash_kv_24_8_t *);
202   CLIB_UNUSED (int verbose) = va_arg (*args, int);
203   u32 af = kv->key[2] >> 32;
204   u32 len = kv->key[2] & 0xffffffff;
205   if (AF_IP4 == af)
206     s = format (s, "%U/%d", format_ip4_address, &kv->key[0], len);
207   else
208     s = format (s, "%U/%d", format_ip6_address, &kv->key[0], len);
209   return (s);
210 }
211
212 void
213 cnat_set_snat (ip4_address_t * ip4, ip6_address_t * ip6, u32 sw_if_index)
214 {
215   cnat_lazy_init ();
216
217   cnat_translation_unwatch_addr (INDEX_INVALID, CNAT_RESOLV_ADDR_SNAT);
218
219   ip_address_set (&cnat_main.snat_ip4.ce_ip, ip4, AF_IP4);
220   ip_address_set (&cnat_main.snat_ip6.ce_ip, ip6, AF_IP6);
221   cnat_main.snat_ip4.ce_sw_if_index = sw_if_index;
222   cnat_main.snat_ip6.ce_sw_if_index = sw_if_index;
223
224   cnat_resolve_ep (&cnat_main.snat_ip4);
225   cnat_resolve_ep (&cnat_main.snat_ip6);
226   cnat_translation_watch_addr (INDEX_INVALID, 0, &cnat_main.snat_ip4,
227                                CNAT_RESOLV_ADDR_SNAT);
228   cnat_translation_watch_addr (INDEX_INVALID, 0, &cnat_main.snat_ip6,
229                                CNAT_RESOLV_ADDR_SNAT);
230 }
231
232 static clib_error_t *
233 cnat_set_snat_cli (vlib_main_t * vm,
234                    unformat_input_t * input, vlib_cli_command_t * cmd)
235 {
236   unformat_input_t _line_input, *line_input = &_line_input;
237   vnet_main_t *vnm = vnet_get_main ();
238   ip4_address_t ip4 = { {0} };
239   ip6_address_t ip6 = { {0} };
240   clib_error_t *e = 0;
241   u32 sw_if_index = INDEX_INVALID;
242
243   cnat_lazy_init ();
244
245   /* Get a line of input. */
246   if (!unformat_user (input, unformat_line_input, line_input))
247     return 0;
248
249   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
250     {
251       if (unformat_user (line_input, unformat_ip4_address, &ip4))
252         ;
253       else if (unformat_user (line_input, unformat_ip6_address, &ip6))
254         ;
255       else if (unformat_user (line_input, unformat_vnet_sw_interface,
256                               vnm, &sw_if_index))
257         ;
258       else
259         {
260           e = clib_error_return (0, "unknown input '%U'",
261                                  format_unformat_error, input);
262           goto done;
263         }
264     }
265
266   cnat_set_snat (&ip4, &ip6, sw_if_index);
267
268 done:
269   unformat_free (line_input);
270
271   return (e);
272 }
273
274 VLIB_CLI_COMMAND (cnat_set_snat_command, static) =
275 {
276   .path = "cnat snat with",
277   .short_help = "cnat snat with [<ip4-address>][<ip6-address>][sw_if_index]",
278   .function = cnat_set_snat_cli,
279 };
280
281 static clib_error_t *
282 cnat_snat_exclude (vlib_main_t * vm,
283                    unformat_input_t * input, vlib_cli_command_t * cmd)
284 {
285   ip_prefix_t pfx;
286   u8 is_add = 1;
287   int rv;
288
289   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
290     {
291       if (unformat (input, "%U", unformat_ip_prefix, &pfx))
292         ;
293       else if (unformat (input, "del"))
294         is_add = 0;
295       else
296         return (clib_error_return (0, "unknown input '%U'",
297                                    format_unformat_error, input));
298     }
299
300   if (is_add)
301     rv = cnat_add_snat_prefix (&pfx);
302   else
303     rv = cnat_del_snat_prefix (&pfx);
304
305   if (rv)
306     {
307       return (clib_error_return (0, "error %d", rv, input));
308     }
309
310   return (NULL);
311 }
312
313 VLIB_CLI_COMMAND (cnat_snat_exclude_command, static) =
314 {
315   .path = "cnat snat exclude",
316   .short_help = "cnat snat exclude [ip]",
317   .function = cnat_snat_exclude,
318 };
319
320 static clib_error_t *
321 cnat_show_snat (vlib_main_t * vm,
322                 unformat_input_t * input, vlib_cli_command_t * cmd)
323 {
324   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
325   vlib_cli_output (vm, "Source NAT\nip4: %U\nip6: %U\n",
326                    format_cnat_endpoint, &cnat_main.snat_ip4,
327                    format_cnat_endpoint, &cnat_main.snat_ip6);
328   vlib_cli_output (vm, "Prefixes:\n%U\n",
329                    format_bihash_24_8, &table->ip_hash, 1);
330   return (NULL);
331 }
332
333 VLIB_CLI_COMMAND (cnat_show_snat_command, static) =
334 {
335   .path = "show cnat snat",
336   .short_help = "show cnat snat",
337   .function = cnat_show_snat,
338 };
339
340 static clib_error_t *
341 cnat_snat_init (vlib_main_t * vm)
342 {
343   cnat_main_t *cm = &cnat_main;
344   cnat_snat_pfx_table_t *table = &cm->snat_pfx_table;
345   int i;
346   for (i = 0; i < ARRAY_LEN (table->ip_masks); i++)
347     {
348       u32 j, i0, i1;
349
350       i0 = i / 32;
351       i1 = i % 32;
352
353       for (j = 0; j < i0; j++)
354         table->ip_masks[i].as_u32[j] = ~0;
355
356       if (i1)
357         table->ip_masks[i].as_u32[i0] =
358           clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
359     }
360   clib_bihash_init_24_8 (&table->ip_hash, "snat prefixes",
361                          cm->snat_hash_buckets, cm->snat_hash_memory);
362   clib_bihash_set_kvp_format_fn_24_8 (&table->ip_hash,
363                                       format_cnat_snat_prefix);
364
365   return (NULL);
366 }
367
368 VLIB_INIT_FUNCTION (cnat_snat_init);
369
370
371 /*
372  * fd.io coding-style-patch-verification: ON
373  *
374  * Local Variables:
375  * eval: (c-set-style "gnu")
376  * End:
377  */