IPSEC: move SA counters into the stats segment
[vpp.git] / src / vnet / ipsec / ipsec_sa.h
1 /*
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>
20 #include <vnet/fib/fib_node.h>
21
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
36 typedef 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
53 typedef 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
61 typedef enum
62 {
63   IPSEC_PROTOCOL_AH = 0,
64   IPSEC_PROTOCOL_ESP = 1
65 } ipsec_protocol_t;
66
67 #define IPSEC_N_PROTOCOLS (IPSEC_PROTOCOL_ESP+1)
68
69 #define IPSEC_KEY_MAX_LEN 128
70 typedef 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
92 typedef enum ipsec_sad_flags_t_
93 {
94 #define _(v, f, s) IPSEC_SA_FLAG_##f = v,
95   foreach_ipsec_sa_flags
96 #undef _
97 } ipsec_sa_flags_t;
98
99 typedef struct
100 {
101   fib_node_t node;
102   u32 id;
103   u32 spi;
104   u32 stat_index;
105   ipsec_protocol_t protocol;
106
107   ipsec_crypto_alg_t crypto_alg;
108   ipsec_key_t crypto_key;
109
110   ipsec_integ_alg_t integ_alg;
111   ipsec_key_t integ_key;
112
113   u8 use_esn;
114   u8 use_anti_replay;
115
116   u8 is_tunnel;
117   u8 is_tunnel_ip6;
118   u8 udp_encap;
119   ip46_address_t tunnel_src_addr;
120   ip46_address_t tunnel_dst_addr;
121
122   fib_node_index_t fib_entry_index;
123   u32 sibling;
124   dpo_id_t dpo[IPSEC_N_PROTOCOLS];
125
126   u32 tx_fib_index;
127   u32 salt;
128
129   /* runtime */
130   u32 seq;
131   u32 seq_hi;
132   u32 last_seq;
133   u32 last_seq_hi;
134   u64 replay_window;
135 } ipsec_sa_t;
136
137 /**
138  * @brief
139  * SA packet & bytes counters
140  */
141 extern vlib_combined_counter_main_t ipsec_sa_counters;
142
143 extern void ipsec_mk_key (ipsec_key_t * key, const u8 * data, u8 len);
144
145 extern int ipsec_sa_add (u32 id,
146                          u32 spi,
147                          ipsec_protocol_t proto,
148                          ipsec_crypto_alg_t crypto_alg,
149                          const ipsec_key_t * ck,
150                          ipsec_integ_alg_t integ_alg,
151                          const ipsec_key_t * ik,
152                          ipsec_sa_flags_t flags,
153                          u32 tx_table_id,
154                          const ip46_address_t * tunnel_src_addr,
155                          const ip46_address_t * tunnel_dst_addr,
156                          u32 * sa_index);
157 extern u32 ipsec_sa_del (u32 id);
158 extern void ipsec_sa_stack (ipsec_sa_t * sa);
159
160 extern u8 ipsec_is_sa_used (u32 sa_index);
161 extern int ipsec_set_sa_key (u32 id,
162                              const ipsec_key_t * ck, const ipsec_key_t * ik);
163 extern u32 ipsec_get_sa_index_by_sa_id (u32 sa_id);
164
165 typedef walk_rc_t (*ipsec_sa_walk_cb_t) (ipsec_sa_t * sa, void *ctx);
166 extern void ipsec_sa_walk (ipsec_sa_walk_cb_t cd, void *ctx);
167
168 extern u8 *format_ipsec_crypto_alg (u8 * s, va_list * args);
169 extern u8 *format_ipsec_integ_alg (u8 * s, va_list * args);
170 extern u8 *format_ipsec_sa (u8 * s, va_list * args);
171 extern u8 *format_ipsec_key (u8 * s, va_list * args);
172 extern uword unformat_ipsec_crypto_alg (unformat_input_t * input,
173                                         va_list * args);
174 extern uword unformat_ipsec_integ_alg (unformat_input_t * input,
175                                        va_list * args);
176 extern uword unformat_ipsec_key (unformat_input_t * input, va_list * args);
177
178 #endif /* __IPSEC_SPD_SA_H__ */
179
180 /*
181  * fd.io coding-style-patch-verification: ON
182  *
183  * Local Variables:
184  * eval: (c-set-style "gnu")
185  * End:
186  */