xfrm4_output.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. iph = skb->nh.iph;
  35. skb->h.ipiph = iph;
  36. skb->nh.raw = skb_push(skb, x->props.header_len);
  37. top_iph = skb->nh.iph;
  38. if (!x->props.mode) {
  39. skb->h.raw += iph->ihl*4;
  40. memmove(top_iph, iph, iph->ihl*4);
  41. return;
  42. }
  43. top_iph->ihl = 5;
  44. top_iph->version = 4;
  45. /* DS disclosed */
  46. top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos);
  47. if (x->props.flags & XFRM_STATE_NOECN)
  48. IP_ECN_clear(top_iph);
  49. top_iph->frag_off = iph->frag_off & htons(IP_DF);
  50. if (!top_iph->frag_off)
  51. __ip_select_ident(top_iph, dst, 0);
  52. top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
  53. top_iph->saddr = x->props.saddr.a4;
  54. top_iph->daddr = x->id.daddr.a4;
  55. top_iph->protocol = IPPROTO_IPIP;
  56. memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  57. }
  58. static int xfrm4_tunnel_check_size(struct sk_buff *skb)
  59. {
  60. int mtu, ret = 0;
  61. struct dst_entry *dst;
  62. struct iphdr *iph = skb->nh.iph;
  63. if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
  64. goto out;
  65. IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
  66. if (!(iph->frag_off & htons(IP_DF)) || skb->local_df)
  67. goto out;
  68. dst = skb->dst;
  69. mtu = dst_mtu(dst);
  70. if (skb->len > mtu) {
  71. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
  72. ret = -EMSGSIZE;
  73. }
  74. out:
  75. return ret;
  76. }
  77. int xfrm4_output(struct sk_buff *skb)
  78. {
  79. struct dst_entry *dst = skb->dst;
  80. struct xfrm_state *x = dst->xfrm;
  81. int err;
  82. if (skb->ip_summed == CHECKSUM_HW) {
  83. err = skb_checksum_help(skb, 0);
  84. if (err)
  85. goto error_nolock;
  86. }
  87. if (x->props.mode) {
  88. err = xfrm4_tunnel_check_size(skb);
  89. if (err)
  90. goto error_nolock;
  91. }
  92. spin_lock_bh(&x->lock);
  93. err = xfrm_state_check(x, skb);
  94. if (err)
  95. goto error;
  96. xfrm4_encap(skb);
  97. err = x->type->output(x, skb);
  98. if (err)
  99. goto error;
  100. x->curlft.bytes += skb->len;
  101. x->curlft.packets++;
  102. spin_unlock_bh(&x->lock);
  103. if (!(skb->dst = dst_pop(dst))) {
  104. err = -EHOSTUNREACH;
  105. goto error_nolock;
  106. }
  107. err = NET_XMIT_BYPASS;
  108. out_exit:
  109. return err;
  110. error:
  111. spin_unlock_bh(&x->lock);
  112. error_nolock:
  113. kfree_skb(skb);
  114. goto out_exit;
  115. }