xfrm6_output.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/skbuff.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/icmpv6.h>
  14. #include <net/dsfield.h>
  15. #include <net/inet_ecn.h>
  16. #include <net/ipv6.h>
  17. #include <net/xfrm.h>
  18. /* Add encapsulation header.
  19. *
  20. * In transport mode, the IP header and mutable extension headers will be moved
  21. * forward to make space for the encapsulation header.
  22. *
  23. * In tunnel mode, the top IP header will be constructed per RFC 2401.
  24. * The following fields in it shall be filled in by x->type->output:
  25. * payload_len
  26. *
  27. * On exit, skb->h will be set to the start of the encapsulation header to be
  28. * filled in by x->type->output and skb->nh will be set to the nextheader field
  29. * of the extension header directly preceding the encapsulation header, or in
  30. * its absence, that of the top IP header. The value of skb->data will always
  31. * point to the top IP header.
  32. */
  33. static void xfrm6_encap(struct sk_buff *skb)
  34. {
  35. struct dst_entry *dst = skb->dst;
  36. struct xfrm_state *x = dst->xfrm;
  37. struct ipv6hdr *iph, *top_iph;
  38. int dsfield;
  39. skb_push(skb, x->props.header_len);
  40. iph = skb->nh.ipv6h;
  41. if (!x->props.mode) {
  42. u8 *prevhdr;
  43. int hdr_len;
  44. hdr_len = ip6_find_1stfragopt(skb, &prevhdr);
  45. skb->nh.raw = prevhdr - x->props.header_len;
  46. skb->h.raw = skb->data + hdr_len;
  47. memmove(skb->data, iph, hdr_len);
  48. return;
  49. }
  50. skb->nh.raw = skb->data;
  51. top_iph = skb->nh.ipv6h;
  52. skb->nh.raw = &top_iph->nexthdr;
  53. skb->h.ipv6h = top_iph + 1;
  54. top_iph->version = 6;
  55. top_iph->priority = iph->priority;
  56. top_iph->flow_lbl[0] = iph->flow_lbl[0];
  57. top_iph->flow_lbl[1] = iph->flow_lbl[1];
  58. top_iph->flow_lbl[2] = iph->flow_lbl[2];
  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->nexthdr = IPPROTO_IPV6;
  65. top_iph->hop_limit = dst_metric(dst->child, RTAX_HOPLIMIT);
  66. ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
  67. ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
  68. }
  69. static int xfrm6_tunnel_check_size(struct sk_buff *skb)
  70. {
  71. int mtu, ret = 0;
  72. struct dst_entry *dst = skb->dst;
  73. mtu = dst_mtu(dst);
  74. if (mtu < IPV6_MIN_MTU)
  75. mtu = IPV6_MIN_MTU;
  76. if (skb->len > mtu) {
  77. skb->dev = dst->dev;
  78. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
  79. ret = -EMSGSIZE;
  80. }
  81. return ret;
  82. }
  83. int xfrm6_output(struct sk_buff *skb)
  84. {
  85. struct dst_entry *dst = skb->dst;
  86. struct xfrm_state *x = dst->xfrm;
  87. int err;
  88. if (skb->ip_summed == CHECKSUM_HW) {
  89. err = skb_checksum_help(skb, 0);
  90. if (err)
  91. goto error_nolock;
  92. }
  93. if (x->props.mode) {
  94. err = xfrm6_tunnel_check_size(skb);
  95. if (err)
  96. goto error_nolock;
  97. }
  98. spin_lock_bh(&x->lock);
  99. err = xfrm_state_check(x, skb);
  100. if (err)
  101. goto error;
  102. xfrm6_encap(skb);
  103. err = x->type->output(x, skb);
  104. if (err)
  105. goto error;
  106. x->curlft.bytes += skb->len;
  107. x->curlft.packets++;
  108. spin_unlock_bh(&x->lock);
  109. skb->nh.raw = skb->data;
  110. if (!(skb->dst = dst_pop(dst))) {
  111. err = -EHOSTUNREACH;
  112. goto error_nolock;
  113. }
  114. err = NET_XMIT_BYPASS;
  115. out_exit:
  116. return err;
  117. error:
  118. spin_unlock_bh(&x->lock);
  119. error_nolock:
  120. kfree_skb(skb);
  121. goto out_exit;
  122. }