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_snat.h>
19 #include <cnat/cnat_inline.h>
20 #include <cnat/cnat_src_policy.h>
22 typedef enum cnat_snat_next_
28 vlib_node_registration_t cnat_snat_ip4_node;
29 vlib_node_registration_t cnat_snat_ip6_node;
31 /* CNat sub for source NAT as a feature arc on ip[46]-unicast
32 This node's sub shouldn't apply to the same flows as
35 cnat_snat_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
36 vlib_buffer_t *b, cnat_node_ctx_t *ctx,
37 int session_not_found, cnat_session_t *session)
39 cnat_main_t *cm = &cnat_main;
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 /* By default don't follow previous next0 */
64 vnet_feature_next (&arc_next0, b);
67 /* Wrong session key */
68 if (session->key.cs_proto == 0)
71 if (!session_not_found)
73 /* session table hit */
74 cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
78 ip46_address_t ip46_dst_address;
79 if (AF_IP4 == ctx->af)
80 ip46_address_set_ip4 (&ip46_dst_address, &ip4->dst_address);
82 ip46_address_set_ip6 (&ip46_dst_address, &ip6->dst_address);
83 rv = cnat_search_snat_prefix (&ip46_dst_address, ctx->af);
86 /* Prefix table hit, we shouldn't source NAT */
89 /* New flow, create the sessions if necessary. session will be a snat
90 session, and rsession will be a dnat session
91 Note: packet going through this path are going to the outside,
92 so they will never hit the NAT again (they are not going towards
94 if (AF_IP4 == ctx->af)
96 if (!(cm->snat_ip4.ce_flags & CNAT_EP_FLAG_RESOLVED))
98 ip46_address_set_ip4 (&session->value.cs_ip[VLIB_RX],
99 &ip_addr_v4 (&cm->snat_ip4.ce_ip));
100 ip46_address_set_ip4 (&session->value.cs_ip[VLIB_TX],
105 if (!(cm->snat_ip6.ce_flags & CNAT_EP_FLAG_RESOLVED))
107 ip46_address_set_ip6 (&session->value.cs_ip[VLIB_RX],
108 &ip_addr_v6 (&cm->snat_ip6.ce_ip));
109 ip46_address_set_ip6 (&session->value.cs_ip[VLIB_TX],
115 rv = cnat_allocate_port (&sport, iproto);
118 vlib_node_increment_counter (vm, cnat_snat_ip4_node.index,
119 CNAT_ERROR_EXHAUSTED_PORTS, 1);
120 next0 = CNAT_SNAT_NEXT_DROP;
123 session->value.cs_port[VLIB_RX] = sport;
124 session->value.cs_port[VLIB_TX] = sport;
125 if (iproto == IP_PROTOCOL_TCP || iproto == IP_PROTOCOL_UDP)
126 session->value.cs_port[VLIB_TX] = udp0->dst_port;
128 session->value.cs_lbi = INDEX_INVALID;
129 session->value.flags =
130 CNAT_SESSION_FLAG_NO_CLIENT | CNAT_SESSION_FLAG_ALLOC_PORT;
131 trace_flags |= CNAT_TRACE_SESSION_CREATED;
133 cnat_session_create (session, ctx, CNAT_LOCATION_FIB,
134 CNAT_SESSION_FLAG_HAS_SNAT);
138 if (AF_IP4 == ctx->af)
139 cnat_translation_ip4 (session, ip4, udp0);
141 cnat_translation_ip6 (session, ip6, udp0);
144 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
146 trace_flags |= session_not_found ? 0 : CNAT_TRACE_SESSION_FOUND;
147 cnat_add_trace (vm, node, b, session, NULL, trace_flags);
152 VLIB_NODE_FN (cnat_snat_ip4_node) (vlib_main_t * vm,
153 vlib_node_runtime_t * node,
154 vlib_frame_t * frame)
156 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
157 return cnat_node_inline (vm, node, frame, cnat_snat_node_fn, AF_IP4,
158 CNAT_LOCATION_FIB, 1 /* do_trace */);
159 return cnat_node_inline (vm, node, frame, cnat_snat_node_fn, AF_IP4,
160 CNAT_LOCATION_FIB, 0 /* do_trace */);
163 VLIB_NODE_FN (cnat_snat_ip6_node) (vlib_main_t * vm,
164 vlib_node_runtime_t * node,
165 vlib_frame_t * frame)
167 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
168 return cnat_node_inline (vm, node, frame, cnat_snat_node_fn, AF_IP6,
169 CNAT_LOCATION_FIB, 1 /* do_trace */);
170 return cnat_node_inline (vm, node, frame, cnat_snat_node_fn, AF_IP6,
171 CNAT_LOCATION_FIB, 0 /* do_trace */);
174 VLIB_REGISTER_NODE (cnat_snat_ip4_node) = {
175 .name = "cnat-snat-ip4",
176 .vector_size = sizeof (u32),
177 .format_trace = format_cnat_trace,
178 .type = VLIB_NODE_TYPE_INTERNAL,
179 .n_errors = CNAT_N_ERROR,
180 .error_strings = cnat_error_strings,
181 .n_next_nodes = CNAT_SNAT_N_NEXT,
183 [CNAT_SNAT_NEXT_DROP] = "ip4-drop",
187 VLIB_REGISTER_NODE (cnat_snat_ip6_node) = {
188 .name = "cnat-snat-ip6",
189 .vector_size = sizeof (u32),
190 .format_trace = format_cnat_trace,
191 .type = VLIB_NODE_TYPE_INTERNAL,
192 .n_errors = CNAT_N_ERROR,
193 .error_strings = cnat_error_strings,
194 .n_next_nodes = CNAT_SNAT_N_NEXT,
196 [CNAT_SNAT_NEXT_DROP] = "ip6-drop",
200 VNET_FEATURE_INIT (cnat_snat_ip4_node, static) = {
201 .arc_name = "ip4-unicast",
202 .node_name = "cnat-snat-ip4",
205 VNET_FEATURE_INIT (cnat_snat_ip6_node, static) = {
206 .arc_name = "ip6-unicast",
207 .node_name = "cnat-snat-ip6",
211 * fd.io coding-style-patch-verification: ON
214 * eval: (c-set-style "gnu")