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 <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. u32 arg = info;
  63. if (handler)
  64. handler->err_handler(skb, &arg);
  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");