xfrm_replay.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. EXPORT_SYMBOL(xfrm_replay_seqhi);
  43. ;
  44. static void xfrm_replay_notify(struct xfrm_state *x, int event)
  45. {
  46. struct km_event c;
  47. /* we send notify messages in case
  48. * 1. we updated on of the sequence numbers, and the seqno difference
  49. * is at least x->replay_maxdiff, in this case we also update the
  50. * timeout of our timer function
  51. * 2. if x->replay_maxage has elapsed since last update,
  52. * and there were changes
  53. *
  54. * The state structure must be locked!
  55. */
  56. switch (event) {
  57. case XFRM_REPLAY_UPDATE:
  58. if (!x->replay_maxdiff ||
  59. ((x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
  60. (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff))) {
  61. if (x->xflags & XFRM_TIME_DEFER)
  62. event = XFRM_REPLAY_TIMEOUT;
  63. else
  64. return;
  65. }
  66. break;
  67. case XFRM_REPLAY_TIMEOUT:
  68. if (memcmp(&x->replay, &x->preplay,
  69. sizeof(struct xfrm_replay_state)) == 0) {
  70. x->xflags |= XFRM_TIME_DEFER;
  71. return;
  72. }
  73. break;
  74. }
  75. memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
  76. c.event = XFRM_MSG_NEWAE;
  77. c.data.aevent = event;
  78. km_state_notify(x, &c);
  79. if (x->replay_maxage &&
  80. !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
  81. x->xflags &= ~XFRM_TIME_DEFER;
  82. }
  83. static int xfrm_replay_overflow(struct xfrm_state *x, struct sk_buff *skb)
  84. {
  85. int err = 0;
  86. struct net *net = xs_net(x);
  87. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  88. XFRM_SKB_CB(skb)->seq.output.low = ++x->replay.oseq;
  89. XFRM_SKB_CB(skb)->seq.output.hi = 0;
  90. if (unlikely(x->replay.oseq == 0)) {
  91. x->replay.oseq--;
  92. xfrm_audit_state_replay_overflow(x, skb);
  93. err = -EOVERFLOW;
  94. return err;
  95. }
  96. if (xfrm_aevent_is_on(net))
  97. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  98. }
  99. return err;
  100. }
  101. static int xfrm_replay_check(struct xfrm_state *x,
  102. struct sk_buff *skb, __be32 net_seq)
  103. {
  104. u32 diff;
  105. u32 seq = ntohl(net_seq);
  106. if (!x->props.replay_window)
  107. return 0;
  108. if (unlikely(seq == 0))
  109. goto err;
  110. if (likely(seq > x->replay.seq))
  111. return 0;
  112. diff = x->replay.seq - seq;
  113. if (diff >= x->props.replay_window) {
  114. x->stats.replay_window++;
  115. goto err;
  116. }
  117. if (x->replay.bitmap & (1U << diff)) {
  118. x->stats.replay++;
  119. goto err;
  120. }
  121. return 0;
  122. err:
  123. xfrm_audit_state_replay(x, skb, net_seq);
  124. return -EINVAL;
  125. }
  126. static void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
  127. {
  128. u32 diff;
  129. u32 seq = ntohl(net_seq);
  130. if (!x->props.replay_window)
  131. return;
  132. if (seq > x->replay.seq) {
  133. diff = seq - x->replay.seq;
  134. if (diff < x->props.replay_window)
  135. x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
  136. else
  137. x->replay.bitmap = 1;
  138. x->replay.seq = seq;
  139. } else {
  140. diff = x->replay.seq - seq;
  141. x->replay.bitmap |= (1U << diff);
  142. }
  143. if (xfrm_aevent_is_on(xs_net(x)))
  144. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  145. }
  146. static int xfrm_replay_overflow_bmp(struct xfrm_state *x, struct sk_buff *skb)
  147. {
  148. int err = 0;
  149. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  150. struct net *net = xs_net(x);
  151. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  152. XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq;
  153. XFRM_SKB_CB(skb)->seq.output.hi = 0;
  154. if (unlikely(replay_esn->oseq == 0)) {
  155. replay_esn->oseq--;
  156. xfrm_audit_state_replay_overflow(x, skb);
  157. err = -EOVERFLOW;
  158. return err;
  159. }
  160. if (xfrm_aevent_is_on(net))
  161. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  162. }
  163. return err;
  164. }
  165. static int xfrm_replay_check_bmp(struct xfrm_state *x,
  166. struct sk_buff *skb, __be32 net_seq)
  167. {
  168. unsigned int bitnr, nr;
  169. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  170. u32 pos;
  171. u32 seq = ntohl(net_seq);
  172. u32 diff = replay_esn->seq - seq;
  173. if (!replay_esn->replay_window)
  174. return 0;
  175. if (unlikely(seq == 0))
  176. goto err;
  177. if (likely(seq > replay_esn->seq))
  178. return 0;
  179. if (diff >= replay_esn->replay_window) {
  180. x->stats.replay_window++;
  181. goto err;
  182. }
  183. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  184. if (pos >= diff)
  185. bitnr = (pos - diff) % replay_esn->replay_window;
  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. return 0;
  193. err_replay:
  194. x->stats.replay++;
  195. err:
  196. xfrm_audit_state_replay(x, skb, net_seq);
  197. return -EINVAL;
  198. }
  199. static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq)
  200. {
  201. unsigned int bitnr, nr, i;
  202. u32 diff;
  203. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  204. u32 seq = ntohl(net_seq);
  205. u32 pos;
  206. if (!replay_esn->replay_window)
  207. return;
  208. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  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. } else {
  219. nr = (replay_esn->replay_window - 1) >> 5;
  220. for (i = 0; i <= nr; i++)
  221. replay_esn->bmp[i] = 0;
  222. }
  223. bitnr = (pos + diff) % replay_esn->replay_window;
  224. replay_esn->seq = seq;
  225. } else {
  226. diff = replay_esn->seq - seq;
  227. if (pos >= diff)
  228. bitnr = (pos - diff) % replay_esn->replay_window;
  229. else
  230. bitnr = replay_esn->replay_window - (diff - pos);
  231. }
  232. nr = bitnr >> 5;
  233. bitnr = bitnr & 0x1F;
  234. replay_esn->bmp[nr] |= (1U << bitnr);
  235. if (xfrm_aevent_is_on(xs_net(x)))
  236. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  237. }
  238. static void xfrm_replay_notify_bmp(struct xfrm_state *x, int event)
  239. {
  240. struct km_event c;
  241. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  242. struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
  243. /* we send notify messages in case
  244. * 1. we updated on of the sequence numbers, and the seqno difference
  245. * is at least x->replay_maxdiff, in this case we also update the
  246. * timeout of our timer function
  247. * 2. if x->replay_maxage has elapsed since last update,
  248. * and there were changes
  249. *
  250. * The state structure must be locked!
  251. */
  252. switch (event) {
  253. case XFRM_REPLAY_UPDATE:
  254. if (!x->replay_maxdiff ||
  255. ((replay_esn->seq - preplay_esn->seq < x->replay_maxdiff) &&
  256. (replay_esn->oseq - preplay_esn->oseq
  257. < x->replay_maxdiff))) {
  258. if (x->xflags & XFRM_TIME_DEFER)
  259. event = XFRM_REPLAY_TIMEOUT;
  260. else
  261. return;
  262. }
  263. break;
  264. case XFRM_REPLAY_TIMEOUT:
  265. if (memcmp(x->replay_esn, x->preplay_esn,
  266. xfrm_replay_state_esn_len(replay_esn)) == 0) {
  267. x->xflags |= XFRM_TIME_DEFER;
  268. return;
  269. }
  270. break;
  271. }
  272. memcpy(x->preplay_esn, x->replay_esn,
  273. xfrm_replay_state_esn_len(replay_esn));
  274. c.event = XFRM_MSG_NEWAE;
  275. c.data.aevent = event;
  276. km_state_notify(x, &c);
  277. if (x->replay_maxage &&
  278. !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
  279. x->xflags &= ~XFRM_TIME_DEFER;
  280. }
  281. static void xfrm_replay_notify_esn(struct xfrm_state *x, int event)
  282. {
  283. u32 seq_diff, oseq_diff;
  284. struct km_event c;
  285. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  286. struct xfrm_replay_state_esn *preplay_esn = x->preplay_esn;
  287. /* we send notify messages in case
  288. * 1. we updated on of the sequence numbers, and the seqno difference
  289. * is at least x->replay_maxdiff, in this case we also update the
  290. * timeout of our timer function
  291. * 2. if x->replay_maxage has elapsed since last update,
  292. * and there were changes
  293. *
  294. * The state structure must be locked!
  295. */
  296. switch (event) {
  297. case XFRM_REPLAY_UPDATE:
  298. if (x->replay_maxdiff) {
  299. if (replay_esn->seq_hi == preplay_esn->seq_hi)
  300. seq_diff = replay_esn->seq - preplay_esn->seq;
  301. else
  302. seq_diff = ~preplay_esn->seq + replay_esn->seq
  303. + 1;
  304. if (replay_esn->oseq_hi == preplay_esn->oseq_hi)
  305. oseq_diff = replay_esn->oseq
  306. - preplay_esn->oseq;
  307. else
  308. oseq_diff = ~preplay_esn->oseq
  309. + replay_esn->oseq + 1;
  310. if (seq_diff >= x->replay_maxdiff ||
  311. oseq_diff >= x->replay_maxdiff)
  312. break;
  313. }
  314. if (x->xflags & XFRM_TIME_DEFER)
  315. event = XFRM_REPLAY_TIMEOUT;
  316. else
  317. return;
  318. break;
  319. case XFRM_REPLAY_TIMEOUT:
  320. if (memcmp(x->replay_esn, x->preplay_esn,
  321. xfrm_replay_state_esn_len(replay_esn)) == 0) {
  322. x->xflags |= XFRM_TIME_DEFER;
  323. return;
  324. }
  325. break;
  326. }
  327. memcpy(x->preplay_esn, x->replay_esn,
  328. xfrm_replay_state_esn_len(replay_esn));
  329. c.event = XFRM_MSG_NEWAE;
  330. c.data.aevent = event;
  331. km_state_notify(x, &c);
  332. if (x->replay_maxage &&
  333. !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
  334. x->xflags &= ~XFRM_TIME_DEFER;
  335. }
  336. static int xfrm_replay_overflow_esn(struct xfrm_state *x, struct sk_buff *skb)
  337. {
  338. int err = 0;
  339. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  340. struct net *net = xs_net(x);
  341. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  342. XFRM_SKB_CB(skb)->seq.output.low = ++replay_esn->oseq;
  343. XFRM_SKB_CB(skb)->seq.output.hi = replay_esn->oseq_hi;
  344. if (unlikely(replay_esn->oseq == 0)) {
  345. XFRM_SKB_CB(skb)->seq.output.hi = ++replay_esn->oseq_hi;
  346. if (replay_esn->oseq_hi == 0) {
  347. replay_esn->oseq--;
  348. replay_esn->oseq_hi--;
  349. xfrm_audit_state_replay_overflow(x, skb);
  350. err = -EOVERFLOW;
  351. return err;
  352. }
  353. }
  354. if (xfrm_aevent_is_on(net))
  355. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  356. }
  357. return err;
  358. }
  359. static int xfrm_replay_check_esn(struct xfrm_state *x,
  360. struct sk_buff *skb, __be32 net_seq)
  361. {
  362. unsigned int bitnr, nr;
  363. u32 diff;
  364. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  365. u32 pos;
  366. u32 seq = ntohl(net_seq);
  367. u32 wsize = replay_esn->replay_window;
  368. u32 top = replay_esn->seq;
  369. u32 bottom = top - wsize + 1;
  370. if (!wsize)
  371. return 0;
  372. if (unlikely(seq == 0 && replay_esn->seq_hi == 0 &&
  373. (replay_esn->seq < replay_esn->replay_window - 1)))
  374. goto err;
  375. diff = top - seq;
  376. if (likely(top >= wsize - 1)) {
  377. /* A. same subspace */
  378. if (likely(seq > top) || seq < bottom)
  379. return 0;
  380. } else {
  381. /* B. window spans two subspaces */
  382. if (likely(seq > top && seq < bottom))
  383. return 0;
  384. if (seq >= bottom)
  385. diff = ~seq + top + 1;
  386. }
  387. if (diff >= replay_esn->replay_window) {
  388. x->stats.replay_window++;
  389. goto err;
  390. }
  391. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  392. if (pos >= diff)
  393. bitnr = (pos - diff) % replay_esn->replay_window;
  394. else
  395. bitnr = replay_esn->replay_window - (diff - pos);
  396. nr = bitnr >> 5;
  397. bitnr = bitnr & 0x1F;
  398. if (replay_esn->bmp[nr] & (1U << bitnr))
  399. goto err_replay;
  400. return 0;
  401. err_replay:
  402. x->stats.replay++;
  403. err:
  404. xfrm_audit_state_replay(x, skb, net_seq);
  405. return -EINVAL;
  406. }
  407. static int xfrm_replay_recheck_esn(struct xfrm_state *x,
  408. struct sk_buff *skb, __be32 net_seq)
  409. {
  410. if (unlikely(XFRM_SKB_CB(skb)->seq.input.hi !=
  411. htonl(xfrm_replay_seqhi(x, net_seq)))) {
  412. x->stats.replay_window++;
  413. return -EINVAL;
  414. }
  415. return xfrm_replay_check_esn(x, skb, net_seq);
  416. }
  417. static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq)
  418. {
  419. unsigned int bitnr, nr, i;
  420. int wrap;
  421. u32 diff, pos, seq, seq_hi;
  422. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  423. if (!replay_esn->replay_window)
  424. return;
  425. seq = ntohl(net_seq);
  426. pos = (replay_esn->seq - 1) % replay_esn->replay_window;
  427. seq_hi = xfrm_replay_seqhi(x, net_seq);
  428. wrap = seq_hi - replay_esn->seq_hi;
  429. if ((!wrap && seq > replay_esn->seq) || wrap > 0) {
  430. if (likely(!wrap))
  431. diff = seq - replay_esn->seq;
  432. else
  433. diff = ~replay_esn->seq + seq + 1;
  434. if (diff < replay_esn->replay_window) {
  435. for (i = 1; i < diff; i++) {
  436. bitnr = (pos + i) % replay_esn->replay_window;
  437. nr = bitnr >> 5;
  438. bitnr = bitnr & 0x1F;
  439. replay_esn->bmp[nr] &= ~(1U << bitnr);
  440. }
  441. } else {
  442. nr = (replay_esn->replay_window - 1) >> 5;
  443. for (i = 0; i <= nr; i++)
  444. replay_esn->bmp[i] = 0;
  445. }
  446. bitnr = (pos + diff) % replay_esn->replay_window;
  447. replay_esn->seq = seq;
  448. if (unlikely(wrap > 0))
  449. replay_esn->seq_hi++;
  450. } else {
  451. diff = replay_esn->seq - seq;
  452. if (pos >= diff)
  453. bitnr = (pos - diff) % replay_esn->replay_window;
  454. else
  455. bitnr = replay_esn->replay_window - (diff - pos);
  456. }
  457. xfrm_dev_state_advance_esn(x);
  458. nr = bitnr >> 5;
  459. bitnr = bitnr & 0x1F;
  460. replay_esn->bmp[nr] |= (1U << bitnr);
  461. if (xfrm_aevent_is_on(xs_net(x)))
  462. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  463. }
  464. #ifdef CONFIG_XFRM_OFFLOAD
  465. static int xfrm_replay_overflow_offload(struct xfrm_state *x, struct sk_buff *skb)
  466. {
  467. int err = 0;
  468. struct net *net = xs_net(x);
  469. struct xfrm_offload *xo = xfrm_offload(skb);
  470. __u32 oseq = x->replay.oseq;
  471. if (!xo)
  472. return xfrm_replay_overflow(x, skb);
  473. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  474. if (!skb_is_gso(skb)) {
  475. XFRM_SKB_CB(skb)->seq.output.low = ++oseq;
  476. xo->seq.low = oseq;
  477. } else {
  478. XFRM_SKB_CB(skb)->seq.output.low = oseq + 1;
  479. xo->seq.low = oseq + 1;
  480. oseq += skb_shinfo(skb)->gso_segs;
  481. }
  482. XFRM_SKB_CB(skb)->seq.output.hi = 0;
  483. xo->seq.hi = 0;
  484. if (unlikely(oseq < x->replay.oseq)) {
  485. xfrm_audit_state_replay_overflow(x, skb);
  486. err = -EOVERFLOW;
  487. return err;
  488. }
  489. x->replay.oseq = oseq;
  490. if (xfrm_aevent_is_on(net))
  491. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  492. }
  493. return err;
  494. }
  495. static int xfrm_replay_overflow_offload_bmp(struct xfrm_state *x, struct sk_buff *skb)
  496. {
  497. int err = 0;
  498. struct xfrm_offload *xo = xfrm_offload(skb);
  499. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  500. struct net *net = xs_net(x);
  501. __u32 oseq = replay_esn->oseq;
  502. if (!xo)
  503. return xfrm_replay_overflow_bmp(x, skb);
  504. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  505. if (!skb_is_gso(skb)) {
  506. XFRM_SKB_CB(skb)->seq.output.low = ++oseq;
  507. xo->seq.low = oseq;
  508. } else {
  509. XFRM_SKB_CB(skb)->seq.output.low = oseq + 1;
  510. xo->seq.low = oseq + 1;
  511. oseq += skb_shinfo(skb)->gso_segs;
  512. }
  513. XFRM_SKB_CB(skb)->seq.output.hi = 0;
  514. xo->seq.hi = 0;
  515. if (unlikely(oseq < replay_esn->oseq)) {
  516. xfrm_audit_state_replay_overflow(x, skb);
  517. err = -EOVERFLOW;
  518. return err;
  519. } else {
  520. replay_esn->oseq = oseq;
  521. }
  522. if (xfrm_aevent_is_on(net))
  523. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  524. }
  525. return err;
  526. }
  527. static int xfrm_replay_overflow_offload_esn(struct xfrm_state *x, struct sk_buff *skb)
  528. {
  529. int err = 0;
  530. struct xfrm_offload *xo = xfrm_offload(skb);
  531. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  532. struct net *net = xs_net(x);
  533. __u32 oseq = replay_esn->oseq;
  534. __u32 oseq_hi = replay_esn->oseq_hi;
  535. if (!xo)
  536. return xfrm_replay_overflow_esn(x, skb);
  537. if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
  538. if (!skb_is_gso(skb)) {
  539. XFRM_SKB_CB(skb)->seq.output.low = ++oseq;
  540. XFRM_SKB_CB(skb)->seq.output.hi = oseq_hi;
  541. xo->seq.low = oseq;
  542. xo->seq.hi = oseq_hi;
  543. } else {
  544. XFRM_SKB_CB(skb)->seq.output.low = oseq + 1;
  545. XFRM_SKB_CB(skb)->seq.output.hi = oseq_hi;
  546. xo->seq.low = oseq + 1;
  547. xo->seq.hi = oseq_hi;
  548. oseq += skb_shinfo(skb)->gso_segs;
  549. }
  550. if (unlikely(oseq < replay_esn->oseq)) {
  551. XFRM_SKB_CB(skb)->seq.output.hi = ++oseq_hi;
  552. xo->seq.hi = oseq_hi;
  553. replay_esn->oseq_hi = oseq_hi;
  554. if (replay_esn->oseq_hi == 0) {
  555. replay_esn->oseq--;
  556. replay_esn->oseq_hi--;
  557. xfrm_audit_state_replay_overflow(x, skb);
  558. err = -EOVERFLOW;
  559. return err;
  560. }
  561. }
  562. replay_esn->oseq = oseq;
  563. if (xfrm_aevent_is_on(net))
  564. x->repl->notify(x, XFRM_REPLAY_UPDATE);
  565. }
  566. return err;
  567. }
  568. static const struct xfrm_replay xfrm_replay_legacy = {
  569. .advance = xfrm_replay_advance,
  570. .check = xfrm_replay_check,
  571. .recheck = xfrm_replay_check,
  572. .notify = xfrm_replay_notify,
  573. .overflow = xfrm_replay_overflow_offload,
  574. };
  575. static const struct xfrm_replay xfrm_replay_bmp = {
  576. .advance = xfrm_replay_advance_bmp,
  577. .check = xfrm_replay_check_bmp,
  578. .recheck = xfrm_replay_check_bmp,
  579. .notify = xfrm_replay_notify_bmp,
  580. .overflow = xfrm_replay_overflow_offload_bmp,
  581. };
  582. static const struct xfrm_replay xfrm_replay_esn = {
  583. .advance = xfrm_replay_advance_esn,
  584. .check = xfrm_replay_check_esn,
  585. .recheck = xfrm_replay_recheck_esn,
  586. .notify = xfrm_replay_notify_esn,
  587. .overflow = xfrm_replay_overflow_offload_esn,
  588. };
  589. #else
  590. static const struct xfrm_replay xfrm_replay_legacy = {
  591. .advance = xfrm_replay_advance,
  592. .check = xfrm_replay_check,
  593. .recheck = xfrm_replay_check,
  594. .notify = xfrm_replay_notify,
  595. .overflow = xfrm_replay_overflow,
  596. };
  597. static const struct xfrm_replay xfrm_replay_bmp = {
  598. .advance = xfrm_replay_advance_bmp,
  599. .check = xfrm_replay_check_bmp,
  600. .recheck = xfrm_replay_check_bmp,
  601. .notify = xfrm_replay_notify_bmp,
  602. .overflow = xfrm_replay_overflow_bmp,
  603. };
  604. static const struct xfrm_replay xfrm_replay_esn = {
  605. .advance = xfrm_replay_advance_esn,
  606. .check = xfrm_replay_check_esn,
  607. .recheck = xfrm_replay_recheck_esn,
  608. .notify = xfrm_replay_notify_esn,
  609. .overflow = xfrm_replay_overflow_esn,
  610. };
  611. #endif
  612. int xfrm_init_replay(struct xfrm_state *x)
  613. {
  614. struct xfrm_replay_state_esn *replay_esn = x->replay_esn;
  615. if (replay_esn) {
  616. if (replay_esn->replay_window >
  617. replay_esn->bmp_len * sizeof(__u32) * 8)
  618. return -EINVAL;
  619. if (x->props.flags & XFRM_STATE_ESN) {
  620. if (replay_esn->replay_window == 0)
  621. return -EINVAL;
  622. x->repl = &xfrm_replay_esn;
  623. } else {
  624. x->repl = &xfrm_replay_bmp;
  625. }
  626. } else {
  627. x->repl = &xfrm_replay_legacy;
  628. }
  629. return 0;
  630. }
  631. EXPORT_SYMBOL(xfrm_init_replay);