Line data Source code
1 : /*
2 : * Copyright (c) 2020 Cisco and/or its affiliates.
3 : * Licensed under the Apache License, Version 2.0 (the "License");
4 : * you may not use this file except in compliance with the License.
5 : * You may obtain a copy of the License at:
6 : *
7 : * http://www.apache.org/licenses/LICENSE-2.0
8 : *
9 : * Unless required by applicable law or agreed to in writing, software
10 : * distributed under the License is distributed on an "AS IS" BASIS,
11 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : * See the License for the specific language governing permissions and
13 : * limitations under the License.
14 : */
15 :
16 : #include <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>
22 :
23 : #include <vnet/dpo/load_balance.h>
24 : #include <vnet/dpo/load_balance_map.h>
25 :
26 : #include <vnet/ip/ip4_inlines.h>
27 : #include <vnet/ip/ip6_inlines.h>
28 :
29 : typedef enum cnat_feature_next_
30 : {
31 : CNAT_FEATURE_NEXT_DROP,
32 : CNAT_FEATURE_N_NEXT,
33 : } cnat_feature_next_t;
34 :
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;
39 :
40 : always_inline uword
41 0 : 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)
44 : {
45 0 : vlib_combined_counter_main_t *cntm = &cnat_translation_counters;
46 0 : const cnat_translation_t *ct = NULL;
47 0 : ip4_header_t *ip4 = NULL;
48 : ip_protocol_t iproto;
49 0 : ip6_header_t *ip6 = NULL;
50 : udp_header_t *udp0;
51 : cnat_client_t *cc;
52 : u32 next0;
53 : index_t cti;
54 0 : u8 trace_flags = 0;
55 :
56 : /* By default follow arc default next */
57 0 : vnet_feature_next (&next0, b);
58 :
59 0 : if (AF_IP4 == ctx->af)
60 : {
61 0 : ip4 = vlib_buffer_get_current (b);
62 0 : iproto = ip4->protocol;
63 0 : udp0 = (udp_header_t *) (ip4 + 1);
64 0 : cc = cnat_client_ip4_find (
65 0 : &ip4->dst_address); /* TODO do this only if no session? */
66 : }
67 : else
68 : {
69 0 : ip6 = vlib_buffer_get_current (b);
70 0 : iproto = ip6->protocol;
71 0 : udp0 = (udp_header_t *) (ip6 + 1);
72 0 : cc = cnat_client_ip6_find (&ip6->dst_address); /* TODO: same as above */
73 : }
74 :
75 : /* Wrong session key */
76 0 : if (session->key.cs_proto == 0)
77 0 : goto trace;
78 :
79 0 : if (!session_not_found)
80 : /* session table hit */
81 0 : cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
82 0 : else if (!cc)
83 0 : goto trace; /* dst address is not a vip */
84 : else
85 : {
86 0 : ct = cnat_find_translation (
87 0 : cc->parent_cci, clib_host_to_net_u16 (udp0->dst_port), iproto);
88 0 : if (NULL == ct)
89 : /* Dont translate */
90 : /* TODO: create identity session to avoid slowpath ? */
91 0 : goto trace;
92 :
93 : /* New flow, create the sessions */
94 : const load_balance_t *lb0;
95 : cnat_ep_trk_t *trk0;
96 0 : u32 rsession_flags = CNAT_SESSION_FLAG_NO_CLIENT;
97 0 : u32 dpoi_index = -1;
98 :
99 0 : lb0 = load_balance_get (ct->ct_lb.dpoi_index);
100 0 : if (!lb0->lb_n_buckets)
101 : /* Can't translate TODO: should drop / reject? */
102 0 : goto trace;
103 :
104 : /* session table miss */
105 0 : trk0 = cnat_load_balance (ct, ctx->af, ip4, ip6, &dpoi_index);
106 0 : if (PREDICT_FALSE (NULL == trk0))
107 : {
108 : /* Dont translate & Follow the fib programming */
109 0 : vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
110 0 : next0 = cc->cc_parent.dpoi_next_node;
111 0 : goto trace;
112 : }
113 :
114 0 : ip46_address_copy (&session->value.cs_ip[VLIB_TX],
115 0 : &trk0->ct_ep[VLIB_TX].ce_ip.ip);
116 :
117 : /* never source nat in this node */
118 0 : if (AF_IP4 == ctx->af)
119 0 : ip46_address_set_ip4 (&session->value.cs_ip[VLIB_RX],
120 0 : &ip4->src_address);
121 : else
122 0 : ip46_address_set_ip6 (&session->value.cs_ip[VLIB_RX],
123 0 : &ip6->src_address);
124 :
125 0 : session->value.cs_port[VLIB_TX] =
126 0 : clib_host_to_net_u16 (trk0->ct_ep[VLIB_TX].ce_port);
127 0 : session->value.cs_port[VLIB_RX] = udp0->src_port;
128 0 : session->value.flags = 0;
129 :
130 0 : if (trk0->ct_flags & CNAT_TRK_FLAG_NO_NAT)
131 : {
132 : const dpo_id_t *dpo0;
133 : const load_balance_t *lb1;
134 :
135 0 : lb1 = load_balance_get (trk0->ct_dpo.dpoi_index);
136 : /* Assume backend has exactly one item in LB */
137 0 : dpo0 = load_balance_get_bucket_i (lb1, 0);
138 :
139 0 : session->value.dpoi_next_node = dpo0->dpoi_next_node;
140 0 : session->value.cs_lbi = dpo0->dpoi_index;
141 0 : session->value.flags = CNAT_SESSION_FLAG_NO_NAT;
142 : }
143 :
144 : /* refcnt session in current client */
145 0 : cnat_client_cnt_session (cc);
146 0 : cnat_session_create (session, ctx);
147 0 : if (!(ct->flags & CNAT_TR_FLAG_NO_RETURN_SESSION))
148 0 : cnat_rsession_create (session, ctx, CNAT_LOCATION_OUTPUT,
149 : rsession_flags);
150 0 : trace_flags |= CNAT_TRACE_SESSION_CREATED;
151 : }
152 :
153 0 : if (session->value.flags & CNAT_SESSION_FLAG_NO_NAT)
154 : {
155 : /* If we don't translate, directly do the lookup & bypass arc */
156 0 : next0 = session->value.dpoi_next_node;
157 0 : vnet_buffer (b)->ip.adj_index[VLIB_TX] = session->value.cs_lbi;
158 0 : goto trace;
159 : }
160 :
161 0 : if (AF_IP4 == ctx->af)
162 0 : cnat_translation_ip4 (session, ip4, udp0, vnet_buffer (b)->oflags);
163 : else
164 0 : cnat_translation_ip6 (session, ip6, udp0, vnet_buffer (b)->oflags);
165 :
166 0 : if (NULL != ct)
167 : {
168 0 : cti = ct - cnat_translation_pool;
169 0 : vlib_increment_combined_counter (cntm, ctx->thread_index, cti, 1,
170 : vlib_buffer_length_in_chain (vm, b));
171 : }
172 :
173 0 : trace:
174 0 : if (PREDICT_FALSE (ctx->do_trace))
175 : {
176 0 : trace_flags |= session_not_found ? 0 : CNAT_TRACE_SESSION_FOUND;
177 0 : cnat_add_trace (vm, node, b, session, ct, trace_flags);
178 : }
179 0 : return next0;
180 : }
181 :
182 575 : VLIB_NODE_FN (cnat_input_feature_ip4_node)
183 : (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
184 : {
185 0 : if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
186 0 : return cnat_node_inline (vm, node, frame, cnat_input_feature_fn, AF_IP4,
187 : CNAT_LOCATION_INPUT, 1 /* do_trace */);
188 0 : return cnat_node_inline (vm, node, frame, cnat_input_feature_fn, AF_IP4,
189 : CNAT_LOCATION_INPUT, 0 /* do_trace */);
190 : }
191 :
192 164204 : VLIB_REGISTER_NODE (cnat_input_feature_ip4_node) = {
193 : .name = "cnat-input-ip4",
194 : .vector_size = sizeof (u32),
195 : .format_trace = format_cnat_trace,
196 : .type = VLIB_NODE_TYPE_INTERNAL,
197 : .n_errors = CNAT_N_ERROR,
198 : .error_strings = cnat_error_strings,
199 : .sibling_of = "ip4-lookup",
200 : };
201 :
202 64539 : VNET_FEATURE_INIT (cnat_in_ip4_feature, static) = {
203 : .arc_name = "ip4-unicast",
204 : .node_name = "cnat-input-ip4",
205 : .runs_before = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
206 : };
207 :
208 575 : VLIB_NODE_FN (cnat_input_feature_ip6_node)
209 : (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
210 : {
211 0 : if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
212 0 : return cnat_node_inline (vm, node, frame, cnat_input_feature_fn, AF_IP6,
213 : CNAT_LOCATION_INPUT, 1 /* do_trace */);
214 0 : return cnat_node_inline (vm, node, frame, cnat_input_feature_fn, AF_IP6,
215 : CNAT_LOCATION_INPUT, 0 /* do_trace */);
216 : }
217 :
218 164204 : VLIB_REGISTER_NODE (cnat_input_feature_ip6_node) = {
219 : .name = "cnat-input-ip6",
220 : .vector_size = sizeof (u32),
221 : .format_trace = format_cnat_trace,
222 : .type = VLIB_NODE_TYPE_INTERNAL,
223 : .n_errors = CNAT_N_ERROR,
224 : .error_strings = cnat_error_strings,
225 : .sibling_of = "ip6-lookup",
226 : };
227 :
228 64539 : VNET_FEATURE_INIT (cnat_in_ip6_feature, static) = {
229 : .arc_name = "ip6-unicast",
230 : .node_name = "cnat-input-ip6",
231 : .runs_before = VNET_FEATURES ("acl-plugin-in-ip6-fa"),
232 : };
233 :
234 : /* output feature node, creates snat sessions when required and
235 : * translates back for existing sessions */
236 : always_inline uword
237 0 : cnat_output_feature_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
238 : vlib_buffer_t *b, cnat_node_ctx_t *ctx,
239 : int session_not_found, cnat_session_t *session)
240 : {
241 0 : cnat_snat_policy_main_t *cpm = &cnat_snat_policy_main;
242 0 : ip4_header_t *ip4 = NULL;
243 : ip_protocol_t iproto;
244 0 : ip6_header_t *ip6 = NULL;
245 : udp_header_t *udp0;
246 0 : u32 iph_offset = 0;
247 : u32 next0;
248 : u16 sport;
249 0 : u8 do_snat = 0;
250 0 : u8 trace_flags = 0;
251 : int rv;
252 :
253 : /* By default follow arc default next */
254 0 : vnet_feature_next (&next0, b);
255 0 : iph_offset = vnet_buffer (b)->ip.save_rewrite_length;
256 :
257 0 : if (AF_IP4 == ctx->af)
258 : {
259 0 : ip4 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b) + iph_offset);
260 0 : iproto = ip4->protocol;
261 0 : udp0 = (udp_header_t *) (ip4 + 1);
262 : }
263 : else
264 : {
265 0 : ip6 = (ip6_header_t *) ((u8 *) vlib_buffer_get_current (b) + iph_offset);
266 0 : iproto = ip6->protocol;
267 0 : udp0 = (udp_header_t *) (ip6 + 1);
268 : }
269 :
270 : /* Wrong session key */
271 0 : if (session->key.cs_proto == 0)
272 0 : goto trace;
273 :
274 0 : if (!session_not_found)
275 : {
276 : /* session table hit */
277 0 : cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
278 : }
279 0 : else if (!cpm->snat_policy)
280 0 : goto trace;
281 : else
282 : {
283 0 : do_snat = cpm->snat_policy (b, session);
284 0 : if (do_snat != 1)
285 0 : goto trace;
286 :
287 0 : if (AF_IP4 == ctx->af)
288 : {
289 0 : if (ip_address_is_zero (&cpm->snat_ip4.ce_ip))
290 0 : goto trace;
291 :
292 0 : ip46_address_set_ip4 (&session->value.cs_ip[VLIB_RX],
293 0 : &ip_addr_v4 (&cpm->snat_ip4.ce_ip));
294 0 : ip46_address_set_ip4 (&session->value.cs_ip[VLIB_TX],
295 0 : &ip4->dst_address);
296 : }
297 : else
298 : {
299 0 : if (ip_address_is_zero (&cpm->snat_ip6.ce_ip))
300 0 : goto trace;
301 :
302 0 : ip46_address_set_ip6 (&session->value.cs_ip[VLIB_RX],
303 0 : &ip_addr_v6 (&cpm->snat_ip6.ce_ip));
304 0 : ip46_address_set_ip6 (&session->value.cs_ip[VLIB_TX],
305 0 : &ip6->dst_address);
306 : }
307 0 : sport = 0;
308 0 : rv = cnat_allocate_port (&sport, iproto);
309 0 : if (rv)
310 : {
311 0 : vlib_node_increment_counter (vm, cnat_output_feature_ip6_node.index,
312 : CNAT_ERROR_EXHAUSTED_PORTS, 1);
313 0 : next0 = CNAT_FEATURE_NEXT_DROP;
314 0 : goto trace;
315 : }
316 0 : session->value.cs_port[VLIB_RX] = sport;
317 0 : session->value.cs_port[VLIB_TX] = sport;
318 0 : if (iproto == IP_PROTOCOL_TCP || iproto == IP_PROTOCOL_UDP)
319 0 : session->value.cs_port[VLIB_TX] = udp0->dst_port;
320 :
321 0 : session->value.cs_lbi = INDEX_INVALID;
322 0 : session->value.flags =
323 : CNAT_SESSION_FLAG_NO_CLIENT | CNAT_SESSION_FLAG_ALLOC_PORT;
324 :
325 0 : trace_flags |= CNAT_TRACE_SESSION_CREATED;
326 :
327 0 : cnat_session_create (session, ctx);
328 0 : cnat_rsession_create (session, ctx, CNAT_LOCATION_INPUT,
329 : CNAT_SESSION_FLAG_NO_CLIENT |
330 : CNAT_SESSION_RETRY_SNAT);
331 : }
332 :
333 0 : if (AF_IP4 == ctx->af)
334 0 : cnat_translation_ip4 (session, ip4, udp0, vnet_buffer (b)->oflags);
335 : else
336 0 : cnat_translation_ip6 (session, ip6, udp0, vnet_buffer (b)->oflags);
337 :
338 0 : trace:
339 0 : if (PREDICT_FALSE (ctx->do_trace))
340 : {
341 0 : trace_flags |= do_snat ? 0 : CNAT_TRACE_NO_NAT;
342 0 : trace_flags |= session_not_found ? 0 : CNAT_TRACE_SESSION_FOUND;
343 0 : cnat_add_trace (vm, node, b, session, NULL, trace_flags);
344 : }
345 0 : return next0;
346 : }
347 :
348 575 : VLIB_NODE_FN (cnat_output_feature_ip4_node)
349 : (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
350 : {
351 0 : if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
352 0 : return cnat_node_inline (vm, node, frame, cnat_output_feature_fn, AF_IP4,
353 : CNAT_LOCATION_OUTPUT, 1 /* do_trace */);
354 0 : return cnat_node_inline (vm, node, frame, cnat_output_feature_fn, AF_IP4,
355 : CNAT_LOCATION_OUTPUT, 0 /* do_trace */);
356 : }
357 :
358 164204 : VLIB_REGISTER_NODE (cnat_output_feature_ip4_node) = {
359 : .name = "cnat-output-ip4",
360 : .vector_size = sizeof (u32),
361 : .format_trace = format_cnat_trace,
362 : .type = VLIB_NODE_TYPE_INTERNAL,
363 : .n_errors = CNAT_N_ERROR,
364 : .error_strings = cnat_error_strings,
365 : .n_next_nodes = CNAT_FEATURE_N_NEXT,
366 : .next_nodes = {
367 : [CNAT_FEATURE_NEXT_DROP] = "error-drop",
368 : },
369 : };
370 :
371 64539 : VNET_FEATURE_INIT (cnat_out_ip4_feature, static) = {
372 : .arc_name = "ip4-output",
373 : .node_name = "cnat-output-ip4",
374 : .runs_before = VNET_FEATURES ("gso-ip4"),
375 : .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
376 : };
377 :
378 575 : VLIB_NODE_FN (cnat_output_feature_ip6_node)
379 : (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
380 : {
381 0 : if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
382 0 : return cnat_node_inline (vm, node, frame, cnat_output_feature_fn, AF_IP6,
383 : CNAT_LOCATION_OUTPUT, 1 /* do_trace */);
384 0 : return cnat_node_inline (vm, node, frame, cnat_output_feature_fn, AF_IP6,
385 : CNAT_LOCATION_OUTPUT, 0 /* do_trace */);
386 : }
387 :
388 164204 : VLIB_REGISTER_NODE (cnat_output_feature_ip6_node) = {
389 : .name = "cnat-output-ip6",
390 : .vector_size = sizeof (u32),
391 : .format_trace = format_cnat_trace,
392 : .type = VLIB_NODE_TYPE_INTERNAL,
393 : .n_errors = CNAT_N_ERROR,
394 : .error_strings = cnat_error_strings,
395 : .n_next_nodes = CNAT_FEATURE_N_NEXT,
396 : .next_nodes = {
397 : [CNAT_FEATURE_NEXT_DROP] = "error-drop",
398 : },
399 : };
400 :
401 64539 : VNET_FEATURE_INIT (cnat_out_ip6_feature, static) = {
402 : .arc_name = "ip6-output",
403 : .node_name = "cnat-output-ip6",
404 : .runs_before = VNET_FEATURES ("gso-ip6"),
405 : .runs_after = VNET_FEATURES ("acl-plugin-out-ip6-fa"),
406 : };
407 :
408 : /*
409 : * fd.io coding-style-patch-verification: ON
410 : *
411 : * Local Variables:
412 : * eval: (c-set-style "gnu")
413 : * End:
414 : */
|