2 * ipsec_tun.h : IPSEC tunnel protection
4 * Copyright (c) 2015 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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/ipsec/ipsec_tun.h>
19 #include <vnet/ipsec/esp.h>
20 #include <vnet/udp/udp.h>
23 * Pool of tunnel protection objects
25 ipsec_tun_protect_t *ipsec_protect_pool;
28 * DB of protected tunnels
30 typedef struct ipsec_protect_db_t_
36 static ipsec_protect_db_t ipsec_protect_db;
39 ipsec_tun_protect_feature_set (ipsec_tun_protect_t * itp, u8 enable)
41 u32 sai = itp->itp_out_sa;
42 int is_ip4, is_l2, rv;
44 is_ip4 = ip46_address_is_ip4 (&itp->itp_tun.src);
45 is_l2 = itp->itp_flags & IPSEC_PROTECT_L2;
50 rv = vnet_feature_enable_disable ("ethernet-output",
52 itp->itp_sw_if_index, enable,
55 rv = vnet_feature_enable_disable ("ip4-output",
57 itp->itp_sw_if_index, enable,
63 rv = vnet_feature_enable_disable ("ethernet-output",
65 itp->itp_sw_if_index, enable,
68 rv = vnet_feature_enable_disable ("ip6-output",
70 itp->itp_sw_if_index, enable,
79 ipsec_tun_protect_db_add (ipsec_main_t * im, const ipsec_tun_protect_t * itp)
85 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
87 sa = ipsec_sa_get (sai);
89 ipsec_tun_lkup_result_t res = {
90 .tun_index = itp - ipsec_protect_pool,
95 * The key is formed from the tunnel's destination
96 * as the packet lookup is done from the packet's source
98 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
100 ipsec4_tunnel_key_t key = {
101 .remote_ip = itp->itp_crypto.dst.ip4.as_u32,
102 .spi = clib_host_to_net_u32 (sa->spi),
104 hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
108 ipsec6_tunnel_key_t key = {
109 .remote_ip = itp->itp_crypto.dst.ip6,
110 .spi = clib_host_to_net_u32 (sa->spi),
112 hash_set_mem_alloc (&im->tun6_protect_by_key, &key, res.as_u64);
119 ipsec_tun_protect_db_remove (ipsec_main_t * im,
120 const ipsec_tun_protect_t * itp)
122 const ipsec_sa_t *sa;
125 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
127 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
129 ipsec4_tunnel_key_t key = {
130 .remote_ip = itp->itp_crypto.dst.ip4.as_u32,
131 .spi = clib_host_to_net_u32 (sa->spi),
133 hash_unset (im->tun4_protect_by_key, &key);
137 ipsec6_tunnel_key_t key = {
138 .remote_ip = itp->itp_crypto.dst.ip6,
139 .spi = clib_host_to_net_u32 (sa->spi),
141 hash_unset_mem_free (&im->tun6_protect_by_key, &key);
148 ipsec_tun_protect_config (ipsec_main_t * im,
149 ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
154 itp->itp_n_sa_in = vec_len (sas_in);
155 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
156 itp->itp_in_sas[ii] = sas_in[ii];
157 itp->itp_out_sa = sa_out;
160 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
162 if (ipsec_sa_is_set_IS_TUNNEL (sa))
164 itp->itp_crypto.src = sa->tunnel_dst_addr;
165 itp->itp_crypto.dst = sa->tunnel_src_addr;
166 ipsec_sa_set_IS_PROTECT (sa);
167 itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
171 itp->itp_crypto.src = itp->itp_tun.src;
172 itp->itp_crypto.dst = itp->itp_tun.dst;
173 itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
179 * add to the DB against each SA
181 ipsec_tun_protect_db_add (im, itp);
184 * enable the encrypt feature for egress.
186 ipsec_tun_protect_feature_set (itp, 1);
191 ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
196 ipsec_tun_protect_feature_set (itp, 0);
199 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
201 ipsec_sa_unset_IS_PROTECT (sa);
204 ipsec_tun_protect_db_remove (im, itp);
206 ipsec_sa_unlock(itp->itp_out_sa);
208 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
210 ipsec_sa_unlock(sai);
216 ipsec_tun_protect_find (u32 sw_if_index)
218 if (vec_len (ipsec_protect_db.tunnels) < sw_if_index)
219 return (INDEX_INVALID);
221 return (ipsec_protect_db.tunnels[sw_if_index]);
225 ipsec_tun_protect_update (u32 sw_if_index, u32 sa_out, u32 * sas_in)
228 ipsec_tun_protect_t *itp;
234 vec_validate_init_empty (ipsec_protect_db.tunnels, sw_if_index,
236 itpi = ipsec_protect_db.tunnels[sw_if_index];
238 vec_foreach_index (ii, sas_in)
240 sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
241 if (~0 == sas_in[ii])
243 rv = VNET_API_ERROR_INVALID_VALUE;
248 sa_out = ipsec_sa_find_and_lock (sa_out);
252 rv = VNET_API_ERROR_INVALID_VALUE;
256 if (INDEX_INVALID == itpi)
258 vnet_device_class_t *dev_class;
259 vnet_hw_interface_t *hi;
263 vnm = vnet_get_main ();
264 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
265 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
267 if (NULL == dev_class->ip_tun_desc)
269 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
273 pool_get_zero (ipsec_protect_pool, itp);
275 itp->itp_sw_if_index = sw_if_index;
276 ipsec_protect_db.tunnels[sw_if_index] = itp - ipsec_protect_pool;
277 ipsec_protect_db.count++;
279 itp->itp_n_sa_in = vec_len (sas_in);
280 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
281 itp->itp_in_sas[ii] = sas_in[ii];
282 itp->itp_out_sa = sa_out;
284 rv = dev_class->ip_tun_desc (sw_if_index,
286 &itp->itp_tun.dst, &is_l2);
292 itp->itp_flags |= IPSEC_PROTECT_L2;
295 * add to the tunnel DB for ingress
296 * - if the SA is in trasnport mode, then the packates will arrivw
297 * with the IP src,dst of the protected tunnel, in which case we can
298 * simply strip the IP header and hand the payload to the protocol
299 * appropriate input handler
300 * - if the SA is in tunnel mode then there are two IP headers present
301 * one for the crytpo tunnel endpoints (described in the SA) and one
302 * for the tunnel endpoints. The outer IP headers in the srriving
303 * packets will have the crypto endpoints. So the DB needs to contain
304 * the crpto endpoint. Once the crypto header is stripped, revealing,
305 * the tunnel-IP we have 2 choices:
306 * 1) do a tunnel lookup based on the revealed header
307 * 2) skip the tunnel lookup and assume that the packet matches the
308 * one that is protected here.
309 * If we did 1) then we would allow our peer to use the SA for tunnel
310 * X to inject traffic onto tunnel Y, this is not good. If we do 2)
311 * then we don't verify that the peer is indeed using SA for tunnel
312 * X and addressing tunnel X. So we take a compromise, once the SA
313 * matches to tunnel X we veriy that the inner IP matches the value
314 * of the tunnel we are protecting, else it's dropped.
316 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
318 if (1 == hash_elts (im->tun4_protect_by_key))
319 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
320 ipsec4_tun_input_node.index);
321 if (1 == hash_elts (im->tun6_protect_by_key))
322 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
323 ipsec6_tun_input_node.index);
327 /* updating SAs only */
328 itp = pool_elt_at_index (ipsec_protect_pool, itpi);
330 ipsec_tun_protect_unconfig (im, itp);
331 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
340 ipsec_tun_protect_del (u32 sw_if_index)
342 ipsec_tun_protect_t *itp;
348 vec_validate_init_empty (ipsec_protect_db.tunnels, sw_if_index,
350 itpi = ipsec_protect_db.tunnels[sw_if_index];
352 if (INDEX_INVALID == itpi)
353 return (VNET_API_ERROR_NO_SUCH_ENTRY);
355 itp = ipsec_tun_protect_get (itpi);
356 ipsec_tun_protect_unconfig (im, itp);
358 ipsec_protect_db.tunnels[itp->itp_sw_if_index] = INDEX_INVALID;
360 pool_put (ipsec_protect_pool, itp);
362 /* if (0 == hash_elts (im->tun4_protect_by_key)) */
363 /* ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP); */
364 /* if (0 == hash_elts (im->tun6_protect_by_key)) */
365 /* ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP); */
371 ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
376 pool_foreach_index(itpi, ipsec_protect_pool,
384 ipsec_tunnel_protect_init (vlib_main_t * vm)
389 im->tun6_protect_by_key = hash_create_mem (0,
390 sizeof (ipsec6_tunnel_key_t),
392 im->tun4_protect_by_key = hash_create (0, sizeof (u64));
397 VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
401 * fd.io coding-style-patch-verification: ON
404 * eval: (c-set-style "gnu")