Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
Neale Ranns | 999c8ee | 2019-02-01 03:31:24 -0800 | [diff] [blame] | 2 | * ipsec.c : IPSEC module functions |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <vnet/api_errno.h> |
| 20 | #include <vnet/ip/ip.h> |
| 21 | #include <vnet/interface.h> |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 22 | #include <vnet/udp/udp.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | |
| 24 | #include <vnet/ipsec/ipsec.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 25 | #include <vnet/ipsec/esp.h> |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 26 | #include <vnet/ipsec/ah.h> |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 27 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 28 | ipsec_main_t ipsec_main; |
Kingwel Xie | 561d1ca | 2019-03-05 22:56:17 -0500 | [diff] [blame] | 29 | ipsec_proto_main_t ipsec_proto_main; |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 30 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | static void |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 32 | ipsec_rand_seed (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 34 | struct |
| 35 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | time_t time; |
| 37 | pid_t pid; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 38 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | } seed_data; |
| 40 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 41 | seed_data.time = time (NULL); |
| 42 | seed_data.pid = getpid (); |
| 43 | seed_data.p = (void *) &seed_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 45 | RAND_seed ((const void *) &seed_data, sizeof (seed_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static clib_error_t * |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 49 | ipsec_check_ah_support (ipsec_sa_t * sa) |
| 50 | { |
| 51 | if (sa->integ_alg == IPSEC_INTEG_ALG_NONE) |
| 52 | return clib_error_return (0, "unsupported none integ-alg"); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static clib_error_t * |
| 57 | ipsec_check_esp_support (ipsec_sa_t * sa) |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 58 | { |
| 59 | if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) |
| 60 | return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg"); |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 61 | if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192) |
| 62 | return clib_error_return (0, "unsupported aes-gcm-192 crypto-alg"); |
| 63 | if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256) |
| 64 | return clib_error_return (0, "unsupported aes-gcm-256 crypto-alg"); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 69 | clib_error_t * |
| 70 | ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add) |
| 71 | { |
| 72 | ipsec_ah_backend_t *ah = |
| 73 | pool_elt_at_index (im->ah_backends, im->ah_current_backend); |
| 74 | if (ah->add_del_sa_sess_cb) |
| 75 | { |
| 76 | clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add); |
| 77 | if (err) |
| 78 | return err; |
| 79 | } |
| 80 | ipsec_esp_backend_t *esp = |
| 81 | pool_elt_at_index (im->esp_backends, im->esp_current_backend); |
| 82 | if (esp->add_del_sa_sess_cb) |
| 83 | { |
| 84 | clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add); |
| 85 | if (err) |
| 86 | return err; |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | clib_error_t * |
| 92 | ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa) |
| 93 | { |
| 94 | clib_error_t *error = 0; |
Matthew Smith | 461caa5 | 2018-12-21 11:53:16 -0600 | [diff] [blame] | 95 | |
| 96 | if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH)) |
| 97 | { |
| 98 | ipsec_ah_backend_t *ah = |
| 99 | pool_elt_at_index (im->ah_backends, im->ah_current_backend); |
| 100 | ASSERT (ah->check_support_cb); |
| 101 | error = ah->check_support_cb (sa); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | ipsec_esp_backend_t *esp = |
| 106 | pool_elt_at_index (im->esp_backends, im->esp_current_backend); |
| 107 | ASSERT (esp->check_support_cb); |
| 108 | error = esp->check_support_cb (sa); |
| 109 | } |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 110 | return error; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static void |
| 115 | ipsec_add_node (vlib_main_t * vm, const char *node_name, |
| 116 | const char *prev_node_name, u32 * out_node_index, |
| 117 | u32 * out_next_index) |
| 118 | { |
| 119 | vlib_node_t *prev_node, *node; |
| 120 | prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name); |
| 121 | ASSERT (prev_node); |
| 122 | node = vlib_get_node_by_name (vm, (u8 *) node_name); |
| 123 | ASSERT (node); |
| 124 | *out_node_index = node->index; |
| 125 | *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index); |
| 126 | } |
| 127 | |
| 128 | u32 |
| 129 | ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im, |
| 130 | const char *name, |
| 131 | const char *ah4_encrypt_node_name, |
| 132 | const char *ah4_decrypt_node_name, |
| 133 | const char *ah6_encrypt_node_name, |
| 134 | const char *ah6_decrypt_node_name, |
| 135 | check_support_cb_t ah_check_support_cb, |
| 136 | add_del_sa_sess_cb_t ah_add_del_sa_sess_cb) |
| 137 | { |
| 138 | ipsec_ah_backend_t *b; |
| 139 | pool_get (im->ah_backends, b); |
Kingwel Xie | 561d1ca | 2019-03-05 22:56:17 -0500 | [diff] [blame] | 140 | b->name = format (0, "%s%c", name, 0); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 141 | |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 142 | ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 143 | &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 144 | ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 145 | &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 146 | ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 147 | &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 148 | ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 149 | &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index); |
| 150 | |
| 151 | b->check_support_cb = ah_check_support_cb; |
| 152 | b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb; |
| 153 | return b - im->ah_backends; |
| 154 | } |
| 155 | |
| 156 | u32 |
| 157 | ipsec_register_esp_backend (vlib_main_t * vm, ipsec_main_t * im, |
| 158 | const char *name, |
| 159 | const char *esp4_encrypt_node_name, |
| 160 | const char *esp4_decrypt_node_name, |
| 161 | const char *esp6_encrypt_node_name, |
| 162 | const char *esp6_decrypt_node_name, |
| 163 | check_support_cb_t esp_check_support_cb, |
| 164 | add_del_sa_sess_cb_t esp_add_del_sa_sess_cb) |
| 165 | { |
| 166 | ipsec_esp_backend_t *b; |
| 167 | pool_get (im->esp_backends, b); |
Kingwel Xie | 561d1ca | 2019-03-05 22:56:17 -0500 | [diff] [blame] | 168 | b->name = format (0, "%s%c", name, 0); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 169 | |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 170 | ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 171 | &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 172 | ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 173 | &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 174 | ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 175 | &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index); |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 176 | ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 177 | &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index); |
| 178 | |
| 179 | b->check_support_cb = esp_check_support_cb; |
| 180 | b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb; |
| 181 | return b - im->esp_backends; |
| 182 | } |
| 183 | |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 184 | static walk_rc_t |
| 185 | ipsec_sa_restack (ipsec_sa_t * sa, void *ctx) |
| 186 | { |
| 187 | ipsec_sa_stack (sa); |
| 188 | |
| 189 | return (WALK_CONTINUE); |
| 190 | } |
| 191 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 192 | int |
| 193 | ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx) |
| 194 | { |
| 195 | if (pool_elts (im->sad) > 0 |
| 196 | || pool_is_free_index (im->ah_backends, backend_idx)) |
| 197 | { |
| 198 | return -1; |
| 199 | } |
| 200 | ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx); |
| 201 | im->ah_current_backend = backend_idx; |
| 202 | im->ah4_encrypt_node_index = b->ah4_encrypt_node_index; |
| 203 | im->ah4_decrypt_node_index = b->ah4_decrypt_node_index; |
| 204 | im->ah4_encrypt_next_index = b->ah4_encrypt_next_index; |
| 205 | im->ah4_decrypt_next_index = b->ah4_decrypt_next_index; |
| 206 | im->ah6_encrypt_node_index = b->ah6_encrypt_node_index; |
| 207 | im->ah6_decrypt_node_index = b->ah6_decrypt_node_index; |
| 208 | im->ah6_encrypt_next_index = b->ah6_encrypt_next_index; |
| 209 | im->ah6_decrypt_next_index = b->ah6_decrypt_next_index; |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 210 | |
| 211 | ipsec_sa_walk (ipsec_sa_restack, NULL); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | int |
| 216 | ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx) |
| 217 | { |
| 218 | if (pool_elts (im->sad) > 0 |
| 219 | || pool_is_free_index (im->esp_backends, backend_idx)) |
| 220 | { |
| 221 | return -1; |
| 222 | } |
| 223 | ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx); |
| 224 | im->esp_current_backend = backend_idx; |
| 225 | im->esp4_encrypt_node_index = b->esp4_encrypt_node_index; |
| 226 | im->esp4_decrypt_node_index = b->esp4_decrypt_node_index; |
| 227 | im->esp4_encrypt_next_index = b->esp4_encrypt_next_index; |
| 228 | im->esp4_decrypt_next_index = b->esp4_decrypt_next_index; |
| 229 | im->esp6_encrypt_node_index = b->esp6_encrypt_node_index; |
| 230 | im->esp6_decrypt_node_index = b->esp6_decrypt_node_index; |
| 231 | im->esp6_encrypt_next_index = b->esp6_encrypt_next_index; |
| 232 | im->esp6_decrypt_next_index = b->esp6_decrypt_next_index; |
Neale Ranns | b4cfd55 | 2019-02-13 02:08:06 -0800 | [diff] [blame] | 233 | |
| 234 | ipsec_sa_walk (ipsec_sa_restack, NULL); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 235 | return 0; |
| 236 | } |
| 237 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 238 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | ipsec_init (vlib_main_t * vm) |
| 240 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 241 | clib_error_t *error; |
| 242 | ipsec_main_t *im = &ipsec_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 244 | ipsec_rand_seed (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 246 | clib_memset (im, 0, sizeof (im[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 248 | im->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | im->vlib_main = vm; |
| 250 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 251 | im->spd_index_by_spd_id = hash_create (0, sizeof (uword)); |
| 252 | im->sa_index_by_sa_id = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword)); |
| 254 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 255 | vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop"); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 256 | ASSERT (node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | im->error_drop_node_index = node->index; |
| 258 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 259 | u32 idx = ipsec_register_ah_backend (vm, im, "default openssl backend", |
| 260 | "ah4-encrypt", |
| 261 | "ah4-decrypt", |
| 262 | "ah6-encrypt", |
| 263 | "ah6-decrypt", |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 264 | ipsec_check_ah_support, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 265 | NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 267 | im->ah_default_backend = idx; |
| 268 | int rv = ipsec_select_ah_backend (im, idx); |
| 269 | ASSERT (0 == rv); |
| 270 | (void) (rv); // avoid warning |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 271 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 272 | idx = ipsec_register_esp_backend (vm, im, "default openssl backend", |
| 273 | "esp4-encrypt", |
| 274 | "esp4-decrypt", |
| 275 | "esp6-encrypt", |
| 276 | "esp6-decrypt", |
Klement Sekera | 4fd5a9d | 2019-01-30 11:11:23 +0100 | [diff] [blame] | 277 | ipsec_check_esp_support, NULL); |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 278 | im->esp_default_backend = idx; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 279 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 280 | rv = ipsec_select_esp_backend (im, idx); |
| 281 | ASSERT (0 == rv); |
| 282 | (void) (rv); // avoid warning |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | if ((error = vlib_call_init_function (vm, ipsec_cli_init))) |
| 285 | return error; |
| 286 | |
| 287 | if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init))) |
| 288 | return error; |
| 289 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 290 | ipsec_proto_init (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | VLIB_INIT_FUNCTION (ipsec_init); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * fd.io coding-style-patch-verification: ON |
| 299 | * |
| 300 | * Local Variables: |
| 301 | * eval: (c-set-style "gnu") |
| 302 | * End: |
| 303 | */ |