2 * Copyright (C) 1997-2002 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
7 #if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL)
12 # include <sys/ptimers.h>
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
19 #if !defined(__FreeBSD_version)
20 # include <sys/ioctl.h>
22 #include <sys/fcntl.h>
23 #if !defined(_KERNEL) && !defined(KERNEL)
29 # include <sys/protosw.h>
31 #include <sys/socket.h>
34 # include <sys/systm.h>
36 # include <linux/string.h>
39 #if !defined(__SVR4) && !defined(__svr4__)
41 # include <sys/mbuf.h>
44 # include <sys/byteorder.h>
46 # include <sys/dditypes.h>
48 # include <sys/stream.h>
49 # include <sys/kmem.h>
52 # include <sys/queue.h>
58 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
63 # include <netinet/ip_var.h>
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>
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$";
83 #if defined(_KERNEL) && (SOLARIS || defined(__sgi))
84 extern KRWLOCK_T ipf_nat, ipf_state;
88 #define MIN(a,b) (((a)<(b))?(a):(b))
91 static int appr_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int ));
94 #define AP_SESS_SIZE 53
96 #include "netinet/ip_ftp_pxy.c"
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"
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[] = {
109 { NULL, "ftp", (char)IPPROTO_TCP, 0, 0, ippr_ftp_init, NULL,
110 ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL },
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 },
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 },
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,
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 },
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 },
135 { NULL, "", '\0', 0, 0, NULL, NULL, NULL }
140 * Dynamically add a new kernel proxy. Ensure that it is unique in the
141 * collection compiled in and dynamically added.
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)))
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)))
159 ap->apr_next = ap_proxylist;
161 return (*ap->apr_init)();
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.
175 for (app = &ap_proxylist; (a = *app); app = &a->apr_next)
177 a->apr_flags |= APR_DELETE;
179 if (ap->apr_ref != 0)
188 * Return 1 if the packet is a good match against a proxy, else 0.
190 int appr_ok(ip, tcp, nat)
195 aproxy_t *apr = nat->in_apr;
196 u_short dport = nat->in_dport;
198 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
199 (ip->ip_p != apr->apr_p))
201 if (((tcp != NULL) && (tcp->th_dport != dport)) || (!tcp && dport))
208 * If a proxy has a match function, call that to do extended packet
211 int appr_match(fin, nat)
222 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
223 (nat->nat_aps == NULL))
225 if (apr->apr_match != NULL)
226 if ((*apr->apr_match)(fin, nat->nat_aps, nat) != 0)
233 * Allocate a new application proxy structure and fill it in with the
234 * relevant details. call the init function once complete, prior to
237 int appr_new(fin, ip, nat)
242 register ap_session_t *aps;
245 if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL))
248 apr = nat->nat_ptr->in_apr;
250 if (!apr || (apr->apr_flags & APR_DELETE) || (ip->ip_p != apr->apr_p))
253 KMALLOC(aps, ap_session_t *);
256 bzero((char *)aps, sizeof(*aps));
257 aps->aps_p = ip->ip_p;
258 aps->aps_data = NULL;
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);
270 aps->aps_next = ap_sess_list;
279 * check to see if a packet should be passed through an active proxy routine
280 * if one has been setup for it.
282 int appr_check(ip, fin, nat)
287 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
288 mb_t *m = fin->fin_qfm;
291 tcphdr_t *tcp = NULL;
299 if ((aps != NULL) && (aps->aps_p == ip->ip_p)) {
300 if (ip->ip_p == IPPROTO_TCP) {
301 tcp = (tcphdr_t *)fin->fin_dp;
303 * verify that the checksum is correct. If not, then
304 * don't do anything with this packet.
306 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
307 if (dohwcksum && (m->b_ick_flag == ICK_VALID)) {
312 sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
314 sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
316 if (sum != tcp->th_sum) {
317 frstats[fin->fin_out].fr_tcpbad++;
324 if (fin->fin_out != 0) {
325 if (apr->apr_outpkt != NULL)
326 err = (*apr->apr_outpkt)(fin, ip, aps, nat);
328 if (apr->apr_inpkt != NULL)
329 err = (*apr->apr_inpkt)(fin, ip, aps, nat);
342 err = appr_fixseqack(fin, ip, aps, APR_INC(err));
343 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
345 tcp->th_sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
347 tcp->th_sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
350 aps->aps_bytes += ip->ip_len;
359 * Search for an proxy by the protocol it is being used with and its name.
361 aproxy_t *appr_lookup(pr, name)
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))) {
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))) {
394 ap_session_t *a, **ap;
400 for (ap = &ap_sess_list; (a = *ap); ap = &a->aps_next)
407 if ((apr != NULL) && (apr->apr_del != NULL))
408 (*apr->apr_del)(aps);
410 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
411 KFREES(aps->aps_data, aps->aps_psiz);
416 static int appr_fixseqack(fin, ip, aps, inc)
422 int sel, ch = 0, out, nlen;
427 tcp = (tcphdr_t *)fin->fin_dp;
430 nlen -= (ip->ip_hl << 2) + (tcp->th_off << 2);
435 seq1 = (u_32_t)ntohl(tcp->th_seq);
436 sel = aps->aps_sel[out];
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;
443 if (aps->aps_seqoff[sel]) {
444 seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
446 seq2 = aps->aps_seqoff[sel];
448 tcp->th_seq = htonl(seq1);
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;
460 seq1 = ntohl(tcp->th_ack);
461 sel = aps->aps_sel[1 - out];
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;
468 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
469 seq2 = aps->aps_ackoff[sel];
470 tcp->th_ack = htonl(seq1 - seq2);
474 seq1 = ntohl(tcp->th_seq);
475 sel = aps->aps_sel[out];
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;
482 if (aps->aps_ackoff[sel]) {
483 seq2 = aps->aps_ackmin[sel] -
484 aps->aps_ackoff[sel];
486 seq2 = aps->aps_ackoff[sel];
488 tcp->th_seq = htonl(seq1);
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;
500 seq1 = ntohl(tcp->th_ack);
501 sel = aps->aps_sel[1 - out];
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;
508 if (aps->aps_seqoff[sel] && (seq1 > aps->aps_seqmin[sel])) {
509 seq2 = aps->aps_seqoff[sel];
510 tcp->th_ack = htonl(seq1 - seq2);
519 * Initialise hook for kernel application proxies.
520 * Call the initialise routine for all the compiled in kernel proxies.
527 for (ap = ap_proxies; ap->apr_p; ap++) {
528 err = (*ap->apr_init)();
537 * Unload hook for kernel application proxies.
538 * Call the finialise routine for all the compiled in kernel proxies.
544 for (ap = ap_proxies; ap->apr_p; ap++)
547 for (ap = ap_proxylist; ap; ap = ap->apr_next)