blob: 44641d99019673df78ac4e4a32da25476ce922c0 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * encap.c : L2TPv3 tunnel encapsulation
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_encap_error \
27_(NETWORK_TO_USER, "L2TP L2 network to user (ip6) pkts") \
Pierre Pfister80ee2132016-06-22 12:54:48 +010028_(LOOKUP_FAIL_TO_L3, "L2TP L2 session lookup failed pkts") \
29_(ADMIN_DOWN, "L2TP tunnel is down")
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
Calvinee275a72016-08-10 11:01:41 -040031static char *l2t_encap_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070032#define _(sym,string) string,
33 foreach_l2t_encap_error
34#undef _
35};
36
Calvinee275a72016-08-10 11:01:41 -040037typedef enum
38{
Ed Warnickecb9cada2015-12-08 15:45:58 -070039#define _(sym,str) L2T_ENCAP_ERROR_##sym,
Calvinee275a72016-08-10 11:01:41 -040040 foreach_l2t_encap_error
Ed Warnickecb9cada2015-12-08 15:45:58 -070041#undef _
42 L2T_ENCAP_N_ERROR,
43} l2t_encap_error_t;
44
45
Calvinee275a72016-08-10 11:01:41 -040046typedef enum
47{
48 L2T_ENCAP_NEXT_DROP,
49 L2T_ENCAP_NEXT_IP6_LOOKUP,
50 L2T_ENCAP_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070051} l2t_encap_next_t;
52
Calvinee275a72016-08-10 11:01:41 -040053typedef struct
54{
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 u32 cached_session_index;
56 u32 cached_sw_if_index;
Calvinee275a72016-08-10 11:01:41 -040057 vnet_main_t *vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070058} l2tp_encap_runtime_t;
59
60vlib_node_registration_t l2t_encap_node;
61
62#define NSTAGES 3
63
Calvinee275a72016-08-10 11:01:41 -040064static inline void
Dave Barach66446b92018-07-23 12:20:09 -040065stage0 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -070066{
Calvinee275a72016-08-10 11:01:41 -040067 vlib_prefetch_buffer_header (b, STORE);
68 CLIB_PREFETCH (b->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
Ed Warnickecb9cada2015-12-08 15:45:58 -070069}
70
Calvinee275a72016-08-10 11:01:41 -040071static inline void
Dave Barach66446b92018-07-23 12:20:09 -040072stage1 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
Calvinee275a72016-08-10 11:01:41 -040074 l2tp_encap_runtime_t *rt = (void *) node->runtime_data;
Calvinee275a72016-08-10 11:01:41 -040075 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
Calvinee275a72016-08-10 11:01:41 -040077 u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
78 u32 session_index = rt->cached_session_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
Calvinee275a72016-08-10 11:01:41 -040080 if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 {
Calvinee275a72016-08-10 11:01:41 -040082 hi = vnet_get_sup_hw_interface (rt->vnet_main, sw_if_index);
83 session_index = rt->cached_session_index = hi->dev_instance;
84 rt->cached_sw_if_index = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 }
86
Calvinee275a72016-08-10 11:01:41 -040087 /* Remember mapping index, prefetch the mini counter */
88 vnet_buffer (b)->l2t.next_index = L2T_ENCAP_NEXT_IP6_LOOKUP;
89 vnet_buffer (b)->l2t.session_index = session_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
Calvinee275a72016-08-10 11:01:41 -040091 /* $$$$ prefetch counter... */
Ed Warnickecb9cada2015-12-08 15:45:58 -070092}
93
Calvinee275a72016-08-10 11:01:41 -040094static inline u32
Dave Barach66446b92018-07-23 12:20:09 -040095last_stage (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -070096{
Calvinee275a72016-08-10 11:01:41 -040097 l2t_main_t *lm = &l2t_main;
98 vlib_node_t *n = vlib_get_node (vm, l2t_encap_node.index);
99 u32 node_counter_base_index = n->error_heap_index;
100 vlib_error_main_t *em = &vm->error_main;
101 l2tpv3_header_t *l2tp;
102 u32 session_index;
103 u32 counter_index;
104 l2t_session_t *s;
105 ip6_header_t *ip6;
106 u16 payload_length;
107 u32 next_index = L2T_ENCAP_NEXT_IP6_LOOKUP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
Calvinee275a72016-08-10 11:01:41 -0400109 /* Other-than-output pkt? We're done... */
110 if (vnet_buffer (b)->l2t.next_index != L2T_ENCAP_NEXT_IP6_LOOKUP)
111 return vnet_buffer (b)->l2t.next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112
Calvinee275a72016-08-10 11:01:41 -0400113 em->counters[node_counter_base_index + L2T_ENCAP_ERROR_NETWORK_TO_USER] +=
114 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
Calvinee275a72016-08-10 11:01:41 -0400116 session_index = vnet_buffer (b)->l2t.session_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
Calvinee275a72016-08-10 11:01:41 -0400118 counter_index =
119 session_index_to_counter_index (session_index,
120 SESSION_COUNTER_NETWORK_TO_USER);
Pierre Pfister08e03122016-07-15 09:19:39 +0100121
Calvinee275a72016-08-10 11:01:41 -0400122 /* per-mapping byte stats include the ethernet header */
123 vlib_increment_combined_counter (&lm->counter_main,
Damjan Marion586afd72017-04-05 19:18:20 +0200124 vlib_get_thread_index (),
Calvinee275a72016-08-10 11:01:41 -0400125 counter_index, 1 /* packet_increment */ ,
126 vlib_buffer_length_in_chain (vm, b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
Calvinee275a72016-08-10 11:01:41 -0400128 s = pool_elt_at_index (lm->sessions, session_index);
129
130 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->encap_fib_index;
131
132 /* Paint on an l2tpv3 hdr */
133 vlib_buffer_advance (b, -(s->l2tp_hdr_size));
134 l2tp = vlib_buffer_get_current (b);
135
136 l2tp->session_id = s->remote_session_id;
137 l2tp->cookie = s->remote_cookie;
138 if (PREDICT_FALSE (s->l2_sublayer_present))
139 {
140 l2tp->l2_specific_sublayer = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 }
142
Calvinee275a72016-08-10 11:01:41 -0400143 /* Paint on an ip6 header */
144 vlib_buffer_advance (b, -(sizeof (*ip6)));
145 ip6 = vlib_buffer_get_current (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
Calvinee275a72016-08-10 11:01:41 -0400147 if (PREDICT_FALSE (!(s->admin_up)))
148 {
149 b->error = node->errors[L2T_ENCAP_ERROR_ADMIN_DOWN];
150 next_index = L2T_ENCAP_NEXT_DROP;
151 goto done;
Pierre Pfister80ee2132016-06-22 12:54:48 +0100152 }
153
Calvinee275a72016-08-10 11:01:41 -0400154 ip6->ip_version_traffic_class_and_flow_label =
155 clib_host_to_net_u32 (0x6 << 28);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Calvinee275a72016-08-10 11:01:41 -0400157 /* calculate ip6 payload length */
158 payload_length = vlib_buffer_length_in_chain (vm, b);
159 payload_length -= sizeof (*ip6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
Calvinee275a72016-08-10 11:01:41 -0400161 ip6->payload_length = clib_host_to_net_u16 (payload_length);
162 ip6->protocol = IP_PROTOCOL_L2TP;
163 ip6->hop_limit = 0xff;
164 ip6->src_address.as_u64[0] = s->our_address.as_u64[0];
165 ip6->src_address.as_u64[1] = s->our_address.as_u64[1];
166 ip6->dst_address.as_u64[0] = s->client_address.as_u64[0];
167 ip6->dst_address.as_u64[1] = s->client_address.as_u64[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Pierre Pfister80ee2132016-06-22 12:54:48 +0100169
Calvinee275a72016-08-10 11:01:41 -0400170done:
171 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
172 {
173 l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
174 t->is_user_to_network = 0;
175 t->our_address.as_u64[0] = ip6->src_address.as_u64[0];
176 t->our_address.as_u64[1] = ip6->src_address.as_u64[1];
177 t->client_address.as_u64[0] = ip6->dst_address.as_u64[0];
178 t->client_address.as_u64[1] = ip6->dst_address.as_u64[1];
179 t->session_index = session_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 }
181
Calvinee275a72016-08-10 11:01:41 -0400182 return next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183}
184
185#include <vnet/pipeline.h>
186
Calvinee275a72016-08-10 11:01:41 -0400187uword
188l2t_encap_node_fn (vlib_main_t * vm,
189 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190{
Calvinee275a72016-08-10 11:01:41 -0400191 return dispatch_pipeline (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192}
193
194
Calvinee275a72016-08-10 11:01:41 -0400195/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196VLIB_REGISTER_NODE (l2t_encap_node) = {
197 .function = l2t_encap_node_fn,
198 .name = "l2tp-encap",
199 .vector_size = sizeof (u32),
200 .format_trace = format_l2t_trace,
201 .type = VLIB_NODE_TYPE_INTERNAL,
202 .runtime_data_bytes = sizeof (l2tp_encap_runtime_t),
Calvinee275a72016-08-10 11:01:41 -0400203
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 .n_errors = ARRAY_LEN(l2t_encap_error_strings),
205 .error_strings = l2t_encap_error_strings,
206
207 .n_next_nodes = L2T_ENCAP_N_NEXT,
208
Calvinee275a72016-08-10 11:01:41 -0400209 /* add dispositions here */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 .next_nodes = {
Calvinee275a72016-08-10 11:01:41 -0400211 [L2T_ENCAP_NEXT_IP6_LOOKUP] = "ip6-lookup",
212 [L2T_ENCAP_NEXT_DROP] = "error-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213 },
214};
Calvinee275a72016-08-10 11:01:41 -0400215/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Calvinee275a72016-08-10 11:01:41 -0400217VLIB_NODE_FUNCTION_MULTIARCH (l2t_encap_node, l2t_encap_node_fn);
218void
219l2tp_encap_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220{
Calvinee275a72016-08-10 11:01:41 -0400221 l2tp_encap_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222
223 rt = vlib_node_get_runtime_data (vm, l2t_encap_node.index);
Calvinee275a72016-08-10 11:01:41 -0400224 rt->vnet_main = vnet_get_main ();
225 rt->cached_sw_if_index = (u32) ~ 0;
226 rt->cached_session_index = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227}
Calvinee275a72016-08-10 11:01:41 -0400228
229/*
230 * fd.io coding-style-patch-verification: ON
231 *
232 * Local Variables:
233 * eval: (c-set-style "gnu")
234 * End:
235 */