blob: 7ae00992faa7987c9f7012113ccddb480513b10c [file] [log] [blame]
Neale Rannsc87b66c2019-02-07 07:26:12 -08001/*
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 Ranns302b25a2020-10-19 13:23:33 +000020#define foreach_ipsec_protect_flags \
21 _(L2, 1, "l2") \
22 _(ENCAPED, 2, "encapped") \
23 _(ITF, 4, "itf") \
24
25typedef enum ipsec_protect_flags_t_
26{
27 IPSEC_PROTECT_NONE = 0,
28#define _(a,b,c) IPSEC_PROTECT_##a = b,
29 foreach_ipsec_protect_flags
30#undef _
31} __clib_packed ipsec_protect_flags_t;
32
33extern u8 *format_ipsec_tun_protect_flags (u8 * s, va_list * args);
34
Neale Ranns7b4e52f2020-05-24 16:17:50 +000035/**
36 * result of a lookup in the protection bihash
37 */
38typedef struct ipsec_tun_lkup_result_t_
39{
Neale Ranns302b25a2020-10-19 13:23:33 +000040 u32 tun_index;
41 u32 sa_index;
42 u32 sw_if_index;
43 ipsec_protect_flags_t flags;
44 u8 __pad[3];
Neale Ranns7b4e52f2020-05-24 16:17:50 +000045} ipsec_tun_lkup_result_t;
Neale Ranns12989b52019-09-26 16:20:19 +000046
Neale Ranns7b4e52f2020-05-24 16:17:50 +000047typedef struct ipsec4_tunnel_kv_t
48{
Neale Ranns12989b52019-09-26 16:20:19 +000049 /*
50 * Key fields: remote ip and spi on incoming packet
51 * all fields in NET byte order
52 */
Neale Ranns7b4e52f2020-05-24 16:17:50 +000053 u64 key;
54 ipsec_tun_lkup_result_t value;
55} __clib_packed ipsec4_tunnel_kv_t;
Neale Ranns12989b52019-09-26 16:20:19 +000056
Neale Ranns302b25a2020-10-19 13:23:33 +000057STATIC_ASSERT_SIZEOF (ipsec4_tunnel_kv_t, sizeof (clib_bihash_kv_8_16_t));
Neale Ranns7b4e52f2020-05-24 16:17:50 +000058STATIC_ASSERT_OFFSET_OF (ipsec4_tunnel_kv_t, value,
Neale Ranns302b25a2020-10-19 13:23:33 +000059 STRUCT_OFFSET_OF (clib_bihash_kv_8_16_t, value));
Neale Ranns7b4e52f2020-05-24 16:17:50 +000060
61static inline void
62ipsec4_tunnel_mk_key (ipsec4_tunnel_kv_t * k,
63 const ip4_address_t * ip, u32 spi)
64{
65 k->key = (((u64) ip->as_u32) << 32 | spi);
66}
67
68static inline void
69ipsec4_tunnel_extract_key (const ipsec4_tunnel_kv_t * k,
70 ip4_address_t * ip, u32 * spi)
71{
72 *spi = (u32) k->key;
73 (*ip).as_u32 = k->key >> 32;
74}
75
76typedef struct ipsec6_tunnel_kv_t_
77{
78 /*
79 * Key fields: remote ip and spi on incoming packet
80 * all fields in NET byte order
81 */
82 struct
83 {
84 ip6_address_t remote_ip;
85 u32 spi;
86 u32 __pad;
87 } key;
88 ipsec_tun_lkup_result_t value;
89} __clib_packed ipsec6_tunnel_kv_t;
90
Neale Ranns302b25a2020-10-19 13:23:33 +000091STATIC_ASSERT_SIZEOF (ipsec6_tunnel_kv_t, sizeof (clib_bihash_kv_24_16_t));
Neale Ranns7b4e52f2020-05-24 16:17:50 +000092STATIC_ASSERT_OFFSET_OF (ipsec6_tunnel_kv_t, value,
Neale Ranns302b25a2020-10-19 13:23:33 +000093 STRUCT_OFFSET_OF (clib_bihash_kv_24_16_t, value));
Neale Ranns7b4e52f2020-05-24 16:17:50 +000094
95extern u8 *format_ipsec4_tunnel_kv (u8 * s, va_list * args);
96extern u8 *format_ipsec6_tunnel_kv (u8 * s, va_list * args);
Neale Ranns12989b52019-09-26 16:20:19 +000097
Neale Rannsc87b66c2019-02-07 07:26:12 -080098typedef struct ipsec_ep_t_
99{
100 ip46_address_t src;
101 ip46_address_t dst;
102} ipsec_ep_t;
103
Matthew Smithdc3e9662020-04-10 20:27:33 -0500104#define ITP_MAX_N_SA_IN 4
105
Neale Rannsc87b66c2019-02-07 07:26:12 -0800106typedef struct ipsec_tun_protect_t_
107{
108 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000109 index_t itp_out_sa;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800110
111 /* not using a vector since we want the memory inline
112 * with this struct */
113 u32 itp_n_sa_in;
Matthew Smithdc3e9662020-04-10 20:27:33 -0500114 index_t itp_in_sas[ITP_MAX_N_SA_IN];
Neale Rannsc87b66c2019-02-07 07:26:12 -0800115
116 u32 itp_sw_if_index;
117
118 ipsec_ep_t itp_crypto;
119
120 ipsec_protect_flags_t itp_flags;
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000121 adj_index_t itp_ai;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800122
123 ipsec_ep_t itp_tun;
124
Neale Ranns28287212019-12-16 00:53:11 +0000125 ip_address_t *itp_key;
126
Neale Rannsc87b66c2019-02-07 07:26:12 -0800127} ipsec_tun_protect_t;
128
129#define FOR_EACH_IPSEC_PROTECT_INPUT_SAI(_itp, _sai, body) \
130{ \
131 u32 __ii; \
132 for (__ii = 0; __ii < _itp->itp_n_sa_in; __ii++) { \
133 _sai = itp->itp_in_sas[__ii]; \
134 body; \
135 } \
136}
137#define FOR_EACH_IPSEC_PROTECT_INPUT_SA(_itp, _sa, body) \
138{ \
139 u32 __ii; \
140 for (__ii = 0; __ii < _itp->itp_n_sa_in; __ii++) { \
141 _sa = ipsec_sa_get(itp->itp_in_sas[__ii]); \
142 body; \
143 } \
144}
145
Neale Ranns28287212019-12-16 00:53:11 +0000146extern int ipsec_tun_protect_update (u32 sw_if_index,
147 const ip_address_t * nh,
148 u32 sa_out, u32 * sa_ins);
Neale Ranns12989b52019-09-26 16:20:19 +0000149
Neale Ranns28287212019-12-16 00:53:11 +0000150extern int ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800151
152typedef walk_rc_t (*ipsec_tun_protect_walk_cb_t) (index_t itpi, void *arg);
153extern void ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn,
154 void *cttx);
Neale Ranns28287212019-12-16 00:53:11 +0000155extern void ipsec_tun_protect_walk_itf (u32 sw_if_index,
156 ipsec_tun_protect_walk_cb_t fn,
157 void *cttx);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800158
159extern u8 *format_ipsec_tun_protect (u8 * s, va_list * args);
Neale Ranns28287212019-12-16 00:53:11 +0000160extern u8 *format_ipsec_tun_protect_index (u8 * s, va_list * args);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800161
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000162extern void ipsec_tun_register_nodes (ip_address_family_t af);
163extern void ipsec_tun_unregister_nodes (ip_address_family_t af);
164
Neale Rannsc87b66c2019-02-07 07:26:12 -0800165// FIXME
166extern vlib_node_registration_t ipsec4_tun_input_node;
167extern vlib_node_registration_t ipsec6_tun_input_node;
168
169/*
170 * DP API
171 */
Neale Ranns28287212019-12-16 00:53:11 +0000172extern ipsec_tun_protect_t *ipsec_tun_protect_pool;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800173
Neale Rannsc87b66c2019-02-07 07:26:12 -0800174always_inline ipsec_tun_protect_t *
175ipsec_tun_protect_get (u32 index)
176{
Neale Ranns28287212019-12-16 00:53:11 +0000177 return (pool_elt_at_index (ipsec_tun_protect_pool, index));
178}
179
180extern index_t *ipsec_tun_protect_sa_by_adj_index;
181always_inline index_t
182ipsec_tun_protect_get_sa_out (adj_index_t ai)
183{
184 ASSERT (vec_len (ipsec_tun_protect_sa_by_adj_index) > ai);
185 ASSERT (INDEX_INVALID != ipsec_tun_protect_sa_by_adj_index[ai]);
186
187 return (ipsec_tun_protect_sa_by_adj_index[ai]);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800188}
189
190/*
191 * fd.io coding-style-patch-verification: ON
192 *
193 * Local Variables:
194 * eval: (c-set-style "gnu")
195 * End:
196 */