blob: 44f9642ce473d5db4a1cc8f0cdb53d13e3f5b0bd [file] [log] [blame]
Neale Ranns999c8ee2019-02-01 03:31:24 -08001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef __IPSEC_SPD_SA_H__
16#define __IPSEC_SPD_SA_H__
17
18#include <vlib/vlib.h>
19#include <vnet/ip/ip.h>
Neale Ranns8d7c5022019-02-06 01:41:05 -080020#include <vnet/fib/fib_node.h>
Neale Ranns999c8ee2019-02-01 03:31:24 -080021
Damjan Marion1f4e1cb2019-03-28 19:19:31 +010022#define IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE (64)
23
Neale Ranns999c8ee2019-02-01 03:31:24 -080024#define foreach_ipsec_crypto_alg \
25 _ (0, NONE, "none") \
26 _ (1, AES_CBC_128, "aes-cbc-128") \
27 _ (2, AES_CBC_192, "aes-cbc-192") \
28 _ (3, AES_CBC_256, "aes-cbc-256") \
29 _ (4, AES_CTR_128, "aes-ctr-128") \
30 _ (5, AES_CTR_192, "aes-ctr-192") \
31 _ (6, AES_CTR_256, "aes-ctr-256") \
32 _ (7, AES_GCM_128, "aes-gcm-128") \
33 _ (8, AES_GCM_192, "aes-gcm-192") \
34 _ (9, AES_GCM_256, "aes-gcm-256") \
35 _ (10, DES_CBC, "des-cbc") \
36 _ (11, 3DES_CBC, "3des-cbc")
37
38typedef enum
39{
40#define _(v, f, s) IPSEC_CRYPTO_ALG_##f = v,
41 foreach_ipsec_crypto_alg
42#undef _
43 IPSEC_CRYPTO_N_ALG,
44} ipsec_crypto_alg_t;
45
46#define foreach_ipsec_integ_alg \
47 _ (0, NONE, "none") \
48 _ (1, MD5_96, "md5-96") /* RFC2403 */ \
49 _ (2, SHA1_96, "sha1-96") /* RFC2404 */ \
50 _ (3, SHA_256_96, "sha-256-96") /* draft-ietf-ipsec-ciph-sha-256-00 */ \
51 _ (4, SHA_256_128, "sha-256-128") /* RFC4868 */ \
52 _ (5, SHA_384_192, "sha-384-192") /* RFC4868 */ \
53 _ (6, SHA_512_256, "sha-512-256") /* RFC4868 */
54
55typedef enum
56{
57#define _(v, f, s) IPSEC_INTEG_ALG_##f = v,
58 foreach_ipsec_integ_alg
59#undef _
60 IPSEC_INTEG_N_ALG,
61} ipsec_integ_alg_t;
62
63typedef enum
64{
65 IPSEC_PROTOCOL_AH = 0,
66 IPSEC_PROTOCOL_ESP = 1
67} ipsec_protocol_t;
68
Neale Ranns8d7c5022019-02-06 01:41:05 -080069#define IPSEC_N_PROTOCOLS (IPSEC_PROTOCOL_ESP+1)
70
71#define IPSEC_KEY_MAX_LEN 128
72typedef struct ipsec_key_t_
73{
74 u8 len;
75 u8 data[IPSEC_KEY_MAX_LEN];
76} ipsec_key_t;
77
78/*
79 * Enable extended sequence numbers
80 * Enable Anti-replay
81 * IPsec tunnel mode if non-zero, else transport mode
82 * IPsec tunnel mode is IPv6 if non-zero,
83 * else IPv4 tunnel only valid if is_tunnel is non-zero
84 * enable UDP encapsulation for NAT traversal
85 */
86#define foreach_ipsec_sa_flags \
87 _ (0, NONE, "none") \
88 _ (1, USE_EXTENDED_SEQ_NUM, "esn") \
89 _ (2, USE_ANTI_REPLAY, "anti-replay") \
90 _ (4, IS_TUNNEL, "tunnel") \
91 _ (8, IS_TUNNEL_V6, "tunnel-v6") \
92 _ (16, UDP_ENCAP, "udp-encap") \
93
94typedef enum ipsec_sad_flags_t_
95{
96#define _(v, f, s) IPSEC_SA_FLAG_##f = v,
97 foreach_ipsec_sa_flags
98#undef _
Damjan Marion1f4e1cb2019-03-28 19:19:31 +010099} __clib_packed ipsec_sa_flags_t;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100100
101STATIC_ASSERT (sizeof (ipsec_sa_flags_t) == 1, "IPSEC SA flags > 1 byte");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800102
Neale Ranns999c8ee2019-02-01 03:31:24 -0800103typedef struct
104{
Damjan Mariond709cbc2019-03-26 13:16:42 +0100105 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800106
Damjan Mariond709cbc2019-03-26 13:16:42 +0100107 /* flags */
108 ipsec_sa_flags_t flags;
109
Damjan Marionb966e8b2019-03-20 16:07:09 +0100110 u8 crypto_iv_size;
111 u8 crypto_block_size;
Damjan Marionb966e8b2019-03-20 16:07:09 +0100112 u8 integ_trunc_size;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100113 u32 spi;
Neale Ranns999c8ee2019-02-01 03:31:24 -0800114 u32 seq;
115 u32 seq_hi;
116 u32 last_seq;
117 u32 last_seq_hi;
118 u64 replay_window;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100119
120 vnet_crypto_op_type_t crypto_enc_op_type;
121 vnet_crypto_op_type_t crypto_dec_op_type;
122 vnet_crypto_op_type_t integ_op_type;
123
124 dpo_id_t dpo[IPSEC_N_PROTOCOLS];
125
126 /* data accessed by dataplane code should be above this comment */
127 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
128
129 union
130 {
131 ip4_header_t ip4_hdr;
132 ip6_header_t ip6_hdr;
133 };
134 udp_header_t udp_hdr;
135
136
137 fib_node_t node;
138 u32 id;
139 u32 stat_index;
140 ipsec_protocol_t protocol;
141
142 ipsec_crypto_alg_t crypto_alg;
143 ipsec_key_t crypto_key;
144
145 ipsec_integ_alg_t integ_alg;
146 ipsec_key_t integ_key;
147
148 ip46_address_t tunnel_src_addr;
149 ip46_address_t tunnel_dst_addr;
150
151 fib_node_index_t fib_entry_index;
152 u32 sibling;
153
154 u32 tx_fib_index;
155 u32 salt;
156
157 /* runtime */
Neale Ranns999c8ee2019-02-01 03:31:24 -0800158} ipsec_sa_t;
159
Damjan Mariond709cbc2019-03-26 13:16:42 +0100160STATIC_ASSERT_OFFSET_OF (ipsec_sa_t, cacheline1, CLIB_CACHE_LINE_BYTES);
161
162#define _(a,v,s) \
163 always_inline int \
164 ipsec_sa_is_set_##v (const ipsec_sa_t *sa) { \
165 return (sa->flags & IPSEC_SA_FLAG_##v); \
166 }
167foreach_ipsec_sa_flags
168#undef _
169#define _(a,v,s) \
170 always_inline int \
171 ipsec_sa_set_##v (ipsec_sa_t *sa) { \
172 return (sa->flags |= IPSEC_SA_FLAG_##v); \
173 }
174 foreach_ipsec_sa_flags
175#undef _
Neale Rannseba31ec2019-02-17 18:04:27 +0000176/**
177 * @brief
178 * SA packet & bytes counters
179 */
180extern vlib_combined_counter_main_t ipsec_sa_counters;
181
Neale Ranns8d7c5022019-02-06 01:41:05 -0800182extern void ipsec_mk_key (ipsec_key_t * key, const u8 * data, u8 len);
183
184extern int ipsec_sa_add (u32 id,
185 u32 spi,
186 ipsec_protocol_t proto,
187 ipsec_crypto_alg_t crypto_alg,
188 const ipsec_key_t * ck,
189 ipsec_integ_alg_t integ_alg,
190 const ipsec_key_t * ik,
191 ipsec_sa_flags_t flags,
192 u32 tx_table_id,
193 const ip46_address_t * tunnel_src_addr,
194 const ip46_address_t * tunnel_dst_addr,
195 u32 * sa_index);
196extern u32 ipsec_sa_del (u32 id);
Neale Rannsb4cfd552019-02-13 02:08:06 -0800197extern void ipsec_sa_stack (ipsec_sa_t * sa);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100198extern void ipsec_sa_set_crypto_alg (ipsec_sa_t * sa,
199 ipsec_crypto_alg_t crypto_alg);
200extern void ipsec_sa_set_integ_alg (ipsec_sa_t * sa,
201 ipsec_integ_alg_t integ_alg);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800202
Neale Ranns999c8ee2019-02-01 03:31:24 -0800203extern u8 ipsec_is_sa_used (u32 sa_index);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800204extern int ipsec_set_sa_key (u32 id,
205 const ipsec_key_t * ck, const ipsec_key_t * ik);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800206extern u32 ipsec_get_sa_index_by_sa_id (u32 sa_id);
207
Neale Rannsb4cfd552019-02-13 02:08:06 -0800208typedef walk_rc_t (*ipsec_sa_walk_cb_t) (ipsec_sa_t * sa, void *ctx);
209extern void ipsec_sa_walk (ipsec_sa_walk_cb_t cd, void *ctx);
210
Neale Ranns999c8ee2019-02-01 03:31:24 -0800211extern u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
212extern u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800213extern u8 *format_ipsec_sa (u8 * s, va_list * args);
214extern u8 *format_ipsec_key (u8 * s, va_list * args);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800215extern uword unformat_ipsec_crypto_alg (unformat_input_t * input,
216 va_list * args);
217extern uword unformat_ipsec_integ_alg (unformat_input_t * input,
218 va_list * args);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800219extern uword unformat_ipsec_key (unformat_input_t * input, va_list * args);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800220
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100221always_inline int
222ipsec_sa_anti_replay_check (ipsec_sa_t * sa, u32 * seqp)
223{
224 u32 seq, diff, tl, th;
225 if ((sa->flags & IPSEC_SA_FLAG_USE_ANTI_REPLAY) == 0)
226 return 0;
227
228 seq = clib_net_to_host_u32 (*seqp);
229
230 if ((sa->flags & IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM) == 0)
231 {
232
233 if (PREDICT_TRUE (seq > sa->last_seq))
234 return 0;
235
236 diff = sa->last_seq - seq;
237
238 if (IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE > diff)
239 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
240 else
241 return 1;
242
243 return 0;
244 }
245
246 tl = sa->last_seq;
247 th = sa->last_seq_hi;
248 diff = tl - seq;
249
250 if (PREDICT_TRUE (tl >= (IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE - 1)))
251 {
252 if (seq >= (tl - IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE + 1))
253 {
254 sa->seq_hi = th;
255 if (seq <= tl)
256 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
257 else
258 return 0;
259 }
260 else
261 {
262 sa->seq_hi = th + 1;
263 return 0;
264 }
265 }
266 else
267 {
268 if (seq >= (tl - IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE + 1))
269 {
270 sa->seq_hi = th - 1;
271 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
272 }
273 else
274 {
275 sa->seq_hi = th;
276 if (seq <= tl)
277 return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
278 else
279 return 0;
280 }
281 }
282
283 return 0;
284}
285
286always_inline void
287ipsec_sa_anti_replay_advance (ipsec_sa_t * sa, u32 * seqp)
288{
289 u32 pos, seq;
290 if (PREDICT_TRUE (sa->flags & IPSEC_SA_FLAG_USE_ANTI_REPLAY) == 0)
291 return;
292
293 seq = clib_host_to_net_u32 (*seqp);
294 if (PREDICT_TRUE (sa->flags & IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM))
295 {
296 int wrap = sa->seq_hi - sa->last_seq_hi;
297
298 if (wrap == 0 && seq > sa->last_seq)
299 {
300 pos = seq - sa->last_seq;
301 if (pos < IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE)
302 sa->replay_window = ((sa->replay_window) << pos) | 1;
303 else
304 sa->replay_window = 1;
305 sa->last_seq = seq;
306 }
307 else if (wrap > 0)
308 {
309 pos = ~seq + sa->last_seq + 1;
310 if (pos < IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE)
311 sa->replay_window = ((sa->replay_window) << pos) | 1;
312 else
313 sa->replay_window = 1;
314 sa->last_seq = seq;
315 sa->last_seq_hi = sa->seq_hi;
316 }
317 else if (wrap < 0)
318 {
319 pos = ~seq + sa->last_seq + 1;
320 sa->replay_window |= (1ULL << pos);
321 }
322 else
323 {
324 pos = sa->last_seq - seq;
325 sa->replay_window |= (1ULL << pos);
326 }
327 }
328 else
329 {
330 if (seq > sa->last_seq)
331 {
332 pos = seq - sa->last_seq;
333 if (pos < IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE)
334 sa->replay_window = ((sa->replay_window) << pos) | 1;
335 else
336 sa->replay_window = 1;
337 sa->last_seq = seq;
338 }
339 else
340 {
341 pos = sa->last_seq - seq;
342 sa->replay_window |= (1ULL << pos);
343 }
344 }
345}
346
Neale Ranns999c8ee2019-02-01 03:31:24 -0800347#endif /* __IPSEC_SPD_SA_H__ */
348
349/*
350 * fd.io coding-style-patch-verification: ON
351 *
352 * Local Variables:
353 * eval: (c-set-style "gnu")
354 * End:
355 */