xfrm_input.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. struct net *net = dev_net(skb->dev);
  91. int err;
  92. __be32 seq;
  93. struct xfrm_state *x;
  94. xfrm_address_t *daddr;
  95. struct xfrm_mode *inner_mode;
  96. unsigned int family;
  97. int decaps = 0;
  98. int async = 0;
  99. /* A negative encap_type indicates async resumption. */
  100. if (encap_type < 0) {
  101. async = 1;
  102. x = xfrm_input_state(skb);
  103. seq = XFRM_SKB_CB(skb)->seq.input.low;
  104. goto resume;
  105. }
  106. /* Allocate new secpath or COW existing one. */
  107. if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
  108. struct sec_path *sp;
  109. sp = secpath_dup(skb->sp);
  110. if (!sp) {
  111. XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
  112. goto drop;
  113. }
  114. if (skb->sp)
  115. secpath_put(skb->sp);
  116. skb->sp = sp;
  117. }
  118. daddr = (xfrm_address_t *)(skb_network_header(skb) +
  119. XFRM_SPI_SKB_CB(skb)->daddroff);
  120. family = XFRM_SPI_SKB_CB(skb)->family;
  121. seq = 0;
  122. if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
  123. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  124. goto drop;
  125. }
  126. do {
  127. if (skb->sp->len == XFRM_MAX_DEPTH) {
  128. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  129. goto drop;
  130. }
  131. x = xfrm_state_lookup(net, skb->mark, daddr, spi, nexthdr, family);
  132. if (x == NULL) {
  133. XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
  134. xfrm_audit_state_notfound(skb, family, spi, seq);
  135. goto drop;
  136. }
  137. skb->sp->xvec[skb->sp->len++] = x;
  138. spin_lock(&x->lock);
  139. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  140. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEINVALID);
  141. goto drop_unlock;
  142. }
  143. if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
  144. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
  145. goto drop_unlock;
  146. }
  147. if (x->props.replay_window && x->repl->check(x, skb, seq)) {
  148. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  149. goto drop_unlock;
  150. }
  151. if (xfrm_state_check_expire(x)) {
  152. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
  153. goto drop_unlock;
  154. }
  155. spin_unlock(&x->lock);
  156. XFRM_SKB_CB(skb)->seq.input.low = seq;
  157. nexthdr = x->type->input(x, skb);
  158. if (nexthdr == -EINPROGRESS)
  159. return 0;
  160. resume:
  161. spin_lock(&x->lock);
  162. if (nexthdr <= 0) {
  163. if (nexthdr == -EBADMSG) {
  164. xfrm_audit_state_icvfail(x, skb,
  165. x->type->proto);
  166. x->stats.integrity_failed++;
  167. }
  168. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  169. goto drop_unlock;
  170. }
  171. /* only the first xfrm gets the encap type */
  172. encap_type = 0;
  173. x->repl->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(net, 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(net, LINUX_MIB_XFRMINHDRERROR);
  201. goto drop;
  202. }
  203. } while (!err);
  204. nf_reset(skb);
  205. if (decaps) {
  206. skb_dst_drop(skb);
  207. netif_rx(skb);
  208. return 0;
  209. } else {
  210. return x->inner_mode->afinfo->transport_finish(skb, async);
  211. }
  212. drop_unlock:
  213. spin_unlock(&x->lock);
  214. drop:
  215. kfree_skb(skb);
  216. return 0;
  217. }
  218. EXPORT_SYMBOL(xfrm_input);
  219. int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
  220. {
  221. return xfrm_input(skb, nexthdr, 0, -1);
  222. }
  223. EXPORT_SYMBOL(xfrm_input_resume);
  224. void __init xfrm_input_init(void)
  225. {
  226. secpath_cachep = kmem_cache_create("secpath_cache",
  227. sizeof(struct sec_path),
  228. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  229. NULL);
  230. }