xfrm4_mode_tunnel.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * xfrm4_mode_tunnel.c - Tunnel mode encapsulation for IPv4.
  3. *
  4. * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/stringify.h>
  11. #include <net/dst.h>
  12. #include <net/inet_ecn.h>
  13. #include <net/ip.h>
  14. #include <net/xfrm.h>
  15. static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
  16. {
  17. struct iphdr *outer_iph = ip_hdr(skb);
  18. struct iphdr *inner_iph = ipip_hdr(skb);
  19. if (INET_ECN_is_ce(outer_iph->tos))
  20. IP_ECN_set_ce(inner_iph);
  21. }
  22. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  23. {
  24. if (INET_ECN_is_ce(iph->tos))
  25. IP6_ECN_set_ce(ipv6_hdr(skb));
  26. }
  27. /* Add encapsulation header.
  28. *
  29. * The top IP header will be constructed per RFC 2401.
  30. */
  31. static int xfrm4_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
  32. {
  33. struct dst_entry *dst = skb->dst;
  34. struct iphdr *top_iph;
  35. int flags;
  36. skb_set_network_header(skb, -x->props.header_len);
  37. skb->mac_header = skb->network_header +
  38. offsetof(struct iphdr, protocol);
  39. skb->transport_header = skb->network_header + sizeof(*top_iph);
  40. top_iph = ip_hdr(skb);
  41. top_iph->ihl = 5;
  42. top_iph->version = 4;
  43. top_iph->protocol = x->inner_mode->afinfo->proto;
  44. /* DS disclosed */
  45. top_iph->tos = INET_ECN_encapsulate(XFRM_MODE_SKB_CB(skb)->tos,
  46. XFRM_MODE_SKB_CB(skb)->tos);
  47. flags = x->props.flags;
  48. if (flags & XFRM_STATE_NOECN)
  49. IP_ECN_clear(top_iph);
  50. top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
  51. 0 : XFRM_MODE_SKB_CB(skb)->frag_off;
  52. ip_select_ident(top_iph, dst->child, NULL);
  53. top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
  54. top_iph->saddr = x->props.saddr.a4;
  55. top_iph->daddr = x->id.daddr.a4;
  56. return 0;
  57. }
  58. static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  59. {
  60. struct iphdr *iph = ip_hdr(skb);
  61. const unsigned char *old_mac;
  62. int err = -EINVAL;
  63. switch (iph->protocol){
  64. case IPPROTO_IPIP:
  65. break;
  66. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  67. case IPPROTO_IPV6:
  68. break;
  69. #endif
  70. default:
  71. goto out;
  72. }
  73. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  74. goto out;
  75. if (skb_cloned(skb) &&
  76. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  77. goto out;
  78. iph = ip_hdr(skb);
  79. if (iph->protocol == IPPROTO_IPIP) {
  80. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  81. ipv4_copy_dscp(ipv4_get_dsfield(iph), ipip_hdr(skb));
  82. if (!(x->props.flags & XFRM_STATE_NOECN))
  83. ipip_ecn_decapsulate(skb);
  84. }
  85. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  86. else {
  87. if (!(x->props.flags & XFRM_STATE_NOECN))
  88. ipip6_ecn_decapsulate(iph, skb);
  89. skb->protocol = htons(ETH_P_IPV6);
  90. }
  91. #endif
  92. old_mac = skb_mac_header(skb);
  93. skb_set_mac_header(skb, -skb->mac_len);
  94. memmove(skb_mac_header(skb), old_mac, skb->mac_len);
  95. skb_reset_network_header(skb);
  96. err = 0;
  97. out:
  98. return err;
  99. }
  100. static struct xfrm_mode xfrm4_tunnel_mode = {
  101. .input = xfrm4_tunnel_input,
  102. .output2 = xfrm4_tunnel_output,
  103. .output = xfrm4_prepare_output,
  104. .owner = THIS_MODULE,
  105. .encap = XFRM_MODE_TUNNEL,
  106. .flags = XFRM_MODE_FLAG_TUNNEL,
  107. };
  108. static int __init xfrm4_tunnel_init(void)
  109. {
  110. return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
  111. }
  112. static void __exit xfrm4_tunnel_exit(void)
  113. {
  114. int err;
  115. err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET);
  116. BUG_ON(err);
  117. }
  118. module_init(xfrm4_tunnel_init);
  119. module_exit(xfrm4_tunnel_exit);
  120. MODULE_LICENSE("GPL");
  121. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);