xfrm4_tunnel.c 2.7 KB

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