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.h>
20 #define foreach_ipsec_protect_flags \
22 _ (ENCAPED, 2, "encapped") \
26 typedef enum ipsec_protect_flags_t_
28 IPSEC_PROTECT_NONE = 0,
29 #define _(a,b,c) IPSEC_PROTECT_##a = b,
30 foreach_ipsec_protect_flags
32 } __clib_packed ipsec_protect_flags_t;
34 extern u8 *format_ipsec_tun_protect_flags (u8 * s, va_list * args);
37 * result of a lookup in the protection bihash
39 typedef struct ipsec_tun_lkup_result_t_
44 ipsec_protect_flags_t flags;
46 } ipsec_tun_lkup_result_t;
48 typedef struct ipsec4_tunnel_kv_t
51 * Key fields: remote ip and spi on incoming packet
52 * all fields in NET byte order
55 ipsec_tun_lkup_result_t value;
56 } __clib_packed ipsec4_tunnel_kv_t;
58 STATIC_ASSERT_SIZEOF (ipsec4_tunnel_kv_t, sizeof (clib_bihash_kv_8_16_t));
59 STATIC_ASSERT_OFFSET_OF (ipsec4_tunnel_kv_t, value,
60 STRUCT_OFFSET_OF (clib_bihash_kv_8_16_t, value));
63 ipsec4_tunnel_mk_key (ipsec4_tunnel_kv_t * k,
64 const ip4_address_t * ip, u32 spi)
66 k->key = (((u64) ip->as_u32) << 32 | spi);
70 ipsec4_tunnel_extract_key (const ipsec4_tunnel_kv_t * k,
71 ip4_address_t * ip, u32 * spi)
74 (*ip).as_u32 = k->key >> 32;
77 typedef struct ipsec6_tunnel_kv_t_
80 * Key fields: remote ip and spi on incoming packet
81 * all fields in NET byte order
85 ip6_address_t remote_ip;
89 ipsec_tun_lkup_result_t value;
90 } __clib_packed ipsec6_tunnel_kv_t;
92 STATIC_ASSERT_SIZEOF (ipsec6_tunnel_kv_t, sizeof (clib_bihash_kv_24_16_t));
93 STATIC_ASSERT_OFFSET_OF (ipsec6_tunnel_kv_t, value,
94 STRUCT_OFFSET_OF (clib_bihash_kv_24_16_t, value));
96 extern u8 *format_ipsec4_tunnel_kv (u8 * s, va_list * args);
97 extern u8 *format_ipsec6_tunnel_kv (u8 * s, va_list * args);
99 typedef struct ipsec_ep_t_
105 #define ITP_MAX_N_SA_IN 4
107 typedef struct ipsec_tun_protect_t_
109 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
112 /* not using a vector since we want the memory inline
113 * with this struct */
115 index_t itp_in_sas[ITP_MAX_N_SA_IN];
119 ipsec_ep_t itp_crypto;
121 ipsec_protect_flags_t itp_flags;
126 ip_address_t *itp_key;
128 } ipsec_tun_protect_t;
130 #define FOR_EACH_IPSEC_PROTECT_INPUT_SAI(_itp, _sai, body) \
133 for (__ii = 0; __ii < _itp->itp_n_sa_in; __ii++) { \
134 _sai = itp->itp_in_sas[__ii]; \
138 #define FOR_EACH_IPSEC_PROTECT_INPUT_SA(_itp, _sa, body) \
141 for (__ii = 0; __ii < _itp->itp_n_sa_in; __ii++) { \
142 _sa = ipsec_sa_get(itp->itp_in_sas[__ii]); \
147 extern int ipsec_tun_protect_update (u32 sw_if_index,
148 const ip_address_t * nh,
149 u32 sa_out, u32 * sa_ins);
151 extern int ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh);
153 typedef walk_rc_t (*ipsec_tun_protect_walk_cb_t) (index_t itpi, void *arg);
154 extern void ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn,
156 extern void ipsec_tun_protect_walk_itf (u32 sw_if_index,
157 ipsec_tun_protect_walk_cb_t fn,
160 extern u8 *format_ipsec_tun_protect (u8 * s, va_list * args);
161 extern u8 *format_ipsec_tun_protect_index (u8 * s, va_list * args);
163 extern void ipsec_tun_register_nodes (ip_address_family_t af);
164 extern void ipsec_tun_unregister_nodes (ip_address_family_t af);
169 extern ipsec_tun_protect_t *ipsec_tun_protect_pool;
171 always_inline ipsec_tun_protect_t *
172 ipsec_tun_protect_get (u32 index)
174 return (pool_elt_at_index (ipsec_tun_protect_pool, index));
177 extern index_t *ipsec_tun_protect_sa_by_adj_index;
178 always_inline index_t
179 ipsec_tun_protect_get_sa_out (adj_index_t ai)
181 ASSERT (vec_len (ipsec_tun_protect_sa_by_adj_index) > ai);
182 ASSERT (INDEX_INVALID != ipsec_tun_protect_sa_by_adj_index[ai]);
184 return (ipsec_tun_protect_sa_by_adj_index[ai]);
188 * fd.io coding-style-patch-verification: ON
191 * eval: (c-set-style "gnu")