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 */
30 format_l2t_trace (u8 * s, va_list * args)
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 *);
34 l2t_trace_t *t = va_arg (*args, l2t_trace_t *);
36 if (t->is_user_to_network)
37 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);
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, t->session_index);
48 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, vlib_cli_command_t * cmd)
96 l2t_session_t *session;
97 l2t_main_t *lm = &l2t_main;
101 if (unformat (input, "verbose") || unformat (input, "v"))
104 if (pool_elts (lm->sessions) == 0)
105 vlib_cli_output (vm, "No l2tp sessions...");
107 vlib_cli_output (vm, "%u l2tp sessions...", pool_elts (lm->sessions));
111 switch (lm->lookup_type)
113 case L2T_LOOKUP_SRC_ADDRESS:
114 keystr = "src address";
117 case L2T_LOOKUP_DST_ADDRESS:
118 keystr = "dst address";
121 case L2T_LOOKUP_SESSION_ID:
122 keystr = "session id";
130 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);
144 VLIB_CLI_COMMAND (show_session_detail_command, static) = {
145 .path = "show l2tpv3",
146 .short_help = "show l2tpv3 [verbose]",
147 .function = show_l2tp_command_fn,
151 static clib_error_t *
152 test_counters_command_fn (vlib_main_t * vm,
153 unformat_input_t * input, vlib_cli_command_t * cmd)
155 l2t_session_t *session;
156 l2t_main_t *lm = &l2t_main;
160 u32 cpu_index = os_get_cpu_number ();
163 pool_foreach (session, lm->sessions,
165 session_index = session - lm->sessions;
167 session_index_to_counter_index (session_index,
168 SESSION_COUNTER_USER_TO_NETWORK);
169 vlib_increment_combined_counter (&lm->counter_main,
172 1/*pkt*/, 1111 /*bytes*/);
173 vlib_increment_combined_counter (&lm->counter_main,
176 1/*pkt*/, 2222 /*bytes*/);
181 vlib_cli_output (vm, "Incremented %d active counters\n", nincr);
187 VLIB_CLI_COMMAND (test_counters_command, static) = {
188 .path = "test counters",
189 .short_help = "increment all active counters",
190 .function = test_counters_command_fn,
194 static clib_error_t *
195 clear_counters_command_fn (vlib_main_t * vm,
196 unformat_input_t * input, vlib_cli_command_t * cmd)
198 l2t_session_t *session;
199 l2t_main_t *lm = &l2t_main;
205 pool_foreach (session, lm->sessions,
207 session_index = session - lm->sessions;
209 session_index_to_counter_index (session_index,
210 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);
216 vlib_cli_output (vm, "Cleared %d active counters\n", nincr);
222 VLIB_CLI_COMMAND (clear_counters_command, static) = {
223 .path = "clear counters",
224 .short_help = "clear all active counters",
225 .function = clear_counters_command_fn,
230 format_l2tpv3_name (u8 * s, va_list * args)
232 l2t_main_t *lm = &l2t_main;
233 u32 i = va_arg (*args, u32);
234 u32 show_dev_instance = ~0;
236 if (i < vec_len (lm->dev_inst_by_real))
237 show_dev_instance = lm->dev_inst_by_real[i];
239 if (show_dev_instance != ~0)
240 i = show_dev_instance;
242 return format (s, "l2tpv3_tunnel%d", i);
246 l2tpv3_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
248 l2t_main_t *lm = &l2t_main;
250 vec_validate_init_empty (lm->dev_inst_by_real, hi->dev_instance, ~0);
252 lm->dev_inst_by_real[hi->dev_instance] = new_dev_instance;
258 dummy_interface_tx (vlib_main_t * vm,
259 vlib_node_runtime_t * node, vlib_frame_t * frame)
261 clib_warning ("you shouldn't be here, leaking buffers...");
262 return frame->n_vectors;
266 VNET_DEVICE_CLASS (l2tpv3_device_class,static) = {
268 .format_device_name = format_l2tpv3_name,
269 .name_renumber = l2tpv3_name_renumber,
270 .tx_function = dummy_interface_tx,
275 dummy_set_rewrite (vnet_main_t * vnm,
278 void *dst_address, void *rewrite, uword max_rewrite_bytes)
281 * Conundrum: packets from tun/tap destined for the tunnel
282 * actually have this rewrite applied. Transit packets do not.
283 * To make the two cases equivalent, don't generate a
284 * rewrite here, build the entire header in the fast path.
290 format_l2tp_header_with_length (u8 * s, va_list * args)
292 u32 dev_instance = va_arg (*args, u32);
293 s = format (s, "unimplemented dev %u", dev_instance);
298 VNET_HW_INTERFACE_CLASS (l2tpv3_hw_class) = {
300 .format_header = format_l2tp_header_with_length,
301 .set_rewrite = dummy_set_rewrite,
306 create_l2tpv3_ipv6_tunnel (l2t_main_t * lm,
307 ip6_address_t * client_address,
308 ip6_address_t * our_address,
309 u32 local_session_id,
310 u32 remote_session_id,
313 int l2_sublayer_present,
314 u32 encap_fib_index, u32 * sw_if_index)
316 l2t_session_t *s = 0;
317 vnet_main_t *vnm = lm->vnet_main;
318 vnet_hw_interface_t *hi;
319 uword *p = (uword *) ~ 0;
321 l2tpv3_header_t l2tp_hdr;
322 ip6_address_t *dst_address_copy, *src_address_copy;
325 remote_session_id = clib_host_to_net_u32 (remote_session_id);
326 local_session_id = clib_host_to_net_u32 (local_session_id);
328 switch (lm->lookup_type)
330 case L2T_LOOKUP_SRC_ADDRESS:
331 p = hash_get_mem (lm->session_by_src_address, client_address);
334 case L2T_LOOKUP_DST_ADDRESS:
335 p = hash_get_mem (lm->session_by_dst_address, our_address);
338 case L2T_LOOKUP_SESSION_ID:
339 p = hash_get (lm->session_by_session_id, local_session_id);
346 /* adding a session: session must not already exist */
348 return VNET_API_ERROR_INVALID_VALUE;
350 pool_get (lm->sessions, s);
351 memset (s, 0, sizeof (*s));
352 clib_memcpy (&s->our_address, our_address, sizeof (s->our_address));
353 clib_memcpy (&s->client_address, client_address,
354 sizeof (s->client_address));
355 s->local_cookie[0] = clib_host_to_net_u64 (local_cookie);
356 s->remote_cookie = clib_host_to_net_u64 (remote_cookie);
357 s->local_session_id = local_session_id;
358 s->remote_session_id = remote_session_id;
359 s->l2_sublayer_present = l2_sublayer_present;
360 /* precompute l2tp header size */
361 s->l2tp_hdr_size = l2_sublayer_present ?
362 sizeof (l2tpv3_header_t) :
363 sizeof (l2tpv3_header_t) - sizeof (l2tp_hdr.l2_specific_sublayer);
365 s->encap_fib_index = encap_fib_index;
367 /* Setup hash table entries */
368 switch (lm->lookup_type)
370 case L2T_LOOKUP_SRC_ADDRESS:
371 src_address_copy = clib_mem_alloc (sizeof (*src_address_copy));
372 clib_memcpy (src_address_copy, client_address,
373 sizeof (*src_address_copy));
374 hash_set_mem (lm->session_by_src_address, src_address_copy,
377 case L2T_LOOKUP_DST_ADDRESS:
378 dst_address_copy = clib_mem_alloc (sizeof (*dst_address_copy));
379 clib_memcpy (dst_address_copy, our_address, sizeof (*dst_address_copy));
380 hash_set_mem (lm->session_by_dst_address, dst_address_copy,
383 case L2T_LOOKUP_SESSION_ID:
384 hash_set (lm->session_by_session_id, local_session_id,
392 /* validate counters */
394 session_index_to_counter_index (s - lm->sessions,
395 SESSION_COUNTER_USER_TO_NETWORK);
396 vlib_validate_combined_counter (&lm->counter_main, counter_index);
397 vlib_validate_combined_counter (&lm->counter_main, counter_index + 1);
399 if (vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) > 0)
401 hw_if_index = lm->free_l2tpv3_tunnel_hw_if_indices
402 [vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) - 1];
403 _vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) -= 1;
405 hi = vnet_get_hw_interface (vnm, hw_if_index);
406 hi->dev_instance = s - lm->sessions;
407 hi->hw_instance = hi->dev_instance;
411 hw_if_index = vnet_register_interface
412 (vnm, l2tpv3_device_class.index, s - lm->sessions,
413 l2tpv3_hw_class.index, s - lm->sessions);
414 hi = vnet_get_hw_interface (vnm, hw_if_index);
415 hi->output_node_index = l2t_encap_node.index;
416 /* $$$$ initialize custom dispositions, if needed */
419 s->hw_if_index = hw_if_index;
420 s->sw_if_index = hi->sw_if_index;
423 *sw_if_index = hi->sw_if_index;
428 static clib_error_t *
429 create_l2tpv3_tunnel_command_fn (vlib_main_t * vm,
430 unformat_input_t * input,
431 vlib_cli_command_t * cmd)
433 ip6_address_t client_address, our_address;
434 unformat_input_t _line_input, *line_input = &_line_input;
435 l2t_main_t *lm = &l2t_main;
436 u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0;
437 u32 local_session_id = 1, remote_session_id = 1;
438 int our_address_set = 0, client_address_set = 0;
439 int l2_sublayer_present = 0;
442 u32 encap_fib_id = ~0;
443 u32 encap_fib_index = ~0;
445 /* Get a line of input. */
446 if (!unformat_user (input, unformat_line_input, line_input))
449 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
451 if (unformat (line_input, "client %U",
452 unformat_ip6_address, &client_address))
453 client_address_set = 1;
454 else if (unformat (line_input, "our %U",
455 unformat_ip6_address, &our_address))
457 else if (unformat (line_input, "local-cookie %llx", &local_cookie))
459 else if (unformat (line_input, "remote-cookie %llx", &remote_cookie))
461 else if (unformat (line_input, "local-session-id %d",
464 else if (unformat (line_input, "remote-session-id %d",
467 else if (unformat (line_input, "fib-id %d", &encap_fib_id))
469 else if (unformat (line_input, "l2-sublayer-present"))
470 l2_sublayer_present = 1;
472 return clib_error_return (0, "parse error: '%U'",
473 format_unformat_error, line_input);
476 unformat_free (line_input);
478 if (encap_fib_id != ~0)
481 ip6_main_t *im = &ip6_main;
482 if (!(p = hash_get (im->fib_index_by_table_id, encap_fib_id)))
483 return clib_error_return (0, "No fib with id %d", encap_fib_id);
484 encap_fib_index = p[0];
488 encap_fib_index = ~0;
491 if (our_address_set == 0)
492 return clib_error_return (0, "our address not specified");
493 if (client_address_set == 0)
494 return clib_error_return (0, "client address not specified");
496 rv = create_l2tpv3_ipv6_tunnel (lm, &client_address, &our_address,
497 local_session_id, remote_session_id,
498 local_cookie, remote_cookie,
500 encap_fib_index, &sw_if_index);
504 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
505 vnet_get_main (), sw_if_index);
507 case VNET_API_ERROR_INVALID_VALUE:
508 return clib_error_return (0, "session already exists...");
510 case VNET_API_ERROR_NO_SUCH_ENTRY:
511 return clib_error_return (0, "session does not exist...");
514 return clib_error_return (0, "l2tp_session_add_del returned %d", rv);
521 VLIB_CLI_COMMAND (create_l2tpv3_tunnel_command, static) =
523 .path = "create l2tpv3 tunnel",
525 "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,
531 l2tpv3_set_tunnel_cookies (l2t_main_t * lm,
533 u64 new_local_cookie, u64 new_remote_cookie)
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);
540 if (pool_is_free_index (lm->sessions, hi->dev_instance))
541 return VNET_API_ERROR_INVALID_VALUE;
543 s = pool_elt_at_index (lm->sessions, hi->dev_instance);
545 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);
553 static clib_error_t *
554 set_l2tp_tunnel_cookie_command_fn (vlib_main_t * vm,
555 unformat_input_t * input,
556 vlib_cli_command_t * cmd)
558 l2t_main_t *lm = &l2t_main;
559 vnet_main_t *vnm = vnet_get_main ();
560 u32 sw_if_index = ~0;
561 u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0;
565 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
567 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
570 else if (unformat (input, "local %llx", &local_cookie))
572 else if (unformat (input, "remote %llx", &remote_cookie))
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");
584 rv = l2tpv3_set_tunnel_cookies (lm, sw_if_index,
585 local_cookie, remote_cookie);
592 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
593 return clib_error_return (0, "invalid interface");
596 return clib_error_return (0, "l2tp_session_set_cookies returned %d",
604 VLIB_CLI_COMMAND (set_l2tp_tunnel_cookie_command, static) =
606 .path = "set l2tpv3 tunnel cookie",
608 "set l2tpv3 tunnel cookie <intfc> local <hex> remote <hex>",
609 .function = set_l2tp_tunnel_cookie_command_fn,
614 l2tpv3_interface_enable_disable (vnet_main_t * vnm,
615 u32 sw_if_index, int enable_disable)
617 ip6_main_t *im = &ip6_main;
618 ip_lookup_main_t *lm = &im->lookup_main;
619 ip_config_main_t *rx_cm = &lm->rx_config_mains[VNET_UNICAST];
621 ip6_l2tpv3_config_t config;
624 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
625 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
627 feature_index = im->ip6_unicast_rx_feature_l2tp_decap;
629 ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
631 ? vnet_config_add_feature
632 : vnet_config_del_feature)
633 (vlib_get_main (), &rx_cm->config_main,
634 ci, feature_index, &config, sizeof (config));
635 rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
639 /* Enable/disable L2TPv3 intercept on IP6 fowarding path */
640 static clib_error_t *
641 set_ip6_l2tpv3 (vlib_main_t * vm,
642 unformat_input_t * input, vlib_cli_command_t * cmd)
644 u32 sw_if_index = ~0;
647 vnet_main_t *vnm = vnet_get_main ();
649 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
651 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
654 else if (unformat (input, "del"))
660 if (sw_if_index == ~0)
661 return clib_error_return (0, "interface required");
663 rv = l2tpv3_interface_enable_disable (vnm, sw_if_index, is_add);
670 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
671 return clib_error_return (0, "invalid interface");
674 return clib_error_return (0,
675 "l2tp_interface_enable_disable returned %d",
682 VLIB_CLI_COMMAND (set_interface_ip6_l2tpv3, static) =
684 .path = "set interface ip6 l2tpv3",
685 .function = set_ip6_l2tpv3,
686 .short_help = "set interface ip6 l2tpv3 <intfc> [del]",
690 static clib_error_t *
691 l2tp_config (vlib_main_t * vm, unformat_input_t * input)
693 l2t_main_t *lm = &l2t_main;
695 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
697 if (unformat (input, "lookup-v6-src"))
698 lm->lookup_type = L2T_LOOKUP_SRC_ADDRESS;
699 else if (unformat (input, "lookup-v6-dst"))
700 lm->lookup_type = L2T_LOOKUP_DST_ADDRESS;
701 else if (unformat (input, "lookup-session-id"))
702 lm->lookup_type = L2T_LOOKUP_SESSION_ID;
704 return clib_error_return (0, "unknown input `%U'",
705 format_unformat_error, input);
710 VLIB_CONFIG_FUNCTION (l2tp_config, "l2tp");
714 l2tp_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
716 l2t_main_t *lm = &l2t_main;
717 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
718 if (hi->hw_class_index != l2tpv3_hw_class.index)
721 u32 session_index = hi->dev_instance;
722 l2t_session_t *s = pool_elt_at_index (lm->sessions, session_index);
723 s->admin_up = ! !(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
727 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2tp_sw_interface_up_down);
730 l2tp_init (vlib_main_t * vm)
732 l2t_main_t *lm = &l2t_main;
733 ip_main_t *im = &ip_main;
734 ip_protocol_info_t *pi;
736 lm->vnet_main = vnet_get_main ();
738 lm->lookup_type = L2T_LOOKUP_DST_ADDRESS;
740 lm->session_by_src_address = hash_create_mem
741 (0, sizeof (ip6_address_t) /* key bytes */ ,
742 sizeof (u32) /* value bytes */ );
743 lm->session_by_dst_address = hash_create_mem
744 (0, sizeof (ip6_address_t) /* key bytes */ ,
745 sizeof (u32) /* value bytes */ );
746 lm->session_by_session_id = hash_create (0, sizeof (uword));
748 pi = ip_get_protocol_info (im, IP_PROTOCOL_L2TP);
749 pi->unformat_pg_edit = unformat_pg_l2tp_header;
751 /* insure these nodes are included in build */
752 l2tp_encap_init (vm);
758 VLIB_INIT_FUNCTION (l2tp_init);
761 * fd.io coding-style-patch-verification: ON
764 * eval: (c-set-style "gnu")