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