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