Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 15 | #ifndef __IPSEC_H__ |
| 16 | #define __IPSEC_H__ |
| 17 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 18 | #include <vnet/ip/ip.h> |
| 19 | #include <vnet/feature/feature.h> |
| 20 | |
Klement Sekera | 4f10db3 | 2018-11-12 14:32:19 +0100 | [diff] [blame] | 21 | #include <openssl/hmac.h> |
| 22 | #include <openssl/rand.h> |
| 23 | #include <openssl/evp.h> |
| 24 | |
| 25 | #include <vppinfra/types.h> |
| 26 | #include <vppinfra/cache.h> |
| 27 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 28 | #define IPSEC_FLAG_IPSEC_GRE_TUNNEL (1 << 0) |
| 29 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 30 | #define foreach_ipsec_output_next \ |
| 31 | _ (DROP, "error-drop") \ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 32 | _ (ESP4_ENCRYPT, "esp4-encrypt") \ |
| 33 | _ (AH4_ENCRYPT, "ah4-encrypt") \ |
| 34 | _ (ESP6_ENCRYPT, "esp6-encrypt") \ |
| 35 | _ (AH6_ENCRYPT, "ah6-encrypt") |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 36 | |
| 37 | #define _(v, s) IPSEC_OUTPUT_NEXT_##v, |
| 38 | typedef enum |
| 39 | { |
| 40 | foreach_ipsec_output_next |
| 41 | #undef _ |
| 42 | IPSEC_OUTPUT_N_NEXT, |
| 43 | } ipsec_output_next_t; |
| 44 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 45 | #define foreach_ipsec_input_next \ |
| 46 | _ (DROP, "error-drop") \ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 47 | _ (ESP4_DECRYPT, "esp4-decrypt") \ |
| 48 | _ (AH4_DECRYPT, "ah4-decrypt") \ |
| 49 | _ (ESP6_DECRYPT, "esp6-decrypt") \ |
| 50 | _ (AH6_DECRYPT, "ah6-decrypt") |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 51 | |
| 52 | #define _(v, s) IPSEC_INPUT_NEXT_##v, |
| 53 | typedef enum |
| 54 | { |
| 55 | foreach_ipsec_input_next |
| 56 | #undef _ |
| 57 | IPSEC_INPUT_N_NEXT, |
| 58 | } ipsec_input_next_t; |
| 59 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | #define foreach_ipsec_policy_action \ |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 61 | _ (0, BYPASS, "bypass") \ |
| 62 | _ (1, DISCARD, "discard") \ |
| 63 | _ (2, RESOLVE, "resolve") \ |
| 64 | _ (3, PROTECT, "protect") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 66 | typedef enum |
| 67 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 68 | #define _(v, f, s) IPSEC_POLICY_ACTION_##f = v, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | foreach_ipsec_policy_action |
| 70 | #undef _ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | } ipsec_policy_action_t; |
| 72 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 73 | #define IPSEC_POLICY_N_ACTION (IPSEC_POLICY_ACTION_PROTECT + 1) |
| 74 | |
| 75 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 76 | #define foreach_ipsec_crypto_alg \ |
| 77 | _ (0, NONE, "none") \ |
| 78 | _ (1, AES_CBC_128, "aes-cbc-128") \ |
| 79 | _ (2, AES_CBC_192, "aes-cbc-192") \ |
| 80 | _ (3, AES_CBC_256, "aes-cbc-256") \ |
| 81 | _ (4, AES_CTR_128, "aes-ctr-128") \ |
| 82 | _ (5, AES_CTR_192, "aes-ctr-192") \ |
| 83 | _ (6, AES_CTR_256, "aes-ctr-256") \ |
| 84 | _ (7, AES_GCM_128, "aes-gcm-128") \ |
| 85 | _ (8, AES_GCM_192, "aes-gcm-192") \ |
| 86 | _ (9, AES_GCM_256, "aes-gcm-256") \ |
| 87 | _ (10, DES_CBC, "des-cbc") \ |
| 88 | _ (11, 3DES_CBC, "3des-cbc") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 90 | typedef enum |
| 91 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 92 | #define _(v, f, s) IPSEC_CRYPTO_ALG_##f = v, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | foreach_ipsec_crypto_alg |
| 94 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 95 | IPSEC_CRYPTO_N_ALG, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | } ipsec_crypto_alg_t; |
| 97 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 98 | #define foreach_ipsec_integ_alg \ |
| 99 | _ (0, NONE, "none") \ |
| 100 | _ (1, MD5_96, "md5-96") /* RFC2403 */ \ |
| 101 | _ (2, SHA1_96, "sha1-96") /* RFC2404 */ \ |
| 102 | _ (3, SHA_256_96, "sha-256-96") /* draft-ietf-ipsec-ciph-sha-256-00 */ \ |
| 103 | _ (4, SHA_256_128, "sha-256-128") /* RFC4868 */ \ |
| 104 | _ (5, SHA_384_192, "sha-384-192") /* RFC4868 */ \ |
| 105 | _ (6, SHA_512_256, "sha-512-256") /* RFC4868 */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 107 | typedef enum |
| 108 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 109 | #define _(v, f, s) IPSEC_INTEG_ALG_##f = v, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | foreach_ipsec_integ_alg |
| 111 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 112 | IPSEC_INTEG_N_ALG, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | } ipsec_integ_alg_t; |
| 114 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 115 | typedef enum |
| 116 | { |
| 117 | IPSEC_PROTOCOL_AH = 0, |
| 118 | IPSEC_PROTOCOL_ESP = 1 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | } ipsec_protocol_t; |
| 120 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 121 | typedef struct |
| 122 | { |
| 123 | u32 id; |
| 124 | u32 spi; |
| 125 | ipsec_protocol_t protocol; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 127 | ipsec_crypto_alg_t crypto_alg; |
| 128 | u8 crypto_key_len; |
| 129 | u8 crypto_key[128]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 131 | ipsec_integ_alg_t integ_alg; |
| 132 | u8 integ_key_len; |
| 133 | u8 integ_key[128]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 135 | u8 use_esn; |
| 136 | u8 use_anti_replay; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 138 | u8 is_tunnel; |
| 139 | u8 is_tunnel_ip6; |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 140 | u8 udp_encap; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 141 | ip46_address_t tunnel_src_addr; |
| 142 | ip46_address_t tunnel_dst_addr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 144 | u32 tx_fib_index; |
Radu Nicolau | 6929ea9 | 2016-11-29 11:00:30 +0000 | [diff] [blame] | 145 | u32 salt; |
| 146 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 147 | /* runtime */ |
| 148 | u32 seq; |
| 149 | u32 seq_hi; |
| 150 | u32 last_seq; |
| 151 | u32 last_seq_hi; |
| 152 | u64 replay_window; |
Radu Nicolau | cb33dc2 | 2017-02-16 16:49:46 +0000 | [diff] [blame] | 153 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 154 | /* lifetime data */ |
Radu Nicolau | cb33dc2 | 2017-02-16 16:49:46 +0000 | [diff] [blame] | 155 | u64 total_data_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | } ipsec_sa_t; |
| 157 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 158 | typedef struct |
| 159 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | ip46_address_t start, stop; |
| 161 | } ip46_address_range_t; |
| 162 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 163 | typedef struct |
| 164 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | u16 start, stop; |
| 166 | } port_range_t; |
| 167 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 168 | typedef struct |
| 169 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | u8 is_add; |
| 171 | u8 esn; |
| 172 | u8 anti_replay; |
| 173 | ip4_address_t local_ip, remote_ip; |
| 174 | u32 local_spi; |
| 175 | u32 remote_spi; |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 176 | ipsec_crypto_alg_t crypto_alg; |
| 177 | u8 local_crypto_key_len; |
| 178 | u8 local_crypto_key[128]; |
| 179 | u8 remote_crypto_key_len; |
| 180 | u8 remote_crypto_key[128]; |
| 181 | ipsec_integ_alg_t integ_alg; |
| 182 | u8 local_integ_key_len; |
| 183 | u8 local_integ_key[128]; |
| 184 | u8 remote_integ_key_len; |
| 185 | u8 remote_integ_key[128]; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 186 | u8 renumber; |
| 187 | u32 show_instance; |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 188 | u8 udp_encap; |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 189 | u32 tx_table_id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | } ipsec_add_del_tunnel_args_t; |
| 191 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 192 | typedef struct |
| 193 | { |
| 194 | u8 is_add; |
| 195 | u32 local_sa_id; |
| 196 | u32 remote_sa_id; |
| 197 | ip4_address_t local_ip; |
| 198 | ip4_address_t remote_ip; |
| 199 | } ipsec_add_del_ipsec_gre_tunnel_args_t; |
| 200 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 201 | typedef enum |
| 202 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | IPSEC_IF_SET_KEY_TYPE_NONE, |
| 204 | IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO, |
| 205 | IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO, |
| 206 | IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG, |
| 207 | IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG, |
| 208 | } ipsec_if_set_key_type_t; |
| 209 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 210 | typedef struct |
| 211 | { |
| 212 | u32 id; |
| 213 | i32 priority; |
| 214 | u8 is_outbound; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 216 | // Selector |
| 217 | u8 is_ipv6; |
| 218 | ip46_address_range_t laddr; |
| 219 | ip46_address_range_t raddr; |
| 220 | u8 protocol; |
| 221 | port_range_t lport; |
| 222 | port_range_t rport; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 224 | // Policy |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 225 | ipsec_policy_action_t policy; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 226 | u32 sa_id; |
| 227 | u32 sa_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 229 | // Counter |
| 230 | vlib_counter_t counter; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | } ipsec_policy_t; |
| 232 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 233 | typedef struct |
| 234 | { |
| 235 | u32 id; |
| 236 | /* pool of policies */ |
| 237 | ipsec_policy_t *policies; |
| 238 | /* vectors of policy indices */ |
| 239 | u32 *ipv4_outbound_policies; |
| 240 | u32 *ipv6_outbound_policies; |
| 241 | u32 *ipv4_inbound_protect_policy_indices; |
| 242 | u32 *ipv4_inbound_policy_discard_and_bypass_indices; |
| 243 | u32 *ipv6_inbound_protect_policy_indices; |
| 244 | u32 *ipv6_inbound_policy_discard_and_bypass_indices; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | } ipsec_spd_t; |
| 246 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 247 | typedef struct |
| 248 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | u32 spd_index; |
| 250 | } ip4_ipsec_config_t; |
| 251 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 252 | typedef struct |
| 253 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | u32 spd_index; |
| 255 | } ip6_ipsec_config_t; |
| 256 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 257 | typedef struct |
| 258 | { |
Dave Barach | eb987d3 | 2018-05-03 08:26:39 -0400 | [diff] [blame] | 259 | /* Required for pool_get_aligned */ |
| 260 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | u32 input_sa_index; |
| 262 | u32 output_sa_index; |
| 263 | u32 hw_if_index; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 264 | u32 show_instance; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | } ipsec_tunnel_if_t; |
| 266 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 267 | typedef clib_error_t *(*add_del_sa_sess_cb_t) (u32 sa_index, u8 is_add); |
| 268 | typedef clib_error_t *(*check_support_cb_t) (ipsec_sa_t * sa); |
| 269 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 270 | typedef struct |
| 271 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 272 | u8 *name; |
| 273 | /* add/del callback */ |
| 274 | add_del_sa_sess_cb_t add_del_sa_sess_cb; |
| 275 | /* check support function */ |
| 276 | check_support_cb_t check_support_cb; |
| 277 | u32 ah4_encrypt_node_index; |
| 278 | u32 ah4_decrypt_node_index; |
| 279 | u32 ah4_encrypt_next_index; |
| 280 | u32 ah4_decrypt_next_index; |
| 281 | u32 ah6_encrypt_node_index; |
| 282 | u32 ah6_decrypt_node_index; |
| 283 | u32 ah6_encrypt_next_index; |
| 284 | u32 ah6_decrypt_next_index; |
| 285 | } ipsec_ah_backend_t; |
| 286 | |
| 287 | typedef struct |
| 288 | { |
| 289 | u8 *name; |
| 290 | /* add/del callback */ |
| 291 | add_del_sa_sess_cb_t add_del_sa_sess_cb; |
| 292 | /* check support function */ |
| 293 | check_support_cb_t check_support_cb; |
| 294 | u32 esp4_encrypt_node_index; |
| 295 | u32 esp4_decrypt_node_index; |
| 296 | u32 esp4_encrypt_next_index; |
| 297 | u32 esp4_decrypt_next_index; |
| 298 | u32 esp6_encrypt_node_index; |
| 299 | u32 esp6_decrypt_node_index; |
| 300 | u32 esp6_encrypt_next_index; |
| 301 | u32 esp6_decrypt_next_index; |
| 302 | } ipsec_esp_backend_t; |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 303 | |
| 304 | typedef struct |
| 305 | { |
Klement Sekera | 4f10db3 | 2018-11-12 14:32:19 +0100 | [diff] [blame] | 306 | const EVP_CIPHER *type; |
| 307 | u8 iv_size; |
| 308 | u8 block_size; |
| 309 | } ipsec_proto_main_crypto_alg_t; |
| 310 | |
| 311 | typedef struct |
| 312 | { |
| 313 | const EVP_MD *md; |
| 314 | u8 trunc_size; |
| 315 | } ipsec_proto_main_integ_alg_t; |
| 316 | |
| 317 | typedef struct |
| 318 | { |
| 319 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
| 320 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 321 | EVP_CIPHER_CTX *encrypt_ctx; |
| 322 | #else |
| 323 | EVP_CIPHER_CTX encrypt_ctx; |
| 324 | #endif |
| 325 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
| 326 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 327 | EVP_CIPHER_CTX *decrypt_ctx; |
| 328 | #else |
| 329 | EVP_CIPHER_CTX decrypt_ctx; |
| 330 | #endif |
| 331 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline2); |
| 332 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 333 | HMAC_CTX *hmac_ctx; |
| 334 | #else |
| 335 | HMAC_CTX hmac_ctx; |
| 336 | #endif |
| 337 | ipsec_crypto_alg_t last_encrypt_alg; |
| 338 | ipsec_crypto_alg_t last_decrypt_alg; |
| 339 | ipsec_integ_alg_t last_integ_alg; |
| 340 | } ipsec_proto_main_per_thread_data_t; |
| 341 | |
| 342 | typedef struct |
| 343 | { |
| 344 | ipsec_proto_main_crypto_alg_t *ipsec_proto_main_crypto_algs; |
| 345 | ipsec_proto_main_integ_alg_t *ipsec_proto_main_integ_algs; |
| 346 | ipsec_proto_main_per_thread_data_t *per_thread_data; |
| 347 | } ipsec_proto_main_t; |
| 348 | |
| 349 | extern ipsec_proto_main_t ipsec_proto_main; |
| 350 | |
| 351 | typedef struct |
| 352 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | /* pool of tunnel instances */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 354 | ipsec_spd_t *spds; |
| 355 | ipsec_sa_t *sad; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 356 | |
| 357 | /* pool of tunnel interfaces */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 358 | ipsec_tunnel_if_t *tunnel_interfaces; |
| 359 | u32 *free_tunnel_if_indices; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 361 | u32 **empty_buffers; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 363 | uword *tunnel_index_by_key; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | |
| 365 | /* convenience */ |
| 366 | vlib_main_t *vlib_main; |
| 367 | vnet_main_t *vnet_main; |
| 368 | |
| 369 | /* next node indices */ |
| 370 | u32 feature_next_node_index[32]; |
| 371 | |
| 372 | /* hashes */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 373 | uword *spd_index_by_spd_id; |
| 374 | uword *spd_index_by_sw_if_index; |
| 375 | uword *sa_index_by_sa_id; |
| 376 | uword *ipsec_if_pool_index_by_key; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 377 | uword *ipsec_if_real_dev_by_show_dev; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 379 | /* node indices */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | u32 error_drop_node_index; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 381 | u32 esp4_encrypt_node_index; |
| 382 | u32 esp4_decrypt_node_index; |
| 383 | u32 ah4_encrypt_node_index; |
| 384 | u32 ah4_decrypt_node_index; |
| 385 | u32 esp6_encrypt_node_index; |
| 386 | u32 esp6_decrypt_node_index; |
| 387 | u32 ah6_encrypt_node_index; |
| 388 | u32 ah6_decrypt_node_index; |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 389 | /* next node indices */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 390 | u32 esp4_encrypt_next_index; |
| 391 | u32 esp4_decrypt_next_index; |
| 392 | u32 ah4_encrypt_next_index; |
| 393 | u32 ah4_decrypt_next_index; |
| 394 | u32 esp6_encrypt_next_index; |
| 395 | u32 esp6_decrypt_next_index; |
| 396 | u32 ah6_encrypt_next_index; |
| 397 | u32 ah6_decrypt_next_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 399 | /* pool of ah backends */ |
| 400 | ipsec_ah_backend_t *ah_backends; |
| 401 | /* pool of esp backends */ |
| 402 | ipsec_esp_backend_t *esp_backends; |
| 403 | /* index of current ah backend */ |
| 404 | u32 ah_current_backend; |
| 405 | /* index of current esp backend */ |
| 406 | u32 esp_current_backend; |
| 407 | /* index of default ah backend */ |
| 408 | u32 ah_default_backend; |
| 409 | /* index of default esp backend */ |
| 410 | u32 esp_default_backend; |
Klement Sekera | ee52d87 | 2018-06-07 19:36:07 +0200 | [diff] [blame] | 411 | |
| 412 | /* helper for sort function */ |
| 413 | ipsec_spd_t *spd_to_sort; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | } ipsec_main_t; |
| 415 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 416 | extern ipsec_main_t ipsec_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 418 | clib_error_t *ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, |
| 419 | u8 is_add); |
| 420 | |
| 421 | clib_error_t *ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa); |
| 422 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 423 | extern vlib_node_registration_t esp4_encrypt_node; |
| 424 | extern vlib_node_registration_t esp4_decrypt_node; |
| 425 | extern vlib_node_registration_t ah4_encrypt_node; |
| 426 | extern vlib_node_registration_t ah4_decrypt_node; |
| 427 | extern vlib_node_registration_t esp6_encrypt_node; |
| 428 | extern vlib_node_registration_t esp6_decrypt_node; |
| 429 | extern vlib_node_registration_t ah6_encrypt_node; |
| 430 | extern vlib_node_registration_t ah6_decrypt_node; |
Jean-Mickael Guerin | 8941ec2 | 2016-03-04 14:14:21 +0100 | [diff] [blame] | 431 | extern vlib_node_registration_t ipsec_if_input_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 433 | /* |
| 434 | * functions |
| 435 | */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 436 | int ipsec_set_interface_spd (vlib_main_t * vm, u32 sw_if_index, u32 spd_id, |
| 437 | int is_add); |
| 438 | int ipsec_add_del_spd (vlib_main_t * vm, u32 spd_id, int is_add); |
| 439 | int ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy, |
| 440 | int is_add); |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 441 | int ipsec_add_del_sa (vlib_main_t * vm, ipsec_sa_t * new_sa, int is_add); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 442 | int ipsec_set_sa_key (vlib_main_t * vm, ipsec_sa_t * sa_update); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 444 | u32 ipsec_get_sa_index_by_sa_id (u32 sa_id); |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 445 | u8 ipsec_is_sa_used (u32 sa_index); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 446 | u8 *format_ipsec_policy_action (u8 * s, va_list * args); |
| 447 | u8 *format_ipsec_crypto_alg (u8 * s, va_list * args); |
| 448 | u8 *format_ipsec_integ_alg (u8 * s, va_list * args); |
| 449 | u8 *format_ipsec_replay_window (u8 * s, va_list * args); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | uword unformat_ipsec_policy_action (unformat_input_t * input, va_list * args); |
| 451 | uword unformat_ipsec_crypto_alg (unformat_input_t * input, va_list * args); |
| 452 | uword unformat_ipsec_integ_alg (unformat_input_t * input, va_list * args); |
| 453 | |
Matthew Smith | e04d09d | 2017-05-14 21:47:18 -0500 | [diff] [blame] | 454 | int ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm, |
| 455 | ipsec_add_del_tunnel_args_t * args, |
| 456 | u32 * sw_if_index); |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 457 | int ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args); |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 458 | int ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm, |
| 459 | ipsec_add_del_ipsec_gre_tunnel_args_t * |
| 460 | args); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 461 | int ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index, |
| 462 | ipsec_if_set_key_type_t type, u8 alg, u8 * key); |
Matthew Smith | ca514fd | 2017-10-12 12:06:59 -0500 | [diff] [blame] | 463 | int ipsec_set_interface_sa (vnet_main_t * vnm, u32 hw_if_index, u32 sa_id, |
| 464 | u8 is_outbound); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 465 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | /* |
| 467 | * inline functions |
| 468 | */ |
| 469 | |
| 470 | always_inline void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 471 | ipsec_alloc_empty_buffers (vlib_main_t * vm, ipsec_main_t * im) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | { |
Damjan Marion | 067cd62 | 2018-07-11 12:47:43 +0200 | [diff] [blame] | 473 | u32 thread_index = vm->thread_index; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 474 | uword l = vec_len (im->empty_buffers[thread_index]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 475 | uword n_alloc = 0; |
| 476 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 477 | if (PREDICT_FALSE (l < VLIB_FRAME_SIZE)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 479 | if (!im->empty_buffers[thread_index]) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 480 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 481 | vec_alloc (im->empty_buffers[thread_index], 2 * VLIB_FRAME_SIZE); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 482 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 484 | n_alloc = vlib_buffer_alloc (vm, im->empty_buffers[thread_index] + l, |
Damjan Marion | 6765549 | 2016-11-15 12:50:28 +0100 | [diff] [blame] | 485 | 2 * VLIB_FRAME_SIZE - l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 486 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 487 | _vec_len (im->empty_buffers[thread_index]) = l + n_alloc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Matus Fabian | 08a6f01 | 2016-11-15 06:08:51 -0800 | [diff] [blame] | 491 | static_always_inline u32 |
| 492 | get_next_output_feature_node_index (vlib_buffer_t * b, |
| 493 | vlib_node_runtime_t * nr) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 494 | { |
Matus Fabian | 08a6f01 | 2016-11-15 06:08:51 -0800 | [diff] [blame] | 495 | u32 next; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 496 | vlib_main_t *vm = vlib_get_main (); |
Matus Fabian | 08a6f01 | 2016-11-15 06:08:51 -0800 | [diff] [blame] | 497 | vlib_node_t *node = vlib_get_node (vm, nr->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 498 | |
Damjan Marion | 7d98a12 | 2018-07-19 20:42:08 +0200 | [diff] [blame] | 499 | vnet_feature_next (&next, b); |
Matus Fabian | 08a6f01 | 2016-11-15 06:08:51 -0800 | [diff] [blame] | 500 | return node->next_nodes[next]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 501 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 502 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 503 | u32 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im, |
| 504 | const char *name, |
| 505 | const char *ah4_encrypt_node_name, |
| 506 | const char *ah4_decrypt_node_name, |
| 507 | const char *ah6_encrypt_node_name, |
| 508 | const char *ah6_decrypt_node_name, |
| 509 | check_support_cb_t ah_check_support_cb, |
| 510 | add_del_sa_sess_cb_t ah_add_del_sa_sess_cb); |
| 511 | |
| 512 | u32 ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im, |
| 513 | const char *name, |
| 514 | const char *esp4_encrypt_node_name, |
| 515 | const char *esp4_decrypt_node_name, |
| 516 | const char *esp6_encrypt_node_name, |
| 517 | const char *esp6_decrypt_node_name, |
| 518 | check_support_cb_t esp_check_support_cb, |
| 519 | add_del_sa_sess_cb_t esp_add_del_sa_sess_cb); |
| 520 | |
| 521 | int ipsec_select_ah_backend (ipsec_main_t * im, u32 ah_backend_idx); |
| 522 | int ipsec_select_esp_backend (ipsec_main_t * im, u32 esp_backend_idx); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 523 | #endif /* __IPSEC_H__ */ |
| 524 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 525 | /* |
| 526 | * fd.io coding-style-patch-verification: ON |
| 527 | * |
| 528 | * Local Variables: |
| 529 | * eval: (c-set-style "gnu") |
| 530 | * End: |
| 531 | */ |