xfrm_input.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
  73. {
  74. int err;
  75. err = x->outer_mode->afinfo->extract_input(x, skb);
  76. if (err)
  77. return err;
  78. skb->protocol = x->inner_mode->afinfo->eth_proto;
  79. return x->inner_mode->input2(x, skb);
  80. }
  81. EXPORT_SYMBOL(xfrm_prepare_input);
  82. int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
  83. {
  84. int err;
  85. __be32 seq;
  86. struct xfrm_state *x;
  87. xfrm_address_t *daddr;
  88. unsigned int family;
  89. int decaps = 0;
  90. int async = 0;
  91. /* A negative encap_type indicates async resumption. */
  92. if (encap_type < 0) {
  93. async = 1;
  94. x = xfrm_input_state(skb);
  95. seq = XFRM_SKB_CB(skb)->seq.input;
  96. goto resume;
  97. }
  98. /* Allocate new secpath or COW existing one. */
  99. if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
  100. struct sec_path *sp;
  101. sp = secpath_dup(skb->sp);
  102. if (!sp) {
  103. XFRM_INC_STATS(LINUX_MIB_XFRMINERROR);
  104. goto drop;
  105. }
  106. if (skb->sp)
  107. secpath_put(skb->sp);
  108. skb->sp = sp;
  109. }
  110. daddr = (xfrm_address_t *)(skb_network_header(skb) +
  111. XFRM_SPI_SKB_CB(skb)->daddroff);
  112. family = XFRM_SPI_SKB_CB(skb)->family;
  113. seq = 0;
  114. if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
  115. XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
  116. goto drop;
  117. }
  118. do {
  119. if (skb->sp->len == XFRM_MAX_DEPTH) {
  120. XFRM_INC_STATS(LINUX_MIB_XFRMINBUFFERERROR);
  121. goto drop;
  122. }
  123. x = xfrm_state_lookup(daddr, spi, nexthdr, family);
  124. if (x == NULL) {
  125. XFRM_INC_STATS(LINUX_MIB_XFRMINNOSTATES);
  126. xfrm_audit_state_notfound(skb, family, spi, seq);
  127. goto drop;
  128. }
  129. skb->sp->xvec[skb->sp->len++] = x;
  130. spin_lock(&x->lock);
  131. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  132. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEINVALID);
  133. goto drop_unlock;
  134. }
  135. if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
  136. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEMISMATCH);
  137. goto drop_unlock;
  138. }
  139. if (x->props.replay_window && xfrm_replay_check(x, skb, seq)) {
  140. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATESEQERROR);
  141. goto drop_unlock;
  142. }
  143. if (xfrm_state_check_expire(x)) {
  144. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEEXPIRED);
  145. goto drop_unlock;
  146. }
  147. spin_unlock(&x->lock);
  148. XFRM_SKB_CB(skb)->seq.input = seq;
  149. nexthdr = x->type->input(x, skb);
  150. if (nexthdr == -EINPROGRESS)
  151. return 0;
  152. resume:
  153. spin_lock(&x->lock);
  154. if (nexthdr <= 0) {
  155. if (nexthdr == -EBADMSG) {
  156. xfrm_audit_state_icvfail(x, skb,
  157. x->type->proto);
  158. x->stats.integrity_failed++;
  159. }
  160. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEPROTOERROR);
  161. goto drop_unlock;
  162. }
  163. /* only the first xfrm gets the encap type */
  164. encap_type = 0;
  165. if (x->props.replay_window)
  166. xfrm_replay_advance(x, seq);
  167. x->curlft.bytes += skb->len;
  168. x->curlft.packets++;
  169. spin_unlock(&x->lock);
  170. XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
  171. if (x->inner_mode->input(x, skb)) {
  172. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEMODEERROR);
  173. goto drop;
  174. }
  175. if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
  176. decaps = 1;
  177. break;
  178. }
  179. /*
  180. * We need the inner address. However, we only get here for
  181. * transport mode so the outer address is identical.
  182. */
  183. daddr = &x->id.daddr;
  184. family = x->outer_mode->afinfo->family;
  185. err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
  186. if (err < 0) {
  187. XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
  188. goto drop;
  189. }
  190. } while (!err);
  191. nf_reset(skb);
  192. if (decaps) {
  193. dst_release(skb->dst);
  194. skb->dst = NULL;
  195. netif_rx(skb);
  196. return 0;
  197. } else {
  198. return x->inner_mode->afinfo->transport_finish(skb, async);
  199. }
  200. drop_unlock:
  201. spin_unlock(&x->lock);
  202. drop:
  203. kfree_skb(skb);
  204. return 0;
  205. }
  206. EXPORT_SYMBOL(xfrm_input);
  207. int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
  208. {
  209. return xfrm_input(skb, nexthdr, 0, -1);
  210. }
  211. EXPORT_SYMBOL(xfrm_input_resume);
  212. void __init xfrm_input_init(void)
  213. {
  214. secpath_cachep = kmem_cache_create("secpath_cache",
  215. sizeof(struct sec_path),
  216. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  217. NULL);
  218. }