2 * decap.c : L2TPv3 tunnel decapsulation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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")
31 static char * l2t_decap_error_strings[] = {
32 #define _(sym,string) string,
33 foreach_l2t_decap_error
38 #define _(sym,str) L2T_DECAP_ERROR_##sym,
39 foreach_l2t_decap_error
46 L2T_DECAP_NEXT_L2_INPUT,
48 /* Pseudo next index */
49 L2T_DECAP_NEXT_NO_INTERCEPT = L2T_DECAP_N_NEXT,
54 static inline void stage0 (vlib_main_t * vm,
55 vlib_node_runtime_t * node,
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 */
61 CLIB_PREFETCH (b->data, 2*CLIB_CACHE_LINE_BYTES, STORE);
64 static inline void stage1 (vlib_main_t * vm,
65 vlib_node_runtime_t * node,
68 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
69 l2t_main_t *lm = &l2t_main;
70 ip6_header_t * ip6 = vlib_buffer_get_current (b);
73 l2tpv3_header_t * l2t;
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;
81 /* Make up your minds, people... */
82 switch (lm->lookup_type) {
83 case L2T_LOOKUP_SRC_ADDRESS:
84 p = hash_get_mem (lm->session_by_src_address, &ip6->src_address);
86 case L2T_LOOKUP_DST_ADDRESS:
87 p = hash_get_mem (lm->session_by_dst_address, &ip6->dst_address);
89 case L2T_LOOKUP_SESSION_ID:
90 l2t = (l2tpv3_header_t*)(ip6+1);
91 p = hash_get (lm->session_by_session_id, l2t->session_id);
97 if (PREDICT_FALSE(p == 0)) {
98 vnet_buffer(b)->l2t.next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
101 session_index = p[0];
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;
108 /* $$$$$ prefetch counter */
111 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
114 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
115 l2t_main_t *lm = &l2t_main;
116 ip6_header_t * ip6 = vlib_buffer_get_current (b);
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;
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;
132 em->counters[node_counter_base_index + L2T_DECAP_ERROR_USER_TO_NETWORK] += 1;
134 session_index = vnet_buffer(b)->l2t.session_index;
137 session_index_to_counter_index (session_index,
138 SESSION_COUNTER_USER_TO_NETWORK);
140 /* per-mapping byte stats include the ethernet header */
141 vlib_increment_combined_counter (&lm->counter_main,
144 1 /* packet_increment */,
145 vlib_buffer_length_in_chain (vm, b) +
146 sizeof (ethernet_header_t));
148 session = pool_elt_at_index (lm->sessions, session_index);
150 l2tp = vlib_buffer_get_current (b) + sizeof (*ip6);
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;
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;
168 vnet_buffer(b)->sw_if_index[VLIB_RX] = session->sw_if_index;
170 /* strip the ip6 and L2TP header */
171 vlib_buffer_advance (b, sizeof (*ip6) + session->l2tp_hdr_size);
173 /* Required to make the l2 tag push / pop code work on l2 subifs */
174 vnet_update_l2_len (b);
176 if (PREDICT_FALSE(b->flags & VLIB_BUFFER_IS_TRACED)) {
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;
190 return L2T_DECAP_NEXT_L2_INPUT;
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;
197 ip_config_main_t * cm = &lm->rx_config_mains[VNET_UNICAST];
198 ip6_l2tpv3_config_t * c0;
200 vnet_get_config_data (&cm->config_main,
201 &vnet_buffer (b)->ip.current_config_index,
206 if (PREDICT_FALSE(b->flags & VLIB_BUFFER_IS_TRACED)) {
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;
222 #include <vnet/pipeline.h>
224 static uword l2t_decap_node_fn (vlib_main_t * vm,
225 vlib_node_runtime_t * node,
226 vlib_frame_t * frame)
228 return dispatch_pipeline (vm, node, frame);
231 VLIB_REGISTER_NODE (l2t_decap_node) = {
232 .function = l2t_decap_node_fn,
233 .name = "l2tp-decap",
234 .vector_size = sizeof (u32),
235 .format_trace = format_l2t_trace,
236 .type = VLIB_NODE_TYPE_INTERNAL,
238 .n_errors = ARRAY_LEN(l2t_decap_error_strings),
239 .error_strings = l2t_decap_error_strings,
241 .n_next_nodes = L2T_DECAP_N_NEXT,
243 /* edit / add dispositions here */
245 [L2T_DECAP_NEXT_L2_INPUT] = "l2-input",
246 [L2T_DECAP_NEXT_DROP] = "error-drop",
250 VLIB_NODE_FUNCTION_MULTIARCH (l2t_decap_node, l2t_decap_node_fn)
252 void l2tp_decap_init (void)
254 ip6_register_protocol (IP_PROTOCOL_L2TP, l2t_decap_node.index);