gre_demux.c 2.8 KB

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