blob: 304862b5ee6e61ff228575cb3fede864241fb5e0 [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>
24
25/* Statistics (not really errors) */
Calvinee275a72016-08-10 11:01:41 -040026#define foreach_l2t_decap_error \
Ed Warnickecb9cada2015-12-08 15:45:58 -070027_(USER_TO_NETWORK, "L2TP user (ip6) to L2 network pkts") \
28_(SESSION_ID_MISMATCH, "l2tpv3 local session id mismatches") \
Pierre Pfistera026eb12016-06-20 14:52:22 +010029_(COOKIE_MISMATCH, "l2tpv3 local cookie mismatches") \
Pierre Pfister80ee2132016-06-22 12:54:48 +010030_(NO_SESSION, "l2tpv3 session not found") \
31_(ADMIN_DOWN, "l2tpv3 tunnel is down")
Ed Warnickecb9cada2015-12-08 15:45:58 -070032
Calvinee275a72016-08-10 11:01:41 -040033static char *l2t_decap_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070034#define _(sym,string) string,
35 foreach_l2t_decap_error
36#undef _
37};
38
Calvinee275a72016-08-10 11:01:41 -040039typedef enum
40{
Ed Warnickecb9cada2015-12-08 15:45:58 -070041#define _(sym,str) L2T_DECAP_ERROR_##sym,
Calvinee275a72016-08-10 11:01:41 -040042 foreach_l2t_decap_error
Ed Warnickecb9cada2015-12-08 15:45:58 -070043#undef _
44 L2T_DECAP_N_ERROR,
45} l2t_DECAP_error_t;
46
Calvinee275a72016-08-10 11:01:41 -040047typedef enum
48{
49 L2T_DECAP_NEXT_DROP,
50 L2T_DECAP_NEXT_L2_INPUT,
51 L2T_DECAP_N_NEXT,
52 /* Pseudo next index */
53 L2T_DECAP_NEXT_NO_INTERCEPT = L2T_DECAP_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070054} l2t_decap_next_t;
55
56#define NSTAGES 3
57
Calvinee275a72016-08-10 11:01:41 -040058static inline void
59stage0 (vlib_main_t * vm, vlib_node_runtime_t * node, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070060{
Calvinee275a72016-08-10 11:01:41 -040061 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
62 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
68stage1 (vlib_main_t * vm, vlib_node_runtime_t * node, u32 bi)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
Calvinee275a72016-08-10 11:01:41 -040070 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
71 l2t_main_t *lm = &l2t_main;
72 ip6_header_t *ip6 = vlib_buffer_get_current (b);
73 u32 session_index;
74 uword *p = 0;
75 l2tpv3_header_t *l2t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
Calvinee275a72016-08-10 11:01:41 -040077 /* Not L2tpv3 (0x73, 0t115)? Use the normal path. */
78 if (PREDICT_FALSE (ip6->protocol != IP_PROTOCOL_L2TP))
79 {
80 vnet_buffer (b)->l2t.next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
81 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 }
83
Calvinee275a72016-08-10 11:01:41 -040084 /* Make up your minds, people... */
85 switch (lm->lookup_type)
86 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 case L2T_LOOKUP_SRC_ADDRESS:
Calvinee275a72016-08-10 11:01:41 -040088 p = hash_get_mem (lm->session_by_src_address, &ip6->src_address);
89 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 case L2T_LOOKUP_DST_ADDRESS:
Calvinee275a72016-08-10 11:01:41 -040091 p = hash_get_mem (lm->session_by_dst_address, &ip6->dst_address);
92 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 case L2T_LOOKUP_SESSION_ID:
Calvinee275a72016-08-10 11:01:41 -040094 l2t = (l2tpv3_header_t *) (ip6 + 1);
95 p = hash_get (lm->session_by_session_id, l2t->session_id);
96 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 default:
Calvinee275a72016-08-10 11:01:41 -040098 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 }
100
Calvinee275a72016-08-10 11:01:41 -0400101 if (PREDICT_FALSE (p == 0))
102 {
103 vnet_buffer (b)->l2t.next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
104 return;
105 }
106 else
107 {
108 session_index = p[0];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 }
110
Calvinee275a72016-08-10 11:01:41 -0400111 /* Remember mapping index, prefetch the mini counter */
112 vnet_buffer (b)->l2t.next_index = L2T_DECAP_NEXT_L2_INPUT;
113 vnet_buffer (b)->l2t.session_index = session_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114
Calvinee275a72016-08-10 11:01:41 -0400115 /* $$$$$ prefetch counter */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116}
117
Calvinee275a72016-08-10 11:01:41 -0400118static inline u32
119last_stage (vlib_main_t * vm, vlib_node_runtime_t * node, u32 bi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120{
Calvinee275a72016-08-10 11:01:41 -0400121 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
122 l2t_main_t *lm = &l2t_main;
123 ip6_header_t *ip6 = vlib_buffer_get_current (b);
124 vlib_node_t *n = vlib_get_node (vm, node->node_index);
125 u32 node_counter_base_index = n->error_heap_index;
126 vlib_error_main_t *em = &vm->error_main;
127 l2tpv3_header_t *l2tp;
128 u32 counter_index;
Dave Barach75665d32016-11-17 11:36:59 -0500129 l2t_session_t *session = 0;
Calvinee275a72016-08-10 11:01:41 -0400130 u32 session_index;
131 u32 next_index;
132 u8 l2tp_decap_local = (l2t_decap_local_node.index == n->index);
133
134 /* Other-than-output pkt? We're done... */
135 if (vnet_buffer (b)->l2t.next_index != L2T_DECAP_NEXT_L2_INPUT)
136 {
137 next_index = vnet_buffer (b)->l2t.next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 goto done;
139 }
140
Calvinee275a72016-08-10 11:01:41 -0400141 em->counters[node_counter_base_index + L2T_DECAP_ERROR_USER_TO_NETWORK] +=
142 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143
Calvinee275a72016-08-10 11:01:41 -0400144 session_index = vnet_buffer (b)->l2t.session_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145
Calvinee275a72016-08-10 11:01:41 -0400146 counter_index =
147 session_index_to_counter_index (session_index,
148 SESSION_COUNTER_USER_TO_NETWORK);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Calvinee275a72016-08-10 11:01:41 -0400150 /* per-mapping byte stats include the ethernet header */
151 vlib_increment_combined_counter (&lm->counter_main,
Damjan Marion586afd72017-04-05 19:18:20 +0200152 vlib_get_thread_index (),
Calvinee275a72016-08-10 11:01:41 -0400153 counter_index, 1 /* packet_increment */ ,
154 vlib_buffer_length_in_chain (vm, b) +
155 sizeof (ethernet_header_t));
156
157 session = pool_elt_at_index (lm->sessions, session_index);
158
159 l2tp = vlib_buffer_get_current (b) + sizeof (*ip6);
160
161 if (PREDICT_FALSE (l2tp->session_id != session->local_session_id))
162 {
163 /* Key matched but session id does not. Assume packet is not for us. */
164 em->counters[node_counter_base_index +
165 L2T_DECAP_ERROR_SESSION_ID_MISMATCH] += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
167 goto done;
168 }
169
Calvinee275a72016-08-10 11:01:41 -0400170 if (PREDICT_FALSE (l2tp->cookie != session->local_cookie[0]))
171 {
172 if (l2tp->cookie != session->local_cookie[1])
173 {
174 /* Key and session ID matched, but cookie doesn't. Drop this packet. */
175 b->error = node->errors[L2T_DECAP_ERROR_COOKIE_MISMATCH];
176 next_index = L2T_DECAP_NEXT_DROP;
177 goto done;
178 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 }
180
Calvinee275a72016-08-10 11:01:41 -0400181 vnet_buffer (b)->sw_if_index[VLIB_RX] = session->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Calvinee275a72016-08-10 11:01:41 -0400183 if (PREDICT_FALSE (!(session->admin_up)))
184 {
185 b->error = node->errors[L2T_DECAP_ERROR_ADMIN_DOWN];
186 next_index = L2T_DECAP_NEXT_DROP;
187 goto done;
Pierre Pfister80ee2132016-06-22 12:54:48 +0100188 }
189
Calvinee275a72016-08-10 11:01:41 -0400190 /* strip the ip6 and L2TP header */
191 vlib_buffer_advance (b, sizeof (*ip6) + session->l2tp_hdr_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
Calvinee275a72016-08-10 11:01:41 -0400193 /* Required to make the l2 tag push / pop code work on l2 subifs */
194 vnet_update_l2_len (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195
Calvinee275a72016-08-10 11:01:41 -0400196 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
197 {
198 l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
199 t->is_user_to_network = 1;
200 t->our_address.as_u64[0] = ip6->dst_address.as_u64[0];
201 t->our_address.as_u64[1] = ip6->dst_address.as_u64[1];
202 t->client_address.as_u64[0] = ip6->src_address.as_u64[0];
203 t->client_address.as_u64[1] = ip6->src_address.as_u64[1];
204 t->session_index = session_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205 }
206
Calvinee275a72016-08-10 11:01:41 -0400207 return L2T_DECAP_NEXT_L2_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Calvinee275a72016-08-10 11:01:41 -0400209done:
210 if (next_index == L2T_DECAP_NEXT_NO_INTERCEPT)
211 {
212 /* Small behavioral change between l2tp-decap and l2tp-decap-local */
213 if (l2tp_decap_local)
214 {
215 b->error = node->errors[L2T_DECAP_ERROR_NO_SESSION];
216 next_index = L2T_DECAP_NEXT_DROP;
217 }
218 else
219 {
220 /* Go to next node on the ip6 configuration chain */
Dave Barach7afe9e32016-11-22 15:23:41 -0500221 if (PREDICT_TRUE (session != 0))
Damjan Marion7d98a122018-07-19 20:42:08 +0200222 vnet_feature_next (&next_index, b);
Calvinee275a72016-08-10 11:01:41 -0400223 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224 }
225
Calvinee275a72016-08-10 11:01:41 -0400226 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
227 {
228 l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
229 t->is_user_to_network = 1;
230 t->our_address.as_u64[0] = ip6->dst_address.as_u64[0];
231 t->our_address.as_u64[1] = ip6->dst_address.as_u64[1];
232 t->client_address.as_u64[0] = ip6->src_address.as_u64[0];
233 t->client_address.as_u64[1] = ip6->src_address.as_u64[1];
234 t->session_index = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 }
Calvinee275a72016-08-10 11:01:41 -0400236 return next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237}
238
239#include <vnet/pipeline.h>
240
Calvinee275a72016-08-10 11:01:41 -0400241static uword
242l2t_decap_node_fn (vlib_main_t * vm,
243 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244{
Calvinee275a72016-08-10 11:01:41 -0400245 return dispatch_pipeline (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246}
247
Pierre Pfistera026eb12016-06-20 14:52:22 +0100248/*
249 * l2tp-decap and l2tp-decap-local have very slightly different behavior.
250 * When a packet has no associated session l2tp-decap let it go to ip6 forward,
251 * while l2tp-decap-local drops it.
252 */
253
Calvinee275a72016-08-10 11:01:41 -0400254/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255VLIB_REGISTER_NODE (l2t_decap_node) = {
256 .function = l2t_decap_node_fn,
257 .name = "l2tp-decap",
258 .vector_size = sizeof (u32),
259 .format_trace = format_l2t_trace,
260 .type = VLIB_NODE_TYPE_INTERNAL,
Calvinee275a72016-08-10 11:01:41 -0400261
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 .n_errors = ARRAY_LEN(l2t_decap_error_strings),
263 .error_strings = l2t_decap_error_strings,
264
265 .n_next_nodes = L2T_DECAP_N_NEXT,
266
267 /* edit / add dispositions here */
268 .next_nodes = {
269 [L2T_DECAP_NEXT_L2_INPUT] = "l2-input",
270 [L2T_DECAP_NEXT_DROP] = "error-drop",
271 },
272};
Calvinee275a72016-08-10 11:01:41 -0400273/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Calvinee275a72016-08-10 11:01:41 -0400275VLIB_NODE_FUNCTION_MULTIARCH (l2t_decap_node, l2t_decap_node_fn);
276/* *INDENT-OFF* */
Pierre Pfistera026eb12016-06-20 14:52:22 +0100277VLIB_REGISTER_NODE (l2t_decap_local_node) = {
278 .function = l2t_decap_node_fn,
279 .name = "l2tp-decap-local",
280 .vector_size = sizeof (u32),
281 .format_trace = format_l2t_trace,
282 .type = VLIB_NODE_TYPE_INTERNAL,
283
284 .n_errors = ARRAY_LEN(l2t_decap_error_strings),
285 .error_strings = l2t_decap_error_strings,
286
287 .n_next_nodes = L2T_DECAP_N_NEXT,
288
289 /* edit / add dispositions here */
290 .next_nodes = {
Calvinee275a72016-08-10 11:01:41 -0400291 [L2T_DECAP_NEXT_L2_INPUT] = "l2-input",
292 [L2T_DECAP_NEXT_DROP] = "error-drop",
Pierre Pfistera026eb12016-06-20 14:52:22 +0100293 },
294};
Calvinee275a72016-08-10 11:01:41 -0400295/* *INDENT-ON* */
Pierre Pfistera026eb12016-06-20 14:52:22 +0100296
Calvinee275a72016-08-10 11:01:41 -0400297void
298l2tp_decap_init (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299{
Pierre Pfistera026eb12016-06-20 14:52:22 +0100300 ip6_register_protocol (IP_PROTOCOL_L2TP, l2t_decap_local_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301}
Calvinee275a72016-08-10 11:01:41 -0400302
303/*
304 * fd.io coding-style-patch-verification: ON
305 *
306 * Local Variables:
307 * eval: (c-set-style "gnu")
308 * End:
309 */