xfrm_replay.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. if (unlikely(x->replay.oseq == 0)) {
  88. x->replay.oseq--;
  89. xfrm_audit_state_replay_overflow(x, skb);
  90. err = -EOVERFLOW;
  91. return err;
  92. }
  93. if (xfrm_aevent_is_on(net))
  94. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  95. }
  96. return err;
  97. }
  98. static int xfrm_replay_check(struct xfrm_state *x,
  99. struct sk_buff *skb, __be32 net_seq)
  100. {
  101. u32 diff;
  102. u32 seq = ntohl(net_seq);
  103. if (!x->props.replay_window)
  104. return 0;
  105. if (unlikely(seq == 0))
  106. goto err;
  107. if (likely(seq > x->replay.seq))
  108. return 0;
  109. diff = x->replay.seq - seq;
  110. if (diff >= min_t(unsigned int, x->props.replay_window,
  111. sizeof(x->replay.bitmap) * 8)) {
  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. if (unlikely(replay_esn->oseq == 0)) {
  152. replay_esn->oseq--;
  153. xfrm_audit_state_replay_overflow(x, skb);
  154. err = -EOVERFLOW;
  155. return err;
  156. }
  157. if (xfrm_aevent_is_on(net))
  158. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  159. }
  160. return err;
  161. }
  162. static int xfrm_replay_check_bmp(struct xfrm_state *x,
  163. struct sk_buff *skb, __be32 net_seq)
  164. {
  165. unsigned int bitnr, nr;
  166. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  167. u32 pos;
  168. u32 seq = ntohl(net_seq);
  169. u32 diff = replay_esn->seq - seq;
  170. if (!replay_esn->replay_window)
  171. return 0;
  172. if (unlikely(seq == 0))
  173. goto err;
  174. if (likely(seq > replay_esn->seq))
  175. return 0;
  176. if (diff >= replay_esn->replay_window) {
  177. x->stats.replay_window++;
  178. goto err;
  179. }
  180. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  181. if (pos >= diff)
  182. bitnr = (pos - diff) % replay_esn->replay_window;
  183. else
  184. bitnr = replay_esn->replay_window - (diff - pos);
  185. nr = bitnr >> 5;
  186. bitnr = bitnr & 0x1F;
  187. if (replay_esn->bmp[nr] & (1U << bitnr))
  188. goto err_replay;
  189. return 0;
  190. err_replay:
  191. x->stats.replay++;
  192. err:
  193. xfrm_audit_state_replay(x, skb, net_seq);
  194. return -EINVAL;
  195. }
  196. static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq)
  197. {
  198. unsigned int bitnr, nr, i;
  199. u32 diff;
  200. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  201. u32 seq = ntohl(net_seq);
  202. u32 pos;
  203. if (!replay_esn->replay_window)
  204. return;
  205. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  206. if (seq > replay_esn->seq) {
  207. diff = seq - replay_esn->seq;
  208. if (diff < replay_esn->replay_window) {
  209. for (i = 1; i < diff; i++) {
  210. bitnr = (pos + i) % replay_esn->replay_window;
  211. nr = bitnr >> 5;
  212. bitnr = bitnr & 0x1F;
  213. replay_esn->bmp[nr] &= ~(1U << bitnr);
  214. }
  215. } else {
  216. nr = (replay_esn->replay_window - 1) >> 5;
  217. for (i = 0; i <= nr; i++)
  218. replay_esn->bmp[i] = 0;
  219. }
  220. bitnr = (pos + diff) % replay_esn->replay_window;
  221. replay_esn->seq = seq;
  222. } else {
  223. diff = replay_esn->seq - seq;
  224. if (pos >= diff)
  225. bitnr = (pos - diff) % replay_esn->replay_window;
  226. else
  227. bitnr = replay_esn->replay_window - (diff - pos);
  228. }
  229. nr = bitnr >> 5;
  230. bitnr = bitnr & 0x1F;
  231. replay_esn->bmp[nr] |= (1U << bitnr);
  232. if (xfrm_aevent_is_on(xs_net(x)))
  233. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  234. }
  235. static void xfrm_replay_notify_bmp(struct xfrm_state *x, int event)
  236. {
  237. struct km_event c;
  238. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  239. struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
  240. /* we send notify messages in case
  241. * 1. we updated on of the sequence numbers, and the seqno difference
  242. * is at least x->replay_maxdiff, in this case we also update the
  243. * timeout of our timer function
  244. * 2. if x->replay_maxage has elapsed since last update,
  245. * and there were changes
  246. *
  247. * The state structure must be locked!
  248. */
  249. switch (event) {
  250. case XFRM_REPLAY_UPDATE:
  251. if (x->replay_maxdiff &&
  252. (replay_esn->seq - preplay_esn->seq < x->replay_maxdiff) &&
  253. (replay_esn->oseq - preplay_esn->oseq < x->replay_maxdiff)) {
  254. if (x->xflags & XFRM_TIME_DEFER)
  255. event = XFRM_REPLAY_TIMEOUT;
  256. else
  257. return;
  258. }
  259. break;
  260. case XFRM_REPLAY_TIMEOUT:
  261. if (memcmp(x->replay_esn, x->preplay_esn,
  262. xfrm_replay_state_esn_len(replay_esn)) == 0) {
  263. x->xflags |= XFRM_TIME_DEFER;
  264. return;
  265. }
  266. break;
  267. }
  268. memcpy(x->preplay_esn, x->replay_esn,
  269. xfrm_replay_state_esn_len(replay_esn));
  270. c.event = XFRM_MSG_NEWAE;
  271. c.data.aevent = event;
  272. km_state_notify(x, &c);
  273. if (x->replay_maxage &&
  274. !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
  275. x->xflags &= ~XFRM_TIME_DEFER;
  276. }
  277. static void xfrm_replay_notify_esn(struct xfrm_state *x, int event)
  278. {
  279. u32 seq_diff, oseq_diff;
  280. struct km_event c;
  281. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  282. struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
  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. switch (event) {
  293. case XFRM_REPLAY_UPDATE:
  294. if (!x->replay_maxdiff)
  295. break;
  296. if (replay_esn->seq_hi == preplay_esn->seq_hi)
  297. seq_diff = replay_esn->seq - preplay_esn->seq;
  298. else
  299. seq_diff = ~preplay_esn->seq + replay_esn->seq + 1;
  300. if (replay_esn->oseq_hi == preplay_esn->oseq_hi)
  301. oseq_diff = replay_esn->oseq - preplay_esn->oseq;
  302. else
  303. oseq_diff = ~preplay_esn->oseq + replay_esn->oseq + 1;
  304. if (seq_diff < x->replay_maxdiff &&
  305. oseq_diff < x->replay_maxdiff) {
  306. if (x->xflags & XFRM_TIME_DEFER)
  307. event = XFRM_REPLAY_TIMEOUT;
  308. else
  309. return;
  310. }
  311. break;
  312. case XFRM_REPLAY_TIMEOUT:
  313. if (memcmp(x->replay_esn, x->preplay_esn,
  314. xfrm_replay_state_esn_len(replay_esn)) == 0) {
  315. x->xflags |= XFRM_TIME_DEFER;
  316. return;
  317. }
  318. break;
  319. }
  320. memcpy(x->preplay_esn, x->replay_esn,
  321. xfrm_replay_state_esn_len(replay_esn));
  322. c.event = XFRM_MSG_NEWAE;
  323. c.data.aevent = event;
  324. km_state_notify(x, &c);
  325. if (x->replay_maxage &&
  326. !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
  327. x->xflags &= ~XFRM_TIME_DEFER;
  328. }
  329. static int xfrm_replay_overflow_esn(struct xfrm_state *x, struct sk_buff *skb)
  330. {
  331. int err = 0;
  332. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  333. struct net *net = xs_net(x);
  334. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  335. XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq;
  336. XFRM_SKB_CB(skb)->seq.output.hi = replay_esn->oseq_hi;
  337. if (unlikely(replay_esn->oseq == 0)) {
  338. XFRM_SKB_CB(skb)->seq.output.hi = ++replay_esn->oseq_hi;
  339. if (replay_esn->oseq_hi == 0) {
  340. replay_esn->oseq--;
  341. replay_esn->oseq_hi--;
  342. xfrm_audit_state_replay_overflow(x, skb);
  343. err = -EOVERFLOW;
  344. return err;
  345. }
  346. }
  347. if (xfrm_aevent_is_on(net))
  348. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  349. }
  350. return err;
  351. }
  352. static int xfrm_replay_check_esn(struct xfrm_state *x,
  353. struct sk_buff *skb, __be32 net_seq)
  354. {
  355. unsigned int bitnr, nr;
  356. u32 diff;
  357. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  358. u32 pos;
  359. u32 seq = ntohl(net_seq);
  360. u32 wsize = replay_esn->replay_window;
  361. u32 top = replay_esn->seq;
  362. u32 bottom = top - wsize + 1;
  363. if (!wsize)
  364. return 0;
  365. if (unlikely(seq == 0 && replay_esn->seq_hi == 0 &&
  366. (replay_esn->seq < replay_esn->replay_window - 1)))
  367. goto err;
  368. diff = top - seq;
  369. if (likely(top >= wsize - 1)) {
  370. /* A. same subspace */
  371. if (likely(seq > top) || seq < bottom)
  372. return 0;
  373. } else {
  374. /* B. window spans two subspaces */
  375. if (likely(seq > top && seq < bottom))
  376. return 0;
  377. if (seq >= bottom)
  378. diff = ~seq + top + 1;
  379. }
  380. if (diff >= replay_esn->replay_window) {
  381. x->stats.replay_window++;
  382. goto err;
  383. }
  384. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  385. if (pos >= diff)
  386. bitnr = (pos - diff) % replay_esn->replay_window;
  387. else
  388. bitnr = replay_esn->replay_window - (diff - pos);
  389. nr = bitnr >> 5;
  390. bitnr = bitnr & 0x1F;
  391. if (replay_esn->bmp[nr] & (1U << bitnr))
  392. goto err_replay;
  393. return 0;
  394. err_replay:
  395. x->stats.replay++;
  396. err:
  397. xfrm_audit_state_replay(x, skb, net_seq);
  398. return -EINVAL;
  399. }
  400. static int xfrm_replay_recheck_esn(struct xfrm_state *x,
  401. struct sk_buff *skb, __be32 net_seq)
  402. {
  403. if (unlikely(XFRM_SKB_CB(skb)->seq.input.hi !=
  404. htonl(xfrm_replay_seqhi(x, net_seq)))) {
  405. x->stats.replay_window++;
  406. return -EINVAL;
  407. }
  408. return xfrm_replay_check_esn(x, skb, net_seq);
  409. }
  410. static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq)
  411. {
  412. unsigned int bitnr, nr, i;
  413. int wrap;
  414. u32 diff, pos, seq, seq_hi;
  415. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  416. if (!replay_esn->replay_window)
  417. return;
  418. seq = ntohl(net_seq);
  419. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  420. seq_hi = xfrm_replay_seqhi(x, net_seq);
  421. wrap = seq_hi - replay_esn->seq_hi;
  422. if ((!wrap && seq > replay_esn->seq) || wrap > 0) {
  423. if (likely(!wrap))
  424. diff = seq - replay_esn->seq;
  425. else
  426. diff = ~replay_esn->seq + seq + 1;
  427. if (diff < replay_esn->replay_window) {
  428. for (i = 1; i < diff; i++) {
  429. bitnr = (pos + i) % replay_esn->replay_window;
  430. nr = bitnr >> 5;
  431. bitnr = bitnr & 0x1F;
  432. replay_esn->bmp[nr] &= ~(1U << bitnr);
  433. }
  434. } else {
  435. nr = (replay_esn->replay_window - 1) >> 5;
  436. for (i = 0; i <= nr; i++)
  437. replay_esn->bmp[i] = 0;
  438. }
  439. bitnr = (pos + diff) % replay_esn->replay_window;
  440. replay_esn->seq = seq;
  441. if (unlikely(wrap > 0))
  442. replay_esn->seq_hi++;
  443. } else {
  444. diff = replay_esn->seq - seq;
  445. if (pos >= diff)
  446. bitnr = (pos - diff) % replay_esn->replay_window;
  447. else
  448. bitnr = replay_esn->replay_window - (diff - pos);
  449. }
  450. nr = bitnr >> 5;
  451. bitnr = bitnr & 0x1F;
  452. replay_esn->bmp[nr] |= (1U << bitnr);
  453. if (xfrm_aevent_is_on(xs_net(x)))
  454. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  455. }
  456. static struct xfrm_replay xfrm_replay_legacy = {
  457. .advance = xfrm_replay_advance,
  458. .check = xfrm_replay_check,
  459. .recheck = xfrm_replay_check,
  460. .notify = xfrm_replay_notify,
  461. .overflow = xfrm_replay_overflow,
  462. };
  463. static struct xfrm_replay xfrm_replay_bmp = {
  464. .advance = xfrm_replay_advance_bmp,
  465. .check = xfrm_replay_check_bmp,
  466. .recheck = xfrm_replay_check_bmp,
  467. .notify = xfrm_replay_notify_bmp,
  468. .overflow = xfrm_replay_overflow_bmp,
  469. };
  470. static struct xfrm_replay xfrm_replay_esn = {
  471. .advance = xfrm_replay_advance_esn,
  472. .check = xfrm_replay_check_esn,
  473. .recheck = xfrm_replay_recheck_esn,
  474. .notify = xfrm_replay_notify_esn,
  475. .overflow = xfrm_replay_overflow_esn,
  476. };
  477. int xfrm_init_replay(struct xfrm_state *x)
  478. {
  479. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  480. if (replay_esn) {
  481. if (replay_esn->replay_window >
  482. replay_esn->bmp_len * sizeof(__u32) * 8)
  483. return -EINVAL;
  484. if (x->props.flags & XFRM_STATE_ESN) {
  485. if (replay_esn->replay_window == 0)
  486. return -EINVAL;
  487. x->repl = &xfrm_replay_esn;
  488. } else
  489. x->repl = &xfrm_replay_bmp;
  490. } else
  491. x->repl = &xfrm_replay_legacy;
  492. return 0;
  493. }
  494. EXPORT_SYMBOL(xfrm_init_replay);