xfrm4_output.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
  3. * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #include <linux/skbuff.h>
  11. #include <linux/spinlock.h>
  12. #include <net/inet_ecn.h>
  13. #include <net/ip.h>
  14. #include <net/xfrm.h>
  15. #include <net/icmp.h>
  16. /* Add encapsulation header.
  17. *
  18. * In transport mode, the IP header will be moved forward to make space
  19. * for the encapsulation header.
  20. *
  21. * In tunnel mode, the top IP header will be constructed per RFC 2401.
  22. * The following fields in it shall be filled in by x->type->output:
  23. * tot_len
  24. * check
  25. *
  26. * On exit, skb->h will be set to the start of the payload to be processed
  27. * by x->type->output and skb->nh will be set to the top IP header.
  28. */
  29. static void xfrm4_encap(struct sk_buff *skb)
  30. {
  31. struct dst_entry *dst = skb->dst;
  32. struct xfrm_state *x = dst->xfrm;
  33. struct iphdr *iph, *top_iph;
  34. int flags;
  35. iph = skb->nh.iph;
  36. skb->h.ipiph = iph;
  37. skb->nh.raw = skb_push(skb, x->props.header_len);
  38. top_iph = skb->nh.iph;
  39. if (!x->props.mode) {
  40. skb->h.raw += iph->ihl*4;
  41. memmove(top_iph, iph, iph->ihl*4);
  42. return;
  43. }
  44. top_iph->ihl = 5;
  45. top_iph->version = 4;
  46. /* DS disclosed */
  47. top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos);
  48. flags = x->props.flags;
  49. if (flags & XFRM_STATE_NOECN)
  50. IP_ECN_clear(top_iph);
  51. top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
  52. 0 : (iph->frag_off & htons(IP_DF));
  53. if (!top_iph->frag_off)
  54. __ip_select_ident(top_iph, dst, 0);
  55. top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
  56. top_iph->saddr = x->props.saddr.a4;
  57. top_iph->daddr = x->id.daddr.a4;
  58. top_iph->protocol = IPPROTO_IPIP;
  59. memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  60. }
  61. static int xfrm4_tunnel_check_size(struct sk_buff *skb)
  62. {
  63. int mtu, ret = 0;
  64. struct dst_entry *dst;
  65. struct iphdr *iph = skb->nh.iph;
  66. if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
  67. goto out;
  68. IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
  69. if (!(iph->frag_off & htons(IP_DF)) || skb->local_df)
  70. goto out;
  71. dst = skb->dst;
  72. mtu = dst_mtu(dst);
  73. if (skb->len > mtu) {
  74. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
  75. ret = -EMSGSIZE;
  76. }
  77. out:
  78. return ret;
  79. }
  80. int xfrm4_output(struct sk_buff *skb)
  81. {
  82. struct dst_entry *dst = skb->dst;
  83. struct xfrm_state *x = dst->xfrm;
  84. int err;
  85. if (skb->ip_summed == CHECKSUM_HW) {
  86. err = skb_checksum_help(skb, 0);
  87. if (err)
  88. goto error_nolock;
  89. }
  90. if (x->props.mode) {
  91. err = xfrm4_tunnel_check_size(skb);
  92. if (err)
  93. goto error_nolock;
  94. }
  95. spin_lock_bh(&x->lock);
  96. err = xfrm_state_check(x, skb);
  97. if (err)
  98. goto error;
  99. xfrm4_encap(skb);
  100. err = x->type->output(x, skb);
  101. if (err)
  102. goto error;
  103. x->curlft.bytes += skb->len;
  104. x->curlft.packets++;
  105. spin_unlock_bh(&x->lock);
  106. if (!(skb->dst = dst_pop(dst))) {
  107. err = -EHOSTUNREACH;
  108. goto error_nolock;
  109. }
  110. err = NET_XMIT_BYPASS;
  111. out_exit:
  112. return err;
  113. error:
  114. spin_unlock_bh(&x->lock);
  115. error_nolock:
  116. kfree_skb(skb);
  117. goto out_exit;
  118. }