Line data Source code
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_snat_policy.h>
19 : #include <cnat/cnat_inline.h>
20 : #include <cnat/cnat_src_policy.h>
21 :
22 : typedef enum cnat_snat_next_
23 : {
24 : CNAT_SNAT_NEXT_DROP,
25 : CNAT_SNAT_N_NEXT,
26 : } cnat_snat_next_t;
27 :
28 : vlib_node_registration_t cnat_snat_ip4_node;
29 : vlib_node_registration_t cnat_snat_ip6_node;
30 :
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
33 : cnat_vip_inline */
34 : static uword
35 660 : 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)
38 : {
39 660 : cnat_snat_policy_main_t *cpm = &cnat_snat_policy_main;
40 660 : ip4_header_t *ip4 = NULL;
41 : ip_protocol_t iproto;
42 660 : ip6_header_t *ip6 = NULL;
43 : udp_header_t *udp0;
44 : u32 arc_next0;
45 : u16 next0;
46 : u16 sport;
47 660 : u8 trace_flags = 0;
48 : int rv, do_snat;
49 :
50 660 : if (AF_IP4 == ctx->af)
51 : {
52 165 : ip4 = vlib_buffer_get_current (b);
53 165 : iproto = ip4->protocol;
54 165 : udp0 = (udp_header_t *) (ip4 + 1);
55 : }
56 : else
57 : {
58 495 : ip6 = vlib_buffer_get_current (b);
59 495 : iproto = ip6->protocol;
60 495 : udp0 = (udp_header_t *) (ip6 + 1);
61 : }
62 :
63 : /* By default don't follow previous next0 */
64 660 : vnet_feature_next (&arc_next0, b);
65 660 : next0 = arc_next0;
66 :
67 : /* Wrong session key */
68 660 : if (session->key.cs_proto == 0)
69 0 : goto trace;
70 :
71 660 : if (!session_not_found)
72 : {
73 : /* session table hit */
74 520 : cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
75 : }
76 : else
77 : {
78 : ip46_address_t ip46_dst_address;
79 140 : if (AF_IP4 == ctx->af)
80 35 : ip46_address_set_ip4 (&ip46_dst_address, &ip4->dst_address);
81 : else
82 105 : ip46_address_set_ip6 (&ip46_dst_address, &ip6->dst_address);
83 :
84 140 : do_snat = cpm->snat_policy (b, session);
85 140 : if (!do_snat)
86 120 : goto trace;
87 :
88 : /* New flow, create the sessions if necessary. session will be a snat
89 : session, and rsession will be a dnat session
90 : Note: packet going through this path are going to the outside,
91 : so they will never hit the NAT again (they are not going towards
92 : a VIP) */
93 20 : if (AF_IP4 == ctx->af)
94 : {
95 5 : if (!(cpm->snat_ip4.ce_flags & CNAT_EP_FLAG_RESOLVED))
96 0 : goto trace;
97 5 : ip46_address_set_ip4 (&session->value.cs_ip[VLIB_RX],
98 5 : &ip_addr_v4 (&cpm->snat_ip4.ce_ip));
99 5 : ip46_address_set_ip4 (&session->value.cs_ip[VLIB_TX],
100 5 : &ip4->dst_address);
101 : }
102 : else
103 : {
104 15 : if (!(cpm->snat_ip6.ce_flags & CNAT_EP_FLAG_RESOLVED))
105 0 : goto trace;
106 15 : ip46_address_set_ip6 (&session->value.cs_ip[VLIB_RX],
107 15 : &ip_addr_v6 (&cpm->snat_ip6.ce_ip));
108 15 : ip46_address_set_ip6 (&session->value.cs_ip[VLIB_TX],
109 15 : &ip6->dst_address);
110 : }
111 :
112 :
113 20 : sport = 0;
114 20 : rv = cnat_allocate_port (&sport, iproto);
115 20 : if (rv)
116 : {
117 0 : vlib_node_increment_counter (vm, cnat_snat_ip4_node.index,
118 : CNAT_ERROR_EXHAUSTED_PORTS, 1);
119 0 : next0 = CNAT_SNAT_NEXT_DROP;
120 0 : goto trace;
121 : }
122 20 : session->value.cs_port[VLIB_RX] = sport;
123 20 : session->value.cs_port[VLIB_TX] = sport;
124 20 : if (iproto == IP_PROTOCOL_TCP || iproto == IP_PROTOCOL_UDP)
125 16 : session->value.cs_port[VLIB_TX] = udp0->dst_port;
126 :
127 20 : session->value.cs_lbi = INDEX_INVALID;
128 20 : session->value.flags =
129 : CNAT_SESSION_FLAG_NO_CLIENT | CNAT_SESSION_FLAG_ALLOC_PORT;
130 20 : trace_flags |= CNAT_TRACE_SESSION_CREATED;
131 :
132 20 : cnat_session_create (session, ctx);
133 20 : cnat_rsession_create (session, ctx, CNAT_LOCATION_FIB,
134 : CNAT_SESSION_FLAG_HAS_SNAT);
135 : }
136 :
137 540 : if (AF_IP4 == ctx->af)
138 135 : cnat_translation_ip4 (session, ip4, udp0, vnet_buffer (b)->oflags);
139 : else
140 405 : cnat_translation_ip6 (session, ip6, udp0, vnet_buffer (b)->oflags);
141 :
142 660 : trace:
143 660 : if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
144 : {
145 660 : trace_flags |= session_not_found ? 0 : CNAT_TRACE_SESSION_FOUND;
146 660 : cnat_add_trace (vm, node, b, session, NULL, trace_flags);
147 : }
148 660 : return next0;
149 : }
150 :
151 586 : VLIB_NODE_FN (cnat_snat_ip4_node) (vlib_main_t * vm,
152 : vlib_node_runtime_t * node,
153 : vlib_frame_t * frame)
154 : {
155 11 : if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
156 11 : return cnat_node_inline (vm, node, frame, cnat_snat_node_fn, AF_IP4,
157 : CNAT_LOCATION_FIB, 1 /* do_trace */);
158 0 : return cnat_node_inline (vm, node, frame, cnat_snat_node_fn, AF_IP4,
159 : CNAT_LOCATION_FIB, 0 /* do_trace */);
160 : }
161 :
162 608 : VLIB_NODE_FN (cnat_snat_ip6_node) (vlib_main_t * vm,
163 : vlib_node_runtime_t * node,
164 : vlib_frame_t * frame)
165 : {
166 33 : if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
167 33 : return cnat_node_inline (vm, node, frame, cnat_snat_node_fn, AF_IP6,
168 : CNAT_LOCATION_FIB, 1 /* do_trace */);
169 0 : return cnat_node_inline (vm, node, frame, cnat_snat_node_fn, AF_IP6,
170 : CNAT_LOCATION_FIB, 0 /* do_trace */);
171 : }
172 :
173 164204 : VLIB_REGISTER_NODE (cnat_snat_ip4_node) = {
174 : .name = "cnat-snat-ip4",
175 : .vector_size = sizeof (u32),
176 : .format_trace = format_cnat_trace,
177 : .type = VLIB_NODE_TYPE_INTERNAL,
178 : .n_errors = CNAT_N_ERROR,
179 : .error_strings = cnat_error_strings,
180 : .n_next_nodes = CNAT_SNAT_N_NEXT,
181 : .next_nodes = {
182 : [CNAT_SNAT_NEXT_DROP] = "ip4-drop",
183 : },
184 : };
185 :
186 164204 : VLIB_REGISTER_NODE (cnat_snat_ip6_node) = {
187 : .name = "cnat-snat-ip6",
188 : .vector_size = sizeof (u32),
189 : .format_trace = format_cnat_trace,
190 : .type = VLIB_NODE_TYPE_INTERNAL,
191 : .n_errors = CNAT_N_ERROR,
192 : .error_strings = cnat_error_strings,
193 : .n_next_nodes = CNAT_SNAT_N_NEXT,
194 : .next_nodes = {
195 : [CNAT_SNAT_NEXT_DROP] = "ip6-drop",
196 : },
197 : };
198 :
199 64539 : VNET_FEATURE_INIT (cnat_snat_ip4_node, static) = {
200 : .arc_name = "ip4-unicast",
201 : .node_name = "cnat-snat-ip4",
202 : };
203 :
204 64539 : VNET_FEATURE_INIT (cnat_snat_ip6_node, static) = {
205 : .arc_name = "ip6-unicast",
206 : .node_name = "cnat-snat-ip6",
207 : };
208 :
209 : /*
210 : * fd.io coding-style-patch-verification: ON
211 : *
212 : * Local Variables:
213 : * eval: (c-set-style "gnu")
214 : * End:
215 : */
|