xfrm_replay.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * xfrm_replay.c - xfrm replay detection, derived from xfrm_state.c.
  3. *
  4. * Copyright (C) 2010 secunet Security Networks AG
  5. * Copyright (C) 2010 Steffen Klassert <steffen.klassert@secunet.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <linux/export.h>
  21. #include <net/xfrm.h>
  22. u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq)
  23. {
  24. u32 seq, seq_hi, bottom;
  25. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  26. if (!(x->props.flags & XFRM_STATE_ESN))
  27. return 0;
  28. seq = ntohl(net_seq);
  29. seq_hi = replay_esn->seq_hi;
  30. bottom = replay_esn->seq - replay_esn->replay_window + 1;
  31. if (likely(replay_esn->seq >= replay_esn->replay_window - 1)) {
  32. /* A. same subspace */
  33. if (unlikely(seq < bottom))
  34. seq_hi++;
  35. } else {
  36. /* B. window spans two subspaces */
  37. if (unlikely(seq >= bottom))
  38. seq_hi--;
  39. }
  40. return seq_hi;
  41. }
  42. static void xfrm_replay_notify(struct xfrm_state *x, int event)
  43. {
  44. struct km_event c;
  45. /* we send notify messages in case
  46. * 1. we updated on of the sequence numbers, and the seqno difference
  47. * is at least x->replay_maxdiff, in this case we also update the
  48. * timeout of our timer function
  49. * 2. if x->replay_maxage has elapsed since last update,
  50. * and there were changes
  51. *
  52. * The state structure must be locked!
  53. */
  54. switch (event) {
  55. case XFRM_REPLAY_UPDATE:
  56. if (!x->replay_maxdiff ||
  57. ((x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
  58. (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff))) {
  59. if (x->xflags & XFRM_TIME_DEFER)
  60. event = XFRM_REPLAY_TIMEOUT;
  61. else
  62. return;
  63. }
  64. break;
  65. case XFRM_REPLAY_TIMEOUT:
  66. if (memcmp(&x->replay, &x->preplay,
  67. sizeof(struct xfrm_replay_state)) == 0) {
  68. x->xflags |= XFRM_TIME_DEFER;
  69. return;
  70. }
  71. break;
  72. }
  73. memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
  74. c.event = XFRM_MSG_NEWAE;
  75. c.data.aevent = event;
  76. km_state_notify(x, &c);
  77. if (x->replay_maxage &&
  78. !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
  79. x->xflags &= ~XFRM_TIME_DEFER;
  80. }
  81. static int xfrm_replay_overflow(struct xfrm_state *x, struct sk_buff *skb)
  82. {
  83. int err = 0;
  84. struct net *net = xs_net(x);
  85. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  86. XFRM_SKB_CB(skb)->seq.output.low = ++x->replay.oseq;
  87. XFRM_SKB_CB(skb)->seq.output.hi = 0;
  88. if (unlikely(x->replay.oseq == 0)) {
  89. x->replay.oseq--;
  90. xfrm_audit_state_replay_overflow(x, skb);
  91. err = -EOVERFLOW;
  92. return err;
  93. }
  94. if (xfrm_aevent_is_on(net))
  95. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  96. }
  97. return err;
  98. }
  99. static int xfrm_replay_check(struct xfrm_state *x,
  100. struct sk_buff *skb, __be32 net_seq)
  101. {
  102. u32 diff;
  103. u32 seq = ntohl(net_seq);
  104. if (!x->props.replay_window)
  105. return 0;
  106. if (unlikely(seq == 0))
  107. goto err;
  108. if (likely(seq > x->replay.seq))
  109. return 0;
  110. diff = x->replay.seq - seq;
  111. if (diff >= x->props.replay_window) {
  112. x->stats.replay_window++;
  113. goto err;
  114. }
  115. if (x->replay.bitmap & (1U << diff)) {
  116. x->stats.replay++;
  117. goto err;
  118. }
  119. return 0;
  120. err:
  121. xfrm_audit_state_replay(x, skb, net_seq);
  122. return -EINVAL;
  123. }
  124. static void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
  125. {
  126. u32 diff;
  127. u32 seq = ntohl(net_seq);
  128. if (!x->props.replay_window)
  129. return;
  130. if (seq > x->replay.seq) {
  131. diff = seq - x->replay.seq;
  132. if (diff < x->props.replay_window)
  133. x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
  134. else
  135. x->replay.bitmap = 1;
  136. x->replay.seq = seq;
  137. } else {
  138. diff = x->replay.seq - seq;
  139. x->replay.bitmap |= (1U << diff);
  140. }
  141. if (xfrm_aevent_is_on(xs_net(x)))
  142. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  143. }
  144. static int xfrm_replay_overflow_bmp(struct xfrm_state *x, struct sk_buff *skb)
  145. {
  146. int err = 0;
  147. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  148. struct net *net = xs_net(x);
  149. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  150. XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq;
  151. XFRM_SKB_CB(skb)->seq.output.hi = 0;
  152. if (unlikely(replay_esn->oseq == 0)) {
  153. replay_esn->oseq--;
  154. xfrm_audit_state_replay_overflow(x, skb);
  155. err = -EOVERFLOW;
  156. return err;
  157. }
  158. if (xfrm_aevent_is_on(net))
  159. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  160. }
  161. return err;
  162. }
  163. static int xfrm_replay_check_bmp(struct xfrm_state *x,
  164. struct sk_buff *skb, __be32 net_seq)
  165. {
  166. unsigned int bitnr, nr;
  167. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  168. u32 pos;
  169. u32 seq = ntohl(net_seq);
  170. u32 diff = replay_esn->seq - seq;
  171. if (!replay_esn->replay_window)
  172. return 0;
  173. if (unlikely(seq == 0))
  174. goto err;
  175. if (likely(seq > replay_esn->seq))
  176. return 0;
  177. if (diff >= replay_esn->replay_window) {
  178. x->stats.replay_window++;
  179. goto err;
  180. }
  181. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  182. if (pos >= diff)
  183. bitnr = (pos - diff) % replay_esn->replay_window;
  184. else
  185. bitnr = replay_esn->replay_window - (diff - pos);
  186. nr = bitnr >> 5;
  187. bitnr = bitnr & 0x1F;
  188. if (replay_esn->bmp[nr] & (1U << bitnr))
  189. goto err_replay;
  190. return 0;
  191. err_replay:
  192. x->stats.replay++;
  193. err:
  194. xfrm_audit_state_replay(x, skb, net_seq);
  195. return -EINVAL;
  196. }
  197. static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq)
  198. {
  199. unsigned int bitnr, nr, i;
  200. u32 diff;
  201. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  202. u32 seq = ntohl(net_seq);
  203. u32 pos;
  204. if (!replay_esn->replay_window)
  205. return;
  206. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  207. if (seq > replay_esn->seq) {
  208. diff = seq - replay_esn->seq;
  209. if (diff < replay_esn->replay_window) {
  210. for (i = 1; i < diff; i++) {
  211. bitnr = (pos + i) % replay_esn->replay_window;
  212. nr = bitnr >> 5;
  213. bitnr = bitnr & 0x1F;
  214. replay_esn->bmp[nr] &= ~(1U << bitnr);
  215. }
  216. } else {
  217. nr = (replay_esn->replay_window - 1) >> 5;
  218. for (i = 0; i <= nr; i++)
  219. replay_esn->bmp[i] = 0;
  220. }
  221. bitnr = (pos + diff) % replay_esn->replay_window;
  222. replay_esn->seq = seq;
  223. } else {
  224. diff = replay_esn->seq - seq;
  225. if (pos >= diff)
  226. bitnr = (pos - diff) % replay_esn->replay_window;
  227. else
  228. bitnr = replay_esn->replay_window - (diff - pos);
  229. }
  230. nr = bitnr >> 5;
  231. bitnr = bitnr & 0x1F;
  232. replay_esn->bmp[nr] |= (1U << bitnr);
  233. if (xfrm_aevent_is_on(xs_net(x)))
  234. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  235. }
  236. static void xfrm_replay_notify_bmp(struct xfrm_state *x, int event)
  237. {
  238. struct km_event c;
  239. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  240. struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
  241. /* we send notify messages in case
  242. * 1. we updated on of the sequence numbers, and the seqno difference
  243. * is at least x->replay_maxdiff, in this case we also update the
  244. * timeout of our timer function
  245. * 2. if x->replay_maxage has elapsed since last update,
  246. * and there were changes
  247. *
  248. * The state structure must be locked!
  249. */
  250. switch (event) {
  251. case XFRM_REPLAY_UPDATE:
  252. if (!x->replay_maxdiff ||
  253. ((replay_esn->seq - preplay_esn->seq < x->replay_maxdiff) &&
  254. (replay_esn->oseq - preplay_esn->oseq
  255. < x->replay_maxdiff))) {
  256. if (x->xflags & XFRM_TIME_DEFER)
  257. event = XFRM_REPLAY_TIMEOUT;
  258. else
  259. return;
  260. }
  261. break;
  262. case XFRM_REPLAY_TIMEOUT:
  263. if (memcmp(x->replay_esn, x->preplay_esn,
  264. xfrm_replay_state_esn_len(replay_esn)) == 0) {
  265. x->xflags |= XFRM_TIME_DEFER;
  266. return;
  267. }
  268. break;
  269. }
  270. memcpy(x->preplay_esn, x->replay_esn,
  271. xfrm_replay_state_esn_len(replay_esn));
  272. c.event = XFRM_MSG_NEWAE;
  273. c.data.aevent = event;
  274. km_state_notify(x, &c);
  275. if (x->replay_maxage &&
  276. !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
  277. x->xflags &= ~XFRM_TIME_DEFER;
  278. }
  279. static void xfrm_replay_notify_esn(struct xfrm_state *x, int event)
  280. {
  281. u32 seq_diff, oseq_diff;
  282. struct km_event c;
  283. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  284. struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
  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. switch (event) {
  295. case XFRM_REPLAY_UPDATE:
  296. if (x->replay_maxdiff) {
  297. if (replay_esn->seq_hi == preplay_esn->seq_hi)
  298. seq_diff = replay_esn->seq - preplay_esn->seq;
  299. else
  300. seq_diff = ~preplay_esn->seq + replay_esn->seq
  301. + 1;
  302. if (replay_esn->oseq_hi == preplay_esn->oseq_hi)
  303. oseq_diff = replay_esn->oseq
  304. - preplay_esn->oseq;
  305. else
  306. oseq_diff = ~preplay_esn->oseq
  307. + replay_esn->oseq + 1;
  308. if (seq_diff >= x->replay_maxdiff ||
  309. oseq_diff >= x->replay_maxdiff)
  310. break;
  311. }
  312. if (x->xflags & XFRM_TIME_DEFER)
  313. event = XFRM_REPLAY_TIMEOUT;
  314. else
  315. return;
  316. break;
  317. case XFRM_REPLAY_TIMEOUT:
  318. if (memcmp(x->replay_esn, x->preplay_esn,
  319. xfrm_replay_state_esn_len(replay_esn)) == 0) {
  320. x->xflags |= XFRM_TIME_DEFER;
  321. return;
  322. }
  323. break;
  324. }
  325. memcpy(x->preplay_esn, x->replay_esn,
  326. xfrm_replay_state_esn_len(replay_esn));
  327. c.event = XFRM_MSG_NEWAE;
  328. c.data.aevent = event;
  329. km_state_notify(x, &c);
  330. if (x->replay_maxage &&
  331. !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
  332. x->xflags &= ~XFRM_TIME_DEFER;
  333. }
  334. static int xfrm_replay_overflow_esn(struct xfrm_state *x, struct sk_buff *skb)
  335. {
  336. int err = 0;
  337. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  338. struct net *net = xs_net(x);
  339. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  340. XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq;
  341. XFRM_SKB_CB(skb)->seq.output.hi = replay_esn->oseq_hi;
  342. if (unlikely(replay_esn->oseq == 0)) {
  343. XFRM_SKB_CB(skb)->seq.output.hi = ++replay_esn->oseq_hi;
  344. if (replay_esn->oseq_hi == 0) {
  345. replay_esn->oseq--;
  346. replay_esn->oseq_hi--;
  347. xfrm_audit_state_replay_overflow(x, skb);
  348. err = -EOVERFLOW;
  349. return err;
  350. }
  351. }
  352. if (xfrm_aevent_is_on(net))
  353. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  354. }
  355. return err;
  356. }
  357. static int xfrm_replay_check_esn(struct xfrm_state *x,
  358. struct sk_buff *skb, __be32 net_seq)
  359. {
  360. unsigned int bitnr, nr;
  361. u32 diff;
  362. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  363. u32 pos;
  364. u32 seq = ntohl(net_seq);
  365. u32 wsize = replay_esn->replay_window;
  366. u32 top = replay_esn->seq;
  367. u32 bottom = top - wsize + 1;
  368. if (!wsize)
  369. return 0;
  370. if (unlikely(seq == 0 && replay_esn->seq_hi == 0 &&
  371. (replay_esn->seq < replay_esn->replay_window - 1)))
  372. goto err;
  373. diff = top - seq;
  374. if (likely(top >= wsize - 1)) {
  375. /* A. same subspace */
  376. if (likely(seq > top) || seq < bottom)
  377. return 0;
  378. } else {
  379. /* B. window spans two subspaces */
  380. if (likely(seq > top && seq < bottom))
  381. return 0;
  382. if (seq >= bottom)
  383. diff = ~seq + top + 1;
  384. }
  385. if (diff >= replay_esn->replay_window) {
  386. x->stats.replay_window++;
  387. goto err;
  388. }
  389. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  390. if (pos >= diff)
  391. bitnr = (pos - diff) % replay_esn->replay_window;
  392. else
  393. bitnr = replay_esn->replay_window - (diff - pos);
  394. nr = bitnr >> 5;
  395. bitnr = bitnr & 0x1F;
  396. if (replay_esn->bmp[nr] & (1U << bitnr))
  397. goto err_replay;
  398. return 0;
  399. err_replay:
  400. x->stats.replay++;
  401. err:
  402. xfrm_audit_state_replay(x, skb, net_seq);
  403. return -EINVAL;
  404. }
  405. static int xfrm_replay_recheck_esn(struct xfrm_state *x,
  406. struct sk_buff *skb, __be32 net_seq)
  407. {
  408. if (unlikely(XFRM_SKB_CB(skb)->seq.input.hi !=
  409. htonl(xfrm_replay_seqhi(x, net_seq)))) {
  410. x->stats.replay_window++;
  411. return -EINVAL;
  412. }
  413. return xfrm_replay_check_esn(x, skb, net_seq);
  414. }
  415. static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq)
  416. {
  417. unsigned int bitnr, nr, i;
  418. int wrap;
  419. u32 diff, pos, seq, seq_hi;
  420. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  421. if (!replay_esn->replay_window)
  422. return;
  423. seq = ntohl(net_seq);
  424. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  425. seq_hi = xfrm_replay_seqhi(x, net_seq);
  426. wrap = seq_hi - replay_esn->seq_hi;
  427. if ((!wrap && seq > replay_esn->seq) || wrap > 0) {
  428. if (likely(!wrap))
  429. diff = seq - replay_esn->seq;
  430. else
  431. diff = ~replay_esn->seq + seq + 1;
  432. if (diff < replay_esn->replay_window) {
  433. for (i = 1; i < diff; i++) {
  434. bitnr = (pos + i) % replay_esn->replay_window;
  435. nr = bitnr >> 5;
  436. bitnr = bitnr & 0x1F;
  437. replay_esn->bmp[nr] &= ~(1U << bitnr);
  438. }
  439. } else {
  440. nr = (replay_esn->replay_window - 1) >> 5;
  441. for (i = 0; i <= nr; i++)
  442. replay_esn->bmp[i] = 0;
  443. }
  444. bitnr = (pos + diff) % replay_esn->replay_window;
  445. replay_esn->seq = seq;
  446. if (unlikely(wrap > 0))
  447. replay_esn->seq_hi++;
  448. } else {
  449. diff = replay_esn->seq - seq;
  450. if (pos >= diff)
  451. bitnr = (pos - diff) % replay_esn->replay_window;
  452. else
  453. bitnr = replay_esn->replay_window - (diff - pos);
  454. }
  455. nr = bitnr >> 5;
  456. bitnr = bitnr & 0x1F;
  457. replay_esn->bmp[nr] |= (1U << bitnr);
  458. if (xfrm_aevent_is_on(xs_net(x)))
  459. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  460. }
  461. static struct xfrm_replay xfrm_replay_legacy = {
  462. .advance = xfrm_replay_advance,
  463. .check = xfrm_replay_check,
  464. .recheck = xfrm_replay_check,
  465. .notify = xfrm_replay_notify,
  466. .overflow = xfrm_replay_overflow,
  467. };
  468. static struct xfrm_replay xfrm_replay_bmp = {
  469. .advance = xfrm_replay_advance_bmp,
  470. .check = xfrm_replay_check_bmp,
  471. .recheck = xfrm_replay_check_bmp,
  472. .notify = xfrm_replay_notify_bmp,
  473. .overflow = xfrm_replay_overflow_bmp,
  474. };
  475. static struct xfrm_replay xfrm_replay_esn = {
  476. .advance = xfrm_replay_advance_esn,
  477. .check = xfrm_replay_check_esn,
  478. .recheck = xfrm_replay_recheck_esn,
  479. .notify = xfrm_replay_notify_esn,
  480. .overflow = xfrm_replay_overflow_esn,
  481. };
  482. int xfrm_init_replay(struct xfrm_state *x)
  483. {
  484. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  485. if (replay_esn) {
  486. if (replay_esn->replay_window >
  487. replay_esn->bmp_len * sizeof(__u32) * 8)
  488. return -EINVAL;
  489. if (x->props.flags & XFRM_STATE_ESN) {
  490. if (replay_esn->replay_window == 0)
  491. return -EINVAL;
  492. x->repl = &xfrm_replay_esn;
  493. } else
  494. x->repl = &xfrm_replay_bmp;
  495. } else
  496. x->repl = &xfrm_replay_legacy;
  497. return 0;
  498. }
  499. EXPORT_SYMBOL(xfrm_init_replay);