xfrm_input.c 11 KB

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