2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vlibmemory/api.h>
17 #include <cnat/cnat_node.h>
18 #include <cnat/cnat_translation.h>
19 #include <cnat/cnat_inline.h>
20 #include <cnat/cnat_src_policy.h>
21 #include <cnat/cnat_snat_policy.h>
23 #include <vnet/dpo/load_balance.h>
24 #include <vnet/dpo/load_balance_map.h>
26 #include <vnet/ip/ip4_inlines.h>
27 #include <vnet/ip/ip6_inlines.h>
29 typedef enum cnat_feature_next_
31 CNAT_FEATURE_NEXT_DROP,
33 } cnat_feature_next_t;
35 vlib_node_registration_t cnat_input_feature_ip4_node;
36 vlib_node_registration_t cnat_input_feature_ip6_node;
37 vlib_node_registration_t cnat_output_feature_ip4_node;
38 vlib_node_registration_t cnat_output_feature_ip6_node;
41 cnat_input_feature_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
42 vlib_buffer_t *b, cnat_node_ctx_t *ctx,
43 int session_not_found, cnat_session_t *session)
45 vlib_combined_counter_main_t *cntm = &cnat_translation_counters;
46 const cnat_translation_t *ct = NULL;
47 ip4_header_t *ip4 = NULL;
49 ip6_header_t *ip6 = NULL;
56 /* By default follow arc default next */
57 vnet_feature_next (&next0, b);
59 if (AF_IP4 == ctx->af)
61 ip4 = vlib_buffer_get_current (b);
62 iproto = ip4->protocol;
63 udp0 = (udp_header_t *) (ip4 + 1);
64 cc = cnat_client_ip4_find (
65 &ip4->dst_address); /* TODO do this only if no session? */
69 ip6 = vlib_buffer_get_current (b);
70 iproto = ip6->protocol;
71 udp0 = (udp_header_t *) (ip6 + 1);
72 cc = cnat_client_ip6_find (&ip6->dst_address); /* TODO: same as above */
75 /* Wrong session key */
76 if (session->key.cs_proto == 0)
79 if (!session_not_found)
80 /* session table hit */
81 cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
83 goto trace; /* dst address is not a vip */
86 ct = cnat_find_translation (
87 cc->parent_cci, clib_host_to_net_u16 (udp0->dst_port), iproto);
90 /* TODO: create identity session to avoid slowpath ? */
93 /* New flow, create the sessions */
94 const load_balance_t *lb0;
96 u32 rsession_flags = CNAT_SESSION_FLAG_NO_CLIENT;
99 lb0 = load_balance_get (ct->ct_lb.dpoi_index);
100 if (!lb0->lb_n_buckets)
101 /* Can't translate TODO: should drop / reject? */
104 /* session table miss */
105 trk0 = cnat_load_balance (ct, ctx->af, ip4, ip6, &dpoi_index);
106 if (PREDICT_FALSE (NULL == trk0))
108 /* Dont translate & Follow the fib programming */
109 vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
110 next0 = cc->cc_parent.dpoi_next_node;
114 ip46_address_copy (&session->value.cs_ip[VLIB_TX],
115 &trk0->ct_ep[VLIB_TX].ce_ip.ip);
117 /* never source nat in this node */
118 if (AF_IP4 == ctx->af)
119 ip46_address_set_ip4 (&session->value.cs_ip[VLIB_RX],
122 ip46_address_set_ip6 (&session->value.cs_ip[VLIB_RX],
125 session->value.cs_port[VLIB_TX] =
126 clib_host_to_net_u16 (trk0->ct_ep[VLIB_TX].ce_port);
127 session->value.cs_port[VLIB_RX] = udp0->src_port;
129 const dpo_id_t *dpo0;
130 const load_balance_t *lb1;
131 fib_entry_t *fib_entry;
132 fib_entry = fib_entry_get (trk0->ct_fei);
134 lb1 = load_balance_get (fib_entry->fe_lb /*[fct] */.dpoi_index);
135 dpo0 = load_balance_get_bucket_i (lb1, 0);
137 session->value.dpoi_next_node = dpo0->dpoi_next_node;
138 session->value.cs_lbi = dpo0->dpoi_index;
140 if (trk0->ct_flags & CNAT_TRK_FLAG_NO_NAT)
141 session->value.flags |= CNAT_SESSION_FLAG_NO_NAT;
143 /* refcnt session in current client */
144 cnat_client_cnt_session (cc);
145 cnat_session_create (session, ctx, CNAT_LOCATION_OUTPUT, rsession_flags);
146 trace_flags |= CNAT_TRACE_SESSION_CREATED;
149 next0 = session->value.dpoi_next_node;
150 vnet_buffer (b)->ip.adj_index[VLIB_TX] = session->value.cs_lbi;
152 if (session->value.flags & CNAT_SESSION_FLAG_NO_NAT)
155 if (AF_IP4 == ctx->af)
156 cnat_translation_ip4 (session, ip4, udp0);
158 cnat_translation_ip6 (session, ip6, udp0);
162 cti = ct - cnat_translation_pool;
163 vlib_increment_combined_counter (cntm, ctx->thread_index, cti, 1,
164 vlib_buffer_length_in_chain (vm, b));
168 if (PREDICT_FALSE (ctx->do_trace))
170 trace_flags |= session_not_found ? 0 : CNAT_TRACE_SESSION_FOUND;
171 cnat_add_trace (vm, node, b, session, ct, trace_flags);
176 VLIB_NODE_FN (cnat_input_feature_ip4_node)
177 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
179 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
180 return cnat_node_inline (vm, node, frame, cnat_input_feature_fn, AF_IP4,
181 CNAT_LOCATION_INPUT, 1 /* do_trace */);
182 return cnat_node_inline (vm, node, frame, cnat_input_feature_fn, AF_IP4,
183 CNAT_LOCATION_INPUT, 0 /* do_trace */);
186 VLIB_REGISTER_NODE (cnat_input_feature_ip4_node) = {
187 .name = "cnat-input-ip4",
188 .vector_size = sizeof (u32),
189 .format_trace = format_cnat_trace,
190 .type = VLIB_NODE_TYPE_INTERNAL,
191 .n_errors = CNAT_N_ERROR,
192 .error_strings = cnat_error_strings,
193 .n_next_nodes = IP_LOOKUP_N_NEXT,
194 .next_nodes = IP4_LOOKUP_NEXT_NODES,
197 VNET_FEATURE_INIT (cnat_in_ip4_feature, static) = {
198 .arc_name = "ip4-unicast",
199 .node_name = "cnat-input-ip4",
200 .runs_before = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
203 VLIB_NODE_FN (cnat_input_feature_ip6_node)
204 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
206 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
207 return cnat_node_inline (vm, node, frame, cnat_input_feature_fn, AF_IP6,
208 CNAT_LOCATION_INPUT, 1 /* do_trace */);
209 return cnat_node_inline (vm, node, frame, cnat_input_feature_fn, AF_IP6,
210 CNAT_LOCATION_INPUT, 0 /* do_trace */);
213 VLIB_REGISTER_NODE (cnat_input_feature_ip6_node) = {
214 .name = "cnat-input-ip6",
215 .vector_size = sizeof (u32),
216 .format_trace = format_cnat_trace,
217 .type = VLIB_NODE_TYPE_INTERNAL,
218 .n_errors = CNAT_N_ERROR,
219 .error_strings = cnat_error_strings,
220 .n_next_nodes = IP6_LOOKUP_N_NEXT,
221 .next_nodes = IP6_LOOKUP_NEXT_NODES,
224 VNET_FEATURE_INIT (cnat_in_ip6_feature, static) = {
225 .arc_name = "ip6-unicast",
226 .node_name = "cnat-input-ip6",
227 .runs_before = VNET_FEATURES ("acl-plugin-in-ip6-fa"),
230 /* output feature node, creates snat sessions when required and
231 * translates back for existing sessions */
233 cnat_output_feature_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
234 vlib_buffer_t *b, cnat_node_ctx_t *ctx,
235 int session_not_found, cnat_session_t *session)
237 cnat_snat_policy_main_t *cpm = &cnat_snat_policy_main;
238 ip4_header_t *ip4 = NULL;
239 ip_protocol_t iproto;
240 ip6_header_t *ip6 = NULL;
249 /* By default follow arc default next */
250 vnet_feature_next (&next0, b);
251 iph_offset = vnet_buffer (b)->ip.save_rewrite_length;
253 if (AF_IP4 == ctx->af)
255 ip4 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b) + iph_offset);
256 iproto = ip4->protocol;
257 udp0 = (udp_header_t *) (ip4 + 1);
261 ip6 = (ip6_header_t *) ((u8 *) vlib_buffer_get_current (b) + iph_offset);
262 iproto = ip6->protocol;
263 udp0 = (udp_header_t *) (ip6 + 1);
266 /* Wrong session key */
267 if (session->key.cs_proto == 0)
270 if (!session_not_found)
272 /* session table hit */
273 cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
275 else if (!cpm->snat_policy)
279 do_snat = cpm->snat_policy (b, session);
283 if (AF_IP4 == ctx->af)
285 if (ip_address_is_zero (&cpm->snat_ip4.ce_ip))
288 ip46_address_set_ip4 (&session->value.cs_ip[VLIB_RX],
289 &ip_addr_v4 (&cpm->snat_ip4.ce_ip));
290 ip46_address_set_ip4 (&session->value.cs_ip[VLIB_TX],
295 if (ip_address_is_zero (&cpm->snat_ip6.ce_ip))
298 ip46_address_set_ip6 (&session->value.cs_ip[VLIB_RX],
299 &ip_addr_v6 (&cpm->snat_ip6.ce_ip));
300 ip46_address_set_ip6 (&session->value.cs_ip[VLIB_TX],
304 rv = cnat_allocate_port (&sport, iproto);
307 vlib_node_increment_counter (vm, cnat_output_feature_ip6_node.index,
308 CNAT_ERROR_EXHAUSTED_PORTS, 1);
309 next0 = CNAT_FEATURE_NEXT_DROP;
312 session->value.cs_port[VLIB_RX] = sport;
313 session->value.cs_port[VLIB_TX] = sport;
314 if (iproto == IP_PROTOCOL_TCP || iproto == IP_PROTOCOL_UDP)
315 session->value.cs_port[VLIB_TX] = udp0->dst_port;
317 session->value.cs_lbi = INDEX_INVALID;
318 session->value.flags =
319 CNAT_SESSION_FLAG_NO_CLIENT | CNAT_SESSION_FLAG_ALLOC_PORT;
321 trace_flags |= CNAT_TRACE_SESSION_CREATED;
322 cnat_session_create (session, ctx, CNAT_LOCATION_INPUT,
323 CNAT_SESSION_FLAG_NO_CLIENT);
326 if (AF_IP4 == ctx->af)
327 cnat_translation_ip4 (session, ip4, udp0);
329 cnat_translation_ip6 (session, ip6, udp0);
332 if (PREDICT_FALSE (ctx->do_trace))
334 trace_flags |= do_snat ? 0 : CNAT_TRACE_NO_NAT;
335 trace_flags |= session_not_found ? 0 : CNAT_TRACE_SESSION_FOUND;
336 cnat_add_trace (vm, node, b, session, NULL, trace_flags);
341 VLIB_NODE_FN (cnat_output_feature_ip4_node)
342 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
344 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
345 return cnat_node_inline (vm, node, frame, cnat_output_feature_fn, AF_IP4,
346 CNAT_LOCATION_OUTPUT, 1 /* do_trace */);
347 return cnat_node_inline (vm, node, frame, cnat_output_feature_fn, AF_IP4,
348 CNAT_LOCATION_OUTPUT, 0 /* do_trace */);
351 VLIB_REGISTER_NODE (cnat_output_feature_ip4_node) = {
352 .name = "cnat-output-ip4",
353 .vector_size = sizeof (u32),
354 .format_trace = format_cnat_trace,
355 .type = VLIB_NODE_TYPE_INTERNAL,
356 .n_errors = CNAT_N_ERROR,
357 .error_strings = cnat_error_strings,
358 .n_next_nodes = CNAT_FEATURE_N_NEXT,
360 [CNAT_FEATURE_NEXT_DROP] = "error-drop",
364 VNET_FEATURE_INIT (cnat_out_ip4_feature, static) = {
365 .arc_name = "ip4-output",
366 .node_name = "cnat-output-ip4",
367 .runs_before = VNET_FEATURES ("gso-ip4"),
368 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
371 VLIB_NODE_FN (cnat_output_feature_ip6_node)
372 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
374 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
375 return cnat_node_inline (vm, node, frame, cnat_output_feature_fn, AF_IP6,
376 CNAT_LOCATION_OUTPUT, 1 /* do_trace */);
377 return cnat_node_inline (vm, node, frame, cnat_output_feature_fn, AF_IP6,
378 CNAT_LOCATION_OUTPUT, 0 /* do_trace */);
381 VLIB_REGISTER_NODE (cnat_output_feature_ip6_node) = {
382 .name = "cnat-output-ip6",
383 .vector_size = sizeof (u32),
384 .format_trace = format_cnat_trace,
385 .type = VLIB_NODE_TYPE_INTERNAL,
386 .n_errors = CNAT_N_ERROR,
387 .error_strings = cnat_error_strings,
388 .n_next_nodes = CNAT_FEATURE_N_NEXT,
390 [CNAT_FEATURE_NEXT_DROP] = "error-drop",
394 VNET_FEATURE_INIT (cnat_out_ip6_feature, static) = {
395 .arc_name = "ip6-output",
396 .node_name = "cnat-output-ip6",
397 .runs_before = VNET_FEATURES ("gso-ip6"),
398 .runs_after = VNET_FEATURES ("acl-plugin-out-ip6-fa"),
402 * fd.io coding-style-patch-verification: ON
405 * eval: (c-set-style "gnu")