VPP-143: Correctly drop local l2tp packets when no session is found
[vpp.git] / vnet / vnet / l2tp / decap.c
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 _(NO_SESSION, "l2tpv3 session not found")
31
32 static char * l2t_decap_error_strings[] = {
33 #define _(sym,string) string,
34   foreach_l2t_decap_error
35 #undef _
36 };
37
38 typedef enum {
39 #define _(sym,str) L2T_DECAP_ERROR_##sym,
40     foreach_l2t_decap_error
41 #undef _
42     L2T_DECAP_N_ERROR,
43 } l2t_DECAP_error_t;
44
45 typedef enum { 
46     L2T_DECAP_NEXT_DROP,
47     L2T_DECAP_NEXT_L2_INPUT,
48     L2T_DECAP_N_NEXT,
49     /* Pseudo next index */
50     L2T_DECAP_NEXT_NO_INTERCEPT = L2T_DECAP_N_NEXT,
51 } l2t_decap_next_t;
52
53 #define NSTAGES 3
54
55 static inline void stage0 (vlib_main_t * vm,
56                            vlib_node_runtime_t * node,
57                            u32 buffer_index)
58 {
59     vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
60     vlib_prefetch_buffer_header (b, STORE);
61     /* l2tpv3 header is a long way away, need 2 cache lines */
62     CLIB_PREFETCH (b->data, 2*CLIB_CACHE_LINE_BYTES, STORE);
63 }
64
65 static inline void stage1 (vlib_main_t * vm,
66                            vlib_node_runtime_t * node,
67                            u32 bi)
68 {
69     vlib_buffer_t *b = vlib_get_buffer (vm, bi);
70     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;
75
76     /* Not L2tpv3 (0x73, 0t115)? Use the normal path. */
77     if (PREDICT_FALSE(ip6->protocol != IP_PROTOCOL_L2TP)) {
78         vnet_buffer(b)->l2t.next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
79         return;
80     }
81
82     /* Make up your minds, people... */
83     switch (lm->lookup_type) {
84     case L2T_LOOKUP_SRC_ADDRESS:
85         p = hash_get_mem (lm->session_by_src_address, &ip6->src_address);
86         break;
87     case L2T_LOOKUP_DST_ADDRESS:
88         p = hash_get_mem (lm->session_by_dst_address, &ip6->dst_address);
89         break;
90     case L2T_LOOKUP_SESSION_ID:
91         l2t = (l2tpv3_header_t*)(ip6+1);
92         p = hash_get (lm->session_by_session_id, l2t->session_id);
93         break;
94     default:
95         ASSERT(0);
96     }
97
98     if (PREDICT_FALSE(p == 0)) {
99         vnet_buffer(b)->l2t.next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
100         return;
101     } else {
102         session_index = p[0];
103     }
104
105     /* Remember mapping index, prefetch the mini counter */
106     vnet_buffer(b)->l2t.next_index = L2T_DECAP_NEXT_L2_INPUT;
107     vnet_buffer(b)->l2t.session_index = session_index;
108
109     /* $$$$$ prefetch counter */
110 }
111
112 static inline u32 last_stage (vlib_main_t *vm, vlib_node_runtime_t *node,
113                               u32 bi)
114 {
115     vlib_buffer_t *b = vlib_get_buffer (vm, bi);
116     l2t_main_t *lm = &l2t_main;
117     ip6_header_t * ip6 = vlib_buffer_get_current (b);
118     vlib_node_t *n = vlib_get_node (vm, node->node_index);
119     u32 node_counter_base_index = n->error_heap_index;
120     vlib_error_main_t * em = &vm->error_main;
121     l2tpv3_header_t * l2tp;
122     u32 counter_index;
123     l2t_session_t * session;
124     u32 session_index;
125     u32 next_index;
126     u8 l2tp_decap_local = (l2t_decap_local_node.index == n->index);
127     
128     /* Other-than-output pkt? We're done... */
129     if (vnet_buffer(b)->l2t.next_index != L2T_DECAP_NEXT_L2_INPUT) {
130       next_index = vnet_buffer(b)->l2t.next_index;
131       goto done;
132     }
133
134     em->counters[node_counter_base_index + L2T_DECAP_ERROR_USER_TO_NETWORK] += 1;
135     
136     session_index = vnet_buffer(b)->l2t.session_index;
137
138     counter_index = 
139         session_index_to_counter_index (session_index,
140                                         SESSION_COUNTER_USER_TO_NETWORK);
141
142     /* per-mapping byte stats include the ethernet header */
143     vlib_increment_combined_counter (&lm->counter_main, 
144                                      os_get_cpu_number(),
145                                      counter_index,
146                                      1 /* packet_increment */,
147                                      vlib_buffer_length_in_chain (vm, b) +
148                                      sizeof (ethernet_header_t));
149     
150     session = pool_elt_at_index (lm->sessions, session_index);
151
152     l2tp = vlib_buffer_get_current (b) + sizeof (*ip6);
153     
154     if (PREDICT_FALSE(l2tp->session_id != session->local_session_id)) {
155       // Key matched but session id does not. Assume packet is not for us.
156       em->counters[node_counter_base_index + L2T_DECAP_ERROR_SESSION_ID_MISMATCH] += 1;
157       next_index = L2T_DECAP_NEXT_NO_INTERCEPT;
158       goto done;
159     }
160
161     if (PREDICT_FALSE (l2tp->cookie != session->local_cookie[0])) {
162         if (l2tp->cookie != session->local_cookie[1]) {
163             // Key and session ID matched, but cookie doesn't. Drop this packet.
164             b->error = node->errors[L2T_DECAP_ERROR_COOKIE_MISMATCH];
165             next_index = L2T_DECAP_NEXT_DROP;
166             goto done;
167         }
168     }
169
170     vnet_buffer(b)->sw_if_index[VLIB_RX] = session->sw_if_index;
171
172     /* strip the ip6 and L2TP header */
173     vlib_buffer_advance (b, sizeof (*ip6) + session->l2tp_hdr_size);
174
175     /* Required to make the l2 tag push / pop code work on l2 subifs */
176     vnet_update_l2_len (b);
177
178     if (PREDICT_FALSE(b->flags & VLIB_BUFFER_IS_TRACED)) {
179         l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
180         t->is_user_to_network = 1;
181         t->our_address.as_u64[0] = 
182             ip6->dst_address.as_u64[0];
183         t->our_address.as_u64[1] = 
184             ip6->dst_address.as_u64[1];
185         t->client_address.as_u64[0] = 
186             ip6->src_address.as_u64[0];
187         t->client_address.as_u64[1] = 
188             ip6->src_address.as_u64[1];
189         t->session_index = session_index;
190     }
191
192     return L2T_DECAP_NEXT_L2_INPUT;
193
194  done:
195    if (next_index == L2T_DECAP_NEXT_NO_INTERCEPT) {
196        // Small behavioral change between l2tp-decap and l2tp-decap-local
197        if (l2tp_decap_local) {
198            b->error = node->errors[L2T_DECAP_ERROR_NO_SESSION];
199            next_index = L2T_DECAP_NEXT_DROP;
200        } else {
201            // Go to next node on the ip6 configuration chain
202            ip6_main_t * im = &ip6_main;
203            ip_lookup_main_t * lm = &im->lookup_main;
204            ip_config_main_t * cm = &lm->rx_config_mains[VNET_UNICAST];
205            ip6_l2tpv3_config_t * c0;
206
207            vnet_get_config_data (&cm->config_main,
208                                  &b->current_config_index,
209                                  &next_index,
210                                  sizeof (c0[0]));
211        }
212     }
213
214     if (PREDICT_FALSE(b->flags & VLIB_BUFFER_IS_TRACED)) {
215         l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
216         t->is_user_to_network = 1;
217         t->our_address.as_u64[0] = 
218             ip6->dst_address.as_u64[0];
219         t->our_address.as_u64[1] = 
220             ip6->dst_address.as_u64[1];
221         t->client_address.as_u64[0] = 
222             ip6->src_address.as_u64[0];
223         t->client_address.as_u64[1] = 
224             ip6->src_address.as_u64[1];
225         t->session_index = ~0;
226     }
227     return next_index;
228 }
229
230 #include <vnet/pipeline.h>
231
232 static uword l2t_decap_node_fn (vlib_main_t * vm,
233                               vlib_node_runtime_t * node,
234                               vlib_frame_t * frame)
235 {
236     return dispatch_pipeline (vm, node, frame);
237 }
238
239 /*
240  * l2tp-decap and l2tp-decap-local have very slightly different behavior.
241  * When a packet has no associated session l2tp-decap let it go to ip6 forward,
242  * while l2tp-decap-local drops it.
243  */
244
245 VLIB_REGISTER_NODE (l2t_decap_node) = {
246   .function = l2t_decap_node_fn,
247   .name = "l2tp-decap",
248   .vector_size = sizeof (u32),
249   .format_trace = format_l2t_trace,
250   .type = VLIB_NODE_TYPE_INTERNAL,
251   
252   .n_errors = ARRAY_LEN(l2t_decap_error_strings),
253   .error_strings = l2t_decap_error_strings,
254
255   .n_next_nodes = L2T_DECAP_N_NEXT,
256
257   /* edit / add dispositions here */
258   .next_nodes = {
259         [L2T_DECAP_NEXT_L2_INPUT] = "l2-input",
260         [L2T_DECAP_NEXT_DROP] = "error-drop",
261   },
262 };
263
264 VLIB_NODE_FUNCTION_MULTIARCH (l2t_decap_node, l2t_decap_node_fn)
265
266 VLIB_REGISTER_NODE (l2t_decap_local_node) = {
267   .function = l2t_decap_node_fn,
268   .name = "l2tp-decap-local",
269   .vector_size = sizeof (u32),
270   .format_trace = format_l2t_trace,
271   .type = VLIB_NODE_TYPE_INTERNAL,
272
273   .n_errors = ARRAY_LEN(l2t_decap_error_strings),
274   .error_strings = l2t_decap_error_strings,
275
276   .n_next_nodes = L2T_DECAP_N_NEXT,
277
278   /* edit / add dispositions here */
279   .next_nodes = {
280         [L2T_DECAP_NEXT_L2_INPUT] = "l2-input",
281         [L2T_DECAP_NEXT_DROP] = "error-drop",
282   },
283 };
284
285 void l2tp_decap_init (void) 
286 {
287   ip6_register_protocol (IP_PROTOCOL_L2TP, l2t_decap_local_node.index);
288 }