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