blob: 7523a575f6d16ded0c5724156cfe280598c5c505 [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
Pablo Neira Ayusoad6d9502016-01-03 22:41:24 +010020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Michal Schmidt6fecd192007-02-07 15:05:12 -080022#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/netfilter.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Michal Schmidt6fecd192007-02-07 15:05:12 -080026#include <linux/in.h>
27#include <linux/tcp.h>
28#include <net/netfilter/nf_conntrack.h>
29#include <net/netfilter/nf_conntrack_helper.h>
30#include <net/netfilter/nf_conntrack_expect.h>
31#include <linux/netfilter/nf_conntrack_sane.h>
32
33MODULE_LICENSE("GPL");
34MODULE_AUTHOR("Michal Schmidt <mschmidt@redhat.com>");
35MODULE_DESCRIPTION("SANE connection tracking helper");
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +010036MODULE_ALIAS_NFCT_HELPER("sane");
Michal Schmidt6fecd192007-02-07 15:05:12 -080037
38static char *sane_buffer;
39
40static DEFINE_SPINLOCK(nf_sane_lock);
41
42#define MAX_PORTS 8
43static u_int16_t ports[MAX_PORTS];
44static unsigned int ports_c;
45module_param_array(ports, ushort, &ports_c, 0400);
46
Michal Schmidt6fecd192007-02-07 15:05:12 -080047struct sane_request {
48 __be32 RPC_code;
49#define SANE_NET_START 7 /* RPC code */
50
51 __be32 handle;
52};
53
54struct sane_reply_net_start {
55 __be32 status;
56#define SANE_STATUS_SUCCESS 0
57
58 __be16 zero;
59 __be16 port;
60 /* other fields aren't interesting for conntrack */
61};
62
Herbert Xu3db05fe2007-10-15 00:53:15 -070063static int help(struct sk_buff *skb,
Michal Schmidt6fecd192007-02-07 15:05:12 -080064 unsigned int protoff,
65 struct nf_conn *ct,
66 enum ip_conntrack_info ctinfo)
67{
68 unsigned int dataoff, datalen;
Jan Engelhardt02e23f42008-01-31 04:51:45 -080069 const struct tcphdr *th;
70 struct tcphdr _tcph;
71 void *sb_ptr;
Michal Schmidt6fecd192007-02-07 15:05:12 -080072 int ret = NF_ACCEPT;
73 int dir = CTINFO2DIR(ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +020074 struct nf_ct_sane_master *ct_sane_info = nfct_help_data(ct);
Michal Schmidt6fecd192007-02-07 15:05:12 -080075 struct nf_conntrack_expect *exp;
76 struct nf_conntrack_tuple *tuple;
77 struct sane_request *req;
78 struct sane_reply_net_start *reply;
Michal Schmidt6fecd192007-02-07 15:05:12 -080079
Michal Schmidt6fecd192007-02-07 15:05:12 -080080 /* Until there's been traffic both ways, don't look in packets. */
81 if (ctinfo != IP_CT_ESTABLISHED &&
Eric Dumazetfb048832011-05-19 15:44:27 +020082 ctinfo != IP_CT_ESTABLISHED_REPLY)
Michal Schmidt6fecd192007-02-07 15:05:12 -080083 return NF_ACCEPT;
84
85 /* Not a full tcp header? */
Herbert Xu3db05fe2007-10-15 00:53:15 -070086 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
Michal Schmidt6fecd192007-02-07 15:05:12 -080087 if (th == NULL)
88 return NF_ACCEPT;
89
90 /* No data? */
91 dataoff = protoff + th->doff * 4;
Herbert Xu3db05fe2007-10-15 00:53:15 -070092 if (dataoff >= skb->len)
Michal Schmidt6fecd192007-02-07 15:05:12 -080093 return NF_ACCEPT;
94
Herbert Xu3db05fe2007-10-15 00:53:15 -070095 datalen = skb->len - dataoff;
Michal Schmidt6fecd192007-02-07 15:05:12 -080096
97 spin_lock_bh(&nf_sane_lock);
Herbert Xu3db05fe2007-10-15 00:53:15 -070098 sb_ptr = skb_header_pointer(skb, dataoff, datalen, sane_buffer);
Michal Schmidt6fecd192007-02-07 15:05:12 -080099 BUG_ON(sb_ptr == NULL);
100
101 if (dir == IP_CT_DIR_ORIGINAL) {
102 if (datalen != sizeof(struct sane_request))
103 goto out;
104
Jan Engelhardt02e23f42008-01-31 04:51:45 -0800105 req = sb_ptr;
Michal Schmidt6fecd192007-02-07 15:05:12 -0800106 if (req->RPC_code != htonl(SANE_NET_START)) {
107 /* Not an interesting command */
108 ct_sane_info->state = SANE_STATE_NORMAL;
109 goto out;
110 }
111
112 /* We're interested in the next reply */
113 ct_sane_info->state = SANE_STATE_START_REQUESTED;
114 goto out;
115 }
116
117 /* Is it a reply to an uninteresting command? */
118 if (ct_sane_info->state != SANE_STATE_START_REQUESTED)
119 goto out;
120
121 /* It's a reply to SANE_NET_START. */
122 ct_sane_info->state = SANE_STATE_NORMAL;
123
124 if (datalen < sizeof(struct sane_reply_net_start)) {
Pablo Neira Ayusoad6d9502016-01-03 22:41:24 +0100125 pr_debug("NET_START reply too short\n");
Michal Schmidt6fecd192007-02-07 15:05:12 -0800126 goto out;
127 }
128
Jan Engelhardt02e23f42008-01-31 04:51:45 -0800129 reply = sb_ptr;
Michal Schmidt6fecd192007-02-07 15:05:12 -0800130 if (reply->status != htonl(SANE_STATUS_SUCCESS)) {
131 /* saned refused the command */
Pablo Neira Ayusoad6d9502016-01-03 22:41:24 +0100132 pr_debug("unsuccessful SANE_STATUS = %u\n",
Patrick McHardy0d537782007-07-07 22:39:38 -0700133 ntohl(reply->status));
Michal Schmidt6fecd192007-02-07 15:05:12 -0800134 goto out;
135 }
136
137 /* Invalid saned reply? Ignore it. */
138 if (reply->zero != 0)
139 goto out;
140
Patrick McHardy68236452007-07-07 22:30:49 -0700141 exp = nf_ct_expect_alloc(ct);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800142 if (exp == NULL) {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100143 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
Michal Schmidt6fecd192007-02-07 15:05:12 -0800144 ret = NF_DROP;
145 goto out;
146 }
147
148 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200149 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
Patrick McHardy6002f2662008-03-25 20:09:15 -0700150 &tuple->src.u3, &tuple->dst.u3,
Patrick McHardy68236452007-07-07 22:30:49 -0700151 IPPROTO_TCP, NULL, &reply->port);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800152
Pablo Neira Ayusoad6d9502016-01-03 22:41:24 +0100153 pr_debug("expect: ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200154 nf_ct_dump_tuple(&exp->tuple);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800155
156 /* Can't expect this? Best to drop packet now. */
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100157 if (nf_ct_expect_related(exp) != 0) {
158 nf_ct_helper_log(skb, ct, "cannot add expectation");
Michal Schmidt6fecd192007-02-07 15:05:12 -0800159 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100160 }
Michal Schmidt6fecd192007-02-07 15:05:12 -0800161
Patrick McHardy68236452007-07-07 22:30:49 -0700162 nf_ct_expect_put(exp);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800163
164out:
165 spin_unlock_bh(&nf_sane_lock);
166 return ret;
167}
168
Patrick McHardyec59a112007-07-07 22:37:03 -0700169static struct nf_conntrack_helper sane[MAX_PORTS][2] __read_mostly;
Michal Schmidt6fecd192007-02-07 15:05:12 -0800170
Patrick McHardy6002f2662008-03-25 20:09:15 -0700171static const struct nf_conntrack_expect_policy sane_exp_policy = {
172 .max_expected = 1,
173 .timeout = 5 * 60,
174};
175
Michal Schmidt6fecd192007-02-07 15:05:12 -0800176/* don't make this __exit, since it's called from __init ! */
177static void nf_conntrack_sane_fini(void)
178{
179 int i, j;
180
181 for (i = 0; i < ports_c; i++) {
182 for (j = 0; j < 2; j++) {
Pablo Neira Ayusoad6d9502016-01-03 22:41:24 +0100183 pr_debug("unregistering helper for pf: %d port: %d\n",
Patrick McHardy0d537782007-07-07 22:39:38 -0700184 sane[i][j].tuple.src.l3num, ports[i]);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800185 nf_conntrack_helper_unregister(&sane[i][j]);
186 }
187 }
188
189 kfree(sane_buffer);
190}
191
192static int __init nf_conntrack_sane_init(void)
193{
194 int i, j = -1, ret = 0;
Michal Schmidt6fecd192007-02-07 15:05:12 -0800195
196 sane_buffer = kmalloc(65536, GFP_KERNEL);
197 if (!sane_buffer)
198 return -ENOMEM;
199
200 if (ports_c == 0)
201 ports[ports_c++] = SANE_PORT;
202
203 /* FIXME should be configurable whether IPv4 and IPv6 connections
204 are tracked or not - YK */
205 for (i = 0; i < ports_c; i++) {
206 sane[i][0].tuple.src.l3num = PF_INET;
207 sane[i][1].tuple.src.l3num = PF_INET6;
208 for (j = 0; j < 2; j++) {
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +0200209 sane[i][j].data_len = sizeof(struct nf_ct_sane_master);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800210 sane[i][j].tuple.src.u.tcp.port = htons(ports[i]);
211 sane[i][j].tuple.dst.protonum = IPPROTO_TCP;
Patrick McHardy6002f2662008-03-25 20:09:15 -0700212 sane[i][j].expect_policy = &sane_exp_policy;
Michal Schmidt6fecd192007-02-07 15:05:12 -0800213 sane[i][j].me = THIS_MODULE;
214 sane[i][j].help = help;
Michal Schmidt6fecd192007-02-07 15:05:12 -0800215 if (ports[i] == SANE_PORT)
Pablo Neira Ayuso3a8fc532012-01-15 16:34:08 +0100216 sprintf(sane[i][j].name, "sane");
Michal Schmidt6fecd192007-02-07 15:05:12 -0800217 else
Pablo Neira Ayuso3a8fc532012-01-15 16:34:08 +0100218 sprintf(sane[i][j].name, "sane-%d", ports[i]);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800219
Pablo Neira Ayusoad6d9502016-01-03 22:41:24 +0100220 pr_debug("registering helper for pf: %d port: %d\n",
Patrick McHardy0d537782007-07-07 22:39:38 -0700221 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) {
Pablo Neira Ayusoad6d9502016-01-03 22:41:24 +0100224 pr_err("failed to register helper for pf: %d port: %d\n",
225 sane[i][j].tuple.src.l3num, ports[i]);
Michal Schmidt6fecd192007-02-07 15:05:12 -0800226 nf_conntrack_sane_fini();
227 return ret;
228 }
229 }
230 }
231
232 return 0;
233}
234
235module_init(nf_conntrack_sane_init);
236module_exit(nf_conntrack_sane_fini);