gre_demux.c 4.8 KB

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