tunnel4.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* tunnel4.c: Generic IP tunnel transformer.
  2. *
  3. * Copyright (C) 2003 David S. Miller (davem@redhat.com)
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/skbuff.h>
  10. #include <net/icmp.h>
  11. #include <net/ip.h>
  12. #include <net/protocol.h>
  13. #include <net/xfrm.h>
  14. static struct xfrm_tunnel *tunnel4_handlers;
  15. static DEFINE_MUTEX(tunnel4_mutex);
  16. int xfrm4_tunnel_register(struct xfrm_tunnel *handler)
  17. {
  18. struct xfrm_tunnel **pprev;
  19. int ret = -EEXIST;
  20. int priority = handler->priority;
  21. mutex_lock(&tunnel4_mutex);
  22. for (pprev = &tunnel4_handlers; *pprev; pprev = &(*pprev)->next) {
  23. if ((*pprev)->priority > priority)
  24. break;
  25. if ((*pprev)->priority == priority)
  26. goto err;
  27. }
  28. handler->next = *pprev;
  29. *pprev = handler;
  30. ret = 0;
  31. err:
  32. mutex_unlock(&tunnel4_mutex);
  33. return ret;
  34. }
  35. EXPORT_SYMBOL(xfrm4_tunnel_register);
  36. int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler)
  37. {
  38. struct xfrm_tunnel **pprev;
  39. int ret = -ENOENT;
  40. mutex_lock(&tunnel4_mutex);
  41. for (pprev = &tunnel4_handlers; *pprev; pprev = &(*pprev)->next) {
  42. if (*pprev == handler) {
  43. *pprev = handler->next;
  44. ret = 0;
  45. break;
  46. }
  47. }
  48. mutex_unlock(&tunnel4_mutex);
  49. synchronize_net();
  50. return ret;
  51. }
  52. EXPORT_SYMBOL(xfrm4_tunnel_deregister);
  53. static int tunnel4_rcv(struct sk_buff *skb)
  54. {
  55. struct xfrm_tunnel *handler;
  56. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  57. goto drop;
  58. for (handler = tunnel4_handlers; handler; handler = handler->next)
  59. if (!handler->handler(skb))
  60. return 0;
  61. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  62. drop:
  63. kfree_skb(skb);
  64. return 0;
  65. }
  66. static void tunnel4_err(struct sk_buff *skb, u32 info)
  67. {
  68. struct xfrm_tunnel *handler;
  69. for (handler = tunnel4_handlers; handler; handler = handler->next)
  70. if (!handler->err_handler(skb, info))
  71. break;
  72. }
  73. static struct net_protocol tunnel4_protocol = {
  74. .handler = tunnel4_rcv,
  75. .err_handler = tunnel4_err,
  76. .no_policy = 1,
  77. };
  78. static int __init tunnel4_init(void)
  79. {
  80. if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) {
  81. printk(KERN_ERR "tunnel4 init: can't add protocol\n");
  82. return -EAGAIN;
  83. }
  84. return 0;
  85. }
  86. static void __exit tunnel4_fini(void)
  87. {
  88. if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
  89. printk(KERN_ERR "tunnel4 close: can't remove protocol\n");
  90. }
  91. module_init(tunnel4_init);
  92. module_exit(tunnel4_fini);
  93. MODULE_LICENSE("GPL");