Initial commit of vpp code.
[vpp.git] / vnet / vnet / vcgn / cnat_ipv4_tcp_inside_input_exceptions.c
1 /* 
2  *---------------------------------------------------------------------------
3  * cnat_ipv4_tcp_inside_input_exceptions.c -
4  * cnat_ipv4_tcp_inside_input_exceptions node pipeline stage functions
5  *
6  *
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:
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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  *---------------------------------------------------------------------------
20  */
21
22 #include <vlib/vlib.h>
23 #include <vnet/vnet.h>
24 #include <vppinfra/error.h>
25 #include <vnet/buffer.h>
26
27 #include "cnat_db.h"
28 #include "tcp_header_definitions.h"
29 #include "cnat_config.h"
30 #include "cnat_global.h"
31 #include "cnat_v4_functions.h"
32
33
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")
41
42 typedef enum {
43 #define _(sym,str) sym,
44   foreach_cnat_ipv4_tcp_inside_input_exc_error
45 #undef _
46   CNAT_IPV4_TCP_INSIDE_INPUT_EXCEPTIONS_N_ERROR,
47 } cnat_ipv4_tcp_inside_input_exc_error_t;
48
49
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
53 #undef _
54 };
55
56 typedef struct {
57   u32 cached_next_index;
58   /* $$$$ add data here */
59
60   /* convenience variables */
61   vlib_main_t * vlib_main;
62   vnet_main_t * vnet_main;
63 } cnat_ipv4_tcp_inside_input_exc_main_t;
64
65 typedef enum {
66     CNAT_V4_TCP_I2O_E_T,
67     //CNAT_V4_TCP_I2O_E_ICMP,
68     CNAT_V4_TCP_I2O_E_D, 
69     CNAT_V4_TCP_I2O_E_NEXT,
70 } cnat_ipv4_udp_inside_input_exc_next_t;
71
72 #define CNAT_V4_TCP_I2O_E_ICMP CNAT_V4_TCP_I2O_E_D
73
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;
76
77 #define NSTAGES 2
78
79 /*
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.
82  */
83 #define stage0 generic_stage0
84
85
86 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
87                               u32 bi)
88 {
89     vlib_buffer_t *b0 = vlib_get_buffer (vm, bi);
90     vlib_node_t *n = 
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;
94
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;
104     u32 window;
105     u8 scale;
106
107     window = (u32)clib_net_to_host_u16(tcp->window_size);
108     calculate_window_scale(tcp, &scale);
109   
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);
112
113     PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
114                              dest_info.k.vrf, CNAT_TCP)
115
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)))) {
118
119         /*
120          * If the packet is dropped due to both reasons,
121          * count it as invalid packet drop
122          */
123         if (!tcp->src_port) {
124             counter = CNAT_V4_TCP_I2O_E_D_INVALID_PKT;
125         } else {
126             counter = CNAT_V4_TCP_I2O_E_D_NON_SYN_PKT;
127         }
128         disposition = CNAT_V4_TCP_I2O_E_D;
129         goto in2out_e;
130     }
131
132     PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
133                              ki.k.k.vrf, CNAT_TCP)
134
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);
137
138     db = cnat_get_main_db_entry_v2(&ki, PORT_SINGLE, PORT_TYPE_DYNAMIC, &info,
139                 &dest_info);
140
141
142 #if DEBUG > 1
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);
147         }
148 #endif
149
150
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,
154          */
155
156         /*
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
160          */
161         if (PREDICT_TRUE(info.gen_icmp_msg == CNAT_ICMP_MSG)) {
162             /* KEEPING THINGS COMMENTED HERE..MAY NEED TO REVISIT AGAIN */
163             #if 0
164             u32 *fd = (u32*)ctx->feature_data;
165             fd[0] = info.svi_addr;
166             fd[1] = CNAT_ICMP_DEST_UNREACHABLE;
167
168             /* 
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
172              */
173             ctx->ru.rx.direction = 0; // 0 - o2i, 1 - i2o
174             #endif
175             disposition = CNAT_V4_TCP_I2O_E_ICMP;
176             counter = CNAT_V4_TCP_I2O_E_GEN_ICMP;
177
178         } else {
179             disposition = CNAT_V4_TCP_I2O_E_D;
180             counter = CNAT_V4_TCP_I2O_E_DROP;
181         }
182         //DEBUG_I2O_DROP(CNAT_DEBUG_DROP_TCP)
183     } else {
184
185         if (PLATFORM_HANDLE_TTL_DECREMENT) {
186             /*
187              * Decrement TTL and update IPv4 checksum
188              */
189             ipv4_decr_ttl_n_calc_csum(ip);
190         }
191
192         /* NAT the packet and fix checksum */
193
194         tcp_in2out_nat_mss_n_checksum(ip, 
195                                       tcp, 
196                                       db->out2in_key.k.ipv4, 
197                                       db->out2in_key.k.port, 
198                                       db 
199                                       /*, db->in2out_key.k.vrf */);
200
201         /* this must be inside to outside SYN, do mss here */
202
203         /* update translation counters */
204         db->in2out_pkts++;
205
206         /* set keepalive timer */
207
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
212             //db entry
213                                         
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);
218             db->scale              = scale;
219             db->diff_window        = window;
220         }               
221 #if DEBUG > 1
222             PLATFORM_DEBUG_PRINT("\nMain DB seq no = %u," 
223                                     "ack no = %u, window = %u,"
224                                     "scale = %u",
225                                     db->proto_data.tcp_seq_chk.seq_no,
226                                     db->proto_data.tcp_seq_chk.ack_no,
227                                     db->diff_window
228                                     db->scale);
229 #endif      
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;
234         } else {
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;
242                 goto in2out_e;
243             }
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);
246             sdb->scale       = scale;
247             sdb->window      = window;
248
249 #if DEBUG > 1
250             PLATFORM_DEBUG_PRINT("\nSDB seq no = %u, ack no = %u, window = %u"
251                                  "\nSDB scale  = %u" ,
252                                   sdb->tcp_seq_num,
253                                   sdb->ack_no,
254                                   sdb->window,
255                                   sdb->scale);
256 #endif      
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;
261         }
262
263         //PLATFORM_CNAT_SET_TX_VRF(ctx,db->out2in_key.k.vrf)
264
265         counter = CNAT_V4_TCP_I2O_E_T_PKT;
266         in2out_forwarding_count++;
267     }
268
269 in2out_e:
270
271     em->counters[node_counter_base_index + counter] += 1;
272     
273     return disposition;
274 }
275
276 #include <vnet/pipeline.h>
277
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)
281 {
282     return dispatch_pipeline (vm, node, frame);
283 }
284
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,
290
291   .n_errors = ARRAY_LEN(cnat_ipv4_tcp_inside_input_exc_error_strings),
292   .error_strings = cnat_ipv4_tcp_inside_input_exc_error_strings,
293
294   .n_next_nodes = CNAT_V4_TCP_I2O_E_NEXT,
295
296   /* edit / add dispositions here */
297   .next_nodes = {
298         [CNAT_V4_TCP_I2O_E_T] = "ip4-input",
299         [CNAT_V4_TCP_I2O_E_D] = "error-drop",
300   },
301 };
302
303
304 clib_error_t *cnat_ipv4_tcp_inside_input_exc_init (vlib_main_t *vm)
305 {
306   cnat_ipv4_tcp_inside_input_exc_main_t * mp = &cnat_ipv4_tcp_inside_input_exc_main;
307
308   mp->vlib_main = vm;
309   mp->vnet_main = vnet_get_main();
310
311   return 0;
312 }
313
314 VLIB_INIT_FUNCTION (cnat_ipv4_tcp_inside_input_exc_init);