Initial commit of vpp code.
[vpp.git] / vnet / vnet / vcgn / cnat_ipv4_udp_inside_input_exceptions.c
1 /*
2  *---------------------------------------------------------------------------
3  * cnat_ipv4_udp_inside_input_exception_stages.c - cnat_ipv4_udp_inside_input_exception node pipeline stage functions
4  *
5  *
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:
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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  *---------------------------------------------------------------------------
19  */
20
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 #include <vppinfra/error.h>
24 #include <vnet/buffer.h>
25
26 #include "cnat_global.h"
27 #include "cnat_db.h"
28 #include "cnat_ipv4_udp.h"
29
30 /*
31  * Dump these counters via the "show error" CLI command
32  */
33
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")
45
46 typedef enum {
47 #define _(sym,str) sym,
48   foreach_cnat_ipv4_udp_inside_input_exc_error
49 #undef _
50   CNAT_IPV4_UDP_INSIDE_INPUT_EXCEPTIONS_N_ERROR,
51 } cnat_ipv4_udp_inside_input_exc_error_t;
52
53
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
57 #undef _
58 };
59
60 typedef struct {
61   u32 cached_next_index;
62   /* $$$$ add data here */
63
64   /* convenience variables */
65   vlib_main_t * vlib_main;
66   vnet_main_t * vnet_main;
67 } cnat_ipv4_udp_inside_input_exc_main_t;
68
69 typedef enum {
70     CNAT_V4_UDP_I2O_T,
71     CNAT_V4_UDP_I2O_D,
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;
75
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;
78
79 #define NSTAGES 2
80
81 /*
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.
84  */
85 #define stage0 generic_stage0
86
87 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
88                               u32 bi)
89 {
90     vlib_buffer_t *b0 = vlib_get_buffer (vm, bi);
91     vlib_node_t *n = 
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;
95
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;
106
107     cnat_key_t dest_info;
108
109     PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
110                              vnet_buffer(b0)->vcgn_uii.key.k.vrf,
111                              CNAT_UDP)
112
113     vnet_buffer(b0)->vcgn_uii.key.k.ipv4 = clib_net_to_host_u32(ip->src_addr);
114
115     PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
116                              ki.k.k.vrf, CNAT_UDP)
117
118     ki.k.k.ipv4 = clib_net_to_host_u32(ip->src_addr);
119
120     
121     /* MUST REVISIT: commentting frag check. Unconditional destination port 
122      * update. DONOT remove this #if 0 */
123     ki.k.k.port =
124         clib_net_to_host_u16(udp->src_port);
125     dest_info.k.port =
126         clib_net_to_host_u16(udp->dest_port);
127 #if 0
128     if(PREDICT_FALSE(ctx->ru.rx.frag)) {
129 #ifdef TOBE_PORTED
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;
135 #endif
136     } else {
137         ki.k.k.port =
138             clib_net_to_host_u16(udp->src_port);
139         dest_info.k.port =
140             clib_net_to_host_u16(udp->dest_port);
141     }
142 #endif /* if 0 */
143
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)
147
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,
151                                            &info, &dest_info);
152         } else {
153             db = cnat_get_main_db_entry_v2(&ki, PORT_S_EVEN, PORT_TYPE_DYNAMIC,
154                                            &info, &dest_info);
155         }
156     } else {
157         /*
158      * No UDP port value of 0 - drop it
159      */
160         db = NULL;
161         info.error = CNAT_ERR_BAD_TCP_UDP_PORT;
162     }
163
164     if (PREDICT_TRUE((u64)db)) {
165
166         if (PLATFORM_HANDLE_TTL_DECREMENT) {
167             /*
168              * Decrement TTL and update IPv4 checksum
169              */
170             ipv4_decr_ttl_n_calc_csum(ip);
171         }
172
173         /*
174          * step 6 do nat before fwd pkt
175          */
176         swap_ip_src_udp_port(ip, udp, db);
177         /*
178          * update db for this pkt
179          */
180         CNAT_DB_UPDATE_IN2OUT_TIMER
181
182         /* Check timeout db if there is config for this */
183         (void) query_and_update_db_timeout((void *)db, MAIN_DB_TYPE);
184
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++;
189
190     } else {
191         switch (info.error) {
192         case (CNAT_NO_VRF_RUN):
193             em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DR_PKT] += 1;
194             break;
195         case (CNAT_OUT_LIMIT):
196             em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DO_PKT] += 1;
197             break;
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;
203             break;
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;
209             break;
210         case (CNAT_ERR_BAD_TCP_UDP_PORT):
211             em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DI_PKT] += 1;
212             break;
213         case (CNAT_ERR_NO_SESSION_DB):
214             em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DS_PKT] += 1;
215             break;
216         default:
217             em->counters[node_counter_base_index + CNAT_V4_UDP_I2O_DC_PKT] += 1;
218             break;
219         }
220         /*
221          * send to icmp msg generate node
222          */
223         if (info.gen_icmp_msg == CNAT_ICMP_MSG) {
224 #ifdef TOBE_PORTED
225             u32 *fd = (u32*)ctx->feature_data;
226             fd[0] = info.svi_addr;
227             fd[1] = CNAT_ICMP_DEST_UNREACHABLE;
228 #endif
229             disposition = CNAT_V4_ICMP_G_I2O_T;
230             counter = CNAT_V4_ICMP_G_I2O_T_PKT;
231         } else {
232             disposition = CNAT_V4_UDP_I2O_D;
233             counter = CNAT_V4_UDP_I2O_D_PKT;
234         }
235         DEBUG_I2O_DROP(CNAT_DEBUG_DROP_UDP)
236     }
237
238     em->counters[node_counter_base_index + counter] += 1;
239     
240     return disposition;
241 }
242
243
244 #include <vnet/pipeline.h>
245
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)
249 {
250     return dispatch_pipeline (vm, node, frame);
251 }
252
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,
258
259   .n_errors = ARRAY_LEN(cnat_ipv4_udp_inside_input_exc_error_strings),
260   .error_strings = cnat_ipv4_udp_inside_input_exc_error_strings,
261
262   .n_next_nodes = CNAT_V4_UDP_INSIDE_INPUT_EXC_N_NEXT,
263
264   /* edit / add dispositions here */
265   .next_nodes = {
266         [CNAT_V4_UDP_I2O_T] = "ip4-input",
267         [CNAT_V4_UDP_I2O_D] = "error-drop",
268   },
269 };
270
271
272 clib_error_t *cnat_ipv4_udp_inside_input_exc_init (vlib_main_t *vm)
273 {
274   cnat_ipv4_udp_inside_input_exc_main_t * mp = &cnat_ipv4_udp_inside_input_exc_main;
275
276   mp->vlib_main = vm;
277   mp->vnet_main = vnet_get_main();
278
279   return 0;
280 }
281
282 VLIB_INIT_FUNCTION (cnat_ipv4_udp_inside_input_exc_init);
283