xfrm4_mode_tunnel.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 = skb->nh.iph;
  18. struct iphdr *inner_iph = skb->h.ipiph;
  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(skb->nh.ipv6h);
  26. }
  27. /* Add encapsulation header.
  28. *
  29. * The top IP header will be constructed per RFC 2401. The following fields
  30. * in it shall be filled in by x->type->output:
  31. * tot_len
  32. * check
  33. *
  34. * On exit, skb->h will be set to the start of the payload to be processed
  35. * by x->type->output and skb->nh will be set to the top IP header.
  36. */
  37. static int xfrm4_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
  38. {
  39. struct dst_entry *dst = skb->dst;
  40. struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
  41. struct iphdr *iph, *top_iph;
  42. int flags;
  43. iph = skb->nh.iph;
  44. skb->h.ipiph = iph;
  45. skb->nh.raw = skb_push(skb, x->props.header_len);
  46. top_iph = skb->nh.iph;
  47. top_iph->ihl = 5;
  48. top_iph->version = 4;
  49. flags = x->props.flags;
  50. /* DS disclosed */
  51. if (xdst->route->ops->family == AF_INET) {
  52. top_iph->protocol = IPPROTO_IPIP;
  53. top_iph->tos = INET_ECN_encapsulate(iph->tos, iph->tos);
  54. top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
  55. 0 : (iph->frag_off & htons(IP_DF));
  56. }
  57. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  58. else {
  59. struct ipv6hdr *ipv6h = (struct ipv6hdr*)iph;
  60. top_iph->protocol = IPPROTO_IPV6;
  61. top_iph->tos = INET_ECN_encapsulate(iph->tos, ipv6_get_dsfield(ipv6h));
  62. top_iph->frag_off = 0;
  63. }
  64. #endif
  65. if (flags & XFRM_STATE_NOECN)
  66. IP_ECN_clear(top_iph);
  67. if (!top_iph->frag_off)
  68. __ip_select_ident(top_iph, dst->child, 0);
  69. top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
  70. top_iph->saddr = x->props.saddr.a4;
  71. top_iph->daddr = x->id.daddr.a4;
  72. memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  73. return 0;
  74. }
  75. static int xfrm4_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  76. {
  77. struct iphdr *iph = skb->nh.iph;
  78. const unsigned char *old_mac;
  79. int err = -EINVAL;
  80. switch (iph->protocol){
  81. case IPPROTO_IPIP:
  82. break;
  83. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  84. case IPPROTO_IPV6:
  85. break;
  86. #endif
  87. default:
  88. goto out;
  89. }
  90. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  91. goto out;
  92. if (skb_cloned(skb) &&
  93. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  94. goto out;
  95. iph = skb->nh.iph;
  96. if (iph->protocol == IPPROTO_IPIP) {
  97. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  98. ipv4_copy_dscp(iph, skb->h.ipiph);
  99. if (!(x->props.flags & XFRM_STATE_NOECN))
  100. ipip_ecn_decapsulate(skb);
  101. }
  102. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  103. else {
  104. if (!(x->props.flags & XFRM_STATE_NOECN))
  105. ipip6_ecn_decapsulate(iph, skb);
  106. skb->protocol = htons(ETH_P_IPV6);
  107. }
  108. #endif
  109. old_mac = skb_mac_header(skb);
  110. skb_set_mac_header(skb, -skb->mac_len);
  111. memmove(skb_mac_header(skb), old_mac, skb->mac_len);
  112. skb_reset_network_header(skb);
  113. err = 0;
  114. out:
  115. return err;
  116. }
  117. static struct xfrm_mode xfrm4_tunnel_mode = {
  118. .input = xfrm4_tunnel_input,
  119. .output = xfrm4_tunnel_output,
  120. .owner = THIS_MODULE,
  121. .encap = XFRM_MODE_TUNNEL,
  122. };
  123. static int __init xfrm4_tunnel_init(void)
  124. {
  125. return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
  126. }
  127. static void __exit xfrm4_tunnel_exit(void)
  128. {
  129. int err;
  130. err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET);
  131. BUG_ON(err);
  132. }
  133. module_init(xfrm4_tunnel_init);
  134. module_exit(xfrm4_tunnel_exit);
  135. MODULE_LICENSE("GPL");
  136. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);