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