xfrm4_tunnel.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* xfrm4_tunnel.c: Generic IP tunnel transformer.
  2. *
  3. * Copyright (C) 2003 David S. Miller (davem@redhat.com)
  4. */
  5. #include <linux/skbuff.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <net/xfrm.h>
  9. #include <net/ip.h>
  10. #include <net/protocol.h>
  11. static int ipip_output(struct xfrm_state *x, struct sk_buff *skb)
  12. {
  13. skb_push(skb, -skb_network_offset(skb));
  14. return 0;
  15. }
  16. static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb)
  17. {
  18. return ip_hdr(skb)->protocol;
  19. }
  20. static int ipip_init_state(struct xfrm_state *x)
  21. {
  22. if (x->props.mode != XFRM_MODE_TUNNEL)
  23. return -EINVAL;
  24. if (x->encap)
  25. return -EINVAL;
  26. x->props.header_len = sizeof(struct iphdr);
  27. return 0;
  28. }
  29. static void ipip_destroy(struct xfrm_state *x)
  30. {
  31. }
  32. static const struct xfrm_type ipip_type = {
  33. .description = "IPIP",
  34. .owner = THIS_MODULE,
  35. .proto = IPPROTO_IPIP,
  36. .init_state = ipip_init_state,
  37. .destructor = ipip_destroy,
  38. .input = ipip_xfrm_rcv,
  39. .output = ipip_output
  40. };
  41. static int xfrm_tunnel_rcv(struct sk_buff *skb)
  42. {
  43. return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr);
  44. }
  45. static int xfrm_tunnel_err(struct sk_buff *skb, u32 info)
  46. {
  47. return -ENOENT;
  48. }
  49. static struct xfrm_tunnel xfrm_tunnel_handler __read_mostly = {
  50. .handler = xfrm_tunnel_rcv,
  51. .err_handler = xfrm_tunnel_err,
  52. .priority = 2,
  53. };
  54. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  55. static struct xfrm_tunnel xfrm64_tunnel_handler __read_mostly = {
  56. .handler = xfrm_tunnel_rcv,
  57. .err_handler = xfrm_tunnel_err,
  58. .priority = 2,
  59. };
  60. #endif
  61. static int __init ipip_init(void)
  62. {
  63. if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
  64. printk(KERN_INFO "ipip init: can't add xfrm type\n");
  65. return -EAGAIN;
  66. }
  67. if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) {
  68. printk(KERN_INFO "ipip init: can't add xfrm handler for AF_INET\n");
  69. xfrm_unregister_type(&ipip_type, AF_INET);
  70. return -EAGAIN;
  71. }
  72. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  73. if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) {
  74. printk(KERN_INFO "ipip init: can't add xfrm handler for AF_INET6\n");
  75. xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET);
  76. xfrm_unregister_type(&ipip_type, AF_INET);
  77. return -EAGAIN;
  78. }
  79. #endif
  80. return 0;
  81. }
  82. static void __exit ipip_fini(void)
  83. {
  84. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  85. if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6))
  86. printk(KERN_INFO "ipip close: can't remove xfrm handler for AF_INET6\n");
  87. #endif
  88. if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET))
  89. printk(KERN_INFO "ipip close: can't remove xfrm handler for AF_INET\n");
  90. if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
  91. printk(KERN_INFO "ipip close: can't remove xfrm type\n");
  92. }
  93. module_init(ipip_init);
  94. module_exit(ipip_fini);
  95. MODULE_LICENSE("GPL");
  96. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP);