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