xfrm6_mode_tunnel.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. The following fields
  32. * in it shall be filled in by x->type->output:
  33. * payload_len
  34. *
  35. * On exit, skb->h will be set to the start of the encapsulation header to be
  36. * filled in by x->type->output and the mac header will be set to the
  37. * nextheader field of the extension header directly preceding the
  38. * encapsulation header, or in its absence, that of the top IP header.
  39. * The value of skb->data and the network header will always point to the
  40. * top IP header.
  41. */
  42. static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
  43. {
  44. struct dst_entry *dst = skb->dst;
  45. struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
  46. struct ipv6hdr *iph, *top_iph;
  47. int dsfield;
  48. skb_push(skb, x->props.header_len);
  49. iph = ipv6_hdr(skb);
  50. skb_set_mac_header(skb, offsetof(struct ipv6hdr, nexthdr));
  51. skb_reset_network_header(skb);
  52. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  53. top_iph = ipv6_hdr(skb);
  54. top_iph->version = 6;
  55. if (xdst->route->ops->family == AF_INET6) {
  56. top_iph->priority = iph->priority;
  57. top_iph->flow_lbl[0] = iph->flow_lbl[0];
  58. top_iph->flow_lbl[1] = iph->flow_lbl[1];
  59. top_iph->flow_lbl[2] = iph->flow_lbl[2];
  60. top_iph->nexthdr = IPPROTO_IPV6;
  61. } else {
  62. top_iph->priority = 0;
  63. top_iph->flow_lbl[0] = 0;
  64. top_iph->flow_lbl[1] = 0;
  65. top_iph->flow_lbl[2] = 0;
  66. top_iph->nexthdr = IPPROTO_IPIP;
  67. }
  68. dsfield = ipv6_get_dsfield(top_iph);
  69. dsfield = INET_ECN_encapsulate(dsfield, dsfield);
  70. if (x->props.flags & XFRM_STATE_NOECN)
  71. dsfield &= ~INET_ECN_MASK;
  72. ipv6_change_dsfield(top_iph, 0, dsfield);
  73. top_iph->hop_limit = dst_metric(dst->child, RTAX_HOPLIMIT);
  74. ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
  75. ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
  76. skb->protocol = htons(ETH_P_IPV6);
  77. return 0;
  78. }
  79. static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  80. {
  81. int err = -EINVAL;
  82. const unsigned char *old_mac;
  83. const unsigned char *nh = skb_network_header(skb);
  84. if (nh[IP6CB(skb)->nhoff] != IPPROTO_IPV6 &&
  85. nh[IP6CB(skb)->nhoff] != IPPROTO_IPIP)
  86. goto out;
  87. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  88. goto out;
  89. if (skb_cloned(skb) &&
  90. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  91. goto out;
  92. nh = skb_network_header(skb);
  93. if (nh[IP6CB(skb)->nhoff] == IPPROTO_IPV6) {
  94. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  95. ipv6_copy_dscp(ipv6_hdr(skb), ipipv6_hdr(skb));
  96. if (!(x->props.flags & XFRM_STATE_NOECN))
  97. ipip6_ecn_decapsulate(skb);
  98. } else {
  99. if (!(x->props.flags & XFRM_STATE_NOECN))
  100. ip6ip_ecn_decapsulate(skb);
  101. skb->protocol = htons(ETH_P_IP);
  102. }
  103. old_mac = skb_mac_header(skb);
  104. skb_set_mac_header(skb, -skb->mac_len);
  105. memmove(skb_mac_header(skb), old_mac, skb->mac_len);
  106. skb_reset_network_header(skb);
  107. err = 0;
  108. out:
  109. return err;
  110. }
  111. static struct xfrm_mode xfrm6_tunnel_mode = {
  112. .input = xfrm6_tunnel_input,
  113. .output = xfrm6_tunnel_output,
  114. .owner = THIS_MODULE,
  115. .encap = XFRM_MODE_TUNNEL,
  116. };
  117. static int __init xfrm6_tunnel_init(void)
  118. {
  119. return xfrm_register_mode(&xfrm6_tunnel_mode, AF_INET6);
  120. }
  121. static void __exit xfrm6_tunnel_exit(void)
  122. {
  123. int err;
  124. err = xfrm_unregister_mode(&xfrm6_tunnel_mode, AF_INET6);
  125. BUG_ON(err);
  126. }
  127. module_init(xfrm6_tunnel_init);
  128. module_exit(xfrm6_tunnel_exit);
  129. MODULE_LICENSE("GPL");
  130. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TUNNEL);