gre.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * GRE over IPv4 demultiplexer driver
  3. *
  4. * Authors: Dmitry Kozlov (xeb@mail.ru)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kmod.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/in.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/version.h>
  19. #include <linux/spinlock.h>
  20. #include <net/protocol.h>
  21. #include <net/gre.h>
  22. const struct gre_protocol *gre_proto[GREPROTO_MAX] __read_mostly;
  23. static DEFINE_SPINLOCK(gre_proto_lock);
  24. int gre_add_protocol(const struct gre_protocol *proto, u8 version)
  25. {
  26. if (version >= GREPROTO_MAX)
  27. goto err_out;
  28. spin_lock(&gre_proto_lock);
  29. if (gre_proto[version])
  30. goto err_out_unlock;
  31. rcu_assign_pointer(gre_proto[version], proto);
  32. spin_unlock(&gre_proto_lock);
  33. return 0;
  34. err_out_unlock:
  35. spin_unlock(&gre_proto_lock);
  36. err_out:
  37. return -1;
  38. }
  39. EXPORT_SYMBOL_GPL(gre_add_protocol);
  40. int gre_del_protocol(const struct gre_protocol *proto, u8 version)
  41. {
  42. if (version >= GREPROTO_MAX)
  43. goto err_out;
  44. spin_lock(&gre_proto_lock);
  45. if (gre_proto[version] != proto)
  46. goto err_out_unlock;
  47. rcu_assign_pointer(gre_proto[version], NULL);
  48. spin_unlock(&gre_proto_lock);
  49. synchronize_rcu();
  50. return 0;
  51. err_out_unlock:
  52. spin_unlock(&gre_proto_lock);
  53. err_out:
  54. return -1;
  55. }
  56. EXPORT_SYMBOL_GPL(gre_del_protocol);
  57. static int gre_rcv(struct sk_buff *skb)
  58. {
  59. const struct gre_protocol *proto;
  60. u8 ver;
  61. int ret;
  62. if (!pskb_may_pull(skb, 12))
  63. goto drop;
  64. ver = skb->data[1]&0x7f;
  65. if (ver >= GREPROTO_MAX)
  66. goto drop;
  67. rcu_read_lock();
  68. proto = rcu_dereference(gre_proto[ver]);
  69. if (!proto || !proto->handler)
  70. goto drop_unlock;
  71. ret = proto->handler(skb);
  72. rcu_read_unlock();
  73. return ret;
  74. drop_unlock:
  75. rcu_read_unlock();
  76. drop:
  77. kfree_skb(skb);
  78. return NET_RX_DROP;
  79. }
  80. static void gre_err(struct sk_buff *skb, u32 info)
  81. {
  82. const struct gre_protocol *proto;
  83. u8 ver;
  84. if (!pskb_may_pull(skb, 12))
  85. goto drop;
  86. ver = skb->data[1]&0x7f;
  87. if (ver >= GREPROTO_MAX)
  88. goto drop;
  89. rcu_read_lock();
  90. proto = rcu_dereference(gre_proto[ver]);
  91. if (!proto || !proto->err_handler)
  92. goto drop_unlock;
  93. proto->err_handler(skb, info);
  94. rcu_read_unlock();
  95. return;
  96. drop_unlock:
  97. rcu_read_unlock();
  98. drop:
  99. kfree_skb(skb);
  100. }
  101. static const struct net_protocol net_gre_protocol = {
  102. .handler = gre_rcv,
  103. .err_handler = gre_err,
  104. .netns_ok = 1,
  105. };
  106. static int __init gre_init(void)
  107. {
  108. pr_info("GRE over IPv4 demultiplexor driver");
  109. if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
  110. pr_err("gre: can't add protocol\n");
  111. return -EAGAIN;
  112. }
  113. return 0;
  114. }
  115. static void __exit gre_exit(void)
  116. {
  117. inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
  118. }
  119. module_init(gre_init);
  120. module_exit(gre_exit);
  121. MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
  122. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  123. MODULE_LICENSE("GPL");