Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ipsec_tun.h : IPSEC tunnel protection |
| 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/ipsec/ipsec.h> |
| 19 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 20 | /* *INDENT-OFF* */ |
| 21 | typedef CLIB_PACKED(struct { |
| 22 | /* |
| 23 | * Key fields: remote ip and spi on incoming packet |
| 24 | * all fields in NET byte order |
| 25 | */ |
| 26 | union { |
| 27 | struct { |
| 28 | ip4_address_t remote_ip; |
| 29 | u32 spi; |
| 30 | }; |
| 31 | u64 as_u64; |
| 32 | }; |
| 33 | }) ipsec4_tunnel_key_t; |
| 34 | /* *INDENT-ON* */ |
| 35 | |
| 36 | /* *INDENT-OFF* */ |
| 37 | typedef CLIB_PACKED(struct { |
| 38 | /* |
| 39 | * Key fields: remote ip and spi on incoming packet |
| 40 | * all fields in NET byte order |
| 41 | */ |
| 42 | ip6_address_t remote_ip; |
| 43 | u32 spi; |
| 44 | }) ipsec6_tunnel_key_t; |
| 45 | /* *INDENT-ON* */ |
| 46 | |
| 47 | extern u8 *format_ipsec4_tunnel_key (u8 * s, va_list * args); |
| 48 | extern u8 *format_ipsec6_tunnel_key (u8 * s, va_list * args); |
| 49 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 50 | #define foreach_ipsec_protect_flags \ |
| 51 | _(L2, 1, "l2") \ |
| 52 | _(ENCAPED, 2, "encapped") \ |
| 53 | _(ITF, 4, "itf") \ |
| 54 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 55 | typedef enum ipsec_protect_flags_t_ |
| 56 | { |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 57 | IPSEC_PROTECT_NONE = 0, |
| 58 | #define _(a,b,c) IPSEC_PROTECT_##a = b, |
| 59 | foreach_ipsec_protect_flags |
| 60 | #undef _ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 61 | } __clib_packed ipsec_protect_flags_t; |
| 62 | |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 63 | extern u8 *format_ipsec_tun_protect_flags (u8 * s, va_list * args); |
| 64 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 65 | typedef struct ipsec_ep_t_ |
| 66 | { |
| 67 | ip46_address_t src; |
| 68 | ip46_address_t dst; |
| 69 | } ipsec_ep_t; |
| 70 | |
Matthew Smith | dc3e966 | 2020-04-10 20:27:33 -0500 | [diff] [blame] | 71 | #define ITP_MAX_N_SA_IN 4 |
| 72 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 73 | typedef struct ipsec_tun_protect_t_ |
| 74 | { |
| 75 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 76 | index_t itp_out_sa; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 77 | |
| 78 | /* not using a vector since we want the memory inline |
| 79 | * with this struct */ |
| 80 | u32 itp_n_sa_in; |
Matthew Smith | dc3e966 | 2020-04-10 20:27:33 -0500 | [diff] [blame] | 81 | index_t itp_in_sas[ITP_MAX_N_SA_IN]; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 82 | |
| 83 | u32 itp_sw_if_index; |
| 84 | |
| 85 | ipsec_ep_t itp_crypto; |
| 86 | |
| 87 | ipsec_protect_flags_t itp_flags; |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 88 | adj_index_t itp_ai; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 89 | |
| 90 | ipsec_ep_t itp_tun; |
| 91 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 92 | ip_address_t *itp_key; |
| 93 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 94 | } ipsec_tun_protect_t; |
| 95 | |
| 96 | #define FOR_EACH_IPSEC_PROTECT_INPUT_SAI(_itp, _sai, body) \ |
| 97 | { \ |
| 98 | u32 __ii; \ |
| 99 | for (__ii = 0; __ii < _itp->itp_n_sa_in; __ii++) { \ |
| 100 | _sai = itp->itp_in_sas[__ii]; \ |
| 101 | body; \ |
| 102 | } \ |
| 103 | } |
| 104 | #define FOR_EACH_IPSEC_PROTECT_INPUT_SA(_itp, _sa, body) \ |
| 105 | { \ |
| 106 | u32 __ii; \ |
| 107 | for (__ii = 0; __ii < _itp->itp_n_sa_in; __ii++) { \ |
| 108 | _sa = ipsec_sa_get(itp->itp_in_sas[__ii]); \ |
| 109 | body; \ |
| 110 | } \ |
| 111 | } |
| 112 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 113 | extern int ipsec_tun_protect_update_one (u32 sw_if_index, |
| 114 | const ip_address_t * nh, |
| 115 | u32 sa_out, u32 sa_in); |
| 116 | extern int ipsec_tun_protect_update (u32 sw_if_index, |
| 117 | const ip_address_t * nh, |
| 118 | u32 sa_out, u32 * sa_ins); |
| 119 | extern int ipsec_tun_protect_update_in (u32 sw_if_index, |
| 120 | const ip_address_t * nh, u32 sa_in); |
| 121 | extern int ipsec_tun_protect_update_out (u32 sw_if_index, |
| 122 | const ip_address_t * nh, u32 sa_out); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 123 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 124 | extern int ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 125 | |
| 126 | typedef walk_rc_t (*ipsec_tun_protect_walk_cb_t) (index_t itpi, void *arg); |
| 127 | extern void ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, |
| 128 | void *cttx); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 129 | extern void ipsec_tun_protect_walk_itf (u32 sw_if_index, |
| 130 | ipsec_tun_protect_walk_cb_t fn, |
| 131 | void *cttx); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 132 | |
| 133 | extern u8 *format_ipsec_tun_protect (u8 * s, va_list * args); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 134 | extern u8 *format_ipsec_tun_protect_index (u8 * s, va_list * args); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 135 | |
Neale Ranns | 8d6d74c | 2020-02-20 09:45:16 +0000 | [diff] [blame] | 136 | extern void ipsec_tun_register_nodes (ip_address_family_t af); |
| 137 | extern void ipsec_tun_unregister_nodes (ip_address_family_t af); |
| 138 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 139 | // FIXME |
| 140 | extern vlib_node_registration_t ipsec4_tun_input_node; |
| 141 | extern vlib_node_registration_t ipsec6_tun_input_node; |
| 142 | |
| 143 | /* |
| 144 | * DP API |
| 145 | */ |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 146 | extern ipsec_tun_protect_t *ipsec_tun_protect_pool; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 147 | |
| 148 | typedef struct ipsec_tun_lkup_result_t_ |
| 149 | { |
| 150 | union |
| 151 | { |
| 152 | struct |
| 153 | { |
| 154 | u32 tun_index; |
| 155 | u32 sa_index; |
| 156 | }; |
| 157 | u64 as_u64; |
| 158 | }; |
| 159 | } ipsec_tun_lkup_result_t; |
| 160 | |
| 161 | always_inline ipsec_tun_protect_t * |
| 162 | ipsec_tun_protect_get (u32 index) |
| 163 | { |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 164 | return (pool_elt_at_index (ipsec_tun_protect_pool, index)); |
| 165 | } |
| 166 | |
| 167 | extern index_t *ipsec_tun_protect_sa_by_adj_index; |
| 168 | always_inline index_t |
| 169 | ipsec_tun_protect_get_sa_out (adj_index_t ai) |
| 170 | { |
| 171 | ASSERT (vec_len (ipsec_tun_protect_sa_by_adj_index) > ai); |
| 172 | ASSERT (INDEX_INVALID != ipsec_tun_protect_sa_by_adj_index[ai]); |
| 173 | |
| 174 | return (ipsec_tun_protect_sa_by_adj_index[ai]); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * fd.io coding-style-patch-verification: ON |
| 179 | * |
| 180 | * Local Variables: |
| 181 | * eval: (c-set-style "gnu") |
| 182 | * End: |
| 183 | */ |