xfrm6_output.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * xfrm6_output.c - Common IPsec encapsulation code for IPv6.
  3. * Copyright (C) 2002 USAGI/WIDE Project
  4. * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/compiler.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/icmpv6.h>
  15. #include <linux/netfilter_ipv6.h>
  16. #include <net/dsfield.h>
  17. #include <net/inet_ecn.h>
  18. #include <net/ipv6.h>
  19. #include <net/xfrm.h>
  20. /* Add encapsulation header.
  21. *
  22. * In transport mode, the IP header and mutable extension headers will be moved
  23. * forward to make space for the encapsulation header.
  24. *
  25. * In tunnel mode, the top IP header will be constructed per RFC 2401.
  26. * The following fields in it shall be filled in by x->type->output:
  27. * payload_len
  28. *
  29. * On exit, skb->h will be set to the start of the encapsulation header to be
  30. * filled in by x->type->output and skb->nh will be set to the nextheader field
  31. * of the extension header directly preceding the encapsulation header, or in
  32. * its absence, that of the top IP header. The value of skb->data will always
  33. * point to the top IP header.
  34. */
  35. static void xfrm6_encap(struct sk_buff *skb)
  36. {
  37. struct dst_entry *dst = skb->dst;
  38. struct xfrm_state *x = dst->xfrm;
  39. struct ipv6hdr *iph, *top_iph;
  40. int dsfield;
  41. skb_push(skb, x->props.header_len);
  42. iph = skb->nh.ipv6h;
  43. if (!x->props.mode) {
  44. u8 *prevhdr;
  45. int hdr_len;
  46. hdr_len = ip6_find_1stfragopt(skb, &prevhdr);
  47. skb->nh.raw = prevhdr - x->props.header_len;
  48. skb->h.raw = skb->data + hdr_len;
  49. memmove(skb->data, iph, hdr_len);
  50. return;
  51. }
  52. skb->nh.raw = skb->data;
  53. top_iph = skb->nh.ipv6h;
  54. skb->nh.raw = &top_iph->nexthdr;
  55. skb->h.ipv6h = top_iph + 1;
  56. top_iph->version = 6;
  57. top_iph->priority = iph->priority;
  58. top_iph->flow_lbl[0] = iph->flow_lbl[0];
  59. top_iph->flow_lbl[1] = iph->flow_lbl[1];
  60. top_iph->flow_lbl[2] = iph->flow_lbl[2];
  61. dsfield = ipv6_get_dsfield(top_iph);
  62. dsfield = INET_ECN_encapsulate(dsfield, dsfield);
  63. if (x->props.flags & XFRM_STATE_NOECN)
  64. dsfield &= ~INET_ECN_MASK;
  65. ipv6_change_dsfield(top_iph, 0, dsfield);
  66. top_iph->nexthdr = IPPROTO_IPV6;
  67. top_iph->hop_limit = dst_metric(dst->child, RTAX_HOPLIMIT);
  68. ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
  69. ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
  70. }
  71. static int xfrm6_tunnel_check_size(struct sk_buff *skb)
  72. {
  73. int mtu, ret = 0;
  74. struct dst_entry *dst = skb->dst;
  75. mtu = dst_mtu(dst);
  76. if (mtu < IPV6_MIN_MTU)
  77. mtu = IPV6_MIN_MTU;
  78. if (skb->len > mtu) {
  79. skb->dev = dst->dev;
  80. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
  81. ret = -EMSGSIZE;
  82. }
  83. return ret;
  84. }
  85. static int xfrm6_output_one(struct sk_buff *skb)
  86. {
  87. struct dst_entry *dst = skb->dst;
  88. struct xfrm_state *x = dst->xfrm;
  89. int err;
  90. if (skb->ip_summed == CHECKSUM_HW) {
  91. err = skb_checksum_help(skb, 0);
  92. if (err)
  93. goto error_nolock;
  94. }
  95. if (x->props.mode) {
  96. err = xfrm6_tunnel_check_size(skb);
  97. if (err)
  98. goto error_nolock;
  99. }
  100. do {
  101. spin_lock_bh(&x->lock);
  102. err = xfrm_state_check(x, skb);
  103. if (err)
  104. goto error;
  105. xfrm6_encap(skb);
  106. err = x->type->output(x, skb);
  107. if (err)
  108. goto error;
  109. x->curlft.bytes += skb->len;
  110. x->curlft.packets++;
  111. spin_unlock_bh(&x->lock);
  112. skb->nh.raw = skb->data;
  113. if (!(skb->dst = dst_pop(dst))) {
  114. err = -EHOSTUNREACH;
  115. goto error_nolock;
  116. }
  117. dst = skb->dst;
  118. x = dst->xfrm;
  119. } while (x && !x->props.mode);
  120. IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
  121. err = 0;
  122. out_exit:
  123. return err;
  124. error:
  125. spin_unlock_bh(&x->lock);
  126. error_nolock:
  127. kfree_skb(skb);
  128. goto out_exit;
  129. }
  130. static int xfrm6_output_finish(struct sk_buff *skb)
  131. {
  132. int err;
  133. while (likely((err = xfrm6_output_one(skb)) == 0)) {
  134. nf_reset(skb);
  135. err = nf_hook(PF_INET6, NF_IP6_LOCAL_OUT, &skb, NULL,
  136. skb->dst->dev, dst_output);
  137. if (unlikely(err != 1))
  138. break;
  139. if (!skb->dst->xfrm)
  140. return dst_output(skb);
  141. err = nf_hook(PF_INET6, NF_IP6_POST_ROUTING, &skb, NULL,
  142. skb->dst->dev, xfrm6_output_finish);
  143. if (unlikely(err != 1))
  144. break;
  145. }
  146. return err;
  147. }
  148. int xfrm6_output(struct sk_buff *skb)
  149. {
  150. return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dst->dev,
  151. xfrm6_output_finish);
  152. }