gre.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/ip.h>
  18. #include <linux/netdevice.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. const struct iphdr *iph = (const struct iphdr *)skb->data;
  85. u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
  86. if (ver >= GREPROTO_MAX)
  87. return;
  88. rcu_read_lock();
  89. proto = rcu_dereference(gre_proto[ver]);
  90. if (proto && proto->err_handler)
  91. proto->err_handler(skb, info);
  92. rcu_read_unlock();
  93. }
  94. static const struct net_protocol net_gre_protocol = {
  95. .handler = gre_rcv,
  96. .err_handler = gre_err,
  97. .netns_ok = 1,
  98. };
  99. static int __init gre_init(void)
  100. {
  101. pr_info("GRE over IPv4 demultiplexor driver");
  102. if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
  103. pr_err("gre: can't add protocol\n");
  104. return -EAGAIN;
  105. }
  106. return 0;
  107. }
  108. static void __exit gre_exit(void)
  109. {
  110. inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
  111. }
  112. module_init(gre_init);
  113. module_exit(gre_exit);
  114. MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
  115. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  116. MODULE_LICENSE("GPL");