]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/ipfilter/netinet/ip_ftp_pxy.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / sys / contrib / ipfilter / netinet / ip_ftp_pxy.c
1 /*
2  * Simple FTP transparent proxy for in-kernel use.  For use with the NAT
3  * code.
4  * $FreeBSD$
5  */
6 #if SOLARIS && defined(_KERNEL)
7 extern  kmutex_t        ipf_rw;
8 #endif
9
10 #define isdigit(x)      ((x) >= '0' && (x) <= '9')
11
12 #define IPF_FTP_PROXY
13
14 #define IPF_MINPORTLEN  18
15 #define IPF_MAXPORTLEN  30
16 #define IPF_MIN227LEN   39
17 #define IPF_MAX227LEN   51
18
19
20 int ippr_ftp_init __P((void));
21 int ippr_ftp_out __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
22 int ippr_ftp_in __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
23 int ippr_ftp_portmsg __P((fr_info_t *, ip_t *, nat_t *));
24 int ippr_ftp_pasvmsg __P((fr_info_t *, ip_t *, nat_t *));
25
26 u_short ipf_ftp_atoi __P((char **));
27
28 static  frentry_t       natfr;
29
30
31 /*
32  * Initialize local structures.
33  */
34 int ippr_ftp_init()
35 {
36         bzero((char *)&natfr, sizeof(natfr));
37         natfr.fr_ref = 1;
38         natfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
39         return 0;
40 }
41
42
43 /*
44  * ipf_ftp_atoi - implement a version of atoi which processes numbers in
45  * pairs separated by commas (which are expected to be in the range 0 - 255),
46  * returning a 16 bit number combining either side of the , as the MSB and
47  * LSB.
48  */
49 u_short ipf_ftp_atoi(ptr)
50 char **ptr;
51 {
52         register char *s = *ptr, c;
53         register u_char i = 0, j = 0;
54
55         while ((c = *s++) && isdigit(c)) {
56                 i *= 10;
57                 i += c - '0';
58         }
59         if (c != ',') {
60                 *ptr = NULL;
61                 return 0;
62         }
63         while ((c = *s++) && isdigit(c)) {
64                 j *= 10;
65                 j += c - '0';
66         }
67         *ptr = s;
68         return (i << 8) | j;
69 }
70
71
72 int ippr_ftp_portmsg(fin, ip, nat)
73 fr_info_t *fin;
74 ip_t *ip;
75 nat_t *nat;
76 {
77         char portbuf[IPF_MAXPORTLEN + 1], newbuf[IPF_MAXPORTLEN + 1], *s;
78         tcphdr_t *tcp, tcph, *tcp2 = &tcph;
79         size_t nlen = 0, dlen, olen;
80         u_short a5, a6, sp, dp;
81         u_int a1, a2, a3, a4;
82         struct in_addr swip;
83         int off, inc = 0;
84         fr_info_t fi;
85         nat_t *ipn;
86         mb_t *m;
87 #if     SOLARIS
88         mb_t *m1;
89 #endif
90
91         tcp = (tcphdr_t *)fin->fin_dp;
92         bzero(portbuf, sizeof(portbuf));
93         off = (ip->ip_hl << 2) + (tcp->th_off << 2);
94
95 #if     SOLARIS
96         m = fin->fin_qfm;
97
98         dlen = msgdsize(m) - off;
99         if (dlen > 0)
100                 copyout_mblk(m, off, MIN(sizeof(portbuf), dlen), portbuf);
101 #else
102         m = *(mb_t **)fin->fin_mp;
103
104         dlen = mbufchainlen(m) - off;
105         if (dlen > 0)
106                 m_copydata(m, off, MIN(sizeof(portbuf), dlen), portbuf);
107 #endif
108         if (dlen == 0)
109                 return 0;
110         portbuf[sizeof(portbuf) - 1] = '\0';
111         *newbuf = '\0';
112         if (!strncmp(portbuf, "PORT ", 5)) { 
113                 if (dlen < IPF_MINPORTLEN)
114                         return 0;
115         } else
116                 return 0;
117
118         /*
119          * Skip the PORT command + space
120          */
121         s = portbuf + 5;
122         /*
123          * Pick out the address components, two at a time.
124          */
125         a1 = ipf_ftp_atoi(&s);
126         if (!s)
127                 return 0;
128         a2 = ipf_ftp_atoi(&s);
129         if (!s)
130                 return 0;
131
132         /*
133          * check that IP address in the PORT/PASV reply is the same as the
134          * sender of the command - prevents using PORT for port scanning.
135          */
136         a1 <<= 16;
137         a1 |= a2;
138         if (a1 != ntohl(nat->nat_inip.s_addr))
139                 return 0;
140
141         a5 = ipf_ftp_atoi(&s);
142         if (!s)
143                 return 0;
144         if (*s == ')')
145                 s++;
146
147         /*
148          * check for CR-LF at the end.
149          */
150         if (*s == '\n')
151                 s--;
152         if ((*s == '\r') && (*(s + 1) == '\n')) {
153                 s += 2;
154                 a6 = a5 & 0xff;
155         } else
156                 return 0;
157         a5 >>= 8;
158         /*
159          * Calculate new address parts for PORT command
160          */
161         a1 = ntohl(ip->ip_src.s_addr);
162         a2 = (a1 >> 16) & 0xff;
163         a3 = (a1 >> 8) & 0xff;
164         a4 = a1 & 0xff;
165         a1 >>= 24;
166         olen = s - portbuf;
167         (void) sprintf(newbuf, "%s %u,%u,%u,%u,%u,%u\r\n",
168                        "PORT", a1, a2, a3, a4, a5, a6);
169
170         nlen = strlen(newbuf);
171         inc = nlen - olen;
172 #if SOLARIS
173         for (m1 = m; m1->b_cont; m1 = m1->b_cont)
174                 ;
175         if ((inc > 0) && (m1->b_datap->db_lim - m1->b_wptr < inc)) {
176                 mblk_t *nm;
177
178                 /* alloc enough to keep same trailer space for lower driver */
179                 nm = allocb(nlen, BPRI_MED);
180                 PANIC((!nm),("ippr_ftp_out: allocb failed"));
181
182                 nm->b_band = m1->b_band;
183                 nm->b_wptr += nlen;
184
185                 m1->b_wptr -= olen;
186                 PANIC((m1->b_wptr < m1->b_rptr),
187                       ("ippr_ftp_out: cannot handle fragmented data block"));
188
189                 linkb(m1, nm);
190         } else {
191                 if (m1->b_datap->db_struiolim == m1->b_wptr)
192                         m1->b_datap->db_struiolim += inc;
193                 m1->b_datap->db_struioflag &= ~STRUIO_IP;
194                 m1->b_wptr += inc;
195         }
196         copyin_mblk(m, off, nlen, newbuf);
197 #else
198         if (inc < 0)
199                 m_adj(m, inc);
200         /* the mbuf chain will be extended if necessary by m_copyback() */
201         m_copyback(m, off, nlen, newbuf);
202 #endif
203         if (inc != 0) {
204 #if SOLARIS || defined(__sgi)
205                 register u_32_t sum1, sum2;
206
207                 sum1 = ip->ip_len;
208                 sum2 = ip->ip_len + inc;
209
210                 /* Because ~1 == -2, We really need ~1 == -1 */
211                 if (sum1 > sum2)
212                         sum2--;
213                 sum2 -= sum1;
214                 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
215
216                 fix_outcksum(&ip->ip_sum, sum2, 0);
217 #endif
218                 ip->ip_len += inc;
219         }
220
221         /*
222          * Add skeleton NAT entry for connection which will come back the
223          * other way.
224          */
225         sp = htons(a5 << 8 | a6);
226         /*
227          * The server may not make the connection back from port 20, but
228          * it is the most likely so use it here to check for a conflicting
229          * mapping.
230          */
231         dp = htons(fin->fin_data[1] - 1);
232         ipn = nat_outlookup(fin->fin_ifp, IPN_TCP, nat->nat_p, nat->nat_inip,
233                             ip->ip_dst, (dp << 16) | sp);
234         if (ipn == NULL) {
235                 bcopy((char *)fin, (char *)&fi, sizeof(fi));
236                 bzero((char *)tcp2, sizeof(*tcp2));
237                 tcp2->th_win = htons(8192);
238                 tcp2->th_sport = sp;
239                 tcp2->th_dport = 0; /* XXX - don't specify remote port */
240                 fi.fin_data[0] = ntohs(sp);
241                 fi.fin_data[1] = 0;
242                 fi.fin_dp = (char *)tcp2;
243                 swip = ip->ip_src;
244                 ip->ip_src = nat->nat_inip;
245                 ipn = nat_new(nat->nat_ptr, ip, &fi, IPN_TCP|FI_W_DPORT,
246                               NAT_OUTBOUND);
247                 if (ipn != NULL) {
248                         ipn->nat_age = fr_defnatage;
249                         (void) fr_addstate(ip, &fi, FI_W_DPORT);
250                 }
251                 ip->ip_src = swip;
252         }
253         return inc;
254 }
255
256
257 int ippr_ftp_out(fin, ip, aps, nat)
258 fr_info_t *fin;
259 ip_t *ip;
260 ap_session_t *aps;
261 nat_t *nat;
262 {
263         return ippr_ftp_portmsg(fin, ip, nat);
264 }
265
266
267 int ippr_ftp_pasvmsg(fin, ip, nat)
268 fr_info_t *fin;
269 ip_t *ip;
270 nat_t *nat;
271 {
272         char portbuf[IPF_MAX227LEN + 1], newbuf[IPF_MAX227LEN + 1], *s;
273         int off, olen, dlen, nlen = 0, inc = 0;
274         tcphdr_t tcph, *tcp2 = &tcph;
275         struct in_addr swip, swip2;
276         u_short a5, a6, dp, sp;
277         u_int a1, a2, a3, a4;
278         tcphdr_t *tcp;
279         fr_info_t fi;
280         nat_t *ipn;
281         mb_t *m;
282 #if     SOLARIS
283         mb_t *m1;
284 #endif
285
286         tcp = (tcphdr_t *)fin->fin_dp;
287         off = (ip->ip_hl << 2) + (tcp->th_off << 2);
288         m = *(mb_t **)fin->fin_mp;
289         bzero(portbuf, sizeof(portbuf));
290
291 #if     SOLARIS
292         m = fin->fin_qfm;
293
294         dlen = msgdsize(m) - off;
295         if (dlen > 0)
296                 copyout_mblk(m, off, MIN(sizeof(portbuf), dlen), portbuf);
297 #else
298         dlen = mbufchainlen(m) - off;
299         if (dlen > 0)
300                 m_copydata(m, off, MIN(sizeof(portbuf), dlen), portbuf);
301 #endif
302         if (dlen == 0)
303                 return 0;
304         portbuf[sizeof(portbuf) - 1] = '\0';
305         *newbuf = '\0';
306
307         if (!strncmp(portbuf, "227 ", 4)) {
308                 if (dlen < IPF_MIN227LEN)
309                         return 0;
310                 else if (strncmp(portbuf, "227 Entering Passive Mode", 25))
311                         return 0;
312         } else
313                 return 0;
314         /*
315          * Skip the PORT command + space
316          */
317         s = portbuf + 25;
318         while (*s && !isdigit(*s))
319                 s++;
320         /*
321          * Pick out the address components, two at a time.
322          */
323         a1 = ipf_ftp_atoi(&s);
324         if (!s)
325                 return 0;
326         a2 = ipf_ftp_atoi(&s);
327         if (!s)
328                 return 0;
329
330         /*
331          * check that IP address in the PORT/PASV reply is the same as the
332          * sender of the command - prevents using PORT for port scanning.
333          */
334         a1 <<= 16;
335         a1 |= a2;
336         if (a1 != ntohl(nat->nat_oip.s_addr))
337                 return 0;
338
339         a5 = ipf_ftp_atoi(&s);
340         if (!s)
341                 return 0;
342
343         if (*s == ')')
344                 s++;
345         if (*s == '\n')
346                 s--;
347         /*
348          * check for CR-LF at the end.
349          */
350         if ((*s == '\r') && (*(s + 1) == '\n')) {
351                 s += 2;
352                 a6 = a5 & 0xff;
353         } else
354                 return 0;
355         a5 >>= 8;
356         /*
357          * Calculate new address parts for 227 reply
358          */
359         a1 = ntohl(ip->ip_src.s_addr);
360         a2 = (a1 >> 16) & 0xff;
361         a3 = (a1 >> 8) & 0xff;
362         a4 = a1 & 0xff;
363         a1 >>= 24;
364         olen = s - portbuf;
365         (void) sprintf(newbuf, "%s %u,%u,%u,%u,%u,%u\r\n",
366                        "227 Entering Passive Mode", a1, a2, a3, a4, a5, a6);
367
368         nlen = strlen(newbuf);
369         inc = nlen - olen;
370 #if SOLARIS
371         for (m1 = m; m1->b_cont; m1 = m1->b_cont)
372                 ;
373         if ((inc > 0) && (m1->b_datap->db_lim - m1->b_wptr < inc)) {
374                 mblk_t *nm;
375
376                 /* alloc enough to keep same trailer space for lower driver */
377                 nm = allocb(nlen, BPRI_MED);
378                 PANIC((!nm),("ippr_ftp_out: allocb failed"));
379
380                 nm->b_band = m1->b_band;
381                 nm->b_wptr += nlen;
382
383                 m1->b_wptr -= olen;
384                 PANIC((m1->b_wptr < m1->b_rptr),
385                       ("ippr_ftp_out: cannot handle fragmented data block"));
386
387                 linkb(m1, nm);
388         } else {
389                 m1->b_wptr += inc;
390         }
391         copyin_mblk(m, off, nlen, newbuf);
392 #else
393         if (inc < 0)
394                 m_adj(m, inc);
395         /* the mbuf chain will be extended if necessary by m_copyback() */
396         m_copyback(m, off, nlen, newbuf);
397 #endif
398         if (inc != 0) {
399 #if SOLARIS || defined(__sgi)
400                 register u_32_t sum1, sum2;
401
402                 sum1 = ip->ip_len;
403                 sum2 = ip->ip_len + inc;
404
405                 /* Because ~1 == -2, We really need ~1 == -1 */
406                 if (sum1 > sum2)
407                         sum2--;
408                 sum2 -= sum1;
409                 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
410
411                 fix_outcksum(&ip->ip_sum, sum2, 0);
412 #endif
413                 ip->ip_len += inc;
414         }
415
416         /*
417          * Add skeleton NAT entry for connection which will come back the
418          * other way.
419          */
420         sp = 0;
421         dp = htons(fin->fin_data[1] - 1);
422         ipn = nat_outlookup(fin->fin_ifp, IPN_TCP, nat->nat_p, nat->nat_inip,
423                             ip->ip_dst, (dp << 16) | sp);
424         if (ipn == NULL) {
425                 bcopy((char *)fin, (char *)&fi, sizeof(fi));
426                 bzero((char *)tcp2, sizeof(*tcp2));
427                 tcp2->th_win = htons(8192);
428                 tcp2->th_sport = 0;             /* XXX - fake it for nat_new */
429                 fi.fin_data[0] = a5 << 8 | a6;
430                 tcp2->th_dport = htons(fi.fin_data[0]);
431                 fi.fin_data[1] = 0;
432                 fi.fin_dp = (char *)tcp2;
433                 swip = ip->ip_src;
434                 swip2 = ip->ip_dst;
435                 ip->ip_dst = ip->ip_src;
436                 ip->ip_src = nat->nat_inip;
437                 ipn = nat_new(nat->nat_ptr, ip, &fi, IPN_TCP|FI_W_SPORT,
438                               NAT_OUTBOUND);
439                 if (ipn != NULL) {
440                         ipn->nat_age = fr_defnatage;
441                         (void) fr_addstate(ip, &fi, FI_W_SPORT);
442                 }
443                 ip->ip_src = swip;
444                 ip->ip_dst = swip2;
445         }
446         return inc;
447 }
448
449
450 int ippr_ftp_in(fin, ip, aps, nat)
451 fr_info_t *fin;
452 ip_t *ip;
453 ap_session_t *aps;
454 nat_t *nat;
455 {
456
457         return ippr_ftp_pasvmsg(fin, ip, nat);
458 }