1 /* IRC extension for TCP NAT alteration.
2 * (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
3 * based on a copy of RR's ip_nat_ftp.c
5 * ip_nat_irc.c,v 1.16 2001/12/06 07:42:10 laforge Exp
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * insmod ip_nat_irc.o ports=port1,port2,...port<MAX_PORTS>
15 * please give the ports of all IRC servers You wish to connect to.
16 * If You don't specify ports, the default will be port 6667
19 #include <linux/module.h>
20 #include <linux/netfilter_ipv4.h>
22 #include <linux/tcp.h>
23 #include <linux/kernel.h>
25 #include <linux/netfilter_ipv4/ip_nat.h>
26 #include <linux/netfilter_ipv4/ip_nat_helper.h>
27 #include <linux/netfilter_ipv4/ip_nat_rule.h>
28 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
29 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
30 #include <linux/moduleparam.h>
35 #define DEBUGP(format, args...)
39 static int ports[MAX_PORTS];
42 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
43 MODULE_DESCRIPTION("IRC (DCC) NAT helper");
44 MODULE_LICENSE("GPL");
45 module_param_array(ports, int, ports_c, 0400);
46 MODULE_PARM_DESC(ports, "port numbers of IRC servers");
48 /* FIXME: Time out? --RR */
51 irc_nat_expected(struct sk_buff **pskb,
53 struct ip_conntrack *ct,
54 struct ip_nat_info *info)
56 struct ip_nat_multi_range mr;
57 u_int32_t newdstip, newsrcip, newip;
59 struct ip_conntrack *master = master_ct(ct);
64 IP_NF_ASSERT(!(info->initialized & (1 << HOOK2MANIP(hooknum))));
66 DEBUGP("nat_expected: We have a connection!\n");
68 newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
69 newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
70 DEBUGP("nat_expected: DCC cmd. %u.%u.%u.%u->%u.%u.%u.%u\n",
71 NIPQUAD(newsrcip), NIPQUAD(newdstip));
73 if (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC)
78 DEBUGP("nat_expected: IP to %u.%u.%u.%u\n", NIPQUAD(newip));
81 /* We don't want to manip the per-protocol, just the IPs. */
82 mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
83 mr.range[0].min_ip = mr.range[0].max_ip = newip;
85 return ip_nat_setup_info(ct, &mr, hooknum);
88 static int irc_data_fixup(const struct ip_ct_irc_expect *exp_irc_info,
89 struct ip_conntrack *ct,
90 struct sk_buff **pskb,
91 enum ip_conntrack_info ctinfo,
92 struct ip_conntrack_expect *expect)
95 struct ip_conntrack_tuple t;
96 struct iphdr *iph = (*pskb)->nh.iph;
97 struct tcphdr *tcph = (void *) iph + iph->ihl * 4;
100 /* "4294967296 65635 " */
103 DEBUGP("IRC_NAT: info (seq %u + %u) in %u\n",
104 expect->seq, exp_irc_info->len,
107 newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
109 /* Alter conntrack's expectations. */
112 for (port = exp_irc_info->port; port != 0; port++) {
113 t.dst.u.tcp.port = htons(port);
114 if (ip_conntrack_change_expect(expect, &t) == 0) {
115 DEBUGP("using port %d", port);
123 /* strlen("\1DCC CHAT chat AAAAAAAA P\1\n")=27
124 * strlen("\1DCC SCHAT chat AAAAAAAA P\1\n")=28
125 * strlen("\1DCC SEND F AAAAAAAA P S\1\n")=26
126 * strlen("\1DCC MOVE F AAAAAAAA P S\1\n")=26
127 * strlen("\1DCC TSEND F AAAAAAAA P S\1\n")=27
128 * AAAAAAAAA: bound addr (1.0.0.0==16777216, min 8 digits,
129 * 255.255.255.255==4294967296, 10 digits)
130 * P: bound port (min 1 d, max 5d (65635))
131 * F: filename (min 1 d )
133 * 0x01, \n: terminators
136 sprintf(buffer, "%u %u", ntohl(newip), port);
137 DEBUGP("ip_nat_irc: Inserting '%s' == %u.%u.%u.%u, port %u\n",
138 buffer, NIPQUAD(newip), port);
140 return ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
141 expect->seq - ntohl(tcph->seq),
142 exp_irc_info->len, buffer,
146 static unsigned int help(struct ip_conntrack *ct,
147 struct ip_conntrack_expect *exp,
148 struct ip_nat_info *info,
149 enum ip_conntrack_info ctinfo,
150 unsigned int hooknum,
151 struct sk_buff **pskb)
153 struct iphdr *iph = (*pskb)->nh.iph;
154 struct tcphdr *tcph = (void *) iph + iph->ihl * 4;
155 unsigned int datalen;
157 struct ip_ct_irc_expect *exp_irc_info;
160 DEBUGP("ip_nat_irc: no exp!!");
162 exp_irc_info = &exp->help.exp_irc_info;
164 /* Only mangle things once: original direction in POST_ROUTING
165 and reply direction on PRE_ROUTING. */
166 dir = CTINFO2DIR(ctinfo);
167 if (!((hooknum == NF_IP_POST_ROUTING && dir == IP_CT_DIR_ORIGINAL)
168 || (hooknum == NF_IP_PRE_ROUTING && dir == IP_CT_DIR_REPLY))) {
169 DEBUGP("nat_irc: Not touching dir %s at hook %s\n",
170 dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY",
171 hooknum == NF_IP_POST_ROUTING ? "POSTROUTING"
172 : hooknum == NF_IP_PRE_ROUTING ? "PREROUTING"
173 : hooknum == NF_IP_LOCAL_OUT ? "OUTPUT" : "???");
176 DEBUGP("got beyond not touching\n");
178 datalen = (*pskb)->len - iph->ihl * 4 - tcph->doff * 4;
179 /* Check whether the whole IP/address pattern is carried in the payload */
180 if (between(exp->seq + exp_irc_info->len,
182 ntohl(tcph->seq) + datalen)) {
183 if (!irc_data_fixup(exp_irc_info, ct, pskb, ctinfo, exp))
186 /* Half a match? This means a partial retransmisison.
187 It's a cracker being funky. */
188 if (net_ratelimit()) {
190 ("IRC_NAT: partial packet %u/%u in %u/%u\n",
191 exp->seq, exp_irc_info->len,
193 ntohl(tcph->seq) + datalen);
200 static struct ip_nat_helper ip_nat_irc_helpers[MAX_PORTS];
201 static char irc_names[MAX_PORTS][10];
203 /* This function is intentionally _NOT_ defined as __exit, because
204 * it is needed by init() */
205 static void fini(void)
209 for (i = 0; i < ports_c; i++) {
210 DEBUGP("ip_nat_irc: unregistering helper for port %d\n",
212 ip_nat_helper_unregister(&ip_nat_irc_helpers[i]);
216 static int __init init(void)
220 struct ip_nat_helper *hlpr;
224 ports[ports_c++] = IRC_PORT;
226 for (i = 0; i < ports_c; i++) {
227 hlpr = &ip_nat_irc_helpers[i];
228 hlpr->tuple.dst.protonum = IPPROTO_TCP;
229 hlpr->tuple.src.u.tcp.port = htons(ports[i]);
230 hlpr->mask.src.u.tcp.port = 0xFFFF;
231 hlpr->mask.dst.protonum = 0xFFFF;
234 hlpr->me = THIS_MODULE;
235 hlpr->expect = irc_nat_expected;
237 tmpname = &irc_names[i][0];
238 if (ports[i] == IRC_PORT)
239 sprintf(tmpname, "irc");
241 sprintf(tmpname, "irc-%d", i);
242 hlpr->name = tmpname;
245 ("ip_nat_irc: Trying to register helper for port %d: name %s\n",
246 ports[i], hlpr->name);
247 ret = ip_nat_helper_register(hlpr);
251 ("ip_nat_irc: error registering helper for port %d\n",
260 NEEDS_CONNTRACK(irc);