Skip to content

Commit 7db21ef

Browse files
committed
net/mlx5e: Set IPsec replay sequence numbers
"ip xfrm state ..." command allows users to configure replay sequence numbers with replay-seq* arguments for RX and replay-oseq* for TX. Add the needed driver logic to support setting them. Link: https://lore.kernel.org/r/a9b17827eff2b29a4951225efa684a6cd38f74fe.1680162300.git.leonro@nvidia.com Reviewed-by: Raed Salem <raeds@nvidia.com> Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
1 parent f4979e2 commit 7db21ef

File tree

3 files changed

+60
-24
lines changed

3 files changed

+60
-24
lines changed

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,46 @@ static struct mlx5e_ipsec_pol_entry *to_ipsec_pol_entry(struct xfrm_policy *x)
5252

5353
static bool mlx5e_ipsec_update_esn_state(struct mlx5e_ipsec_sa_entry *sa_entry)
5454
{
55-
struct xfrm_replay_state_esn *replay_esn;
55+
struct xfrm_state *x = sa_entry->x;
5656
u32 seq_bottom = 0;
57+
u32 esn, esn_msb;
5758
u8 overlap;
5859

59-
replay_esn = sa_entry->x->replay_esn;
60-
if (replay_esn->seq >= replay_esn->replay_window)
61-
seq_bottom = replay_esn->seq - replay_esn->replay_window + 1;
60+
switch (x->xso.type) {
61+
case XFRM_DEV_OFFLOAD_PACKET:
62+
switch (x->xso.dir) {
63+
case XFRM_DEV_OFFLOAD_IN:
64+
esn = x->replay_esn->seq;
65+
esn_msb = x->replay_esn->seq_hi;
66+
break;
67+
case XFRM_DEV_OFFLOAD_OUT:
68+
esn = x->replay_esn->oseq;
69+
esn_msb = x->replay_esn->oseq_hi;
70+
break;
71+
default:
72+
WARN_ON(true);
73+
return false;
74+
}
75+
break;
76+
case XFRM_DEV_OFFLOAD_CRYPTO:
77+
/* Already parsed by XFRM core */
78+
esn = x->replay_esn->seq;
79+
break;
80+
default:
81+
WARN_ON(true);
82+
return false;
83+
}
6284

6385
overlap = sa_entry->esn_state.overlap;
6486

65-
sa_entry->esn_state.esn = xfrm_replay_seqhi(sa_entry->x,
66-
htonl(seq_bottom));
87+
if (esn >= x->replay_esn->replay_window)
88+
seq_bottom = esn - x->replay_esn->replay_window + 1;
89+
90+
if (x->xso.type == XFRM_DEV_OFFLOAD_CRYPTO)
91+
esn_msb = xfrm_replay_seqhi(x, htonl(seq_bottom));
92+
93+
sa_entry->esn_state.esn = esn;
94+
sa_entry->esn_state.esn_msb = esn_msb;
6795

6896
if (unlikely(overlap && seq_bottom < MLX5E_IPSEC_ESN_SCOPE_MID)) {
6997
sa_entry->esn_state.overlap = 0;
@@ -224,10 +252,10 @@ void mlx5e_ipsec_build_accel_xfrm_attrs(struct mlx5e_ipsec_sa_entry *sa_entry,
224252

225253
/* esn */
226254
if (x->props.flags & XFRM_STATE_ESN) {
227-
attrs->esn_trigger = true;
228-
attrs->esn = sa_entry->esn_state.esn;
229-
attrs->esn_overlap = sa_entry->esn_state.overlap;
230-
attrs->replay_window = x->replay_esn->replay_window;
255+
attrs->replay_esn.trigger = true;
256+
attrs->replay_esn.esn = sa_entry->esn_state.esn;
257+
attrs->replay_esn.esn_msb = sa_entry->esn_state.esn_msb;
258+
attrs->replay_esn.overlap = sa_entry->esn_state.overlap;
231259
}
232260

233261
attrs->dir = x->xso.dir;

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ struct mlx5_ipsec_lft {
6767
u64 numb_rounds_soft;
6868
};
6969

70+
struct mlx5_replay_esn {
71+
u32 replay_window;
72+
u32 esn;
73+
u32 esn_msb;
74+
u8 overlap : 1;
75+
u8 trigger : 1;
76+
};
77+
7078
struct mlx5_accel_esp_xfrm_attrs {
71-
u32 esn;
7279
u32 spi;
7380
u32 flags;
7481
struct aes_gcm_keymat aes_gcm;
@@ -85,11 +92,9 @@ struct mlx5_accel_esp_xfrm_attrs {
8592

8693
struct upspec upspec;
8794
u8 dir : 2;
88-
u8 esn_overlap : 1;
89-
u8 esn_trigger : 1;
9095
u8 type : 2;
9196
u8 family;
92-
u32 replay_window;
97+
struct mlx5_replay_esn replay_esn;
9398
u32 authsize;
9499
u32 reqid;
95100
struct mlx5_ipsec_lft lft;
@@ -160,6 +165,7 @@ struct mlx5e_ipsec {
160165

161166
struct mlx5e_ipsec_esn_state {
162167
u32 esn;
168+
u32 esn_msb;
163169
u8 overlap: 1;
164170
};
165171

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,17 @@ static void mlx5e_ipsec_packet_setup(void *obj, u32 pdn,
7676
void *aso_ctx;
7777

7878
aso_ctx = MLX5_ADDR_OF(ipsec_obj, obj, ipsec_aso);
79-
if (attrs->esn_trigger) {
79+
if (attrs->replay_esn.trigger) {
8080
MLX5_SET(ipsec_aso, aso_ctx, esn_event_arm, 1);
8181

8282
if (attrs->dir == XFRM_DEV_OFFLOAD_IN) {
8383
MLX5_SET(ipsec_aso, aso_ctx, window_sz,
84-
attrs->replay_window / 64);
84+
attrs->replay_esn.replay_window / 64);
8585
MLX5_SET(ipsec_aso, aso_ctx, mode,
8686
MLX5_IPSEC_ASO_REPLAY_PROTECTION);
87-
}
87+
}
88+
MLX5_SET(ipsec_aso, aso_ctx, mode_parameter,
89+
attrs->replay_esn.esn);
8890
}
8991

9092
/* ASO context */
@@ -136,10 +138,10 @@ static int mlx5_create_ipsec_obj(struct mlx5e_ipsec_sa_entry *sa_entry)
136138
salt_iv_p = MLX5_ADDR_OF(ipsec_obj, obj, implicit_iv);
137139
memcpy(salt_iv_p, &aes_gcm->seq_iv, sizeof(aes_gcm->seq_iv));
138140
/* esn */
139-
if (attrs->esn_trigger) {
141+
if (attrs->replay_esn.trigger) {
140142
MLX5_SET(ipsec_obj, obj, esn_en, 1);
141-
MLX5_SET(ipsec_obj, obj, esn_msb, attrs->esn);
142-
MLX5_SET(ipsec_obj, obj, esn_overlap, attrs->esn_overlap);
143+
MLX5_SET(ipsec_obj, obj, esn_msb, attrs->replay_esn.esn_msb);
144+
MLX5_SET(ipsec_obj, obj, esn_overlap, attrs->replay_esn.overlap);
143145
}
144146

145147
MLX5_SET(ipsec_obj, obj, dekn, sa_entry->enc_key_id);
@@ -252,8 +254,8 @@ static int mlx5_modify_ipsec_obj(struct mlx5e_ipsec_sa_entry *sa_entry,
252254
MLX5_SET64(ipsec_obj, obj, modify_field_select,
253255
MLX5_MODIFY_IPSEC_BITMASK_ESN_OVERLAP |
254256
MLX5_MODIFY_IPSEC_BITMASK_ESN_MSB);
255-
MLX5_SET(ipsec_obj, obj, esn_msb, attrs->esn);
256-
MLX5_SET(ipsec_obj, obj, esn_overlap, attrs->esn_overlap);
257+
MLX5_SET(ipsec_obj, obj, esn_msb, attrs->replay_esn.esn_msb);
258+
MLX5_SET(ipsec_obj, obj, esn_overlap, attrs->replay_esn.overlap);
257259

258260
/* general object fields set */
259261
MLX5_SET(general_obj_in_cmd_hdr, in, opcode, MLX5_CMD_OP_MODIFY_GENERAL_OBJECT);
@@ -290,7 +292,7 @@ static void mlx5e_ipsec_update_esn_state(struct mlx5e_ipsec_sa_entry *sa_entry,
290292
struct mlx5_wqe_aso_ctrl_seg data = {};
291293

292294
if (mode_param < MLX5E_IPSEC_ESN_SCOPE_MID) {
293-
sa_entry->esn_state.esn++;
295+
sa_entry->esn_state.esn_msb++;
294296
sa_entry->esn_state.overlap = 0;
295297
} else {
296298
sa_entry->esn_state.overlap = 1;
@@ -434,7 +436,7 @@ static void mlx5e_ipsec_handle_event(struct work_struct *_work)
434436
if (ret)
435437
goto unlock;
436438

437-
if (attrs->esn_trigger &&
439+
if (attrs->replay_esn.trigger &&
438440
!MLX5_GET(ipsec_aso, aso->ctx, esn_event_arm)) {
439441
u32 mode_param = MLX5_GET(ipsec_aso, aso->ctx, mode_parameter);
440442

0 commit comments

Comments
 (0)