cnat: Fix session with deleted tr
[vpp.git] / src / plugins / cnat / cnat_node_vip.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
22 #include <vnet/dpo/load_balance.h>
23 #include <vnet/dpo/load_balance_map.h>
24
25 #include <vnet/ip/ip4_inlines.h>
26 #include <vnet/ip/ip6_inlines.h>
27
28 typedef struct cnat_translation_trace_t_
29 {
30   cnat_session_t session;
31   cnat_translation_t tr;
32   u32 found_session;
33   u32 created_session;
34   u32 has_tr;
35 } cnat_translation_trace_t;
36
37 typedef enum cnat_translation_next_t_
38 {
39   CNAT_TRANSLATION_NEXT_DROP,
40   CNAT_TRANSLATION_NEXT_LOOKUP,
41   CNAT_TRANSLATION_N_NEXT,
42 } cnat_translation_next_t;
43
44 vlib_node_registration_t cnat_vip_ip4_node;
45 vlib_node_registration_t cnat_vip_ip6_node;
46
47 static u8 *
48 format_cnat_translation_trace (u8 * s, va_list * args)
49 {
50   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52   cnat_translation_trace_t *t = va_arg (*args, cnat_translation_trace_t *);
53
54   if (t->found_session)
55     s = format (s, "found: %U", format_cnat_session, &t->session, 1);
56   else if (t->created_session)
57     s = format (s, "created: %U\n  tr: %U",
58                 format_cnat_session, &t->session, 1,
59                 format_cnat_translation, &t->tr, 0);
60   else if (t->has_tr)
61     s = format (s, "tr pass: %U", format_cnat_translation, &t->tr, 0);
62   else
63     s = format (s, "not found");
64   return s;
65 }
66
67 /* CNat sub for NAT behind a fib entry (VIP or interposed real IP) */
68 static uword
69 cnat_vip_node_fn (vlib_main_t * vm,
70                   vlib_node_runtime_t * node,
71                   vlib_buffer_t * b,
72                   cnat_node_ctx_t * ctx, int rv, cnat_session_t * session)
73 {
74   vlib_combined_counter_main_t *cntm = &cnat_translation_counters;
75   const cnat_translation_t *ct = NULL;
76   ip4_header_t *ip4 = NULL;
77   ip_protocol_t iproto;
78   ip6_header_t *ip6 = NULL;
79   udp_header_t *udp0;
80   cnat_client_t *cc;
81   u16 next0;
82   index_t cti;
83   int created_session = 0;
84   cnat_src_policy_main_t *cspm = &cnat_src_policy_main;
85   if (AF_IP4 == ctx->af)
86     {
87       ip4 = vlib_buffer_get_current (b);
88       iproto = ip4->protocol;
89       udp0 = (udp_header_t *) (ip4 + 1);
90     }
91   else
92     {
93       ip6 = vlib_buffer_get_current (b);
94       iproto = ip6->protocol;
95       udp0 = (udp_header_t *) (ip6 + 1);
96     }
97
98   cc = cnat_client_get (vnet_buffer (b)->ip.adj_index[VLIB_TX]);
99
100   if (iproto != IP_PROTOCOL_UDP && iproto != IP_PROTOCOL_TCP
101       && iproto != IP_PROTOCOL_ICMP && iproto != IP_PROTOCOL_ICMP6)
102     {
103       /* Dont translate & follow the fib programming */
104       next0 = cc->cc_parent.dpoi_next_node;
105       vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
106       goto trace;
107     }
108
109   if (!rv)
110     {
111       /* session table hit */
112       cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
113
114       if (INDEX_INVALID != session->value.cs_lbi)
115         {
116           /* Translate & follow the translation given LB */
117           next0 = session->value.dpoi_next_node;
118           vnet_buffer (b)->ip.adj_index[VLIB_TX] = session->value.cs_lbi;
119         }
120       else if (session->value.flags & CNAT_SESSION_FLAG_HAS_SNAT)
121         {
122           /* The return needs DNAT, so we need an additionnal
123            * lookup after translation */
124           next0 = CNAT_TRANSLATION_NEXT_LOOKUP;
125         }
126       else
127         {
128           /* Translate & follow the fib programming */
129           next0 = cc->cc_parent.dpoi_next_node;
130           vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
131         }
132     }
133   else
134     {
135       ct =
136         cnat_find_translation (cc->parent_cci,
137                                clib_host_to_net_u16 (udp0->dst_port), iproto);
138       if (NULL == ct)
139         {
140           /* Dont translate & Follow the fib programming */
141           vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
142           next0 = cc->cc_parent.dpoi_next_node;
143           goto trace;
144         }
145
146       /* New flow, create the sessions */
147       const load_balance_t *lb0;
148       cnat_ep_trk_t *trk0;
149       u32 hash_c0, bucket0;
150       u32 rsession_flags = 0;
151       const dpo_id_t *dpo0;
152
153       lb0 = load_balance_get (ct->ct_lb.dpoi_index);
154       if (!lb0->lb_n_buckets)
155         {
156           /* Dont translate & Follow the fib programming */
157           vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
158           next0 = cc->cc_parent.dpoi_next_node;
159           goto trace;
160         }
161
162       /* session table miss */
163       hash_c0 = (AF_IP4 == ctx->af ?
164                  ip4_compute_flow_hash (ip4, lb0->lb_hash_config) :
165                  ip6_compute_flow_hash (ip6, lb0->lb_hash_config));
166       bucket0 = hash_c0 % lb0->lb_n_buckets;
167       dpo0 = load_balance_get_fwd_bucket (lb0, bucket0);
168
169       /* add the session */
170       trk0 = &ct->ct_paths[bucket0];
171
172       ip46_address_copy (&session->value.cs_ip[VLIB_TX],
173                          &trk0->ct_ep[VLIB_TX].ce_ip.ip);
174       if (ip_address_is_zero (&trk0->ct_ep[VLIB_RX].ce_ip))
175         {
176           if (AF_IP4 == ctx->af)
177             ip46_address_set_ip4 (&session->value.cs_ip[VLIB_RX],
178                                   &ip4->src_address);
179           else
180             ip46_address_set_ip6 (&session->value.cs_ip[VLIB_RX],
181                                   &ip6->src_address);
182         }
183       else
184         {
185           /* We source NAT with the translation */
186           rsession_flags |= CNAT_SESSION_FLAG_HAS_SNAT;
187           ip46_address_copy (&session->value.cs_ip[VLIB_RX],
188                              &trk0->ct_ep[VLIB_RX].ce_ip.ip);
189         }
190       session->value.cs_port[VLIB_TX] =
191         clib_host_to_net_u16 (trk0->ct_ep[VLIB_TX].ce_port);
192       session->value.cs_port[VLIB_RX] =
193         clib_host_to_net_u16 (trk0->ct_ep[VLIB_RX].ce_port);
194
195       session->value.dpoi_next_node = ct->ct_lb.dpoi_next_node;
196       session->value.cs_lbi = dpo0->dpoi_index;
197
198       rv = cspm->vip_policy (vm, b, session, &rsession_flags, ct, ctx);
199       if (CNAT_SOURCE_ERROR_USE_DEFAULT == rv)
200         rv = cspm->default_policy (vm, b, session, &rsession_flags, ct, ctx);
201       if (rv)
202         {
203           if (CNAT_SOURCE_ERROR_EXHAUSTED_PORTS == rv)
204             vlib_node_increment_counter (vm, cnat_vip_ip4_node.index,
205                                          CNAT_ERROR_EXHAUSTED_PORTS, 1);
206           next0 = CNAT_TRANSLATION_NEXT_DROP;
207           goto trace;
208         }
209
210       /* refcnt session in current client */
211       cnat_client_cnt_session (cc);
212       cnat_session_create (session, ctx, rsession_flags);
213       created_session = 1;
214
215       next0 = ct->ct_lb.dpoi_next_node;
216       vnet_buffer (b)->ip.adj_index[VLIB_TX] = session->value.cs_lbi;
217     }
218
219   if (AF_IP4 == ctx->af)
220     cnat_translation_ip4 (session, ip4, udp0);
221   else
222     cnat_translation_ip6 (session, ip6, udp0);
223
224   if (NULL != ct)
225     {
226       cti = ct - cnat_translation_pool;
227       vlib_increment_combined_counter (cntm, ctx->thread_index, cti, 1,
228                                        vlib_buffer_length_in_chain (vm, b));
229     }
230
231 trace:
232   if (PREDICT_FALSE (ctx->do_trace))
233     {
234       cnat_translation_trace_t *t;
235
236       t = vlib_add_trace (vm, node, b, sizeof (*t));
237
238       t->found_session = !rv;
239       t->created_session = created_session;
240       if (t->found_session || t->created_session)
241         clib_memcpy (&t->session, session, sizeof (t->session));
242       t->has_tr = (NULL != ct);
243       if (t->has_tr)
244         clib_memcpy (&t->tr, ct, sizeof (cnat_translation_t));
245     }
246   return next0;
247 }
248
249 VLIB_NODE_FN (cnat_vip_ip4_node) (vlib_main_t * vm,
250                                   vlib_node_runtime_t * node,
251                                   vlib_frame_t * frame)
252 {
253   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
254     return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP4,
255                              1 /* do_trace */ );
256   return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP4,
257                            0 /* do_trace */ );
258 }
259
260 VLIB_NODE_FN (cnat_vip_ip6_node) (vlib_main_t * vm,
261                                   vlib_node_runtime_t * node,
262                                   vlib_frame_t * frame)
263 {
264   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
265     return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP6,
266                              1 /* do_trace */ );
267   return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP6,
268                            0 /* do_trace */ );
269 }
270
271 /* *INDENT-OFF* */
272 VLIB_REGISTER_NODE (cnat_vip_ip4_node) =
273 {
274   .name = "ip4-cnat-tx",
275   .vector_size = sizeof (u32),
276   .format_trace = format_cnat_translation_trace,
277   .type = VLIB_NODE_TYPE_INTERNAL,
278   .n_errors = 0,
279   .n_next_nodes = CNAT_TRANSLATION_N_NEXT,
280   .next_nodes =
281   {
282     [CNAT_TRANSLATION_NEXT_DROP] = "ip4-drop",
283     [CNAT_TRANSLATION_NEXT_LOOKUP] = "ip4-lookup",
284   }
285 };
286 VLIB_REGISTER_NODE (cnat_vip_ip6_node) =
287 {
288   .name = "ip6-cnat-tx",
289   .vector_size = sizeof (u32),
290   .format_trace = format_cnat_translation_trace,
291   .type = VLIB_NODE_TYPE_INTERNAL,
292   .n_errors = 0,
293   .n_next_nodes = CNAT_TRANSLATION_N_NEXT,
294   .next_nodes =
295   {
296     [CNAT_TRANSLATION_NEXT_DROP] = "ip6-drop",
297     [CNAT_TRANSLATION_NEXT_LOOKUP] = "ip6-lookup",
298   }
299 };
300 /* *INDENT-ON* */
301
302 /*
303  * fd.io coding-style-patch-verification: ON
304  *
305  * Local Variables:
306  * eval: (c-set-style "gnu")
307  * End:
308  */