Thomas Gleixner | a61127c | 2019-05-29 16:57:49 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 2 | /* |
| 3 | * xfrm_replay.c - xfrm replay detection, derived from xfrm_state.c. |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2010 secunet Security Networks AG |
| 6 | * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com> |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 9 | #include <linux/export.h> |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 10 | #include <net/xfrm.h> |
| 11 | |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 12 | u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq) |
| 13 | { |
| 14 | u32 seq, seq_hi, bottom; |
| 15 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
| 16 | |
| 17 | if (!(x->props.flags & XFRM_STATE_ESN)) |
| 18 | return 0; |
| 19 | |
| 20 | seq = ntohl(net_seq); |
| 21 | seq_hi = replay_esn->seq_hi; |
| 22 | bottom = replay_esn->seq - replay_esn->replay_window + 1; |
| 23 | |
| 24 | if (likely(replay_esn->seq >= replay_esn->replay_window - 1)) { |
| 25 | /* A. same subspace */ |
| 26 | if (unlikely(seq < bottom)) |
| 27 | seq_hi++; |
| 28 | } else { |
| 29 | /* B. window spans two subspaces */ |
| 30 | if (unlikely(seq >= bottom)) |
| 31 | seq_hi--; |
| 32 | } |
| 33 | |
| 34 | return seq_hi; |
| 35 | } |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 36 | EXPORT_SYMBOL(xfrm_replay_seqhi); |
| 37 | ; |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 38 | static void xfrm_replay_notify(struct xfrm_state *x, int event) |
| 39 | { |
| 40 | struct km_event c; |
| 41 | /* we send notify messages in case |
| 42 | * 1. we updated on of the sequence numbers, and the seqno difference |
| 43 | * is at least x->replay_maxdiff, in this case we also update the |
| 44 | * timeout of our timer function |
| 45 | * 2. if x->replay_maxage has elapsed since last update, |
| 46 | * and there were changes |
| 47 | * |
| 48 | * The state structure must be locked! |
| 49 | */ |
| 50 | |
| 51 | switch (event) { |
| 52 | case XFRM_REPLAY_UPDATE: |
Thomas Egerer | cd808fc | 2013-09-19 13:19:19 +0200 | [diff] [blame] | 53 | if (!x->replay_maxdiff || |
| 54 | ((x->replay.seq - x->preplay.seq < x->replay_maxdiff) && |
| 55 | (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff))) { |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 56 | if (x->xflags & XFRM_TIME_DEFER) |
| 57 | event = XFRM_REPLAY_TIMEOUT; |
| 58 | else |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | break; |
| 63 | |
| 64 | case XFRM_REPLAY_TIMEOUT: |
| 65 | if (memcmp(&x->replay, &x->preplay, |
| 66 | sizeof(struct xfrm_replay_state)) == 0) { |
| 67 | x->xflags |= XFRM_TIME_DEFER; |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state)); |
| 75 | c.event = XFRM_MSG_NEWAE; |
| 76 | c.data.aevent = event; |
| 77 | km_state_notify(x, &c); |
| 78 | |
| 79 | if (x->replay_maxage && |
| 80 | !mod_timer(&x->rtimer, jiffies + x->replay_maxage)) |
| 81 | x->xflags &= ~XFRM_TIME_DEFER; |
| 82 | } |
| 83 | |
| 84 | static int xfrm_replay_overflow(struct xfrm_state *x, struct sk_buff *skb) |
| 85 | { |
| 86 | int err = 0; |
| 87 | struct net *net = xs_net(x); |
| 88 | |
| 89 | if (x->type->flags & XFRM_TYPE_REPLAY_PROT) { |
| 90 | XFRM_SKB_CB(skb)->seq.output.low = ++x->replay.oseq; |
Herbert Xu | 407d34e | 2015-05-21 00:38:12 +0800 | [diff] [blame] | 91 | XFRM_SKB_CB(skb)->seq.output.hi = 0; |
Petr Vaněk | 428d245 | 2020-05-30 14:39:12 +0200 | [diff] [blame] | 92 | if (unlikely(x->replay.oseq == 0) && |
| 93 | !(x->props.extra_flags & XFRM_SA_XFLAG_OSEQ_MAY_WRAP)) { |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 94 | x->replay.oseq--; |
| 95 | xfrm_audit_state_replay_overflow(x, skb); |
| 96 | err = -EOVERFLOW; |
| 97 | |
| 98 | return err; |
| 99 | } |
| 100 | if (xfrm_aevent_is_on(net)) |
| 101 | x->repl->notify(x, XFRM_REPLAY_UPDATE); |
| 102 | } |
| 103 | |
| 104 | return err; |
| 105 | } |
| 106 | |
| 107 | static int xfrm_replay_check(struct xfrm_state *x, |
| 108 | struct sk_buff *skb, __be32 net_seq) |
| 109 | { |
| 110 | u32 diff; |
| 111 | u32 seq = ntohl(net_seq); |
| 112 | |
Steffen Klassert | 36ae014 | 2011-03-28 19:45:52 +0000 | [diff] [blame] | 113 | if (!x->props.replay_window) |
| 114 | return 0; |
| 115 | |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 116 | if (unlikely(seq == 0)) |
| 117 | goto err; |
| 118 | |
| 119 | if (likely(seq > x->replay.seq)) |
| 120 | return 0; |
| 121 | |
| 122 | diff = x->replay.seq - seq; |
Fan Du | 33fce60 | 2013-09-17 15:14:13 +0800 | [diff] [blame] | 123 | if (diff >= x->props.replay_window) { |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 124 | x->stats.replay_window++; |
| 125 | goto err; |
| 126 | } |
| 127 | |
| 128 | if (x->replay.bitmap & (1U << diff)) { |
| 129 | x->stats.replay++; |
| 130 | goto err; |
| 131 | } |
| 132 | return 0; |
| 133 | |
| 134 | err: |
| 135 | xfrm_audit_state_replay(x, skb, net_seq); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | |
| 139 | static void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq) |
| 140 | { |
| 141 | u32 diff; |
| 142 | u32 seq = ntohl(net_seq); |
| 143 | |
| 144 | if (!x->props.replay_window) |
| 145 | return; |
| 146 | |
| 147 | if (seq > x->replay.seq) { |
| 148 | diff = seq - x->replay.seq; |
| 149 | if (diff < x->props.replay_window) |
| 150 | x->replay.bitmap = ((x->replay.bitmap) << diff) | 1; |
| 151 | else |
| 152 | x->replay.bitmap = 1; |
| 153 | x->replay.seq = seq; |
| 154 | } else { |
| 155 | diff = x->replay.seq - seq; |
| 156 | x->replay.bitmap |= (1U << diff); |
| 157 | } |
| 158 | |
| 159 | if (xfrm_aevent_is_on(xs_net(x))) |
Steffen Klassert | 1265fd6 | 2012-03-21 23:36:13 +0000 | [diff] [blame] | 160 | x->repl->notify(x, XFRM_REPLAY_UPDATE); |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 163 | static int xfrm_replay_overflow_bmp(struct xfrm_state *x, struct sk_buff *skb) |
| 164 | { |
| 165 | int err = 0; |
| 166 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
| 167 | struct net *net = xs_net(x); |
| 168 | |
| 169 | if (x->type->flags & XFRM_TYPE_REPLAY_PROT) { |
| 170 | XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq; |
Herbert Xu | 407d34e | 2015-05-21 00:38:12 +0800 | [diff] [blame] | 171 | XFRM_SKB_CB(skb)->seq.output.hi = 0; |
Petr Vaněk | 428d245 | 2020-05-30 14:39:12 +0200 | [diff] [blame] | 172 | if (unlikely(replay_esn->oseq == 0) && |
| 173 | !(x->props.extra_flags & XFRM_SA_XFLAG_OSEQ_MAY_WRAP)) { |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 174 | replay_esn->oseq--; |
| 175 | xfrm_audit_state_replay_overflow(x, skb); |
| 176 | err = -EOVERFLOW; |
| 177 | |
| 178 | return err; |
| 179 | } |
| 180 | if (xfrm_aevent_is_on(net)) |
| 181 | x->repl->notify(x, XFRM_REPLAY_UPDATE); |
| 182 | } |
| 183 | |
| 184 | return err; |
| 185 | } |
| 186 | |
| 187 | static int xfrm_replay_check_bmp(struct xfrm_state *x, |
| 188 | struct sk_buff *skb, __be32 net_seq) |
| 189 | { |
| 190 | unsigned int bitnr, nr; |
| 191 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
Steffen Klassert | 36ae014 | 2011-03-28 19:45:52 +0000 | [diff] [blame] | 192 | u32 pos; |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 193 | u32 seq = ntohl(net_seq); |
| 194 | u32 diff = replay_esn->seq - seq; |
Steffen Klassert | 36ae014 | 2011-03-28 19:45:52 +0000 | [diff] [blame] | 195 | |
| 196 | if (!replay_esn->replay_window) |
| 197 | return 0; |
| 198 | |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 199 | if (unlikely(seq == 0)) |
| 200 | goto err; |
| 201 | |
| 202 | if (likely(seq > replay_esn->seq)) |
| 203 | return 0; |
| 204 | |
| 205 | if (diff >= replay_esn->replay_window) { |
| 206 | x->stats.replay_window++; |
| 207 | goto err; |
| 208 | } |
| 209 | |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 210 | pos = (replay_esn->seq - 1) % replay_esn->replay_window; |
| 211 | |
| 212 | if (pos >= diff) |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 213 | bitnr = (pos - diff) % replay_esn->replay_window; |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 214 | else |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 215 | bitnr = replay_esn->replay_window - (diff - pos); |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 216 | |
| 217 | nr = bitnr >> 5; |
| 218 | bitnr = bitnr & 0x1F; |
| 219 | if (replay_esn->bmp[nr] & (1U << bitnr)) |
| 220 | goto err_replay; |
| 221 | |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 222 | return 0; |
| 223 | |
| 224 | err_replay: |
| 225 | x->stats.replay++; |
| 226 | err: |
| 227 | xfrm_audit_state_replay(x, skb, net_seq); |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | |
| 231 | static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq) |
| 232 | { |
| 233 | unsigned int bitnr, nr, i; |
| 234 | u32 diff; |
| 235 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
| 236 | u32 seq = ntohl(net_seq); |
Nickolai Zeldovich | e2f6725 | 2013-01-17 13:58:28 -0500 | [diff] [blame] | 237 | u32 pos; |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 238 | |
| 239 | if (!replay_esn->replay_window) |
| 240 | return; |
| 241 | |
Nickolai Zeldovich | e2f6725 | 2013-01-17 13:58:28 -0500 | [diff] [blame] | 242 | pos = (replay_esn->seq - 1) % replay_esn->replay_window; |
| 243 | |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 244 | if (seq > replay_esn->seq) { |
| 245 | diff = seq - replay_esn->seq; |
| 246 | |
| 247 | if (diff < replay_esn->replay_window) { |
| 248 | for (i = 1; i < diff; i++) { |
| 249 | bitnr = (pos + i) % replay_esn->replay_window; |
| 250 | nr = bitnr >> 5; |
| 251 | bitnr = bitnr & 0x1F; |
| 252 | replay_esn->bmp[nr] &= ~(1U << bitnr); |
| 253 | } |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 254 | } else { |
Steffen Klassert | e756682 | 2011-06-05 20:46:03 +0000 | [diff] [blame] | 255 | nr = (replay_esn->replay_window - 1) >> 5; |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 256 | for (i = 0; i <= nr; i++) |
| 257 | replay_esn->bmp[i] = 0; |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 260 | bitnr = (pos + diff) % replay_esn->replay_window; |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 261 | replay_esn->seq = seq; |
| 262 | } else { |
| 263 | diff = replay_esn->seq - seq; |
| 264 | |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 265 | if (pos >= diff) |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 266 | bitnr = (pos - diff) % replay_esn->replay_window; |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 267 | else |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 268 | bitnr = replay_esn->replay_window - (diff - pos); |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 271 | nr = bitnr >> 5; |
| 272 | bitnr = bitnr & 0x1F; |
| 273 | replay_esn->bmp[nr] |= (1U << bitnr); |
| 274 | |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 275 | if (xfrm_aevent_is_on(xs_net(x))) |
Steffen Klassert | 1265fd6 | 2012-03-21 23:36:13 +0000 | [diff] [blame] | 276 | x->repl->notify(x, XFRM_REPLAY_UPDATE); |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static void xfrm_replay_notify_bmp(struct xfrm_state *x, int event) |
| 280 | { |
| 281 | struct km_event c; |
| 282 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
| 283 | struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn; |
| 284 | |
| 285 | /* we send notify messages in case |
| 286 | * 1. we updated on of the sequence numbers, and the seqno difference |
| 287 | * is at least x->replay_maxdiff, in this case we also update the |
| 288 | * timeout of our timer function |
| 289 | * 2. if x->replay_maxage has elapsed since last update, |
| 290 | * and there were changes |
| 291 | * |
| 292 | * The state structure must be locked! |
| 293 | */ |
| 294 | |
| 295 | switch (event) { |
| 296 | case XFRM_REPLAY_UPDATE: |
Thomas Egerer | cd808fc | 2013-09-19 13:19:19 +0200 | [diff] [blame] | 297 | if (!x->replay_maxdiff || |
| 298 | ((replay_esn->seq - preplay_esn->seq < x->replay_maxdiff) && |
| 299 | (replay_esn->oseq - preplay_esn->oseq |
| 300 | < x->replay_maxdiff))) { |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 301 | if (x->xflags & XFRM_TIME_DEFER) |
| 302 | event = XFRM_REPLAY_TIMEOUT; |
| 303 | else |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | break; |
| 308 | |
| 309 | case XFRM_REPLAY_TIMEOUT: |
| 310 | if (memcmp(x->replay_esn, x->preplay_esn, |
| 311 | xfrm_replay_state_esn_len(replay_esn)) == 0) { |
| 312 | x->xflags |= XFRM_TIME_DEFER; |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | memcpy(x->preplay_esn, x->replay_esn, |
| 320 | xfrm_replay_state_esn_len(replay_esn)); |
| 321 | c.event = XFRM_MSG_NEWAE; |
| 322 | c.data.aevent = event; |
| 323 | km_state_notify(x, &c); |
| 324 | |
| 325 | if (x->replay_maxage && |
| 326 | !mod_timer(&x->rtimer, jiffies + x->replay_maxage)) |
| 327 | x->xflags &= ~XFRM_TIME_DEFER; |
| 328 | } |
| 329 | |
Steffen Klassert | 0017c0b | 2013-03-11 10:32:16 +0100 | [diff] [blame] | 330 | static void xfrm_replay_notify_esn(struct xfrm_state *x, int event) |
| 331 | { |
| 332 | u32 seq_diff, oseq_diff; |
| 333 | struct km_event c; |
| 334 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
| 335 | struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn; |
| 336 | |
| 337 | /* we send notify messages in case |
| 338 | * 1. we updated on of the sequence numbers, and the seqno difference |
| 339 | * is at least x->replay_maxdiff, in this case we also update the |
| 340 | * timeout of our timer function |
| 341 | * 2. if x->replay_maxage has elapsed since last update, |
| 342 | * and there were changes |
| 343 | * |
| 344 | * The state structure must be locked! |
| 345 | */ |
| 346 | |
| 347 | switch (event) { |
| 348 | case XFRM_REPLAY_UPDATE: |
Thomas Egerer | cd808fc | 2013-09-19 13:19:19 +0200 | [diff] [blame] | 349 | if (x->replay_maxdiff) { |
| 350 | if (replay_esn->seq_hi == preplay_esn->seq_hi) |
| 351 | seq_diff = replay_esn->seq - preplay_esn->seq; |
Steffen Klassert | 0017c0b | 2013-03-11 10:32:16 +0100 | [diff] [blame] | 352 | else |
Thomas Egerer | cd808fc | 2013-09-19 13:19:19 +0200 | [diff] [blame] | 353 | seq_diff = ~preplay_esn->seq + replay_esn->seq |
| 354 | + 1; |
| 355 | |
| 356 | if (replay_esn->oseq_hi == preplay_esn->oseq_hi) |
| 357 | oseq_diff = replay_esn->oseq |
| 358 | - preplay_esn->oseq; |
| 359 | else |
| 360 | oseq_diff = ~preplay_esn->oseq |
| 361 | + replay_esn->oseq + 1; |
| 362 | |
| 363 | if (seq_diff >= x->replay_maxdiff || |
| 364 | oseq_diff >= x->replay_maxdiff) |
| 365 | break; |
Steffen Klassert | 0017c0b | 2013-03-11 10:32:16 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Thomas Egerer | cd808fc | 2013-09-19 13:19:19 +0200 | [diff] [blame] | 368 | if (x->xflags & XFRM_TIME_DEFER) |
| 369 | event = XFRM_REPLAY_TIMEOUT; |
| 370 | else |
| 371 | return; |
| 372 | |
Steffen Klassert | 0017c0b | 2013-03-11 10:32:16 +0100 | [diff] [blame] | 373 | break; |
| 374 | |
| 375 | case XFRM_REPLAY_TIMEOUT: |
| 376 | if (memcmp(x->replay_esn, x->preplay_esn, |
| 377 | xfrm_replay_state_esn_len(replay_esn)) == 0) { |
| 378 | x->xflags |= XFRM_TIME_DEFER; |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | memcpy(x->preplay_esn, x->replay_esn, |
| 386 | xfrm_replay_state_esn_len(replay_esn)); |
| 387 | c.event = XFRM_MSG_NEWAE; |
| 388 | c.data.aevent = event; |
| 389 | km_state_notify(x, &c); |
| 390 | |
| 391 | if (x->replay_maxage && |
| 392 | !mod_timer(&x->rtimer, jiffies + x->replay_maxage)) |
| 393 | x->xflags &= ~XFRM_TIME_DEFER; |
| 394 | } |
| 395 | |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 396 | static int xfrm_replay_overflow_esn(struct xfrm_state *x, struct sk_buff *skb) |
| 397 | { |
| 398 | int err = 0; |
| 399 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
| 400 | struct net *net = xs_net(x); |
| 401 | |
| 402 | if (x->type->flags & XFRM_TYPE_REPLAY_PROT) { |
| 403 | XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq; |
| 404 | XFRM_SKB_CB(skb)->seq.output.hi = replay_esn->oseq_hi; |
| 405 | |
| 406 | if (unlikely(replay_esn->oseq == 0)) { |
| 407 | XFRM_SKB_CB(skb)->seq.output.hi = ++replay_esn->oseq_hi; |
| 408 | |
| 409 | if (replay_esn->oseq_hi == 0) { |
| 410 | replay_esn->oseq--; |
| 411 | replay_esn->oseq_hi--; |
| 412 | xfrm_audit_state_replay_overflow(x, skb); |
| 413 | err = -EOVERFLOW; |
| 414 | |
| 415 | return err; |
| 416 | } |
| 417 | } |
| 418 | if (xfrm_aevent_is_on(net)) |
| 419 | x->repl->notify(x, XFRM_REPLAY_UPDATE); |
| 420 | } |
| 421 | |
| 422 | return err; |
| 423 | } |
| 424 | |
| 425 | static int xfrm_replay_check_esn(struct xfrm_state *x, |
| 426 | struct sk_buff *skb, __be32 net_seq) |
| 427 | { |
| 428 | unsigned int bitnr, nr; |
| 429 | u32 diff; |
| 430 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
Steffen Klassert | 36ae014 | 2011-03-28 19:45:52 +0000 | [diff] [blame] | 431 | u32 pos; |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 432 | u32 seq = ntohl(net_seq); |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 433 | u32 wsize = replay_esn->replay_window; |
| 434 | u32 top = replay_esn->seq; |
| 435 | u32 bottom = top - wsize + 1; |
| 436 | |
Steffen Klassert | 36ae014 | 2011-03-28 19:45:52 +0000 | [diff] [blame] | 437 | if (!wsize) |
| 438 | return 0; |
| 439 | |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 440 | if (unlikely(seq == 0 && replay_esn->seq_hi == 0 && |
| 441 | (replay_esn->seq < replay_esn->replay_window - 1))) |
| 442 | goto err; |
| 443 | |
| 444 | diff = top - seq; |
| 445 | |
| 446 | if (likely(top >= wsize - 1)) { |
| 447 | /* A. same subspace */ |
| 448 | if (likely(seq > top) || seq < bottom) |
| 449 | return 0; |
| 450 | } else { |
| 451 | /* B. window spans two subspaces */ |
| 452 | if (likely(seq > top && seq < bottom)) |
| 453 | return 0; |
| 454 | if (seq >= bottom) |
| 455 | diff = ~seq + top + 1; |
| 456 | } |
| 457 | |
| 458 | if (diff >= replay_esn->replay_window) { |
| 459 | x->stats.replay_window++; |
| 460 | goto err; |
| 461 | } |
| 462 | |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 463 | pos = (replay_esn->seq - 1) % replay_esn->replay_window; |
| 464 | |
| 465 | if (pos >= diff) |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 466 | bitnr = (pos - diff) % replay_esn->replay_window; |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 467 | else |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 468 | bitnr = replay_esn->replay_window - (diff - pos); |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 469 | |
| 470 | nr = bitnr >> 5; |
| 471 | bitnr = bitnr & 0x1F; |
| 472 | if (replay_esn->bmp[nr] & (1U << bitnr)) |
| 473 | goto err_replay; |
| 474 | |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 475 | return 0; |
| 476 | |
| 477 | err_replay: |
| 478 | x->stats.replay++; |
| 479 | err: |
| 480 | xfrm_audit_state_replay(x, skb, net_seq); |
| 481 | return -EINVAL; |
| 482 | } |
| 483 | |
Steffen Klassert | 3b59df4 | 2012-09-04 00:03:29 +0000 | [diff] [blame] | 484 | static int xfrm_replay_recheck_esn(struct xfrm_state *x, |
| 485 | struct sk_buff *skb, __be32 net_seq) |
| 486 | { |
| 487 | if (unlikely(XFRM_SKB_CB(skb)->seq.input.hi != |
| 488 | htonl(xfrm_replay_seqhi(x, net_seq)))) { |
| 489 | x->stats.replay_window++; |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
| 493 | return xfrm_replay_check_esn(x, skb, net_seq); |
| 494 | } |
| 495 | |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 496 | static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq) |
| 497 | { |
| 498 | unsigned int bitnr, nr, i; |
| 499 | int wrap; |
| 500 | u32 diff, pos, seq, seq_hi; |
| 501 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
| 502 | |
| 503 | if (!replay_esn->replay_window) |
| 504 | return; |
| 505 | |
| 506 | seq = ntohl(net_seq); |
| 507 | pos = (replay_esn->seq - 1) % replay_esn->replay_window; |
| 508 | seq_hi = xfrm_replay_seqhi(x, net_seq); |
| 509 | wrap = seq_hi - replay_esn->seq_hi; |
| 510 | |
| 511 | if ((!wrap && seq > replay_esn->seq) || wrap > 0) { |
| 512 | if (likely(!wrap)) |
| 513 | diff = seq - replay_esn->seq; |
| 514 | else |
| 515 | diff = ~replay_esn->seq + seq + 1; |
| 516 | |
| 517 | if (diff < replay_esn->replay_window) { |
| 518 | for (i = 1; i < diff; i++) { |
| 519 | bitnr = (pos + i) % replay_esn->replay_window; |
| 520 | nr = bitnr >> 5; |
| 521 | bitnr = bitnr & 0x1F; |
| 522 | replay_esn->bmp[nr] &= ~(1U << bitnr); |
| 523 | } |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 524 | } else { |
Steffen Klassert | e756682 | 2011-06-05 20:46:03 +0000 | [diff] [blame] | 525 | nr = (replay_esn->replay_window - 1) >> 5; |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 526 | for (i = 0; i <= nr; i++) |
| 527 | replay_esn->bmp[i] = 0; |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 530 | bitnr = (pos + diff) % replay_esn->replay_window; |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 531 | replay_esn->seq = seq; |
| 532 | |
| 533 | if (unlikely(wrap > 0)) |
| 534 | replay_esn->seq_hi++; |
| 535 | } else { |
| 536 | diff = replay_esn->seq - seq; |
| 537 | |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 538 | if (pos >= diff) |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 539 | bitnr = (pos - diff) % replay_esn->replay_window; |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 540 | else |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 541 | bitnr = replay_esn->replay_window - (diff - pos); |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Yossef Efraim | 50bd870 | 2018-01-14 11:39:10 +0200 | [diff] [blame] | 544 | xfrm_dev_state_advance_esn(x); |
| 545 | |
Steffen Klassert | 1d97437 | 2011-10-11 01:58:37 +0000 | [diff] [blame] | 546 | nr = bitnr >> 5; |
| 547 | bitnr = bitnr & 0x1F; |
| 548 | replay_esn->bmp[nr] |= (1U << bitnr); |
| 549 | |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 550 | if (xfrm_aevent_is_on(xs_net(x))) |
Steffen Klassert | 1265fd6 | 2012-03-21 23:36:13 +0000 | [diff] [blame] | 551 | x->repl->notify(x, XFRM_REPLAY_UPDATE); |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Steffen Klassert | d7dbefc | 2017-04-14 10:07:01 +0200 | [diff] [blame] | 554 | #ifdef CONFIG_XFRM_OFFLOAD |
| 555 | static int xfrm_replay_overflow_offload(struct xfrm_state *x, struct sk_buff *skb) |
| 556 | { |
| 557 | int err = 0; |
| 558 | struct net *net = xs_net(x); |
| 559 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 560 | __u32 oseq = x->replay.oseq; |
| 561 | |
| 562 | if (!xo) |
| 563 | return xfrm_replay_overflow(x, skb); |
| 564 | |
| 565 | if (x->type->flags & XFRM_TYPE_REPLAY_PROT) { |
| 566 | if (!skb_is_gso(skb)) { |
| 567 | XFRM_SKB_CB(skb)->seq.output.low = ++oseq; |
| 568 | xo->seq.low = oseq; |
| 569 | } else { |
| 570 | XFRM_SKB_CB(skb)->seq.output.low = oseq + 1; |
| 571 | xo->seq.low = oseq + 1; |
| 572 | oseq += skb_shinfo(skb)->gso_segs; |
| 573 | } |
| 574 | |
| 575 | XFRM_SKB_CB(skb)->seq.output.hi = 0; |
| 576 | xo->seq.hi = 0; |
Petr Vaněk | 428d245 | 2020-05-30 14:39:12 +0200 | [diff] [blame] | 577 | if (unlikely(oseq < x->replay.oseq) && |
| 578 | !(x->props.extra_flags & XFRM_SA_XFLAG_OSEQ_MAY_WRAP)) { |
Steffen Klassert | d7dbefc | 2017-04-14 10:07:01 +0200 | [diff] [blame] | 579 | xfrm_audit_state_replay_overflow(x, skb); |
| 580 | err = -EOVERFLOW; |
| 581 | |
| 582 | return err; |
| 583 | } |
| 584 | |
| 585 | x->replay.oseq = oseq; |
| 586 | |
| 587 | if (xfrm_aevent_is_on(net)) |
| 588 | x->repl->notify(x, XFRM_REPLAY_UPDATE); |
| 589 | } |
| 590 | |
| 591 | return err; |
| 592 | } |
| 593 | |
| 594 | static int xfrm_replay_overflow_offload_bmp(struct xfrm_state *x, struct sk_buff *skb) |
| 595 | { |
| 596 | int err = 0; |
| 597 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 598 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
| 599 | struct net *net = xs_net(x); |
| 600 | __u32 oseq = replay_esn->oseq; |
| 601 | |
| 602 | if (!xo) |
| 603 | return xfrm_replay_overflow_bmp(x, skb); |
| 604 | |
| 605 | if (x->type->flags & XFRM_TYPE_REPLAY_PROT) { |
| 606 | if (!skb_is_gso(skb)) { |
| 607 | XFRM_SKB_CB(skb)->seq.output.low = ++oseq; |
| 608 | xo->seq.low = oseq; |
| 609 | } else { |
| 610 | XFRM_SKB_CB(skb)->seq.output.low = oseq + 1; |
| 611 | xo->seq.low = oseq + 1; |
| 612 | oseq += skb_shinfo(skb)->gso_segs; |
| 613 | } |
| 614 | |
| 615 | XFRM_SKB_CB(skb)->seq.output.hi = 0; |
| 616 | xo->seq.hi = 0; |
Petr Vaněk | 428d245 | 2020-05-30 14:39:12 +0200 | [diff] [blame] | 617 | if (unlikely(oseq < replay_esn->oseq) && |
| 618 | !(x->props.extra_flags & XFRM_SA_XFLAG_OSEQ_MAY_WRAP)) { |
Steffen Klassert | d7dbefc | 2017-04-14 10:07:01 +0200 | [diff] [blame] | 619 | xfrm_audit_state_replay_overflow(x, skb); |
| 620 | err = -EOVERFLOW; |
| 621 | |
| 622 | return err; |
| 623 | } else { |
| 624 | replay_esn->oseq = oseq; |
| 625 | } |
| 626 | |
| 627 | if (xfrm_aevent_is_on(net)) |
| 628 | x->repl->notify(x, XFRM_REPLAY_UPDATE); |
| 629 | } |
| 630 | |
| 631 | return err; |
| 632 | } |
| 633 | |
| 634 | static int xfrm_replay_overflow_offload_esn(struct xfrm_state *x, struct sk_buff *skb) |
| 635 | { |
| 636 | int err = 0; |
| 637 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 638 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
| 639 | struct net *net = xs_net(x); |
| 640 | __u32 oseq = replay_esn->oseq; |
| 641 | __u32 oseq_hi = replay_esn->oseq_hi; |
| 642 | |
| 643 | if (!xo) |
| 644 | return xfrm_replay_overflow_esn(x, skb); |
| 645 | |
| 646 | if (x->type->flags & XFRM_TYPE_REPLAY_PROT) { |
| 647 | if (!skb_is_gso(skb)) { |
| 648 | XFRM_SKB_CB(skb)->seq.output.low = ++oseq; |
| 649 | XFRM_SKB_CB(skb)->seq.output.hi = oseq_hi; |
| 650 | xo->seq.low = oseq; |
| 651 | xo->seq.hi = oseq_hi; |
| 652 | } else { |
| 653 | XFRM_SKB_CB(skb)->seq.output.low = oseq + 1; |
| 654 | XFRM_SKB_CB(skb)->seq.output.hi = oseq_hi; |
Steffen Klassert | b8b549e | 2018-02-28 09:23:19 +0100 | [diff] [blame] | 655 | xo->seq.low = oseq + 1; |
Steffen Klassert | d7dbefc | 2017-04-14 10:07:01 +0200 | [diff] [blame] | 656 | xo->seq.hi = oseq_hi; |
| 657 | oseq += skb_shinfo(skb)->gso_segs; |
| 658 | } |
| 659 | |
| 660 | if (unlikely(oseq < replay_esn->oseq)) { |
| 661 | XFRM_SKB_CB(skb)->seq.output.hi = ++oseq_hi; |
| 662 | xo->seq.hi = oseq_hi; |
Yossef Efraim | 0ba23a2 | 2017-11-28 11:49:28 +0200 | [diff] [blame] | 663 | replay_esn->oseq_hi = oseq_hi; |
Steffen Klassert | d7dbefc | 2017-04-14 10:07:01 +0200 | [diff] [blame] | 664 | if (replay_esn->oseq_hi == 0) { |
| 665 | replay_esn->oseq--; |
| 666 | replay_esn->oseq_hi--; |
| 667 | xfrm_audit_state_replay_overflow(x, skb); |
| 668 | err = -EOVERFLOW; |
| 669 | |
| 670 | return err; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | replay_esn->oseq = oseq; |
Steffen Klassert | d7dbefc | 2017-04-14 10:07:01 +0200 | [diff] [blame] | 675 | |
| 676 | if (xfrm_aevent_is_on(net)) |
| 677 | x->repl->notify(x, XFRM_REPLAY_UPDATE); |
| 678 | } |
| 679 | |
| 680 | return err; |
| 681 | } |
| 682 | |
| 683 | static const struct xfrm_replay xfrm_replay_legacy = { |
| 684 | .advance = xfrm_replay_advance, |
| 685 | .check = xfrm_replay_check, |
| 686 | .recheck = xfrm_replay_check, |
| 687 | .notify = xfrm_replay_notify, |
| 688 | .overflow = xfrm_replay_overflow_offload, |
| 689 | }; |
| 690 | |
| 691 | static const struct xfrm_replay xfrm_replay_bmp = { |
| 692 | .advance = xfrm_replay_advance_bmp, |
| 693 | .check = xfrm_replay_check_bmp, |
| 694 | .recheck = xfrm_replay_check_bmp, |
| 695 | .notify = xfrm_replay_notify_bmp, |
| 696 | .overflow = xfrm_replay_overflow_offload_bmp, |
| 697 | }; |
| 698 | |
| 699 | static const struct xfrm_replay xfrm_replay_esn = { |
| 700 | .advance = xfrm_replay_advance_esn, |
| 701 | .check = xfrm_replay_check_esn, |
| 702 | .recheck = xfrm_replay_recheck_esn, |
| 703 | .notify = xfrm_replay_notify_esn, |
| 704 | .overflow = xfrm_replay_overflow_offload_esn, |
| 705 | }; |
| 706 | #else |
Julia Lawall | e45a8a9 | 2016-08-09 18:27:08 +0200 | [diff] [blame] | 707 | static const struct xfrm_replay xfrm_replay_legacy = { |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 708 | .advance = xfrm_replay_advance, |
| 709 | .check = xfrm_replay_check, |
Steffen Klassert | 3b59df4 | 2012-09-04 00:03:29 +0000 | [diff] [blame] | 710 | .recheck = xfrm_replay_check, |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 711 | .notify = xfrm_replay_notify, |
| 712 | .overflow = xfrm_replay_overflow, |
| 713 | }; |
| 714 | |
Julia Lawall | e45a8a9 | 2016-08-09 18:27:08 +0200 | [diff] [blame] | 715 | static const struct xfrm_replay xfrm_replay_bmp = { |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 716 | .advance = xfrm_replay_advance_bmp, |
| 717 | .check = xfrm_replay_check_bmp, |
Steffen Klassert | 3b59df4 | 2012-09-04 00:03:29 +0000 | [diff] [blame] | 718 | .recheck = xfrm_replay_check_bmp, |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 719 | .notify = xfrm_replay_notify_bmp, |
| 720 | .overflow = xfrm_replay_overflow_bmp, |
| 721 | }; |
| 722 | |
Julia Lawall | e45a8a9 | 2016-08-09 18:27:08 +0200 | [diff] [blame] | 723 | static const struct xfrm_replay xfrm_replay_esn = { |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 724 | .advance = xfrm_replay_advance_esn, |
| 725 | .check = xfrm_replay_check_esn, |
Steffen Klassert | 3b59df4 | 2012-09-04 00:03:29 +0000 | [diff] [blame] | 726 | .recheck = xfrm_replay_recheck_esn, |
Steffen Klassert | 0017c0b | 2013-03-11 10:32:16 +0100 | [diff] [blame] | 727 | .notify = xfrm_replay_notify_esn, |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 728 | .overflow = xfrm_replay_overflow_esn, |
| 729 | }; |
Steffen Klassert | d7dbefc | 2017-04-14 10:07:01 +0200 | [diff] [blame] | 730 | #endif |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 731 | |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 732 | int xfrm_init_replay(struct xfrm_state *x) |
| 733 | { |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 734 | struct xfrm_replay_state_esn *replay_esn = x->replay_esn; |
| 735 | |
| 736 | if (replay_esn) { |
| 737 | if (replay_esn->replay_window > |
Steffen Klassert | 3f602b0 | 2011-04-25 19:39:24 +0000 | [diff] [blame] | 738 | replay_esn->bmp_len * sizeof(__u32) * 8) |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 739 | return -EINVAL; |
| 740 | |
Ulrich Weber | aafd0d8 | 2012-11-08 11:15:44 +0100 | [diff] [blame] | 741 | if (x->props.flags & XFRM_STATE_ESN) { |
| 742 | if (replay_esn->replay_window == 0) |
| 743 | return -EINVAL; |
| 744 | x->repl = &xfrm_replay_esn; |
Steffen Klassert | d7dbefc | 2017-04-14 10:07:01 +0200 | [diff] [blame] | 745 | } else { |
Ulrich Weber | aafd0d8 | 2012-11-08 11:15:44 +0100 | [diff] [blame] | 746 | x->repl = &xfrm_replay_bmp; |
Steffen Klassert | d7dbefc | 2017-04-14 10:07:01 +0200 | [diff] [blame] | 747 | } |
| 748 | } else { |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 749 | x->repl = &xfrm_replay_legacy; |
Steffen Klassert | d7dbefc | 2017-04-14 10:07:01 +0200 | [diff] [blame] | 750 | } |
Steffen Klassert | 97e15c3 | 2011-03-08 00:09:09 +0000 | [diff] [blame] | 751 | |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 752 | return 0; |
| 753 | } |
| 754 | EXPORT_SYMBOL(xfrm_init_replay); |