2 * Copyright (C) 1997-2002 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 #if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL)
10 #if defined(__sgi) && (IRIX > 602)
11 # include <sys/ptimers.h>
13 #include <sys/errno.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
18 #if !defined(__FreeBSD_version)
19 # include <sys/ioctl.h>
21 #include <sys/fcntl.h>
22 #if !defined(_KERNEL) && !defined(KERNEL)
28 # include <sys/protosw.h>
30 #include <sys/socket.h>
33 # include <sys/systm.h>
35 # include <linux/string.h>
38 #if !defined(__SVR4) && !defined(__svr4__)
40 # include <sys/mbuf.h>
43 # include <sys/byteorder.h>
45 # include <sys/dditypes.h>
47 # include <sys/stream.h>
48 # include <sys/kmem.h>
51 # include <sys/queue.h>
57 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
62 # include <netinet/ip_var.h>
64 #include <netinet/tcp.h>
65 #include <netinet/udp.h>
66 #include <netinet/ip_icmp.h>
67 #include "netinet/ip_compat.h"
68 #include <netinet/tcpip.h>
69 #include "netinet/ip_fil.h"
70 #include "netinet/ip_nat.h"
71 #include "netinet/ip_state.h"
72 #include "netinet/ip_proxy.h"
73 #if (__FreeBSD_version >= 300000)
74 # include <sys/malloc.h>
78 /*static const char rcsid[] = "@(#)$Id: ip_proxy.c,v 2.9.2.24 2002/08/28 12:45:51 darrenr Exp $";*/
79 static const char rcsid[] = "@(#)$FreeBSD: src/sys/contrib/ipfilter/netinet/ip_proxy.c,v 1.11.2.5 2003/03/01 03:55:54 darrenr Exp $";
82 #if defined(_KERNEL) && (SOLARIS || defined(__sgi))
83 extern KRWLOCK_T ipf_nat, ipf_state;
87 #define MIN(a,b) (((a)<(b))?(a):(b))
90 static int appr_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int ));
95 #define AP_SESS_SIZE 53
97 #include "netinet/ip_ftp_pxy.c"
99 #include "netinet/ip_rcmd_pxy.c"
100 #include "netinet/ip_raudio_pxy.c"
101 #include "netinet/ip_netbios_pxy.c"
102 #include "netinet/ip_h323_pxy.c"
104 #include "netinet/ip_ipsec_pxy.c"
106 ap_session_t *ap_sess_tab[AP_SESS_SIZE];
107 ap_session_t *ap_sess_list = NULL;
108 aproxy_t *ap_proxylist = NULL;
109 aproxy_t ap_proxies[] = {
111 { NULL, "ftp", (char)IPPROTO_TCP, 0, 0, ippr_ftp_init, NULL,
112 ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL },
114 #ifdef IPF_RCMD_PROXY
115 { NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, ippr_rcmd_init, NULL,
116 ippr_rcmd_new, NULL, NULL, ippr_rcmd_out, NULL },
118 #ifdef IPF_RAUDIO_PROXY
119 { NULL, "raudio", (char)IPPROTO_TCP, 0, 0, ippr_raudio_init, NULL,
120 ippr_raudio_new, NULL, ippr_raudio_in, ippr_raudio_out, NULL },
122 #ifdef IPF_IPSEC_PROXY
123 { NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, ippr_ipsec_init, NULL,
124 ippr_ipsec_new, ippr_ipsec_del, NULL, ippr_ipsec_out,
127 #ifdef IPF_NETBIOS_PROXY
128 { NULL, "netbios", (char)IPPROTO_UDP, 0, 0, ippr_netbios_init, NULL,
129 NULL, NULL, NULL, ippr_netbios_out, NULL },
131 #ifdef IPF_H323_PROXY
132 { NULL, "h323", (char)IPPROTO_TCP, 0, 0, ippr_h323_init, NULL,
133 ippr_h323_new, ippr_h323_del, ippr_h323_in, ippr_h323_out, NULL },
134 { NULL, "h245", (char)IPPROTO_TCP, 0, 0, ippr_h245_init, NULL,
135 ippr_h245_new, NULL, NULL, ippr_h245_out, NULL },
137 { NULL, "", '\0', 0, 0, NULL, NULL, NULL }
142 * Dynamically add a new kernel proxy. Ensure that it is unique in the
143 * collection compiled in and dynamically added.
150 for (a = ap_proxies; a->apr_p; a++)
151 if ((a->apr_p == ap->apr_p) &&
152 !strncmp(a->apr_label, ap->apr_label,
153 sizeof(ap->apr_label)))
156 for (a = ap_proxylist; a && a->apr_p; a = a->apr_next)
157 if ((a->apr_p == ap->apr_p) &&
158 !strncmp(a->apr_label, ap->apr_label,
159 sizeof(ap->apr_label)))
161 ap->apr_next = ap_proxylist;
163 return (*ap->apr_init)();
168 * Delete a proxy that has been added dynamically from those available.
169 * If it is in use, return 1 (do not destroy NOW), not in use 0 or -1
170 * if it cannot be matched.
177 for (app = &ap_proxylist; (a = *app); app = &a->apr_next)
179 a->apr_flags |= APR_DELETE;
181 if (ap->apr_ref != 0)
190 * Return 1 if the packet is a good match against a proxy, else 0.
192 int appr_ok(ip, tcp, nat)
197 aproxy_t *apr = nat->in_apr;
198 u_short dport = nat->in_dport;
200 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
201 (ip->ip_p != apr->apr_p))
203 if (((tcp != NULL) && (tcp->th_dport != dport)) || (!tcp && dport))
210 * If a proxy has a match function, call that to do extended packet
213 int appr_match(fin, nat)
224 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
225 (nat->nat_aps == NULL))
227 if (apr->apr_match != NULL)
228 if ((*apr->apr_match)(fin, nat->nat_aps, nat) != 0)
235 * Allocate a new application proxy structure and fill it in with the
236 * relevant details. call the init function once complete, prior to
239 int appr_new(fin, ip, nat)
244 register ap_session_t *aps;
247 if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL))
250 apr = nat->nat_ptr->in_apr;
252 if (!apr || (apr->apr_flags & APR_DELETE) || (ip->ip_p != apr->apr_p))
255 KMALLOC(aps, ap_session_t *);
258 bzero((char *)aps, sizeof(*aps));
259 aps->aps_p = ip->ip_p;
260 aps->aps_data = NULL;
263 if (apr->apr_new != NULL)
264 if ((*apr->apr_new)(fin, ip, aps, nat) == -1) {
265 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
266 KFREES(aps->aps_data, aps->aps_psiz);
272 aps->aps_next = ap_sess_list;
281 * check to see if a packet should be passed through an active proxy routine
282 * if one has been setup for it.
284 int appr_check(ip, fin, nat)
289 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
290 mb_t *m = fin->fin_qfm;
293 tcphdr_t *tcp = NULL;
301 if ((aps != NULL) && (aps->aps_p == ip->ip_p)) {
302 if (ip->ip_p == IPPROTO_TCP) {
303 tcp = (tcphdr_t *)fin->fin_dp;
305 * verify that the checksum is correct. If not, then
306 * don't do anything with this packet.
308 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
309 if (dohwcksum && (m->b_ick_flag == ICK_VALID)) {
314 sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
316 sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
318 if (sum != tcp->th_sum) {
319 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
320 printf("proxy tcp checksum failure\n");
322 frstats[fin->fin_out].fr_tcpbad++;
327 * Don't bother the proxy with these...or in fact,
328 * should we free up proxy stuff when seen?
330 if ((tcp->th_flags & TH_RST) != 0)
336 if (fin->fin_out != 0) {
337 if (apr->apr_outpkt != NULL)
338 err = (*apr->apr_outpkt)(fin, ip, aps, nat);
340 if (apr->apr_inpkt != NULL)
341 err = (*apr->apr_inpkt)(fin, ip, aps, nat);
346 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
347 printf("proxy says bad packet received\n");
352 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
353 printf("proxy says free app proxy data\n");
361 err = appr_fixseqack(fin, ip, aps, APR_INC(err));
362 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
364 tcp->th_sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
366 tcp->th_sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
369 aps->aps_bytes += ip->ip_len;
378 * Search for an proxy by the protocol it is being used with and its name.
380 aproxy_t *appr_lookup(pr, name)
386 for (ap = ap_proxies; ap->apr_p; ap++)
387 if ((ap->apr_p == pr) &&
388 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
393 for (ap = ap_proxylist; ap; ap = ap->apr_next)
394 if ((ap->apr_p == pr) &&
395 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
413 ap_session_t *a, **ap;
419 for (ap = &ap_sess_list; (a = *ap); ap = &a->aps_next)
426 if ((apr != NULL) && (apr->apr_del != NULL))
427 (*apr->apr_del)(aps);
429 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
430 KFREES(aps->aps_data, aps->aps_psiz);
436 * returns 2 if ack or seq number in TCP header is changed, returns 0 otherwise
438 static int appr_fixseqack(fin, ip, aps, inc)
444 int sel, ch = 0, out, nlen;
449 tcp = (tcphdr_t *)fin->fin_dp;
452 * ip_len has already been adjusted by 'inc'.
455 nlen -= (ip->ip_hl << 2) + (tcp->th_off << 2);
461 seq1 = (u_32_t)ntohl(tcp->th_seq);
462 sel = aps->aps_sel[out];
464 /* switch to other set ? */
465 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
466 (seq1 > aps->aps_seqmin[!sel])) {
468 printf("proxy out switch set seq %d -> %d %x > %x\n",
469 sel, !sel, seq1, aps->aps_seqmin[!sel]);
471 sel = aps->aps_sel[out] = !sel;
474 if (aps->aps_seqoff[sel]) {
475 seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
477 seq2 = aps->aps_seqoff[sel];
479 tcp->th_seq = htonl(seq1);
484 if (inc && (seq1 > aps->aps_seqmin[!sel])) {
485 aps->aps_seqmin[sel] = seq1 + nlen - 1;
486 aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
488 printf("proxy seq set %d at %x to %d + %d\n", sel,
489 aps->aps_seqmin[sel], aps->aps_seqoff[sel],
496 seq1 = ntohl(tcp->th_ack);
497 sel = aps->aps_sel[1 - out];
499 /* switch to other set ? */
500 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
501 (seq1 > aps->aps_ackmin[!sel])) {
503 printf("proxy out switch set ack %d -> %d %x > %x\n",
504 sel, !sel, seq1, aps->aps_ackmin[!sel]);
506 sel = aps->aps_sel[1 - out] = !sel;
509 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
510 seq2 = aps->aps_ackoff[sel];
511 tcp->th_ack = htonl(seq1 - seq2);
515 seq1 = ntohl(tcp->th_seq);
516 sel = aps->aps_sel[out];
518 /* switch to other set ? */
519 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
520 (seq1 > aps->aps_ackmin[!sel])) {
522 printf("proxy in switch set ack %d -> %d %x > %x\n",
523 sel, !sel, seq1, aps->aps_ackmin[!sel]);
525 sel = aps->aps_sel[out] = !sel;
528 if (aps->aps_ackoff[sel]) {
529 seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
531 seq2 = aps->aps_ackoff[sel];
533 tcp->th_seq = htonl(seq1);
538 if (inc && (seq1 > aps->aps_ackmin[!sel])) {
539 aps->aps_ackmin[!sel] = seq1 + nlen - 1;
540 aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
542 printf("proxy ack set %d at %x to %d + %d\n", !sel,
543 aps->aps_seqmin[!sel], aps->aps_seqoff[sel],
550 seq1 = ntohl(tcp->th_ack);
551 sel = aps->aps_sel[1 - out];
553 /* switch to other set ? */
554 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
555 (seq1 > aps->aps_seqmin[!sel])) {
557 printf("proxy in switch set seq %d -> %d %x > %x\n",
558 sel, !sel, seq1, aps->aps_seqmin[!sel]);
560 sel = aps->aps_sel[1 - out] = !sel;
563 if (aps->aps_seqoff[sel] != 0) {
565 printf("sel %d seqoff %d seq1 %x seqmin %x\n", sel,
566 aps->aps_seqoff[sel], seq1,
567 aps->aps_seqmin[sel]);
569 if (seq1 > aps->aps_seqmin[sel]) {
570 seq2 = aps->aps_seqoff[sel];
571 tcp->th_ack = htonl(seq1 - seq2);
577 printf("appr_fixseqack: seq %x ack %x\n", ntohl(tcp->th_seq),
585 * Initialise hook for kernel application proxies.
586 * Call the initialise routine for all the compiled in kernel proxies.
593 for (ap = ap_proxies; ap->apr_p; ap++) {
594 err = (*ap->apr_init)();
603 * Unload hook for kernel application proxies.
604 * Call the finialise routine for all the compiled in kernel proxies.
610 for (ap = ap_proxies; ap->apr_p; ap++)
613 for (ap = ap_proxylist; ap; ap = ap->apr_next)