2 *---------------------------------------------------------------------------
3 * cnat_ipv4_icmp_query_inside_input_exception.c - cnat_ipv4_icmp_query_inside_input_exception node pipeline stage functions
5 * Copyright (c) 2008-2014 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *---------------------------------------------------------------------------
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vppinfra/error.h>
23 #include <vnet/buffer.h>
25 #include "cnat_ipv4_icmp.h"
27 #define foreach_cnat_ipv4_icmp_q_inside_input_exc_error \
28 _(CNAT_V4_ICMP_Q_I2O_E_T_PKT, "v4 icmp query i2o-e transmit") \
29 _(CNAT_V4_ICMP_Q_I2O_E_G_PKT, "v4 icmp query i2o-e gen icmp msg") \
30 _(CNAT_V4_ICMP_Q_I2O_E_D_PKT, "v4 icmp query i2o-e pkt drop") \
31 _(CNAT_V4_ICMP_Q_I2O_E_DC_PKT, "v4 icmp query i2o-e drop (no config)") \
32 _(CNAT_V4_ICMP_Q_I2O_E_DR_PKT, "v4 icmp query i2o-e drop (not in run state)") \
33 _(CNAT_V4_ICMP_Q_I2O_E_DD_PKT, "v4 icmp query i2o-e drop (no direct port)") \
34 _(CNAT_V4_ICMP_Q_I2O_E_DA_PKT, "v4 icmp query i2o-e drop (no any port)") \
35 _(CNAT_V4_ICMP_Q_I2O_E_DO_PKT, "v4 icmp query i2o-e drop (out of port limit)") \
36 _(CNAT_V4_ICMP_Q_I2O_E_DS_PKT, "v4 icmp query i2o_e drop (out of session db)")
39 #define _(sym,str) sym,
40 foreach_cnat_ipv4_icmp_q_inside_input_exc_error
42 CNAT_IPV4_ICMP_Q_INSIDE_INPUT_EXCEPTIONS_N_ERROR,
43 } cnat_ipv4_icmp_q_inside_input_exc_error_t;
46 static char * cnat_ipv4_icmp_q_inside_input_exc_error_strings[] = {
47 #define _(sym,string) string,
48 foreach_cnat_ipv4_icmp_q_inside_input_exc_error
53 u32 cached_next_index;
54 /* $$$$ add data here */
56 /* convenience variables */
57 vlib_main_t * vlib_main;
58 vnet_main_t * vnet_main;
59 } cnat_ipv4_icmp_q_inside_input_exc_main_t;
62 CNAT_V4_ICMP_Q_E_I2O_T,
63 //CNAT_V4_ICMP_Q_E_I2O_GEN,
64 CNAT_V4_ICMP_Q_E_I2O_D,
65 CNAT_V4_ICMP_Q_E_I2O_NEXT,
66 } cnat_ipv4_icmp_q_inside_input_exc_next_t;
68 #define CNAT_V4_ICMP_Q_E_I2O_GEN CNAT_V4_ICMP_Q_E_I2O_T
70 cnat_ipv4_icmp_q_inside_input_exc_main_t cnat_ipv4_icmp_q_inside_input_exc_main;
71 vlib_node_registration_t cnat_ipv4_icmp_q_inside_input_exc_node;
76 * Use the generic buffer metadata + first line of packet data prefetch
77 * stage function from <api/pipeline.h>. This is usually a Good Idea.
79 #define stage0 generic_stage0
81 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
84 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi);
85 int disposition = CNAT_V4_ICMP_Q_E_I2O_T;
86 int counter = CNAT_V4_ICMP_Q_I2O_E_T_PKT;
88 ipv4_header *ip = (ipv4_header *)vlib_buffer_get_current(b0);
89 u8 ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
90 icmp_v4_t *icmp = (icmp_v4_t *)((u8*)ip + ipv4_hdr_len);
91 vlib_node_t *n = vlib_get_node (vm, cnat_ipv4_icmp_q_inside_input_exc_node.index);
92 u32 node_counter_base_index = n->error_heap_index;
93 vlib_error_main_t * em = &vm->error_main;
96 cnat_gen_icmp_info info;
97 cnat_db_key_bucket_t ki;
98 cnat_main_db_entry_t *db = NULL;
100 PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
101 ki.k.k.vrf, CNAT_ICMP)
104 clib_net_to_host_u32(ip->src_addr);
106 clib_net_to_host_u16(icmp->identifier);
108 dest_info.k.port = 0;
109 dest_info.k.ipv4 = clib_net_to_host_u32(ip->dest_addr);
110 PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
111 dest_info.k.vrf, CNAT_ICMP)
113 db = cnat_get_main_db_entry_v2(&ki, PORT_SINGLE, PORT_TYPE_DYNAMIC,
115 if (PREDICT_TRUE(db != 0)) {
117 if (PREDICT_FALSE(icmp_debug_flag)) {
118 printf("\nDUMPING ICMP PKT BEFORE\n");
122 if (PLATFORM_HANDLE_TTL_DECREMENT) {
124 * Decrement TTL and update IPv4 checksum
126 ipv4_decr_ttl_n_calc_csum(ip);
130 * step 6 do nat before fwd pkt
132 swap_ip_src_icmp_id(ip, icmp, db, db->in2out_key.k.vrf);
134 if (PREDICT_FALSE(icmp_debug_flag)) {
135 printf("\nDUMPING ICMP PKT AFTER\n");
140 * update db for this pkt
142 CNAT_DB_UPDATE_IN2OUT_TIMER
143 in2out_forwarding_count++;
146 switch (info.error) {
147 case (CNAT_NO_VRF_RUN):
148 counter = CNAT_V4_ICMP_Q_I2O_E_DR_PKT;
150 case (CNAT_OUT_LIMIT):
151 counter = CNAT_V4_ICMP_Q_I2O_E_DO_PKT;
153 case (CNAT_NO_PORT_ANY):
154 case (CNAT_NO_POOL_ANY):
155 case (CNAT_BAD_INUSE_ANY):
156 case (CNAT_NOT_FOUND_ANY):
157 counter = CNAT_V4_ICMP_Q_I2O_E_DA_PKT;
159 case (CNAT_INV_PORT_DIRECT):
160 case (CNAT_DEL_PORT_DIRECT):
161 case (CNAT_BAD_INUSE_DIRECT):
162 case (CNAT_NOT_FOUND_DIRECT):
163 counter = CNAT_V4_ICMP_Q_I2O_E_DD_PKT;
165 case (CNAT_ERR_NO_SESSION_DB):
166 counter = CNAT_V4_ICMP_Q_I2O_E_DS_PKT;
169 counter = CNAT_V4_ICMP_Q_I2O_E_DC_PKT;
173 * send to icmp msg generate node
175 if (info.gen_icmp_msg == CNAT_ICMP_MSG) {
177 u32 *fd = (u32*)ctx->feature_data;
178 fd[0] = info.svi_addr;
179 fd[1] = CNAT_ICMP_DEST_UNREACHABLE;
181 disposition = CNAT_V4_ICMP_Q_E_I2O_GEN;
182 counter = CNAT_V4_ICMP_Q_I2O_E_G_PKT;
184 disposition = CNAT_V4_ICMP_Q_E_I2O_D;
185 counter = CNAT_V4_ICMP_Q_I2O_E_D_PKT;
187 DEBUG_I2O_DROP(CNAT_DEBUG_DROP_ICMP)
190 em->counters[node_counter_base_index + counter] += 1;
195 #include <vnet/pipeline.h>
197 static uword cnat_ipv4_icmp_q_inside_input_exc_node_fn (vlib_main_t * vm,
198 vlib_node_runtime_t * node,
199 vlib_frame_t * frame)
201 return dispatch_pipeline (vm, node, frame);
204 VLIB_REGISTER_NODE (cnat_ipv4_icmp_q_inside_input_exc_node) = {
205 .function = cnat_ipv4_icmp_q_inside_input_exc_node_fn,
206 .name = "vcgn-v4-icmp-q-i2o-e",
207 .vector_size = sizeof (u32),
208 .type = VLIB_NODE_TYPE_INTERNAL,
210 .n_errors = ARRAY_LEN(cnat_ipv4_icmp_q_inside_input_exc_error_strings),
211 .error_strings = cnat_ipv4_icmp_q_inside_input_exc_error_strings,
213 .n_next_nodes = CNAT_V4_ICMP_Q_E_I2O_NEXT,
215 /* edit / add dispositions here */
217 //[CNAT_V4_ICMP_Q_E_I2O_GEN] = "icmp_msg_gen", /* Currently it will go
218 //to ip4-input node. We have to port icmp msg generator node */
219 [CNAT_V4_ICMP_Q_E_I2O_T] = "ip4-input",
220 [CNAT_V4_ICMP_Q_E_I2O_D] = "error-drop",
225 clib_error_t *cnat_ipv4_icmp_q_inside_input_exc_init (vlib_main_t *vm)
227 cnat_ipv4_icmp_q_inside_input_exc_main_t * mp = &cnat_ipv4_icmp_q_inside_input_exc_main;
230 mp->vnet_main = vnet_get_main();
235 VLIB_INIT_FUNCTION (cnat_ipv4_icmp_q_inside_input_exc_init);