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