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.26 2002/12/06 11:40:23 darrenr Exp $";
81 #if defined(_KERNEL) && (SOLARIS || defined(__sgi))
82 extern KRWLOCK_T ipf_nat, ipf_state;
86 #define MIN(a,b) (((a)<(b))?(a):(b))
89 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_h323_pxy.c"
103 #include "netinet/ip_ipsec_pxy.c"
105 ap_session_t *ap_sess_tab[AP_SESS_SIZE];
106 ap_session_t *ap_sess_list = NULL;
107 aproxy_t *ap_proxylist = NULL;
108 aproxy_t ap_proxies[] = {
110 { NULL, "ftp", (char)IPPROTO_TCP, 0, 0, ippr_ftp_init, NULL,
111 ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL },
113 #ifdef IPF_RCMD_PROXY
114 { NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, ippr_rcmd_init, NULL,
115 ippr_rcmd_new, NULL, NULL, ippr_rcmd_out, NULL },
117 #ifdef IPF_RAUDIO_PROXY
118 { NULL, "raudio", (char)IPPROTO_TCP, 0, 0, ippr_raudio_init, NULL,
119 ippr_raudio_new, NULL, ippr_raudio_in, ippr_raudio_out, NULL },
121 #ifdef IPF_IPSEC_PROXY
122 { NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, ippr_ipsec_init, NULL,
123 ippr_ipsec_new, ippr_ipsec_del, NULL, ippr_ipsec_out,
126 #ifdef IPF_NETBIOS_PROXY
127 { NULL, "netbios", (char)IPPROTO_UDP, 0, 0, ippr_netbios_init, NULL,
128 NULL, NULL, NULL, ippr_netbios_out, NULL },
130 #ifdef IPF_H323_PROXY
131 { NULL, "h323", (char)IPPROTO_TCP, 0, 0, ippr_h323_init, NULL,
132 ippr_h323_new, ippr_h323_del, ippr_h323_in, ippr_h323_out, NULL },
133 { NULL, "h245", (char)IPPROTO_TCP, 0, 0, ippr_h245_init, NULL,
134 ippr_h245_new, NULL, NULL, ippr_h245_out, NULL },
136 { NULL, "", '\0', 0, 0, NULL, NULL, NULL }
141 * Dynamically add a new kernel proxy. Ensure that it is unique in the
142 * collection compiled in and dynamically added.
149 for (a = ap_proxies; a->apr_p; a++)
150 if ((a->apr_p == ap->apr_p) &&
151 !strncmp(a->apr_label, ap->apr_label,
152 sizeof(ap->apr_label)))
155 for (a = ap_proxylist; a && a->apr_p; a = a->apr_next)
156 if ((a->apr_p == ap->apr_p) &&
157 !strncmp(a->apr_label, ap->apr_label,
158 sizeof(ap->apr_label)))
160 ap->apr_next = ap_proxylist;
162 return (*ap->apr_init)();
167 * Delete a proxy that has been added dynamically from those available.
168 * If it is in use, return 1 (do not destroy NOW), not in use 0 or -1
169 * if it cannot be matched.
176 for (app = &ap_proxylist; (a = *app); app = &a->apr_next)
178 a->apr_flags |= APR_DELETE;
180 if (ap->apr_ref != 0)
189 * Return 1 if the packet is a good match against a proxy, else 0.
191 int appr_ok(ip, tcp, nat)
196 aproxy_t *apr = nat->in_apr;
197 u_short dport = nat->in_dport;
199 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
200 (ip->ip_p != apr->apr_p))
202 if (((tcp != NULL) && (tcp->th_dport != dport)) || (!tcp && dport))
209 * If a proxy has a match function, call that to do extended packet
212 int appr_match(fin, nat)
223 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
224 (nat->nat_aps == NULL))
226 if (apr->apr_match != NULL)
227 if ((*apr->apr_match)(fin, nat->nat_aps, nat) != 0)
234 * Allocate a new application proxy structure and fill it in with the
235 * relevant details. call the init function once complete, prior to
238 int appr_new(fin, ip, nat)
243 register ap_session_t *aps;
246 if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL))
249 apr = nat->nat_ptr->in_apr;
251 if (!apr || (apr->apr_flags & APR_DELETE) || (ip->ip_p != apr->apr_p))
254 KMALLOC(aps, ap_session_t *);
257 bzero((char *)aps, sizeof(*aps));
258 aps->aps_p = ip->ip_p;
259 aps->aps_data = NULL;
262 if (apr->apr_new != NULL)
263 if ((*apr->apr_new)(fin, ip, aps, nat) == -1) {
264 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
265 KFREES(aps->aps_data, aps->aps_psiz);
271 aps->aps_next = ap_sess_list;
280 * check to see if a packet should be passed through an active proxy routine
281 * if one has been setup for it.
283 int appr_check(ip, fin, nat)
288 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
289 mb_t *m = fin->fin_qfm;
292 tcphdr_t *tcp = NULL;
300 if ((aps != NULL) && (aps->aps_p == ip->ip_p)) {
301 if (ip->ip_p == IPPROTO_TCP) {
302 tcp = (tcphdr_t *)fin->fin_dp;
304 * verify that the checksum is correct. If not, then
305 * don't do anything with this packet.
307 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
308 if (dohwcksum && (m->b_ick_flag == ICK_VALID)) {
313 sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
315 sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
317 if (sum != tcp->th_sum) {
318 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
319 printf("proxy tcp checksum failure\n");
321 frstats[fin->fin_out].fr_tcpbad++;
326 * Don't bother the proxy with these...or in fact,
327 * should we free up proxy stuff when seen?
329 if ((tcp->th_flags & TH_RST) != 0)
335 if (fin->fin_out != 0) {
336 if (apr->apr_outpkt != NULL)
337 err = (*apr->apr_outpkt)(fin, ip, aps, nat);
339 if (apr->apr_inpkt != NULL)
340 err = (*apr->apr_inpkt)(fin, ip, aps, nat);
345 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
346 printf("proxy says bad packet received\n");
351 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
352 printf("proxy says free app proxy data\n");
360 err = appr_fixseqack(fin, ip, aps, APR_INC(err));
361 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
363 tcp->th_sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
365 tcp->th_sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
368 aps->aps_bytes += ip->ip_len;
377 * Search for an proxy by the protocol it is being used with and its name.
379 aproxy_t *appr_lookup(pr, name)
385 for (ap = ap_proxies; ap->apr_p; ap++)
386 if ((ap->apr_p == pr) &&
387 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
392 for (ap = ap_proxylist; ap; ap = ap->apr_next)
393 if ((ap->apr_p == pr) &&
394 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
412 ap_session_t *a, **ap;
418 for (ap = &ap_sess_list; (a = *ap); ap = &a->aps_next)
425 if ((apr != NULL) && (apr->apr_del != NULL))
426 (*apr->apr_del)(aps);
428 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
429 KFREES(aps->aps_data, aps->aps_psiz);
435 * returns 2 if ack or seq number in TCP header is changed, returns 0 otherwise
437 static int appr_fixseqack(fin, ip, aps, inc)
443 int sel, ch = 0, out, nlen;
448 tcp = (tcphdr_t *)fin->fin_dp;
451 * ip_len has already been adjusted by 'inc'.
454 nlen -= (ip->ip_hl << 2) + (tcp->th_off << 2);
460 seq1 = (u_32_t)ntohl(tcp->th_seq);
461 sel = aps->aps_sel[out];
463 /* switch to other set ? */
464 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
465 (seq1 > aps->aps_seqmin[!sel])) {
467 printf("proxy out switch set seq %d -> %d %x > %x\n",
468 sel, !sel, seq1, aps->aps_seqmin[!sel]);
470 sel = aps->aps_sel[out] = !sel;
473 if (aps->aps_seqoff[sel]) {
474 seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
476 seq2 = aps->aps_seqoff[sel];
478 tcp->th_seq = htonl(seq1);
483 if (inc && (seq1 > aps->aps_seqmin[!sel])) {
484 aps->aps_seqmin[sel] = seq1 + nlen - 1;
485 aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
487 printf("proxy seq set %d at %x to %d + %d\n", sel,
488 aps->aps_seqmin[sel], aps->aps_seqoff[sel],
495 seq1 = ntohl(tcp->th_ack);
496 sel = aps->aps_sel[1 - out];
498 /* switch to other set ? */
499 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
500 (seq1 > aps->aps_ackmin[!sel])) {
502 printf("proxy out switch set ack %d -> %d %x > %x\n",
503 sel, !sel, seq1, aps->aps_ackmin[!sel]);
505 sel = aps->aps_sel[1 - out] = !sel;
508 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
509 seq2 = aps->aps_ackoff[sel];
510 tcp->th_ack = htonl(seq1 - seq2);
514 seq1 = ntohl(tcp->th_seq);
515 sel = aps->aps_sel[out];
517 /* switch to other set ? */
518 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
519 (seq1 > aps->aps_ackmin[!sel])) {
521 printf("proxy in switch set ack %d -> %d %x > %x\n",
522 sel, !sel, seq1, aps->aps_ackmin[!sel]);
524 sel = aps->aps_sel[out] = !sel;
527 if (aps->aps_ackoff[sel]) {
528 seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
530 seq2 = aps->aps_ackoff[sel];
532 tcp->th_seq = htonl(seq1);
537 if (inc && (seq1 > aps->aps_ackmin[!sel])) {
538 aps->aps_ackmin[!sel] = seq1 + nlen - 1;
539 aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
541 printf("proxy ack set %d at %x to %d + %d\n", !sel,
542 aps->aps_seqmin[!sel], aps->aps_seqoff[sel],
549 seq1 = ntohl(tcp->th_ack);
550 sel = aps->aps_sel[1 - out];
552 /* switch to other set ? */
553 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
554 (seq1 > aps->aps_seqmin[!sel])) {
556 printf("proxy in switch set seq %d -> %d %x > %x\n",
557 sel, !sel, seq1, aps->aps_seqmin[!sel]);
559 sel = aps->aps_sel[1 - out] = !sel;
562 if (aps->aps_seqoff[sel] != 0) {
564 printf("sel %d seqoff %d seq1 %x seqmin %x\n", sel,
565 aps->aps_seqoff[sel], seq1,
566 aps->aps_seqmin[sel]);
568 if (seq1 > aps->aps_seqmin[sel]) {
569 seq2 = aps->aps_seqoff[sel];
570 tcp->th_ack = htonl(seq1 - seq2);
576 printf("appr_fixseqack: seq %x ack %x\n", ntohl(tcp->th_seq),
584 * Initialise hook for kernel application proxies.
585 * Call the initialise routine for all the compiled in kernel proxies.
592 for (ap = ap_proxies; ap->apr_p; ap++) {
593 err = (*ap->apr_init)();
602 * Unload hook for kernel application proxies.
603 * Call the finialise routine for all the compiled in kernel proxies.
609 for (ap = ap_proxies; ap->apr_p; ap++)
612 for (ap = ap_proxylist; ap; ap = ap->apr_next)