2 *---------------------------------------------------------------------------
3 * cnat_ipv4_tcp_inside_input_exceptions.c -
4 * cnat_ipv4_tcp_inside_input_exceptions node pipeline stage functions
7 * Copyright (c) 2008-2014 Cisco and/or its affiliates.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at:
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *---------------------------------------------------------------------------
22 #include <vlib/vlib.h>
23 #include <vnet/vnet.h>
24 #include <vppinfra/error.h>
25 #include <vnet/buffer.h>
28 #include "tcp_header_definitions.h"
29 #include "cnat_config.h"
30 #include "cnat_global.h"
31 #include "cnat_v4_functions.h"
34 #define foreach_cnat_ipv4_tcp_inside_input_exc_error \
35 _(CNAT_V4_TCP_I2O_E_T_PKT, "v4 tcp i2o-e transmit natted pkt") \
36 _(CNAT_V4_TCP_I2O_E_D_NON_SYN_PKT, "v4 tcp i2o-e non syn drop") \
37 _(CNAT_V4_TCP_I2O_E_D_INVALID_PKT, "v4 tcp i2o-e invalid pkt drop") \
38 _(CNAT_V4_TCP_I2O_E_DROP, "v4 tcp i2o-e drop") \
39 _(CNAT_V4_TCP_I2O_E_GEN_ICMP, "v4 tcp i2o-e gen icmp msg") \
40 _(CNAT_V4_TCP_I2O_E_D_NO_SESSION, "v4 tcp i2o-e no session db entry drop")
43 #define _(sym,str) sym,
44 foreach_cnat_ipv4_tcp_inside_input_exc_error
46 CNAT_IPV4_TCP_INSIDE_INPUT_EXCEPTIONS_N_ERROR,
47 } cnat_ipv4_tcp_inside_input_exc_error_t;
50 static char * cnat_ipv4_tcp_inside_input_exc_error_strings[] = {
51 #define _(sym,string) string,
52 foreach_cnat_ipv4_tcp_inside_input_exc_error
57 u32 cached_next_index;
58 /* $$$$ add data here */
60 /* convenience variables */
61 vlib_main_t * vlib_main;
62 vnet_main_t * vnet_main;
63 } cnat_ipv4_tcp_inside_input_exc_main_t;
67 //CNAT_V4_TCP_I2O_E_ICMP,
69 CNAT_V4_TCP_I2O_E_NEXT,
70 } cnat_ipv4_udp_inside_input_exc_next_t;
72 #define CNAT_V4_TCP_I2O_E_ICMP CNAT_V4_TCP_I2O_E_D
74 cnat_ipv4_tcp_inside_input_exc_main_t cnat_ipv4_tcp_inside_input_exc_main;
75 vlib_node_registration_t cnat_ipv4_tcp_inside_input_exc_node;
80 * Use the generic buffer metadata + first line of packet data prefetch
81 * stage function from <api/pipeline.h>. This is usually a Good Idea.
83 #define stage0 generic_stage0
86 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
89 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi);
91 vlib_get_node (vm, cnat_ipv4_tcp_inside_input_exc_node.index);
92 u32 node_counter_base_index = n->error_heap_index;
93 vlib_error_main_t * em = &vm->error_main;
95 cnat_gen_icmp_info info;
96 cnat_db_key_bucket_t ki;
97 cnat_main_db_entry_t *db = NULL;
98 ipv4_header *ip = (ipv4_header *)vlib_buffer_get_current(b0);
99 u8 ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
100 tcp_hdr_type *tcp = (tcp_hdr_type *)((u8*)ip + ipv4_hdr_len);
101 int disposition = CNAT_V4_TCP_I2O_E_T;
102 int counter = CNAT_V4_TCP_I2O_E_T_PKT;
103 cnat_key_t dest_info;
107 window = (u32)clib_net_to_host_u16(tcp->window_size);
108 calculate_window_scale(tcp, &scale);
110 dest_info.k.port = clib_net_to_host_u16(tcp->dest_port);
111 dest_info.k.ipv4 = clib_net_to_host_u32(ip->dest_addr);
113 PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
114 dest_info.k.vrf, CNAT_TCP)
116 /* for TCP if not SYN or if src_port is 0, silently drop the packet */
117 if (PREDICT_FALSE(!((tcp->flags & TCP_FLAG_SYN) && (tcp->src_port)))) {
120 * If the packet is dropped due to both reasons,
121 * count it as invalid packet drop
123 if (!tcp->src_port) {
124 counter = CNAT_V4_TCP_I2O_E_D_INVALID_PKT;
126 counter = CNAT_V4_TCP_I2O_E_D_NON_SYN_PKT;
128 disposition = CNAT_V4_TCP_I2O_E_D;
132 PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
133 ki.k.k.vrf, CNAT_TCP)
135 ki.k.k.ipv4 = clib_net_to_host_u32(ip->src_addr);
136 ki.k.k.port = clib_net_to_host_u16(tcp->src_port);
138 db = cnat_get_main_db_entry_v2(&ki, PORT_SINGLE, PORT_TYPE_DYNAMIC, &info,
143 if(PREDICT_TRUE(db)) {
144 printf("create db %x ip %x->%x port %x->%x dst_ip %x\n", db,
145 db->in2out_key.k.ipv4, db->out2in_key.k.ipv4,
146 db->in2out_key.k.port, db->out2in_key.k.port, db->dst_ipv4);
151 if (PREDICT_FALSE(db == 0)) {
152 /* failed to create new db entry due to either no more port, or user limit reached,
153 * need to generate ICMP type=3,code=13 msg here,
157 * we rate limit the icmp msg per private user,
158 * so we don't flood a user with icmp msg
159 * in case the per user port limit reached
161 if (PREDICT_TRUE(info.gen_icmp_msg == CNAT_ICMP_MSG)) {
162 /* KEEPING THINGS COMMENTED HERE..MAY NEED TO REVISIT AGAIN */
164 u32 *fd = (u32*)ctx->feature_data;
165 fd[0] = info.svi_addr;
166 fd[1] = CNAT_ICMP_DEST_UNREACHABLE;
169 * Let's reverse the direction from i2o to o2i.
170 * This will help using the correct VRF in the fib lookup (AVSM)
171 * especially for the o2i_vrf_override case
173 ctx->ru.rx.direction = 0; // 0 - o2i, 1 - i2o
175 disposition = CNAT_V4_TCP_I2O_E_ICMP;
176 counter = CNAT_V4_TCP_I2O_E_GEN_ICMP;
179 disposition = CNAT_V4_TCP_I2O_E_D;
180 counter = CNAT_V4_TCP_I2O_E_DROP;
182 //DEBUG_I2O_DROP(CNAT_DEBUG_DROP_TCP)
185 if (PLATFORM_HANDLE_TTL_DECREMENT) {
187 * Decrement TTL and update IPv4 checksum
189 ipv4_decr_ttl_n_calc_csum(ip);
192 /* NAT the packet and fix checksum */
194 tcp_in2out_nat_mss_n_checksum(ip,
196 db->out2in_key.k.ipv4,
197 db->out2in_key.k.port,
199 /*, db->in2out_key.k.vrf */);
201 /* this must be inside to outside SYN, do mss here */
203 /* update translation counters */
206 /* set keepalive timer */
208 if(PREDICT_TRUE((dest_info.k.ipv4 == db->dst_ipv4) &&
209 (dest_info.k.port == db->dst_port))) {
210 if(PREDICT_FALSE(!ALG_ENABLED_DB(db))) {
211 //This check is done since proto_data is part of union in main
214 db->proto_data.tcp_seq_chk.seq_no =
215 clib_net_to_host_u32(tcp->seq_num);
216 db->proto_data.tcp_seq_chk.ack_no =
217 clib_net_to_host_u32(tcp->ack_num);
219 db->diff_window = window;
222 PLATFORM_DEBUG_PRINT("\nMain DB seq no = %u,"
223 "ack no = %u, window = %u,"
225 db->proto_data.tcp_seq_chk.seq_no,
226 db->proto_data.tcp_seq_chk.ack_no,
230 V4_TCP_UPDATE_SESSION_FLAG(db, tcp);
231 /* Check timeout db if there is config for this */
232 (void) query_and_update_db_timeout((void *)db, MAIN_DB_TYPE);
233 db->entry_expires = cnat_current_time;
235 /* Got to find out the session entry corresponding to this..*/
236 cnat_session_entry_t *sdb;
237 sdb = cnat_session_db_lookup_entry(
238 &dest_info, db - cnat_main_db);
239 if(PREDICT_FALSE(sdb == NULL)) {
240 disposition = CNAT_V4_TCP_I2O_E_D;
241 counter = CNAT_V4_TCP_I2O_E_D_NO_SESSION;
244 sdb->tcp_seq_num = clib_net_to_host_u32(tcp->seq_num);
245 sdb->ack_no = clib_net_to_host_u32(tcp->ack_num);
247 sdb->window = window;
250 PLATFORM_DEBUG_PRINT("\nSDB seq no = %u, ack no = %u, window = %u"
257 V4_TCP_UPDATE_SESSION_DB_FLAG(sdb, tcp);
258 /* Check timeout db if there is config for this */
259 (void) query_and_update_db_timeout((void *)sdb, SESSION_DB_TYPE);
260 sdb->entry_expires = cnat_current_time;
263 //PLATFORM_CNAT_SET_TX_VRF(ctx,db->out2in_key.k.vrf)
265 counter = CNAT_V4_TCP_I2O_E_T_PKT;
266 in2out_forwarding_count++;
271 em->counters[node_counter_base_index + counter] += 1;
276 #include <vnet/pipeline.h>
278 static uword cnat_ipv4_tcp_inside_input_exc_node_fn (vlib_main_t * vm,
279 vlib_node_runtime_t * node,
280 vlib_frame_t * frame)
282 return dispatch_pipeline (vm, node, frame);
285 VLIB_REGISTER_NODE (cnat_ipv4_tcp_inside_input_exc_node) = {
286 .function = cnat_ipv4_tcp_inside_input_exc_node_fn,
287 .name = "vcgn-v4-tcp-i2o-e",
288 .vector_size = sizeof (u32),
289 .type = VLIB_NODE_TYPE_INTERNAL,
291 .n_errors = ARRAY_LEN(cnat_ipv4_tcp_inside_input_exc_error_strings),
292 .error_strings = cnat_ipv4_tcp_inside_input_exc_error_strings,
294 .n_next_nodes = CNAT_V4_TCP_I2O_E_NEXT,
296 /* edit / add dispositions here */
298 [CNAT_V4_TCP_I2O_E_T] = "ip4-input",
299 [CNAT_V4_TCP_I2O_E_D] = "error-drop",
304 clib_error_t *cnat_ipv4_tcp_inside_input_exc_init (vlib_main_t *vm)
306 cnat_ipv4_tcp_inside_input_exc_main_t * mp = &cnat_ipv4_tcp_inside_input_exc_main;
309 mp->vnet_main = vnet_get_main();
314 VLIB_INIT_FUNCTION (cnat_ipv4_tcp_inside_input_exc_init);