blob: 42ecfa619a5c4c4ec9516d0bba9c1edcc7829c17 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * l2tp.c : L2TPv3 tunnel support
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/l2/l2_input.h>
23#include <vnet/ethernet/ethernet.h>
24#include <vnet/l2tp/l2tp.h>
25
26l2t_main_t l2t_main;
27
28/* packet trace format function */
Calvinee275a72016-08-10 11:01:41 -040029u8 *
30format_l2t_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070031{
32 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
33 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Calvinee275a72016-08-10 11:01:41 -040034 l2t_trace_t *t = va_arg (*args, l2t_trace_t *);
35
Ed Warnickecb9cada2015-12-08 15:45:58 -070036 if (t->is_user_to_network)
Calvinee275a72016-08-10 11:01:41 -040037 s = format (s, "L2T: %U (client) -> %U (our) session %d",
38 format_ip6_address, &t->client_address,
39 format_ip6_address, &t->our_address, t->session_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070040 else
41 s = format (s, "L2T: %U (our) -> %U (client) session %d)",
Calvinee275a72016-08-10 11:01:41 -040042 format_ip6_address, &t->our_address,
43 format_ip6_address, &t->client_address, t->session_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 return s;
45}
46
Calvinee275a72016-08-10 11:01:41 -040047u8 *
48format_l2t_session (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070049{
Calvinee275a72016-08-10 11:01:41 -040050 l2t_session_t *session = va_arg (*args, l2t_session_t *);
51 l2t_main_t *lm = &l2t_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070052 u32 counter_index;
53 vlib_counter_t v;
54
Calvinee275a72016-08-10 11:01:41 -040055 s = format (s, "[%d] %U (our) %U (client) %U (sw_if_index %d)\n",
56 session - lm->sessions,
57 format_ip6_address, &session->our_address,
58 format_ip6_address, &session->client_address,
59 format_vnet_sw_interface_name, lm->vnet_main,
60 vnet_get_sw_interface (lm->vnet_main, session->sw_if_index),
61 session->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
63 s = format (s, " local cookies %016llx %016llx remote cookie %016llx\n",
Calvinee275a72016-08-10 11:01:41 -040064 clib_net_to_host_u64 (session->local_cookie[0]),
65 clib_net_to_host_u64 (session->local_cookie[1]),
66 clib_net_to_host_u64 (session->remote_cookie));
Ed Warnickecb9cada2015-12-08 15:45:58 -070067
68 s = format (s, " local session-id %d remote session-id %d\n",
Calvinee275a72016-08-10 11:01:41 -040069 clib_net_to_host_u32 (session->local_session_id),
70 clib_net_to_host_u32 (session->remote_session_id));
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
Calvinee275a72016-08-10 11:01:41 -040072 s = format (s, " l2 specific sublayer %s\n",
73 session->l2_sublayer_present ? "preset" : "absent");
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
Calvinee275a72016-08-10 11:01:41 -040075 counter_index =
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 session_index_to_counter_index (session - lm->sessions,
Calvinee275a72016-08-10 11:01:41 -040077 SESSION_COUNTER_USER_TO_NETWORK);
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79 vlib_get_combined_counter (&lm->counter_main, counter_index, &v);
80 if (v.packets != 0)
81 s = format (s, " user-to-net: %llu pkts %llu bytes\n",
Calvinee275a72016-08-10 11:01:41 -040082 v.packets, v.bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -070083
Calvinee275a72016-08-10 11:01:41 -040084 vlib_get_combined_counter (&lm->counter_main, counter_index + 1, &v);
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
86 if (v.packets != 0)
87 s = format (s, " net-to-user: %llu pkts %llu bytes\n",
Calvinee275a72016-08-10 11:01:41 -040088 v.packets, v.bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 return s;
90}
91
92static clib_error_t *
93show_l2tp_command_fn (vlib_main_t * vm,
Calvinee275a72016-08-10 11:01:41 -040094 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070095{
96 l2t_session_t *session;
97 l2t_main_t *lm = &l2t_main;
Calvinee275a72016-08-10 11:01:41 -040098 char *keystr = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 int verbose = 0;
Calvinee275a72016-08-10 11:01:41 -0400100
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 if (unformat (input, "verbose") || unformat (input, "v"))
102 verbose = 1;
103
104 if (pool_elts (lm->sessions) == 0)
Calvinee275a72016-08-10 11:01:41 -0400105 vlib_cli_output (vm, "No l2tp sessions...");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 else
Calvinee275a72016-08-10 11:01:41 -0400107 vlib_cli_output (vm, "%u l2tp sessions...", pool_elts (lm->sessions));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
109 if (verbose)
110 {
111 switch (lm->lookup_type)
Calvinee275a72016-08-10 11:01:41 -0400112 {
113 case L2T_LOOKUP_SRC_ADDRESS:
114 keystr = "src address";
115 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Calvinee275a72016-08-10 11:01:41 -0400117 case L2T_LOOKUP_DST_ADDRESS:
118 keystr = "dst address";
119 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
Calvinee275a72016-08-10 11:01:41 -0400121 case L2T_LOOKUP_SESSION_ID:
122 keystr = "session id";
123 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
Calvinee275a72016-08-10 11:01:41 -0400125 default:
126 keystr = "BOGUS!";
127 break;
128 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
130 vlib_cli_output (vm, "L2tp session lookup on %s", keystr);
131
Calvinee275a72016-08-10 11:01:41 -0400132 /* *INDENT-OFF* */
Damjan Marion607de1a2016-08-16 22:53:54 +0200133 pool_foreach (session, lm->sessions,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 ({
135 vlib_cli_output (vm, "%U", format_l2t_session, session);
136 }));
Calvinee275a72016-08-10 11:01:41 -0400137 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 }
Calvinee275a72016-08-10 11:01:41 -0400139
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 return 0;
141}
142
Calvinee275a72016-08-10 11:01:41 -0400143/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144VLIB_CLI_COMMAND (show_session_detail_command, static) = {
145 .path = "show l2tpv3",
146 .short_help = "show l2tpv3 [verbose]",
147 .function = show_l2tp_command_fn,
148};
Calvinee275a72016-08-10 11:01:41 -0400149/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
151static clib_error_t *
152test_counters_command_fn (vlib_main_t * vm,
Calvinee275a72016-08-10 11:01:41 -0400153 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154{
155 l2t_session_t *session;
156 l2t_main_t *lm = &l2t_main;
157 u32 session_index;
158 u32 counter_index;
Calvinee275a72016-08-10 11:01:41 -0400159 u32 nincr = 0;
Damjan Marion067cd622018-07-11 12:47:43 +0200160 u32 thread_index = vm->thread_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Calvinee275a72016-08-10 11:01:41 -0400162 /* *INDENT-OFF* */
Damjan Marion607de1a2016-08-16 22:53:54 +0200163 pool_foreach (session, lm->sessions,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 ({
165 session_index = session - lm->sessions;
Damjan Marion607de1a2016-08-16 22:53:54 +0200166 counter_index =
167 session_index_to_counter_index (session_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 SESSION_COUNTER_USER_TO_NETWORK);
169 vlib_increment_combined_counter (&lm->counter_main,
Damjan Marion586afd72017-04-05 19:18:20 +0200170 thread_index,
Damjan Marion607de1a2016-08-16 22:53:54 +0200171 counter_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 1/*pkt*/, 1111 /*bytes*/);
173 vlib_increment_combined_counter (&lm->counter_main,
Damjan Marion586afd72017-04-05 19:18:20 +0200174 thread_index,
Damjan Marion607de1a2016-08-16 22:53:54 +0200175 counter_index+1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176 1/*pkt*/, 2222 /*bytes*/);
177 nincr++;
Damjan Marion607de1a2016-08-16 22:53:54 +0200178
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 }));
Calvinee275a72016-08-10 11:01:41 -0400180 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 vlib_cli_output (vm, "Incremented %d active counters\n", nincr);
Calvinee275a72016-08-10 11:01:41 -0400182
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 return 0;
184}
185
Calvinee275a72016-08-10 11:01:41 -0400186/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187VLIB_CLI_COMMAND (test_counters_command, static) = {
188 .path = "test counters",
189 .short_help = "increment all active counters",
190 .function = test_counters_command_fn,
191};
Calvinee275a72016-08-10 11:01:41 -0400192/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
194static clib_error_t *
195clear_counters_command_fn (vlib_main_t * vm,
Calvinee275a72016-08-10 11:01:41 -0400196 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197{
198 l2t_session_t *session;
199 l2t_main_t *lm = &l2t_main;
200 u32 session_index;
201 u32 counter_index;
Calvinee275a72016-08-10 11:01:41 -0400202 u32 nincr = 0;
203
204 /* *INDENT-OFF* */
Damjan Marion607de1a2016-08-16 22:53:54 +0200205 pool_foreach (session, lm->sessions,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 ({
207 session_index = session - lm->sessions;
Damjan Marion607de1a2016-08-16 22:53:54 +0200208 counter_index =
209 session_index_to_counter_index (session_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 SESSION_COUNTER_USER_TO_NETWORK);
211 vlib_zero_combined_counter (&lm->counter_main, counter_index);
212 vlib_zero_combined_counter (&lm->counter_main, counter_index+1);
213 nincr++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214 }));
Calvinee275a72016-08-10 11:01:41 -0400215 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216 vlib_cli_output (vm, "Cleared %d active counters\n", nincr);
Calvinee275a72016-08-10 11:01:41 -0400217
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218 return 0;
219}
220
Calvinee275a72016-08-10 11:01:41 -0400221/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222VLIB_CLI_COMMAND (clear_counters_command, static) = {
Calvinee275a72016-08-10 11:01:41 -0400223 .path = "clear counters",
224 .short_help = "clear all active counters",
225 .function = clear_counters_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226};
Calvinee275a72016-08-10 11:01:41 -0400227/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Calvinee275a72016-08-10 11:01:41 -0400229static u8 *
230format_l2tpv3_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231{
232 l2t_main_t *lm = &l2t_main;
233 u32 i = va_arg (*args, u32);
234 u32 show_dev_instance = ~0;
235
236 if (i < vec_len (lm->dev_inst_by_real))
237 show_dev_instance = lm->dev_inst_by_real[i];
238
239 if (show_dev_instance != ~0)
240 i = show_dev_instance;
241
242 return format (s, "l2tpv3_tunnel%d", i);
243}
244
Calvinee275a72016-08-10 11:01:41 -0400245static int
246l2tpv3_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247{
248 l2t_main_t *lm = &l2t_main;
249
250 vec_validate_init_empty (lm->dev_inst_by_real, hi->dev_instance, ~0);
251
Calvinee275a72016-08-10 11:01:41 -0400252 lm->dev_inst_by_real[hi->dev_instance] = new_dev_instance;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
254 return 0;
255}
256
Calvinee275a72016-08-10 11:01:41 -0400257/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258VNET_DEVICE_CLASS (l2tpv3_device_class,static) = {
259 .name = "L2TPv3",
260 .format_device_name = format_l2tpv3_name,
261 .name_renumber = l2tpv3_name_renumber,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262};
Calvinee275a72016-08-10 11:01:41 -0400263/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
Calvinee275a72016-08-10 11:01:41 -0400265static u8 *
266format_l2tp_header_with_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267{
268 u32 dev_instance = va_arg (*args, u32);
269 s = format (s, "unimplemented dev %u", dev_instance);
270 return s;
271}
272
Calvinee275a72016-08-10 11:01:41 -0400273/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274VNET_HW_INTERFACE_CLASS (l2tpv3_hw_class) = {
275 .name = "L2TPV3",
276 .format_header = format_l2tp_header_with_length,
Neale Rannsb80c5362016-10-08 13:03:40 +0100277 .build_rewrite = default_build_rewrite,
278 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279};
Calvinee275a72016-08-10 11:01:41 -0400280/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
Calvinee275a72016-08-10 11:01:41 -0400282int
283create_l2tpv3_ipv6_tunnel (l2t_main_t * lm,
284 ip6_address_t * client_address,
285 ip6_address_t * our_address,
286 u32 local_session_id,
287 u32 remote_session_id,
288 u64 local_cookie,
289 u64 remote_cookie,
290 int l2_sublayer_present,
291 u32 encap_fib_index, u32 * sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292{
293 l2t_session_t *s = 0;
Calvinee275a72016-08-10 11:01:41 -0400294 vnet_main_t *vnm = lm->vnet_main;
295 vnet_hw_interface_t *hi;
296 uword *p = (uword *) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 u32 hw_if_index;
298 l2tpv3_header_t l2tp_hdr;
Calvinee275a72016-08-10 11:01:41 -0400299 ip6_address_t *dst_address_copy, *src_address_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300 u32 counter_index;
301
302 remote_session_id = clib_host_to_net_u32 (remote_session_id);
Calvinee275a72016-08-10 11:01:41 -0400303 local_session_id = clib_host_to_net_u32 (local_session_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304
Calvinee275a72016-08-10 11:01:41 -0400305 switch (lm->lookup_type)
306 {
307 case L2T_LOOKUP_SRC_ADDRESS:
308 p = hash_get_mem (lm->session_by_src_address, client_address);
309 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310
Calvinee275a72016-08-10 11:01:41 -0400311 case L2T_LOOKUP_DST_ADDRESS:
312 p = hash_get_mem (lm->session_by_dst_address, our_address);
313 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
Calvinee275a72016-08-10 11:01:41 -0400315 case L2T_LOOKUP_SESSION_ID:
316 p = hash_get (lm->session_by_session_id, local_session_id);
317 break;
318
319 default:
320 ASSERT (0);
321 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322
323 /* adding a session: session must not already exist */
Calvinee275a72016-08-10 11:01:41 -0400324 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 return VNET_API_ERROR_INVALID_VALUE;
326
327 pool_get (lm->sessions, s);
Dave Barachb7b92992018-10-17 10:38:51 -0400328 clib_memset (s, 0, sizeof (*s));
Damjan Marionf1213b82016-03-13 02:22:06 +0100329 clib_memcpy (&s->our_address, our_address, sizeof (s->our_address));
Calvinee275a72016-08-10 11:01:41 -0400330 clib_memcpy (&s->client_address, client_address,
331 sizeof (s->client_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 s->local_cookie[0] = clib_host_to_net_u64 (local_cookie);
333 s->remote_cookie = clib_host_to_net_u64 (remote_cookie);
334 s->local_session_id = local_session_id;
335 s->remote_session_id = remote_session_id;
336 s->l2_sublayer_present = l2_sublayer_present;
337 /* precompute l2tp header size */
Calvinee275a72016-08-10 11:01:41 -0400338 s->l2tp_hdr_size = l2_sublayer_present ?
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339 sizeof (l2tpv3_header_t) :
Calvinee275a72016-08-10 11:01:41 -0400340 sizeof (l2tpv3_header_t) - sizeof (l2tp_hdr.l2_specific_sublayer);
Pierre Pfister80ee2132016-06-22 12:54:48 +0100341 s->admin_up = 0;
Pierre Pfister08e03122016-07-15 09:19:39 +0100342 s->encap_fib_index = encap_fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
344 /* Setup hash table entries */
Calvinee275a72016-08-10 11:01:41 -0400345 switch (lm->lookup_type)
346 {
347 case L2T_LOOKUP_SRC_ADDRESS:
348 src_address_copy = clib_mem_alloc (sizeof (*src_address_copy));
349 clib_memcpy (src_address_copy, client_address,
350 sizeof (*src_address_copy));
351 hash_set_mem (lm->session_by_src_address, src_address_copy,
352 s - lm->sessions);
353 break;
354 case L2T_LOOKUP_DST_ADDRESS:
355 dst_address_copy = clib_mem_alloc (sizeof (*dst_address_copy));
356 clib_memcpy (dst_address_copy, our_address, sizeof (*dst_address_copy));
357 hash_set_mem (lm->session_by_dst_address, dst_address_copy,
358 s - lm->sessions);
359 break;
360 case L2T_LOOKUP_SESSION_ID:
361 hash_set (lm->session_by_session_id, local_session_id,
362 s - lm->sessions);
363 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364
Calvinee275a72016-08-10 11:01:41 -0400365 default:
366 ASSERT (0);
367 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
369 /* validate counters */
Calvinee275a72016-08-10 11:01:41 -0400370 counter_index =
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371 session_index_to_counter_index (s - lm->sessions,
Calvinee275a72016-08-10 11:01:41 -0400372 SESSION_COUNTER_USER_TO_NETWORK);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373 vlib_validate_combined_counter (&lm->counter_main, counter_index);
Calvinee275a72016-08-10 11:01:41 -0400374 vlib_validate_combined_counter (&lm->counter_main, counter_index + 1);
375
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376 if (vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) > 0)
377 {
378 hw_if_index = lm->free_l2tpv3_tunnel_hw_if_indices
Calvinee275a72016-08-10 11:01:41 -0400379 [vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) - 1];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 _vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) -= 1;
381
382 hi = vnet_get_hw_interface (vnm, hw_if_index);
383 hi->dev_instance = s - lm->sessions;
384 hi->hw_instance = hi->dev_instance;
385 }
Calvinee275a72016-08-10 11:01:41 -0400386 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387 {
388 hw_if_index = vnet_register_interface
Calvinee275a72016-08-10 11:01:41 -0400389 (vnm, l2tpv3_device_class.index, s - lm->sessions,
390 l2tpv3_hw_class.index, s - lm->sessions);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 hi = vnet_get_hw_interface (vnm, hw_if_index);
392 hi->output_node_index = l2t_encap_node.index;
393 /* $$$$ initialize custom dispositions, if needed */
394 }
Calvinee275a72016-08-10 11:01:41 -0400395
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396 s->hw_if_index = hw_if_index;
397 s->sw_if_index = hi->sw_if_index;
398
399 if (sw_if_index)
400 *sw_if_index = hi->sw_if_index;
401
Jakub Grajciarf34f1a42019-05-27 12:23:57 +0200402 if (!lm->proto_registered)
403 {
404 ip6_register_protocol (IP_PROTOCOL_L2TP, l2t_decap_local_node.index);
405 lm->proto_registered = true;
406 }
407
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 return 0;
409}
410
411static clib_error_t *
412create_l2tpv3_tunnel_command_fn (vlib_main_t * vm,
Calvinee275a72016-08-10 11:01:41 -0400413 unformat_input_t * input,
414 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415{
416 ip6_address_t client_address, our_address;
Calvinee275a72016-08-10 11:01:41 -0400417 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 l2t_main_t *lm = &l2t_main;
Calvinee275a72016-08-10 11:01:41 -0400419 u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420 u32 local_session_id = 1, remote_session_id = 1;
421 int our_address_set = 0, client_address_set = 0;
422 int l2_sublayer_present = 0;
423 int rv;
424 u32 sw_if_index;
Pierre Pfister08e03122016-07-15 09:19:39 +0100425 u32 encap_fib_id = ~0;
426 u32 encap_fib_index = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500427 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
429 /* Get a line of input. */
Calvinee275a72016-08-10 11:01:41 -0400430 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 return 0;
432
Calvinee275a72016-08-10 11:01:41 -0400433 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
434 {
435 if (unformat (line_input, "client %U",
436 unformat_ip6_address, &client_address))
437 client_address_set = 1;
438 else if (unformat (line_input, "our %U",
439 unformat_ip6_address, &our_address))
440 our_address_set = 1;
441 else if (unformat (line_input, "local-cookie %llx", &local_cookie))
442 ;
443 else if (unformat (line_input, "remote-cookie %llx", &remote_cookie))
444 ;
445 else if (unformat (line_input, "local-session-id %d",
446 &local_session_id))
447 ;
448 else if (unformat (line_input, "remote-session-id %d",
449 &remote_session_id))
450 ;
451 else if (unformat (line_input, "fib-id %d", &encap_fib_id))
452 ;
453 else if (unformat (line_input, "l2-sublayer-present"))
454 l2_sublayer_present = 1;
455 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500456 {
457 error = clib_error_return (0, "parse error: '%U'",
458 format_unformat_error, line_input);
459 goto done;
460 }
Calvinee275a72016-08-10 11:01:41 -0400461 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Calvinee275a72016-08-10 11:01:41 -0400463 if (encap_fib_id != ~0)
464 {
Pierre Pfister08e03122016-07-15 09:19:39 +0100465 uword *p;
466 ip6_main_t *im = &ip6_main;
467 if (!(p = hash_get (im->fib_index_by_table_id, encap_fib_id)))
Billy McFalla9a20e72017-02-15 11:39:12 -0500468 {
469 error = clib_error_return (0, "No fib with id %d", encap_fib_id);
470 goto done;
471 }
Pierre Pfister08e03122016-07-15 09:19:39 +0100472 encap_fib_index = p[0];
Calvinee275a72016-08-10 11:01:41 -0400473 }
474 else
475 {
Pierre Pfister08e03122016-07-15 09:19:39 +0100476 encap_fib_index = ~0;
Calvinee275a72016-08-10 11:01:41 -0400477 }
Pierre Pfister08e03122016-07-15 09:19:39 +0100478
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 if (our_address_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500480 {
481 error = clib_error_return (0, "our address not specified");
482 goto done;
483 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484 if (client_address_set == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500485 {
486 error = clib_error_return (0, "client address not specified");
487 goto done;
488 }
Calvinee275a72016-08-10 11:01:41 -0400489
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 rv = create_l2tpv3_ipv6_tunnel (lm, &client_address, &our_address,
Calvinee275a72016-08-10 11:01:41 -0400491 local_session_id, remote_session_id,
492 local_cookie, remote_cookie,
493 l2_sublayer_present,
494 encap_fib_index, &sw_if_index);
495 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 {
497 case 0:
Calvinee275a72016-08-10 11:01:41 -0400498 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
499 vnet_get_main (), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 break;
501 case VNET_API_ERROR_INVALID_VALUE:
Billy McFalla9a20e72017-02-15 11:39:12 -0500502 error = clib_error_return (0, "session already exists...");
503 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504
505 case VNET_API_ERROR_NO_SUCH_ENTRY:
Billy McFalla9a20e72017-02-15 11:39:12 -0500506 error = clib_error_return (0, "session does not exist...");
507 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
509 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500510 error = clib_error_return (0, "l2tp_session_add_del returned %d", rv);
511 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512 }
513
Billy McFalla9a20e72017-02-15 11:39:12 -0500514done:
515 unformat_free (line_input);
516
517 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518}
519
Calvinee275a72016-08-10 11:01:41 -0400520/* *INDENT-OFF* */
Damjan Marion607de1a2016-08-16 22:53:54 +0200521VLIB_CLI_COMMAND (create_l2tpv3_tunnel_command, static) =
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522{
Calvinee275a72016-08-10 11:01:41 -0400523 .path = "create l2tpv3 tunnel",
Damjan Marion607de1a2016-08-16 22:53:54 +0200524 .short_help =
Calvinee275a72016-08-10 11:01:41 -0400525 "create l2tpv3 tunnel client <ip6> our <ip6> local-cookie <hex> remote-cookie <hex> local-session <dec> remote-session <dec>",
526 .function = create_l2tpv3_tunnel_command_fn,
527};
528/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
Calvinee275a72016-08-10 11:01:41 -0400530int
531l2tpv3_set_tunnel_cookies (l2t_main_t * lm,
532 u32 sw_if_index,
533 u64 new_local_cookie, u64 new_remote_cookie)
534{
535 l2t_session_t *s;
536 vnet_hw_interface_t *hi;
537 vnet_main_t *vnm = vnet_get_main ();
538 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539
Calvinee275a72016-08-10 11:01:41 -0400540 if (pool_is_free_index (lm->sessions, hi->dev_instance))
541 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542
Calvinee275a72016-08-10 11:01:41 -0400543 s = pool_elt_at_index (lm->sessions, hi->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544
Calvinee275a72016-08-10 11:01:41 -0400545 s->local_cookie[1] = s->local_cookie[0];
546 s->local_cookie[0] = clib_host_to_net_u64 (new_local_cookie);
547 s->remote_cookie = clib_host_to_net_u64 (new_remote_cookie);
548
549 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550}
551
552
553static clib_error_t *
554set_l2tp_tunnel_cookie_command_fn (vlib_main_t * vm,
Calvinee275a72016-08-10 11:01:41 -0400555 unformat_input_t * input,
556 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557{
558 l2t_main_t *lm = &l2t_main;
Calvinee275a72016-08-10 11:01:41 -0400559 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560 u32 sw_if_index = ~0;
Calvinee275a72016-08-10 11:01:41 -0400561 u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
563 int rv;
Calvinee275a72016-08-10 11:01:41 -0400564
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
566 {
Calvinee275a72016-08-10 11:01:41 -0400567 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
568 &sw_if_index))
569 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 else if (unformat (input, "local %llx", &local_cookie))
Calvinee275a72016-08-10 11:01:41 -0400571 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 else if (unformat (input, "remote %llx", &remote_cookie))
Calvinee275a72016-08-10 11:01:41 -0400573 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574 else
Calvinee275a72016-08-10 11:01:41 -0400575 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576 }
577 if (sw_if_index == ~0)
578 return clib_error_return (0, "unknown interface");
579 if (local_cookie == ~0)
580 return clib_error_return (0, "local cookie required");
581 if (remote_cookie == ~0)
582 return clib_error_return (0, "remote cookie required");
583
Calvinee275a72016-08-10 11:01:41 -0400584 rv = l2tpv3_set_tunnel_cookies (lm, sw_if_index,
585 local_cookie, remote_cookie);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586
587 switch (rv)
588 {
589 case 0:
590 break;
591
592 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
593 return clib_error_return (0, "invalid interface");
594
595 default:
596 return clib_error_return (0, "l2tp_session_set_cookies returned %d",
Calvinee275a72016-08-10 11:01:41 -0400597 rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598 }
599
Calvinee275a72016-08-10 11:01:41 -0400600 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601}
602
Calvinee275a72016-08-10 11:01:41 -0400603/* *INDENT-OFF* */
Damjan Marion607de1a2016-08-16 22:53:54 +0200604VLIB_CLI_COMMAND (set_l2tp_tunnel_cookie_command, static) =
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605{
Calvinee275a72016-08-10 11:01:41 -0400606 .path = "set l2tpv3 tunnel cookie",
Damjan Marion607de1a2016-08-16 22:53:54 +0200607 .short_help =
Calvinee275a72016-08-10 11:01:41 -0400608 "set l2tpv3 tunnel cookie <intfc> local <hex> remote <hex>",
609 .function = set_l2tp_tunnel_cookie_command_fn,
610};
611/* *INDENT-ON* */
612
613int
614l2tpv3_interface_enable_disable (vnet_main_t * vnm,
615 u32 sw_if_index, int enable_disable)
616{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617
618 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
619 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
620
Damjan Marion8b3191e2016-11-09 19:54:20 +0100621 vnet_feature_enable_disable ("ip6-unicast", "l2tp-decap", sw_if_index,
622 enable_disable, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623 return 0;
624}
625
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700626/* Enable/disable L2TPv3 intercept on IP6 forwarding path */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627static clib_error_t *
628set_ip6_l2tpv3 (vlib_main_t * vm,
Calvinee275a72016-08-10 11:01:41 -0400629 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630{
631 u32 sw_if_index = ~0;
632 int is_add = 1;
633 int rv;
Calvinee275a72016-08-10 11:01:41 -0400634 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
636 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
637 {
Calvinee275a72016-08-10 11:01:41 -0400638 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
639 &sw_if_index))
640 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641 else if (unformat (input, "del"))
Calvinee275a72016-08-10 11:01:41 -0400642 is_add = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643 else
Calvinee275a72016-08-10 11:01:41 -0400644 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 }
646
647 if (sw_if_index == ~0)
648 return clib_error_return (0, "interface required");
Calvinee275a72016-08-10 11:01:41 -0400649
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 rv = l2tpv3_interface_enable_disable (vnm, sw_if_index, is_add);
651
652 switch (rv)
653 {
654 case 0:
655 break;
656
657 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
658 return clib_error_return (0, "invalid interface");
659
660 default:
Calvinee275a72016-08-10 11:01:41 -0400661 return clib_error_return (0,
662 "l2tp_interface_enable_disable returned %d",
663 rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664 }
665 return 0;
666}
667
Calvinee275a72016-08-10 11:01:41 -0400668/* *INDENT-OFF* */
Damjan Marion607de1a2016-08-16 22:53:54 +0200669VLIB_CLI_COMMAND (set_interface_ip6_l2tpv3, static) =
Calvinee275a72016-08-10 11:01:41 -0400670{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671 .path = "set interface ip6 l2tpv3",
672 .function = set_ip6_l2tpv3,
673 .short_help = "set interface ip6 l2tpv3 <intfc> [del]",
674};
Calvinee275a72016-08-10 11:01:41 -0400675/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
677static clib_error_t *
678l2tp_config (vlib_main_t * vm, unformat_input_t * input)
679{
Calvinee275a72016-08-10 11:01:41 -0400680 l2t_main_t *lm = &l2t_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681
Calvinee275a72016-08-10 11:01:41 -0400682 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
683 {
684 if (unformat (input, "lookup-v6-src"))
685 lm->lookup_type = L2T_LOOKUP_SRC_ADDRESS;
686 else if (unformat (input, "lookup-v6-dst"))
687 lm->lookup_type = L2T_LOOKUP_DST_ADDRESS;
688 else if (unformat (input, "lookup-session-id"))
689 lm->lookup_type = L2T_LOOKUP_SESSION_ID;
690 else
691 return clib_error_return (0, "unknown input `%U'",
692 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693 }
Calvinee275a72016-08-10 11:01:41 -0400694 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695}
696
697VLIB_CONFIG_FUNCTION (l2tp_config, "l2tp");
698
Pierre Pfister80ee2132016-06-22 12:54:48 +0100699
700clib_error_t *
Calvinee275a72016-08-10 11:01:41 -0400701l2tp_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
Pierre Pfister80ee2132016-06-22 12:54:48 +0100702{
703 l2t_main_t *lm = &l2t_main;
704 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
705 if (hi->hw_class_index != l2tpv3_hw_class.index)
706 return 0;
707
708 u32 session_index = hi->dev_instance;
709 l2t_session_t *s = pool_elt_at_index (lm->sessions, session_index);
Calvinee275a72016-08-10 11:01:41 -0400710 s->admin_up = ! !(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
Pierre Pfister80ee2132016-06-22 12:54:48 +0100711 return 0;
712}
713
714VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2tp_sw_interface_up_down);
715
Calvinee275a72016-08-10 11:01:41 -0400716clib_error_t *
717l2tp_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718{
Calvinee275a72016-08-10 11:01:41 -0400719 l2t_main_t *lm = &l2t_main;
720 ip_main_t *im = &ip_main;
721 ip_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722
Calvinee275a72016-08-10 11:01:41 -0400723 lm->vnet_main = vnet_get_main ();
724 lm->vlib_main = vm;
725 lm->lookup_type = L2T_LOOKUP_DST_ADDRESS;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
Calvinee275a72016-08-10 11:01:41 -0400727 lm->session_by_src_address = hash_create_mem
728 (0, sizeof (ip6_address_t) /* key bytes */ ,
729 sizeof (u32) /* value bytes */ );
730 lm->session_by_dst_address = hash_create_mem
731 (0, sizeof (ip6_address_t) /* key bytes */ ,
732 sizeof (u32) /* value bytes */ );
733 lm->session_by_session_id = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734
Calvinee275a72016-08-10 11:01:41 -0400735 pi = ip_get_protocol_info (im, IP_PROTOCOL_L2TP);
736 pi->unformat_pg_edit = unformat_pg_l2tp_header;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737
Jakub Grajciarf34f1a42019-05-27 12:23:57 +0200738 lm->proto_registered = false;
739
Calvinee275a72016-08-10 11:01:41 -0400740 /* insure these nodes are included in build */
741 l2tp_encap_init (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
Calvinee275a72016-08-10 11:01:41 -0400743 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744}
745
Calvinee275a72016-08-10 11:01:41 -0400746VLIB_INIT_FUNCTION (l2tp_init);
747
Damjan Marione9f929b2017-03-16 11:32:09 +0100748clib_error_t *
749l2tp_worker_init (vlib_main_t * vm)
750{
751 l2tp_encap_init (vm);
752
753 return 0;
754}
755
756VLIB_WORKER_INIT_FUNCTION (l2tp_worker_init);
757
Calvinee275a72016-08-10 11:01:41 -0400758/*
759 * fd.io coding-style-patch-verification: ON
760 *
761 * Local Variables:
762 * eval: (c-set-style "gnu")
763 * End:
764 */