xfrm_input.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. switch (family) {
  179. case AF_INET:
  180. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
  181. mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
  182. break;
  183. case AF_INET6:
  184. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
  185. mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
  186. break;
  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. secpath_reset(skb);
  203. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  204. goto drop;
  205. }
  206. do {
  207. if (skb->sp->len == XFRM_MAX_DEPTH) {
  208. secpath_reset(skb);
  209. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  210. goto drop;
  211. }
  212. x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
  213. if (x == NULL) {
  214. secpath_reset(skb);
  215. XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
  216. xfrm_audit_state_notfound(skb, family, spi, seq);
  217. goto drop;
  218. }
  219. skb->mark = xfrm_smark_get(skb->mark, x);
  220. skb->sp->xvec[skb->sp->len++] = x;
  221. spin_lock(&x->lock);
  222. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  223. if (x->km.state == XFRM_STATE_ACQ)
  224. XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
  225. else
  226. XFRM_INC_STATS(net,
  227. LINUX_MIB_XFRMINSTATEINVALID);
  228. goto drop_unlock;
  229. }
  230. if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
  231. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
  232. goto drop_unlock;
  233. }
  234. if (x->repl->check(x, skb, seq)) {
  235. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  236. goto drop_unlock;
  237. }
  238. if (xfrm_state_check_expire(x)) {
  239. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
  240. goto drop_unlock;
  241. }
  242. spin_unlock(&x->lock);
  243. if (xfrm_tunnel_check(skb, x, family)) {
  244. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  245. goto drop;
  246. }
  247. seq_hi = htonl(xfrm_replay_seqhi(x, seq));
  248. XFRM_SKB_CB(skb)->seq.input.low = seq;
  249. XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
  250. skb_dst_force(skb);
  251. dev_hold(skb->dev);
  252. nexthdr = x->type->input(x, skb);
  253. if (nexthdr == -EINPROGRESS)
  254. return 0;
  255. resume:
  256. dev_put(skb->dev);
  257. spin_lock(&x->lock);
  258. if (nexthdr <= 0) {
  259. if (nexthdr == -EBADMSG) {
  260. xfrm_audit_state_icvfail(x, skb,
  261. x->type->proto);
  262. x->stats.integrity_failed++;
  263. }
  264. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
  265. goto drop_unlock;
  266. }
  267. /* only the first xfrm gets the encap type */
  268. encap_type = 0;
  269. if (async && x->repl->recheck(x, skb, seq)) {
  270. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
  271. goto drop_unlock;
  272. }
  273. x->repl->advance(x, seq);
  274. x->curlft.bytes += skb->len;
  275. x->curlft.packets++;
  276. spin_unlock(&x->lock);
  277. XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
  278. inner_mode = x->inner_mode;
  279. if (x->sel.family == AF_UNSPEC) {
  280. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  281. if (inner_mode == NULL) {
  282. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  283. goto drop;
  284. }
  285. }
  286. if (inner_mode->input(x, skb)) {
  287. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
  288. goto drop;
  289. }
  290. if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
  291. decaps = 1;
  292. break;
  293. }
  294. /*
  295. * We need the inner address. However, we only get here for
  296. * transport mode so the outer address is identical.
  297. */
  298. daddr = &x->id.daddr;
  299. family = x->outer_mode->afinfo->family;
  300. err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
  301. if (err < 0) {
  302. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  303. goto drop;
  304. }
  305. } while (!err);
  306. err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
  307. if (err)
  308. goto drop;
  309. nf_reset(skb);
  310. if (decaps) {
  311. skb_dst_drop(skb);
  312. netif_rx(skb);
  313. return 0;
  314. } else {
  315. return x->inner_mode->afinfo->transport_finish(skb, async);
  316. }
  317. drop_unlock:
  318. spin_unlock(&x->lock);
  319. drop:
  320. xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
  321. kfree_skb(skb);
  322. return 0;
  323. }
  324. EXPORT_SYMBOL(xfrm_input);
  325. int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
  326. {
  327. return xfrm_input(skb, nexthdr, 0, -1);
  328. }
  329. EXPORT_SYMBOL(xfrm_input_resume);
  330. void __init xfrm_input_init(void)
  331. {
  332. secpath_cachep = kmem_cache_create("secpath_cache",
  333. sizeof(struct sec_path),
  334. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  335. NULL);
  336. }