1 /* $NetBSD: ip_irc_pxy.c,v 1.3 2012/07/22 14:27:51 darrenr Exp $ */ 2 3 /* 4 * Copyright (C) 2012 by Darren Reed. 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 * 8 * Id: ip_irc_pxy.c,v 1.1.1.2 2012/07/22 13:45:19 darrenr Exp 9 */ 10 11 #include <sys/cdefs.h> 12 __KERNEL_RCSID(1, "$NetBSD: ip_irc_pxy.c,v 1.3 2012/07/22 14:27:51 darrenr Exp $"); 13 14 #define IPF_IRC_PROXY 15 16 #define IPF_IRCBUFSZ 96 /* This *MUST* be >= 64! */ 17 18 19 void ipf_p_irc_main_load(void); 20 void ipf_p_irc_main_unload(void); 21 int ipf_p_irc_new(void *, fr_info_t *, ap_session_t *, nat_t *); 22 int ipf_p_irc_out(void *, fr_info_t *, ap_session_t *, nat_t *); 23 int ipf_p_irc_send(fr_info_t *, nat_t *); 24 int ipf_p_irc_complete(ircinfo_t *, char *, size_t); 25 u_short ipf_irc_atoi(char **); 26 27 static frentry_t ircnatfr; 28 29 int irc_proxy_init = 0; 30 31 32 /* 33 * Initialize local structures. 34 */ 35 void 36 ipf_p_irc_main_load(void) 37 { 38 bzero((char *)&ircnatfr, sizeof(ircnatfr)); 39 ircnatfr.fr_ref = 1; 40 ircnatfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE; 41 MUTEX_INIT(&ircnatfr.fr_lock, "IRC proxy rule lock"); 42 irc_proxy_init = 1; 43 } 44 45 46 void 47 ipf_p_irc_main_unload(void) 48 { 49 if (irc_proxy_init == 1) { 50 MUTEX_DESTROY(&ircnatfr.fr_lock); 51 irc_proxy_init = 0; 52 } 53 } 54 55 56 const char *ipf_p_irc_dcctypes[] = { 57 "CHAT ", /* CHAT chat ipnumber portnumber */ 58 "SEND ", /* SEND filename ipnumber portnumber */ 59 "MOVE ", 60 "TSEND ", 61 "SCHAT ", 62 NULL, 63 }; 64 65 66 /* 67 * :A PRIVMSG B :^ADCC CHAT chat 0 0^A\r\n 68 * PRIVMSG B ^ADCC CHAT chat 0 0^A\r\n 69 */ 70 71 72 int 73 ipf_p_irc_complete(ircinfo_t *ircp, char *buf, size_t len) 74 { 75 register char *s, c; 76 register size_t i; 77 u_32_t l; 78 int j, k; 79 80 ircp->irc_ipnum = 0; 81 ircp->irc_port = 0; 82 83 if (len < 31) 84 return 0; 85 s = buf; 86 c = *s++; 87 i = len - 1; 88 89 if ((c != ':') && (c != 'P')) 90 return 0; 91 92 if (c == ':') { 93 /* 94 * Loosely check that the source is a nickname of some sort 95 */ 96 s++; 97 c = *s; 98 ircp->irc_snick = s; 99 if (!ISALPHA(c)) 100 return 0; 101 i--; 102 for (c = *s; !ISSPACE(c) && (i > 0); i--) 103 c = *s++; 104 if (i < 31) 105 return 0; 106 if (c != 'P') 107 return 0; 108 } else 109 ircp->irc_snick = NULL; 110 111 /* 112 * Check command string 113 */ 114 if (strncmp(s, "PRIVMSG ", 8)) 115 return 0; 116 i -= 8; 117 s += 8; 118 c = *s; 119 ircp->irc_dnick = s; 120 121 /* 122 * Loosely check that the destination is a nickname of some sort 123 */ 124 if (!ISALPHA(c)) 125 return 0; 126 for (; !ISSPACE(c) && (i > 0); i--) 127 c = *s++; 128 if (i < 20) 129 return 0; 130 s++, 131 i--; 132 133 /* 134 * Look for a ^A to start the DCC 135 */ 136 c = *s; 137 if (c == ':') { 138 s++; 139 c = *s; 140 } 141 142 if (strncmp(s, "\001DCC ", 4)) 143 return 0; 144 145 i -= 4; 146 s += 4; 147 148 /* 149 * Check for a recognised DCC command 150 */ 151 for (j = 0, k = 0; ipf_p_irc_dcctypes[j]; j++) { 152 k = MIN(strlen(ipf_p_irc_dcctypes[j]), i); 153 if (!strncmp(ipf_p_irc_dcctypes[j], s, k)) 154 break; 155 } 156 if (!ipf_p_irc_dcctypes[j]) 157 return 0; 158 159 ircp->irc_type = s; 160 i -= k; 161 s += k; 162 163 if (i < 11) 164 return 0; 165 166 /* 167 * Check for the arg 168 */ 169 c = *s; 170 if (ISSPACE(c)) 171 return 0; 172 ircp->irc_arg = s; 173 for (; (c != ' ') && (c != '\001') && (i > 0); i--) 174 c = *s++; 175 176 if (c == '\001') /* In reality a ^A can quote another ^A...*/ 177 return 0; 178 179 if (i < 5) 180 return 0; 181 182 s++; 183 i--; 184 c = *s; 185 if (!ISDIGIT(c)) 186 return 0; 187 ircp->irc_addr = s; 188 /* 189 * Get the IP# 190 */ 191 for (l = 0; ISDIGIT(c) && (i > 0); i--) { 192 l *= 10; 193 l += c - '0'; 194 c = *s++; 195 } 196 197 if (i < 4) 198 return 0; 199 200 if (c != ' ') 201 return 0; 202 203 ircp->irc_ipnum = l; 204 s++; 205 i--; 206 c = *s; 207 if (!ISDIGIT(c)) 208 return 0; 209 /* 210 * Get the port# 211 */ 212 for (l = 0; ISDIGIT(c) && (i > 0); i--) { 213 l *= 10; 214 l += c - '0'; 215 c = *s++; 216 } 217 if (i < 3) 218 return 0; 219 if (strncmp(s, "\001\r\n", 3)) 220 return 0; 221 s += 3; 222 ircp->irc_len = s - buf; 223 ircp->irc_port = l; 224 return 1; 225 } 226 227 228 int 229 ipf_p_irc_new(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat) 230 { 231 ircinfo_t *irc; 232 233 if (fin->fin_v != 4) 234 return -1; 235 236 KMALLOC(irc, ircinfo_t *); 237 if (irc == NULL) 238 return -1; 239 240 nat = nat; /* LINT */ 241 242 aps->aps_data = irc; 243 aps->aps_psiz = sizeof(ircinfo_t); 244 245 bzero((char *)irc, sizeof(*irc)); 246 return 0; 247 } 248 249 250 int 251 ipf_p_irc_send(fr_info_t *fin, nat_t *nat) 252 { 253 char ctcpbuf[IPF_IRCBUFSZ], newbuf[IPF_IRCBUFSZ]; 254 tcphdr_t *tcp, tcph, *tcp2 = &tcph; 255 int off, inc = 0, i, dlen; 256 ipf_main_softc_t *softc; 257 size_t nlen = 0, olen; 258 struct in_addr swip; 259 u_short a5, sp; 260 ircinfo_t *irc; 261 fr_info_t fi; 262 nat_t *nat2; 263 u_int a1; 264 ip_t *ip; 265 mb_t *m; 266 #ifdef MENTAT 267 mb_t *m1; 268 #endif 269 softc = fin->fin_main_soft; 270 271 m = fin->fin_m; 272 ip = fin->fin_ip; 273 tcp = (tcphdr_t *)fin->fin_dp; 274 bzero(ctcpbuf, sizeof(ctcpbuf)); 275 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff; 276 277 #ifdef __sgi 278 dlen = fin->fin_plen - off; 279 #else 280 dlen = MSGDSIZE(m) - off; 281 #endif 282 if (dlen <= 0) 283 return 0; 284 COPYDATA(m, off, MIN(sizeof(ctcpbuf), dlen), ctcpbuf); 285 286 if (dlen <= 0) 287 return 0; 288 ctcpbuf[sizeof(ctcpbuf) - 1] = '\0'; 289 *newbuf = '\0'; 290 291 irc = nat->nat_aps->aps_data; 292 if (ipf_p_irc_complete(irc, ctcpbuf, dlen) == 0) 293 return 0; 294 295 /* 296 * check that IP address in the DCC reply is the same as the 297 * sender of the command - prevents use for port scanning. 298 */ 299 if (irc->irc_ipnum != ntohl(nat->nat_osrcaddr)) 300 return 0; 301 302 a5 = irc->irc_port; 303 304 /* 305 * Calculate new address parts for the DCC command 306 */ 307 a1 = ntohl(ip->ip_src.s_addr); 308 olen = irc->irc_len; 309 i = irc->irc_addr - ctcpbuf; 310 i++; 311 (void) strncpy(newbuf, ctcpbuf, i); 312 /* DO NOT change these! */ 313 #if defined(SNPRINTF) && defined(KERNEL) 314 SNPRINTF(newbuf, sizeof(newbuf) - i, "%u %u\001\r\n", a1, a5); 315 #else 316 (void) sprintf(newbuf, "%u %u\001\r\n", a1, a5); 317 #endif 318 319 nlen = strlen(newbuf); 320 inc = nlen - olen; 321 322 if ((inc + fin->fin_plen) > 65535) 323 return 0; 324 325 #ifdef MENTAT 326 for (m1 = m; m1->b_cont; m1 = m1->b_cont) 327 ; 328 if ((inc > 0) && (m1->b_datap->db_lim - m1->b_wptr < inc)) { 329 mblk_t *nm; 330 331 /* alloc enough to keep same trailer space for lower driver */ 332 nm = allocb(nlen, BPRI_MED); 333 PANIC((!nm),("ipf_p_irc_out: allocb failed")); 334 335 nm->b_band = m1->b_band; 336 nm->b_wptr += nlen; 337 338 m1->b_wptr -= olen; 339 PANIC((m1->b_wptr < m1->b_rptr), 340 ("ipf_p_irc_out: cannot handle fragmented data block")); 341 342 linkb(m1, nm); 343 } else { 344 # if SOLARIS && defined(ICK_VALID) 345 if (m1->b_datap->db_struiolim == m1->b_wptr) 346 m1->b_datap->db_struiolim += inc; 347 m1->b_datap->db_struioflag &= ~STRUIO_IP; 348 # endif 349 m1->b_wptr += inc; 350 } 351 #else 352 if (inc < 0) 353 m_adj(m, inc); 354 /* the mbuf chain will be extended if necessary by m_copyback() */ 355 #endif 356 COPYBACK(m, off, nlen, newbuf); 357 fin->fin_flx |= FI_DOCKSUM; 358 359 if (inc != 0) { 360 #if defined(MENTAT) || defined(__sgi) 361 register u_32_t sum1, sum2; 362 363 sum1 = fin->fin_plen; 364 sum2 = fin->fin_plen + inc; 365 366 /* Because ~1 == -2, We really need ~1 == -1 */ 367 if (sum1 > sum2) 368 sum2--; 369 sum2 -= sum1; 370 sum2 = (sum2 & 0xffff) + (sum2 >> 16); 371 372 ipf_fix_outcksum(0, &ip->ip_sum, sum2, 0); 373 #endif 374 fin->fin_plen += inc; 375 ip->ip_len = htons(fin->fin_plen); 376 fin->fin_dlen += inc; 377 } 378 379 /* 380 * Add skeleton NAT entry for connection which will come back the 381 * other way. 382 */ 383 sp = htons(a5); 384 /* 385 * Don't allow the PORT command to specify a port < 1024 due to 386 * security crap. 387 */ 388 if (ntohs(sp) < 1024) 389 return 0; 390 391 /* 392 * The server may not make the connection back from port 20, but 393 * it is the most likely so use it here to check for a conflicting 394 * mapping. 395 */ 396 bcopy((void *)fin, (void *)&fi, sizeof(fi)); 397 fi.fin_data[0] = sp; 398 fi.fin_data[1] = fin->fin_data[1]; 399 nat2 = ipf_nat_outlookup(fin, IPN_TCP, nat->nat_pr[1], nat->nat_nsrcip, 400 ip->ip_dst); 401 if (nat2 == NULL) { 402 #ifdef USE_MUTEXES 403 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 404 #endif 405 406 bcopy((void *)fin, (void *)&fi, sizeof(fi)); 407 bzero((char *)tcp2, sizeof(*tcp2)); 408 tcp2->th_win = htons(8192); 409 tcp2->th_sport = sp; 410 tcp2->th_dport = 0; /* XXX - don't specify remote port */ 411 fi.fin_data[0] = ntohs(sp); 412 fi.fin_data[1] = 0; 413 fi.fin_dp = (char *)tcp2; 414 fi.fin_fr = &ircnatfr; 415 fi.fin_dlen = sizeof(*tcp2); 416 fi.fin_plen = fi.fin_hlen + sizeof(*tcp2); 417 swip = ip->ip_src; 418 ip->ip_src = nat->nat_nsrcip; 419 MUTEX_ENTER(&softn->ipf_nat_new); 420 nat2 = ipf_nat_add(&fi, nat->nat_ptr, NULL, 421 NAT_SLAVE|IPN_TCP|SI_W_DPORT, NAT_OUTBOUND); 422 MUTEX_EXIT(&softn->ipf_nat_new); 423 if (nat2 != NULL) { 424 (void) ipf_nat_proto(&fi, nat2, 0); 425 MUTEX_ENTER(&nat2->nat_lock); 426 ipf_nat_update(&fi, nat2); 427 MUTEX_EXIT(&nat2->nat_lock); 428 429 (void) ipf_state_add(softc, &fi, NULL, SI_W_DPORT); 430 } 431 ip->ip_src = swip; 432 } 433 return inc; 434 } 435 436 437 int 438 ipf_p_irc_out(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat) 439 { 440 aps = aps; /* LINT */ 441 return ipf_p_irc_send(fin, nat); 442 } 443