gre.c 10.0 KB

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