nf_conntrack_sane.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* SANE connection tracking helper
  2. * (SANE = Scanner Access Now Easy)
  3. * For documentation about the SANE network protocol see
  4. * http://www.sane-project.org/html/doc015.html
  5. */
  6. /* Copyright (C) 2007 Red Hat, Inc.
  7. * Author: Michal Schmidt <mschmidt@redhat.com>
  8. * Based on the FTP conntrack helper (net/netfilter/nf_conntrack_ftp.c):
  9. * (C) 1999-2001 Paul `Rusty' Russell
  10. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  11. * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
  12. * (C) 2003 Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/netfilter.h>
  21. #include <linux/slab.h>
  22. #include <linux/in.h>
  23. #include <linux/tcp.h>
  24. #include <net/netfilter/nf_conntrack.h>
  25. #include <net/netfilter/nf_conntrack_helper.h>
  26. #include <net/netfilter/nf_conntrack_expect.h>
  27. #include <linux/netfilter/nf_conntrack_sane.h>
  28. MODULE_LICENSE("GPL");
  29. MODULE_AUTHOR("Michal Schmidt <mschmidt@redhat.com>");
  30. MODULE_DESCRIPTION("SANE connection tracking helper");
  31. MODULE_ALIAS_NFCT_HELPER("sane");
  32. static char *sane_buffer;
  33. static DEFINE_SPINLOCK(nf_sane_lock);
  34. #define MAX_PORTS 8
  35. static u_int16_t ports[MAX_PORTS];
  36. static unsigned int ports_c;
  37. module_param_array(ports, ushort, &ports_c, 0400);
  38. struct sane_request {
  39. __be32 RPC_code;
  40. #define SANE_NET_START 7 /* RPC code */
  41. __be32 handle;
  42. };
  43. struct sane_reply_net_start {
  44. __be32 status;
  45. #define SANE_STATUS_SUCCESS 0
  46. __be16 zero;
  47. __be16 port;
  48. /* other fields aren't interesting for conntrack */
  49. };
  50. static int help(struct sk_buff *skb,
  51. unsigned int protoff,
  52. struct nf_conn *ct,
  53. enum ip_conntrack_info ctinfo)
  54. {
  55. unsigned int dataoff, datalen;
  56. const struct tcphdr *th;
  57. struct tcphdr _tcph;
  58. void *sb_ptr;
  59. int ret = NF_ACCEPT;
  60. int dir = CTINFO2DIR(ctinfo);
  61. struct nf_ct_sane_master *ct_sane_info = nfct_help_data(ct);
  62. struct nf_conntrack_expect *exp;
  63. struct nf_conntrack_tuple *tuple;
  64. struct sane_request *req;
  65. struct sane_reply_net_start *reply;
  66. /* Until there's been traffic both ways, don't look in packets. */
  67. if (ctinfo != IP_CT_ESTABLISHED &&
  68. ctinfo != IP_CT_ESTABLISHED_REPLY)
  69. return NF_ACCEPT;
  70. /* Not a full tcp header? */
  71. th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
  72. if (th == NULL)
  73. return NF_ACCEPT;
  74. /* No data? */
  75. dataoff = protoff + th->doff * 4;
  76. if (dataoff >= skb->len)
  77. return NF_ACCEPT;
  78. datalen = skb->len - dataoff;
  79. spin_lock_bh(&nf_sane_lock);
  80. sb_ptr = skb_header_pointer(skb, dataoff, datalen, sane_buffer);
  81. BUG_ON(sb_ptr == NULL);
  82. if (dir == IP_CT_DIR_ORIGINAL) {
  83. if (datalen != sizeof(struct sane_request))
  84. goto out;
  85. req = sb_ptr;
  86. if (req->RPC_code != htonl(SANE_NET_START)) {
  87. /* Not an interesting command */
  88. ct_sane_info->state = SANE_STATE_NORMAL;
  89. goto out;
  90. }
  91. /* We're interested in the next reply */
  92. ct_sane_info->state = SANE_STATE_START_REQUESTED;
  93. goto out;
  94. }
  95. /* Is it a reply to an uninteresting command? */
  96. if (ct_sane_info->state != SANE_STATE_START_REQUESTED)
  97. goto out;
  98. /* It's a reply to SANE_NET_START. */
  99. ct_sane_info->state = SANE_STATE_NORMAL;
  100. if (datalen < sizeof(struct sane_reply_net_start)) {
  101. pr_debug("nf_ct_sane: NET_START reply too short\n");
  102. goto out;
  103. }
  104. reply = sb_ptr;
  105. if (reply->status != htonl(SANE_STATUS_SUCCESS)) {
  106. /* saned refused the command */
  107. pr_debug("nf_ct_sane: unsuccessful SANE_STATUS = %u\n",
  108. ntohl(reply->status));
  109. goto out;
  110. }
  111. /* Invalid saned reply? Ignore it. */
  112. if (reply->zero != 0)
  113. goto out;
  114. exp = nf_ct_expect_alloc(ct);
  115. if (exp == NULL) {
  116. nf_ct_helper_log(skb, ct, "cannot alloc expectation");
  117. ret = NF_DROP;
  118. goto out;
  119. }
  120. tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
  121. nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
  122. &tuple->src.u3, &tuple->dst.u3,
  123. IPPROTO_TCP, NULL, &reply->port);
  124. pr_debug("nf_ct_sane: expect: ");
  125. nf_ct_dump_tuple(&exp->tuple);
  126. /* Can't expect this? Best to drop packet now. */
  127. if (nf_ct_expect_related(exp) != 0) {
  128. nf_ct_helper_log(skb, ct, "cannot add expectation");
  129. ret = NF_DROP;
  130. }
  131. nf_ct_expect_put(exp);
  132. out:
  133. spin_unlock_bh(&nf_sane_lock);
  134. return ret;
  135. }
  136. static struct nf_conntrack_helper sane[MAX_PORTS][2] __read_mostly;
  137. static const struct nf_conntrack_expect_policy sane_exp_policy = {
  138. .max_expected = 1,
  139. .timeout = 5 * 60,
  140. };
  141. /* don't make this __exit, since it's called from __init ! */
  142. static void nf_conntrack_sane_fini(void)
  143. {
  144. int i, j;
  145. for (i = 0; i < ports_c; i++) {
  146. for (j = 0; j < 2; j++) {
  147. pr_debug("nf_ct_sane: unregistering helper for pf: %d "
  148. "port: %d\n",
  149. sane[i][j].tuple.src.l3num, ports[i]);
  150. nf_conntrack_helper_unregister(&sane[i][j]);
  151. }
  152. }
  153. kfree(sane_buffer);
  154. }
  155. static int __init nf_conntrack_sane_init(void)
  156. {
  157. int i, j = -1, ret = 0;
  158. sane_buffer = kmalloc(65536, GFP_KERNEL);
  159. if (!sane_buffer)
  160. return -ENOMEM;
  161. if (ports_c == 0)
  162. ports[ports_c++] = SANE_PORT;
  163. /* FIXME should be configurable whether IPv4 and IPv6 connections
  164. are tracked or not - YK */
  165. for (i = 0; i < ports_c; i++) {
  166. sane[i][0].tuple.src.l3num = PF_INET;
  167. sane[i][1].tuple.src.l3num = PF_INET6;
  168. for (j = 0; j < 2; j++) {
  169. sane[i][j].data_len = sizeof(struct nf_ct_sane_master);
  170. sane[i][j].tuple.src.u.tcp.port = htons(ports[i]);
  171. sane[i][j].tuple.dst.protonum = IPPROTO_TCP;
  172. sane[i][j].expect_policy = &sane_exp_policy;
  173. sane[i][j].me = THIS_MODULE;
  174. sane[i][j].help = help;
  175. if (ports[i] == SANE_PORT)
  176. sprintf(sane[i][j].name, "sane");
  177. else
  178. sprintf(sane[i][j].name, "sane-%d", ports[i]);
  179. pr_debug("nf_ct_sane: registering helper for pf: %d "
  180. "port: %d\n",
  181. sane[i][j].tuple.src.l3num, ports[i]);
  182. ret = nf_conntrack_helper_register(&sane[i][j]);
  183. if (ret) {
  184. printk(KERN_ERR "nf_ct_sane: failed to "
  185. "register helper for pf: %d port: %d\n",
  186. sane[i][j].tuple.src.l3num, ports[i]);
  187. nf_conntrack_sane_fini();
  188. return ret;
  189. }
  190. }
  191. }
  192. return 0;
  193. }
  194. module_init(nf_conntrack_sane_init);
  195. module_exit(nf_conntrack_sane_fini);