blob: 50ab317759ea9c2c03f908f726d0a1e292eeded7 [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>
Matus Fabian2ba92e32017-08-21 07:05:03 -070022#include <nat/nat.h>
23#include <nat/nat_ipfix_logging.h>
24#include <nat/nat_det.h>
25#include <nat/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",
Matus Fabian2ba92e32017-08-21 07:05:03 -070037 .node_name = "nat44-in2out",
38 .runs_before = VNET_FEATURES ("nat44-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",
Matus Fabian2ba92e32017-08-21 07:05:03 -070042 .node_name = "nat44-out2in",
Damjan Marion8b3191e2016-11-09 19:54:20 +010043 .runs_before = VNET_FEATURES ("ip4-lookup"),
Dave Barach20c02cb2016-06-26 10:42:08 -040044};
Matus Fabian36ea2d62017-10-24 04:13:49 -070045VNET_FEATURE_INIT (ip4_nat_classify, static) = {
46 .arc_name = "ip4-unicast",
47 .node_name = "nat44-classify",
48 .runs_before = VNET_FEATURES ("ip4-lookup"),
49};
Matus Fabian066f0342017-02-10 03:48:01 -080050VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
51 .arc_name = "ip4-unicast",
Matus Fabian2ba92e32017-08-21 07:05:03 -070052 .node_name = "nat44-det-in2out",
53 .runs_before = VNET_FEATURES ("nat44-det-out2in"),
Matus Fabian066f0342017-02-10 03:48:01 -080054};
55VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
56 .arc_name = "ip4-unicast",
Matus Fabian2ba92e32017-08-21 07:05:03 -070057 .node_name = "nat44-det-out2in",
Matus Fabian066f0342017-02-10 03:48:01 -080058 .runs_before = VNET_FEATURES ("ip4-lookup"),
59};
Matus Fabian36ea2d62017-10-24 04:13:49 -070060VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
61 .arc_name = "ip4-unicast",
62 .node_name = "nat44-det-classify",
63 .runs_before = VNET_FEATURES ("ip4-lookup"),
64};
Matus Fabian475f0552016-10-19 06:17:52 -070065VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
66 .arc_name = "ip4-unicast",
Matus Fabian2ba92e32017-08-21 07:05:03 -070067 .node_name = "nat44-in2out-worker-handoff",
68 .runs_before = VNET_FEATURES ("nat44-out2in-worker-handoff"),
Matus Fabian475f0552016-10-19 06:17:52 -070069};
70VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
71 .arc_name = "ip4-unicast",
Matus Fabian2ba92e32017-08-21 07:05:03 -070072 .node_name = "nat44-out2in-worker-handoff",
Matus Fabian475f0552016-10-19 06:17:52 -070073 .runs_before = VNET_FEATURES ("ip4-lookup"),
74};
Matus Fabian36ea2d62017-10-24 04:13:49 -070075VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
76 .arc_name = "ip4-unicast",
77 .node_name = "nat44-handoff-classify",
78 .runs_before = VNET_FEATURES ("ip4-lookup"),
79};
Damjan Marion8b3191e2016-11-09 19:54:20 +010080VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
81 .arc_name = "ip4-unicast",
Matus Fabian2ba92e32017-08-21 07:05:03 -070082 .node_name = "nat44-in2out-fast",
83 .runs_before = VNET_FEATURES ("nat44-out2in-fast"),
Matus Fabiandb649882016-08-26 05:45:27 -070084};
Damjan Marion8b3191e2016-11-09 19:54:20 +010085VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
86 .arc_name = "ip4-unicast",
Matus Fabian2ba92e32017-08-21 07:05:03 -070087 .node_name = "nat44-out2in-fast",
Damjan Marion8b3191e2016-11-09 19:54:20 +010088 .runs_before = VNET_FEATURES ("ip4-lookup"),
Matus Fabiandb649882016-08-26 05:45:27 -070089};
Matus Fabian161c59c2017-07-21 03:46:03 -070090VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
91 .arc_name = "ip4-unicast",
Matus Fabian2ba92e32017-08-21 07:05:03 -070092 .node_name = "nat44-hairpin-dst",
Matus Fabian161c59c2017-07-21 03:46:03 -070093 .runs_before = VNET_FEATURES ("ip4-lookup"),
94};
Matus Fabiandb649882016-08-26 05:45:27 -070095
Matus Fabian93d84c92017-07-19 08:06:01 -070096/* Hook up output features */
97VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
98 .arc_name = "ip4-output",
Matus Fabian2ba92e32017-08-21 07:05:03 -070099 .node_name = "nat44-in2out-output",
Matus Fabian93d84c92017-07-19 08:06:01 -0700100 .runs_before = VNET_FEATURES ("interface-output"),
101};
Matus Fabian93d84c92017-07-19 08:06:01 -0700102VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
103 .arc_name = "ip4-output",
Matus Fabian2ba92e32017-08-21 07:05:03 -0700104 .node_name = "nat44-in2out-output-worker-handoff",
Matus Fabian93d84c92017-07-19 08:06:01 -0700105 .runs_before = VNET_FEATURES ("interface-output"),
106};
Matus Fabian161c59c2017-07-21 03:46:03 -0700107VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
108 .arc_name = "ip4-output",
Matus Fabian2ba92e32017-08-21 07:05:03 -0700109 .node_name = "nat44-hairpin-src",
Matus Fabian161c59c2017-07-21 03:46:03 -0700110 .runs_before = VNET_FEATURES ("interface-output"),
111};
Matus Fabian93d84c92017-07-19 08:06:01 -0700112
Matus Fabian87da4762017-10-04 08:03:56 -0700113/* Hook up ip4-local features */
114VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
115{
116 .arc_name = "ip4-local",
117 .node_name = "nat44-hairpinning",
118 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
119};
120
Matus Fabian93d84c92017-07-19 08:06:01 -0700121
Damjan Marion3b46cba2017-01-23 21:13:45 +0100122/* *INDENT-OFF* */
123VLIB_PLUGIN_REGISTER () = {
124 .version = VPP_BUILD_VER,
Damjan Marion1bfb0dd2017-03-22 11:08:39 +0100125 .description = "Network Address Translation",
Damjan Marion3b46cba2017-01-23 21:13:45 +0100126};
127/* *INDENT-ON* */
Dave Barach20c02cb2016-06-26 10:42:08 -0400128
Matus Fabian36ea2d62017-10-24 04:13:49 -0700129vlib_node_registration_t nat44_classify_node;
130vlib_node_registration_t nat44_det_classify_node;
131vlib_node_registration_t nat44_handoff_classify_node;
132
133typedef enum {
134 NAT44_CLASSIFY_NEXT_IN2OUT,
135 NAT44_CLASSIFY_NEXT_OUT2IN,
136 NAT44_CLASSIFY_N_NEXT,
137} nat44_classify_next_t;
138
139static inline uword
140nat44_classify_node_fn_inline (vlib_main_t * vm,
141 vlib_node_runtime_t * node,
142 vlib_frame_t * frame)
143{
144 u32 n_left_from, * from, * to_next;
145 nat44_classify_next_t next_index;
146 snat_main_t *sm = &snat_main;
147
148 from = vlib_frame_vector_args (frame);
149 n_left_from = frame->n_vectors;
150 next_index = node->cached_next_index;
151
152 while (n_left_from > 0)
153 {
154 u32 n_left_to_next;
155
156 vlib_get_next_frame (vm, node, next_index,
157 to_next, n_left_to_next);
158
159 while (n_left_from > 0 && n_left_to_next > 0)
160 {
161 u32 bi0;
162 vlib_buffer_t *b0;
163 u32 next0 = NAT44_CLASSIFY_NEXT_IN2OUT;
164 ip4_header_t *ip0;
165 snat_address_t *ap;
166 snat_session_key_t m_key0;
167 clib_bihash_kv_8_8_t kv0, value0;
168
169 /* speculatively enqueue b0 to the current next frame */
170 bi0 = from[0];
171 to_next[0] = bi0;
172 from += 1;
173 to_next += 1;
174 n_left_from -= 1;
175 n_left_to_next -= 1;
176
177 b0 = vlib_get_buffer (vm, bi0);
178 ip0 = vlib_buffer_get_current (b0);
179
180 vec_foreach (ap, sm->addresses)
181 {
182 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
183 {
184 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
185 break;
186 }
187 }
188
189 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
190 {
191 m_key0.addr = ip0->dst_address;
192 m_key0.port = 0;
193 m_key0.protocol = 0;
194 m_key0.fib_index = sm->outside_fib_index;
195 kv0.key = m_key0.as_u64;
196 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
197 {
198 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
199 }
200 }
201 /* verify speculative enqueue, maybe switch current next frame */
202 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
203 to_next, n_left_to_next,
204 bi0, next0);
205 }
206
207 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
208 }
209
210 return frame->n_vectors;
211}
212
213static uword
214nat44_classify_node_fn (vlib_main_t * vm,
215 vlib_node_runtime_t * node,
216 vlib_frame_t * frame)
217{
218 return nat44_classify_node_fn_inline (vm, node, frame);
219};
220
221VLIB_REGISTER_NODE (nat44_classify_node) = {
222 .function = nat44_classify_node_fn,
223 .name = "nat44-classify",
224 .vector_size = sizeof (u32),
225 .type = VLIB_NODE_TYPE_INTERNAL,
226 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
227 .next_nodes = {
228 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out",
229 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in",
230 },
231};
232
233VLIB_NODE_FUNCTION_MULTIARCH (nat44_classify_node,
234 nat44_classify_node_fn);
235
236static uword
237nat44_det_classify_node_fn (vlib_main_t * vm,
238 vlib_node_runtime_t * node,
239 vlib_frame_t * frame)
240{
241 return nat44_classify_node_fn_inline (vm, node, frame);
242};
243
244VLIB_REGISTER_NODE (nat44_det_classify_node) = {
245 .function = nat44_det_classify_node_fn,
246 .name = "nat44-det-classify",
247 .vector_size = sizeof (u32),
248 .type = VLIB_NODE_TYPE_INTERNAL,
249 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
250 .next_nodes = {
251 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-det-in2out",
252 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-det-out2in",
253 },
254};
255
256VLIB_NODE_FUNCTION_MULTIARCH (nat44_det_classify_node,
257 nat44_det_classify_node_fn);
258
259static uword
260nat44_handoff_classify_node_fn (vlib_main_t * vm,
261 vlib_node_runtime_t * node,
262 vlib_frame_t * frame)
263{
264 return nat44_classify_node_fn_inline (vm, node, frame);
265};
266
267VLIB_REGISTER_NODE (nat44_handoff_classify_node) = {
268 .function = nat44_handoff_classify_node_fn,
269 .name = "nat44-handoff-classify",
270 .vector_size = sizeof (u32),
271 .type = VLIB_NODE_TYPE_INTERNAL,
272 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
273 .next_nodes = {
274 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out-worker-handoff",
275 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in-worker-handoff",
276 },
277};
278
279VLIB_NODE_FUNCTION_MULTIARCH (nat44_handoff_classify_node,
280 nat44_handoff_classify_node_fn);
281
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800282/**
283 * @brief Add/del NAT address to FIB.
284 *
285 * Add the external NAT address to the FIB as receive entries. This ensures
286 * that VPP will reply to ARP for this address and we don't need to enable
287 * proxy ARP on the outside interface.
288 *
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800289 * @param addr IPv4 address.
Matus Fabian066f0342017-02-10 03:48:01 -0800290 * @param plen address prefix length
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800291 * @param sw_if_index Interface.
292 * @param is_add If 0 delete, otherwise add.
293 */
Matus Fabian066f0342017-02-10 03:48:01 -0800294void
295snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
296 int is_add)
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800297{
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800298 fib_prefix_t prefix = {
Matus Fabian066f0342017-02-10 03:48:01 -0800299 .fp_len = p_len,
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800300 .fp_proto = FIB_PROTOCOL_IP4,
301 .fp_addr = {
302 .ip4.as_u32 = addr->as_u32,
303 },
304 };
305 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
306
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800307 if (is_add)
308 fib_table_entry_update_one_path(fib_index,
309 &prefix,
Matus Fabiandccbee32017-01-31 22:20:30 -0800310 FIB_SOURCE_PLUGIN_HI,
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800311 (FIB_ENTRY_FLAG_CONNECTED |
312 FIB_ENTRY_FLAG_LOCAL |
313 FIB_ENTRY_FLAG_EXCLUSIVE),
Neale Rannsda78f952017-05-24 09:15:43 -0700314 DPO_PROTO_IP4,
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800315 NULL,
316 sw_if_index,
317 ~0,
318 1,
319 NULL,
320 FIB_ROUTE_PATH_FLAG_NONE);
321 else
322 fib_table_entry_delete(fib_index,
323 &prefix,
Matus Fabiandccbee32017-01-31 22:20:30 -0800324 FIB_SOURCE_PLUGIN_HI);
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800325}
326
Juraj Slobodaeab38d92017-03-06 19:55:21 -0800327void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
Dave Barach20c02cb2016-06-26 10:42:08 -0400328{
329 snat_address_t * ap;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800330 snat_interface_t *i;
Matus Fabian624b8d92017-09-12 04:15:30 -0700331 vlib_thread_main_t *tm = vlib_get_thread_main ();
Dave Barach20c02cb2016-06-26 10:42:08 -0400332
Juraj Slobodaeab38d92017-03-06 19:55:21 -0800333 if (vrf_id != ~0)
334 sm->vrf_mode = 1;
335
Matus Fabian860dacc2016-10-25 04:19:26 -0700336 /* Check if address already exists */
337 vec_foreach (ap, sm->addresses)
338 {
339 if (ap->addr.as_u32 == addr->as_u32)
340 return;
341 }
342
Dave Barach20c02cb2016-06-26 10:42:08 -0400343 vec_add2 (sm->addresses, ap, 1);
344 ap->addr = *addr;
Matus Fabianf8d84902017-07-23 23:41:03 -0700345 if (vrf_id != ~0)
346 ap->fib_index =
Neale Ranns15002542017-09-10 04:39:11 -0700347 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
348 FIB_SOURCE_PLUGIN_HI);
Matus Fabianf8d84902017-07-23 23:41:03 -0700349 else
350 ap->fib_index = ~0;
Matus Fabian09d96f42017-02-02 01:43:00 -0800351#define _(N, i, n, s) \
Matus Fabian624b8d92017-09-12 04:15:30 -0700352 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
353 ap->busy_##n##_ports = 0; \
354 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
Matus Fabian09d96f42017-02-02 01:43:00 -0800355 foreach_snat_protocol
356#undef _
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800357
358 /* Add external address to FIB */
359 pool_foreach (i, sm->interfaces,
360 ({
Matus Fabian36ea2d62017-10-24 04:13:49 -0700361 if (nat_interface_is_inside(i))
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800362 continue;
363
Matus Fabian066f0342017-02-10 03:48:01 -0800364 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
Matus Fabiandccbee32017-01-31 22:20:30 -0800365 break;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800366 }));
Matus Fabian93d84c92017-07-19 08:06:01 -0700367 pool_foreach (i, sm->output_feature_interfaces,
368 ({
Matus Fabian36ea2d62017-10-24 04:13:49 -0700369 if (nat_interface_is_inside(i))
Matus Fabian93d84c92017-07-19 08:06:01 -0700370 continue;
371
372 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
373 break;
374 }));
Dave Barach20c02cb2016-06-26 10:42:08 -0400375}
376
Matus Fabian724b8152016-10-04 03:23:43 -0700377static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
378 ip4_address_t addr)
379{
380 snat_static_mapping_t *m;
381 pool_foreach (m, sm->static_mappings,
382 ({
383 if (m->external_addr.as_u32 == addr.as_u32)
384 return 1;
385 }));
386
387 return 0;
388}
389
Matus Fabiancfe0fc92017-05-10 06:37:47 -0700390void increment_v4_address (ip4_address_t * a)
Dave Barach20c02cb2016-06-26 10:42:08 -0400391{
392 u32 v;
Matus Fabian2ba92e32017-08-21 07:05:03 -0700393
Dave Barach20c02cb2016-06-26 10:42:08 -0400394 v = clib_net_to_host_u32(a->as_u32) + 1;
395 a->as_u32 = clib_host_to_net_u32(v);
396}
397
Matus Fabian2ba92e32017-08-21 07:05:03 -0700398static void
399snat_add_static_mapping_when_resolved (snat_main_t * sm,
400 ip4_address_t l_addr,
401 u16 l_port,
402 u32 sw_if_index,
403 u16 e_port,
Dave Barach8b275372017-01-16 10:54:02 -0500404 u32 vrf_id,
Matus Fabian09d96f42017-02-02 01:43:00 -0800405 snat_protocol_t proto,
Matus Fabian2ba92e32017-08-21 07:05:03 -0700406 int addr_only,
Dave Barach8b275372017-01-16 10:54:02 -0500407 int is_add)
408{
409 snat_static_map_resolve_t *rp;
410
411 vec_add2 (sm->to_resolve, rp, 1);
412 rp->l_addr.as_u32 = l_addr.as_u32;
413 rp->l_port = l_port;
414 rp->sw_if_index = sw_if_index;
415 rp->e_port = e_port;
416 rp->vrf_id = vrf_id;
Matus Fabian09d96f42017-02-02 01:43:00 -0800417 rp->proto = proto;
Dave Barach8b275372017-01-16 10:54:02 -0500418 rp->addr_only = addr_only;
419 rp->is_add = is_add;
420}
421
Matus Fabiandb649882016-08-26 05:45:27 -0700422/**
423 * @brief Add static mapping.
424 *
425 * Create static mapping between local addr+port and external addr+port.
426 *
427 * @param l_addr Local IPv4 address.
428 * @param e_addr External IPv4 address.
429 * @param l_port Local port number.
430 * @param e_port External port number.
431 * @param vrf_id VRF ID.
432 * @param addr_only If 0 address port and pair mapping, otherwise address only.
Matus Fabian36532bd2017-01-23 23:42:28 -0800433 * @param sw_if_index External port instead of specific IP address.
Matus Fabiandb649882016-08-26 05:45:27 -0700434 * @param is_add If 0 delete static mapping, otherwise add.
435 *
436 * @returns
437 */
438int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
439 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
Matus Fabian09d96f42017-02-02 01:43:00 -0800440 u32 sw_if_index, snat_protocol_t proto, int is_add)
Matus Fabiandb649882016-08-26 05:45:27 -0700441{
442 snat_main_t * sm = &snat_main;
443 snat_static_mapping_t *m;
Matus Fabian09d96f42017-02-02 01:43:00 -0800444 snat_session_key_t m_key;
Matus Fabiandb649882016-08-26 05:45:27 -0700445 clib_bihash_kv_8_8_t kv, value;
446 snat_address_t *a = 0;
447 u32 fib_index = ~0;
448 uword * p;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800449 snat_interface_t *interface;
Matus Fabiandb649882016-08-26 05:45:27 -0700450 int i;
451
Dave Barach8b275372017-01-16 10:54:02 -0500452 /* If the external address is a specific interface address */
453 if (sw_if_index != ~0)
454 {
455 ip4_address_t * first_int_addr;
456
457 /* Might be already set... */
Matus Fabian2ba92e32017-08-21 07:05:03 -0700458 first_int_addr = ip4_interface_first_address
Dave Barach8b275372017-01-16 10:54:02 -0500459 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
460
461 /* DHCP resolution required? */
462 if (first_int_addr == 0)
463 {
Matus Fabian2ba92e32017-08-21 07:05:03 -0700464 snat_add_static_mapping_when_resolved
Matus Fabian09d96f42017-02-02 01:43:00 -0800465 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
Dave Barach8b275372017-01-16 10:54:02 -0500466 addr_only, is_add);
467 return 0;
468 }
469 else
470 e_addr.as_u32 = first_int_addr->as_u32;
471 }
472
Matus Fabiandb649882016-08-26 05:45:27 -0700473 m_key.addr = e_addr;
474 m_key.port = addr_only ? 0 : e_port;
Matus Fabian09d96f42017-02-02 01:43:00 -0800475 m_key.protocol = addr_only ? 0 : proto;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700476 m_key.fib_index = sm->outside_fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700477 kv.key = m_key.as_u64;
478 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
479 m = 0;
480 else
481 m = pool_elt_at_index (sm->static_mappings, value.value);
482
483 if (is_add)
484 {
485 if (m)
486 return VNET_API_ERROR_VALUE_EXIST;
487
488 /* Convert VRF id to FIB index */
489 if (vrf_id != ~0)
490 {
491 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
492 if (!p)
493 return VNET_API_ERROR_NO_SUCH_FIB;
494 fib_index = p[0];
495 }
496 /* If not specified use inside VRF id from SNAT plugin startup config */
497 else
498 {
Matus Fabian31c31aa2017-02-05 22:45:57 -0800499 fib_index = sm->inside_fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700500 vrf_id = sm->inside_vrf_id;
501 }
502
Matus Fabiandb649882016-08-26 05:45:27 -0700503 /* Find external address in allocated addresses and reserve port for
504 address and port pair mapping when dynamic translations enabled */
505 if (!addr_only && !(sm->static_mapping_only))
506 {
507 for (i = 0; i < vec_len (sm->addresses); i++)
508 {
509 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
510 {
511 a = sm->addresses + i;
512 /* External port must be unused */
Matus Fabian09d96f42017-02-02 01:43:00 -0800513 switch (proto)
514 {
515#define _(N, j, n, s) \
516 case SNAT_PROTOCOL_##N: \
517 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
518 return VNET_API_ERROR_INVALID_VALUE; \
519 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
520 if (e_port > 1024) \
Matus Fabian624b8d92017-09-12 04:15:30 -0700521 { \
522 a->busy_##n##_ports++; \
Matus Fabianed3c1602017-09-21 05:07:12 -0700523 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
Matus Fabian624b8d92017-09-12 04:15:30 -0700524 } \
Matus Fabian09d96f42017-02-02 01:43:00 -0800525 break;
526 foreach_snat_protocol
527#undef _
528 default:
529 clib_warning("unknown_protocol");
530 return VNET_API_ERROR_INVALID_VALUE_2;
531 }
Matus Fabiandb649882016-08-26 05:45:27 -0700532 break;
533 }
534 }
535 /* External address must be allocated */
536 if (!a)
537 return VNET_API_ERROR_NO_SUCH_ENTRY;
538 }
539
540 pool_get (sm->static_mappings, m);
541 memset (m, 0, sizeof (*m));
542 m->local_addr = l_addr;
543 m->external_addr = e_addr;
544 m->addr_only = addr_only;
545 m->vrf_id = vrf_id;
546 m->fib_index = fib_index;
547 if (!addr_only)
548 {
549 m->local_port = l_port;
550 m->external_port = e_port;
Matus Fabian09d96f42017-02-02 01:43:00 -0800551 m->proto = proto;
Matus Fabiandb649882016-08-26 05:45:27 -0700552 }
553
554 m_key.addr = m->local_addr;
555 m_key.port = m->local_port;
Matus Fabian09d96f42017-02-02 01:43:00 -0800556 m_key.protocol = m->proto;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700557 m_key.fib_index = m->fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700558 kv.key = m_key.as_u64;
559 kv.value = m - sm->static_mappings;
560 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
561
562 m_key.addr = m->external_addr;
563 m_key.port = m->external_port;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700564 m_key.fib_index = sm->outside_fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700565 kv.key = m_key.as_u64;
566 kv.value = m - sm->static_mappings;
567 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
Matus Fabian49331682016-12-01 01:32:03 -0800568
Matus Fabian49331682016-12-01 01:32:03 -0800569 if (sm->workers)
570 {
Matus Fabian7865b5c2017-09-26 01:23:01 -0700571 ip4_header_t ip = {
572 .src_address = m->local_addr,
573 };
574 m->worker_index = sm->worker_in2out_cb (&ip, m->fib_index);
Matus Fabian49331682016-12-01 01:32:03 -0800575 }
Matus Fabiandb649882016-08-26 05:45:27 -0700576 }
577 else
578 {
579 if (!m)
580 return VNET_API_ERROR_NO_SUCH_ENTRY;
581
582 /* Free external address port */
583 if (!addr_only && !(sm->static_mapping_only))
584 {
585 for (i = 0; i < vec_len (sm->addresses); i++)
586 {
587 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
588 {
589 a = sm->addresses + i;
Matus Fabian09d96f42017-02-02 01:43:00 -0800590 switch (proto)
591 {
592#define _(N, j, n, s) \
593 case SNAT_PROTOCOL_##N: \
594 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
595 if (e_port > 1024) \
Matus Fabian624b8d92017-09-12 04:15:30 -0700596 { \
597 a->busy_##n##_ports--; \
Matus Fabianed3c1602017-09-21 05:07:12 -0700598 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
Matus Fabian624b8d92017-09-12 04:15:30 -0700599 } \
Matus Fabian09d96f42017-02-02 01:43:00 -0800600 break;
601 foreach_snat_protocol
602#undef _
603 default:
604 clib_warning("unknown_protocol");
605 return VNET_API_ERROR_INVALID_VALUE_2;
606 }
Matus Fabiandb649882016-08-26 05:45:27 -0700607 break;
608 }
609 }
610 }
611
612 m_key.addr = m->local_addr;
613 m_key.port = m->local_port;
Matus Fabian09d96f42017-02-02 01:43:00 -0800614 m_key.protocol = m->proto;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700615 m_key.fib_index = m->fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700616 kv.key = m_key.as_u64;
617 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
618
619 m_key.addr = m->external_addr;
620 m_key.port = m->external_port;
Matus Fabian7e46a4d2016-10-06 04:28:29 -0700621 m_key.fib_index = sm->outside_fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700622 kv.key = m_key.as_u64;
623 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
624
625 /* Delete session(s) for static mapping if exist */
626 if (!(sm->static_mapping_only) ||
627 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
628 {
629 snat_user_key_t u_key;
630 snat_user_t *u;
631 dlist_elt_t * head, * elt;
Matus Fabian475f0552016-10-19 06:17:52 -0700632 u32 elt_index, head_index, del_elt_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700633 u32 ses_index;
Matus Fabian475f0552016-10-19 06:17:52 -0700634 u64 user_index;
Matus Fabiandb649882016-08-26 05:45:27 -0700635 snat_session_t * s;
Matus Fabian475f0552016-10-19 06:17:52 -0700636 snat_main_per_thread_data_t *tsm;
Matus Fabiandb649882016-08-26 05:45:27 -0700637
638 u_key.addr = m->local_addr;
639 u_key.fib_index = m->fib_index;
640 kv.key = u_key.as_u64;
Matus Fabian46ade032017-10-12 04:17:45 -0700641 if (sm->num_workers > 1)
Matus Fabian7865b5c2017-09-26 01:23:01 -0700642 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
Matus Fabian092b3cd2017-09-19 05:42:38 -0700643 else
644 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
645 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
Matus Fabiandb649882016-08-26 05:45:27 -0700646 {
Matus Fabian475f0552016-10-19 06:17:52 -0700647 user_index = value.value;
Matus Fabian475f0552016-10-19 06:17:52 -0700648 u = pool_elt_at_index (tsm->users, user_index);
Matus Fabiandb649882016-08-26 05:45:27 -0700649 if (u->nstaticsessions)
650 {
651 head_index = u->sessions_per_user_list_head_index;
Matus Fabian475f0552016-10-19 06:17:52 -0700652 head = pool_elt_at_index (tsm->list_pool, head_index);
Matus Fabiandb649882016-08-26 05:45:27 -0700653 elt_index = head->next;
Matus Fabian475f0552016-10-19 06:17:52 -0700654 elt = pool_elt_at_index (tsm->list_pool, elt_index);
Matus Fabiandb649882016-08-26 05:45:27 -0700655 ses_index = elt->value;
656 while (ses_index != ~0)
657 {
Matus Fabian475f0552016-10-19 06:17:52 -0700658 s = pool_elt_at_index (tsm->sessions, ses_index);
659 del_elt_index = elt_index;
660 elt_index = elt->next;
661 elt = pool_elt_at_index (tsm->list_pool, elt_index);
662 ses_index = elt->value;
Matus Fabiandb649882016-08-26 05:45:27 -0700663
664 if (!addr_only)
665 {
666 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
667 (clib_net_to_host_u16 (s->out2in.port) != e_port))
668 continue;
669 }
Matus Fabian475f0552016-10-19 06:17:52 -0700670
Matus Fabian7968e6c2017-07-06 05:37:49 -0700671 if (snat_is_unk_proto_session (s))
672 {
673 clib_bihash_kv_16_8_t up_kv;
Matus Fabian704018c2017-09-04 02:17:18 -0700674 nat_ed_ses_key_t up_key;
Matus Fabian7968e6c2017-07-06 05:37:49 -0700675 up_key.l_addr = s->in2out.addr;
676 up_key.r_addr = s->ext_host_addr;
677 up_key.fib_index = s->in2out.fib_index;
678 up_key.proto = s->in2out.port;
Matus Fabian704018c2017-09-04 02:17:18 -0700679 up_key.rsvd = 0;
680 up_key.l_port = 0;
Matus Fabian7968e6c2017-07-06 05:37:49 -0700681 up_kv.key[0] = up_key.as_u64[0];
682 up_kv.key[1] = up_key.as_u64[1];
Matus Fabian704018c2017-09-04 02:17:18 -0700683 if (clib_bihash_add_del_16_8 (&sm->in2out_ed,
Matus Fabian7968e6c2017-07-06 05:37:49 -0700684 &up_kv, 0))
685 clib_warning ("in2out key del failed");
686
687 up_key.l_addr = s->out2in.addr;
688 up_key.fib_index = s->out2in.fib_index;
689 up_kv.key[0] = up_key.as_u64[0];
690 up_kv.key[1] = up_key.as_u64[1];
Matus Fabian704018c2017-09-04 02:17:18 -0700691 if (clib_bihash_add_del_16_8 (&sm->out2in_ed,
Matus Fabian7968e6c2017-07-06 05:37:49 -0700692 &up_kv, 0))
693 clib_warning ("out2in key del failed");
694
695 goto delete;
696 }
Matus Fabianeea28d72017-01-13 04:15:54 -0800697 /* log NAT event */
698 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
699 s->out2in.addr.as_u32,
700 s->in2out.protocol,
701 s->in2out.port,
702 s->out2in.port,
703 s->in2out.fib_index);
704
Matus Fabiandb649882016-08-26 05:45:27 -0700705 value.key = s->in2out.as_u64;
Matus Fabian092b3cd2017-09-19 05:42:38 -0700706 if (clib_bihash_add_del_8_8 (&tsm->in2out, &value, 0))
Matus Fabian7968e6c2017-07-06 05:37:49 -0700707 clib_warning ("in2out key del failed");
Matus Fabiandb649882016-08-26 05:45:27 -0700708 value.key = s->out2in.as_u64;
Matus Fabian092b3cd2017-09-19 05:42:38 -0700709 if (clib_bihash_add_del_8_8 (&tsm->out2in, &value, 0))
Matus Fabian7968e6c2017-07-06 05:37:49 -0700710 clib_warning ("out2in key del failed");
711delete:
Matus Fabian475f0552016-10-19 06:17:52 -0700712 pool_put (tsm->sessions, s);
713
714 clib_dlist_remove (tsm->list_pool, del_elt_index);
715 pool_put_index (tsm->list_pool, del_elt_index);
716 u->nstaticsessions--;
Matus Fabiandb649882016-08-26 05:45:27 -0700717
718 if (!addr_only)
719 break;
Matus Fabiandb649882016-08-26 05:45:27 -0700720 }
721 if (addr_only)
722 {
Matus Fabian475f0552016-10-19 06:17:52 -0700723 pool_put (tsm->users, u);
Matus Fabian092b3cd2017-09-19 05:42:38 -0700724 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
Matus Fabiandb649882016-08-26 05:45:27 -0700725 }
Matus Fabiandb649882016-08-26 05:45:27 -0700726 }
727 }
728 }
729
730 /* Delete static mapping from pool */
731 pool_put (sm->static_mappings, m);
732 }
733
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800734 if (!addr_only)
735 return 0;
736
737 /* Add/delete external address to FIB */
738 pool_foreach (interface, sm->interfaces,
739 ({
Matus Fabian36ea2d62017-10-24 04:13:49 -0700740 if (nat_interface_is_inside(interface))
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800741 continue;
742
Matus Fabian066f0342017-02-10 03:48:01 -0800743 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
Matus Fabiandccbee32017-01-31 22:20:30 -0800744 break;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800745 }));
Matus Fabian93d84c92017-07-19 08:06:01 -0700746 pool_foreach (interface, sm->output_feature_interfaces,
747 ({
Matus Fabian36ea2d62017-10-24 04:13:49 -0700748 if (nat_interface_is_inside(interface))
Matus Fabian93d84c92017-07-19 08:06:01 -0700749 continue;
750
751 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
752 break;
753 }));
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800754
Matus Fabiandb649882016-08-26 05:45:27 -0700755 return 0;
756}
757
Matus Fabian704018c2017-09-04 02:17:18 -0700758int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
759 snat_protocol_t proto, u32 vrf_id,
760 nat44_lb_addr_port_t *locals, u8 is_add)
761{
762 snat_main_t * sm = &snat_main;
763 snat_static_mapping_t *m;
764 snat_session_key_t m_key;
765 clib_bihash_kv_8_8_t kv, value;
766 u32 fib_index;
767 snat_address_t *a = 0;
768 int i;
769 nat44_lb_addr_port_t *local;
Matus Fabian704018c2017-09-04 02:17:18 -0700770 u32 worker_index = 0;
Matus Fabian092b3cd2017-09-19 05:42:38 -0700771 snat_main_per_thread_data_t *tsm;
Matus Fabian704018c2017-09-04 02:17:18 -0700772
773 m_key.addr = e_addr;
774 m_key.port = e_port;
775 m_key.protocol = proto;
776 m_key.fib_index = sm->outside_fib_index;
777 kv.key = m_key.as_u64;
778 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
779 m = 0;
780 else
781 m = pool_elt_at_index (sm->static_mappings, value.value);
782
783 if (is_add)
784 {
785 if (m)
786 return VNET_API_ERROR_VALUE_EXIST;
787
788 if (vec_len (locals) < 2)
789 return VNET_API_ERROR_INVALID_VALUE;
790
791 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
Neale Ranns15002542017-09-10 04:39:11 -0700792 vrf_id,
793 FIB_SOURCE_PLUGIN_HI);
Matus Fabian704018c2017-09-04 02:17:18 -0700794
795 /* Find external address in allocated addresses and reserve port for
796 address and port pair mapping when dynamic translations enabled */
797 if (!sm->static_mapping_only)
798 {
799 for (i = 0; i < vec_len (sm->addresses); i++)
800 {
801 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
802 {
803 a = sm->addresses + i;
804 /* External port must be unused */
805 switch (proto)
806 {
807#define _(N, j, n, s) \
808 case SNAT_PROTOCOL_##N: \
809 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
810 return VNET_API_ERROR_INVALID_VALUE; \
811 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
812 if (e_port > 1024) \
Matus Fabian624b8d92017-09-12 04:15:30 -0700813 { \
814 a->busy_##n##_ports++; \
Matus Fabianed3c1602017-09-21 05:07:12 -0700815 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
Matus Fabian624b8d92017-09-12 04:15:30 -0700816 } \
Matus Fabian704018c2017-09-04 02:17:18 -0700817 break;
818 foreach_snat_protocol
819#undef _
820 default:
821 clib_warning("unknown_protocol");
822 return VNET_API_ERROR_INVALID_VALUE_2;
823 }
824 break;
825 }
826 }
827 /* External address must be allocated */
828 if (!a)
829 return VNET_API_ERROR_NO_SUCH_ENTRY;
830 }
831
832 pool_get (sm->static_mappings, m);
833 memset (m, 0, sizeof (*m));
834 m->external_addr = e_addr;
835 m->addr_only = 0;
836 m->vrf_id = vrf_id;
837 m->fib_index = fib_index;
838 m->external_port = e_port;
839 m->proto = proto;
840
841 m_key.addr = m->external_addr;
842 m_key.port = m->external_port;
843 m_key.protocol = m->proto;
844 m_key.fib_index = sm->outside_fib_index;
845 kv.key = m_key.as_u64;
846 kv.value = m - sm->static_mappings;
847 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1))
848 {
849 clib_warning ("static_mapping_by_external key add failed");
850 return VNET_API_ERROR_UNSPECIFIED;
851 }
Matus Fabian704018c2017-09-04 02:17:18 -0700852
853 /* Assign worker */
854 if (sm->workers)
855 {
Matus Fabian7865b5c2017-09-26 01:23:01 -0700856 worker_index = sm->first_worker_index +
857 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
Matus Fabian092b3cd2017-09-19 05:42:38 -0700858 tsm = vec_elt_at_index (sm->per_thread_data, worker_index);
Matus Fabianed3c1602017-09-21 05:07:12 -0700859 m->worker_index = worker_index;
Matus Fabian092b3cd2017-09-19 05:42:38 -0700860 }
861 else
862 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
863
864 m_key.port = clib_host_to_net_u16 (m->external_port);
865 kv.key = m_key.as_u64;
866 kv.value = ~0ULL;
867 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
868 {
869 clib_warning ("static_mapping_by_local key add failed");
870 return VNET_API_ERROR_UNSPECIFIED;
Matus Fabian704018c2017-09-04 02:17:18 -0700871 }
872
Matus Fabian092b3cd2017-09-19 05:42:38 -0700873 m_key.fib_index = m->fib_index;
Matus Fabian704018c2017-09-04 02:17:18 -0700874 for (i = 0; i < vec_len (locals); i++)
875 {
876 m_key.addr = locals[i].addr;
877 m_key.port = locals[i].port;
878 kv.key = m_key.as_u64;
879 kv.value = m - sm->static_mappings;
880 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
flyingeagle23e8efd7d2017-09-11 16:02:40 +0800881 locals[i].prefix = (i == 0) ? locals[i].probability :\
882 (locals[i - 1].prefix + locals[i].probability);
Matus Fabian704018c2017-09-04 02:17:18 -0700883 vec_add1 (m->locals, locals[i]);
Matus Fabian7865b5c2017-09-26 01:23:01 -0700884
Matus Fabian704018c2017-09-04 02:17:18 -0700885 m_key.port = clib_host_to_net_u16 (locals[i].port);
886 kv.key = m_key.as_u64;
887 kv.value = ~0ULL;
Matus Fabian092b3cd2017-09-19 05:42:38 -0700888 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
Matus Fabian704018c2017-09-04 02:17:18 -0700889 {
890 clib_warning ("in2out key add failed");
891 return VNET_API_ERROR_UNSPECIFIED;
892 }
Matus Fabian704018c2017-09-04 02:17:18 -0700893 }
894 }
895 else
896 {
897 if (!m)
898 return VNET_API_ERROR_NO_SUCH_ENTRY;
899
Neale Ranns15002542017-09-10 04:39:11 -0700900 fib_table_unlock (m->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_HI);
Matus Fabian704018c2017-09-04 02:17:18 -0700901
902 /* Free external address port */
903 if (!sm->static_mapping_only)
904 {
905 for (i = 0; i < vec_len (sm->addresses); i++)
906 {
907 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
908 {
909 a = sm->addresses + i;
910 switch (proto)
911 {
912#define _(N, j, n, s) \
913 case SNAT_PROTOCOL_##N: \
914 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
915 if (e_port > 1024) \
Matus Fabian624b8d92017-09-12 04:15:30 -0700916 { \
917 a->busy_##n##_ports--; \
Matus Fabianed3c1602017-09-21 05:07:12 -0700918 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
Matus Fabian624b8d92017-09-12 04:15:30 -0700919 } \
Matus Fabian704018c2017-09-04 02:17:18 -0700920 break;
921 foreach_snat_protocol
922#undef _
923 default:
924 clib_warning("unknown_protocol");
925 return VNET_API_ERROR_INVALID_VALUE_2;
926 }
927 break;
928 }
929 }
930 }
931
Matus Fabianed3c1602017-09-21 05:07:12 -0700932 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
Matus Fabian704018c2017-09-04 02:17:18 -0700933 m_key.addr = m->external_addr;
934 m_key.port = m->external_port;
935 m_key.protocol = m->proto;
936 m_key.fib_index = sm->outside_fib_index;
937 kv.key = m_key.as_u64;
938 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0))
939 {
940 clib_warning ("static_mapping_by_external key del failed");
941 return VNET_API_ERROR_UNSPECIFIED;
942 }
Matus Fabian7865b5c2017-09-26 01:23:01 -0700943
Matus Fabian704018c2017-09-04 02:17:18 -0700944 m_key.port = clib_host_to_net_u16 (m->external_port);
945 kv.key = m_key.as_u64;
Matus Fabian092b3cd2017-09-19 05:42:38 -0700946 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
Matus Fabian704018c2017-09-04 02:17:18 -0700947 {
948 clib_warning ("outi2in key del failed");
949 return VNET_API_ERROR_UNSPECIFIED;
950 }
951
952 vec_foreach (local, m->locals)
953 {
954 m_key.addr = local->addr;
955 m_key.port = local->port;
956 m_key.fib_index = m->fib_index;
957 kv.key = m_key.as_u64;
958 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
959 {
960 clib_warning ("static_mapping_by_local key del failed");
961 return VNET_API_ERROR_UNSPECIFIED;
962 }
Matus Fabian7865b5c2017-09-26 01:23:01 -0700963
Matus Fabian704018c2017-09-04 02:17:18 -0700964 m_key.port = clib_host_to_net_u16 (local->port);
965 kv.key = m_key.as_u64;
Matus Fabian092b3cd2017-09-19 05:42:38 -0700966 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
Matus Fabian704018c2017-09-04 02:17:18 -0700967 {
968 clib_warning ("in2out key del failed");
969 return VNET_API_ERROR_UNSPECIFIED;
970 }
971 }
flyingeagle2301ffc0c2017-09-13 19:03:56 +0800972 vec_free(m->locals);
Matus Fabian704018c2017-09-04 02:17:18 -0700973
974 pool_put (sm->static_mappings, m);
975 }
976
977 return 0;
978}
979
Matus Fabian36532bd2017-01-23 23:42:28 -0800980int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
981{
982 snat_address_t *a = 0;
983 snat_session_t *ses;
984 u32 *ses_to_be_removed = 0, *ses_index;
985 clib_bihash_kv_8_8_t kv, value;
986 snat_user_key_t user_key;
987 snat_user_t *u;
988 snat_main_per_thread_data_t *tsm;
989 snat_static_mapping_t *m;
Matus Fabiane1ae29a2017-01-27 00:47:58 -0800990 snat_interface_t *interface;
Matus Fabian36532bd2017-01-23 23:42:28 -0800991 int i;
992
993 /* Find SNAT address */
994 for (i=0; i < vec_len (sm->addresses); i++)
995 {
996 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
997 {
998 a = sm->addresses + i;
999 break;
1000 }
1001 }
1002 if (!a)
1003 return VNET_API_ERROR_NO_SUCH_ENTRY;
1004
1005 if (delete_sm)
1006 {
1007 pool_foreach (m, sm->static_mappings,
1008 ({
1009 if (m->external_addr.as_u32 == addr.as_u32)
1010 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1011 m->local_port, m->external_port,
Matus Fabian09d96f42017-02-02 01:43:00 -08001012 m->vrf_id, m->addr_only, ~0,
1013 m->proto, 0);
Matus Fabian36532bd2017-01-23 23:42:28 -08001014 }));
1015 }
1016 else
1017 {
1018 /* Check if address is used in some static mapping */
1019 if (is_snat_address_used_in_static_mapping(sm, addr))
1020 {
1021 clib_warning ("address used in static mapping");
1022 return VNET_API_ERROR_UNSPECIFIED;
1023 }
1024 }
1025
Matus Fabianf8d84902017-07-23 23:41:03 -07001026 if (a->fib_index != ~0)
Neale Ranns15002542017-09-10 04:39:11 -07001027 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4,
1028 FIB_SOURCE_PLUGIN_HI);
Matus Fabianf8d84902017-07-23 23:41:03 -07001029
Matus Fabian36532bd2017-01-23 23:42:28 -08001030 /* Delete sessions using address */
Matus Fabian09d96f42017-02-02 01:43:00 -08001031 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
Matus Fabian36532bd2017-01-23 23:42:28 -08001032 {
1033 vec_foreach (tsm, sm->per_thread_data)
1034 {
1035 pool_foreach (ses, tsm->sessions, ({
1036 if (ses->out2in.addr.as_u32 == addr.as_u32)
1037 {
Matus Fabian7968e6c2017-07-06 05:37:49 -07001038 if (snat_is_unk_proto_session (ses))
1039 {
1040 clib_bihash_kv_16_8_t up_kv;
Matus Fabian704018c2017-09-04 02:17:18 -07001041 nat_ed_ses_key_t up_key;
Matus Fabian7968e6c2017-07-06 05:37:49 -07001042 up_key.l_addr = ses->in2out.addr;
1043 up_key.r_addr = ses->ext_host_addr;
1044 up_key.fib_index = ses->in2out.fib_index;
1045 up_key.proto = ses->in2out.port;
Matus Fabian704018c2017-09-04 02:17:18 -07001046 up_key.rsvd = 0;
1047 up_key.l_port = 0;
Matus Fabian7968e6c2017-07-06 05:37:49 -07001048 up_kv.key[0] = up_key.as_u64[0];
1049 up_kv.key[1] = up_key.as_u64[1];
Matus Fabian704018c2017-09-04 02:17:18 -07001050 if (clib_bihash_add_del_16_8 (&sm->in2out_ed,
Matus Fabian7968e6c2017-07-06 05:37:49 -07001051 &up_kv, 0))
1052 clib_warning ("in2out key del failed");
1053
1054 up_key.l_addr = ses->out2in.addr;
1055 up_key.fib_index = ses->out2in.fib_index;
1056 up_kv.key[0] = up_key.as_u64[0];
1057 up_kv.key[1] = up_key.as_u64[1];
Matus Fabian704018c2017-09-04 02:17:18 -07001058 if (clib_bihash_add_del_16_8 (&sm->out2in_ed,
Matus Fabian7968e6c2017-07-06 05:37:49 -07001059 &up_kv, 0))
1060 clib_warning ("out2in key del failed");
1061 }
1062 else
1063 {
1064 /* log NAT event */
1065 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
1066 ses->out2in.addr.as_u32,
1067 ses->in2out.protocol,
1068 ses->in2out.port,
1069 ses->out2in.port,
1070 ses->in2out.fib_index);
1071 kv.key = ses->in2out.as_u64;
Matus Fabian092b3cd2017-09-19 05:42:38 -07001072 clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0);
Matus Fabian7968e6c2017-07-06 05:37:49 -07001073 kv.key = ses->out2in.as_u64;
Matus Fabian092b3cd2017-09-19 05:42:38 -07001074 clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0);
Matus Fabian7968e6c2017-07-06 05:37:49 -07001075 }
Matus Fabian36532bd2017-01-23 23:42:28 -08001076 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
Matus Fabian36532bd2017-01-23 23:42:28 -08001077 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
1078 user_key.addr = ses->in2out.addr;
1079 user_key.fib_index = ses->in2out.fib_index;
1080 kv.key = user_key.as_u64;
Matus Fabian092b3cd2017-09-19 05:42:38 -07001081 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
Matus Fabian36532bd2017-01-23 23:42:28 -08001082 {
1083 u = pool_elt_at_index (tsm->users, value.value);
1084 u->nsessions--;
1085 }
1086 }
1087 }));
1088
1089 vec_foreach (ses_index, ses_to_be_removed)
1090 pool_put_index (tsm->sessions, ses_index[0]);
1091
1092 vec_free (ses_to_be_removed);
1093 }
1094 }
1095
1096 vec_del1 (sm->addresses, i);
1097
Matus Fabiane1ae29a2017-01-27 00:47:58 -08001098 /* Delete external address from FIB */
1099 pool_foreach (interface, sm->interfaces,
1100 ({
Matus Fabian36ea2d62017-10-24 04:13:49 -07001101 if (nat_interface_is_inside(interface))
Matus Fabiane1ae29a2017-01-27 00:47:58 -08001102 continue;
1103
Matus Fabian066f0342017-02-10 03:48:01 -08001104 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
Matus Fabiandccbee32017-01-31 22:20:30 -08001105 break;
Matus Fabiane1ae29a2017-01-27 00:47:58 -08001106 }));
Matus Fabian93d84c92017-07-19 08:06:01 -07001107 pool_foreach (interface, sm->output_feature_interfaces,
1108 ({
Matus Fabian36ea2d62017-10-24 04:13:49 -07001109 if (nat_interface_is_inside(interface))
Matus Fabian93d84c92017-07-19 08:06:01 -07001110 continue;
1111
1112 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1113 break;
1114 }));
Matus Fabiane1ae29a2017-01-27 00:47:58 -08001115
Matus Fabian36532bd2017-01-23 23:42:28 -08001116 return 0;
1117}
1118
Matus Fabiancfe0fc92017-05-10 06:37:47 -07001119int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
Matus Fabian588144a2016-10-24 03:30:00 -07001120{
1121 snat_main_t *sm = &snat_main;
1122 snat_interface_t *i;
Matus Fabian36ea2d62017-10-24 04:13:49 -07001123 const char * feature_name, *del_feature_name;
Matus Fabiane1ae29a2017-01-27 00:47:58 -08001124 snat_address_t * ap;
1125 snat_static_mapping_t * m;
Matus Fabian066f0342017-02-10 03:48:01 -08001126 snat_det_map_t * dm;
Matus Fabian588144a2016-10-24 03:30:00 -07001127
1128 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
Matus Fabian2ba92e32017-08-21 07:05:03 -07001129 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
Matus Fabian588144a2016-10-24 03:30:00 -07001130 else
Matus Fabian475f0552016-10-19 06:17:52 -07001131 {
Matus Fabian066f0342017-02-10 03:48:01 -08001132 if (sm->num_workers > 1 && !sm->deterministic)
Matus Fabian2ba92e32017-08-21 07:05:03 -07001133 feature_name = is_inside ? "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
Matus Fabian066f0342017-02-10 03:48:01 -08001134 else if (sm->deterministic)
Matus Fabian2ba92e32017-08-21 07:05:03 -07001135 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
Matus Fabian475f0552016-10-19 06:17:52 -07001136 else
Matus Fabian2ba92e32017-08-21 07:05:03 -07001137 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
Matus Fabian475f0552016-10-19 06:17:52 -07001138 }
Matus Fabian588144a2016-10-24 03:30:00 -07001139
Matus Fabian066f0342017-02-10 03:48:01 -08001140 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1141 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
Matus Fabian475f0552016-10-19 06:17:52 -07001142
Matus Fabian066f0342017-02-10 03:48:01 -08001143 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1144 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
Matus Fabian475f0552016-10-19 06:17:52 -07001145
Matus Fabian588144a2016-10-24 03:30:00 -07001146 pool_foreach (i, sm->interfaces,
1147 ({
1148 if (i->sw_if_index == sw_if_index)
1149 {
1150 if (is_del)
Matus Fabian36ea2d62017-10-24 04:13:49 -07001151 {
1152 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1153 {
1154 if (is_inside)
1155 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1156 else
1157 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1158
1159 if (sm->num_workers > 1 && !sm->deterministic)
1160 del_feature_name = "nat44-handoff-classify";
1161 else if (sm->deterministic)
1162 del_feature_name = "nat44-det-classify";
1163 else
1164 del_feature_name = "nat44-classify";
1165
1166 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1167 sw_if_index, 0, 0, 0);
1168 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1169 sw_if_index, 1, 0, 0);
1170 }
1171 else
1172 {
1173 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1174 sw_if_index, 0, 0, 0);
1175 pool_put (sm->interfaces, i);
1176 }
1177 }
Matus Fabian588144a2016-10-24 03:30:00 -07001178 else
Matus Fabian36ea2d62017-10-24 04:13:49 -07001179 {
1180 if ((nat_interface_is_inside(i) && is_inside) ||
1181 (nat_interface_is_outside(i) && !is_inside))
1182 return 0;
1183
1184 if (sm->num_workers > 1 && !sm->deterministic)
1185 {
1186 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1187 "nat44-out2in-worker-handoff";
1188 feature_name = "nat44-handoff-classify";
1189 }
1190 else if (sm->deterministic)
1191 {
1192 del_feature_name = !is_inside ? "nat44-det-in2out" :
1193 "nat44-det-out2in";
1194 feature_name = "nat44-det-classify";
1195 }
1196 else
1197 {
1198 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1199 feature_name = "nat44-classify";
1200 }
1201
1202 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1203 sw_if_index, 0, 0, 0);
1204 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1205 sw_if_index, 1, 0, 0);
1206 goto set_flags;
1207 }
Matus Fabian588144a2016-10-24 03:30:00 -07001208
Matus Fabiane1ae29a2017-01-27 00:47:58 -08001209 goto fib;
Matus Fabian588144a2016-10-24 03:30:00 -07001210 }
1211 }));
1212
1213 if (is_del)
1214 return VNET_API_ERROR_NO_SUCH_ENTRY;
1215
1216 pool_get (sm->interfaces, i);
1217 i->sw_if_index = sw_if_index;
Matus Fabian36ea2d62017-10-24 04:13:49 -07001218 i->flags = 0;
1219 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
1220
1221set_flags:
1222 if (is_inside)
1223 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1224 else
1225 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
Matus Fabian588144a2016-10-24 03:30:00 -07001226
Matus Fabiane1ae29a2017-01-27 00:47:58 -08001227 /* Add/delete external addresses to FIB */
1228fib:
1229 if (is_inside)
Matus Fabian87da4762017-10-04 08:03:56 -07001230 {
1231 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1232 sw_if_index, !is_del, 0, 0);
1233 return 0;
1234 }
Matus Fabiane1ae29a2017-01-27 00:47:58 -08001235
1236 vec_foreach (ap, sm->addresses)
Matus Fabian066f0342017-02-10 03:48:01 -08001237 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
Matus Fabiane1ae29a2017-01-27 00:47:58 -08001238
1239 pool_foreach (m, sm->static_mappings,
1240 ({
1241 if (!(m->addr_only))
1242 continue;
1243
Matus Fabian066f0342017-02-10 03:48:01 -08001244 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1245 }));
1246
1247 pool_foreach (dm, sm->det_maps,
1248 ({
1249 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
Matus Fabiane1ae29a2017-01-27 00:47:58 -08001250 }));
1251
Matus Fabian588144a2016-10-24 03:30:00 -07001252 return 0;
1253}
1254
Matus Fabian93d84c92017-07-19 08:06:01 -07001255int snat_interface_add_del_output_feature (u32 sw_if_index,
1256 u8 is_inside,
1257 int is_del)
1258{
1259 snat_main_t *sm = &snat_main;
1260 snat_interface_t *i;
1261 snat_address_t * ap;
1262 snat_static_mapping_t * m;
1263
1264 if (sm->deterministic ||
1265 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1266 return VNET_API_ERROR_UNSUPPORTED;
1267
1268 if (is_inside)
Matus Fabian161c59c2017-07-21 03:46:03 -07001269 {
Matus Fabian2ba92e32017-08-21 07:05:03 -07001270 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
Matus Fabian161c59c2017-07-21 03:46:03 -07001271 sw_if_index, !is_del, 0, 0);
Matus Fabian2ba92e32017-08-21 07:05:03 -07001272 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
Matus Fabian161c59c2017-07-21 03:46:03 -07001273 sw_if_index, !is_del, 0, 0);
1274 goto fq;
1275 }
Matus Fabian93d84c92017-07-19 08:06:01 -07001276
1277 if (sm->num_workers > 1)
1278 {
Matus Fabian2ba92e32017-08-21 07:05:03 -07001279 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in-worker-handoff",
Matus Fabian93d84c92017-07-19 08:06:01 -07001280 sw_if_index, !is_del, 0, 0);
1281 vnet_feature_enable_disable ("ip4-output",
Matus Fabian2ba92e32017-08-21 07:05:03 -07001282 "nat44-in2out-output-worker-handoff",
Matus Fabian93d84c92017-07-19 08:06:01 -07001283 sw_if_index, !is_del, 0, 0);
1284 }
1285 else
1286 {
Matus Fabian2ba92e32017-08-21 07:05:03 -07001287 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in", sw_if_index,
Matus Fabian93d84c92017-07-19 08:06:01 -07001288 !is_del, 0, 0);
Matus Fabian2ba92e32017-08-21 07:05:03 -07001289 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
Matus Fabian93d84c92017-07-19 08:06:01 -07001290 sw_if_index, !is_del, 0, 0);
1291 }
1292
Matus Fabian161c59c2017-07-21 03:46:03 -07001293fq:
Matus Fabian93d84c92017-07-19 08:06:01 -07001294 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1295 sm->fq_in2out_output_index =
1296 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1297
1298 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1299 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1300
Matus Fabian93d84c92017-07-19 08:06:01 -07001301 pool_foreach (i, sm->output_feature_interfaces,
1302 ({
1303 if (i->sw_if_index == sw_if_index)
1304 {
1305 if (is_del)
1306 pool_put (sm->output_feature_interfaces, i);
1307 else
1308 return VNET_API_ERROR_VALUE_EXIST;
1309
1310 goto fib;
1311 }
1312 }));
1313
1314 if (is_del)
1315 return VNET_API_ERROR_NO_SUCH_ENTRY;
1316
1317 pool_get (sm->output_feature_interfaces, i);
1318 i->sw_if_index = sw_if_index;
Matus Fabian36ea2d62017-10-24 04:13:49 -07001319 i->flags = 0;
1320 if (is_inside)
1321 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1322 else
1323 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
Matus Fabian93d84c92017-07-19 08:06:01 -07001324
1325 /* Add/delete external addresses to FIB */
1326fib:
1327 if (is_inside)
1328 return 0;
1329
1330 vec_foreach (ap, sm->addresses)
1331 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1332
1333 pool_foreach (m, sm->static_mappings,
1334 ({
1335 if (!(m->addr_only))
1336 continue;
1337
1338 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1339 }));
1340
1341 return 0;
1342}
1343
Matus Fabiancfe0fc92017-05-10 06:37:47 -07001344int snat_set_workers (uword * bitmap)
Matus Fabian475f0552016-10-19 06:17:52 -07001345{
1346 snat_main_t *sm = &snat_main;
Matus Fabian7801ca22017-08-03 00:58:05 -07001347 int i, j = 0;
Matus Fabian475f0552016-10-19 06:17:52 -07001348
1349 if (sm->num_workers < 2)
1350 return VNET_API_ERROR_FEATURE_DISABLED;
1351
1352 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1353 return VNET_API_ERROR_INVALID_WORKER;
1354
1355 vec_free (sm->workers);
1356 clib_bitmap_foreach (i, bitmap,
1357 ({
1358 vec_add1(sm->workers, i);
Matus Fabian7801ca22017-08-03 00:58:05 -07001359 sm->per_thread_data[i].snat_thread_index = j;
1360 j++;
Matus Fabian475f0552016-10-19 06:17:52 -07001361 }));
1362
Matus Fabian7801ca22017-08-03 00:58:05 -07001363 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1364 sm->num_snat_thread = _vec_len (sm->workers);
1365
Matus Fabian475f0552016-10-19 06:17:52 -07001366 return 0;
1367}
1368
Dave Barach8b275372017-01-16 10:54:02 -05001369
Dave Barachcab65ec2017-01-11 13:01:14 -05001370static void
1371snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1372 uword opaque,
1373 u32 sw_if_index,
1374 ip4_address_t * address,
1375 u32 address_length,
1376 u32 if_address_index,
1377 u32 is_delete);
1378
Dave Barach20c02cb2016-06-26 10:42:08 -04001379static clib_error_t * snat_init (vlib_main_t * vm)
1380{
1381 snat_main_t * sm = &snat_main;
Matus Fabian08ce4322017-06-19 05:28:27 -07001382 clib_error_t * error = 0;
Dave Barach20c02cb2016-06-26 10:42:08 -04001383 ip4_main_t * im = &ip4_main;
1384 ip_lookup_main_t * lm = &im->lookup_main;
Matus Fabian475f0552016-10-19 06:17:52 -07001385 uword *p;
1386 vlib_thread_registration_t *tr;
1387 vlib_thread_main_t *tm = vlib_get_thread_main ();
1388 uword *bitmap = 0;
1389 u32 i;
Dave Barachcab65ec2017-01-11 13:01:14 -05001390 ip4_add_del_interface_address_callback_t cb4;
Dave Barach20c02cb2016-06-26 10:42:08 -04001391
Dave Barach20c02cb2016-06-26 10:42:08 -04001392 sm->vlib_main = vm;
1393 sm->vnet_main = vnet_get_main();
1394 sm->ip4_main = im;
1395 sm->ip4_lookup_main = lm;
1396 sm->api_main = &api_main;
Matus Fabian475f0552016-10-19 06:17:52 -07001397 sm->first_worker_index = 0;
1398 sm->next_worker = 0;
1399 sm->num_workers = 0;
Matus Fabian7801ca22017-08-03 00:58:05 -07001400 sm->num_snat_thread = 1;
Matus Fabian475f0552016-10-19 06:17:52 -07001401 sm->workers = 0;
Matus Fabian7801ca22017-08-03 00:58:05 -07001402 sm->port_per_thread = 0xffff - 1024;
Matus Fabian475f0552016-10-19 06:17:52 -07001403 sm->fq_in2out_index = ~0;
1404 sm->fq_out2in_index = ~0;
Matus Fabian6a0946f2017-04-12 03:36:13 -07001405 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1406 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1407 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1408 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
Matus Fabian475f0552016-10-19 06:17:52 -07001409
1410 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1411 if (p)
1412 {
1413 tr = (vlib_thread_registration_t *) p[0];
1414 if (tr)
1415 {
1416 sm->num_workers = tr->count;
1417 sm->first_worker_index = tr->first_index;
1418 }
1419 }
1420
Matus Fabian7801ca22017-08-03 00:58:05 -07001421 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1422
Matus Fabian475f0552016-10-19 06:17:52 -07001423 /* Use all available workers by default */
1424 if (sm->num_workers > 1)
1425 {
1426 for (i=0; i < sm->num_workers; i++)
1427 bitmap = clib_bitmap_set (bitmap, i, 1);
1428 snat_set_workers(bitmap);
1429 clib_bitmap_free (bitmap);
1430 }
Matus Fabian7801ca22017-08-03 00:58:05 -07001431 else
1432 {
1433 sm->per_thread_data[0].snat_thread_index = 0;
1434 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001435
Matus Fabiancfe0fc92017-05-10 06:37:47 -07001436 error = snat_api_init(vm, sm);
Matus Fabian08ce4322017-06-19 05:28:27 -07001437 if (error)
1438 return error;
Dave Barach20c02cb2016-06-26 10:42:08 -04001439
Dave Barachcab65ec2017-01-11 13:01:14 -05001440 /* Set up the interface address add/del callback */
1441 cb4.function = snat_ip4_add_del_interface_address_cb;
1442 cb4.function_opaque = 0;
1443
1444 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1445
Matus Fabianeea28d72017-01-13 04:15:54 -08001446 /* Init IPFIX logging */
1447 snat_ipfix_logging_init(vm);
1448
Matus Fabian08ce4322017-06-19 05:28:27 -07001449 error = nat64_init(vm);
Matus Fabian06596c52017-06-06 04:53:28 -07001450
Dave Barach20c02cb2016-06-26 10:42:08 -04001451 return error;
1452}
1453
1454VLIB_INIT_FUNCTION (snat_init);
1455
Matus Fabian2ba92e32017-08-21 07:05:03 -07001456void snat_free_outside_address_and_port (snat_main_t * sm,
Matus Fabian624b8d92017-09-12 04:15:30 -07001457 u32 thread_index,
Matus Fabian2ba92e32017-08-21 07:05:03 -07001458 snat_session_key_t * k,
Dave Barach20c02cb2016-06-26 10:42:08 -04001459 u32 address_index)
1460{
1461 snat_address_t *a;
1462 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
Matus Fabian2ba92e32017-08-21 07:05:03 -07001463
Dave Barach20c02cb2016-06-26 10:42:08 -04001464 ASSERT (address_index < vec_len (sm->addresses));
1465
1466 a = sm->addresses + address_index;
1467
Matus Fabian09d96f42017-02-02 01:43:00 -08001468 switch (k->protocol)
1469 {
1470#define _(N, i, n, s) \
1471 case SNAT_PROTOCOL_##N: \
1472 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1473 port_host_byte_order) == 1); \
1474 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1475 port_host_byte_order, 0); \
1476 a->busy_##n##_ports--; \
Matus Fabian624b8d92017-09-12 04:15:30 -07001477 a->busy_##n##_ports_per_thread[thread_index]--; \
Matus Fabian09d96f42017-02-02 01:43:00 -08001478 break;
1479 foreach_snat_protocol
1480#undef _
1481 default:
1482 clib_warning("unknown_protocol");
1483 return;
1484 }
Matus Fabian2ba92e32017-08-21 07:05:03 -07001485}
Dave Barach20c02cb2016-06-26 10:42:08 -04001486
Matus Fabiandb649882016-08-26 05:45:27 -07001487/**
Matus Fabian2ba92e32017-08-21 07:05:03 -07001488 * @brief Match NAT44 static mapping.
Matus Fabiandb649882016-08-26 05:45:27 -07001489 *
Matus Fabian2ba92e32017-08-21 07:05:03 -07001490 * @param sm NAT main.
Matus Fabiandb649882016-08-26 05:45:27 -07001491 * @param match Address and port to match.
1492 * @param mapping External or local address and port of the matched mapping.
1493 * @param by_external If 0 match by local address otherwise match by external
1494 * address.
Juraj Slobodad3677682017-04-14 03:24:45 +02001495 * @param is_addr_only If matched mapping is address only
Matus Fabiandb649882016-08-26 05:45:27 -07001496 *
1497 * @returns 0 if match found otherwise 1.
1498 */
1499int snat_static_mapping_match (snat_main_t * sm,
1500 snat_session_key_t match,
1501 snat_session_key_t * mapping,
Juraj Slobodad3677682017-04-14 03:24:45 +02001502 u8 by_external,
1503 u8 *is_addr_only)
Matus Fabiandb649882016-08-26 05:45:27 -07001504{
1505 clib_bihash_kv_8_8_t kv, value;
1506 snat_static_mapping_t *m;
Matus Fabian09d96f42017-02-02 01:43:00 -08001507 snat_session_key_t m_key;
Matus Fabiandb649882016-08-26 05:45:27 -07001508 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
Matus Fabian704018c2017-09-04 02:17:18 -07001509 u32 rand, lo = 0, hi, mid;
Matus Fabiandb649882016-08-26 05:45:27 -07001510
1511 if (by_external)
1512 mapping_hash = &sm->static_mapping_by_external;
1513
1514 m_key.addr = match.addr;
1515 m_key.port = clib_net_to_host_u16 (match.port);
Matus Fabian09d96f42017-02-02 01:43:00 -08001516 m_key.protocol = match.protocol;
Matus Fabian7e46a4d2016-10-06 04:28:29 -07001517 m_key.fib_index = match.fib_index;
Matus Fabiandb649882016-08-26 05:45:27 -07001518
1519 kv.key = m_key.as_u64;
1520
1521 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1522 {
1523 /* Try address only mapping */
1524 m_key.port = 0;
Matus Fabian09d96f42017-02-02 01:43:00 -08001525 m_key.protocol = 0;
Matus Fabiandb649882016-08-26 05:45:27 -07001526 kv.key = m_key.as_u64;
1527 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1528 return 1;
1529 }
1530
1531 m = pool_elt_at_index (sm->static_mappings, value.value);
1532
1533 if (by_external)
1534 {
Matus Fabian704018c2017-09-04 02:17:18 -07001535 if (vec_len (m->locals))
1536 {
1537 hi = vec_len (m->locals) - 1;
1538 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
1539 while (lo < hi)
1540 {
flyingeagle236a58f4a2017-09-12 15:10:46 +08001541 mid = ((hi - lo) >> 1) + lo;
Matus Fabian704018c2017-09-04 02:17:18 -07001542 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
1543 }
1544 if (!(m->locals[lo].prefix >= rand))
1545 return 1;
1546 mapping->addr = m->locals[lo].addr;
1547 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
1548 }
1549 else
1550 {
1551 mapping->addr = m->local_addr;
1552 /* Address only mapping doesn't change port */
1553 mapping->port = m->addr_only ? match.port
1554 : clib_host_to_net_u16 (m->local_port);
1555 }
Matus Fabiandb649882016-08-26 05:45:27 -07001556 mapping->fib_index = m->fib_index;
Matus Fabian704018c2017-09-04 02:17:18 -07001557 mapping->protocol = m->proto;
Matus Fabiandb649882016-08-26 05:45:27 -07001558 }
1559 else
1560 {
1561 mapping->addr = m->external_addr;
1562 /* Address only mapping doesn't change port */
1563 mapping->port = m->addr_only ? match.port
1564 : clib_host_to_net_u16 (m->external_port);
1565 mapping->fib_index = sm->outside_fib_index;
1566 }
1567
Juraj Slobodad3677682017-04-14 03:24:45 +02001568 if (PREDICT_FALSE(is_addr_only != 0))
1569 *is_addr_only = m->addr_only;
1570
Matus Fabiandb649882016-08-26 05:45:27 -07001571 return 0;
1572}
1573
Matus Fabian7801ca22017-08-03 00:58:05 -07001574static_always_inline u16
1575snat_random_port (snat_main_t * sm, u16 min, u16 max)
1576{
1577 return min + random_u32 (&sm->random_seed) /
1578 (random_u32_max() / (max - min + 1) + 1);
1579}
1580
1581int snat_alloc_outside_address_and_port (snat_main_t * sm,
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001582 u32 fib_index,
Matus Fabian7801ca22017-08-03 00:58:05 -07001583 u32 thread_index,
Dave Barach20c02cb2016-06-26 10:42:08 -04001584 snat_session_key_t * k,
1585 u32 * address_indexp)
1586{
1587 int i;
1588 snat_address_t *a;
1589 u32 portnum;
1590
1591 for (i = 0; i < vec_len (sm->addresses); i++)
1592 {
Matus Fabian09d96f42017-02-02 01:43:00 -08001593 a = sm->addresses + i;
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001594 if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
1595 continue;
Matus Fabian09d96f42017-02-02 01:43:00 -08001596 switch (k->protocol)
Dave Barach20c02cb2016-06-26 10:42:08 -04001597 {
Matus Fabian09d96f42017-02-02 01:43:00 -08001598#define _(N, j, n, s) \
1599 case SNAT_PROTOCOL_##N: \
Matus Fabian624b8d92017-09-12 04:15:30 -07001600 if (a->busy_##n##_ports_per_thread[thread_index] < sm->port_per_thread) \
Matus Fabian09d96f42017-02-02 01:43:00 -08001601 { \
1602 while (1) \
1603 { \
Matus Fabian7801ca22017-08-03 00:58:05 -07001604 portnum = (sm->port_per_thread * \
1605 sm->per_thread_data[thread_index].snat_thread_index) + \
Matus Fabiandaf51622017-09-13 23:00:53 -07001606 snat_random_port(sm, 1, sm->port_per_thread) + 1024; \
Matus Fabian09d96f42017-02-02 01:43:00 -08001607 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1608 continue; \
1609 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
Matus Fabian624b8d92017-09-12 04:15:30 -07001610 a->busy_##n##_ports_per_thread[thread_index]++; \
Matus Fabian09d96f42017-02-02 01:43:00 -08001611 a->busy_##n##_ports++; \
1612 k->addr = a->addr; \
1613 k->port = clib_host_to_net_u16(portnum); \
1614 *address_indexp = i; \
1615 return 0; \
1616 } \
1617 } \
1618 break;
1619 foreach_snat_protocol
1620#undef _
1621 default:
1622 clib_warning("unknown protocol");
1623 return 1;
Dave Barach20c02cb2016-06-26 10:42:08 -04001624 }
Matus Fabian09d96f42017-02-02 01:43:00 -08001625
Dave Barach20c02cb2016-06-26 10:42:08 -04001626 }
1627 /* Totally out of translations to use... */
Matus Fabianeea28d72017-01-13 04:15:54 -08001628 snat_ipfix_logging_addresses_exhausted(0);
Dave Barach20c02cb2016-06-26 10:42:08 -04001629 return 1;
1630}
1631
1632
1633static clib_error_t *
1634add_address_command_fn (vlib_main_t * vm,
1635 unformat_input_t * input,
1636 vlib_cli_command_t * cmd)
1637{
Matus Fabiandb649882016-08-26 05:45:27 -07001638 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barach20c02cb2016-06-26 10:42:08 -04001639 snat_main_t * sm = &snat_main;
1640 ip4_address_t start_addr, end_addr, this_addr;
1641 u32 start_host_order, end_host_order;
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001642 u32 vrf_id = ~0;
Dave Barach20c02cb2016-06-26 10:42:08 -04001643 int i, count;
Matus Fabian724b8152016-10-04 03:23:43 -07001644 int is_add = 1;
1645 int rv = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05001646 clib_error_t *error = 0;
Dave Barach20c02cb2016-06-26 10:42:08 -04001647
Matus Fabiandb649882016-08-26 05:45:27 -07001648 /* Get a line of input. */
1649 if (!unformat_user (input, unformat_line_input, line_input))
1650 return 0;
1651
Matus Fabian724b8152016-10-04 03:23:43 -07001652 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1653 {
1654 if (unformat (line_input, "%U - %U",
1655 unformat_ip4_address, &start_addr,
1656 unformat_ip4_address, &end_addr))
1657 ;
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001658 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
1659 ;
Matus Fabian724b8152016-10-04 03:23:43 -07001660 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1661 end_addr = start_addr;
1662 else if (unformat (line_input, "del"))
1663 is_add = 0;
1664 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001665 {
1666 error = clib_error_return (0, "unknown input '%U'",
1667 format_unformat_error, line_input);
1668 goto done;
1669 }
Matus Fabian724b8152016-10-04 03:23:43 -07001670 }
Matus Fabiandb649882016-08-26 05:45:27 -07001671
1672 if (sm->static_mapping_only)
Billy McFalla9a20e72017-02-15 11:39:12 -05001673 {
1674 error = clib_error_return (0, "static mapping only mode");
1675 goto done;
1676 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001677
1678 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1679 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
Matus Fabian2ba92e32017-08-21 07:05:03 -07001680
Dave Barach20c02cb2016-06-26 10:42:08 -04001681 if (end_host_order < start_host_order)
Billy McFalla9a20e72017-02-15 11:39:12 -05001682 {
1683 error = clib_error_return (0, "end address less than start address");
1684 goto done;
1685 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001686
1687 count = (end_host_order - start_host_order) + 1;
1688
1689 if (count > 1024)
1690 clib_warning ("%U - %U, %d addresses...",
1691 format_ip4_address, &start_addr,
1692 format_ip4_address, &end_addr,
1693 count);
Matus Fabian2ba92e32017-08-21 07:05:03 -07001694
Dave Barach20c02cb2016-06-26 10:42:08 -04001695 this_addr = start_addr;
1696
1697 for (i = 0; i < count; i++)
1698 {
Matus Fabian724b8152016-10-04 03:23:43 -07001699 if (is_add)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001700 snat_add_address (sm, &this_addr, vrf_id);
Matus Fabian724b8152016-10-04 03:23:43 -07001701 else
Matus Fabian36532bd2017-01-23 23:42:28 -08001702 rv = snat_del_address (sm, this_addr, 0);
Matus Fabian724b8152016-10-04 03:23:43 -07001703
1704 switch (rv)
1705 {
1706 case VNET_API_ERROR_NO_SUCH_ENTRY:
Billy McFalla9a20e72017-02-15 11:39:12 -05001707 error = clib_error_return (0, "S-NAT address not exist.");
1708 goto done;
Matus Fabian724b8152016-10-04 03:23:43 -07001709 case VNET_API_ERROR_UNSPECIFIED:
Billy McFalla9a20e72017-02-15 11:39:12 -05001710 error = clib_error_return (0, "S-NAT address used in static mapping.");
1711 goto done;
Matus Fabian724b8152016-10-04 03:23:43 -07001712 default:
1713 break;
1714 }
1715
Dave Barach20c02cb2016-06-26 10:42:08 -04001716 increment_v4_address (&this_addr);
1717 }
1718
Billy McFalla9a20e72017-02-15 11:39:12 -05001719done:
1720 unformat_free (line_input);
1721
1722 return error;
Dave Barach20c02cb2016-06-26 10:42:08 -04001723}
1724
1725VLIB_CLI_COMMAND (add_address_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07001726 .path = "nat44 add address",
Hongjun Ni2bd3f8a2017-08-29 20:39:42 +08001727 .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
Juraj Slobodaeab38d92017-03-06 19:55:21 -08001728 "[tenant-vrf <vrf-id>] [del]",
Dave Barach20c02cb2016-06-26 10:42:08 -04001729 .function = add_address_command_fn,
1730};
1731
1732static clib_error_t *
1733snat_feature_command_fn (vlib_main_t * vm,
1734 unformat_input_t * input,
1735 vlib_cli_command_t * cmd)
1736{
Matus Fabiandb649882016-08-26 05:45:27 -07001737 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barach20c02cb2016-06-26 10:42:08 -04001738 vnet_main_t * vnm = vnet_get_main();
Dave Barach20c02cb2016-06-26 10:42:08 -04001739 clib_error_t * error = 0;
Matus Fabian588144a2016-10-24 03:30:00 -07001740 u32 sw_if_index;
Dave Barach20c02cb2016-06-26 10:42:08 -04001741 u32 * inside_sw_if_indices = 0;
1742 u32 * outside_sw_if_indices = 0;
Matus Fabian93d84c92017-07-19 08:06:01 -07001743 u8 is_output_feature = 0;
Dave Barach20c02cb2016-06-26 10:42:08 -04001744 int is_del = 0;
1745 int i;
1746
1747 sw_if_index = ~0;
1748
Matus Fabiandb649882016-08-26 05:45:27 -07001749 /* Get a line of input. */
1750 if (!unformat_user (input, unformat_line_input, line_input))
1751 return 0;
1752
1753 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Dave Barach20c02cb2016-06-26 10:42:08 -04001754 {
Matus Fabiandb649882016-08-26 05:45:27 -07001755 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
Dave Barach20c02cb2016-06-26 10:42:08 -04001756 vnm, &sw_if_index))
1757 vec_add1 (inside_sw_if_indices, sw_if_index);
Matus Fabiandb649882016-08-26 05:45:27 -07001758 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
Dave Barach20c02cb2016-06-26 10:42:08 -04001759 vnm, &sw_if_index))
1760 vec_add1 (outside_sw_if_indices, sw_if_index);
Matus Fabian93d84c92017-07-19 08:06:01 -07001761 else if (unformat (line_input, "output-feature"))
1762 is_output_feature = 1;
Matus Fabiandb649882016-08-26 05:45:27 -07001763 else if (unformat (line_input, "del"))
Dave Barach20c02cb2016-06-26 10:42:08 -04001764 is_del = 1;
1765 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001766 {
1767 error = clib_error_return (0, "unknown input '%U'",
1768 format_unformat_error, line_input);
1769 goto done;
1770 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001771 }
1772
1773 if (vec_len (inside_sw_if_indices))
1774 {
Dave Barach20c02cb2016-06-26 10:42:08 -04001775 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1776 {
1777 sw_if_index = inside_sw_if_indices[i];
Matus Fabian93d84c92017-07-19 08:06:01 -07001778 if (is_output_feature)
1779 {
1780 if (snat_interface_add_del_output_feature (sw_if_index, 1, is_del))
1781 {
1782 error = clib_error_return (0, "%s %U failed",
1783 is_del ? "del" : "add",
1784 format_vnet_sw_interface_name, vnm,
1785 vnet_get_sw_interface (vnm,
1786 sw_if_index));
1787 goto done;
1788 }
1789 }
1790 else
1791 {
1792 if (snat_interface_add_del (sw_if_index, 1, is_del))
1793 {
1794 error = clib_error_return (0, "%s %U failed",
1795 is_del ? "del" : "add",
1796 format_vnet_sw_interface_name, vnm,
1797 vnet_get_sw_interface (vnm,
1798 sw_if_index));
1799 goto done;
1800 }
1801 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001802 }
1803 }
1804
1805 if (vec_len (outside_sw_if_indices))
1806 {
Dave Barach20c02cb2016-06-26 10:42:08 -04001807 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1808 {
1809 sw_if_index = outside_sw_if_indices[i];
Matus Fabian93d84c92017-07-19 08:06:01 -07001810 if (is_output_feature)
1811 {
1812 if (snat_interface_add_del_output_feature (sw_if_index, 0, is_del))
1813 {
1814 error = clib_error_return (0, "%s %U failed",
1815 is_del ? "del" : "add",
1816 format_vnet_sw_interface_name, vnm,
1817 vnet_get_sw_interface (vnm,
1818 sw_if_index));
1819 goto done;
1820 }
1821 }
1822 else
1823 {
1824 if (snat_interface_add_del (sw_if_index, 0, is_del))
1825 {
1826 error = clib_error_return (0, "%s %U failed",
1827 is_del ? "del" : "add",
1828 format_vnet_sw_interface_name, vnm,
1829 vnet_get_sw_interface (vnm,
1830 sw_if_index));
1831 goto done;
1832 }
1833 }
Dave Barach20c02cb2016-06-26 10:42:08 -04001834 }
1835 }
1836
Billy McFalla9a20e72017-02-15 11:39:12 -05001837done:
1838 unformat_free (line_input);
Dave Barach20c02cb2016-06-26 10:42:08 -04001839 vec_free (inside_sw_if_indices);
1840 vec_free (outside_sw_if_indices);
1841
1842 return error;
1843}
1844
1845VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07001846 .path = "set interface nat44",
Dave Barach20c02cb2016-06-26 10:42:08 -04001847 .function = snat_feature_command_fn,
Matus Fabian2ba92e32017-08-21 07:05:03 -07001848 .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
Matus Fabian93d84c92017-07-19 08:06:01 -07001849 "[del]",
Dave Barach20c02cb2016-06-26 10:42:08 -04001850};
1851
Matus Fabian09d96f42017-02-02 01:43:00 -08001852uword
1853unformat_snat_protocol (unformat_input_t * input, va_list * args)
1854{
1855 u32 *r = va_arg (*args, u32 *);
1856
1857 if (0);
1858#define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
1859 foreach_snat_protocol
1860#undef _
1861 else
1862 return 0;
1863 return 1;
1864}
1865
1866u8 *
1867format_snat_protocol (u8 * s, va_list * args)
1868{
1869 u32 i = va_arg (*args, u32);
1870 u8 *t = 0;
1871
1872 switch (i)
1873 {
1874#define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
1875 foreach_snat_protocol
1876#undef _
1877 default:
1878 s = format (s, "unknown");
Matus Fabianf8cd5812017-07-11 03:55:02 -07001879 return s;
Matus Fabian09d96f42017-02-02 01:43:00 -08001880 }
1881 s = format (s, "%s", t);
1882 return s;
1883}
1884
Dave Barach20c02cb2016-06-26 10:42:08 -04001885static clib_error_t *
Matus Fabiandb649882016-08-26 05:45:27 -07001886add_static_mapping_command_fn (vlib_main_t * vm,
1887 unformat_input_t * input,
1888 vlib_cli_command_t * cmd)
1889{
1890 unformat_input_t _line_input, *line_input = &_line_input;
1891 clib_error_t * error = 0;
1892 ip4_address_t l_addr, e_addr;
1893 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1894 int is_add = 1;
1895 int addr_only = 1;
Dave Barach8b275372017-01-16 10:54:02 -05001896 u32 sw_if_index = ~0;
1897 vnet_main_t * vnm = vnet_get_main();
Matus Fabiandb649882016-08-26 05:45:27 -07001898 int rv;
Matus Fabian09d96f42017-02-02 01:43:00 -08001899 snat_protocol_t proto;
Matus Fabianb449f482017-02-05 22:14:41 -08001900 u8 proto_set = 0;
Matus Fabiandb649882016-08-26 05:45:27 -07001901
1902 /* Get a line of input. */
1903 if (!unformat_user (input, unformat_line_input, line_input))
1904 return 0;
1905
1906 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1907 {
1908 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1909 &l_port))
1910 addr_only = 0;
1911 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1912 ;
1913 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1914 &e_addr, &e_port))
1915 addr_only = 0;
1916 else if (unformat (line_input, "external %U", unformat_ip4_address,
1917 &e_addr))
1918 ;
Dave Barach8b275372017-01-16 10:54:02 -05001919 else if (unformat (line_input, "external %U %u",
1920 unformat_vnet_sw_interface, vnm, &sw_if_index,
1921 &e_port))
1922 addr_only = 0;
1923
1924 else if (unformat (line_input, "external %U",
1925 unformat_vnet_sw_interface, vnm, &sw_if_index))
1926 ;
Matus Fabiandb649882016-08-26 05:45:27 -07001927 else if (unformat (line_input, "vrf %u", &vrf_id))
1928 ;
Matus Fabian09d96f42017-02-02 01:43:00 -08001929 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
Matus Fabianb449f482017-02-05 22:14:41 -08001930 proto_set = 1;
Matus Fabiandb649882016-08-26 05:45:27 -07001931 else if (unformat (line_input, "del"))
1932 is_add = 0;
1933 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001934 {
1935 error = clib_error_return (0, "unknown input: '%U'",
1936 format_unformat_error, line_input);
1937 goto done;
1938 }
Matus Fabiandb649882016-08-26 05:45:27 -07001939 }
Matus Fabiandb649882016-08-26 05:45:27 -07001940
Matus Fabianb449f482017-02-05 22:14:41 -08001941 if (!addr_only && !proto_set)
Billy McFalla9a20e72017-02-15 11:39:12 -05001942 {
1943 error = clib_error_return (0, "missing protocol");
1944 goto done;
1945 }
Matus Fabianb449f482017-02-05 22:14:41 -08001946
Matus Fabiandb649882016-08-26 05:45:27 -07001947 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
Matus Fabian09d96f42017-02-02 01:43:00 -08001948 vrf_id, addr_only, sw_if_index, proto, is_add);
Matus Fabiandb649882016-08-26 05:45:27 -07001949
1950 switch (rv)
1951 {
1952 case VNET_API_ERROR_INVALID_VALUE:
Billy McFalla9a20e72017-02-15 11:39:12 -05001953 error = clib_error_return (0, "External port already in use.");
1954 goto done;
Matus Fabiandb649882016-08-26 05:45:27 -07001955 case VNET_API_ERROR_NO_SUCH_ENTRY:
1956 if (is_add)
Billy McFalla9a20e72017-02-15 11:39:12 -05001957 error = clib_error_return (0, "External addres must be allocated.");
Matus Fabiandb649882016-08-26 05:45:27 -07001958 else
Billy McFalla9a20e72017-02-15 11:39:12 -05001959 error = clib_error_return (0, "Mapping not exist.");
1960 goto done;
Matus Fabiandb649882016-08-26 05:45:27 -07001961 case VNET_API_ERROR_NO_SUCH_FIB:
Billy McFalla9a20e72017-02-15 11:39:12 -05001962 error = clib_error_return (0, "No such VRF id.");
1963 goto done;
Matus Fabiandb649882016-08-26 05:45:27 -07001964 case VNET_API_ERROR_VALUE_EXIST:
Billy McFalla9a20e72017-02-15 11:39:12 -05001965 error = clib_error_return (0, "Mapping already exist.");
1966 goto done;
Matus Fabiandb649882016-08-26 05:45:27 -07001967 default:
1968 break;
1969 }
1970
Billy McFalla9a20e72017-02-15 11:39:12 -05001971done:
1972 unformat_free (line_input);
1973
Matus Fabiandb649882016-08-26 05:45:27 -07001974 return error;
1975}
1976
1977/*?
1978 * @cliexpar
1979 * @cliexstart{snat add static mapping}
1980 * Static mapping allows hosts on the external network to initiate connection
1981 * to to the local network host.
1982 * To create static mapping between local host address 10.0.0.3 port 6303 and
Matus Fabianb449f482017-02-05 22:14:41 -08001983 * external address 4.4.4.4 port 3606 for TCP protocol use:
Hongjun Ni2bd3f8a2017-08-29 20:39:42 +08001984 * vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
Matus Fabian2ba92e32017-08-21 07:05:03 -07001985 * If not runnig "static mapping only" NAT plugin mode use before:
1986 * vpp# nat44 add address 4.4.4.4
Matus Fabiandb649882016-08-26 05:45:27 -07001987 * To create static mapping between local and external address use:
Matus Fabian2ba92e32017-08-21 07:05:03 -07001988 * vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
Matus Fabiandb649882016-08-26 05:45:27 -07001989 * @cliexend
1990?*/
1991VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07001992 .path = "nat44 add static mapping",
Matus Fabiandb649882016-08-26 05:45:27 -07001993 .function = add_static_mapping_command_fn,
1994 .short_help =
Hongjun Ni2bd3f8a2017-08-29 20:39:42 +08001995 "nat44 add static mapping tcp|udp|icmp local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
Matus Fabiandb649882016-08-26 05:45:27 -07001996};
1997
1998static clib_error_t *
Matus Fabian704018c2017-09-04 02:17:18 -07001999add_lb_static_mapping_command_fn (vlib_main_t * vm,
2000 unformat_input_t * input,
2001 vlib_cli_command_t * cmd)
2002{
2003 unformat_input_t _line_input, *line_input = &_line_input;
2004 clib_error_t * error = 0;
2005 ip4_address_t l_addr, e_addr;
2006 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
2007 int is_add = 1;
2008 int rv;
2009 snat_protocol_t proto;
2010 u8 proto_set = 0;
2011 nat44_lb_addr_port_t *locals = 0, local;
2012
2013 /* Get a line of input. */
2014 if (!unformat_user (input, unformat_line_input, line_input))
2015 return 0;
2016
2017 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2018 {
2019 if (unformat (line_input, "local %U:%u probability %u",
2020 unformat_ip4_address, &l_addr, &l_port, &probability))
2021 {
2022 memset (&local, 0, sizeof (local));
2023 local.addr = l_addr;
2024 local.port = (u16) l_port;
2025 local.probability = (u8) probability;
2026 vec_add1 (locals, local);
2027 }
2028 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
2029 &e_addr, &e_port))
2030 ;
2031 else if (unformat (line_input, "vrf %u", &vrf_id))
2032 ;
2033 else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
2034 &proto))
2035 proto_set = 1;
2036 else if (unformat (line_input, "del"))
2037 is_add = 0;
2038 else
2039 {
2040 error = clib_error_return (0, "unknown input: '%U'",
2041 format_unformat_error, line_input);
2042 goto done;
2043 }
2044 }
2045
2046 if (vec_len (locals) < 2)
2047 {
2048 error = clib_error_return (0, "at least two local must be set");
2049 goto done;
2050 }
2051
2052 if (!proto_set)
2053 {
2054 error = clib_error_return (0, "missing protocol");
2055 goto done;
2056 }
2057
2058 rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, vrf_id,
2059 locals, is_add);
2060
2061 switch (rv)
2062 {
2063 case VNET_API_ERROR_INVALID_VALUE:
2064 error = clib_error_return (0, "External port already in use.");
2065 goto done;
2066 case VNET_API_ERROR_NO_SUCH_ENTRY:
2067 if (is_add)
2068 error = clib_error_return (0, "External addres must be allocated.");
2069 else
2070 error = clib_error_return (0, "Mapping not exist.");
2071 goto done;
2072 case VNET_API_ERROR_VALUE_EXIST:
2073 error = clib_error_return (0, "Mapping already exist.");
2074 goto done;
2075 default:
2076 break;
2077 }
2078
2079done:
2080 unformat_free (line_input);
2081 vec_free (locals);
2082
2083 return error;
2084}
2085
2086VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
2087 .path = "nat44 add load-balancing static mapping",
2088 .function = add_lb_static_mapping_command_fn,
2089 .short_help =
2090 "nat44 add load-balancing static mapping protocol tcp|udp external <addr>:<port> local <addr>:<port> probability <n> [vrf <table-id>] [del]",
2091};
2092
2093static clib_error_t *
Matus Fabian475f0552016-10-19 06:17:52 -07002094set_workers_command_fn (vlib_main_t * vm,
2095 unformat_input_t * input,
2096 vlib_cli_command_t * cmd)
2097{
2098 unformat_input_t _line_input, *line_input = &_line_input;
2099 uword *bitmap = 0;
2100 int rv = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05002101 clib_error_t *error = 0;
Matus Fabian475f0552016-10-19 06:17:52 -07002102
2103 /* Get a line of input. */
2104 if (!unformat_user (input, unformat_line_input, line_input))
2105 return 0;
2106
2107 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2108 {
2109 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
2110 ;
2111 else
Billy McFalla9a20e72017-02-15 11:39:12 -05002112 {
2113 error = clib_error_return (0, "unknown input '%U'",
2114 format_unformat_error, line_input);
2115 goto done;
2116 }
Matus Fabian475f0552016-10-19 06:17:52 -07002117 }
Matus Fabian475f0552016-10-19 06:17:52 -07002118
2119 if (bitmap == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -05002120 {
2121 error = clib_error_return (0, "List of workers must be specified.");
2122 goto done;
2123 }
Matus Fabian475f0552016-10-19 06:17:52 -07002124
2125 rv = snat_set_workers(bitmap);
2126
2127 clib_bitmap_free (bitmap);
2128
2129 switch (rv)
2130 {
2131 case VNET_API_ERROR_INVALID_WORKER:
Billy McFalla9a20e72017-02-15 11:39:12 -05002132 error = clib_error_return (0, "Invalid worker(s).");
2133 goto done;
Matus Fabian475f0552016-10-19 06:17:52 -07002134 case VNET_API_ERROR_FEATURE_DISABLED:
Billy McFalla9a20e72017-02-15 11:39:12 -05002135 error = clib_error_return (0,
Matus Fabian475f0552016-10-19 06:17:52 -07002136 "Supported only if 2 or more workes available.");
Billy McFalla9a20e72017-02-15 11:39:12 -05002137 goto done;
Matus Fabian475f0552016-10-19 06:17:52 -07002138 default:
2139 break;
2140 }
2141
Billy McFalla9a20e72017-02-15 11:39:12 -05002142done:
2143 unformat_free (line_input);
2144
2145 return error;
Matus Fabian475f0552016-10-19 06:17:52 -07002146}
2147
2148/*?
2149 * @cliexpar
2150 * @cliexstart{set snat workers}
Matus Fabian2ba92e32017-08-21 07:05:03 -07002151 * Set NAT workers if 2 or more workers available, use:
Matus Fabian475f0552016-10-19 06:17:52 -07002152 * vpp# set snat workers 0-2,5
2153 * @cliexend
2154?*/
2155VLIB_CLI_COMMAND (set_workers_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07002156 .path = "set nat workers",
Matus Fabian475f0552016-10-19 06:17:52 -07002157 .function = set_workers_command_fn,
2158 .short_help =
Matus Fabian2ba92e32017-08-21 07:05:03 -07002159 "set nat workers <workers-list>",
Matus Fabian475f0552016-10-19 06:17:52 -07002160};
2161
2162static clib_error_t *
Matus Fabianeea28d72017-01-13 04:15:54 -08002163snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
2164 unformat_input_t * input,
2165 vlib_cli_command_t * cmd)
2166{
2167 unformat_input_t _line_input, *line_input = &_line_input;
2168 u32 domain_id = 0;
2169 u32 src_port = 0;
2170 u8 enable = 1;
2171 int rv = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05002172 clib_error_t *error = 0;
Matus Fabianeea28d72017-01-13 04:15:54 -08002173
2174 /* Get a line of input. */
2175 if (!unformat_user (input, unformat_line_input, line_input))
2176 return 0;
2177
2178 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2179 {
2180 if (unformat (line_input, "domain %d", &domain_id))
2181 ;
2182 else if (unformat (line_input, "src-port %d", &src_port))
2183 ;
2184 else if (unformat (line_input, "disable"))
2185 enable = 0;
2186 else
Billy McFalla9a20e72017-02-15 11:39:12 -05002187 {
2188 error = clib_error_return (0, "unknown input '%U'",
2189 format_unformat_error, line_input);
2190 goto done;
2191 }
Matus Fabianeea28d72017-01-13 04:15:54 -08002192 }
Matus Fabianeea28d72017-01-13 04:15:54 -08002193
2194 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
2195
2196 if (rv)
Billy McFalla9a20e72017-02-15 11:39:12 -05002197 {
2198 error = clib_error_return (0, "ipfix logging enable failed");
2199 goto done;
2200 }
Matus Fabianeea28d72017-01-13 04:15:54 -08002201
Billy McFalla9a20e72017-02-15 11:39:12 -05002202done:
2203 unformat_free (line_input);
2204
2205 return error;
Matus Fabianeea28d72017-01-13 04:15:54 -08002206}
2207
2208/*?
2209 * @cliexpar
2210 * @cliexstart{snat ipfix logging}
Matus Fabian2ba92e32017-08-21 07:05:03 -07002211 * To enable NAT IPFIX logging use:
2212 * vpp# nat ipfix logging
Matus Fabianeea28d72017-01-13 04:15:54 -08002213 * To set IPFIX exporter use:
2214 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
2215 * @cliexend
2216?*/
2217VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07002218 .path = "nat ipfix logging",
Matus Fabianeea28d72017-01-13 04:15:54 -08002219 .function = snat_ipfix_logging_enable_disable_command_fn,
Matus Fabian2ba92e32017-08-21 07:05:03 -07002220 .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
Matus Fabianeea28d72017-01-13 04:15:54 -08002221};
2222
Matus Fabian066f0342017-02-10 03:48:01 -08002223static u32
2224snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2225{
2226 snat_main_t *sm = &snat_main;
Matus Fabian066f0342017-02-10 03:48:01 -08002227 u32 next_worker_index = 0;
Matus Fabian7865b5c2017-09-26 01:23:01 -07002228 u32 hash;
Matus Fabian066f0342017-02-10 03:48:01 -08002229
Matus Fabian7865b5c2017-09-26 01:23:01 -07002230 next_worker_index = sm->first_worker_index;
2231 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2232 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >>24);
Matus Fabian066f0342017-02-10 03:48:01 -08002233
Matus Fabian7865b5c2017-09-26 01:23:01 -07002234 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2235 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
Matus Fabian066f0342017-02-10 03:48:01 -08002236 else
Matus Fabian7865b5c2017-09-26 01:23:01 -07002237 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
Matus Fabian066f0342017-02-10 03:48:01 -08002238
2239 return next_worker_index;
2240}
2241
2242static u32
2243snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2244{
2245 snat_main_t *sm = &snat_main;
Matus Fabianed3c1602017-09-21 05:07:12 -07002246 udp_header_t *udp;
2247 u16 port;
2248 snat_session_key_t m_key;
2249 clib_bihash_kv_8_8_t kv, value;
2250 snat_static_mapping_t *m;
2251 nat_ed_ses_key_t key;
2252 clib_bihash_kv_16_8_t s_kv, s_value;
2253 snat_main_per_thread_data_t *tsm;
2254 snat_session_t *s;
2255 int i;
2256 u32 proto;
Matus Fabian066f0342017-02-10 03:48:01 -08002257
Matus Fabianed3c1602017-09-21 05:07:12 -07002258 /* first try static mappings without port */
2259 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
Matus Fabian066f0342017-02-10 03:48:01 -08002260 {
Matus Fabianed3c1602017-09-21 05:07:12 -07002261 m_key.addr = ip0->dst_address;
2262 m_key.port = 0;
2263 m_key.protocol = 0;
2264 m_key.fib_index = rx_fib_index0;
2265 kv.key = m_key.as_u64;
2266 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2267 {
2268 m = pool_elt_at_index (sm->static_mappings, value.value);
2269 return m->worker_index;
2270 }
Matus Fabian066f0342017-02-10 03:48:01 -08002271 }
2272
Matus Fabianed3c1602017-09-21 05:07:12 -07002273 proto = ip_proto_to_snat_proto (ip0->protocol);
2274 udp = ip4_next_header (ip0);
2275 port = udp->dst_port;
Matus Fabian066f0342017-02-10 03:48:01 -08002276
Matus Fabianed3c1602017-09-21 05:07:12 -07002277 /* unknown protocol */
2278 if (PREDICT_FALSE (proto == ~0))
Matus Fabian066f0342017-02-10 03:48:01 -08002279 {
Matus Fabianed3c1602017-09-21 05:07:12 -07002280 key.l_addr = ip0->dst_address;
2281 key.r_addr = ip0->src_address;
2282 key.fib_index = rx_fib_index0;
2283 key.proto = ip0->protocol;
2284 key.rsvd = 0;
2285 key.l_port = 0;
2286 s_kv.key[0] = key.as_u64[0];
2287 s_kv.key[1] = key.as_u64[1];
Matus Fabian066f0342017-02-10 03:48:01 -08002288
Matus Fabianed3c1602017-09-21 05:07:12 -07002289 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
Matus Fabian066f0342017-02-10 03:48:01 -08002290 {
Matus Fabianed3c1602017-09-21 05:07:12 -07002291 for (i = 0; i < _vec_len (sm->per_thread_data); i++)
Matus Fabian066f0342017-02-10 03:48:01 -08002292 {
Matus Fabianed3c1602017-09-21 05:07:12 -07002293 tsm = vec_elt_at_index (sm->per_thread_data, i);
2294 if (!pool_is_free_index(tsm->sessions, s_value.value))
2295 {
2296 s = pool_elt_at_index (tsm->sessions, s_value.value);
2297 if (s->out2in.addr.as_u32 == ip0->dst_address.as_u32 &&
2298 s->out2in.port == ip0->protocol &&
2299 snat_is_unk_proto_session (s))
2300 return i;
2301 }
Matus Fabian066f0342017-02-10 03:48:01 -08002302 }
Matus Fabianed3c1602017-09-21 05:07:12 -07002303 }
2304
2305 /* if no session use current thread */
2306 return vlib_get_thread_index ();
2307 }
2308
2309 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2310 {
2311 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2312 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2313 if (!icmp_is_error_message (icmp))
2314 port = echo->identifier;
Matus Fabian066f0342017-02-10 03:48:01 -08002315 else
2316 {
Matus Fabianed3c1602017-09-21 05:07:12 -07002317 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2318 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2319 void *l4_header = ip4_next_header (inner_ip);
2320 switch (proto)
2321 {
2322 case SNAT_PROTOCOL_ICMP:
2323 icmp = (icmp46_header_t*)l4_header;
2324 echo = (icmp_echo_header_t *)(icmp + 1);
2325 port = echo->identifier;
2326 break;
2327 case SNAT_PROTOCOL_UDP:
2328 case SNAT_PROTOCOL_TCP:
2329 port = ((tcp_udp_header_t*)l4_header)->src_port;
2330 break;
2331 default:
2332 return vlib_get_thread_index ();
2333 }
Matus Fabian066f0342017-02-10 03:48:01 -08002334 }
Matus Fabian066f0342017-02-10 03:48:01 -08002335 }
Matus Fabian066f0342017-02-10 03:48:01 -08002336
Matus Fabianed3c1602017-09-21 05:07:12 -07002337 /* try static mappings with port */
2338 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2339 {
2340 m_key.addr = ip0->dst_address;
2341 m_key.port = clib_net_to_host_u16 (port);
2342 m_key.protocol = proto;
2343 m_key.fib_index = rx_fib_index0;
2344 kv.key = m_key.as_u64;
2345 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2346 {
2347 m = pool_elt_at_index (sm->static_mappings, value.value);
2348 return m->worker_index;
2349 }
2350 }
2351
2352 /* worker by outside port */
2353 return (u32) ((clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread);
Matus Fabian066f0342017-02-10 03:48:01 -08002354}
2355
Matus Fabianeea28d72017-01-13 04:15:54 -08002356static clib_error_t *
Dave Barach20c02cb2016-06-26 10:42:08 -04002357snat_config (vlib_main_t * vm, unformat_input_t * input)
2358{
2359 snat_main_t * sm = &snat_main;
2360 u32 translation_buckets = 1024;
2361 u32 translation_memory_size = 128<<20;
2362 u32 user_buckets = 128;
2363 u32 user_memory_size = 64<<20;
2364 u32 max_translations_per_user = 100;
2365 u32 outside_vrf_id = 0;
Matus Fabiandb649882016-08-26 05:45:27 -07002366 u32 inside_vrf_id = 0;
2367 u32 static_mapping_buckets = 1024;
2368 u32 static_mapping_memory_size = 64<<20;
2369 u8 static_mapping_only = 0;
2370 u8 static_mapping_connection_tracking = 0;
Matus Fabian092b3cd2017-09-19 05:42:38 -07002371 snat_main_per_thread_data_t *tsm;
Dave Barach20c02cb2016-06-26 10:42:08 -04002372
Matus Fabian066f0342017-02-10 03:48:01 -08002373 sm->deterministic = 0;
2374
Dave Barach20c02cb2016-06-26 10:42:08 -04002375 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2376 {
2377 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2378 ;
2379 else if (unformat (input, "translation hash memory %d",
2380 &translation_memory_size));
2381 else if (unformat (input, "user hash buckets %d", &user_buckets))
2382 ;
2383 else if (unformat (input, "user hash memory %d",
2384 &user_memory_size))
2385 ;
2386 else if (unformat (input, "max translations per user %d",
2387 &max_translations_per_user))
2388 ;
2389 else if (unformat (input, "outside VRF id %d",
2390 &outside_vrf_id))
2391 ;
Matus Fabiandb649882016-08-26 05:45:27 -07002392 else if (unformat (input, "inside VRF id %d",
2393 &inside_vrf_id))
2394 ;
2395 else if (unformat (input, "static mapping only"))
2396 {
2397 static_mapping_only = 1;
2398 if (unformat (input, "connection tracking"))
2399 static_mapping_connection_tracking = 1;
2400 }
Matus Fabian066f0342017-02-10 03:48:01 -08002401 else if (unformat (input, "deterministic"))
2402 sm->deterministic = 1;
2403 else
Matus Fabiandb649882016-08-26 05:45:27 -07002404 return clib_error_return (0, "unknown input '%U'",
Dave Barach20c02cb2016-06-26 10:42:08 -04002405 format_unformat_error, input);
2406 }
2407
2408 /* for show commands, etc. */
2409 sm->translation_buckets = translation_buckets;
2410 sm->translation_memory_size = translation_memory_size;
Matus Fabian41fef502017-09-22 02:43:05 -07002411 /* do not exceed load factor 10 */
2412 sm->max_translations = 10 * translation_buckets;
Dave Barach20c02cb2016-06-26 10:42:08 -04002413 sm->user_buckets = user_buckets;
2414 sm->user_memory_size = user_memory_size;
2415 sm->max_translations_per_user = max_translations_per_user;
2416 sm->outside_vrf_id = outside_vrf_id;
Matus Fabian31c31aa2017-02-05 22:45:57 -08002417 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
Neale Ranns15002542017-09-10 04:39:11 -07002418 outside_vrf_id,
2419 FIB_SOURCE_PLUGIN_HI);
Matus Fabiandb649882016-08-26 05:45:27 -07002420 sm->inside_vrf_id = inside_vrf_id;
Matus Fabian31c31aa2017-02-05 22:45:57 -08002421 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
Neale Ranns15002542017-09-10 04:39:11 -07002422 inside_vrf_id,
2423 FIB_SOURCE_PLUGIN_HI);
Matus Fabiandb649882016-08-26 05:45:27 -07002424 sm->static_mapping_only = static_mapping_only;
2425 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
Dave Barach20c02cb2016-06-26 10:42:08 -04002426
Matus Fabian066f0342017-02-10 03:48:01 -08002427 if (sm->deterministic)
Matus Fabiandb649882016-08-26 05:45:27 -07002428 {
Matus Fabian066f0342017-02-10 03:48:01 -08002429 sm->in2out_node_index = snat_det_in2out_node.index;
Matus Fabian93d84c92017-07-19 08:06:01 -07002430 sm->in2out_output_node_index = ~0;
Matus Fabian066f0342017-02-10 03:48:01 -08002431 sm->out2in_node_index = snat_det_out2in_node.index;
Juraj Sloboda7a1bde02017-04-03 08:43:58 +02002432 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2433 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
Matus Fabiandb649882016-08-26 05:45:27 -07002434 }
Matus Fabian066f0342017-02-10 03:48:01 -08002435 else
2436 {
2437 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2438 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2439 sm->in2out_node_index = snat_in2out_node.index;
Matus Fabian93d84c92017-07-19 08:06:01 -07002440 sm->in2out_output_node_index = snat_in2out_output_node.index;
Matus Fabian066f0342017-02-10 03:48:01 -08002441 sm->out2in_node_index = snat_out2in_node.index;
2442 if (!static_mapping_only ||
2443 (static_mapping_only && static_mapping_connection_tracking))
2444 {
Juraj Sloboda557a71c2017-02-22 05:16:06 -08002445 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2446 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2447
Matus Fabian092b3cd2017-09-19 05:42:38 -07002448 vec_foreach (tsm, sm->per_thread_data)
2449 {
2450 clib_bihash_init_8_8 (&tsm->in2out, "in2out", translation_buckets,
2451 translation_memory_size);
2452
2453 clib_bihash_init_8_8 (&tsm->out2in, "out2in", translation_buckets,
2454 translation_memory_size);
2455
2456 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
2457 user_memory_size);
2458 }
2459
Matus Fabian704018c2017-09-04 02:17:18 -07002460 clib_bihash_init_16_8 (&sm->in2out_ed, "in2out-ed",
Matus Fabian7968e6c2017-07-06 05:37:49 -07002461 translation_buckets, translation_memory_size);
2462
Matus Fabian704018c2017-09-04 02:17:18 -07002463 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
Matus Fabian7968e6c2017-07-06 05:37:49 -07002464 translation_buckets, translation_memory_size);
Matus Fabian066f0342017-02-10 03:48:01 -08002465 }
Juraj Sloboda557a71c2017-02-22 05:16:06 -08002466 else
2467 {
2468 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2469 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2470 }
Matus Fabian066f0342017-02-10 03:48:01 -08002471 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2472 "static_mapping_by_local", static_mapping_buckets,
2473 static_mapping_memory_size);
2474
2475 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2476 "static_mapping_by_external", static_mapping_buckets,
2477 static_mapping_memory_size);
2478 }
2479
Dave Barach20c02cb2016-06-26 10:42:08 -04002480 return 0;
2481}
2482
Matus Fabian2ba92e32017-08-21 07:05:03 -07002483VLIB_CONFIG_FUNCTION (snat_config, "nat");
Dave Barach20c02cb2016-06-26 10:42:08 -04002484
Matus Fabian066f0342017-02-10 03:48:01 -08002485u8 * format_snat_session_state (u8 * s, va_list * args)
2486{
2487 u32 i = va_arg (*args, u32);
2488 u8 *t = 0;
2489
2490 switch (i)
2491 {
2492#define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
2493 foreach_snat_session_state
2494#undef _
2495 default:
2496 t = format (t, "unknown");
2497 }
2498 s = format (s, "%s", t);
2499 return s;
2500}
2501
Dave Barach20c02cb2016-06-26 10:42:08 -04002502u8 * format_snat_key (u8 * s, va_list * args)
2503{
2504 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
Dave Barach20c02cb2016-06-26 10:42:08 -04002505
Matus Fabian704018c2017-09-04 02:17:18 -07002506 s = format (s, "%U proto %U port %d fib %d",
2507 format_ip4_address, &key->addr,
2508 format_snat_protocol, key->protocol,
Matus Fabiandb649882016-08-26 05:45:27 -07002509 clib_net_to_host_u16 (key->port), key->fib_index);
Dave Barach20c02cb2016-06-26 10:42:08 -04002510 return s;
2511}
2512
2513u8 * format_snat_session (u8 * s, va_list * args)
2514{
2515 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2516 snat_session_t * sess = va_arg (*args, snat_session_t *);
2517
Matus Fabian7968e6c2017-07-06 05:37:49 -07002518 if (snat_is_unk_proto_session (sess))
2519 {
2520 s = format (s, " i2o %U proto %u fib %u\n",
2521 format_ip4_address, &sess->in2out.addr, sess->in2out.port,
2522 sess->in2out.fib_index);
2523 s = format (s, " o2i %U proto %u fib %u\n",
2524 format_ip4_address, &sess->out2in.addr, sess->out2in.port,
2525 sess->out2in.fib_index);
2526 }
2527 else
2528 {
2529 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2530 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2531 }
Matus Fabian704018c2017-09-04 02:17:18 -07002532 if (sess->ext_host_addr.as_u32)
2533 s = format (s, " external host %U\n",
2534 format_ip4_address, &sess->ext_host_addr);
Dave Barach20c02cb2016-06-26 10:42:08 -04002535 s = format (s, " last heard %.2f\n", sess->last_heard);
2536 s = format (s, " total pkts %d, total bytes %lld\n",
2537 sess->total_pkts, sess->total_bytes);
Matus Fabiandb649882016-08-26 05:45:27 -07002538 if (snat_is_session_static (sess))
2539 s = format (s, " static translation\n");
2540 else
2541 s = format (s, " dynamic translation\n");
Matus Fabian704018c2017-09-04 02:17:18 -07002542 if (sess->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
2543 s = format (s, " load-balancing\n");
Dave Barach20c02cb2016-06-26 10:42:08 -04002544
2545 return s;
2546}
2547
2548u8 * format_snat_user (u8 * s, va_list * args)
2549{
Matus Fabian475f0552016-10-19 06:17:52 -07002550 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
Dave Barach20c02cb2016-06-26 10:42:08 -04002551 snat_user_t * u = va_arg (*args, snat_user_t *);
2552 int verbose = va_arg (*args, int);
2553 dlist_elt_t * head, * elt;
2554 u32 elt_index, head_index;
2555 u32 session_index;
2556 snat_session_t * sess;
2557
Matus Fabiandb649882016-08-26 05:45:27 -07002558 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2559 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
Dave Barach20c02cb2016-06-26 10:42:08 -04002560
2561 if (verbose == 0)
2562 return s;
2563
Matus Fabiandb649882016-08-26 05:45:27 -07002564 if (u->nsessions || u->nstaticsessions)
Dave Barach20c02cb2016-06-26 10:42:08 -04002565 {
Matus Fabiandb649882016-08-26 05:45:27 -07002566 head_index = u->sessions_per_user_list_head_index;
2567 head = pool_elt_at_index (sm->list_pool, head_index);
Dave Barach20c02cb2016-06-26 10:42:08 -04002568
Matus Fabiandb649882016-08-26 05:45:27 -07002569 elt_index = head->next;
Dave Barach20c02cb2016-06-26 10:42:08 -04002570 elt = pool_elt_at_index (sm->list_pool, elt_index);
2571 session_index = elt->value;
Matus Fabiandb649882016-08-26 05:45:27 -07002572
2573 while (session_index != ~0)
2574 {
2575 sess = pool_elt_at_index (sm->sessions, session_index);
2576
2577 s = format (s, " %U\n", format_snat_session, sm, sess);
2578
2579 elt_index = elt->next;
2580 elt = pool_elt_at_index (sm->list_pool, elt_index);
2581 session_index = elt->value;
2582 }
Dave Barach20c02cb2016-06-26 10:42:08 -04002583 }
2584
2585 return s;
2586}
2587
Matus Fabiandb649882016-08-26 05:45:27 -07002588u8 * format_snat_static_mapping (u8 * s, va_list * args)
2589{
2590 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
Matus Fabian704018c2017-09-04 02:17:18 -07002591 nat44_lb_addr_port_t *local;
Matus Fabiandb649882016-08-26 05:45:27 -07002592
2593 if (m->addr_only)
2594 s = format (s, "local %U external %U vrf %d",
2595 format_ip4_address, &m->local_addr,
2596 format_ip4_address, &m->external_addr,
2597 m->vrf_id);
2598 else
Matus Fabian704018c2017-09-04 02:17:18 -07002599 {
2600 if (vec_len (m->locals))
2601 {
2602 s = format (s, "%U vrf %d external %U:%d",
2603 format_snat_protocol, m->proto,
2604 m->vrf_id,
2605 format_ip4_address, &m->external_addr, m->external_port);
2606 vec_foreach (local, m->locals)
2607 s = format (s, "\n local %U:%d probability %d\%",
2608 format_ip4_address, &local->addr, local->port,
2609 local->probability);
2610 }
2611 else
2612 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2613 format_snat_protocol, m->proto,
2614 format_ip4_address, &m->local_addr, m->local_port,
2615 format_ip4_address, &m->external_addr, m->external_port,
2616 m->vrf_id);
2617 }
Matus Fabiandb649882016-08-26 05:45:27 -07002618 return s;
2619}
2620
Matus Fabiane22e5462017-02-14 23:33:43 -08002621u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
2622{
2623 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
2624 vnet_main_t *vnm = vnet_get_main();
2625
2626 if (m->addr_only)
2627 s = format (s, "local %U external %U vrf %d",
2628 format_ip4_address, &m->l_addr,
2629 format_vnet_sw_interface_name, vnm,
2630 vnet_get_sw_interface (vnm, m->sw_if_index),
2631 m->vrf_id);
2632 else
2633 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2634 format_snat_protocol, m->proto,
2635 format_ip4_address, &m->l_addr, m->l_port,
2636 format_vnet_sw_interface_name, vnm,
2637 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
2638 m->vrf_id);
2639
2640 return s;
2641}
2642
Matus Fabian066f0342017-02-10 03:48:01 -08002643u8 * format_det_map_ses (u8 * s, va_list * args)
2644{
2645 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2646 ip4_address_t in_addr, out_addr;
2647 u32 in_offset, out_offset;
2648 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2649 u32 * i = va_arg (*args, u32 *);
2650
2651 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2652 in_addr.as_u32 = clib_host_to_net_u32 (
2653 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2654 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2655 clib_net_to_host_u32(det_map->in_addr.as_u32);
2656 out_offset = in_offset / det_map->sharing_ratio;
2657 out_addr.as_u32 = clib_host_to_net_u32(
2658 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2659 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2660 format_ip4_address, &in_addr,
2661 clib_net_to_host_u16 (ses->in_port),
2662 format_ip4_address, &out_addr,
2663 clib_net_to_host_u16 (ses->out.out_port),
2664 format_ip4_address, &ses->out.ext_host_addr,
2665 clib_net_to_host_u16 (ses->out.ext_host_port),
2666 format_snat_session_state, ses->state,
2667 ses->expire);
2668
2669 return s;
2670}
2671
Dave Barach20c02cb2016-06-26 10:42:08 -04002672static clib_error_t *
2673show_snat_command_fn (vlib_main_t * vm,
2674 unformat_input_t * input,
2675 vlib_cli_command_t * cmd)
2676{
2677 int verbose = 0;
2678 snat_main_t * sm = &snat_main;
2679 snat_user_t * u;
Matus Fabiandb649882016-08-26 05:45:27 -07002680 snat_static_mapping_t *m;
Matus Fabian588144a2016-10-24 03:30:00 -07002681 snat_interface_t *i;
Matus Fabian07ea7612016-12-15 05:30:37 -08002682 snat_address_t * ap;
Matus Fabian588144a2016-10-24 03:30:00 -07002683 vnet_main_t *vnm = vnet_get_main();
Matus Fabian475f0552016-10-19 06:17:52 -07002684 snat_main_per_thread_data_t *tsm;
Matus Fabian8bf68e82017-01-12 04:24:35 -08002685 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
Matus Fabian475f0552016-10-19 06:17:52 -07002686 uword j = 0;
Matus Fabiane22e5462017-02-14 23:33:43 -08002687 snat_static_map_resolve_t *rp;
Matus Fabian066f0342017-02-10 03:48:01 -08002688 snat_det_map_t * dm;
2689 snat_det_session_t * ses;
Dave Barach20c02cb2016-06-26 10:42:08 -04002690
2691 if (unformat (input, "detail"))
2692 verbose = 1;
2693 else if (unformat (input, "verbose"))
2694 verbose = 2;
2695
Matus Fabiandb649882016-08-26 05:45:27 -07002696 if (sm->static_mapping_only)
Dave Barach20c02cb2016-06-26 10:42:08 -04002697 {
Matus Fabiandb649882016-08-26 05:45:27 -07002698 if (sm->static_mapping_connection_tracking)
Matus Fabian2ba92e32017-08-21 07:05:03 -07002699 vlib_cli_output (vm, "NAT plugin mode: static mapping only connection "
Matus Fabiandb649882016-08-26 05:45:27 -07002700 "tracking");
2701 else
Matus Fabian2ba92e32017-08-21 07:05:03 -07002702 vlib_cli_output (vm, "NAT plugin mode: static mapping only");
Matus Fabiandb649882016-08-26 05:45:27 -07002703 }
Matus Fabian066f0342017-02-10 03:48:01 -08002704 else if (sm->deterministic)
2705 {
Matus Fabian2ba92e32017-08-21 07:05:03 -07002706 vlib_cli_output (vm, "NAT plugin mode: deterministic mapping");
Matus Fabian066f0342017-02-10 03:48:01 -08002707 }
Matus Fabiandb649882016-08-26 05:45:27 -07002708 else
2709 {
Matus Fabian2ba92e32017-08-21 07:05:03 -07002710 vlib_cli_output (vm, "NAT plugin mode: dynamic translations enabled");
Matus Fabiandb649882016-08-26 05:45:27 -07002711 }
Dave Barach20c02cb2016-06-26 10:42:08 -04002712
Matus Fabian588144a2016-10-24 03:30:00 -07002713 if (verbose > 0)
2714 {
2715 pool_foreach (i, sm->interfaces,
2716 ({
2717 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2718 vnet_get_sw_interface (vnm, i->sw_if_index),
Matus Fabian36ea2d62017-10-24 04:13:49 -07002719 (nat_interface_is_inside(i) &&
2720 nat_interface_is_outside(i)) ? "in out" :
2721 (nat_interface_is_inside(i) ? "in" : "out"));
Matus Fabian588144a2016-10-24 03:30:00 -07002722 }));
Matus Fabian07ea7612016-12-15 05:30:37 -08002723
Matus Fabian93d84c92017-07-19 08:06:01 -07002724 pool_foreach (i, sm->output_feature_interfaces,
2725 ({
2726 vlib_cli_output (vm, "%U output-feature %s",
2727 format_vnet_sw_interface_name, vnm,
2728 vnet_get_sw_interface (vnm, i->sw_if_index),
Matus Fabian36ea2d62017-10-24 04:13:49 -07002729 (nat_interface_is_inside(i) &&
2730 nat_interface_is_outside(i)) ? "in out" :
2731 (nat_interface_is_inside(i) ? "in" : "out"));
Matus Fabian93d84c92017-07-19 08:06:01 -07002732 }));
2733
Matus Fabian8bf68e82017-01-12 04:24:35 -08002734 if (vec_len (sm->auto_add_sw_if_indices))
2735 {
Matus Fabian2ba92e32017-08-21 07:05:03 -07002736 vlib_cli_output (vm, "NAT44 pool addresses interfaces:");
Matus Fabian8bf68e82017-01-12 04:24:35 -08002737 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2738 {
2739 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2740 vnet_get_sw_interface (vnm, *sw_if_index));
2741 }
2742 }
2743
Matus Fabian07ea7612016-12-15 05:30:37 -08002744 vec_foreach (ap, sm->addresses)
2745 {
Matus Fabian07ea7612016-12-15 05:30:37 -08002746 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002747 if (ap->fib_index != ~0)
2748 vlib_cli_output (vm, " tenant VRF: %u",
2749 ip4_fib_get(ap->fib_index)->table_id);
2750 else
2751 vlib_cli_output (vm, " tenant VRF independent");
Matus Fabian09d96f42017-02-02 01:43:00 -08002752#define _(N, i, n, s) \
2753 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
2754 foreach_snat_protocol
2755#undef _
Matus Fabian07ea7612016-12-15 05:30:37 -08002756 }
Matus Fabian588144a2016-10-24 03:30:00 -07002757 }
2758
Matus Fabian475f0552016-10-19 06:17:52 -07002759 if (sm->num_workers > 1)
2760 {
2761 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2762 if (verbose > 0)
2763 {
2764 vec_foreach (worker, sm->workers)
2765 {
2766 vlib_worker_thread_t *w =
2767 vlib_worker_threads + *worker + sm->first_worker_index;
Matus Fabianeea28d72017-01-13 04:15:54 -08002768 vlib_cli_output (vm, " %s", w->name);
Matus Fabian475f0552016-10-19 06:17:52 -07002769 }
2770 }
2771 }
2772
Matus Fabian066f0342017-02-10 03:48:01 -08002773 if (sm->deterministic)
Matus Fabiandb649882016-08-26 05:45:27 -07002774 {
Matus Fabian6a0946f2017-04-12 03:36:13 -07002775 vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
2776 vlib_cli_output (vm, "tcp-established timeout: %dsec",
2777 sm->tcp_established_timeout);
2778 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
2779 sm->tcp_transitory_timeout);
2780 vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
Matus Fabian066f0342017-02-10 03:48:01 -08002781 vlib_cli_output (vm, "%d deterministic mappings",
2782 pool_elts (sm->det_maps));
Matus Fabiandb649882016-08-26 05:45:27 -07002783 if (verbose > 0)
2784 {
Matus Fabian066f0342017-02-10 03:48:01 -08002785 pool_foreach (dm, sm->det_maps,
Matus Fabiandb649882016-08-26 05:45:27 -07002786 ({
Matus Fabian066f0342017-02-10 03:48:01 -08002787 vlib_cli_output (vm, "in %U/%d out %U/%d\n",
2788 format_ip4_address, &dm->in_addr, dm->in_plen,
2789 format_ip4_address, &dm->out_addr, dm->out_plen);
2790 vlib_cli_output (vm, " outside address sharing ratio: %d\n",
2791 dm->sharing_ratio);
2792 vlib_cli_output (vm, " number of ports per inside host: %d\n",
2793 dm->ports_per_host);
2794 vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
2795 if (verbose > 1)
2796 {
2797 vec_foreach_index (j, dm->sessions)
2798 {
2799 ses = vec_elt_at_index (dm->sessions, j);
2800 if (ses->in_port)
2801 vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses,
2802 &j);
2803 }
2804 }
Matus Fabiandb649882016-08-26 05:45:27 -07002805 }));
2806 }
2807 }
2808 else
2809 {
Matus Fabian066f0342017-02-10 03:48:01 -08002810 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
Matus Fabian475f0552016-10-19 06:17:52 -07002811 {
Matus Fabian066f0342017-02-10 03:48:01 -08002812 vlib_cli_output (vm, "%d static mappings",
2813 pool_elts (sm->static_mappings));
Matus Fabian475f0552016-10-19 06:17:52 -07002814
Matus Fabian066f0342017-02-10 03:48:01 -08002815 if (verbose > 0)
Matus Fabian475f0552016-10-19 06:17:52 -07002816 {
Matus Fabiandb649882016-08-26 05:45:27 -07002817 pool_foreach (m, sm->static_mappings,
2818 ({
2819 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2820 }));
Matus Fabian066f0342017-02-10 03:48:01 -08002821 }
2822 }
2823 else
2824 {
2825 vec_foreach (tsm, sm->per_thread_data)
2826 {
2827 users_num += pool_elts (tsm->users);
2828 sessions_num += pool_elts (tsm->sessions);
2829 }
2830
2831 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2832 " %d static mappings",
2833 users_num,
2834 vec_len (sm->addresses),
2835 sessions_num,
2836 pool_elts (sm->static_mappings));
2837
2838 if (verbose > 0)
2839 {
Matus Fabian704018c2017-09-04 02:17:18 -07002840 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->in2out_ed,
2841 verbose - 1);
2842 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->out2in_ed,
2843 verbose - 1);
Matus Fabian066f0342017-02-10 03:48:01 -08002844 vec_foreach_index (j, sm->per_thread_data)
Matus Fabiane22e5462017-02-14 23:33:43 -08002845 {
Matus Fabian066f0342017-02-10 03:48:01 -08002846 tsm = vec_elt_at_index (sm->per_thread_data, j);
2847
2848 if (pool_elts (tsm->users) == 0)
2849 continue;
2850
2851 vlib_worker_thread_t *w = vlib_worker_threads + j;
2852 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2853 w->lcore_id);
Matus Fabian092b3cd2017-09-19 05:42:38 -07002854 vlib_cli_output (vm, " %U", format_bihash_8_8, &tsm->in2out,
2855 verbose - 1);
2856 vlib_cli_output (vm, " %U", format_bihash_8_8, &tsm->out2in,
2857 verbose - 1);
Matus Fabian066f0342017-02-10 03:48:01 -08002858 vlib_cli_output (vm, " %d list pool elements",
2859 pool_elts (tsm->list_pool));
2860
2861 pool_foreach (u, tsm->users,
2862 ({
2863 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2864 verbose - 1);
2865 }));
2866 }
2867
2868 if (pool_elts (sm->static_mappings))
2869 {
2870 vlib_cli_output (vm, "static mappings:");
2871 pool_foreach (m, sm->static_mappings,
2872 ({
2873 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2874 }));
2875 for (j = 0; j < vec_len (sm->to_resolve); j++)
2876 {
2877 rp = sm->to_resolve + j;
2878 vlib_cli_output (vm, "%U",
2879 format_snat_static_map_to_resolve, rp);
2880 }
Matus Fabiane22e5462017-02-14 23:33:43 -08002881 }
Matus Fabiandb649882016-08-26 05:45:27 -07002882 }
2883 }
Dave Barach20c02cb2016-06-26 10:42:08 -04002884 }
Dave Barach20c02cb2016-06-26 10:42:08 -04002885 return 0;
2886}
2887
2888VLIB_CLI_COMMAND (show_snat_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07002889 .path = "show nat44",
2890 .short_help = "show nat44",
Dave Barach20c02cb2016-06-26 10:42:08 -04002891 .function = show_snat_command_fn,
2892};
Dave Barachcab65ec2017-01-11 13:01:14 -05002893
2894
2895static void
2896snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2897 uword opaque,
2898 u32 sw_if_index,
2899 ip4_address_t * address,
2900 u32 address_length,
2901 u32 if_address_index,
2902 u32 is_delete)
2903{
2904 snat_main_t *sm = &snat_main;
Dave Barach8b275372017-01-16 10:54:02 -05002905 snat_static_map_resolve_t *rp;
2906 u32 *indices_to_delete = 0;
Dave Barachcab65ec2017-01-11 13:01:14 -05002907 int i, j;
Dave Barach8b275372017-01-16 10:54:02 -05002908 int rv;
Dave Barachcab65ec2017-01-11 13:01:14 -05002909
2910 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2911 {
2912 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2913 {
2914 if (!is_delete)
2915 {
2916 /* Don't trip over lease renewal, static config */
2917 for (j = 0; j < vec_len(sm->addresses); j++)
2918 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2919 return;
2920
Juraj Slobodaeab38d92017-03-06 19:55:21 -08002921 snat_add_address (sm, address, ~0);
Dave Barach8b275372017-01-16 10:54:02 -05002922 /* Scan static map resolution vector */
2923 for (j = 0; j < vec_len (sm->to_resolve); j++)
2924 {
2925 rp = sm->to_resolve + j;
2926 /* On this interface? */
2927 if (rp->sw_if_index == sw_if_index)
2928 {
2929 /* Add the static mapping */
2930 rv = snat_add_static_mapping (rp->l_addr,
2931 address[0],
2932 rp->l_port,
2933 rp->e_port,
2934 rp->vrf_id,
2935 rp->addr_only,
2936 ~0 /* sw_if_index */,
Matus Fabian09d96f42017-02-02 01:43:00 -08002937 rp->proto,
Dave Barach8b275372017-01-16 10:54:02 -05002938 rp->is_add);
2939 if (rv)
2940 clib_warning ("snat_add_static_mapping returned %d",
2941 rv);
2942 vec_add1 (indices_to_delete, j);
2943 }
2944 }
2945 /* If we resolved any of the outstanding static mappings */
2946 if (vec_len(indices_to_delete))
2947 {
2948 /* Delete them */
2949 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2950 vec_delete(sm->to_resolve, 1, j);
2951 vec_free(indices_to_delete);
2952 }
Dave Barachcab65ec2017-01-11 13:01:14 -05002953 return;
2954 }
2955 else
2956 {
Matus Fabian36532bd2017-01-23 23:42:28 -08002957 (void) snat_del_address(sm, address[0], 1);
Dave Barachcab65ec2017-01-11 13:01:14 -05002958 return;
2959 }
2960 }
2961 }
2962}
2963
2964
Matus Fabiancfe0fc92017-05-10 06:37:47 -07002965int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del)
Dave Barachcab65ec2017-01-11 13:01:14 -05002966{
2967 ip4_main_t * ip4_main = sm->ip4_main;
2968 ip4_address_t * first_int_addr;
Matus Fabian36532bd2017-01-23 23:42:28 -08002969 snat_static_map_resolve_t *rp;
2970 u32 *indices_to_delete = 0;
2971 int i, j;
Dave Barachcab65ec2017-01-11 13:01:14 -05002972
2973 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2974 0 /* just want the address*/);
2975
2976 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2977 {
2978 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
Matus Fabian8bf68e82017-01-12 04:24:35 -08002979 {
2980 if (is_del)
2981 {
2982 /* if have address remove it */
2983 if (first_int_addr)
Matus Fabian36532bd2017-01-23 23:42:28 -08002984 (void) snat_del_address (sm, first_int_addr[0], 1);
2985 else
2986 {
2987 for (j = 0; j < vec_len (sm->to_resolve); j++)
2988 {
2989 rp = sm->to_resolve + j;
2990 if (rp->sw_if_index == sw_if_index)
2991 vec_add1 (indices_to_delete, j);
2992 }
2993 if (vec_len(indices_to_delete))
2994 {
2995 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2996 vec_del1(sm->to_resolve, j);
2997 vec_free(indices_to_delete);
2998 }
2999 }
Matus Fabian8bf68e82017-01-12 04:24:35 -08003000 vec_del1(sm->auto_add_sw_if_indices, i);
3001 }
3002 else
3003 return VNET_API_ERROR_VALUE_EXIST;
3004
3005 return 0;
3006 }
Dave Barachcab65ec2017-01-11 13:01:14 -05003007 }
Matus Fabian2ba92e32017-08-21 07:05:03 -07003008
Matus Fabian8bf68e82017-01-12 04:24:35 -08003009 if (is_del)
3010 return VNET_API_ERROR_NO_SUCH_ENTRY;
3011
Dave Barachcab65ec2017-01-11 13:01:14 -05003012 /* add to the auto-address list */
3013 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
3014
3015 /* If the address is already bound - or static - add it now */
3016 if (first_int_addr)
Juraj Slobodaeab38d92017-03-06 19:55:21 -08003017 snat_add_address (sm, first_int_addr, ~0);
Dave Barachcab65ec2017-01-11 13:01:14 -05003018
3019 return 0;
3020}
3021
3022static clib_error_t *
3023snat_add_interface_address_command_fn (vlib_main_t * vm,
3024 unformat_input_t * input,
3025 vlib_cli_command_t * cmd)
3026{
3027 snat_main_t *sm = &snat_main;
3028 unformat_input_t _line_input, *line_input = &_line_input;
3029 u32 sw_if_index;
3030 int rv;
Matus Fabian8bf68e82017-01-12 04:24:35 -08003031 int is_del = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -05003032 clib_error_t *error = 0;
Dave Barachcab65ec2017-01-11 13:01:14 -05003033
3034 /* Get a line of input. */
3035 if (!unformat_user (input, unformat_line_input, line_input))
3036 return 0;
3037
3038 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3039 {
3040 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
3041 sm->vnet_main, &sw_if_index))
3042 ;
Matus Fabian8bf68e82017-01-12 04:24:35 -08003043 else if (unformat (line_input, "del"))
3044 is_del = 1;
Dave Barachcab65ec2017-01-11 13:01:14 -05003045 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003046 {
3047 error = clib_error_return (0, "unknown input '%U'",
3048 format_unformat_error, line_input);
3049 goto done;
3050 }
Dave Barachcab65ec2017-01-11 13:01:14 -05003051 }
3052
Matus Fabian8bf68e82017-01-12 04:24:35 -08003053 rv = snat_add_interface_address (sm, sw_if_index, is_del);
Dave Barachcab65ec2017-01-11 13:01:14 -05003054
3055 switch (rv)
3056 {
3057 case 0:
3058 break;
3059
3060 default:
Billy McFalla9a20e72017-02-15 11:39:12 -05003061 error = clib_error_return (0, "snat_add_interface_address returned %d",
3062 rv);
3063 goto done;
Dave Barachcab65ec2017-01-11 13:01:14 -05003064 }
Billy McFalla9a20e72017-02-15 11:39:12 -05003065
3066done:
3067 unformat_free (line_input);
3068
3069 return error;
Dave Barachcab65ec2017-01-11 13:01:14 -05003070}
3071
3072VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07003073 .path = "nat44 add interface address",
3074 .short_help = "nat44 add interface address <interface> [del]",
Dave Barachcab65ec2017-01-11 13:01:14 -05003075 .function = snat_add_interface_address_command_fn,
3076};
Matus Fabian066f0342017-02-10 03:48:01 -08003077
3078static clib_error_t *
3079snat_det_map_command_fn (vlib_main_t * vm,
3080 unformat_input_t * input,
3081 vlib_cli_command_t * cmd)
3082{
3083 snat_main_t *sm = &snat_main;
3084 unformat_input_t _line_input, *line_input = &_line_input;
3085 ip4_address_t in_addr, out_addr;
3086 u32 in_plen, out_plen;
3087 int is_add = 1, rv;
3088 clib_error_t *error = 0;
3089
3090 /* Get a line of input. */
3091 if (!unformat_user (input, unformat_line_input, line_input))
3092 return 0;
3093
3094 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3095 {
3096 if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
3097 ;
3098 else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
3099 ;
3100 else if (unformat (line_input, "del"))
3101 is_add = 0;
3102 else
3103 {
3104 error = clib_error_return (0, "unknown input '%U'",
3105 format_unformat_error, line_input);
3106 goto done;
3107 }
3108 }
3109
3110 unformat_free (line_input);
3111
3112 rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
3113 is_add);
3114
3115 if (rv)
3116 {
3117 error = clib_error_return (0, "snat_det_add_map return %d", rv);
3118 goto done;
3119 }
3120
3121done:
3122 unformat_free (line_input);
3123
3124 return error;
3125}
3126
3127/*?
3128 * @cliexpar
3129 * @cliexstart{snat deterministic add}
3130 * Create bijective mapping of inside address to outside address and port range
3131 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
3132 * CGN deployments.
3133 * To create deterministic mapping between inside network 10.0.0.0/18 and
3134 * outside network 1.1.1.0/30 use:
Matus Fabian2ba92e32017-08-21 07:05:03 -07003135 * # vpp# nat44 deterministic add in 10.0.0.0/18 out 1.1.1.0/30
Matus Fabian066f0342017-02-10 03:48:01 -08003136 * @cliexend
3137?*/
3138VLIB_CLI_COMMAND (snat_det_map_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07003139 .path = "nat44 deterministic add",
3140 .short_help = "nat44 deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
Matus Fabian066f0342017-02-10 03:48:01 -08003141 .function = snat_det_map_command_fn,
3142};
3143
3144static clib_error_t *
3145snat_det_forward_command_fn (vlib_main_t * vm,
3146 unformat_input_t * input,
3147 vlib_cli_command_t * cmd)
3148{
3149 snat_main_t *sm = &snat_main;
3150 unformat_input_t _line_input, *line_input = &_line_input;
3151 ip4_address_t in_addr, out_addr;
3152 u16 lo_port;
3153 snat_det_map_t * dm;
3154 clib_error_t *error = 0;
3155
3156 /* Get a line of input. */
3157 if (!unformat_user (input, unformat_line_input, line_input))
3158 return 0;
3159
3160 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3161 {
3162 if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
3163 ;
3164 else
3165 {
3166 error = clib_error_return (0, "unknown input '%U'",
3167 format_unformat_error, line_input);
3168 goto done;
3169 }
3170 }
3171
3172 unformat_free (line_input);
3173
3174 dm = snat_det_map_by_user(sm, &in_addr);
3175 if (!dm)
3176 vlib_cli_output (vm, "no match");
3177 else
3178 {
3179 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
3180 vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
3181 lo_port, lo_port + dm->ports_per_host - 1);
3182 }
3183
3184done:
3185 unformat_free (line_input);
3186
3187 return error;
3188}
3189
3190/*?
3191 * @cliexpar
3192 * @cliexstart{snat deterministic forward}
3193 * Return outside address and port range from inside address for deterministic
3194 * NAT.
3195 * To obtain outside address and port of inside host use:
Matus Fabian2ba92e32017-08-21 07:05:03 -07003196 * vpp# nat44 deterministic forward 10.0.0.2
Matus Fabian066f0342017-02-10 03:48:01 -08003197 * 1.1.1.0:<1054-1068>
3198 * @cliexend
3199?*/
3200VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07003201 .path = "nat44 deterministic forward",
3202 .short_help = "nat44 deterministic forward <addr>",
Matus Fabian066f0342017-02-10 03:48:01 -08003203 .function = snat_det_forward_command_fn,
3204};
3205
3206static clib_error_t *
3207snat_det_reverse_command_fn (vlib_main_t * vm,
3208 unformat_input_t * input,
3209 vlib_cli_command_t * cmd)
3210{
3211 snat_main_t *sm = &snat_main;
3212 unformat_input_t _line_input, *line_input = &_line_input;
3213 ip4_address_t in_addr, out_addr;
3214 u32 out_port;
3215 snat_det_map_t * dm;
3216 clib_error_t *error = 0;
3217
3218 /* Get a line of input. */
3219 if (!unformat_user (input, unformat_line_input, line_input))
3220 return 0;
3221
3222 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3223 {
3224 if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
3225 ;
3226 else
3227 {
3228 error = clib_error_return (0, "unknown input '%U'",
3229 format_unformat_error, line_input);
3230 }
3231 }
3232
3233 unformat_free (line_input);
3234
3235 if (out_port < 1024 || out_port > 65535)
3236 {
3237 error = clib_error_return (0, "wrong port, must be <1024-65535>");
3238 goto done;
3239 }
3240
3241 dm = snat_det_map_by_out(sm, &out_addr);
3242 if (!dm)
3243 vlib_cli_output (vm, "no match");
3244 else
3245 {
3246 snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
3247 vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
3248 }
3249
3250done:
3251 unformat_free (line_input);
3252
3253 return error;
3254}
3255
3256/*?
3257 * @cliexpar
3258 * @cliexstart{snat deterministic reverse}
3259 * Return inside address from outside address and port for deterministic NAT.
3260 * To obtain inside host address from outside address and port use:
Matus Fabian2ba92e32017-08-21 07:05:03 -07003261 * #vpp nat44 deterministic reverse 1.1.1.1:1276
Matus Fabian066f0342017-02-10 03:48:01 -08003262 * 10.0.16.16
3263 * @cliexend
3264?*/
3265VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07003266 .path = "nat44 deterministic reverse",
3267 .short_help = "nat44 deterministic reverse <addr>:<port>",
Matus Fabian066f0342017-02-10 03:48:01 -08003268 .function = snat_det_reverse_command_fn,
3269};
Matus Fabian6a0946f2017-04-12 03:36:13 -07003270
3271static clib_error_t *
3272set_timeout_command_fn (vlib_main_t * vm,
3273 unformat_input_t * input,
3274 vlib_cli_command_t * cmd)
3275{
3276 snat_main_t *sm = &snat_main;
3277 unformat_input_t _line_input, *line_input = &_line_input;
3278 clib_error_t *error = 0;
3279
3280 /* Get a line of input. */
3281 if (!unformat_user (input, unformat_line_input, line_input))
3282 return 0;
3283
3284 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3285 {
3286 if (unformat (line_input, "udp %u", &sm->udp_timeout))
3287 ;
3288 else if (unformat (line_input, "tcp-established %u",
3289 &sm->tcp_established_timeout))
3290 ;
3291 else if (unformat (line_input, "tcp-transitory %u",
3292 &sm->tcp_transitory_timeout))
3293 ;
3294 else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
3295 ;
3296 else if (unformat (line_input, "reset"))
3297 {
3298 sm->udp_timeout = SNAT_UDP_TIMEOUT;
3299 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
3300 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
3301 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
3302 }
3303 else
3304 {
3305 error = clib_error_return (0, "unknown input '%U'",
3306 format_unformat_error, line_input);
3307 goto done;
3308 }
3309 }
3310
3311 unformat_free (line_input);
3312
3313done:
3314 unformat_free (line_input);
3315
3316 return error;
3317}
3318
3319/*?
3320 * @cliexpar
3321 * @cliexstart{set snat deterministic timeout}
3322 * Set values of timeouts for deterministic NAT (in seconds), use:
Matus Fabian2ba92e32017-08-21 07:05:03 -07003323 * vpp# set nat44 deterministic timeout udp 120 tcp-established 7500
Matus Fabian6a0946f2017-04-12 03:36:13 -07003324 * tcp-transitory 250 icmp 90
3325 * To reset default values use:
Matus Fabian2ba92e32017-08-21 07:05:03 -07003326 * vpp# set nat44 deterministic timeout reset
Matus Fabian6a0946f2017-04-12 03:36:13 -07003327 * @cliexend
3328?*/
3329VLIB_CLI_COMMAND (set_timeout_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07003330 .path = "set nat44 deterministic timeout",
Matus Fabian6a0946f2017-04-12 03:36:13 -07003331 .function = set_timeout_command_fn,
3332 .short_help =
Matus Fabian2ba92e32017-08-21 07:05:03 -07003333 "set nat44 deterministic timeout [udp <sec> | tcp-established <sec> "
Matus Fabian6a0946f2017-04-12 03:36:13 -07003334 "tcp-transitory <sec> | icmp <sec> | reset]",
3335};
Martin Gálik6bc8c642017-04-19 01:12:27 -07003336
3337static clib_error_t *
3338snat_det_close_session_out_fn (vlib_main_t *vm,
3339 unformat_input_t * input,
3340 vlib_cli_command_t * cmd)
3341{
3342 snat_main_t *sm = &snat_main;
3343 unformat_input_t _line_input, *line_input = &_line_input;
3344 ip4_address_t out_addr, ext_addr, in_addr;
Aequitasfc4510b2017-09-23 12:58:49 +08003345 u32 out_port, ext_port;
Martin Gálik6bc8c642017-04-19 01:12:27 -07003346 snat_det_map_t * dm;
3347 snat_det_session_t * ses;
3348 snat_det_out_key_t key;
3349 clib_error_t *error = 0;
3350
3351 /* Get a line of input. */
3352 if (!unformat_user (input, unformat_line_input, line_input))
3353 return 0;
3354
3355 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3356 {
3357 if (unformat (line_input, "%U:%d %U:%d",
3358 unformat_ip4_address, &out_addr, &out_port,
3359 unformat_ip4_address, &ext_addr, &ext_port))
3360 ;
3361 else
3362 {
3363 error = clib_error_return (0, "unknown input '%U'",
3364 format_unformat_error, line_input);
3365 goto done;
3366 }
3367 }
3368
3369 unformat_free (line_input);
3370
3371 dm = snat_det_map_by_out(sm, &out_addr);
3372 if (!dm)
3373 vlib_cli_output (vm, "no match");
3374 else
3375 {
Aequitasfc4510b2017-09-23 12:58:49 +08003376 snat_det_reverse(dm, &ext_addr, (u16)out_port, &in_addr);
Martin Gálik6bc8c642017-04-19 01:12:27 -07003377 key.ext_host_addr = out_addr;
Aequitasfc4510b2017-09-23 12:58:49 +08003378 key.ext_host_port = ntohs((u16)ext_port);
3379 key.out_port = ntohs((u16)out_port);
Martin Gálik6bc8c642017-04-19 01:12:27 -07003380 ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
3381 if (!ses)
3382 vlib_cli_output (vm, "no match");
3383 else
3384 snat_det_ses_close(dm, ses);
3385 }
3386
3387done:
3388 unformat_free (line_input);
3389
3390 return error;
3391}
3392
3393/*?
3394 * @cliexpar
3395 * @cliexstart{snat deterministic close session out}
3396 * Close session using outside ip address and port
3397 * and external ip address and port, use:
Matus Fabian2ba92e32017-08-21 07:05:03 -07003398 * vpp# nat44 deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
Martin Gálik6bc8c642017-04-19 01:12:27 -07003399 * @cliexend
3400?*/
3401VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07003402 .path = "nat44 deterministic close session out",
3403 .short_help = "nat44 deterministic close session out "
Martin Gálik6bc8c642017-04-19 01:12:27 -07003404 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
3405 .function = snat_det_close_session_out_fn,
3406};
3407
3408static clib_error_t *
3409snat_det_close_session_in_fn (vlib_main_t *vm,
3410 unformat_input_t * input,
3411 vlib_cli_command_t * cmd)
3412{
3413 snat_main_t *sm = &snat_main;
3414 unformat_input_t _line_input, *line_input = &_line_input;
3415 ip4_address_t in_addr, ext_addr;
Aequitasfc4510b2017-09-23 12:58:49 +08003416 u32 in_port, ext_port;
Martin Gálik6bc8c642017-04-19 01:12:27 -07003417 snat_det_map_t * dm;
3418 snat_det_session_t * ses;
3419 snat_det_out_key_t key;
3420 clib_error_t *error = 0;
3421
3422 /* Get a line of input. */
3423 if (!unformat_user (input, unformat_line_input, line_input))
3424 return 0;
3425
3426 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3427 {
3428 if (unformat (line_input, "%U:%d %U:%d",
3429 unformat_ip4_address, &in_addr, &in_port,
3430 unformat_ip4_address, &ext_addr, &ext_port))
3431 ;
3432 else
3433 {
3434 error = clib_error_return (0, "unknown input '%U'",
3435 format_unformat_error, line_input);
3436 goto done;
3437 }
3438 }
3439
3440 unformat_free (line_input);
3441
3442 dm = snat_det_map_by_user (sm, &in_addr);
3443 if (!dm)
3444 vlib_cli_output (vm, "no match");
3445 else
3446 {
3447 key.ext_host_addr = ext_addr;
Aequitasfc4510b2017-09-23 12:58:49 +08003448 key.ext_host_port = ntohs ((u16)ext_port);
3449 ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs((u16)in_port), key);
Martin Gálik6bc8c642017-04-19 01:12:27 -07003450 if (!ses)
3451 vlib_cli_output (vm, "no match");
3452 else
3453 snat_det_ses_close(dm, ses);
3454 }
3455
3456done:
3457 unformat_free(line_input);
3458
3459 return error;
3460}
3461
3462/*?
3463 * @cliexpar
3464 * @cliexstart{snat deterministic close_session_in}
3465 * Close session using inside ip address and port
3466 * and external ip address and port, use:
Matus Fabian2ba92e32017-08-21 07:05:03 -07003467 * vpp# nat44 deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
Martin Gálik6bc8c642017-04-19 01:12:27 -07003468 * @cliexend
3469?*/
3470VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
Matus Fabian2ba92e32017-08-21 07:05:03 -07003471 .path = "nat44 deterministic close session in",
3472 .short_help = "nat44 deterministic close session in "
Martin Gálik6bc8c642017-04-19 01:12:27 -07003473 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
3474 .function = snat_det_close_session_in_fn,
3475};