xfrm4_mode_tunnel.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 *inner_iph = ipip_hdr(skb);
  18. if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
  19. IP_ECN_set_ce(inner_iph);
  20. }
  21. /* Add encapsulation header.
  22. *
  23. * The top IP header will be constructed per RFC 2401.
  24. */
  25. static int xfrm4_mode_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
  26. {
  27. struct dst_entry *dst = skb_dst(skb);
  28. struct iphdr *top_iph;
  29. int flags;
  30. skb_set_network_header(skb, -x->props.header_len);
  31. skb->mac_header = skb->network_header +
  32. offsetof(struct iphdr, protocol);
  33. skb->transport_header = skb->network_header + sizeof(*top_iph);
  34. top_iph = ip_hdr(skb);
  35. top_iph->ihl = 5;
  36. top_iph->version = 4;
  37. top_iph->protocol = xfrm_af2proto(skb_dst(skb)->ops->family);
  38. /* DS disclosed */
  39. top_iph->tos = INET_ECN_encapsulate(XFRM_MODE_SKB_CB(skb)->tos,
  40. XFRM_MODE_SKB_CB(skb)->tos);
  41. flags = x->props.flags;
  42. if (flags & XFRM_STATE_NOECN)
  43. IP_ECN_clear(top_iph);
  44. top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
  45. 0 : (XFRM_MODE_SKB_CB(skb)->frag_off & htons(IP_DF));
  46. ip_select_ident(top_iph, dst->child, NULL);
  47. top_iph->ttl = dst_metric(dst->child, RTAX_HOPLIMIT);
  48. top_iph->saddr = x->props.saddr.a4;
  49. top_iph->daddr = x->id.daddr.a4;
  50. return 0;
  51. }
  52. static int xfrm4_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
  53. {
  54. const unsigned char *old_mac;
  55. int err = -EINVAL;
  56. if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPIP)
  57. goto out;
  58. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  59. goto out;
  60. if (skb_cloned(skb) &&
  61. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  62. goto out;
  63. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  64. ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipip_hdr(skb));
  65. if (!(x->props.flags & XFRM_STATE_NOECN))
  66. ipip_ecn_decapsulate(skb);
  67. old_mac = skb_mac_header(skb);
  68. skb_set_mac_header(skb, -skb->mac_len);
  69. memmove(skb_mac_header(skb), old_mac, skb->mac_len);
  70. skb_reset_network_header(skb);
  71. err = 0;
  72. out:
  73. return err;
  74. }
  75. static struct xfrm_mode xfrm4_tunnel_mode = {
  76. .input2 = xfrm4_mode_tunnel_input,
  77. .input = xfrm_prepare_input,
  78. .output2 = xfrm4_mode_tunnel_output,
  79. .output = xfrm4_prepare_output,
  80. .owner = THIS_MODULE,
  81. .encap = XFRM_MODE_TUNNEL,
  82. .flags = XFRM_MODE_FLAG_TUNNEL,
  83. };
  84. static int __init xfrm4_mode_tunnel_init(void)
  85. {
  86. return xfrm_register_mode(&xfrm4_tunnel_mode, AF_INET);
  87. }
  88. static void __exit xfrm4_mode_tunnel_exit(void)
  89. {
  90. int err;
  91. err = xfrm_unregister_mode(&xfrm4_tunnel_mode, AF_INET);
  92. BUG_ON(err);
  93. }
  94. module_init(xfrm4_mode_tunnel_init);
  95. module_exit(xfrm4_mode_tunnel_exit);
  96. MODULE_LICENSE("GPL");
  97. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TUNNEL);