xfrm4_tunnel.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. struct iphdr *iph = ip_hdr(skb);
  14. iph->tot_len = htons(skb->len);
  15. ip_send_check(iph);
  16. return 0;
  17. }
  18. static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb)
  19. {
  20. return 0;
  21. }
  22. static int ipip_init_state(struct xfrm_state *x)
  23. {
  24. if (x->props.mode != XFRM_MODE_TUNNEL)
  25. return -EINVAL;
  26. if (x->encap)
  27. return -EINVAL;
  28. x->props.header_len = sizeof(struct iphdr);
  29. return 0;
  30. }
  31. static void ipip_destroy(struct xfrm_state *x)
  32. {
  33. }
  34. static struct xfrm_type ipip_type = {
  35. .description = "IPIP",
  36. .owner = THIS_MODULE,
  37. .proto = IPPROTO_IPIP,
  38. .init_state = ipip_init_state,
  39. .destructor = ipip_destroy,
  40. .input = ipip_xfrm_rcv,
  41. .output = ipip_output
  42. };
  43. static int xfrm_tunnel_err(struct sk_buff *skb, u32 info)
  44. {
  45. return -ENOENT;
  46. }
  47. static struct xfrm_tunnel xfrm_tunnel_handler = {
  48. .handler = xfrm4_rcv,
  49. .err_handler = xfrm_tunnel_err,
  50. .priority = 2,
  51. };
  52. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  53. static struct xfrm_tunnel xfrm64_tunnel_handler = {
  54. .handler = xfrm4_rcv,
  55. .err_handler = xfrm_tunnel_err,
  56. .priority = 2,
  57. };
  58. #endif
  59. static int __init ipip_init(void)
  60. {
  61. if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
  62. printk(KERN_INFO "ipip init: can't add xfrm type\n");
  63. return -EAGAIN;
  64. }
  65. if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) {
  66. printk(KERN_INFO "ipip init: can't add xfrm handler for AF_INET\n");
  67. xfrm_unregister_type(&ipip_type, AF_INET);
  68. return -EAGAIN;
  69. }
  70. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  71. if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) {
  72. printk(KERN_INFO "ipip init: can't add xfrm handler for AF_INET6\n");
  73. xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET);
  74. xfrm_unregister_type(&ipip_type, AF_INET);
  75. return -EAGAIN;
  76. }
  77. #endif
  78. return 0;
  79. }
  80. static void __exit ipip_fini(void)
  81. {
  82. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  83. if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6))
  84. printk(KERN_INFO "ipip close: can't remove xfrm handler for AF_INET6\n");
  85. #endif
  86. if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET))
  87. printk(KERN_INFO "ipip close: can't remove xfrm handler for AF_INET\n");
  88. if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
  89. printk(KERN_INFO "ipip close: can't remove xfrm type\n");
  90. }
  91. module_init(ipip_init);
  92. module_exit(ipip_fini);
  93. MODULE_LICENSE("GPL");
  94. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP);