xfrm_input.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. #include <net/ip_tunnels.h>
  16. #include <net/ip6_tunnel.h>
  17. static struct kmem_cache *secpath_cachep __read_mostly;
  18. static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
  19. static struct xfrm_input_afinfo __rcu *xfrm_input_afinfo[NPROTO];
  20. int xfrm_input_register_afinfo(struct xfrm_input_afinfo *afinfo)
  21. {
  22. int err = 0;
  23. if (unlikely(afinfo == NULL))
  24. return -EINVAL;
  25. if (unlikely(afinfo->family >= NPROTO))
  26. return -EAFNOSUPPORT;
  27. spin_lock_bh(&xfrm_input_afinfo_lock);
  28. if (unlikely(xfrm_input_afinfo[afinfo->family] != NULL))
  29. err = -EEXIST;
  30. else
  31. rcu_assign_pointer(xfrm_input_afinfo[afinfo->family], afinfo);
  32. spin_unlock_bh(&xfrm_input_afinfo_lock);
  33. return err;
  34. }
  35. EXPORT_SYMBOL(xfrm_input_register_afinfo);
  36. int xfrm_input_unregister_afinfo(struct xfrm_input_afinfo *afinfo)
  37. {
  38. int err = 0;
  39. if (unlikely(afinfo == NULL))
  40. return -EINVAL;
  41. if (unlikely(afinfo->family >= NPROTO))
  42. return -EAFNOSUPPORT;
  43. spin_lock_bh(&xfrm_input_afinfo_lock);
  44. if (likely(xfrm_input_afinfo[afinfo->family] != NULL)) {
  45. if (unlikely(xfrm_input_afinfo[afinfo->family] != afinfo))
  46. err = -EINVAL;
  47. else
  48. RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->family], NULL);
  49. }
  50. spin_unlock_bh(&xfrm_input_afinfo_lock);
  51. synchronize_rcu();
  52. return err;
  53. }
  54. EXPORT_SYMBOL(xfrm_input_unregister_afinfo);
  55. static struct xfrm_input_afinfo *xfrm_input_get_afinfo(unsigned int family)
  56. {
  57. struct xfrm_input_afinfo *afinfo;
  58. if (unlikely(family >= NPROTO))
  59. return NULL;
  60. rcu_read_lock();
  61. afinfo = rcu_dereference(xfrm_input_afinfo[family]);
  62. if (unlikely(!afinfo))
  63. rcu_read_unlock();
  64. return afinfo;
  65. }
  66. static void xfrm_input_put_afinfo(struct xfrm_input_afinfo *afinfo)
  67. {
  68. rcu_read_unlock();
  69. }
  70. static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
  71. int err)
  72. {
  73. int ret;
  74. struct xfrm_input_afinfo *afinfo = xfrm_input_get_afinfo(family);
  75. if (!afinfo)
  76. return -EAFNOSUPPORT;
  77. ret = afinfo->callback(skb, protocol, err);
  78. xfrm_input_put_afinfo(afinfo);
  79. return ret;
  80. }
  81. void __secpath_destroy(struct sec_path *sp)
  82. {
  83. int i;
  84. for (i = 0; i < sp->len; i++)
  85. xfrm_state_put(sp->xvec[i]);
  86. kmem_cache_free(secpath_cachep, sp);
  87. }
  88. EXPORT_SYMBOL(__secpath_destroy);
  89. struct sec_path *secpath_dup(struct sec_path *src)
  90. {
  91. struct sec_path *sp;
  92. sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
  93. if (!sp)
  94. return NULL;
  95. sp->len = 0;
  96. if (src) {
  97. int i;
  98. memcpy(sp, src, sizeof(*sp));
  99. for (i = 0; i < sp->len; i++)
  100. xfrm_state_hold(sp->xvec[i]);
  101. }
  102. atomic_set(&sp->refcnt, 1);
  103. return sp;
  104. }
  105. EXPORT_SYMBOL(secpath_dup);
  106. /* Fetch spi and seq from ipsec header */
  107. int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
  108. {
  109. int offset, offset_seq;
  110. int hlen;
  111. switch (nexthdr) {
  112. case IPPROTO_AH:
  113. hlen = sizeof(struct ip_auth_hdr);
  114. offset = offsetof(struct ip_auth_hdr, spi);
  115. offset_seq = offsetof(struct ip_auth_hdr, seq_no);
  116. break;
  117. case IPPROTO_ESP:
  118. hlen = sizeof(struct ip_esp_hdr);
  119. offset = offsetof(struct ip_esp_hdr, spi);
  120. offset_seq = offsetof(struct ip_esp_hdr, seq_no);
  121. break;
  122. case IPPROTO_COMP:
  123. if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
  124. return -EINVAL;
  125. *spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
  126. *seq = 0;
  127. return 0;
  128. default:
  129. return 1;
  130. }
  131. if (!pskb_may_pull(skb, hlen))
  132. return -EINVAL;
  133. *spi = *(__be32 *)(skb_transport_header(skb) + offset);
  134. *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
  135. return 0;
  136. }
  137. int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
  138. {
  139. struct xfrm_mode *inner_mode = x->inner_mode;
  140. int err;
  141. err = x->outer_mode->afinfo->extract_input(x, skb);
  142. if (err)
  143. return err;
  144. if (x->sel.family == AF_UNSPEC) {
  145. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  146. if (inner_mode == NULL)
  147. return -EAFNOSUPPORT;
  148. }
  149. skb->protocol = inner_mode->afinfo->eth_proto;
  150. return inner_mode->input2(x, skb);
  151. }
  152. EXPORT_SYMBOL(xfrm_prepare_input);
  153. int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
  154. {
  155. struct net *net = dev_net(skb->dev);
  156. int err;
  157. __be32 seq;
  158. __be32 seq_hi;
  159. struct xfrm_state *x = NULL;
  160. xfrm_address_t *daddr;
  161. struct xfrm_mode *inner_mode;
  162. u32 mark = skb->mark;
  163. unsigned int family;
  164. int decaps = 0;
  165. int async = 0;
  166. /* A negative encap_type indicates async resumption. */
  167. if (encap_type < 0) {
  168. async = 1;
  169. x = xfrm_input_state(skb);
  170. seq = XFRM_SKB_CB(skb)->seq.input.low;
  171. family = x->outer_mode->afinfo->family;
  172. goto resume;
  173. }
  174. daddr = (xfrm_address_t *)(skb_network_header(skb) +
  175. XFRM_SPI_SKB_CB(skb)->daddroff);
  176. family = XFRM_SPI_SKB_CB(skb)->family;
  177. /* if tunnel is present override skb->mark value with tunnel i_key */
  178. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4) {
  179. switch (family) {
  180. case AF_INET:
  181. mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
  182. break;
  183. case AF_INET6:
  184. mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
  185. break;
  186. }
  187. }
  188. /* Allocate new secpath or COW existing one. */
  189. if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
  190. struct sec_path *sp;
  191. sp = secpath_dup(skb->sp);
  192. if (!sp) {
  193. XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
  194. goto drop;
  195. }
  196. if (skb->sp)
  197. secpath_put(skb->sp);
  198. skb->sp = sp;
  199. }
  200. seq = 0;
  201. if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
  202. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  203. goto drop;
  204. }
  205. do {
  206. if (skb->sp->len == XFRM_MAX_DEPTH) {
  207. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  208. goto drop;
  209. }
  210. x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
  211. if (x == NULL) {
  212. XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
  213. xfrm_audit_state_notfound(skb, family, spi, seq);
  214. goto drop;
  215. }
  216. skb->sp->xvec[skb->sp->len++] = x;
  217. spin_lock(&x->lock);
  218. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  219. if (x->km.state == XFRM_STATE_ACQ)
  220. XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
  221. else
  222. XFRM_INC_STATS(net,
  223. LINUX_MIB_XFRMINSTATEINVALID);
  224. goto drop_unlock;
  225. }
  226. if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
  227. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
  228. goto drop_unlock;
  229. }
  230. if (x->repl->check(x, skb, seq)) {
  231. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  232. goto drop_unlock;
  233. }
  234. if (xfrm_state_check_expire(x)) {
  235. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
  236. goto drop_unlock;
  237. }
  238. spin_unlock(&x->lock);
  239. if (xfrm_tunnel_check(skb, x, family)) {
  240. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  241. goto drop;
  242. }
  243. seq_hi = htonl(xfrm_replay_seqhi(x, seq));
  244. XFRM_SKB_CB(skb)->seq.input.low = seq;
  245. XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
  246. skb_dst_force(skb);
  247. dev_hold(skb->dev);
  248. nexthdr = x->type->input(x, skb);
  249. if (nexthdr == -EINPROGRESS)
  250. return 0;
  251. resume:
  252. dev_put(skb->dev);
  253. spin_lock(&x->lock);
  254. if (nexthdr <= 0) {
  255. if (nexthdr == -EBADMSG) {
  256. xfrm_audit_state_icvfail(x, skb,
  257. x->type->proto);
  258. x->stats.integrity_failed++;
  259. }
  260. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  261. goto drop_unlock;
  262. }
  263. /* only the first xfrm gets the encap type */
  264. encap_type = 0;
  265. if (async && x->repl->recheck(x, skb, seq)) {
  266. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  267. goto drop_unlock;
  268. }
  269. x->repl->advance(x, seq);
  270. x->curlft.bytes += skb->len;
  271. x->curlft.packets++;
  272. spin_unlock(&x->lock);
  273. XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
  274. inner_mode = x->inner_mode;
  275. if (x->sel.family == AF_UNSPEC) {
  276. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  277. if (inner_mode == NULL) {
  278. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  279. goto drop;
  280. }
  281. }
  282. if (inner_mode->input(x, skb)) {
  283. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  284. goto drop;
  285. }
  286. if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
  287. decaps = 1;
  288. break;
  289. }
  290. /*
  291. * We need the inner address. However, we only get here for
  292. * transport mode so the outer address is identical.
  293. */
  294. daddr = &x->id.daddr;
  295. family = x->outer_mode->afinfo->family;
  296. err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
  297. if (err < 0) {
  298. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  299. goto drop;
  300. }
  301. } while (!err);
  302. err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
  303. if (err)
  304. goto drop;
  305. nf_reset(skb);
  306. if (decaps) {
  307. skb_dst_drop(skb);
  308. netif_rx(skb);
  309. return 0;
  310. } else {
  311. return x->inner_mode->afinfo->transport_finish(skb, async);
  312. }
  313. drop_unlock:
  314. spin_unlock(&x->lock);
  315. drop:
  316. xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
  317. kfree_skb(skb);
  318. return 0;
  319. }
  320. EXPORT_SYMBOL(xfrm_input);
  321. int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
  322. {
  323. return xfrm_input(skb, nexthdr, 0, -1);
  324. }
  325. EXPORT_SYMBOL(xfrm_input_resume);
  326. void __init xfrm_input_init(void)
  327. {
  328. secpath_cachep = kmem_cache_create("secpath_cache",
  329. sizeof(struct sec_path),
  330. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  331. NULL);
  332. }