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