xfrm_input.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xfrm_input.c
  4. *
  5. * Changes:
  6. * YOSHIFUJI Hideaki @USAGI
  7. * Split up af-specific portion
  8. *
  9. */
  10. #include <linux/bottom_half.h>
  11. #include <linux/cache.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/percpu.h>
  17. #include <net/dst.h>
  18. #include <net/ip.h>
  19. #include <net/xfrm.h>
  20. #include <net/ip_tunnels.h>
  21. #include <net/ip6_tunnel.h>
  22. struct xfrm_trans_tasklet {
  23. struct tasklet_struct tasklet;
  24. struct sk_buff_head queue;
  25. };
  26. struct xfrm_trans_cb {
  27. union {
  28. struct inet_skb_parm h4;
  29. #if IS_ENABLED(CONFIG_IPV6)
  30. struct inet6_skb_parm h6;
  31. #endif
  32. } header;
  33. int (*finish)(struct net *net, struct sock *sk, struct sk_buff *skb);
  34. };
  35. #define XFRM_TRANS_SKB_CB(__skb) ((struct xfrm_trans_cb *)&((__skb)->cb[0]))
  36. static struct kmem_cache *secpath_cachep __ro_after_init;
  37. static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
  38. static struct xfrm_input_afinfo const __rcu *xfrm_input_afinfo[AF_INET6 + 1];
  39. static struct gro_cells gro_cells;
  40. static struct net_device xfrm_napi_dev;
  41. static DEFINE_PER_CPU(struct xfrm_trans_tasklet, xfrm_trans_tasklet);
  42. int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo)
  43. {
  44. int err = 0;
  45. if (WARN_ON(afinfo->family >= ARRAY_SIZE(xfrm_input_afinfo)))
  46. return -EAFNOSUPPORT;
  47. spin_lock_bh(&xfrm_input_afinfo_lock);
  48. if (unlikely(xfrm_input_afinfo[afinfo->family] != NULL))
  49. err = -EEXIST;
  50. else
  51. rcu_assign_pointer(xfrm_input_afinfo[afinfo->family], afinfo);
  52. spin_unlock_bh(&xfrm_input_afinfo_lock);
  53. return err;
  54. }
  55. EXPORT_SYMBOL(xfrm_input_register_afinfo);
  56. int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo)
  57. {
  58. int err = 0;
  59. spin_lock_bh(&xfrm_input_afinfo_lock);
  60. if (likely(xfrm_input_afinfo[afinfo->family] != NULL)) {
  61. if (unlikely(xfrm_input_afinfo[afinfo->family] != afinfo))
  62. err = -EINVAL;
  63. else
  64. RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->family], NULL);
  65. }
  66. spin_unlock_bh(&xfrm_input_afinfo_lock);
  67. synchronize_rcu();
  68. return err;
  69. }
  70. EXPORT_SYMBOL(xfrm_input_unregister_afinfo);
  71. static const struct xfrm_input_afinfo *xfrm_input_get_afinfo(unsigned int family)
  72. {
  73. const struct xfrm_input_afinfo *afinfo;
  74. if (WARN_ON_ONCE(family >= ARRAY_SIZE(xfrm_input_afinfo)))
  75. return NULL;
  76. rcu_read_lock();
  77. afinfo = rcu_dereference(xfrm_input_afinfo[family]);
  78. if (unlikely(!afinfo))
  79. rcu_read_unlock();
  80. return afinfo;
  81. }
  82. static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
  83. int err)
  84. {
  85. int ret;
  86. const struct xfrm_input_afinfo *afinfo = xfrm_input_get_afinfo(family);
  87. if (!afinfo)
  88. return -EAFNOSUPPORT;
  89. ret = afinfo->callback(skb, protocol, err);
  90. rcu_read_unlock();
  91. return ret;
  92. }
  93. void __secpath_destroy(struct sec_path *sp)
  94. {
  95. int i;
  96. for (i = 0; i < sp->len; i++)
  97. xfrm_state_put(sp->xvec[i]);
  98. kmem_cache_free(secpath_cachep, sp);
  99. }
  100. EXPORT_SYMBOL(__secpath_destroy);
  101. struct sec_path *secpath_dup(struct sec_path *src)
  102. {
  103. struct sec_path *sp;
  104. sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
  105. if (!sp)
  106. return NULL;
  107. sp->len = 0;
  108. sp->olen = 0;
  109. memset(sp->ovec, 0, sizeof(sp->ovec));
  110. if (src) {
  111. int i;
  112. memcpy(sp, src, sizeof(*sp));
  113. for (i = 0; i < sp->len; i++)
  114. xfrm_state_hold(sp->xvec[i]);
  115. }
  116. refcount_set(&sp->refcnt, 1);
  117. return sp;
  118. }
  119. EXPORT_SYMBOL(secpath_dup);
  120. int secpath_set(struct sk_buff *skb)
  121. {
  122. struct sec_path *sp;
  123. /* Allocate new secpath or COW existing one. */
  124. if (!skb->sp || refcount_read(&skb->sp->refcnt) != 1) {
  125. sp = secpath_dup(skb->sp);
  126. if (!sp)
  127. return -ENOMEM;
  128. if (skb->sp)
  129. secpath_put(skb->sp);
  130. skb->sp = sp;
  131. }
  132. return 0;
  133. }
  134. EXPORT_SYMBOL(secpath_set);
  135. /* Fetch spi and seq from ipsec header */
  136. int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
  137. {
  138. int offset, offset_seq;
  139. int hlen;
  140. switch (nexthdr) {
  141. case IPPROTO_AH:
  142. hlen = sizeof(struct ip_auth_hdr);
  143. offset = offsetof(struct ip_auth_hdr, spi);
  144. offset_seq = offsetof(struct ip_auth_hdr, seq_no);
  145. break;
  146. case IPPROTO_ESP:
  147. hlen = sizeof(struct ip_esp_hdr);
  148. offset = offsetof(struct ip_esp_hdr, spi);
  149. offset_seq = offsetof(struct ip_esp_hdr, seq_no);
  150. break;
  151. case IPPROTO_COMP:
  152. if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
  153. return -EINVAL;
  154. *spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
  155. *seq = 0;
  156. return 0;
  157. default:
  158. return 1;
  159. }
  160. if (!pskb_may_pull(skb, hlen))
  161. return -EINVAL;
  162. *spi = *(__be32 *)(skb_transport_header(skb) + offset);
  163. *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
  164. return 0;
  165. }
  166. EXPORT_SYMBOL(xfrm_parse_spi);
  167. int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
  168. {
  169. struct xfrm_mode *inner_mode = x->inner_mode;
  170. int err;
  171. err = x->outer_mode->afinfo->extract_input(x, skb);
  172. if (err)
  173. return err;
  174. if (x->sel.family == AF_UNSPEC) {
  175. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  176. if (inner_mode == NULL)
  177. return -EAFNOSUPPORT;
  178. }
  179. skb->protocol = inner_mode->afinfo->eth_proto;
  180. return inner_mode->input2(x, skb);
  181. }
  182. EXPORT_SYMBOL(xfrm_prepare_input);
  183. int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
  184. {
  185. struct net *net = dev_net(skb->dev);
  186. int err;
  187. __be32 seq;
  188. __be32 seq_hi;
  189. struct xfrm_state *x = NULL;
  190. xfrm_address_t *daddr;
  191. struct xfrm_mode *inner_mode;
  192. u32 mark = skb->mark;
  193. unsigned int family = AF_UNSPEC;
  194. int decaps = 0;
  195. int async = 0;
  196. bool xfrm_gro = false;
  197. bool crypto_done = false;
  198. struct xfrm_offload *xo = xfrm_offload(skb);
  199. if (encap_type < 0) {
  200. x = xfrm_input_state(skb);
  201. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  202. if (x->km.state == XFRM_STATE_ACQ)
  203. XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
  204. else
  205. XFRM_INC_STATS(net,
  206. LINUX_MIB_XFRMINSTATEINVALID);
  207. goto drop;
  208. }
  209. family = x->outer_mode->afinfo->family;
  210. /* An encap_type of -1 indicates async resumption. */
  211. if (encap_type == -1) {
  212. async = 1;
  213. seq = XFRM_SKB_CB(skb)->seq.input.low;
  214. goto resume;
  215. }
  216. /* encap_type < -1 indicates a GRO call. */
  217. encap_type = 0;
  218. seq = XFRM_SPI_SKB_CB(skb)->seq;
  219. if (xo && (xo->flags & CRYPTO_DONE)) {
  220. crypto_done = true;
  221. family = XFRM_SPI_SKB_CB(skb)->family;
  222. if (!(xo->status & CRYPTO_SUCCESS)) {
  223. if (xo->status &
  224. (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
  225. CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
  226. CRYPTO_TUNNEL_AH_AUTH_FAILED |
  227. CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {
  228. xfrm_audit_state_icvfail(x, skb,
  229. x->type->proto);
  230. x->stats.integrity_failed++;
  231. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  232. goto drop;
  233. }
  234. if (xo->status & CRYPTO_INVALID_PROTOCOL) {
  235. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  236. goto drop;
  237. }
  238. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  239. goto drop;
  240. }
  241. if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
  242. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  243. goto drop;
  244. }
  245. }
  246. goto lock;
  247. }
  248. family = XFRM_SPI_SKB_CB(skb)->family;
  249. /* if tunnel is present override skb->mark value with tunnel i_key */
  250. switch (family) {
  251. case AF_INET:
  252. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
  253. mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
  254. break;
  255. case AF_INET6:
  256. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
  257. mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
  258. break;
  259. }
  260. err = secpath_set(skb);
  261. if (err) {
  262. XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
  263. goto drop;
  264. }
  265. seq = 0;
  266. if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
  267. secpath_reset(skb);
  268. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  269. goto drop;
  270. }
  271. daddr = (xfrm_address_t *)(skb_network_header(skb) +
  272. XFRM_SPI_SKB_CB(skb)->daddroff);
  273. do {
  274. if (skb->sp->len == XFRM_MAX_DEPTH) {
  275. secpath_reset(skb);
  276. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  277. goto drop;
  278. }
  279. x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
  280. if (x == NULL) {
  281. secpath_reset(skb);
  282. XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
  283. xfrm_audit_state_notfound(skb, family, spi, seq);
  284. goto drop;
  285. }
  286. skb->mark = xfrm_smark_get(skb->mark, x);
  287. skb->sp->xvec[skb->sp->len++] = x;
  288. lock:
  289. spin_lock(&x->lock);
  290. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  291. if (x->km.state == XFRM_STATE_ACQ)
  292. XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
  293. else
  294. XFRM_INC_STATS(net,
  295. LINUX_MIB_XFRMINSTATEINVALID);
  296. goto drop_unlock;
  297. }
  298. if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
  299. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
  300. goto drop_unlock;
  301. }
  302. if (x->repl->check(x, skb, seq)) {
  303. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  304. goto drop_unlock;
  305. }
  306. if (xfrm_state_check_expire(x)) {
  307. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
  308. goto drop_unlock;
  309. }
  310. spin_unlock(&x->lock);
  311. if (xfrm_tunnel_check(skb, x, family)) {
  312. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  313. goto drop;
  314. }
  315. seq_hi = htonl(xfrm_replay_seqhi(x, seq));
  316. XFRM_SKB_CB(skb)->seq.input.low = seq;
  317. XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
  318. skb_dst_force(skb);
  319. dev_hold(skb->dev);
  320. if (crypto_done)
  321. nexthdr = x->type_offload->input_tail(x, skb);
  322. else
  323. nexthdr = x->type->input(x, skb);
  324. if (nexthdr == -EINPROGRESS)
  325. return 0;
  326. resume:
  327. dev_put(skb->dev);
  328. spin_lock(&x->lock);
  329. if (nexthdr <= 0) {
  330. if (nexthdr == -EBADMSG) {
  331. xfrm_audit_state_icvfail(x, skb,
  332. x->type->proto);
  333. x->stats.integrity_failed++;
  334. }
  335. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  336. goto drop_unlock;
  337. }
  338. /* only the first xfrm gets the encap type */
  339. encap_type = 0;
  340. if (async && x->repl->recheck(x, skb, seq)) {
  341. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  342. goto drop_unlock;
  343. }
  344. x->repl->advance(x, seq);
  345. x->curlft.bytes += skb->len;
  346. x->curlft.packets++;
  347. spin_unlock(&x->lock);
  348. XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
  349. inner_mode = x->inner_mode;
  350. if (x->sel.family == AF_UNSPEC) {
  351. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  352. if (inner_mode == NULL) {
  353. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  354. goto drop;
  355. }
  356. }
  357. if (inner_mode->input(x, skb)) {
  358. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  359. goto drop;
  360. }
  361. if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
  362. decaps = 1;
  363. break;
  364. }
  365. /*
  366. * We need the inner address. However, we only get here for
  367. * transport mode so the outer address is identical.
  368. */
  369. daddr = &x->id.daddr;
  370. family = x->outer_mode->afinfo->family;
  371. err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
  372. if (err < 0) {
  373. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  374. goto drop;
  375. }
  376. crypto_done = false;
  377. } while (!err);
  378. err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
  379. if (err)
  380. goto drop;
  381. nf_reset(skb);
  382. if (decaps) {
  383. if (skb->sp)
  384. skb->sp->olen = 0;
  385. skb_dst_drop(skb);
  386. gro_cells_receive(&gro_cells, skb);
  387. return 0;
  388. } else {
  389. xo = xfrm_offload(skb);
  390. if (xo)
  391. xfrm_gro = xo->flags & XFRM_GRO;
  392. err = x->inner_mode->afinfo->transport_finish(skb, xfrm_gro || async);
  393. if (xfrm_gro) {
  394. if (skb->sp)
  395. skb->sp->olen = 0;
  396. skb_dst_drop(skb);
  397. gro_cells_receive(&gro_cells, skb);
  398. return err;
  399. }
  400. return err;
  401. }
  402. drop_unlock:
  403. spin_unlock(&x->lock);
  404. drop:
  405. xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
  406. kfree_skb(skb);
  407. return 0;
  408. }
  409. EXPORT_SYMBOL(xfrm_input);
  410. int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
  411. {
  412. return xfrm_input(skb, nexthdr, 0, -1);
  413. }
  414. EXPORT_SYMBOL(xfrm_input_resume);
  415. static void xfrm_trans_reinject(unsigned long data)
  416. {
  417. struct xfrm_trans_tasklet *trans = (void *)data;
  418. struct sk_buff_head queue;
  419. struct sk_buff *skb;
  420. __skb_queue_head_init(&queue);
  421. skb_queue_splice_init(&trans->queue, &queue);
  422. while ((skb = __skb_dequeue(&queue)))
  423. XFRM_TRANS_SKB_CB(skb)->finish(dev_net(skb->dev), NULL, skb);
  424. }
  425. int xfrm_trans_queue(struct sk_buff *skb,
  426. int (*finish)(struct net *, struct sock *,
  427. struct sk_buff *))
  428. {
  429. struct xfrm_trans_tasklet *trans;
  430. trans = this_cpu_ptr(&xfrm_trans_tasklet);
  431. if (skb_queue_len(&trans->queue) >= netdev_max_backlog)
  432. return -ENOBUFS;
  433. XFRM_TRANS_SKB_CB(skb)->finish = finish;
  434. __skb_queue_tail(&trans->queue, skb);
  435. tasklet_schedule(&trans->tasklet);
  436. return 0;
  437. }
  438. EXPORT_SYMBOL(xfrm_trans_queue);
  439. void __init xfrm_input_init(void)
  440. {
  441. int err;
  442. int i;
  443. init_dummy_netdev(&xfrm_napi_dev);
  444. err = gro_cells_init(&gro_cells, &xfrm_napi_dev);
  445. if (err)
  446. gro_cells.cells = NULL;
  447. secpath_cachep = kmem_cache_create("secpath_cache",
  448. sizeof(struct sec_path),
  449. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  450. NULL);
  451. for_each_possible_cpu(i) {
  452. struct xfrm_trans_tasklet *trans;
  453. trans = &per_cpu(xfrm_trans_tasklet, i);
  454. __skb_queue_head_init(&trans->queue);
  455. tasklet_init(&trans->tasklet, xfrm_trans_reinject,
  456. (unsigned long)trans);
  457. }
  458. }