Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * gre_interface.c: gre interfaces |
| 3 | * |
| 4 | * Copyright (c) 2016 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 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 18 | * @file |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 19 | * @brief L2-GRE over IPSec tunnel interface. |
| 20 | * |
| 21 | * Creates ipsec-gre tunnel interface. |
| 22 | * Provides a command line interface so humans can interact with VPP. |
| 23 | */ |
| 24 | |
| 25 | #include <vnet/vnet.h> |
| 26 | #include <vnet/pg/pg.h> |
| 27 | #include <vnet/ipsec-gre/ipsec_gre.h> |
| 28 | #include <vnet/ip/format.h> |
| 29 | #include <vnet/ipsec/ipsec.h> |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 30 | #include <vnet/l2/l2_input.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 31 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 32 | #include <vnet/ipsec/esp.h> |
| 33 | |
| 34 | u8 * |
| 35 | format_ipsec_gre_tunnel (u8 * s, va_list * args) |
| 36 | { |
| 37 | ipsec_gre_tunnel_t *t = va_arg (*args, ipsec_gre_tunnel_t *); |
| 38 | ipsec_gre_main_t *gm = &ipsec_gre_main; |
| 39 | |
| 40 | s = format (s, |
| 41 | "[%d] %U (src) %U (dst) local-sa %d remote-sa %d", |
| 42 | t - gm->tunnels, |
| 43 | format_ip4_address, &t->tunnel_src, |
| 44 | format_ip4_address, &t->tunnel_dst, |
| 45 | t->local_sa_id, t->remote_sa_id); |
| 46 | return s; |
| 47 | } |
| 48 | |
| 49 | static clib_error_t * |
| 50 | show_ipsec_gre_tunnel_command_fn (vlib_main_t * vm, |
| 51 | unformat_input_t * input, |
| 52 | vlib_cli_command_t * cmd) |
| 53 | { |
| 54 | ipsec_gre_main_t *igm = &ipsec_gre_main; |
| 55 | ipsec_gre_tunnel_t *t; |
| 56 | |
| 57 | if (pool_elts (igm->tunnels) == 0) |
| 58 | vlib_cli_output (vm, "No IPSec GRE tunnels configured..."); |
| 59 | |
| 60 | /* *INDENT-OFF* */ |
| 61 | pool_foreach (t, igm->tunnels, |
| 62 | ({ |
| 63 | vlib_cli_output (vm, "%U", format_ipsec_gre_tunnel, t); |
| 64 | })); |
| 65 | /* *INDENT-ON* */ |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /* *INDENT-OFF* */ |
| 71 | VLIB_CLI_COMMAND (show_ipsec_gre_tunnel_command, static) = { |
| 72 | .path = "show ipsec gre tunnel", |
| 73 | .function = show_ipsec_gre_tunnel_command_fn, |
| 74 | }; |
| 75 | /* *INDENT-ON* */ |
| 76 | |
| 77 | /* force inclusion from application's main.c */ |
| 78 | clib_error_t * |
| 79 | ipsec_gre_interface_init (vlib_main_t * vm) |
| 80 | { |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | VLIB_INIT_FUNCTION (ipsec_gre_interface_init); |
| 85 | |
| 86 | /** |
| 87 | * @brief Add or delete ipsec-gre tunnel interface. |
| 88 | * |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 89 | * @param *a vnet_ipsec_gre_tunnel_add_del_args_t - tunnel interface parameters |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 90 | * @param *sw_if_indexp u32 - software interface index |
| 91 | * @return int - 0 if success otherwise <code>VNET_API_ERROR_</code> |
| 92 | */ |
| 93 | int |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 94 | vnet_ipsec_gre_tunnel_add_del (const ipsec_gre_tunnel_add_del_args_t * a, |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 95 | u32 * sw_if_indexp) |
| 96 | { |
| 97 | ipsec_gre_main_t *igm = &ipsec_gre_main; |
| 98 | vnet_main_t *vnm = igm->vnet_main; |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 99 | ipsec_main_t *im = &ipsec_main; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 100 | ipsec_gre_tunnel_t *t; |
| 101 | vnet_hw_interface_t *hi; |
| 102 | u32 hw_if_index, sw_if_index; |
| 103 | u32 slot; |
| 104 | uword *p; |
| 105 | u64 key; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 106 | |
| 107 | key = (u64) a->src.as_u32 << 32 | (u64) a->dst.as_u32; |
| 108 | p = hash_get (igm->tunnel_by_key, key); |
| 109 | |
| 110 | if (a->is_add) |
| 111 | { |
| 112 | /* check if same src/dst pair exists */ |
| 113 | if (p) |
| 114 | return VNET_API_ERROR_INVALID_VALUE; |
| 115 | |
| 116 | pool_get_aligned (igm->tunnels, t, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 117 | clib_memset (t, 0, sizeof (*t)); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 118 | |
| 119 | if (vec_len (igm->free_ipsec_gre_tunnel_hw_if_indices) > 0) |
| 120 | { |
| 121 | vnet_interface_main_t *im = &vnm->interface_main; |
| 122 | |
| 123 | hw_if_index = igm->free_ipsec_gre_tunnel_hw_if_indices |
| 124 | [vec_len (igm->free_ipsec_gre_tunnel_hw_if_indices) - 1]; |
| 125 | _vec_len (igm->free_ipsec_gre_tunnel_hw_if_indices) -= 1; |
| 126 | |
| 127 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 128 | hi->dev_instance = t - igm->tunnels; |
| 129 | hi->hw_instance = hi->dev_instance; |
| 130 | |
| 131 | /* clear old stats of freed tunnel before reuse */ |
| 132 | sw_if_index = hi->sw_if_index; |
| 133 | vnet_interface_counter_lock (im); |
| 134 | vlib_zero_combined_counter |
| 135 | (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], |
| 136 | sw_if_index); |
| 137 | vlib_zero_combined_counter |
| 138 | (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], |
| 139 | sw_if_index); |
| 140 | vlib_zero_simple_counter |
| 141 | (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index); |
| 142 | vnet_interface_counter_unlock (im); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | hw_if_index = vnet_register_interface |
| 147 | (vnm, ipsec_gre_device_class.index, t - igm->tunnels, |
| 148 | ipsec_gre_hw_interface_class.index, t - igm->tunnels); |
| 149 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 150 | sw_if_index = hi->sw_if_index; |
| 151 | } |
| 152 | |
| 153 | t->hw_if_index = hw_if_index; |
| 154 | t->sw_if_index = sw_if_index; |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 155 | t->local_sa_id = a->local_sa_id; |
| 156 | t->remote_sa_id = a->remote_sa_id; |
| 157 | t->local_sa = ipsec_get_sa_index_by_sa_id (t->local_sa_id); |
| 158 | t->remote_sa = ipsec_get_sa_index_by_sa_id (t->remote_sa_id); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 159 | |
Matus Fabian | 025943b | 2016-10-07 03:29:09 -0700 | [diff] [blame] | 160 | ip4_sw_interface_enable_disable (sw_if_index, 1); |
| 161 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 162 | vec_validate_init_empty (igm->tunnel_index_by_sw_if_index, |
| 163 | sw_if_index, ~0); |
| 164 | igm->tunnel_index_by_sw_if_index[sw_if_index] = t - igm->tunnels; |
| 165 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 166 | hi->min_packet_bytes = 64 + sizeof (gre_header_t) + |
| 167 | sizeof (ip4_header_t) + sizeof (esp_header_t) + sizeof (esp_footer_t); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 168 | |
| 169 | /* Standard default gre MTU. */ |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 170 | /* TODO: Should take tunnel overhead into consideration */ |
| 171 | vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 172 | |
| 173 | clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src)); |
| 174 | clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst)); |
| 175 | |
| 176 | hash_set (igm->tunnel_by_key, key, t - igm->tunnels); |
| 177 | |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 178 | slot = vlib_node_add_next_with_slot |
| 179 | (vnm->vlib_main, hi->tx_node_index, im->esp4_encrypt_node_index, |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 180 | IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT); |
| 181 | |
| 182 | ASSERT (slot == IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 183 | } |
| 184 | else |
| 185 | { /* !is_add => delete */ |
| 186 | /* tunnel needs to exist */ |
| 187 | if (!p) |
| 188 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 189 | |
| 190 | t = pool_elt_at_index (igm->tunnels, p[0]); |
| 191 | |
| 192 | sw_if_index = t->sw_if_index; |
Matus Fabian | 025943b | 2016-10-07 03:29:09 -0700 | [diff] [blame] | 193 | ip4_sw_interface_enable_disable (sw_if_index, 0); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 194 | vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ ); |
| 195 | /* make sure tunnel is removed from l2 bd or xconnect */ |
Neale Ranns | b474380 | 2018-09-05 09:13:57 -0700 | [diff] [blame] | 196 | set_int_l2_mode (igm->vlib_main, vnm, MODE_L3, sw_if_index, 0, |
| 197 | L2_BD_PORT_TYPE_NORMAL, 0, 0); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 198 | vec_add1 (igm->free_ipsec_gre_tunnel_hw_if_indices, t->hw_if_index); |
| 199 | igm->tunnel_index_by_sw_if_index[sw_if_index] = ~0; |
| 200 | |
| 201 | hash_unset (igm->tunnel_by_key, key); |
| 202 | pool_put (igm->tunnels, t); |
| 203 | } |
| 204 | |
| 205 | if (sw_if_indexp) |
| 206 | *sw_if_indexp = sw_if_index; |
| 207 | |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 208 | return ipsec_add_del_ipsec_gre_tunnel (vnm, a); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static clib_error_t * |
| 212 | create_ipsec_gre_tunnel_command_fn (vlib_main_t * vm, |
| 213 | unformat_input_t * input, |
| 214 | vlib_cli_command_t * cmd) |
| 215 | { |
| 216 | unformat_input_t _line_input, *line_input = &_line_input; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 217 | u32 num_m_args = 0; |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 218 | ipsec_gre_tunnel_add_del_args_t _a, *a = &_a; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 219 | int rv; |
| 220 | u32 sw_if_index; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 221 | clib_error_t *error = NULL; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 222 | |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 223 | clib_memset (a, 0, sizeof (*a)); |
| 224 | a->is_add = 1; |
| 225 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 226 | /* Get a line of input. */ |
| 227 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 228 | return 0; |
| 229 | |
| 230 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 231 | { |
| 232 | if (unformat (line_input, "del")) |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 233 | a->is_add = 0; |
| 234 | else if (unformat (line_input, "src %U", unformat_ip4_address, &a->src)) |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 235 | num_m_args++; |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 236 | else if (unformat (line_input, "dst %U", unformat_ip4_address, &a->dst)) |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 237 | num_m_args++; |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 238 | else if (unformat (line_input, "local-sa %d", &a->local_sa_id)) |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 239 | num_m_args++; |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 240 | else if (unformat (line_input, "remote-sa %d", &a->remote_sa_id)) |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 241 | num_m_args++; |
| 242 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 243 | { |
| 244 | error = clib_error_return (0, "unknown input `%U'", |
| 245 | format_unformat_error, line_input); |
| 246 | goto done; |
| 247 | } |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 248 | } |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 249 | |
| 250 | if (num_m_args < 4) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 251 | { |
| 252 | error = clib_error_return (0, "mandatory argument(s) missing"); |
| 253 | goto done; |
| 254 | } |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 255 | |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 256 | if (memcmp (&a->src, &a->dst, sizeof (a->src)) == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 257 | { |
| 258 | error = clib_error_return (0, "src and dst are identical"); |
| 259 | goto done; |
| 260 | } |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 261 | |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 262 | rv = vnet_ipsec_gre_tunnel_add_del (a, &sw_if_index); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 263 | |
| 264 | switch (rv) |
| 265 | { |
| 266 | case 0: |
| 267 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, |
| 268 | vnet_get_main (), sw_if_index); |
| 269 | break; |
| 270 | case VNET_API_ERROR_INVALID_VALUE: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 271 | error = clib_error_return (0, "GRE tunnel already exists..."); |
| 272 | goto done; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 273 | default: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 274 | error = clib_error_return (0, |
Neale Ranns | e524d45 | 2019-02-19 15:22:46 +0000 | [diff] [blame] | 275 | "vnet_ipsec_gre_tunnel_add_del returned %d", |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 276 | rv); |
| 277 | goto done; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 280 | done: |
| 281 | unformat_free (line_input); |
| 282 | |
| 283 | return error; |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* *INDENT-OFF* */ |
| 287 | VLIB_CLI_COMMAND (create_ipsec_gre_tunnel_command, static) = { |
| 288 | .path = "create ipsec gre tunnel", |
| 289 | .short_help = "create ipsec gre tunnel src <addr> dst <addr> " |
| 290 | "local-sa <id> remote-sa <id> [del]", |
| 291 | .function = create_ipsec_gre_tunnel_command_fn, |
| 292 | }; |
| 293 | /* *INDENT-ON* */ |
| 294 | |
| 295 | /* |
| 296 | * fd.io coding-style-patch-verification: ON |
| 297 | * |
| 298 | * Local Variables: |
| 299 | * eval: (c-set-style "gnu") |
| 300 | * End: |
| 301 | */ |