cnat: Disable default scanner process
[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 u8 *
105 format_cnat_snat_prefix (u8 * s, va_list * args)
106 {
107   clib_bihash_kv_24_8_t *kv = va_arg (*args, clib_bihash_kv_24_8_t *);
108   CLIB_UNUSED (int verbose) = va_arg (*args, int);
109   u32 af = kv->key[2] >> 32;
110   u32 len = kv->key[2] & 0xffffffff;
111   if (AF_IP4 == af)
112     s = format (s, "%U/%d", format_ip4_address, &kv->key[0], len);
113   else
114     s = format (s, "%U/%d", format_ip6_address, &kv->key[0], len);
115   return (s);
116 }
117
118 static clib_error_t *
119 cnat_set_snat (vlib_main_t * vm,
120                unformat_input_t * input, vlib_cli_command_t * cmd)
121 {
122   unformat_input_t _line_input, *line_input = &_line_input;
123   clib_error_t *e = 0;
124   ip_address_t addr;
125
126   cnat_lazy_init ();
127
128   /* Get a line of input. */
129   if (!unformat_user (input, unformat_line_input, line_input))
130     return 0;
131
132   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
133     {
134       if (unformat (line_input, "%U", unformat_ip_address, &addr))
135         {
136           if (ip_addr_version (&addr) == AF_IP4)
137             clib_memcpy (&cnat_main.snat_ip4, &ip_addr_v4 (&addr),
138                          sizeof (ip4_address_t));
139           else
140             clib_memcpy (&cnat_main.snat_ip6, &ip_addr_v6 (&addr),
141                          sizeof (ip6_address_t));
142         }
143       else
144         {
145           e = clib_error_return (0, "unknown input '%U'",
146                                  format_unformat_error, input);
147           goto done;
148         }
149     }
150
151 done:
152   unformat_free (line_input);
153
154   return (e);
155 }
156
157 /* *INDENT-OFF* */
158 VLIB_CLI_COMMAND (cnat_set_snat_command, static) =
159 {
160   .path = "cnat snat with",
161   .short_help = "cnat snat with [<ip4-address>][<ip6-address>]",
162   .function = cnat_set_snat,
163 };
164 /* *INDENT-ON* */
165
166 static clib_error_t *
167 cnat_snat_exclude (vlib_main_t * vm,
168                    unformat_input_t * input, vlib_cli_command_t * cmd)
169 {
170   ip_prefix_t pfx;
171   u8 is_add = 1;
172   int rv;
173
174   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
175     {
176       if (unformat (input, "%U", unformat_ip_prefix, &pfx))
177         ;
178       else if (unformat (input, "del"))
179         is_add = 0;
180       else
181         return (clib_error_return (0, "unknown input '%U'",
182                                    format_unformat_error, input));
183     }
184
185   if (is_add)
186     rv = cnat_add_snat_prefix (&pfx);
187   else
188     rv = cnat_del_snat_prefix (&pfx);
189
190   if (rv)
191     {
192       return (clib_error_return (0, "error %d", rv, input));
193     }
194
195   return (NULL);
196 }
197
198 /* *INDENT-OFF* */
199 VLIB_CLI_COMMAND (cnat_snat_exclude_command, static) =
200 {
201   .path = "cnat snat exclude",
202   .short_help = "cnat snat exclude [ip]",
203   .function = cnat_snat_exclude,
204 };
205 /* *INDENT-ON* */
206
207 static clib_error_t *
208 cnat_show_snat (vlib_main_t * vm,
209                 unformat_input_t * input, vlib_cli_command_t * cmd)
210 {
211   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
212   vlib_cli_output (vm, "Source NAT\nip4: %U\nip6: %U\n",
213                    format_ip4_address, &cnat_main.snat_ip4,
214                    format_ip6_address, &cnat_main.snat_ip6);
215   vlib_cli_output (vm, "Prefixes:\n%U\n",
216                    format_bihash_24_8, &table->ip_hash, 1);
217   return (NULL);
218 }
219
220 /* *INDENT-OFF* */
221 VLIB_CLI_COMMAND (cnat_show_snat_command, static) =
222 {
223   .path = "show cnat snat",
224   .short_help = "show cnat snat",
225   .function = cnat_show_snat,
226 };
227 /* *INDENT-ON* */
228
229 static clib_error_t *
230 cnat_snat_init (vlib_main_t * vm)
231 {
232   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
233   cnat_main_t *cm = &cnat_main;
234   int i;
235   for (i = 0; i < ARRAY_LEN (table->ip_masks); i++)
236     {
237       u32 j, i0, i1;
238
239       i0 = i / 32;
240       i1 = i % 32;
241
242       for (j = 0; j < i0; j++)
243         table->ip_masks[i].as_u32[j] = ~0;
244
245       if (i1)
246         table->ip_masks[i].as_u32[i0] =
247           clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
248     }
249   clib_bihash_init_24_8 (&table->ip_hash, "snat prefixes",
250                          cm->snat_hash_buckets, cm->snat_hash_memory);
251   clib_bihash_set_kvp_format_fn_24_8 (&table->ip_hash,
252                                       format_cnat_snat_prefix);
253
254   return (NULL);
255 }
256
257 VLIB_INIT_FUNCTION (cnat_snat_init);
258
259
260 /*
261  * fd.io coding-style-patch-verification: ON
262  *
263  * Local Variables:
264  * eval: (c-set-style "gnu")
265  * End:
266  */