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