blob: ab1e3cdb1f9ae66af3ca06b258d92ac56fb3d3c4 [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);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000286extern index_t ipsec_sa_find_and_lock (u32 id);
287extern int ipsec_sa_unlock_id (u32 id);
288extern void ipsec_sa_unlock (index_t sai);
Neale Ranns12989b52019-09-26 16:20:19 +0000289extern void ipsec_sa_lock (index_t sai);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800290extern void ipsec_sa_clear (index_t sai);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100291extern void ipsec_sa_set_crypto_alg (ipsec_sa_t * sa,
292 ipsec_crypto_alg_t crypto_alg);
293extern void ipsec_sa_set_integ_alg (ipsec_sa_t * sa,
294 ipsec_integ_alg_t integ_alg);
Benoît Ganne5527a782022-01-18 15:56:41 +0100295extern void ipsec_sa_set_async_mode (ipsec_sa_t *sa, int is_enabled);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800296
Neale Rannsb4cfd552019-02-13 02:08:06 -0800297typedef walk_rc_t (*ipsec_sa_walk_cb_t) (ipsec_sa_t * sa, void *ctx);
298extern void ipsec_sa_walk (ipsec_sa_walk_cb_t cd, void *ctx);
299
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000300extern u8 *format_ipsec_replay_window (u8 *s, va_list *args);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800301extern u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
302extern u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800303extern u8 *format_ipsec_sa (u8 * s, va_list * args);
304extern u8 *format_ipsec_key (u8 * s, va_list * args);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800305extern uword unformat_ipsec_crypto_alg (unformat_input_t * input,
306 va_list * args);
307extern uword unformat_ipsec_integ_alg (unformat_input_t * input,
308 va_list * args);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800309extern uword unformat_ipsec_key (unformat_input_t * input, va_list * args);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800310
Filip Tehlare5d34912020-03-02 15:17:37 +0000311#define IPSEC_UDP_PORT_NONE ((u16)~0)
312
Neale Ranns6afaae12019-07-17 15:07:14 +0000313/*
314 * Anti Replay definitions
315 */
316
317#define IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE (64)
318#define IPSEC_SA_ANTI_REPLAY_WINDOW_MAX_INDEX (IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE-1)
319
320/*
321 * sequence number less than the lower bound are outside of the window
322 * From RFC4303 Appendix A:
323 * Bl = Tl - W + 1
324 */
325#define IPSEC_SA_ANTI_REPLAY_WINDOW_LOWER_BOUND(_tl) (_tl - IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE + 1)
326
Neale Ranns5b891102021-06-28 13:31:28 +0000327always_inline int
328ipsec_sa_anti_replay_check (const ipsec_sa_t *sa, u32 seq)
329{
330 if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) &&
331 sa->replay_window & (1ULL << (sa->seq - seq)))
332 return 1;
333 else
334 return 0;
335}
336
Neale Ranns6afaae12019-07-17 15:07:14 +0000337/*
338 * Anti replay check.
339 * inputs need to be in host byte order.
Neale Ranns5b891102021-06-28 13:31:28 +0000340 *
341 * The function runs in two contexts. pre and post decrypt.
342 * Pre-decrypt it:
343 * 1 - determines if a packet is a replay - a simple check in the window
344 * 2 - returns the hi-seq number that should be used to decrypt.
345 * post-decrypt:
346 * Checks whether the packet is a replay or falls out of window
347 *
348 * This funcion should be called even without anti-replay enabled to ensure
349 * the high sequence number is set.
Neale Ranns6afaae12019-07-17 15:07:14 +0000350 */
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100351always_inline int
Neale Ranns5b891102021-06-28 13:31:28 +0000352ipsec_sa_anti_replay_and_sn_advance (const ipsec_sa_t *sa, u32 seq,
353 u32 hi_seq_used, bool post_decrypt,
354 u32 *hi_seq_req)
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100355{
Neale Ranns5b891102021-06-28 13:31:28 +0000356 ASSERT ((post_decrypt == false) == (hi_seq_req != 0));
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100357
Neale Ranns6afaae12019-07-17 15:07:14 +0000358 if (!ipsec_sa_is_set_USE_ESN (sa))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100359 {
Neale Ranns5b891102021-06-28 13:31:28 +0000360 if (hi_seq_req)
361 /* no ESN, therefore the hi-seq is always 0 */
362 *hi_seq_req = 0;
363
364 if (!ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100365 return 0;
366
Neale Ranns5b891102021-06-28 13:31:28 +0000367 if (PREDICT_TRUE (seq > sa->seq))
368 return 0;
369
370 u32 diff = sa->seq - seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100371
372 if (IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE > diff)
Neale Ranns5b891102021-06-28 13:31:28 +0000373 return ((sa->replay_window & (1ULL << diff)) ? 1 : 0);
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100374 else
375 return 1;
376
377 return 0;
378 }
379
Neale Ranns5b891102021-06-28 13:31:28 +0000380 if (!ipsec_sa_is_set_USE_ANTI_REPLAY (sa))
381 {
382 /* there's no AR configured for this SA, but in order
383 * to know whether a packet has wrapped the hi ESN we need
384 * to know whether it is out of window. if we use the default
385 * lower bound then we are effectively forcing AR because
386 * out of window packets will get the increased hi seq number
387 * and will thus fail to decrypt. IOW we need a window to know
388 * if the SN has wrapped, but we don't want a window to check for
389 * anti replay. to resolve the contradiction we use a huge window.
390 * if the packet is not within 2^30 of the current SN, we'll consider
391 * it a wrap.
392 */
393 if (hi_seq_req)
394 {
395 if (seq >= sa->seq)
396 /* The packet's sequence number is larger that the SA's.
397 * that can't be a warp - unless we lost more than
398 * 2^32 packets ... how could we know? */
399 *hi_seq_req = sa->seq_hi;
400 else
401 {
402 /* The packet's SN is less than the SAs, so either the SN has
403 * wrapped or the SN is just old. */
404 if (sa->seq - seq > (1 << 30))
405 /* It's really really really old => it wrapped */
406 *hi_seq_req = sa->seq_hi + 1;
407 else
408 *hi_seq_req = sa->seq_hi;
409 }
410 }
411 /*
412 * else
413 * this is post-decrpyt and since it decrypted we accept it
414 */
415 return 0;
416 }
417 if (PREDICT_TRUE (sa->seq >= (IPSEC_SA_ANTI_REPLAY_WINDOW_MAX_INDEX)))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100418 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000419 /*
420 * the last sequence number VPP recieved is more than one
421 * window size greater than zero.
422 * Case A from RFC4303 Appendix A.
423 */
Neale Ranns5b891102021-06-28 13:31:28 +0000424 if (seq < IPSEC_SA_ANTI_REPLAY_WINDOW_LOWER_BOUND (sa->seq))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100425 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000426 /*
427 * the received sequence number is lower than the lower bound
428 * of the window, this could mean either a replay packet or that
429 * the high sequence number has wrapped. if it decrypts corrently
430 * then it's the latter.
431 */
Neale Ranns5b891102021-06-28 13:31:28 +0000432 if (post_decrypt)
433 {
434 if (hi_seq_used == sa->seq_hi)
435 /* the high sequence number used to succesfully decrypt this
436 * packet is the same as the last-sequnence number of the SA.
437 * that means this packet did not cause a wrap.
438 * this packet is thus out of window and should be dropped */
439 return 1;
440 else
441 /* The packet decrypted with a different high sequence number
442 * to the SA, that means it is the wrap packet and should be
443 * accepted */
444 return 0;
445 }
446 else
447 {
448 /* pre-decrypt it might be the might that casues a wrap, we
449 * need to decrpyt to find out */
450 if (hi_seq_req)
451 *hi_seq_req = sa->seq_hi + 1;
452 return 0;
453 }
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100454 }
455 else
456 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000457 /*
458 * the recieved sequence number greater than the low
459 * end of the window.
460 */
Neale Ranns5b891102021-06-28 13:31:28 +0000461 if (hi_seq_req)
462 *hi_seq_req = sa->seq_hi;
463 if (seq <= sa->seq)
Neale Ranns6afaae12019-07-17 15:07:14 +0000464 /*
465 * The recieved seq number is within bounds of the window
466 * check if it's a duplicate
467 */
Neale Ranns5b891102021-06-28 13:31:28 +0000468 return (ipsec_sa_anti_replay_check (sa, seq));
Neale Ranns6afaae12019-07-17 15:07:14 +0000469 else
470 /*
471 * The received sequence number is greater than the window
472 * upper bound. this packet will move the window along, assuming
473 * it decrypts correctly.
474 */
475 return 0;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100476 }
477 }
478 else
479 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000480 /*
481 * the last sequence number VPP recieved is within one window
482 * size of zero, i.e. 0 < TL < WINDOW_SIZE, the lower bound is thus a
483 * large sequence number.
484 * Note that the check below uses unsiged integer arthimetic, so the
485 * RHS will be a larger number.
486 * Case B from RFC4303 Appendix A.
487 */
Neale Ranns5b891102021-06-28 13:31:28 +0000488 if (seq < IPSEC_SA_ANTI_REPLAY_WINDOW_LOWER_BOUND (sa->seq))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100489 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000490 /*
491 * the sequence number is less than the lower bound.
492 */
Neale Ranns5b891102021-06-28 13:31:28 +0000493 if (seq <= sa->seq)
Neale Ranns6afaae12019-07-17 15:07:14 +0000494 {
495 /*
496 * the packet is within the window upper bound.
497 * check for duplicates.
498 */
Neale Ranns5b891102021-06-28 13:31:28 +0000499 if (hi_seq_req)
500 *hi_seq_req = sa->seq_hi;
501 return (ipsec_sa_anti_replay_check (sa, seq));
Neale Ranns6afaae12019-07-17 15:07:14 +0000502 }
503 else
504 {
505 /*
506 * the packet is less the window lower bound or greater than
507 * the higher bound, depending on how you look at it...
508 * We're assuming, given that the last sequence number received,
509 * TL < WINDOW_SIZE, that a largeer seq num is more likely to be
510 * a packet that moves the window forward, than a packet that has
511 * wrapped the high sequence again. If it were the latter then
512 * we've lost close to 2^32 packets.
513 */
Neale Ranns5b891102021-06-28 13:31:28 +0000514 if (hi_seq_req)
515 *hi_seq_req = sa->seq_hi;
Neale Ranns6afaae12019-07-17 15:07:14 +0000516 return 0;
517 }
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100518 }
519 else
520 {
Neale Ranns6afaae12019-07-17 15:07:14 +0000521 /*
522 * the packet seq number is between the lower bound (a large nubmer)
523 * and MAX_SEQ_NUM. This is in the window since the window upper bound
524 * tl > 0.
525 * However, since TL is the other side of 0 to the received
526 * packet, the SA has moved on to a higher sequence number.
527 */
Neale Ranns5b891102021-06-28 13:31:28 +0000528 if (hi_seq_req)
529 *hi_seq_req = sa->seq_hi - 1;
530 return (ipsec_sa_anti_replay_check (sa, seq));
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100531 }
532 }
533
Neale Ranns5b891102021-06-28 13:31:28 +0000534 /* unhandled case */
535 ASSERT (0);
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100536 return 0;
537}
538
Neale Rannse11203e2021-09-21 12:34:19 +0000539always_inline u32
540ipsec_sa_anti_replay_window_shift (ipsec_sa_t *sa, u32 inc)
541{
542 u32 n_lost = 0;
543
544 if (inc < IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE)
545 {
546 if (sa->seq > IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE)
547 {
548 /*
549 * count how many holes there are in the portion
550 * of the window that we will right shift of the end
551 * as a result of this increments
552 */
553 u64 mask = (((u64) 1 << inc) - 1) << (BITS (u64) - inc);
554 u64 old = sa->replay_window & mask;
555 /* the number of packets we saw in this section of the window */
556 u64 seen = count_set_bits (old);
557
558 /*
559 * the number we missed is the size of the window section
560 * minus the number we saw.
561 */
562 n_lost = inc - seen;
563 }
564 sa->replay_window = ((sa->replay_window) << inc) | 1;
565 }
566 else
567 {
568 /* holes in the replay window are lost packets */
569 n_lost = BITS (u64) - count_set_bits (sa->replay_window);
570
571 /* any sequence numbers that now fall outside the window
572 * are forever lost */
573 n_lost += inc - IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE;
574
575 sa->replay_window = 1;
576 }
577
578 return (n_lost);
579}
580
Neale Ranns6afaae12019-07-17 15:07:14 +0000581/*
582 * Anti replay window advance
583 * inputs need to be in host byte order.
Neale Ranns5b891102021-06-28 13:31:28 +0000584 * This function both advances the anti-replay window and the sequence number
585 * We always need to move on the SN but the window updates are only needed
586 * if AR is on.
587 * However, updating the window is trivial, so we do it anyway to save
588 * the branch cost.
Neale Ranns6afaae12019-07-17 15:07:14 +0000589 */
Neale Rannse11203e2021-09-21 12:34:19 +0000590always_inline u64
591ipsec_sa_anti_replay_advance (ipsec_sa_t *sa, u32 thread_index, u32 seq,
592 u32 hi_seq)
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100593{
Neale Rannse11203e2021-09-21 12:34:19 +0000594 u64 n_lost = 0;
Neale Ranns6afaae12019-07-17 15:07:14 +0000595 u32 pos;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100596
Neale Ranns5b891102021-06-28 13:31:28 +0000597 if (ipsec_sa_is_set_USE_ESN (sa))
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100598 {
Neale Ranns5b891102021-06-28 13:31:28 +0000599 int wrap = hi_seq - sa->seq_hi;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100600
Neale Ranns5b891102021-06-28 13:31:28 +0000601 if (wrap == 0 && seq > sa->seq)
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100602 {
Neale Ranns5b891102021-06-28 13:31:28 +0000603 pos = seq - sa->seq;
Neale Rannse11203e2021-09-21 12:34:19 +0000604 n_lost = ipsec_sa_anti_replay_window_shift (sa, pos);
Neale Ranns5b891102021-06-28 13:31:28 +0000605 sa->seq = seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100606 }
607 else if (wrap > 0)
608 {
Neale Ranns5b891102021-06-28 13:31:28 +0000609 pos = ~seq + sa->seq + 1;
Neale Rannse11203e2021-09-21 12:34:19 +0000610 n_lost = ipsec_sa_anti_replay_window_shift (sa, pos);
Neale Ranns5b891102021-06-28 13:31:28 +0000611 sa->seq = seq;
612 sa->seq_hi = hi_seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100613 }
614 else if (wrap < 0)
615 {
Neale Ranns5b891102021-06-28 13:31:28 +0000616 pos = ~seq + sa->seq + 1;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100617 sa->replay_window |= (1ULL << pos);
618 }
619 else
620 {
Neale Ranns5b891102021-06-28 13:31:28 +0000621 pos = sa->seq - seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100622 sa->replay_window |= (1ULL << pos);
623 }
624 }
625 else
626 {
Neale Ranns5b891102021-06-28 13:31:28 +0000627 if (seq > sa->seq)
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100628 {
Neale Ranns5b891102021-06-28 13:31:28 +0000629 pos = seq - sa->seq;
Neale Rannse11203e2021-09-21 12:34:19 +0000630 n_lost = ipsec_sa_anti_replay_window_shift (sa, pos);
Neale Ranns5b891102021-06-28 13:31:28 +0000631 sa->seq = seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100632 }
633 else
634 {
Neale Ranns5b891102021-06-28 13:31:28 +0000635 pos = sa->seq - seq;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100636 sa->replay_window |= (1ULL << pos);
637 }
638 }
Neale Rannse11203e2021-09-21 12:34:19 +0000639
640 return n_lost;
Damjan Marion1f4e1cb2019-03-28 19:19:31 +0100641}
642
Neale Rannsf62a8c02019-04-02 08:13:33 +0000643
644/*
645 * Makes choice for thread_id should be assigned.
646 * if input ~0, gets random worker_id based on unix_time_now_nsec
647*/
Benoît Ganne5527a782022-01-18 15:56:41 +0100648always_inline u16
649ipsec_sa_assign_thread (u16 thread_id)
Neale Rannsf62a8c02019-04-02 08:13:33 +0000650{
651 return ((thread_id) ? thread_id
652 : (unix_time_now_nsec () % vlib_num_workers ()) + 1);
653}
654
Neale Rannsc5fe57d2021-02-25 16:01:28 +0000655always_inline ipsec_sa_t *
656ipsec_sa_get (u32 sa_index)
657{
658 return (pool_elt_at_index (ipsec_sa_pool, sa_index));
659}
660
Neale Ranns999c8ee2019-02-01 03:31:24 -0800661#endif /* __IPSEC_SPD_SA_H__ */
662
663/*
664 * fd.io coding-style-patch-verification: ON
665 *
666 * Local Variables:
667 * eval: (c-set-style "gnu")
668 * End:
669 */