gre.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. static const struct gre_protocol __rcu *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 (rcu_dereference_protected(gre_proto[version],
  46. lockdep_is_held(&gre_proto_lock)) != proto)
  47. goto err_out_unlock;
  48. rcu_assign_pointer(gre_proto[version], NULL);
  49. spin_unlock(&gre_proto_lock);
  50. synchronize_rcu();
  51. return 0;
  52. err_out_unlock:
  53. spin_unlock(&gre_proto_lock);
  54. err_out:
  55. return -1;
  56. }
  57. EXPORT_SYMBOL_GPL(gre_del_protocol);
  58. static int gre_rcv(struct sk_buff *skb)
  59. {
  60. const struct gre_protocol *proto;
  61. u8 ver;
  62. int ret;
  63. if (!pskb_may_pull(skb, 12))
  64. goto drop;
  65. ver = skb->data[1]&0x7f;
  66. if (ver >= GREPROTO_MAX)
  67. goto drop;
  68. rcu_read_lock();
  69. proto = rcu_dereference(gre_proto[ver]);
  70. if (!proto || !proto->handler)
  71. goto drop_unlock;
  72. ret = proto->handler(skb);
  73. rcu_read_unlock();
  74. return ret;
  75. drop_unlock:
  76. rcu_read_unlock();
  77. drop:
  78. kfree_skb(skb);
  79. return NET_RX_DROP;
  80. }
  81. static void gre_err(struct sk_buff *skb, u32 info)
  82. {
  83. const struct gre_protocol *proto;
  84. u8 ver;
  85. if (!pskb_may_pull(skb, 12))
  86. goto drop;
  87. ver = skb->data[1]&0x7f;
  88. if (ver >= GREPROTO_MAX)
  89. goto drop;
  90. rcu_read_lock();
  91. proto = rcu_dereference(gre_proto[ver]);
  92. if (!proto || !proto->err_handler)
  93. goto drop_unlock;
  94. proto->err_handler(skb, info);
  95. rcu_read_unlock();
  96. return;
  97. drop_unlock:
  98. rcu_read_unlock();
  99. drop:
  100. kfree_skb(skb);
  101. }
  102. static const struct net_protocol net_gre_protocol = {
  103. .handler = gre_rcv,
  104. .err_handler = gre_err,
  105. .netns_ok = 1,
  106. };
  107. static int __init gre_init(void)
  108. {
  109. pr_info("GRE over IPv4 demultiplexor driver");
  110. if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
  111. pr_err("gre: can't add protocol\n");
  112. return -EAGAIN;
  113. }
  114. return 0;
  115. }
  116. static void __exit gre_exit(void)
  117. {
  118. inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
  119. }
  120. module_init(gre_init);
  121. module_exit(gre_exit);
  122. MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
  123. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  124. MODULE_LICENSE("GPL");