blob: 1cd215340f5f6c423e3f0e2a5e8ca1e069b72de1 [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
22#define foreach_ipsec_crypto_alg \
23 _ (0, NONE, "none") \
24 _ (1, AES_CBC_128, "aes-cbc-128") \
25 _ (2, AES_CBC_192, "aes-cbc-192") \
26 _ (3, AES_CBC_256, "aes-cbc-256") \
27 _ (4, AES_CTR_128, "aes-ctr-128") \
28 _ (5, AES_CTR_192, "aes-ctr-192") \
29 _ (6, AES_CTR_256, "aes-ctr-256") \
30 _ (7, AES_GCM_128, "aes-gcm-128") \
31 _ (8, AES_GCM_192, "aes-gcm-192") \
32 _ (9, AES_GCM_256, "aes-gcm-256") \
33 _ (10, DES_CBC, "des-cbc") \
34 _ (11, 3DES_CBC, "3des-cbc")
35
36typedef enum
37{
38#define _(v, f, s) IPSEC_CRYPTO_ALG_##f = v,
39 foreach_ipsec_crypto_alg
40#undef _
41 IPSEC_CRYPTO_N_ALG,
42} ipsec_crypto_alg_t;
43
44#define foreach_ipsec_integ_alg \
45 _ (0, NONE, "none") \
46 _ (1, MD5_96, "md5-96") /* RFC2403 */ \
47 _ (2, SHA1_96, "sha1-96") /* RFC2404 */ \
48 _ (3, SHA_256_96, "sha-256-96") /* draft-ietf-ipsec-ciph-sha-256-00 */ \
49 _ (4, SHA_256_128, "sha-256-128") /* RFC4868 */ \
50 _ (5, SHA_384_192, "sha-384-192") /* RFC4868 */ \
51 _ (6, SHA_512_256, "sha-512-256") /* RFC4868 */
52
53typedef enum
54{
55#define _(v, f, s) IPSEC_INTEG_ALG_##f = v,
56 foreach_ipsec_integ_alg
57#undef _
58 IPSEC_INTEG_N_ALG,
59} ipsec_integ_alg_t;
60
61typedef enum
62{
63 IPSEC_PROTOCOL_AH = 0,
64 IPSEC_PROTOCOL_ESP = 1
65} ipsec_protocol_t;
66
Neale Ranns8d7c5022019-02-06 01:41:05 -080067#define IPSEC_N_PROTOCOLS (IPSEC_PROTOCOL_ESP+1)
68
69#define IPSEC_KEY_MAX_LEN 128
70typedef struct ipsec_key_t_
71{
72 u8 len;
73 u8 data[IPSEC_KEY_MAX_LEN];
74} ipsec_key_t;
75
76/*
77 * Enable extended sequence numbers
78 * Enable Anti-replay
79 * IPsec tunnel mode if non-zero, else transport mode
80 * IPsec tunnel mode is IPv6 if non-zero,
81 * else IPv4 tunnel only valid if is_tunnel is non-zero
82 * enable UDP encapsulation for NAT traversal
83 */
84#define foreach_ipsec_sa_flags \
85 _ (0, NONE, "none") \
86 _ (1, USE_EXTENDED_SEQ_NUM, "esn") \
87 _ (2, USE_ANTI_REPLAY, "anti-replay") \
88 _ (4, IS_TUNNEL, "tunnel") \
89 _ (8, IS_TUNNEL_V6, "tunnel-v6") \
90 _ (16, UDP_ENCAP, "udp-encap") \
91
92typedef enum ipsec_sad_flags_t_
93{
94#define _(v, f, s) IPSEC_SA_FLAG_##f = v,
95 foreach_ipsec_sa_flags
96#undef _
Damjan Mariond709cbc2019-03-26 13:16:42 +010097} __attribute__ ((packed)) ipsec_sa_flags_t;
98
99STATIC_ASSERT (sizeof (ipsec_sa_flags_t) == 1, "IPSEC SA flags > 1 byte");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800100
Neale Ranns999c8ee2019-02-01 03:31:24 -0800101typedef struct
102{
Damjan Mariond709cbc2019-03-26 13:16:42 +0100103 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800104
Damjan Mariond709cbc2019-03-26 13:16:42 +0100105 /* flags */
106 ipsec_sa_flags_t flags;
107
Damjan Marionb966e8b2019-03-20 16:07:09 +0100108 u8 crypto_iv_size;
109 u8 crypto_block_size;
Damjan Marionb966e8b2019-03-20 16:07:09 +0100110 u8 integ_trunc_size;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100111 u32 spi;
Neale Ranns999c8ee2019-02-01 03:31:24 -0800112 u32 seq;
113 u32 seq_hi;
114 u32 last_seq;
115 u32 last_seq_hi;
116 u64 replay_window;
Damjan Mariond709cbc2019-03-26 13:16:42 +0100117
118 vnet_crypto_op_type_t crypto_enc_op_type;
119 vnet_crypto_op_type_t crypto_dec_op_type;
120 vnet_crypto_op_type_t integ_op_type;
121
122 dpo_id_t dpo[IPSEC_N_PROTOCOLS];
123
124 /* data accessed by dataplane code should be above this comment */
125 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
126
127 union
128 {
129 ip4_header_t ip4_hdr;
130 ip6_header_t ip6_hdr;
131 };
132 udp_header_t udp_hdr;
133
134
135 fib_node_t node;
136 u32 id;
137 u32 stat_index;
138 ipsec_protocol_t protocol;
139
140 ipsec_crypto_alg_t crypto_alg;
141 ipsec_key_t crypto_key;
142
143 ipsec_integ_alg_t integ_alg;
144 ipsec_key_t integ_key;
145
146 ip46_address_t tunnel_src_addr;
147 ip46_address_t tunnel_dst_addr;
148
149 fib_node_index_t fib_entry_index;
150 u32 sibling;
151
152 u32 tx_fib_index;
153 u32 salt;
154
155 /* runtime */
Neale Ranns999c8ee2019-02-01 03:31:24 -0800156} ipsec_sa_t;
157
Damjan Mariond709cbc2019-03-26 13:16:42 +0100158STATIC_ASSERT_OFFSET_OF (ipsec_sa_t, cacheline1, CLIB_CACHE_LINE_BYTES);
159
160#define _(a,v,s) \
161 always_inline int \
162 ipsec_sa_is_set_##v (const ipsec_sa_t *sa) { \
163 return (sa->flags & IPSEC_SA_FLAG_##v); \
164 }
165foreach_ipsec_sa_flags
166#undef _
167#define _(a,v,s) \
168 always_inline int \
169 ipsec_sa_set_##v (ipsec_sa_t *sa) { \
170 return (sa->flags |= IPSEC_SA_FLAG_##v); \
171 }
172 foreach_ipsec_sa_flags
173#undef _
Neale Rannseba31ec2019-02-17 18:04:27 +0000174/**
175 * @brief
176 * SA packet & bytes counters
177 */
178extern vlib_combined_counter_main_t ipsec_sa_counters;
179
Neale Ranns8d7c5022019-02-06 01:41:05 -0800180extern void ipsec_mk_key (ipsec_key_t * key, const u8 * data, u8 len);
181
182extern int ipsec_sa_add (u32 id,
183 u32 spi,
184 ipsec_protocol_t proto,
185 ipsec_crypto_alg_t crypto_alg,
186 const ipsec_key_t * ck,
187 ipsec_integ_alg_t integ_alg,
188 const ipsec_key_t * ik,
189 ipsec_sa_flags_t flags,
190 u32 tx_table_id,
191 const ip46_address_t * tunnel_src_addr,
192 const ip46_address_t * tunnel_dst_addr,
193 u32 * sa_index);
194extern u32 ipsec_sa_del (u32 id);
Neale Rannsb4cfd552019-02-13 02:08:06 -0800195extern void ipsec_sa_stack (ipsec_sa_t * sa);
Damjan Marionb966e8b2019-03-20 16:07:09 +0100196extern void ipsec_sa_set_crypto_alg (ipsec_sa_t * sa,
197 ipsec_crypto_alg_t crypto_alg);
198extern void ipsec_sa_set_integ_alg (ipsec_sa_t * sa,
199 ipsec_integ_alg_t integ_alg);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800200
Neale Ranns999c8ee2019-02-01 03:31:24 -0800201extern u8 ipsec_is_sa_used (u32 sa_index);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800202extern int ipsec_set_sa_key (u32 id,
203 const ipsec_key_t * ck, const ipsec_key_t * ik);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800204extern u32 ipsec_get_sa_index_by_sa_id (u32 sa_id);
205
Neale Rannsb4cfd552019-02-13 02:08:06 -0800206typedef walk_rc_t (*ipsec_sa_walk_cb_t) (ipsec_sa_t * sa, void *ctx);
207extern void ipsec_sa_walk (ipsec_sa_walk_cb_t cd, void *ctx);
208
Neale Ranns999c8ee2019-02-01 03:31:24 -0800209extern u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
210extern u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800211extern u8 *format_ipsec_sa (u8 * s, va_list * args);
212extern u8 *format_ipsec_key (u8 * s, va_list * args);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800213extern uword unformat_ipsec_crypto_alg (unformat_input_t * input,
214 va_list * args);
215extern uword unformat_ipsec_integ_alg (unformat_input_t * input,
216 va_list * args);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800217extern uword unformat_ipsec_key (unformat_input_t * input, va_list * args);
Neale Ranns999c8ee2019-02-01 03:31:24 -0800218
219#endif /* __IPSEC_SPD_SA_H__ */
220
221/*
222 * fd.io coding-style-patch-verification: ON
223 *
224 * Local Variables:
225 * eval: (c-set-style "gnu")
226 * End:
227 */