tunnel4.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 struct xfrm_tunnel *tunnel64_handlers;
  16. static DEFINE_MUTEX(tunnel4_mutex);
  17. static inline struct xfrm_tunnel **fam_handlers(unsigned short family)
  18. {
  19. return (family == AF_INET) ? &tunnel4_handlers : &tunnel64_handlers;
  20. }
  21. int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
  22. {
  23. struct xfrm_tunnel **pprev;
  24. int ret = -EEXIST;
  25. int priority = handler->priority;
  26. mutex_lock(&tunnel4_mutex);
  27. for (pprev = fam_handlers(family); *pprev; pprev = &(*pprev)->next) {
  28. if ((*pprev)->priority > priority)
  29. break;
  30. if ((*pprev)->priority == priority)
  31. goto err;
  32. }
  33. handler->next = *pprev;
  34. *pprev = handler;
  35. ret = 0;
  36. err:
  37. mutex_unlock(&tunnel4_mutex);
  38. return ret;
  39. }
  40. EXPORT_SYMBOL(xfrm4_tunnel_register);
  41. int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
  42. {
  43. struct xfrm_tunnel **pprev;
  44. int ret = -ENOENT;
  45. mutex_lock(&tunnel4_mutex);
  46. for (pprev = fam_handlers(family); *pprev; pprev = &(*pprev)->next) {
  47. if (*pprev == handler) {
  48. *pprev = handler->next;
  49. ret = 0;
  50. break;
  51. }
  52. }
  53. mutex_unlock(&tunnel4_mutex);
  54. synchronize_net();
  55. return ret;
  56. }
  57. EXPORT_SYMBOL(xfrm4_tunnel_deregister);
  58. static int tunnel4_rcv(struct sk_buff *skb)
  59. {
  60. struct xfrm_tunnel *handler;
  61. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  62. goto drop;
  63. for (handler = tunnel4_handlers; handler; handler = handler->next)
  64. if (!handler->handler(skb))
  65. return 0;
  66. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  67. drop:
  68. kfree_skb(skb);
  69. return 0;
  70. }
  71. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  72. static int tunnel64_rcv(struct sk_buff *skb)
  73. {
  74. struct xfrm_tunnel *handler;
  75. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  76. goto drop;
  77. for (handler = tunnel64_handlers; handler; handler = handler->next)
  78. if (!handler->handler(skb))
  79. return 0;
  80. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  81. drop:
  82. kfree_skb(skb);
  83. return 0;
  84. }
  85. #endif
  86. static void tunnel4_err(struct sk_buff *skb, u32 info)
  87. {
  88. struct xfrm_tunnel *handler;
  89. for (handler = tunnel4_handlers; handler; handler = handler->next)
  90. if (!handler->err_handler(skb, info))
  91. break;
  92. }
  93. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  94. static void tunnel64_err(struct sk_buff *skb, u32 info)
  95. {
  96. struct xfrm_tunnel *handler;
  97. for (handler = tunnel64_handlers; handler; handler = handler->next)
  98. if (!handler->err_handler(skb, info))
  99. break;
  100. }
  101. #endif
  102. static struct net_protocol tunnel4_protocol = {
  103. .handler = tunnel4_rcv,
  104. .err_handler = tunnel4_err,
  105. .no_policy = 1,
  106. .netns_ok = 1,
  107. };
  108. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  109. static struct net_protocol tunnel64_protocol = {
  110. .handler = tunnel64_rcv,
  111. .err_handler = tunnel64_err,
  112. .no_policy = 1,
  113. .netns_ok = 1,
  114. };
  115. #endif
  116. static int __init tunnel4_init(void)
  117. {
  118. if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) {
  119. printk(KERN_ERR "tunnel4 init: can't add protocol\n");
  120. return -EAGAIN;
  121. }
  122. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  123. if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
  124. printk(KERN_ERR "tunnel64 init: can't add protocol\n");
  125. inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
  126. return -EAGAIN;
  127. }
  128. #endif
  129. return 0;
  130. }
  131. static void __exit tunnel4_fini(void)
  132. {
  133. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  134. if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
  135. printk(KERN_ERR "tunnel64 close: can't remove protocol\n");
  136. #endif
  137. if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
  138. printk(KERN_ERR "tunnel4 close: can't remove protocol\n");
  139. }
  140. module_init(tunnel4_init);
  141. module_exit(tunnel4_fini);
  142. MODULE_LICENSE("GPL");