Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 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 | |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | MODULE_AUTHOR("Michal Schmidt <mschmidt@redhat.com>"); |
| 33 | MODULE_DESCRIPTION("SANE connection tracking helper"); |
Pablo Neira Ayuso | 4dc06f9 | 2008-11-17 16:01:42 +0100 | [diff] [blame] | 34 | MODULE_ALIAS_NFCT_HELPER("sane"); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 35 | |
| 36 | static char *sane_buffer; |
| 37 | |
| 38 | static DEFINE_SPINLOCK(nf_sane_lock); |
| 39 | |
| 40 | #define MAX_PORTS 8 |
| 41 | static u_int16_t ports[MAX_PORTS]; |
| 42 | static unsigned int ports_c; |
| 43 | module_param_array(ports, ushort, &ports_c, 0400); |
| 44 | |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 45 | struct sane_request { |
| 46 | __be32 RPC_code; |
| 47 | #define SANE_NET_START 7 /* RPC code */ |
| 48 | |
| 49 | __be32 handle; |
| 50 | }; |
| 51 | |
| 52 | struct 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 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 61 | static int help(struct sk_buff *skb, |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 62 | unsigned int protoff, |
| 63 | struct nf_conn *ct, |
| 64 | enum ip_conntrack_info ctinfo) |
| 65 | { |
| 66 | unsigned int dataoff, datalen; |
Jan Engelhardt | 02e23f4 | 2008-01-31 04:51:45 -0800 | [diff] [blame] | 67 | const struct tcphdr *th; |
| 68 | struct tcphdr _tcph; |
| 69 | void *sb_ptr; |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 70 | int ret = NF_ACCEPT; |
| 71 | int dir = CTINFO2DIR(ctinfo); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 72 | struct nf_ct_sane_master *ct_sane_info = nfct_help_data(ct); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 73 | struct nf_conntrack_expect *exp; |
| 74 | struct nf_conntrack_tuple *tuple; |
| 75 | struct sane_request *req; |
| 76 | struct sane_reply_net_start *reply; |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 77 | |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 78 | /* Until there's been traffic both ways, don't look in packets. */ |
| 79 | if (ctinfo != IP_CT_ESTABLISHED && |
Eric Dumazet | fb04883 | 2011-05-19 15:44:27 +0200 | [diff] [blame] | 80 | ctinfo != IP_CT_ESTABLISHED_REPLY) |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 81 | return NF_ACCEPT; |
| 82 | |
| 83 | /* Not a full tcp header? */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 84 | th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 85 | if (th == NULL) |
| 86 | return NF_ACCEPT; |
| 87 | |
| 88 | /* No data? */ |
| 89 | dataoff = protoff + th->doff * 4; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 90 | if (dataoff >= skb->len) |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 91 | return NF_ACCEPT; |
| 92 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 93 | datalen = skb->len - dataoff; |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 94 | |
| 95 | spin_lock_bh(&nf_sane_lock); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 96 | sb_ptr = skb_header_pointer(skb, dataoff, datalen, sane_buffer); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 97 | 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 Engelhardt | 02e23f4 | 2008-01-31 04:51:45 -0800 | [diff] [blame] | 103 | req = sb_ptr; |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 104 | 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 McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 123 | pr_debug("nf_ct_sane: NET_START reply too short\n"); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 124 | goto out; |
| 125 | } |
| 126 | |
Jan Engelhardt | 02e23f4 | 2008-01-31 04:51:45 -0800 | [diff] [blame] | 127 | reply = sb_ptr; |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 128 | if (reply->status != htonl(SANE_STATUS_SUCCESS)) { |
| 129 | /* saned refused the command */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 130 | pr_debug("nf_ct_sane: unsuccessful SANE_STATUS = %u\n", |
| 131 | ntohl(reply->status)); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 132 | goto out; |
| 133 | } |
| 134 | |
| 135 | /* Invalid saned reply? Ignore it. */ |
| 136 | if (reply->zero != 0) |
| 137 | goto out; |
| 138 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 139 | exp = nf_ct_expect_alloc(ct); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 140 | if (exp == NULL) { |
| 141 | ret = NF_DROP; |
| 142 | goto out; |
| 143 | } |
| 144 | |
| 145 | tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 146 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 147 | &tuple->src.u3, &tuple->dst.u3, |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 148 | IPPROTO_TCP, NULL, &reply->port); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 149 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 150 | pr_debug("nf_ct_sane: expect: "); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 151 | nf_ct_dump_tuple(&exp->tuple); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 152 | |
| 153 | /* Can't expect this? Best to drop packet now. */ |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 154 | if (nf_ct_expect_related(exp) != 0) |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 155 | ret = NF_DROP; |
| 156 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 157 | nf_ct_expect_put(exp); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 158 | |
| 159 | out: |
| 160 | spin_unlock_bh(&nf_sane_lock); |
| 161 | return ret; |
| 162 | } |
| 163 | |
Patrick McHardy | ec59a11 | 2007-07-07 22:37:03 -0700 | [diff] [blame] | 164 | static struct nf_conntrack_helper sane[MAX_PORTS][2] __read_mostly; |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 165 | |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 166 | static const struct nf_conntrack_expect_policy sane_exp_policy = { |
| 167 | .max_expected = 1, |
| 168 | .timeout = 5 * 60, |
| 169 | }; |
| 170 | |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 171 | /* don't make this __exit, since it's called from __init ! */ |
| 172 | static void nf_conntrack_sane_fini(void) |
| 173 | { |
| 174 | int i, j; |
| 175 | |
| 176 | for (i = 0; i < ports_c; i++) { |
| 177 | for (j = 0; j < 2; j++) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 178 | pr_debug("nf_ct_sane: unregistering helper for pf: %d " |
| 179 | "port: %d\n", |
| 180 | sane[i][j].tuple.src.l3num, ports[i]); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 181 | nf_conntrack_helper_unregister(&sane[i][j]); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | kfree(sane_buffer); |
| 186 | } |
| 187 | |
| 188 | static int __init nf_conntrack_sane_init(void) |
| 189 | { |
| 190 | int i, j = -1, ret = 0; |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 191 | |
| 192 | sane_buffer = kmalloc(65536, GFP_KERNEL); |
| 193 | if (!sane_buffer) |
| 194 | return -ENOMEM; |
| 195 | |
| 196 | if (ports_c == 0) |
| 197 | ports[ports_c++] = SANE_PORT; |
| 198 | |
| 199 | /* FIXME should be configurable whether IPv4 and IPv6 connections |
| 200 | are tracked or not - YK */ |
| 201 | for (i = 0; i < ports_c; i++) { |
| 202 | sane[i][0].tuple.src.l3num = PF_INET; |
| 203 | sane[i][1].tuple.src.l3num = PF_INET6; |
| 204 | for (j = 0; j < 2; j++) { |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 205 | sane[i][j].data_len = sizeof(struct nf_ct_sane_master); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 206 | sane[i][j].tuple.src.u.tcp.port = htons(ports[i]); |
| 207 | sane[i][j].tuple.dst.protonum = IPPROTO_TCP; |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 208 | sane[i][j].expect_policy = &sane_exp_policy; |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 209 | sane[i][j].me = THIS_MODULE; |
| 210 | sane[i][j].help = help; |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 211 | if (ports[i] == SANE_PORT) |
Pablo Neira Ayuso | 3a8fc53 | 2012-01-15 16:34:08 +0100 | [diff] [blame] | 212 | sprintf(sane[i][j].name, "sane"); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 213 | else |
Pablo Neira Ayuso | 3a8fc53 | 2012-01-15 16:34:08 +0100 | [diff] [blame] | 214 | sprintf(sane[i][j].name, "sane-%d", ports[i]); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 215 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 216 | pr_debug("nf_ct_sane: registering helper for pf: %d " |
| 217 | "port: %d\n", |
| 218 | sane[i][j].tuple.src.l3num, ports[i]); |
Michal Schmidt | 6fecd19 | 2007-02-07 15:05:12 -0800 | [diff] [blame] | 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 | |
| 233 | module_init(nf_conntrack_sane_init); |
| 234 | module_exit(nf_conntrack_sane_fini); |