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