2 * Copyright (C) 2012 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
11 #define IPF_IRCBUFSZ 96 /* This *MUST* be >= 64! */
14 void ipf_p_irc_main_load __P((void));
15 void ipf_p_irc_main_unload __P((void));
16 int ipf_p_irc_new __P((void *, fr_info_t *, ap_session_t *, nat_t *));
17 int ipf_p_irc_out __P((void *, fr_info_t *, ap_session_t *, nat_t *));
18 int ipf_p_irc_send __P((fr_info_t *, nat_t *));
19 int ipf_p_irc_complete __P((ircinfo_t *, char *, size_t));
20 u_short ipf_irc_atoi __P((char **));
22 static frentry_t ircnatfr;
24 int irc_proxy_init = 0;
28 * Initialize local structures.
33 bzero((char *)&ircnatfr, sizeof(ircnatfr));
35 ircnatfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
36 MUTEX_INIT(&ircnatfr.fr_lock, "IRC proxy rule lock");
42 ipf_p_irc_main_unload()
44 if (irc_proxy_init == 1) {
45 MUTEX_DESTROY(&ircnatfr.fr_lock);
51 const char *ipf_p_irc_dcctypes[] = {
52 "CHAT ", /* CHAT chat ipnumber portnumber */
53 "SEND ", /* SEND filename ipnumber portnumber */
62 * :A PRIVMSG B :^ADCC CHAT chat 0 0^A\r\n
63 * PRIVMSG B ^ADCC CHAT chat 0 0^A\r\n
68 ipf_p_irc_complete(ircp, buf, len)
87 if ((c != ':') && (c != 'P'))
92 * Loosely check that the source is a nickname of some sort
100 for (c = *s; !ISSPACE(c) && (i > 0); i--)
107 ircp->irc_snick = NULL;
110 * Check command string
112 if (strncmp(s, "PRIVMSG ", 8))
120 * Loosely check that the destination is a nickname of some sort
124 for (; !ISSPACE(c) && (i > 0); i--)
132 * Look for a ^A to start the DCC
140 if (strncmp(s, "\001DCC ", 4))
147 * Check for a recognised DCC command
149 for (j = 0, k = 0; ipf_p_irc_dcctypes[j]; j++) {
150 k = MIN(strlen(ipf_p_irc_dcctypes[j]), i);
151 if (!strncmp(ipf_p_irc_dcctypes[j], s, k))
154 if (!ipf_p_irc_dcctypes[j])
171 for (; (c != ' ') && (c != '\001') && (i > 0); i--)
174 if (c == '\001') /* In reality a ^A can quote another ^A...*/
189 for (l = 0; ISDIGIT(c) && (i > 0); i--) {
210 for (l = 0; ISDIGIT(c) && (i > 0); i--) {
217 if (strncmp(s, "\001\r\n", 3))
220 ircp->irc_len = s - buf;
227 ipf_p_irc_new(arg, fin, aps, nat)
238 KMALLOC(irc, ircinfo_t *);
242 nat = nat; /* LINT */
245 aps->aps_psiz = sizeof(ircinfo_t);
247 bzero((char *)irc, sizeof(*irc));
253 ipf_p_irc_send(fin, nat)
257 char ctcpbuf[IPF_IRCBUFSZ], newbuf[IPF_IRCBUFSZ];
258 tcphdr_t *tcp, tcph, *tcp2 = &tcph;
259 int off, inc = 0, i, dlen;
260 ipf_main_softc_t *softc;
261 size_t nlen = 0, olen;
273 softc = fin->fin_main_soft;
277 tcp = (tcphdr_t *)fin->fin_dp;
278 bzero(ctcpbuf, sizeof(ctcpbuf));
279 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
282 dlen = fin->fin_plen - off;
284 dlen = MSGDSIZE(m) - off;
288 COPYDATA(m, off, MIN(sizeof(ctcpbuf), dlen), ctcpbuf);
292 ctcpbuf[sizeof(ctcpbuf) - 1] = '\0';
295 irc = nat->nat_aps->aps_data;
296 if (ipf_p_irc_complete(irc, ctcpbuf, dlen) == 0)
300 * check that IP address in the DCC reply is the same as the
301 * sender of the command - prevents use for port scanning.
303 if (irc->irc_ipnum != ntohl(nat->nat_osrcaddr))
309 * Calculate new address parts for the DCC command
311 a1 = ntohl(ip->ip_src.s_addr);
313 i = irc->irc_addr - ctcpbuf;
315 (void) strncpy(newbuf, ctcpbuf, i);
316 /* DO NOT change these! */
317 #if defined(SNPRINTF) && defined(KERNEL)
318 SNPRINTF(newbuf, sizeof(newbuf) - i, "%u %u\001\r\n", a1, a5);
320 (void) sprintf(newbuf, "%u %u\001\r\n", a1, a5);
323 nlen = strlen(newbuf);
326 if ((inc + fin->fin_plen) > 65535)
330 for (m1 = m; m1->b_cont; m1 = m1->b_cont)
332 if ((inc > 0) && (m1->b_datap->db_lim - m1->b_wptr < inc)) {
335 /* alloc enough to keep same trailer space for lower driver */
336 nm = allocb(nlen, BPRI_MED);
337 PANIC((!nm),("ipf_p_irc_out: allocb failed"));
339 nm->b_band = m1->b_band;
343 PANIC((m1->b_wptr < m1->b_rptr),
344 ("ipf_p_irc_out: cannot handle fragmented data block"));
348 # if SOLARIS && defined(ICK_VALID)
349 if (m1->b_datap->db_struiolim == m1->b_wptr)
350 m1->b_datap->db_struiolim += inc;
351 m1->b_datap->db_struioflag &= ~STRUIO_IP;
358 /* the mbuf chain will be extended if necessary by m_copyback() */
360 COPYBACK(m, off, nlen, newbuf);
361 fin->fin_flx |= FI_DOCKSUM;
364 #if defined(MENTAT) || defined(__sgi)
365 register u_32_t sum1, sum2;
367 sum1 = fin->fin_plen;
368 sum2 = fin->fin_plen + inc;
370 /* Because ~1 == -2, We really need ~1 == -1 */
374 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
376 ipf_fix_outcksum(0, &ip->ip_sum, sum2, 0);
378 fin->fin_plen += inc;
379 ip->ip_len = htons(fin->fin_plen);
380 fin->fin_dlen += inc;
384 * Add skeleton NAT entry for connection which will come back the
389 * Don't allow the PORT command to specify a port < 1024 due to
392 if (ntohs(sp) < 1024)
396 * The server may not make the connection back from port 20, but
397 * it is the most likely so use it here to check for a conflicting
400 bcopy((caddr_t)fin, (caddr_t)&fi, sizeof(fi));
402 fi.fin_data[1] = fin->fin_data[1];
403 nat2 = ipf_nat_outlookup(fin, IPN_TCP, nat->nat_pr[1], nat->nat_nsrcip,
407 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
410 bcopy((caddr_t)fin, (caddr_t)&fi, sizeof(fi));
411 bzero((char *)tcp2, sizeof(*tcp2));
412 tcp2->th_win = htons(8192);
414 tcp2->th_dport = 0; /* XXX - don't specify remote port */
415 fi.fin_data[0] = ntohs(sp);
417 fi.fin_dp = (char *)tcp2;
418 fi.fin_fr = &ircnatfr;
419 fi.fin_dlen = sizeof(*tcp2);
420 fi.fin_plen = fi.fin_hlen + sizeof(*tcp2);
422 ip->ip_src = nat->nat_nsrcip;
423 MUTEX_ENTER(&softn->ipf_nat_new);
424 nat2 = ipf_nat_add(&fi, nat->nat_ptr, NULL,
425 NAT_SLAVE|IPN_TCP|SI_W_DPORT, NAT_OUTBOUND);
426 MUTEX_EXIT(&softn->ipf_nat_new);
428 (void) ipf_nat_proto(&fi, nat2, 0);
429 MUTEX_ENTER(&nat2->nat_lock);
430 ipf_nat_update(&fi, nat2);
431 MUTEX_EXIT(&nat2->nat_lock);
433 (void) ipf_state_add(softc, &fi, NULL, SI_W_DPORT);
442 ipf_p_irc_out(arg, fin, aps, nat)
448 aps = aps; /* LINT */
449 return ipf_p_irc_send(fin, nat);