xfrm_input.c 5.9 KB

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