gre.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kmod.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/in.h>
  18. #include <linux/ip.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/spinlock.h>
  21. #include <net/protocol.h>
  22. #include <net/gre.h>
  23. static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
  24. static DEFINE_SPINLOCK(gre_proto_lock);
  25. int gre_add_protocol(const struct gre_protocol *proto, u8 version)
  26. {
  27. if (version >= GREPROTO_MAX)
  28. goto err_out;
  29. spin_lock(&gre_proto_lock);
  30. if (gre_proto[version])
  31. goto err_out_unlock;
  32. RCU_INIT_POINTER(gre_proto[version], proto);
  33. spin_unlock(&gre_proto_lock);
  34. return 0;
  35. err_out_unlock:
  36. spin_unlock(&gre_proto_lock);
  37. err_out:
  38. return -1;
  39. }
  40. EXPORT_SYMBOL_GPL(gre_add_protocol);
  41. int gre_del_protocol(const struct gre_protocol *proto, u8 version)
  42. {
  43. if (version >= GREPROTO_MAX)
  44. goto err_out;
  45. spin_lock(&gre_proto_lock);
  46. if (rcu_dereference_protected(gre_proto[version],
  47. lockdep_is_held(&gre_proto_lock)) != proto)
  48. goto err_out_unlock;
  49. RCU_INIT_POINTER(gre_proto[version], NULL);
  50. spin_unlock(&gre_proto_lock);
  51. synchronize_rcu();
  52. return 0;
  53. err_out_unlock:
  54. spin_unlock(&gre_proto_lock);
  55. err_out:
  56. return -1;
  57. }
  58. EXPORT_SYMBOL_GPL(gre_del_protocol);
  59. static int gre_rcv(struct sk_buff *skb)
  60. {
  61. const struct gre_protocol *proto;
  62. u8 ver;
  63. int ret;
  64. if (!pskb_may_pull(skb, 12))
  65. goto drop;
  66. ver = skb->data[1]&0x7f;
  67. if (ver >= GREPROTO_MAX)
  68. goto drop;
  69. rcu_read_lock();
  70. proto = rcu_dereference(gre_proto[ver]);
  71. if (!proto || !proto->handler)
  72. goto drop_unlock;
  73. ret = proto->handler(skb);
  74. rcu_read_unlock();
  75. return ret;
  76. drop_unlock:
  77. rcu_read_unlock();
  78. drop:
  79. kfree_skb(skb);
  80. return NET_RX_DROP;
  81. }
  82. static void gre_err(struct sk_buff *skb, u32 info)
  83. {
  84. const struct gre_protocol *proto;
  85. const struct iphdr *iph = (const struct iphdr *)skb->data;
  86. u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
  87. if (ver >= GREPROTO_MAX)
  88. return;
  89. rcu_read_lock();
  90. proto = rcu_dereference(gre_proto[ver]);
  91. if (proto && proto->err_handler)
  92. proto->err_handler(skb, info);
  93. rcu_read_unlock();
  94. }
  95. static const struct net_protocol net_gre_protocol = {
  96. .handler = gre_rcv,
  97. .err_handler = gre_err,
  98. .netns_ok = 1,
  99. };
  100. static int __init gre_init(void)
  101. {
  102. pr_info("GRE over IPv4 demultiplexor driver\n");
  103. if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
  104. pr_err("can't add protocol\n");
  105. return -EAGAIN;
  106. }
  107. return 0;
  108. }
  109. static void __exit gre_exit(void)
  110. {
  111. inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
  112. }
  113. module_init(gre_init);
  114. module_exit(gre_exit);
  115. MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
  116. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  117. MODULE_LICENSE("GPL");