ip_ftp_pxy.c revision 328203
11592Srgrimes/* $FreeBSD: stable/10/sys/contrib/ipfilter/netinet/ip_ftp_pxy.c 328203 2018-01-20 18:00:42Z cy $ */ 21592Srgrimes 31592Srgrimes/* 41592Srgrimes * Copyright (C) 2012 by Darren Reed. 51592Srgrimes * 61592Srgrimes * See the IPFILTER.LICENCE file for details on licencing. 71592Srgrimes * 81592Srgrimes * Simple FTP transparent proxy for in-kernel use. For use with the NAT 91592Srgrimes * code. 101592Srgrimes * 111592Srgrimes * $FreeBSD: stable/10/sys/contrib/ipfilter/netinet/ip_ftp_pxy.c 328203 2018-01-20 18:00:42Z cy $ 12262435Sbrueffer * Id: ip_ftp_pxy.c,v 2.88.2.19 2006/04/01 10:14:53 darrenr Exp $ 131592Srgrimes */ 141592Srgrimes 151592Srgrimes#define IPF_FTP_PROXY 161592Srgrimes 171592Srgrimes#define IPF_MINPORTLEN 18 181592Srgrimes#define IPF_MINEPRTLEN 20 191592Srgrimes#define IPF_MAXPORTLEN 30 201592Srgrimes#define IPF_MIN227LEN 39 211592Srgrimes#define IPF_MAX227LEN 51 221592Srgrimes#define IPF_MIN229LEN 47 231592Srgrimes#define IPF_MAX229LEN 51 241592Srgrimes 251592Srgrimes#define FTPXY_GO 0 261592Srgrimes#define FTPXY_INIT 1 271592Srgrimes#define FTPXY_USER_1 2 281592Srgrimes#define FTPXY_USOK_1 3 2950476Speter#define FTPXY_PASS_1 4 301592Srgrimes#define FTPXY_PAOK_1 5 31320646Sallanjude#define FTPXY_AUTH_1 6 321592Srgrimes#define FTPXY_AUOK_1 7 3379529Sru#define FTPXY_ADAT_1 8 341592Srgrimes#define FTPXY_ADOK_1 9 351592Srgrimes#define FTPXY_ACCT_1 10 361592Srgrimes#define FTPXY_ACOK_1 11 371592Srgrimes#define FTPXY_USER_2 12 3868949Sru#define FTPXY_USOK_2 13 3911486Sdg#define FTPXY_PASS_2 14 40320646Sallanjude#define FTPXY_PAOK_2 15 41320646Sallanjude 42320646Sallanjude#define FTPXY_JUNK_OK 0 43320646Sallanjude#define FTPXY_JUNK_BAD 1 /* Ignore all commands for this connection */ 44320646Sallanjude#define FTPXY_JUNK_EOL 2 /* consume the rest of this line only */ 45320646Sallanjude#define FTPXY_JUNK_CONT 3 /* Saerching for next numeric */ 46320646Sallanjude 47320646Sallanjude/* 48320646Sallanjude * Values for FTP commands. Numerics cover 0-999 491592Srgrimes */ 5099500Scharnier#define FTPXY_C_PASV 1000 5199500Scharnier#define FTPXY_C_PORT 1001 5299500Scharnier#define FTPXY_C_EPSV 1002 531592Srgrimes#define FTPXY_C_EPRT 1003 54131487Sru 55131487Sru 561592Srgrimestypedef struct ipf_ftp_softc_s { 571592Srgrimes int ipf_p_ftp_pasvonly; 581592Srgrimes /* Do not require logins before transfers */ 5968949Sru int ipf_p_ftp_insecure; 6031405Scharnier int ipf_p_ftp_pasvrdr; 6111486Sdg /* PASV must be last command prior to 227 */ 62131487Sru int ipf_p_ftp_forcepasv; 63131487Sru int ipf_p_ftp_debug; 6411486Sdg int ipf_p_ftp_single_xfer; 651592Srgrimes void *ipf_p_ftp_tune; 661592Srgrimes} ipf_ftp_softc_t; 671592Srgrimes 681592Srgrimes 691592Srgrimesvoid ipf_p_ftp_main_load __P((void)); 701592Srgrimesvoid ipf_p_ftp_main_unload __P((void)); 711592Srgrimesvoid *ipf_p_ftp_soft_create __P((ipf_main_softc_t *)); 721592Srgrimesvoid ipf_p_ftp_soft_destroy __P((ipf_main_softc_t *, void *)); 731592Srgrimes 741592Srgrimesint ipf_p_ftp_client __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *, 7599500Scharnier ftpinfo_t *, int)); 7699500Scharnierint ipf_p_ftp_complete __P((char *, size_t)); 7799500Scharnierint ipf_p_ftp_in __P((void *, fr_info_t *, ap_session_t *, nat_t *)); 7855562Sphantomint ipf_p_ftp_new __P((void *, fr_info_t *, ap_session_t *, nat_t *)); 7955562Sphantomvoid ipf_p_ftp_del __P((ipf_main_softc_t *, ap_session_t *)); 8055562Sphantomint ipf_p_ftp_out __P((void *, fr_info_t *, ap_session_t *, nat_t *)); 811592Srgrimesint ipf_p_ftp_pasv __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *, 821592Srgrimes ftpinfo_t *, int)); 831592Srgrimesint ipf_p_ftp_epsv __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *, 841592Srgrimes ftpinfo_t *, int)); 851592Srgrimesint ipf_p_ftp_port __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *, 861592Srgrimes ftpinfo_t *, int)); 871592Srgrimesint ipf_p_ftp_process __P((ipf_ftp_softc_t *, fr_info_t *, nat_t *, 881592Srgrimes ftpinfo_t *, int)); 891592Srgrimesint ipf_p_ftp_server __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *, 901592Srgrimes ftpinfo_t *, int)); 911592Srgrimesint ipf_p_ftp_valid __P((ipf_ftp_softc_t *, ftpinfo_t *, int, char *, size_t)); 921592Srgrimesint ipf_p_ftp_server_valid __P((ipf_ftp_softc_t *, ftpside_t *, char *, 931592Srgrimes size_t)); 941592Srgrimesint ipf_p_ftp_client_valid __P((ipf_ftp_softc_t *, ftpside_t *, char *, 951592Srgrimes size_t)); 961592Srgrimesu_short ipf_p_ftp_atoi __P((char **)); 971592Srgrimesint ipf_p_ftp_pasvreply __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *, 981592Srgrimes ftpinfo_t *, u_int, char *, char *)); 991592Srgrimesint ipf_p_ftp_eprt __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *, 1001592Srgrimes ftpinfo_t *, int)); 1011592Srgrimesint ipf_p_ftp_eprt4 __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *, 1021592Srgrimes ftpinfo_t *, int)); 1031592Srgrimesint ipf_p_ftp_eprt6 __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *, 1041592Srgrimes ftpinfo_t *, int)); 1051592Srgrimesint ipf_p_ftp_addport __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *, 1061592Srgrimes ftpinfo_t *, int, int, int)); 1071592Srgrimesvoid ipf_p_ftp_setpending __P((ipf_main_softc_t *, ftpinfo_t *)); 10879754Sdd 10931405Scharnier/* 1101592Srgrimes * Debug levels 1111592Srgrimes */ 11279754Sdd#define DEBUG_SECURITY 0x01 1131592Srgrimes#define DEBUG_ERROR 0x02 1141592Srgrimes#define DEBUG_INFO 0x04 11579754Sdd#define DEBUG_PARSE_ERR 0x08 1161592Srgrimes#define DEBUG_PARSE_INFO 0x10 1171592Srgrimes#define DEBUG_PARSE 0x20 1181592Srgrimes 1191592Srgrimesstatic int ipf_p_ftp_proxy_init = 0; 1201592Srgrimesstatic frentry_t ftppxyfr; 1211592Srgrimesstatic ipftuneable_t ipf_ftp_tuneables[] = { 1221592Srgrimes { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_debug) }, 1231592Srgrimes "ftp_debug", 0, 0x7f, 1241592Srgrimes stsizeof(ipf_ftp_softc_t, ipf_p_ftp_debug), 1251592Srgrimes 0, NULL, NULL }, 1261592Srgrimes { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_pasvonly) }, 1271592Srgrimes "ftp_pasvonly", 0, 1, 1281592Srgrimes stsizeof(ipf_ftp_softc_t, ipf_p_ftp_pasvonly), 1291592Srgrimes 0, NULL, NULL }, 1301592Srgrimes { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_insecure) }, 1311592Srgrimes "ftp_insecure", 0, 1, 13231405Scharnier stsizeof(ipf_ftp_softc_t, ipf_p_ftp_insecure), 133131487Sru 0, NULL, NULL }, 134131487Sru { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_pasvrdr) }, 1351592Srgrimes "ftp_pasvrdr", 0, 1, 1361592Srgrimes stsizeof(ipf_ftp_softc_t, ipf_p_ftp_pasvrdr), 1371592Srgrimes 0, NULL, NULL }, 1381592Srgrimes { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_forcepasv) }, 1391592Srgrimes "ftp_forcepasv", 0, 1, 140131487Sru stsizeof(ipf_ftp_softc_t, ipf_p_ftp_forcepasv), 141131487Sru 0, NULL, NULL }, 1421592Srgrimes { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_single_xfer) }, 1431592Srgrimes "ftp_single_xfer", 0, 1, 144108221Sru stsizeof(ipf_ftp_softc_t, ipf_p_ftp_single_xfer), 1451592Srgrimes 0, NULL, NULL }, 1461592Srgrimes { { NULL }, NULL, 0, 0, 0, 0, NULL, NULL } 1471592Srgrimes}; 1481592Srgrimes 1491592Srgrimes 1501592Srgrimesvoid 1511592Srgrimesipf_p_ftp_main_load() 1521592Srgrimes{ 1531592Srgrimes bzero((char *)&ftppxyfr, sizeof(ftppxyfr)); 1541592Srgrimes ftppxyfr.fr_ref = 1; 155140414Sru ftppxyfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE; 156140414Sru 157140414Sru MUTEX_INIT(&ftppxyfr.fr_lock, "FTP Proxy Mutex"); 158140414Sru ipf_p_ftp_proxy_init = 1; 159140414Sru} 160140414Sru 161140414Sru 1621592Srgrimesvoid 1631592Srgrimesipf_p_ftp_main_unload() 1641592Srgrimes{ 1651592Srgrimes 1661592Srgrimes if (ipf_p_ftp_proxy_init == 1) { 16731405Scharnier MUTEX_DESTROY(&ftppxyfr.fr_lock); 1681592Srgrimes ipf_p_ftp_proxy_init = 0; 1691592Srgrimes } 1701592Srgrimes} 1711592Srgrimes 17231405Scharnier 1731592Srgrimes/* 1741592Srgrimes * Initialize local structures. 1751592Srgrimes */ 1761592Srgrimesvoid * 1771592Srgrimesipf_p_ftp_soft_create(softc) 17879366Sru ipf_main_softc_t *softc; 17955562Sphantom{ 18055562Sphantom ipf_ftp_softc_t *softf; 18113680Swosch 18255562Sphantom KMALLOC(softf, ipf_ftp_softc_t *); 18368756Sben if (softf == NULL) 184140414Sru return NULL; 185140414Sru 186140414Sru bzero((char *)softf, sizeof(*softf)); 187140414Sru#if defined(_KERNEL) 188140414Sru softf->ipf_p_ftp_debug = 0; 189140414Sru#else 190140414Sru softf->ipf_p_ftp_debug = DEBUG_PARSE_ERR; 1911592Srgrimes#endif 1921592Srgrimes softf->ipf_p_ftp_forcepasv = 1; 193131487Sru 194131487Sru softf->ipf_p_ftp_tune = ipf_tune_array_copy(softf, 19555562Sphantom sizeof(ipf_ftp_tuneables), 19655562Sphantom ipf_ftp_tuneables); 19755562Sphantom if (softf->ipf_p_ftp_tune == NULL) { 1981592Srgrimes ipf_p_ftp_soft_destroy(softc, softf); 1991592Srgrimes return NULL; 2001592Srgrimes } 2011592Srgrimes if (ipf_tune_array_link(softc, softf->ipf_p_ftp_tune) == -1) { 2021592Srgrimes ipf_p_ftp_soft_destroy(softc, softf); 203 return NULL; 204 } 205 206 return softf; 207} 208 209 210void 211ipf_p_ftp_soft_destroy(softc, arg) 212 ipf_main_softc_t *softc; 213 void *arg; 214{ 215 ipf_ftp_softc_t *softf = arg; 216 217 if (softf->ipf_p_ftp_tune != NULL) { 218 ipf_tune_array_unlink(softc, softf->ipf_p_ftp_tune); 219 KFREES(softf->ipf_p_ftp_tune, sizeof(ipf_ftp_tuneables)); 220 softf->ipf_p_ftp_tune = NULL; 221 } 222 223 KFREE(softf); 224} 225 226 227int 228ipf_p_ftp_new(arg, fin, aps, nat) 229 void *arg; 230 fr_info_t *fin; 231 ap_session_t *aps; 232 nat_t *nat; 233{ 234 ftpinfo_t *ftp; 235 ftpside_t *f; 236 237 KMALLOC(ftp, ftpinfo_t *); 238 if (ftp == NULL) 239 return -1; 240 241 nat = nat; /* LINT */ 242 243 aps->aps_data = ftp; 244 aps->aps_psiz = sizeof(ftpinfo_t); 245 aps->aps_sport = htons(fin->fin_sport); 246 aps->aps_dport = htons(fin->fin_dport); 247 248 bzero((char *)ftp, sizeof(*ftp)); 249 f = &ftp->ftp_side[0]; 250 f->ftps_rptr = f->ftps_buf; 251 f->ftps_wptr = f->ftps_buf; 252 f = &ftp->ftp_side[1]; 253 f->ftps_rptr = f->ftps_buf; 254 f->ftps_wptr = f->ftps_buf; 255 ftp->ftp_passok = FTPXY_INIT; 256 ftp->ftp_incok = 0; 257 return 0; 258} 259 260 261void 262ipf_p_ftp_setpending(ipf_main_softc_t *softc, ftpinfo_t *ftp) 263{ 264 if (ftp->ftp_pendnat != NULL) 265 ipf_nat_setpending(softc, ftp->ftp_pendnat); 266 267 if (ftp->ftp_pendstate != NULL) { 268 READ_ENTER(&softc->ipf_state); 269 ipf_state_setpending(softc, ftp->ftp_pendstate); 270 RWLOCK_EXIT(&softc->ipf_state); 271 } 272} 273 274 275void 276ipf_p_ftp_del(softc, aps) 277 ipf_main_softc_t *softc; 278 ap_session_t *aps; 279{ 280 ftpinfo_t *ftp; 281 282 ftp = aps->aps_data; 283 if (ftp != NULL) 284 ipf_p_ftp_setpending(softc, ftp); 285} 286 287 288int 289ipf_p_ftp_port(softf, fin, ip, nat, ftp, dlen) 290 ipf_ftp_softc_t *softf; 291 fr_info_t *fin; 292 ip_t *ip; 293 nat_t *nat; 294 ftpinfo_t *ftp; 295 int dlen; 296{ 297 char newbuf[IPF_FTPBUFSZ], *s; 298 u_int a1, a2, a3, a4; 299 u_short a5, a6, sp; 300 size_t nlen, olen; 301 tcphdr_t *tcp; 302 int inc, off; 303 ftpside_t *f; 304 mb_t *m; 305 306 m = fin->fin_m; 307 f = &ftp->ftp_side[0]; 308 tcp = (tcphdr_t *)fin->fin_dp; 309 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff; 310 311 /* 312 * Check for client sending out PORT message. 313 */ 314 if (dlen < IPF_MINPORTLEN) { 315 DT3(ftp_PORT_error_dlen, nat_t *, nat, ftpside_t *, f, 316 u_int, dlen); 317 if (softf->ipf_p_ftp_debug & DEBUG_PARSE) 318 printf("ipf_p_ftp_port:dlen(%d) < IPF_MINPORTLEN\n", 319 dlen); 320 return 0; 321 } 322 /* 323 * Skip the PORT command + space 324 */ 325 s = f->ftps_rptr + 5; 326 /* 327 * Pick out the address components, two at a time. 328 */ 329 a1 = ipf_p_ftp_atoi(&s); 330 if (s == NULL) { 331 DT2(ftp_PORT_error_atoi_1, nat_t *, nat, ftpside_t *, f); 332 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 333 printf("ipf_p_ftp_port:ipf_p_ftp_atoi(%d) failed\n", 1); 334 return 0; 335 } 336 a2 = ipf_p_ftp_atoi(&s); 337 if (s == NULL) { 338 DT2(ftp_PORT_error_atoi_2, nat_t *, nat, ftpside_t *, f); 339 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 340 printf("ipf_p_ftp_port:ipf_p_ftp_atoi(%d) failed\n", 2); 341 return 0; 342 } 343 344 /* 345 * Check that IP address in the PORT/PASV reply is the same as the 346 * sender of the command - prevents using PORT for port scanning. 347 */ 348 a1 <<= 16; 349 a1 |= a2; 350 if (((nat->nat_dir == NAT_OUTBOUND) && 351 (a1 != ntohl(nat->nat_osrcaddr))) || 352 ((nat->nat_dir == NAT_INBOUND) && 353 (a1 != ntohl(nat->nat_nsrcaddr)))) { 354 DT3(ftp_PORT_error_address, nat_t *, nat, ftpside_t *, f, 355 u_int, a1); 356 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 357 printf("ipf_p_ftp_port:%s != nat->nat_inip\n", "a1"); 358 return APR_ERR(1); 359 } 360 361 a5 = ipf_p_ftp_atoi(&s); 362 if (s == NULL) { 363 DT2(ftp_PORT_error_atoi_3, nat_t *, nat, ftpside_t *, f); 364 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 365 printf("ipf_p_ftp_port:ipf_p_ftp_atoi(%d) failed\n", 3); 366 return 0; 367 } 368 if (*s == ')') 369 s++; 370 371 /* 372 * check for CR-LF at the end. 373 */ 374 if (*s == '\n') 375 s--; 376 if ((*s != '\r') || (*(s + 1) != '\n')) { 377 DT2(ftp_PORT_error_no_crlf, nat_t *, nat, ftpside_t *, f); 378 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 379 printf("ipf_p_ftp_port:missing %s\n", "cr-lf"); 380 return 0; 381 } 382 s += 2; 383 a6 = a5 & 0xff; 384 385 /* 386 * Calculate the source port. Verification of > 1024 is in 387 * ipf_p_ftp_addport. 388 */ 389 a5 >>= 8; 390 a5 &= 0xff; 391 sp = a5 << 8 | a6; 392 393 /* 394 * Calculate new address parts for PORT command 395 */ 396 if (nat->nat_dir == NAT_INBOUND) 397 a1 = ntohl(nat->nat_ndstaddr); 398 else 399 a1 = ntohl(ip->ip_src.s_addr); 400 a1 = ntohl(ip->ip_src.s_addr); 401 a2 = (a1 >> 16) & 0xff; 402 a3 = (a1 >> 8) & 0xff; 403 a4 = a1 & 0xff; 404 a1 >>= 24; 405 olen = s - f->ftps_rptr; 406 /* DO NOT change this to snprintf! */ 407#if defined(SNPRINTF) && defined(_KERNEL) 408 SNPRINTF(newbuf, sizeof(newbuf), "%s %u,%u,%u,%u,%u,%u\r\n", 409 "PORT", a1, a2, a3, a4, a5, a6); 410#else 411 (void) sprintf(newbuf, "%s %u,%u,%u,%u,%u,%u\r\n", 412 "PORT", a1, a2, a3, a4, a5, a6); 413#endif 414 415 nlen = strlen(newbuf); 416 inc = nlen - olen; 417 if ((inc + fin->fin_plen) > 65535) { 418 DT3(ftp_PORT_error_inc, nat_t *, nat, ftpside_t *, f, 419 int, inc); 420 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 421 printf("ipf_p_ftp_port:inc(%d) + ip->ip_len > 65535\n", 422 inc); 423 return 0; 424 } 425 426#if !defined(_KERNEL) 427 M_ADJ(m, inc); 428#else 429 /* 430 * m_adj takes care of pkthdr.len, if required and treats inc<0 to 431 * mean remove -len bytes from the end of the packet. 432 * The mbuf chain will be extended if necessary by m_copyback(). 433 */ 434 if (inc < 0) 435 M_ADJ(m, inc); 436#endif /* !defined(_KERNEL) */ 437 COPYBACK(m, off, nlen, newbuf); 438 fin->fin_flx |= FI_DOCKSUM; 439 440 if (inc != 0) { 441 fin->fin_plen += inc; 442 ip->ip_len = htons(fin->fin_plen); 443 fin->fin_dlen += inc; 444 } 445 446 f->ftps_cmd = FTPXY_C_PORT; 447 return ipf_p_ftp_addport(softf, fin, ip, nat, ftp, dlen, sp, inc); 448} 449 450 451int 452ipf_p_ftp_addport(softf, fin, ip, nat, ftp, dlen, nport, inc) 453 ipf_ftp_softc_t *softf; 454 fr_info_t *fin; 455 ip_t *ip; 456 nat_t *nat; 457 ftpinfo_t *ftp; 458 int dlen, nport, inc; 459{ 460 tcphdr_t tcph, *tcp2 = &tcph; 461 ipf_main_softc_t *softc; 462 ipf_nat_softc_t *softn; 463 int direction; 464 fr_info_t fi; 465 ipnat_t *ipn; 466 nat_t *nat2; 467 u_short sp; 468 int flags; 469 470 softc = fin->fin_main_soft; 471 softn = softc->ipf_nat_soft; 472 473 if ((ftp->ftp_pendnat != NULL) || (ftp->ftp_pendstate != NULL)) { 474 if (softf->ipf_p_ftp_single_xfer != 0) { 475 DT2(ftp_PORT_error_add_active, nat_t *, nat, 476 ftpinfo_t *, ftp); 477 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 478 printf("ipf_p_ftp_addport:xfer active %p/%p\n", 479 ftp->ftp_pendnat, ftp->ftp_pendstate); 480 return 0; 481 } 482 ipf_p_ftp_setpending(softc, ftp); 483 } 484 485 /* 486 * Add skeleton NAT entry for connection which will come back the 487 * other way. 488 */ 489 sp = nport; 490 /* 491 * Don't allow the PORT command to specify a port < 1024 due to 492 * security risks. 493 */ 494 if (sp < 1024) { 495 DT3(ftp_PORT_error_port, nat_t *, nat, ftpinfo_t *, ftp, 496 u_int, sp); 497 if (softf->ipf_p_ftp_debug & DEBUG_SECURITY) 498 printf("ipf_p_ftp_addport:sp(%d) < 1024\n", sp); 499 return 0; 500 } 501 /* 502 * The server may not make the connection back from port 20, but 503 * it is the most likely so use it here to check for a conflicting 504 * mapping. 505 */ 506 bcopy((char *)fin, (char *)&fi, sizeof(fi)); 507 fi.fin_flx |= FI_IGNORE; 508 fi.fin_data[0] = sp; 509 fi.fin_data[1] = fin->fin_data[1] - 1; 510 fi.fin_src6 = nat->nat_ndst6; 511 fi.fin_dst6 = nat->nat_nsrc6; 512 513 if (nat->nat_v[0] == 6) { 514#ifndef USE_INET6 515 return APR_INC(inc); 516#endif 517 } 518 519 /* 520 * Add skeleton NAT entry for connection which will come back the 521 * other way. 522 */ 523#ifdef USE_INET6 524 if (nat->nat_v[0] == 6) { 525 if (nat->nat_dir == NAT_OUTBOUND) { 526 nat2 = ipf_nat6_outlookup(&fi, IPN_TCP|NAT_SEARCH, 527 nat->nat_pr[1], 528 &nat->nat_osrc6.in6, 529 &nat->nat_odst6.in6); 530 } else { 531 nat2 = ipf_nat6_inlookup(&fi, IPN_TCP|NAT_SEARCH, 532 nat->nat_pr[0], 533 &nat->nat_odst6.in6, 534 &nat->nat_osrc6.in6); 535 } 536 } else 537#endif 538 { 539 if (nat->nat_dir == NAT_OUTBOUND) { 540 nat2 = ipf_nat_outlookup(&fi, IPN_TCP|NAT_SEARCH, 541 nat->nat_pr[1], 542 nat->nat_osrcip, 543 nat->nat_odstip); 544 } else { 545 nat2 = ipf_nat_inlookup(&fi, IPN_TCP|NAT_SEARCH, 546 nat->nat_pr[0], 547 nat->nat_odstip, 548 nat->nat_osrcip); 549 } 550 } 551 if (nat2 != NULL) 552 return APR_INC(inc); 553 554 ipn = ipf_proxy_rule_rev(nat); 555 if (ipn == NULL) 556 return APR_ERR(1); 557 ipn->in_use = 0; 558 559 fi.fin_fr = &ftppxyfr; 560 fi.fin_dp = (char *)tcp2; 561 fi.fin_dlen = sizeof(*tcp2); 562 fi.fin_plen = fi.fin_hlen + sizeof(*tcp2); 563 fi.fin_flx &= FI_LOWTTL|FI_FRAG|FI_TCPUDP|FI_OPTIONS|FI_IGNORE; 564 fi.fin_data[1] = sp; 565 fi.fin_data[0] = 0; 566 567 bzero((char *)tcp2, sizeof(*tcp2)); 568 tcp2->th_sport = 0; 569 tcp2->th_dport = htons(sp); 570 571 tcp2->th_win = htons(8192); 572 TCP_OFF_A(tcp2, 5); 573 tcp2->th_flags = TH_SYN; 574 575 if (nat->nat_dir == NAT_INBOUND) { 576 fi.fin_out = 1; 577 direction = NAT_OUTBOUND; 578 } else { 579 fi.fin_out = 0; 580 direction = NAT_INBOUND; 581 } 582 flags = SI_W_SPORT|NAT_SLAVE|IPN_TCP; 583 584 MUTEX_ENTER(&softn->ipf_nat_new); 585 if (nat->nat_v[0] == 6) { 586#ifdef USE_INET6 587 nat2 = ipf_nat6_add(&fi, ipn, &ftp->ftp_pendnat, flags, 588 direction); 589#endif 590 } else { 591 nat2 = ipf_nat_add(&fi, ipn, &ftp->ftp_pendnat, flags, 592 direction); 593 } 594 MUTEX_EXIT(&softn->ipf_nat_new); 595 596 if (nat2 == NULL) { 597 KFREES(ipn, ipn->in_size); 598 return APR_ERR(1); 599 } 600 601 (void) ipf_nat_proto(&fi, nat2, IPN_TCP); 602 MUTEX_ENTER(&nat2->nat_lock); 603 ipf_nat_update(&fi, nat2); 604 MUTEX_EXIT(&nat2->nat_lock); 605 fi.fin_ifp = NULL; 606 if (nat2->nat_dir == NAT_INBOUND) 607 fi.fin_dst6 = nat->nat_osrc6; 608 if (ipf_state_add(softc, &fi, (ipstate_t **)&ftp->ftp_pendstate, 609 SI_W_SPORT) != 0) 610 ipf_nat_setpending(softc, nat2); 611 612 return APR_INC(inc); 613} 614 615 616int 617ipf_p_ftp_client(softf, fin, ip, nat, ftp, dlen) 618 ipf_ftp_softc_t *softf; 619 fr_info_t *fin; 620 nat_t *nat; 621 ftpinfo_t *ftp; 622 ip_t *ip; 623 int dlen; 624{ 625 char *rptr, *wptr, cmd[6], c; 626 ftpside_t *f; 627 int inc, i; 628 629 inc = 0; 630 f = &ftp->ftp_side[0]; 631 rptr = f->ftps_rptr; 632 wptr = f->ftps_wptr; 633 634 for (i = 0; (i < 5) && (i < dlen); i++) { 635 c = rptr[i]; 636 if (ISALPHA(c)) { 637 cmd[i] = TOUPPER(c); 638 } else { 639 cmd[i] = c; 640 } 641 } 642 cmd[i] = '\0'; 643 644 ftp->ftp_incok = 0; 645 DT2(ftp_client_command, char [], cmd, int, ftp->ftp_passok); 646 if (!strncmp(cmd, "USER ", 5) || !strncmp(cmd, "XAUT ", 5)) { 647 if (ftp->ftp_passok == FTPXY_ADOK_1 || 648 ftp->ftp_passok == FTPXY_AUOK_1) { 649 ftp->ftp_passok = FTPXY_USER_2; 650 ftp->ftp_incok = 1; 651 } else { 652 ftp->ftp_passok = FTPXY_USER_1; 653 ftp->ftp_incok = 1; 654 } 655 } else if (!strncmp(cmd, "AUTH ", 5)) { 656 ftp->ftp_passok = FTPXY_AUTH_1; 657 ftp->ftp_incok = 1; 658 } else if (!strncmp(cmd, "PASS ", 5)) { 659 if (ftp->ftp_passok == FTPXY_USOK_1) { 660 ftp->ftp_passok = FTPXY_PASS_1; 661 ftp->ftp_incok = 1; 662 } else if (ftp->ftp_passok == FTPXY_USOK_2) { 663 ftp->ftp_passok = FTPXY_PASS_2; 664 ftp->ftp_incok = 1; 665 } 666 } else if ((ftp->ftp_passok == FTPXY_AUOK_1) && 667 !strncmp(cmd, "ADAT ", 5)) { 668 ftp->ftp_passok = FTPXY_ADAT_1; 669 ftp->ftp_incok = 1; 670 } else if ((ftp->ftp_passok == FTPXY_PAOK_1 || 671 ftp->ftp_passok == FTPXY_PAOK_2) && 672 !strncmp(cmd, "ACCT ", 5)) { 673 ftp->ftp_passok = FTPXY_ACCT_1; 674 ftp->ftp_incok = 1; 675 } else if ((ftp->ftp_passok == FTPXY_GO) && 676 !softf->ipf_p_ftp_pasvonly && 677 !strncmp(cmd, "PORT ", 5)) { 678 inc = ipf_p_ftp_port(softf, fin, ip, nat, ftp, dlen); 679 } else if ((ftp->ftp_passok == FTPXY_GO) && 680 !softf->ipf_p_ftp_pasvonly && 681 !strncmp(cmd, "EPRT ", 5)) { 682 inc = ipf_p_ftp_eprt(softf, fin, ip, nat, ftp, dlen); 683 } else if (softf->ipf_p_ftp_insecure && 684 !softf->ipf_p_ftp_pasvonly && 685 !strncmp(cmd, "PORT ", 5)) { 686 inc = ipf_p_ftp_port(softf, fin, ip, nat, ftp, dlen); 687 } 688 if (softf->ipf_p_ftp_debug & DEBUG_PARSE) 689 printf("ipf_p_ftp_client: cmd[%s] passok %d incok %d inc %d\n", 690 cmd, ftp->ftp_passok, ftp->ftp_incok, inc); 691 692 DT2(ftp_client_passok, char *, cmd, int, ftp->ftp_passok); 693 while ((*rptr++ != '\n') && (rptr < wptr)) 694 ; 695 f->ftps_rptr = rptr; 696 return inc; 697} 698 699 700int 701ipf_p_ftp_pasv(softf, fin, ip, nat, ftp, dlen) 702 ipf_ftp_softc_t *softf; 703 fr_info_t *fin; 704 ip_t *ip; 705 nat_t *nat; 706 ftpinfo_t *ftp; 707 int dlen; 708{ 709 u_int a1, a2, a3, a4, data_ip; 710 char newbuf[IPF_FTPBUFSZ]; 711 const char *brackets[2]; 712 u_short a5, a6; 713 ftpside_t *f; 714 char *s; 715 716 if ((softf->ipf_p_ftp_forcepasv != 0) && 717 (ftp->ftp_side[0].ftps_cmd != FTPXY_C_PASV)) { 718 DT2(ftp_PASV_error_state, nat_t *, nat, ftpinfo_t *, ftp); 719 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 720 printf("ipf_p_ftp_pasv:ftps_cmd(%d) != FTPXY_C_PASV\n", 721 ftp->ftp_side[0].ftps_cmd); 722 return 0; 723 } 724 725 f = &ftp->ftp_side[1]; 726 727#define PASV_REPLEN 24 728 /* 729 * Check for PASV reply message. 730 */ 731 if (dlen < IPF_MIN227LEN) { 732 DT3(ftp_PASV_error_short, nat_t *, nat, ftpinfo_t *, ftp, 733 int, dlen); 734 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 735 printf("ipf_p_ftp_pasv:dlen(%d) < IPF_MIN227LEN\n", 736 dlen); 737 return 0; 738 } else if (strncmp(f->ftps_rptr, 739 "227 Entering Passive Mod", PASV_REPLEN)) { 740 DT2(ftp_PASV_error_string, nat_t *, nat, ftpinfo_t *, ftp); 741 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 742 printf("ipf_p_ftp_pasv:%d reply wrong\n", 227); 743 return 0; 744 } 745 746 brackets[0] = ""; 747 brackets[1] = ""; 748 /* 749 * Skip the PASV reply + space 750 */ 751 s = f->ftps_rptr + PASV_REPLEN; 752 while (*s && !ISDIGIT(*s)) { 753 if (*s == '(') { 754 brackets[0] = "("; 755 brackets[1] = ")"; 756 } 757 s++; 758 } 759 760 /* 761 * Pick out the address components, two at a time. 762 */ 763 a1 = ipf_p_ftp_atoi(&s); 764 if (s == NULL) { 765 DT2(ftp_PASV_error_atoi_1, nat_t *, nat, ftpside_t *, f); 766 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 767 printf("ipf_p_ftp_pasv:ipf_p_ftp_atoi(%d) failed\n", 1); 768 return 0; 769 } 770 a2 = ipf_p_ftp_atoi(&s); 771 if (s == NULL) { 772 DT2(ftp_PASV_error_atoi_2, nat_t *, nat, ftpside_t *, f); 773 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 774 printf("ipf_p_ftp_pasv:ipf_p_ftp_atoi(%d) failed\n", 2); 775 return 0; 776 } 777 778 /* 779 * check that IP address in the PASV reply is the same as the 780 * sender of the command - prevents using PASV for port scanning. 781 */ 782 a1 <<= 16; 783 a1 |= a2; 784 785 if (((nat->nat_dir == NAT_INBOUND) && 786 (a1 != ntohl(nat->nat_ndstaddr))) || 787 ((nat->nat_dir == NAT_OUTBOUND) && 788 (a1 != ntohl(nat->nat_odstaddr)))) { 789 DT3(ftp_PASV_error_address, nat_t *, nat, ftpside_t *, f, 790 u_int, a1); 791 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 792 printf("ipf_p_ftp_pasv:%s != nat->nat_oip\n", "a1"); 793 return 0; 794 } 795 796 a5 = ipf_p_ftp_atoi(&s); 797 if (s == NULL) { 798 DT2(ftp_PASV_error_atoi_3, nat_t *, nat, ftpside_t *, f); 799 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 800 printf("ipf_p_ftp_pasv:ipf_p_ftp_atoi(%d) failed\n", 3); 801 return 0; 802 } 803 804 if (*s == ')') 805 s++; 806 if (*s == '.') 807 s++; 808 if (*s == '\n') 809 s--; 810 /* 811 * check for CR-LF at the end. 812 */ 813 if ((*s != '\r') || (*(s + 1) != '\n')) { 814 DT(pasv_missing_crlf); 815 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 816 printf("ipf_p_ftp_pasv:missing %s", "cr-lf\n"); 817 return 0; 818 } 819 s += 2; 820 821 a6 = a5 & 0xff; 822 a5 >>= 8; 823 /* 824 * Calculate new address parts for 227 reply 825 */ 826 if (nat->nat_dir == NAT_INBOUND) { 827 data_ip = nat->nat_odstaddr; 828 a1 = ntohl(data_ip); 829 } else 830 data_ip = htonl(a1); 831 832 a2 = (a1 >> 16) & 0xff; 833 a3 = (a1 >> 8) & 0xff; 834 a4 = a1 & 0xff; 835 a1 >>= 24; 836 837#if defined(SNPRINTF) && defined(_KERNEL) 838 SNPRINTF(newbuf, sizeof(newbuf), "%s %s%u,%u,%u,%u,%u,%u%s\r\n", 839 "227 Entering Passive Mode", brackets[0], a1, a2, a3, a4, 840 a5, a6, brackets[1]); 841#else 842 (void) sprintf(newbuf, "%s %s%u,%u,%u,%u,%u,%u%s\r\n", 843 "227 Entering Passive Mode", brackets[0], a1, a2, a3, a4, 844 a5, a6, brackets[1]); 845#endif 846 return ipf_p_ftp_pasvreply(softf, fin, ip, nat, ftp, (a5 << 8 | a6), 847 newbuf, s); 848} 849 850int 851ipf_p_ftp_pasvreply(softf, fin, ip, nat, ftp, port, newmsg, s) 852 ipf_ftp_softc_t *softf; 853 fr_info_t *fin; 854 ip_t *ip; 855 nat_t *nat; 856 ftpinfo_t *ftp; 857 u_int port; 858 char *newmsg; 859 char *s; 860{ 861 int inc, off, nflags; 862 tcphdr_t *tcp, tcph, *tcp2; 863 ipf_main_softc_t *softc; 864 ipf_nat_softc_t *softn; 865 size_t nlen, olen; 866#ifdef USE_INET6 867 ip6_t *ip6; 868#endif 869 ipnat_t *ipn; 870 fr_info_t fi; 871 ftpside_t *f; 872 nat_t *nat2; 873 mb_t *m; 874 875 softc = fin->fin_main_soft; 876 softn = softc->ipf_nat_soft; 877 878 if ((ftp->ftp_pendnat != NULL) || (ftp->ftp_pendstate != NULL)) 879 ipf_p_ftp_setpending(softc, ftp); 880 881 m = fin->fin_m; 882 tcp = (tcphdr_t *)fin->fin_dp; 883 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff; 884 885 tcp2 = &tcph; 886 inc = 0; 887 888 f = &ftp->ftp_side[1]; 889 olen = s - f->ftps_rptr; 890 nlen = strlen(newmsg); 891 inc = nlen - olen; 892 if ((inc + fin->fin_plen) > 65535) { 893 DT3(ftp_PASV_error_inc, nat_t *, nat, ftpside_t *, f, 894 int, inc); 895 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 896 printf("ipf_p_ftp_pasv:inc(%d) + ip->ip_len > 65535\n", 897 inc); 898 return 0; 899 } 900 901 ipn = ipf_proxy_rule_fwd(nat); 902 if (ipn == NULL) 903 return APR_ERR(1); 904 ipn->in_use = 0; 905 906 /* 907 * Add skeleton NAT entry for connection which will come back the 908 * other way. 909 */ 910 bzero((char *)tcp2, sizeof(*tcp2)); 911 bcopy((char *)fin, (char *)&fi, sizeof(fi)); 912 fi.fin_flx |= FI_IGNORE; 913 fi.fin_data[0] = 0; 914 fi.fin_data[1] = port; 915 nflags = IPN_TCP|SI_W_SPORT; 916 917 fi.fin_fr = &ftppxyfr; 918 fi.fin_dp = (char *)tcp2; 919 fi.fin_out = 1 - fin->fin_out; 920 fi.fin_dlen = sizeof(*tcp2); 921 fi.fin_src6 = nat->nat_osrc6; 922 fi.fin_dst6 = nat->nat_odst6; 923 fi.fin_plen = fi.fin_hlen + sizeof(*tcp); 924 fi.fin_flx &= FI_LOWTTL|FI_FRAG|FI_TCPUDP|FI_OPTIONS|FI_IGNORE; 925 926 TCP_OFF_A(tcp2, 5); 927 tcp2->th_flags = TH_SYN; 928 tcp2->th_win = htons(8192); 929 tcp2->th_dport = htons(port); 930 931 MUTEX_ENTER(&softn->ipf_nat_new); 932#ifdef USE_INET6 933 if (nat->nat_v[0] == 6) 934 nat2 = ipf_nat6_add(&fi, ipn, &ftp->ftp_pendnat, 935 nflags, nat->nat_dir); 936 else 937#endif 938 nat2 = ipf_nat_add(&fi, ipn, &ftp->ftp_pendnat, 939 nflags, nat->nat_dir); 940 MUTEX_EXIT(&softn->ipf_nat_new); 941 942 if (nat2 == NULL) { 943 KFREES(ipn, ipn->in_size); 944 return APR_ERR(1); 945 } 946 947 (void) ipf_nat_proto(&fi, nat2, IPN_TCP); 948 MUTEX_ENTER(&nat2->nat_lock); 949 ipf_nat_update(&fi, nat2); 950 MUTEX_EXIT(&nat2->nat_lock); 951 fi.fin_ifp = NULL; 952 if (nat->nat_dir == NAT_INBOUND) { 953 if (nat->nat_v[0] == 6) { 954#ifdef USE_INET6 955 fi.fin_dst6 = nat->nat_ndst6; 956#endif 957 } else { 958 fi.fin_daddr = nat->nat_ndstaddr; 959 } 960 } 961 if (ipf_state_add(softc, &fi, (ipstate_t **)&ftp->ftp_pendstate, 962 SI_W_SPORT) != 0) 963 ipf_nat_setpending(softc, nat2); 964 965#if !defined(_KERNEL) 966 M_ADJ(m, inc); 967#else 968 /* 969 * m_adj takes care of pkthdr.len, if required and treats inc<0 to 970 * mean remove -len bytes from the end of the packet. 971 * The mbuf chain will be extended if necessary by m_copyback(). 972 */ 973 if (inc < 0) 974 M_ADJ(m, inc); 975#endif /* !defined(_KERNEL) */ 976 COPYBACK(m, off, nlen, newmsg); 977 fin->fin_flx |= FI_DOCKSUM; 978 979 if (inc != 0) { 980 fin->fin_plen += inc; 981 fin->fin_dlen += inc; 982 if (nat->nat_v[0] == 6) { 983#ifdef USE_INET6 984 ip6 = (ip6_t *)fin->fin_ip; 985 u_short len = ntohs(ip6->ip6_plen) + inc; 986 ip6->ip6_plen = htons(len); 987#endif 988 } else { 989 ip->ip_len = htons(fin->fin_plen); 990 } 991 } 992 993 return APR_INC(inc); 994} 995 996 997int 998ipf_p_ftp_server(softf, fin, ip, nat, ftp, dlen) 999 ipf_ftp_softc_t *softf; 1000 fr_info_t *fin; 1001 ip_t *ip; 1002 nat_t *nat; 1003 ftpinfo_t *ftp; 1004 int dlen; 1005{ 1006 char *rptr, *wptr; 1007 ftpside_t *f; 1008 int inc; 1009 1010 inc = 0; 1011 f = &ftp->ftp_side[1]; 1012 rptr = f->ftps_rptr; 1013 wptr = f->ftps_wptr; 1014 1015 DT2(ftp_server_response, char *, rptr, int, ftp->ftp_passok); 1016 if (*rptr == ' ') 1017 goto server_cmd_ok; 1018 if (!ISDIGIT(*rptr) || !ISDIGIT(*(rptr + 1)) || !ISDIGIT(*(rptr + 2))) 1019 return 0; 1020 if (softf->ipf_p_ftp_debug & DEBUG_PARSE) 1021 printf("ipf_p_ftp_server_1: cmd[%4.4s] passok %d\n", 1022 rptr, ftp->ftp_passok); 1023 if (ftp->ftp_passok == FTPXY_GO) { 1024 if (!strncmp(rptr, "227 ", 4)) 1025 inc = ipf_p_ftp_pasv(softf, fin, ip, nat, ftp, dlen); 1026 else if (!strncmp(rptr, "229 ", 4)) 1027 inc = ipf_p_ftp_epsv(softf, fin, ip, nat, ftp, dlen); 1028 else if (strncmp(rptr, "200", 3)) { 1029 /* 1030 * 200 is returned for a successful command. 1031 */ 1032 ; 1033 } 1034 } else if (softf->ipf_p_ftp_insecure && !strncmp(rptr, "227 ", 4)) { 1035 inc = ipf_p_ftp_pasv(softf, fin, ip, nat, ftp, dlen); 1036 } else if (softf->ipf_p_ftp_insecure && !strncmp(rptr, "229 ", 4)) { 1037 inc = ipf_p_ftp_epsv(softf, fin, ip, nat, ftp, dlen); 1038 } else if (*rptr == '5' || *rptr == '4') 1039 ftp->ftp_passok = FTPXY_INIT; 1040 else if (ftp->ftp_incok) { 1041 if (*rptr == '3') { 1042 if (ftp->ftp_passok == FTPXY_ACCT_1) 1043 ftp->ftp_passok = FTPXY_GO; 1044 else 1045 ftp->ftp_passok++; 1046 } else if (*rptr == '2') { 1047 switch (ftp->ftp_passok) 1048 { 1049 case FTPXY_USER_1 : 1050 case FTPXY_USER_2 : 1051 case FTPXY_PASS_1 : 1052 case FTPXY_PASS_2 : 1053 case FTPXY_ACCT_1 : 1054 ftp->ftp_passok = FTPXY_GO; 1055 break; 1056 default : 1057 ftp->ftp_passok += 3; 1058 break; 1059 } 1060 } 1061 } 1062 ftp->ftp_incok = 0; 1063server_cmd_ok: 1064 if (softf->ipf_p_ftp_debug & DEBUG_PARSE) 1065 printf("ipf_p_ftp_server_2: cmd[%4.4s] passok %d\n", 1066 rptr, ftp->ftp_passok); 1067 DT3(ftp_server_passok, char *,rptr, int, ftp->ftp_incok, 1068 int, ftp->ftp_passok); 1069 1070 while ((*rptr++ != '\n') && (rptr < wptr)) 1071 ; 1072 f->ftps_rptr = rptr; 1073 return inc; 1074} 1075 1076 1077/* 1078 * 0 FTPXY_JUNK_OK 1079 * 1 FTPXY_JUNK_BAD 1080 * 2 FTPXY_JUNK_EOL 1081 * 3 FTPXY_JUNK_CONT 1082 * 1083 * Look to see if the buffer starts with something which we recognise as 1084 * being the correct syntax for the FTP protocol. 1085 */ 1086int 1087ipf_p_ftp_client_valid(softf, ftps, buf, len) 1088 ipf_ftp_softc_t *softf; 1089 ftpside_t *ftps; 1090 char *buf; 1091 size_t len; 1092{ 1093 register char *s, c, pc; 1094 register size_t i = len; 1095 char cmd[5]; 1096 1097 s = buf; 1098 1099 if (ftps->ftps_junk == FTPXY_JUNK_BAD) 1100 return FTPXY_JUNK_BAD; 1101 1102 if (i < 5) { 1103 DT1(client_valid, int, i); 1104 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 1105 printf("ipf_p_ftp_client_valid:i(%d) < 5\n", (int)i); 1106 return 2; 1107 } 1108 1109 i--; 1110 c = *s++; 1111 1112 if (ISALPHA(c)) { 1113 cmd[0] = TOUPPER(c); 1114 c = *s++; 1115 i--; 1116 if (ISALPHA(c)) { 1117 cmd[1] = TOUPPER(c); 1118 c = *s++; 1119 i--; 1120 if (ISALPHA(c)) { 1121 cmd[2] = TOUPPER(c); 1122 c = *s++; 1123 i--; 1124 if (ISALPHA(c)) { 1125 cmd[3] = TOUPPER(c); 1126 c = *s++; 1127 i--; 1128 if ((c != ' ') && (c != '\r')) 1129 goto bad_client_command; 1130 } else if ((c != ' ') && (c != '\r')) 1131 goto bad_client_command; 1132 } else 1133 goto bad_client_command; 1134 } else 1135 goto bad_client_command; 1136 } else { 1137bad_client_command: 1138 DT4(client_junk, int, len, int, i, int, c, char *, buf); 1139 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 1140 printf("%s:bad:junk %d len %d/%d c 0x%x buf [%*.*s]\n", 1141 "ipf_p_ftp_client_valid", 1142 ftps->ftps_junk, (int)len, (int)i, c, 1143 (int)len, (int)len, buf); 1144 return FTPXY_JUNK_BAD; 1145 } 1146 1147 for (; i; i--) { 1148 pc = c; 1149 c = *s++; 1150 if ((pc == '\r') && (c == '\n')) { 1151 cmd[4] = '\0'; 1152 if (!strcmp(cmd, "PASV")) { 1153 ftps->ftps_cmd = FTPXY_C_PASV; 1154 } else if (!strcmp(cmd, "EPSV")) { 1155 ftps->ftps_cmd = FTPXY_C_EPSV; 1156 } else { 1157 ftps->ftps_cmd = 0; 1158 } 1159 return 0; 1160 } 1161 } 1162#if !defined(_KERNEL) 1163 printf("ipf_p_ftp_client_valid:junk after cmd[%*.*s]\n", 1164 (int)len, (int)len, buf); 1165#endif 1166 return FTPXY_JUNK_EOL; 1167} 1168 1169 1170int 1171ipf_p_ftp_server_valid(softf, ftps, buf, len) 1172 ipf_ftp_softc_t *softf; 1173 ftpside_t *ftps; 1174 char *buf; 1175 size_t len; 1176{ 1177 register char *s, c, pc; 1178 register size_t i = len; 1179 int cmd; 1180 1181 s = buf; 1182 cmd = 0; 1183 1184 if (ftps->ftps_junk == FTPXY_JUNK_BAD) 1185 return FTPXY_JUNK_BAD; 1186 1187 if (i < 5) { 1188 DT1(server_valid, int, i); 1189 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 1190 printf("ipf_p_ftp_servert_valid:i(%d) < 5\n", (int)i); 1191 return 2; 1192 } 1193 1194 c = *s++; 1195 i--; 1196 if (c == ' ') { 1197 cmd = -1; 1198 goto search_eol; 1199 } 1200 1201 if (ISDIGIT(c)) { 1202 cmd = (c - '0') * 100; 1203 c = *s++; 1204 i--; 1205 if (ISDIGIT(c)) { 1206 cmd += (c - '0') * 10; 1207 c = *s++; 1208 i--; 1209 if (ISDIGIT(c)) { 1210 cmd += (c - '0'); 1211 c = *s++; 1212 i--; 1213 if ((c != '-') && (c != ' ')) 1214 goto bad_server_command; 1215 if (c == '-') 1216 return FTPXY_JUNK_CONT; 1217 } else 1218 goto bad_server_command; 1219 } else 1220 goto bad_server_command; 1221 } else { 1222bad_server_command: 1223 DT4(server_junk, int len, buf, int, i, int, c, char *, buf); 1224 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_INFO) 1225 printf("%s:bad:junk %d len %d/%d c 0x%x buf [%*.*s]\n", 1226 "ipf_p_ftp_server_valid", 1227 ftps->ftps_junk, (int)len, (int)i, 1228 c, (int)len, (int)len, buf); 1229 if (ftps->ftps_junk == FTPXY_JUNK_CONT) 1230 return FTPXY_JUNK_CONT; 1231 return FTPXY_JUNK_BAD; 1232 } 1233search_eol: 1234 for (; i; i--) { 1235 pc = c; 1236 c = *s++; 1237 if ((pc == '\r') && (c == '\n')) { 1238 if (cmd == -1) { 1239 if (ftps->ftps_junk == FTPXY_JUNK_CONT) 1240 return FTPXY_JUNK_CONT; 1241 } else { 1242 ftps->ftps_cmd = cmd; 1243 } 1244 return FTPXY_JUNK_OK; 1245 } 1246 } 1247 1248 DT2(junk_eol, int, len, char *, buf); 1249 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_INFO) 1250 printf("ipf_p_ftp_server_valid:junk after cmd[%*.*s]\n", 1251 (int)len, (int)len, buf); 1252 return FTPXY_JUNK_EOL; 1253} 1254 1255 1256int 1257ipf_p_ftp_valid(softf, ftp, side, buf, len) 1258 ipf_ftp_softc_t *softf; 1259 ftpinfo_t *ftp; 1260 int side; 1261 char *buf; 1262 size_t len; 1263{ 1264 ftpside_t *ftps; 1265 int ret; 1266 1267 ftps = &ftp->ftp_side[side]; 1268 1269 if (side == 0) 1270 ret = ipf_p_ftp_client_valid(softf, ftps, buf, len); 1271 else 1272 ret = ipf_p_ftp_server_valid(softf, ftps, buf, len); 1273 return ret; 1274} 1275 1276 1277/* 1278 * For map rules, the following applies: 1279 * rv == 0 for outbound processing, 1280 * rv == 1 for inbound processing. 1281 * For rdr rules, the following applies: 1282 * rv == 0 for inbound processing, 1283 * rv == 1 for outbound processing. 1284 */ 1285int 1286ipf_p_ftp_process(softf, fin, nat, ftp, rv) 1287 ipf_ftp_softc_t *softf; 1288 fr_info_t *fin; 1289 nat_t *nat; 1290 ftpinfo_t *ftp; 1291 int rv; 1292{ 1293 int mlen, len, off, inc, i, sel, sel2, ok, ackoff, seqoff, retry; 1294 char *rptr, *wptr, *s; 1295 u_32_t thseq, thack; 1296 ap_session_t *aps; 1297 ftpside_t *f, *t; 1298 tcphdr_t *tcp; 1299 ip_t *ip; 1300 mb_t *m; 1301 1302 m = fin->fin_m; 1303 ip = fin->fin_ip; 1304 tcp = (tcphdr_t *)fin->fin_dp; 1305 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff; 1306 1307 f = &ftp->ftp_side[rv]; 1308 t = &ftp->ftp_side[1 - rv]; 1309 thseq = ntohl(tcp->th_seq); 1310 thack = ntohl(tcp->th_ack); 1311#ifdef __sgi 1312 mlen = fin->fin_plen - off; 1313#else 1314 mlen = MSGDSIZE(m) - off; 1315#endif 1316 1317 DT3(process_debug, tcphdr_t *, tcp, int, off, int, mlen); 1318 if (softf->ipf_p_ftp_debug & DEBUG_INFO) 1319 printf("ipf_p_ftp_process: %d:%d,%d, mlen %d flags %x\n", 1320 fin->fin_out, fin->fin_sport, fin->fin_dport, 1321 mlen, tcp->th_flags); 1322 1323 if ((mlen == 0) && ((tcp->th_flags & TH_OPENING) == TH_OPENING)) { 1324 f->ftps_seq[0] = thseq + 1; 1325 t->ftps_seq[0] = thack; 1326 return 0; 1327 } else if (mlen < 0) { 1328 return 0; 1329 } 1330 1331 aps = nat->nat_aps; 1332 1333 sel = aps->aps_sel[1 - rv]; 1334 sel2 = aps->aps_sel[rv]; 1335 if (rv == 1) { 1336 seqoff = aps->aps_seqoff[sel]; 1337 if (aps->aps_seqmin[sel] > seqoff + thseq) 1338 seqoff = aps->aps_seqoff[!sel]; 1339 ackoff = aps->aps_ackoff[sel2]; 1340 if (aps->aps_ackmin[sel2] > ackoff + thack) 1341 ackoff = aps->aps_ackoff[!sel2]; 1342 } else { 1343 seqoff = aps->aps_ackoff[sel]; 1344 if (softf->ipf_p_ftp_debug & DEBUG_INFO) 1345 printf("seqoff %d thseq %x ackmin %x\n", seqoff, thseq, 1346 aps->aps_ackmin[sel]); 1347 if (aps->aps_ackmin[sel] > seqoff + thseq) 1348 seqoff = aps->aps_ackoff[!sel]; 1349 1350 ackoff = aps->aps_seqoff[sel2]; 1351 if (softf->ipf_p_ftp_debug & DEBUG_INFO) 1352 printf("ackoff %d thack %x seqmin %x\n", ackoff, thack, 1353 aps->aps_seqmin[sel2]); 1354 if (ackoff > 0) { 1355 if (aps->aps_seqmin[sel2] > ackoff + thack) 1356 ackoff = aps->aps_seqoff[!sel2]; 1357 } else { 1358 if (aps->aps_seqmin[sel2] > thack) 1359 ackoff = aps->aps_seqoff[!sel2]; 1360 } 1361 } 1362 if (softf->ipf_p_ftp_debug & DEBUG_INFO) { 1363 printf("%s: %x seq %x/%d ack %x/%d len %d/%d off %d\n", 1364 rv ? "IN" : "OUT", tcp->th_flags, thseq, seqoff, 1365 thack, ackoff, mlen, fin->fin_plen, off); 1366 printf("sel %d seqmin %x/%x offset %d/%d\n", sel, 1367 aps->aps_seqmin[sel], aps->aps_seqmin[sel2], 1368 aps->aps_seqoff[sel], aps->aps_seqoff[sel2]); 1369 printf("sel %d ackmin %x/%x offset %d/%d\n", sel2, 1370 aps->aps_ackmin[sel], aps->aps_ackmin[sel2], 1371 aps->aps_ackoff[sel], aps->aps_ackoff[sel2]); 1372 } 1373 1374 /* 1375 * XXX - Ideally, this packet should get dropped because we now know 1376 * that it is out of order (and there is no real danger in doing so 1377 * apart from causing packets to go through here ordered). 1378 */ 1379 if (softf->ipf_p_ftp_debug & DEBUG_INFO) { 1380 printf("rv %d t:seq[0] %x seq[1] %x %d/%d\n", 1381 rv, t->ftps_seq[0], t->ftps_seq[1], seqoff, ackoff); 1382 } 1383 1384 ok = 0; 1385 if (t->ftps_seq[0] == 0) { 1386 t->ftps_seq[0] = thack; 1387 ok = 1; 1388 } else { 1389 if (ackoff == 0) { 1390 if (t->ftps_seq[0] == thack) 1391 ok = 1; 1392 else if (t->ftps_seq[1] == thack) { 1393 t->ftps_seq[0] = thack; 1394 ok = 1; 1395 } 1396 } else { 1397 if (t->ftps_seq[0] + ackoff == thack) { 1398 t->ftps_seq[0] = thack; 1399 ok = 1; 1400 } else if (t->ftps_seq[0] == thack + ackoff) { 1401 t->ftps_seq[0] = thack + ackoff; 1402 ok = 1; 1403 } else if (t->ftps_seq[1] + ackoff == thack) { 1404 t->ftps_seq[0] = thack; 1405 ok = 1; 1406 } else if (t->ftps_seq[1] == thack + ackoff) { 1407 t->ftps_seq[0] = thack + ackoff; 1408 ok = 1; 1409 } 1410 } 1411 } 1412 1413 if (softf->ipf_p_ftp_debug & DEBUG_INFO) { 1414 if (!ok) 1415 printf("%s ok\n", "not"); 1416 } 1417 1418 if (!mlen) { 1419 if (t->ftps_seq[0] + ackoff != thack && 1420 t->ftps_seq[1] + ackoff != thack) { 1421 DT3(thack, ftpside_t *t, t, int, ackoff, u_32_t, thack); 1422 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) { 1423 printf("%s:seq[0](%u) + (%d) != (%u)\n", 1424 "ipf_p_ftp_process", t->ftps_seq[0], 1425 ackoff, thack); 1426 printf("%s:seq[0](%u) + (%d) != (%u)\n", 1427 "ipf_p_ftp_process", t->ftps_seq[1], 1428 ackoff, thack); 1429 } 1430 return APR_ERR(1); 1431 } 1432 1433 if (softf->ipf_p_ftp_debug & DEBUG_PARSE) { 1434 printf("ipf_p_ftp_process:f:seq[0] %x seq[1] %x\n", 1435 f->ftps_seq[0], f->ftps_seq[1]); 1436 } 1437 1438 if (tcp->th_flags & TH_FIN) { 1439 if (thseq == f->ftps_seq[1]) { 1440 f->ftps_seq[0] = f->ftps_seq[1] - seqoff; 1441 f->ftps_seq[1] = thseq + 1 - seqoff; 1442 } else { 1443 DT2(thseq, ftpside_t *t, t, u_32_t, thseq); 1444 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) { 1445 printf("FIN: thseq %x seqoff %d ftps_seq %x\n", 1446 thseq, seqoff, f->ftps_seq[0]); 1447 } 1448 return APR_ERR(1); 1449 } 1450 } 1451 f->ftps_len = 0; 1452 return 0; 1453 } 1454 1455 ok = 0; 1456 if ((thseq == f->ftps_seq[0]) || (thseq == f->ftps_seq[1])) { 1457 ok = 1; 1458 /* 1459 * Retransmitted data packet. 1460 */ 1461 } else if ((thseq + mlen == f->ftps_seq[0]) || 1462 (thseq + mlen == f->ftps_seq[1])) { 1463 ok = 1; 1464 } 1465 1466 if (ok == 0) { 1467 DT3(ok_0, ftpside_t *, f, u_32_t, thseq, int, mlen); 1468 inc = thseq - f->ftps_seq[0]; 1469 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) { 1470 printf("inc %d sel %d rv %d\n", inc, sel, rv); 1471 printf("th_seq %x ftps_seq %x/%x\n", 1472 thseq, f->ftps_seq[0], f->ftps_seq[1]); 1473 printf("ackmin %x ackoff %d\n", aps->aps_ackmin[sel], 1474 aps->aps_ackoff[sel]); 1475 printf("seqmin %x seqoff %d\n", aps->aps_seqmin[sel], 1476 aps->aps_seqoff[sel]); 1477 } 1478 1479 return APR_ERR(1); 1480 } 1481 1482 inc = 0; 1483 rptr = f->ftps_rptr; 1484 wptr = f->ftps_wptr; 1485 f->ftps_seq[0] = thseq; 1486 f->ftps_seq[1] = f->ftps_seq[0] + mlen; 1487 f->ftps_len = mlen; 1488 1489 while (mlen > 0) { 1490 len = MIN(mlen, sizeof(f->ftps_buf) - (wptr - rptr)); 1491 if (len == 0) 1492 break; 1493 COPYDATA(m, off, len, wptr); 1494 mlen -= len; 1495 off += len; 1496 wptr += len; 1497 1498whilemore: 1499 if (softf->ipf_p_ftp_debug & DEBUG_PARSE) 1500 printf("%s:len %d/%d off %d wptr %lx junk %d [%*.*s]\n", 1501 "ipf_p_ftp_process", 1502 len, mlen, off, (u_long)wptr, f->ftps_junk, 1503 len, len, rptr); 1504 1505 f->ftps_wptr = wptr; 1506 if (f->ftps_junk != FTPXY_JUNK_OK) { 1507 i = f->ftps_junk; 1508 f->ftps_junk = ipf_p_ftp_valid(softf, ftp, rv, rptr, 1509 wptr - rptr); 1510 DT2(junk_transit, int, i, int, f->ftps_junk); 1511 1512 if (softf->ipf_p_ftp_debug & DEBUG_PARSE) 1513 printf("%s:junk %d -> %d\n", 1514 "ipf_p_ftp_process", i, f->ftps_junk); 1515 1516 if (f->ftps_junk == FTPXY_JUNK_BAD) { 1517 DT(buffer_full); 1518 if (wptr - rptr == sizeof(f->ftps_buf)) { 1519 if (softf->ipf_p_ftp_debug & 1520 DEBUG_PARSE_INFO) 1521 printf("%s:full buffer\n", 1522 "ipf_p_ftp_process"); 1523 f->ftps_rptr = f->ftps_buf; 1524 f->ftps_wptr = f->ftps_buf; 1525 rptr = f->ftps_rptr; 1526 wptr = f->ftps_wptr; 1527 continue; 1528 } 1529 } 1530 } 1531 1532 while ((f->ftps_junk == FTPXY_JUNK_OK) && (wptr > rptr)) { 1533 len = wptr - rptr; 1534 f->ftps_junk = ipf_p_ftp_valid(softf, ftp, rv, 1535 rptr, len); 1536 1537 if (softf->ipf_p_ftp_debug & DEBUG_PARSE) { 1538 printf("%s=%d len %d rv %d ptr %lx/%lx ", 1539 "ipf_p_ftp_valid", 1540 f->ftps_junk, len, rv, (u_long)rptr, 1541 (u_long)wptr); 1542 printf("buf [%*.*s]\n", len, len, rptr); 1543 } 1544 1545 if (f->ftps_junk == FTPXY_JUNK_OK) { 1546 f->ftps_cmds++; 1547 f->ftps_rptr = rptr; 1548 if (rv) 1549 inc += ipf_p_ftp_server(softf, fin, ip, 1550 nat, ftp, len); 1551 else 1552 inc += ipf_p_ftp_client(softf, fin, ip, 1553 nat, ftp, len); 1554 rptr = f->ftps_rptr; 1555 wptr = f->ftps_wptr; 1556 } 1557 } 1558 1559 /* 1560 * Off to a bad start so lets just forget about using the 1561 * ftp proxy for this connection. 1562 */ 1563 if ((f->ftps_cmds == 0) && (f->ftps_junk == FTPXY_JUNK_BAD)) { 1564 /* f->ftps_seq[1] += inc; */ 1565 1566 DT(ftp_junk_cmd); 1567 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 1568 printf("%s:cmds == 0 junk == 1\n", 1569 "ipf_p_ftp_process"); 1570 return APR_ERR(2); 1571 } 1572 1573 retry = 0; 1574 if ((f->ftps_junk != FTPXY_JUNK_OK) && (rptr < wptr)) { 1575 for (s = rptr; s < wptr; s++) { 1576 if ((*s == '\r') && (s + 1 < wptr) && 1577 (*(s + 1) == '\n')) { 1578 rptr = s + 2; 1579 retry = 1; 1580 if (f->ftps_junk != FTPXY_JUNK_CONT) 1581 f->ftps_junk = FTPXY_JUNK_OK; 1582 break; 1583 } 1584 } 1585 } 1586 1587 if (rptr == wptr) { 1588 rptr = wptr = f->ftps_buf; 1589 } else { 1590 /* 1591 * Compact the buffer back to the start. The junk 1592 * flag should already be set and because we're not 1593 * throwing away any data, it is preserved from its 1594 * current state. 1595 */ 1596 if (rptr > f->ftps_buf) { 1597 bcopy(rptr, f->ftps_buf, wptr - rptr); 1598 wptr -= rptr - f->ftps_buf; 1599 rptr = f->ftps_buf; 1600 } 1601 } 1602 f->ftps_rptr = rptr; 1603 f->ftps_wptr = wptr; 1604 if (retry) 1605 goto whilemore; 1606 } 1607 1608 /* f->ftps_seq[1] += inc; */ 1609 if (tcp->th_flags & TH_FIN) 1610 f->ftps_seq[1]++; 1611 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_INFO) { 1612#ifdef __sgi 1613 mlen = fin->fin_plen; 1614#else 1615 mlen = MSGDSIZE(m); 1616#endif 1617 mlen -= off; 1618 printf("ftps_seq[1] = %x inc %d len %d\n", 1619 f->ftps_seq[1], inc, mlen); 1620 } 1621 1622 f->ftps_rptr = rptr; 1623 f->ftps_wptr = wptr; 1624 return APR_INC(inc); 1625} 1626 1627 1628int 1629ipf_p_ftp_out(arg, fin, aps, nat) 1630 void *arg; 1631 fr_info_t *fin; 1632 ap_session_t *aps; 1633 nat_t *nat; 1634{ 1635 ipf_ftp_softc_t *softf = arg; 1636 ftpinfo_t *ftp; 1637 int rev; 1638 1639 ftp = aps->aps_data; 1640 if (ftp == NULL) 1641 return 0; 1642 1643 rev = (nat->nat_dir == NAT_OUTBOUND) ? 0 : 1; 1644 if (ftp->ftp_side[1 - rev].ftps_ifp == NULL) 1645 ftp->ftp_side[1 - rev].ftps_ifp = fin->fin_ifp; 1646 1647 return ipf_p_ftp_process(softf, fin, nat, ftp, rev); 1648} 1649 1650 1651int 1652ipf_p_ftp_in(arg, fin, aps, nat) 1653 void *arg; 1654 fr_info_t *fin; 1655 ap_session_t *aps; 1656 nat_t *nat; 1657{ 1658 ipf_ftp_softc_t *softf = arg; 1659 ftpinfo_t *ftp; 1660 int rev; 1661 1662 ftp = aps->aps_data; 1663 if (ftp == NULL) 1664 return 0; 1665 1666 rev = (nat->nat_dir == NAT_OUTBOUND) ? 0 : 1; 1667 if (ftp->ftp_side[rev].ftps_ifp == NULL) 1668 ftp->ftp_side[rev].ftps_ifp = fin->fin_ifp; 1669 1670 return ipf_p_ftp_process(softf, fin, nat, ftp, 1 - rev); 1671} 1672 1673 1674/* 1675 * ipf_p_ftp_atoi - implement a version of atoi which processes numbers in 1676 * pairs separated by commas (which are expected to be in the range 0 - 255), 1677 * returning a 16 bit number combining either side of the , as the MSB and 1678 * LSB. 1679 */ 1680u_short 1681ipf_p_ftp_atoi(ptr) 1682 char **ptr; 1683{ 1684 register char *s = *ptr, c; 1685 register u_char i = 0, j = 0; 1686 1687 while (((c = *s++) != '\0') && ISDIGIT(c)) { 1688 i *= 10; 1689 i += c - '0'; 1690 } 1691 if (c != ',') { 1692 *ptr = NULL; 1693 return 0; 1694 } 1695 while (((c = *s++) != '\0') && ISDIGIT(c)) { 1696 j *= 10; 1697 j += c - '0'; 1698 } 1699 *ptr = s; 1700 i &= 0xff; 1701 j &= 0xff; 1702 return (i << 8) | j; 1703} 1704 1705 1706int 1707ipf_p_ftp_eprt(softf, fin, ip, nat, ftp, dlen) 1708 ipf_ftp_softc_t *softf; 1709 fr_info_t *fin; 1710 ip_t *ip; 1711 nat_t *nat; 1712 ftpinfo_t *ftp; 1713 int dlen; 1714{ 1715 ftpside_t *f; 1716 1717 /* 1718 * Check for client sending out EPRT message. 1719 */ 1720 if (dlen < IPF_MINEPRTLEN) { 1721 DT1(epert_dlen, int, dlen); 1722 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 1723 printf("ipf_p_ftp_eprt:dlen(%d) < IPF_MINEPRTLEN\n", 1724 dlen); 1725 return 0; 1726 } 1727 1728 /* 1729 * Parse the EPRT command. Format is: 1730 * "EPRT |1|1.2.3.4|2000|" for IPv4 and 1731 * "EPRT |2|ef00::1:2|2000|" for IPv6 1732 */ 1733 f = &ftp->ftp_side[0]; 1734 if (f->ftps_rptr[5] != '|') 1735 return 0; 1736 if (f->ftps_rptr[5] == f->ftps_rptr[7]) { 1737 if (f->ftps_rptr[6] == '1' && nat->nat_v[0] == 4) 1738 return ipf_p_ftp_eprt4(softf, fin, ip, nat, ftp, dlen); 1739#ifdef USE_INET6 1740 if (f->ftps_rptr[6] == '2' && nat->nat_v[0] == 6) 1741 return ipf_p_ftp_eprt6(softf, fin, ip, nat, ftp, dlen); 1742#endif 1743 } 1744 return 0; 1745} 1746 1747 1748int 1749ipf_p_ftp_eprt4(softf, fin, ip, nat, ftp, dlen) 1750 ipf_ftp_softc_t *softf; 1751 fr_info_t *fin; 1752 ip_t *ip; 1753 nat_t *nat; 1754 ftpinfo_t *ftp; 1755 int dlen; 1756{ 1757 int a1, a2, a3, a4, port, olen, nlen, inc, off; 1758 char newbuf[IPF_FTPBUFSZ]; 1759 char *s, c, delim; 1760 u_32_t addr, i; 1761 tcphdr_t *tcp; 1762 ftpside_t *f; 1763 mb_t *m; 1764 1765 m = fin->fin_m; 1766 tcp = (tcphdr_t *)fin->fin_dp; 1767 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff; 1768 f = &ftp->ftp_side[0]; 1769 delim = f->ftps_rptr[5]; 1770 s = f->ftps_rptr + 8; 1771 1772 /* 1773 * get the IP address. 1774 */ 1775 i = 0; 1776 while (((c = *s++) != '\0') && ISDIGIT(c)) { 1777 i *= 10; 1778 i += c - '0'; 1779 } 1780 if (i > 255) 1781 return 0; 1782 if (c != '.') 1783 return 0; 1784 addr = (i << 24); 1785 1786 i = 0; 1787 while (((c = *s++) != '\0') && ISDIGIT(c)) { 1788 i *= 10; 1789 i += c - '0'; 1790 } 1791 if (i > 255) 1792 return 0; 1793 if (c != '.') 1794 return 0; 1795 addr |= (addr << 16); 1796 1797 i = 0; 1798 while (((c = *s++) != '\0') && ISDIGIT(c)) { 1799 i *= 10; 1800 i += c - '0'; 1801 } 1802 if (i > 255) 1803 return 0; 1804 if (c != '.') 1805 return 0; 1806 addr |= (addr << 8); 1807 1808 i = 0; 1809 while (((c = *s++) != '\0') && ISDIGIT(c)) { 1810 i *= 10; 1811 i += c - '0'; 1812 } 1813 if (i > 255) 1814 return 0; 1815 if (c != delim) 1816 return 0; 1817 addr |= addr; 1818 1819 /* 1820 * Get the port number 1821 */ 1822 i = 0; 1823 while (((c = *s++) != '\0') && ISDIGIT(c)) { 1824 i *= 10; 1825 i += c - '0'; 1826 } 1827 if (i > 65535) 1828 return 0; 1829 if (c != delim) 1830 return 0; 1831 port = i; 1832 1833 /* 1834 * Check for CR-LF at the end of the command string. 1835 */ 1836 if ((*s != '\r') || (*(s + 1) != '\n')) { 1837 DT(eprt4_no_crlf); 1838 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 1839 printf("ipf_p_ftp_eprt4:missing %s\n", "cr-lf"); 1840 return 0; 1841 } 1842 s += 2; 1843 1844 /* 1845 * Calculate new address parts for PORT command 1846 */ 1847 if (nat->nat_dir == NAT_INBOUND) 1848 a1 = ntohl(nat->nat_odstaddr); 1849 else 1850 a1 = ntohl(ip->ip_src.s_addr); 1851 a2 = (a1 >> 16) & 0xff; 1852 a3 = (a1 >> 8) & 0xff; 1853 a4 = a1 & 0xff; 1854 a1 >>= 24; 1855 olen = s - f->ftps_rptr; 1856 /* DO NOT change this to snprintf! */ 1857 /* 1858 * While we could force the use of | as a delimiter here, it makes 1859 * sense to preserve whatever character is being used by the systems 1860 * involved in the communication. 1861 */ 1862#if defined(SNPRINTF) && defined(_KERNEL) 1863 SNPRINTF(newbuf, sizeof(newbuf), "%s %c1%c%u.%u.%u.%u%c%u%c\r\n", 1864 "EPRT", delim, delim, a1, a2, a3, a4, delim, port, delim); 1865#else 1866 (void) sprintf(newbuf, "%s %c1%c%u.%u.%u.%u%c%u%c\r\n", 1867 "EPRT", delim, delim, a1, a2, a3, a4, delim, port, 1868 delim); 1869#endif 1870 1871 nlen = strlen(newbuf); 1872 inc = nlen - olen; 1873 if ((inc + fin->fin_plen) > 65535) { 1874 DT2(eprt4_len, int, inc, int, fin->fin_plen); 1875 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 1876 printf("ipf_p_ftp_eprt4:inc(%d) + ip->ip_len > 65535\n", 1877 inc); 1878 return 0; 1879 } 1880 1881 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff; 1882#if !defined(_KERNEL) 1883 M_ADJ(m, inc); 1884#else 1885 if (inc < 0) 1886 M_ADJ(m, inc); 1887#endif 1888 /* the mbuf chain will be extended if necessary by m_copyback() */ 1889 COPYBACK(m, off, nlen, newbuf); 1890 fin->fin_flx |= FI_DOCKSUM; 1891 1892 if (inc != 0) { 1893 fin->fin_plen += inc; 1894 ip->ip_len = htons(fin->fin_plen); 1895 fin->fin_dlen += inc; 1896 } 1897 1898 f->ftps_cmd = FTPXY_C_EPRT; 1899 return ipf_p_ftp_addport(softf, fin, ip, nat, ftp, dlen, port, inc); 1900} 1901 1902 1903int 1904ipf_p_ftp_epsv(softf, fin, ip, nat, ftp, dlen) 1905 ipf_ftp_softc_t *softf; 1906 fr_info_t *fin; 1907 ip_t *ip; 1908 nat_t *nat; 1909 ftpinfo_t *ftp; 1910 int dlen; 1911{ 1912 char newbuf[IPF_FTPBUFSZ]; 1913 u_short ap = 0; 1914 ftpside_t *f; 1915 char *s; 1916 1917 if ((softf->ipf_p_ftp_forcepasv != 0) && 1918 (ftp->ftp_side[0].ftps_cmd != FTPXY_C_EPSV)) { 1919 DT1(epsv_cmd, int, ftp->ftp_side[0].ftps_cmd); 1920 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 1921 printf("ipf_p_ftp_epsv:ftps_cmd(%d) != FTPXY_C_EPSV\n", 1922 ftp->ftp_side[0].ftps_cmd); 1923 return 0; 1924 } 1925 f = &ftp->ftp_side[1]; 1926 1927#define EPSV_REPLEN 33 1928 /* 1929 * Check for EPSV reply message. 1930 */ 1931 if (dlen < IPF_MIN229LEN) { 1932 return (0); 1933 } else if (strncmp(f->ftps_rptr, 1934 "229 Entering Extended Passive Mode", EPSV_REPLEN)) { 1935 return (0); 1936} 1937 1938 /* 1939 * Skip the EPSV command + space 1940 */ 1941 s = f->ftps_rptr + 33; 1942 while (*s && !ISDIGIT(*s)) 1943 s++; 1944 1945 /* 1946 * As per RFC 2428, there are no addres components in the EPSV 1947 * response. So we'll go straight to getting the port. 1948 */ 1949 while (*s && ISDIGIT(*s)) { 1950 ap *= 10; 1951 ap += *s++ - '0'; 1952 } 1953 1954 if (*s == '|') 1955 s++; 1956 if (*s == ')') 1957 s++; 1958 if (*s == '\n') 1959 s--; 1960 /* 1961 * check for CR-LF at the end. 1962 */ 1963 if ((*s != '\r') || (*(s + 1) != '\n')) { 1964 return 0; 1965 } 1966 s += 2; 1967 1968#if defined(SNPRINTF) && defined(_KERNEL) 1969 SNPRINTF(newbuf, sizeof(newbuf), "%s (|||%u|)\r\n", 1970 "229 Entering Extended Passive Mode", ap); 1971#else 1972 (void) sprintf(newbuf, "%s (|||%u|)\r\n", 1973 "229 Entering Extended Passive Mode", ap); 1974#endif 1975 1976 return ipf_p_ftp_pasvreply(softf, fin, ip, nat, ftp, (u_int)ap, 1977 newbuf, s); 1978} 1979 1980#ifdef USE_INET6 1981int 1982ipf_p_ftp_eprt6(softf, fin, ip, nat, ftp, dlen) 1983 ipf_ftp_softc_t *softf; 1984 fr_info_t *fin; 1985 ip_t *ip; 1986 nat_t *nat; 1987 ftpinfo_t *ftp; 1988 int dlen; 1989{ 1990 int port, olen, nlen, inc, off, left, i; 1991 char newbuf[IPF_FTPBUFSZ]; 1992 char *s, c; 1993 i6addr_t addr, *a6; 1994 tcphdr_t *tcp; 1995 ip6_t *ip6; 1996 char delim; 1997 u_short whole; 1998 u_short part; 1999 ftpside_t *f; 2000 u_short *t; 2001 int fwd; 2002 mb_t *m; 2003 u_32_t a; 2004 2005 m = fin->fin_m; 2006 ip6 = (ip6_t *)ip; 2007 f = &ftp->ftp_side[0]; 2008 s = f->ftps_rptr + 8; 2009 f = &ftp->ftp_side[0]; 2010 delim = f->ftps_rptr[5]; 2011 tcp = (tcphdr_t *)fin->fin_dp; 2012 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff; 2013 2014 addr.i6[0] = 0; 2015 addr.i6[1] = 0; 2016 addr.i6[2] = 0; 2017 addr.i6[3] = 0; 2018 /* 2019 * Parse an IPv6 address. 2020 * Go forward until either :: or | is found. If :: is found, 2021 * reverse direction. Direction change is performed to ease 2022 * parsing an unknown number of 0s in the middle. 2023 */ 2024 whole = 0; 2025 t = (u_short *)&addr; 2026 fwd = 1; 2027 for (part = 0; (c = *s) != '\0'; ) { 2028 if (c == delim) { 2029 *t = htons((u_short)whole); 2030 break; 2031 } 2032 if (c == ':') { 2033 *t = part; 2034 if (fwd) { 2035 *t = htons((u_short)whole); 2036 t++; 2037 } else { 2038 *t = htons((u_short)(whole >> 16)); 2039 t--; 2040 } 2041 whole = 0; 2042 if (fwd == 1 && s[1] == ':') { 2043 while (*s && *s != '|') 2044 s++; 2045 if ((c = *s) != delim) 2046 break; 2047 t = (u_short *)&addr.i6[3]; 2048 t++; 2049 fwd = 0; 2050 } else if (fwd == 0 && s[-1] == ':') { 2051 break; 2052 } 2053 } else { 2054 if (c >= '0' && c <= '9') { 2055 c -= '0'; 2056 } else if (c >= 'a' && c <= 'f') { 2057 c -= 'a' + 10; 2058 } else if (c >= 'A' && c <= 'F') { 2059 c -= 'A' + 10; 2060 } 2061 if (fwd) { 2062 whole <<= 8; 2063 whole |= c; 2064 } else { 2065 whole >>= 8; 2066 whole |= ((u_32_t)c) << 24; 2067 } 2068 } 2069 if (fwd) 2070 s++; 2071 else 2072 s--; 2073 } 2074 if (c != ':' && c != delim) 2075 return 0; 2076 2077 while (*s != '|') 2078 s++; 2079 s++; 2080 2081 /* 2082 * Get the port number 2083 */ 2084 i = 0; 2085 while (((c = *s++) != '\0') && ISDIGIT(c)) { 2086 i *= 10; 2087 i += c - '0'; 2088 } 2089 if (i > 65535) 2090 return 0; 2091 if (c != delim) 2092 return 0; 2093 port = (u_short)(i & 0xffff); 2094 2095 /* 2096 * Check for CR-LF at the end of the command string. 2097 */ 2098 if ((*s != '\r') || (*(s + 1) != '\n')) { 2099 DT(eprt6_no_crlf); 2100 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR) 2101 printf("ipf_p_ftp_eprt6:missing %s\n", "cr-lf"); 2102 return 0; 2103 } 2104 s += 2; 2105 2106 /* 2107 * Calculate new address parts for PORT command 2108 */ 2109 a6 = (i6addr_t *)&ip6->ip6_src; 2110 olen = s - f->ftps_rptr; 2111 /* DO NOT change this to snprintf! */ 2112 /* 2113 * While we could force the use of | as a delimiter here, it makes 2114 * sense to preserve whatever character is being used by the systems 2115 * involved in the communication. 2116 */ 2117 s = newbuf; 2118 left = sizeof(newbuf); 2119#if defined(SNPRINTF) && defined(_KERNEL) 2120 SNPRINTF(newbuf, left, "EPRT %c2%c", delim, delim); 2121 left -= strlen(s) + 1; 2122 s += strlen(s); 2123 a = ntohl(a6->i6[0]); 2124 SNPRINTF(s, left, "%x:%x:", a >> 16, a & 0xffff); 2125 left -= strlen(s); 2126 s += strlen(s); 2127 a = ntohl(a6->i6[1]); 2128 SNPRINTF(s, left, "%x:%x:", a >> 16, a & 0xffff); 2129 left -= strlen(s); 2130 s += strlen(s); 2131 a = ntohl(a6->i6[2]); 2132 SNPRINTF(s, left, "%x:%x:", a >> 16, a & 0xffff); 2133 left -= strlen(s); 2134 s += strlen(s); 2135 a = ntohl(a6->i6[3]); 2136 SNPRINTF(s, left, "%x:%x", a >> 16, a & 0xffff); 2137 left -= strlen(s); 2138 s += strlen(s); 2139 SNPRINTF(s, left, "|%d|\r\n", port); 2140#else 2141 (void) sprintf(s, "EPRT %c2%c", delim, delim); 2142 s += strlen(s); 2143 a = ntohl(a6->i6[0]); 2144 sprintf(s, "%x:%x:", a >> 16, a & 0xffff); 2145 s += strlen(s); 2146 a = ntohl(a6->i6[1]); 2147 sprintf(s, "%x:%x:", a >> 16, a & 0xffff); 2148 left -= strlen(s); 2149 s += strlen(s); 2150 a = ntohl(a6->i6[2]); 2151 sprintf(s, "%x:%x:", a >> 16, a & 0xffff); 2152 left -= strlen(s); 2153 s += strlen(s); 2154 a = ntohl(a6->i6[3]); 2155 sprintf(s, "%x:%x", a >> 16, a & 0xffff); 2156 left -= strlen(s); 2157 s += strlen(s); 2158 sprintf(s, "|%d|\r\n", port); 2159#endif 2160 nlen = strlen(newbuf); 2161 inc = nlen - olen; 2162 if ((inc + fin->fin_plen) > 65535) { 2163 DT2(eprt6_len, int, inc, int, fin->fin_plen); 2164 if (softf->ipf_p_ftp_debug & DEBUG_ERROR) 2165 printf("ipf_p_ftp_eprt6:inc(%d) + ip->ip_len > 65535\n", 2166 inc); 2167 return 0; 2168 } 2169 2170 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff; 2171#if !defined(_KERNEL) 2172 M_ADJ(m, inc); 2173#else 2174 if (inc < 0) 2175 M_ADJ(m, inc); 2176#endif 2177 /* the mbuf chain will be extended if necessary by m_copyback() */ 2178 COPYBACK(m, off, nlen, newbuf); 2179 fin->fin_flx |= FI_DOCKSUM; 2180 2181 if (inc != 0) { 2182 fin->fin_plen += inc; 2183 ip6->ip6_plen = htons(fin->fin_plen - fin->fin_hlen); 2184 fin->fin_dlen += inc; 2185 } 2186 2187 f->ftps_cmd = FTPXY_C_EPRT; 2188 return ipf_p_ftp_addport(softf, fin, ip, nat, ftp, dlen, port, inc); 2189} 2190#endif 2191