xfrm4_mode_tunnel.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/gfp.h>
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/stringify.h>
  12. #include <net/dst.h>
  13. #include <net/inet_ecn.h>
  14. #include <net/ip.h>
  15. #include <net/xfrm.h>
  16. static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
  17. {
  18. struct iphdr *inner_iph = ipip_hdr(skb);
  19. if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
  20. IP_ECN_set_ce(inner_iph);
  21. }
  22. /* Add encapsulation header.
  23. *
  24. * The top IP header will be constructed per RFC 2401.
  25. */
  26. static int xfrm4_mode_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
  27. {
  28. struct dst_entry *dst = skb_dst(skb);
  29. struct iphdr *top_iph;
  30. int flags;
  31. skb_set_inner_network_header(skb, skb_network_offset(skb));
  32. skb_set_inner_transport_header(skb, skb_transport_offset(skb));
  33. skb_set_network_header(skb, -x->props.header_len);
  34. skb->mac_header = skb->network_header +
  35. offsetof(struct iphdr, protocol);
  36. skb->transport_header = skb->network_header + sizeof(*top_iph);
  37. top_iph = ip_hdr(skb);
  38. top_iph->ihl = 5;
  39. top_iph->version = 4;
  40. top_iph->protocol = xfrm_af2proto(skb_dst(skb)->ops->family);
  41. /* DS disclosing depends on XFRM_SA_XFLAG_DONT_ENCAP_DSCP */
  42. if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP)
  43. top_iph->tos = 0;
  44. else
  45. top_iph->tos = XFRM_MODE_SKB_CB(skb)->tos;
  46. top_iph->tos = INET_ECN_encapsulate(top_iph->tos,
  47. XFRM_MODE_SKB_CB(skb)->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 : (XFRM_MODE_SKB_CB(skb)->frag_off & htons(IP_DF));
  53. top_iph->ttl = ip4_dst_hoplimit(xfrm_dst_child(dst));
  54. top_iph->saddr = x->props.saddr.a4;
  55. top_iph->daddr = x->id.daddr.a4;
  56. ip_select_ident(dev_net(dst->dev), skb, NULL);
  57. return 0;
  58. }
  59. static int xfrm4_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  60. {
  61. int err = -EINVAL;
  62. if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPIP)
  63. goto out;
  64. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  65. goto out;
  66. err = skb_unclone(skb, GFP_ATOMIC);
  67. if (err)
  68. goto out;
  69. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  70. ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipip_hdr(skb));
  71. if (!(x->props.flags & XFRM_STATE_NOECN))
  72. ipip_ecn_decapsulate(skb);
  73. skb_reset_network_header(skb);
  74. skb_mac_header_rebuild(skb);
  75. if (skb->mac_len)
  76. eth_hdr(skb)->h_proto = skb->protocol;
  77. err = 0;
  78. out:
  79. return err;
  80. }
  81. static struct sk_buff *xfrm4_mode_tunnel_gso_segment(struct xfrm_state *x,
  82. struct sk_buff *skb,
  83. netdev_features_t features)
  84. {
  85. __skb_push(skb, skb->mac_len);
  86. return skb_mac_gso_segment(skb, features);
  87. }
  88. static void xfrm4_mode_tunnel_xmit(struct xfrm_state *x, struct sk_buff *skb)
  89. {
  90. struct xfrm_offload *xo = xfrm_offload(skb);
  91. if (xo->flags & XFRM_GSO_SEGMENT)
  92. skb->transport_header = skb->network_header +
  93. sizeof(struct iphdr);
  94. skb_reset_mac_len(skb);
  95. pskb_pull(skb, skb->mac_len + x->props.header_len);
  96. }
  97. static struct xfrm_mode xfrm4_tunnel_mode = {
  98. .input2 = xfrm4_mode_tunnel_input,
  99. .input = xfrm_prepare_input,
  100. .output2 = xfrm4_mode_tunnel_output,
  101. .output = xfrm4_prepare_output,
  102. .gso_segment = xfrm4_mode_tunnel_gso_segment,
  103. .xmit = xfrm4_mode_tunnel_xmit,
  104. .owner = THIS_MODULE,
  105. .encap = XFRM_MODE_TUNNEL,
  106. .flags = XFRM_MODE_FLAG_TUNNEL,
  107. };
  108. static int __init xfrm4_mode_tunnel_init(void)
  109. {
  110. return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
  111. }
  112. static void __exit xfrm4_mode_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_mode_tunnel_init);
  119. module_exit(xfrm4_mode_tunnel_exit);
  120. MODULE_LICENSE("GPL");
  121. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);