i386: Reserve the right performance counter for the Intel PerfMon NMI watchdog
[usb.git] / net / netfilter / nf_conntrack_sane.c
blob355d371bac93d8bf1f7265d4cecdb7915cdc3157
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 */
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>
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.
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>
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Michal Schmidt <mschmidt@redhat.com>");
32 MODULE_DESCRIPTION("SANE connection tracking helper");
34 static char *sane_buffer;
36 static DEFINE_SPINLOCK(nf_sane_lock);
38 #define MAX_PORTS 8
39 static u_int16_t ports[MAX_PORTS];
40 static unsigned int ports_c;
41 module_param_array(ports, ushort, &ports_c, 0400);
43 struct sane_request {
44 __be32 RPC_code;
45 #define SANE_NET_START 7 /* RPC code */
47 __be32 handle;
50 struct sane_reply_net_start {
51 __be32 status;
52 #define SANE_STATUS_SUCCESS 0
54 __be16 zero;
55 __be16 port;
56 /* other fields aren't interesting for conntrack */
59 static int help(struct sk_buff **pskb,
60 unsigned int protoff,
61 struct nf_conn *ct,
62 enum ip_conntrack_info ctinfo)
64 unsigned int dataoff, datalen;
65 struct tcphdr _tcph, *th;
66 char *sb_ptr;
67 int ret = NF_ACCEPT;
68 int dir = CTINFO2DIR(ctinfo);
69 struct nf_ct_sane_master *ct_sane_info;
70 struct nf_conntrack_expect *exp;
71 struct nf_conntrack_tuple *tuple;
72 struct sane_request *req;
73 struct sane_reply_net_start *reply;
74 int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
76 ct_sane_info = &nfct_help(ct)->help.ct_sane_info;
77 /* Until there's been traffic both ways, don't look in packets. */
78 if (ctinfo != IP_CT_ESTABLISHED &&
79 ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY)
80 return NF_ACCEPT;
82 /* Not a full tcp header? */
83 th = skb_header_pointer(*pskb, protoff, sizeof(_tcph), &_tcph);
84 if (th == NULL)
85 return NF_ACCEPT;
87 /* No data? */
88 dataoff = protoff + th->doff * 4;
89 if (dataoff >= (*pskb)->len)
90 return NF_ACCEPT;
92 datalen = (*pskb)->len - dataoff;
94 spin_lock_bh(&nf_sane_lock);
95 sb_ptr = skb_header_pointer(*pskb, dataoff, datalen, sane_buffer);
96 BUG_ON(sb_ptr == NULL);
98 if (dir == IP_CT_DIR_ORIGINAL) {
99 if (datalen != sizeof(struct sane_request))
100 goto out;
102 req = (struct sane_request *)sb_ptr;
103 if (req->RPC_code != htonl(SANE_NET_START)) {
104 /* Not an interesting command */
105 ct_sane_info->state = SANE_STATE_NORMAL;
106 goto out;
109 /* We're interested in the next reply */
110 ct_sane_info->state = SANE_STATE_START_REQUESTED;
111 goto out;
114 /* Is it a reply to an uninteresting command? */
115 if (ct_sane_info->state != SANE_STATE_START_REQUESTED)
116 goto out;
118 /* It's a reply to SANE_NET_START. */
119 ct_sane_info->state = SANE_STATE_NORMAL;
121 if (datalen < sizeof(struct sane_reply_net_start)) {
122 pr_debug("nf_ct_sane: NET_START reply too short\n");
123 goto out;
126 reply = (struct sane_reply_net_start *)sb_ptr;
127 if (reply->status != htonl(SANE_STATUS_SUCCESS)) {
128 /* saned refused the command */
129 pr_debug("nf_ct_sane: unsuccessful SANE_STATUS = %u\n",
130 ntohl(reply->status));
131 goto out;
134 /* Invalid saned reply? Ignore it. */
135 if (reply->zero != 0)
136 goto out;
138 exp = nf_ct_expect_alloc(ct);
139 if (exp == NULL) {
140 ret = NF_DROP;
141 goto out;
144 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
145 nf_ct_expect_init(exp, family, &tuple->src.u3, &tuple->dst.u3,
146 IPPROTO_TCP, NULL, &reply->port);
148 pr_debug("nf_ct_sane: expect: ");
149 NF_CT_DUMP_TUPLE(&exp->tuple);
151 /* Can't expect this? Best to drop packet now. */
152 if (nf_ct_expect_related(exp) != 0)
153 ret = NF_DROP;
155 nf_ct_expect_put(exp);
157 out:
158 spin_unlock_bh(&nf_sane_lock);
159 return ret;
162 static struct nf_conntrack_helper sane[MAX_PORTS][2] __read_mostly;
163 static char sane_names[MAX_PORTS][2][sizeof("sane-65535")] __read_mostly;
165 /* don't make this __exit, since it's called from __init ! */
166 static void nf_conntrack_sane_fini(void)
168 int i, j;
170 for (i = 0; i < ports_c; i++) {
171 for (j = 0; j < 2; j++) {
172 pr_debug("nf_ct_sane: unregistering helper for pf: %d "
173 "port: %d\n",
174 sane[i][j].tuple.src.l3num, ports[i]);
175 nf_conntrack_helper_unregister(&sane[i][j]);
179 kfree(sane_buffer);
182 static int __init nf_conntrack_sane_init(void)
184 int i, j = -1, ret = 0;
185 char *tmpname;
187 sane_buffer = kmalloc(65536, GFP_KERNEL);
188 if (!sane_buffer)
189 return -ENOMEM;
191 if (ports_c == 0)
192 ports[ports_c++] = SANE_PORT;
194 /* FIXME should be configurable whether IPv4 and IPv6 connections
195 are tracked or not - YK */
196 for (i = 0; i < ports_c; i++) {
197 sane[i][0].tuple.src.l3num = PF_INET;
198 sane[i][1].tuple.src.l3num = PF_INET6;
199 for (j = 0; j < 2; j++) {
200 sane[i][j].tuple.src.u.tcp.port = htons(ports[i]);
201 sane[i][j].tuple.dst.protonum = IPPROTO_TCP;
202 sane[i][j].max_expected = 1;
203 sane[i][j].timeout = 5 * 60; /* 5 Minutes */
204 sane[i][j].me = THIS_MODULE;
205 sane[i][j].help = help;
206 tmpname = &sane_names[i][j][0];
207 if (ports[i] == SANE_PORT)
208 sprintf(tmpname, "sane");
209 else
210 sprintf(tmpname, "sane-%d", ports[i]);
211 sane[i][j].name = tmpname;
213 pr_debug("nf_ct_sane: registering helper for pf: %d "
214 "port: %d\n",
215 sane[i][j].tuple.src.l3num, ports[i]);
216 ret = nf_conntrack_helper_register(&sane[i][j]);
217 if (ret) {
218 printk(KERN_ERR "nf_ct_sane: failed to "
219 "register helper for pf: %d port: %d\n",
220 sane[i][j].tuple.src.l3num, ports[i]);
221 nf_conntrack_sane_fini();
222 return ret;
227 return 0;
230 module_init(nf_conntrack_sane_init);
231 module_exit(nf_conntrack_sane_fini);