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 typedef struct cnat_snat_trace_
30 cnat_session_t session;
35 vlib_node_registration_t cnat_snat_ip4_node;
36 vlib_node_registration_t cnat_snat_ip6_node;
39 format_cnat_snat_trace (u8 * s, va_list * args)
41 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
42 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
43 cnat_snat_trace_t *t = va_arg (*args, cnat_snat_trace_t *);
46 s = format (s, "found: %U", format_cnat_session, &t->session, 1);
47 else if (t->created_session)
48 s = format (s, "created: %U\n tr: %U",
49 format_cnat_session, &t->session, 1);
51 s = format (s, "not found");
55 /* CNat sub for source NAT as a feature arc on ip[46]-unicast
56 This node's sub shouldn't apply to the same flows as
59 cnat_snat_inline (vlib_main_t * vm,
60 vlib_node_runtime_t * node,
62 cnat_node_ctx_t * ctx, int rv, cnat_session_t * session)
64 cnat_main_t *cm = &cnat_main;
65 int created_session = 0;
74 if (AF_IP4 == ctx->af)
76 ip4 = vlib_buffer_get_current (b);
77 iproto = ip4->protocol;
78 udp0 = (udp_header_t *) (ip4 + 1);
82 ip6 = vlib_buffer_get_current (b);
83 iproto = ip6->protocol;
84 udp0 = (udp_header_t *) (ip6 + 1);
87 /* By default don't follow previous next0 */
88 vnet_feature_next (&arc_next0, b);
91 if (iproto != IP_PROTOCOL_UDP && iproto != IP_PROTOCOL_TCP
92 && iproto != IP_PROTOCOL_ICMP && iproto != IP_PROTOCOL_ICMP6)
100 /* session table hit */
101 cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
105 ip46_address_t ip46_dst_address;
106 if (AF_IP4 == ctx->af)
107 ip46_address_set_ip4 (&ip46_dst_address, &ip4->dst_address);
109 ip46_address_set_ip6 (&ip46_dst_address, &ip6->dst_address);
110 rv = cnat_search_snat_prefix (&ip46_dst_address, ctx->af);
113 /* Prefix table hit, we shouldn't source NAT */
116 /* New flow, create the sessions if necessary. session will be a snat
117 session, and rsession will be a dnat session
118 Note: packet going through this path are going to the outside,
119 so they will never hit the NAT again (they are not going towards
121 if (AF_IP4 == ctx->af)
123 ip46_address_set_ip4 (&session->value.cs_ip[VLIB_RX],
124 &ip_addr_v4 (&cm->snat_ip4.ce_ip));
125 ip46_address_set_ip4 (&session->value.cs_ip[VLIB_TX],
130 ip46_address_set_ip6 (&session->value.cs_ip[VLIB_RX],
131 &ip_addr_v6 (&cm->snat_ip6.ce_ip));
132 ip46_address_set_ip6 (&session->value.cs_ip[VLIB_TX],
138 rv = cnat_allocate_port (&sport, iproto);
141 vlib_node_increment_counter (vm, cnat_snat_ip4_node.index,
142 CNAT_ERROR_EXHAUSTED_PORTS, 1);
143 next0 = CNAT_SNAT_NEXT_DROP;
146 session->value.cs_port[VLIB_RX] = sport;
147 session->value.cs_port[VLIB_TX] = sport;
148 if (iproto == IP_PROTOCOL_TCP || iproto == IP_PROTOCOL_UDP)
149 session->value.cs_port[VLIB_TX] = udp0->dst_port;
151 session->value.cs_lbi = INDEX_INVALID;
152 session->value.flags =
153 CNAT_SESSION_FLAG_NO_CLIENT | CNAT_SESSION_FLAG_ALLOC_PORT;
156 cnat_session_create (session, ctx, CNAT_SESSION_FLAG_HAS_SNAT);
160 if (AF_IP4 == ctx->af)
161 cnat_translation_ip4 (session, ip4, udp0);
163 cnat_translation_ip6 (session, ip6, udp0);
166 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
168 cnat_snat_trace_t *t;
170 t = vlib_add_trace (vm, node, b, sizeof (*t));
172 t->found_session = !rv;
173 t->created_session = created_session;
174 if (t->found_session || t->created_session)
175 clib_memcpy (&t->session, session, sizeof (t->session));
180 VLIB_NODE_FN (cnat_snat_ip4_node) (vlib_main_t * vm,
181 vlib_node_runtime_t * node,
182 vlib_frame_t * frame)
184 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
185 return cnat_node_inline (vm, node, frame, cnat_snat_inline, AF_IP4,
187 return cnat_node_inline (vm, node, frame, cnat_snat_inline, AF_IP4,
191 VLIB_NODE_FN (cnat_snat_ip6_node) (vlib_main_t * vm,
192 vlib_node_runtime_t * node,
193 vlib_frame_t * frame)
195 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
196 return cnat_node_inline (vm, node, frame, cnat_snat_inline, AF_IP6,
198 return cnat_node_inline (vm, node, frame, cnat_snat_inline, AF_IP6,
203 VLIB_REGISTER_NODE (cnat_snat_ip4_node) =
205 .name = "ip4-cnat-snat",
206 .vector_size = sizeof (u32),
207 .format_trace = format_cnat_snat_trace,
208 .type = VLIB_NODE_TYPE_INTERNAL,
209 .n_errors = CNAT_N_ERROR,
210 .error_strings = cnat_error_strings,
211 .n_next_nodes = CNAT_SNAT_N_NEXT,
214 [CNAT_SNAT_NEXT_DROP] = "ip4-drop",
218 VLIB_REGISTER_NODE (cnat_snat_ip6_node) =
220 .name = "ip6-cnat-snat",
221 .vector_size = sizeof (u32),
222 .format_trace = format_cnat_snat_trace,
223 .type = VLIB_NODE_TYPE_INTERNAL,
224 .n_errors = CNAT_N_ERROR,
225 .error_strings = cnat_error_strings,
226 .n_next_nodes = CNAT_SNAT_N_NEXT,
229 [CNAT_SNAT_NEXT_DROP] = "ip6-drop",
233 VNET_FEATURE_INIT (cnat_snat_ip4_node, static) =
235 .arc_name = "ip4-unicast",
236 .node_name = "ip4-cnat-snat",
239 VNET_FEATURE_INIT (cnat_snat_ip6_node, static) =
241 .arc_name = "ip6-unicast",
242 .node_name = "ip6-cnat-snat",
248 * fd.io coding-style-patch-verification: ON
251 * eval: (c-set-style "gnu")