2 * l2tp.c : L2TPv3 tunnel support
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/l2/l2_input.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/l2tp/l2tp.h>
28 /* packet trace format function */
29 u8 * format_l2t_trace (u8 * s, va_list * args)
31 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33 l2t_trace_t * t = va_arg (*args, l2t_trace_t *);
35 if (t->is_user_to_network)
36 s = format (s, "L2T: %U (client) -> %U (our) session %d",
37 format_ip6_address, &t->client_address,
38 format_ip6_address, &t->our_address,
41 s = format (s, "L2T: %U (our) -> %U (client) session %d)",
42 format_ip6_address, &t->our_address,
43 format_ip6_address, &t->client_address,
48 u8 * format_l2t_session (u8 * s, va_list * args)
50 l2t_session_t * session = va_arg (*args, l2t_session_t *);
51 l2t_main_t * lm = &l2t_main;
55 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);
63 s = format (s, " local cookies %016llx %016llx remote cookie %016llx\n",
64 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));
68 s = format (s, " local session-id %d remote session-id %d\n",
69 clib_net_to_host_u32 (session->local_session_id),
70 clib_net_to_host_u32 (session->remote_session_id));
72 s = format (s, " l2 specific sublayer %s\n",
73 session->l2_sublayer_present ? "preset" : "absent");
76 session_index_to_counter_index (session - lm->sessions,
77 SESSION_COUNTER_USER_TO_NETWORK);
79 vlib_get_combined_counter (&lm->counter_main, counter_index, &v);
81 s = format (s, " user-to-net: %llu pkts %llu bytes\n",
84 vlib_get_combined_counter (&lm->counter_main, counter_index+1, &v);
87 s = format (s, " net-to-user: %llu pkts %llu bytes\n",
93 show_l2tp_command_fn (vlib_main_t * vm,
94 unformat_input_t * input,
95 vlib_cli_command_t * cmd)
97 l2t_session_t *session;
98 l2t_main_t *lm = &l2t_main;
102 if (unformat (input, "verbose") || unformat (input, "v"))
105 if (pool_elts (lm->sessions) == 0)
106 vlib_cli_output (vm, "No l2tp sessions...");
108 vlib_cli_output (vm, "%u l2tp sessions...", pool_elts (lm->sessions));
112 switch (lm->lookup_type)
114 case L2T_LOOKUP_SRC_ADDRESS:
115 keystr = "src address";
118 case L2T_LOOKUP_DST_ADDRESS:
119 keystr = "dst address";
122 case L2T_LOOKUP_SESSION_ID:
123 keystr = "session id";
131 vlib_cli_output (vm, "L2tp session lookup on %s", keystr);
133 pool_foreach (session, lm->sessions,
135 vlib_cli_output (vm, "%U", format_l2t_session, session);
142 VLIB_CLI_COMMAND (show_session_detail_command, static) = {
143 .path = "show l2tpv3",
144 .short_help = "show l2tpv3 [verbose]",
145 .function = show_l2tp_command_fn,
148 static clib_error_t *
149 test_counters_command_fn (vlib_main_t * vm,
150 unformat_input_t * input,
151 vlib_cli_command_t * cmd)
153 l2t_session_t *session;
154 l2t_main_t *lm = &l2t_main;
158 u32 cpu_index = os_get_cpu_number();
160 pool_foreach (session, lm->sessions,
162 session_index = session - lm->sessions;
164 session_index_to_counter_index (session_index,
165 SESSION_COUNTER_USER_TO_NETWORK);
166 vlib_increment_combined_counter (&lm->counter_main,
169 1/*pkt*/, 1111 /*bytes*/);
170 vlib_increment_combined_counter (&lm->counter_main,
173 1/*pkt*/, 2222 /*bytes*/);
177 vlib_cli_output (vm, "Incremented %d active counters\n", nincr);
182 VLIB_CLI_COMMAND (test_counters_command, static) = {
183 .path = "test counters",
184 .short_help = "increment all active counters",
185 .function = test_counters_command_fn,
188 static clib_error_t *
189 clear_counters_command_fn (vlib_main_t * vm,
190 unformat_input_t * input,
191 vlib_cli_command_t * cmd)
193 l2t_session_t *session;
194 l2t_main_t *lm = &l2t_main;
199 pool_foreach (session, lm->sessions,
201 session_index = session - lm->sessions;
203 session_index_to_counter_index (session_index,
204 SESSION_COUNTER_USER_TO_NETWORK);
205 vlib_zero_combined_counter (&lm->counter_main, counter_index);
206 vlib_zero_combined_counter (&lm->counter_main, counter_index+1);
210 vlib_cli_output (vm, "Cleared %d active counters\n", nincr);
215 VLIB_CLI_COMMAND (clear_counters_command, static) = {
216 .path = "clear counters",
217 .short_help = "clear all active counters",
218 .function = clear_counters_command_fn,
221 static u8 * format_l2tpv3_name (u8 * s, va_list * args)
223 l2t_main_t *lm = &l2t_main;
224 u32 i = va_arg (*args, u32);
225 u32 show_dev_instance = ~0;
227 if (i < vec_len (lm->dev_inst_by_real))
228 show_dev_instance = lm->dev_inst_by_real[i];
230 if (show_dev_instance != ~0)
231 i = show_dev_instance;
233 return format (s, "l2tpv3_tunnel%d", i);
236 static int l2tpv3_name_renumber (vnet_hw_interface_t * hi,
237 u32 new_dev_instance)
239 l2t_main_t *lm = &l2t_main;
241 vec_validate_init_empty (lm->dev_inst_by_real, hi->dev_instance, ~0);
243 lm->dev_inst_by_real [hi->dev_instance] = new_dev_instance;
248 static uword dummy_interface_tx (vlib_main_t * vm,
249 vlib_node_runtime_t * node,
250 vlib_frame_t * frame)
252 clib_warning ("you shouldn't be here, leaking buffers...");
253 return frame->n_vectors;
256 VNET_DEVICE_CLASS (l2tpv3_device_class,static) = {
258 .format_device_name = format_l2tpv3_name,
259 .name_renumber = l2tpv3_name_renumber,
260 .tx_function = dummy_interface_tx,
263 static uword dummy_set_rewrite (vnet_main_t * vnm,
268 uword max_rewrite_bytes)
271 * Conundrum: packets from tun/tap destined for the tunnel
272 * actually have this rewrite applied. Transit packets do not.
273 * To make the two cases equivalent, don't generate a
274 * rewrite here, build the entire header in the fast path.
279 static u8 * format_l2tp_header_with_length (u8 * s, va_list * args)
281 u32 dev_instance = va_arg (*args, u32);
282 s = format (s, "unimplemented dev %u", dev_instance);
286 VNET_HW_INTERFACE_CLASS (l2tpv3_hw_class) = {
288 .format_header = format_l2tp_header_with_length,
289 .set_rewrite = dummy_set_rewrite,
292 int create_l2tpv3_ipv6_tunnel (l2t_main_t * lm,
293 ip6_address_t * client_address,
294 ip6_address_t * our_address,
295 u32 local_session_id,
296 u32 remote_session_id,
299 int l2_sublayer_present,
302 l2t_session_t *s = 0;
303 vnet_main_t * vnm = lm->vnet_main;
304 vnet_hw_interface_t * hi;
305 uword * p = (uword *) ~0;
307 l2tpv3_header_t l2tp_hdr;
308 ip6_address_t * dst_address_copy, * src_address_copy;
311 remote_session_id = clib_host_to_net_u32 (remote_session_id);
312 local_session_id = clib_host_to_net_u32 (local_session_id);
314 switch (lm->lookup_type) {
315 case L2T_LOOKUP_SRC_ADDRESS:
316 p = hash_get_mem (lm->session_by_src_address, client_address);
319 case L2T_LOOKUP_DST_ADDRESS:
320 p = hash_get_mem (lm->session_by_dst_address, our_address);
323 case L2T_LOOKUP_SESSION_ID:
324 p = hash_get (lm->session_by_session_id, local_session_id);
331 /* adding a session: session must not already exist */
333 return VNET_API_ERROR_INVALID_VALUE;
335 pool_get (lm->sessions, s);
336 memset (s, 0, sizeof (*s));
337 clib_memcpy (&s->our_address, our_address, sizeof (s->our_address));
338 clib_memcpy (&s->client_address, client_address, sizeof (s->client_address));
339 s->local_cookie[0] = clib_host_to_net_u64 (local_cookie);
340 s->remote_cookie = clib_host_to_net_u64 (remote_cookie);
341 s->local_session_id = local_session_id;
342 s->remote_session_id = remote_session_id;
343 s->l2_sublayer_present = l2_sublayer_present;
344 /* precompute l2tp header size */
345 s->l2tp_hdr_size = l2_sublayer_present ?
346 sizeof (l2tpv3_header_t) :
347 sizeof (l2tpv3_header_t) - sizeof(l2tp_hdr.l2_specific_sublayer);
350 /* Setup hash table entries */
351 switch (lm->lookup_type) {
352 case L2T_LOOKUP_SRC_ADDRESS:
353 src_address_copy = clib_mem_alloc (sizeof (*src_address_copy));
354 clib_memcpy (src_address_copy, client_address, sizeof (*src_address_copy));
355 hash_set_mem (lm->session_by_src_address, src_address_copy,
358 case L2T_LOOKUP_DST_ADDRESS:
359 dst_address_copy = clib_mem_alloc (sizeof (*dst_address_copy));
360 clib_memcpy (dst_address_copy, our_address, sizeof (*dst_address_copy));
361 hash_set_mem (lm->session_by_dst_address, dst_address_copy,
364 case L2T_LOOKUP_SESSION_ID:
365 hash_set (lm->session_by_session_id, local_session_id,
373 /* validate counters */
375 session_index_to_counter_index (s - lm->sessions,
376 SESSION_COUNTER_USER_TO_NETWORK);
377 vlib_validate_combined_counter (&lm->counter_main, counter_index);
378 vlib_validate_combined_counter (&lm->counter_main, counter_index+1);
380 if (vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) > 0)
382 hw_if_index = lm->free_l2tpv3_tunnel_hw_if_indices
383 [vec_len (lm->free_l2tpv3_tunnel_hw_if_indices)-1];
384 _vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) -= 1;
386 hi = vnet_get_hw_interface (vnm, hw_if_index);
387 hi->dev_instance = s - lm->sessions;
388 hi->hw_instance = hi->dev_instance;
392 hw_if_index = vnet_register_interface
393 (vnm, l2tpv3_device_class.index, s - lm->sessions,
394 l2tpv3_hw_class.index, s - lm->sessions);
395 hi = vnet_get_hw_interface (vnm, hw_if_index);
396 hi->output_node_index = l2t_encap_node.index;
397 /* $$$$ initialize custom dispositions, if needed */
400 s->hw_if_index = hw_if_index;
401 s->sw_if_index = hi->sw_if_index;
404 *sw_if_index = hi->sw_if_index;
409 static clib_error_t *
410 create_l2tpv3_tunnel_command_fn (vlib_main_t * vm,
411 unformat_input_t * input,
412 vlib_cli_command_t * cmd)
414 ip6_address_t client_address, our_address;
415 unformat_input_t _line_input, * line_input = &_line_input;
416 l2t_main_t *lm = &l2t_main;
417 u64 local_cookie = (u64)~0, remote_cookie = (u64)~0;
418 u32 local_session_id = 1, remote_session_id = 1;
419 int our_address_set = 0, client_address_set = 0;
420 int l2_sublayer_present = 0;
424 /* Get a line of input. */
425 if (! unformat_user (input, unformat_line_input, line_input))
428 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
429 if (unformat (line_input, "client %U",
430 unformat_ip6_address, &client_address))
431 client_address_set = 1;
432 else if (unformat (line_input, "our %U",
433 unformat_ip6_address, &our_address))
435 else if (unformat (line_input, "local-cookie %llx", &local_cookie))
437 else if (unformat (line_input, "remote-cookie %llx", &remote_cookie))
439 else if (unformat (line_input, "local-session-id %d",
442 else if (unformat (line_input, "remote-session-id %d",
445 else if (unformat (line_input, "l2-sublayer-present"))
446 l2_sublayer_present = 1;
448 return clib_error_return (0, "parse error: '%U'",
449 format_unformat_error, line_input);
452 unformat_free (line_input);
454 if (our_address_set == 0)
455 return clib_error_return (0, "our address not specified");
456 if (client_address_set == 0)
457 return clib_error_return (0, "client address not specified");
459 rv = create_l2tpv3_ipv6_tunnel (lm, &client_address, &our_address,
460 local_session_id, remote_session_id,
461 local_cookie, remote_cookie,
467 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
469 case VNET_API_ERROR_INVALID_VALUE:
470 return clib_error_return (0, "session already exists...");
472 case VNET_API_ERROR_NO_SUCH_ENTRY:
473 return clib_error_return (0, "session does not exist...");
476 return clib_error_return (0, "l2tp_session_add_del returned %d", rv);
482 VLIB_CLI_COMMAND (create_l2tpv3_tunnel_command, static) = {
483 .path = "create l2tpv3 tunnel",
485 "create l2tpv3 tunnel client <ip6> our <ip6> local-cookie <hex> remote-cookie <hex> local-session <dec> remote-session <dec>",
486 .function = create_l2tpv3_tunnel_command_fn,
489 int l2tpv3_set_tunnel_cookies (l2t_main_t * lm,
491 u64 new_local_cookie,
492 u64 new_remote_cookie)
495 vnet_hw_interface_t * hi;
496 vnet_main_t * vnm = vnet_get_main();
497 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
499 if (pool_is_free_index (lm->sessions, hi->dev_instance))
500 return VNET_API_ERROR_INVALID_VALUE;
502 s = pool_elt_at_index (lm->sessions, hi->dev_instance);
504 s->local_cookie[1] = s->local_cookie[0];
505 s->local_cookie[0] = clib_host_to_net_u64(new_local_cookie);
506 s->remote_cookie = clib_host_to_net_u64(new_remote_cookie);
512 static clib_error_t *
513 set_l2tp_tunnel_cookie_command_fn (vlib_main_t * vm,
514 unformat_input_t * input,
515 vlib_cli_command_t * cmd)
517 l2t_main_t *lm = &l2t_main;
518 vnet_main_t * vnm = vnet_get_main();
519 u32 sw_if_index = ~0;
520 u64 local_cookie = (u64)~0, remote_cookie = (u64)~0;
524 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
526 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
529 else if (unformat (input, "local %llx", &local_cookie))
531 else if (unformat (input, "remote %llx", &remote_cookie))
536 if (sw_if_index == ~0)
537 return clib_error_return (0, "unknown interface");
538 if (local_cookie == ~0)
539 return clib_error_return (0, "local cookie required");
540 if (remote_cookie == ~0)
541 return clib_error_return (0, "remote cookie required");
543 rv = l2tpv3_set_tunnel_cookies (lm, sw_if_index,
544 local_cookie, remote_cookie);
551 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
552 return clib_error_return (0, "invalid interface");
555 return clib_error_return (0, "l2tp_session_set_cookies returned %d",
562 VLIB_CLI_COMMAND (set_l2tp_tunnel_cookie_command, static) = {
563 .path = "set l2tpv3 tunnel cookie",
565 "set l2tpv3 tunnel cookie <intfc> local <hex> remote <hex>",
566 .function = set_l2tp_tunnel_cookie_command_fn,
569 int l2tpv3_interface_enable_disable (vnet_main_t * vnm,
573 ip6_main_t * im = &ip6_main;
574 ip_lookup_main_t * lm = &im->lookup_main;
575 ip_config_main_t * rx_cm = &lm->rx_config_mains[VNET_UNICAST];
577 ip6_l2tpv3_config_t config;
580 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
581 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
583 feature_index = im->ip6_unicast_rx_feature_l2tp_decap;
585 ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
587 ? vnet_config_add_feature
588 : vnet_config_del_feature)
589 (vlib_get_main(), &rx_cm->config_main,
594 rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
598 /* Enable/disable L2TPv3 intercept on IP6 fowarding path */
599 static clib_error_t *
600 set_ip6_l2tpv3 (vlib_main_t * vm,
601 unformat_input_t * input,
602 vlib_cli_command_t * cmd)
604 u32 sw_if_index = ~0;
607 vnet_main_t * vnm = vnet_get_main();
609 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
611 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
614 else if (unformat (input, "del"))
620 if (sw_if_index == ~0)
621 return clib_error_return (0, "interface required");
623 rv = l2tpv3_interface_enable_disable (vnm, sw_if_index, is_add);
630 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
631 return clib_error_return (0, "invalid interface");
634 return clib_error_return (0, "l2tp_interface_enable_disable returned %d",
640 VLIB_CLI_COMMAND (set_interface_ip6_l2tpv3, static) = {
641 .path = "set interface ip6 l2tpv3",
642 .function = set_ip6_l2tpv3,
643 .short_help = "set interface ip6 l2tpv3 <intfc> [del]",
646 static clib_error_t *
647 l2tp_config (vlib_main_t * vm, unformat_input_t * input)
649 l2t_main_t *lm = &l2t_main;
651 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
652 if (unformat (input, "lookup-v6-src"))
653 lm->lookup_type = L2T_LOOKUP_SRC_ADDRESS;
654 else if (unformat (input, "lookup-v6-dst"))
655 lm->lookup_type = L2T_LOOKUP_DST_ADDRESS;
656 else if (unformat (input, "lookup-session-id"))
657 lm->lookup_type = L2T_LOOKUP_SESSION_ID;
658 else return clib_error_return (0, "unknown input `%U'",
659 format_unformat_error, input);
664 VLIB_CONFIG_FUNCTION (l2tp_config, "l2tp");
668 l2tp_sw_interface_up_down (vnet_main_t * vnm,
672 l2t_main_t *lm = &l2t_main;
673 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
674 if (hi->hw_class_index != l2tpv3_hw_class.index)
677 u32 session_index = hi->dev_instance;
678 l2t_session_t *s = pool_elt_at_index (lm->sessions, session_index);
679 s->admin_up = !! (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
683 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2tp_sw_interface_up_down);
685 clib_error_t *l2tp_init (vlib_main_t *vm)
687 l2t_main_t *lm = &l2t_main;
688 ip_main_t * im = &ip_main;
689 ip_protocol_info_t * pi;
691 lm->vnet_main = vnet_get_main();
693 lm->lookup_type = L2T_LOOKUP_DST_ADDRESS;
695 lm->session_by_src_address = hash_create_mem
696 (0, sizeof (ip6_address_t) /* key bytes */,
697 sizeof (u32) /* value bytes */);
698 lm->session_by_dst_address = hash_create_mem
699 (0, sizeof (ip6_address_t) /* key bytes */,
700 sizeof (u32) /* value bytes */);
701 lm->session_by_session_id = hash_create (0, sizeof (uword));
703 pi = ip_get_protocol_info (im, IP_PROTOCOL_L2TP);
704 pi->unformat_pg_edit = unformat_pg_l2tp_header;
706 /* insure these nodes are included in build */
713 VLIB_INIT_FUNCTION(l2tp_init);