xfrm6_mode_tunnel.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * xfrm6_mode_tunnel.c - Tunnel mode encapsulation for IPv6.
  3. *
  4. * Copyright (C) 2002 USAGI/WIDE Project
  5. * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/stringify.h>
  12. #include <net/dsfield.h>
  13. #include <net/dst.h>
  14. #include <net/inet_ecn.h>
  15. #include <net/ipv6.h>
  16. #include <net/xfrm.h>
  17. static inline void ipip6_ecn_decapsulate(struct sk_buff *skb)
  18. {
  19. struct ipv6hdr *outer_iph = skb->nh.ipv6h;
  20. struct ipv6hdr *inner_iph = skb->h.ipv6h;
  21. if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
  22. IP6_ECN_set_ce(inner_iph);
  23. }
  24. /* Add encapsulation header.
  25. *
  26. * The top IP header will be constructed per RFC 2401. The following fields
  27. * in it shall be filled in by x->type->output:
  28. * payload_len
  29. *
  30. * On exit, skb->h will be set to the start of the encapsulation header to be
  31. * filled in by x->type->output and skb->nh will be set to the nextheader field
  32. * of the extension header directly preceding the encapsulation header, or in
  33. * its absence, that of the top IP header. The value of skb->data will always
  34. * point to the top IP header.
  35. */
  36. static int xfrm6_tunnel_output(struct sk_buff *skb)
  37. {
  38. struct dst_entry *dst = skb->dst;
  39. struct xfrm_state *x = dst->xfrm;
  40. struct ipv6hdr *iph, *top_iph;
  41. int dsfield;
  42. skb_push(skb, x->props.header_len);
  43. iph = skb->nh.ipv6h;
  44. skb->nh.raw = skb->data;
  45. top_iph = skb->nh.ipv6h;
  46. skb->nh.raw = &top_iph->nexthdr;
  47. skb->h.ipv6h = top_iph + 1;
  48. top_iph->version = 6;
  49. top_iph->priority = iph->priority;
  50. top_iph->flow_lbl[0] = iph->flow_lbl[0];
  51. top_iph->flow_lbl[1] = iph->flow_lbl[1];
  52. top_iph->flow_lbl[2] = iph->flow_lbl[2];
  53. dsfield = ipv6_get_dsfield(top_iph);
  54. dsfield = INET_ECN_encapsulate(dsfield, dsfield);
  55. if (x->props.flags & XFRM_STATE_NOECN)
  56. dsfield &= ~INET_ECN_MASK;
  57. ipv6_change_dsfield(top_iph, 0, dsfield);
  58. top_iph->nexthdr = IPPROTO_IPV6;
  59. top_iph->hop_limit = dst_metric(dst->child, RTAX_HOPLIMIT);
  60. ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
  61. ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
  62. return 0;
  63. }
  64. static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  65. {
  66. int err = -EINVAL;
  67. if (skb->nh.raw[IP6CB(skb)->nhoff] != IPPROTO_IPV6)
  68. goto out;
  69. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  70. goto out;
  71. if (skb_cloned(skb) &&
  72. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  73. goto out;
  74. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  75. ipv6_copy_dscp(skb->nh.ipv6h, skb->h.ipv6h);
  76. if (!(x->props.flags & XFRM_STATE_NOECN))
  77. ipip6_ecn_decapsulate(skb);
  78. skb->mac.raw = memmove(skb->data - skb->mac_len,
  79. skb->mac.raw, skb->mac_len);
  80. skb->nh.raw = skb->data;
  81. err = 0;
  82. out:
  83. return err;
  84. }
  85. static struct xfrm_mode xfrm6_tunnel_mode = {
  86. .input = xfrm6_tunnel_input,
  87. .output = xfrm6_tunnel_output,
  88. .owner = THIS_MODULE,
  89. .encap = XFRM_MODE_TUNNEL,
  90. };
  91. static int __init xfrm6_tunnel_init(void)
  92. {
  93. return xfrm_register_mode(&xfrm6_tunnel_mode, AF_INET6);
  94. }
  95. static void __exit xfrm6_tunnel_exit(void)
  96. {
  97. int err;
  98. err = xfrm_unregister_mode(&xfrm6_tunnel_mode, AF_INET6);
  99. BUG_ON(err);
  100. }
  101. module_init(xfrm6_tunnel_init);
  102. module_exit(xfrm6_tunnel_exit);
  103. MODULE_LICENSE("GPL");
  104. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TUNNEL);