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_inline.h>
19 #include <cnat/cnat_src_policy.h>
21 typedef enum cnat_translation_next_t_
23 CNAT_TRANSLATION_NEXT_DROP,
24 CNAT_TRANSLATION_NEXT_LOOKUP,
25 CNAT_TRANSLATION_N_NEXT,
26 } cnat_translation_next_t;
28 vlib_node_registration_t cnat_vip_ip4_node;
29 vlib_node_registration_t cnat_vip_ip6_node;
31 /* CNat sub for NAT behind a fib entry (VIP or interposed real IP) */
33 cnat_vip_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b,
34 cnat_node_ctx_t *ctx, int session_not_found,
35 cnat_session_t *session)
37 vlib_combined_counter_main_t *cntm = &cnat_translation_counters;
38 cnat_src_policy_main_t *cspm = &cnat_src_policy_main;
39 const cnat_translation_t *ct = NULL;
40 ip4_header_t *ip4 = NULL;
42 ip6_header_t *ip6 = NULL;
50 if (AF_IP4 == ctx->af)
52 ip4 = vlib_buffer_get_current (b);
53 iproto = ip4->protocol;
54 udp0 = (udp_header_t *) (ip4 + 1);
58 ip6 = vlib_buffer_get_current (b);
59 iproto = ip6->protocol;
60 udp0 = (udp_header_t *) (ip6 + 1);
63 cc = cnat_client_get (vnet_buffer (b)->ip.adj_index[VLIB_TX]);
65 if (iproto != IP_PROTOCOL_UDP && iproto != IP_PROTOCOL_TCP
66 && iproto != IP_PROTOCOL_ICMP && iproto != IP_PROTOCOL_ICMP6)
68 /* Dont translate & follow the fib programming */
69 next0 = cc->cc_parent.dpoi_next_node;
70 vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
74 if (!session_not_found)
76 /* session table hit */
77 cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
79 if (INDEX_INVALID != session->value.cs_lbi)
81 /* Translate & follow the translation given LB */
82 next0 = session->value.dpoi_next_node;
83 vnet_buffer (b)->ip.adj_index[VLIB_TX] = session->value.cs_lbi;
85 else if (session->value.flags & CNAT_SESSION_FLAG_HAS_SNAT)
87 /* The return needs DNAT, so we need an additionnal
88 * lookup after translation */
89 next0 = CNAT_TRANSLATION_NEXT_LOOKUP;
93 /* Translate & follow the fib programming */
94 next0 = cc->cc_parent.dpoi_next_node;
95 vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
101 cnat_find_translation (cc->parent_cci,
102 clib_host_to_net_u16 (udp0->dst_port), iproto);
105 /* Dont translate & Follow the fib programming */
106 vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
107 next0 = cc->cc_parent.dpoi_next_node;
111 /* New flow, create the sessions */
112 const load_balance_t *lb0;
114 u32 hash_c0, bucket0;
115 u32 rsession_flags = 0;
116 const dpo_id_t *dpo0;
118 lb0 = load_balance_get (ct->ct_lb.dpoi_index);
119 if (!lb0->lb_n_buckets)
121 /* Dont translate & Follow the fib programming */
122 vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
123 next0 = cc->cc_parent.dpoi_next_node;
127 /* session table miss */
128 hash_c0 = (AF_IP4 == ctx->af ?
129 ip4_compute_flow_hash (ip4, lb0->lb_hash_config) :
130 ip6_compute_flow_hash (ip6, lb0->lb_hash_config));
131 bucket0 = hash_c0 % lb0->lb_n_buckets;
132 dpo0 = load_balance_get_fwd_bucket (lb0, bucket0);
134 /* add the session */
135 trk0 = &ct->ct_paths[bucket0];
137 ip46_address_copy (&session->value.cs_ip[VLIB_TX],
138 &trk0->ct_ep[VLIB_TX].ce_ip.ip);
139 if (ip_address_is_zero (&trk0->ct_ep[VLIB_RX].ce_ip))
141 if (AF_IP4 == ctx->af)
142 ip46_address_set_ip4 (&session->value.cs_ip[VLIB_RX],
145 ip46_address_set_ip6 (&session->value.cs_ip[VLIB_RX],
150 /* We source NAT with the translation */
151 rsession_flags |= CNAT_SESSION_FLAG_HAS_SNAT;
152 ip46_address_copy (&session->value.cs_ip[VLIB_RX],
153 &trk0->ct_ep[VLIB_RX].ce_ip.ip);
155 session->value.cs_port[VLIB_TX] =
156 clib_host_to_net_u16 (trk0->ct_ep[VLIB_TX].ce_port);
157 session->value.cs_port[VLIB_RX] =
158 clib_host_to_net_u16 (trk0->ct_ep[VLIB_RX].ce_port);
160 session->value.dpoi_next_node = ct->ct_lb.dpoi_next_node;
161 session->value.cs_lbi = dpo0->dpoi_index;
163 rv = cspm->vip_policy (vm, b, session, &rsession_flags, ct, ctx);
164 if (CNAT_SOURCE_ERROR_USE_DEFAULT == rv)
165 rv = cspm->default_policy (vm, b, session, &rsession_flags, ct, ctx);
168 if (CNAT_SOURCE_ERROR_EXHAUSTED_PORTS == rv)
170 vlib_node_registration_t *node =
171 (AF_IP4 == ctx->af) ? &cnat_vip_ip4_node : &cnat_vip_ip6_node;
172 vlib_node_increment_counter (vm, node->index,
173 CNAT_ERROR_EXHAUSTED_PORTS, 1);
175 next0 = CNAT_TRANSLATION_NEXT_DROP;
179 /* refcnt session in current client */
180 cnat_client_cnt_session (cc);
181 cnat_session_create (session, ctx, CNAT_LOCATION_FIB, rsession_flags);
182 trace_flags |= CNAT_TRACE_SESSION_CREATED;
184 next0 = ct->ct_lb.dpoi_next_node;
185 vnet_buffer (b)->ip.adj_index[VLIB_TX] = session->value.cs_lbi;
188 if (AF_IP4 == ctx->af)
189 cnat_translation_ip4 (session, ip4, udp0);
191 cnat_translation_ip6 (session, ip6, udp0);
195 cti = ct - cnat_translation_pool;
196 vlib_increment_combined_counter (cntm, ctx->thread_index, cti, 1,
197 vlib_buffer_length_in_chain (vm, b));
201 if (PREDICT_FALSE (ctx->do_trace))
203 trace_flags |= session_not_found ? 0 : CNAT_TRACE_SESSION_FOUND;
204 cnat_add_trace (vm, node, b, session, ct, trace_flags);
209 VLIB_NODE_FN (cnat_vip_ip4_node) (vlib_main_t * vm,
210 vlib_node_runtime_t * node,
211 vlib_frame_t * frame)
213 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
214 return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP4,
215 CNAT_LOCATION_FIB, 1 /* do_trace */);
216 return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP4,
217 CNAT_LOCATION_FIB, 0 /* do_trace */);
220 VLIB_NODE_FN (cnat_vip_ip6_node) (vlib_main_t * vm,
221 vlib_node_runtime_t * node,
222 vlib_frame_t * frame)
224 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
225 return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP6,
226 CNAT_LOCATION_FIB, 1 /* do_trace */);
227 return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP6,
228 CNAT_LOCATION_FIB, 0 /* do_trace */);
231 VLIB_REGISTER_NODE (cnat_vip_ip4_node) =
233 .name = "ip4-cnat-tx",
234 .vector_size = sizeof (u32),
235 .format_trace = format_cnat_trace,
236 .type = VLIB_NODE_TYPE_INTERNAL,
238 .n_next_nodes = CNAT_TRANSLATION_N_NEXT,
241 [CNAT_TRANSLATION_NEXT_DROP] = "ip4-drop",
242 [CNAT_TRANSLATION_NEXT_LOOKUP] = "ip4-lookup",
245 VLIB_REGISTER_NODE (cnat_vip_ip6_node) =
247 .name = "ip6-cnat-tx",
248 .vector_size = sizeof (u32),
249 .format_trace = format_cnat_trace,
250 .type = VLIB_NODE_TYPE_INTERNAL,
252 .n_next_nodes = CNAT_TRANSLATION_N_NEXT,
255 [CNAT_TRANSLATION_NEXT_DROP] = "ip6-drop",
256 [CNAT_TRANSLATION_NEXT_LOOKUP] = "ip6-lookup",
261 * fd.io coding-style-patch-verification: ON
264 * eval: (c-set-style "gnu")