gre_demux.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
  54. bool *csum_err, __be16 proto, int nhs)
  55. {
  56. const struct gre_base_hdr *greh;
  57. __be32 *options;
  58. int hdr_len;
  59. if (unlikely(!pskb_may_pull(skb, nhs + sizeof(struct gre_base_hdr))))
  60. return -EINVAL;
  61. greh = (struct gre_base_hdr *)(skb->data + nhs);
  62. if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
  63. return -EINVAL;
  64. tpi->flags = gre_flags_to_tnl_flags(greh->flags);
  65. hdr_len = gre_calc_hlen(tpi->flags);
  66. if (!pskb_may_pull(skb, nhs + hdr_len))
  67. return -EINVAL;
  68. greh = (struct gre_base_hdr *)(skb->data + nhs);
  69. tpi->proto = greh->protocol;
  70. options = (__be32 *)(greh + 1);
  71. if (greh->flags & GRE_CSUM) {
  72. if (skb_checksum_simple_validate(skb)) {
  73. *csum_err = true;
  74. return -EINVAL;
  75. }
  76. skb_checksum_try_convert(skb, IPPROTO_GRE, 0,
  77. null_compute_pseudo);
  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. /* ERSPAN ver 1 and 2 protocol sets GRE key field
  103. * to 0 and sets the configured key in the
  104. * inner erspan header field
  105. */
  106. if (greh->protocol == htons(ETH_P_ERSPAN) ||
  107. greh->protocol == htons(ETH_P_ERSPAN2)) {
  108. struct erspan_base_hdr *ershdr;
  109. if (!pskb_may_pull(skb, nhs + hdr_len + sizeof(*ershdr)))
  110. return -EINVAL;
  111. ershdr = (struct erspan_base_hdr *)options;
  112. tpi->key = cpu_to_be32(get_session_id(ershdr));
  113. }
  114. return hdr_len;
  115. }
  116. EXPORT_SYMBOL(gre_parse_header);
  117. static int gre_rcv(struct sk_buff *skb)
  118. {
  119. const struct gre_protocol *proto;
  120. u8 ver;
  121. int ret;
  122. if (!pskb_may_pull(skb, 12))
  123. goto drop;
  124. ver = skb->data[1]&0x7f;
  125. if (ver >= GREPROTO_MAX)
  126. goto drop;
  127. rcu_read_lock();
  128. proto = rcu_dereference(gre_proto[ver]);
  129. if (!proto || !proto->handler)
  130. goto drop_unlock;
  131. ret = proto->handler(skb);
  132. rcu_read_unlock();
  133. return ret;
  134. drop_unlock:
  135. rcu_read_unlock();
  136. drop:
  137. kfree_skb(skb);
  138. return NET_RX_DROP;
  139. }
  140. static void gre_err(struct sk_buff *skb, u32 info)
  141. {
  142. const struct gre_protocol *proto;
  143. const struct iphdr *iph = (const struct iphdr *)skb->data;
  144. u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
  145. if (ver >= GREPROTO_MAX)
  146. return;
  147. rcu_read_lock();
  148. proto = rcu_dereference(gre_proto[ver]);
  149. if (proto && proto->err_handler)
  150. proto->err_handler(skb, info);
  151. rcu_read_unlock();
  152. }
  153. static const struct net_protocol net_gre_protocol = {
  154. .handler = gre_rcv,
  155. .err_handler = gre_err,
  156. .netns_ok = 1,
  157. };
  158. static int __init gre_init(void)
  159. {
  160. pr_info("GRE over IPv4 demultiplexor driver\n");
  161. if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
  162. pr_err("can't add protocol\n");
  163. return -EAGAIN;
  164. }
  165. return 0;
  166. }
  167. static void __exit gre_exit(void)
  168. {
  169. inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
  170. }
  171. module_init(gre_init);
  172. module_exit(gre_exit);
  173. MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
  174. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  175. MODULE_LICENSE("GPL");