blob: dcfecbb81c4617784178009bc02de37b9a32cb00 [file] [log] [blame]
Michal Schmidt6fecd192007-02-07 15:05:12 -08001/* 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
7/* Copyright (C) 2007 Red Hat, Inc.
8 * Author: Michal Schmidt <mschmidt@redhat.com>
9 * Based on the FTP conntrack helper (net/netfilter/nf_conntrack_ftp.c):
10 * (C) 1999-2001 Paul `Rusty' Russell
11 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
12 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
13 * (C) 2003 Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
18 */
19
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/netfilter.h>
23#include <linux/in.h>
24#include <linux/tcp.h>
25#include <net/netfilter/nf_conntrack.h>
26#include <net/netfilter/nf_conntrack_helper.h>
27#include <net/netfilter/nf_conntrack_expect.h>
28#include <linux/netfilter/nf_conntrack_sane.h>
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Michal Schmidt <mschmidt@redhat.com>");
32MODULE_DESCRIPTION("SANE connection tracking helper");
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +010033MODULE_ALIAS_NFCT_HELPER("sane");
Michal Schmidt6fecd192007-02-07 15:05:12 -080034
35static char *sane_buffer;
36
37static DEFINE_SPINLOCK(nf_sane_lock);
38
39#define MAX_PORTS 8
40static u_int16_t ports[MAX_PORTS];
41static unsigned int ports_c;
42module_param_array(ports, ushort, &ports_c, 0400);
43
Michal Schmidt6fecd192007-02-07 15:05:12 -080044struct sane_request {
45 __be32 RPC_code;
46#define SANE_NET_START 7 /* RPC code */
47
48 __be32 handle;
49};
50
51struct sane_reply_net_start {
52 __be32 status;
53#define SANE_STATUS_SUCCESS 0
54
55 __be16 zero;
56 __be16 port;
57 /* other fields aren't interesting for conntrack */
58};
59
Herbert Xu3db05fe2007-10-15 00:53:15 -070060static int help(struct sk_buff *skb,
Michal Schmidt6fecd192007-02-07 15:05:12 -080061 unsigned int protoff,
62 struct nf_conn *ct,
63 enum ip_conntrack_info ctinfo)
64{
65 unsigned int dataoff, datalen;
Jan Engelhardt02e23f42008-01-31 04:51:45 -080066 const struct tcphdr *th;
67 struct tcphdr _tcph;
68 void *sb_ptr;
Michal Schmidt6fecd192007-02-07 15:05:12 -080069 int ret = NF_ACCEPT;
70 int dir = CTINFO2DIR(ctinfo);
71 struct nf_ct_sane_master *ct_sane_info;
72 struct nf_conntrack_expect *exp;
73 struct nf_conntrack_tuple *tuple;
74 struct sane_request *req;
75 struct sane_reply_net_start *reply;
Michal Schmidt6fecd192007-02-07 15:05:12 -080076
77 ct_sane_info = &nfct_help(ct)->help.ct_sane_info;
78 /* Until there's been traffic both ways, don't look in packets. */
79 if (ctinfo != IP_CT_ESTABLISHED &&
80 ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY)
81 return NF_ACCEPT;
82
83 /* Not a full tcp header? */
Herbert Xu3db05fe2007-10-15 00:53:15 -070084 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
Michal Schmidt6fecd192007-02-07 15:05:12 -080085 if (th == NULL)
86 return NF_ACCEPT;
87
88 /* No data? */
89 dataoff = protoff + th->doff * 4;
Herbert Xu3db05fe2007-10-15 00:53:15 -070090 if (dataoff >= skb->len)
Michal Schmidt6fecd192007-02-07 15:05:12 -080091 return NF_ACCEPT;
92
Herbert Xu3db05fe2007-10-15 00:53:15 -070093 datalen = skb->len - dataoff;
Michal Schmidt6fecd192007-02-07 15:05:12 -080094
95 spin_lock_bh(&nf_sane_lock);
Herbert Xu3db05fe2007-10-15 00:53:15 -070096 sb_ptr = skb_header_pointer(skb, dataoff, datalen, sane_buffer);
Michal Schmidt6fecd192007-02-07 15:05:12 -080097 BUG_ON(sb_ptr == NULL);
98
99 if (dir == IP_CT_DIR_ORIGINAL) {
100 if (datalen != sizeof(struct sane_request))
101 goto out;
102
Jan Engelhardt02e23f42008-01-31 04:51:45 -0800103 req = sb_ptr;
Michal Schmidt6fecd192007-02-07 15:05:12 -0800104 if (req->RPC_code != htonl(SANE_NET_START)) {
105 /* Not an interesting command */
106 ct_sane_info->state = SANE_STATE_NORMAL;
107 goto out;
108 }
109
110 /* We're interested in the next reply */
111 ct_sane_info->state = SANE_STATE_START_REQUESTED;
112 goto out;
113 }
114
115 /* Is it a reply to an uninteresting command? */
116 if (ct_sane_info->state != SANE_STATE_START_REQUESTED)
117 goto out;
118
119 /* It's a reply to SANE_NET_START. */
120 ct_sane_info->state = SANE_STATE_NORMAL;
121
122 if (datalen < sizeof(struct sane_reply_net_start)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700123 pr_debug("nf_ct_sane: NET_START reply too short\n");
Michal Schmidt6fecd192007-02-07 15:05:12 -0800124 goto out;
125 }
126
Jan Engelhardt02e23f42008-01-31 04:51:45 -0800127 reply = sb_ptr;
Michal Schmidt6fecd192007-02-07 15:05:12 -0800128 if (reply->status != htonl(SANE_STATUS_SUCCESS)) {
129 /* saned refused the command */
Patrick McHardy0d537782007-07-07 22:39:38 -0700130 pr_debug("nf_ct_sane: unsuccessful SANE_STATUS = %u\n",
131 ntohl(reply->status));
Michal Schmidt6fecd192007-02-07 15:05:12 -0800132 goto out;
133 }
134
135 /* Invalid saned reply? Ignore it. */
136 if (reply->zero != 0)
137 goto out;
138
Patrick McHardy68236452007-07-07 22:30:49 -0700139 exp = nf_ct_expect_alloc(ct);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800140 if (exp == NULL) {
141 ret = NF_DROP;
142 goto out;
143 }
144
145 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200146 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
Patrick McHardy6002f2662008-03-25 20:09:15 -0700147 &tuple->src.u3, &tuple->dst.u3,
Patrick McHardy68236452007-07-07 22:30:49 -0700148 IPPROTO_TCP, NULL, &reply->port);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800149
Patrick McHardy0d537782007-07-07 22:39:38 -0700150 pr_debug("nf_ct_sane: expect: ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200151 nf_ct_dump_tuple(&exp->tuple);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800152
153 /* Can't expect this? Best to drop packet now. */
Patrick McHardy68236452007-07-07 22:30:49 -0700154 if (nf_ct_expect_related(exp) != 0)
Michal Schmidt6fecd192007-02-07 15:05:12 -0800155 ret = NF_DROP;
156
Patrick McHardy68236452007-07-07 22:30:49 -0700157 nf_ct_expect_put(exp);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800158
159out:
160 spin_unlock_bh(&nf_sane_lock);
161 return ret;
162}
163
Patrick McHardyec59a112007-07-07 22:37:03 -0700164static struct nf_conntrack_helper sane[MAX_PORTS][2] __read_mostly;
165static char sane_names[MAX_PORTS][2][sizeof("sane-65535")] __read_mostly;
Michal Schmidt6fecd192007-02-07 15:05:12 -0800166
Patrick McHardy6002f2662008-03-25 20:09:15 -0700167static const struct nf_conntrack_expect_policy sane_exp_policy = {
168 .max_expected = 1,
169 .timeout = 5 * 60,
170};
171
Michal Schmidt6fecd192007-02-07 15:05:12 -0800172/* don't make this __exit, since it's called from __init ! */
173static void nf_conntrack_sane_fini(void)
174{
175 int i, j;
176
177 for (i = 0; i < ports_c; i++) {
178 for (j = 0; j < 2; j++) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700179 pr_debug("nf_ct_sane: unregistering helper for pf: %d "
180 "port: %d\n",
181 sane[i][j].tuple.src.l3num, ports[i]);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800182 nf_conntrack_helper_unregister(&sane[i][j]);
183 }
184 }
185
186 kfree(sane_buffer);
187}
188
189static int __init nf_conntrack_sane_init(void)
190{
191 int i, j = -1, ret = 0;
192 char *tmpname;
193
194 sane_buffer = kmalloc(65536, GFP_KERNEL);
195 if (!sane_buffer)
196 return -ENOMEM;
197
198 if (ports_c == 0)
199 ports[ports_c++] = SANE_PORT;
200
201 /* FIXME should be configurable whether IPv4 and IPv6 connections
202 are tracked or not - YK */
203 for (i = 0; i < ports_c; i++) {
204 sane[i][0].tuple.src.l3num = PF_INET;
205 sane[i][1].tuple.src.l3num = PF_INET6;
206 for (j = 0; j < 2; j++) {
207 sane[i][j].tuple.src.u.tcp.port = htons(ports[i]);
208 sane[i][j].tuple.dst.protonum = IPPROTO_TCP;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700209 sane[i][j].expect_policy = &sane_exp_policy;
Michal Schmidt6fecd192007-02-07 15:05:12 -0800210 sane[i][j].me = THIS_MODULE;
211 sane[i][j].help = help;
212 tmpname = &sane_names[i][j][0];
213 if (ports[i] == SANE_PORT)
214 sprintf(tmpname, "sane");
215 else
216 sprintf(tmpname, "sane-%d", ports[i]);
217 sane[i][j].name = tmpname;
218
Patrick McHardy0d537782007-07-07 22:39:38 -0700219 pr_debug("nf_ct_sane: registering helper for pf: %d "
220 "port: %d\n",
221 sane[i][j].tuple.src.l3num, ports[i]);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800222 ret = nf_conntrack_helper_register(&sane[i][j]);
223 if (ret) {
224 printk(KERN_ERR "nf_ct_sane: failed to "
225 "register helper for pf: %d port: %d\n",
226 sane[i][j].tuple.src.l3num, ports[i]);
227 nf_conntrack_sane_fini();
228 return ret;
229 }
230 }
231 }
232
233 return 0;
234}
235
236module_init(nf_conntrack_sane_init);
237module_exit(nf_conntrack_sane_fini);