xfrm_replay.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 <net/xfrm.h>
  21. u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq)
  22. {
  23. u32 seq, seq_hi, bottom;
  24. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  25. if (!(x->props.flags & XFRM_STATE_ESN))
  26. return 0;
  27. seq = ntohl(net_seq);
  28. seq_hi = replay_esn->seq_hi;
  29. bottom = replay_esn->seq - replay_esn->replay_window + 1;
  30. if (likely(replay_esn->seq >= replay_esn->replay_window - 1)) {
  31. /* A. same subspace */
  32. if (unlikely(seq < bottom))
  33. seq_hi++;
  34. } else {
  35. /* B. window spans two subspaces */
  36. if (unlikely(seq >= bottom))
  37. seq_hi--;
  38. }
  39. return seq_hi;
  40. }
  41. static void xfrm_replay_notify(struct xfrm_state *x, int event)
  42. {
  43. struct km_event c;
  44. /* we send notify messages in case
  45. * 1. we updated on of the sequence numbers, and the seqno difference
  46. * is at least x->replay_maxdiff, in this case we also update the
  47. * timeout of our timer function
  48. * 2. if x->replay_maxage has elapsed since last update,
  49. * and there were changes
  50. *
  51. * The state structure must be locked!
  52. */
  53. switch (event) {
  54. case XFRM_REPLAY_UPDATE:
  55. if (x->replay_maxdiff &&
  56. (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
  57. (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
  58. if (x->xflags & XFRM_TIME_DEFER)
  59. event = XFRM_REPLAY_TIMEOUT;
  60. else
  61. return;
  62. }
  63. break;
  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. break;
  71. }
  72. memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
  73. c.event = XFRM_MSG_NEWAE;
  74. c.data.aevent = event;
  75. km_state_notify(x, &c);
  76. if (x->replay_maxage &&
  77. !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
  78. x->xflags &= ~XFRM_TIME_DEFER;
  79. }
  80. static int xfrm_replay_overflow(struct xfrm_state *x, struct sk_buff *skb)
  81. {
  82. int err = 0;
  83. struct net *net = xs_net(x);
  84. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  85. XFRM_SKB_CB(skb)->seq.output.low = ++x->replay.oseq;
  86. if (unlikely(x->replay.oseq == 0)) {
  87. x->replay.oseq--;
  88. xfrm_audit_state_replay_overflow(x, skb);
  89. err = -EOVERFLOW;
  90. return err;
  91. }
  92. if (xfrm_aevent_is_on(net))
  93. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  94. }
  95. return err;
  96. }
  97. static int xfrm_replay_check(struct xfrm_state *x,
  98. struct sk_buff *skb, __be32 net_seq)
  99. {
  100. u32 diff;
  101. u32 seq = ntohl(net_seq);
  102. if (!x->props.replay_window)
  103. return 0;
  104. if (unlikely(seq == 0))
  105. goto err;
  106. if (likely(seq > x->replay.seq))
  107. return 0;
  108. diff = x->replay.seq - seq;
  109. if (diff >= min_t(unsigned int, x->props.replay_window,
  110. sizeof(x->replay.bitmap) * 8)) {
  111. x->stats.replay_window++;
  112. goto err;
  113. }
  114. if (x->replay.bitmap & (1U << diff)) {
  115. x->stats.replay++;
  116. goto err;
  117. }
  118. return 0;
  119. err:
  120. xfrm_audit_state_replay(x, skb, net_seq);
  121. return -EINVAL;
  122. }
  123. static void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
  124. {
  125. u32 diff;
  126. u32 seq = ntohl(net_seq);
  127. if (!x->props.replay_window)
  128. return;
  129. if (seq > x->replay.seq) {
  130. diff = seq - x->replay.seq;
  131. if (diff < x->props.replay_window)
  132. x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
  133. else
  134. x->replay.bitmap = 1;
  135. x->replay.seq = seq;
  136. } else {
  137. diff = x->replay.seq - seq;
  138. x->replay.bitmap |= (1U << diff);
  139. }
  140. if (xfrm_aevent_is_on(xs_net(x)))
  141. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  142. }
  143. static int xfrm_replay_overflow_bmp(struct xfrm_state *x, struct sk_buff *skb)
  144. {
  145. int err = 0;
  146. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  147. struct net *net = xs_net(x);
  148. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  149. XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq;
  150. if (unlikely(replay_esn->oseq == 0)) {
  151. replay_esn->oseq--;
  152. xfrm_audit_state_replay_overflow(x, skb);
  153. err = -EOVERFLOW;
  154. return err;
  155. }
  156. if (xfrm_aevent_is_on(net))
  157. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  158. }
  159. return err;
  160. }
  161. static int xfrm_replay_check_bmp(struct xfrm_state *x,
  162. struct sk_buff *skb, __be32 net_seq)
  163. {
  164. unsigned int bitnr, nr;
  165. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  166. u32 pos;
  167. u32 seq = ntohl(net_seq);
  168. u32 diff = replay_esn->seq - seq;
  169. if (!replay_esn->replay_window)
  170. return 0;
  171. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  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. if (pos >= diff) {
  181. bitnr = (pos - diff) % replay_esn->replay_window;
  182. nr = bitnr >> 5;
  183. bitnr = bitnr & 0x1F;
  184. if (replay_esn->bmp[nr] & (1U << bitnr))
  185. goto err_replay;
  186. } else {
  187. bitnr = replay_esn->replay_window - (diff - pos);
  188. nr = bitnr >> 5;
  189. bitnr = bitnr & 0x1F;
  190. if (replay_esn->bmp[nr] & (1U << bitnr))
  191. goto err_replay;
  192. }
  193. return 0;
  194. err_replay:
  195. x->stats.replay++;
  196. err:
  197. xfrm_audit_state_replay(x, skb, net_seq);
  198. return -EINVAL;
  199. }
  200. static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq)
  201. {
  202. unsigned int bitnr, nr, i;
  203. u32 diff;
  204. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  205. u32 seq = ntohl(net_seq);
  206. u32 pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  207. if (!replay_esn->replay_window)
  208. return;
  209. if (seq > replay_esn->seq) {
  210. diff = seq - replay_esn->seq;
  211. if (diff < replay_esn->replay_window) {
  212. for (i = 1; i < diff; i++) {
  213. bitnr = (pos + i) % replay_esn->replay_window;
  214. nr = bitnr >> 5;
  215. bitnr = bitnr & 0x1F;
  216. replay_esn->bmp[nr] &= ~(1U << bitnr);
  217. }
  218. bitnr = (pos + diff) % replay_esn->replay_window;
  219. nr = bitnr >> 5;
  220. bitnr = bitnr & 0x1F;
  221. replay_esn->bmp[nr] |= (1U << bitnr);
  222. } else {
  223. nr = (replay_esn->replay_window - 1) >> 5;
  224. for (i = 0; i <= nr; i++)
  225. replay_esn->bmp[i] = 0;
  226. bitnr = (pos + diff) % replay_esn->replay_window;
  227. nr = bitnr >> 5;
  228. bitnr = bitnr & 0x1F;
  229. replay_esn->bmp[nr] |= (1U << bitnr);
  230. }
  231. replay_esn->seq = seq;
  232. } else {
  233. diff = replay_esn->seq - seq;
  234. if (pos >= diff) {
  235. bitnr = (pos - diff) % replay_esn->replay_window;
  236. nr = bitnr >> 5;
  237. bitnr = bitnr & 0x1F;
  238. replay_esn->bmp[nr] |= (1U << bitnr);
  239. } else {
  240. bitnr = replay_esn->replay_window - (diff - pos);
  241. nr = bitnr >> 5;
  242. bitnr = bitnr & 0x1F;
  243. replay_esn->bmp[nr] |= (1U << bitnr);
  244. }
  245. }
  246. if (xfrm_aevent_is_on(xs_net(x)))
  247. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  248. }
  249. static void xfrm_replay_notify_bmp(struct xfrm_state *x, int event)
  250. {
  251. struct km_event c;
  252. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  253. struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
  254. /* we send notify messages in case
  255. * 1. we updated on of the sequence numbers, and the seqno difference
  256. * is at least x->replay_maxdiff, in this case we also update the
  257. * timeout of our timer function
  258. * 2. if x->replay_maxage has elapsed since last update,
  259. * and there were changes
  260. *
  261. * The state structure must be locked!
  262. */
  263. switch (event) {
  264. case XFRM_REPLAY_UPDATE:
  265. if (x->replay_maxdiff &&
  266. (replay_esn->seq - preplay_esn->seq < x->replay_maxdiff) &&
  267. (replay_esn->oseq - preplay_esn->oseq < x->replay_maxdiff)) {
  268. if (x->xflags & XFRM_TIME_DEFER)
  269. event = XFRM_REPLAY_TIMEOUT;
  270. else
  271. return;
  272. }
  273. break;
  274. case XFRM_REPLAY_TIMEOUT:
  275. if (memcmp(x->replay_esn, x->preplay_esn,
  276. xfrm_replay_state_esn_len(replay_esn)) == 0) {
  277. x->xflags |= XFRM_TIME_DEFER;
  278. return;
  279. }
  280. break;
  281. }
  282. memcpy(x->preplay_esn, x->replay_esn,
  283. xfrm_replay_state_esn_len(replay_esn));
  284. c.event = XFRM_MSG_NEWAE;
  285. c.data.aevent = event;
  286. km_state_notify(x, &c);
  287. if (x->replay_maxage &&
  288. !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
  289. x->xflags &= ~XFRM_TIME_DEFER;
  290. }
  291. static int xfrm_replay_overflow_esn(struct xfrm_state *x, struct sk_buff *skb)
  292. {
  293. int err = 0;
  294. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  295. struct net *net = xs_net(x);
  296. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  297. XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq;
  298. XFRM_SKB_CB(skb)->seq.output.hi = replay_esn->oseq_hi;
  299. if (unlikely(replay_esn->oseq == 0)) {
  300. XFRM_SKB_CB(skb)->seq.output.hi = ++replay_esn->oseq_hi;
  301. if (replay_esn->oseq_hi == 0) {
  302. replay_esn->oseq--;
  303. replay_esn->oseq_hi--;
  304. xfrm_audit_state_replay_overflow(x, skb);
  305. err = -EOVERFLOW;
  306. return err;
  307. }
  308. }
  309. if (xfrm_aevent_is_on(net))
  310. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  311. }
  312. return err;
  313. }
  314. static int xfrm_replay_check_esn(struct xfrm_state *x,
  315. struct sk_buff *skb, __be32 net_seq)
  316. {
  317. unsigned int bitnr, nr;
  318. u32 diff;
  319. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  320. u32 pos;
  321. u32 seq = ntohl(net_seq);
  322. u32 wsize = replay_esn->replay_window;
  323. u32 top = replay_esn->seq;
  324. u32 bottom = top - wsize + 1;
  325. if (!wsize)
  326. return 0;
  327. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  328. if (unlikely(seq == 0 && replay_esn->seq_hi == 0 &&
  329. (replay_esn->seq < replay_esn->replay_window - 1)))
  330. goto err;
  331. diff = top - seq;
  332. if (likely(top >= wsize - 1)) {
  333. /* A. same subspace */
  334. if (likely(seq > top) || seq < bottom)
  335. return 0;
  336. } else {
  337. /* B. window spans two subspaces */
  338. if (likely(seq > top && seq < bottom))
  339. return 0;
  340. if (seq >= bottom)
  341. diff = ~seq + top + 1;
  342. }
  343. if (diff >= replay_esn->replay_window) {
  344. x->stats.replay_window++;
  345. goto err;
  346. }
  347. if (pos >= diff) {
  348. bitnr = (pos - diff) % replay_esn->replay_window;
  349. nr = bitnr >> 5;
  350. bitnr = bitnr & 0x1F;
  351. if (replay_esn->bmp[nr] & (1U << bitnr))
  352. goto err_replay;
  353. } else {
  354. bitnr = replay_esn->replay_window - (diff - pos);
  355. nr = bitnr >> 5;
  356. bitnr = bitnr & 0x1F;
  357. if (replay_esn->bmp[nr] & (1U << bitnr))
  358. goto err_replay;
  359. }
  360. return 0;
  361. err_replay:
  362. x->stats.replay++;
  363. err:
  364. xfrm_audit_state_replay(x, skb, net_seq);
  365. return -EINVAL;
  366. }
  367. static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq)
  368. {
  369. unsigned int bitnr, nr, i;
  370. int wrap;
  371. u32 diff, pos, seq, seq_hi;
  372. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  373. if (!replay_esn->replay_window)
  374. return;
  375. seq = ntohl(net_seq);
  376. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  377. seq_hi = xfrm_replay_seqhi(x, net_seq);
  378. wrap = seq_hi - replay_esn->seq_hi;
  379. if ((!wrap && seq > replay_esn->seq) || wrap > 0) {
  380. if (likely(!wrap))
  381. diff = seq - replay_esn->seq;
  382. else
  383. diff = ~replay_esn->seq + seq + 1;
  384. if (diff < replay_esn->replay_window) {
  385. for (i = 1; i < diff; i++) {
  386. bitnr = (pos + i) % replay_esn->replay_window;
  387. nr = bitnr >> 5;
  388. bitnr = bitnr & 0x1F;
  389. replay_esn->bmp[nr] &= ~(1U << bitnr);
  390. }
  391. bitnr = (pos + diff) % replay_esn->replay_window;
  392. nr = bitnr >> 5;
  393. bitnr = bitnr & 0x1F;
  394. replay_esn->bmp[nr] |= (1U << bitnr);
  395. } else {
  396. nr = (replay_esn->replay_window - 1) >> 5;
  397. for (i = 0; i <= nr; i++)
  398. replay_esn->bmp[i] = 0;
  399. bitnr = (pos + diff) % replay_esn->replay_window;
  400. nr = bitnr >> 5;
  401. bitnr = bitnr & 0x1F;
  402. replay_esn->bmp[nr] |= (1U << bitnr);
  403. }
  404. replay_esn->seq = seq;
  405. if (unlikely(wrap > 0))
  406. replay_esn->seq_hi++;
  407. } else {
  408. diff = replay_esn->seq - seq;
  409. if (pos >= diff) {
  410. bitnr = (pos - diff) % replay_esn->replay_window;
  411. nr = bitnr >> 5;
  412. bitnr = bitnr & 0x1F;
  413. replay_esn->bmp[nr] |= (1U << bitnr);
  414. } else {
  415. bitnr = replay_esn->replay_window - (diff - pos);
  416. nr = bitnr >> 5;
  417. bitnr = bitnr & 0x1F;
  418. replay_esn->bmp[nr] |= (1U << bitnr);
  419. }
  420. }
  421. if (xfrm_aevent_is_on(xs_net(x)))
  422. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  423. }
  424. static struct xfrm_replay xfrm_replay_legacy = {
  425. .advance = xfrm_replay_advance,
  426. .check = xfrm_replay_check,
  427. .notify = xfrm_replay_notify,
  428. .overflow = xfrm_replay_overflow,
  429. };
  430. static struct xfrm_replay xfrm_replay_bmp = {
  431. .advance = xfrm_replay_advance_bmp,
  432. .check = xfrm_replay_check_bmp,
  433. .notify = xfrm_replay_notify_bmp,
  434. .overflow = xfrm_replay_overflow_bmp,
  435. };
  436. static struct xfrm_replay xfrm_replay_esn = {
  437. .advance = xfrm_replay_advance_esn,
  438. .check = xfrm_replay_check_esn,
  439. .notify = xfrm_replay_notify_bmp,
  440. .overflow = xfrm_replay_overflow_esn,
  441. };
  442. int xfrm_init_replay(struct xfrm_state *x)
  443. {
  444. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  445. if (replay_esn) {
  446. if (replay_esn->replay_window >
  447. replay_esn->bmp_len * sizeof(__u32) * 8)
  448. return -EINVAL;
  449. if ((x->props.flags & XFRM_STATE_ESN) && replay_esn->replay_window == 0)
  450. return -EINVAL;
  451. if ((x->props.flags & XFRM_STATE_ESN) && x->replay_esn)
  452. x->repl = &xfrm_replay_esn;
  453. else
  454. x->repl = &xfrm_replay_bmp;
  455. } else
  456. x->repl = &xfrm_replay_legacy;
  457. return 0;
  458. }
  459. EXPORT_SYMBOL(xfrm_init_replay);