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