4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Simple FTP transparent proxy for in-kernel use. For use with the NAT
12 * Id: ip_ftp_pxy.c,v 2.88.2.19 2006/04/01 10:14:53 darrenr Exp $
17 #define IPF_MINPORTLEN 18
18 #define IPF_MINEPRTLEN 20
19 #define IPF_MAXPORTLEN 30
20 #define IPF_MIN227LEN 39
21 #define IPF_MAX227LEN 51
22 #define IPF_MIN229LEN 47
23 #define IPF_MAX229LEN 51
27 #define FTPXY_USER_1 2
28 #define FTPXY_USOK_1 3
29 #define FTPXY_PASS_1 4
30 #define FTPXY_PAOK_1 5
31 #define FTPXY_AUTH_1 6
32 #define FTPXY_AUOK_1 7
33 #define FTPXY_ADAT_1 8
34 #define FTPXY_ADOK_1 9
35 #define FTPXY_ACCT_1 10
36 #define FTPXY_ACOK_1 11
37 #define FTPXY_USER_2 12
38 #define FTPXY_USOK_2 13
39 #define FTPXY_PASS_2 14
40 #define FTPXY_PAOK_2 15
42 #define FTPXY_JUNK_OK 0
43 #define FTPXY_JUNK_BAD 1 /* Ignore all commands for this connection */
44 #define FTPXY_JUNK_EOL 2 /* consume the rest of this line only */
45 #define FTPXY_JUNK_CONT 3 /* Saerching for next numeric */
48 * Values for FTP commands. Numerics cover 0-999
50 #define FTPXY_C_PASV 1000
51 #define FTPXY_C_PORT 1001
52 #define FTPXY_C_EPSV 1002
53 #define FTPXY_C_EPRT 1003
56 typedef struct ipf_ftp_softc_s {
57 int ipf_p_ftp_pasvonly;
58 /* Do not require logins before transfers */
59 int ipf_p_ftp_insecure;
60 int ipf_p_ftp_pasvrdr;
61 /* PASV must be last command prior to 227 */
62 int ipf_p_ftp_forcepasv;
64 int ipf_p_ftp_single_xfer;
69 void ipf_p_ftp_main_load __P((void));
70 void ipf_p_ftp_main_unload __P((void));
71 void *ipf_p_ftp_soft_create __P((ipf_main_softc_t *));
72 void ipf_p_ftp_soft_destroy __P((ipf_main_softc_t *, void *));
74 int ipf_p_ftp_client __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *,
76 int ipf_p_ftp_complete __P((char *, size_t));
77 int ipf_p_ftp_in __P((void *, fr_info_t *, ap_session_t *, nat_t *));
78 int ipf_p_ftp_new __P((void *, fr_info_t *, ap_session_t *, nat_t *));
79 void ipf_p_ftp_del __P((ipf_main_softc_t *, ap_session_t *));
80 int ipf_p_ftp_out __P((void *, fr_info_t *, ap_session_t *, nat_t *));
81 int ipf_p_ftp_pasv __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *,
83 int ipf_p_ftp_epsv __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *,
85 int ipf_p_ftp_port __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *,
87 int ipf_p_ftp_process __P((ipf_ftp_softc_t *, fr_info_t *, nat_t *,
89 int ipf_p_ftp_server __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *,
91 int ipf_p_ftp_valid __P((ipf_ftp_softc_t *, ftpinfo_t *, int, char *, size_t));
92 int ipf_p_ftp_server_valid __P((ipf_ftp_softc_t *, ftpside_t *, char *,
94 int ipf_p_ftp_client_valid __P((ipf_ftp_softc_t *, ftpside_t *, char *,
96 u_short ipf_p_ftp_atoi __P((char **));
97 int ipf_p_ftp_pasvreply __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *,
98 ftpinfo_t *, u_int, char *, char *));
99 int ipf_p_ftp_eprt __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *,
101 int ipf_p_ftp_eprt4 __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *,
103 int ipf_p_ftp_eprt6 __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *,
105 int ipf_p_ftp_addport __P((ipf_ftp_softc_t *, fr_info_t *, ip_t *, nat_t *,
106 ftpinfo_t *, int, int, int));
107 void ipf_p_ftp_setpending __P((ipf_main_softc_t *, ftpinfo_t *));
112 #define DEBUG_SECURITY 0x01
113 #define DEBUG_ERROR 0x02
114 #define DEBUG_INFO 0x04
115 #define DEBUG_PARSE_ERR 0x08
116 #define DEBUG_PARSE_INFO 0x10
117 #define DEBUG_PARSE 0x20
119 static int ipf_p_ftp_proxy_init = 0;
120 static frentry_t ftppxyfr;
121 static ipftuneable_t ipf_ftp_tuneables[] = {
122 { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_debug) },
123 "ftp_debug", 0, 0x7f,
124 stsizeof(ipf_ftp_softc_t, ipf_p_ftp_debug),
126 { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_pasvonly) },
127 "ftp_pasvonly", 0, 1,
128 stsizeof(ipf_ftp_softc_t, ipf_p_ftp_pasvonly),
130 { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_insecure) },
131 "ftp_insecure", 0, 1,
132 stsizeof(ipf_ftp_softc_t, ipf_p_ftp_insecure),
134 { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_pasvrdr) },
136 stsizeof(ipf_ftp_softc_t, ipf_p_ftp_pasvrdr),
138 { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_forcepasv) },
139 "ftp_forcepasv", 0, 1,
140 stsizeof(ipf_ftp_softc_t, ipf_p_ftp_forcepasv),
142 { { (void *)offsetof(ipf_ftp_softc_t, ipf_p_ftp_single_xfer) },
143 "ftp_single_xfer", 0, 1,
144 stsizeof(ipf_ftp_softc_t, ipf_p_ftp_single_xfer),
146 { { NULL }, NULL, 0, 0, 0, 0, NULL, NULL }
151 ipf_p_ftp_main_load()
153 bzero((char *)&ftppxyfr, sizeof(ftppxyfr));
155 ftppxyfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
157 MUTEX_INIT(&ftppxyfr.fr_lock, "FTP Proxy Mutex");
158 ipf_p_ftp_proxy_init = 1;
163 ipf_p_ftp_main_unload()
166 if (ipf_p_ftp_proxy_init == 1) {
167 MUTEX_DESTROY(&ftppxyfr.fr_lock);
168 ipf_p_ftp_proxy_init = 0;
174 * Initialize local structures.
177 ipf_p_ftp_soft_create(softc)
178 ipf_main_softc_t *softc;
180 ipf_ftp_softc_t *softf;
182 KMALLOC(softf, ipf_ftp_softc_t *);
186 bzero((char *)softf, sizeof(*softf));
188 softf->ipf_p_ftp_debug = 0;
190 softf->ipf_p_ftp_debug = DEBUG_PARSE_ERR;
192 softf->ipf_p_ftp_forcepasv = 1;
194 softf->ipf_p_ftp_tune = ipf_tune_array_copy(softf,
195 sizeof(ipf_ftp_tuneables),
197 if (softf->ipf_p_ftp_tune == NULL) {
198 ipf_p_ftp_soft_destroy(softc, softf);
201 if (ipf_tune_array_link(softc, softf->ipf_p_ftp_tune) == -1) {
202 ipf_p_ftp_soft_destroy(softc, softf);
211 ipf_p_ftp_soft_destroy(softc, arg)
212 ipf_main_softc_t *softc;
215 ipf_ftp_softc_t *softf = arg;
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;
228 ipf_p_ftp_new(arg, fin, aps, nat)
237 KMALLOC(ftp, ftpinfo_t *);
241 nat = nat; /* LINT */
244 aps->aps_psiz = sizeof(ftpinfo_t);
245 aps->aps_sport = htons(fin->fin_sport);
246 aps->aps_dport = htons(fin->fin_dport);
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;
262 ipf_p_ftp_setpending(ipf_main_softc_t *softc, ftpinfo_t *ftp)
264 if (ftp->ftp_pendnat != NULL)
265 ipf_nat_setpending(softc, ftp->ftp_pendnat);
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);
276 ipf_p_ftp_del(softc, aps)
277 ipf_main_softc_t *softc;
284 ipf_p_ftp_setpending(softc, ftp);
289 ipf_p_ftp_port(softf, fin, ip, nat, ftp, dlen)
290 ipf_ftp_softc_t *softf;
297 char newbuf[IPF_FTPBUFSZ], *s;
298 u_int a1, a2, a3, a4;
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;
312 * Check for client sending out PORT message.
314 if (dlen < IPF_MINPORTLEN) {
315 DT3(ftp_PORT_error_dlen, nat_t *, nat, ftpside_t *, f,
317 if (softf->ipf_p_ftp_debug & DEBUG_PARSE)
318 printf("ipf_p_ftp_port:dlen(%d) < IPF_MINPORTLEN\n",
323 * Skip the PORT command + space
325 s = f->ftps_rptr + 5;
327 * Pick out the address components, two at a time.
329 a1 = ipf_p_ftp_atoi(&s);
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);
336 a2 = ipf_p_ftp_atoi(&s);
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);
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.
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,
356 if (softf->ipf_p_ftp_debug & DEBUG_ERROR)
357 printf("ipf_p_ftp_port:%s != nat->nat_inip\n", "a1");
361 a5 = ipf_p_ftp_atoi(&s);
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);
372 * check for CR-LF at the end.
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");
386 * Calculate the source port. Verification of > 1024 is in
394 * Calculate new address parts for PORT command
396 if (nat->nat_dir == NAT_INBOUND)
397 a1 = ntohl(nat->nat_ndstaddr);
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;
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);
411 (void) sprintf(newbuf, "%s %u,%u,%u,%u,%u,%u\r\n",
412 "PORT", a1, a2, a3, a4, a5, a6);
415 nlen = strlen(newbuf);
417 if ((inc + fin->fin_plen) > 65535) {
418 DT3(ftp_PORT_error_inc, nat_t *, nat, ftpside_t *, f,
420 if (softf->ipf_p_ftp_debug & DEBUG_ERROR)
421 printf("ipf_p_ftp_port:inc(%d) + ip->ip_len > 65535\n",
426 #if !defined(_KERNEL)
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().
436 #endif /* !defined(_KERNEL) */
437 COPYBACK(m, off, nlen, newbuf);
438 fin->fin_flx |= FI_DOCKSUM;
441 fin->fin_plen += inc;
442 ip->ip_len = htons(fin->fin_plen);
443 fin->fin_dlen += inc;
446 f->ftps_cmd = FTPXY_C_PORT;
447 return ipf_p_ftp_addport(softf, fin, ip, nat, ftp, dlen, sp, inc);
452 ipf_p_ftp_addport(softf, fin, ip, nat, ftp, dlen, nport, inc)
453 ipf_ftp_softc_t *softf;
458 int dlen, nport, inc;
460 tcphdr_t tcph, *tcp2 = &tcph;
461 ipf_main_softc_t *softc;
462 ipf_nat_softc_t *softn;
470 softc = fin->fin_main_soft;
471 softn = softc->ipf_nat_soft;
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,
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);
482 ipf_p_ftp_setpending(softc, ftp);
486 * Add skeleton NAT entry for connection which will come back the
491 * Don't allow the PORT command to specify a port < 1024 due to
495 DT3(ftp_PORT_error_port, nat_t *, nat, ftpinfo_t *, ftp,
497 if (softf->ipf_p_ftp_debug & DEBUG_SECURITY)
498 printf("ipf_p_ftp_addport:sp(%d) < 1024\n", sp);
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
506 bcopy((char *)fin, (char *)&fi, sizeof(fi));
507 fi.fin_flx |= FI_IGNORE;
509 fi.fin_data[1] = fin->fin_data[1] - 1;
510 fi.fin_src6 = nat->nat_ndst6;
511 fi.fin_dst6 = nat->nat_nsrc6;
513 if (nat->nat_v[0] == 6) {
520 * Add skeleton NAT entry for connection which will come back the
524 if (nat->nat_v[0] == 6) {
525 if (nat->nat_dir == NAT_OUTBOUND) {
526 nat2 = ipf_nat6_outlookup(&fi, IPN_TCP|NAT_SEARCH,
529 &nat->nat_odst6.in6);
531 nat2 = ipf_nat6_inlookup(&fi, IPN_TCP|NAT_SEARCH,
534 &nat->nat_osrc6.in6);
539 if (nat->nat_dir == NAT_OUTBOUND) {
540 nat2 = ipf_nat_outlookup(&fi, IPN_TCP|NAT_SEARCH,
545 nat2 = ipf_nat_inlookup(&fi, IPN_TCP|NAT_SEARCH,
554 ipn = ipf_proxy_rule_rev(nat);
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;
567 bzero((char *)tcp2, sizeof(*tcp2));
569 tcp2->th_dport = htons(sp);
571 tcp2->th_win = htons(8192);
573 tcp2->th_flags = TH_SYN;
575 if (nat->nat_dir == NAT_INBOUND) {
577 direction = NAT_OUTBOUND;
580 direction = NAT_INBOUND;
582 flags = SI_W_SPORT|NAT_SLAVE|IPN_TCP;
584 MUTEX_ENTER(&softn->ipf_nat_new);
585 if (nat->nat_v[0] == 6) {
587 nat2 = ipf_nat6_add(&fi, ipn, &ftp->ftp_pendnat, flags,
591 nat2 = ipf_nat_add(&fi, ipn, &ftp->ftp_pendnat, flags,
594 MUTEX_EXIT(&softn->ipf_nat_new);
597 KFREES(ipn, ipn->in_size);
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);
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,
610 ipf_nat_setpending(softc, nat2);
617 ipf_p_ftp_client(softf, fin, ip, nat, ftp, dlen)
618 ipf_ftp_softc_t *softf;
625 char *rptr, *wptr, cmd[6], c;
630 f = &ftp->ftp_side[0];
634 for (i = 0; (i < 5) && (i < dlen); i++) {
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;
652 ftp->ftp_passok = FTPXY_USER_1;
655 } else if (!strncmp(cmd, "AUTH ", 5)) {
656 ftp->ftp_passok = FTPXY_AUTH_1;
658 } else if (!strncmp(cmd, "PASS ", 5)) {
659 if (ftp->ftp_passok == FTPXY_USOK_1) {
660 ftp->ftp_passok = FTPXY_PASS_1;
662 } else if (ftp->ftp_passok == FTPXY_USOK_2) {
663 ftp->ftp_passok = FTPXY_PASS_2;
666 } else if ((ftp->ftp_passok == FTPXY_AUOK_1) &&
667 !strncmp(cmd, "ADAT ", 5)) {
668 ftp->ftp_passok = FTPXY_ADAT_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;
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);
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);
692 DT2(ftp_client_passok, char *, cmd, int, ftp->ftp_passok);
693 while ((*rptr++ != '\n') && (rptr < wptr))
701 ipf_p_ftp_pasv(softf, fin, ip, nat, ftp, dlen)
702 ipf_ftp_softc_t *softf;
709 u_int a1, a2, a3, a4, data_ip;
710 char newbuf[IPF_FTPBUFSZ];
711 const char *brackets[2];
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);
725 f = &ftp->ftp_side[1];
727 #define PASV_REPLEN 24
729 * Check for PASV reply message.
731 if (dlen < IPF_MIN227LEN) {
732 DT3(ftp_PASV_error_short, nat_t *, nat, ftpinfo_t *, ftp,
734 if (softf->ipf_p_ftp_debug & DEBUG_ERROR)
735 printf("ipf_p_ftp_pasv:dlen(%d) < IPF_MIN227LEN\n",
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);
749 * Skip the PASV reply + space
751 s = f->ftps_rptr + PASV_REPLEN;
752 while (*s && !ISDIGIT(*s)) {
761 * Pick out the address components, two at a time.
763 a1 = ipf_p_ftp_atoi(&s);
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);
770 a2 = ipf_p_ftp_atoi(&s);
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);
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.
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,
791 if (softf->ipf_p_ftp_debug & DEBUG_ERROR)
792 printf("ipf_p_ftp_pasv:%s != nat->nat_oip\n", "a1");
796 a5 = ipf_p_ftp_atoi(&s);
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);
811 * check for CR-LF at the end.
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");
824 * Calculate new address parts for 227 reply
826 if (nat->nat_dir == NAT_INBOUND) {
827 data_ip = nat->nat_odstaddr;
832 a2 = (a1 >> 16) & 0xff;
833 a3 = (a1 >> 8) & 0xff;
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]);
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]);
846 return ipf_p_ftp_pasvreply(softf, fin, ip, nat, ftp, (a5 << 8 | a6),
851 ipf_p_ftp_pasvreply(softf, fin, ip, nat, ftp, port, newmsg, s)
852 ipf_ftp_softc_t *softf;
861 int inc, off, nflags;
862 tcphdr_t *tcp, tcph, *tcp2;
863 ipf_main_softc_t *softc;
864 ipf_nat_softc_t *softn;
875 softc = fin->fin_main_soft;
876 softn = softc->ipf_nat_soft;
878 if ((ftp->ftp_pendnat != NULL) || (ftp->ftp_pendstate != NULL))
879 ipf_p_ftp_setpending(softc, ftp);
882 tcp = (tcphdr_t *)fin->fin_dp;
883 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
888 f = &ftp->ftp_side[1];
889 olen = s - f->ftps_rptr;
890 nlen = strlen(newmsg);
892 if ((inc + fin->fin_plen) > 65535) {
893 DT3(ftp_PASV_error_inc, nat_t *, nat, ftpside_t *, f,
895 if (softf->ipf_p_ftp_debug & DEBUG_ERROR)
896 printf("ipf_p_ftp_pasv:inc(%d) + ip->ip_len > 65535\n",
901 ipn = ipf_proxy_rule_fwd(nat);
907 * Add skeleton NAT entry for connection which will come back the
910 bzero((char *)tcp2, sizeof(*tcp2));
911 bcopy((char *)fin, (char *)&fi, sizeof(fi));
912 fi.fin_flx |= FI_IGNORE;
914 fi.fin_data[1] = port;
915 nflags = IPN_TCP|SI_W_SPORT;
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;
927 tcp2->th_flags = TH_SYN;
928 tcp2->th_win = htons(8192);
929 tcp2->th_dport = htons(port);
931 MUTEX_ENTER(&softn->ipf_nat_new);
933 if (nat->nat_v[0] == 6)
934 nat2 = ipf_nat6_add(&fi, ipn, &ftp->ftp_pendnat,
935 nflags, nat->nat_dir);
938 nat2 = ipf_nat_add(&fi, ipn, &ftp->ftp_pendnat,
939 nflags, nat->nat_dir);
940 MUTEX_EXIT(&softn->ipf_nat_new);
943 KFREES(ipn, ipn->in_size);
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);
952 if (nat->nat_dir == NAT_INBOUND) {
953 if (nat->nat_v[0] == 6) {
955 fi.fin_dst6 = nat->nat_ndst6;
958 fi.fin_daddr = nat->nat_ndstaddr;
961 if (ipf_state_add(softc, &fi, (ipstate_t **)&ftp->ftp_pendstate,
963 ipf_nat_setpending(softc, nat2);
965 #if !defined(_KERNEL)
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().
975 #endif /* !defined(_KERNEL) */
976 COPYBACK(m, off, nlen, newmsg);
977 fin->fin_flx |= FI_DOCKSUM;
980 fin->fin_plen += inc;
981 fin->fin_dlen += inc;
982 if (nat->nat_v[0] == 6) {
984 ip6 = (ip6_t *)fin->fin_ip;
985 u_short len = ntohs(ip6->ip6_plen) + inc;
986 ip6->ip6_plen = htons(len);
989 ip->ip_len = htons(fin->fin_plen);
998 ipf_p_ftp_server(softf, fin, ip, nat, ftp, dlen)
999 ipf_ftp_softc_t *softf;
1011 f = &ftp->ftp_side[1];
1012 rptr = f->ftps_rptr;
1013 wptr = f->ftps_wptr;
1015 DT2(ftp_server_response, char *, rptr, int, ftp->ftp_passok);
1018 if (!ISDIGIT(*rptr) || !ISDIGIT(*(rptr + 1)) || !ISDIGIT(*(rptr + 2)))
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)) {
1030 * 200 is returned for a successful command.
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) {
1042 if (ftp->ftp_passok == FTPXY_ACCT_1)
1043 ftp->ftp_passok = FTPXY_GO;
1046 } else if (*rptr == '2') {
1047 switch (ftp->ftp_passok)
1054 ftp->ftp_passok = FTPXY_GO;
1057 ftp->ftp_passok += 3;
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);
1070 while ((*rptr++ != '\n') && (rptr < wptr))
1072 f->ftps_rptr = rptr;
1083 * Look to see if the buffer starts with something which we recognise as
1084 * being the correct syntax for the FTP protocol.
1087 ipf_p_ftp_client_valid(softf, ftps, buf, len)
1088 ipf_ftp_softc_t *softf;
1093 register char *s, c, pc;
1094 register size_t i = len;
1099 if (ftps->ftps_junk == FTPXY_JUNK_BAD)
1100 return FTPXY_JUNK_BAD;
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);
1113 cmd[0] = TOUPPER(c);
1117 cmd[1] = TOUPPER(c);
1121 cmd[2] = TOUPPER(c);
1125 cmd[3] = TOUPPER(c);
1128 if ((c != ' ') && (c != '\r'))
1129 goto bad_client_command;
1130 } else if ((c != ' ') && (c != '\r'))
1131 goto bad_client_command;
1133 goto bad_client_command;
1135 goto bad_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;
1150 if ((pc == '\r') && (c == '\n')) {
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;
1162 #if !defined(_KERNEL)
1163 printf("ipf_p_ftp_client_valid:junk after cmd[%*.*s]\n",
1164 (int)len, (int)len, buf);
1166 return FTPXY_JUNK_EOL;
1171 ipf_p_ftp_server_valid(softf, ftps, buf, len)
1172 ipf_ftp_softc_t *softf;
1177 register char *s, c, pc;
1178 register size_t i = len;
1184 if (ftps->ftps_junk == FTPXY_JUNK_BAD)
1185 return FTPXY_JUNK_BAD;
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);
1202 cmd = (c - '0') * 100;
1206 cmd += (c - '0') * 10;
1213 if ((c != '-') && (c != ' '))
1214 goto bad_server_command;
1216 return FTPXY_JUNK_CONT;
1218 goto bad_server_command;
1220 goto bad_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;
1237 if ((pc == '\r') && (c == '\n')) {
1239 if (ftps->ftps_junk == FTPXY_JUNK_CONT)
1240 return FTPXY_JUNK_CONT;
1242 ftps->ftps_cmd = cmd;
1244 return FTPXY_JUNK_OK;
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;
1257 ipf_p_ftp_valid(softf, ftp, side, buf, len)
1258 ipf_ftp_softc_t *softf;
1267 ftps = &ftp->ftp_side[side];
1270 ret = ipf_p_ftp_client_valid(softf, ftps, buf, len);
1272 ret = ipf_p_ftp_server_valid(softf, ftps, buf, len);
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.
1286 ipf_p_ftp_process(softf, fin, nat, ftp, rv)
1287 ipf_ftp_softc_t *softf;
1293 int mlen, len, off, inc, i, sel, sel2, ok, ackoff, seqoff, retry;
1294 char *rptr, *wptr, *s;
1295 u_32_t thseq, thack;
1304 tcp = (tcphdr_t *)fin->fin_dp;
1305 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
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);
1312 mlen = fin->fin_plen - off;
1314 mlen = MSGDSIZE(m) - off;
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);
1323 if ((mlen == 0) && ((tcp->th_flags & TH_OPENING) == TH_OPENING)) {
1324 f->ftps_seq[0] = thseq + 1;
1325 t->ftps_seq[0] = thack;
1327 } else if (mlen < 0) {
1333 sel = aps->aps_sel[1 - rv];
1334 sel2 = aps->aps_sel[rv];
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];
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];
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]);
1355 if (aps->aps_seqmin[sel2] > ackoff + thack)
1356 ackoff = aps->aps_seqoff[!sel2];
1358 if (aps->aps_seqmin[sel2] > thack)
1359 ackoff = aps->aps_seqoff[!sel2];
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]);
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).
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);
1385 if (t->ftps_seq[0] == 0) {
1386 t->ftps_seq[0] = thack;
1390 if (t->ftps_seq[0] == thack)
1392 else if (t->ftps_seq[1] == thack) {
1393 t->ftps_seq[0] = thack;
1397 if (t->ftps_seq[0] + ackoff == thack) {
1398 t->ftps_seq[0] = thack;
1400 } else if (t->ftps_seq[0] == thack + ackoff) {
1401 t->ftps_seq[0] = thack + ackoff;
1403 } else if (t->ftps_seq[1] + ackoff == thack) {
1404 t->ftps_seq[0] = thack;
1406 } else if (t->ftps_seq[1] == thack + ackoff) {
1407 t->ftps_seq[0] = thack + ackoff;
1413 if (softf->ipf_p_ftp_debug & DEBUG_INFO) {
1415 printf("%s ok\n", "not");
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],
1426 printf("%s:seq[0](%u) + (%d) != (%u)\n",
1427 "ipf_p_ftp_process", t->ftps_seq[1],
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]);
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;
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]);
1456 if ((thseq == f->ftps_seq[0]) || (thseq == f->ftps_seq[1])) {
1459 * Retransmitted data packet.
1461 } else if ((thseq + mlen == f->ftps_seq[0]) ||
1462 (thseq + mlen == f->ftps_seq[1])) {
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]);
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;
1490 len = MIN(mlen, sizeof(f->ftps_buf) - (wptr - rptr));
1493 COPYDATA(m, off, len, wptr);
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,
1505 f->ftps_wptr = wptr;
1506 if (f->ftps_junk != FTPXY_JUNK_OK) {
1508 f->ftps_junk = ipf_p_ftp_valid(softf, ftp, rv, rptr,
1510 DT2(junk_transit, int, i, int, f->ftps_junk);
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);
1516 if (f->ftps_junk == FTPXY_JUNK_BAD) {
1518 if (wptr - rptr == sizeof(f->ftps_buf)) {
1519 if (softf->ipf_p_ftp_debug &
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;
1532 while ((f->ftps_junk == FTPXY_JUNK_OK) && (wptr > rptr)) {
1534 f->ftps_junk = ipf_p_ftp_valid(softf, ftp, rv,
1537 if (softf->ipf_p_ftp_debug & DEBUG_PARSE) {
1538 printf("%s=%d len %d rv %d ptr %lx/%lx ",
1540 f->ftps_junk, len, rv, (u_long)rptr,
1542 printf("buf [%*.*s]\n", len, len, rptr);
1545 if (f->ftps_junk == FTPXY_JUNK_OK) {
1547 f->ftps_rptr = rptr;
1549 inc += ipf_p_ftp_server(softf, fin, ip,
1552 inc += ipf_p_ftp_client(softf, fin, ip,
1554 rptr = f->ftps_rptr;
1555 wptr = f->ftps_wptr;
1560 * Off to a bad start so lets just forget about using the
1561 * ftp proxy for this connection.
1563 if ((f->ftps_cmds == 0) && (f->ftps_junk == FTPXY_JUNK_BAD)) {
1564 /* f->ftps_seq[1] += inc; */
1567 if (softf->ipf_p_ftp_debug & DEBUG_ERROR)
1568 printf("%s:cmds == 0 junk == 1\n",
1569 "ipf_p_ftp_process");
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')) {
1580 if (f->ftps_junk != FTPXY_JUNK_CONT)
1581 f->ftps_junk = FTPXY_JUNK_OK;
1588 rptr = wptr = f->ftps_buf;
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
1596 if (rptr > f->ftps_buf) {
1597 bcopy(rptr, f->ftps_buf, wptr - rptr);
1598 wptr -= rptr - f->ftps_buf;
1602 f->ftps_rptr = rptr;
1603 f->ftps_wptr = wptr;
1608 /* f->ftps_seq[1] += inc; */
1609 if (tcp->th_flags & TH_FIN)
1611 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_INFO) {
1613 mlen = fin->fin_plen;
1618 printf("ftps_seq[1] = %x inc %d len %d\n",
1619 f->ftps_seq[1], inc, mlen);
1622 f->ftps_rptr = rptr;
1623 f->ftps_wptr = wptr;
1624 return APR_INC(inc);
1629 ipf_p_ftp_out(arg, fin, aps, nat)
1635 ipf_ftp_softc_t *softf = arg;
1639 ftp = aps->aps_data;
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;
1647 return ipf_p_ftp_process(softf, fin, nat, ftp, rev);
1652 ipf_p_ftp_in(arg, fin, aps, nat)
1658 ipf_ftp_softc_t *softf = arg;
1662 ftp = aps->aps_data;
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;
1670 return ipf_p_ftp_process(softf, fin, nat, ftp, 1 - rev);
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
1684 register char *s = *ptr, c;
1685 register u_char i = 0, j = 0;
1687 while (((c = *s++) != '\0') && ISDIGIT(c)) {
1695 while (((c = *s++) != '\0') && ISDIGIT(c)) {
1702 return (i << 8) | j;
1707 ipf_p_ftp_eprt(softf, fin, ip, nat, ftp, dlen)
1708 ipf_ftp_softc_t *softf;
1718 * Check for client sending out EPRT message.
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",
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
1733 f = &ftp->ftp_side[0];
1734 if (f->ftps_rptr[5] != '|')
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);
1740 if (f->ftps_rptr[6] == '2' && nat->nat_v[0] == 6)
1741 return ipf_p_ftp_eprt6(softf, fin, ip, nat, ftp, dlen);
1749 ipf_p_ftp_eprt4(softf, fin, ip, nat, ftp, dlen)
1750 ipf_ftp_softc_t *softf;
1757 int a1, a2, a3, a4, port, olen, nlen, inc, off;
1758 char newbuf[IPF_FTPBUFSZ];
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;
1773 * get the IP address.
1776 while (((c = *s++) != '\0') && ISDIGIT(c)) {
1787 while (((c = *s++) != '\0') && ISDIGIT(c)) {
1795 addr |= (addr << 16);
1798 while (((c = *s++) != '\0') && ISDIGIT(c)) {
1806 addr |= (addr << 8);
1809 while (((c = *s++) != '\0') && ISDIGIT(c)) {
1820 * Get the port number
1823 while (((c = *s++) != '\0') && ISDIGIT(c)) {
1834 * Check for CR-LF at the end of the command string.
1836 if ((*s != '\r') || (*(s + 1) != '\n')) {
1838 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR)
1839 printf("ipf_p_ftp_eprt4:missing %s\n", "cr-lf");
1845 * Calculate new address parts for PORT command
1847 if (nat->nat_dir == NAT_INBOUND)
1848 a1 = ntohl(nat->nat_odstaddr);
1850 a1 = ntohl(ip->ip_src.s_addr);
1851 a2 = (a1 >> 16) & 0xff;
1852 a3 = (a1 >> 8) & 0xff;
1855 olen = s - f->ftps_rptr;
1856 /* DO NOT change this to snprintf! */
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.
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);
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,
1871 nlen = strlen(newbuf);
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",
1881 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
1882 #if !defined(_KERNEL)
1888 /* the mbuf chain will be extended if necessary by m_copyback() */
1889 COPYBACK(m, off, nlen, newbuf);
1890 fin->fin_flx |= FI_DOCKSUM;
1893 fin->fin_plen += inc;
1894 ip->ip_len = htons(fin->fin_plen);
1895 fin->fin_dlen += inc;
1898 f->ftps_cmd = FTPXY_C_EPRT;
1899 return ipf_p_ftp_addport(softf, fin, ip, nat, ftp, dlen, port, inc);
1904 ipf_p_ftp_epsv(softf, fin, ip, nat, ftp, dlen)
1905 ipf_ftp_softc_t *softf;
1912 char newbuf[IPF_FTPBUFSZ];
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);
1925 f = &ftp->ftp_side[1];
1927 #define EPSV_REPLEN 33
1929 * Check for EPSV reply message.
1931 if (dlen < IPF_MIN229LEN) {
1933 } else if (strncmp(f->ftps_rptr,
1934 "229 Entering Extended Passive Mode", EPSV_REPLEN)) {
1939 * Skip the EPSV command + space
1941 s = f->ftps_rptr + 33;
1942 while (*s && !ISDIGIT(*s))
1946 * As per RFC 2428, there are no addres components in the EPSV
1947 * response. So we'll go straight to getting the port.
1949 while (*s && ISDIGIT(*s)) {
1965 * check for CR-LF at the end.
1967 if ((*s != '\r') || (*(s + 1) != '\n')) {
1972 #if defined(SNPRINTF) && defined(_KERNEL)
1973 SNPRINTF(newbuf, sizeof(newbuf), "%s (|||%u|)\r\n",
1974 "229 Entering Extended Passive Mode", ap);
1976 (void) sprintf(newbuf, "%s (|||%u|)\r\n",
1977 "229 Entering Extended Passive Mode", ap);
1980 return ipf_p_ftp_pasvreply(softf, fin, ip, nat, ftp, (u_int)ap,
1986 ipf_p_ftp_eprt6(softf, fin, ip, nat, ftp, dlen)
1987 ipf_ftp_softc_t *softf;
1994 int port, olen, nlen, inc, off, left, i;
1995 char newbuf[IPF_FTPBUFSZ];
2011 f = &ftp->ftp_side[0];
2012 s = f->ftps_rptr + 8;
2013 f = &ftp->ftp_side[0];
2014 delim = f->ftps_rptr[5];
2015 tcp = (tcphdr_t *)fin->fin_dp;
2016 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
2023 * Parse an IPv6 address.
2024 * Go forward until either :: or | is found. If :: is found,
2025 * reverse direction. Direction change is performed to ease
2026 * parsing an unknown number of 0s in the middle.
2029 t = (u_short *)&addr;
2031 for (part = 0; (c = *s) != '\0'; ) {
2033 *t = htons((u_short)whole);
2039 *t = htons((u_short)whole);
2042 *t = htons((u_short)(whole >> 16));
2046 if (fwd == 1 && s[1] == ':') {
2047 while (*s && *s != '|')
2049 if ((c = *s) != delim)
2051 t = (u_short *)&addr.i6[3];
2054 } else if (fwd == 0 && s[-1] == ':') {
2058 if (c >= '0' && c <= '9') {
2060 } else if (c >= 'a' && c <= 'f') {
2062 } else if (c >= 'A' && c <= 'F') {
2070 whole |= ((u_32_t)c) << 24;
2078 if (c != ':' && c != delim)
2086 * Get the port number
2089 while (((c = *s++) != '\0') && ISDIGIT(c)) {
2097 port = (u_short)(i & 0xffff);
2100 * Check for CR-LF at the end of the command string.
2102 if ((*s != '\r') || (*(s + 1) != '\n')) {
2104 if (softf->ipf_p_ftp_debug & DEBUG_PARSE_ERR)
2105 printf("ipf_p_ftp_eprt6:missing %s\n", "cr-lf");
2111 * Calculate new address parts for PORT command
2113 a6 = (i6addr_t *)&ip6->ip6_src;
2114 olen = s - f->ftps_rptr;
2115 /* DO NOT change this to snprintf! */
2117 * While we could force the use of | as a delimiter here, it makes
2118 * sense to preserve whatever character is being used by the systems
2119 * involved in the communication.
2122 left = sizeof(newbuf);
2123 #if defined(SNPRINTF) && defined(_KERNEL)
2124 SNPRINTF(newbuf, left, "EPRT %c2%c", delim, delim);
2125 left -= strlen(s) + 1;
2127 a = ntohl(a6->i6[0]);
2128 SNPRINTF(s, left, "%x:%x:", a >> 16, a & 0xffff);
2131 a = ntohl(a6->i6[1]);
2132 SNPRINTF(s, left, "%x:%x:", a >> 16, a & 0xffff);
2135 a = ntohl(a6->i6[2]);
2136 SNPRINTF(s, left, "%x:%x:", a >> 16, a & 0xffff);
2139 a = ntohl(a6->i6[3]);
2140 SNPRINTF(s, left, "%x:%x", a >> 16, a & 0xffff);
2143 sprintf(s, "|%d|\r\n", port);
2145 (void) sprintf(s, "EPRT %c2%c", delim, delim);
2147 a = ntohl(a6->i6[0]);
2148 sprintf(s, "%x:%x:", a >> 16, a & 0xffff);
2150 a = ntohl(a6->i6[1]);
2151 sprintf(s, "%x:%x:", a >> 16, a & 0xffff);
2154 a = ntohl(a6->i6[2]);
2155 sprintf(s, "%x:%x:", a >> 16, a & 0xffff);
2158 a = ntohl(a6->i6[3]);
2159 sprintf(s, "%x:%x", a >> 16, a & 0xffff);
2162 sprintf(s, "|%d|\r\n", port);
2164 nlen = strlen(newbuf);
2166 if ((inc + fin->fin_plen) > 65535) {
2167 DT2(eprt6_len, int, inc, int, fin->fin_plen);
2168 if (softf->ipf_p_ftp_debug & DEBUG_ERROR)
2169 printf("ipf_p_ftp_eprt6:inc(%d) + ip->ip_len > 65535\n",
2174 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
2175 #if !defined(_KERNEL)
2181 /* the mbuf chain will be extended if necessary by m_copyback() */
2182 COPYBACK(m, off, nlen, newbuf);
2183 fin->fin_flx |= FI_DOCKSUM;
2186 fin->fin_plen += inc;
2187 ip6->ip6_plen = htons(fin->fin_plen - fin->fin_hlen);
2188 fin->fin_dlen += inc;
2191 f->ftps_cmd = FTPXY_C_EPRT;
2192 return ipf_p_ftp_addport(softf, fin, ip, nat, ftp, dlen, port, inc);