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