2 *---------------------------------------------------------------------------
3 * cnat_ipv4_udp_inside_input_exception_stages.c - cnat_ipv4_udp_inside_input_exception node pipeline stage functions
6 * Copyright (c) 2008-2014 Cisco and/or its affiliates.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at:
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *---------------------------------------------------------------------------
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 #include <vppinfra/error.h>
24 #include <vnet/buffer.h>
26 #include "cnat_global.h"
28 #include "cnat_ipv4_udp.h"
31 * Dump these counters via the "show error" CLI command
34 #define foreach_cnat_ipv4_udp_inside_input_exc_error \
35 _(CNAT_V4_UDP_I2O_T_PKT, "v4 udp i2o transmit") \
36 _(CNAT_V4_UDP_I2O_D_PKT, "v4 udp i2o drop") \
37 _(CNAT_V4_ICMP_G_I2O_T_PKT, "v4 udp i2o icmp msg gen") \
38 _(CNAT_V4_UDP_I2O_DC_PKT, "v4 udp i2o (no config) drop") \
39 _(CNAT_V4_UDP_I2O_DR_PKT, "v4 udp i2o (not in run state) drop") \
40 _(CNAT_V4_UDP_I2O_DD_PKT, "v4 udp i2o (no direct port) drop") \
41 _(CNAT_V4_UDP_I2O_DA_PKT, "v4 udp i2o (no any port) drop") \
42 _(CNAT_V4_UDP_I2O_DO_PKT, "v4 udp i2o (out of port limit) drop") \
43 _(CNAT_V4_UDP_I2O_DI_PKT, "v4 udp i2o (invalid packet) drop") \
44 _(CNAT_V4_UDP_I2O_DS_PKT, "v4 udp i2o (no sessoon db) drop")
47 #define _(sym,str) sym,
48 foreach_cnat_ipv4_udp_inside_input_exc_error
50 CNAT_IPV4_UDP_INSIDE_INPUT_EXCEPTIONS_N_ERROR,
51 } cnat_ipv4_udp_inside_input_exc_error_t;
54 static char * cnat_ipv4_udp_inside_input_exc_error_strings[] = {
55 #define _(sym,string) string,
56 foreach_cnat_ipv4_udp_inside_input_exc_error
61 u32 cached_next_index;
62 /* $$$$ add data here */
64 /* convenience variables */
65 vlib_main_t * vlib_main;
66 vnet_main_t * vnet_main;
67 } cnat_ipv4_udp_inside_input_exc_main_t;
72 CNAT_V4_ICMP_G_I2O_T = CNAT_V4_UDP_I2O_D, /* TOBE_PORTED */
73 CNAT_V4_UDP_INSIDE_INPUT_EXC_N_NEXT,
74 } cnat_ipv4_udp_inside_input_exc_next_t;
76 cnat_ipv4_udp_inside_input_exc_main_t cnat_ipv4_udp_inside_input_exc_main;
77 vlib_node_registration_t cnat_ipv4_udp_inside_input_exc_node;
82 * Use the generic buffer metadata + first line of packet data prefetch
83 * stage function from <api/pipeline.h>. This is usually a Good Idea.
85 #define stage0 generic_stage0
87 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
90 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi);
92 vlib_get_node (vm, cnat_ipv4_udp_inside_input_exc_node.index);
93 u32 node_counter_base_index = n->error_heap_index;
94 vlib_error_main_t * em = &vm->error_main;
96 cnat_gen_icmp_info info;
97 cnat_db_key_bucket_t ki;
98 spp_ctx_t *ctx __attribute__((unused))
99 = (spp_ctx_t *) &vnet_buffer(b0)->vcgn_uii;
100 cnat_main_db_entry_t *db = NULL;
101 ipv4_header *ip = (ipv4_header *)vlib_buffer_get_current(b0);
102 u8 ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
103 udp_hdr_type_t *udp = (udp_hdr_type_t *)((u8*)ip + ipv4_hdr_len);
104 int disposition = CNAT_V4_UDP_I2O_T;
105 int counter = CNAT_V4_UDP_I2O_T_PKT;
107 cnat_key_t dest_info;
109 PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
110 vnet_buffer(b0)->vcgn_uii.key.k.vrf,
113 vnet_buffer(b0)->vcgn_uii.key.k.ipv4 = clib_net_to_host_u32(ip->src_addr);
115 PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
116 ki.k.k.vrf, CNAT_UDP)
118 ki.k.k.ipv4 = clib_net_to_host_u32(ip->src_addr);
121 /* MUST REVISIT: commentting frag check. Unconditional destination port
122 * update. DONOT remove this #if 0 */
124 clib_net_to_host_u16(udp->src_port);
126 clib_net_to_host_u16(udp->dest_port);
128 if(PREDICT_FALSE(ctx->ru.rx.frag)) {
130 /* Must have routed through cnat_v4_frag_in2out node */
131 u16 *feature_data_ports = (u16 *)&ctx->feature_data[2];
132 ki.k.k.port = *feature_data_ports;
133 feature_data_ports++;
134 dest_info.k.port = *feature_data_ports;
138 clib_net_to_host_u16(udp->src_port);
140 clib_net_to_host_u16(udp->dest_port);
144 dest_info.k.ipv4 = clib_net_to_host_u32(ip->dest_addr);
145 PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
146 dest_info.k.vrf, CNAT_UDP)
148 if (PREDICT_TRUE(ki.k.k.port)) {
149 if (ki.k.k.port & 0x1) {
150 db = cnat_get_main_db_entry_v2(&ki, PORT_S_ODD, PORT_TYPE_DYNAMIC,
153 db = cnat_get_main_db_entry_v2(&ki, PORT_S_EVEN, PORT_TYPE_DYNAMIC,
158 * No UDP port value of 0 - drop it
161 info.error = CNAT_ERR_BAD_TCP_UDP_PORT;
164 if (PREDICT_TRUE((u64)db)) {
166 if (PLATFORM_HANDLE_TTL_DECREMENT) {
168 * Decrement TTL and update IPv4 checksum
170 ipv4_decr_ttl_n_calc_csum(ip);
174 * step 6 do nat before fwd pkt
176 swap_ip_src_udp_port(ip, udp, db);
178 * update db for this pkt
180 CNAT_DB_UPDATE_IN2OUT_TIMER
182 /* Check timeout db if there is config for this */
183 (void) query_and_update_db_timeout((void *)db, MAIN_DB_TYPE);
185 /* Temporarily keeping it commented */
186 //PLATFORM_CNAT_SET_TX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_TX],
187 // db->out2in_key.k.vrf)
188 in2out_forwarding_count++;
191 switch (info.error) {
192 case (CNAT_NO_VRF_RUN):
193 em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DR_PKT] += 1;
195 case (CNAT_OUT_LIMIT):
196 em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DO_PKT] += 1;
198 case (CNAT_NO_PORT_ANY):
199 case (CNAT_NO_POOL_ANY):
200 case (CNAT_BAD_INUSE_ANY):
201 case (CNAT_NOT_FOUND_ANY):
202 em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DA_PKT] += 1;
204 case (CNAT_INV_PORT_DIRECT):
205 case (CNAT_DEL_PORT_DIRECT):
206 case (CNAT_BAD_INUSE_DIRECT):
207 case (CNAT_NOT_FOUND_DIRECT):
208 em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DD_PKT] += 1;
210 case (CNAT_ERR_BAD_TCP_UDP_PORT):
211 em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DI_PKT] += 1;
213 case (CNAT_ERR_NO_SESSION_DB):
214 em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DS_PKT] += 1;
217 em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DC_PKT] += 1;
221 * send to icmp msg generate node
223 if (info.gen_icmp_msg == CNAT_ICMP_MSG) {
225 u32 *fd = (u32*)ctx->feature_data;
226 fd[0] = info.svi_addr;
227 fd[1] = CNAT_ICMP_DEST_UNREACHABLE;
229 disposition = CNAT_V4_ICMP_G_I2O_T;
230 counter = CNAT_V4_ICMP_G_I2O_T_PKT;
232 disposition = CNAT_V4_UDP_I2O_D;
233 counter = CNAT_V4_UDP_I2O_D_PKT;
235 DEBUG_I2O_DROP(CNAT_DEBUG_DROP_UDP)
238 em->counters[node_counter_base_index + counter] += 1;
244 #include <vnet/pipeline.h>
246 static uword cnat_ipv4_udp_inside_input_exc_node_fn (vlib_main_t * vm,
247 vlib_node_runtime_t * node,
248 vlib_frame_t * frame)
250 return dispatch_pipeline (vm, node, frame);
253 VLIB_REGISTER_NODE (cnat_ipv4_udp_inside_input_exc_node) = {
254 .function = cnat_ipv4_udp_inside_input_exc_node_fn,
255 .name = "vcgn-v4-udp-i2o-e",
256 .vector_size = sizeof (u32),
257 .type = VLIB_NODE_TYPE_INTERNAL,
259 .n_errors = ARRAY_LEN(cnat_ipv4_udp_inside_input_exc_error_strings),
260 .error_strings = cnat_ipv4_udp_inside_input_exc_error_strings,
262 .n_next_nodes = CNAT_V4_UDP_INSIDE_INPUT_EXC_N_NEXT,
264 /* edit / add dispositions here */
266 [CNAT_V4_UDP_I2O_T] = "ip4-input",
267 [CNAT_V4_UDP_I2O_D] = "error-drop",
272 clib_error_t *cnat_ipv4_udp_inside_input_exc_init (vlib_main_t *vm)
274 cnat_ipv4_udp_inside_input_exc_main_t * mp = &cnat_ipv4_udp_inside_input_exc_main;
277 mp->vnet_main = vnet_get_main();
282 VLIB_INIT_FUNCTION (cnat_ipv4_udp_inside_input_exc_init);