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