|
| 1 | +/* Amanda extension for IP connection tracking |
| 2 | + * |
| 3 | + * (C) 2002 by Brian J. Murrell <[email protected]> |
| 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> |
| 18 | + |
| 19 | +#include <net/netfilter/nf_conntrack.h> |
| 20 | +#include <net/netfilter/nf_conntrack_expect.h> |
| 21 | +#include <net/netfilter/nf_conntrack_ecache.h> |
| 22 | +#include <net/netfilter/nf_conntrack_helper.h> |
| 23 | +#include <linux/netfilter/nf_conntrack_amanda.h> |
| 24 | + |
| 25 | +static unsigned int master_timeout __read_mostly = 300; |
| 26 | +static char *ts_algo = "kmp"; |
| 27 | + |
| 28 | +MODULE_AUTHOR( "Brian J. Murrell <[email protected]>"); |
| 29 | +MODULE_DESCRIPTION("Amanda connection tracking module"); |
| 30 | +MODULE_LICENSE("GPL"); |
| 31 | +MODULE_ALIAS("ip_conntrack_amanda"); |
| 32 | + |
| 33 | +module_param(master_timeout, uint, 0600); |
| 34 | +MODULE_PARM_DESC(master_timeout, "timeout for the master connection"); |
| 35 | +module_param(ts_algo, charp, 0400); |
| 36 | +MODULE_PARM_DESC(ts_algo, "textsearch algorithm to use (default kmp)"); |
| 37 | + |
| 38 | +unsigned int (*nf_nat_amanda_hook)(struct sk_buff **pskb, |
| 39 | + enum ip_conntrack_info ctinfo, |
| 40 | + unsigned int matchoff, |
| 41 | + unsigned int matchlen, |
| 42 | + struct nf_conntrack_expect *exp) |
| 43 | + __read_mostly; |
| 44 | +EXPORT_SYMBOL_GPL(nf_nat_amanda_hook); |
| 45 | + |
| 46 | +enum amanda_strings { |
| 47 | + SEARCH_CONNECT, |
| 48 | + SEARCH_NEWLINE, |
| 49 | + SEARCH_DATA, |
| 50 | + SEARCH_MESG, |
| 51 | + SEARCH_INDEX, |
| 52 | +}; |
| 53 | + |
| 54 | +static struct { |
| 55 | + char *string; |
| 56 | + size_t len; |
| 57 | + struct ts_config *ts; |
| 58 | +} search[] __read_mostly = { |
| 59 | + [SEARCH_CONNECT] = { |
| 60 | + .string = "CONNECT ", |
| 61 | + .len = 8, |
| 62 | + }, |
| 63 | + [SEARCH_NEWLINE] = { |
| 64 | + .string = "\n", |
| 65 | + .len = 1, |
| 66 | + }, |
| 67 | + [SEARCH_DATA] = { |
| 68 | + .string = "DATA ", |
| 69 | + .len = 5, |
| 70 | + }, |
| 71 | + [SEARCH_MESG] = { |
| 72 | + .string = "MESG ", |
| 73 | + .len = 5, |
| 74 | + }, |
| 75 | + [SEARCH_INDEX] = { |
| 76 | + .string = "INDEX ", |
| 77 | + .len = 6, |
| 78 | + }, |
| 79 | +}; |
| 80 | + |
| 81 | +static int amanda_help(struct sk_buff **pskb, |
| 82 | + unsigned int protoff, |
| 83 | + struct nf_conn *ct, |
| 84 | + enum ip_conntrack_info ctinfo) |
| 85 | +{ |
| 86 | + struct ts_state ts; |
| 87 | + struct nf_conntrack_expect *exp; |
| 88 | + struct nf_conntrack_tuple *tuple; |
| 89 | + unsigned int dataoff, start, stop, off, i; |
| 90 | + char pbuf[sizeof("65535")], *tmp; |
| 91 | + u_int16_t len; |
| 92 | + __be16 port; |
| 93 | + int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; |
| 94 | + int ret = NF_ACCEPT; |
| 95 | + typeof(nf_nat_amanda_hook) nf_nat_amanda; |
| 96 | + |
| 97 | + /* Only look at packets from the Amanda server */ |
| 98 | + if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) |
| 99 | + return NF_ACCEPT; |
| 100 | + |
| 101 | + /* increase the UDP timeout of the master connection as replies from |
| 102 | + * Amanda clients to the server can be quite delayed */ |
| 103 | + nf_ct_refresh(ct, *pskb, master_timeout * HZ); |
| 104 | + |
| 105 | + /* No data? */ |
| 106 | + dataoff = protoff + sizeof(struct udphdr); |
| 107 | + if (dataoff >= (*pskb)->len) { |
| 108 | + if (net_ratelimit()) |
| 109 | + printk("amanda_help: skblen = %u\n", (*pskb)->len); |
| 110 | + return NF_ACCEPT; |
| 111 | + } |
| 112 | + |
| 113 | + memset(&ts, 0, sizeof(ts)); |
| 114 | + start = skb_find_text(*pskb, dataoff, (*pskb)->len, |
| 115 | + search[SEARCH_CONNECT].ts, &ts); |
| 116 | + if (start == UINT_MAX) |
| 117 | + goto out; |
| 118 | + start += dataoff + search[SEARCH_CONNECT].len; |
| 119 | + |
| 120 | + memset(&ts, 0, sizeof(ts)); |
| 121 | + stop = skb_find_text(*pskb, start, (*pskb)->len, |
| 122 | + search[SEARCH_NEWLINE].ts, &ts); |
| 123 | + if (stop == UINT_MAX) |
| 124 | + goto out; |
| 125 | + stop += start; |
| 126 | + |
| 127 | + for (i = SEARCH_DATA; i <= SEARCH_INDEX; i++) { |
| 128 | + memset(&ts, 0, sizeof(ts)); |
| 129 | + off = skb_find_text(*pskb, start, stop, search[i].ts, &ts); |
| 130 | + if (off == UINT_MAX) |
| 131 | + continue; |
| 132 | + off += start + search[i].len; |
| 133 | + |
| 134 | + len = min_t(unsigned int, sizeof(pbuf) - 1, stop - off); |
| 135 | + if (skb_copy_bits(*pskb, off, pbuf, len)) |
| 136 | + break; |
| 137 | + pbuf[len] = '\0'; |
| 138 | + |
| 139 | + port = htons(simple_strtoul(pbuf, &tmp, 10)); |
| 140 | + len = tmp - pbuf; |
| 141 | + if (port == 0 || len > 5) |
| 142 | + break; |
| 143 | + |
| 144 | + exp = nf_conntrack_expect_alloc(ct); |
| 145 | + if (exp == NULL) { |
| 146 | + ret = NF_DROP; |
| 147 | + goto out; |
| 148 | + } |
| 149 | + tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; |
| 150 | + nf_conntrack_expect_init(exp, family, |
| 151 | + &tuple->src.u3, &tuple->dst.u3, |
| 152 | + IPPROTO_TCP, NULL, &port); |
| 153 | + |
| 154 | + nf_nat_amanda = rcu_dereference(nf_nat_amanda_hook); |
| 155 | + if (nf_nat_amanda && ct->status & IPS_NAT_MASK) |
| 156 | + ret = nf_nat_amanda(pskb, ctinfo, off - dataoff, |
| 157 | + len, exp); |
| 158 | + else if (nf_conntrack_expect_related(exp) != 0) |
| 159 | + ret = NF_DROP; |
| 160 | + nf_conntrack_expect_put(exp); |
| 161 | + } |
| 162 | + |
| 163 | +out: |
| 164 | + return ret; |
| 165 | +} |
| 166 | + |
| 167 | +static struct nf_conntrack_helper amanda_helper[2] __read_mostly = { |
| 168 | + { |
| 169 | + .name = "amanda", |
| 170 | + .max_expected = 3, |
| 171 | + .timeout = 180, |
| 172 | + .me = THIS_MODULE, |
| 173 | + .help = amanda_help, |
| 174 | + .tuple.src.l3num = AF_INET, |
| 175 | + .tuple.src.u.udp.port = __constant_htons(10080), |
| 176 | + .tuple.dst.protonum = IPPROTO_UDP, |
| 177 | + .mask.src.l3num = 0xFFFF, |
| 178 | + .mask.src.u.udp.port = __constant_htons(0xFFFF), |
| 179 | + .mask.dst.protonum = 0xFF, |
| 180 | + }, |
| 181 | + { |
| 182 | + .name = "amanda", |
| 183 | + .max_expected = 3, |
| 184 | + .timeout = 180, |
| 185 | + .me = THIS_MODULE, |
| 186 | + .help = amanda_help, |
| 187 | + .tuple.src.l3num = AF_INET6, |
| 188 | + .tuple.src.u.udp.port = __constant_htons(10080), |
| 189 | + .tuple.dst.protonum = IPPROTO_UDP, |
| 190 | + .mask.src.l3num = 0xFFFF, |
| 191 | + .mask.src.u.udp.port = __constant_htons(0xFFFF), |
| 192 | + .mask.dst.protonum = 0xFF, |
| 193 | + }, |
| 194 | +}; |
| 195 | + |
| 196 | +static void __exit nf_conntrack_amanda_fini(void) |
| 197 | +{ |
| 198 | + int i; |
| 199 | + |
| 200 | + nf_conntrack_helper_unregister(&amanda_helper[0]); |
| 201 | + nf_conntrack_helper_unregister(&amanda_helper[1]); |
| 202 | + for (i = 0; i < ARRAY_SIZE(search); i++) |
| 203 | + textsearch_destroy(search[i].ts); |
| 204 | +} |
| 205 | + |
| 206 | +static int __init nf_conntrack_amanda_init(void) |
| 207 | +{ |
| 208 | + int ret, i; |
| 209 | + |
| 210 | + ret = -ENOMEM; |
| 211 | + for (i = 0; i < ARRAY_SIZE(search); i++) { |
| 212 | + search[i].ts = textsearch_prepare(ts_algo, search[i].string, |
| 213 | + search[i].len, |
| 214 | + GFP_KERNEL, TS_AUTOLOAD); |
| 215 | + if (search[i].ts == NULL) |
| 216 | + goto err1; |
| 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: |
| 229 | + for (; i >= 0; i--) { |
| 230 | + if (search[i].ts) |
| 231 | + textsearch_destroy(search[i].ts); |
| 232 | + } |
| 233 | + return ret; |
| 234 | +} |
| 235 | + |
| 236 | +module_init(nf_conntrack_amanda_init); |
| 237 | +module_exit(nf_conntrack_amanda_fini); |
0 commit comments