xfrm6_mode_tunnel.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 = ipv6_hdr(skb);
  20. struct ipv6hdr *inner_iph = ipipv6_hdr(skb);
  21. if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
  22. IP6_ECN_set_ce(inner_iph);
  23. }
  24. static inline void ip6ip_ecn_decapsulate(struct sk_buff *skb)
  25. {
  26. if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6_hdr(skb))))
  27. IP_ECN_set_ce(ipip_hdr(skb));
  28. }
  29. /* Add encapsulation header.
  30. *
  31. * The top IP header will be constructed per RFC 2401.
  32. */
  33. static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
  34. {
  35. struct dst_entry *dst = skb->dst;
  36. struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
  37. struct ipv6hdr *iph, *top_iph;
  38. int dsfield;
  39. iph = ipv6_hdr(skb);
  40. skb_set_network_header(skb, -x->props.header_len);
  41. skb->mac_header = skb->network_header +
  42. offsetof(struct ipv6hdr, nexthdr);
  43. skb->transport_header = skb->network_header + sizeof(*iph);
  44. top_iph = ipv6_hdr(skb);
  45. top_iph->version = 6;
  46. if (xdst->route->ops->family == AF_INET6) {
  47. top_iph->priority = iph->priority;
  48. top_iph->flow_lbl[0] = iph->flow_lbl[0];
  49. top_iph->flow_lbl[1] = iph->flow_lbl[1];
  50. top_iph->flow_lbl[2] = iph->flow_lbl[2];
  51. top_iph->nexthdr = IPPROTO_IPV6;
  52. } else {
  53. top_iph->priority = 0;
  54. top_iph->flow_lbl[0] = 0;
  55. top_iph->flow_lbl[1] = 0;
  56. top_iph->flow_lbl[2] = 0;
  57. top_iph->nexthdr = IPPROTO_IPIP;
  58. }
  59. dsfield = ipv6_get_dsfield(top_iph);
  60. dsfield = INET_ECN_encapsulate(dsfield, dsfield);
  61. if (x->props.flags & XFRM_STATE_NOECN)
  62. dsfield &= ~INET_ECN_MASK;
  63. ipv6_change_dsfield(top_iph, 0, dsfield);
  64. top_iph->hop_limit = dst_metric(dst->child, RTAX_HOPLIMIT);
  65. ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
  66. ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
  67. skb->protocol = htons(ETH_P_IPV6);
  68. return 0;
  69. }
  70. static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  71. {
  72. int err = -EINVAL;
  73. const unsigned char *old_mac;
  74. const unsigned char *nh = skb_network_header(skb);
  75. if (nh[IP6CB(skb)->nhoff] != IPPROTO_IPV6 &&
  76. nh[IP6CB(skb)->nhoff] != IPPROTO_IPIP)
  77. goto out;
  78. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  79. goto out;
  80. if (skb_cloned(skb) &&
  81. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  82. goto out;
  83. nh = skb_network_header(skb);
  84. if (nh[IP6CB(skb)->nhoff] == IPPROTO_IPV6) {
  85. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  86. ipv6_copy_dscp(ipv6_hdr(skb), ipipv6_hdr(skb));
  87. if (!(x->props.flags & XFRM_STATE_NOECN))
  88. ipip6_ecn_decapsulate(skb);
  89. } else {
  90. if (!(x->props.flags & XFRM_STATE_NOECN))
  91. ip6ip_ecn_decapsulate(skb);
  92. skb->protocol = htons(ETH_P_IP);
  93. }
  94. old_mac = skb_mac_header(skb);
  95. skb_set_mac_header(skb, -skb->mac_len);
  96. memmove(skb_mac_header(skb), old_mac, skb->mac_len);
  97. skb_reset_network_header(skb);
  98. err = 0;
  99. out:
  100. return err;
  101. }
  102. static struct xfrm_mode xfrm6_tunnel_mode = {
  103. .input = xfrm6_tunnel_input,
  104. .output = xfrm6_tunnel_output,
  105. .owner = THIS_MODULE,
  106. .encap = XFRM_MODE_TUNNEL,
  107. .flags = XFRM_MODE_FLAG_TUNNEL,
  108. };
  109. static int __init xfrm6_tunnel_init(void)
  110. {
  111. return xfrm_register_mode(&xfrm6_tunnel_mode, AF_INET6);
  112. }
  113. static void __exit xfrm6_tunnel_exit(void)
  114. {
  115. int err;
  116. err = xfrm_unregister_mode(&xfrm6_tunnel_mode, AF_INET6);
  117. BUG_ON(err);
  118. }
  119. module_init(xfrm6_tunnel_init);
  120. module_exit(xfrm6_tunnel_exit);
  121. MODULE_LICENSE("GPL");
  122. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TUNNEL);