gre_demux.c 9.1 KB

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