gre_demux.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. static struct gre_cisco_protocol __rcu *gre_cisco_proto_list[GRE_IP_PROTO_MAX];
  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. void gre_build_header(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
  53. int hdr_len)
  54. {
  55. struct gre_base_hdr *greh;
  56. skb_push(skb, hdr_len);
  57. skb_reset_transport_header(skb);
  58. greh = (struct gre_base_hdr *)skb->data;
  59. greh->flags = tnl_flags_to_gre_flags(tpi->flags);
  60. greh->protocol = tpi->proto;
  61. if (tpi->flags&(TUNNEL_KEY|TUNNEL_CSUM|TUNNEL_SEQ)) {
  62. __be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4);
  63. if (tpi->flags&TUNNEL_SEQ) {
  64. *ptr = tpi->seq;
  65. ptr--;
  66. }
  67. if (tpi->flags&TUNNEL_KEY) {
  68. *ptr = tpi->key;
  69. ptr--;
  70. }
  71. if (tpi->flags&TUNNEL_CSUM &&
  72. !(skb_shinfo(skb)->gso_type &
  73. (SKB_GSO_GRE|SKB_GSO_GRE_CSUM))) {
  74. *ptr = 0;
  75. *(__sum16 *)ptr = csum_fold(skb_checksum(skb, 0,
  76. skb->len, 0));
  77. }
  78. }
  79. }
  80. EXPORT_SYMBOL_GPL(gre_build_header);
  81. static int parse_gre_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
  82. bool *csum_err)
  83. {
  84. unsigned int ip_hlen = ip_hdrlen(skb);
  85. const struct gre_base_hdr *greh;
  86. __be32 *options;
  87. int hdr_len;
  88. if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr))))
  89. return -EINVAL;
  90. greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen);
  91. if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
  92. return -EINVAL;
  93. tpi->flags = gre_flags_to_tnl_flags(greh->flags);
  94. hdr_len = ip_gre_calc_hlen(tpi->flags);
  95. if (!pskb_may_pull(skb, hdr_len))
  96. return -EINVAL;
  97. greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen);
  98. tpi->proto = greh->protocol;
  99. options = (__be32 *)(greh + 1);
  100. if (greh->flags & GRE_CSUM) {
  101. if (skb_checksum_simple_validate(skb)) {
  102. *csum_err = true;
  103. return -EINVAL;
  104. }
  105. options++;
  106. }
  107. if (greh->flags & GRE_KEY) {
  108. tpi->key = *options;
  109. options++;
  110. } else
  111. tpi->key = 0;
  112. if (unlikely(greh->flags & GRE_SEQ)) {
  113. tpi->seq = *options;
  114. options++;
  115. } else
  116. tpi->seq = 0;
  117. /* WCCP version 1 and 2 protocol decoding.
  118. * - Change protocol to IP
  119. * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
  120. */
  121. if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
  122. tpi->proto = htons(ETH_P_IP);
  123. if ((*(u8 *)options & 0xF0) != 0x40) {
  124. hdr_len += 4;
  125. if (!pskb_may_pull(skb, hdr_len))
  126. return -EINVAL;
  127. }
  128. }
  129. return iptunnel_pull_header(skb, hdr_len, tpi->proto);
  130. }
  131. static int gre_cisco_rcv(struct sk_buff *skb)
  132. {
  133. struct tnl_ptk_info tpi;
  134. int i;
  135. bool csum_err = false;
  136. #ifdef CONFIG_NET_IPGRE_BROADCAST
  137. if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
  138. /* Looped back packet, drop it! */
  139. if (rt_is_output_route(skb_rtable(skb)))
  140. goto drop;
  141. }
  142. #endif
  143. if (parse_gre_header(skb, &tpi, &csum_err) < 0)
  144. goto drop;
  145. rcu_read_lock();
  146. for (i = 0; i < GRE_IP_PROTO_MAX; i++) {
  147. struct gre_cisco_protocol *proto;
  148. int ret;
  149. proto = rcu_dereference(gre_cisco_proto_list[i]);
  150. if (!proto)
  151. continue;
  152. ret = proto->handler(skb, &tpi);
  153. if (ret == PACKET_RCVD) {
  154. rcu_read_unlock();
  155. return 0;
  156. }
  157. }
  158. rcu_read_unlock();
  159. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  160. drop:
  161. kfree_skb(skb);
  162. return 0;
  163. }
  164. static void gre_cisco_err(struct sk_buff *skb, u32 info)
  165. {
  166. /* All the routers (except for Linux) return only
  167. * 8 bytes of packet payload. It means, that precise relaying of
  168. * ICMP in the real Internet is absolutely infeasible.
  169. *
  170. * Moreover, Cisco "wise men" put GRE key to the third word
  171. * in GRE header. It makes impossible maintaining even soft
  172. * state for keyed
  173. * GRE tunnels with enabled checksum. Tell them "thank you".
  174. *
  175. * Well, I wonder, rfc1812 was written by Cisco employee,
  176. * what the hell these idiots break standards established
  177. * by themselves???
  178. */
  179. const int type = icmp_hdr(skb)->type;
  180. const int code = icmp_hdr(skb)->code;
  181. struct tnl_ptk_info tpi;
  182. bool csum_err = false;
  183. int i;
  184. if (parse_gre_header(skb, &tpi, &csum_err)) {
  185. if (!csum_err) /* ignore csum errors. */
  186. return;
  187. }
  188. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  189. ipv4_update_pmtu(skb, dev_net(skb->dev), info,
  190. skb->dev->ifindex, 0, IPPROTO_GRE, 0);
  191. return;
  192. }
  193. if (type == ICMP_REDIRECT) {
  194. ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex, 0,
  195. IPPROTO_GRE, 0);
  196. return;
  197. }
  198. rcu_read_lock();
  199. for (i = 0; i < GRE_IP_PROTO_MAX; i++) {
  200. struct gre_cisco_protocol *proto;
  201. proto = rcu_dereference(gre_cisco_proto_list[i]);
  202. if (!proto)
  203. continue;
  204. if (proto->err_handler(skb, info, &tpi) == PACKET_RCVD)
  205. goto out;
  206. }
  207. out:
  208. rcu_read_unlock();
  209. }
  210. static int gre_rcv(struct sk_buff *skb)
  211. {
  212. const struct gre_protocol *proto;
  213. u8 ver;
  214. int ret;
  215. if (!pskb_may_pull(skb, 12))
  216. goto drop;
  217. ver = skb->data[1]&0x7f;
  218. if (ver >= GREPROTO_MAX)
  219. goto drop;
  220. rcu_read_lock();
  221. proto = rcu_dereference(gre_proto[ver]);
  222. if (!proto || !proto->handler)
  223. goto drop_unlock;
  224. ret = proto->handler(skb);
  225. rcu_read_unlock();
  226. return ret;
  227. drop_unlock:
  228. rcu_read_unlock();
  229. drop:
  230. kfree_skb(skb);
  231. return NET_RX_DROP;
  232. }
  233. static void gre_err(struct sk_buff *skb, u32 info)
  234. {
  235. const struct gre_protocol *proto;
  236. const struct iphdr *iph = (const struct iphdr *)skb->data;
  237. u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
  238. if (ver >= GREPROTO_MAX)
  239. return;
  240. rcu_read_lock();
  241. proto = rcu_dereference(gre_proto[ver]);
  242. if (proto && proto->err_handler)
  243. proto->err_handler(skb, info);
  244. rcu_read_unlock();
  245. }
  246. static const struct net_protocol net_gre_protocol = {
  247. .handler = gre_rcv,
  248. .err_handler = gre_err,
  249. .netns_ok = 1,
  250. };
  251. static const struct gre_protocol ipgre_protocol = {
  252. .handler = gre_cisco_rcv,
  253. .err_handler = gre_cisco_err,
  254. };
  255. int gre_cisco_register(struct gre_cisco_protocol *newp)
  256. {
  257. struct gre_cisco_protocol **proto = (struct gre_cisco_protocol **)
  258. &gre_cisco_proto_list[newp->priority];
  259. return (cmpxchg(proto, NULL, newp) == NULL) ? 0 : -EBUSY;
  260. }
  261. EXPORT_SYMBOL_GPL(gre_cisco_register);
  262. int gre_cisco_unregister(struct gre_cisco_protocol *del_proto)
  263. {
  264. struct gre_cisco_protocol **proto = (struct gre_cisco_protocol **)
  265. &gre_cisco_proto_list[del_proto->priority];
  266. int ret;
  267. ret = (cmpxchg(proto, del_proto, NULL) == del_proto) ? 0 : -EINVAL;
  268. if (ret)
  269. return ret;
  270. synchronize_net();
  271. return 0;
  272. }
  273. EXPORT_SYMBOL_GPL(gre_cisco_unregister);
  274. static int __init gre_init(void)
  275. {
  276. pr_info("GRE over IPv4 demultiplexor driver\n");
  277. if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
  278. pr_err("can't add protocol\n");
  279. goto err;
  280. }
  281. if (gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO) < 0) {
  282. pr_info("%s: can't add ipgre handler\n", __func__);
  283. goto err_gre;
  284. }
  285. return 0;
  286. err_gre:
  287. inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
  288. err:
  289. return -EAGAIN;
  290. }
  291. static void __exit gre_exit(void)
  292. {
  293. gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
  294. inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
  295. }
  296. module_init(gre_init);
  297. module_exit(gre_exit);
  298. MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
  299. MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  300. MODULE_LICENSE("GPL");