blob: 97eb061c16bded60eeb5af5cfea8e52e49bd4cd7 [file] [log] [blame]
Dave Barach20c02cb2016-06-26 10:42:08 -04001/*
2 * snat.c - simple nat plugin
3 *
4 * Copyright (c) 2016 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/vnet.h>
Dave Barachcab65ec2017-01-11 13:01:14 -050019#include <vnet/ip/ip.h>
20#include <vnet/ip/ip4.h>
Dave Barach20c02cb2016-06-26 10:42:08 -040021#include <vnet/plugin/plugin.h>
Dave Barach20c02cb2016-06-26 10:42:08 -040022#include <snat/snat.h>
Matus Fabianeea28d72017-01-13 04:15:54 -080023#include <snat/snat_ipfix_logging.h>
Matus Fabian066f0342017-02-10 03:48:01 -080024#include <snat/snat_det.h>
Matus Fabian06596c52017-06-06 04:53:28 -070025#include <snat/nat64.h>
Matus Fabiane1ae29a2017-01-27 00:47:58 -080026#include <vnet/fib/fib_table.h>
27#include <vnet/fib/ip4_fib.h>
Dave Barach20c02cb2016-06-26 10:42:08 -040028
Damjan Marion3b46cba2017-01-23 21:13:45 +010029#include <vpp/app/version.h>
Dave Barach20c02cb2016-06-26 10:42:08 -040030
31snat_main_t snat_main;
32
Dave Barach20c02cb2016-06-26 10:42:08 -040033
Dave Barach20c02cb2016-06-26 10:42:08 -040034/* Hook up input features */
Damjan Marion8b3191e2016-11-09 19:54:20 +010035VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
36 .arc_name = "ip4-unicast",
Dave Barach20c02cb2016-06-26 10:42:08 -040037 .node_name = "snat-in2out",
Damjan Marion8b3191e2016-11-09 19:54:20 +010038 .runs_before = VNET_FEATURES ("snat-out2in"),
Dave Barach20c02cb2016-06-26 10:42:08 -040039};
Damjan Marion8b3191e2016-11-09 19:54:20 +010040VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
41 .arc_name = "ip4-unicast",
Dave Barach20c02cb2016-06-26 10:42:08 -040042 .node_name = "snat-out2in",
Damjan Marion8b3191e2016-11-09 19:54:20 +010043 .runs_before = VNET_FEATURES ("ip4-lookup"),
Dave Barach20c02cb2016-06-26 10:42:08 -040044};
Matus Fabian066f0342017-02-10 03:48:01 -080045VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
46 .arc_name = "ip4-unicast",
47 .node_name = "snat-det-in2out",
48 .runs_before = VNET_FEATURES ("snat-det-out2in"),
49};
50VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
51 .arc_name = "ip4-unicast",
52 .node_name = "snat-det-out2in",
53 .runs_before = VNET_FEATURES ("ip4-lookup"),
54};
Matus Fabian475f0552016-10-19 06:17:52 -070055VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
56 .arc_name = "ip4-unicast",
57 .node_name = "snat-in2out-worker-handoff",
58 .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
59};
60VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
61 .arc_name = "ip4-unicast",
62 .node_name = "snat-out2in-worker-handoff",
63 .runs_before = VNET_FEATURES ("ip4-lookup"),
64};
Damjan Marion8b3191e2016-11-09 19:54:20 +010065VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
66 .arc_name = "ip4-unicast",
Matus Fabiandb649882016-08-26 05:45:27 -070067 .node_name = "snat-in2out-fast",
Damjan Marion8b3191e2016-11-09 19:54:20 +010068 .runs_before = VNET_FEATURES ("snat-out2in-fast"),
Matus Fabiandb649882016-08-26 05:45:27 -070069};
Damjan Marion8b3191e2016-11-09 19:54:20 +010070VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
71 .arc_name = "ip4-unicast",
Matus Fabiandb649882016-08-26 05:45:27 -070072 .node_name = "snat-out2in-fast",
Damjan Marion8b3191e2016-11-09 19:54:20 +010073 .runs_before = VNET_FEATURES ("ip4-lookup"),
Matus Fabiandb649882016-08-26 05:45:27 -070074};
75
Damjan Marion3b46cba2017-01-23 21:13:45 +010076/* *INDENT-OFF* */
77VLIB_PLUGIN_REGISTER () = {
78 .version = VPP_BUILD_VER,
Damjan Marion1bfb0dd2017-03-22 11:08:39 +010079 .description = "Network Address Translation",
Damjan Marion3b46cba2017-01-23 21:13:45 +010080};
81/* *INDENT-ON* */
Dave Barach20c02cb2016-06-26 10:42:08 -040082
Matus Fabiane1ae29a2017-01-27 00:47:58 -080083/**
84 * @brief Add/del NAT address to FIB.
85 *
86 * Add the external NAT address to the FIB as receive entries. This ensures
87 * that VPP will reply to ARP for this address and we don't need to enable
88 * proxy ARP on the outside interface.
89 *
Matus Fabiane1ae29a2017-01-27 00:47:58 -080090 * @param addr IPv4 address.
Matus Fabian066f0342017-02-10 03:48:01 -080091 * @param plen address prefix length
Matus Fabiane1ae29a2017-01-27 00:47:58 -080092 * @param sw_if_index Interface.
93 * @param is_add If 0 delete, otherwise add.
94 */
Matus Fabian066f0342017-02-10 03:48:01 -080095void
96snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
97 int is_add)
Matus Fabiane1ae29a2017-01-27 00:47:58 -080098{
Matus Fabiane1ae29a2017-01-27 00:47:58 -080099 fib_prefix_t prefix = {
Matus Fabian066f0342017-02-10 03:48:01 -0800100 .fp_len = p_len,
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800101 .fp_proto = FIB_PROTOCOL_IP4,
102 .fp_addr = {
103 .ip4.as_u32 = addr->as_u32,
104 },
105 };
106 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
107
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800108 if (is_add)
109 fib_table_entry_update_one_path(fib_index,
110 &prefix,
Matus Fabiandccbee32017-01-31 22:20:30 -0800111 FIB_SOURCE_PLUGIN_HI,
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800112 (FIB_ENTRY_FLAG_CONNECTED |
113 FIB_ENTRY_FLAG_LOCAL |
114 FIB_ENTRY_FLAG_EXCLUSIVE),
115 FIB_PROTOCOL_IP4,
116 NULL,
117 sw_if_index,
118 ~0,
119 1,
120 NULL,
121 FIB_ROUTE_PATH_FLAG_NONE);
122 else
123 fib_table_entry_delete(fib_index,
124 &prefix,
Matus Fabiandccbee32017-01-31 22:20:30 -0800125 FIB_SOURCE_PLUGIN_HI);
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800126}
127
Juraj Slobodaeab38d92017-03-06 19:55:21 -0800128void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
Dave Barach20c02cb2016-06-26 10:42:08 -0400129{
130 snat_address_t * ap;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800131 snat_interface_t *i;
Dave Barach20c02cb2016-06-26 10:42:08 -0400132
Juraj Slobodaeab38d92017-03-06 19:55:21 -0800133 if (vrf_id != ~0)
134 sm->vrf_mode = 1;
135
Matus Fabian860dacc2016-10-25 04:19:26 -0700136 /* Check if address already exists */
137 vec_foreach (ap, sm->addresses)
138 {
139 if (ap->addr.as_u32 == addr->as_u32)
140 return;
141 }
142
Dave Barach20c02cb2016-06-26 10:42:08 -0400143 vec_add2 (sm->addresses, ap, 1);
144 ap->addr = *addr;
Juraj Slobodaeab38d92017-03-06 19:55:21 -0800145 ap->fib_index = ip4_fib_index_from_table_id(vrf_id);
Matus Fabian09d96f42017-02-02 01:43:00 -0800146#define _(N, i, n, s) \
147 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535);
148 foreach_snat_protocol
149#undef _
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800150
151 /* Add external address to FIB */
152 pool_foreach (i, sm->interfaces,
153 ({
154 if (i->is_inside)
155 continue;
156
Matus Fabian066f0342017-02-10 03:48:01 -0800157 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
Matus Fabiandccbee32017-01-31 22:20:30 -0800158 break;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800159 }));
Dave Barach20c02cb2016-06-26 10:42:08 -0400160}
161
Matus Fabian724b8152016-10-04 03:23:43 -0700162static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
163 ip4_address_t addr)
164{
165 snat_static_mapping_t *m;
166 pool_foreach (m, sm->static_mappings,
167 ({
168 if (m->external_addr.as_u32 == addr.as_u32)
169 return 1;
170 }));
171
172 return 0;
173}
174
Matus Fabiancfe0fc92017-05-10 06:37:47 -0700175void increment_v4_address (ip4_address_t * a)
Dave Barach20c02cb2016-06-26 10:42:08 -0400176{
177 u32 v;
178
179 v = clib_net_to_host_u32(a->as_u32) + 1;
180 a->as_u32 = clib_host_to_net_u32(v);
181}
182
Dave Barach8b275372017-01-16 10:54:02 -0500183static void
184snat_add_static_mapping_when_resolved (snat_main_t * sm,
185 ip4_address_t l_addr,
186 u16 l_port,
187 u32 sw_if_index,
188 u16 e_port,
189 u32 vrf_id,
Matus Fabian09d96f42017-02-02 01:43:00 -0800190 snat_protocol_t proto,
Dave Barach8b275372017-01-16 10:54:02 -0500191 int addr_only,
192 int is_add)
193{
194 snat_static_map_resolve_t *rp;
195
196 vec_add2 (sm->to_resolve, rp, 1);
197 rp->l_addr.as_u32 = l_addr.as_u32;
198 rp->l_port = l_port;
199 rp->sw_if_index = sw_if_index;
200 rp->e_port = e_port;
201 rp->vrf_id = vrf_id;
Matus Fabian09d96f42017-02-02 01:43:00 -0800202 rp->proto = proto;
Dave Barach8b275372017-01-16 10:54:02 -0500203 rp->addr_only = addr_only;
204 rp->is_add = is_add;
205}
206
Matus Fabiandb649882016-08-26 05:45:27 -0700207/**
208 * @brief Add static mapping.
209 *
210 * Create static mapping between local addr+port and external addr+port.
211 *
212 * @param l_addr Local IPv4 address.
213 * @param e_addr External IPv4 address.
214 * @param l_port Local port number.
215 * @param e_port External port number.
216 * @param vrf_id VRF ID.
217 * @param addr_only If 0 address port and pair mapping, otherwise address only.
Matus Fabian36532bd2017-01-23 23:42:28 -0800218 * @param sw_if_index External port instead of specific IP address.
Matus Fabiandb649882016-08-26 05:45:27 -0700219 * @param is_add If 0 delete static mapping, otherwise add.
220 *
221 * @returns
222 */
223int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
224 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
Matus Fabian09d96f42017-02-02 01:43:00 -0800225 u32 sw_if_index, snat_protocol_t proto, int is_add)
Matus Fabiandb649882016-08-26 05:45:27 -0700226{
227 snat_main_t * sm = &snat_main;
228 snat_static_mapping_t *m;
Matus Fabian09d96f42017-02-02 01:43:00 -0800229 snat_session_key_t m_key;
Matus Fabiandb649882016-08-26 05:45:27 -0700230 clib_bihash_kv_8_8_t kv, value;
231 snat_address_t *a = 0;
232 u32 fib_index = ~0;
233 uword * p;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800234 snat_interface_t *interface;
Matus Fabiandb649882016-08-26 05:45:27 -0700235 int i;
236
Dave Barach8b275372017-01-16 10:54:02 -0500237 /* If the external address is a specific interface address */
238 if (sw_if_index != ~0)
239 {
240 ip4_address_t * first_int_addr;
241
242 /* Might be already set... */
243 first_int_addr = ip4_interface_first_address
244 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
245
246 /* DHCP resolution required? */
247 if (first_int_addr == 0)
248 {
249 snat_add_static_mapping_when_resolved
Matus Fabian09d96f42017-02-02 01:43:00 -0800250 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
Dave Barach8b275372017-01-16 10:54:02 -0500251 addr_only, is_add);
252 return 0;
253 }
254 else
255 e_addr.as_u32 = first_int_addr->as_u32;
256 }
257
Matus Fabiandb649882016-08-26 05:45:27 -0700258 m_key.addr = e_addr;
259 m_key.port = addr_only ? 0 : e_port;
Matus Fabian09d96f42017-02-02 01:43:00 -0800260 m_key.protocol = addr_only ? 0 : proto;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700261 m_key.fib_index = sm->outside_fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700262 kv.key = m_key.as_u64;
263 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
264 m = 0;
265 else
266 m = pool_elt_at_index (sm->static_mappings, value.value);
267
268 if (is_add)
269 {
270 if (m)
271 return VNET_API_ERROR_VALUE_EXIST;
272
273 /* Convert VRF id to FIB index */
274 if (vrf_id != ~0)
275 {
276 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
277 if (!p)
278 return VNET_API_ERROR_NO_SUCH_FIB;
279 fib_index = p[0];
280 }
281 /* If not specified use inside VRF id from SNAT plugin startup config */
282 else
283 {
Matus Fabian31c31aa2017-02-05 22:45:57 -0800284 fib_index = sm->inside_fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700285 vrf_id = sm->inside_vrf_id;
286 }
287
Matus Fabiandb649882016-08-26 05:45:27 -0700288 /* Find external address in allocated addresses and reserve port for
289 address and port pair mapping when dynamic translations enabled */
290 if (!addr_only && !(sm->static_mapping_only))
291 {
292 for (i = 0; i < vec_len (sm->addresses); i++)
293 {
294 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
295 {
296 a = sm->addresses + i;
297 /* External port must be unused */
Matus Fabian09d96f42017-02-02 01:43:00 -0800298 switch (proto)
299 {
300#define _(N, j, n, s) \
301 case SNAT_PROTOCOL_##N: \
302 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
303 return VNET_API_ERROR_INVALID_VALUE; \
304 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
305 if (e_port > 1024) \
306 a->busy_##n##_ports++; \
307 break;
308 foreach_snat_protocol
309#undef _
310 default:
311 clib_warning("unknown_protocol");
312 return VNET_API_ERROR_INVALID_VALUE_2;
313 }
Matus Fabiandb649882016-08-26 05:45:27 -0700314 break;
315 }
316 }
317 /* External address must be allocated */
318 if (!a)
319 return VNET_API_ERROR_NO_SUCH_ENTRY;
320 }
321
322 pool_get (sm->static_mappings, m);
323 memset (m, 0, sizeof (*m));
324 m->local_addr = l_addr;
325 m->external_addr = e_addr;
326 m->addr_only = addr_only;
327 m->vrf_id = vrf_id;
328 m->fib_index = fib_index;
329 if (!addr_only)
330 {
331 m->local_port = l_port;
332 m->external_port = e_port;
Matus Fabian09d96f42017-02-02 01:43:00 -0800333 m->proto = proto;
Matus Fabiandb649882016-08-26 05:45:27 -0700334 }
335
336 m_key.addr = m->local_addr;
337 m_key.port = m->local_port;
Matus Fabian09d96f42017-02-02 01:43:00 -0800338 m_key.protocol = m->proto;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700339 m_key.fib_index = m->fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700340 kv.key = m_key.as_u64;
341 kv.value = m - sm->static_mappings;
342 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
343
344 m_key.addr = m->external_addr;
345 m_key.port = m->external_port;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700346 m_key.fib_index = sm->outside_fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700347 kv.key = m_key.as_u64;
348 kv.value = m - sm->static_mappings;
349 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
Matus Fabian49331682016-12-01 01:32:03 -0800350
351 /* Assign worker */
352 if (sm->workers)
353 {
354 snat_user_key_t w_key0;
Matus Fabian09d96f42017-02-02 01:43:00 -0800355 snat_worker_key_t w_key1;
Matus Fabian49331682016-12-01 01:32:03 -0800356
357 w_key0.addr = m->local_addr;
358 w_key0.fib_index = m->fib_index;
359 kv.key = w_key0.as_u64;
360
361 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
362 {
363 kv.value = sm->first_worker_index +
364 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
365
366 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
367 }
368 else
369 {
370 kv.value = value.value;
371 }
372
373 w_key1.addr = m->external_addr;
374 w_key1.port = clib_host_to_net_u16 (m->external_port);
375 w_key1.fib_index = sm->outside_fib_index;
376 kv.key = w_key1.as_u64;
377 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
378 }
Matus Fabiandb649882016-08-26 05:45:27 -0700379 }
380 else
381 {
382 if (!m)
383 return VNET_API_ERROR_NO_SUCH_ENTRY;
384
385 /* Free external address port */
386 if (!addr_only && !(sm->static_mapping_only))
387 {
388 for (i = 0; i < vec_len (sm->addresses); i++)
389 {
390 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
391 {
392 a = sm->addresses + i;
Matus Fabian09d96f42017-02-02 01:43:00 -0800393 switch (proto)
394 {
395#define _(N, j, n, s) \
396 case SNAT_PROTOCOL_##N: \
397 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
398 if (e_port > 1024) \
399 a->busy_##n##_ports--; \
400 break;
401 foreach_snat_protocol
402#undef _
403 default:
404 clib_warning("unknown_protocol");
405 return VNET_API_ERROR_INVALID_VALUE_2;
406 }
Matus Fabiandb649882016-08-26 05:45:27 -0700407 break;
408 }
409 }
410 }
411
412 m_key.addr = m->local_addr;
413 m_key.port = m->local_port;
Matus Fabian09d96f42017-02-02 01:43:00 -0800414 m_key.protocol = m->proto;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700415 m_key.fib_index = m->fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700416 kv.key = m_key.as_u64;
417 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
418
419 m_key.addr = m->external_addr;
420 m_key.port = m->external_port;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700421 m_key.fib_index = sm->outside_fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700422 kv.key = m_key.as_u64;
423 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
424
425 /* Delete session(s) for static mapping if exist */
426 if (!(sm->static_mapping_only) ||
427 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
428 {
429 snat_user_key_t u_key;
430 snat_user_t *u;
431 dlist_elt_t * head, * elt;
Matus Fabian475f0552016-10-19 06:17:52 -0700432 u32 elt_index, head_index, del_elt_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700433 u32 ses_index;
Matus Fabian475f0552016-10-19 06:17:52 -0700434 u64 user_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700435 snat_session_t * s;
Matus Fabian475f0552016-10-19 06:17:52 -0700436 snat_main_per_thread_data_t *tsm;
Matus Fabiandb649882016-08-26 05:45:27 -0700437
438 u_key.addr = m->local_addr;
439 u_key.fib_index = m->fib_index;
440 kv.key = u_key.as_u64;
441 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
442 {
Matus Fabian475f0552016-10-19 06:17:52 -0700443 user_index = value.value;
Matus Fabian38fb44f2016-11-28 05:36:24 -0800444 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
445 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
446 else
447 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
Matus Fabian475f0552016-10-19 06:17:52 -0700448 u = pool_elt_at_index (tsm->users, user_index);
Matus Fabiandb649882016-08-26 05:45:27 -0700449 if (u->nstaticsessions)
450 {
451 head_index = u->sessions_per_user_list_head_index;
Matus Fabian475f0552016-10-19 06:17:52 -0700452 head = pool_elt_at_index (tsm->list_pool, head_index);
Matus Fabiandb649882016-08-26 05:45:27 -0700453 elt_index = head->next;
Matus Fabian475f0552016-10-19 06:17:52 -0700454 elt = pool_elt_at_index (tsm->list_pool, elt_index);
Matus Fabiandb649882016-08-26 05:45:27 -0700455 ses_index = elt->value;
456 while (ses_index != ~0)
457 {
Matus Fabian475f0552016-10-19 06:17:52 -0700458 s = pool_elt_at_index (tsm->sessions, ses_index);
459 del_elt_index = elt_index;
460 elt_index = elt->next;
461 elt = pool_elt_at_index (tsm->list_pool, elt_index);
462 ses_index = elt->value;
Matus Fabiandb649882016-08-26 05:45:27 -0700463
464 if (!addr_only)
465 {
466 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
467 (clib_net_to_host_u16 (s->out2in.port) != e_port))
468 continue;
469 }
Matus Fabian475f0552016-10-19 06:17:52 -0700470
Matus Fabian7968e6c2017-07-06 05:37:49 -0700471 if (snat_is_unk_proto_session (s))
472 {
473 clib_bihash_kv_16_8_t up_kv;
474 snat_unk_proto_ses_key_t up_key;
475 up_key.l_addr = s->in2out.addr;
476 up_key.r_addr = s->ext_host_addr;
477 up_key.fib_index = s->in2out.fib_index;
478 up_key.proto = s->in2out.port;
479 up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
480 up_kv.key[0] = up_key.as_u64[0];
481 up_kv.key[1] = up_key.as_u64[1];
482 if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
483 &up_kv, 0))
484 clib_warning ("in2out key del failed");
485
486 up_key.l_addr = s->out2in.addr;
487 up_key.fib_index = s->out2in.fib_index;
488 up_kv.key[0] = up_key.as_u64[0];
489 up_kv.key[1] = up_key.as_u64[1];
490 if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
491 &up_kv, 0))
492 clib_warning ("out2in key del failed");
493
494 goto delete;
495 }
Matus Fabianeea28d72017-01-13 04:15:54 -0800496 /* log NAT event */
497 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
498 s->out2in.addr.as_u32,
499 s->in2out.protocol,
500 s->in2out.port,
501 s->out2in.port,
502 s->in2out.fib_index);
503
Matus Fabiandb649882016-08-26 05:45:27 -0700504 value.key = s->in2out.as_u64;
Matus Fabian7968e6c2017-07-06 05:37:49 -0700505 if (clib_bihash_add_del_8_8 (&sm->in2out, &value, 0))
506 clib_warning ("in2out key del failed");
Matus Fabiandb649882016-08-26 05:45:27 -0700507 value.key = s->out2in.as_u64;
Matus Fabian7968e6c2017-07-06 05:37:49 -0700508 if (clib_bihash_add_del_8_8 (&sm->out2in, &value, 0))
509 clib_warning ("out2in key del failed");
510delete:
Matus Fabian475f0552016-10-19 06:17:52 -0700511 pool_put (tsm->sessions, s);
512
513 clib_dlist_remove (tsm->list_pool, del_elt_index);
514 pool_put_index (tsm->list_pool, del_elt_index);
515 u->nstaticsessions--;
Matus Fabiandb649882016-08-26 05:45:27 -0700516
517 if (!addr_only)
518 break;
Matus Fabiandb649882016-08-26 05:45:27 -0700519 }
520 if (addr_only)
521 {
Matus Fabian475f0552016-10-19 06:17:52 -0700522 pool_put (tsm->users, u);
Matus Fabiandb649882016-08-26 05:45:27 -0700523 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
524 }
Matus Fabiandb649882016-08-26 05:45:27 -0700525 }
526 }
527 }
528
529 /* Delete static mapping from pool */
530 pool_put (sm->static_mappings, m);
531 }
532
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800533 if (!addr_only)
534 return 0;
535
536 /* Add/delete external address to FIB */
537 pool_foreach (interface, sm->interfaces,
538 ({
539 if (interface->is_inside)
540 continue;
541
Matus Fabian066f0342017-02-10 03:48:01 -0800542 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
Matus Fabiandccbee32017-01-31 22:20:30 -0800543 break;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800544 }));
545
Matus Fabiandb649882016-08-26 05:45:27 -0700546 return 0;
547}
548
Matus Fabian36532bd2017-01-23 23:42:28 -0800549int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
550{
551 snat_address_t *a = 0;
552 snat_session_t *ses;
553 u32 *ses_to_be_removed = 0, *ses_index;
554 clib_bihash_kv_8_8_t kv, value;
555 snat_user_key_t user_key;
556 snat_user_t *u;
557 snat_main_per_thread_data_t *tsm;
558 snat_static_mapping_t *m;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800559 snat_interface_t *interface;
Matus Fabian36532bd2017-01-23 23:42:28 -0800560 int i;
561
562 /* Find SNAT address */
563 for (i=0; i < vec_len (sm->addresses); i++)
564 {
565 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
566 {
567 a = sm->addresses + i;
568 break;
569 }
570 }
571 if (!a)
572 return VNET_API_ERROR_NO_SUCH_ENTRY;
573
574 if (delete_sm)
575 {
576 pool_foreach (m, sm->static_mappings,
577 ({
578 if (m->external_addr.as_u32 == addr.as_u32)
579 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
580 m->local_port, m->external_port,
Matus Fabian09d96f42017-02-02 01:43:00 -0800581 m->vrf_id, m->addr_only, ~0,
582 m->proto, 0);
Matus Fabian36532bd2017-01-23 23:42:28 -0800583 }));
584 }
585 else
586 {
587 /* Check if address is used in some static mapping */
588 if (is_snat_address_used_in_static_mapping(sm, addr))
589 {
590 clib_warning ("address used in static mapping");
591 return VNET_API_ERROR_UNSPECIFIED;
592 }
593 }
594
595 /* Delete sessions using address */
Matus Fabian09d96f42017-02-02 01:43:00 -0800596 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
Matus Fabian36532bd2017-01-23 23:42:28 -0800597 {
598 vec_foreach (tsm, sm->per_thread_data)
599 {
600 pool_foreach (ses, tsm->sessions, ({
601 if (ses->out2in.addr.as_u32 == addr.as_u32)
602 {
Matus Fabian7968e6c2017-07-06 05:37:49 -0700603 if (snat_is_unk_proto_session (ses))
604 {
605 clib_bihash_kv_16_8_t up_kv;
606 snat_unk_proto_ses_key_t up_key;
607 up_key.l_addr = ses->in2out.addr;
608 up_key.r_addr = ses->ext_host_addr;
609 up_key.fib_index = ses->in2out.fib_index;
610 up_key.proto = ses->in2out.port;
611 up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
612 up_kv.key[0] = up_key.as_u64[0];
613 up_kv.key[1] = up_key.as_u64[1];
614 if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
615 &up_kv, 0))
616 clib_warning ("in2out key del failed");
617
618 up_key.l_addr = ses->out2in.addr;
619 up_key.fib_index = ses->out2in.fib_index;
620 up_kv.key[0] = up_key.as_u64[0];
621 up_kv.key[1] = up_key.as_u64[1];
622 if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
623 &up_kv, 0))
624 clib_warning ("out2in key del failed");
625 }
626 else
627 {
628 /* log NAT event */
629 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
630 ses->out2in.addr.as_u32,
631 ses->in2out.protocol,
632 ses->in2out.port,
633 ses->out2in.port,
634 ses->in2out.fib_index);
635 kv.key = ses->in2out.as_u64;
636 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
637 kv.key = ses->out2in.as_u64;
638 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
639 }
Matus Fabian36532bd2017-01-23 23:42:28 -0800640 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
Matus Fabian36532bd2017-01-23 23:42:28 -0800641 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
642 user_key.addr = ses->in2out.addr;
643 user_key.fib_index = ses->in2out.fib_index;
644 kv.key = user_key.as_u64;
645 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
646 {
647 u = pool_elt_at_index (tsm->users, value.value);
648 u->nsessions--;
649 }
650 }
651 }));
652
653 vec_foreach (ses_index, ses_to_be_removed)
654 pool_put_index (tsm->sessions, ses_index[0]);
655
656 vec_free (ses_to_be_removed);
657 }
658 }
659
660 vec_del1 (sm->addresses, i);
661
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800662 /* Delete external address from FIB */
663 pool_foreach (interface, sm->interfaces,
664 ({
665 if (interface->is_inside)
666 continue;
667
Matus Fabian066f0342017-02-10 03:48:01 -0800668 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
Matus Fabiandccbee32017-01-31 22:20:30 -0800669 break;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800670 }));
671
Matus Fabian36532bd2017-01-23 23:42:28 -0800672 return 0;
673}
674
Matus Fabiancfe0fc92017-05-10 06:37:47 -0700675int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
Matus Fabian588144a2016-10-24 03:30:00 -0700676{
677 snat_main_t *sm = &snat_main;
678 snat_interface_t *i;
Damjan Marion8b3191e2016-11-09 19:54:20 +0100679 const char * feature_name;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800680 snat_address_t * ap;
681 snat_static_mapping_t * m;
Matus Fabian066f0342017-02-10 03:48:01 -0800682 snat_det_map_t * dm;
Matus Fabian588144a2016-10-24 03:30:00 -0700683
684 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
Damjan Marion8b3191e2016-11-09 19:54:20 +0100685 feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
Matus Fabian588144a2016-10-24 03:30:00 -0700686 else
Matus Fabian475f0552016-10-19 06:17:52 -0700687 {
Matus Fabian066f0342017-02-10 03:48:01 -0800688 if (sm->num_workers > 1 && !sm->deterministic)
Matus Fabian475f0552016-10-19 06:17:52 -0700689 feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
Matus Fabian066f0342017-02-10 03:48:01 -0800690 else if (sm->deterministic)
691 feature_name = is_inside ? "snat-det-in2out" : "snat-det-out2in";
Matus Fabian475f0552016-10-19 06:17:52 -0700692 else
693 feature_name = is_inside ? "snat-in2out" : "snat-out2in";
694 }
Matus Fabian588144a2016-10-24 03:30:00 -0700695
Damjan Marion8b3191e2016-11-09 19:54:20 +0100696 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
697 !is_del, 0, 0);
Matus Fabian588144a2016-10-24 03:30:00 -0700698
Matus Fabian066f0342017-02-10 03:48:01 -0800699 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
700 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
Matus Fabian475f0552016-10-19 06:17:52 -0700701
Matus Fabian066f0342017-02-10 03:48:01 -0800702 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
703 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
Matus Fabian475f0552016-10-19 06:17:52 -0700704
Matus Fabian588144a2016-10-24 03:30:00 -0700705 pool_foreach (i, sm->interfaces,
706 ({
707 if (i->sw_if_index == sw_if_index)
708 {
709 if (is_del)
710 pool_put (sm->interfaces, i);
711 else
712 return VNET_API_ERROR_VALUE_EXIST;
713
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800714 goto fib;
Matus Fabian588144a2016-10-24 03:30:00 -0700715 }
716 }));
717
718 if (is_del)
719 return VNET_API_ERROR_NO_SUCH_ENTRY;
720
721 pool_get (sm->interfaces, i);
722 i->sw_if_index = sw_if_index;
723 i->is_inside = is_inside;
724
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800725 /* Add/delete external addresses to FIB */
726fib:
727 if (is_inside)
728 return 0;
729
730 vec_foreach (ap, sm->addresses)
Matus Fabian066f0342017-02-10 03:48:01 -0800731 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800732
733 pool_foreach (m, sm->static_mappings,
734 ({
735 if (!(m->addr_only))
736 continue;
737
Matus Fabian066f0342017-02-10 03:48:01 -0800738 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
739 }));
740
741 pool_foreach (dm, sm->det_maps,
742 ({
743 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800744 }));
745
Matus Fabian588144a2016-10-24 03:30:00 -0700746 return 0;
747}
748
Matus Fabiancfe0fc92017-05-10 06:37:47 -0700749int snat_set_workers (uword * bitmap)
Matus Fabian475f0552016-10-19 06:17:52 -0700750{
751 snat_main_t *sm = &snat_main;
752 int i;
753
754 if (sm->num_workers < 2)
755 return VNET_API_ERROR_FEATURE_DISABLED;
756
757 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
758 return VNET_API_ERROR_INVALID_WORKER;
759
760 vec_free (sm->workers);
761 clib_bitmap_foreach (i, bitmap,
762 ({
763 vec_add1(sm->workers, i);
764 }));
765
766 return 0;
767}
768
Dave Barach8b275372017-01-16 10:54:02 -0500769
Dave Barachcab65ec2017-01-11 13:01:14 -0500770static void
771snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
772 uword opaque,
773 u32 sw_if_index,
774 ip4_address_t * address,
775 u32 address_length,
776 u32 if_address_index,
777 u32 is_delete);
778
Dave Barach20c02cb2016-06-26 10:42:08 -0400779static clib_error_t * snat_init (vlib_main_t * vm)
780{
781 snat_main_t * sm = &snat_main;
Matus Fabian08ce4322017-06-19 05:28:27 -0700782 clib_error_t * error = 0;
Dave Barach20c02cb2016-06-26 10:42:08 -0400783 ip4_main_t * im = &ip4_main;
784 ip_lookup_main_t * lm = &im->lookup_main;
Matus Fabian475f0552016-10-19 06:17:52 -0700785 uword *p;
786 vlib_thread_registration_t *tr;
787 vlib_thread_main_t *tm = vlib_get_thread_main ();
788 uword *bitmap = 0;
789 u32 i;
Dave Barachcab65ec2017-01-11 13:01:14 -0500790 ip4_add_del_interface_address_callback_t cb4;
Dave Barach20c02cb2016-06-26 10:42:08 -0400791
Dave Barach20c02cb2016-06-26 10:42:08 -0400792 sm->vlib_main = vm;
793 sm->vnet_main = vnet_get_main();
794 sm->ip4_main = im;
795 sm->ip4_lookup_main = lm;
796 sm->api_main = &api_main;
Matus Fabian475f0552016-10-19 06:17:52 -0700797 sm->first_worker_index = 0;
798 sm->next_worker = 0;
799 sm->num_workers = 0;
800 sm->workers = 0;
801 sm->fq_in2out_index = ~0;
802 sm->fq_out2in_index = ~0;
Matus Fabian6a0946f2017-04-12 03:36:13 -0700803 sm->udp_timeout = SNAT_UDP_TIMEOUT;
804 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
805 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
806 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
Matus Fabian475f0552016-10-19 06:17:52 -0700807
808 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
809 if (p)
810 {
811 tr = (vlib_thread_registration_t *) p[0];
812 if (tr)
813 {
814 sm->num_workers = tr->count;
815 sm->first_worker_index = tr->first_index;
816 }
817 }
818
819 /* Use all available workers by default */
820 if (sm->num_workers > 1)
821 {
822 for (i=0; i < sm->num_workers; i++)
823 bitmap = clib_bitmap_set (bitmap, i, 1);
824 snat_set_workers(bitmap);
825 clib_bitmap_free (bitmap);
826 }
Dave Barach20c02cb2016-06-26 10:42:08 -0400827
Matus Fabiancfe0fc92017-05-10 06:37:47 -0700828 error = snat_api_init(vm, sm);
Matus Fabian08ce4322017-06-19 05:28:27 -0700829 if (error)
830 return error;
Dave Barach20c02cb2016-06-26 10:42:08 -0400831
Dave Barachcab65ec2017-01-11 13:01:14 -0500832 /* Set up the interface address add/del callback */
833 cb4.function = snat_ip4_add_del_interface_address_cb;
834 cb4.function_opaque = 0;
835
836 vec_add1 (im->add_del_interface_address_callbacks, cb4);
837
Matus Fabianeea28d72017-01-13 04:15:54 -0800838 /* Init IPFIX logging */
839 snat_ipfix_logging_init(vm);
840
Matus Fabian08ce4322017-06-19 05:28:27 -0700841 error = nat64_init(vm);
Matus Fabian06596c52017-06-06 04:53:28 -0700842
Dave Barach20c02cb2016-06-26 10:42:08 -0400843 return error;
844}
845
846VLIB_INIT_FUNCTION (snat_init);
847
848void snat_free_outside_address_and_port (snat_main_t * sm,
849 snat_session_key_t * k,
850 u32 address_index)
851{
852 snat_address_t *a;
853 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
854
855 ASSERT (address_index < vec_len (sm->addresses));
856
857 a = sm->addresses + address_index;
858
Matus Fabian09d96f42017-02-02 01:43:00 -0800859 switch (k->protocol)
860 {
861#define _(N, i, n, s) \
862 case SNAT_PROTOCOL_##N: \
863 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
864 port_host_byte_order) == 1); \
865 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
866 port_host_byte_order, 0); \
867 a->busy_##n##_ports--; \
868 break;
869 foreach_snat_protocol
870#undef _
871 default:
872 clib_warning("unknown_protocol");
873 return;
874 }
Dave Barach20c02cb2016-06-26 10:42:08 -0400875}
876
Matus Fabiandb649882016-08-26 05:45:27 -0700877/**
878 * @brief Match SNAT static mapping.
879 *
880 * @param sm SNAT main.
881 * @param match Address and port to match.
882 * @param mapping External or local address and port of the matched mapping.
883 * @param by_external If 0 match by local address otherwise match by external
884 * address.
Juraj Slobodad3677682017-04-14 03:24:45 +0200885 * @param is_addr_only If matched mapping is address only
Matus Fabiandb649882016-08-26 05:45:27 -0700886 *
887 * @returns 0 if match found otherwise 1.
888 */
889int snat_static_mapping_match (snat_main_t * sm,
890 snat_session_key_t match,
891 snat_session_key_t * mapping,
Juraj Slobodad3677682017-04-14 03:24:45 +0200892 u8 by_external,
893 u8 *is_addr_only)
Matus Fabiandb649882016-08-26 05:45:27 -0700894{
895 clib_bihash_kv_8_8_t kv, value;
896 snat_static_mapping_t *m;
Matus Fabian09d96f42017-02-02 01:43:00 -0800897 snat_session_key_t m_key;
Matus Fabiandb649882016-08-26 05:45:27 -0700898 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
899
900 if (by_external)
901 mapping_hash = &sm->static_mapping_by_external;
902
903 m_key.addr = match.addr;
904 m_key.port = clib_net_to_host_u16 (match.port);
Matus Fabian09d96f42017-02-02 01:43:00 -0800905 m_key.protocol = match.protocol;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700906 m_key.fib_index = match.fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700907
908 kv.key = m_key.as_u64;
909
910 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
911 {
912 /* Try address only mapping */
913 m_key.port = 0;
Matus Fabian09d96f42017-02-02 01:43:00 -0800914 m_key.protocol = 0;
Matus Fabiandb649882016-08-26 05:45:27 -0700915 kv.key = m_key.as_u64;
916 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
917 return 1;
918 }
919
920 m = pool_elt_at_index (sm->static_mappings, value.value);
921
922 if (by_external)
923 {
924 mapping->addr = m->local_addr;
925 /* Address only mapping doesn't change port */
926 mapping->port = m->addr_only ? match.port
927 : clib_host_to_net_u16 (m->local_port);
928 mapping->fib_index = m->fib_index;
929 }
930 else
931 {
932 mapping->addr = m->external_addr;
933 /* Address only mapping doesn't change port */
934 mapping->port = m->addr_only ? match.port
935 : clib_host_to_net_u16 (m->external_port);
936 mapping->fib_index = sm->outside_fib_index;
937 }
938
Juraj Slobodad3677682017-04-14 03:24:45 +0200939 if (PREDICT_FALSE(is_addr_only != 0))
940 *is_addr_only = m->addr_only;
941
Matus Fabiandb649882016-08-26 05:45:27 -0700942 return 0;
943}
944
Dave Barach20c02cb2016-06-26 10:42:08 -0400945int snat_alloc_outside_address_and_port (snat_main_t * sm,
Juraj Slobodaeab38d92017-03-06 19:55:21 -0800946 u32 fib_index,
Dave Barach20c02cb2016-06-26 10:42:08 -0400947 snat_session_key_t * k,
948 u32 * address_indexp)
949{
950 int i;
951 snat_address_t *a;
952 u32 portnum;
953
954 for (i = 0; i < vec_len (sm->addresses); i++)
955 {
Matus Fabian09d96f42017-02-02 01:43:00 -0800956 a = sm->addresses + i;
Juraj Slobodaeab38d92017-03-06 19:55:21 -0800957 if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
958 continue;
Matus Fabian09d96f42017-02-02 01:43:00 -0800959 switch (k->protocol)
Dave Barach20c02cb2016-06-26 10:42:08 -0400960 {
Matus Fabian09d96f42017-02-02 01:43:00 -0800961#define _(N, j, n, s) \
962 case SNAT_PROTOCOL_##N: \
963 if (a->busy_##n##_ports < (65535-1024)) \
964 { \
965 while (1) \
966 { \
967 portnum = random_u32 (&sm->random_seed); \
968 portnum &= 0xFFFF; \
969 if (portnum < 1024) \
970 continue; \
971 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
972 continue; \
973 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
974 a->busy_##n##_ports++; \
975 k->addr = a->addr; \
976 k->port = clib_host_to_net_u16(portnum); \
977 *address_indexp = i; \
978 return 0; \
979 } \
980 } \
981 break;
982 foreach_snat_protocol
983#undef _
984 default:
985 clib_warning("unknown protocol");
986 return 1;
Dave Barach20c02cb2016-06-26 10:42:08 -0400987 }
Matus Fabian09d96f42017-02-02 01:43:00 -0800988
Dave Barach20c02cb2016-06-26 10:42:08 -0400989 }
990 /* Totally out of translations to use... */
Matus Fabianeea28d72017-01-13 04:15:54 -0800991 snat_ipfix_logging_addresses_exhausted(0);
Dave Barach20c02cb2016-06-26 10:42:08 -0400992 return 1;
993}
994
995
996static clib_error_t *
997add_address_command_fn (vlib_main_t * vm,
998 unformat_input_t * input,
999 vlib_cli_command_t * cmd)
1000{
Matus Fabiandb649882016-08-26 05:45:27 -07001001 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barach20c02cb2016-06-26 10:42:08 -04001002 snat_main_t * sm = &snat_main;
1003 ip4_address_t start_addr, end_addr, this_addr;
1004 u32 start_host_order, end_host_order;
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001005 u32 vrf_id = ~0;
Dave Barach20c02cb2016-06-26 10:42:08 -04001006 int i, count;
Matus Fabian724b8152016-10-04 03:23:43 -07001007 int is_add = 1;
1008 int rv = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05001009 clib_error_t *error = 0;
Dave Barach20c02cb2016-06-26 10:42:08 -04001010
Matus Fabiandb649882016-08-26 05:45:27 -07001011 /* Get a line of input. */
1012 if (!unformat_user (input, unformat_line_input, line_input))
1013 return 0;
1014
Matus Fabian724b8152016-10-04 03:23:43 -07001015 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1016 {
1017 if (unformat (line_input, "%U - %U",
1018 unformat_ip4_address, &start_addr,
1019 unformat_ip4_address, &end_addr))
1020 ;
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001021 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
1022 ;
Matus Fabian724b8152016-10-04 03:23:43 -07001023 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1024 end_addr = start_addr;
1025 else if (unformat (line_input, "del"))
1026 is_add = 0;
1027 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001028 {
1029 error = clib_error_return (0, "unknown input '%U'",
1030 format_unformat_error, line_input);
1031 goto done;
1032 }
Matus Fabian724b8152016-10-04 03:23:43 -07001033 }
Matus Fabiandb649882016-08-26 05:45:27 -07001034
1035 if (sm->static_mapping_only)
Billy McFalla9a20e72017-02-15 11:39:12 -05001036 {
1037 error = clib_error_return (0, "static mapping only mode");
1038 goto done;
1039 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001040
1041 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1042 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1043
1044 if (end_host_order < start_host_order)
Billy McFalla9a20e72017-02-15 11:39:12 -05001045 {
1046 error = clib_error_return (0, "end address less than start address");
1047 goto done;
1048 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001049
1050 count = (end_host_order - start_host_order) + 1;
1051
1052 if (count > 1024)
1053 clib_warning ("%U - %U, %d addresses...",
1054 format_ip4_address, &start_addr,
1055 format_ip4_address, &end_addr,
1056 count);
1057
1058 this_addr = start_addr;
1059
1060 for (i = 0; i < count; i++)
1061 {
Matus Fabian724b8152016-10-04 03:23:43 -07001062 if (is_add)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001063 snat_add_address (sm, &this_addr, vrf_id);
Matus Fabian724b8152016-10-04 03:23:43 -07001064 else
Matus Fabian36532bd2017-01-23 23:42:28 -08001065 rv = snat_del_address (sm, this_addr, 0);
Matus Fabian724b8152016-10-04 03:23:43 -07001066
1067 switch (rv)
1068 {
1069 case VNET_API_ERROR_NO_SUCH_ENTRY:
Billy McFalla9a20e72017-02-15 11:39:12 -05001070 error = clib_error_return (0, "S-NAT address not exist.");
1071 goto done;
Matus Fabian724b8152016-10-04 03:23:43 -07001072 case VNET_API_ERROR_UNSPECIFIED:
Billy McFalla9a20e72017-02-15 11:39:12 -05001073 error = clib_error_return (0, "S-NAT address used in static mapping.");
1074 goto done;
Matus Fabian724b8152016-10-04 03:23:43 -07001075 default:
1076 break;
1077 }
1078
Dave Barach20c02cb2016-06-26 10:42:08 -04001079 increment_v4_address (&this_addr);
1080 }
1081
Billy McFalla9a20e72017-02-15 11:39:12 -05001082done:
1083 unformat_free (line_input);
1084
1085 return error;
Dave Barach20c02cb2016-06-26 10:42:08 -04001086}
1087
1088VLIB_CLI_COMMAND (add_address_command, static) = {
1089 .path = "snat add address",
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001090 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] "
1091 "[tenant-vrf <vrf-id>] [del]",
Dave Barach20c02cb2016-06-26 10:42:08 -04001092 .function = add_address_command_fn,
1093};
1094
1095static clib_error_t *
1096snat_feature_command_fn (vlib_main_t * vm,
1097 unformat_input_t * input,
1098 vlib_cli_command_t * cmd)
1099{
Matus Fabiandb649882016-08-26 05:45:27 -07001100 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barach20c02cb2016-06-26 10:42:08 -04001101 vnet_main_t * vnm = vnet_get_main();
Dave Barach20c02cb2016-06-26 10:42:08 -04001102 clib_error_t * error = 0;
Matus Fabian588144a2016-10-24 03:30:00 -07001103 u32 sw_if_index;
Dave Barach20c02cb2016-06-26 10:42:08 -04001104 u32 * inside_sw_if_indices = 0;
1105 u32 * outside_sw_if_indices = 0;
1106 int is_del = 0;
1107 int i;
1108
1109 sw_if_index = ~0;
1110
Matus Fabiandb649882016-08-26 05:45:27 -07001111 /* Get a line of input. */
1112 if (!unformat_user (input, unformat_line_input, line_input))
1113 return 0;
1114
1115 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Dave Barach20c02cb2016-06-26 10:42:08 -04001116 {
Matus Fabiandb649882016-08-26 05:45:27 -07001117 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
Dave Barach20c02cb2016-06-26 10:42:08 -04001118 vnm, &sw_if_index))
1119 vec_add1 (inside_sw_if_indices, sw_if_index);
Matus Fabiandb649882016-08-26 05:45:27 -07001120 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
Dave Barach20c02cb2016-06-26 10:42:08 -04001121 vnm, &sw_if_index))
1122 vec_add1 (outside_sw_if_indices, sw_if_index);
Matus Fabiandb649882016-08-26 05:45:27 -07001123 else if (unformat (line_input, "del"))
Dave Barach20c02cb2016-06-26 10:42:08 -04001124 is_del = 1;
1125 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001126 {
1127 error = clib_error_return (0, "unknown input '%U'",
1128 format_unformat_error, line_input);
1129 goto done;
1130 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001131 }
1132
1133 if (vec_len (inside_sw_if_indices))
1134 {
Dave Barach20c02cb2016-06-26 10:42:08 -04001135 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1136 {
1137 sw_if_index = inside_sw_if_indices[i];
Matus Fabian588144a2016-10-24 03:30:00 -07001138 snat_interface_add_del (sw_if_index, 1, is_del);
Dave Barach20c02cb2016-06-26 10:42:08 -04001139 }
1140 }
1141
1142 if (vec_len (outside_sw_if_indices))
1143 {
Dave Barach20c02cb2016-06-26 10:42:08 -04001144 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1145 {
1146 sw_if_index = outside_sw_if_indices[i];
Matus Fabian588144a2016-10-24 03:30:00 -07001147 snat_interface_add_del (sw_if_index, 0, is_del);
Dave Barach20c02cb2016-06-26 10:42:08 -04001148 }
1149 }
1150
Billy McFalla9a20e72017-02-15 11:39:12 -05001151done:
1152 unformat_free (line_input);
Dave Barach20c02cb2016-06-26 10:42:08 -04001153 vec_free (inside_sw_if_indices);
1154 vec_free (outside_sw_if_indices);
1155
1156 return error;
1157}
1158
1159VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1160 .path = "set interface snat",
1161 .function = snat_feature_command_fn,
1162 .short_help = "set interface snat in <intfc> out <intfc> [del]",
1163};
1164
Matus Fabian09d96f42017-02-02 01:43:00 -08001165uword
1166unformat_snat_protocol (unformat_input_t * input, va_list * args)
1167{
1168 u32 *r = va_arg (*args, u32 *);
1169
1170 if (0);
1171#define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
1172 foreach_snat_protocol
1173#undef _
1174 else
1175 return 0;
1176 return 1;
1177}
1178
1179u8 *
1180format_snat_protocol (u8 * s, va_list * args)
1181{
1182 u32 i = va_arg (*args, u32);
1183 u8 *t = 0;
1184
1185 switch (i)
1186 {
1187#define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
1188 foreach_snat_protocol
1189#undef _
1190 default:
1191 s = format (s, "unknown");
1192 }
1193 s = format (s, "%s", t);
1194 return s;
1195}
1196
Dave Barach20c02cb2016-06-26 10:42:08 -04001197static clib_error_t *
Matus Fabiandb649882016-08-26 05:45:27 -07001198add_static_mapping_command_fn (vlib_main_t * vm,
1199 unformat_input_t * input,
1200 vlib_cli_command_t * cmd)
1201{
1202 unformat_input_t _line_input, *line_input = &_line_input;
1203 clib_error_t * error = 0;
1204 ip4_address_t l_addr, e_addr;
1205 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1206 int is_add = 1;
1207 int addr_only = 1;
Dave Barach8b275372017-01-16 10:54:02 -05001208 u32 sw_if_index = ~0;
1209 vnet_main_t * vnm = vnet_get_main();
Matus Fabiandb649882016-08-26 05:45:27 -07001210 int rv;
Matus Fabian09d96f42017-02-02 01:43:00 -08001211 snat_protocol_t proto;
Matus Fabianb449f482017-02-05 22:14:41 -08001212 u8 proto_set = 0;
Matus Fabiandb649882016-08-26 05:45:27 -07001213
1214 /* Get a line of input. */
1215 if (!unformat_user (input, unformat_line_input, line_input))
1216 return 0;
1217
1218 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1219 {
1220 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1221 &l_port))
1222 addr_only = 0;
1223 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1224 ;
1225 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1226 &e_addr, &e_port))
1227 addr_only = 0;
1228 else if (unformat (line_input, "external %U", unformat_ip4_address,
1229 &e_addr))
1230 ;
Dave Barach8b275372017-01-16 10:54:02 -05001231 else if (unformat (line_input, "external %U %u",
1232 unformat_vnet_sw_interface, vnm, &sw_if_index,
1233 &e_port))
1234 addr_only = 0;
1235
1236 else if (unformat (line_input, "external %U",
1237 unformat_vnet_sw_interface, vnm, &sw_if_index))
1238 ;
Matus Fabiandb649882016-08-26 05:45:27 -07001239 else if (unformat (line_input, "vrf %u", &vrf_id))
1240 ;
Matus Fabian09d96f42017-02-02 01:43:00 -08001241 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
Matus Fabianb449f482017-02-05 22:14:41 -08001242 proto_set = 1;
Matus Fabiandb649882016-08-26 05:45:27 -07001243 else if (unformat (line_input, "del"))
1244 is_add = 0;
1245 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001246 {
1247 error = clib_error_return (0, "unknown input: '%U'",
1248 format_unformat_error, line_input);
1249 goto done;
1250 }
Matus Fabiandb649882016-08-26 05:45:27 -07001251 }
Matus Fabiandb649882016-08-26 05:45:27 -07001252
Matus Fabianb449f482017-02-05 22:14:41 -08001253 if (!addr_only && !proto_set)
Billy McFalla9a20e72017-02-15 11:39:12 -05001254 {
1255 error = clib_error_return (0, "missing protocol");
1256 goto done;
1257 }
Matus Fabianb449f482017-02-05 22:14:41 -08001258
Matus Fabiandb649882016-08-26 05:45:27 -07001259 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
Matus Fabian09d96f42017-02-02 01:43:00 -08001260 vrf_id, addr_only, sw_if_index, proto, is_add);
Matus Fabiandb649882016-08-26 05:45:27 -07001261
1262 switch (rv)
1263 {
1264 case VNET_API_ERROR_INVALID_VALUE:
Billy McFalla9a20e72017-02-15 11:39:12 -05001265 error = clib_error_return (0, "External port already in use.");
1266 goto done;
Matus Fabiandb649882016-08-26 05:45:27 -07001267 case VNET_API_ERROR_NO_SUCH_ENTRY:
1268 if (is_add)
Billy McFalla9a20e72017-02-15 11:39:12 -05001269 error = clib_error_return (0, "External addres must be allocated.");
Matus Fabiandb649882016-08-26 05:45:27 -07001270 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001271 error = clib_error_return (0, "Mapping not exist.");
1272 goto done;
Matus Fabiandb649882016-08-26 05:45:27 -07001273 case VNET_API_ERROR_NO_SUCH_FIB:
Billy McFalla9a20e72017-02-15 11:39:12 -05001274 error = clib_error_return (0, "No such VRF id.");
1275 goto done;
Matus Fabiandb649882016-08-26 05:45:27 -07001276 case VNET_API_ERROR_VALUE_EXIST:
Billy McFalla9a20e72017-02-15 11:39:12 -05001277 error = clib_error_return (0, "Mapping already exist.");
1278 goto done;
Matus Fabiandb649882016-08-26 05:45:27 -07001279 default:
1280 break;
1281 }
1282
Billy McFalla9a20e72017-02-15 11:39:12 -05001283done:
1284 unformat_free (line_input);
1285
Matus Fabiandb649882016-08-26 05:45:27 -07001286 return error;
1287}
1288
1289/*?
1290 * @cliexpar
1291 * @cliexstart{snat add static mapping}
1292 * Static mapping allows hosts on the external network to initiate connection
1293 * to to the local network host.
1294 * To create static mapping between local host address 10.0.0.3 port 6303 and
Matus Fabianb449f482017-02-05 22:14:41 -08001295 * external address 4.4.4.4 port 3606 for TCP protocol use:
1296 * vpp# snat add static mapping local tcp 10.0.0.3 6303 external 4.4.4.4 3606
Matus Fabiandb649882016-08-26 05:45:27 -07001297 * If not runnig "static mapping only" S-NAT plugin mode use before:
1298 * vpp# snat add address 4.4.4.4
1299 * To create static mapping between local and external address use:
1300 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1301 * @cliexend
1302?*/
1303VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1304 .path = "snat add static mapping",
1305 .function = add_static_mapping_command_fn,
1306 .short_help =
Matus Fabianb449f482017-02-05 22:14:41 -08001307 "snat add static mapping local tcp|udp|icmp <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
Matus Fabiandb649882016-08-26 05:45:27 -07001308};
1309
1310static clib_error_t *
Matus Fabian475f0552016-10-19 06:17:52 -07001311set_workers_command_fn (vlib_main_t * vm,
1312 unformat_input_t * input,
1313 vlib_cli_command_t * cmd)
1314{
1315 unformat_input_t _line_input, *line_input = &_line_input;
1316 uword *bitmap = 0;
1317 int rv = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05001318 clib_error_t *error = 0;
Matus Fabian475f0552016-10-19 06:17:52 -07001319
1320 /* Get a line of input. */
1321 if (!unformat_user (input, unformat_line_input, line_input))
1322 return 0;
1323
1324 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1325 {
1326 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1327 ;
1328 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001329 {
1330 error = clib_error_return (0, "unknown input '%U'",
1331 format_unformat_error, line_input);
1332 goto done;
1333 }
Matus Fabian475f0552016-10-19 06:17:52 -07001334 }
Matus Fabian475f0552016-10-19 06:17:52 -07001335
1336 if (bitmap == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -05001337 {
1338 error = clib_error_return (0, "List of workers must be specified.");
1339 goto done;
1340 }
Matus Fabian475f0552016-10-19 06:17:52 -07001341
1342 rv = snat_set_workers(bitmap);
1343
1344 clib_bitmap_free (bitmap);
1345
1346 switch (rv)
1347 {
1348 case VNET_API_ERROR_INVALID_WORKER:
Billy McFalla9a20e72017-02-15 11:39:12 -05001349 error = clib_error_return (0, "Invalid worker(s).");
1350 goto done;
Matus Fabian475f0552016-10-19 06:17:52 -07001351 case VNET_API_ERROR_FEATURE_DISABLED:
Billy McFalla9a20e72017-02-15 11:39:12 -05001352 error = clib_error_return (0,
Matus Fabian475f0552016-10-19 06:17:52 -07001353 "Supported only if 2 or more workes available.");
Billy McFalla9a20e72017-02-15 11:39:12 -05001354 goto done;
Matus Fabian475f0552016-10-19 06:17:52 -07001355 default:
1356 break;
1357 }
1358
Billy McFalla9a20e72017-02-15 11:39:12 -05001359done:
1360 unformat_free (line_input);
1361
1362 return error;
Matus Fabian475f0552016-10-19 06:17:52 -07001363}
1364
1365/*?
1366 * @cliexpar
1367 * @cliexstart{set snat workers}
1368 * Set SNAT workers if 2 or more workers available, use:
1369 * vpp# set snat workers 0-2,5
1370 * @cliexend
1371?*/
1372VLIB_CLI_COMMAND (set_workers_command, static) = {
1373 .path = "set snat workers",
1374 .function = set_workers_command_fn,
1375 .short_help =
1376 "set snat workers <workers-list>",
1377};
1378
1379static clib_error_t *
Matus Fabianeea28d72017-01-13 04:15:54 -08001380snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1381 unformat_input_t * input,
1382 vlib_cli_command_t * cmd)
1383{
1384 unformat_input_t _line_input, *line_input = &_line_input;
1385 u32 domain_id = 0;
1386 u32 src_port = 0;
1387 u8 enable = 1;
1388 int rv = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05001389 clib_error_t *error = 0;
Matus Fabianeea28d72017-01-13 04:15:54 -08001390
1391 /* Get a line of input. */
1392 if (!unformat_user (input, unformat_line_input, line_input))
1393 return 0;
1394
1395 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1396 {
1397 if (unformat (line_input, "domain %d", &domain_id))
1398 ;
1399 else if (unformat (line_input, "src-port %d", &src_port))
1400 ;
1401 else if (unformat (line_input, "disable"))
1402 enable = 0;
1403 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001404 {
1405 error = clib_error_return (0, "unknown input '%U'",
1406 format_unformat_error, line_input);
1407 goto done;
1408 }
Matus Fabianeea28d72017-01-13 04:15:54 -08001409 }
Matus Fabianeea28d72017-01-13 04:15:54 -08001410
1411 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1412
1413 if (rv)
Billy McFalla9a20e72017-02-15 11:39:12 -05001414 {
1415 error = clib_error_return (0, "ipfix logging enable failed");
1416 goto done;
1417 }
Matus Fabianeea28d72017-01-13 04:15:54 -08001418
Billy McFalla9a20e72017-02-15 11:39:12 -05001419done:
1420 unformat_free (line_input);
1421
1422 return error;
Matus Fabianeea28d72017-01-13 04:15:54 -08001423}
1424
1425/*?
1426 * @cliexpar
1427 * @cliexstart{snat ipfix logging}
1428 * To enable SNAT IPFIX logging use:
1429 * vpp# snat ipfix logging
1430 * To set IPFIX exporter use:
1431 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1432 * @cliexend
1433?*/
1434VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1435 .path = "snat ipfix logging",
1436 .function = snat_ipfix_logging_enable_disable_command_fn,
1437 .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1438};
1439
Matus Fabian066f0342017-02-10 03:48:01 -08001440static u32
1441snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1442{
1443 snat_main_t *sm = &snat_main;
1444 snat_user_key_t key0;
1445 clib_bihash_kv_8_8_t kv0, value0;
1446 u32 next_worker_index = 0;
1447
1448 key0.addr = ip0->src_address;
1449 key0.fib_index = rx_fib_index0;
1450
1451 kv0.key = key0.as_u64;
1452
1453 /* Ever heard of of the "user" before? */
1454 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
1455 {
1456 /* No, assign next available worker (RR) */
1457 next_worker_index = sm->first_worker_index;
1458 if (vec_len (sm->workers))
1459 {
1460 next_worker_index +=
1461 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1462 }
1463
1464 /* add non-traslated packets worker lookup */
1465 kv0.value = next_worker_index;
1466 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
1467 }
1468 else
1469 next_worker_index = value0.value;
1470
1471 return next_worker_index;
1472}
1473
1474static u32
1475snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1476{
1477 snat_main_t *sm = &snat_main;
1478 snat_worker_key_t key0;
1479 clib_bihash_kv_8_8_t kv0, value0;
1480 udp_header_t * udp0;
1481 u32 next_worker_index = 0;
1482
1483 udp0 = ip4_next_header (ip0);
1484
1485 key0.addr = ip0->dst_address;
1486 key0.port = udp0->dst_port;
1487 key0.fib_index = rx_fib_index0;
1488
1489 if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
1490 {
1491 icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
1492 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
1493 key0.port = echo0->identifier;
1494 }
1495
1496 kv0.key = key0.as_u64;
1497
1498 /* Ever heard of of the "user" before? */
1499 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1500 {
1501 key0.port = 0;
1502 kv0.key = key0.as_u64;
1503
1504 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1505 {
1506 /* No, assign next available worker (RR) */
1507 next_worker_index = sm->first_worker_index;
1508 if (vec_len (sm->workers))
1509 {
1510 next_worker_index +=
1511 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1512 }
1513 }
1514 else
1515 {
1516 /* Static mapping without port */
1517 next_worker_index = value0.value;
1518 }
1519
1520 /* Add to translated packets worker lookup */
1521 kv0.value = next_worker_index;
1522 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
1523 }
1524 else
1525 next_worker_index = value0.value;
1526
1527 return next_worker_index;
1528}
1529
Matus Fabianeea28d72017-01-13 04:15:54 -08001530static clib_error_t *
Dave Barach20c02cb2016-06-26 10:42:08 -04001531snat_config (vlib_main_t * vm, unformat_input_t * input)
1532{
1533 snat_main_t * sm = &snat_main;
1534 u32 translation_buckets = 1024;
1535 u32 translation_memory_size = 128<<20;
1536 u32 user_buckets = 128;
1537 u32 user_memory_size = 64<<20;
1538 u32 max_translations_per_user = 100;
1539 u32 outside_vrf_id = 0;
Matus Fabiandb649882016-08-26 05:45:27 -07001540 u32 inside_vrf_id = 0;
1541 u32 static_mapping_buckets = 1024;
1542 u32 static_mapping_memory_size = 64<<20;
1543 u8 static_mapping_only = 0;
1544 u8 static_mapping_connection_tracking = 0;
Matus Fabian475f0552016-10-19 06:17:52 -07001545 vlib_thread_main_t *tm = vlib_get_thread_main ();
Dave Barach20c02cb2016-06-26 10:42:08 -04001546
Matus Fabian066f0342017-02-10 03:48:01 -08001547 sm->deterministic = 0;
1548
Dave Barach20c02cb2016-06-26 10:42:08 -04001549 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1550 {
1551 if (unformat (input, "translation hash buckets %d", &translation_buckets))
1552 ;
1553 else if (unformat (input, "translation hash memory %d",
1554 &translation_memory_size));
1555 else if (unformat (input, "user hash buckets %d", &user_buckets))
1556 ;
1557 else if (unformat (input, "user hash memory %d",
1558 &user_memory_size))
1559 ;
1560 else if (unformat (input, "max translations per user %d",
1561 &max_translations_per_user))
1562 ;
1563 else if (unformat (input, "outside VRF id %d",
1564 &outside_vrf_id))
1565 ;
Matus Fabiandb649882016-08-26 05:45:27 -07001566 else if (unformat (input, "inside VRF id %d",
1567 &inside_vrf_id))
1568 ;
1569 else if (unformat (input, "static mapping only"))
1570 {
1571 static_mapping_only = 1;
1572 if (unformat (input, "connection tracking"))
1573 static_mapping_connection_tracking = 1;
1574 }
Matus Fabian066f0342017-02-10 03:48:01 -08001575 else if (unformat (input, "deterministic"))
1576 sm->deterministic = 1;
1577 else
Matus Fabiandb649882016-08-26 05:45:27 -07001578 return clib_error_return (0, "unknown input '%U'",
Dave Barach20c02cb2016-06-26 10:42:08 -04001579 format_unformat_error, input);
1580 }
1581
1582 /* for show commands, etc. */
1583 sm->translation_buckets = translation_buckets;
1584 sm->translation_memory_size = translation_memory_size;
1585 sm->user_buckets = user_buckets;
1586 sm->user_memory_size = user_memory_size;
1587 sm->max_translations_per_user = max_translations_per_user;
1588 sm->outside_vrf_id = outside_vrf_id;
Matus Fabian31c31aa2017-02-05 22:45:57 -08001589 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1590 outside_vrf_id);
Matus Fabiandb649882016-08-26 05:45:27 -07001591 sm->inside_vrf_id = inside_vrf_id;
Matus Fabian31c31aa2017-02-05 22:45:57 -08001592 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1593 inside_vrf_id);
Matus Fabiandb649882016-08-26 05:45:27 -07001594 sm->static_mapping_only = static_mapping_only;
1595 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
Dave Barach20c02cb2016-06-26 10:42:08 -04001596
Matus Fabian066f0342017-02-10 03:48:01 -08001597 if (sm->deterministic)
Matus Fabiandb649882016-08-26 05:45:27 -07001598 {
Matus Fabian066f0342017-02-10 03:48:01 -08001599 sm->in2out_node_index = snat_det_in2out_node.index;
1600 sm->out2in_node_index = snat_det_out2in_node.index;
Juraj Sloboda7a1bde02017-04-03 08:43:58 +02001601 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
1602 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
Matus Fabiandb649882016-08-26 05:45:27 -07001603 }
Matus Fabian066f0342017-02-10 03:48:01 -08001604 else
1605 {
1606 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
1607 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
1608 sm->in2out_node_index = snat_in2out_node.index;
1609 sm->out2in_node_index = snat_out2in_node.index;
1610 if (!static_mapping_only ||
1611 (static_mapping_only && static_mapping_connection_tracking))
1612 {
Juraj Sloboda557a71c2017-02-22 05:16:06 -08001613 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
1614 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
1615
Matus Fabian066f0342017-02-10 03:48:01 -08001616 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
1617 user_memory_size);
Matus Fabiandb649882016-08-26 05:45:27 -07001618
Matus Fabian066f0342017-02-10 03:48:01 -08001619 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
1620 user_memory_size);
1621
1622 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1623
1624 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1625 translation_memory_size);
1626
1627 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1628 translation_memory_size);
1629
1630 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1631 user_memory_size);
Matus Fabian7968e6c2017-07-06 05:37:49 -07001632
1633 clib_bihash_init_16_8 (&sm->in2out_unk_proto, "in2out-unk-proto",
1634 translation_buckets, translation_memory_size);
1635
1636 clib_bihash_init_16_8 (&sm->out2in_unk_proto, "out2in-unk-proto",
1637 translation_buckets, translation_memory_size);
Matus Fabian066f0342017-02-10 03:48:01 -08001638 }
Juraj Sloboda557a71c2017-02-22 05:16:06 -08001639 else
1640 {
1641 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
1642 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
1643 }
Matus Fabian066f0342017-02-10 03:48:01 -08001644 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1645 "static_mapping_by_local", static_mapping_buckets,
1646 static_mapping_memory_size);
1647
1648 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1649 "static_mapping_by_external", static_mapping_buckets,
1650 static_mapping_memory_size);
1651 }
1652
Dave Barach20c02cb2016-06-26 10:42:08 -04001653 return 0;
1654}
1655
1656VLIB_CONFIG_FUNCTION (snat_config, "snat");
1657
Matus Fabian066f0342017-02-10 03:48:01 -08001658u8 * format_snat_session_state (u8 * s, va_list * args)
1659{
1660 u32 i = va_arg (*args, u32);
1661 u8 *t = 0;
1662
1663 switch (i)
1664 {
1665#define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
1666 foreach_snat_session_state
1667#undef _
1668 default:
1669 t = format (t, "unknown");
1670 }
1671 s = format (s, "%s", t);
1672 return s;
1673}
1674
Dave Barach20c02cb2016-06-26 10:42:08 -04001675u8 * format_snat_key (u8 * s, va_list * args)
1676{
1677 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1678 char * protocol_string = "unknown";
1679 static char *protocol_strings[] = {
1680 "UDP",
1681 "TCP",
1682 "ICMP",
1683 };
1684
1685 if (key->protocol < ARRAY_LEN(protocol_strings))
1686 protocol_string = protocol_strings[key->protocol];
1687
1688 s = format (s, "%U proto %s port %d fib %d",
1689 format_ip4_address, &key->addr, protocol_string,
Matus Fabiandb649882016-08-26 05:45:27 -07001690 clib_net_to_host_u16 (key->port), key->fib_index);
Dave Barach20c02cb2016-06-26 10:42:08 -04001691 return s;
1692}
1693
1694u8 * format_snat_session (u8 * s, va_list * args)
1695{
1696 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1697 snat_session_t * sess = va_arg (*args, snat_session_t *);
1698
Matus Fabian7968e6c2017-07-06 05:37:49 -07001699 if (snat_is_unk_proto_session (sess))
1700 {
1701 s = format (s, " i2o %U proto %u fib %u\n",
1702 format_ip4_address, &sess->in2out.addr, sess->in2out.port,
1703 sess->in2out.fib_index);
1704 s = format (s, " o2i %U proto %u fib %u\n",
1705 format_ip4_address, &sess->out2in.addr, sess->out2in.port,
1706 sess->out2in.fib_index);
1707 }
1708 else
1709 {
1710 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
1711 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
1712 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001713 s = format (s, " last heard %.2f\n", sess->last_heard);
1714 s = format (s, " total pkts %d, total bytes %lld\n",
1715 sess->total_pkts, sess->total_bytes);
Matus Fabiandb649882016-08-26 05:45:27 -07001716 if (snat_is_session_static (sess))
1717 s = format (s, " static translation\n");
1718 else
1719 s = format (s, " dynamic translation\n");
Dave Barach20c02cb2016-06-26 10:42:08 -04001720
1721 return s;
1722}
1723
1724u8 * format_snat_user (u8 * s, va_list * args)
1725{
Matus Fabian475f0552016-10-19 06:17:52 -07001726 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
Dave Barach20c02cb2016-06-26 10:42:08 -04001727 snat_user_t * u = va_arg (*args, snat_user_t *);
1728 int verbose = va_arg (*args, int);
1729 dlist_elt_t * head, * elt;
1730 u32 elt_index, head_index;
1731 u32 session_index;
1732 snat_session_t * sess;
1733
Matus Fabiandb649882016-08-26 05:45:27 -07001734 s = format (s, "%U: %d dynamic translations, %d static translations\n",
1735 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
Dave Barach20c02cb2016-06-26 10:42:08 -04001736
1737 if (verbose == 0)
1738 return s;
1739
Matus Fabiandb649882016-08-26 05:45:27 -07001740 if (u->nsessions || u->nstaticsessions)
Dave Barach20c02cb2016-06-26 10:42:08 -04001741 {
Matus Fabiandb649882016-08-26 05:45:27 -07001742 head_index = u->sessions_per_user_list_head_index;
1743 head = pool_elt_at_index (sm->list_pool, head_index);
Dave Barach20c02cb2016-06-26 10:42:08 -04001744
Matus Fabiandb649882016-08-26 05:45:27 -07001745 elt_index = head->next;
Dave Barach20c02cb2016-06-26 10:42:08 -04001746 elt = pool_elt_at_index (sm->list_pool, elt_index);
1747 session_index = elt->value;
Matus Fabiandb649882016-08-26 05:45:27 -07001748
1749 while (session_index != ~0)
1750 {
1751 sess = pool_elt_at_index (sm->sessions, session_index);
1752
1753 s = format (s, " %U\n", format_snat_session, sm, sess);
1754
1755 elt_index = elt->next;
1756 elt = pool_elt_at_index (sm->list_pool, elt_index);
1757 session_index = elt->value;
1758 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001759 }
1760
1761 return s;
1762}
1763
Matus Fabiandb649882016-08-26 05:45:27 -07001764u8 * format_snat_static_mapping (u8 * s, va_list * args)
1765{
1766 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
1767
1768 if (m->addr_only)
1769 s = format (s, "local %U external %U vrf %d",
1770 format_ip4_address, &m->local_addr,
1771 format_ip4_address, &m->external_addr,
1772 m->vrf_id);
1773 else
Matus Fabian09d96f42017-02-02 01:43:00 -08001774 s = format (s, "%U local %U:%d external %U:%d vrf %d",
1775 format_snat_protocol, m->proto,
Matus Fabiandb649882016-08-26 05:45:27 -07001776 format_ip4_address, &m->local_addr, m->local_port,
1777 format_ip4_address, &m->external_addr, m->external_port,
1778 m->vrf_id);
1779
1780 return s;
1781}
1782
Matus Fabiane22e5462017-02-14 23:33:43 -08001783u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
1784{
1785 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
1786 vnet_main_t *vnm = vnet_get_main();
1787
1788 if (m->addr_only)
1789 s = format (s, "local %U external %U vrf %d",
1790 format_ip4_address, &m->l_addr,
1791 format_vnet_sw_interface_name, vnm,
1792 vnet_get_sw_interface (vnm, m->sw_if_index),
1793 m->vrf_id);
1794 else
1795 s = format (s, "%U local %U:%d external %U:%d vrf %d",
1796 format_snat_protocol, m->proto,
1797 format_ip4_address, &m->l_addr, m->l_port,
1798 format_vnet_sw_interface_name, vnm,
1799 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
1800 m->vrf_id);
1801
1802 return s;
1803}
1804
Matus Fabian066f0342017-02-10 03:48:01 -08001805u8 * format_det_map_ses (u8 * s, va_list * args)
1806{
1807 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
1808 ip4_address_t in_addr, out_addr;
1809 u32 in_offset, out_offset;
1810 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
1811 u32 * i = va_arg (*args, u32 *);
1812
1813 u32 user_index = *i / SNAT_DET_SES_PER_USER;
1814 in_addr.as_u32 = clib_host_to_net_u32 (
1815 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
1816 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
1817 clib_net_to_host_u32(det_map->in_addr.as_u32);
1818 out_offset = in_offset / det_map->sharing_ratio;
1819 out_addr.as_u32 = clib_host_to_net_u32(
1820 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
1821 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
1822 format_ip4_address, &in_addr,
1823 clib_net_to_host_u16 (ses->in_port),
1824 format_ip4_address, &out_addr,
1825 clib_net_to_host_u16 (ses->out.out_port),
1826 format_ip4_address, &ses->out.ext_host_addr,
1827 clib_net_to_host_u16 (ses->out.ext_host_port),
1828 format_snat_session_state, ses->state,
1829 ses->expire);
1830
1831 return s;
1832}
1833
Dave Barach20c02cb2016-06-26 10:42:08 -04001834static clib_error_t *
1835show_snat_command_fn (vlib_main_t * vm,
1836 unformat_input_t * input,
1837 vlib_cli_command_t * cmd)
1838{
1839 int verbose = 0;
1840 snat_main_t * sm = &snat_main;
1841 snat_user_t * u;
Matus Fabiandb649882016-08-26 05:45:27 -07001842 snat_static_mapping_t *m;
Matus Fabian588144a2016-10-24 03:30:00 -07001843 snat_interface_t *i;
Matus Fabian07ea7612016-12-15 05:30:37 -08001844 snat_address_t * ap;
Matus Fabian588144a2016-10-24 03:30:00 -07001845 vnet_main_t *vnm = vnet_get_main();
Matus Fabian475f0552016-10-19 06:17:52 -07001846 snat_main_per_thread_data_t *tsm;
Matus Fabian8bf68e82017-01-12 04:24:35 -08001847 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
Matus Fabian475f0552016-10-19 06:17:52 -07001848 uword j = 0;
Matus Fabiane22e5462017-02-14 23:33:43 -08001849 snat_static_map_resolve_t *rp;
Matus Fabian066f0342017-02-10 03:48:01 -08001850 snat_det_map_t * dm;
1851 snat_det_session_t * ses;
Dave Barach20c02cb2016-06-26 10:42:08 -04001852
1853 if (unformat (input, "detail"))
1854 verbose = 1;
1855 else if (unformat (input, "verbose"))
1856 verbose = 2;
1857
Matus Fabiandb649882016-08-26 05:45:27 -07001858 if (sm->static_mapping_only)
Dave Barach20c02cb2016-06-26 10:42:08 -04001859 {
Matus Fabiandb649882016-08-26 05:45:27 -07001860 if (sm->static_mapping_connection_tracking)
1861 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
1862 "tracking");
1863 else
1864 vlib_cli_output (vm, "SNAT mode: static mapping only");
1865 }
Matus Fabian066f0342017-02-10 03:48:01 -08001866 else if (sm->deterministic)
1867 {
1868 vlib_cli_output (vm, "SNAT mode: deterministic mapping");
1869 }
Matus Fabiandb649882016-08-26 05:45:27 -07001870 else
1871 {
1872 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
1873 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001874
Matus Fabian588144a2016-10-24 03:30:00 -07001875 if (verbose > 0)
1876 {
1877 pool_foreach (i, sm->interfaces,
1878 ({
1879 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
1880 vnet_get_sw_interface (vnm, i->sw_if_index),
1881 i->is_inside ? "in" : "out");
1882 }));
Matus Fabian07ea7612016-12-15 05:30:37 -08001883
Matus Fabian8bf68e82017-01-12 04:24:35 -08001884 if (vec_len (sm->auto_add_sw_if_indices))
1885 {
1886 vlib_cli_output (vm, "SNAT pool addresses interfaces:");
1887 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
1888 {
1889 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
1890 vnet_get_sw_interface (vnm, *sw_if_index));
1891 }
1892 }
1893
Matus Fabian07ea7612016-12-15 05:30:37 -08001894 vec_foreach (ap, sm->addresses)
1895 {
Matus Fabian07ea7612016-12-15 05:30:37 -08001896 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001897 if (ap->fib_index != ~0)
1898 vlib_cli_output (vm, " tenant VRF: %u",
1899 ip4_fib_get(ap->fib_index)->table_id);
1900 else
1901 vlib_cli_output (vm, " tenant VRF independent");
Matus Fabian09d96f42017-02-02 01:43:00 -08001902#define _(N, i, n, s) \
1903 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
1904 foreach_snat_protocol
1905#undef _
Matus Fabian07ea7612016-12-15 05:30:37 -08001906 }
Matus Fabian588144a2016-10-24 03:30:00 -07001907 }
1908
Matus Fabian475f0552016-10-19 06:17:52 -07001909 if (sm->num_workers > 1)
1910 {
1911 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
1912 if (verbose > 0)
1913 {
1914 vec_foreach (worker, sm->workers)
1915 {
1916 vlib_worker_thread_t *w =
1917 vlib_worker_threads + *worker + sm->first_worker_index;
Matus Fabianeea28d72017-01-13 04:15:54 -08001918 vlib_cli_output (vm, " %s", w->name);
Matus Fabian475f0552016-10-19 06:17:52 -07001919 }
1920 }
1921 }
1922
Matus Fabian066f0342017-02-10 03:48:01 -08001923 if (sm->deterministic)
Matus Fabiandb649882016-08-26 05:45:27 -07001924 {
Matus Fabian6a0946f2017-04-12 03:36:13 -07001925 vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
1926 vlib_cli_output (vm, "tcp-established timeout: %dsec",
1927 sm->tcp_established_timeout);
1928 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1929 sm->tcp_transitory_timeout);
1930 vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
Matus Fabian066f0342017-02-10 03:48:01 -08001931 vlib_cli_output (vm, "%d deterministic mappings",
1932 pool_elts (sm->det_maps));
Matus Fabiandb649882016-08-26 05:45:27 -07001933 if (verbose > 0)
1934 {
Matus Fabian066f0342017-02-10 03:48:01 -08001935 pool_foreach (dm, sm->det_maps,
Matus Fabiandb649882016-08-26 05:45:27 -07001936 ({
Matus Fabian066f0342017-02-10 03:48:01 -08001937 vlib_cli_output (vm, "in %U/%d out %U/%d\n",
1938 format_ip4_address, &dm->in_addr, dm->in_plen,
1939 format_ip4_address, &dm->out_addr, dm->out_plen);
1940 vlib_cli_output (vm, " outside address sharing ratio: %d\n",
1941 dm->sharing_ratio);
1942 vlib_cli_output (vm, " number of ports per inside host: %d\n",
1943 dm->ports_per_host);
1944 vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
1945 if (verbose > 1)
1946 {
1947 vec_foreach_index (j, dm->sessions)
1948 {
1949 ses = vec_elt_at_index (dm->sessions, j);
1950 if (ses->in_port)
1951 vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses,
1952 &j);
1953 }
1954 }
Matus Fabiandb649882016-08-26 05:45:27 -07001955 }));
1956 }
1957 }
1958 else
1959 {
Matus Fabian066f0342017-02-10 03:48:01 -08001960 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
Matus Fabian475f0552016-10-19 06:17:52 -07001961 {
Matus Fabian066f0342017-02-10 03:48:01 -08001962 vlib_cli_output (vm, "%d static mappings",
1963 pool_elts (sm->static_mappings));
Matus Fabian475f0552016-10-19 06:17:52 -07001964
Matus Fabian066f0342017-02-10 03:48:01 -08001965 if (verbose > 0)
Matus Fabian475f0552016-10-19 06:17:52 -07001966 {
Matus Fabiandb649882016-08-26 05:45:27 -07001967 pool_foreach (m, sm->static_mappings,
1968 ({
1969 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
1970 }));
Matus Fabian066f0342017-02-10 03:48:01 -08001971 }
1972 }
1973 else
1974 {
1975 vec_foreach (tsm, sm->per_thread_data)
1976 {
1977 users_num += pool_elts (tsm->users);
1978 sessions_num += pool_elts (tsm->sessions);
1979 }
1980
1981 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
1982 " %d static mappings",
1983 users_num,
1984 vec_len (sm->addresses),
1985 sessions_num,
1986 pool_elts (sm->static_mappings));
1987
1988 if (verbose > 0)
1989 {
1990 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
1991 verbose - 1);
1992 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
1993 verbose - 1);
1994 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
1995 verbose - 1);
1996 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
1997 verbose - 1);
1998 vec_foreach_index (j, sm->per_thread_data)
Matus Fabiane22e5462017-02-14 23:33:43 -08001999 {
Matus Fabian066f0342017-02-10 03:48:01 -08002000 tsm = vec_elt_at_index (sm->per_thread_data, j);
2001
2002 if (pool_elts (tsm->users) == 0)
2003 continue;
2004
2005 vlib_worker_thread_t *w = vlib_worker_threads + j;
2006 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2007 w->lcore_id);
2008 vlib_cli_output (vm, " %d list pool elements",
2009 pool_elts (tsm->list_pool));
2010
2011 pool_foreach (u, tsm->users,
2012 ({
2013 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2014 verbose - 1);
2015 }));
2016 }
2017
2018 if (pool_elts (sm->static_mappings))
2019 {
2020 vlib_cli_output (vm, "static mappings:");
2021 pool_foreach (m, sm->static_mappings,
2022 ({
2023 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2024 }));
2025 for (j = 0; j < vec_len (sm->to_resolve); j++)
2026 {
2027 rp = sm->to_resolve + j;
2028 vlib_cli_output (vm, "%U",
2029 format_snat_static_map_to_resolve, rp);
2030 }
Matus Fabiane22e5462017-02-14 23:33:43 -08002031 }
Matus Fabiandb649882016-08-26 05:45:27 -07002032 }
2033 }
Dave Barach20c02cb2016-06-26 10:42:08 -04002034 }
Dave Barach20c02cb2016-06-26 10:42:08 -04002035 return 0;
2036}
2037
2038VLIB_CLI_COMMAND (show_snat_command, static) = {
2039 .path = "show snat",
2040 .short_help = "show snat",
2041 .function = show_snat_command_fn,
2042};
Dave Barachcab65ec2017-01-11 13:01:14 -05002043
2044
2045static void
2046snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2047 uword opaque,
2048 u32 sw_if_index,
2049 ip4_address_t * address,
2050 u32 address_length,
2051 u32 if_address_index,
2052 u32 is_delete)
2053{
2054 snat_main_t *sm = &snat_main;
Dave Barach8b275372017-01-16 10:54:02 -05002055 snat_static_map_resolve_t *rp;
2056 u32 *indices_to_delete = 0;
Dave Barachcab65ec2017-01-11 13:01:14 -05002057 int i, j;
Dave Barach8b275372017-01-16 10:54:02 -05002058 int rv;
Dave Barachcab65ec2017-01-11 13:01:14 -05002059
2060 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2061 {
2062 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2063 {
2064 if (!is_delete)
2065 {
2066 /* Don't trip over lease renewal, static config */
2067 for (j = 0; j < vec_len(sm->addresses); j++)
2068 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2069 return;
2070
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002071 snat_add_address (sm, address, ~0);
Dave Barach8b275372017-01-16 10:54:02 -05002072 /* Scan static map resolution vector */
2073 for (j = 0; j < vec_len (sm->to_resolve); j++)
2074 {
2075 rp = sm->to_resolve + j;
2076 /* On this interface? */
2077 if (rp->sw_if_index == sw_if_index)
2078 {
2079 /* Add the static mapping */
2080 rv = snat_add_static_mapping (rp->l_addr,
2081 address[0],
2082 rp->l_port,
2083 rp->e_port,
2084 rp->vrf_id,
2085 rp->addr_only,
2086 ~0 /* sw_if_index */,
Matus Fabian09d96f42017-02-02 01:43:00 -08002087 rp->proto,
Dave Barach8b275372017-01-16 10:54:02 -05002088 rp->is_add);
2089 if (rv)
2090 clib_warning ("snat_add_static_mapping returned %d",
2091 rv);
2092 vec_add1 (indices_to_delete, j);
2093 }
2094 }
2095 /* If we resolved any of the outstanding static mappings */
2096 if (vec_len(indices_to_delete))
2097 {
2098 /* Delete them */
2099 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2100 vec_delete(sm->to_resolve, 1, j);
2101 vec_free(indices_to_delete);
2102 }
Dave Barachcab65ec2017-01-11 13:01:14 -05002103 return;
2104 }
2105 else
2106 {
Matus Fabian36532bd2017-01-23 23:42:28 -08002107 (void) snat_del_address(sm, address[0], 1);
Dave Barachcab65ec2017-01-11 13:01:14 -05002108 return;
2109 }
2110 }
2111 }
2112}
2113
2114
Matus Fabiancfe0fc92017-05-10 06:37:47 -07002115int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del)
Dave Barachcab65ec2017-01-11 13:01:14 -05002116{
2117 ip4_main_t * ip4_main = sm->ip4_main;
2118 ip4_address_t * first_int_addr;
Matus Fabian36532bd2017-01-23 23:42:28 -08002119 snat_static_map_resolve_t *rp;
2120 u32 *indices_to_delete = 0;
2121 int i, j;
Dave Barachcab65ec2017-01-11 13:01:14 -05002122
2123 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2124 0 /* just want the address*/);
2125
2126 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2127 {
2128 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
Matus Fabian8bf68e82017-01-12 04:24:35 -08002129 {
2130 if (is_del)
2131 {
2132 /* if have address remove it */
2133 if (first_int_addr)
Matus Fabian36532bd2017-01-23 23:42:28 -08002134 (void) snat_del_address (sm, first_int_addr[0], 1);
2135 else
2136 {
2137 for (j = 0; j < vec_len (sm->to_resolve); j++)
2138 {
2139 rp = sm->to_resolve + j;
2140 if (rp->sw_if_index == sw_if_index)
2141 vec_add1 (indices_to_delete, j);
2142 }
2143 if (vec_len(indices_to_delete))
2144 {
2145 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2146 vec_del1(sm->to_resolve, j);
2147 vec_free(indices_to_delete);
2148 }
2149 }
Matus Fabian8bf68e82017-01-12 04:24:35 -08002150 vec_del1(sm->auto_add_sw_if_indices, i);
2151 }
2152 else
2153 return VNET_API_ERROR_VALUE_EXIST;
2154
2155 return 0;
2156 }
Dave Barachcab65ec2017-01-11 13:01:14 -05002157 }
2158
Matus Fabian8bf68e82017-01-12 04:24:35 -08002159 if (is_del)
2160 return VNET_API_ERROR_NO_SUCH_ENTRY;
2161
Dave Barachcab65ec2017-01-11 13:01:14 -05002162 /* add to the auto-address list */
2163 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2164
2165 /* If the address is already bound - or static - add it now */
2166 if (first_int_addr)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002167 snat_add_address (sm, first_int_addr, ~0);
Dave Barachcab65ec2017-01-11 13:01:14 -05002168
2169 return 0;
2170}
2171
2172static clib_error_t *
2173snat_add_interface_address_command_fn (vlib_main_t * vm,
2174 unformat_input_t * input,
2175 vlib_cli_command_t * cmd)
2176{
2177 snat_main_t *sm = &snat_main;
2178 unformat_input_t _line_input, *line_input = &_line_input;
2179 u32 sw_if_index;
2180 int rv;
Matus Fabian8bf68e82017-01-12 04:24:35 -08002181 int is_del = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05002182 clib_error_t *error = 0;
Dave Barachcab65ec2017-01-11 13:01:14 -05002183
2184 /* Get a line of input. */
2185 if (!unformat_user (input, unformat_line_input, line_input))
2186 return 0;
2187
2188 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2189 {
2190 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2191 sm->vnet_main, &sw_if_index))
2192 ;
Matus Fabian8bf68e82017-01-12 04:24:35 -08002193 else if (unformat (line_input, "del"))
2194 is_del = 1;
Dave Barachcab65ec2017-01-11 13:01:14 -05002195 else
Billy McFalla9a20e72017-02-15 11:39:12 -05002196 {
2197 error = clib_error_return (0, "unknown input '%U'",
2198 format_unformat_error, line_input);
2199 goto done;
2200 }
Dave Barachcab65ec2017-01-11 13:01:14 -05002201 }
2202
Matus Fabian8bf68e82017-01-12 04:24:35 -08002203 rv = snat_add_interface_address (sm, sw_if_index, is_del);
Dave Barachcab65ec2017-01-11 13:01:14 -05002204
2205 switch (rv)
2206 {
2207 case 0:
2208 break;
2209
2210 default:
Billy McFalla9a20e72017-02-15 11:39:12 -05002211 error = clib_error_return (0, "snat_add_interface_address returned %d",
2212 rv);
2213 goto done;
Dave Barachcab65ec2017-01-11 13:01:14 -05002214 }
Billy McFalla9a20e72017-02-15 11:39:12 -05002215
2216done:
2217 unformat_free (line_input);
2218
2219 return error;
Dave Barachcab65ec2017-01-11 13:01:14 -05002220}
2221
2222VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2223 .path = "snat add interface address",
Matus Fabian8bf68e82017-01-12 04:24:35 -08002224 .short_help = "snat add interface address <interface> [del]",
Dave Barachcab65ec2017-01-11 13:01:14 -05002225 .function = snat_add_interface_address_command_fn,
2226};
Matus Fabian066f0342017-02-10 03:48:01 -08002227
2228static clib_error_t *
2229snat_det_map_command_fn (vlib_main_t * vm,
2230 unformat_input_t * input,
2231 vlib_cli_command_t * cmd)
2232{
2233 snat_main_t *sm = &snat_main;
2234 unformat_input_t _line_input, *line_input = &_line_input;
2235 ip4_address_t in_addr, out_addr;
2236 u32 in_plen, out_plen;
2237 int is_add = 1, rv;
2238 clib_error_t *error = 0;
2239
2240 /* Get a line of input. */
2241 if (!unformat_user (input, unformat_line_input, line_input))
2242 return 0;
2243
2244 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2245 {
2246 if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
2247 ;
2248 else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
2249 ;
2250 else if (unformat (line_input, "del"))
2251 is_add = 0;
2252 else
2253 {
2254 error = clib_error_return (0, "unknown input '%U'",
2255 format_unformat_error, line_input);
2256 goto done;
2257 }
2258 }
2259
2260 unformat_free (line_input);
2261
2262 rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
2263 is_add);
2264
2265 if (rv)
2266 {
2267 error = clib_error_return (0, "snat_det_add_map return %d", rv);
2268 goto done;
2269 }
2270
2271done:
2272 unformat_free (line_input);
2273
2274 return error;
2275}
2276
2277/*?
2278 * @cliexpar
2279 * @cliexstart{snat deterministic add}
2280 * Create bijective mapping of inside address to outside address and port range
2281 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
2282 * CGN deployments.
2283 * To create deterministic mapping between inside network 10.0.0.0/18 and
2284 * outside network 1.1.1.0/30 use:
2285 * # vpp# snat deterministic add in 10.0.0.0/18 out 1.1.1.0/30
2286 * @cliexend
2287?*/
2288VLIB_CLI_COMMAND (snat_det_map_command, static) = {
2289 .path = "snat deterministic add",
2290 .short_help = "snat deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
2291 .function = snat_det_map_command_fn,
2292};
2293
2294static clib_error_t *
2295snat_det_forward_command_fn (vlib_main_t * vm,
2296 unformat_input_t * input,
2297 vlib_cli_command_t * cmd)
2298{
2299 snat_main_t *sm = &snat_main;
2300 unformat_input_t _line_input, *line_input = &_line_input;
2301 ip4_address_t in_addr, out_addr;
2302 u16 lo_port;
2303 snat_det_map_t * dm;
2304 clib_error_t *error = 0;
2305
2306 /* Get a line of input. */
2307 if (!unformat_user (input, unformat_line_input, line_input))
2308 return 0;
2309
2310 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2311 {
2312 if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
2313 ;
2314 else
2315 {
2316 error = clib_error_return (0, "unknown input '%U'",
2317 format_unformat_error, line_input);
2318 goto done;
2319 }
2320 }
2321
2322 unformat_free (line_input);
2323
2324 dm = snat_det_map_by_user(sm, &in_addr);
2325 if (!dm)
2326 vlib_cli_output (vm, "no match");
2327 else
2328 {
2329 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2330 vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
2331 lo_port, lo_port + dm->ports_per_host - 1);
2332 }
2333
2334done:
2335 unformat_free (line_input);
2336
2337 return error;
2338}
2339
2340/*?
2341 * @cliexpar
2342 * @cliexstart{snat deterministic forward}
2343 * Return outside address and port range from inside address for deterministic
2344 * NAT.
2345 * To obtain outside address and port of inside host use:
2346 * vpp# snat deterministic forward 10.0.0.2
2347 * 1.1.1.0:<1054-1068>
2348 * @cliexend
2349?*/
2350VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
2351 .path = "snat deterministic forward",
2352 .short_help = "snat deterministic forward <addr>",
2353 .function = snat_det_forward_command_fn,
2354};
2355
2356static clib_error_t *
2357snat_det_reverse_command_fn (vlib_main_t * vm,
2358 unformat_input_t * input,
2359 vlib_cli_command_t * cmd)
2360{
2361 snat_main_t *sm = &snat_main;
2362 unformat_input_t _line_input, *line_input = &_line_input;
2363 ip4_address_t in_addr, out_addr;
2364 u32 out_port;
2365 snat_det_map_t * dm;
2366 clib_error_t *error = 0;
2367
2368 /* Get a line of input. */
2369 if (!unformat_user (input, unformat_line_input, line_input))
2370 return 0;
2371
2372 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2373 {
2374 if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
2375 ;
2376 else
2377 {
2378 error = clib_error_return (0, "unknown input '%U'",
2379 format_unformat_error, line_input);
2380 }
2381 }
2382
2383 unformat_free (line_input);
2384
2385 if (out_port < 1024 || out_port > 65535)
2386 {
2387 error = clib_error_return (0, "wrong port, must be <1024-65535>");
2388 goto done;
2389 }
2390
2391 dm = snat_det_map_by_out(sm, &out_addr);
2392 if (!dm)
2393 vlib_cli_output (vm, "no match");
2394 else
2395 {
2396 snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
2397 vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
2398 }
2399
2400done:
2401 unformat_free (line_input);
2402
2403 return error;
2404}
2405
2406/*?
2407 * @cliexpar
2408 * @cliexstart{snat deterministic reverse}
2409 * Return inside address from outside address and port for deterministic NAT.
2410 * To obtain inside host address from outside address and port use:
2411 * #vpp snat deterministic reverse 1.1.1.1:1276
2412 * 10.0.16.16
2413 * @cliexend
2414?*/
2415VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
2416 .path = "snat deterministic reverse",
2417 .short_help = "snat deterministic reverse <addr>:<port>",
2418 .function = snat_det_reverse_command_fn,
2419};
Matus Fabian6a0946f2017-04-12 03:36:13 -07002420
2421static clib_error_t *
2422set_timeout_command_fn (vlib_main_t * vm,
2423 unformat_input_t * input,
2424 vlib_cli_command_t * cmd)
2425{
2426 snat_main_t *sm = &snat_main;
2427 unformat_input_t _line_input, *line_input = &_line_input;
2428 clib_error_t *error = 0;
2429
2430 /* Get a line of input. */
2431 if (!unformat_user (input, unformat_line_input, line_input))
2432 return 0;
2433
2434 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2435 {
2436 if (unformat (line_input, "udp %u", &sm->udp_timeout))
2437 ;
2438 else if (unformat (line_input, "tcp-established %u",
2439 &sm->tcp_established_timeout))
2440 ;
2441 else if (unformat (line_input, "tcp-transitory %u",
2442 &sm->tcp_transitory_timeout))
2443 ;
2444 else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
2445 ;
2446 else if (unformat (line_input, "reset"))
2447 {
2448 sm->udp_timeout = SNAT_UDP_TIMEOUT;
2449 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2450 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2451 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2452 }
2453 else
2454 {
2455 error = clib_error_return (0, "unknown input '%U'",
2456 format_unformat_error, line_input);
2457 goto done;
2458 }
2459 }
2460
2461 unformat_free (line_input);
2462
2463done:
2464 unformat_free (line_input);
2465
2466 return error;
2467}
2468
2469/*?
2470 * @cliexpar
2471 * @cliexstart{set snat deterministic timeout}
2472 * Set values of timeouts for deterministic NAT (in seconds), use:
2473 * vpp# set snat deterministic timeout udp 120 tcp-established 7500
2474 * tcp-transitory 250 icmp 90
2475 * To reset default values use:
2476 * vpp# set snat deterministic timeout reset
2477 * @cliexend
2478?*/
2479VLIB_CLI_COMMAND (set_timeout_command, static) = {
2480 .path = "set snat deterministic timeout",
2481 .function = set_timeout_command_fn,
2482 .short_help =
2483 "set snat deterministic timeout [udp <sec> | tcp-established <sec> "
2484 "tcp-transitory <sec> | icmp <sec> | reset]",
2485};
Martin Gálik6bc8c642017-04-19 01:12:27 -07002486
2487static clib_error_t *
2488snat_det_close_session_out_fn (vlib_main_t *vm,
2489 unformat_input_t * input,
2490 vlib_cli_command_t * cmd)
2491{
2492 snat_main_t *sm = &snat_main;
2493 unformat_input_t _line_input, *line_input = &_line_input;
2494 ip4_address_t out_addr, ext_addr, in_addr;
2495 u16 out_port, ext_port;
2496 snat_det_map_t * dm;
2497 snat_det_session_t * ses;
2498 snat_det_out_key_t key;
2499 clib_error_t *error = 0;
2500
2501 /* Get a line of input. */
2502 if (!unformat_user (input, unformat_line_input, line_input))
2503 return 0;
2504
2505 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2506 {
2507 if (unformat (line_input, "%U:%d %U:%d",
2508 unformat_ip4_address, &out_addr, &out_port,
2509 unformat_ip4_address, &ext_addr, &ext_port))
2510 ;
2511 else
2512 {
2513 error = clib_error_return (0, "unknown input '%U'",
2514 format_unformat_error, line_input);
2515 goto done;
2516 }
2517 }
2518
2519 unformat_free (line_input);
2520
2521 dm = snat_det_map_by_out(sm, &out_addr);
2522 if (!dm)
2523 vlib_cli_output (vm, "no match");
2524 else
2525 {
2526 snat_det_reverse(dm, &ext_addr, out_port, &in_addr);
2527 key.ext_host_addr = out_addr;
2528 key.ext_host_port = ntohs(ext_port);
2529 key.out_port = ntohs(out_port);
2530 ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
2531 if (!ses)
2532 vlib_cli_output (vm, "no match");
2533 else
2534 snat_det_ses_close(dm, ses);
2535 }
2536
2537done:
2538 unformat_free (line_input);
2539
2540 return error;
2541}
2542
2543/*?
2544 * @cliexpar
2545 * @cliexstart{snat deterministic close session out}
2546 * Close session using outside ip address and port
2547 * and external ip address and port, use:
2548 * vpp# snat deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
2549 * @cliexend
2550?*/
2551VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
2552 .path = "snat deterministic close session out",
2553 .short_help = "snat deterministic close session out "
2554 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
2555 .function = snat_det_close_session_out_fn,
2556};
2557
2558static clib_error_t *
2559snat_det_close_session_in_fn (vlib_main_t *vm,
2560 unformat_input_t * input,
2561 vlib_cli_command_t * cmd)
2562{
2563 snat_main_t *sm = &snat_main;
2564 unformat_input_t _line_input, *line_input = &_line_input;
2565 ip4_address_t in_addr, ext_addr;
2566 u16 in_port, ext_port;
2567 snat_det_map_t * dm;
2568 snat_det_session_t * ses;
2569 snat_det_out_key_t key;
2570 clib_error_t *error = 0;
2571
2572 /* Get a line of input. */
2573 if (!unformat_user (input, unformat_line_input, line_input))
2574 return 0;
2575
2576 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2577 {
2578 if (unformat (line_input, "%U:%d %U:%d",
2579 unformat_ip4_address, &in_addr, &in_port,
2580 unformat_ip4_address, &ext_addr, &ext_port))
2581 ;
2582 else
2583 {
2584 error = clib_error_return (0, "unknown input '%U'",
2585 format_unformat_error, line_input);
2586 goto done;
2587 }
2588 }
2589
2590 unformat_free (line_input);
2591
2592 dm = snat_det_map_by_user (sm, &in_addr);
2593 if (!dm)
2594 vlib_cli_output (vm, "no match");
2595 else
2596 {
2597 key.ext_host_addr = ext_addr;
2598 key.ext_host_port = ntohs (ext_port);
2599 ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs(in_port), key);
2600 if (!ses)
2601 vlib_cli_output (vm, "no match");
2602 else
2603 snat_det_ses_close(dm, ses);
2604 }
2605
2606done:
2607 unformat_free(line_input);
2608
2609 return error;
2610}
2611
2612/*?
2613 * @cliexpar
2614 * @cliexstart{snat deterministic close_session_in}
2615 * Close session using inside ip address and port
2616 * and external ip address and port, use:
2617 * vpp# snat deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
2618 * @cliexend
2619?*/
2620VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
2621 .path = "snat deterministic close session in",
2622 .short_help = "snat deterministic close session in "
2623 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
2624 .function = snat_det_close_session_in_fn,
2625};