FD.io VPP  v21.10.1-2-g0a485f517
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 <l2tp/l2tp.h>
24 #include <vnet/l2/l2_input.h>
25 
26 /* Statistics (not really errors) */
27 #define foreach_l2t_decap_error \
28 _(USER_TO_NETWORK, "L2TP user (ip6) to L2 network pkts") \
29 _(SESSION_ID_MISMATCH, "l2tpv3 local session id mismatches") \
30 _(COOKIE_MISMATCH, "l2tpv3 local cookie mismatches") \
31 _(NO_SESSION, "l2tpv3 session not found") \
32 _(ADMIN_DOWN, "l2tpv3 tunnel is down")
33 
34 static char *l2t_decap_error_strings[] = {
35 #define _(sym,string) string,
37 #undef _
38 };
39 
40 typedef enum
41 {
42 #define _(sym,str) L2T_DECAP_ERROR_##sym,
44 #undef _
47 
48 typedef enum
49 {
53  /* Pseudo next index */
56 
57 #define NSTAGES 3
58 
59 static inline void
61 {
63  /* l2tpv3 header is a long way away, need 2 cache lines */
65 }
66 
67 static inline void
69 {
70  l2t_main_t *lm = &l2t_main;
72  u32 session_index;
73  uword *p = 0;
74  l2tpv3_header_t *l2t;
75 
76  /* Not L2tpv3 (0x73, 0t115)? Use the normal path. */
77  if (PREDICT_FALSE (ip6->protocol != IP_PROTOCOL_L2TP))
78  {
79  vnet_buffer (b)->l2t.next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
80  return;
81  }
82 
83  /* Make up your minds, people... */
84  switch (lm->lookup_type)
85  {
87  p = hash_get_mem (lm->session_by_src_address, &ip6->src_address);
88  break;
90  p = hash_get_mem (lm->session_by_dst_address, &ip6->dst_address);
91  break;
93  l2t = (l2tpv3_header_t *) (ip6 + 1);
94  p = hash_get (lm->session_by_session_id, l2t->session_id);
95  break;
96  default:
97  ASSERT (0);
98  }
99 
100  if (PREDICT_FALSE (p == 0))
101  {
102  vnet_buffer (b)->l2t.next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
103  return;
104  }
105  else
106  {
107  session_index = p[0];
108  }
109 
110  /* Remember mapping index, prefetch the mini counter */
111  vnet_buffer (b)->l2t.next_index = L2T_DECAP_NEXT_L2_INPUT;
112  vnet_buffer (b)->l2t.session_index = session_index;
113 
114  /* $$$$$ prefetch counter */
115 }
116 
117 static inline u32
119 {
120  l2t_main_t *lm = &l2t_main;
122  vlib_node_t *n = vlib_get_node (vm, node->node_index);
123  u32 node_counter_base_index = n->error_heap_index;
125  l2tpv3_header_t *l2tp;
127  l2t_session_t *session = 0;
128  u32 session_index;
129  u32 next_index;
130  u8 l2tp_decap_local = (l2t_decap_local_node.index == n->index);
131 
132  /* Other-than-output pkt? We're done... */
133  if (vnet_buffer (b)->l2t.next_index != L2T_DECAP_NEXT_L2_INPUT)
134  {
135  next_index = vnet_buffer (b)->l2t.next_index;
136  goto done;
137  }
138 
139  em->counters[node_counter_base_index + L2T_DECAP_ERROR_USER_TO_NETWORK] +=
140  1;
141 
142  session_index = vnet_buffer (b)->l2t.session_index;
143 
144  counter_index =
145  session_index_to_counter_index (session_index,
147 
148  /* per-mapping byte stats include the ethernet header */
151  counter_index, 1 /* packet_increment */ ,
153  sizeof (ethernet_header_t));
154 
155  session = pool_elt_at_index (lm->sessions, session_index);
156 
157  l2tp = vlib_buffer_get_current (b) + sizeof (*ip6);
158 
159  if (PREDICT_FALSE (l2tp->session_id != session->local_session_id))
160  {
161  /* Key matched but session id does not. Assume packet is not for us. */
162  em->counters[node_counter_base_index +
163  L2T_DECAP_ERROR_SESSION_ID_MISMATCH] += 1;
165  goto done;
166  }
167 
168  if (PREDICT_FALSE (l2tp->cookie != session->local_cookie[0]))
169  {
170  if (l2tp->cookie != session->local_cookie[1])
171  {
172  /* Key and session ID matched, but cookie doesn't. Drop this packet. */
173  b->error = node->errors[L2T_DECAP_ERROR_COOKIE_MISMATCH];
175  goto done;
176  }
177  }
178 
179  vnet_buffer (b)->sw_if_index[VLIB_RX] = session->sw_if_index;
180 
181  if (PREDICT_FALSE (!(session->admin_up)))
182  {
183  b->error = node->errors[L2T_DECAP_ERROR_ADMIN_DOWN];
185  goto done;
186  }
187 
188  /* strip the ip6 and L2TP header */
189  vlib_buffer_advance (b, sizeof (*ip6) + session->l2tp_hdr_size);
190 
191  /* Required to make the l2 tag push / pop code work on l2 subifs */
193 
194  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
195  {
196  l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
197  t->is_user_to_network = 1;
198  t->our_address.as_u64[0] = ip6->dst_address.as_u64[0];
199  t->our_address.as_u64[1] = ip6->dst_address.as_u64[1];
200  t->client_address.as_u64[0] = ip6->src_address.as_u64[0];
201  t->client_address.as_u64[1] = ip6->src_address.as_u64[1];
202  t->session_index = session_index;
203  }
204 
206 
207 done:
209  {
210  /* Small behavioral change between l2tp-decap and l2tp-decap-local */
211  if (l2tp_decap_local)
212  {
213  b->error = node->errors[L2T_DECAP_ERROR_NO_SESSION];
215  }
216  else
217  {
218  /* Go to next node on the ip6 configuration chain */
219  if (PREDICT_TRUE (session != 0))
221  }
222  }
223 
224  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
225  {
226  l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
227  t->is_user_to_network = 1;
228  t->our_address.as_u64[0] = ip6->dst_address.as_u64[0];
229  t->our_address.as_u64[1] = ip6->dst_address.as_u64[1];
230  t->client_address.as_u64[0] = ip6->src_address.as_u64[0];
231  t->client_address.as_u64[1] = ip6->src_address.as_u64[1];
232  t->session_index = ~0;
233  }
234  return next_index;
235 }
236 
237 #include <vnet/pipeline.h>
238 
242 {
243  return dispatch_pipeline (vm, node, frame);
244 }
245 
246 /*
247  * l2tp-decap and l2tp-decap-local have very slightly different behavior.
248  * When a packet has no associated session l2tp-decap let it go to ip6 forward,
249  * while l2tp-decap-local drops it.
250  */
251 
252 /* *INDENT-OFF* */
254  .name = "l2tp-decap",
255  .vector_size = sizeof (u32),
256  .format_trace = format_l2t_trace,
258 
259  .n_errors = ARRAY_LEN(l2t_decap_error_strings),
260  .error_strings = l2t_decap_error_strings,
261 
262  .n_next_nodes = L2T_DECAP_N_NEXT,
263 
264  /* edit / add dispositions here */
265  .next_nodes = {
266  [L2T_DECAP_NEXT_L2_INPUT] = "l2-input",
267  [L2T_DECAP_NEXT_DROP] = "error-drop",
268  },
269 };
270 /* *INDENT-ON* */
271 
273 
274 /* *INDENT-OFF* */
276  .function = l2t_decap_node_fn,
277  .name = "l2tp-decap-local",
278  .vector_size = sizeof (u32),
279  .format_trace = format_l2t_trace,
281 
282  .n_errors = ARRAY_LEN(l2t_decap_error_strings),
283  .error_strings = l2t_decap_error_strings,
284 
285  .n_next_nodes = L2T_DECAP_N_NEXT,
286 
287  /* edit / add dispositions here */
288  .next_nodes = {
289  [L2T_DECAP_NEXT_L2_INPUT] = "l2-input",
290  [L2T_DECAP_NEXT_DROP] = "error-drop",
291  },
292 };
293 /* *INDENT-ON* */
294 
295 /*
296  * fd.io coding-style-patch-verification: ON
297  *
298  * Local Variables:
299  * eval: (c-set-style "gnu")
300  * End:
301  */
vlib_error_main_t
Definition: error.h:61
l2t_decap_node_fn
vlib_node_function_t l2t_decap_node_fn
l2t_session_t::sw_if_index
u32 sw_if_index
Definition: l2tp.h:39
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
vlib_prefetch_buffer_header
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:231
pipeline.h
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
l2t_main_t
Definition: l2tp.h:58
l2t_main_t::sessions
l2t_session_t * sessions
Definition: l2tp.h:61
L2T_LOOKUP_SESSION_ID
@ L2T_LOOKUP_SESSION_ID
Definition: l2tp.h:55
format_l2t_trace
u8 * format_l2t_trace(u8 *s, va_list *args)
Definition: l2tp.c:30
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
l2t_trace_t::is_user_to_network
int is_user_to_network
Definition: l2tp.h:90
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
stage1
static void stage1(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b)
Definition: decap.c:68
last_stage
static u32 last_stage(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b)
Definition: decap.c:118
vlib_main_t::error_main
vlib_error_main_t error_main
Definition: main.h:179
l2t_session_t::local_session_id
u32 local_session_id
Definition: l2tp.h:34
l2t_main_t::session_by_dst_address
uword * session_by_dst_address
Definition: l2tp.h:65
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
L2T_DECAP_NEXT_NO_INTERCEPT
@ L2T_DECAP_NEXT_NO_INTERCEPT
Definition: decap.c:54
counter_index
static u32 counter_index(vlib_main_t *vm, vlib_error_t e)
Definition: drop.c:67
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
l2t_DECAP_error_t
l2t_DECAP_error_t
Definition: decap.c:40
vlib_node_function_t
uword() vlib_node_function_t(struct vlib_main_t *vm, struct vlib_node_runtime_t *node, struct vlib_frame_t *frame)
Definition: node.h:54
vlib_frame_t
Definition: node.h:372
vlib_buffer_length_in_chain
static 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:433
ethernet.h
session_index_to_counter_index
static u32 session_index_to_counter_index(u32 session_index, u32 counter_id)
Definition: l2tp.h:108
l2t_session_t::admin_up
u8 admin_up
Definition: l2tp.h:48
L2T_DECAP_N_ERROR
@ L2T_DECAP_N_ERROR
Definition: decap.c:45
CLIB_PREFETCH
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:76
hash.h
vlib_buffer_advance
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:276
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
vlib_error_main_t::counters
u64 * counters
Definition: error.h:64
vlib_node_t::index
u32 index
Definition: node.h:269
error.h
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
l2t_session_t::l2tp_hdr_size
u8 l2tp_hdr_size
Definition: l2tp.h:44
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:441
l2t_main_t::session_by_session_id
uword * session_by_session_id
Definition: l2tp.h:66
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_get_thread_index
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:187
hash_get
#define hash_get(h, key)
Definition: hash.h:249
vnet_feature_next
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:322
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
l2t_main_t::lookup_type
ip6_to_l2_lookup_t lookup_type
Definition: l2tp.h:68
uword
u64 uword
Definition: types.h:112
ethernet_header_t
Definition: packet.h:52
l2t_trace_t::client_address
ip6_address_t client_address
Definition: l2tp.h:93
vlib_get_node
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:86
L2T_DECAP_NEXT_L2_INPUT
@ L2T_DECAP_NEXT_L2_INPUT
Definition: decap.c:51
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:58
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
l2t_decap_next_t
l2t_decap_next_t
Definition: decap.c:48
l2t_decap_error_strings
static char * l2t_decap_error_strings[]
Definition: decap.c:34
l2_input.h
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
l2t_main_t::session_by_src_address
uword * session_by_src_address
Definition: l2tp.h:64
vnet_update_l2_len
static u16 vnet_update_l2_len(vlib_buffer_t *b)
Definition: l2_input.h:300
l2t_session_t::local_cookie
u64 local_cookie[2]
Definition: l2tp.h:32
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
L2T_DECAP_N_NEXT
@ L2T_DECAP_N_NEXT
Definition: decap.c:52
ip.h
L2T_LOOKUP_DST_ADDRESS
@ L2T_LOOKUP_DST_ADDRESS
Definition: l2tp.h:54
u32
unsigned int u32
Definition: types.h:88
l2t_trace_t::our_address
ip6_address_t our_address
Definition: l2tp.h:92
ip6
vl_api_ip6_address_t ip6
Definition: one.api:424
L2T_LOOKUP_SRC_ADDRESS
@ L2T_LOOKUP_SRC_ADDRESS
Definition: l2tp.h:53
ip6_header_t
Definition: ip6_packet.h:294
stage0
static void stage0(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b)
Definition: decap.c:60
l2t_main
l2t_main_t l2t_main
Definition: l2tp.c:26
l2t_decap_node
vlib_node_registration_t l2t_decap_node
(constructor) VLIB_REGISTER_NODE (l2t_decap_node)
Definition: decap.c:253
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
foreach_l2t_decap_error
#define foreach_l2t_decap_error
Definition: decap.c:27
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
l2t_session_t
Definition: l2tp.h:25
vlib_node_t::error_heap_index
u32 error_heap_index
Definition: node.h:315
L2T_DECAP_NEXT_DROP
@ L2T_DECAP_NEXT_DROP
Definition: decap.c:50
l2t_trace_t
Definition: l2tp.h:88
l2t_decap_local_node
vlib_node_registration_t l2t_decap_local_node
(constructor) VLIB_REGISTER_NODE (l2t_decap_local_node)
Definition: decap.c:275
l2t_main_t::counter_main
vlib_combined_counter_main_t counter_main
Definition: l2tp.h:71
l2tp.h
SESSION_COUNTER_USER_TO_NETWORK
@ SESSION_COUNTER_USER_TO_NETWORK
Definition: l2tp.h:103
vnet.h
vlib_node_runtime_t
Definition: node.h:454
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
l2t_trace_t::session_index
u32 session_index
Definition: l2tp.h:91
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_increment_combined_counter
vlib_increment_combined_counter(ccm, ti, sw_if_index, n_buffers, n_bytes)
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169