xfrm_input.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. if (encap_type == -1)
  208. dev_put(skb->dev);
  209. goto drop;
  210. }
  211. family = x->outer_mode->afinfo->family;
  212. /* An encap_type of -1 indicates async resumption. */
  213. if (encap_type == -1) {
  214. async = 1;
  215. seq = XFRM_SKB_CB(skb)->seq.input.low;
  216. goto resume;
  217. }
  218. /* encap_type < -1 indicates a GRO call. */
  219. encap_type = 0;
  220. seq = XFRM_SPI_SKB_CB(skb)->seq;
  221. if (xo && (xo->flags & CRYPTO_DONE)) {
  222. crypto_done = true;
  223. family = XFRM_SPI_SKB_CB(skb)->family;
  224. if (!(xo->status & CRYPTO_SUCCESS)) {
  225. if (xo->status &
  226. (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
  227. CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
  228. CRYPTO_TUNNEL_AH_AUTH_FAILED |
  229. CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {
  230. xfrm_audit_state_icvfail(x, skb,
  231. x->type->proto);
  232. x->stats.integrity_failed++;
  233. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  234. goto drop;
  235. }
  236. if (xo->status & CRYPTO_INVALID_PROTOCOL) {
  237. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  238. goto drop;
  239. }
  240. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  241. goto drop;
  242. }
  243. if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
  244. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  245. goto drop;
  246. }
  247. }
  248. goto lock;
  249. }
  250. family = XFRM_SPI_SKB_CB(skb)->family;
  251. /* if tunnel is present override skb->mark value with tunnel i_key */
  252. switch (family) {
  253. case AF_INET:
  254. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
  255. mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
  256. break;
  257. case AF_INET6:
  258. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
  259. mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
  260. break;
  261. }
  262. err = secpath_set(skb);
  263. if (err) {
  264. XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
  265. goto drop;
  266. }
  267. seq = 0;
  268. if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
  269. secpath_reset(skb);
  270. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  271. goto drop;
  272. }
  273. daddr = (xfrm_address_t *)(skb_network_header(skb) +
  274. XFRM_SPI_SKB_CB(skb)->daddroff);
  275. do {
  276. if (skb->sp->len == XFRM_MAX_DEPTH) {
  277. secpath_reset(skb);
  278. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  279. goto drop;
  280. }
  281. x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
  282. if (x == NULL) {
  283. secpath_reset(skb);
  284. XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
  285. xfrm_audit_state_notfound(skb, family, spi, seq);
  286. goto drop;
  287. }
  288. skb->mark = xfrm_smark_get(skb->mark, x);
  289. skb->sp->xvec[skb->sp->len++] = x;
  290. skb_dst_force(skb);
  291. if (!skb_dst(skb)) {
  292. XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
  293. goto drop;
  294. }
  295. lock:
  296. spin_lock(&x->lock);
  297. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  298. if (x->km.state == XFRM_STATE_ACQ)
  299. XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
  300. else
  301. XFRM_INC_STATS(net,
  302. LINUX_MIB_XFRMINSTATEINVALID);
  303. goto drop_unlock;
  304. }
  305. if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
  306. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
  307. goto drop_unlock;
  308. }
  309. if (x->repl->check(x, skb, seq)) {
  310. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  311. goto drop_unlock;
  312. }
  313. if (xfrm_state_check_expire(x)) {
  314. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
  315. goto drop_unlock;
  316. }
  317. spin_unlock(&x->lock);
  318. if (xfrm_tunnel_check(skb, x, family)) {
  319. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  320. goto drop;
  321. }
  322. seq_hi = htonl(xfrm_replay_seqhi(x, seq));
  323. XFRM_SKB_CB(skb)->seq.input.low = seq;
  324. XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
  325. dev_hold(skb->dev);
  326. if (crypto_done)
  327. nexthdr = x->type_offload->input_tail(x, skb);
  328. else
  329. nexthdr = x->type->input(x, skb);
  330. if (nexthdr == -EINPROGRESS)
  331. return 0;
  332. resume:
  333. dev_put(skb->dev);
  334. spin_lock(&x->lock);
  335. if (nexthdr <= 0) {
  336. if (nexthdr == -EBADMSG) {
  337. xfrm_audit_state_icvfail(x, skb,
  338. x->type->proto);
  339. x->stats.integrity_failed++;
  340. }
  341. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  342. goto drop_unlock;
  343. }
  344. /* only the first xfrm gets the encap type */
  345. encap_type = 0;
  346. if (async && x->repl->recheck(x, skb, seq)) {
  347. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  348. goto drop_unlock;
  349. }
  350. x->repl->advance(x, seq);
  351. x->curlft.bytes += skb->len;
  352. x->curlft.packets++;
  353. spin_unlock(&x->lock);
  354. XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
  355. inner_mode = x->inner_mode;
  356. if (x->sel.family == AF_UNSPEC) {
  357. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  358. if (inner_mode == NULL) {
  359. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  360. goto drop;
  361. }
  362. }
  363. if (inner_mode->input(x, skb)) {
  364. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  365. goto drop;
  366. }
  367. if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
  368. decaps = 1;
  369. break;
  370. }
  371. /*
  372. * We need the inner address. However, we only get here for
  373. * transport mode so the outer address is identical.
  374. */
  375. daddr = &x->id.daddr;
  376. family = x->outer_mode->afinfo->family;
  377. err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
  378. if (err < 0) {
  379. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  380. goto drop;
  381. }
  382. crypto_done = false;
  383. } while (!err);
  384. err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
  385. if (err)
  386. goto drop;
  387. nf_reset(skb);
  388. if (decaps) {
  389. if (skb->sp)
  390. skb->sp->olen = 0;
  391. skb_dst_drop(skb);
  392. gro_cells_receive(&gro_cells, skb);
  393. return 0;
  394. } else {
  395. xo = xfrm_offload(skb);
  396. if (xo)
  397. xfrm_gro = xo->flags & XFRM_GRO;
  398. err = x->inner_mode->afinfo->transport_finish(skb, xfrm_gro || async);
  399. if (xfrm_gro) {
  400. if (skb->sp)
  401. skb->sp->olen = 0;
  402. skb_dst_drop(skb);
  403. gro_cells_receive(&gro_cells, skb);
  404. return err;
  405. }
  406. return err;
  407. }
  408. drop_unlock:
  409. spin_unlock(&x->lock);
  410. drop:
  411. xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
  412. kfree_skb(skb);
  413. return 0;
  414. }
  415. EXPORT_SYMBOL(xfrm_input);
  416. int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
  417. {
  418. return xfrm_input(skb, nexthdr, 0, -1);
  419. }
  420. EXPORT_SYMBOL(xfrm_input_resume);
  421. static void xfrm_trans_reinject(unsigned long data)
  422. {
  423. struct xfrm_trans_tasklet *trans = (void *)data;
  424. struct sk_buff_head queue;
  425. struct sk_buff *skb;
  426. __skb_queue_head_init(&queue);
  427. skb_queue_splice_init(&trans->queue, &queue);
  428. while ((skb = __skb_dequeue(&queue)))
  429. XFRM_TRANS_SKB_CB(skb)->finish(dev_net(skb->dev), NULL, skb);
  430. }
  431. int xfrm_trans_queue(struct sk_buff *skb,
  432. int (*finish)(struct net *, struct sock *,
  433. struct sk_buff *))
  434. {
  435. struct xfrm_trans_tasklet *trans;
  436. trans = this_cpu_ptr(&xfrm_trans_tasklet);
  437. if (skb_queue_len(&trans->queue) >= netdev_max_backlog)
  438. return -ENOBUFS;
  439. XFRM_TRANS_SKB_CB(skb)->finish = finish;
  440. __skb_queue_tail(&trans->queue, skb);
  441. tasklet_schedule(&trans->tasklet);
  442. return 0;
  443. }
  444. EXPORT_SYMBOL(xfrm_trans_queue);
  445. void __init xfrm_input_init(void)
  446. {
  447. int err;
  448. int i;
  449. init_dummy_netdev(&xfrm_napi_dev);
  450. err = gro_cells_init(&gro_cells, &xfrm_napi_dev);
  451. if (err)
  452. gro_cells.cells = NULL;
  453. secpath_cachep = kmem_cache_create("secpath_cache",
  454. sizeof(struct sec_path),
  455. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  456. NULL);
  457. for_each_possible_cpu(i) {
  458. struct xfrm_trans_tasklet *trans;
  459. trans = &per_cpu(xfrm_trans_tasklet, i);
  460. __skb_queue_head_init(&trans->queue);
  461. tasklet_init(&trans->tasklet, xfrm_trans_reinject,
  462. (unsigned long)trans);
  463. }
  464. }