FD.io VPP  v16.06
Vector Packet Processing
decap.c
Go to the documentation of this file.
1 /*
2  * decap.c : L2TPv3 tunnel decapsulation
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vppinfra/error.h>
19 #include <vppinfra/hash.h>
20 #include <vnet/vnet.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/l2tp/l2tp.h>
24 
25 /* Statistics (not really errors) */
26 #define foreach_l2t_decap_error \
27 _(USER_TO_NETWORK, "L2TP user (ip6) to L2 network pkts") \
28 _(SESSION_ID_MISMATCH, "l2tpv3 local session id mismatches") \
29 _(COOKIE_MISMATCH, "l2tpv3 local cookie mismatches")
30 
31 static char * l2t_decap_error_strings[] = {
32 #define _(sym,string) string,
34 #undef _
35 };
36 
37 typedef enum {
38 #define _(sym,str) L2T_DECAP_ERROR_##sym,
40 #undef _
43 
44 typedef enum {
48  /* Pseudo next index */
51 
52 #define NSTAGES 3
53 
54 static inline void stage0 (vlib_main_t * vm,
55  vlib_node_runtime_t * node,
56  u32 buffer_index)
57 {
58  vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
59  vlib_prefetch_buffer_header (b, STORE);
60  /* l2tpv3 header is a long way away, need 2 cache lines */
62 }
63 
64 static inline void stage1 (vlib_main_t * vm,
65  vlib_node_runtime_t * node,
66  u32 bi)
67 {
68  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
69  l2t_main_t *lm = &l2t_main;
71  u32 session_index;
72  uword *p = 0;
73  l2tpv3_header_t * l2t;
74 
75  /* Not L2tpv3 (0x73, 0t115)? Use the normal path. */
76  if (PREDICT_FALSE(ip6->protocol != IP_PROTOCOL_L2TP)) {
77  vnet_buffer(b)->l2t.next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
78  return;
79  }
80 
81  /* Make up your minds, people... */
82  switch (lm->lookup_type) {
85  break;
88  break;
90  l2t = (l2tpv3_header_t*)(ip6+1);
91  p = hash_get (lm->session_by_session_id, l2t->session_id);
92  break;
93  default:
94  ASSERT(0);
95  }
96 
97  if (PREDICT_FALSE(p == 0)) {
98  vnet_buffer(b)->l2t.next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
99  return;
100  } else {
101  session_index = p[0];
102  }
103 
104  /* Remember mapping index, prefetch the mini counter */
105  vnet_buffer(b)->l2t.next_index = L2T_DECAP_NEXT_L2_INPUT;
106  vnet_buffer(b)->l2t.session_index = session_index;
107 
108  /* $$$$$ prefetch counter */
109 }
110 
111 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
112  u32 bi)
113 {
114  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
115  l2t_main_t *lm = &l2t_main;
117  vlib_node_t *n = vlib_get_node (vm, l2t_decap_node.index);
118  u32 node_counter_base_index = n->error_heap_index;
119  vlib_error_main_t * em = &vm->error_main;
120  l2tpv3_header_t * l2tp;
122  l2t_session_t * session;
123  u32 session_index;
124  u32 next_index;
125 
126  /* Other-than-output pkt? We're done... */
127  if (vnet_buffer(b)->l2t.next_index != L2T_DECAP_NEXT_L2_INPUT) {
128  next_index = vnet_buffer(b)->l2t.next_index;
129  goto done;
130  }
131 
132  em->counters[node_counter_base_index + L2T_DECAP_ERROR_USER_TO_NETWORK] += 1;
133 
134  session_index = vnet_buffer(b)->l2t.session_index;
135 
136  counter_index =
137  session_index_to_counter_index (session_index,
139 
140  /* per-mapping byte stats include the ethernet header */
143  counter_index,
144  1 /* packet_increment */,
146  sizeof (ethernet_header_t));
147 
148  session = pool_elt_at_index (lm->sessions, session_index);
149 
150  l2tp = vlib_buffer_get_current (b) + sizeof (*ip6);
151 
152  if (PREDICT_FALSE(l2tp->session_id != session->local_session_id)) {
153  // Key matched but session id does not. Assume packet is not for us.
154  em->counters[node_counter_base_index + L2T_DECAP_ERROR_SESSION_ID_MISMATCH] += 1;
155  next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
156  goto done;
157  }
158 
159  if (PREDICT_FALSE (l2tp->cookie != session->local_cookie[0])) {
160  if (l2tp->cookie != session->local_cookie[1]) {
161  // Key and session ID matched, but cookie doesn't. Drop this packet.
162  b->error = node->errors[L2T_DECAP_ERROR_COOKIE_MISMATCH];
163  next_index = L2T_DECAP_NEXT_DROP;
164  goto done;
165  }
166  }
167 
168  vnet_buffer(b)->sw_if_index[VLIB_RX] = session->sw_if_index;
169 
170  /* strip the ip6 and L2TP header */
171  vlib_buffer_advance (b, sizeof (*ip6) + session->l2tp_hdr_size);
172 
173  /* Required to make the l2 tag push / pop code work on l2 subifs */
174  vnet_update_l2_len (b);
175 
177  l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
178  t->is_user_to_network = 1;
179  t->our_address.as_u64[0] =
180  ip6->dst_address.as_u64[0];
181  t->our_address.as_u64[1] =
182  ip6->dst_address.as_u64[1];
183  t->client_address.as_u64[0] =
184  ip6->src_address.as_u64[0];
185  t->client_address.as_u64[1] =
186  ip6->src_address.as_u64[1];
187  t->session_index = session_index;
188  }
189 
191 
192  done:
193  if (next_index == L2T_DECAP_NEXT_NO_INTERCEPT) {
194  // Go to next node on the ip6 configuration chain
195  ip6_main_t * im = &ip6_main;
196  ip_lookup_main_t * lm = &im->lookup_main;
198  ip6_l2tpv3_config_t * c0;
199 
201  &vnet_buffer (b)->ip.current_config_index,
202  &next_index,
203  sizeof (c0[0]));
204  }
205 
207  l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
208  t->is_user_to_network = 1;
209  t->our_address.as_u64[0] =
210  ip6->dst_address.as_u64[0];
211  t->our_address.as_u64[1] =
212  ip6->dst_address.as_u64[1];
213  t->client_address.as_u64[0] =
214  ip6->src_address.as_u64[0];
215  t->client_address.as_u64[1] =
216  ip6->src_address.as_u64[1];
217  t->session_index = ~0;
218  }
219  return next_index;
220 }
221 
222 #include <vnet/pipeline.h>
223 
225  vlib_node_runtime_t * node,
226  vlib_frame_t * frame)
227 {
228  return dispatch_pipeline (vm, node, frame);
229 }
230 
232  .function = l2t_decap_node_fn,
233  .name = "l2tp-decap",
234  .vector_size = sizeof (u32),
235  .format_trace = format_l2t_trace,
237 
238  .n_errors = ARRAY_LEN(l2t_decap_error_strings),
239  .error_strings = l2t_decap_error_strings,
240 
241  .n_next_nodes = L2T_DECAP_N_NEXT,
242 
243  /* edit / add dispositions here */
244  .next_nodes = {
245  [L2T_DECAP_NEXT_L2_INPUT] = "l2-input",
246  [L2T_DECAP_NEXT_DROP] = "error-drop",
247  },
248 };
249 
250 void l2tp_decap_init (void)
251 {
252  ip6_register_protocol (IP_PROTOCOL_L2TP, l2t_decap_node.index);
253 }
int is_user_to_network
Definition: l2tp.h:81
u64 local_cookie[2]
Definition: l2tp.h:33
u32 error_heap_index
Definition: node.h:244
ip6_address_t client_address
Definition: l2tp.h:84
void ip6_register_protocol(u32 protocol, u32 node_index)
Definition: ip6_forward.c:1908
always_inline vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Definition: node_funcs.h:46
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
static u32 last_stage(vlib_main_t *vm, vlib_node_runtime_t *node, u32 bi)
Definition: decap.c:111
l2t_DECAP_error_t
Definition: decap.c:37
u64 as_u64[2]
Definition: ip6_packet.h:50
uword * session_by_session_id
Definition: l2tp.h:60
ip_config_main_t rx_config_mains[VNET_N_CAST]
Definition: lookup.h:401
vlib_node_registration_t l2t_decap_node
(constructor) VLIB_REGISTER_NODE (l2t_decap_node)
Definition: decap.c:231
vlib_error_t * errors
Definition: node.h:378
ip6_address_t src_address
Definition: ip6_packet.h:293
static u32 session_index_to_counter_index(u32 session_index, u32 counter_id)
Definition: l2tp.h:96
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
l2t_main_t l2t_main
Definition: l2tp.c:26
static char * l2t_decap_error_strings[]
Definition: decap.c:31
always_inline void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Definition: counter.h:210
always_inline uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:112
static void stage1(vlib_main_t *vm, vlib_node_runtime_t *node, u32 bi)
Definition: decap.c:64
#define hash_get(h, key)
Definition: hash.h:231
#define pool_elt_at_index(p, i)
Definition: pool.h:346
vlib_error_main_t error_main
Definition: main.h:124
u32 local_session_id
Definition: l2tp.h:35
u8 * format_l2t_trace(u8 *s, va_list *args)
Definition: l2tp.c:29
uword os_get_cpu_number(void)
Definition: unix-misc.c:206
#define PREDICT_FALSE(x)
Definition: clib.h:97
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:129
ip6_to_l2_lookup_t lookup_type
Definition: l2tp.h:62
ip6_address_t our_address
Definition: l2tp.h:83
u64 * counters
Definition: error.h:73
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
static void stage0(vlib_main_t *vm, vlib_node_runtime_t *node, u32 buffer_index)
Definition: decap.c:54
#define ARRAY_LEN(x)
Definition: clib.h:59
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:300
ip6_main_t ip6_main
Definition: ip6_forward.c:2490
ip_lookup_main_t lookup_main
Definition: ip6.h:135
#define foreach_l2t_decap_error
Definition: decap.c:26
static uword l2t_decap_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: decap.c:224
uword * session_by_dst_address
Definition: l2tp.h:59
l2t_decap_next_t
Definition: decap.c:44
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:91
vlib_combined_counter_main_t counter_main
Definition: l2tp.h:65
u64 uword
Definition: types.h:112
void l2tp_decap_init(void)
Definition: decap.c:250
u8 l2tp_hdr_size
Definition: l2tp.h:42
always_inline void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:197
static void vnet_update_l2_len(vlib_buffer_t *b)
Definition: l2_input.h:226
l2t_session_t * sessions
Definition: l2tp.h:55
always_inline void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
#define hash_get_mem(h, key)
Definition: hash.h:251
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:162
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
u8 data[0]
Packet data.
Definition: buffer.h:150
uword * session_by_src_address
Definition: l2tp.h:58
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
always_inline void * vnet_get_config_data(vnet_config_main_t *cm, u32 *config_index, u32 *next_index, u32 n_data_bytes)
Definition: config.h:115
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
u32 sw_if_index
Definition: l2tp.h:40
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
vnet_config_main_t config_main
Definition: lookup.h:343
always_inline u32 counter_index(vlib_main_t *vm, vlib_error_t e)
Definition: defs.h:45
u32 session_index
Definition: l2tp.h:82
ip6_address_t dst_address
Definition: ip6_packet.h:293