xfrm4_tunnel.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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;
  14. iph = skb->nh.iph;
  15. iph->tot_len = htons(skb->len);
  16. ip_send_check(iph);
  17. return 0;
  18. }
  19. static int ipip_xfrm_rcv(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
  20. {
  21. return 0;
  22. }
  23. static struct xfrm_tunnel *ipip_handler;
  24. static DEFINE_MUTEX(xfrm4_tunnel_mutex);
  25. int xfrm4_tunnel_register(struct xfrm_tunnel *handler)
  26. {
  27. int ret;
  28. mutex_lock(&xfrm4_tunnel_mutex);
  29. ret = 0;
  30. if (ipip_handler != NULL)
  31. ret = -EINVAL;
  32. if (!ret)
  33. ipip_handler = handler;
  34. mutex_unlock(&xfrm4_tunnel_mutex);
  35. return ret;
  36. }
  37. EXPORT_SYMBOL(xfrm4_tunnel_register);
  38. int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler)
  39. {
  40. int ret;
  41. mutex_lock(&xfrm4_tunnel_mutex);
  42. ret = 0;
  43. if (ipip_handler != handler)
  44. ret = -EINVAL;
  45. if (!ret)
  46. ipip_handler = NULL;
  47. mutex_unlock(&xfrm4_tunnel_mutex);
  48. synchronize_net();
  49. return ret;
  50. }
  51. EXPORT_SYMBOL(xfrm4_tunnel_deregister);
  52. static int ipip_rcv(struct sk_buff *skb)
  53. {
  54. struct xfrm_tunnel *handler = ipip_handler;
  55. /* Tunnel devices take precedence. */
  56. if (handler && handler->handler(skb) == 0)
  57. return 0;
  58. return xfrm4_rcv(skb);
  59. }
  60. static void ipip_err(struct sk_buff *skb, u32 info)
  61. {
  62. struct xfrm_tunnel *handler = ipip_handler;
  63. if (handler)
  64. handler->err_handler(skb, info);
  65. }
  66. static int ipip_init_state(struct xfrm_state *x)
  67. {
  68. if (!x->props.mode)
  69. return -EINVAL;
  70. if (x->encap)
  71. return -EINVAL;
  72. x->props.header_len = sizeof(struct iphdr);
  73. return 0;
  74. }
  75. static void ipip_destroy(struct xfrm_state *x)
  76. {
  77. }
  78. static struct xfrm_type ipip_type = {
  79. .description = "IPIP",
  80. .owner = THIS_MODULE,
  81. .proto = IPPROTO_IPIP,
  82. .init_state = ipip_init_state,
  83. .destructor = ipip_destroy,
  84. .input = ipip_xfrm_rcv,
  85. .output = ipip_output
  86. };
  87. static struct net_protocol ipip_protocol = {
  88. .handler = ipip_rcv,
  89. .err_handler = ipip_err,
  90. .no_policy = 1,
  91. };
  92. static int __init ipip_init(void)
  93. {
  94. if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
  95. printk(KERN_INFO "ipip init: can't add xfrm type\n");
  96. return -EAGAIN;
  97. }
  98. if (inet_add_protocol(&ipip_protocol, IPPROTO_IPIP) < 0) {
  99. printk(KERN_INFO "ipip init: can't add protocol\n");
  100. xfrm_unregister_type(&ipip_type, AF_INET);
  101. return -EAGAIN;
  102. }
  103. return 0;
  104. }
  105. static void __exit ipip_fini(void)
  106. {
  107. if (inet_del_protocol(&ipip_protocol, IPPROTO_IPIP) < 0)
  108. printk(KERN_INFO "ipip close: can't remove protocol\n");
  109. if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
  110. printk(KERN_INFO "ipip close: can't remove xfrm type\n");
  111. }
  112. module_init(ipip_init);
  113. module_exit(ipip_fini);
  114. MODULE_LICENSE("GPL");