netfilter: nf_ct_helper: allocate 16 bytes for the helper and policy names
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / net / netfilter / nf_conntrack_sane.c
CommitLineData
6fecd198
MS
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
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>
5a0e3ad6 23#include <linux/slab.h>
6fecd198
MS
24#include <linux/in.h>
25#include <linux/tcp.h>
26#include <net/netfilter/nf_conntrack.h>
27#include <net/netfilter/nf_conntrack_helper.h>
28#include <net/netfilter/nf_conntrack_expect.h>
29#include <linux/netfilter/nf_conntrack_sane.h>
30
31MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Michal Schmidt <mschmidt@redhat.com>");
33MODULE_DESCRIPTION("SANE connection tracking helper");
4dc06f96 34MODULE_ALIAS_NFCT_HELPER("sane");
6fecd198
MS
35
36static char *sane_buffer;
37
38static DEFINE_SPINLOCK(nf_sane_lock);
39
40#define MAX_PORTS 8
41static u_int16_t ports[MAX_PORTS];
42static unsigned int ports_c;
43module_param_array(ports, ushort, &ports_c, 0400);
44
6fecd198
MS
45struct sane_request {
46 __be32 RPC_code;
47#define SANE_NET_START 7 /* RPC code */
48
49 __be32 handle;
50};
51
52struct sane_reply_net_start {
53 __be32 status;
54#define SANE_STATUS_SUCCESS 0
55
56 __be16 zero;
57 __be16 port;
58 /* other fields aren't interesting for conntrack */
59};
60
3db05fea 61static int help(struct sk_buff *skb,
6fecd198
MS
62 unsigned int protoff,
63 struct nf_conn *ct,
64 enum ip_conntrack_info ctinfo)
65{
66 unsigned int dataoff, datalen;
02e23f40
JE
67 const struct tcphdr *th;
68 struct tcphdr _tcph;
69 void *sb_ptr;
6fecd198
MS
70 int ret = NF_ACCEPT;
71 int dir = CTINFO2DIR(ctinfo);
72 struct nf_ct_sane_master *ct_sane_info;
73 struct nf_conntrack_expect *exp;
74 struct nf_conntrack_tuple *tuple;
75 struct sane_request *req;
76 struct sane_reply_net_start *reply;
6fecd198
MS
77
78 ct_sane_info = &nfct_help(ct)->help.ct_sane_info;
79 /* Until there's been traffic both ways, don't look in packets. */
80 if (ctinfo != IP_CT_ESTABLISHED &&
fb048833 81 ctinfo != IP_CT_ESTABLISHED_REPLY)
6fecd198
MS
82 return NF_ACCEPT;
83
84 /* Not a full tcp header? */
3db05fea 85 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
6fecd198
MS
86 if (th == NULL)
87 return NF_ACCEPT;
88
89 /* No data? */
90 dataoff = protoff + th->doff * 4;
3db05fea 91 if (dataoff >= skb->len)
6fecd198
MS
92 return NF_ACCEPT;
93
3db05fea 94 datalen = skb->len - dataoff;
6fecd198
MS
95
96 spin_lock_bh(&nf_sane_lock);
3db05fea 97 sb_ptr = skb_header_pointer(skb, dataoff, datalen, sane_buffer);
6fecd198
MS
98 BUG_ON(sb_ptr == NULL);
99
100 if (dir == IP_CT_DIR_ORIGINAL) {
101 if (datalen != sizeof(struct sane_request))
102 goto out;
103
02e23f40 104 req = sb_ptr;
6fecd198
MS
105 if (req->RPC_code != htonl(SANE_NET_START)) {
106 /* Not an interesting command */
107 ct_sane_info->state = SANE_STATE_NORMAL;
108 goto out;
109 }
110
111 /* We're interested in the next reply */
112 ct_sane_info->state = SANE_STATE_START_REQUESTED;
113 goto out;
114 }
115
116 /* Is it a reply to an uninteresting command? */
117 if (ct_sane_info->state != SANE_STATE_START_REQUESTED)
118 goto out;
119
120 /* It's a reply to SANE_NET_START. */
121 ct_sane_info->state = SANE_STATE_NORMAL;
122
123 if (datalen < sizeof(struct sane_reply_net_start)) {
0d53778e 124 pr_debug("nf_ct_sane: NET_START reply too short\n");
6fecd198
MS
125 goto out;
126 }
127
02e23f40 128 reply = sb_ptr;
6fecd198
MS
129 if (reply->status != htonl(SANE_STATUS_SUCCESS)) {
130 /* saned refused the command */
0d53778e
PM
131 pr_debug("nf_ct_sane: unsuccessful SANE_STATUS = %u\n",
132 ntohl(reply->status));
6fecd198
MS
133 goto out;
134 }
135
136 /* Invalid saned reply? Ignore it. */
137 if (reply->zero != 0)
138 goto out;
139
6823645d 140 exp = nf_ct_expect_alloc(ct);
6fecd198
MS
141 if (exp == NULL) {
142 ret = NF_DROP;
143 goto out;
144 }
145
146 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
5e8fbe2a 147 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
6002f266 148 &tuple->src.u3, &tuple->dst.u3,
6823645d 149 IPPROTO_TCP, NULL, &reply->port);
6fecd198 150
0d53778e 151 pr_debug("nf_ct_sane: expect: ");
3c9fba65 152 nf_ct_dump_tuple(&exp->tuple);
6fecd198
MS
153
154 /* Can't expect this? Best to drop packet now. */
6823645d 155 if (nf_ct_expect_related(exp) != 0)
6fecd198
MS
156 ret = NF_DROP;
157
6823645d 158 nf_ct_expect_put(exp);
6fecd198
MS
159
160out:
161 spin_unlock_bh(&nf_sane_lock);
162 return ret;
163}
164
ec59a111 165static struct nf_conntrack_helper sane[MAX_PORTS][2] __read_mostly;
6fecd198 166
6002f266
PM
167static const struct nf_conntrack_expect_policy sane_exp_policy = {
168 .max_expected = 1,
169 .timeout = 5 * 60,
170};
171
6fecd198
MS
172/* 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++) {
0d53778e
PM
179 pr_debug("nf_ct_sane: unregistering helper for pf: %d "
180 "port: %d\n",
181 sane[i][j].tuple.src.l3num, ports[i]);
6fecd198
MS
182 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;
6fecd198
MS
192
193 sane_buffer = kmalloc(65536, GFP_KERNEL);
194 if (!sane_buffer)
195 return -ENOMEM;
196
197 if (ports_c == 0)
198 ports[ports_c++] = SANE_PORT;
199
200 /* FIXME should be configurable whether IPv4 and IPv6 connections
201 are tracked or not - YK */
202 for (i = 0; i < ports_c; i++) {
203 sane[i][0].tuple.src.l3num = PF_INET;
204 sane[i][1].tuple.src.l3num = PF_INET6;
205 for (j = 0; j < 2; j++) {
206 sane[i][j].tuple.src.u.tcp.port = htons(ports[i]);
207 sane[i][j].tuple.dst.protonum = IPPROTO_TCP;
6002f266 208 sane[i][j].expect_policy = &sane_exp_policy;
6fecd198
MS
209 sane[i][j].me = THIS_MODULE;
210 sane[i][j].help = help;
6fecd198 211 if (ports[i] == SANE_PORT)
3a8fc53a 212 sprintf(sane[i][j].name, "sane");
6fecd198 213 else
3a8fc53a 214 sprintf(sane[i][j].name, "sane-%d", ports[i]);
6fecd198 215
0d53778e
PM
216 pr_debug("nf_ct_sane: registering helper for pf: %d "
217 "port: %d\n",
218 sane[i][j].tuple.src.l3num, ports[i]);
6fecd198
MS
219 ret = nf_conntrack_helper_register(&sane[i][j]);
220 if (ret) {
221 printk(KERN_ERR "nf_ct_sane: failed to "
222 "register helper for pf: %d port: %d\n",
223 sane[i][j].tuple.src.l3num, ports[i]);
224 nf_conntrack_sane_fini();
225 return ret;
226 }
227 }
228 }
229
230 return 0;
231}
232
233module_init(nf_conntrack_sane_init);
234module_exit(nf_conntrack_sane_fini);