blob: a9abfdb05e2d1b7c491a29c24206c94a753cff03 [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>
Benoît Ganne5527a782022-01-18 15:56:41 +010019#include <vppinfra/pcg.h>
Filip Tehlare5d34912020-03-02 15:17:37 +000020#include <vnet/crypto/crypto.h>
Neale Ranns999c8ee2019-02-01 03:31:24 -080021#include <vnet/ip/ip.h>
Neale Ranns8d7c5022019-02-06 01:41:05 -080022#include <vnet/fib/fib_node.h>
Neale Ranns041add72020-01-02 04:06:10 +000023#include <vnet/tunnel/tunnel.h>
Neale Ranns999c8ee2019-02-01 03:31:24 -080024
Benoît Ganne5527a782022-01-18 15:56:41 +010025#define ESP_MAX_ICV_SIZE (32)
26#define ESP_MAX_IV_SIZE (16)
27#define ESP_MAX_BLOCK_SIZE (16)
28
Vladimir Ratnikovd7c030d2022-09-13 13:09:53 +000029#define foreach_ipsec_crypto_alg \
30 _ (0, NONE, "none") \
31 _ (1, AES_CBC_128, "aes-cbc-128") \
32 _ (2, AES_CBC_192, "aes-cbc-192") \
33 _ (3, AES_CBC_256, "aes-cbc-256") \
34 _ (4, AES_CTR_128, "aes-ctr-128") \
35 _ (5, AES_CTR_192, "aes-ctr-192") \
36 _ (6, AES_CTR_256, "aes-ctr-256") \
37 _ (7, AES_GCM_128, "aes-gcm-128") \
38 _ (8, AES_GCM_192, "aes-gcm-192") \
39 _ (9, AES_GCM_256, "aes-gcm-256") \
40 _ (10, DES_CBC, "des-cbc") \
41 _ (11, 3DES_CBC, "3des-cbc") \
42 _ (12, CHACHA20_POLY1305, "chacha20-poly1305")
Neale Ranns999c8ee2019-02-01 03:31:24 -080043
44typedef enum
45{
46#define _(v, f, s) IPSEC_CRYPTO_ALG_##f = v,
47 foreach_ipsec_crypto_alg
48#undef _
49 IPSEC_CRYPTO_N_ALG,
Neale Ranns123b5eb2020-10-16 14:03:55 +000050} __clib_packed ipsec_crypto_alg_t;
Neale Ranns999c8ee2019-02-01 03:31:24 -080051
Neale Ranns47feb112019-04-11 15:14:07 +000052#define IPSEC_CRYPTO_ALG_IS_GCM(_alg) \
53 (((_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) || \
54 (_alg == IPSEC_CRYPTO_ALG_AES_GCM_192) || \
55 (_alg == IPSEC_CRYPTO_ALG_AES_GCM_256)))
56
Benoît Ganne490b9272021-01-22 18:03:09 +010057#define IPSEC_CRYPTO_ALG_IS_CTR(_alg) \
58 (((_alg == IPSEC_CRYPTO_ALG_AES_CTR_128) || \
59 (_alg == IPSEC_CRYPTO_ALG_AES_CTR_192) || \
60 (_alg == IPSEC_CRYPTO_ALG_AES_CTR_256)))
61
Vladimir Ratnikovd7c030d2022-09-13 13:09:53 +000062#define IPSEC_CRYPTO_ALG_CTR_AEAD_OTHERS(_alg) \
63 (_alg == IPSEC_CRYPTO_ALG_CHACHA20_POLY1305)
64
Neale Ranns999c8ee2019-02-01 03:31:24 -080065#define foreach_ipsec_integ_alg \
66 _ (0, NONE, "none") \
67 _ (1, MD5_96, "md5-96") /* RFC2403 */ \
68 _ (2, SHA1_96, "sha1-96") /* RFC2404 */ \
69 _ (3, SHA_256_96, "sha-256-96") /* draft-ietf-ipsec-ciph-sha-256-00 */ \
70 _ (4, SHA_256_128, "sha-256-128") /* RFC4868 */ \
71 _ (5, SHA_384_192, "sha-384-192") /* RFC4868 */ \
72 _ (6, SHA_512_256, "sha-512-256") /* RFC4868 */
73
74typedef enum
75{
76#define _(v, f, s) IPSEC_INTEG_ALG_##f = v,
77 foreach_ipsec_integ_alg
78#undef _
79 IPSEC_INTEG_N_ALG,
Neale Ranns123b5eb2020-10-16 14:03:55 +000080} __clib_packed ipsec_integ_alg_t;
Neale Ranns999c8ee2019-02-01 03:31:24 -080081
82typedef enum
83{
84 IPSEC_PROTOCOL_AH = 0,
85 IPSEC_PROTOCOL_ESP = 1
Neale Ranns123b5eb2020-10-16 14:03:55 +000086} __clib_packed ipsec_protocol_t;
Neale Ranns999c8ee2019-02-01 03:31:24 -080087
Neale Ranns8d7c5022019-02-06 01:41:05 -080088#define IPSEC_KEY_MAX_LEN 128
89typedef struct ipsec_key_t_
90{
91 u8 len;
92 u8 data[IPSEC_KEY_MAX_LEN];
93} ipsec_key_t;
94
95/*
96 * Enable extended sequence numbers
97 * Enable Anti-replay
98 * IPsec tunnel mode if non-zero, else transport mode
99 * IPsec tunnel mode is IPv6 if non-zero,
100 * else IPv4 tunnel only valid if is_tunnel is non-zero
101 * enable UDP encapsulation for NAT traversal
102 */
Benoît Ganne490b9272021-01-22 18:03:09 +0100103#define foreach_ipsec_sa_flags \
104 _ (0, NONE, "none") \
105 _ (1, USE_ESN, "esn") \
106 _ (2, USE_ANTI_REPLAY, "anti-replay") \
107 _ (4, IS_TUNNEL, "tunnel") \
108 _ (8, IS_TUNNEL_V6, "tunnel-v6") \
109 _ (16, UDP_ENCAP, "udp-encap") \
110 _ (32, IS_PROTECT, "Protect") \
111 _ (64, IS_INBOUND, "inbound") \
112 _ (128, IS_AEAD, "aead") \
Neale Rannsf16e9a52021-02-25 19:09:24 +0000113 _ (256, IS_CTR, "ctr") \
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000114 _ (512, IS_ASYNC, "async") \
115 _ (1024, NO_ALGO_NO_DROP, "no-algo-no-drop")
Neale Ranns8d7c5022019-02-06 01:41:05 -0800116
117typedef enum ipsec_sad_flags_t_
118{
119#define _(v, f, s) IPSEC_SA_FLAG_##f = v,
120 foreach_ipsec_sa_flags
121#undef _
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100122} __clib_packed ipsec_sa_flags_t;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100123
Benoît Ganne490b9272021-01-22 18:03:09 +0100124STATIC_ASSERT (sizeof (ipsec_sa_flags_t) == 2, "IPSEC SA flags != 2 byte");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800125
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100126#define foreach_ipsec_sa_err \
127 _ (0, LOST, lost, "packets lost") \
128 _ (1, HANDOFF, handoff, "hand-off") \
129 _ (2, INTEG_ERROR, integ_error, "Integrity check failed") \
130 _ (3, DECRYPTION_FAILED, decryption_failed, "Decryption failed") \
131 _ (4, CRYPTO_ENGINE_ERROR, crypto_engine_error, \
132 "crypto engine error (dropped)") \
133 _ (5, REPLAY, replay, "SA replayed packet") \
134 _ (6, RUNT, runt, "undersized packet") \
135 _ (7, NO_BUFFERS, no_buffers, "no buffers (dropped)") \
136 _ (8, OVERSIZED_HEADER, oversized_header, \
137 "buffer with oversized header (dropped)") \
138 _ (9, NO_TAIL_SPACE, no_tail_space, \
139 "no enough buffer tail space (dropped)") \
140 _ (10, TUN_NO_PROTO, tun_no_proto, "no tunnel protocol") \
141 _ (11, UNSUP_PAYLOAD, unsup_payload, "unsupported payload") \
142 _ (12, SEQ_CYCLED, seq_cycled, "sequence number cycled (dropped)") \
143 _ (13, CRYPTO_QUEUE_FULL, crypto_queue_full, "crypto queue full (dropped)") \
144 _ (14, NO_ENCRYPTION, no_encryption, "no Encrypting SA (dropped)") \
145 _ (15, DROP_FRAGMENTS, drop_fragments, "IP fragments drop")
146
147typedef enum
148{
149#define _(v, f, s, d) IPSEC_SA_ERROR_##f = v,
150 foreach_ipsec_sa_err
151#undef _
152 IPSEC_SA_N_ERRORS,
153} __clib_packed ipsec_sa_err_t;
154
Neale Ranns999c8ee2019-02-01 03:31:24 -0800155typedef struct
156{
Damjan Mariond709cbc2019-03-26 13:16:42 +0100157 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800158
Benoît Ganne5527a782022-01-18 15:56:41 +0100159 clib_pcg64i_random_t iv_prng;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100160
Neale Ranns999c8ee2019-02-01 03:31:24 -0800161 u64 replay_window;
Neale Ranns72f2a3a2019-06-17 15:43:38 +0000162 dpo_id_t dpo;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100163
Damjan Mariond1bed682019-04-24 15:20:35 +0200164 vnet_crypto_key_index_t crypto_key_index;
165 vnet_crypto_key_index_t integ_key_index;
Fan Zhangf5395782020-04-29 14:00:03 +0100166
Benoît Ganne5527a782022-01-18 15:56:41 +0100167 u32 spi;
168 u32 seq;
169 u32 seq_hi;
Fan Zhangf5395782020-04-29 14:00:03 +0100170
Benoît Ganne5527a782022-01-18 15:56:41 +0100171 u16 crypto_enc_op_id;
172 u16 crypto_dec_op_id;
173 u16 integ_op_id;
174 ipsec_sa_flags_t flags;
175 u16 thread_index;
Fan Zhangf5395782020-04-29 14:00:03 +0100176
Benoît Ganne5527a782022-01-18 15:56:41 +0100177 u16 integ_icv_size : 6;
178 u16 crypto_iv_size : 5;
179 u16 esp_block_align : 5;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100180
Benoît Ganne490b9272021-01-22 18:03:09 +0100181 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Damjan Mariond709cbc2019-03-26 13:16:42 +0100182
183 union
184 {
185 ip4_header_t ip4_hdr;
186 ip6_header_t ip6_hdr;
187 };
188 udp_header_t udp_hdr;
189
Benoît Ganne490b9272021-01-22 18:03:09 +0100190 /* Salt used in CTR modes (incl. GCM) - stored in network byte order */
Neale Ranns80f6fd52019-04-16 02:41:34 +0000191 u32 salt;
Fan Zhangf5395782020-04-29 14:00:03 +0100192
Neale Ranns123b5eb2020-10-16 14:03:55 +0000193 ipsec_protocol_t protocol;
Neale Ranns041add72020-01-02 04:06:10 +0000194 tunnel_encap_decap_flags_t tunnel_flags;
Neale Ranns9ec846c2021-02-09 14:04:02 +0000195 u8 __pad[2];
Neale Ranns123b5eb2020-10-16 14:03:55 +0000196
197 /* data accessed by dataplane code should be above this comment */
198 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
199
200 /* Elements with u64 size multiples */
Neale Ranns9ec846c2021-02-09 14:04:02 +0000201 tunnel_t tunnel;
Neale Ranns123b5eb2020-10-16 14:03:55 +0000202 fib_node_t node;
203
204 /* elements with u32 size */
205 u32 id;
206 u32 stat_index;
207 vnet_crypto_alg_t integ_calg;
208 vnet_crypto_alg_t crypto_calg;
Benoît Ganne5527a782022-01-18 15:56:41 +0100209 u32 crypto_sync_key_index;
210 u32 integ_sync_key_index;
211 u32 crypto_async_key_index;
212
213 /* elements with u16 size */
214 u16 crypto_sync_enc_op_id;
215 u16 crypto_sync_dec_op_id;
216 u16 integ_sync_op_id;
217 u16 crypto_async_enc_op_id;
218 u16 crypto_async_dec_op_id;
Neale Ranns123b5eb2020-10-16 14:03:55 +0000219
Neale Ranns123b5eb2020-10-16 14:03:55 +0000220 /* else u8 packed */
221 ipsec_crypto_alg_t crypto_alg;
222 ipsec_integ_alg_t integ_alg;
223
224 ipsec_key_t integ_key;
225 ipsec_key_t crypto_key;
Neale Ranns999c8ee2019-02-01 03:31:24 -0800226} ipsec_sa_t;
227
Benoît Ganne5527a782022-01-18 15:56:41 +0100228STATIC_ASSERT (VNET_CRYPTO_N_OP_IDS < (1 << 16), "crypto ops overflow");
229STATIC_ASSERT (ESP_MAX_ICV_SIZE < (1 << 6), "integer icv overflow");
230STATIC_ASSERT (ESP_MAX_IV_SIZE < (1 << 5), "esp iv overflow");
231STATIC_ASSERT (ESP_MAX_BLOCK_SIZE < (1 << 5), "esp alignment overflow");
Damjan Mariond709cbc2019-03-26 13:16:42 +0100232STATIC_ASSERT_OFFSET_OF (ipsec_sa_t, cacheline1, CLIB_CACHE_LINE_BYTES);
Neale Ranns123b5eb2020-10-16 14:03:55 +0000233STATIC_ASSERT_OFFSET_OF (ipsec_sa_t, cacheline2, 2 * CLIB_CACHE_LINE_BYTES);
Damjan Mariond709cbc2019-03-26 13:16:42 +0100234
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000235/**
236 * Pool of IPSec SAs
237 */
238extern ipsec_sa_t *ipsec_sa_pool;
239
Neale Rannsaa7d7662021-02-10 08:42:49 +0000240/*
241 * Ensure that the IPsec data does not overlap with the IP data in
242 * the buffer meta data
243 */
244STATIC_ASSERT (STRUCT_OFFSET_OF (vnet_buffer_opaque_t, ipsec.sad_index) ==
245 STRUCT_OFFSET_OF (vnet_buffer_opaque_t, ip.save_protocol),
246 "IPSec data is overlapping with IP data");
247
Damjan Mariond709cbc2019-03-26 13:16:42 +0100248#define _(a,v,s) \
249 always_inline int \
250 ipsec_sa_is_set_##v (const ipsec_sa_t *sa) { \
251 return (sa->flags & IPSEC_SA_FLAG_##v); \
252 }
253foreach_ipsec_sa_flags
254#undef _
255#define _(a,v,s) \
256 always_inline int \
257 ipsec_sa_set_##v (ipsec_sa_t *sa) { \
258 return (sa->flags |= IPSEC_SA_FLAG_##v); \
259 }
260 foreach_ipsec_sa_flags
261#undef _
Neale Rannsc87b66c2019-02-07 07:26:12 -0800262#define _(a,v,s) \
263 always_inline int \
264 ipsec_sa_unset_##v (ipsec_sa_t *sa) { \
265 return (sa->flags &= ~IPSEC_SA_FLAG_##v); \
266 }
267 foreach_ipsec_sa_flags
268#undef _
Neale Rannseba31ec2019-02-17 18:04:27 +0000269/**
270 * @brief
271 * SA packet & bytes counters
272 */
273extern vlib_combined_counter_main_t ipsec_sa_counters;
Arthur de Kerhorad95b062022-11-16 19:12:05 +0100274extern vlib_simple_counter_main_t ipsec_sa_err_counters[IPSEC_SA_N_ERRORS];
Neale Rannseba31ec2019-02-17 18:04:27 +0000275
Neale Ranns8d7c5022019-02-06 01:41:05 -0800276extern void ipsec_mk_key (ipsec_key_t * key, const u8 * data, u8 len);
277
Arthur de Kerhor4117b242022-08-31 19:13:03 +0200278extern int ipsec_sa_update (u32 id, u16 src_port, u16 dst_port,
279 const tunnel_t *tun, bool is_tun);
Neale Ranns9ec846c2021-02-09 14:04:02 +0000280extern int
281ipsec_sa_add_and_lock (u32 id, u32 spi, ipsec_protocol_t proto,
282 ipsec_crypto_alg_t crypto_alg, const ipsec_key_t *ck,
283 ipsec_integ_alg_t integ_alg, const ipsec_key_t *ik,
284 ipsec_sa_flags_t flags, u32 salt, u16 src_port,
285 u16 dst_port, const tunnel_t *tun, u32 *sa_out_index);
Maxime Peim1271e3a2023-03-20 14:13:56 +0000286extern int ipsec_sa_bind (u32 id, u32 worker, bool bind);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000287extern index_t ipsec_sa_find_and_lock (u32 id);
288extern int ipsec_sa_unlock_id (u32 id);
289extern void ipsec_sa_unlock (index_t sai);
Neale Ranns12989b52019-09-26 16:20:19 +0000290extern void ipsec_sa_lock (index_t sai);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800291extern void ipsec_sa_clear (index_t sai);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100292extern void ipsec_sa_set_crypto_alg (ipsec_sa_t * sa,
293 ipsec_crypto_alg_t crypto_alg);
294extern void ipsec_sa_set_integ_alg (ipsec_sa_t * sa,
295 ipsec_integ_alg_t integ_alg);
Benoît Ganne5527a782022-01-18 15:56:41 +0100296extern void ipsec_sa_set_async_mode (ipsec_sa_t *sa, int is_enabled);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800297
Neale Rannsb4cfd552019-02-13 02:08:06 -0800298typedef walk_rc_t (*ipsec_sa_walk_cb_t) (ipsec_sa_t * sa, void *ctx);
299extern void ipsec_sa_walk (ipsec_sa_walk_cb_t cd, void *ctx);
300
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000301extern u8 *format_ipsec_replay_window (u8 *s, va_list *args);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800302extern u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
303extern u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800304extern u8 *format_ipsec_sa (u8 * s, va_list * args);
305extern u8 *format_ipsec_key (u8 * s, va_list * args);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800306extern uword unformat_ipsec_crypto_alg (unformat_input_t * input,
307 va_list * args);
308extern uword unformat_ipsec_integ_alg (unformat_input_t * input,
309 va_list * args);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800310extern uword unformat_ipsec_key (unformat_input_t * input, va_list * args);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800311
Filip Tehlare5d34912020-03-02 15:17:37 +0000312#define IPSEC_UDP_PORT_NONE ((u16)~0)
313
Neale Ranns6afaae12019-07-17 15:07:14 +0000314/*
315 * Anti Replay definitions
316 */
317
318#define IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE (64)
319#define IPSEC_SA_ANTI_REPLAY_WINDOW_MAX_INDEX (IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE-1)
320
321/*
322 * sequence number less than the lower bound are outside of the window
323 * From RFC4303 Appendix A:
324 * Bl = Tl - W + 1
325 */
326#define IPSEC_SA_ANTI_REPLAY_WINDOW_LOWER_BOUND(_tl) (_tl - IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE + 1)
327
Neale Ranns5b891102021-06-28 13:31:28 +0000328always_inline int
329ipsec_sa_anti_replay_check (const ipsec_sa_t *sa, u32 seq)
330{
331 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
332 sa->replay_window & (1ULL << (sa->seq - seq)))
333 return 1;
334 else
335 return 0;
336}
337
Neale Ranns6afaae12019-07-17 15:07:14 +0000338/*
339 * Anti replay check.
340 * inputs need to be in host byte order.
Neale Ranns5b891102021-06-28 13:31:28 +0000341 *
342 * The function runs in two contexts. pre and post decrypt.
343 * Pre-decrypt it:
344 * 1 - determines if a packet is a replay - a simple check in the window
345 * 2 - returns the hi-seq number that should be used to decrypt.
346 * post-decrypt:
347 * Checks whether the packet is a replay or falls out of window
348 *
349 * This funcion should be called even without anti-replay enabled to ensure
350 * the high sequence number is set.
Neale Ranns6afaae12019-07-17 15:07:14 +0000351 */
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100352always_inline int
Neale Ranns5b891102021-06-28 13:31:28 +0000353ipsec_sa_anti_replay_and_sn_advance (const ipsec_sa_t *sa, u32 seq,
354 u32 hi_seq_used, bool post_decrypt,
355 u32 *hi_seq_req)
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100356{
Neale Ranns5b891102021-06-28 13:31:28 +0000357 ASSERT ((post_decrypt == false) == (hi_seq_req != 0));
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100358
Neale Ranns6afaae12019-07-17 15:07:14 +0000359 if (!ipsec_sa_is_set_USE_ESN (sa))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100360 {
Neale Ranns5b891102021-06-28 13:31:28 +0000361 if (hi_seq_req)
362 /* no ESN, therefore the hi-seq is always 0 */
363 *hi_seq_req = 0;
364
365 if (!ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100366 return 0;
367
Neale Ranns5b891102021-06-28 13:31:28 +0000368 if (PREDICT_TRUE (seq > sa->seq))
369 return 0;
370
371 u32 diff = sa->seq - seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100372
373 if (IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE > diff)
Neale Ranns5b891102021-06-28 13:31:28 +0000374 return ((sa->replay_window & (1ULL << diff)) ? 1 : 0);
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100375 else
376 return 1;
377
378 return 0;
379 }
380
Neale Ranns5b891102021-06-28 13:31:28 +0000381 if (!ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
382 {
383 /* there's no AR configured for this SA, but in order
384 * to know whether a packet has wrapped the hi ESN we need
385 * to know whether it is out of window. if we use the default
386 * lower bound then we are effectively forcing AR because
387 * out of window packets will get the increased hi seq number
388 * and will thus fail to decrypt. IOW we need a window to know
389 * if the SN has wrapped, but we don't want a window to check for
390 * anti replay. to resolve the contradiction we use a huge window.
391 * if the packet is not within 2^30 of the current SN, we'll consider
392 * it a wrap.
393 */
394 if (hi_seq_req)
395 {
396 if (seq >= sa->seq)
397 /* The packet's sequence number is larger that the SA's.
398 * that can't be a warp - unless we lost more than
399 * 2^32 packets ... how could we know? */
400 *hi_seq_req = sa->seq_hi;
401 else
402 {
403 /* The packet's SN is less than the SAs, so either the SN has
404 * wrapped or the SN is just old. */
405 if (sa->seq - seq > (1 << 30))
406 /* It's really really really old => it wrapped */
407 *hi_seq_req = sa->seq_hi + 1;
408 else
409 *hi_seq_req = sa->seq_hi;
410 }
411 }
412 /*
413 * else
414 * this is post-decrpyt and since it decrypted we accept it
415 */
416 return 0;
417 }
418 if (PREDICT_TRUE (sa->seq >= (IPSEC_SA_ANTI_REPLAY_WINDOW_MAX_INDEX)))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100419 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000420 /*
421 * the last sequence number VPP recieved is more than one
422 * window size greater than zero.
423 * Case A from RFC4303 Appendix A.
424 */
Neale Ranns5b891102021-06-28 13:31:28 +0000425 if (seq < IPSEC_SA_ANTI_REPLAY_WINDOW_LOWER_BOUND (sa->seq))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100426 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000427 /*
428 * the received sequence number is lower than the lower bound
429 * of the window, this could mean either a replay packet or that
430 * the high sequence number has wrapped. if it decrypts corrently
431 * then it's the latter.
432 */
Neale Ranns5b891102021-06-28 13:31:28 +0000433 if (post_decrypt)
434 {
435 if (hi_seq_used == sa->seq_hi)
436 /* the high sequence number used to succesfully decrypt this
437 * packet is the same as the last-sequnence number of the SA.
438 * that means this packet did not cause a wrap.
439 * this packet is thus out of window and should be dropped */
440 return 1;
441 else
442 /* The packet decrypted with a different high sequence number
443 * to the SA, that means it is the wrap packet and should be
444 * accepted */
445 return 0;
446 }
447 else
448 {
449 /* pre-decrypt it might be the might that casues a wrap, we
450 * need to decrpyt to find out */
451 if (hi_seq_req)
452 *hi_seq_req = sa->seq_hi + 1;
453 return 0;
454 }
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100455 }
456 else
457 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000458 /*
459 * the recieved sequence number greater than the low
460 * end of the window.
461 */
Neale Ranns5b891102021-06-28 13:31:28 +0000462 if (hi_seq_req)
463 *hi_seq_req = sa->seq_hi;
464 if (seq <= sa->seq)
Neale Ranns6afaae12019-07-17 15:07:14 +0000465 /*
466 * The recieved seq number is within bounds of the window
467 * check if it's a duplicate
468 */
Neale Ranns5b891102021-06-28 13:31:28 +0000469 return (ipsec_sa_anti_replay_check (sa, seq));
Neale Ranns6afaae12019-07-17 15:07:14 +0000470 else
471 /*
472 * The received sequence number is greater than the window
473 * upper bound. this packet will move the window along, assuming
474 * it decrypts correctly.
475 */
476 return 0;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100477 }
478 }
479 else
480 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000481 /*
482 * the last sequence number VPP recieved is within one window
483 * size of zero, i.e. 0 < TL < WINDOW_SIZE, the lower bound is thus a
484 * large sequence number.
485 * Note that the check below uses unsiged integer arthimetic, so the
486 * RHS will be a larger number.
487 * Case B from RFC4303 Appendix A.
488 */
Neale Ranns5b891102021-06-28 13:31:28 +0000489 if (seq < IPSEC_SA_ANTI_REPLAY_WINDOW_LOWER_BOUND (sa->seq))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100490 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000491 /*
492 * the sequence number is less than the lower bound.
493 */
Neale Ranns5b891102021-06-28 13:31:28 +0000494 if (seq <= sa->seq)
Neale Ranns6afaae12019-07-17 15:07:14 +0000495 {
496 /*
497 * the packet is within the window upper bound.
498 * check for duplicates.
499 */
Neale Ranns5b891102021-06-28 13:31:28 +0000500 if (hi_seq_req)
501 *hi_seq_req = sa->seq_hi;
502 return (ipsec_sa_anti_replay_check (sa, seq));
Neale Ranns6afaae12019-07-17 15:07:14 +0000503 }
504 else
505 {
506 /*
507 * the packet is less the window lower bound or greater than
508 * the higher bound, depending on how you look at it...
509 * We're assuming, given that the last sequence number received,
510 * TL < WINDOW_SIZE, that a largeer seq num is more likely to be
511 * a packet that moves the window forward, than a packet that has
512 * wrapped the high sequence again. If it were the latter then
513 * we've lost close to 2^32 packets.
514 */
Neale Ranns5b891102021-06-28 13:31:28 +0000515 if (hi_seq_req)
516 *hi_seq_req = sa->seq_hi;
Neale Ranns6afaae12019-07-17 15:07:14 +0000517 return 0;
518 }
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100519 }
520 else
521 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000522 /*
523 * the packet seq number is between the lower bound (a large nubmer)
524 * and MAX_SEQ_NUM. This is in the window since the window upper bound
525 * tl > 0.
526 * However, since TL is the other side of 0 to the received
527 * packet, the SA has moved on to a higher sequence number.
528 */
Neale Ranns5b891102021-06-28 13:31:28 +0000529 if (hi_seq_req)
530 *hi_seq_req = sa->seq_hi - 1;
531 return (ipsec_sa_anti_replay_check (sa, seq));
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100532 }
533 }
534
Neale Ranns5b891102021-06-28 13:31:28 +0000535 /* unhandled case */
536 ASSERT (0);
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100537 return 0;
538}
539
Neale Rannse11203e2021-09-21 12:34:19 +0000540always_inline u32
541ipsec_sa_anti_replay_window_shift (ipsec_sa_t *sa, u32 inc)
542{
543 u32 n_lost = 0;
544
545 if (inc < IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE)
546 {
547 if (sa->seq > IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE)
548 {
549 /*
550 * count how many holes there are in the portion
551 * of the window that we will right shift of the end
552 * as a result of this increments
553 */
554 u64 mask = (((u64) 1 << inc) - 1) << (BITS (u64) - inc);
555 u64 old = sa->replay_window & mask;
556 /* the number of packets we saw in this section of the window */
557 u64 seen = count_set_bits (old);
558
559 /*
560 * the number we missed is the size of the window section
561 * minus the number we saw.
562 */
563 n_lost = inc - seen;
564 }
565 sa->replay_window = ((sa->replay_window) << inc) | 1;
566 }
567 else
568 {
569 /* holes in the replay window are lost packets */
570 n_lost = BITS (u64) - count_set_bits (sa->replay_window);
571
572 /* any sequence numbers that now fall outside the window
573 * are forever lost */
574 n_lost += inc - IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE;
575
576 sa->replay_window = 1;
577 }
578
579 return (n_lost);
580}
581
Neale Ranns6afaae12019-07-17 15:07:14 +0000582/*
583 * Anti replay window advance
584 * inputs need to be in host byte order.
Neale Ranns5b891102021-06-28 13:31:28 +0000585 * This function both advances the anti-replay window and the sequence number
586 * We always need to move on the SN but the window updates are only needed
587 * if AR is on.
588 * However, updating the window is trivial, so we do it anyway to save
589 * the branch cost.
Neale Ranns6afaae12019-07-17 15:07:14 +0000590 */
Neale Rannse11203e2021-09-21 12:34:19 +0000591always_inline u64
592ipsec_sa_anti_replay_advance (ipsec_sa_t *sa, u32 thread_index, u32 seq,
593 u32 hi_seq)
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100594{
Neale Rannse11203e2021-09-21 12:34:19 +0000595 u64 n_lost = 0;
Neale Ranns6afaae12019-07-17 15:07:14 +0000596 u32 pos;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100597
Neale Ranns5b891102021-06-28 13:31:28 +0000598 if (ipsec_sa_is_set_USE_ESN (sa))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100599 {
Neale Ranns5b891102021-06-28 13:31:28 +0000600 int wrap = hi_seq - sa->seq_hi;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100601
Neale Ranns5b891102021-06-28 13:31:28 +0000602 if (wrap == 0 && seq > sa->seq)
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100603 {
Neale Ranns5b891102021-06-28 13:31:28 +0000604 pos = seq - sa->seq;
Neale Rannse11203e2021-09-21 12:34:19 +0000605 n_lost = ipsec_sa_anti_replay_window_shift (sa, pos);
Neale Ranns5b891102021-06-28 13:31:28 +0000606 sa->seq = seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100607 }
608 else if (wrap > 0)
609 {
Neale Ranns5b891102021-06-28 13:31:28 +0000610 pos = ~seq + sa->seq + 1;
Neale Rannse11203e2021-09-21 12:34:19 +0000611 n_lost = ipsec_sa_anti_replay_window_shift (sa, pos);
Neale Ranns5b891102021-06-28 13:31:28 +0000612 sa->seq = seq;
613 sa->seq_hi = hi_seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100614 }
615 else if (wrap < 0)
616 {
Neale Ranns5b891102021-06-28 13:31:28 +0000617 pos = ~seq + sa->seq + 1;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100618 sa->replay_window |= (1ULL << pos);
619 }
620 else
621 {
Neale Ranns5b891102021-06-28 13:31:28 +0000622 pos = sa->seq - seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100623 sa->replay_window |= (1ULL << pos);
624 }
625 }
626 else
627 {
Neale Ranns5b891102021-06-28 13:31:28 +0000628 if (seq > sa->seq)
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100629 {
Neale Ranns5b891102021-06-28 13:31:28 +0000630 pos = seq - sa->seq;
Neale Rannse11203e2021-09-21 12:34:19 +0000631 n_lost = ipsec_sa_anti_replay_window_shift (sa, pos);
Neale Ranns5b891102021-06-28 13:31:28 +0000632 sa->seq = seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100633 }
634 else
635 {
Neale Ranns5b891102021-06-28 13:31:28 +0000636 pos = sa->seq - seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100637 sa->replay_window |= (1ULL << pos);
638 }
639 }
Neale Rannse11203e2021-09-21 12:34:19 +0000640
641 return n_lost;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100642}
643
Neale Rannsf62a8c02019-04-02 08:13:33 +0000644
645/*
646 * Makes choice for thread_id should be assigned.
647 * if input ~0, gets random worker_id based on unix_time_now_nsec
648*/
Benoît Ganne5527a782022-01-18 15:56:41 +0100649always_inline u16
650ipsec_sa_assign_thread (u16 thread_id)
Neale Rannsf62a8c02019-04-02 08:13:33 +0000651{
652 return ((thread_id) ? thread_id
653 : (unix_time_now_nsec () % vlib_num_workers ()) + 1);
654}
655
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000656always_inline ipsec_sa_t *
657ipsec_sa_get (u32 sa_index)
658{
659 return (pool_elt_at_index (ipsec_sa_pool, sa_index));
660}
661
Neale Ranns999c8ee2019-02-01 03:31:24 -0800662#endif /* __IPSEC_SPD_SA_H__ */
663
664/*
665 * fd.io coding-style-patch-verification: ON
666 *
667 * Local Variables:
668 * eval: (c-set-style "gnu")
669 * End:
670 */