xfrm4_mode_tunnel.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * xfrm4_mode_tunnel.c - Tunnel mode encapsulation for IPv4.
  3. *
  4. * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/stringify.h>
  11. #include <net/dst.h>
  12. #include <net/inet_ecn.h>
  13. #include <net/ip.h>
  14. #include <net/xfrm.h>
  15. static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
  16. {
  17. struct iphdr *outer_iph = skb->nh.iph;
  18. struct iphdr *inner_iph = skb->h.ipiph;
  19. if (INET_ECN_is_ce(outer_iph->tos))
  20. IP_ECN_set_ce(inner_iph);
  21. }
  22. /* Add encapsulation header.
  23. *
  24. * The top IP header will be constructed per RFC 2401. The following fields
  25. * in it shall be filled in by x->type->output:
  26. * tot_len
  27. * check
  28. *
  29. * On exit, skb->h will be set to the start of the payload to be processed
  30. * by x->type->output and skb->nh will be set to the top IP header.
  31. */
  32. static int xfrm4_tunnel_output(struct sk_buff *skb)
  33. {
  34. struct dst_entry *dst = skb->dst;
  35. struct xfrm_state *x = dst->xfrm;
  36. struct iphdr *iph, *top_iph;
  37. int flags;
  38. iph = skb->nh.iph;
  39. skb->h.ipiph = iph;
  40. skb->nh.raw = skb_push(skb, x->props.header_len);
  41. top_iph = skb->nh.iph;
  42. top_iph->ihl = 5;
  43. top_iph->version = 4;
  44. /* DS disclosed */
  45. top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos);
  46. flags = x->props.flags;
  47. if (flags & XFRM_STATE_NOECN)
  48. IP_ECN_clear(top_iph);
  49. top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
  50. 0 : (iph->frag_off & htons(IP_DF));
  51. if (!top_iph->frag_off)
  52. __ip_select_ident(top_iph, dst->child, 0);
  53. top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
  54. top_iph->saddr = x->props.saddr.a4;
  55. top_iph->daddr = x->id.daddr.a4;
  56. top_iph->protocol = IPPROTO_IPIP;
  57. memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  58. return 0;
  59. }
  60. static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  61. {
  62. struct iphdr *iph = skb->nh.iph;
  63. int err = -EINVAL;
  64. if (iph->protocol != IPPROTO_IPIP)
  65. goto out;
  66. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  67. goto out;
  68. if (skb_cloned(skb) &&
  69. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  70. goto out;
  71. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  72. ipv4_copy_dscp(iph, skb->h.ipiph);
  73. if (!(x->props.flags & XFRM_STATE_NOECN))
  74. ipip_ecn_decapsulate(skb);
  75. skb->mac.raw = memmove(skb->data - skb->mac_len,
  76. skb->mac.raw, skb->mac_len);
  77. skb->nh.raw = skb->data;
  78. err = 0;
  79. out:
  80. return err;
  81. }
  82. static struct xfrm_mode xfrm4_tunnel_mode = {
  83. .input = xfrm4_tunnel_input,
  84. .output = xfrm4_tunnel_output,
  85. .owner = THIS_MODULE,
  86. .encap = XFRM_MODE_TUNNEL,
  87. };
  88. static int __init xfrm4_tunnel_init(void)
  89. {
  90. return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
  91. }
  92. static void __exit xfrm4_tunnel_exit(void)
  93. {
  94. int err;
  95. err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET);
  96. BUG_ON(err);
  97. }
  98. module_init(xfrm4_tunnel_init);
  99. module_exit(xfrm4_tunnel_exit);
  100. MODULE_LICENSE("GPL");
  101. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);