FD.io VPP  v17.01.1-3-gc6833f8
Vector Packet Processing
cnat_ipv4_tcp_outside_input.c
Go to the documentation of this file.
1 /*
2  *---------------------------------------------------------------------------
3  * cnat_ipv4_tcp_outside_input.c - cnat_v4_tcp_out2in 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_db.h"
27 #include "tcp_header_definitions.h"
28 #include "cnat_config.h"
29 #include "cnat_global.h"
30 #include "cnat_ipv4_udp.h"
31 #include "cnat_v4_functions.h"
32 
33 
34 #define foreach_cnat_ipv4_tcp_outside_input_error \
35 _(CNAT_V4_TCP_O2I_R_PKT, "v4 tcp o2i pkt received") \
36 _(CNAT_V4_TCP_O2I_T_PKT, "v4 tcp o2i pkt natted & transmitted") \
37 _(CNAT_V4_TCP_O2I_LOOKUP_FAILED, "v4 tcp o2i lookup failed") \
38 _(CNAT_V4_TCP_O2I_TTL_GEN, "v4 tcp o2i generated TTL Expiry ICMP packet") \
39 _(CNAT_V4_TCP_O2I_TTL_DROP, "v4 tcp o2i drop due to failure in creating TTL expiry ICMP msg") \
40 _(CNAT_V4_TCP_O2I_PTB_GEN, "v4 tcp o2i PTB ICMP pkt generation") \
41 _(CNAT_V4_UDP_O2I_PTB_DROP, "v4 tcp o2i drop due to failure in creating PTB ICMP pkt") \
42 _(CNAT_V4_TCP_O2I_SESSION_DROP, "v4 tcp o2i drop due to failure in creating session db") \
43 _(CNAT_V4_TCP_O2I_SEQ_MISMATCH_DROP, "v4 tcp o2i drop due to TCP sequence mismatch") \
44 _(CNAT_V4_TCP_O2I_FILTER_DROP, "v4 tcp o2i drop due to endpoint filtering") \
45 _(CNAT_V4_TCP_O2I_NON_SYN_RST_DROP, "v4 tcp o2i drop due no syn/rst flag") \
46 _(CNAT_V4_TCP_O2I_FIRST_FRAG_DROP, "v4 tcp o2i first fragment drop") \
47 _(CNAT_V4_TCP_O2I_SUB_FRAG_NO_DB_DROP, "v4 tcp o2i subsequest frag no DB drop")
48 
49 typedef enum {
50 #define _(sym,str) sym,
52 #undef _
55 
57 #define _(sym,string) string,
59 #undef _
60 };
61 
62 typedef struct {
64  /* $$$$ add data here */
65 
66  /* convenience variables */
70 
71 typedef enum {
72  //CNAT_V4_TCP_O2I_E,
77 
80 
81 #define NSTAGES 6
82 
83 /*
84  * Use the generic buffer metadata + first line of packet data prefetch
85  * stage function from <api/pipeline.h>. This is usually a Good Idea.
86  */
87 #define stage0 generic_stage0
88 
89 
90 static inline void
91 stage1(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
92 {
93  u64 a, b, c;
94  u32 bucket;
95  u8 *prefetch_target;
96 
97 
98  vlib_buffer_t * b0 = vlib_get_buffer (vm, buffer_index);
100  u8 ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
101  tcp_hdr_type *tcp = (tcp_hdr_type *)((u8*)ip + ipv4_hdr_len);
102 
103  u64 tmp = 0;
104  tmp = vnet_buffer(b0)->vcgn_uii.key.k.ipv4 =
105  clib_net_to_host_u32(ip->dest_addr);
106  vnet_buffer(b0)->vcgn_uii.key.k.port =
107  clib_net_to_host_u16 (tcp->dest_port);
108 
109  tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.port) << 32;
110 
111  PLATFORM_CNAT_SET_RX_VRF(vnet_buffer(b0)->sw_if_index[VLIB_RX],
112  vnet_buffer(b0)->vcgn_uii.key.k.vrf,
113  CNAT_TCP)
114  tmp |= ((u64)vnet_buffer(b0)->vcgn_uii.key.k.vrf) << 48;
115 
117 
118  prefetch_target = (u8 *)(&cnat_out2in_hash[bucket]);
119  vnet_buffer(b0)->vcgn_uii.bucket = bucket;
120 
121  /* Prefetch the hash bucket */
122  CLIB_PREFETCH(prefetch_target, CLIB_CACHE_LINE_BYTES, LOAD);
123 }
124 
125 static inline void
126 stage2(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
127 { /* nothing */ }
128 
129 #define SPP_LOG2_CACHE_LINE_BYTES 6
130 #define SPP_CACHE_LINE_BYTES (1 << SPP_LOG2_CACHE_LINE_BYTES)
131 
132 static inline void
133 stage3(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
134 {
135  vlib_buffer_t * b0 = vlib_get_buffer(vm, buffer_index);
136  uword prefetch_target0, prefetch_target1;
137  u32 bucket = vnet_buffer(b0)->vcgn_uii.bucket;
138 
139  /* read the hash bucket */
140  u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket
141  = cnat_out2in_hash[bucket].next;
142 
143  if (PREDICT_TRUE(db_index != EMPTY)) {
144  /*
145  * Prefetch database keys. We save space by not cache-line
146  * aligning the DB entries. We don't want to waste LSU
147  * bandwidth prefetching stuff we won't need.
148  */
149  prefetch_target0 = (uword)(cnat_main_db + db_index);
150  CLIB_PREFETCH((void*)prefetch_target0, CLIB_CACHE_LINE_BYTES, STORE);
151  /* Just beyond DB key #2 */
152  prefetch_target1 = prefetch_target0 +
154  /* If the targets are in different lines, do the second prefetch */
155  if (PREDICT_FALSE((prefetch_target0 & ~(SPP_CACHE_LINE_BYTES-1)) !=
156  (prefetch_target1 & ~(SPP_CACHE_LINE_BYTES-1)))) {
157  CLIB_PREFETCH((void *)prefetch_target1, CLIB_CACHE_LINE_BYTES, STORE);
158  }
159  }
160 }
161 
162 static inline void
163 stage4(vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
164 {
166  vlib_buffer_t * b0 = vlib_get_buffer(vm, buffer_index);
167  u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket;
168 
169  /*
170  * Note: if the search already failed (empty bucket),
171  * the answer is already in the pipeline context structure
172  */
173  if (PREDICT_TRUE(db_index != EMPTY)) {
174 
175  /*
176  * Note: hash collisions suck. We can't easily prefetch around them.
177  * The first trip around the track will be fast. After that, maybe
178  * not so much...
179  */
180  do {
181  db = cnat_main_db + db_index;
182  if (PREDICT_TRUE(db->out2in_key.key64 ==
183  vnet_buffer(b0)->vcgn_uii.key.key64)) {
184  break;
185  }
186  db_index = db->out2in_hash.next;
187  } while (db_index != EMPTY);
188 
189  /* Stick the answer back into the pipeline context structure */
190  vnet_buffer(b0)->vcgn_uii.bucket = db_index;
191  }
192 }
193 
194 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
195  u32 bi)
196 {
197  vlib_buffer_t *b0 = vlib_get_buffer (vm, bi);
198  u32 db_index = vnet_buffer(b0)->vcgn_uii.bucket;
199  spp_ctx_t *ctx = (spp_ctx_t *) &vnet_buffer(b0)->vcgn_uii;
200  int disposition = CNAT_V4_TCP_O2I_T;
201  int counter = CNAT_V4_TCP_O2I_T_PKT;
202 
204  u8 ipv4_hdr_len = (ip->version_hdr_len_words & 0xf) << 2;
205  tcp_hdr_type *tcp = (tcp_hdr_type *)((u8*)ip + ipv4_hdr_len);
207  u32 node_counter_base_index = n->error_heap_index;
208  vlib_error_main_t * em = &vm->error_main;
209  cnat_session_entry_t *session_db = NULL;
211  cnat_key_t dest_info;
212 
213  INCREMENT_NODE_COUNTER(CNAT_V4_TCP_O2I_R_PKT);
214 
215  if (PREDICT_FALSE(db_index == EMPTY)) {
217  counter = CNAT_V4_TCP_O2I_LOOKUP_FAILED;
218  disposition = CNAT_V4_TCP_O2I_D;
219  } else {
221  if (PREDICT_FALSE(ip->ttl <= 1)) {
222  /* Try to generate ICMP error msg, as TTL is <= 1 */
224  ip, ctx->ru.rx.uidb_index)) {
225  /* Generated ICMP */
226  disposition = CNAT_V4_TCP_O2I_T_PKT; //CNAT_REWRITE_OUTPUT;
227  counter = CNAT_V4_TCP_O2I_TTL_GEN;
228  } else {
229  /* Could not generated ICMP - drop the packet */
230  disposition = CNAT_V4_TCP_O2I_D;
231  counter = CNAT_V4_TCP_O2I_TTL_DROP;
232  }
233  goto drop_pkt;
234  }
235  }
236  db = cnat_main_db + db_index;
237 #if 0
238  window = db->diff_window;
239  stored_seq_no = db->proto_data.tcp_seq_chk.seq_no;
240  stored_ack_no = db->proto_data.tcp_seq_chk.ack_no;
241  vrf_map_p = cnat_map_by_vrf + db->vrfmap_index;
242  vrf_index = (db->in2out_key.k.vrf & CNAT_VRF_MASK);
243 #endif
244  /* For Out2In packet, the dest info is src address and port */
245  dest_info.k.port = clib_net_to_host_u16(tcp->src_port);
246  dest_info.k.ipv4 = clib_net_to_host_u32(ip->src_addr);
247 
249 
250  /* No DBL support, so just update the destn and proceed */
251  db->dst_ipv4 = dest_info.k.ipv4;
252  db->dst_port = dest_info.k.port;
253  goto update_pkt;
254  }
255 
256 
257  if(PREDICT_FALSE(db->dst_ipv4 != dest_info.k.ipv4 ||
258  db->dst_port != dest_info.k.port)) {
259 
260  if(PREDICT_TRUE(db->nsessions == 0)) {
261  /* Should be a static entry
262  * Note this session as the first session and log
263  */
264  cnat_add_dest_n_log(db, &dest_info);
265  //goto packet_upd;
266  } else if(PREDICT_FALSE(db->nsessions == 1)) {
267  /* Destn is not same as in main db. Multiple session
268  * scenario
269  */
270  dest_info.k.vrf = db->in2out_key.k.vrf;
271  session_db = cnat_handle_1to2_session(db, &dest_info);
272  if(PREDICT_FALSE(session_db == NULL)) {
273  disposition = CNAT_V4_TCP_O2I_D;
274  counter = CNAT_V4_TCP_O2I_SESSION_DROP;
275  goto drop_pkt;
276  }
277  } else { /* There are already multiple destinations */
278  dest_info.k.vrf = db->in2out_key.k.vrf;
279  /* If session already exists,
280  * cnat_create_session_db_entry will return the existing db
281  * else create a new db
282  * If could not create, return NULL
283  */
284  session_db = cnat_create_session_db_entry(&dest_info, db, TRUE);
285  if(PREDICT_FALSE(session_db == NULL)) {
286  disposition = CNAT_V4_TCP_O2I_D;
287  counter = CNAT_V4_TCP_O2I_SESSION_DROP;
288  goto drop_pkt;
289  }
290  }
291  /* useful for ALG only */
292  #if 0
293  if(PREDICT_TRUE(session_db)) {
294  stored_seq_no = session_db->tcp_seq_num;
295  stored_ack_no = session_db->ack_no;
296  window = session_db->window;
297  }
298  #endif
299  }
300 
301 
302 update_pkt:
303 
304  counter = CNAT_V4_TCP_O2I_T_PKT;
305 
307  /*
308  * Decrement TTL and update IPv4 checksum
309  */
311  }
312 
313  /* update ip checksum, newchecksum = ~(~oldchecksum + ~old + new) */
315  &(ip->dest_addr),
316  &(tcp->dest_port),
317  db->in2out_key.k.ipv4,
318  db->in2out_key.k.port);
319 
320  /* CNAT_PPTP_ALG_SUPPORT */
321  db->out2in_pkts++;
322 
324 
326 
327 
328  if(PREDICT_FALSE(session_db != NULL)) {
329  V4_TCP_UPDATE_SESSION_DB_FLAG(session_db, tcp);
330  CNAT_DB_TIMEOUT_RST(session_db);
331  } else {
334  }
335 
336  }
337 
338 drop_pkt:
339  em->counters[node_counter_base_index + counter] += 1;
340  return disposition;
341 }
342 
343 #include <vnet/pipeline.h>
344 
346  vlib_node_runtime_t * node,
347  vlib_frame_t * frame)
348 {
349  return dispatch_pipeline (vm, node, frame);
350 }
351 
352 
355  .name = "vcgn-v4-tcp-o2i",
356  .vector_size = sizeof (u32),
358 
361 
362  .n_next_nodes = CNAT_V4_TCP_O2I_NEXT,
363 
364  /* edit / add dispositions here */
365  .next_nodes = {
366  //[CNAT_V4_TCP_O2I_E] = "vcgn-v4-tcp-o2i-e",
367  [CNAT_V4_TCP_O2I_T] = "ip4-input",
368  [CNAT_V4_TCP_O2I_D] = "error-drop",
369  },
370 };
371 
373 {
375 
376  mp->vlib_main = vm;
377  mp->vnet_main = vnet_get_main();
378 
379  return 0;
380 }
381 
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
static u32 last_stage(vlib_main_t *vm, vlib_node_runtime_t *node, u32 bi)
nat44_dslite_global_stats_t nat44_dslite_global_stats[2]
Definition: cnat_db_v2.c:211
cnat_session_entry_t * cnat_create_session_db_entry(cnat_key_t *ko, cnat_main_db_entry_t *bdb, u8 log)
Definition: cnat_db_v2.c:2643
a
Definition: bitmap.h:516
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
#define PREDICT_TRUE(x)
Definition: clib.h:98
void ipv4_decr_ttl_n_calc_csum(ipv4_header *ipv4)
#define NULL
Definition: clib.h:55
u32 tcp_seq_num
Definition: cnat_db.h:369
u64 key64
Definition: cnat_db.h:109
#define V4_TCP_UPDATE_SESSION_FLAG(db, tcp)
u32 ack_no
Definition: cnat_db.h:372
Definition: cnat_db.h:153
struct _vlib_node_registration vlib_node_registration_t
#define STRUCT_OFFSET_OF(t, f)
Definition: clib.h:62
cnat_session_entry_t * cnat_handle_1to2_session(cnat_main_db_entry_t *mdb, cnat_key_t *dest_info)
Definition: cnat_db_v2.c:1027
#define CNAT_VRF_MASK
Definition: cnat_db.h:96
index_slist_t out2in_hash
Definition: cnat_db.h:155
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
clib_error_t * cnat_ipv4_tcp_outside_input_init(vlib_main_t *vm)
#define CNAT_DB_TIMEOUT_RST(db)
Definition: cnat_db.h:561
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:194
union cnat_main_db_entry_t::@266 proto_data
static void cnat_v4_recalculate_tcp_checksum(ipv4_header *ip, tcp_hdr_type *tcp, u32 *ip_addr_ptr, u16 *tcp_port_addr_ptr, u32 new_ip, u16 new_port)
unsigned long u64
Definition: types.h:89
static void stage2(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
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
#define PREDICT_FALSE(x)
Definition: clib.h:97
cnat_ipv4_tcp_outside_input_t
int icmpv4_generate_with_throttling(spp_ctx_t *ctx, ipv4_header *ipv4, u16 rx_uidb_index)
struct cnat_main_db_entry_t::@266::tcp_seq_chk_t tcp_seq_chk
u64 * counters
Definition: error.h:78
cnat_ipv4_tcp_outside_input_next_t
svmdb_client_t * c
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
#define SPP_CACHE_LINE_BYTES
static void stage3(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
#define foreach_cnat_ipv4_tcp_outside_input_error
#define ARRAY_LEN(x)
Definition: clib.h:59
cnat_db_key_t k
Definition: cnat_db.h:108
static void stage4(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
void cnat_add_dest_n_log(cnat_main_db_entry_t *mdb, cnat_key_t *dest_info)
Definition: cnat_db_v2.c:1083
unsigned int u32
Definition: types.h:88
static uword cnat_ipv4_tcp_outside_input_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
#define vnet_buffer(b)
Definition: buffer.h:361
#define TRUE
Definition: cnat_db.h:78
#define PLATFORM_DBL_SUPPORT
#define INCREMENT_NODE_COUNTER(c)
cnat_key_t out2in_key
Definition: cnat_db.h:198
index_slist_t * cnat_out2in_hash
Definition: cnat_db_v2.c:195
#define CNAT_TCP
Definition: cnat_db.h:94
static void stage1(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
u64 uword
Definition: types.h:112
u16 nsessions
Definition: cnat_db.h:266
u32 window
Definition: cnat_db.h:375
Definition: cnat_db.h:336
cnat_vrfmap_t * cnat_map_by_vrf
Definition: cnat_db_v2.c:218
#define CNAT_MAIN_HASH_MASK
Definition: cnat_db.h:56
unsigned char u8
Definition: types.h:56
u32 dst_ipv4
Definition: cnat_db.h:254
u16 vrfmap_index
Definition: cnat_db.h:192
struct _spp_ctx spp_ctx_t
u32 out2in_pkts
Definition: cnat_db.h:207
cnat_ipv4_tcp_outside_input_main_t cnat_ipv4_tcp_outside_input_main
#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 EMPTY
Definition: index_list.h:24
#define PLATFORM_HANDLE_TTL_DECREMENT
#define CNAT_V4_GET_HASH(key64, hash, mask)
Definition: cnat_db.h:536
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
#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:57
vlib_node_registration_t cnat_ipv4_tcp_outside_input_node
(constructor) VLIB_REGISTER_NODE (cnat_ipv4_tcp_outside_input_node)
static char * cnat_ipv4_tcp_outside_input_error_strings[]
Definition: defs.h:46
cnat_key_t in2out_key
Definition: cnat_db.h:201
nat44_dslite_common_stats_t nat44_dslite_common_stats[255]
Definition: cnat_db_v2.c:210