0d911a2876c476c31f00210c62e5e1a2f379f26b
[vpp.git] / src / vnet / ipsec / ipsec_tun.h
1 /*
2  * ipsec_tun.h : IPSEC tunnel protection
3  *
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:
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 #include <vnet/ipsec/ipsec.h>
19
20 /**
21  * result of a lookup in the protection bihash
22  */
23 typedef struct ipsec_tun_lkup_result_t_
24 {
25   union
26   {
27     struct
28     {
29       u32 tun_index;
30       u32 sa_index;
31     };
32     u64 as_u64;
33   };
34 } ipsec_tun_lkup_result_t;
35
36 typedef struct ipsec4_tunnel_kv_t
37 {
38   /*
39    * Key fields: remote ip and spi on incoming packet
40    * all fields in NET byte order
41    */
42   u64 key;
43   ipsec_tun_lkup_result_t value;
44 } __clib_packed ipsec4_tunnel_kv_t;
45
46 STATIC_ASSERT_SIZEOF (ipsec4_tunnel_kv_t, sizeof (clib_bihash_kv_8_8_t));
47 STATIC_ASSERT_OFFSET_OF (ipsec4_tunnel_kv_t, value,
48                          STRUCT_OFFSET_OF (clib_bihash_kv_8_8_t, value));
49
50 static inline void
51 ipsec4_tunnel_mk_key (ipsec4_tunnel_kv_t * k,
52                       const ip4_address_t * ip, u32 spi)
53 {
54   k->key = (((u64) ip->as_u32) << 32 | spi);
55 }
56
57 static inline void
58 ipsec4_tunnel_extract_key (const ipsec4_tunnel_kv_t * k,
59                            ip4_address_t * ip, u32 * spi)
60 {
61   *spi = (u32) k->key;
62   (*ip).as_u32 = k->key >> 32;
63 }
64
65 typedef struct ipsec6_tunnel_kv_t_
66 {
67   /*
68    * Key fields: remote ip and spi on incoming packet
69    * all fields in NET byte order
70    */
71   struct
72   {
73     ip6_address_t remote_ip;
74     u32 spi;
75     u32 __pad;
76   } key;
77   ipsec_tun_lkup_result_t value;
78 } __clib_packed ipsec6_tunnel_kv_t;
79
80 STATIC_ASSERT_SIZEOF (ipsec6_tunnel_kv_t, sizeof (clib_bihash_kv_24_8_t));
81 STATIC_ASSERT_OFFSET_OF (ipsec6_tunnel_kv_t, value,
82                          STRUCT_OFFSET_OF (clib_bihash_kv_24_8_t, value));
83
84 extern u8 *format_ipsec4_tunnel_kv (u8 * s, va_list * args);
85 extern u8 *format_ipsec6_tunnel_kv (u8 * s, va_list * args);
86
87 #define foreach_ipsec_protect_flags \
88   _(L2, 1, "l2")                    \
89   _(ENCAPED, 2, "encapped")         \
90   _(ITF, 4, "itf")                  \
91
92 typedef enum ipsec_protect_flags_t_
93 {
94   IPSEC_PROTECT_NONE = 0,
95 #define _(a,b,c) IPSEC_PROTECT_##a = b,
96   foreach_ipsec_protect_flags
97 #undef _
98 } __clib_packed ipsec_protect_flags_t;
99
100 extern u8 *format_ipsec_tun_protect_flags (u8 * s, va_list * args);
101
102 typedef struct ipsec_ep_t_
103 {
104   ip46_address_t src;
105   ip46_address_t dst;
106 } ipsec_ep_t;
107
108 #define ITP_MAX_N_SA_IN 4
109
110 typedef struct ipsec_tun_protect_t_
111 {
112   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
113   index_t itp_out_sa;
114
115   /* not using a vector since we want the memory inline
116    * with this struct */
117   u32 itp_n_sa_in;
118   index_t itp_in_sas[ITP_MAX_N_SA_IN];
119
120   u32 itp_sw_if_index;
121
122   ipsec_ep_t itp_crypto;
123
124   ipsec_protect_flags_t itp_flags;
125   adj_index_t itp_ai;
126
127   ipsec_ep_t itp_tun;
128
129   ip_address_t *itp_key;
130
131 } ipsec_tun_protect_t;
132
133 #define FOR_EACH_IPSEC_PROTECT_INPUT_SAI(_itp, _sai, body) \
134 {                                                          \
135   u32 __ii;                                                \
136   for (__ii = 0; __ii < _itp->itp_n_sa_in; __ii++) {       \
137     _sai = itp->itp_in_sas[__ii];                          \
138     body;                                                  \
139   }                                                        \
140 }
141 #define FOR_EACH_IPSEC_PROTECT_INPUT_SA(_itp, _sa, body)   \
142 {                                                          \
143   u32 __ii;                                                \
144   for (__ii = 0; __ii < _itp->itp_n_sa_in; __ii++) {       \
145     _sa = ipsec_sa_get(itp->itp_in_sas[__ii]);             \
146     body;                                                  \
147   }                                                        \
148 }
149
150 extern int ipsec_tun_protect_update_one (u32 sw_if_index,
151                                          const ip_address_t * nh,
152                                          u32 sa_out, u32 sa_in);
153 extern int ipsec_tun_protect_update (u32 sw_if_index,
154                                      const ip_address_t * nh,
155                                      u32 sa_out, u32 * sa_ins);
156 extern int ipsec_tun_protect_update_in (u32 sw_if_index,
157                                         const ip_address_t * nh, u32 sa_in);
158 extern int ipsec_tun_protect_update_out (u32 sw_if_index,
159                                          const ip_address_t * nh, u32 sa_out);
160
161 extern int ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh);
162
163 typedef walk_rc_t (*ipsec_tun_protect_walk_cb_t) (index_t itpi, void *arg);
164 extern void ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn,
165                                     void *cttx);
166 extern void ipsec_tun_protect_walk_itf (u32 sw_if_index,
167                                         ipsec_tun_protect_walk_cb_t fn,
168                                         void *cttx);
169
170 extern u8 *format_ipsec_tun_protect (u8 * s, va_list * args);
171 extern u8 *format_ipsec_tun_protect_index (u8 * s, va_list * args);
172
173 extern void ipsec_tun_register_nodes (ip_address_family_t af);
174 extern void ipsec_tun_unregister_nodes (ip_address_family_t af);
175
176 extern void ipsec_tun_table_init (ip_address_family_t af,
177                                   uword table_size, u32 n_buckets);
178
179 // FIXME
180 extern vlib_node_registration_t ipsec4_tun_input_node;
181 extern vlib_node_registration_t ipsec6_tun_input_node;
182
183 /*
184  * DP API
185  */
186 extern ipsec_tun_protect_t *ipsec_tun_protect_pool;
187
188 always_inline ipsec_tun_protect_t *
189 ipsec_tun_protect_get (u32 index)
190 {
191   return (pool_elt_at_index (ipsec_tun_protect_pool, index));
192 }
193
194 extern index_t *ipsec_tun_protect_sa_by_adj_index;
195 always_inline index_t
196 ipsec_tun_protect_get_sa_out (adj_index_t ai)
197 {
198   ASSERT (vec_len (ipsec_tun_protect_sa_by_adj_index) > ai);
199   ASSERT (INDEX_INVALID != ipsec_tun_protect_sa_by_adj_index[ai]);
200
201   return (ipsec_tun_protect_sa_by_adj_index[ai]);
202 }
203
204 /*
205  * fd.io coding-style-patch-verification: ON
206  *
207  * Local Variables:
208  * eval: (c-set-style "gnu")
209  * End:
210  */