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