Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 1 | /* Amanda extension for IP connection tracking |
| 2 | * |
| 3 | * (C) 2002 by Brian J. Murrell <netfilter@interlinx.bc.ca> |
| 4 | * based on HW's ip_conntrack_irc.c as well as other modules |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/textsearch.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/in.h> |
| 17 | #include <linux/udp.h> |
Yasuyuki Kozakai | 1863f09 | 2006-12-02 22:12:54 -0800 | [diff] [blame] | 18 | #include <linux/netfilter.h> |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 19 | |
| 20 | #include <net/netfilter/nf_conntrack.h> |
| 21 | #include <net/netfilter/nf_conntrack_expect.h> |
| 22 | #include <net/netfilter/nf_conntrack_ecache.h> |
| 23 | #include <net/netfilter/nf_conntrack_helper.h> |
| 24 | #include <linux/netfilter/nf_conntrack_amanda.h> |
| 25 | |
| 26 | static unsigned int master_timeout __read_mostly = 300; |
| 27 | static char *ts_algo = "kmp"; |
| 28 | |
| 29 | MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>"); |
| 30 | MODULE_DESCRIPTION("Amanda connection tracking module"); |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | MODULE_ALIAS("ip_conntrack_amanda"); |
Pablo Neira Ayuso | 4dc06f9 | 2008-11-17 16:01:42 +0100 | [diff] [blame] | 33 | MODULE_ALIAS_NFCT_HELPER("amanda"); |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 34 | |
| 35 | module_param(master_timeout, uint, 0600); |
| 36 | MODULE_PARM_DESC(master_timeout, "timeout for the master connection"); |
| 37 | module_param(ts_algo, charp, 0400); |
| 38 | MODULE_PARM_DESC(ts_algo, "textsearch algorithm to use (default kmp)"); |
| 39 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 40 | unsigned int (*nf_nat_amanda_hook)(struct sk_buff *skb, |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 41 | enum ip_conntrack_info ctinfo, |
| 42 | unsigned int matchoff, |
| 43 | unsigned int matchlen, |
| 44 | struct nf_conntrack_expect *exp) |
| 45 | __read_mostly; |
| 46 | EXPORT_SYMBOL_GPL(nf_nat_amanda_hook); |
| 47 | |
| 48 | enum amanda_strings { |
| 49 | SEARCH_CONNECT, |
| 50 | SEARCH_NEWLINE, |
| 51 | SEARCH_DATA, |
| 52 | SEARCH_MESG, |
| 53 | SEARCH_INDEX, |
| 54 | }; |
| 55 | |
| 56 | static struct { |
Jan Engelhardt | 58c0fb0 | 2008-04-14 11:15:42 +0200 | [diff] [blame] | 57 | const char *string; |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 58 | size_t len; |
| 59 | struct ts_config *ts; |
| 60 | } search[] __read_mostly = { |
| 61 | [SEARCH_CONNECT] = { |
| 62 | .string = "CONNECT ", |
| 63 | .len = 8, |
| 64 | }, |
| 65 | [SEARCH_NEWLINE] = { |
| 66 | .string = "\n", |
| 67 | .len = 1, |
| 68 | }, |
| 69 | [SEARCH_DATA] = { |
| 70 | .string = "DATA ", |
| 71 | .len = 5, |
| 72 | }, |
| 73 | [SEARCH_MESG] = { |
| 74 | .string = "MESG ", |
| 75 | .len = 5, |
| 76 | }, |
| 77 | [SEARCH_INDEX] = { |
| 78 | .string = "INDEX ", |
| 79 | .len = 6, |
| 80 | }, |
| 81 | }; |
| 82 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 83 | static int amanda_help(struct sk_buff *skb, |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 84 | unsigned int protoff, |
| 85 | struct nf_conn *ct, |
| 86 | enum ip_conntrack_info ctinfo) |
| 87 | { |
| 88 | struct ts_state ts; |
| 89 | struct nf_conntrack_expect *exp; |
| 90 | struct nf_conntrack_tuple *tuple; |
| 91 | unsigned int dataoff, start, stop, off, i; |
| 92 | char pbuf[sizeof("65535")], *tmp; |
| 93 | u_int16_t len; |
| 94 | __be16 port; |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 95 | int ret = NF_ACCEPT; |
| 96 | typeof(nf_nat_amanda_hook) nf_nat_amanda; |
| 97 | |
| 98 | /* Only look at packets from the Amanda server */ |
| 99 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) |
| 100 | return NF_ACCEPT; |
| 101 | |
| 102 | /* increase the UDP timeout of the master connection as replies from |
| 103 | * Amanda clients to the server can be quite delayed */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 104 | nf_ct_refresh(ct, skb, master_timeout * HZ); |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 105 | |
| 106 | /* No data? */ |
| 107 | dataoff = protoff + sizeof(struct udphdr); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 108 | if (dataoff >= skb->len) { |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 109 | if (net_ratelimit()) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 110 | printk("amanda_help: skblen = %u\n", skb->len); |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 111 | return NF_ACCEPT; |
| 112 | } |
| 113 | |
| 114 | memset(&ts, 0, sizeof(ts)); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 115 | start = skb_find_text(skb, dataoff, skb->len, |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 116 | search[SEARCH_CONNECT].ts, &ts); |
| 117 | if (start == UINT_MAX) |
| 118 | goto out; |
| 119 | start += dataoff + search[SEARCH_CONNECT].len; |
| 120 | |
| 121 | memset(&ts, 0, sizeof(ts)); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 122 | stop = skb_find_text(skb, start, skb->len, |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 123 | search[SEARCH_NEWLINE].ts, &ts); |
| 124 | if (stop == UINT_MAX) |
| 125 | goto out; |
| 126 | stop += start; |
| 127 | |
| 128 | for (i = SEARCH_DATA; i <= SEARCH_INDEX; i++) { |
| 129 | memset(&ts, 0, sizeof(ts)); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 130 | off = skb_find_text(skb, start, stop, search[i].ts, &ts); |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 131 | if (off == UINT_MAX) |
| 132 | continue; |
| 133 | off += start + search[i].len; |
| 134 | |
| 135 | len = min_t(unsigned int, sizeof(pbuf) - 1, stop - off); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 136 | if (skb_copy_bits(skb, off, pbuf, len)) |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 137 | break; |
| 138 | pbuf[len] = '\0'; |
| 139 | |
| 140 | port = htons(simple_strtoul(pbuf, &tmp, 10)); |
| 141 | len = tmp - pbuf; |
| 142 | if (port == 0 || len > 5) |
| 143 | break; |
| 144 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 145 | exp = nf_ct_expect_alloc(ct); |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 146 | if (exp == NULL) { |
| 147 | ret = NF_DROP; |
| 148 | goto out; |
| 149 | } |
| 150 | tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 151 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, |
| 152 | nf_ct_l3num(ct), |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 153 | &tuple->src.u3, &tuple->dst.u3, |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 154 | IPPROTO_TCP, NULL, &port); |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 155 | |
| 156 | nf_nat_amanda = rcu_dereference(nf_nat_amanda_hook); |
| 157 | if (nf_nat_amanda && ct->status & IPS_NAT_MASK) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 158 | ret = nf_nat_amanda(skb, ctinfo, off - dataoff, |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 159 | len, exp); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 160 | else if (nf_ct_expect_related(exp) != 0) |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 161 | ret = NF_DROP; |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 162 | nf_ct_expect_put(exp); |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | out: |
| 166 | return ret; |
| 167 | } |
| 168 | |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 169 | static const struct nf_conntrack_expect_policy amanda_exp_policy = { |
| 170 | .max_expected = 3, |
| 171 | .timeout = 180, |
| 172 | }; |
| 173 | |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 174 | static struct nf_conntrack_helper amanda_helper[2] __read_mostly = { |
| 175 | { |
| 176 | .name = "amanda", |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 177 | .me = THIS_MODULE, |
| 178 | .help = amanda_help, |
| 179 | .tuple.src.l3num = AF_INET, |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 180 | .tuple.src.u.udp.port = cpu_to_be16(10080), |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 181 | .tuple.dst.protonum = IPPROTO_UDP, |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 182 | .expect_policy = &amanda_exp_policy, |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 183 | }, |
| 184 | { |
| 185 | .name = "amanda", |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 186 | .me = THIS_MODULE, |
| 187 | .help = amanda_help, |
| 188 | .tuple.src.l3num = AF_INET6, |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 189 | .tuple.src.u.udp.port = cpu_to_be16(10080), |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 190 | .tuple.dst.protonum = IPPROTO_UDP, |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 191 | .expect_policy = &amanda_exp_policy, |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 192 | }, |
| 193 | }; |
| 194 | |
| 195 | static void __exit nf_conntrack_amanda_fini(void) |
| 196 | { |
| 197 | int i; |
| 198 | |
| 199 | nf_conntrack_helper_unregister(&amanda_helper[0]); |
| 200 | nf_conntrack_helper_unregister(&amanda_helper[1]); |
| 201 | for (i = 0; i < ARRAY_SIZE(search); i++) |
| 202 | textsearch_destroy(search[i].ts); |
| 203 | } |
| 204 | |
| 205 | static int __init nf_conntrack_amanda_init(void) |
| 206 | { |
| 207 | int ret, i; |
| 208 | |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 209 | for (i = 0; i < ARRAY_SIZE(search); i++) { |
| 210 | search[i].ts = textsearch_prepare(ts_algo, search[i].string, |
| 211 | search[i].len, |
| 212 | GFP_KERNEL, TS_AUTOLOAD); |
Akinobu Mita | c764c9a | 2007-06-05 12:56:53 -0700 | [diff] [blame] | 213 | if (IS_ERR(search[i].ts)) { |
| 214 | ret = PTR_ERR(search[i].ts); |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 215 | goto err1; |
Akinobu Mita | c764c9a | 2007-06-05 12:56:53 -0700 | [diff] [blame] | 216 | } |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 217 | } |
| 218 | ret = nf_conntrack_helper_register(&amanda_helper[0]); |
| 219 | if (ret < 0) |
| 220 | goto err1; |
| 221 | ret = nf_conntrack_helper_register(&amanda_helper[1]); |
| 222 | if (ret < 0) |
| 223 | goto err2; |
| 224 | return 0; |
| 225 | |
| 226 | err2: |
| 227 | nf_conntrack_helper_unregister(&amanda_helper[0]); |
| 228 | err1: |
Akinobu Mita | c764c9a | 2007-06-05 12:56:53 -0700 | [diff] [blame] | 229 | while (--i >= 0) |
| 230 | textsearch_destroy(search[i].ts); |
| 231 | |
Patrick McHardy | 1695890 | 2006-12-02 22:08:26 -0800 | [diff] [blame] | 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | module_init(nf_conntrack_amanda_init); |
| 236 | module_exit(nf_conntrack_amanda_fini); |