FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
cnat_ipv4_tcp_inside_input_exceptions.c
Go to the documentation of this file.
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,
45 #undef _
48 
49 
51 #define _(sym,string) string,
53 #undef _
54 };
55 
56 typedef struct {
58  /* $$$$ add data here */
59 
60  /* convenience variables */
64 
65 typedef enum {
67  //CNAT_V4_TCP_I2O_E_ICMP,
71 
72 #define CNAT_V4_TCP_I2O_E_ICMP CNAT_V4_TCP_I2O_E_D
73 
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 =
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;
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 
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;
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 
186  /*
187  * Decrement TTL and update IPv4 checksum
188  */
190  }
191 
192  /* NAT the packet and fix checksum */
193 
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
231  /* Check timeout db if there is config for this */
232  (void) query_and_update_db_timeout((void *)db, MAIN_DB_TYPE);
234  } else {
235  /* Got to find out the session entry corresponding to this..*/
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
258  /* Check timeout db if there is config for this */
259  (void) query_and_update_db_timeout((void *)sdb, SESSION_DB_TYPE);
261  }
262 
263  //PLATFORM_CNAT_SET_TX_VRF(ctx,db->out2in_key.k.vrf)
264 
265  counter = CNAT_V4_TCP_I2O_E_T_PKT;
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 
279  vlib_node_runtime_t * node,
280  vlib_frame_t * frame)
281 {
282  return dispatch_pipeline (vm, node, frame);
283 }
284 
287  .name = "vcgn-v4-tcp-i2o-e",
288  .vector_size = sizeof (u32),
290 
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 
305 {
307 
308  mp->vlib_main = vm;
309  mp->vnet_main = vnet_get_main();
310 
311  return 0;
312 }
313 
#define ALG_ENABLED_DB(db)
Definition: cnat_db.h:694
union cnat_main_db_entry_t::@253 proto_data
struct cnat_main_db_entry_t::@253::tcp_seq_chk_t tcp_seq_chk
u32 error_heap_index
Definition: node.h:278
u32 diff_window
Definition: cnat_db.h:275
cnat_main_db_entry_t * cnat_main_db
Definition: cnat_db_v2.c:201
u16 query_and_update_db_timeout(void *db, u8 db_type)
Definition: cnat_db_v2.c:2325
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
#define PREDICT_TRUE(x)
Definition: clib.h:98
static u32 last_stage(vlib_main_t *vm, vlib_node_runtime_t *node, u32 bi)
void ipv4_decr_ttl_n_calc_csum(ipv4_header *ipv4)
#define NULL
Definition: clib.h:55
u32 cnat_current_time
Definition: cnat_global.c:29
u32 tcp_seq_num
Definition: cnat_db.h:369
#define V4_TCP_UPDATE_SESSION_FLAG(db, tcp)
void tcp_in2out_nat_mss_n_checksum(ipv4_header *ip, tcp_hdr_type *tcp, u32 ipv4_addr, u16 port, cnat_main_db_entry_t *db)
u32 ack_no
Definition: cnat_db.h:372
Definition: cnat_db.h:153
struct _vlib_node_registration vlib_node_registration_t
#define MAIN_DB_TYPE
Definition: cnat_db.h:626
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define PLATFORM_DEBUG_PRINT(...)
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
cnat_main_db_entry_t * cnat_get_main_db_entry_v2(cnat_db_key_bucket_t *ki, port_pair_t port_pair_type, port_type_t port_type, cnat_gen_icmp_info *info, cnat_key_t *dest_info)
Definition: cnat_db_v2.c:1647
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:190
cnat_key_t k
Definition: cnat_db.h:113
u8 scale
Definition: cnat_db.h:378
u16 dst_port
Definition: cnat_db.h:257
#define PLATFORM_CNAT_SET_RX_VRF(ctx, rx_vrf, proto)
vlib_error_main_t error_main
Definition: main.h:124
static uword cnat_ipv4_tcp_inside_input_exc_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
#define TCP_FLAG_SYN
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define foreach_cnat_ipv4_tcp_inside_input_exc_error
u32 entry_expires
Definition: cnat_db.h:354
void calculate_window_scale(tcp_hdr_type *tcp_header, u8 *scale)
Definition: cnat_util.c:2135
vlib_node_registration_t cnat_ipv4_tcp_inside_input_exc_node
(constructor) VLIB_REGISTER_NODE (cnat_ipv4_tcp_inside_input_exc_node)
u64 * counters
Definition: error.h:78
#define ARRAY_LEN(x)
Definition: clib.h:59
cnat_db_key_t k
Definition: cnat_db.h:108
cnat_ipv4_tcp_inside_input_exc_main_t cnat_ipv4_tcp_inside_input_exc_main
static char * cnat_ipv4_tcp_inside_input_exc_error_strings[]
unsigned int u32
Definition: types.h:88
u32 entry_expires
Definition: cnat_db.h:213
#define vnet_buffer(b)
Definition: buffer.h:333
cnat_key_t out2in_key
Definition: cnat_db.h:198
#define CNAT_TCP
Definition: cnat_db.h:94
cnat_session_entry_t * cnat_session_db_lookup_entry(cnat_key_t *ko, u32 main_db_index)
Definition: cnat_db_v2.c:2612
u64 uword
Definition: types.h:112
u32 window
Definition: cnat_db.h:375
u8 scale
Definition: cnat_db.h:272
Definition: cnat_db.h:336
clib_error_t * cnat_ipv4_tcp_inside_input_exc_init(vlib_main_t *vm)
unsigned char u8
Definition: types.h:56
u32 dst_ipv4
Definition: cnat_db.h:254
#define CNAT_ICMP_DEST_UNREACHABLE
u32 in2out_forwarding_count
#define SESSION_DB_TYPE
Definition: cnat_db.h:627
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:58
#define PLATFORM_HANDLE_TTL_DECREMENT
#define V4_TCP_UPDATE_SESSION_DB_FLAG(sdb, tcp)
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
Definition: defs.h:46
cnat_key_t in2out_key
Definition: cnat_db.h:201
u32 in2out_pkts
Definition: cnat_db.h:210
cnat_icmp_msg_t gen_icmp_msg
Definition: cnat_db.h:529