xfrm_input.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. static struct kmem_cache *secpath_cachep __read_mostly;
  16. void __secpath_destroy(struct sec_path *sp)
  17. {
  18. int i;
  19. for (i = 0; i < sp->len; i++)
  20. xfrm_state_put(sp->xvec[i]);
  21. kmem_cache_free(secpath_cachep, sp);
  22. }
  23. EXPORT_SYMBOL(__secpath_destroy);
  24. struct sec_path *secpath_dup(struct sec_path *src)
  25. {
  26. struct sec_path *sp;
  27. sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
  28. if (!sp)
  29. return NULL;
  30. sp->len = 0;
  31. if (src) {
  32. int i;
  33. memcpy(sp, src, sizeof(*sp));
  34. for (i = 0; i < sp->len; i++)
  35. xfrm_state_hold(sp->xvec[i]);
  36. }
  37. atomic_set(&sp->refcnt, 1);
  38. return sp;
  39. }
  40. EXPORT_SYMBOL(secpath_dup);
  41. /* Fetch spi and seq from ipsec header */
  42. int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
  43. {
  44. int offset, offset_seq;
  45. int hlen;
  46. switch (nexthdr) {
  47. case IPPROTO_AH:
  48. hlen = sizeof(struct ip_auth_hdr);
  49. offset = offsetof(struct ip_auth_hdr, spi);
  50. offset_seq = offsetof(struct ip_auth_hdr, seq_no);
  51. break;
  52. case IPPROTO_ESP:
  53. hlen = sizeof(struct ip_esp_hdr);
  54. offset = offsetof(struct ip_esp_hdr, spi);
  55. offset_seq = offsetof(struct ip_esp_hdr, seq_no);
  56. break;
  57. case IPPROTO_COMP:
  58. if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
  59. return -EINVAL;
  60. *spi = htonl(ntohs(*(__be16*)(skb_transport_header(skb) + 2)));
  61. *seq = 0;
  62. return 0;
  63. default:
  64. return 1;
  65. }
  66. if (!pskb_may_pull(skb, hlen))
  67. return -EINVAL;
  68. *spi = *(__be32*)(skb_transport_header(skb) + offset);
  69. *seq = *(__be32*)(skb_transport_header(skb) + offset_seq);
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(xfrm_parse_spi);
  73. int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
  74. {
  75. int err;
  76. err = x->outer_mode->afinfo->extract_input(x, skb);
  77. if (err)
  78. return err;
  79. skb->protocol = x->inner_mode->afinfo->eth_proto;
  80. return x->inner_mode->input2(x, skb);
  81. }
  82. EXPORT_SYMBOL(xfrm_prepare_input);
  83. int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
  84. {
  85. int err;
  86. __be32 seq;
  87. struct xfrm_state *x;
  88. xfrm_address_t *daddr;
  89. unsigned int family;
  90. int decaps = 0;
  91. int async = 0;
  92. /* A negative encap_type indicates async resumption. */
  93. if (encap_type < 0) {
  94. async = 1;
  95. x = xfrm_input_state(skb);
  96. seq = XFRM_SKB_CB(skb)->seq;
  97. goto resume;
  98. }
  99. /* Allocate new secpath or COW existing one. */
  100. if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
  101. struct sec_path *sp;
  102. sp = secpath_dup(skb->sp);
  103. if (!sp) {
  104. XFRM_INC_STATS(LINUX_MIB_XFRMINERROR);
  105. goto drop;
  106. }
  107. if (skb->sp)
  108. secpath_put(skb->sp);
  109. skb->sp = sp;
  110. }
  111. daddr = (xfrm_address_t *)(skb_network_header(skb) +
  112. XFRM_SPI_SKB_CB(skb)->daddroff);
  113. family = XFRM_SPI_SKB_CB(skb)->family;
  114. seq = 0;
  115. if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
  116. XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
  117. goto drop;
  118. }
  119. do {
  120. if (skb->sp->len == XFRM_MAX_DEPTH) {
  121. XFRM_INC_STATS(LINUX_MIB_XFRMINBUFFERERROR);
  122. goto drop;
  123. }
  124. x = xfrm_state_lookup(daddr, spi, nexthdr, family);
  125. if (x == NULL) {
  126. XFRM_INC_STATS(LINUX_MIB_XFRMINNOSTATES);
  127. xfrm_audit_state_notfound(skb, family, spi, seq);
  128. goto drop;
  129. }
  130. skb->sp->xvec[skb->sp->len++] = x;
  131. spin_lock(&x->lock);
  132. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  133. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEINVALID);
  134. goto drop_unlock;
  135. }
  136. if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
  137. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEINVALID);
  138. goto drop_unlock;
  139. }
  140. if (x->props.replay_window && xfrm_replay_check(x, skb, seq)) {
  141. XFRM_INC_STATS(LINUX_MIB_XFRMINSEQOUTOFWINDOW);
  142. goto drop_unlock;
  143. }
  144. if (xfrm_state_check_expire(x)) {
  145. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEEXPIRED);
  146. goto drop_unlock;
  147. }
  148. spin_unlock(&x->lock);
  149. XFRM_SKB_CB(skb)->seq = seq;
  150. nexthdr = x->type->input(x, skb);
  151. if (nexthdr == -EINPROGRESS)
  152. return 0;
  153. resume:
  154. spin_lock(&x->lock);
  155. if (nexthdr <= 0) {
  156. if (nexthdr == -EBADMSG) {
  157. xfrm_audit_state_icvfail(x, skb,
  158. x->type->proto);
  159. x->stats.integrity_failed++;
  160. }
  161. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEPROTOERROR);
  162. goto drop_unlock;
  163. }
  164. /* only the first xfrm gets the encap type */
  165. encap_type = 0;
  166. if (x->props.replay_window)
  167. xfrm_replay_advance(x, seq);
  168. x->curlft.bytes += skb->len;
  169. x->curlft.packets++;
  170. spin_unlock(&x->lock);
  171. XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
  172. if (x->inner_mode->input(x, skb)) {
  173. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEMODEERROR);
  174. goto drop;
  175. }
  176. if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
  177. decaps = 1;
  178. break;
  179. }
  180. /*
  181. * We need the inner address. However, we only get here for
  182. * transport mode so the outer address is identical.
  183. */
  184. daddr = &x->id.daddr;
  185. family = x->outer_mode->afinfo->family;
  186. err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
  187. if (err < 0) {
  188. XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
  189. goto drop;
  190. }
  191. } while (!err);
  192. nf_reset(skb);
  193. if (decaps) {
  194. dst_release(skb->dst);
  195. skb->dst = NULL;
  196. netif_rx(skb);
  197. return 0;
  198. } else {
  199. return x->inner_mode->afinfo->transport_finish(skb, async);
  200. }
  201. drop_unlock:
  202. spin_unlock(&x->lock);
  203. drop:
  204. kfree_skb(skb);
  205. return 0;
  206. }
  207. EXPORT_SYMBOL(xfrm_input);
  208. int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
  209. {
  210. return xfrm_input(skb, nexthdr, 0, -1);
  211. }
  212. EXPORT_SYMBOL(xfrm_input_resume);
  213. void __init xfrm_input_init(void)
  214. {
  215. secpath_cachep = kmem_cache_create("secpath_cache",
  216. sizeof(struct sec_path),
  217. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  218. NULL);
  219. }