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