1 /*
2 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34 #ifndef __MLX5E_IPSEC_H__
35 #define __MLX5E_IPSEC_H__
36
37 #include <linux/mlx5/device.h>
38 #include <net/xfrm.h>
39 #include <linux/idr.h>
40 #include "lib/aso.h"
41 #include "lib/devcom.h"
42
43 #define MLX5E_IPSEC_SADB_RX_BITS 10
44 #define MLX5E_IPSEC_ESN_SCOPE_MID 0x80000000L
45
46 struct aes_gcm_keymat {
47 u64 seq_iv;
48
49 u32 salt;
50 u32 icv_len;
51
52 u32 key_len;
53 u32 aes_key[256 / 32];
54 };
55
56 struct upspec {
57 u16 dport;
58 u16 dport_mask;
59 u16 sport;
60 u16 sport_mask;
61 u8 proto;
62 };
63
64 struct mlx5_ipsec_lft {
65 u64 hard_packet_limit;
66 u64 soft_packet_limit;
67 u64 numb_rounds_hard;
68 u64 numb_rounds_soft;
69 };
70
71 struct mlx5_replay_esn {
72 u32 replay_window;
73 u32 esn;
74 u32 esn_msb;
75 u8 overlap : 1;
76 u8 trigger : 1;
77 };
78
79 struct mlx5e_ipsec_addr {
80 union {
81 __be32 a4;
82 __be32 a6[4];
83 } saddr;
84 union {
85 __be32 m4;
86 __be32 m6[4];
87 } smask;
88 union {
89 __be32 a4;
90 __be32 a6[4];
91 } daddr;
92 union {
93 __be32 m4;
94 __be32 m6[4];
95 } dmask;
96 u8 family;
97 };
98
99 struct mlx5_accel_esp_xfrm_attrs {
100 u32 spi;
101 u32 mode;
102 struct aes_gcm_keymat aes_gcm;
103 struct mlx5e_ipsec_addr addrs;
104 struct upspec upspec;
105 u8 dir : 2;
106 u8 type : 2;
107 u8 drop : 1;
108 u8 encap : 1;
109 struct mlx5_replay_esn replay_esn;
110 u32 authsize;
111 u32 reqid;
112 struct mlx5_ipsec_lft lft;
113 union {
114 u8 smac[ETH_ALEN];
115 __be16 sport;
116 };
117 union {
118 u8 dmac[ETH_ALEN];
119 __be16 dport;
120 };
121 };
122
123 enum mlx5_ipsec_cap {
124 MLX5_IPSEC_CAP_CRYPTO = 1 << 0,
125 MLX5_IPSEC_CAP_ESN = 1 << 1,
126 MLX5_IPSEC_CAP_PACKET_OFFLOAD = 1 << 2,
127 MLX5_IPSEC_CAP_ROCE = 1 << 3,
128 MLX5_IPSEC_CAP_PRIO = 1 << 4,
129 MLX5_IPSEC_CAP_TUNNEL = 1 << 5,
130 MLX5_IPSEC_CAP_ESPINUDP = 1 << 6,
131 };
132
133 struct mlx5e_priv;
134
135 struct mlx5e_ipsec_hw_stats {
136 u64 ipsec_rx_pkts;
137 u64 ipsec_rx_bytes;
138 u64 ipsec_rx_drop_pkts;
139 u64 ipsec_rx_drop_bytes;
140 u64 ipsec_rx_drop_mismatch_sa_sel;
141 u64 ipsec_tx_pkts;
142 u64 ipsec_tx_bytes;
143 u64 ipsec_tx_drop_pkts;
144 u64 ipsec_tx_drop_bytes;
145 };
146
147 struct mlx5e_ipsec_sw_stats {
148 atomic64_t ipsec_rx_drop_sp_alloc;
149 atomic64_t ipsec_rx_drop_sadb_miss;
150 atomic64_t ipsec_tx_drop_bundle;
151 atomic64_t ipsec_tx_drop_no_state;
152 atomic64_t ipsec_tx_drop_not_ip;
153 atomic64_t ipsec_tx_drop_trailer;
154 };
155
156 struct mlx5e_ipsec_fc;
157 struct mlx5e_ipsec_tx;
158
159 struct mlx5e_ipsec_work {
160 struct work_struct work;
161 struct mlx5e_ipsec_sa_entry *sa_entry;
162 void *data;
163 };
164
165 struct mlx5e_ipsec_netevent_data {
166 u8 addr[ETH_ALEN];
167 };
168
169 struct mlx5e_ipsec_dwork {
170 struct delayed_work dwork;
171 struct mlx5e_ipsec_sa_entry *sa_entry;
172 };
173
174 struct mlx5e_ipsec_aso {
175 u8 __aligned(64) ctx[MLX5_ST_SZ_BYTES(ipsec_aso)];
176 dma_addr_t dma_addr;
177 struct mlx5_aso *aso;
178 /* Protect ASO WQ access, as it is global to whole IPsec */
179 spinlock_t lock;
180 };
181
182 struct mlx5e_ipsec_rx_create_attr {
183 struct mlx5_flow_namespace *ns;
184 struct mlx5_ttc_table *ttc;
185 u32 family;
186 int prio;
187 int pol_level;
188 int sa_level;
189 int status_level;
190 enum mlx5_flow_namespace_type chains_ns;
191 };
192
193 struct mlx5e_ipsec_ft {
194 struct mutex mutex; /* Protect changes to this struct */
195 struct mlx5_flow_table *pol;
196 struct mlx5_flow_table *sa;
197 struct mlx5_flow_table *sa_sel;
198 struct mlx5_flow_table *status;
199 u32 refcnt;
200 };
201
202 struct mlx5e_ipsec_drop {
203 struct mlx5_flow_handle *rule;
204 struct mlx5_fc *fc;
205 };
206
207 struct mlx5e_ipsec_rule {
208 struct mlx5_flow_handle *rule;
209 struct mlx5_flow_handle *status_pass;
210 struct mlx5_flow_handle *sa_sel;
211 struct mlx5_modify_hdr *modify_hdr;
212 struct mlx5_pkt_reformat *pkt_reformat;
213 struct mlx5_fc *fc;
214 struct mlx5e_ipsec_drop replay;
215 struct mlx5e_ipsec_drop auth;
216 struct mlx5e_ipsec_drop trailer;
217 };
218
219 struct mlx5e_ipsec_miss {
220 struct mlx5_flow_group *group;
221 struct mlx5_flow_handle *rule;
222 struct mlx5_fc *fc;
223 };
224
225 struct mlx5e_ipsec_tx_create_attr {
226 int prio;
227 int pol_level;
228 int sa_level;
229 int cnt_level;
230 enum mlx5_flow_namespace_type chains_ns;
231 };
232
233 struct mlx5e_ipsec_mpv_work {
234 int event;
235 struct work_struct work;
236 struct mlx5e_priv *slave_priv;
237 struct mlx5e_priv *master_priv;
238 };
239
240 struct mlx5e_ipsec {
241 struct mlx5_core_dev *mdev;
242 struct xarray sadb;
243 struct mlx5e_ipsec_sw_stats sw_stats;
244 struct mlx5e_ipsec_hw_stats hw_stats;
245 struct workqueue_struct *wq;
246 struct completion comp;
247 struct mlx5e_flow_steering *fs;
248 struct mlx5e_ipsec_rx *rx_ipv4;
249 struct mlx5e_ipsec_rx *rx_ipv6;
250 struct mlx5e_ipsec_rx *rx_esw;
251 struct mlx5e_ipsec_tx *tx;
252 struct mlx5e_ipsec_tx *tx_esw;
253 struct mlx5e_ipsec_aso *aso;
254 struct notifier_block nb;
255 struct notifier_block netevent_nb;
256 struct mlx5_ipsec_fs *roce;
257 u8 is_uplink_rep: 1;
258 struct mlx5e_ipsec_mpv_work mpv_work;
259 struct xarray ipsec_obj_id_map;
260 };
261
262 struct mlx5e_ipsec_esn_state {
263 u32 esn;
264 u32 esn_msb;
265 u8 overlap: 1;
266 };
267
268 struct mlx5e_ipsec_limits {
269 u64 round;
270 u8 soft_limit_hit : 1;
271 u8 fix_limit : 1;
272 };
273
274 struct mlx5e_ipsec_sa_entry {
275 struct mlx5e_ipsec_esn_state esn_state;
276 struct xfrm_state *x;
277 struct mlx5e_ipsec *ipsec;
278 struct mlx5_accel_esp_xfrm_attrs attrs;
279 void (*set_iv_op)(struct sk_buff *skb, struct xfrm_state *x,
280 struct xfrm_offload *xo);
281 u32 ipsec_obj_id;
282 u32 enc_key_id;
283 struct mlx5e_ipsec_rule ipsec_rule;
284 struct mlx5e_ipsec_work *work;
285 struct mlx5e_ipsec_dwork *dwork;
286 struct mlx5e_ipsec_limits limits;
287 u32 rx_mapped_id;
288 };
289
290 struct mlx5_accel_pol_xfrm_attrs {
291 struct mlx5e_ipsec_addr addrs;
292 struct upspec upspec;
293 u8 action;
294 u8 type : 2;
295 u8 dir : 2;
296 u32 reqid;
297 u32 prio;
298 };
299
300 struct mlx5e_ipsec_pol_entry {
301 struct xfrm_policy *x;
302 struct mlx5e_ipsec *ipsec;
303 struct mlx5e_ipsec_rule ipsec_rule;
304 struct mlx5_accel_pol_xfrm_attrs attrs;
305 };
306
307 #ifdef CONFIG_MLX5_EN_IPSEC
308
309 void mlx5e_ipsec_init(struct mlx5e_priv *priv);
310 void mlx5e_ipsec_cleanup(struct mlx5e_priv *priv);
311 void mlx5e_ipsec_build_netdev(struct mlx5e_priv *priv);
312
313 void mlx5e_accel_ipsec_fs_cleanup(struct mlx5e_ipsec *ipsec);
314 int mlx5e_accel_ipsec_fs_init(struct mlx5e_ipsec *ipsec, struct mlx5_devcom_comp_dev **devcom);
315 int mlx5e_accel_ipsec_fs_add_rule(struct mlx5e_ipsec_sa_entry *sa_entry);
316 void mlx5e_accel_ipsec_fs_del_rule(struct mlx5e_ipsec_sa_entry *sa_entry);
317 int mlx5e_accel_ipsec_fs_add_pol(struct mlx5e_ipsec_pol_entry *pol_entry);
318 void mlx5e_accel_ipsec_fs_del_pol(struct mlx5e_ipsec_pol_entry *pol_entry);
319 void mlx5e_accel_ipsec_fs_modify(struct mlx5e_ipsec_sa_entry *sa_entry);
320 bool mlx5e_ipsec_fs_tunnel_enabled(struct mlx5e_ipsec_sa_entry *sa_entry);
321
322 int mlx5_ipsec_create_sa_ctx(struct mlx5e_ipsec_sa_entry *sa_entry);
323 void mlx5_ipsec_free_sa_ctx(struct mlx5e_ipsec_sa_entry *sa_entry);
324
325 u32 mlx5_ipsec_device_caps(struct mlx5_core_dev *mdev);
326
327 void mlx5_accel_esp_modify_xfrm(struct mlx5e_ipsec_sa_entry *sa_entry,
328 const struct mlx5_accel_esp_xfrm_attrs *attrs);
329
330 int mlx5e_ipsec_aso_init(struct mlx5e_ipsec *ipsec);
331 void mlx5e_ipsec_aso_cleanup(struct mlx5e_ipsec *ipsec);
332
333 int mlx5e_ipsec_aso_query(struct mlx5e_ipsec_sa_entry *sa_entry,
334 struct mlx5_wqe_aso_ctrl_seg *data);
335 void mlx5e_accel_ipsec_fs_read_stats(struct mlx5e_priv *priv,
336 void *ipsec_stats);
337
338 void mlx5e_ipsec_build_accel_xfrm_attrs(struct mlx5e_ipsec_sa_entry *sa_entry,
339 struct mlx5_accel_esp_xfrm_attrs *attrs);
340 void mlx5e_ipsec_handle_mpv_event(int event, struct mlx5e_priv *slave_priv,
341 struct mlx5e_priv *master_priv);
342 void mlx5e_ipsec_send_event(struct mlx5e_priv *priv, int event);
343
344 static inline struct mlx5_core_dev *
mlx5e_ipsec_sa2dev(struct mlx5e_ipsec_sa_entry * sa_entry)345 mlx5e_ipsec_sa2dev(struct mlx5e_ipsec_sa_entry *sa_entry)
346 {
347 return sa_entry->ipsec->mdev;
348 }
349
350 static inline struct mlx5_core_dev *
mlx5e_ipsec_pol2dev(struct mlx5e_ipsec_pol_entry * pol_entry)351 mlx5e_ipsec_pol2dev(struct mlx5e_ipsec_pol_entry *pol_entry)
352 {
353 return pol_entry->ipsec->mdev;
354 }
355
addr6_all_zero(__be32 * addr6)356 static inline bool addr6_all_zero(__be32 *addr6)
357 {
358 static const __be32 zaddr6[4] = {};
359
360 return !memcmp(addr6, zaddr6, sizeof(zaddr6));
361 }
362 #else
mlx5e_ipsec_init(struct mlx5e_priv * priv)363 static inline void mlx5e_ipsec_init(struct mlx5e_priv *priv)
364 {
365 }
366
mlx5e_ipsec_cleanup(struct mlx5e_priv * priv)367 static inline void mlx5e_ipsec_cleanup(struct mlx5e_priv *priv)
368 {
369 }
370
mlx5e_ipsec_build_netdev(struct mlx5e_priv * priv)371 static inline void mlx5e_ipsec_build_netdev(struct mlx5e_priv *priv)
372 {
373 }
374
mlx5_ipsec_device_caps(struct mlx5_core_dev * mdev)375 static inline u32 mlx5_ipsec_device_caps(struct mlx5_core_dev *mdev)
376 {
377 return 0;
378 }
379
mlx5e_ipsec_handle_mpv_event(int event,struct mlx5e_priv * slave_priv,struct mlx5e_priv * master_priv)380 static inline void mlx5e_ipsec_handle_mpv_event(int event, struct mlx5e_priv *slave_priv,
381 struct mlx5e_priv *master_priv)
382 {
383 }
384
mlx5e_ipsec_send_event(struct mlx5e_priv * priv,int event)385 static inline void mlx5e_ipsec_send_event(struct mlx5e_priv *priv, int event)
386 {
387 }
388 #endif
389
390 #endif /* __MLX5E_IPSEC_H__ */
391