Initial commit of vpp code.
[vpp.git] / vnet / vnet / vcgn / cnat_ipv4_icmp_query_inside_input_exception.c
1 /* 
2  *---------------------------------------------------------------------------
3  * cnat_ipv4_icmp_query_inside_input_exception.c - cnat_ipv4_icmp_query_inside_input_exception node pipeline stage functions
4  *
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:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *---------------------------------------------------------------------------
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vppinfra/error.h>
23 #include <vnet/buffer.h>
24
25 #include "cnat_ipv4_icmp.h"
26
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)")
37
38 typedef enum {
39 #define _(sym,str) sym,
40   foreach_cnat_ipv4_icmp_q_inside_input_exc_error
41 #undef _
42   CNAT_IPV4_ICMP_Q_INSIDE_INPUT_EXCEPTIONS_N_ERROR,
43 } cnat_ipv4_icmp_q_inside_input_exc_error_t;
44
45
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
49 #undef _
50 };
51
52 typedef struct {
53   u32 cached_next_index;
54   /* $$$$ add data here */
55
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;
60
61 typedef enum {
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;
67
68 #define CNAT_V4_ICMP_Q_E_I2O_GEN CNAT_V4_ICMP_Q_E_I2O_T
69
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;
72
73 #define NSTAGES 2
74
75 /*
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.
78  */
79 #define stage0 generic_stage0
80
81 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
82                               u32 bi)
83 {
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;
87
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;
94
95     cnat_key_t dest_info;
96     cnat_gen_icmp_info info;
97     cnat_db_key_bucket_t ki;
98     cnat_main_db_entry_t *db = NULL;
99
100     PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
101                         ki.k.k.vrf, CNAT_ICMP)
102
103     ki.k.k.ipv4  =
104          clib_net_to_host_u32(ip->src_addr);
105     ki.k.k.port = 
106          clib_net_to_host_u16(icmp->identifier);
107
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)
112
113     db = cnat_get_main_db_entry_v2(&ki, PORT_SINGLE, PORT_TYPE_DYNAMIC,
114                                 &info, &dest_info);
115     if (PREDICT_TRUE(db != 0)) {
116
117         if (PREDICT_FALSE(icmp_debug_flag)) {
118             printf("\nDUMPING ICMP PKT BEFORE\n");
119             print_icmp_pkt(ip);
120         }
121
122         if (PLATFORM_HANDLE_TTL_DECREMENT) {
123             /*
124              * Decrement TTL and update IPv4 checksum
125              */
126             ipv4_decr_ttl_n_calc_csum(ip);
127         }
128
129         /*
130          * step 6 do nat before fwd pkt
131          */
132         swap_ip_src_icmp_id(ip, icmp, db, db->in2out_key.k.vrf);
133
134         if (PREDICT_FALSE(icmp_debug_flag)) {
135             printf("\nDUMPING ICMP PKT AFTER\n");
136             print_icmp_pkt(ip);
137         }
138
139         /*
140          * update db for this pkt
141          */
142         CNAT_DB_UPDATE_IN2OUT_TIMER 
143         in2out_forwarding_count++;
144
145     } else {
146         switch (info.error) {
147         case (CNAT_NO_VRF_RUN):
148             counter = CNAT_V4_ICMP_Q_I2O_E_DR_PKT;
149             break;
150         case (CNAT_OUT_LIMIT):
151             counter = CNAT_V4_ICMP_Q_I2O_E_DO_PKT;
152             break;
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;
158             break;
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;
164             break;
165         case (CNAT_ERR_NO_SESSION_DB):
166             counter = CNAT_V4_ICMP_Q_I2O_E_DS_PKT;
167             break;
168         default:
169             counter = CNAT_V4_ICMP_Q_I2O_E_DC_PKT;
170             break;
171         }
172         /*
173          * send to icmp msg generate node
174          */
175         if (info.gen_icmp_msg == CNAT_ICMP_MSG) {
176             #if 0
177             u32 *fd = (u32*)ctx->feature_data;
178             fd[0] = info.svi_addr;
179             fd[1] = CNAT_ICMP_DEST_UNREACHABLE;
180             #endif
181             disposition = CNAT_V4_ICMP_Q_E_I2O_GEN;
182             counter = CNAT_V4_ICMP_Q_I2O_E_G_PKT;
183         } else {
184             disposition = CNAT_V4_ICMP_Q_E_I2O_D;
185             counter = CNAT_V4_ICMP_Q_I2O_E_D_PKT;
186         }
187         DEBUG_I2O_DROP(CNAT_DEBUG_DROP_ICMP)
188     }
189             
190     em->counters[node_counter_base_index + counter] += 1;
191     
192     return disposition;
193 }
194
195 #include <vnet/pipeline.h>
196
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)
200 {
201     return dispatch_pipeline (vm, node, frame);
202 }
203
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,
209
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,
212
213   .n_next_nodes = CNAT_V4_ICMP_Q_E_I2O_NEXT,
214
215   /* edit / add dispositions here */
216   .next_nodes = {
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",
221   },
222 };
223
224
225 clib_error_t *cnat_ipv4_icmp_q_inside_input_exc_init (vlib_main_t *vm)
226 {
227   cnat_ipv4_icmp_q_inside_input_exc_main_t * mp = &cnat_ipv4_icmp_q_inside_input_exc_main;
228
229   mp->vlib_main = vm;
230   mp->vnet_main = vnet_get_main();
231
232   return 0;
233 }
234
235 VLIB_INIT_FUNCTION (cnat_ipv4_icmp_q_inside_input_exc_init);