blob: a51ca7f69ddfd62c3fcdc5939f3a70dd10354489 [file] [log] [blame]
Matus Fabian694265d2016-08-10 01:55:36 -07001/*
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 Luke16bcf7d2016-09-01 14:31:46 -040018 * @file
Matus Fabian694265d2016-08-10 01:55:36 -070019 * @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 Ranns3b81a1e2018-09-06 09:50:26 -070030#include <vnet/l2/l2_input.h>
Sergio Gonzalez Monroya10f62b2016-11-25 13:36:12 +000031
Matus Fabian694265d2016-08-10 01:55:36 -070032#include <vnet/ipsec/esp.h>
33
34u8 *
35format_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
49static clib_error_t *
50show_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* */
71VLIB_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 */
78clib_error_t *
79ipsec_gre_interface_init (vlib_main_t * vm)
80{
81 return 0;
82}
83
84VLIB_INIT_FUNCTION (ipsec_gre_interface_init);
85
86/**
87 * @brief Add or delete ipsec-gre tunnel interface.
88 *
89 * @param *a vnet_ipsec_gre_add_del_tunnel_args_t - tunnel interface parameters
90 * @param *sw_if_indexp u32 - software interface index
91 * @return int - 0 if success otherwise <code>VNET_API_ERROR_</code>
92 */
93int
94vnet_ipsec_gre_add_del_tunnel (vnet_ipsec_gre_add_del_tunnel_args_t * a,
95 u32 * sw_if_indexp)
96{
97 ipsec_gre_main_t *igm = &ipsec_gre_main;
98 vnet_main_t *vnm = igm->vnet_main;
99 ip4_main_t *im = &ip4_main;
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;
106 ipsec_add_del_ipsec_gre_tunnel_args_t args;
107
108 memset (&args, 0, sizeof (args));
109 args.is_add = a->is_add;
110 args.local_sa_id = a->lsa;
111 args.remote_sa_id = a->rsa;
112 args.local_ip.as_u32 = a->src.as_u32;
113 args.remote_ip.as_u32 = a->dst.as_u32;
114
115 key = (u64) a->src.as_u32 << 32 | (u64) a->dst.as_u32;
116 p = hash_get (igm->tunnel_by_key, key);
117
118 if (a->is_add)
119 {
120 /* check if same src/dst pair exists */
121 if (p)
122 return VNET_API_ERROR_INVALID_VALUE;
123
124 pool_get_aligned (igm->tunnels, t, CLIB_CACHE_LINE_BYTES);
125 memset (t, 0, sizeof (*t));
126
127 if (vec_len (igm->free_ipsec_gre_tunnel_hw_if_indices) > 0)
128 {
129 vnet_interface_main_t *im = &vnm->interface_main;
130
131 hw_if_index = igm->free_ipsec_gre_tunnel_hw_if_indices
132 [vec_len (igm->free_ipsec_gre_tunnel_hw_if_indices) - 1];
133 _vec_len (igm->free_ipsec_gre_tunnel_hw_if_indices) -= 1;
134
135 hi = vnet_get_hw_interface (vnm, hw_if_index);
136 hi->dev_instance = t - igm->tunnels;
137 hi->hw_instance = hi->dev_instance;
138
139 /* clear old stats of freed tunnel before reuse */
140 sw_if_index = hi->sw_if_index;
141 vnet_interface_counter_lock (im);
142 vlib_zero_combined_counter
143 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
144 sw_if_index);
145 vlib_zero_combined_counter
146 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX],
147 sw_if_index);
148 vlib_zero_simple_counter
149 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
150 vnet_interface_counter_unlock (im);
151 }
152 else
153 {
154 hw_if_index = vnet_register_interface
155 (vnm, ipsec_gre_device_class.index, t - igm->tunnels,
156 ipsec_gre_hw_interface_class.index, t - igm->tunnels);
157 hi = vnet_get_hw_interface (vnm, hw_if_index);
158 sw_if_index = hi->sw_if_index;
159 }
160
161 t->hw_if_index = hw_if_index;
162 t->sw_if_index = sw_if_index;
163 t->local_sa_id = a->lsa;
164 t->remote_sa_id = a->rsa;
165 t->local_sa = ipsec_get_sa_index_by_sa_id (a->lsa);
166 t->remote_sa = ipsec_get_sa_index_by_sa_id (a->rsa);
167
Matus Fabian025943b2016-10-07 03:29:09 -0700168 ip4_sw_interface_enable_disable (sw_if_index, 1);
169
Matus Fabian694265d2016-08-10 01:55:36 -0700170 vec_validate_init_empty (igm->tunnel_index_by_sw_if_index,
171 sw_if_index, ~0);
172 igm->tunnel_index_by_sw_if_index[sw_if_index] = t - igm->tunnels;
173
174 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
175
176 hi->min_packet_bytes = 64 + sizeof (gre_header_t) +
177 sizeof (ip4_header_t) + sizeof (esp_header_t) + sizeof (esp_footer_t);
Matus Fabian694265d2016-08-10 01:55:36 -0700178
179 /* Standard default gre MTU. */
Ole Troand7231612018-06-07 10:17:57 +0200180 /* TODO: Should take tunnel overhead into consideration */
181 vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000);
Matus Fabian694265d2016-08-10 01:55:36 -0700182
183 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
184 clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
185
186 hash_set (igm->tunnel_by_key, key, t - igm->tunnels);
187
188 slot = vlib_node_add_named_next_with_slot
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000189 (vnm->vlib_main, hi->tx_node_index, "esp-encrypt",
Matus Fabian694265d2016-08-10 01:55:36 -0700190 IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT);
191
192 ASSERT (slot == IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT);
193
194 }
195 else
196 { /* !is_add => delete */
197 /* tunnel needs to exist */
198 if (!p)
199 return VNET_API_ERROR_NO_SUCH_ENTRY;
200
201 t = pool_elt_at_index (igm->tunnels, p[0]);
202
203 sw_if_index = t->sw_if_index;
Matus Fabian025943b2016-10-07 03:29:09 -0700204 ip4_sw_interface_enable_disable (sw_if_index, 0);
Matus Fabian694265d2016-08-10 01:55:36 -0700205 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
206 /* make sure tunnel is removed from l2 bd or xconnect */
207 set_int_l2_mode (igm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
208 vec_add1 (igm->free_ipsec_gre_tunnel_hw_if_indices, t->hw_if_index);
209 igm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
210
211 hash_unset (igm->tunnel_by_key, key);
212 pool_put (igm->tunnels, t);
213 }
214
215 if (sw_if_indexp)
216 *sw_if_indexp = sw_if_index;
217
218 return ipsec_add_del_ipsec_gre_tunnel (vnm, &args);
219}
220
221static clib_error_t *
222create_ipsec_gre_tunnel_command_fn (vlib_main_t * vm,
223 unformat_input_t * input,
224 vlib_cli_command_t * cmd)
225{
226 unformat_input_t _line_input, *line_input = &_line_input;
227 u8 is_add = 1;
228 u32 num_m_args = 0;
229 ip4_address_t src, dst;
230 u32 lsa = 0, rsa = 0;
231 vnet_ipsec_gre_add_del_tunnel_args_t _a, *a = &_a;
232 int rv;
233 u32 sw_if_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500234 clib_error_t *error = NULL;
Matus Fabian694265d2016-08-10 01:55:36 -0700235
236 /* Get a line of input. */
237 if (!unformat_user (input, unformat_line_input, line_input))
238 return 0;
239
240 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
241 {
242 if (unformat (line_input, "del"))
243 is_add = 0;
244 else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
245 num_m_args++;
246 else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
247 num_m_args++;
248 else if (unformat (line_input, "local-sa %d", &lsa))
249 num_m_args++;
250 else if (unformat (line_input, "remote-sa %d", &rsa))
251 num_m_args++;
252 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500253 {
254 error = clib_error_return (0, "unknown input `%U'",
255 format_unformat_error, line_input);
256 goto done;
257 }
Matus Fabian694265d2016-08-10 01:55:36 -0700258 }
Matus Fabian694265d2016-08-10 01:55:36 -0700259
260 if (num_m_args < 4)
Billy McFalla9a20e72017-02-15 11:39:12 -0500261 {
262 error = clib_error_return (0, "mandatory argument(s) missing");
263 goto done;
264 }
Matus Fabian694265d2016-08-10 01:55:36 -0700265
266 if (memcmp (&src, &dst, sizeof (src)) == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500267 {
268 error = clib_error_return (0, "src and dst are identical");
269 goto done;
270 }
Matus Fabian694265d2016-08-10 01:55:36 -0700271
272 memset (a, 0, sizeof (*a));
273 a->is_add = is_add;
274 a->lsa = lsa;
275 a->rsa = rsa;
276 clib_memcpy (&a->src, &src, sizeof (src));
277 clib_memcpy (&a->dst, &dst, sizeof (dst));
278
279 rv = vnet_ipsec_gre_add_del_tunnel (a, &sw_if_index);
280
281 switch (rv)
282 {
283 case 0:
284 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
285 vnet_get_main (), sw_if_index);
286 break;
287 case VNET_API_ERROR_INVALID_VALUE:
Billy McFalla9a20e72017-02-15 11:39:12 -0500288 error = clib_error_return (0, "GRE tunnel already exists...");
289 goto done;
Matus Fabian694265d2016-08-10 01:55:36 -0700290 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500291 error = clib_error_return (0,
292 "vnet_ipsec_gre_add_del_tunnel returned %d",
293 rv);
294 goto done;
Matus Fabian694265d2016-08-10 01:55:36 -0700295 }
296
Billy McFalla9a20e72017-02-15 11:39:12 -0500297done:
298 unformat_free (line_input);
299
300 return error;
Matus Fabian694265d2016-08-10 01:55:36 -0700301}
302
303/* *INDENT-OFF* */
304VLIB_CLI_COMMAND (create_ipsec_gre_tunnel_command, static) = {
305 .path = "create ipsec gre tunnel",
306 .short_help = "create ipsec gre tunnel src <addr> dst <addr> "
307 "local-sa <id> remote-sa <id> [del]",
308 .function = create_ipsec_gre_tunnel_command_fn,
309};
310/* *INDENT-ON* */
311
312/*
313* fd.io coding-style-patch-verification: ON
314*
315* Local Variables:
316* eval: (c-set-style "gnu")
317* End:
318*/