2 * Copyright (C) 1997-2002 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * @(#)$Id: ip_proxy.c,v 2.9.2.24 2002/08/28 12:45:51 darrenr Exp $
7 * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_proxy.c,v 1.11.2.5 2003/03/01 03:55:54 darrenr Exp $
8 * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_proxy.c,v 1.7 2004/02/12 22:35:47 joerg Exp $
10 #if (defined(__DragonFly__) || defined(__FreeBSD__)) && defined(KERNEL) && !defined(_KERNEL)
14 #if defined(__sgi) && (IRIX > 602)
15 # include <sys/ptimers.h>
17 #include <sys/errno.h>
18 #include <sys/types.h>
19 #include <sys/param.h>
22 #if !defined(__DragonFly__) && !defined(__FreeBSD_version)
23 # include <sys/ioctl.h>
25 #include <sys/fcntl.h>
26 #if !defined(_KERNEL) && !defined(KERNEL)
32 # include <sys/protosw.h>
34 #include <sys/socket.h>
37 # include <sys/systm.h>
39 # include <linux/string.h>
42 #if !defined(__SVR4) && !defined(__svr4__)
44 # include <sys/mbuf.h>
47 # include <sys/byteorder.h>
49 # include <sys/dditypes.h>
51 # include <sys/stream.h>
52 # include <sys/kmem.h>
54 #if defined(__DragonFly__) || __FreeBSD__ > 2
55 # include <sys/queue.h>
61 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
66 # include <netinet/ip_var.h>
68 #include <netinet/tcp.h>
69 #include <netinet/udp.h>
70 #include <netinet/ip_icmp.h>
71 #include "ip_compat.h"
72 #include <netinet/tcpip.h>
77 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
78 # include <sys/malloc.h>
81 #if defined(_KERNEL) && (SOLARIS || defined(__sgi))
82 extern KRWLOCK_T ipf_nat, ipf_state;
85 static int appr_fixseqack (fr_info_t *, ip_t *, ap_session_t *, int );
90 #define AP_SESS_SIZE 53
92 #include "ip_ftp_pxy.c"
94 #include "ip_rcmd_pxy.c"
95 #include "ip_raudio_pxy.c"
96 #include "ip_netbios_pxy.c"
97 #include "ip_h323_pxy.c"
99 #include "ip_ipsec_pxy.c"
101 ap_session_t *ap_sess_tab[AP_SESS_SIZE];
102 ap_session_t *ap_sess_list = NULL;
103 aproxy_t *ap_proxylist = NULL;
104 aproxy_t ap_proxies[] = {
106 { NULL, "ftp", (char)IPPROTO_TCP, 0, 0, ippr_ftp_init, NULL,
107 ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL },
109 #ifdef IPF_RCMD_PROXY
110 { NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, ippr_rcmd_init, NULL,
111 ippr_rcmd_new, NULL, NULL, ippr_rcmd_out, NULL },
113 #ifdef IPF_RAUDIO_PROXY
114 { NULL, "raudio", (char)IPPROTO_TCP, 0, 0, ippr_raudio_init, NULL,
115 ippr_raudio_new, NULL, ippr_raudio_in, ippr_raudio_out, NULL },
117 #ifdef IPF_IPSEC_PROXY
118 { NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, ippr_ipsec_init, NULL,
119 ippr_ipsec_new, ippr_ipsec_del, NULL, ippr_ipsec_out,
122 #ifdef IPF_NETBIOS_PROXY
123 { NULL, "netbios", (char)IPPROTO_UDP, 0, 0, ippr_netbios_init, NULL,
124 NULL, NULL, NULL, ippr_netbios_out, NULL },
126 #ifdef IPF_H323_PROXY
127 { NULL, "h323", (char)IPPROTO_TCP, 0, 0, ippr_h323_init, NULL,
128 ippr_h323_new, ippr_h323_del, ippr_h323_in, ippr_h323_out, NULL },
129 { NULL, "h245", (char)IPPROTO_TCP, 0, 0, ippr_h245_init, NULL,
130 ippr_h245_new, NULL, NULL, ippr_h245_out, NULL },
132 { NULL, "", '\0', 0, 0, NULL, NULL, NULL }
137 * Dynamically add a new kernel proxy. Ensure that it is unique in the
138 * collection compiled in and dynamically added.
145 for (a = ap_proxies; a->apr_p; a++)
146 if ((a->apr_p == ap->apr_p) &&
147 !strncmp(a->apr_label, ap->apr_label,
148 sizeof(ap->apr_label)))
151 for (a = ap_proxylist; a && a->apr_p; a = a->apr_next)
152 if ((a->apr_p == ap->apr_p) &&
153 !strncmp(a->apr_label, ap->apr_label,
154 sizeof(ap->apr_label)))
156 ap->apr_next = ap_proxylist;
158 return (*ap->apr_init)();
163 * Delete a proxy that has been added dynamically from those available.
164 * If it is in use, return 1 (do not destroy NOW), not in use 0 or -1
165 * if it cannot be matched.
172 for (app = &ap_proxylist; (a = *app); app = &a->apr_next)
174 a->apr_flags |= APR_DELETE;
176 if (ap->apr_ref != 0)
185 * Return 1 if the packet is a good match against a proxy, else 0.
187 int appr_ok(ip, tcp, nat)
192 aproxy_t *apr = nat->in_apr;
193 u_short dport = nat->in_dport;
195 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
196 (ip->ip_p != apr->apr_p))
198 if (((tcp != NULL) && (tcp->th_dport != dport)) || (!tcp && dport))
205 * If a proxy has a match function, call that to do extended packet
208 int appr_match(fin, nat)
219 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
220 (nat->nat_aps == NULL))
222 if (apr->apr_match != NULL)
223 if ((*apr->apr_match)(fin, nat->nat_aps, nat) != 0)
230 * Allocate a new application proxy structure and fill it in with the
231 * relevant details. call the init function once complete, prior to
234 int appr_new(fin, ip, nat)
242 if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL))
245 apr = nat->nat_ptr->in_apr;
247 if (!apr || (apr->apr_flags & APR_DELETE) || (ip->ip_p != apr->apr_p))
250 KMALLOC(aps, ap_session_t *);
253 bzero((char *)aps, sizeof(*aps));
254 aps->aps_p = ip->ip_p;
255 aps->aps_data = NULL;
258 if (apr->apr_new != NULL)
259 if ((*apr->apr_new)(fin, ip, aps, nat) == -1) {
260 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
261 KFREES(aps->aps_data, aps->aps_psiz);
267 aps->aps_next = ap_sess_list;
276 * check to see if a packet should be passed through an active proxy routine
277 * if one has been setup for it.
279 int appr_check(ip, fin, nat)
284 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
285 mb_t *m = fin->fin_qfm;
288 tcphdr_t *tcp = NULL;
296 if ((aps != NULL) && (aps->aps_p == ip->ip_p)) {
297 if (ip->ip_p == IPPROTO_TCP) {
298 tcp = (tcphdr_t *)fin->fin_dp;
300 * verify that the checksum is correct. If not, then
301 * don't do anything with this packet.
303 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
304 if (dohwcksum && (m->b_ick_flag == ICK_VALID)) {
309 sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
311 sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
313 if (sum != tcp->th_sum) {
314 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
315 printf("proxy tcp checksum failure\n");
317 frstats[fin->fin_out].fr_tcpbad++;
322 * Don't bother the proxy with these...or in fact,
323 * should we free up proxy stuff when seen?
325 if ((tcp->th_flags & TH_RST) != 0)
331 if (fin->fin_out != 0) {
332 if (apr->apr_outpkt != NULL)
333 err = (*apr->apr_outpkt)(fin, ip, aps, nat);
335 if (apr->apr_inpkt != NULL)
336 err = (*apr->apr_inpkt)(fin, ip, aps, nat);
341 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
342 printf("proxy says bad packet received\n");
347 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
348 printf("proxy says free app proxy data\n");
356 err = appr_fixseqack(fin, ip, aps, APR_INC(err));
357 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
359 tcp->th_sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
361 tcp->th_sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
364 aps->aps_bytes += ip->ip_len;
373 * Search for an proxy by the protocol it is being used with and its name.
375 aproxy_t *appr_lookup(pr, name)
381 for (ap = ap_proxies; ap->apr_p; ap++)
382 if ((ap->apr_p == pr) &&
383 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
388 for (ap = ap_proxylist; ap; ap = ap->apr_next)
389 if ((ap->apr_p == pr) &&
390 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
408 ap_session_t *a, **ap;
414 for (ap = &ap_sess_list; (a = *ap); ap = &a->aps_next)
421 if ((apr != NULL) && (apr->apr_del != NULL))
422 (*apr->apr_del)(aps);
424 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
425 KFREES(aps->aps_data, aps->aps_psiz);
431 * returns 2 if ack or seq number in TCP header is changed, returns 0 otherwise
433 static int appr_fixseqack(fin, ip, aps, inc)
439 int sel, ch = 0, out, nlen;
444 tcp = (tcphdr_t *)fin->fin_dp;
447 * ip_len has already been adjusted by 'inc'.
450 nlen -= (ip->ip_hl << 2) + (tcp->th_off << 2);
456 seq1 = (u_32_t)ntohl(tcp->th_seq);
457 sel = aps->aps_sel[out];
459 /* switch to other set ? */
460 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
461 (seq1 > aps->aps_seqmin[!sel])) {
463 printf("proxy out switch set seq %d -> %d %x > %x\n",
464 sel, !sel, seq1, aps->aps_seqmin[!sel]);
466 sel = aps->aps_sel[out] = !sel;
469 if (aps->aps_seqoff[sel]) {
470 seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
472 seq2 = aps->aps_seqoff[sel];
474 tcp->th_seq = htonl(seq1);
479 if (inc && (seq1 > aps->aps_seqmin[!sel])) {
480 aps->aps_seqmin[sel] = seq1 + nlen - 1;
481 aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
483 printf("proxy seq set %d at %x to %d + %d\n", sel,
484 aps->aps_seqmin[sel], aps->aps_seqoff[sel],
491 seq1 = ntohl(tcp->th_ack);
492 sel = aps->aps_sel[1 - out];
494 /* switch to other set ? */
495 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
496 (seq1 > aps->aps_ackmin[!sel])) {
498 printf("proxy out switch set ack %d -> %d %x > %x\n",
499 sel, !sel, seq1, aps->aps_ackmin[!sel]);
501 sel = aps->aps_sel[1 - out] = !sel;
504 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
505 seq2 = aps->aps_ackoff[sel];
506 tcp->th_ack = htonl(seq1 - seq2);
510 seq1 = ntohl(tcp->th_seq);
511 sel = aps->aps_sel[out];
513 /* switch to other set ? */
514 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
515 (seq1 > aps->aps_ackmin[!sel])) {
517 printf("proxy in switch set ack %d -> %d %x > %x\n",
518 sel, !sel, seq1, aps->aps_ackmin[!sel]);
520 sel = aps->aps_sel[out] = !sel;
523 if (aps->aps_ackoff[sel]) {
524 seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
526 seq2 = aps->aps_ackoff[sel];
528 tcp->th_seq = htonl(seq1);
533 if (inc && (seq1 > aps->aps_ackmin[!sel])) {
534 aps->aps_ackmin[!sel] = seq1 + nlen - 1;
535 aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
537 printf("proxy ack set %d at %x to %d + %d\n", !sel,
538 aps->aps_seqmin[!sel], aps->aps_seqoff[sel],
545 seq1 = ntohl(tcp->th_ack);
546 sel = aps->aps_sel[1 - out];
548 /* switch to other set ? */
549 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
550 (seq1 > aps->aps_seqmin[!sel])) {
552 printf("proxy in switch set seq %d -> %d %x > %x\n",
553 sel, !sel, seq1, aps->aps_seqmin[!sel]);
555 sel = aps->aps_sel[1 - out] = !sel;
558 if (aps->aps_seqoff[sel] != 0) {
560 printf("sel %d seqoff %d seq1 %x seqmin %x\n", sel,
561 aps->aps_seqoff[sel], seq1,
562 aps->aps_seqmin[sel]);
564 if (seq1 > aps->aps_seqmin[sel]) {
565 seq2 = aps->aps_seqoff[sel];
566 tcp->th_ack = htonl(seq1 - seq2);
572 printf("appr_fixseqack: seq %x ack %x\n", ntohl(tcp->th_seq),
580 * Initialise hook for kernel application proxies.
581 * Call the initialise routine for all the compiled in kernel proxies.
588 for (ap = ap_proxies; ap->apr_p; ap++) {
589 err = (*ap->apr_init)();
598 * Unload hook for kernel application proxies.
599 * Call the finialise routine for all the compiled in kernel proxies.
605 for (ap = ap_proxies; ap->apr_p; ap++)
608 for (ap = ap_proxylist; ap; ap = ap->apr_next)