1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* IRC extension for IP connection tracking, Version 1.21
3 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
4 * based on RR's ip_conntrack_ftp.c
5 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/skbuff.h>
13 #include <linux/in.h>
14 #include <linux/ip.h>
15 #include <linux/tcp.h>
16 #include <linux/netfilter.h>
17 #include <linux/slab.h>
18
19 #include <net/netfilter/nf_conntrack.h>
20 #include <net/netfilter/nf_conntrack_expect.h>
21 #include <net/netfilter/nf_conntrack_helper.h>
22 #include <linux/netfilter/nf_conntrack_irc.h>
23
24 static unsigned int max_dcc_channels = 8;
25 static unsigned int dcc_timeout __read_mostly = 300;
26 /* This is slow, but it's simple. --RR */
27 static char *irc_buffer;
28 static DEFINE_SPINLOCK(irc_buffer_lock);
29
30 nf_nat_irc_hook_fn __rcu *nf_nat_irc_hook __read_mostly;
31 EXPORT_SYMBOL_GPL(nf_nat_irc_hook);
32
33 #define HELPER_NAME "irc"
34 #define MAX_SEARCH_SIZE 4095
35
36 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
37 MODULE_DESCRIPTION("IRC (DCC) connection tracking helper");
38 MODULE_LICENSE("GPL");
39 MODULE_ALIAS("ip_conntrack_irc");
40 MODULE_ALIAS_NFCT_HELPER(HELPER_NAME);
41
42 module_param(max_dcc_channels, uint, 0400);
43 MODULE_PARM_DESC(max_dcc_channels, "max number of expected DCC channels per "
44 "IRC session");
45 module_param(dcc_timeout, uint, 0400);
46 MODULE_PARM_DESC(dcc_timeout, "timeout on for unestablished DCC channels");
47
48 static const char *const dccprotos[] = {
49 "SEND ", "CHAT ", "MOVE ", "TSEND ", "SCHAT "
50 };
51
52 #define MINMATCHLEN 5
53
54 /* tries to get the ip_addr and port out of a dcc command
55 * return value: -1 on failure, 0 on success
56 * data pointer to first byte of DCC command data
57 * data_end one past end of data
58 * ip returns parsed ip of dcc command
59 * port returns parsed port of dcc command
60 * ad_beg_p returns pointer to first byte of addr data
61 * ad_end_p returns pointer to last byte of addr data
62 */
parse_dcc(char * data,const char * data_end,__be32 * ip,u_int16_t * port,char ** ad_beg_p,char ** ad_end_p)63 static int parse_dcc(char *data, const char *data_end, __be32 *ip,
64 u_int16_t *port, char **ad_beg_p, char **ad_end_p)
65 {
66 char *tmp;
67
68 /* at least 12: "AAAAAAAA P\1\n" */
69 while (*data++ != ' ')
70 if (data > data_end - 12)
71 return -1;
72
73 /* Make sure we have a newline character within the packet boundaries
74 * because simple_strtoul parses until the first invalid character. */
75 for (tmp = data; tmp < data_end; tmp++)
76 if (*tmp == '\n')
77 break;
78 if (tmp >= data_end || *tmp != '\n')
79 return -1;
80
81 *ad_beg_p = data;
82 *ip = cpu_to_be32(simple_strtoul(data, &data, 10));
83
84 /* skip blanks between ip and port */
85 while (*data == ' ') {
86 if (data >= data_end)
87 return -1;
88 data++;
89 }
90
91 *port = simple_strtoul(data, &data, 10);
92 *ad_end_p = data;
93
94 return 0;
95 }
96
help(struct sk_buff * skb,unsigned int protoff,struct nf_conn * ct,enum ip_conntrack_info ctinfo)97 static int help(struct sk_buff *skb, unsigned int protoff,
98 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
99 {
100 unsigned int dataoff;
101 const struct iphdr *iph;
102 const struct tcphdr *th;
103 struct tcphdr _tcph;
104 const char *data_limit;
105 char *data, *ib_ptr;
106 int dir = CTINFO2DIR(ctinfo);
107 struct nf_conntrack_expect *exp;
108 struct nf_conntrack_tuple *tuple;
109 __be32 dcc_ip;
110 u_int16_t dcc_port;
111 __be16 port;
112 int i, ret = NF_ACCEPT;
113 char *addr_beg_p, *addr_end_p;
114 nf_nat_irc_hook_fn *nf_nat_irc;
115 unsigned int datalen;
116
117 /* If packet is coming from IRC server */
118 if (dir == IP_CT_DIR_REPLY)
119 return NF_ACCEPT;
120
121 /* Until there's been traffic both ways, don't look in packets. */
122 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
123 return NF_ACCEPT;
124
125 /* Not a full tcp header? */
126 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
127 if (th == NULL)
128 return NF_ACCEPT;
129
130 /* No data? */
131 dataoff = protoff + th->doff*4;
132 if (dataoff >= skb->len)
133 return NF_ACCEPT;
134
135 datalen = skb->len - dataoff;
136 if (datalen > MAX_SEARCH_SIZE)
137 datalen = MAX_SEARCH_SIZE;
138
139 spin_lock_bh(&irc_buffer_lock);
140 ib_ptr = skb_header_pointer(skb, dataoff, datalen,
141 irc_buffer);
142 if (!ib_ptr) {
143 spin_unlock_bh(&irc_buffer_lock);
144 return NF_ACCEPT;
145 }
146
147 data = ib_ptr;
148 data_limit = ib_ptr + datalen;
149
150 /* Skip any whitespace */
151 while (data < data_limit - 10) {
152 if (*data == ' ' || *data == '\r' || *data == '\n')
153 data++;
154 else
155 break;
156 }
157
158 /* strlen("PRIVMSG x ")=10 */
159 if (data < data_limit - 10) {
160 if (strncasecmp("PRIVMSG ", data, 8))
161 goto out;
162 data += 8;
163 }
164
165 /* strlen(" :\1DCC SENT t AAAAAAAA P\1\n")=26
166 * 7+MINMATCHLEN+strlen("t AAAAAAAA P\1\n")=26
167 */
168 while (data < data_limit - (21 + MINMATCHLEN)) {
169 /* Find first " :", the start of message */
170 if (memcmp(data, " :", 2)) {
171 data++;
172 continue;
173 }
174 data += 2;
175
176 /* then check that place only for the DCC command */
177 if (memcmp(data, "\1DCC ", 5))
178 goto out;
179 data += 5;
180 /* we have at least (21+MINMATCHLEN)-(2+5) bytes valid data left */
181
182 iph = ip_hdr(skb);
183 pr_debug("DCC found in master %pI4:%u %pI4:%u\n",
184 &iph->saddr, ntohs(th->source),
185 &iph->daddr, ntohs(th->dest));
186
187 for (i = 0; i < ARRAY_SIZE(dccprotos); i++) {
188 if (memcmp(data, dccprotos[i], strlen(dccprotos[i]))) {
189 /* no match */
190 continue;
191 }
192 data += strlen(dccprotos[i]);
193 pr_debug("DCC %s detected\n", dccprotos[i]);
194
195 /* we have at least
196 * (21+MINMATCHLEN)-7-dccprotos[i].matchlen bytes valid
197 * data left (== 14/13 bytes) */
198 if (parse_dcc(data, data_limit, &dcc_ip,
199 &dcc_port, &addr_beg_p, &addr_end_p)) {
200 pr_debug("unable to parse dcc command\n");
201 goto out;
202 }
203
204 pr_debug("DCC bound ip/port: %pI4:%u\n",
205 &dcc_ip, dcc_port);
206
207 /* dcc_ip can be the internal OR external (NAT'ed) IP */
208 tuple = &ct->tuplehash[dir].tuple;
209 if ((tuple->src.u3.ip != dcc_ip &&
210 ct->tuplehash[!dir].tuple.dst.u3.ip != dcc_ip) ||
211 dcc_port == 0) {
212 net_warn_ratelimited("Forged DCC command from %pI4: %pI4:%u\n",
213 &tuple->src.u3.ip,
214 &dcc_ip, dcc_port);
215 goto out;
216 }
217
218 exp = nf_ct_expect_alloc(ct);
219 if (exp == NULL) {
220 nf_ct_helper_log(skb, ct,
221 "cannot alloc expectation");
222 ret = NF_DROP;
223 goto out;
224 }
225 tuple = &ct->tuplehash[!dir].tuple;
226 port = htons(dcc_port);
227 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
228 tuple->src.l3num,
229 NULL, &tuple->dst.u3,
230 IPPROTO_TCP, NULL, &port);
231
232 nf_nat_irc = rcu_dereference(nf_nat_irc_hook);
233 if (nf_nat_irc && ct->status & IPS_NAT_MASK)
234 ret = nf_nat_irc(skb, ct, ctinfo, protoff,
235 addr_beg_p - ib_ptr,
236 addr_end_p - addr_beg_p,
237 exp);
238 else if (nf_ct_expect_related(exp, 0) != 0) {
239 nf_ct_helper_log(skb, ct,
240 "cannot add expectation");
241 ret = NF_DROP;
242 }
243 nf_ct_expect_put(exp);
244 goto out;
245 }
246 }
247 out:
248 spin_unlock_bh(&irc_buffer_lock);
249 return ret;
250 }
251
252 static struct nf_conntrack_helper irc __read_mostly;
253 static struct nf_conntrack_helper *irc_ptr __read_mostly;
254 static struct nf_conntrack_expect_policy irc_exp_policy;
255
nf_conntrack_irc_init(void)256 static int __init nf_conntrack_irc_init(void)
257 {
258 int ret;
259
260 nf_conntrack_helper_deprecated(HELPER_NAME);
261
262 if (max_dcc_channels < 1) {
263 pr_err("max_dcc_channels must not be zero\n");
264 return -EINVAL;
265 }
266
267 if (max_dcc_channels > NF_CT_EXPECT_MAX_CNT) {
268 pr_err("max_dcc_channels must not be more than %u\n",
269 NF_CT_EXPECT_MAX_CNT);
270 return -EINVAL;
271 }
272
273 irc_exp_policy.max_expected = max_dcc_channels;
274 irc_exp_policy.timeout = dcc_timeout;
275
276 irc_buffer = kmalloc(MAX_SEARCH_SIZE + 1, GFP_KERNEL);
277 if (!irc_buffer)
278 return -ENOMEM;
279
280 nf_ct_helper_init(&irc, AF_INET, IPPROTO_TCP, HELPER_NAME,
281 &irc_exp_policy,
282 0, help, NULL, THIS_MODULE);
283
284 ret = nf_conntrack_helper_register(&irc, &irc_ptr);
285 if (ret) {
286 pr_err("failed to register helpers\n");
287 kfree(irc_buffer);
288 return ret;
289 }
290
291 return 0;
292 }
293
nf_conntrack_irc_fini(void)294 static void __exit nf_conntrack_irc_fini(void)
295 {
296 nf_conntrack_helper_unregister(irc_ptr);
297 kfree(irc_buffer);
298 }
299
300 module_init(nf_conntrack_irc_init);
301 module_exit(nf_conntrack_irc_fini);
302