Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | l2t_main_t l2t_main; |
| 27 | |
| 28 | /* packet trace format function */ |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 29 | u8 * |
| 30 | format_l2t_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | { |
| 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 *); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 34 | l2t_trace_t *t = va_arg (*args, l2t_trace_t *); |
| 35 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | if (t->is_user_to_network) |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | else |
| 41 | s = format (s, "L2T: %U (our) -> %U (client) session %d)", |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 42 | format_ip6_address, &t->our_address, |
| 43 | format_ip6_address, &t->client_address, t->session_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | return s; |
| 45 | } |
| 46 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 47 | u8 * |
| 48 | format_l2t_session (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 50 | l2t_session_t *session = va_arg (*args, l2t_session_t *); |
| 51 | l2t_main_t *lm = &l2t_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | u32 counter_index; |
| 53 | vlib_counter_t v; |
| 54 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | |
| 63 | s = format (s, " local cookies %016llx %016llx remote cookie %016llx\n", |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 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)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
| 68 | s = format (s, " local session-id %d remote session-id %d\n", |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 69 | clib_net_to_host_u32 (session->local_session_id), |
| 70 | clib_net_to_host_u32 (session->remote_session_id)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 72 | s = format (s, " l2 specific sublayer %s\n", |
| 73 | session->l2_sublayer_present ? "preset" : "absent"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 75 | counter_index = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | session_index_to_counter_index (session - lm->sessions, |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 77 | SESSION_COUNTER_USER_TO_NETWORK); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | |
| 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", |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 82 | v.packets, v.bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 84 | vlib_get_combined_counter (&lm->counter_main, counter_index + 1, &v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
| 86 | if (v.packets != 0) |
| 87 | s = format (s, " net-to-user: %llu pkts %llu bytes\n", |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 88 | v.packets, v.bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | return s; |
| 90 | } |
| 91 | |
| 92 | static clib_error_t * |
| 93 | show_l2tp_command_fn (vlib_main_t * vm, |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 94 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | { |
| 96 | l2t_session_t *session; |
| 97 | l2t_main_t *lm = &l2t_main; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 98 | char *keystr = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | int verbose = 0; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 100 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | if (unformat (input, "verbose") || unformat (input, "v")) |
| 102 | verbose = 1; |
| 103 | |
| 104 | if (pool_elts (lm->sessions) == 0) |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 105 | vlib_cli_output (vm, "No l2tp sessions..."); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | else |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 107 | vlib_cli_output (vm, "%u l2tp sessions...", pool_elts (lm->sessions)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | |
| 109 | if (verbose) |
| 110 | { |
| 111 | switch (lm->lookup_type) |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 112 | { |
| 113 | case L2T_LOOKUP_SRC_ADDRESS: |
| 114 | keystr = "src address"; |
| 115 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 117 | case L2T_LOOKUP_DST_ADDRESS: |
| 118 | keystr = "dst address"; |
| 119 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 121 | case L2T_LOOKUP_SESSION_ID: |
| 122 | keystr = "session id"; |
| 123 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 125 | default: |
| 126 | keystr = "BOGUS!"; |
| 127 | break; |
| 128 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
| 130 | vlib_cli_output (vm, "L2tp session lookup on %s", keystr); |
| 131 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 132 | /* *INDENT-OFF* */ |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 133 | pool_foreach (session, lm->sessions, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | ({ |
| 135 | vlib_cli_output (vm, "%U", format_l2t_session, session); |
| 136 | })); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 137 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | } |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 139 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 143 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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, |
| 148 | }; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 149 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | |
| 151 | static clib_error_t * |
| 152 | test_counters_command_fn (vlib_main_t * vm, |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 153 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | { |
| 155 | l2t_session_t *session; |
| 156 | l2t_main_t *lm = &l2t_main; |
| 157 | u32 session_index; |
| 158 | u32 counter_index; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 159 | u32 nincr = 0; |
Damjan Marion | 067cd62 | 2018-07-11 12:47:43 +0200 | [diff] [blame] | 160 | u32 thread_index = vm->thread_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 162 | /* *INDENT-OFF* */ |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 163 | pool_foreach (session, lm->sessions, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | ({ |
| 165 | session_index = session - lm->sessions; |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 166 | counter_index = |
| 167 | session_index_to_counter_index (session_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | SESSION_COUNTER_USER_TO_NETWORK); |
| 169 | vlib_increment_combined_counter (&lm->counter_main, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 170 | thread_index, |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 171 | counter_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | 1/*pkt*/, 1111 /*bytes*/); |
| 173 | vlib_increment_combined_counter (&lm->counter_main, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 174 | thread_index, |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 175 | counter_index+1, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | 1/*pkt*/, 2222 /*bytes*/); |
| 177 | nincr++; |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 178 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | })); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 180 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | vlib_cli_output (vm, "Incremented %d active counters\n", nincr); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 182 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 186 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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, |
| 191 | }; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 192 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | |
| 194 | static clib_error_t * |
| 195 | clear_counters_command_fn (vlib_main_t * vm, |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 196 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | { |
| 198 | l2t_session_t *session; |
| 199 | l2t_main_t *lm = &l2t_main; |
| 200 | u32 session_index; |
| 201 | u32 counter_index; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 202 | u32 nincr = 0; |
| 203 | |
| 204 | /* *INDENT-OFF* */ |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 205 | pool_foreach (session, lm->sessions, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | ({ |
| 207 | session_index = session - lm->sessions; |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 208 | counter_index = |
| 209 | session_index_to_counter_index (session_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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); |
| 213 | nincr++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | })); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 215 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | vlib_cli_output (vm, "Cleared %d active counters\n", nincr); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 217 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 221 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | VLIB_CLI_COMMAND (clear_counters_command, static) = { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 223 | .path = "clear counters", |
| 224 | .short_help = "clear all active counters", |
| 225 | .function = clear_counters_command_fn, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | }; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 227 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 229 | static u8 * |
| 230 | format_l2tpv3_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | { |
| 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 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 245 | static int |
| 246 | l2tpv3_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | { |
| 248 | l2t_main_t *lm = &l2t_main; |
| 249 | |
| 250 | vec_validate_init_empty (lm->dev_inst_by_real, hi->dev_instance, ~0); |
| 251 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 252 | lm->dev_inst_by_real[hi->dev_instance] = new_dev_instance; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 257 | static uword |
| 258 | dummy_interface_tx (vlib_main_t * vm, |
| 259 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | { |
| 261 | clib_warning ("you shouldn't be here, leaking buffers..."); |
| 262 | return frame->n_vectors; |
| 263 | } |
| 264 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 265 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | VNET_DEVICE_CLASS (l2tpv3_device_class,static) = { |
| 267 | .name = "L2TPv3", |
| 268 | .format_device_name = format_l2tpv3_name, |
| 269 | .name_renumber = l2tpv3_name_renumber, |
| 270 | .tx_function = dummy_interface_tx, |
| 271 | }; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 272 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 274 | static u8 * |
| 275 | format_l2tp_header_with_length (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | { |
| 277 | u32 dev_instance = va_arg (*args, u32); |
| 278 | s = format (s, "unimplemented dev %u", dev_instance); |
| 279 | return s; |
| 280 | } |
| 281 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 282 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | VNET_HW_INTERFACE_CLASS (l2tpv3_hw_class) = { |
| 284 | .name = "L2TPV3", |
| 285 | .format_header = format_l2tp_header_with_length, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 286 | .build_rewrite = default_build_rewrite, |
| 287 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | }; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 289 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 290 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 291 | int |
| 292 | 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, |
| 297 | u64 local_cookie, |
| 298 | u64 remote_cookie, |
| 299 | int l2_sublayer_present, |
| 300 | u32 encap_fib_index, u32 * sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | { |
| 302 | l2t_session_t *s = 0; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 303 | vnet_main_t *vnm = lm->vnet_main; |
| 304 | vnet_hw_interface_t *hi; |
| 305 | uword *p = (uword *) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | u32 hw_if_index; |
| 307 | l2tpv3_header_t l2tp_hdr; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 308 | ip6_address_t *dst_address_copy, *src_address_copy; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | u32 counter_index; |
| 310 | |
| 311 | remote_session_id = clib_host_to_net_u32 (remote_session_id); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 312 | local_session_id = clib_host_to_net_u32 (local_session_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 314 | switch (lm->lookup_type) |
| 315 | { |
| 316 | case L2T_LOOKUP_SRC_ADDRESS: |
| 317 | p = hash_get_mem (lm->session_by_src_address, client_address); |
| 318 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 320 | case L2T_LOOKUP_DST_ADDRESS: |
| 321 | p = hash_get_mem (lm->session_by_dst_address, our_address); |
| 322 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 324 | case L2T_LOOKUP_SESSION_ID: |
| 325 | p = hash_get (lm->session_by_session_id, local_session_id); |
| 326 | break; |
| 327 | |
| 328 | default: |
| 329 | ASSERT (0); |
| 330 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | |
| 332 | /* adding a session: session must not already exist */ |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 333 | if (p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | return VNET_API_ERROR_INVALID_VALUE; |
| 335 | |
| 336 | pool_get (lm->sessions, s); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 337 | clib_memset (s, 0, sizeof (*s)); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 338 | clib_memcpy (&s->our_address, our_address, sizeof (s->our_address)); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 339 | clib_memcpy (&s->client_address, client_address, |
| 340 | sizeof (s->client_address)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | s->local_cookie[0] = clib_host_to_net_u64 (local_cookie); |
| 342 | s->remote_cookie = clib_host_to_net_u64 (remote_cookie); |
| 343 | s->local_session_id = local_session_id; |
| 344 | s->remote_session_id = remote_session_id; |
| 345 | s->l2_sublayer_present = l2_sublayer_present; |
| 346 | /* precompute l2tp header size */ |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 347 | s->l2tp_hdr_size = l2_sublayer_present ? |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | sizeof (l2tpv3_header_t) : |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 349 | sizeof (l2tpv3_header_t) - sizeof (l2tp_hdr.l2_specific_sublayer); |
Pierre Pfister | 80ee213 | 2016-06-22 12:54:48 +0100 | [diff] [blame] | 350 | s->admin_up = 0; |
Pierre Pfister | 08e0312 | 2016-07-15 09:19:39 +0100 | [diff] [blame] | 351 | s->encap_fib_index = encap_fib_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | |
| 353 | /* Setup hash table entries */ |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 354 | switch (lm->lookup_type) |
| 355 | { |
| 356 | case L2T_LOOKUP_SRC_ADDRESS: |
| 357 | src_address_copy = clib_mem_alloc (sizeof (*src_address_copy)); |
| 358 | clib_memcpy (src_address_copy, client_address, |
| 359 | sizeof (*src_address_copy)); |
| 360 | hash_set_mem (lm->session_by_src_address, src_address_copy, |
| 361 | s - lm->sessions); |
| 362 | break; |
| 363 | case L2T_LOOKUP_DST_ADDRESS: |
| 364 | dst_address_copy = clib_mem_alloc (sizeof (*dst_address_copy)); |
| 365 | clib_memcpy (dst_address_copy, our_address, sizeof (*dst_address_copy)); |
| 366 | hash_set_mem (lm->session_by_dst_address, dst_address_copy, |
| 367 | s - lm->sessions); |
| 368 | break; |
| 369 | case L2T_LOOKUP_SESSION_ID: |
| 370 | hash_set (lm->session_by_session_id, local_session_id, |
| 371 | s - lm->sessions); |
| 372 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 374 | default: |
| 375 | ASSERT (0); |
| 376 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 377 | |
| 378 | /* validate counters */ |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 379 | counter_index = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | session_index_to_counter_index (s - lm->sessions, |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 381 | SESSION_COUNTER_USER_TO_NETWORK); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | vlib_validate_combined_counter (&lm->counter_main, counter_index); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 383 | vlib_validate_combined_counter (&lm->counter_main, counter_index + 1); |
| 384 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | if (vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) > 0) |
| 386 | { |
| 387 | hw_if_index = lm->free_l2tpv3_tunnel_hw_if_indices |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 388 | [vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) - 1]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | _vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) -= 1; |
| 390 | |
| 391 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 392 | hi->dev_instance = s - lm->sessions; |
| 393 | hi->hw_instance = hi->dev_instance; |
| 394 | } |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 395 | else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | { |
| 397 | hw_if_index = vnet_register_interface |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 398 | (vnm, l2tpv3_device_class.index, s - lm->sessions, |
| 399 | l2tpv3_hw_class.index, s - lm->sessions); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 400 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 401 | hi->output_node_index = l2t_encap_node.index; |
| 402 | /* $$$$ initialize custom dispositions, if needed */ |
| 403 | } |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 404 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | s->hw_if_index = hw_if_index; |
| 406 | s->sw_if_index = hi->sw_if_index; |
| 407 | |
| 408 | if (sw_if_index) |
| 409 | *sw_if_index = hi->sw_if_index; |
| 410 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static clib_error_t * |
| 415 | create_l2tpv3_tunnel_command_fn (vlib_main_t * vm, |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 416 | unformat_input_t * input, |
| 417 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | { |
| 419 | ip6_address_t client_address, our_address; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 420 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | l2t_main_t *lm = &l2t_main; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 422 | u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | u32 local_session_id = 1, remote_session_id = 1; |
| 424 | int our_address_set = 0, client_address_set = 0; |
| 425 | int l2_sublayer_present = 0; |
| 426 | int rv; |
| 427 | u32 sw_if_index; |
Pierre Pfister | 08e0312 | 2016-07-15 09:19:39 +0100 | [diff] [blame] | 428 | u32 encap_fib_id = ~0; |
| 429 | u32 encap_fib_index = ~0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 430 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | |
| 432 | /* Get a line of input. */ |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 433 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | return 0; |
| 435 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 436 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 437 | { |
| 438 | if (unformat (line_input, "client %U", |
| 439 | unformat_ip6_address, &client_address)) |
| 440 | client_address_set = 1; |
| 441 | else if (unformat (line_input, "our %U", |
| 442 | unformat_ip6_address, &our_address)) |
| 443 | our_address_set = 1; |
| 444 | else if (unformat (line_input, "local-cookie %llx", &local_cookie)) |
| 445 | ; |
| 446 | else if (unformat (line_input, "remote-cookie %llx", &remote_cookie)) |
| 447 | ; |
| 448 | else if (unformat (line_input, "local-session-id %d", |
| 449 | &local_session_id)) |
| 450 | ; |
| 451 | else if (unformat (line_input, "remote-session-id %d", |
| 452 | &remote_session_id)) |
| 453 | ; |
| 454 | else if (unformat (line_input, "fib-id %d", &encap_fib_id)) |
| 455 | ; |
| 456 | else if (unformat (line_input, "l2-sublayer-present")) |
| 457 | l2_sublayer_present = 1; |
| 458 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 459 | { |
| 460 | error = clib_error_return (0, "parse error: '%U'", |
| 461 | format_unformat_error, line_input); |
| 462 | goto done; |
| 463 | } |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 464 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 465 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 466 | if (encap_fib_id != ~0) |
| 467 | { |
Pierre Pfister | 08e0312 | 2016-07-15 09:19:39 +0100 | [diff] [blame] | 468 | uword *p; |
| 469 | ip6_main_t *im = &ip6_main; |
| 470 | if (!(p = hash_get (im->fib_index_by_table_id, encap_fib_id))) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 471 | { |
| 472 | error = clib_error_return (0, "No fib with id %d", encap_fib_id); |
| 473 | goto done; |
| 474 | } |
Pierre Pfister | 08e0312 | 2016-07-15 09:19:39 +0100 | [diff] [blame] | 475 | encap_fib_index = p[0]; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 476 | } |
| 477 | else |
| 478 | { |
Pierre Pfister | 08e0312 | 2016-07-15 09:19:39 +0100 | [diff] [blame] | 479 | encap_fib_index = ~0; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 480 | } |
Pierre Pfister | 08e0312 | 2016-07-15 09:19:39 +0100 | [diff] [blame] | 481 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 482 | if (our_address_set == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 483 | { |
| 484 | error = clib_error_return (0, "our address not specified"); |
| 485 | goto done; |
| 486 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | if (client_address_set == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 488 | { |
| 489 | error = clib_error_return (0, "client address not specified"); |
| 490 | goto done; |
| 491 | } |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 492 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | rv = create_l2tpv3_ipv6_tunnel (lm, &client_address, &our_address, |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 494 | local_session_id, remote_session_id, |
| 495 | local_cookie, remote_cookie, |
| 496 | l2_sublayer_present, |
| 497 | encap_fib_index, &sw_if_index); |
| 498 | switch (rv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 499 | { |
| 500 | case 0: |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 501 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, |
| 502 | vnet_get_main (), sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | break; |
| 504 | case VNET_API_ERROR_INVALID_VALUE: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 505 | error = clib_error_return (0, "session already exists..."); |
| 506 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | |
| 508 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 509 | error = clib_error_return (0, "session does not exist..."); |
| 510 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | |
| 512 | default: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 513 | error = clib_error_return (0, "l2tp_session_add_del returned %d", rv); |
| 514 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 517 | done: |
| 518 | unformat_free (line_input); |
| 519 | |
| 520 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 523 | /* *INDENT-OFF* */ |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 524 | VLIB_CLI_COMMAND (create_l2tpv3_tunnel_command, static) = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 526 | .path = "create l2tpv3 tunnel", |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 527 | .short_help = |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 528 | "create l2tpv3 tunnel client <ip6> our <ip6> local-cookie <hex> remote-cookie <hex> local-session <dec> remote-session <dec>", |
| 529 | .function = create_l2tpv3_tunnel_command_fn, |
| 530 | }; |
| 531 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 533 | int |
| 534 | l2tpv3_set_tunnel_cookies (l2t_main_t * lm, |
| 535 | u32 sw_if_index, |
| 536 | u64 new_local_cookie, u64 new_remote_cookie) |
| 537 | { |
| 538 | l2t_session_t *s; |
| 539 | vnet_hw_interface_t *hi; |
| 540 | vnet_main_t *vnm = vnet_get_main (); |
| 541 | hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 543 | if (pool_is_free_index (lm->sessions, hi->dev_instance)) |
| 544 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 545 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 546 | s = pool_elt_at_index (lm->sessions, hi->dev_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 548 | s->local_cookie[1] = s->local_cookie[0]; |
| 549 | s->local_cookie[0] = clib_host_to_net_u64 (new_local_cookie); |
| 550 | s->remote_cookie = clib_host_to_net_u64 (new_remote_cookie); |
| 551 | |
| 552 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | |
| 556 | static clib_error_t * |
| 557 | set_l2tp_tunnel_cookie_command_fn (vlib_main_t * vm, |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 558 | unformat_input_t * input, |
| 559 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 560 | { |
| 561 | l2t_main_t *lm = &l2t_main; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 562 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 563 | u32 sw_if_index = ~0; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 564 | u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 565 | |
| 566 | int rv; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 567 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 568 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 569 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 570 | if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, |
| 571 | &sw_if_index)) |
| 572 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | else if (unformat (input, "local %llx", &local_cookie)) |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 574 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 575 | else if (unformat (input, "remote %llx", &remote_cookie)) |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 576 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 577 | else |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 578 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 579 | } |
| 580 | if (sw_if_index == ~0) |
| 581 | return clib_error_return (0, "unknown interface"); |
| 582 | if (local_cookie == ~0) |
| 583 | return clib_error_return (0, "local cookie required"); |
| 584 | if (remote_cookie == ~0) |
| 585 | return clib_error_return (0, "remote cookie required"); |
| 586 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 587 | rv = l2tpv3_set_tunnel_cookies (lm, sw_if_index, |
| 588 | local_cookie, remote_cookie); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 589 | |
| 590 | switch (rv) |
| 591 | { |
| 592 | case 0: |
| 593 | break; |
| 594 | |
| 595 | case VNET_API_ERROR_INVALID_SW_IF_INDEX: |
| 596 | return clib_error_return (0, "invalid interface"); |
| 597 | |
| 598 | default: |
| 599 | return clib_error_return (0, "l2tp_session_set_cookies returned %d", |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 600 | rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 603 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 606 | /* *INDENT-OFF* */ |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 607 | VLIB_CLI_COMMAND (set_l2tp_tunnel_cookie_command, static) = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 608 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 609 | .path = "set l2tpv3 tunnel cookie", |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 610 | .short_help = |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 611 | "set l2tpv3 tunnel cookie <intfc> local <hex> remote <hex>", |
| 612 | .function = set_l2tp_tunnel_cookie_command_fn, |
| 613 | }; |
| 614 | /* *INDENT-ON* */ |
| 615 | |
| 616 | int |
| 617 | l2tpv3_interface_enable_disable (vnet_main_t * vnm, |
| 618 | u32 sw_if_index, int enable_disable) |
| 619 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 620 | |
| 621 | if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) |
| 622 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 623 | |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 624 | vnet_feature_enable_disable ("ip6-unicast", "l2tp-decap", sw_if_index, |
| 625 | enable_disable, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 629 | /* Enable/disable L2TPv3 intercept on IP6 forwarding path */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 630 | static clib_error_t * |
| 631 | set_ip6_l2tpv3 (vlib_main_t * vm, |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 632 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | { |
| 634 | u32 sw_if_index = ~0; |
| 635 | int is_add = 1; |
| 636 | int rv; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 637 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 638 | |
| 639 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 640 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 641 | if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, |
| 642 | &sw_if_index)) |
| 643 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 644 | else if (unformat (input, "del")) |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 645 | is_add = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 646 | else |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 647 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | if (sw_if_index == ~0) |
| 651 | return clib_error_return (0, "interface required"); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 652 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 653 | rv = l2tpv3_interface_enable_disable (vnm, sw_if_index, is_add); |
| 654 | |
| 655 | switch (rv) |
| 656 | { |
| 657 | case 0: |
| 658 | break; |
| 659 | |
| 660 | case VNET_API_ERROR_INVALID_SW_IF_INDEX: |
| 661 | return clib_error_return (0, "invalid interface"); |
| 662 | |
| 663 | default: |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 664 | return clib_error_return (0, |
| 665 | "l2tp_interface_enable_disable returned %d", |
| 666 | rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 667 | } |
| 668 | return 0; |
| 669 | } |
| 670 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 671 | /* *INDENT-OFF* */ |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 672 | VLIB_CLI_COMMAND (set_interface_ip6_l2tpv3, static) = |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 673 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 674 | .path = "set interface ip6 l2tpv3", |
| 675 | .function = set_ip6_l2tpv3, |
| 676 | .short_help = "set interface ip6 l2tpv3 <intfc> [del]", |
| 677 | }; |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 678 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 679 | |
| 680 | static clib_error_t * |
| 681 | l2tp_config (vlib_main_t * vm, unformat_input_t * input) |
| 682 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 683 | l2t_main_t *lm = &l2t_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 684 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 685 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 686 | { |
| 687 | if (unformat (input, "lookup-v6-src")) |
| 688 | lm->lookup_type = L2T_LOOKUP_SRC_ADDRESS; |
| 689 | else if (unformat (input, "lookup-v6-dst")) |
| 690 | lm->lookup_type = L2T_LOOKUP_DST_ADDRESS; |
| 691 | else if (unformat (input, "lookup-session-id")) |
| 692 | lm->lookup_type = L2T_LOOKUP_SESSION_ID; |
| 693 | else |
| 694 | return clib_error_return (0, "unknown input `%U'", |
| 695 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 696 | } |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 697 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | VLIB_CONFIG_FUNCTION (l2tp_config, "l2tp"); |
| 701 | |
Pierre Pfister | 80ee213 | 2016-06-22 12:54:48 +0100 | [diff] [blame] | 702 | |
| 703 | clib_error_t * |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 704 | l2tp_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags) |
Pierre Pfister | 80ee213 | 2016-06-22 12:54:48 +0100 | [diff] [blame] | 705 | { |
| 706 | l2t_main_t *lm = &l2t_main; |
| 707 | vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 708 | if (hi->hw_class_index != l2tpv3_hw_class.index) |
| 709 | return 0; |
| 710 | |
| 711 | u32 session_index = hi->dev_instance; |
| 712 | l2t_session_t *s = pool_elt_at_index (lm->sessions, session_index); |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 713 | s->admin_up = ! !(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP); |
Pierre Pfister | 80ee213 | 2016-06-22 12:54:48 +0100 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2tp_sw_interface_up_down); |
| 718 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 719 | clib_error_t * |
| 720 | l2tp_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 721 | { |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 722 | l2t_main_t *lm = &l2t_main; |
| 723 | ip_main_t *im = &ip_main; |
| 724 | ip_protocol_info_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 725 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 726 | lm->vnet_main = vnet_get_main (); |
| 727 | lm->vlib_main = vm; |
| 728 | lm->lookup_type = L2T_LOOKUP_DST_ADDRESS; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 729 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 730 | lm->session_by_src_address = hash_create_mem |
| 731 | (0, sizeof (ip6_address_t) /* key bytes */ , |
| 732 | sizeof (u32) /* value bytes */ ); |
| 733 | lm->session_by_dst_address = hash_create_mem |
| 734 | (0, sizeof (ip6_address_t) /* key bytes */ , |
| 735 | sizeof (u32) /* value bytes */ ); |
| 736 | lm->session_by_session_id = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 737 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 738 | pi = ip_get_protocol_info (im, IP_PROTOCOL_L2TP); |
| 739 | pi->unformat_pg_edit = unformat_pg_l2tp_header; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 741 | /* insure these nodes are included in build */ |
| 742 | l2tp_encap_init (vm); |
| 743 | l2tp_decap_init (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 744 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 745 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 748 | VLIB_INIT_FUNCTION (l2tp_init); |
| 749 | |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 750 | clib_error_t * |
| 751 | l2tp_worker_init (vlib_main_t * vm) |
| 752 | { |
| 753 | l2tp_encap_init (vm); |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | VLIB_WORKER_INIT_FUNCTION (l2tp_worker_init); |
| 759 | |
Calvin | ee275a7 | 2016-08-10 11:01:41 -0400 | [diff] [blame] | 760 | /* |
| 761 | * fd.io coding-style-patch-verification: ON |
| 762 | * |
| 763 | * Local Variables: |
| 764 | * eval: (c-set-style "gnu") |
| 765 | * End: |
| 766 | */ |