blob: 787cc115a7e41658bfda833b2733ff57dd491341 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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>
Neale Ranns3b81a1e2018-09-06 09:50:26 -070024#include <vnet/l2/l2_input.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025
26/* Statistics (not really errors) */
Calvinee275a72016-08-10 11:01:41 -040027#define foreach_l2t_decap_error \
Ed Warnickecb9cada2015-12-08 15:45:58 -070028_(USER_TO_NETWORK, "L2TP user (ip6) to L2 network pkts") \
29_(SESSION_ID_MISMATCH, "l2tpv3 local session id mismatches") \
Pierre Pfistera026eb12016-06-20 14:52:22 +010030_(COOKIE_MISMATCH, "l2tpv3 local cookie mismatches") \
Pierre Pfister80ee2132016-06-22 12:54:48 +010031_(NO_SESSION, "l2tpv3 session not found") \
32_(ADMIN_DOWN, "l2tpv3 tunnel is down")
Ed Warnickecb9cada2015-12-08 15:45:58 -070033
Calvinee275a72016-08-10 11:01:41 -040034static char *l2t_decap_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070035#define _(sym,string) string,
36 foreach_l2t_decap_error
37#undef _
38};
39
Calvinee275a72016-08-10 11:01:41 -040040typedef enum
41{
Ed Warnickecb9cada2015-12-08 15:45:58 -070042#define _(sym,str) L2T_DECAP_ERROR_##sym,
Calvinee275a72016-08-10 11:01:41 -040043 foreach_l2t_decap_error
Ed Warnickecb9cada2015-12-08 15:45:58 -070044#undef _
45 L2T_DECAP_N_ERROR,
46} l2t_DECAP_error_t;
47
Calvinee275a72016-08-10 11:01:41 -040048typedef enum
49{
50 L2T_DECAP_NEXT_DROP,
51 L2T_DECAP_NEXT_L2_INPUT,
52 L2T_DECAP_N_NEXT,
53 /* Pseudo next index */
54 L2T_DECAP_NEXT_NO_INTERCEPT = L2T_DECAP_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070055} l2t_decap_next_t;
56
57#define NSTAGES 3
58
Calvinee275a72016-08-10 11:01:41 -040059static inline void
Dave Barach66446b92018-07-23 12:20:09 -040060stage0 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -070061{
Calvinee275a72016-08-10 11:01:41 -040062 vlib_prefetch_buffer_header (b, STORE);
63 /* l2tpv3 header is a long way away, need 2 cache lines */
64 CLIB_PREFETCH (b->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
Ed Warnickecb9cada2015-12-08 15:45:58 -070065}
66
Calvinee275a72016-08-10 11:01:41 -040067static inline void
Dave Barach66446b92018-07-23 12:20:09 -040068stage1 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
Calvinee275a72016-08-10 11:01:41 -040070 l2t_main_t *lm = &l2t_main;
71 ip6_header_t *ip6 = vlib_buffer_get_current (b);
72 u32 session_index;
73 uword *p = 0;
74 l2tpv3_header_t *l2t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
Calvinee275a72016-08-10 11:01:41 -040076 /* 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 }
82
Calvinee275a72016-08-10 11:01:41 -040083 /* Make up your minds, people... */
84 switch (lm->lookup_type)
85 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 case L2T_LOOKUP_SRC_ADDRESS:
Calvinee275a72016-08-10 11:01:41 -040087 p = hash_get_mem (lm->session_by_src_address, &ip6->src_address);
88 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 case L2T_LOOKUP_DST_ADDRESS:
Calvinee275a72016-08-10 11:01:41 -040090 p = hash_get_mem (lm->session_by_dst_address, &ip6->dst_address);
91 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 case L2T_LOOKUP_SESSION_ID:
Calvinee275a72016-08-10 11:01:41 -040093 l2t = (l2tpv3_header_t *) (ip6 + 1);
94 p = hash_get (lm->session_by_session_id, l2t->session_id);
95 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 default:
Calvinee275a72016-08-10 11:01:41 -040097 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 }
99
Calvinee275a72016-08-10 11:01:41 -0400100 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];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 }
109
Calvinee275a72016-08-10 11:01:41 -0400110 /* 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
Calvinee275a72016-08-10 11:01:41 -0400114 /* $$$$$ prefetch counter */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115}
116
Calvinee275a72016-08-10 11:01:41 -0400117static inline u32
Dave Barach66446b92018-07-23 12:20:09 -0400118last_stage (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119{
Calvinee275a72016-08-10 11:01:41 -0400120 l2t_main_t *lm = &l2t_main;
121 ip6_header_t *ip6 = vlib_buffer_get_current (b);
122 vlib_node_t *n = vlib_get_node (vm, node->node_index);
123 u32 node_counter_base_index = n->error_heap_index;
124 vlib_error_main_t *em = &vm->error_main;
125 l2tpv3_header_t *l2tp;
126 u32 counter_index;
Dave Barach75665d32016-11-17 11:36:59 -0500127 l2t_session_t *session = 0;
Calvinee275a72016-08-10 11:01:41 -0400128 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 goto done;
137 }
138
Calvinee275a72016-08-10 11:01:41 -0400139 em->counters[node_counter_base_index + L2T_DECAP_ERROR_USER_TO_NETWORK] +=
140 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
Calvinee275a72016-08-10 11:01:41 -0400142 session_index = vnet_buffer (b)->l2t.session_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143
Calvinee275a72016-08-10 11:01:41 -0400144 counter_index =
145 session_index_to_counter_index (session_index,
146 SESSION_COUNTER_USER_TO_NETWORK);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
Calvinee275a72016-08-10 11:01:41 -0400148 /* per-mapping byte stats include the ethernet header */
149 vlib_increment_combined_counter (&lm->counter_main,
Damjan Marion586afd72017-04-05 19:18:20 +0200150 vlib_get_thread_index (),
Calvinee275a72016-08-10 11:01:41 -0400151 counter_index, 1 /* packet_increment */ ,
152 vlib_buffer_length_in_chain (vm, b) +
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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
165 goto done;
166 }
167
Calvinee275a72016-08-10 11:01:41 -0400168 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];
174 next_index = L2T_DECAP_NEXT_DROP;
175 goto done;
176 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 }
178
Calvinee275a72016-08-10 11:01:41 -0400179 vnet_buffer (b)->sw_if_index[VLIB_RX] = session->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
Calvinee275a72016-08-10 11:01:41 -0400181 if (PREDICT_FALSE (!(session->admin_up)))
182 {
183 b->error = node->errors[L2T_DECAP_ERROR_ADMIN_DOWN];
184 next_index = L2T_DECAP_NEXT_DROP;
185 goto done;
Pierre Pfister80ee2132016-06-22 12:54:48 +0100186 }
187
Calvinee275a72016-08-10 11:01:41 -0400188 /* strip the ip6 and L2TP header */
189 vlib_buffer_advance (b, sizeof (*ip6) + session->l2tp_hdr_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Calvinee275a72016-08-10 11:01:41 -0400191 /* Required to make the l2 tag push / pop code work on l2 subifs */
192 vnet_update_l2_len (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
Calvinee275a72016-08-10 11:01:41 -0400194 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203 }
204
Calvinee275a72016-08-10 11:01:41 -0400205 return L2T_DECAP_NEXT_L2_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206
Calvinee275a72016-08-10 11:01:41 -0400207done:
208 if (next_index == L2T_DECAP_NEXT_NO_INTERCEPT)
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];
214 next_index = L2T_DECAP_NEXT_DROP;
215 }
216 else
217 {
218 /* Go to next node on the ip6 configuration chain */
Dave Barach7afe9e32016-11-22 15:23:41 -0500219 if (PREDICT_TRUE (session != 0))
Damjan Marion7d98a122018-07-19 20:42:08 +0200220 vnet_feature_next (&next_index, b);
Calvinee275a72016-08-10 11:01:41 -0400221 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 }
223
Calvinee275a72016-08-10 11:01:41 -0400224 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 }
Calvinee275a72016-08-10 11:01:41 -0400234 return next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235}
236
237#include <vnet/pipeline.h>
238
Filip Tehlarc3a0e8d2019-03-11 05:53:35 -0700239VLIB_NODE_FN (l2t_decap_node) (vlib_main_t * vm,
240 vlib_node_runtime_t * node,
241 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242{
Calvinee275a72016-08-10 11:01:41 -0400243 return dispatch_pipeline (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244}
245
Pierre Pfistera026eb12016-06-20 14:52:22 +0100246/*
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
Calvinee275a72016-08-10 11:01:41 -0400252/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253VLIB_REGISTER_NODE (l2t_decap_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254 .name = "l2tp-decap",
255 .vector_size = sizeof (u32),
256 .format_trace = format_l2t_trace,
257 .type = VLIB_NODE_TYPE_INTERNAL,
Calvinee275a72016-08-10 11:01:41 -0400258
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 .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};
Calvinee275a72016-08-10 11:01:41 -0400270/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
Filip Tehlarc3a0e8d2019-03-11 05:53:35 -0700272extern vlib_node_function_t l2t_decap_node_fn;
273
Calvinee275a72016-08-10 11:01:41 -0400274/* *INDENT-OFF* */
Pierre Pfistera026eb12016-06-20 14:52:22 +0100275VLIB_REGISTER_NODE (l2t_decap_local_node) = {
276 .function = l2t_decap_node_fn,
277 .name = "l2tp-decap-local",
278 .vector_size = sizeof (u32),
279 .format_trace = format_l2t_trace,
280 .type = VLIB_NODE_TYPE_INTERNAL,
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 = {
Calvinee275a72016-08-10 11:01:41 -0400289 [L2T_DECAP_NEXT_L2_INPUT] = "l2-input",
290 [L2T_DECAP_NEXT_DROP] = "error-drop",
Pierre Pfistera026eb12016-06-20 14:52:22 +0100291 },
292};
Calvinee275a72016-08-10 11:01:41 -0400293/* *INDENT-ON* */
Pierre Pfistera026eb12016-06-20 14:52:22 +0100294
Calvinee275a72016-08-10 11:01:41 -0400295/*
296 * fd.io coding-style-patch-verification: ON
297 *
298 * Local Variables:
299 * eval: (c-set-style "gnu")
300 * End:
301 */