gre_demux.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/erspan.h>
  27. #include <net/icmp.h>
  28. #include <net/route.h>
  29. #include <net/xfrm.h>
  30. static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
  31. int gre_add_protocol(const struct gre_protocol *proto, u8 version)
  32. {
  33. if (version >= GREPROTO_MAX)
  34. return -EINVAL;
  35. return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
  36. 0 : -EBUSY;
  37. }
  38. EXPORT_SYMBOL_GPL(gre_add_protocol);
  39. int gre_del_protocol(const struct gre_protocol *proto, u8 version)
  40. {
  41. int ret;
  42. if (version >= GREPROTO_MAX)
  43. return -EINVAL;
  44. ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
  45. 0 : -EBUSY;
  46. if (ret)
  47. return ret;
  48. synchronize_rcu();
  49. return 0;
  50. }
  51. EXPORT_SYMBOL_GPL(gre_del_protocol);
  52. /* Fills in tpi and returns header length to be pulled.
  53. * Note that caller must use pskb_may_pull() before pulling GRE header.
  54. */
  55. int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
  56. bool *csum_err, __be16 proto, int nhs)
  57. {
  58. const struct gre_base_hdr *greh;
  59. __be32 *options;
  60. int hdr_len;
  61. if (unlikely(!pskb_may_pull(skb, nhs + sizeof(struct gre_base_hdr))))
  62. return -EINVAL;
  63. greh = (struct gre_base_hdr *)(skb->data + nhs);
  64. if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
  65. return -EINVAL;
  66. tpi->flags = gre_flags_to_tnl_flags(greh->flags);
  67. hdr_len = gre_calc_hlen(tpi->flags);
  68. if (!pskb_may_pull(skb, nhs + hdr_len))
  69. return -EINVAL;
  70. greh = (struct gre_base_hdr *)(skb->data + nhs);
  71. tpi->proto = greh->protocol;
  72. options = (__be32 *)(greh + 1);
  73. if (greh->flags & GRE_CSUM) {
  74. if (!skb_checksum_simple_validate(skb)) {
  75. skb_checksum_try_convert(skb, IPPROTO_GRE, 0,
  76. null_compute_pseudo);
  77. } else if (csum_err) {
  78. *csum_err = true;
  79. return -EINVAL;
  80. }
  81. options++;
  82. }
  83. if (greh->flags & GRE_KEY) {
  84. tpi->key = *options;
  85. options++;
  86. } else {
  87. tpi->key = 0;
  88. }
  89. if (unlikely(greh->flags & GRE_SEQ)) {
  90. tpi->seq = *options;
  91. options++;
  92. } else {
  93. tpi->seq = 0;
  94. }
  95. /* WCCP version 1 and 2 protocol decoding.
  96. * - Change protocol to IPv4/IPv6
  97. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  98. */
  99. if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
  100. u8 _val, *val;
  101. val = skb_header_pointer(skb, nhs + hdr_len,
  102. sizeof(_val), &_val);
  103. if (!val)
  104. return -EINVAL;
  105. tpi->proto = proto;
  106. if ((*val & 0xF0) != 0x40)
  107. hdr_len += 4;
  108. }
  109. tpi->hdr_len = hdr_len;
  110. /* ERSPAN ver 1 and 2 protocol sets GRE key field
  111. * to 0 and sets the configured key in the
  112. * inner erspan header field
  113. */
  114. if (greh->protocol == htons(ETH_P_ERSPAN) ||
  115. greh->protocol == htons(ETH_P_ERSPAN2)) {
  116. struct erspan_base_hdr *ershdr;
  117. if (!pskb_may_pull(skb, nhs + hdr_len + sizeof(*ershdr)))
  118. return -EINVAL;
  119. ershdr = (struct erspan_base_hdr *)(skb->data + nhs + hdr_len);
  120. tpi->key = cpu_to_be32(get_session_id(ershdr));
  121. }
  122. return hdr_len;
  123. }
  124. EXPORT_SYMBOL(gre_parse_header);
  125. static int gre_rcv(struct sk_buff *skb)
  126. {
  127. const struct gre_protocol *proto;
  128. u8 ver;
  129. int ret;
  130. if (!pskb_may_pull(skb, 12))
  131. goto drop;
  132. ver = skb->data[1]&0x7f;
  133. if (ver >= GREPROTO_MAX)
  134. goto drop;
  135. rcu_read_lock();
  136. proto = rcu_dereference(gre_proto[ver]);
  137. if (!proto || !proto->handler)
  138. goto drop_unlock;
  139. ret = proto->handler(skb);
  140. rcu_read_unlock();
  141. return ret;
  142. drop_unlock:
  143. rcu_read_unlock();
  144. drop:
  145. kfree_skb(skb);
  146. return NET_RX_DROP;
  147. }
  148. static void gre_err(struct sk_buff *skb, u32 info)
  149. {
  150. const struct gre_protocol *proto;
  151. const struct iphdr *iph = (const struct iphdr *)skb->data;
  152. u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
  153. if (ver >= GREPROTO_MAX)
  154. return;
  155. rcu_read_lock();
  156. proto = rcu_dereference(gre_proto[ver]);
  157. if (proto && proto->err_handler)
  158. proto->err_handler(skb, info);
  159. rcu_read_unlock();
  160. }
  161. static const struct net_protocol net_gre_protocol = {
  162. .handler = gre_rcv,
  163. .err_handler = gre_err,
  164. .netns_ok = 1,
  165. };
  166. static int __init gre_init(void)
  167. {
  168. pr_info("GRE over IPv4 demultiplexor driver\n");
  169. if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
  170. pr_err("can't add protocol\n");
  171. return -EAGAIN;
  172. }
  173. return 0;
  174. }
  175. static void __exit gre_exit(void)
  176. {
  177. inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
  178. }
  179. module_init(gre_init);
  180. module_exit(gre_exit);
  181. MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
  182. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  183. MODULE_LICENSE("GPL");