2 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 /* Alias_irc.c intercepts packages contain IRC CTCP commands, and
31 changes DCC commands to export a port on the aliasing host instead
34 For this routine to work, the DCC command must fit entirely into a
35 single TCP packet. This will usually happen, but is not
38 The interception is likely to change the length of the packet.
39 The handling of this is copied more-or-less verbatim from
42 Initial version: Eivind Eklund <perhaps@yes.no> (ee) 97-01-29
44 Version 2.1: May, 1997 (cjm)
45 Very minor changes to conform with
46 local/global/function naming conventions
47 withing the packet alising module.
52 #include <sys/param.h>
53 #include <sys/ctype.h>
54 #include <sys/limits.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/module.h>
60 #include <sys/types.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in.h>
68 #include <netinet/ip.h>
69 #include <netinet/tcp.h>
72 #include <netinet/libalias/alias.h>
73 #include <netinet/libalias/alias_local.h>
74 #include <netinet/libalias/alias_mod.h>
76 #include "alias_local.h"
77 #include "alias_mod.h"
80 #define IRC_CONTROL_PORT_NUMBER_1 6667
81 #define IRC_CONTROL_PORT_NUMBER_2 6668
87 AliasHandleIrcOut(struct libalias *, struct ip *, struct alias_link *,
91 fingerprint(struct libalias *la, struct ip *pip, struct alias_data *ah)
94 if (ah->dport == NULL || ah->dport == NULL || ah->lnk == NULL ||
97 if (ntohs(*ah->dport) == IRC_CONTROL_PORT_NUMBER_1
98 || ntohs(*ah->dport) == IRC_CONTROL_PORT_NUMBER_2)
104 protohandler(struct libalias *la, struct ip *pip, struct alias_data *ah)
107 AliasHandleIrcOut(la, pip, ah->lnk, ah->maxpktsize);
111 struct proto_handler handlers[] = {
116 .fingerprint = &fingerprint,
117 .protohandler = &protohandler
123 mod_handler(module_t mod, int type, void *data)
130 LibAliasAttachHandlers(handlers);
134 LibAliasDetachHandlers(handlers);
145 moduledata_t alias_mod = {
146 "alias_irc", mod_handler, NULL
149 /* Kernel module definition. */
151 DECLARE_MODULE(alias_irc, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
152 MODULE_VERSION(alias_irc, 1);
153 MODULE_DEPEND(alias_irc, libalias, 1, 1, 1);
157 AliasHandleIrcOut(struct libalias *la,
158 struct ip *pip, /* IP packet to examine */
159 struct alias_link *lnk, /* Which link are we on? */
160 int maxsize /* Maximum size of IP packet including
164 int hlen, tlen, dlen;
165 struct in_addr true_addr;
169 int i; /* Iterator through the source */
171 /* Calculate data length of TCP packet */
172 tc = (struct tcphdr *)ip_next(pip);
173 hlen = (pip->ip_hl + tc->th_off) << 2;
174 tlen = ntohs(pip->ip_len);
178 * Return if data length is too short - assume an entire PRIVMSG in
181 if (dlen < (int)sizeof(":A!a@n.n PRIVMSG A :aDCC 1 1a") - 1)
184 /* Place string pointer at beginning of data */
187 maxsize -= hlen; /* We're interested in maximum size of
188 * data, not packet */
190 /* Search for a CTCP command [Note 1] */
191 for (i = 0; i < dlen; i++) {
192 if (sptr[i] == '\001')
195 return; /* No CTCP commands in */
196 /* Handle CTCP commands - the buffer may have to be copied */
199 char newpacket[65536]; /* Estimate of maximum packet size
201 unsigned int copyat = i; /* Same */
202 unsigned int iCopy = 0; /* How much data have we written to
203 * copy-back string? */
204 unsigned long org_addr; /* Original IP address */
205 unsigned short org_port; /* Original source port
209 if (i >= dlen || iCopy >= sizeof(newpacket))
211 newpacket[iCopy++] = sptr[i++]; /* Copy the CTCP start
213 /* Start of a CTCP */
214 if (i + 4 >= dlen) /* Too short for DCC */
216 if (sptr[i + 0] != 'D')
218 if (sptr[i + 1] != 'C')
220 if (sptr[i + 2] != 'C')
222 if (sptr[i + 3] != ' ')
224 /* We have a DCC command - handle it! */
225 i += 4; /* Skip "DCC " */
226 if (iCopy + 4 > sizeof(newpacket))
228 newpacket[iCopy++] = 'D';
229 newpacket[iCopy++] = 'C';
230 newpacket[iCopy++] = 'C';
231 newpacket[iCopy++] = ' ';
233 DBprintf(("Found DCC\n"));
235 * Skip any extra spaces (should not occur according to
236 * protocol, but DCC breaks CTCP protocol anyway
238 while (sptr[i] == ' ') {
240 DBprintf(("DCC packet terminated in just spaces\n"));
245 DBprintf(("Transferring command...\n"));
246 while (sptr[i] != ' ') {
247 newpacket[iCopy++] = sptr[i];
248 if (++i >= dlen || iCopy >= sizeof(newpacket)) {
249 DBprintf(("DCC packet terminated during command\n"));
253 /* Copy _one_ space */
254 if (i + 1 < dlen && iCopy < sizeof(newpacket))
255 newpacket[iCopy++] = sptr[i++];
257 DBprintf(("Done command - removing spaces\n"));
259 * Skip any extra spaces (should not occur according to
260 * protocol, but DCC breaks CTCP protocol anyway
262 while (sptr[i] == ' ') {
264 DBprintf(("DCC packet terminated in just spaces (post-command)\n"));
269 DBprintf(("Transferring filename...\n"));
270 while (sptr[i] != ' ') {
271 newpacket[iCopy++] = sptr[i];
272 if (++i >= dlen || iCopy >= sizeof(newpacket)) {
273 DBprintf(("DCC packet terminated during filename\n"));
277 /* Copy _one_ space */
278 if (i + 1 < dlen && iCopy < sizeof(newpacket))
279 newpacket[iCopy++] = sptr[i++];
281 DBprintf(("Done filename - removing spaces\n"));
283 * Skip any extra spaces (should not occur according to
284 * protocol, but DCC breaks CTCP protocol anyway
286 while (sptr[i] == ' ') {
288 DBprintf(("DCC packet terminated in just spaces (post-filename)\n"));
293 DBprintf(("Fetching IP address\n"));
294 /* Fetch IP address */
296 while (i < dlen && isdigit(sptr[i])) {
297 if (org_addr > ULONG_MAX / 10UL) { /* Terminate on overflow */
298 DBprintf(("DCC Address overflow (org_addr == 0x%08lx, next char %c\n", org_addr, sptr[i]));
302 org_addr += sptr[i++] - '0';
304 DBprintf(("Skipping space\n"));
305 if (i + 1 >= dlen || sptr[i] != ' ') {
306 DBprintf(("Overflow (%d >= %d) or bad character (%02x) terminating IP address\n", i + 1, dlen, sptr[i]));
310 * Skip any extra spaces (should not occur according to
311 * protocol, but DCC breaks CTCP protocol anyway, so we
312 * might as well play it safe
314 while (sptr[i] == ' ') {
316 DBprintf(("Packet failure - space overflow.\n"));
320 DBprintf(("Fetching port number\n"));
321 /* Fetch source port */
323 while (i < dlen && isdigit(sptr[i])) {
324 if (org_port > 6554) { /* Terminate on overflow
325 * (65536/10 rounded up */
326 DBprintf(("DCC: port number overflow\n"));
330 org_port += sptr[i++] - '0';
332 /* Skip illegal addresses (or early termination) */
333 if (i >= dlen || (sptr[i] != '\001' && sptr[i] != ' ')) {
334 DBprintf(("Bad port termination\n"));
337 DBprintf(("Got IP %lu and port %u\n", org_addr, (unsigned)org_port));
339 /* We've got the address and port - now alias it */
341 struct alias_link *dcc_lnk;
342 struct in_addr destaddr;
345 true_port = htons(org_port);
346 true_addr.s_addr = htonl(org_addr);
349 /* Sanity/Security checking */
350 if (!org_addr || !org_port ||
351 pip->ip_src.s_addr != true_addr.s_addr ||
352 org_port < IPPORT_RESERVED)
356 * Steal the FTP_DATA_PORT - it doesn't really
357 * matter, and this would probably allow it through
358 * at least _some_ firewalls.
360 dcc_lnk = FindUdpTcpOut(la, true_addr, destaddr,
363 DBprintf(("Got a DCC link\n"));
365 struct in_addr alias_address; /* Address from aliasing */
366 u_short alias_port; /* Port given by
371 /* Generate firewall hole as appropriate */
372 PunchFWHole(dcc_lnk);
375 alias_address = GetAliasAddress(lnk);
376 n = snprintf(&newpacket[iCopy],
377 sizeof(newpacket) - iCopy,
378 "%lu ", (u_long) htonl(alias_address.s_addr));
380 DBprintf(("DCC packet construct failure.\n"));
383 if ((iCopy += n) >= sizeof(newpacket)) { /* Truncated/fit exactly
385 DBprintf(("DCC constructed packet overflow.\n"));
388 alias_port = GetAliasPort(dcc_lnk);
389 n = snprintf(&newpacket[iCopy],
390 sizeof(newpacket) - iCopy,
391 "%u", htons(alias_port));
393 DBprintf(("DCC packet construct failure.\n"));
398 * Done - truncated cases will be taken
399 * care of by lBAD_CTCP
401 DBprintf(("Aliased IP %lu and port %u\n", alias_address.s_addr, (unsigned)alias_port));
405 * An uninteresting CTCP - state entered right after '\001'
406 * has been pushed. Also used to copy the rest of a DCC,
407 * after IP address and port has been handled
410 for (; i < dlen && iCopy < sizeof(newpacket); i++, iCopy++) {
411 newpacket[iCopy] = sptr[i]; /* Copy CTCP unchanged */
412 if (sptr[i] == '\001') {
419 for (; i < dlen && iCopy < sizeof(newpacket); i++, iCopy++) {
420 newpacket[iCopy] = sptr[i]; /* Copy CTCP unchanged */
421 if (sptr[i] == '\001') {
425 /* Handle the end of a packet */
427 iCopy = iCopy > maxsize - copyat ? maxsize - copyat : iCopy;
428 memcpy(sptr + copyat, newpacket, iCopy);
430 /* Save information regarding modified seq and ack numbers */
435 delta = GetDeltaSeqOut(pip, lnk);
436 AddSeq(pip, lnk, delta + copyat + iCopy - dlen);
439 /* Revise IP header */
443 new_len = htons(hlen + iCopy + copyat);
444 DifferentialChecksum(&pip->ip_sum,
448 pip->ip_len = new_len;
451 /* Compute TCP checksum for revised packet */
456 tc->th_sum = TcpChecksum(pip);
464 The initial search will most often fail; it could be replaced with a 32-bit specific search.
465 Such a search would be done for 32-bit unsigned value V:
466 V ^= 0x01010101; (Search is for null bytes)
467 if( ((V-0x01010101)^V) & 0x80808080 ) {
468 (found a null bytes which was a 01 byte)
470 To assert that the processor is 32-bits, do
471 extern int ircdccar[32]; (32 bits)
472 extern int ircdccar[CHAR_BIT*sizeof(unsigned int)];
473 which will generate a type-error on all but 32-bit machines.
475 [Note 2] This routine really ought to be replaced with one that
476 creates a transparent proxy on the aliasing host, to allow arbitary
477 changes in the TCP stream. This should not be too difficult given
478 this base; I (ee) will try to do this some time later.