xfrm_input.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. #include <net/dst_metadata.h>
  23. #include "xfrm_inout.h"
  24. struct xfrm_trans_tasklet {
  25. struct work_struct work;
  26. spinlock_t queue_lock;
  27. struct sk_buff_head queue;
  28. };
  29. struct xfrm_trans_cb {
  30. union {
  31. struct inet_skb_parm h4;
  32. #if IS_ENABLED(CONFIG_IPV6)
  33. struct inet6_skb_parm h6;
  34. #endif
  35. } header;
  36. int (*finish)(struct net *net, struct sock *sk, struct sk_buff *skb);
  37. struct net *net;
  38. };
  39. #define XFRM_TRANS_SKB_CB(__skb) ((struct xfrm_trans_cb *)&((__skb)->cb[0]))
  40. static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
  41. static struct xfrm_input_afinfo const __rcu *xfrm_input_afinfo[2][AF_INET6 + 1];
  42. static struct gro_cells gro_cells;
  43. static struct net_device xfrm_napi_dev;
  44. static DEFINE_PER_CPU(struct xfrm_trans_tasklet, xfrm_trans_tasklet);
  45. int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo)
  46. {
  47. int err = 0;
  48. if (WARN_ON(afinfo->family > AF_INET6))
  49. return -EAFNOSUPPORT;
  50. spin_lock_bh(&xfrm_input_afinfo_lock);
  51. if (unlikely(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family]))
  52. err = -EEXIST;
  53. else
  54. rcu_assign_pointer(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family], afinfo);
  55. spin_unlock_bh(&xfrm_input_afinfo_lock);
  56. return err;
  57. }
  58. EXPORT_SYMBOL(xfrm_input_register_afinfo);
  59. int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo)
  60. {
  61. int err = 0;
  62. spin_lock_bh(&xfrm_input_afinfo_lock);
  63. if (likely(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family])) {
  64. if (unlikely(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family] != afinfo))
  65. err = -EINVAL;
  66. else
  67. RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family], NULL);
  68. }
  69. spin_unlock_bh(&xfrm_input_afinfo_lock);
  70. synchronize_rcu();
  71. return err;
  72. }
  73. EXPORT_SYMBOL(xfrm_input_unregister_afinfo);
  74. static const struct xfrm_input_afinfo *xfrm_input_get_afinfo(u8 family, bool is_ipip)
  75. {
  76. const struct xfrm_input_afinfo *afinfo;
  77. if (WARN_ON_ONCE(family > AF_INET6))
  78. return NULL;
  79. rcu_read_lock();
  80. afinfo = rcu_dereference(xfrm_input_afinfo[is_ipip][family]);
  81. if (unlikely(!afinfo))
  82. rcu_read_unlock();
  83. return afinfo;
  84. }
  85. static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
  86. int err)
  87. {
  88. bool is_ipip = (protocol == IPPROTO_IPIP || protocol == IPPROTO_IPV6);
  89. const struct xfrm_input_afinfo *afinfo;
  90. int ret;
  91. afinfo = xfrm_input_get_afinfo(family, is_ipip);
  92. if (!afinfo)
  93. return -EAFNOSUPPORT;
  94. ret = afinfo->callback(skb, protocol, err);
  95. rcu_read_unlock();
  96. return ret;
  97. }
  98. struct sec_path *secpath_set(struct sk_buff *skb)
  99. {
  100. struct sec_path *sp, *tmp = skb_ext_find(skb, SKB_EXT_SEC_PATH);
  101. sp = skb_ext_add(skb, SKB_EXT_SEC_PATH);
  102. if (!sp)
  103. return NULL;
  104. if (tmp) /* reused existing one (was COW'd if needed) */
  105. return sp;
  106. /* allocated new secpath */
  107. memset(sp->ovec, 0, sizeof(sp->ovec));
  108. sp->olen = 0;
  109. sp->len = 0;
  110. sp->verified_cnt = 0;
  111. return sp;
  112. }
  113. EXPORT_SYMBOL(secpath_set);
  114. /* Fetch spi and seq from ipsec header */
  115. int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
  116. {
  117. int offset, offset_seq;
  118. int hlen;
  119. switch (nexthdr) {
  120. case IPPROTO_AH:
  121. hlen = sizeof(struct ip_auth_hdr);
  122. offset = offsetof(struct ip_auth_hdr, spi);
  123. offset_seq = offsetof(struct ip_auth_hdr, seq_no);
  124. break;
  125. case IPPROTO_ESP:
  126. hlen = sizeof(struct ip_esp_hdr);
  127. offset = offsetof(struct ip_esp_hdr, spi);
  128. offset_seq = offsetof(struct ip_esp_hdr, seq_no);
  129. break;
  130. case IPPROTO_COMP:
  131. if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
  132. return -EINVAL;
  133. *spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
  134. *seq = 0;
  135. return 0;
  136. default:
  137. return 1;
  138. }
  139. if (!pskb_may_pull(skb, hlen))
  140. return -EINVAL;
  141. *spi = *(__be32 *)(skb_transport_header(skb) + offset);
  142. *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
  143. return 0;
  144. }
  145. EXPORT_SYMBOL(xfrm_parse_spi);
  146. static int xfrm4_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb)
  147. {
  148. struct iphdr *iph;
  149. int optlen = 0;
  150. int err = -EINVAL;
  151. if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) {
  152. struct ip_beet_phdr *ph;
  153. int phlen;
  154. if (!pskb_may_pull(skb, sizeof(*ph)))
  155. goto out;
  156. ph = (struct ip_beet_phdr *)skb->data;
  157. phlen = sizeof(*ph) + ph->padlen;
  158. optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen);
  159. if (optlen < 0 || optlen & 3 || optlen > 250)
  160. goto out;
  161. XFRM_MODE_SKB_CB(skb)->protocol = ph->nexthdr;
  162. if (!pskb_may_pull(skb, phlen))
  163. goto out;
  164. __skb_pull(skb, phlen);
  165. }
  166. skb_push(skb, sizeof(*iph));
  167. skb_reset_network_header(skb);
  168. skb_mac_header_rebuild(skb);
  169. xfrm4_beet_make_header(skb);
  170. iph = ip_hdr(skb);
  171. iph->ihl += optlen / 4;
  172. iph->tot_len = htons(skb->len);
  173. iph->daddr = x->sel.daddr.a4;
  174. iph->saddr = x->sel.saddr.a4;
  175. iph->check = 0;
  176. iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
  177. err = 0;
  178. out:
  179. return err;
  180. }
  181. static void ipip_ecn_decapsulate(struct sk_buff *skb)
  182. {
  183. struct iphdr *inner_iph = ipip_hdr(skb);
  184. if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
  185. IP_ECN_set_ce(inner_iph);
  186. }
  187. static int xfrm4_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)
  188. {
  189. int err = -EINVAL;
  190. if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPIP)
  191. goto out;
  192. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  193. goto out;
  194. err = skb_unclone(skb, GFP_ATOMIC);
  195. if (err)
  196. goto out;
  197. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  198. ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipip_hdr(skb));
  199. if (!(x->props.flags & XFRM_STATE_NOECN))
  200. ipip_ecn_decapsulate(skb);
  201. skb_reset_network_header(skb);
  202. skb_mac_header_rebuild(skb);
  203. if (skb->mac_len)
  204. eth_hdr(skb)->h_proto = skb->protocol;
  205. err = 0;
  206. out:
  207. return err;
  208. }
  209. static void ipip6_ecn_decapsulate(struct sk_buff *skb)
  210. {
  211. struct ipv6hdr *inner_iph = ipipv6_hdr(skb);
  212. if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
  213. IP6_ECN_set_ce(skb, inner_iph);
  214. }
  215. static int xfrm6_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)
  216. {
  217. int err = -EINVAL;
  218. if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPV6)
  219. goto out;
  220. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  221. goto out;
  222. err = skb_unclone(skb, GFP_ATOMIC);
  223. if (err)
  224. goto out;
  225. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  226. ipv6_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipipv6_hdr(skb));
  227. if (!(x->props.flags & XFRM_STATE_NOECN))
  228. ipip6_ecn_decapsulate(skb);
  229. skb_reset_network_header(skb);
  230. skb_mac_header_rebuild(skb);
  231. if (skb->mac_len)
  232. eth_hdr(skb)->h_proto = skb->protocol;
  233. err = 0;
  234. out:
  235. return err;
  236. }
  237. static int xfrm6_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb)
  238. {
  239. struct ipv6hdr *ip6h;
  240. int size = sizeof(struct ipv6hdr);
  241. int err;
  242. err = skb_cow_head(skb, size + skb->mac_len);
  243. if (err)
  244. goto out;
  245. __skb_push(skb, size);
  246. skb_reset_network_header(skb);
  247. skb_mac_header_rebuild(skb);
  248. xfrm6_beet_make_header(skb);
  249. ip6h = ipv6_hdr(skb);
  250. ip6h->payload_len = htons(skb->len - size);
  251. ip6h->daddr = x->sel.daddr.in6;
  252. ip6h->saddr = x->sel.saddr.in6;
  253. err = 0;
  254. out:
  255. return err;
  256. }
  257. /* Remove encapsulation header.
  258. *
  259. * The IP header will be moved over the top of the encapsulation
  260. * header.
  261. *
  262. * On entry, the transport header shall point to where the IP header
  263. * should be and the network header shall be set to where the IP
  264. * header currently is. skb->data shall point to the start of the
  265. * payload.
  266. */
  267. static int
  268. xfrm_inner_mode_encap_remove(struct xfrm_state *x,
  269. const struct xfrm_mode *inner_mode,
  270. struct sk_buff *skb)
  271. {
  272. switch (inner_mode->encap) {
  273. case XFRM_MODE_BEET:
  274. if (inner_mode->family == AF_INET)
  275. return xfrm4_remove_beet_encap(x, skb);
  276. if (inner_mode->family == AF_INET6)
  277. return xfrm6_remove_beet_encap(x, skb);
  278. break;
  279. case XFRM_MODE_TUNNEL:
  280. if (inner_mode->family == AF_INET)
  281. return xfrm4_remove_tunnel_encap(x, skb);
  282. if (inner_mode->family == AF_INET6)
  283. return xfrm6_remove_tunnel_encap(x, skb);
  284. break;
  285. }
  286. WARN_ON_ONCE(1);
  287. return -EOPNOTSUPP;
  288. }
  289. static int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
  290. {
  291. const struct xfrm_mode *inner_mode = &x->inner_mode;
  292. switch (x->outer_mode.family) {
  293. case AF_INET:
  294. xfrm4_extract_header(skb);
  295. break;
  296. case AF_INET6:
  297. xfrm6_extract_header(skb);
  298. break;
  299. default:
  300. WARN_ON_ONCE(1);
  301. return -EAFNOSUPPORT;
  302. }
  303. if (x->sel.family == AF_UNSPEC) {
  304. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  305. if (!inner_mode)
  306. return -EAFNOSUPPORT;
  307. }
  308. switch (inner_mode->family) {
  309. case AF_INET:
  310. skb->protocol = htons(ETH_P_IP);
  311. break;
  312. case AF_INET6:
  313. skb->protocol = htons(ETH_P_IPV6);
  314. break;
  315. default:
  316. WARN_ON_ONCE(1);
  317. break;
  318. }
  319. return xfrm_inner_mode_encap_remove(x, inner_mode, skb);
  320. }
  321. /* Remove encapsulation header.
  322. *
  323. * The IP header will be moved over the top of the encapsulation header.
  324. *
  325. * On entry, skb_transport_header() shall point to where the IP header
  326. * should be and skb_network_header() shall be set to where the IP header
  327. * currently is. skb->data shall point to the start of the payload.
  328. */
  329. static int xfrm4_transport_input(struct xfrm_state *x, struct sk_buff *skb)
  330. {
  331. int ihl = skb->data - skb_transport_header(skb);
  332. if (skb->transport_header != skb->network_header) {
  333. memmove(skb_transport_header(skb),
  334. skb_network_header(skb), ihl);
  335. skb->network_header = skb->transport_header;
  336. }
  337. ip_hdr(skb)->tot_len = htons(skb->len + ihl);
  338. skb_reset_transport_header(skb);
  339. return 0;
  340. }
  341. static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb)
  342. {
  343. #if IS_ENABLED(CONFIG_IPV6)
  344. int ihl = skb->data - skb_transport_header(skb);
  345. if (skb->transport_header != skb->network_header) {
  346. memmove(skb_transport_header(skb),
  347. skb_network_header(skb), ihl);
  348. skb->network_header = skb->transport_header;
  349. }
  350. ipv6_hdr(skb)->payload_len = htons(skb->len + ihl -
  351. sizeof(struct ipv6hdr));
  352. skb_reset_transport_header(skb);
  353. return 0;
  354. #else
  355. WARN_ON_ONCE(1);
  356. return -EAFNOSUPPORT;
  357. #endif
  358. }
  359. static int xfrm_inner_mode_input(struct xfrm_state *x,
  360. const struct xfrm_mode *inner_mode,
  361. struct sk_buff *skb)
  362. {
  363. switch (inner_mode->encap) {
  364. case XFRM_MODE_BEET:
  365. case XFRM_MODE_TUNNEL:
  366. return xfrm_prepare_input(x, skb);
  367. case XFRM_MODE_TRANSPORT:
  368. if (inner_mode->family == AF_INET)
  369. return xfrm4_transport_input(x, skb);
  370. if (inner_mode->family == AF_INET6)
  371. return xfrm6_transport_input(x, skb);
  372. break;
  373. case XFRM_MODE_ROUTEOPTIMIZATION:
  374. WARN_ON_ONCE(1);
  375. break;
  376. default:
  377. WARN_ON_ONCE(1);
  378. break;
  379. }
  380. return -EOPNOTSUPP;
  381. }
  382. int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
  383. {
  384. const struct xfrm_state_afinfo *afinfo;
  385. struct net *net = dev_net(skb->dev);
  386. const struct xfrm_mode *inner_mode;
  387. int err;
  388. __be32 seq;
  389. __be32 seq_hi;
  390. struct xfrm_state *x = NULL;
  391. xfrm_address_t *daddr;
  392. u32 mark = skb->mark;
  393. unsigned int family = AF_UNSPEC;
  394. int decaps = 0;
  395. int async = 0;
  396. bool xfrm_gro = false;
  397. bool crypto_done = false;
  398. struct xfrm_offload *xo = xfrm_offload(skb);
  399. struct sec_path *sp;
  400. if (encap_type < 0) {
  401. x = xfrm_input_state(skb);
  402. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  403. if (x->km.state == XFRM_STATE_ACQ)
  404. XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
  405. else
  406. XFRM_INC_STATS(net,
  407. LINUX_MIB_XFRMINSTATEINVALID);
  408. if (encap_type == -1)
  409. dev_put(skb->dev);
  410. goto drop;
  411. }
  412. family = x->outer_mode.family;
  413. /* An encap_type of -1 indicates async resumption. */
  414. if (encap_type == -1) {
  415. async = 1;
  416. seq = XFRM_SKB_CB(skb)->seq.input.low;
  417. goto resume;
  418. }
  419. /* encap_type < -1 indicates a GRO call. */
  420. encap_type = 0;
  421. seq = XFRM_SPI_SKB_CB(skb)->seq;
  422. if (xo && (xo->flags & CRYPTO_DONE)) {
  423. crypto_done = true;
  424. family = XFRM_SPI_SKB_CB(skb)->family;
  425. if (!(xo->status & CRYPTO_SUCCESS)) {
  426. if (xo->status &
  427. (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
  428. CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
  429. CRYPTO_TUNNEL_AH_AUTH_FAILED |
  430. CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {
  431. xfrm_audit_state_icvfail(x, skb,
  432. x->type->proto);
  433. x->stats.integrity_failed++;
  434. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  435. goto drop;
  436. }
  437. if (xo->status & CRYPTO_INVALID_PROTOCOL) {
  438. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  439. goto drop;
  440. }
  441. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  442. goto drop;
  443. }
  444. if (xfrm_parse_spi(skb, nexthdr, &spi, &seq)) {
  445. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  446. goto drop;
  447. }
  448. }
  449. goto lock;
  450. }
  451. family = XFRM_SPI_SKB_CB(skb)->family;
  452. /* if tunnel is present override skb->mark value with tunnel i_key */
  453. switch (family) {
  454. case AF_INET:
  455. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
  456. mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
  457. break;
  458. case AF_INET6:
  459. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
  460. mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
  461. break;
  462. }
  463. sp = secpath_set(skb);
  464. if (!sp) {
  465. XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
  466. goto drop;
  467. }
  468. seq = 0;
  469. if (!spi && xfrm_parse_spi(skb, nexthdr, &spi, &seq)) {
  470. secpath_reset(skb);
  471. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  472. goto drop;
  473. }
  474. daddr = (xfrm_address_t *)(skb_network_header(skb) +
  475. XFRM_SPI_SKB_CB(skb)->daddroff);
  476. do {
  477. sp = skb_sec_path(skb);
  478. if (sp->len == XFRM_MAX_DEPTH) {
  479. secpath_reset(skb);
  480. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  481. goto drop;
  482. }
  483. x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
  484. if (x == NULL) {
  485. secpath_reset(skb);
  486. XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
  487. xfrm_audit_state_notfound(skb, family, spi, seq);
  488. goto drop;
  489. }
  490. /* If nested tunnel, check outer states before context is lost.
  491. * Only nested tunnels need to be checked, since IP addresses change
  492. * as a result of the tunnel mode decapsulation. Similarly, this check
  493. * is limited to nested tunnels to avoid performing another policy
  494. * check on non-nested tunnels. On success, this check also updates the
  495. * secpath's verified_cnt variable, skipping future verifications of
  496. * previously-verified secpath entries.
  497. */
  498. if ((x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL) &&
  499. sp->verified_cnt < sp->len &&
  500. !xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family)) {
  501. goto drop;
  502. }
  503. skb->mark = xfrm_smark_get(skb->mark, x);
  504. sp->xvec[sp->len++] = x;
  505. skb_dst_force(skb);
  506. if (!skb_dst(skb)) {
  507. XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
  508. goto drop;
  509. }
  510. lock:
  511. spin_lock(&x->lock);
  512. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  513. if (x->km.state == XFRM_STATE_ACQ)
  514. XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
  515. else
  516. XFRM_INC_STATS(net,
  517. LINUX_MIB_XFRMINSTATEINVALID);
  518. goto drop_unlock;
  519. }
  520. if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
  521. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
  522. goto drop_unlock;
  523. }
  524. if (xfrm_replay_check(x, skb, seq)) {
  525. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  526. goto drop_unlock;
  527. }
  528. if (xfrm_state_check_expire(x)) {
  529. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
  530. goto drop_unlock;
  531. }
  532. spin_unlock(&x->lock);
  533. if (xfrm_tunnel_check(skb, x, family)) {
  534. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  535. goto drop;
  536. }
  537. seq_hi = htonl(xfrm_replay_seqhi(x, seq));
  538. XFRM_SKB_CB(skb)->seq.input.low = seq;
  539. XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
  540. dev_hold(skb->dev);
  541. if (crypto_done)
  542. nexthdr = x->type_offload->input_tail(x, skb);
  543. else
  544. nexthdr = x->type->input(x, skb);
  545. if (nexthdr == -EINPROGRESS)
  546. return 0;
  547. resume:
  548. dev_put(skb->dev);
  549. spin_lock(&x->lock);
  550. if (nexthdr < 0) {
  551. if (nexthdr == -EBADMSG) {
  552. xfrm_audit_state_icvfail(x, skb,
  553. x->type->proto);
  554. x->stats.integrity_failed++;
  555. }
  556. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  557. goto drop_unlock;
  558. }
  559. /* only the first xfrm gets the encap type */
  560. encap_type = 0;
  561. if (xfrm_replay_recheck(x, skb, seq)) {
  562. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  563. goto drop_unlock;
  564. }
  565. xfrm_replay_advance(x, seq);
  566. x->curlft.bytes += skb->len;
  567. x->curlft.packets++;
  568. spin_unlock(&x->lock);
  569. XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
  570. inner_mode = &x->inner_mode;
  571. if (x->sel.family == AF_UNSPEC) {
  572. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  573. if (inner_mode == NULL) {
  574. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  575. goto drop;
  576. }
  577. }
  578. if (xfrm_inner_mode_input(x, inner_mode, skb)) {
  579. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  580. goto drop;
  581. }
  582. if (x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL) {
  583. decaps = 1;
  584. break;
  585. }
  586. /*
  587. * We need the inner address. However, we only get here for
  588. * transport mode so the outer address is identical.
  589. */
  590. daddr = &x->id.daddr;
  591. family = x->outer_mode.family;
  592. err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
  593. if (err < 0) {
  594. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  595. goto drop;
  596. }
  597. crypto_done = false;
  598. } while (!err);
  599. err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
  600. if (err)
  601. goto drop;
  602. nf_reset_ct(skb);
  603. if (decaps) {
  604. sp = skb_sec_path(skb);
  605. if (sp)
  606. sp->olen = 0;
  607. if (skb_valid_dst(skb))
  608. skb_dst_drop(skb);
  609. gro_cells_receive(&gro_cells, skb);
  610. return 0;
  611. } else {
  612. xo = xfrm_offload(skb);
  613. if (xo)
  614. xfrm_gro = xo->flags & XFRM_GRO;
  615. err = -EAFNOSUPPORT;
  616. rcu_read_lock();
  617. afinfo = xfrm_state_afinfo_get_rcu(x->inner_mode.family);
  618. if (likely(afinfo))
  619. err = afinfo->transport_finish(skb, xfrm_gro || async);
  620. rcu_read_unlock();
  621. if (xfrm_gro) {
  622. sp = skb_sec_path(skb);
  623. if (sp)
  624. sp->olen = 0;
  625. if (skb_valid_dst(skb))
  626. skb_dst_drop(skb);
  627. gro_cells_receive(&gro_cells, skb);
  628. return err;
  629. }
  630. return err;
  631. }
  632. drop_unlock:
  633. spin_unlock(&x->lock);
  634. drop:
  635. xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
  636. kfree_skb(skb);
  637. return 0;
  638. }
  639. EXPORT_SYMBOL(xfrm_input);
  640. int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
  641. {
  642. return xfrm_input(skb, nexthdr, 0, -1);
  643. }
  644. EXPORT_SYMBOL(xfrm_input_resume);
  645. static void xfrm_trans_reinject(struct work_struct *work)
  646. {
  647. struct xfrm_trans_tasklet *trans = container_of(work, struct xfrm_trans_tasklet, work);
  648. struct sk_buff_head queue;
  649. struct sk_buff *skb;
  650. __skb_queue_head_init(&queue);
  651. spin_lock_bh(&trans->queue_lock);
  652. skb_queue_splice_init(&trans->queue, &queue);
  653. spin_unlock_bh(&trans->queue_lock);
  654. local_bh_disable();
  655. while ((skb = __skb_dequeue(&queue)))
  656. XFRM_TRANS_SKB_CB(skb)->finish(XFRM_TRANS_SKB_CB(skb)->net,
  657. NULL, skb);
  658. local_bh_enable();
  659. }
  660. int xfrm_trans_queue_net(struct net *net, struct sk_buff *skb,
  661. int (*finish)(struct net *, struct sock *,
  662. struct sk_buff *))
  663. {
  664. struct xfrm_trans_tasklet *trans;
  665. trans = this_cpu_ptr(&xfrm_trans_tasklet);
  666. if (skb_queue_len(&trans->queue) >= READ_ONCE(netdev_max_backlog))
  667. return -ENOBUFS;
  668. BUILD_BUG_ON(sizeof(struct xfrm_trans_cb) > sizeof(skb->cb));
  669. XFRM_TRANS_SKB_CB(skb)->finish = finish;
  670. XFRM_TRANS_SKB_CB(skb)->net = net;
  671. spin_lock_bh(&trans->queue_lock);
  672. __skb_queue_tail(&trans->queue, skb);
  673. spin_unlock_bh(&trans->queue_lock);
  674. schedule_work(&trans->work);
  675. return 0;
  676. }
  677. EXPORT_SYMBOL(xfrm_trans_queue_net);
  678. int xfrm_trans_queue(struct sk_buff *skb,
  679. int (*finish)(struct net *, struct sock *,
  680. struct sk_buff *))
  681. {
  682. return xfrm_trans_queue_net(dev_net(skb->dev), skb, finish);
  683. }
  684. EXPORT_SYMBOL(xfrm_trans_queue);
  685. void __init xfrm_input_init(void)
  686. {
  687. int err;
  688. int i;
  689. init_dummy_netdev(&xfrm_napi_dev);
  690. err = gro_cells_init(&gro_cells, &xfrm_napi_dev);
  691. if (err)
  692. gro_cells.cells = NULL;
  693. for_each_possible_cpu(i) {
  694. struct xfrm_trans_tasklet *trans;
  695. trans = &per_cpu(xfrm_trans_tasklet, i);
  696. spin_lock_init(&trans->queue_lock);
  697. __skb_queue_head_init(&trans->queue);
  698. INIT_WORK(&trans->work, xfrm_trans_reinject);
  699. }
  700. }