gre_demux.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /* Fills in tpi and returns header length to be pulled. */
  52. int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
  53. bool *csum_err, __be16 proto, int nhs)
  54. {
  55. const struct gre_base_hdr *greh;
  56. __be32 *options;
  57. int hdr_len;
  58. if (unlikely(!pskb_may_pull(skb, nhs + sizeof(struct gre_base_hdr))))
  59. return -EINVAL;
  60. greh = (struct gre_base_hdr *)(skb->data + nhs);
  61. if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
  62. return -EINVAL;
  63. tpi->flags = gre_flags_to_tnl_flags(greh->flags);
  64. hdr_len = gre_calc_hlen(tpi->flags);
  65. if (!pskb_may_pull(skb, nhs + hdr_len))
  66. return -EINVAL;
  67. greh = (struct gre_base_hdr *)(skb->data + nhs);
  68. tpi->proto = greh->protocol;
  69. options = (__be32 *)(greh + 1);
  70. if (greh->flags & GRE_CSUM) {
  71. if (!skb_checksum_simple_validate(skb)) {
  72. skb_checksum_try_convert(skb, IPPROTO_GRE, 0,
  73. null_compute_pseudo);
  74. } else if (csum_err) {
  75. *csum_err = true;
  76. return -EINVAL;
  77. }
  78. options++;
  79. }
  80. if (greh->flags & GRE_KEY) {
  81. tpi->key = *options;
  82. options++;
  83. } else {
  84. tpi->key = 0;
  85. }
  86. if (unlikely(greh->flags & GRE_SEQ)) {
  87. tpi->seq = *options;
  88. options++;
  89. } else {
  90. tpi->seq = 0;
  91. }
  92. /* WCCP version 1 and 2 protocol decoding.
  93. * - Change protocol to IPv4/IPv6
  94. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  95. */
  96. if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
  97. tpi->proto = proto;
  98. if ((*(u8 *)options & 0xF0) != 0x40)
  99. hdr_len += 4;
  100. }
  101. tpi->hdr_len = hdr_len;
  102. return hdr_len;
  103. }
  104. EXPORT_SYMBOL(gre_parse_header);
  105. static int gre_rcv(struct sk_buff *skb)
  106. {
  107. const struct gre_protocol *proto;
  108. u8 ver;
  109. int ret;
  110. if (!pskb_may_pull(skb, 12))
  111. goto drop;
  112. ver = skb->data[1]&0x7f;
  113. if (ver >= GREPROTO_MAX)
  114. goto drop;
  115. rcu_read_lock();
  116. proto = rcu_dereference(gre_proto[ver]);
  117. if (!proto || !proto->handler)
  118. goto drop_unlock;
  119. ret = proto->handler(skb);
  120. rcu_read_unlock();
  121. return ret;
  122. drop_unlock:
  123. rcu_read_unlock();
  124. drop:
  125. kfree_skb(skb);
  126. return NET_RX_DROP;
  127. }
  128. static void gre_err(struct sk_buff *skb, u32 info)
  129. {
  130. const struct gre_protocol *proto;
  131. const struct iphdr *iph = (const struct iphdr *)skb->data;
  132. u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
  133. if (ver >= GREPROTO_MAX)
  134. return;
  135. rcu_read_lock();
  136. proto = rcu_dereference(gre_proto[ver]);
  137. if (proto && proto->err_handler)
  138. proto->err_handler(skb, info);
  139. rcu_read_unlock();
  140. }
  141. static const struct net_protocol net_gre_protocol = {
  142. .handler = gre_rcv,
  143. .err_handler = gre_err,
  144. .netns_ok = 1,
  145. };
  146. static int __init gre_init(void)
  147. {
  148. pr_info("GRE over IPv4 demultiplexor driver\n");
  149. if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
  150. pr_err("can't add protocol\n");
  151. return -EAGAIN;
  152. }
  153. return 0;
  154. }
  155. static void __exit gre_exit(void)
  156. {
  157. inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
  158. }
  159. module_init(gre_init);
  160. module_exit(gre_exit);
  161. MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
  162. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  163. MODULE_LICENSE("GPL");