xfrm_input.c 6.2 KB

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