17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (C) 1997-2003 by Darren Reed.
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate *
6bb1d9de5SJohn Ojemann * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
77c478bd9Sstevel@tonic-gate * Use is subject to license terms.
87c478bd9Sstevel@tonic-gate */
97c478bd9Sstevel@tonic-gate
107c478bd9Sstevel@tonic-gate #if defined(KERNEL) || defined(_KERNEL)
117c478bd9Sstevel@tonic-gate # undef KERNEL
127c478bd9Sstevel@tonic-gate # undef _KERNEL
137c478bd9Sstevel@tonic-gate # define KERNEL 1
147c478bd9Sstevel@tonic-gate # define _KERNEL 1
157c478bd9Sstevel@tonic-gate #endif
167c478bd9Sstevel@tonic-gate #include <sys/errno.h>
177c478bd9Sstevel@tonic-gate #include <sys/types.h>
187c478bd9Sstevel@tonic-gate #include <sys/param.h>
197c478bd9Sstevel@tonic-gate #include <sys/time.h>
207c478bd9Sstevel@tonic-gate #include <sys/file.h>
21ab25eeb5Syz #if !defined(AIX)
22ab25eeb5Syz # include <sys/fcntl.h>
23ab25eeb5Syz #endif
247c478bd9Sstevel@tonic-gate #if !defined(_KERNEL) && !defined(__KERNEL__)
257c478bd9Sstevel@tonic-gate # include <stdio.h>
267c478bd9Sstevel@tonic-gate # include <string.h>
277c478bd9Sstevel@tonic-gate # include <stdlib.h>
287c478bd9Sstevel@tonic-gate # include <ctype.h>
297c478bd9Sstevel@tonic-gate # define _KERNEL
307c478bd9Sstevel@tonic-gate # ifdef __OpenBSD__
317c478bd9Sstevel@tonic-gate struct file;
327c478bd9Sstevel@tonic-gate # endif
337c478bd9Sstevel@tonic-gate # include <sys/uio.h>
347c478bd9Sstevel@tonic-gate # undef _KERNEL
357c478bd9Sstevel@tonic-gate #endif
36ab25eeb5Syz #if !defined(linux)
37ab25eeb5Syz # include <sys/protosw.h>
38ab25eeb5Syz #endif
397c478bd9Sstevel@tonic-gate #include <sys/socket.h>
407c478bd9Sstevel@tonic-gate #if defined(_KERNEL)
417c478bd9Sstevel@tonic-gate # if !defined(__NetBSD__) && !defined(sun) && !defined(__osf__) && \
42ab25eeb5Syz !defined(__OpenBSD__) && !defined(__hpux) && !defined(__sgi) && \
43ab25eeb5Syz !defined(AIX)
447c478bd9Sstevel@tonic-gate # include <sys/ctype.h>
457c478bd9Sstevel@tonic-gate # endif
467c478bd9Sstevel@tonic-gate # include <sys/systm.h>
477c478bd9Sstevel@tonic-gate # if !defined(__SVR4) && !defined(__svr4__)
487c478bd9Sstevel@tonic-gate # include <sys/mbuf.h>
497c478bd9Sstevel@tonic-gate # endif
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
527c478bd9Sstevel@tonic-gate # include <sys/filio.h>
537c478bd9Sstevel@tonic-gate # include <sys/fcntl.h>
547c478bd9Sstevel@tonic-gate # if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM)
557c478bd9Sstevel@tonic-gate # include "opt_ipfilter.h"
567c478bd9Sstevel@tonic-gate # endif
577c478bd9Sstevel@tonic-gate #else
587c478bd9Sstevel@tonic-gate # include <sys/ioctl.h>
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate #if defined(__SVR4) || defined(__svr4__)
617c478bd9Sstevel@tonic-gate # include <sys/byteorder.h>
627c478bd9Sstevel@tonic-gate # ifdef _KERNEL
637c478bd9Sstevel@tonic-gate # include <sys/dditypes.h>
6424109627Syx # include <sys/strsubr.h>
657c478bd9Sstevel@tonic-gate # endif
667c478bd9Sstevel@tonic-gate # include <sys/stream.h>
677c478bd9Sstevel@tonic-gate # include <sys/kmem.h>
6824109627Syx # include <sys/pattr.h>
697c478bd9Sstevel@tonic-gate #endif
707c478bd9Sstevel@tonic-gate #if __FreeBSD__ > 2
717c478bd9Sstevel@tonic-gate # include <sys/queue.h>
727c478bd9Sstevel@tonic-gate #endif
737c478bd9Sstevel@tonic-gate #include <net/if.h>
747c478bd9Sstevel@tonic-gate #ifdef sun
757c478bd9Sstevel@tonic-gate # include <net/af.h>
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate #include <net/route.h>
787c478bd9Sstevel@tonic-gate #include <netinet/in.h>
797c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
807c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
81ab25eeb5Syz #ifndef linux
82ab25eeb5Syz # include <netinet/ip_var.h>
83ab25eeb5Syz #endif
847c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
857c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
867c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
877c478bd9Sstevel@tonic-gate #include "netinet/ip_compat.h"
88ab25eeb5Syz #include <netinet/tcpip.h>
89f4b3ec61Sdh #include "netinet/ipf_stack.h"
907c478bd9Sstevel@tonic-gate #include "netinet/ip_fil.h"
917c478bd9Sstevel@tonic-gate #include "netinet/ip_nat.h"
927c478bd9Sstevel@tonic-gate #include "netinet/ip_state.h"
937c478bd9Sstevel@tonic-gate #include "netinet/ip_proxy.h"
947c478bd9Sstevel@tonic-gate #if (__FreeBSD_version >= 300000)
957c478bd9Sstevel@tonic-gate # include <sys/malloc.h>
967c478bd9Sstevel@tonic-gate #endif
977c478bd9Sstevel@tonic-gate
98ab25eeb5Syz #include "netinet/ip_ftp_pxy.c"
99ab25eeb5Syz #include "netinet/ip_rcmd_pxy.c"
100ab25eeb5Syz # include "netinet/ip_pptp_pxy.c"
101ab25eeb5Syz #if defined(_KERNEL)
102ab25eeb5Syz # include "netinet/ip_irc_pxy.c"
103ab25eeb5Syz # include "netinet/ip_raudio_pxy.c"
104ab25eeb5Syz # include "netinet/ip_h323_pxy.c"
105ab25eeb5Syz # include "netinet/ip_netbios_pxy.c"
1067c478bd9Sstevel@tonic-gate #endif
107ab25eeb5Syz #include "netinet/ip_ipsec_pxy.c"
108ab25eeb5Syz #include "netinet/ip_rpcb_pxy.c"
1097c478bd9Sstevel@tonic-gate
110ab25eeb5Syz /* END OF INCLUDES */
1117c478bd9Sstevel@tonic-gate
112ab25eeb5Syz #if !defined(lint)
113ab25eeb5Syz static const char rcsid[] = "@(#)$Id: ip_proxy.c,v 2.62.2.14 2005/06/18 02:41:33 darrenr Exp $";
114ab25eeb5Syz #endif
1157c478bd9Sstevel@tonic-gate
116ab25eeb5Syz static int appr_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int ));
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate #define AP_SESS_SIZE 53
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate #if defined(_KERNEL)
121ab25eeb5Syz int ipf_proxy_debug = 0;
1227c478bd9Sstevel@tonic-gate #else
123ab25eeb5Syz int ipf_proxy_debug = 2;
1247c478bd9Sstevel@tonic-gate #endif
125f4b3ec61Sdh
126f4b3ec61Sdh static aproxy_t lcl_ap_proxies[] = {
1277c478bd9Sstevel@tonic-gate #ifdef IPF_FTP_PROXY
128f4b3ec61Sdh { NULL, "ftp", (char)IPPROTO_TCP, 0, 0, NULL, ippr_ftp_init, ippr_ftp_fini,
1297c478bd9Sstevel@tonic-gate ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL },
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate #ifdef IPF_IRC_PROXY
132f4b3ec61Sdh { NULL, "irc", (char)IPPROTO_TCP, 0, 0, NULL, ippr_irc_init, ippr_irc_fini,
1337c478bd9Sstevel@tonic-gate ippr_irc_new, NULL, NULL, ippr_irc_out, NULL, NULL },
1347c478bd9Sstevel@tonic-gate #endif
1357c478bd9Sstevel@tonic-gate #ifdef IPF_RCMD_PROXY
136f4b3ec61Sdh { NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, NULL, ippr_rcmd_init, ippr_rcmd_fini,
1377c478bd9Sstevel@tonic-gate ippr_rcmd_new, NULL, ippr_rcmd_in, ippr_rcmd_out, NULL, NULL },
1387c478bd9Sstevel@tonic-gate #endif
1397c478bd9Sstevel@tonic-gate #ifdef IPF_RAUDIO_PROXY
140f4b3ec61Sdh { NULL, "raudio", (char)IPPROTO_TCP, 0, 0, NULL, ippr_raudio_init, ippr_raudio_fini,
1417c478bd9Sstevel@tonic-gate ippr_raudio_new, NULL, ippr_raudio_in, ippr_raudio_out, NULL, NULL },
1427c478bd9Sstevel@tonic-gate #endif
1437c478bd9Sstevel@tonic-gate #ifdef IPF_MSNRPC_PROXY
144f4b3ec61Sdh { NULL, "msnrpc", (char)IPPROTO_TCP, 0, 0, NULL, ippr_msnrpc_init, ippr_msnrpc_fini,
1457c478bd9Sstevel@tonic-gate ippr_msnrpc_new, NULL, ippr_msnrpc_in, ippr_msnrpc_out, NULL, NULL },
1467c478bd9Sstevel@tonic-gate #endif
1477c478bd9Sstevel@tonic-gate #ifdef IPF_NETBIOS_PROXY
148f4b3ec61Sdh { NULL, "netbios", (char)IPPROTO_UDP, 0, 0, NULL, ippr_netbios_init, ippr_netbios_fini,
1497c478bd9Sstevel@tonic-gate NULL, NULL, NULL, ippr_netbios_out, NULL, NULL },
1507c478bd9Sstevel@tonic-gate #endif
1517c478bd9Sstevel@tonic-gate #ifdef IPF_IPSEC_PROXY
152f4b3ec61Sdh { NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, NULL,
1537c478bd9Sstevel@tonic-gate ippr_ipsec_init, ippr_ipsec_fini, ippr_ipsec_new, ippr_ipsec_del,
154ab25eeb5Syz ippr_ipsec_inout, ippr_ipsec_inout, ippr_ipsec_match, NULL },
155ab25eeb5Syz #endif
156ab25eeb5Syz #ifdef IPF_PPTP_PROXY
157f4b3ec61Sdh { NULL, "pptp", (char)IPPROTO_TCP, 0, 0, NULL,
158ab25eeb5Syz ippr_pptp_init, ippr_pptp_fini, ippr_pptp_new, ippr_pptp_del,
159ab25eeb5Syz ippr_pptp_inout, ippr_pptp_inout, NULL, NULL },
1607c478bd9Sstevel@tonic-gate #endif
1617c478bd9Sstevel@tonic-gate #ifdef IPF_H323_PROXY
162f4b3ec61Sdh { NULL, "h323", (char)IPPROTO_TCP, 0, 0, NULL, ippr_h323_init, ippr_h323_fini,
1637c478bd9Sstevel@tonic-gate ippr_h323_new, ippr_h323_del, ippr_h323_in, NULL, NULL },
164f4b3ec61Sdh { NULL, "h245", (char)IPPROTO_TCP, 0, 0, NULL, NULL, NULL,
1657c478bd9Sstevel@tonic-gate ippr_h245_new, NULL, NULL, ippr_h245_out, NULL },
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate #ifdef IPF_RPCB_PROXY
1687c478bd9Sstevel@tonic-gate # if 0
169f4b3ec61Sdh { NULL, "rpcbt", (char)IPPROTO_TCP, 0, 0, NULL,
1707c478bd9Sstevel@tonic-gate ippr_rpcb_init, ippr_rpcb_fini, ippr_rpcb_new, ippr_rpcb_del,
1717c478bd9Sstevel@tonic-gate ippr_rpcb_in, ippr_rpcb_out, NULL, NULL },
1727c478bd9Sstevel@tonic-gate # endif
173f4b3ec61Sdh { NULL, "rpcbu", (char)IPPROTO_UDP, 0, 0, NULL,
1747c478bd9Sstevel@tonic-gate ippr_rpcb_init, ippr_rpcb_fini, ippr_rpcb_new, ippr_rpcb_del,
1757c478bd9Sstevel@tonic-gate ippr_rpcb_in, ippr_rpcb_out, NULL, NULL },
1767c478bd9Sstevel@tonic-gate #endif
177f4b3ec61Sdh { NULL, "", '\0', 0, 0, NULL, NULL, NULL, NULL, NULL }
1787c478bd9Sstevel@tonic-gate };
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate * Dynamically add a new kernel proxy. Ensure that it is unique in the
1827c478bd9Sstevel@tonic-gate * collection compiled in and dynamically added.
1837c478bd9Sstevel@tonic-gate */
appr_add(ap,ifs)184f4b3ec61Sdh int appr_add(ap, ifs)
1857c478bd9Sstevel@tonic-gate aproxy_t *ap;
186f4b3ec61Sdh ipf_stack_t *ifs;
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate aproxy_t *a;
1897c478bd9Sstevel@tonic-gate
190f4b3ec61Sdh for (a = ifs->ifs_ap_proxies; a->apr_p; a++)
1917c478bd9Sstevel@tonic-gate if ((a->apr_p == ap->apr_p) &&
1927c478bd9Sstevel@tonic-gate !strncmp(a->apr_label, ap->apr_label,
193ab25eeb5Syz sizeof(ap->apr_label))) {
194ab25eeb5Syz if (ipf_proxy_debug > 1)
195ab25eeb5Syz printf("appr_add: %s/%d already present (B)\n",
196ab25eeb5Syz a->apr_label, a->apr_p);
1977c478bd9Sstevel@tonic-gate return -1;
198ab25eeb5Syz }
1997c478bd9Sstevel@tonic-gate
200f4b3ec61Sdh for (a = ifs->ifs_ap_proxylist; a->apr_p; a = a->apr_next)
2017c478bd9Sstevel@tonic-gate if ((a->apr_p == ap->apr_p) &&
2027c478bd9Sstevel@tonic-gate !strncmp(a->apr_label, ap->apr_label,
203ab25eeb5Syz sizeof(ap->apr_label))) {
204ab25eeb5Syz if (ipf_proxy_debug > 1)
205ab25eeb5Syz printf("appr_add: %s/%d already present (D)\n",
206ab25eeb5Syz a->apr_label, a->apr_p);
2077c478bd9Sstevel@tonic-gate return -1;
208ab25eeb5Syz }
209f4b3ec61Sdh ap->apr_next = ifs->ifs_ap_proxylist;
210f4b3ec61Sdh ifs->ifs_ap_proxylist = ap;
2117c478bd9Sstevel@tonic-gate if (ap->apr_init != NULL)
212f4b3ec61Sdh return (*ap->apr_init)(&ap->apr_private, ifs);
213f4b3ec61Sdh
2147c478bd9Sstevel@tonic-gate return 0;
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate * Check to see if the proxy this control request has come through for
2207c478bd9Sstevel@tonic-gate * exists, and if it does and it has a control function then invoke that
2217c478bd9Sstevel@tonic-gate * control function.
2227c478bd9Sstevel@tonic-gate */
appr_ctl(ctl,ifs)223f4b3ec61Sdh int appr_ctl(ctl, ifs)
2247c478bd9Sstevel@tonic-gate ap_ctl_t *ctl;
225f4b3ec61Sdh ipf_stack_t *ifs;
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate aproxy_t *a;
2287c478bd9Sstevel@tonic-gate int error;
2297c478bd9Sstevel@tonic-gate
230f4b3ec61Sdh a = appr_lookup(ctl->apc_p, ctl->apc_label, ifs);
231ab25eeb5Syz if (a == NULL) {
232ab25eeb5Syz if (ipf_proxy_debug > 1)
233ab25eeb5Syz printf("appr_ctl: can't find %s/%d\n",
234ab25eeb5Syz ctl->apc_label, ctl->apc_p);
2357c478bd9Sstevel@tonic-gate error = ESRCH;
236ab25eeb5Syz } else if (a->apr_ctl == NULL) {
237ab25eeb5Syz if (ipf_proxy_debug > 1)
238ab25eeb5Syz printf("appr_ctl: no ctl function for %s/%d\n",
239ab25eeb5Syz ctl->apc_label, ctl->apc_p);
2407c478bd9Sstevel@tonic-gate error = ENXIO;
241ab25eeb5Syz } else {
242f4b3ec61Sdh error = (*a->apr_ctl)(a, ctl, a->apr_private);
243ab25eeb5Syz if ((error != 0) && (ipf_proxy_debug > 1))
244ab25eeb5Syz printf("appr_ctl: %s/%d ctl error %d\n",
245ab25eeb5Syz a->apr_label, a->apr_p, error);
246ab25eeb5Syz }
2477c478bd9Sstevel@tonic-gate return error;
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * Delete a proxy that has been added dynamically from those available.
2537c478bd9Sstevel@tonic-gate * If it is in use, return 1 (do not destroy NOW), not in use 0 or -1
2547c478bd9Sstevel@tonic-gate * if it cannot be matched.
2557c478bd9Sstevel@tonic-gate */
appr_del(ap,ifs)256f4b3ec61Sdh int appr_del(ap, ifs)
2577c478bd9Sstevel@tonic-gate aproxy_t *ap;
258f4b3ec61Sdh ipf_stack_t *ifs;
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate aproxy_t *a, **app;
2617c478bd9Sstevel@tonic-gate
262f4b3ec61Sdh for (app = &ifs->ifs_ap_proxylist; ((a = *app) != NULL);
263f4b3ec61Sdh app = &a->apr_next)
2647c478bd9Sstevel@tonic-gate if (a == ap) {
2657c478bd9Sstevel@tonic-gate a->apr_flags |= APR_DELETE;
2667c478bd9Sstevel@tonic-gate *app = a->apr_next;
267ab25eeb5Syz if (ap->apr_ref != 0) {
268ab25eeb5Syz if (ipf_proxy_debug > 2)
269ab25eeb5Syz printf("appr_del: orphaning %s/%d\n",
270ab25eeb5Syz ap->apr_label, ap->apr_p);
2717c478bd9Sstevel@tonic-gate return 1;
272ab25eeb5Syz }
2737c478bd9Sstevel@tonic-gate return 0;
2747c478bd9Sstevel@tonic-gate }
275ab25eeb5Syz if (ipf_proxy_debug > 1)
276ab25eeb5Syz printf("appr_del: proxy %lx not found\n", (u_long)ap);
2777c478bd9Sstevel@tonic-gate return -1;
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate * Return 1 if the packet is a good match against a proxy, else 0.
2837c478bd9Sstevel@tonic-gate */
appr_ok(fin,tcp,nat)2847c478bd9Sstevel@tonic-gate int appr_ok(fin, tcp, nat)
2857c478bd9Sstevel@tonic-gate fr_info_t *fin;
2867c478bd9Sstevel@tonic-gate tcphdr_t *tcp;
2877c478bd9Sstevel@tonic-gate ipnat_t *nat;
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate aproxy_t *apr = nat->in_apr;
2907c478bd9Sstevel@tonic-gate u_short dport = nat->in_dport;
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
2937c478bd9Sstevel@tonic-gate (fin->fin_p != apr->apr_p))
2947c478bd9Sstevel@tonic-gate return 0;
2957c478bd9Sstevel@tonic-gate if ((tcp == NULL) && dport)
2967c478bd9Sstevel@tonic-gate return 0;
2977c478bd9Sstevel@tonic-gate return 1;
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate
appr_ioctl(data,cmd,mode,ifs)301f4b3ec61Sdh int appr_ioctl(data, cmd, mode, ifs)
3027c478bd9Sstevel@tonic-gate caddr_t data;
303ab25eeb5Syz ioctlcmd_t cmd;
3047c478bd9Sstevel@tonic-gate int mode;
305f4b3ec61Sdh ipf_stack_t *ifs;
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate ap_ctl_t ctl;
3087c478bd9Sstevel@tonic-gate caddr_t ptr;
3097c478bd9Sstevel@tonic-gate int error;
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate mode = mode; /* LINT */
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate switch (cmd)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate case SIOCPROXY :
316bb1d9de5SJohn Ojemann error = BCOPYIN(data, &ctl, sizeof(ctl));
317bb1d9de5SJohn Ojemann if (error != 0)
318bb1d9de5SJohn Ojemann return EFAULT;
319bb1d9de5SJohn Ojemann
3207c478bd9Sstevel@tonic-gate ptr = NULL;
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate if (ctl.apc_dsize > 0) {
3237c478bd9Sstevel@tonic-gate KMALLOCS(ptr, caddr_t, ctl.apc_dsize);
3247c478bd9Sstevel@tonic-gate if (ptr == NULL)
3257c478bd9Sstevel@tonic-gate error = ENOMEM;
3267c478bd9Sstevel@tonic-gate else {
3277c478bd9Sstevel@tonic-gate error = copyinptr(ctl.apc_data, ptr,
3287c478bd9Sstevel@tonic-gate ctl.apc_dsize);
3297c478bd9Sstevel@tonic-gate if (error == 0)
3307c478bd9Sstevel@tonic-gate ctl.apc_data = ptr;
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate } else {
3337c478bd9Sstevel@tonic-gate ctl.apc_data = NULL;
3347c478bd9Sstevel@tonic-gate error = 0;
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate if (error == 0)
338f4b3ec61Sdh error = appr_ctl(&ctl, ifs);
3397c478bd9Sstevel@tonic-gate
340ab25eeb5Syz if ((ctl.apc_dsize > 0) && (ptr != NULL) &&
341ab25eeb5Syz (ctl.apc_data == ptr)) {
3427c478bd9Sstevel@tonic-gate KFREES(ptr, ctl.apc_dsize);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate break;
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate default :
3477c478bd9Sstevel@tonic-gate error = EINVAL;
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate return error;
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate * If a proxy has a match function, call that to do extended packet
3557c478bd9Sstevel@tonic-gate * matching.
3567c478bd9Sstevel@tonic-gate */
appr_match(fin,nat)3577c478bd9Sstevel@tonic-gate int appr_match(fin, nat)
3587c478bd9Sstevel@tonic-gate fr_info_t *fin;
3597c478bd9Sstevel@tonic-gate nat_t *nat;
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate aproxy_t *apr;
3627c478bd9Sstevel@tonic-gate ipnat_t *ipn;
363ab25eeb5Syz int result;
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate ipn = nat->nat_ptr;
366ab25eeb5Syz if (ipf_proxy_debug > 8)
367ab25eeb5Syz printf("appr_match(%lx,%lx) aps %lx ptr %lx\n",
368ab25eeb5Syz (u_long)fin, (u_long)nat, (u_long)nat->nat_aps,
369ab25eeb5Syz (u_long)ipn);
370ab25eeb5Syz
371ab25eeb5Syz if ((fin->fin_flx & (FI_SHORT|FI_BAD)) != 0) {
372ab25eeb5Syz if (ipf_proxy_debug > 0)
373ab25eeb5Syz printf("appr_match: flx 0x%x (BAD|SHORT)\n",
374ab25eeb5Syz fin->fin_flx);
3757c478bd9Sstevel@tonic-gate return -1;
376ab25eeb5Syz }
377ab25eeb5Syz
3787c478bd9Sstevel@tonic-gate apr = ipn->in_apr;
379ab25eeb5Syz if ((apr == NULL) || (apr->apr_flags & APR_DELETE)) {
380ab25eeb5Syz if (ipf_proxy_debug > 0)
381ab25eeb5Syz printf("appr_match:apr %lx apr_flags 0x%x\n",
382ab25eeb5Syz (u_long)apr, apr ? apr->apr_flags : 0);
3837c478bd9Sstevel@tonic-gate return -1;
384ab25eeb5Syz }
385ab25eeb5Syz
386ab25eeb5Syz if (apr->apr_match != NULL) {
387f4b3ec61Sdh result = (*apr->apr_match)(fin, nat->nat_aps, nat, apr->apr_private);
388ab25eeb5Syz if (result != 0) {
389ab25eeb5Syz if (ipf_proxy_debug > 4)
390ab25eeb5Syz printf("appr_match: result %d\n", result);
3917c478bd9Sstevel@tonic-gate return -1;
392ab25eeb5Syz }
393ab25eeb5Syz }
3947c478bd9Sstevel@tonic-gate return 0;
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate * Allocate a new application proxy structure and fill it in with the
4007c478bd9Sstevel@tonic-gate * relevant details. call the init function once complete, prior to
4017c478bd9Sstevel@tonic-gate * returning.
4027c478bd9Sstevel@tonic-gate */
appr_new(fin,nat)4037c478bd9Sstevel@tonic-gate int appr_new(fin, nat)
4047c478bd9Sstevel@tonic-gate fr_info_t *fin;
4057c478bd9Sstevel@tonic-gate nat_t *nat;
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate register ap_session_t *aps;
4087c478bd9Sstevel@tonic-gate aproxy_t *apr;
409f4b3ec61Sdh ipf_stack_t *ifs = fin->fin_ifs;
4107c478bd9Sstevel@tonic-gate
411ab25eeb5Syz if (ipf_proxy_debug > 8)
412ab25eeb5Syz printf("appr_new(%lx,%lx) \n", (u_long)fin, (u_long)nat);
413ab25eeb5Syz
414ab25eeb5Syz if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL)) {
415ab25eeb5Syz if (ipf_proxy_debug > 0)
416ab25eeb5Syz printf("appr_new: nat_ptr %lx nat_aps %lx\n",
417ab25eeb5Syz (u_long)nat->nat_ptr, (u_long)nat->nat_aps);
4187c478bd9Sstevel@tonic-gate return -1;
419ab25eeb5Syz }
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate apr = nat->nat_ptr->in_apr;
4227c478bd9Sstevel@tonic-gate
423ab25eeb5Syz if ((apr->apr_flags & APR_DELETE) ||
424ab25eeb5Syz (fin->fin_p != apr->apr_p)) {
425ab25eeb5Syz if (ipf_proxy_debug > 2)
426ab25eeb5Syz printf("appr_new: apr_flags 0x%x p %d/%d\n",
427ab25eeb5Syz apr->apr_flags, fin->fin_p, apr->apr_p);
4287c478bd9Sstevel@tonic-gate return -1;
429ab25eeb5Syz }
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate KMALLOC(aps, ap_session_t *);
432ab25eeb5Syz if (!aps) {
433ab25eeb5Syz if (ipf_proxy_debug > 0)
434ab25eeb5Syz printf("appr_new: malloc failed (%lu)\n",
435ab25eeb5Syz (u_long)sizeof(ap_session_t));
4367c478bd9Sstevel@tonic-gate return -1;
437ab25eeb5Syz }
438ab25eeb5Syz
4397c478bd9Sstevel@tonic-gate bzero((char *)aps, sizeof(*aps));
4407c478bd9Sstevel@tonic-gate aps->aps_p = fin->fin_p;
4417c478bd9Sstevel@tonic-gate aps->aps_data = NULL;
4427c478bd9Sstevel@tonic-gate aps->aps_apr = apr;
4437c478bd9Sstevel@tonic-gate aps->aps_psiz = 0;
4447c478bd9Sstevel@tonic-gate if (apr->apr_new != NULL)
445f4b3ec61Sdh if ((*apr->apr_new)(fin, aps, nat, apr->apr_private) == -1) {
4467c478bd9Sstevel@tonic-gate if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
4477c478bd9Sstevel@tonic-gate KFREES(aps->aps_data, aps->aps_psiz);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate KFREE(aps);
450ab25eeb5Syz if (ipf_proxy_debug > 2)
451ab25eeb5Syz printf("appr_new: new(%lx) failed\n",
452ab25eeb5Syz (u_long)apr->apr_new);
4537c478bd9Sstevel@tonic-gate return -1;
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate aps->aps_nat = nat;
456f4b3ec61Sdh aps->aps_next = ifs->ifs_ap_sess_list;
457f4b3ec61Sdh ifs->ifs_ap_sess_list = aps;
4587c478bd9Sstevel@tonic-gate nat->nat_aps = aps;
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate return 0;
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate /*
4657c478bd9Sstevel@tonic-gate * Check to see if a packet should be passed through an active proxy routine
4667c478bd9Sstevel@tonic-gate * if one has been setup for it. We don't need to check the checksum here if
4677c478bd9Sstevel@tonic-gate * IPFILTER_CKSUM is defined because if it is, a failed check causes FI_BAD
4687c478bd9Sstevel@tonic-gate * to be set.
4697c478bd9Sstevel@tonic-gate */
appr_check(fin,nat)4707c478bd9Sstevel@tonic-gate int appr_check(fin, nat)
4717c478bd9Sstevel@tonic-gate fr_info_t *fin;
4727c478bd9Sstevel@tonic-gate nat_t *nat;
4737c478bd9Sstevel@tonic-gate {
474*af5f29ddSToomas Soome #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6) && \
475*af5f29ddSToomas Soome defined(ICK_VALID)
4767c478bd9Sstevel@tonic-gate mb_t *m;
4777c478bd9Sstevel@tonic-gate int dosum = 1;
4787c478bd9Sstevel@tonic-gate #endif
4797c478bd9Sstevel@tonic-gate tcphdr_t *tcp = NULL;
4807c478bd9Sstevel@tonic-gate udphdr_t *udp = NULL;
48124109627Syx void *tcpudp = NULL;
48224109627Syx u_short *csump;
4837c478bd9Sstevel@tonic-gate ap_session_t *aps;
4847c478bd9Sstevel@tonic-gate aproxy_t *apr;
4857c478bd9Sstevel@tonic-gate ip_t *ip;
4867c478bd9Sstevel@tonic-gate short rv;
4877c478bd9Sstevel@tonic-gate int err;
4887c478bd9Sstevel@tonic-gate #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi)
4897c478bd9Sstevel@tonic-gate u_32_t s1, s2, sd;
4907c478bd9Sstevel@tonic-gate #endif
491f4b3ec61Sdh ipf_stack_t *ifs = fin->fin_ifs;
492f4b3ec61Sdh
493*af5f29ddSToomas Soome #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6)
4947ddc9b1aSDarren Reed net_handle_t net_data_p;
495381a2a9aSdr if (fin->fin_v == 4)
496f4b3ec61Sdh net_data_p = ifs->ifs_ipf_ipv4;
497381a2a9aSdr else
498f4b3ec61Sdh net_data_p = ifs->ifs_ipf_ipv6;
499381a2a9aSdr #endif
5007c478bd9Sstevel@tonic-gate
501ab25eeb5Syz if (fin->fin_flx & FI_BAD) {
502ab25eeb5Syz if (ipf_proxy_debug > 0)
503ab25eeb5Syz printf("appr_check: flx 0x%x (BAD)\n", fin->fin_flx);
5047c478bd9Sstevel@tonic-gate return -1;
505ab25eeb5Syz }
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate #ifndef IPFILTER_CKSUM
5087c478bd9Sstevel@tonic-gate if ((fin->fin_out == 0) && (fr_checkl4sum(fin) == -1)) {
509ab25eeb5Syz if (ipf_proxy_debug > 0)
510ab25eeb5Syz printf("appr_check: l4 checksum failure %d\n",
511ab25eeb5Syz fin->fin_p);
5127c478bd9Sstevel@tonic-gate if (fin->fin_p == IPPROTO_TCP)
513f4b3ec61Sdh ifs->ifs_frstats[fin->fin_out].fr_tcpbad++;
5147c478bd9Sstevel@tonic-gate return -1;
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate #endif
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate aps = nat->nat_aps;
5197c478bd9Sstevel@tonic-gate if ((aps != NULL) && (aps->aps_p == fin->fin_p)) {
5207c478bd9Sstevel@tonic-gate /*
5217c478bd9Sstevel@tonic-gate * If there is data in this packet to be proxied then try and
5227c478bd9Sstevel@tonic-gate * get it all into the one buffer, else drop it.
5237c478bd9Sstevel@tonic-gate */
524ab25eeb5Syz #if defined(MENTAT) || defined(HAVE_M_PULLDOWN)
5257c478bd9Sstevel@tonic-gate if ((fin->fin_dlen > 0) && !(fin->fin_flx & FI_COALESCE))
526ab25eeb5Syz if (fr_coalesce(fin) == -1) {
527ab25eeb5Syz if (ipf_proxy_debug > 0)
528ab25eeb5Syz printf("appr_check: fr_coalesce failed %x\n", fin->fin_flx);
5297c478bd9Sstevel@tonic-gate return -1;
530ab25eeb5Syz }
531ab25eeb5Syz #endif
5327c478bd9Sstevel@tonic-gate ip = fin->fin_ip;
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate switch (fin->fin_p)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate case IPPROTO_TCP :
5377c478bd9Sstevel@tonic-gate tcp = (tcphdr_t *)fin->fin_dp;
5387c478bd9Sstevel@tonic-gate
539*af5f29ddSToomas Soome #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6) && \
540*af5f29ddSToomas Soome defined(ICK_VALID)
5417c478bd9Sstevel@tonic-gate m = fin->fin_qfm;
5427c478bd9Sstevel@tonic-gate if (dohwcksum && (m->b_ick_flag == ICK_VALID))
5437c478bd9Sstevel@tonic-gate dosum = 0;
5447c478bd9Sstevel@tonic-gate #endif
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate * Don't bother the proxy with these...or in fact,
5477c478bd9Sstevel@tonic-gate * should we free up proxy stuff when seen?
5487c478bd9Sstevel@tonic-gate */
5497c478bd9Sstevel@tonic-gate if ((fin->fin_tcpf & TH_RST) != 0)
5507c478bd9Sstevel@tonic-gate break;
5517c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
5527c478bd9Sstevel@tonic-gate case IPPROTO_UDP :
5537c478bd9Sstevel@tonic-gate udp = (udphdr_t *)fin->fin_dp;
5547c478bd9Sstevel@tonic-gate break;
5557c478bd9Sstevel@tonic-gate default :
5567c478bd9Sstevel@tonic-gate break;
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate apr = aps->aps_apr;
5607c478bd9Sstevel@tonic-gate err = 0;
5617c478bd9Sstevel@tonic-gate if (fin->fin_out != 0) {
5627c478bd9Sstevel@tonic-gate if (apr->apr_outpkt != NULL)
563f4b3ec61Sdh err = (*apr->apr_outpkt)(fin, aps, nat, apr->apr_private);
5647c478bd9Sstevel@tonic-gate } else {
5657c478bd9Sstevel@tonic-gate if (apr->apr_inpkt != NULL)
566f4b3ec61Sdh err = (*apr->apr_inpkt)(fin, aps, nat, apr->apr_private);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate
5697c478bd9Sstevel@tonic-gate rv = APR_EXIT(err);
570ab25eeb5Syz if (((ipf_proxy_debug > 0) && (rv != 0)) ||
571ab25eeb5Syz (ipf_proxy_debug > 8))
572ab25eeb5Syz printf("appr_check: out %d err %x rv %d\n",
573ab25eeb5Syz fin->fin_out, err, rv);
574ab25eeb5Syz if (rv == 1)
5757c478bd9Sstevel@tonic-gate return -1;
576ab25eeb5Syz
5777c478bd9Sstevel@tonic-gate if (rv == 2) {
5787c478bd9Sstevel@tonic-gate appr_free(apr);
5797c478bd9Sstevel@tonic-gate nat->nat_aps = NULL;
5807c478bd9Sstevel@tonic-gate return -1;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate * If err != 0 then the data size of the packet has changed
5857c478bd9Sstevel@tonic-gate * so we need to recalculate the header checksums for the
5867c478bd9Sstevel@tonic-gate * packet.
587381a2a9aSdr * inbound packets always need to be adjusted.
5887c478bd9Sstevel@tonic-gate */
5897c478bd9Sstevel@tonic-gate #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi)
59024109627Syx if (err != 0) {
5917c478bd9Sstevel@tonic-gate short adjlen = err & 0xffff;
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate s1 = LONG_SUM(ip->ip_len - adjlen);
5947c478bd9Sstevel@tonic-gate s2 = LONG_SUM(ip->ip_len);
5957c478bd9Sstevel@tonic-gate CALC_SUMD(s1, s2, sd);
59624109627Syx sd = (sd & 0xffff) + (sd >> 16);
59724109627Syx if (!fin->fin_out ||
59824109627Syx !NET_IS_HCK_L3_FULL(net_data_p, fin->fin_m))
59924109627Syx fix_outcksum(&ip->ip_sum, sd);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate #endif
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate /*
6047c478bd9Sstevel@tonic-gate * For TCP packets, we may need to adjust the sequence and
6057c478bd9Sstevel@tonic-gate * acknowledgement numbers to reflect changes in size of the
6067c478bd9Sstevel@tonic-gate * data stream.
6077c478bd9Sstevel@tonic-gate *
60824109627Syx * For both TCP and UDP, recalculate the layer 4 checksum in
60924109627Syx * software checksum case, as we can't tell if data has been
6107c478bd9Sstevel@tonic-gate * changed or not.
6117c478bd9Sstevel@tonic-gate */
6127c478bd9Sstevel@tonic-gate if (tcp != NULL) {
61324109627Syx tcpudp = tcp;
61424109627Syx csump = &tcp->th_sum;
61524109627Syx (void) appr_fixseqack(fin, ip, aps, APR_INC(err));
61624109627Syx } else if (udp != NULL) {
61724109627Syx tcpudp = udp;
61824109627Syx csump = &udp->uh_sum;
61924109627Syx }
62024109627Syx
62124109627Syx if (tcpudp) {
622*af5f29ddSToomas Soome #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(MENTAT)
62324109627Syx if (!fin->fin_out) {
62424109627Syx /*
62524109627Syx * We are incapable of adjusting partial hcksum
62624109627Syx * result for inbound packets here, as the
62724109627Syx * partial hcksum calculation range might not
62824109627Syx * cover the whole payload, and the payload data
62924109627Syx * might be changed by proxy.
63024109627Syx */
63124109627Syx DB_CKSUMFLAGS(fin->fin_m) &= ~HCK_PARTIALCKSUM;
63224109627Syx
63324109627Syx /* Inbound packets always need recalculation. */
63424109627Syx *csump = fr_cksum(fin->fin_qfm, ip,
63524109627Syx fin->fin_p, tcpudp);
63624109627Syx } else if (NET_IS_HCK_L4_PART(net_data_p, fin->fin_m)) {
63724109627Syx if (err != 0) {
63824109627Syx DB_CKSUMEND(fin->fin_m) += (short)err;
63924109627Syx fix_incksum(csump, sd);
64024109627Syx }
64124109627Syx } else if (!NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m))
64224109627Syx *csump = fr_cksum(fin->fin_qfm, ip,
64324109627Syx fin->fin_p, tcpudp);
6447c478bd9Sstevel@tonic-gate #else
64524109627Syx *csump = fr_cksum(fin->fin_m, ip, fin->fin_p, tcpudp);
6467c478bd9Sstevel@tonic-gate #endif
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate aps->aps_bytes += fin->fin_plen;
6497c478bd9Sstevel@tonic-gate aps->aps_pkts++;
6507c478bd9Sstevel@tonic-gate return 1;
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate return 0;
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate /*
6577c478bd9Sstevel@tonic-gate * Search for an proxy by the protocol it is being used with and its name.
6587c478bd9Sstevel@tonic-gate */
appr_lookup(pr,name,ifs)659f4b3ec61Sdh aproxy_t *appr_lookup(pr, name, ifs)
6607c478bd9Sstevel@tonic-gate u_int pr;
6617c478bd9Sstevel@tonic-gate char *name;
662f4b3ec61Sdh ipf_stack_t *ifs;
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate aproxy_t *ap;
6657c478bd9Sstevel@tonic-gate
666ab25eeb5Syz if (ipf_proxy_debug > 8)
667ab25eeb5Syz printf("appr_lookup(%d,%s)\n", pr, name);
668ab25eeb5Syz
669f4b3ec61Sdh for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++)
6707c478bd9Sstevel@tonic-gate if ((ap->apr_p == pr) &&
6717c478bd9Sstevel@tonic-gate !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
6727c478bd9Sstevel@tonic-gate ap->apr_ref++;
6737c478bd9Sstevel@tonic-gate return ap;
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate
676f4b3ec61Sdh for (ap = ifs->ifs_ap_proxylist; ap; ap = ap->apr_next)
6777c478bd9Sstevel@tonic-gate if ((ap->apr_p == pr) &&
6787c478bd9Sstevel@tonic-gate !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
6797c478bd9Sstevel@tonic-gate ap->apr_ref++;
6807c478bd9Sstevel@tonic-gate return ap;
6817c478bd9Sstevel@tonic-gate }
682ab25eeb5Syz if (ipf_proxy_debug > 2)
683ab25eeb5Syz printf("appr_lookup: failed for %d/%s\n", pr, name);
6847c478bd9Sstevel@tonic-gate return NULL;
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate
6877c478bd9Sstevel@tonic-gate
appr_free(ap)6887c478bd9Sstevel@tonic-gate void appr_free(ap)
6897c478bd9Sstevel@tonic-gate aproxy_t *ap;
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate ap->apr_ref--;
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate
6947c478bd9Sstevel@tonic-gate
aps_free(aps,ifs)695f4b3ec61Sdh void aps_free(aps, ifs)
6967c478bd9Sstevel@tonic-gate ap_session_t *aps;
697f4b3ec61Sdh ipf_stack_t *ifs;
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate ap_session_t *a, **ap;
7007c478bd9Sstevel@tonic-gate aproxy_t *apr;
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate if (!aps)
7037c478bd9Sstevel@tonic-gate return;
7047c478bd9Sstevel@tonic-gate
705f4b3ec61Sdh for (ap = &ifs->ifs_ap_sess_list; ((a = *ap) != NULL); ap = &a->aps_next)
7067c478bd9Sstevel@tonic-gate if (a == aps) {
7077c478bd9Sstevel@tonic-gate *ap = a->aps_next;
7087c478bd9Sstevel@tonic-gate break;
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate
7117c478bd9Sstevel@tonic-gate apr = aps->aps_apr;
7127c478bd9Sstevel@tonic-gate if ((apr != NULL) && (apr->apr_del != NULL))
713f4b3ec61Sdh (*apr->apr_del)(aps, apr->apr_private, ifs);
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
7167c478bd9Sstevel@tonic-gate KFREES(aps->aps_data, aps->aps_psiz);
7177c478bd9Sstevel@tonic-gate KFREE(aps);
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate /*
7227c478bd9Sstevel@tonic-gate * returns 2 if ack or seq number in TCP header is changed, returns 0 otherwise
7237c478bd9Sstevel@tonic-gate */
appr_fixseqack(fin,ip,aps,inc)7247c478bd9Sstevel@tonic-gate static int appr_fixseqack(fin, ip, aps, inc)
7257c478bd9Sstevel@tonic-gate fr_info_t *fin;
7267c478bd9Sstevel@tonic-gate ip_t *ip;
7277c478bd9Sstevel@tonic-gate ap_session_t *aps;
7287c478bd9Sstevel@tonic-gate int inc;
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate int sel, ch = 0, out, nlen;
7317c478bd9Sstevel@tonic-gate u_32_t seq1, seq2;
7327c478bd9Sstevel@tonic-gate tcphdr_t *tcp;
7337c478bd9Sstevel@tonic-gate short inc2;
7347c478bd9Sstevel@tonic-gate
7357c478bd9Sstevel@tonic-gate tcp = (tcphdr_t *)fin->fin_dp;
7367c478bd9Sstevel@tonic-gate out = fin->fin_out;
7377c478bd9Sstevel@tonic-gate /*
7387c478bd9Sstevel@tonic-gate * ip_len has already been adjusted by 'inc'.
7397c478bd9Sstevel@tonic-gate */
7407c478bd9Sstevel@tonic-gate nlen = ip->ip_len;
7417c478bd9Sstevel@tonic-gate nlen -= (IP_HL(ip) << 2) + (TCP_OFF(tcp) << 2);
7427c478bd9Sstevel@tonic-gate
7437c478bd9Sstevel@tonic-gate inc2 = inc;
7447c478bd9Sstevel@tonic-gate inc = (int)inc2;
7457c478bd9Sstevel@tonic-gate
7467c478bd9Sstevel@tonic-gate if (out != 0) {
7477c478bd9Sstevel@tonic-gate seq1 = (u_32_t)ntohl(tcp->th_seq);
7487c478bd9Sstevel@tonic-gate sel = aps->aps_sel[out];
7497c478bd9Sstevel@tonic-gate
7507c478bd9Sstevel@tonic-gate /* switch to other set ? */
7517c478bd9Sstevel@tonic-gate if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
7527c478bd9Sstevel@tonic-gate (seq1 > aps->aps_seqmin[!sel])) {
753ab25eeb5Syz if (ipf_proxy_debug > 7)
754ab25eeb5Syz printf("proxy out switch set seq %d -> %d %x > %x\n",
755ab25eeb5Syz sel, !sel, seq1,
756ab25eeb5Syz aps->aps_seqmin[!sel]);
7577c478bd9Sstevel@tonic-gate sel = aps->aps_sel[out] = !sel;
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate
7607c478bd9Sstevel@tonic-gate if (aps->aps_seqoff[sel]) {
7617c478bd9Sstevel@tonic-gate seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
7627c478bd9Sstevel@tonic-gate if (seq1 > seq2) {
7637c478bd9Sstevel@tonic-gate seq2 = aps->aps_seqoff[sel];
7647c478bd9Sstevel@tonic-gate seq1 += seq2;
7657c478bd9Sstevel@tonic-gate tcp->th_seq = htonl(seq1);
7667c478bd9Sstevel@tonic-gate ch = 1;
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate
7707c478bd9Sstevel@tonic-gate if (inc && (seq1 > aps->aps_seqmin[!sel])) {
7717c478bd9Sstevel@tonic-gate aps->aps_seqmin[sel] = seq1 + nlen - 1;
7727c478bd9Sstevel@tonic-gate aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
773ab25eeb5Syz if (ipf_proxy_debug > 7)
774ab25eeb5Syz printf("proxy seq set %d at %x to %d + %d\n",
775ab25eeb5Syz sel, aps->aps_seqmin[sel],
776ab25eeb5Syz aps->aps_seqoff[sel], inc);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate
7797c478bd9Sstevel@tonic-gate /***/
7807c478bd9Sstevel@tonic-gate
7817c478bd9Sstevel@tonic-gate seq1 = ntohl(tcp->th_ack);
7827c478bd9Sstevel@tonic-gate sel = aps->aps_sel[1 - out];
7837c478bd9Sstevel@tonic-gate
7847c478bd9Sstevel@tonic-gate /* switch to other set ? */
7857c478bd9Sstevel@tonic-gate if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
7867c478bd9Sstevel@tonic-gate (seq1 > aps->aps_ackmin[!sel])) {
787ab25eeb5Syz if (ipf_proxy_debug > 7)
788ab25eeb5Syz printf("proxy out switch set ack %d -> %d %x > %x\n",
789ab25eeb5Syz sel, !sel, seq1,
790ab25eeb5Syz aps->aps_ackmin[!sel]);
7917c478bd9Sstevel@tonic-gate sel = aps->aps_sel[1 - out] = !sel;
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate
7947c478bd9Sstevel@tonic-gate if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
7957c478bd9Sstevel@tonic-gate seq2 = aps->aps_ackoff[sel];
7967c478bd9Sstevel@tonic-gate tcp->th_ack = htonl(seq1 - seq2);
7977c478bd9Sstevel@tonic-gate ch = 1;
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate } else {
8007c478bd9Sstevel@tonic-gate seq1 = ntohl(tcp->th_seq);
8017c478bd9Sstevel@tonic-gate sel = aps->aps_sel[out];
8027c478bd9Sstevel@tonic-gate
8037c478bd9Sstevel@tonic-gate /* switch to other set ? */
8047c478bd9Sstevel@tonic-gate if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
8057c478bd9Sstevel@tonic-gate (seq1 > aps->aps_ackmin[!sel])) {
806ab25eeb5Syz if (ipf_proxy_debug > 7)
807ab25eeb5Syz printf("proxy in switch set ack %d -> %d %x > %x\n",
808ab25eeb5Syz sel, !sel, seq1, aps->aps_ackmin[!sel]);
8097c478bd9Sstevel@tonic-gate sel = aps->aps_sel[out] = !sel;
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate if (aps->aps_ackoff[sel]) {
8137c478bd9Sstevel@tonic-gate seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
8147c478bd9Sstevel@tonic-gate if (seq1 > seq2) {
8157c478bd9Sstevel@tonic-gate seq2 = aps->aps_ackoff[sel];
8167c478bd9Sstevel@tonic-gate seq1 += seq2;
8177c478bd9Sstevel@tonic-gate tcp->th_seq = htonl(seq1);
8187c478bd9Sstevel@tonic-gate ch = 1;
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate
8227c478bd9Sstevel@tonic-gate if (inc && (seq1 > aps->aps_ackmin[!sel])) {
8237c478bd9Sstevel@tonic-gate aps->aps_ackmin[!sel] = seq1 + nlen - 1;
8247c478bd9Sstevel@tonic-gate aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
825ab25eeb5Syz
826ab25eeb5Syz if (ipf_proxy_debug > 7)
827ab25eeb5Syz printf("proxy ack set %d at %x to %d + %d\n",
828ab25eeb5Syz !sel, aps->aps_seqmin[!sel],
829ab25eeb5Syz aps->aps_seqoff[sel], inc);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate
8327c478bd9Sstevel@tonic-gate /***/
8337c478bd9Sstevel@tonic-gate
8347c478bd9Sstevel@tonic-gate seq1 = ntohl(tcp->th_ack);
8357c478bd9Sstevel@tonic-gate sel = aps->aps_sel[1 - out];
8367c478bd9Sstevel@tonic-gate
8377c478bd9Sstevel@tonic-gate /* switch to other set ? */
8387c478bd9Sstevel@tonic-gate if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
8397c478bd9Sstevel@tonic-gate (seq1 > aps->aps_seqmin[!sel])) {
840ab25eeb5Syz if (ipf_proxy_debug > 7)
841ab25eeb5Syz printf("proxy in switch set seq %d -> %d %x > %x\n",
842ab25eeb5Syz sel, !sel, seq1, aps->aps_seqmin[!sel]);
8437c478bd9Sstevel@tonic-gate sel = aps->aps_sel[1 - out] = !sel;
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate
8467c478bd9Sstevel@tonic-gate if (aps->aps_seqoff[sel] != 0) {
847ab25eeb5Syz if (ipf_proxy_debug > 7)
848ab25eeb5Syz printf("sel %d seqoff %d seq1 %x seqmin %x\n",
849ab25eeb5Syz sel, aps->aps_seqoff[sel], seq1,
850ab25eeb5Syz aps->aps_seqmin[sel]);
8517c478bd9Sstevel@tonic-gate if (seq1 > aps->aps_seqmin[sel]) {
8527c478bd9Sstevel@tonic-gate seq2 = aps->aps_seqoff[sel];
8537c478bd9Sstevel@tonic-gate tcp->th_ack = htonl(seq1 - seq2);
8547c478bd9Sstevel@tonic-gate ch = 1;
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate }
858ab25eeb5Syz
859ab25eeb5Syz if (ipf_proxy_debug > 8)
860ab25eeb5Syz printf("appr_fixseqack: seq %x ack %x\n",
861ab25eeb5Syz ntohl(tcp->th_seq), ntohl(tcp->th_ack));
8627c478bd9Sstevel@tonic-gate return ch ? 2 : 0;
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate
8657c478bd9Sstevel@tonic-gate
8667c478bd9Sstevel@tonic-gate /*
8677c478bd9Sstevel@tonic-gate * Initialise hook for kernel application proxies.
8687c478bd9Sstevel@tonic-gate * Call the initialise routine for all the compiled in kernel proxies.
8697c478bd9Sstevel@tonic-gate */
appr_init(ifs)870f4b3ec61Sdh int appr_init(ifs)
871f4b3ec61Sdh ipf_stack_t *ifs;
8727c478bd9Sstevel@tonic-gate {
8737c478bd9Sstevel@tonic-gate aproxy_t *ap;
874f4b3ec61Sdh int err = 0;
875f4b3ec61Sdh
876f4b3ec61Sdh /* Since the refcnt is used we make a copy of lcl_ap_proxies */
877f4b3ec61Sdh KMALLOCS(ifs->ifs_ap_proxies, aproxy_t *, sizeof (lcl_ap_proxies));
878f4b3ec61Sdh bcopy(lcl_ap_proxies, ifs->ifs_ap_proxies, sizeof (lcl_ap_proxies));
8797c478bd9Sstevel@tonic-gate
880f4b3ec61Sdh for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++) {
8817c478bd9Sstevel@tonic-gate if (ap->apr_init != NULL) {
882f4b3ec61Sdh err = (*ap->apr_init)(&ap->apr_private, ifs);
8837c478bd9Sstevel@tonic-gate if (err != 0)
8847c478bd9Sstevel@tonic-gate break;
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate }
887f4b3ec61Sdh return 0;
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate
8907c478bd9Sstevel@tonic-gate
8917c478bd9Sstevel@tonic-gate /*
8927c478bd9Sstevel@tonic-gate * Unload hook for kernel application proxies.
8937c478bd9Sstevel@tonic-gate * Call the finialise routine for all the compiled in kernel proxies.
8947c478bd9Sstevel@tonic-gate */
appr_unload(ifs)895f4b3ec61Sdh void appr_unload(ifs)
896f4b3ec61Sdh ipf_stack_t *ifs;
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate aproxy_t *ap;
899f4b3ec61Sdh if(ifs->ifs_ap_proxies == NULL)
900f4b3ec61Sdh return;
9017c478bd9Sstevel@tonic-gate
902f4b3ec61Sdh for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++)
9037c478bd9Sstevel@tonic-gate if (ap->apr_fini != NULL)
904f4b3ec61Sdh (*ap->apr_fini)(&ap->apr_private, ifs);
905f4b3ec61Sdh for (ap = ifs->ifs_ap_proxylist; ap; ap = ap->apr_next)
9067c478bd9Sstevel@tonic-gate if (ap->apr_fini != NULL)
907f4b3ec61Sdh (*ap->apr_fini)(&ap->apr_private, ifs);
908f4b3ec61Sdh
909f4b3ec61Sdh KFREES(ifs->ifs_ap_proxies, sizeof (lcl_ap_proxies));
910f4b3ec61Sdh ifs->ifs_ap_proxies = NULL;
9117c478bd9Sstevel@tonic-gate }
912