ip_conntrack_proto_gre.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * ip_conntrack_proto_gre.c - Version 3.0
  3. *
  4. * Connection tracking protocol helper module for GRE.
  5. *
  6. * GRE is a generic encapsulation protocol, which is generally not very
  7. * suited for NAT, as it has no protocol-specific part as port numbers.
  8. *
  9. * It has an optional key field, which may help us distinguishing two
  10. * connections between the same two hosts.
  11. *
  12. * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
  13. *
  14. * PPTP is built on top of a modified version of GRE, and has a mandatory
  15. * field called "CallID", which serves us for the same purpose as the key
  16. * field in plain GRE.
  17. *
  18. * Documentation about PPTP can be found in RFC 2637
  19. *
  20. * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
  21. *
  22. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/timer.h>
  28. #include <linux/netfilter.h>
  29. #include <linux/ip.h>
  30. #include <linux/in.h>
  31. #include <linux/list.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/interrupt.h>
  34. static DEFINE_RWLOCK(ip_ct_gre_lock);
  35. #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
  36. #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
  37. #include <linux/netfilter_ipv4/ip_conntrack_core.h>
  38. #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
  39. #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
  40. MODULE_LICENSE("GPL");
  41. MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
  42. MODULE_DESCRIPTION("netfilter connection tracking protocol helper for GRE");
  43. /* shamelessly stolen from ip_conntrack_proto_udp.c */
  44. #define GRE_TIMEOUT (30*HZ)
  45. #define GRE_STREAM_TIMEOUT (180*HZ)
  46. #if 0
  47. #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, __FUNCTION__, ## args)
  48. #define DUMP_TUPLE_GRE(x) printk("%u.%u.%u.%u:0x%x -> %u.%u.%u.%u:0x%x\n", \
  49. NIPQUAD((x)->src.ip), ntohs((x)->src.u.gre.key), \
  50. NIPQUAD((x)->dst.ip), ntohs((x)->dst.u.gre.key))
  51. #else
  52. #define DEBUGP(x, args...)
  53. #define DUMP_TUPLE_GRE(x)
  54. #endif
  55. /* GRE KEYMAP HANDLING FUNCTIONS */
  56. static LIST_HEAD(gre_keymap_list);
  57. static inline int gre_key_cmpfn(const struct ip_ct_gre_keymap *km,
  58. const struct ip_conntrack_tuple *t)
  59. {
  60. return ((km->tuple.src.ip == t->src.ip) &&
  61. (km->tuple.dst.ip == t->dst.ip) &&
  62. (km->tuple.dst.protonum == t->dst.protonum) &&
  63. (km->tuple.dst.u.all == t->dst.u.all));
  64. }
  65. /* look up the source key for a given tuple */
  66. static __be16 gre_keymap_lookup(struct ip_conntrack_tuple *t)
  67. {
  68. struct ip_ct_gre_keymap *km;
  69. __be16 key = 0;
  70. read_lock_bh(&ip_ct_gre_lock);
  71. list_for_each_entry(km, &gre_keymap_list, list) {
  72. if (gre_key_cmpfn(km, t)) {
  73. key = km->tuple.src.u.gre.key;
  74. break;
  75. }
  76. }
  77. read_unlock_bh(&ip_ct_gre_lock);
  78. DEBUGP("lookup src key 0x%x up key for ", key);
  79. DUMP_TUPLE_GRE(t);
  80. return key;
  81. }
  82. /* add a single keymap entry, associate with specified master ct */
  83. int
  84. ip_ct_gre_keymap_add(struct ip_conntrack *ct,
  85. struct ip_conntrack_tuple *t, int reply)
  86. {
  87. struct ip_ct_gre_keymap **exist_km, *km;
  88. if (!ct->helper || strcmp(ct->helper->name, "pptp")) {
  89. DEBUGP("refusing to add GRE keymap to non-pptp session\n");
  90. return -1;
  91. }
  92. if (!reply)
  93. exist_km = &ct->help.ct_pptp_info.keymap_orig;
  94. else
  95. exist_km = &ct->help.ct_pptp_info.keymap_reply;
  96. if (*exist_km) {
  97. /* check whether it's a retransmission */
  98. list_for_each_entry(km, &gre_keymap_list, list) {
  99. if (gre_key_cmpfn(km, t) && km == *exist_km)
  100. return 0;
  101. }
  102. DEBUGP("trying to override keymap_%s for ct %p\n",
  103. reply? "reply":"orig", ct);
  104. return -EEXIST;
  105. }
  106. km = kmalloc(sizeof(*km), GFP_ATOMIC);
  107. if (!km)
  108. return -ENOMEM;
  109. memcpy(&km->tuple, t, sizeof(*t));
  110. *exist_km = km;
  111. DEBUGP("adding new entry %p: ", km);
  112. DUMP_TUPLE_GRE(&km->tuple);
  113. write_lock_bh(&ip_ct_gre_lock);
  114. list_add_tail(&km->list, &gre_keymap_list);
  115. write_unlock_bh(&ip_ct_gre_lock);
  116. return 0;
  117. }
  118. /* destroy the keymap entries associated with specified master ct */
  119. void ip_ct_gre_keymap_destroy(struct ip_conntrack *ct)
  120. {
  121. DEBUGP("entering for ct %p\n", ct);
  122. if (!ct->helper || strcmp(ct->helper->name, "pptp")) {
  123. DEBUGP("refusing to destroy GRE keymap to non-pptp session\n");
  124. return;
  125. }
  126. write_lock_bh(&ip_ct_gre_lock);
  127. if (ct->help.ct_pptp_info.keymap_orig) {
  128. DEBUGP("removing %p from list\n",
  129. ct->help.ct_pptp_info.keymap_orig);
  130. list_del(&ct->help.ct_pptp_info.keymap_orig->list);
  131. kfree(ct->help.ct_pptp_info.keymap_orig);
  132. ct->help.ct_pptp_info.keymap_orig = NULL;
  133. }
  134. if (ct->help.ct_pptp_info.keymap_reply) {
  135. DEBUGP("removing %p from list\n",
  136. ct->help.ct_pptp_info.keymap_reply);
  137. list_del(&ct->help.ct_pptp_info.keymap_reply->list);
  138. kfree(ct->help.ct_pptp_info.keymap_reply);
  139. ct->help.ct_pptp_info.keymap_reply = NULL;
  140. }
  141. write_unlock_bh(&ip_ct_gre_lock);
  142. }
  143. /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
  144. /* invert gre part of tuple */
  145. static int gre_invert_tuple(struct ip_conntrack_tuple *tuple,
  146. const struct ip_conntrack_tuple *orig)
  147. {
  148. tuple->dst.u.gre.key = orig->src.u.gre.key;
  149. tuple->src.u.gre.key = orig->dst.u.gre.key;
  150. return 1;
  151. }
  152. /* gre hdr info to tuple */
  153. static int gre_pkt_to_tuple(const struct sk_buff *skb,
  154. unsigned int dataoff,
  155. struct ip_conntrack_tuple *tuple)
  156. {
  157. struct gre_hdr_pptp _pgrehdr, *pgrehdr;
  158. __be16 srckey;
  159. struct gre_hdr _grehdr, *grehdr;
  160. /* first only delinearize old RFC1701 GRE header */
  161. grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
  162. if (!grehdr || grehdr->version != GRE_VERSION_PPTP) {
  163. /* try to behave like "ip_conntrack_proto_generic" */
  164. tuple->src.u.all = 0;
  165. tuple->dst.u.all = 0;
  166. return 1;
  167. }
  168. /* PPTP header is variable length, only need up to the call_id field */
  169. pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
  170. if (!pgrehdr)
  171. return 1;
  172. if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) {
  173. DEBUGP("GRE_VERSION_PPTP but unknown proto\n");
  174. return 0;
  175. }
  176. tuple->dst.u.gre.key = pgrehdr->call_id;
  177. srckey = gre_keymap_lookup(tuple);
  178. tuple->src.u.gre.key = srckey;
  179. return 1;
  180. }
  181. /* print gre part of tuple */
  182. static int gre_print_tuple(struct seq_file *s,
  183. const struct ip_conntrack_tuple *tuple)
  184. {
  185. return seq_printf(s, "srckey=0x%x dstkey=0x%x ",
  186. ntohs(tuple->src.u.gre.key),
  187. ntohs(tuple->dst.u.gre.key));
  188. }
  189. /* print private data for conntrack */
  190. static int gre_print_conntrack(struct seq_file *s,
  191. const struct ip_conntrack *ct)
  192. {
  193. return seq_printf(s, "timeout=%u, stream_timeout=%u ",
  194. (ct->proto.gre.timeout / HZ),
  195. (ct->proto.gre.stream_timeout / HZ));
  196. }
  197. /* Returns verdict for packet, and may modify conntrack */
  198. static int gre_packet(struct ip_conntrack *ct,
  199. const struct sk_buff *skb,
  200. enum ip_conntrack_info conntrackinfo)
  201. {
  202. /* If we've seen traffic both ways, this is a GRE connection.
  203. * Extend timeout. */
  204. if (ct->status & IPS_SEEN_REPLY) {
  205. ip_ct_refresh_acct(ct, conntrackinfo, skb,
  206. ct->proto.gre.stream_timeout);
  207. /* Also, more likely to be important, and not a probe. */
  208. set_bit(IPS_ASSURED_BIT, &ct->status);
  209. ip_conntrack_event_cache(IPCT_STATUS, skb);
  210. } else
  211. ip_ct_refresh_acct(ct, conntrackinfo, skb,
  212. ct->proto.gre.timeout);
  213. return NF_ACCEPT;
  214. }
  215. /* Called when a new connection for this protocol found. */
  216. static int gre_new(struct ip_conntrack *ct,
  217. const struct sk_buff *skb)
  218. {
  219. DEBUGP(": ");
  220. DUMP_TUPLE_GRE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
  221. /* initialize to sane value. Ideally a conntrack helper
  222. * (e.g. in case of pptp) is increasing them */
  223. ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT;
  224. ct->proto.gre.timeout = GRE_TIMEOUT;
  225. return 1;
  226. }
  227. /* Called when a conntrack entry has already been removed from the hashes
  228. * and is about to be deleted from memory */
  229. static void gre_destroy(struct ip_conntrack *ct)
  230. {
  231. struct ip_conntrack *master = ct->master;
  232. DEBUGP(" entering\n");
  233. if (!master)
  234. DEBUGP("no master !?!\n");
  235. else
  236. ip_ct_gre_keymap_destroy(master);
  237. }
  238. /* protocol helper struct */
  239. static struct ip_conntrack_protocol gre = {
  240. .proto = IPPROTO_GRE,
  241. .name = "gre",
  242. .pkt_to_tuple = gre_pkt_to_tuple,
  243. .invert_tuple = gre_invert_tuple,
  244. .print_tuple = gre_print_tuple,
  245. .print_conntrack = gre_print_conntrack,
  246. .packet = gre_packet,
  247. .new = gre_new,
  248. .destroy = gre_destroy,
  249. .me = THIS_MODULE,
  250. #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
  251. defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
  252. .tuple_to_nfattr = ip_ct_port_tuple_to_nfattr,
  253. .nfattr_to_tuple = ip_ct_port_nfattr_to_tuple,
  254. #endif
  255. };
  256. /* ip_conntrack_proto_gre initialization */
  257. int __init ip_ct_proto_gre_init(void)
  258. {
  259. return ip_conntrack_protocol_register(&gre);
  260. }
  261. /* This cannot be __exit, as it is invoked from ip_conntrack_helper_pptp.c's
  262. * init() code on errors.
  263. */
  264. void ip_ct_proto_gre_fini(void)
  265. {
  266. struct list_head *pos, *n;
  267. /* delete all keymap entries */
  268. write_lock_bh(&ip_ct_gre_lock);
  269. list_for_each_safe(pos, n, &gre_keymap_list) {
  270. DEBUGP("deleting keymap %p at module unload time\n", pos);
  271. list_del(pos);
  272. kfree(pos);
  273. }
  274. write_unlock_bh(&ip_ct_gre_lock);
  275. ip_conntrack_protocol_unregister(&gre);
  276. }
  277. EXPORT_SYMBOL(ip_ct_gre_keymap_add);
  278. EXPORT_SYMBOL(ip_ct_gre_keymap_destroy);