]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/ipfilter/netinet/ip_proxy.c
This commit was generated by cvs2svn to compensate for changes in r99179,
[FreeBSD/FreeBSD.git] / sys / contrib / ipfilter / netinet / ip_proxy.c
1 /*
2  * Copyright (C) 1997-2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  */
6
7 #if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL)
8 # define        _KERNEL
9 #endif
10
11 #ifdef __sgi
12 # include <sys/ptimers.h>
13 #endif
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <sys/time.h>
18 #include <sys/file.h>
19 #if !defined(__FreeBSD_version)  
20 # include <sys/ioctl.h>      
21 #endif
22 #include <sys/fcntl.h>
23 #if !defined(_KERNEL) && !defined(KERNEL)
24 # include <stdio.h>
25 # include <string.h>
26 # include <stdlib.h>
27 #endif
28 #ifndef linux
29 # include <sys/protosw.h>
30 #endif
31 #include <sys/socket.h>
32 #if defined(_KERNEL)
33 # if !defined(linux)
34 #  include <sys/systm.h>
35 # else
36 #  include <linux/string.h>
37 # endif
38 #endif
39 #if !defined(__SVR4) && !defined(__svr4__)
40 # ifndef linux
41 #  include <sys/mbuf.h>
42 # endif
43 #else
44 # include <sys/byteorder.h>
45 # ifdef _KERNEL
46 #  include <sys/dditypes.h>
47 # endif
48 # include <sys/stream.h>
49 # include <sys/kmem.h>
50 #endif
51 #if __FreeBSD__ > 2
52 # include <sys/queue.h>
53 #endif
54 #include <net/if.h>
55 #ifdef sun
56 # include <net/af.h>
57 #endif
58 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #ifndef linux
63 # include <netinet/ip_var.h>
64 #endif
65 #include <netinet/tcp.h>
66 #include <netinet/udp.h>
67 #include <netinet/ip_icmp.h>
68 #include "netinet/ip_compat.h"
69 #include <netinet/tcpip.h>
70 #include "netinet/ip_fil.h"
71 #include "netinet/ip_nat.h"
72 #include "netinet/ip_state.h"
73 #include "netinet/ip_proxy.h"
74 #if (__FreeBSD_version >= 300000)
75 # include <sys/malloc.h>
76 #endif
77
78 #if !defined(lint)
79 /* static const char rcsid[] = "@(#)$Id: ip_proxy.c,v 2.9.2.6 2001/07/15 22:06:15 darrenr Exp $"; */
80 static const char rcsid[] = "@(#)$FreeBSD$";
81 #endif
82
83 #if defined(_KERNEL) && (SOLARIS || defined(__sgi))
84 extern  KRWLOCK_T       ipf_nat, ipf_state;
85 #endif
86
87 #ifndef MIN
88 #define MIN(a,b)        (((a)<(b))?(a):(b))
89 #endif
90
91 static int appr_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int ));
92
93
94 #define AP_SESS_SIZE    53
95
96 #include "netinet/ip_ftp_pxy.c"
97 #if defined(_KERNEL)
98 #include "netinet/ip_rcmd_pxy.c"
99 #include "netinet/ip_raudio_pxy.c"
100 #include "netinet/ip_netbios_pxy.c"
101 #include "netinet/ip_ipsec_pxy.c"
102 #endif
103
104 ap_session_t    *ap_sess_tab[AP_SESS_SIZE];
105 ap_session_t    *ap_sess_list = NULL;
106 aproxy_t        *ap_proxylist = NULL;
107 aproxy_t        ap_proxies[] = {
108 #ifdef  IPF_FTP_PROXY
109         { NULL, "ftp", (char)IPPROTO_TCP, 0, 0, ippr_ftp_init, NULL,
110           ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL },
111 #endif
112 #ifdef  IPF_RCMD_PROXY
113         { NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, ippr_rcmd_init, NULL,
114           ippr_rcmd_new, NULL, NULL, ippr_rcmd_out, NULL },
115 #endif
116 #ifdef  IPF_RAUDIO_PROXY
117         { NULL, "raudio", (char)IPPROTO_TCP, 0, 0, ippr_raudio_init, NULL,
118           ippr_raudio_new, NULL, ippr_raudio_in, ippr_raudio_out, NULL },
119 #endif
120 #ifdef IPF_IPSEC_PROXY
121         { NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, ippr_ipsec_init, NULL,
122           ippr_ipsec_new, ippr_ipsec_del, NULL, ippr_ipsec_out,
123           ippr_ipsec_match },
124 #endif
125 #ifdef  IPF_NETBIOS_PROXY
126         { NULL, "netbios", (char)IPPROTO_UDP, 0, 0, ippr_netbios_init, NULL,
127           NULL, NULL, NULL, ippr_netbios_out, NULL },
128 #endif
129 #ifdef  IPF_H323_PROXY
130     { NULL, "h323", (char)IPPROTO_TCP, 0, 0, ippr_h323_init, NULL,
131           ippr_h323_new, ippr_h323_del, ippr_h323_in, ippr_h323_out, NULL },
132     { NULL, "h245", (char)IPPROTO_TCP, 0, 0, ippr_h245_init, NULL,
133           ippr_h245_new, NULL, NULL, ippr_h245_out, NULL },
134 #endif   
135         { NULL, "", '\0', 0, 0, NULL, NULL, NULL }
136 };
137
138
139 /*
140  * Dynamically add a new kernel proxy.  Ensure that it is unique in the
141  * collection compiled in and dynamically added.
142  */
143 int appr_add(ap)
144 aproxy_t *ap;
145 {
146         aproxy_t *a;
147
148         for (a = ap_proxies; a->apr_p; a++)
149                 if ((a->apr_p == ap->apr_p) &&
150                     !strncmp(a->apr_label, ap->apr_label,
151                              sizeof(ap->apr_label)))
152                         return -1;
153
154         for (a = ap_proxylist; a && a->apr_p; a = a->apr_next)
155                 if ((a->apr_p == ap->apr_p) &&
156                     !strncmp(a->apr_label, ap->apr_label,
157                              sizeof(ap->apr_label)))
158                         return -1;
159         ap->apr_next = ap_proxylist;
160         ap_proxylist = ap;
161         return (*ap->apr_init)();
162 }
163
164
165 /*
166  * Delete a proxy that has been added dynamically from those available.
167  * If it is in use, return 1 (do not destroy NOW), not in use 0 or -1
168  * if it cannot be matched.
169  */
170 int appr_del(ap)
171 aproxy_t *ap;
172 {
173         aproxy_t *a, **app;
174
175         for (app = &ap_proxylist; (a = *app); app = &a->apr_next)
176                 if (a == ap) {
177                         a->apr_flags |= APR_DELETE;
178                         *app = a->apr_next;
179                         if (ap->apr_ref != 0)
180                                 return 1;
181                         return 0;
182                 }
183         return -1;
184 }
185
186
187 /*
188  * Return 1 if the packet is a good match against a proxy, else 0.
189  */
190 int appr_ok(ip, tcp, nat)
191 ip_t *ip;
192 tcphdr_t *tcp;
193 ipnat_t *nat;
194 {
195         aproxy_t *apr = nat->in_apr;
196         u_short dport = nat->in_dport;
197
198         if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
199             (ip->ip_p != apr->apr_p))
200                 return 0;
201         if (((tcp != NULL) && (tcp->th_dport != dport)) || (!tcp && dport))
202                 return 0;
203         return 1;
204 }
205
206
207 /*
208  * If a proxy has a match function, call that to do extended packet
209  * matching.
210  */
211 int appr_match(fin, nat)
212 fr_info_t *fin;
213 nat_t *nat;
214 {
215         aproxy_t *apr;
216         ipnat_t *ipn;
217
218         ipn = nat->nat_ptr;
219         if (ipn == NULL)
220                 return -1;
221         apr = ipn->in_apr;
222         if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
223             (nat->nat_aps == NULL))
224                 return -1;
225         if (apr->apr_match != NULL)
226                 if ((*apr->apr_match)(fin, nat->nat_aps, nat) != 0)
227                         return -1;
228         return 0;
229 }
230
231
232 /*
233  * Allocate a new application proxy structure and fill it in with the
234  * relevant details.  call the init function once complete, prior to
235  * returning.
236  */
237 int appr_new(fin, ip, nat)
238 fr_info_t *fin;
239 ip_t *ip;
240 nat_t *nat;
241 {
242         register ap_session_t *aps;
243         aproxy_t *apr;
244
245         if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL))
246                 return -1;
247
248         apr = nat->nat_ptr->in_apr;
249
250         if (!apr || (apr->apr_flags & APR_DELETE) || (ip->ip_p != apr->apr_p))
251                 return -1;
252
253         KMALLOC(aps, ap_session_t *);
254         if (!aps)
255                 return -1;
256         bzero((char *)aps, sizeof(*aps));
257         aps->aps_p = ip->ip_p;
258         aps->aps_data = NULL;
259         aps->aps_apr = apr;
260         aps->aps_psiz = 0;
261         if (apr->apr_new != NULL)
262                 if ((*apr->apr_new)(fin, ip, aps, nat) == -1) {
263                         if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
264                                 KFREES(aps->aps_data, aps->aps_psiz);
265                         }
266                         KFREE(aps);
267                         return -1;
268                 }
269         aps->aps_nat = nat;
270         aps->aps_next = ap_sess_list;
271         ap_sess_list = aps;
272         nat->nat_aps = aps;
273
274         return 0;
275 }
276
277
278 /*
279  * check to see if a packet should be passed through an active proxy routine
280  * if one has been setup for it.
281  */
282 int appr_check(ip, fin, nat)
283 ip_t *ip;
284 fr_info_t *fin;
285 nat_t *nat;
286 {
287 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
288         mb_t *m = fin->fin_qfm;
289         int dosum = 1;
290 #endif
291         tcphdr_t *tcp = NULL;
292         ap_session_t *aps;
293         aproxy_t *apr;
294         u_32_t sum;
295         short rv;
296         int err;
297
298         aps = nat->nat_aps;
299         if ((aps != NULL) && (aps->aps_p == ip->ip_p)) {
300                 if (ip->ip_p == IPPROTO_TCP) {
301                         tcp = (tcphdr_t *)fin->fin_dp;
302                         /*
303                          * verify that the checksum is correct.  If not, then
304                          * don't do anything with this packet.
305                          */
306 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
307                         if (dohwcksum && (m->b_ick_flag == ICK_VALID)) {
308                                 sum = tcp->th_sum;
309                                 dosum = 0;
310                         }
311                         if (dosum)
312                                 sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
313 #else
314                         sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
315 #endif
316                         if (sum != tcp->th_sum) {
317                                 frstats[fin->fin_out].fr_tcpbad++;
318                                 return -1;
319                         }
320                 }
321
322                 apr = aps->aps_apr;
323                 err = 0;
324                 if (fin->fin_out != 0) {
325                         if (apr->apr_outpkt != NULL)
326                                 err = (*apr->apr_outpkt)(fin, ip, aps, nat);
327                 } else {
328                         if (apr->apr_inpkt != NULL)
329                                 err = (*apr->apr_inpkt)(fin, ip, aps, nat);
330                 }
331
332                 rv = APR_EXIT(err);
333                 if (rv == 1)
334                         return -1;
335                 if (rv == 2) {
336                         appr_free(apr);
337                         nat->nat_aps = NULL;
338                         return -1;
339                 }
340
341                 if (tcp != NULL) {
342                         err = appr_fixseqack(fin, ip, aps, APR_INC(err));
343 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
344                         if (dosum)
345                                 tcp->th_sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
346 #else
347                         tcp->th_sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
348 #endif
349                 }
350                 aps->aps_bytes += ip->ip_len;
351                 aps->aps_pkts++;
352                 return 1;
353         }
354         return 0;
355 }
356
357
358 /*
359  * Search for an proxy by the protocol it is being used with and its name.
360  */
361 aproxy_t *appr_lookup(pr, name)
362 u_int pr;
363 char *name;
364 {
365         aproxy_t *ap;
366
367         for (ap = ap_proxies; ap->apr_p; ap++)
368                 if ((ap->apr_p == pr) &&
369                     !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
370                         ap->apr_ref++;
371                         return ap;
372                 }
373
374         for (ap = ap_proxylist; ap; ap = ap->apr_next)
375                 if ((ap->apr_p == pr) &&
376                     !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
377                         ap->apr_ref++;
378                         return ap;
379                 }
380         return NULL;
381 }
382
383
384 void appr_free(ap)
385 aproxy_t *ap;
386 {
387         ap->apr_ref--;
388 }
389
390
391 void aps_free(aps)
392 ap_session_t *aps;
393 {
394         ap_session_t *a, **ap;
395         aproxy_t *apr;
396
397         if (!aps)
398                 return;
399
400         for (ap = &ap_sess_list; (a = *ap); ap = &a->aps_next)
401                 if (a == aps) {
402                         *ap = a->aps_next;
403                         break;
404                 }
405
406         apr = aps->aps_apr;
407         if ((apr != NULL) && (apr->apr_del != NULL))
408                 (*apr->apr_del)(aps);
409  
410         if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
411                 KFREES(aps->aps_data, aps->aps_psiz);
412         KFREE(aps);
413 }
414
415
416 static int appr_fixseqack(fin, ip, aps, inc)
417 fr_info_t *fin;
418 ip_t *ip;
419 ap_session_t *aps;
420 int inc;
421 {
422         int sel, ch = 0, out, nlen;
423         u_32_t seq1, seq2;
424         tcphdr_t *tcp;
425         short inc2;
426
427         tcp = (tcphdr_t *)fin->fin_dp;
428         out = fin->fin_out;
429         nlen = ip->ip_len;
430         nlen -= (ip->ip_hl << 2) + (tcp->th_off << 2);
431         inc2 = inc;
432         inc = (int)inc2;
433
434         if (out != 0) {
435                 seq1 = (u_32_t)ntohl(tcp->th_seq);
436                 sel = aps->aps_sel[out];
437
438                 /* switch to other set ? */
439                 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
440                     (seq1 > aps->aps_seqmin[!sel]))
441                         sel = aps->aps_sel[out] = !sel;
442
443                 if (aps->aps_seqoff[sel]) {
444                         seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
445                         if (seq1 > seq2) {
446                                 seq2 = aps->aps_seqoff[sel];
447                                 seq1 += seq2;
448                                 tcp->th_seq = htonl(seq1);
449                                 ch = 1;
450                         }
451                 }
452
453                 if (inc && (seq1 > aps->aps_seqmin[!sel])) {
454                         aps->aps_seqmin[!sel] = seq1 + nlen - 1;
455                         aps->aps_seqoff[!sel] = aps->aps_seqoff[sel] + inc;
456                 }
457
458                 /***/
459
460                 seq1 = ntohl(tcp->th_ack);
461                 sel = aps->aps_sel[1 - out];
462
463                 /* switch to other set ? */
464                 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
465                     (seq1 > aps->aps_ackmin[!sel]))
466                         sel = aps->aps_sel[1 - out] = !sel;
467
468                 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
469                         seq2 = aps->aps_ackoff[sel];
470                         tcp->th_ack = htonl(seq1 - seq2);
471                         ch = 1;
472                 }
473         } else {
474                 seq1 = ntohl(tcp->th_seq);
475                 sel = aps->aps_sel[out];
476
477                 /* switch to other set ? */
478                 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
479                     (seq1 > aps->aps_ackmin[!sel]))
480                         sel = aps->aps_sel[out] = !sel;
481
482                 if (aps->aps_ackoff[sel]) {
483                         seq2 = aps->aps_ackmin[sel] -
484                                aps->aps_ackoff[sel];
485                         if (seq1 > seq2) {
486                                 seq2 = aps->aps_ackoff[sel];
487                                 seq1 += seq2;
488                                 tcp->th_seq = htonl(seq1);
489                                 ch = 1;
490                         }
491                 }
492
493                 if (inc && (seq1 > aps->aps_ackmin[!sel])) {
494                         aps->aps_ackmin[!sel] = seq1 + nlen - 1;
495                         aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
496                 }
497
498                 /***/
499
500                 seq1 = ntohl(tcp->th_ack);
501                 sel = aps->aps_sel[1 - out];
502
503                 /* switch to other set ? */
504                 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
505                     (seq1 > aps->aps_seqmin[!sel]))
506                         sel = aps->aps_sel[1 - out] = !sel;
507
508                 if (aps->aps_seqoff[sel] && (seq1 > aps->aps_seqmin[sel])) {
509                         seq2 = aps->aps_seqoff[sel];
510                         tcp->th_ack = htonl(seq1 - seq2);
511                         ch = 1;
512                 }
513         }
514         return ch ? 2 : 0;
515 }
516
517
518 /*
519  * Initialise hook for kernel application proxies.
520  * Call the initialise routine for all the compiled in kernel proxies.
521  */
522 int appr_init()
523 {
524         aproxy_t *ap;
525         int err = 0;
526
527         for (ap = ap_proxies; ap->apr_p; ap++) {
528                 err = (*ap->apr_init)();
529                 if (err != 0)
530                         break;
531         }
532         return err;
533 }
534
535
536 /*
537  * Unload hook for kernel application proxies.
538  * Call the finialise routine for all the compiled in kernel proxies.
539  */
540 void appr_unload()
541 {
542         aproxy_t *ap;
543
544         for (ap = ap_proxies; ap->apr_p; ap++)
545                 if (ap->apr_fini)
546                         (*ap->apr_fini)();
547         for (ap = ap_proxylist; ap; ap = ap->apr_next)
548                 if (ap->apr_fini)
549                         (*ap->apr_fini)();
550 }