xref: /titanic_51/usr/src/uts/common/inet/ipf/ip_proxy.c (revision f56257d84449cae424f3943cec01573a5027af36)
1ab25eeb5Syz155240 /*
2ab25eeb5Syz155240  * Copyright (C) 1997-2003 by Darren Reed.
3ab25eeb5Syz155240  *
4ab25eeb5Syz155240  * See the IPFILTER.LICENCE file for details on licencing.
5ab25eeb5Syz155240  *
6bb1d9de5SJohn Ojemann  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
7ab25eeb5Syz155240  * Use is subject to license terms.
8ab25eeb5Syz155240  */
9ab25eeb5Syz155240 
10ab25eeb5Syz155240 #if defined(KERNEL) || defined(_KERNEL)
11ab25eeb5Syz155240 # undef KERNEL
12ab25eeb5Syz155240 # undef _KERNEL
13ab25eeb5Syz155240 # define        KERNEL	1
14ab25eeb5Syz155240 # define        _KERNEL	1
15ab25eeb5Syz155240 #endif
16ab25eeb5Syz155240 #include <sys/errno.h>
17ab25eeb5Syz155240 #include <sys/types.h>
18ab25eeb5Syz155240 #include <sys/param.h>
19ab25eeb5Syz155240 #include <sys/time.h>
20ab25eeb5Syz155240 #include <sys/file.h>
21ab25eeb5Syz155240 #if !defined(AIX)
22ab25eeb5Syz155240 # include <sys/fcntl.h>
23ab25eeb5Syz155240 #endif
24ab25eeb5Syz155240 #if !defined(_KERNEL) && !defined(__KERNEL__)
25ab25eeb5Syz155240 # include <stdio.h>
26ab25eeb5Syz155240 # include <string.h>
27ab25eeb5Syz155240 # include <stdlib.h>
28ab25eeb5Syz155240 # include <ctype.h>
29ab25eeb5Syz155240 # define _KERNEL
30ab25eeb5Syz155240 # ifdef __OpenBSD__
31ab25eeb5Syz155240 struct file;
32ab25eeb5Syz155240 # endif
33ab25eeb5Syz155240 # include <sys/uio.h>
34ab25eeb5Syz155240 # undef _KERNEL
35ab25eeb5Syz155240 #endif
36ab25eeb5Syz155240 #if !defined(linux)
37ab25eeb5Syz155240 # include <sys/protosw.h>
38ab25eeb5Syz155240 #endif
39ab25eeb5Syz155240 #include <sys/socket.h>
40ab25eeb5Syz155240 #if defined(_KERNEL)
41ab25eeb5Syz155240 # if !defined(__NetBSD__) && !defined(sun) && !defined(__osf__) && \
42ab25eeb5Syz155240      !defined(__OpenBSD__) && !defined(__hpux) && !defined(__sgi) && \
43ab25eeb5Syz155240      !defined(AIX)
44ab25eeb5Syz155240 #  include <sys/ctype.h>
45ab25eeb5Syz155240 # endif
46ab25eeb5Syz155240 # include <sys/systm.h>
47ab25eeb5Syz155240 # if !defined(__SVR4) && !defined(__svr4__)
48ab25eeb5Syz155240 #  include <sys/mbuf.h>
49ab25eeb5Syz155240 # endif
50ab25eeb5Syz155240 #endif
51ab25eeb5Syz155240 #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
52ab25eeb5Syz155240 # include <sys/filio.h>
53ab25eeb5Syz155240 # include <sys/fcntl.h>
54ab25eeb5Syz155240 # if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM)
55ab25eeb5Syz155240 #  include "opt_ipfilter.h"
56ab25eeb5Syz155240 # endif
57ab25eeb5Syz155240 #else
58ab25eeb5Syz155240 # include <sys/ioctl.h>
59ab25eeb5Syz155240 #endif
60ab25eeb5Syz155240 #if defined(__SVR4) || defined(__svr4__)
61ab25eeb5Syz155240 # include <sys/byteorder.h>
62ab25eeb5Syz155240 # ifdef _KERNEL
63ab25eeb5Syz155240 #  include <sys/dditypes.h>
6424109627Syx160601 #  include <sys/strsubr.h>
65ab25eeb5Syz155240 # endif
66ab25eeb5Syz155240 # include <sys/stream.h>
67ab25eeb5Syz155240 # include <sys/kmem.h>
6824109627Syx160601 # include <sys/pattr.h>
69ab25eeb5Syz155240 #endif
70ab25eeb5Syz155240 #if __FreeBSD__ > 2
71ab25eeb5Syz155240 # include <sys/queue.h>
72ab25eeb5Syz155240 #endif
73ab25eeb5Syz155240 #include <net/if.h>
74ab25eeb5Syz155240 #ifdef sun
75ab25eeb5Syz155240 # include <net/af.h>
76ab25eeb5Syz155240 #endif
77ab25eeb5Syz155240 #include <net/route.h>
78ab25eeb5Syz155240 #include <netinet/in.h>
79ab25eeb5Syz155240 #include <netinet/in_systm.h>
80ab25eeb5Syz155240 #include <netinet/ip.h>
81ab25eeb5Syz155240 #ifndef linux
82ab25eeb5Syz155240 # include <netinet/ip_var.h>
83ab25eeb5Syz155240 #endif
84ab25eeb5Syz155240 #include <netinet/tcp.h>
85ab25eeb5Syz155240 #include <netinet/udp.h>
86ab25eeb5Syz155240 #include <netinet/ip_icmp.h>
87ab25eeb5Syz155240 #include "netinet/ip_compat.h"
88ab25eeb5Syz155240 #include <netinet/tcpip.h>
89f4b3ec61Sdh155122 #include "netinet/ipf_stack.h"
90ab25eeb5Syz155240 #include "netinet/ip_fil.h"
91ab25eeb5Syz155240 #include "netinet/ip_nat.h"
92ab25eeb5Syz155240 #include "netinet/ip_state.h"
93ab25eeb5Syz155240 #include "netinet/ip_proxy.h"
94ab25eeb5Syz155240 #if (__FreeBSD_version >= 300000)
95ab25eeb5Syz155240 # include <sys/malloc.h>
96ab25eeb5Syz155240 #endif
97ab25eeb5Syz155240 
98ab25eeb5Syz155240 #include "netinet/ip_ftp_pxy.c"
99ab25eeb5Syz155240 #include "netinet/ip_rcmd_pxy.c"
100ab25eeb5Syz155240 # include "netinet/ip_pptp_pxy.c"
101ab25eeb5Syz155240 #if defined(_KERNEL)
102ab25eeb5Syz155240 # include "netinet/ip_irc_pxy.c"
103ab25eeb5Syz155240 # include "netinet/ip_raudio_pxy.c"
104ab25eeb5Syz155240 # include "netinet/ip_h323_pxy.c"
105ab25eeb5Syz155240 # include "netinet/ip_netbios_pxy.c"
106ab25eeb5Syz155240 #endif
107ab25eeb5Syz155240 #include "netinet/ip_ipsec_pxy.c"
108ab25eeb5Syz155240 #include "netinet/ip_rpcb_pxy.c"
109ab25eeb5Syz155240 
110ab25eeb5Syz155240 /* END OF INCLUDES */
111ab25eeb5Syz155240 
112ab25eeb5Syz155240 #if !defined(lint)
113ab25eeb5Syz155240 static const char rcsid[] = "@(#)$Id: ip_proxy.c,v 2.62.2.14 2005/06/18 02:41:33 darrenr Exp $";
114ab25eeb5Syz155240 #endif
115ab25eeb5Syz155240 
116ab25eeb5Syz155240 static int appr_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int ));
117ab25eeb5Syz155240 
118ab25eeb5Syz155240 #define	AP_SESS_SIZE	53
119ab25eeb5Syz155240 
120ab25eeb5Syz155240 #if defined(_KERNEL)
121ab25eeb5Syz155240 int		ipf_proxy_debug = 0;
122ab25eeb5Syz155240 #else
123ab25eeb5Syz155240 int		ipf_proxy_debug = 2;
124ab25eeb5Syz155240 #endif
125f4b3ec61Sdh155122 
126f4b3ec61Sdh155122 static aproxy_t	lcl_ap_proxies[] = {
127ab25eeb5Syz155240 #ifdef	IPF_FTP_PROXY
128f4b3ec61Sdh155122 	{ NULL, "ftp", (char)IPPROTO_TCP, 0, 0, NULL, ippr_ftp_init, ippr_ftp_fini,
129ab25eeb5Syz155240 	  ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL },
130ab25eeb5Syz155240 #endif
131ab25eeb5Syz155240 #ifdef	IPF_IRC_PROXY
132f4b3ec61Sdh155122 	{ NULL, "irc", (char)IPPROTO_TCP, 0, 0, NULL, ippr_irc_init, ippr_irc_fini,
133ab25eeb5Syz155240 	  ippr_irc_new, NULL, NULL, ippr_irc_out, NULL, NULL },
134ab25eeb5Syz155240 #endif
135ab25eeb5Syz155240 #ifdef	IPF_RCMD_PROXY
136f4b3ec61Sdh155122 	{ NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, NULL, ippr_rcmd_init, ippr_rcmd_fini,
137ab25eeb5Syz155240 	  ippr_rcmd_new, NULL, ippr_rcmd_in, ippr_rcmd_out, NULL, NULL },
138ab25eeb5Syz155240 #endif
139ab25eeb5Syz155240 #ifdef	IPF_RAUDIO_PROXY
140f4b3ec61Sdh155122 	{ NULL, "raudio", (char)IPPROTO_TCP, 0, 0, NULL, ippr_raudio_init, ippr_raudio_fini,
141ab25eeb5Syz155240 	  ippr_raudio_new, NULL, ippr_raudio_in, ippr_raudio_out, NULL, NULL },
142ab25eeb5Syz155240 #endif
143ab25eeb5Syz155240 #ifdef	IPF_MSNRPC_PROXY
144f4b3ec61Sdh155122 	{ NULL, "msnrpc", (char)IPPROTO_TCP, 0, 0, NULL, ippr_msnrpc_init, ippr_msnrpc_fini,
145ab25eeb5Syz155240 	  ippr_msnrpc_new, NULL, ippr_msnrpc_in, ippr_msnrpc_out, NULL, NULL },
146ab25eeb5Syz155240 #endif
147ab25eeb5Syz155240 #ifdef	IPF_NETBIOS_PROXY
148f4b3ec61Sdh155122 	{ NULL, "netbios", (char)IPPROTO_UDP, 0, 0, NULL, ippr_netbios_init, ippr_netbios_fini,
149ab25eeb5Syz155240 	  NULL, NULL, NULL, ippr_netbios_out, NULL, NULL },
150ab25eeb5Syz155240 #endif
151ab25eeb5Syz155240 #ifdef	IPF_IPSEC_PROXY
152f4b3ec61Sdh155122 	{ NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, NULL,
153ab25eeb5Syz155240 	  ippr_ipsec_init, ippr_ipsec_fini, ippr_ipsec_new, ippr_ipsec_del,
154ab25eeb5Syz155240 	  ippr_ipsec_inout, ippr_ipsec_inout, ippr_ipsec_match, NULL },
155ab25eeb5Syz155240 #endif
156ab25eeb5Syz155240 #ifdef	IPF_PPTP_PROXY
157f4b3ec61Sdh155122 	{ NULL, "pptp", (char)IPPROTO_TCP, 0, 0, NULL,
158ab25eeb5Syz155240 	  ippr_pptp_init, ippr_pptp_fini, ippr_pptp_new, ippr_pptp_del,
159ab25eeb5Syz155240 	  ippr_pptp_inout, ippr_pptp_inout, NULL, NULL },
160ab25eeb5Syz155240 #endif
161ab25eeb5Syz155240 #ifdef  IPF_H323_PROXY
162f4b3ec61Sdh155122 	{ NULL, "h323", (char)IPPROTO_TCP, 0, 0, NULL, ippr_h323_init, ippr_h323_fini,
163ab25eeb5Syz155240 	  ippr_h323_new, ippr_h323_del, ippr_h323_in, NULL, NULL },
164f4b3ec61Sdh155122 	{ NULL, "h245", (char)IPPROTO_TCP, 0, 0, NULL, NULL, NULL,
165ab25eeb5Syz155240 	  ippr_h245_new, NULL, NULL, ippr_h245_out, NULL },
166ab25eeb5Syz155240 #endif
167ab25eeb5Syz155240 #ifdef	IPF_RPCB_PROXY
168ab25eeb5Syz155240 # if 0
169f4b3ec61Sdh155122 	{ NULL, "rpcbt", (char)IPPROTO_TCP, 0, 0, NULL,
170ab25eeb5Syz155240 	  ippr_rpcb_init, ippr_rpcb_fini, ippr_rpcb_new, ippr_rpcb_del,
171ab25eeb5Syz155240 	  ippr_rpcb_in, ippr_rpcb_out, NULL, NULL },
172ab25eeb5Syz155240 # endif
173f4b3ec61Sdh155122 	{ NULL, "rpcbu", (char)IPPROTO_UDP, 0, 0, NULL,
174ab25eeb5Syz155240 	  ippr_rpcb_init, ippr_rpcb_fini, ippr_rpcb_new, ippr_rpcb_del,
175ab25eeb5Syz155240 	  ippr_rpcb_in, ippr_rpcb_out, NULL, NULL },
176ab25eeb5Syz155240 #endif
177f4b3ec61Sdh155122 	{ NULL, "", '\0', 0, 0, NULL, NULL, NULL, NULL, NULL }
178ab25eeb5Syz155240 };
179ab25eeb5Syz155240 
180ab25eeb5Syz155240 /*
181ab25eeb5Syz155240  * Dynamically add a new kernel proxy.  Ensure that it is unique in the
182ab25eeb5Syz155240  * collection compiled in and dynamically added.
183ab25eeb5Syz155240  */
184f4b3ec61Sdh155122 int appr_add(ap, ifs)
185ab25eeb5Syz155240 aproxy_t *ap;
186f4b3ec61Sdh155122 ipf_stack_t *ifs;
187ab25eeb5Syz155240 {
188ab25eeb5Syz155240 	aproxy_t *a;
189ab25eeb5Syz155240 
190f4b3ec61Sdh155122 	for (a = ifs->ifs_ap_proxies; a->apr_p; a++)
191ab25eeb5Syz155240 		if ((a->apr_p == ap->apr_p) &&
192ab25eeb5Syz155240 		    !strncmp(a->apr_label, ap->apr_label,
193ab25eeb5Syz155240 			     sizeof(ap->apr_label))) {
194ab25eeb5Syz155240 			if (ipf_proxy_debug > 1)
195ab25eeb5Syz155240 				printf("appr_add: %s/%d already present (B)\n",
196ab25eeb5Syz155240 				       a->apr_label, a->apr_p);
197ab25eeb5Syz155240 			return -1;
198ab25eeb5Syz155240 		}
199ab25eeb5Syz155240 
200f4b3ec61Sdh155122 	for (a = ifs->ifs_ap_proxylist; a->apr_p; a = a->apr_next)
201ab25eeb5Syz155240 		if ((a->apr_p == ap->apr_p) &&
202ab25eeb5Syz155240 		    !strncmp(a->apr_label, ap->apr_label,
203ab25eeb5Syz155240 			     sizeof(ap->apr_label))) {
204ab25eeb5Syz155240 			if (ipf_proxy_debug > 1)
205ab25eeb5Syz155240 				printf("appr_add: %s/%d already present (D)\n",
206ab25eeb5Syz155240 				       a->apr_label, a->apr_p);
207ab25eeb5Syz155240 			return -1;
208ab25eeb5Syz155240 		}
209f4b3ec61Sdh155122 	ap->apr_next = ifs->ifs_ap_proxylist;
210f4b3ec61Sdh155122 	ifs->ifs_ap_proxylist = ap;
211ab25eeb5Syz155240 	if (ap->apr_init != NULL)
212f4b3ec61Sdh155122 		return (*ap->apr_init)(&ap->apr_private, ifs);
213f4b3ec61Sdh155122 
214ab25eeb5Syz155240 	return 0;
215ab25eeb5Syz155240 }
216ab25eeb5Syz155240 
217ab25eeb5Syz155240 
218ab25eeb5Syz155240 /*
219ab25eeb5Syz155240  * Check to see if the proxy this control request has come through for
220ab25eeb5Syz155240  * exists, and if it does and it has a control function then invoke that
221ab25eeb5Syz155240  * control function.
222ab25eeb5Syz155240  */
223f4b3ec61Sdh155122 int appr_ctl(ctl, ifs)
224ab25eeb5Syz155240 ap_ctl_t *ctl;
225f4b3ec61Sdh155122 ipf_stack_t *ifs;
226ab25eeb5Syz155240 {
227ab25eeb5Syz155240 	aproxy_t *a;
228ab25eeb5Syz155240 	int error;
229ab25eeb5Syz155240 
230f4b3ec61Sdh155122 	a = appr_lookup(ctl->apc_p, ctl->apc_label, ifs);
231ab25eeb5Syz155240 	if (a == NULL) {
232ab25eeb5Syz155240 		if (ipf_proxy_debug > 1)
233ab25eeb5Syz155240 			printf("appr_ctl: can't find %s/%d\n",
234ab25eeb5Syz155240 				ctl->apc_label, ctl->apc_p);
235ab25eeb5Syz155240 		error = ESRCH;
236ab25eeb5Syz155240 	} else if (a->apr_ctl == NULL) {
237ab25eeb5Syz155240 		if (ipf_proxy_debug > 1)
238ab25eeb5Syz155240 			printf("appr_ctl: no ctl function for %s/%d\n",
239ab25eeb5Syz155240 				ctl->apc_label, ctl->apc_p);
240ab25eeb5Syz155240 		error = ENXIO;
241ab25eeb5Syz155240 	} else {
242f4b3ec61Sdh155122 		error = (*a->apr_ctl)(a, ctl, a->apr_private);
243ab25eeb5Syz155240 		if ((error != 0) && (ipf_proxy_debug > 1))
244ab25eeb5Syz155240 			printf("appr_ctl: %s/%d ctl error %d\n",
245ab25eeb5Syz155240 				a->apr_label, a->apr_p, error);
246ab25eeb5Syz155240 	}
247ab25eeb5Syz155240 	return error;
248ab25eeb5Syz155240 }
249ab25eeb5Syz155240 
250ab25eeb5Syz155240 
251ab25eeb5Syz155240 /*
252ab25eeb5Syz155240  * Delete a proxy that has been added dynamically from those available.
253ab25eeb5Syz155240  * If it is in use, return 1 (do not destroy NOW), not in use 0 or -1
254ab25eeb5Syz155240  * if it cannot be matched.
255ab25eeb5Syz155240  */
256f4b3ec61Sdh155122 int appr_del(ap, ifs)
257ab25eeb5Syz155240 aproxy_t *ap;
258f4b3ec61Sdh155122 ipf_stack_t *ifs;
259ab25eeb5Syz155240 {
260ab25eeb5Syz155240 	aproxy_t *a, **app;
261ab25eeb5Syz155240 
262f4b3ec61Sdh155122 	for (app = &ifs->ifs_ap_proxylist; ((a = *app) != NULL);
263f4b3ec61Sdh155122 	     app = &a->apr_next)
264ab25eeb5Syz155240 		if (a == ap) {
265ab25eeb5Syz155240 			a->apr_flags |= APR_DELETE;
266ab25eeb5Syz155240 			*app = a->apr_next;
267ab25eeb5Syz155240 			if (ap->apr_ref != 0) {
268ab25eeb5Syz155240 				if (ipf_proxy_debug > 2)
269ab25eeb5Syz155240 					printf("appr_del: orphaning %s/%d\n",
270ab25eeb5Syz155240 						ap->apr_label, ap->apr_p);
271ab25eeb5Syz155240 				return 1;
272ab25eeb5Syz155240 			}
273ab25eeb5Syz155240 			return 0;
274ab25eeb5Syz155240 		}
275ab25eeb5Syz155240 	if (ipf_proxy_debug > 1)
276ab25eeb5Syz155240 		printf("appr_del: proxy %lx not found\n", (u_long)ap);
277ab25eeb5Syz155240 	return -1;
278ab25eeb5Syz155240 }
279ab25eeb5Syz155240 
280ab25eeb5Syz155240 
281ab25eeb5Syz155240 /*
282ab25eeb5Syz155240  * Return 1 if the packet is a good match against a proxy, else 0.
283ab25eeb5Syz155240  */
284ab25eeb5Syz155240 int appr_ok(fin, tcp, nat)
285ab25eeb5Syz155240 fr_info_t *fin;
286ab25eeb5Syz155240 tcphdr_t *tcp;
287ab25eeb5Syz155240 ipnat_t *nat;
288ab25eeb5Syz155240 {
289ab25eeb5Syz155240 	aproxy_t *apr = nat->in_apr;
290ab25eeb5Syz155240 	u_short dport = nat->in_dport;
291ab25eeb5Syz155240 
292ab25eeb5Syz155240 	if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
293ab25eeb5Syz155240 	    (fin->fin_p != apr->apr_p))
294ab25eeb5Syz155240 		return 0;
295ab25eeb5Syz155240 	if ((tcp == NULL) && dport)
296ab25eeb5Syz155240 		return 0;
297ab25eeb5Syz155240 	return 1;
298ab25eeb5Syz155240 }
299ab25eeb5Syz155240 
300ab25eeb5Syz155240 
301f4b3ec61Sdh155122 int appr_ioctl(data, cmd, mode, ifs)
302ab25eeb5Syz155240 caddr_t data;
303ab25eeb5Syz155240 ioctlcmd_t cmd;
304ab25eeb5Syz155240 int mode;
305f4b3ec61Sdh155122 ipf_stack_t *ifs;
306ab25eeb5Syz155240 {
307ab25eeb5Syz155240 	ap_ctl_t ctl;
308ab25eeb5Syz155240 	caddr_t ptr;
309ab25eeb5Syz155240 	int error;
310ab25eeb5Syz155240 
311ab25eeb5Syz155240 	mode = mode;	/* LINT */
312ab25eeb5Syz155240 
313ab25eeb5Syz155240 	switch (cmd)
314ab25eeb5Syz155240 	{
315ab25eeb5Syz155240 	case SIOCPROXY :
316bb1d9de5SJohn Ojemann 		error = BCOPYIN(data, &ctl, sizeof(ctl));
317bb1d9de5SJohn Ojemann 		if (error != 0)
318bb1d9de5SJohn Ojemann 			return EFAULT;
319bb1d9de5SJohn Ojemann 
320ab25eeb5Syz155240 		ptr = NULL;
321ab25eeb5Syz155240 
322ab25eeb5Syz155240 		if (ctl.apc_dsize > 0) {
323ab25eeb5Syz155240 			KMALLOCS(ptr, caddr_t, ctl.apc_dsize);
324ab25eeb5Syz155240 			if (ptr == NULL)
325ab25eeb5Syz155240 				error = ENOMEM;
326ab25eeb5Syz155240 			else {
327ab25eeb5Syz155240 				error = copyinptr(ctl.apc_data, ptr,
328ab25eeb5Syz155240 						  ctl.apc_dsize);
329ab25eeb5Syz155240 				if (error == 0)
330ab25eeb5Syz155240 					ctl.apc_data = ptr;
331ab25eeb5Syz155240 			}
332ab25eeb5Syz155240 		} else {
333ab25eeb5Syz155240 			ctl.apc_data = NULL;
334ab25eeb5Syz155240 			error = 0;
335ab25eeb5Syz155240 		}
336ab25eeb5Syz155240 
337ab25eeb5Syz155240 		if (error == 0)
338f4b3ec61Sdh155122 			error = appr_ctl(&ctl, ifs);
339ab25eeb5Syz155240 
340ab25eeb5Syz155240 		if ((ctl.apc_dsize > 0) && (ptr != NULL) &&
341ab25eeb5Syz155240 		    (ctl.apc_data == ptr)) {
342ab25eeb5Syz155240 			KFREES(ptr, ctl.apc_dsize);
343ab25eeb5Syz155240 		}
344ab25eeb5Syz155240 		break;
345ab25eeb5Syz155240 
346ab25eeb5Syz155240 	default :
347ab25eeb5Syz155240 		error = EINVAL;
348ab25eeb5Syz155240 	}
349ab25eeb5Syz155240 	return error;
350ab25eeb5Syz155240 }
351ab25eeb5Syz155240 
352ab25eeb5Syz155240 
353ab25eeb5Syz155240 /*
354ab25eeb5Syz155240  * If a proxy has a match function, call that to do extended packet
355ab25eeb5Syz155240  * matching.
356ab25eeb5Syz155240  */
357ab25eeb5Syz155240 int appr_match(fin, nat)
358ab25eeb5Syz155240 fr_info_t *fin;
359ab25eeb5Syz155240 nat_t *nat;
360ab25eeb5Syz155240 {
361ab25eeb5Syz155240 	aproxy_t *apr;
362ab25eeb5Syz155240 	ipnat_t *ipn;
363ab25eeb5Syz155240 	int result;
364ab25eeb5Syz155240 
365ab25eeb5Syz155240 	ipn = nat->nat_ptr;
366ab25eeb5Syz155240 	if (ipf_proxy_debug > 8)
367ab25eeb5Syz155240 		printf("appr_match(%lx,%lx) aps %lx ptr %lx\n",
368ab25eeb5Syz155240 			(u_long)fin, (u_long)nat, (u_long)nat->nat_aps,
369ab25eeb5Syz155240 			(u_long)ipn);
370ab25eeb5Syz155240 
371ab25eeb5Syz155240 	if ((fin->fin_flx & (FI_SHORT|FI_BAD)) != 0) {
372ab25eeb5Syz155240 		if (ipf_proxy_debug > 0)
373ab25eeb5Syz155240 			printf("appr_match: flx 0x%x (BAD|SHORT)\n",
374ab25eeb5Syz155240 				fin->fin_flx);
375ab25eeb5Syz155240 		return -1;
376ab25eeb5Syz155240 	}
377ab25eeb5Syz155240 
378ab25eeb5Syz155240 	apr = ipn->in_apr;
379ab25eeb5Syz155240 	if ((apr == NULL) || (apr->apr_flags & APR_DELETE)) {
380ab25eeb5Syz155240 		if (ipf_proxy_debug > 0)
381ab25eeb5Syz155240 			printf("appr_match:apr %lx apr_flags 0x%x\n",
382ab25eeb5Syz155240 				(u_long)apr, apr ? apr->apr_flags : 0);
383ab25eeb5Syz155240 		return -1;
384ab25eeb5Syz155240 	}
385ab25eeb5Syz155240 
386ab25eeb5Syz155240 	if (apr->apr_match != NULL) {
387f4b3ec61Sdh155122 		result = (*apr->apr_match)(fin, nat->nat_aps, nat, apr->apr_private);
388ab25eeb5Syz155240 		if (result != 0) {
389ab25eeb5Syz155240 			if (ipf_proxy_debug > 4)
390ab25eeb5Syz155240 				printf("appr_match: result %d\n", result);
391ab25eeb5Syz155240 			return -1;
392ab25eeb5Syz155240 		}
393ab25eeb5Syz155240 	}
394ab25eeb5Syz155240 	return 0;
395ab25eeb5Syz155240 }
396ab25eeb5Syz155240 
397ab25eeb5Syz155240 
398ab25eeb5Syz155240 /*
399ab25eeb5Syz155240  * Allocate a new application proxy structure and fill it in with the
400ab25eeb5Syz155240  * relevant details.  call the init function once complete, prior to
401ab25eeb5Syz155240  * returning.
402ab25eeb5Syz155240  */
403ab25eeb5Syz155240 int appr_new(fin, nat)
404ab25eeb5Syz155240 fr_info_t *fin;
405ab25eeb5Syz155240 nat_t *nat;
406ab25eeb5Syz155240 {
407ab25eeb5Syz155240 	register ap_session_t *aps;
408ab25eeb5Syz155240 	aproxy_t *apr;
409f4b3ec61Sdh155122 	ipf_stack_t *ifs = fin->fin_ifs;
410ab25eeb5Syz155240 
411ab25eeb5Syz155240 	if (ipf_proxy_debug > 8)
412ab25eeb5Syz155240 		printf("appr_new(%lx,%lx) \n", (u_long)fin, (u_long)nat);
413ab25eeb5Syz155240 
414ab25eeb5Syz155240 	if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL)) {
415ab25eeb5Syz155240 		if (ipf_proxy_debug > 0)
416ab25eeb5Syz155240 			printf("appr_new: nat_ptr %lx nat_aps %lx\n",
417ab25eeb5Syz155240 				(u_long)nat->nat_ptr, (u_long)nat->nat_aps);
418ab25eeb5Syz155240 		return -1;
419ab25eeb5Syz155240 	}
420ab25eeb5Syz155240 
421ab25eeb5Syz155240 	apr = nat->nat_ptr->in_apr;
422ab25eeb5Syz155240 
423ab25eeb5Syz155240 	if ((apr->apr_flags & APR_DELETE) ||
424ab25eeb5Syz155240 	    (fin->fin_p != apr->apr_p)) {
425ab25eeb5Syz155240 		if (ipf_proxy_debug > 2)
426ab25eeb5Syz155240 			printf("appr_new: apr_flags 0x%x p %d/%d\n",
427ab25eeb5Syz155240 				apr->apr_flags, fin->fin_p, apr->apr_p);
428ab25eeb5Syz155240 		return -1;
429ab25eeb5Syz155240 	}
430ab25eeb5Syz155240 
431ab25eeb5Syz155240 	KMALLOC(aps, ap_session_t *);
432ab25eeb5Syz155240 	if (!aps) {
433ab25eeb5Syz155240 		if (ipf_proxy_debug > 0)
434ab25eeb5Syz155240 			printf("appr_new: malloc failed (%lu)\n",
435ab25eeb5Syz155240 				(u_long)sizeof(ap_session_t));
436ab25eeb5Syz155240 		return -1;
437ab25eeb5Syz155240 	}
438ab25eeb5Syz155240 
439ab25eeb5Syz155240 	bzero((char *)aps, sizeof(*aps));
440ab25eeb5Syz155240 	aps->aps_p = fin->fin_p;
441ab25eeb5Syz155240 	aps->aps_data = NULL;
442ab25eeb5Syz155240 	aps->aps_apr = apr;
443ab25eeb5Syz155240 	aps->aps_psiz = 0;
444ab25eeb5Syz155240 	if (apr->apr_new != NULL)
445f4b3ec61Sdh155122 		if ((*apr->apr_new)(fin, aps, nat, apr->apr_private) == -1) {
446ab25eeb5Syz155240 			if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
447ab25eeb5Syz155240 				KFREES(aps->aps_data, aps->aps_psiz);
448ab25eeb5Syz155240 			}
449ab25eeb5Syz155240 			KFREE(aps);
450ab25eeb5Syz155240 			if (ipf_proxy_debug > 2)
451ab25eeb5Syz155240 				printf("appr_new: new(%lx) failed\n",
452ab25eeb5Syz155240 					(u_long)apr->apr_new);
453ab25eeb5Syz155240 			return -1;
454ab25eeb5Syz155240 		}
455ab25eeb5Syz155240 	aps->aps_nat = nat;
456f4b3ec61Sdh155122 	aps->aps_next = ifs->ifs_ap_sess_list;
457f4b3ec61Sdh155122 	ifs->ifs_ap_sess_list = aps;
458ab25eeb5Syz155240 	nat->nat_aps = aps;
459ab25eeb5Syz155240 
460ab25eeb5Syz155240 	return 0;
461ab25eeb5Syz155240 }
462ab25eeb5Syz155240 
463ab25eeb5Syz155240 
464ab25eeb5Syz155240 /*
465ab25eeb5Syz155240  * Check to see if a packet should be passed through an active proxy routine
466ab25eeb5Syz155240  * if one has been setup for it.  We don't need to check the checksum here if
467ab25eeb5Syz155240  * IPFILTER_CKSUM is defined because if it is, a failed check causes FI_BAD
468ab25eeb5Syz155240  * to be set.
469ab25eeb5Syz155240  */
470ab25eeb5Syz155240 int appr_check(fin, nat)
471ab25eeb5Syz155240 fr_info_t *fin;
472ab25eeb5Syz155240 nat_t *nat;
473ab25eeb5Syz155240 {
474*f56257d8SToomas Soome #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6) && \
475*f56257d8SToomas Soome 	defined(ICK_VALID)
476ab25eeb5Syz155240 	mb_t *m;
477ab25eeb5Syz155240 	int dosum = 1;
478ab25eeb5Syz155240 #endif
479ab25eeb5Syz155240 	tcphdr_t *tcp = NULL;
480ab25eeb5Syz155240 	udphdr_t *udp = NULL;
48124109627Syx160601 	void *tcpudp = NULL;
48224109627Syx160601 	u_short *csump;
483ab25eeb5Syz155240 	ap_session_t *aps;
484ab25eeb5Syz155240 	aproxy_t *apr;
485ab25eeb5Syz155240 	ip_t *ip;
486ab25eeb5Syz155240 	short rv;
487ab25eeb5Syz155240 	int err;
488ab25eeb5Syz155240 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi)
489ab25eeb5Syz155240 	u_32_t s1, s2, sd;
490ab25eeb5Syz155240 #endif
491f4b3ec61Sdh155122 	ipf_stack_t *ifs = fin->fin_ifs;
492f4b3ec61Sdh155122 
493*f56257d8SToomas Soome #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6)
4947ddc9b1aSDarren Reed 	net_handle_t net_data_p;
495381a2a9aSdr146992 	if (fin->fin_v == 4)
496f4b3ec61Sdh155122 		net_data_p = ifs->ifs_ipf_ipv4;
497381a2a9aSdr146992 	else
498f4b3ec61Sdh155122 		net_data_p = ifs->ifs_ipf_ipv6;
499381a2a9aSdr146992 #endif
500ab25eeb5Syz155240 
501ab25eeb5Syz155240 	if (fin->fin_flx & FI_BAD) {
502ab25eeb5Syz155240 		if (ipf_proxy_debug > 0)
503ab25eeb5Syz155240 			printf("appr_check: flx 0x%x (BAD)\n", fin->fin_flx);
504ab25eeb5Syz155240 		return -1;
505ab25eeb5Syz155240 	}
506ab25eeb5Syz155240 
507ab25eeb5Syz155240 #ifndef IPFILTER_CKSUM
508ab25eeb5Syz155240 	if ((fin->fin_out == 0) && (fr_checkl4sum(fin) == -1)) {
509ab25eeb5Syz155240 		if (ipf_proxy_debug > 0)
510ab25eeb5Syz155240 			printf("appr_check: l4 checksum failure %d\n",
511ab25eeb5Syz155240 				fin->fin_p);
512ab25eeb5Syz155240 		if (fin->fin_p == IPPROTO_TCP)
513f4b3ec61Sdh155122 			ifs->ifs_frstats[fin->fin_out].fr_tcpbad++;
514ab25eeb5Syz155240 		return -1;
515ab25eeb5Syz155240 	}
516ab25eeb5Syz155240 #endif
517ab25eeb5Syz155240 
518ab25eeb5Syz155240 	aps = nat->nat_aps;
519ab25eeb5Syz155240 	if ((aps != NULL) && (aps->aps_p == fin->fin_p)) {
520ab25eeb5Syz155240 		/*
521ab25eeb5Syz155240 		 * If there is data in this packet to be proxied then try and
522ab25eeb5Syz155240 		 * get it all into the one buffer, else drop it.
523ab25eeb5Syz155240 		 */
524ab25eeb5Syz155240 #if defined(MENTAT) || defined(HAVE_M_PULLDOWN)
525ab25eeb5Syz155240 		if ((fin->fin_dlen > 0) && !(fin->fin_flx & FI_COALESCE))
526ab25eeb5Syz155240 			if (fr_coalesce(fin) == -1) {
527ab25eeb5Syz155240 				if (ipf_proxy_debug > 0)
528ab25eeb5Syz155240 					printf("appr_check: fr_coalesce failed %x\n", fin->fin_flx);
529ab25eeb5Syz155240 				return -1;
530ab25eeb5Syz155240 			}
531ab25eeb5Syz155240 #endif
532ab25eeb5Syz155240 		ip = fin->fin_ip;
533ab25eeb5Syz155240 
534ab25eeb5Syz155240 		switch (fin->fin_p)
535ab25eeb5Syz155240 		{
536ab25eeb5Syz155240 		case IPPROTO_TCP :
537ab25eeb5Syz155240 			tcp = (tcphdr_t *)fin->fin_dp;
538ab25eeb5Syz155240 
539*f56257d8SToomas Soome #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6) && \
540*f56257d8SToomas Soome 	defined(ICK_VALID)
541ab25eeb5Syz155240 			m = fin->fin_qfm;
542ab25eeb5Syz155240 			if (dohwcksum && (m->b_ick_flag == ICK_VALID))
543ab25eeb5Syz155240 				dosum = 0;
544ab25eeb5Syz155240 #endif
545ab25eeb5Syz155240 			/*
546ab25eeb5Syz155240 			 * Don't bother the proxy with these...or in fact,
547ab25eeb5Syz155240 			 * should we free up proxy stuff when seen?
548ab25eeb5Syz155240 			 */
549ab25eeb5Syz155240 			if ((fin->fin_tcpf & TH_RST) != 0)
550ab25eeb5Syz155240 				break;
551ab25eeb5Syz155240 			/*FALLTHROUGH*/
552ab25eeb5Syz155240 		case IPPROTO_UDP :
553ab25eeb5Syz155240 			udp = (udphdr_t *)fin->fin_dp;
554ab25eeb5Syz155240 			break;
555ab25eeb5Syz155240 		default :
556ab25eeb5Syz155240 			break;
557ab25eeb5Syz155240 		}
558ab25eeb5Syz155240 
559ab25eeb5Syz155240 		apr = aps->aps_apr;
560ab25eeb5Syz155240 		err = 0;
561ab25eeb5Syz155240 		if (fin->fin_out != 0) {
562ab25eeb5Syz155240 			if (apr->apr_outpkt != NULL)
563f4b3ec61Sdh155122 				err = (*apr->apr_outpkt)(fin, aps, nat, apr->apr_private);
564ab25eeb5Syz155240 		} else {
565ab25eeb5Syz155240 			if (apr->apr_inpkt != NULL)
566f4b3ec61Sdh155122 				err = (*apr->apr_inpkt)(fin, aps, nat, apr->apr_private);
567ab25eeb5Syz155240 		}
568ab25eeb5Syz155240 
569ab25eeb5Syz155240 		rv = APR_EXIT(err);
570ab25eeb5Syz155240 		if (((ipf_proxy_debug > 0) && (rv != 0)) ||
571ab25eeb5Syz155240 		    (ipf_proxy_debug > 8))
572ab25eeb5Syz155240 			printf("appr_check: out %d err %x rv %d\n",
573ab25eeb5Syz155240 				fin->fin_out, err, rv);
574ab25eeb5Syz155240 		if (rv == 1)
575ab25eeb5Syz155240 			return -1;
576ab25eeb5Syz155240 
577ab25eeb5Syz155240 		if (rv == 2) {
578ab25eeb5Syz155240 			appr_free(apr);
579ab25eeb5Syz155240 			nat->nat_aps = NULL;
580ab25eeb5Syz155240 			return -1;
581ab25eeb5Syz155240 		}
582ab25eeb5Syz155240 
583ab25eeb5Syz155240 		/*
584ab25eeb5Syz155240 		 * If err != 0 then the data size of the packet has changed
585ab25eeb5Syz155240 		 * so we need to recalculate the header checksums for the
586ab25eeb5Syz155240 		 * packet.
587381a2a9aSdr146992 		 * inbound packets always need to be adjusted.
588ab25eeb5Syz155240 		 */
589ab25eeb5Syz155240 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi)
59024109627Syx160601 		if (err != 0) {
591ab25eeb5Syz155240 			short adjlen = err & 0xffff;
592ab25eeb5Syz155240 
593ab25eeb5Syz155240 			s1 = LONG_SUM(ip->ip_len - adjlen);
594ab25eeb5Syz155240 			s2 = LONG_SUM(ip->ip_len);
595ab25eeb5Syz155240 			CALC_SUMD(s1, s2, sd);
59624109627Syx160601 			sd = (sd & 0xffff) + (sd >> 16);
59724109627Syx160601 			if (!fin->fin_out ||
59824109627Syx160601 			    !NET_IS_HCK_L3_FULL(net_data_p, fin->fin_m))
599381a2a9aSdr146992 				fix_outcksum(&ip->ip_sum, sd);
600ab25eeb5Syz155240 		}
601ab25eeb5Syz155240 #endif
602ab25eeb5Syz155240 
603ab25eeb5Syz155240 		/*
604ab25eeb5Syz155240 		 * For TCP packets, we may need to adjust the sequence and
605ab25eeb5Syz155240 		 * acknowledgement numbers to reflect changes in size of the
606ab25eeb5Syz155240 		 * data stream.
607ab25eeb5Syz155240 		 *
60824109627Syx160601 		 * For both TCP and UDP, recalculate the layer 4 checksum in
60924109627Syx160601 		 * software checksum case, as we can't tell if data has been
610ab25eeb5Syz155240 		 * changed or not.
611ab25eeb5Syz155240 		 */
612ab25eeb5Syz155240 		if (tcp != NULL) {
61324109627Syx160601 			tcpudp = tcp;
61424109627Syx160601 			csump = &tcp->th_sum;
61524109627Syx160601 			(void) appr_fixseqack(fin, ip, aps, APR_INC(err));
61624109627Syx160601 		} else if (udp != NULL) {
61724109627Syx160601 			tcpudp = udp;
61824109627Syx160601 			csump = &udp->uh_sum;
61924109627Syx160601 		}
62024109627Syx160601 
62124109627Syx160601 		if (tcpudp) {
622*f56257d8SToomas Soome #if defined(SOLARIS) && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(MENTAT)
62324109627Syx160601 			if (!fin->fin_out) {
62424109627Syx160601 				/*
62524109627Syx160601 				 * We are incapable of adjusting partial hcksum
62624109627Syx160601 				 * result for inbound packets here, as the
62724109627Syx160601 				 * partial hcksum calculation range might not
62824109627Syx160601 				 * cover the whole payload, and the payload data
62924109627Syx160601 				 * might be changed by proxy.
63024109627Syx160601 				 */
63124109627Syx160601 				DB_CKSUMFLAGS(fin->fin_m) &= ~HCK_PARTIALCKSUM;
63224109627Syx160601 
63324109627Syx160601 				/* Inbound packets always need recalculation. */
63424109627Syx160601 				*csump = fr_cksum(fin->fin_qfm, ip,
63524109627Syx160601 						  fin->fin_p, tcpudp);
63624109627Syx160601 			} else if (NET_IS_HCK_L4_PART(net_data_p, fin->fin_m)) {
63724109627Syx160601 				if (err != 0) {
63824109627Syx160601 					DB_CKSUMEND(fin->fin_m) += (short)err;
63924109627Syx160601 					fix_incksum(csump, sd);
64024109627Syx160601 				}
64124109627Syx160601 			} else if (!NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m))
64224109627Syx160601 				*csump = fr_cksum(fin->fin_qfm, ip,
64324109627Syx160601 						  fin->fin_p, tcpudp);
644ab25eeb5Syz155240 #else
64524109627Syx160601 			*csump = fr_cksum(fin->fin_m, ip, fin->fin_p, tcpudp);
646ab25eeb5Syz155240 #endif
647ab25eeb5Syz155240 		}
648ab25eeb5Syz155240 		aps->aps_bytes += fin->fin_plen;
649ab25eeb5Syz155240 		aps->aps_pkts++;
650ab25eeb5Syz155240 		return 1;
651ab25eeb5Syz155240 	}
652ab25eeb5Syz155240 	return 0;
653ab25eeb5Syz155240 }
654ab25eeb5Syz155240 
655ab25eeb5Syz155240 
656ab25eeb5Syz155240 /*
657ab25eeb5Syz155240  * Search for an proxy by the protocol it is being used with and its name.
658ab25eeb5Syz155240  */
659f4b3ec61Sdh155122 aproxy_t *appr_lookup(pr, name, ifs)
660ab25eeb5Syz155240 u_int pr;
661ab25eeb5Syz155240 char *name;
662f4b3ec61Sdh155122 ipf_stack_t *ifs;
663ab25eeb5Syz155240 {
664ab25eeb5Syz155240 	aproxy_t *ap;
665ab25eeb5Syz155240 
666ab25eeb5Syz155240 	if (ipf_proxy_debug > 8)
667ab25eeb5Syz155240 		printf("appr_lookup(%d,%s)\n", pr, name);
668ab25eeb5Syz155240 
669f4b3ec61Sdh155122 	for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++)
670ab25eeb5Syz155240 		if ((ap->apr_p == pr) &&
671ab25eeb5Syz155240 		    !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
672ab25eeb5Syz155240 			ap->apr_ref++;
673ab25eeb5Syz155240 			return ap;
674ab25eeb5Syz155240 		}
675ab25eeb5Syz155240 
676f4b3ec61Sdh155122 	for (ap = ifs->ifs_ap_proxylist; ap; ap = ap->apr_next)
677ab25eeb5Syz155240 		if ((ap->apr_p == pr) &&
678ab25eeb5Syz155240 		    !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
679ab25eeb5Syz155240 			ap->apr_ref++;
680ab25eeb5Syz155240 			return ap;
681ab25eeb5Syz155240 		}
682ab25eeb5Syz155240 	if (ipf_proxy_debug > 2)
683ab25eeb5Syz155240 		printf("appr_lookup: failed for %d/%s\n", pr, name);
684ab25eeb5Syz155240 	return NULL;
685ab25eeb5Syz155240 }
686ab25eeb5Syz155240 
687ab25eeb5Syz155240 
688ab25eeb5Syz155240 void appr_free(ap)
689ab25eeb5Syz155240 aproxy_t *ap;
690ab25eeb5Syz155240 {
691ab25eeb5Syz155240 	ap->apr_ref--;
692ab25eeb5Syz155240 }
693ab25eeb5Syz155240 
694ab25eeb5Syz155240 
695f4b3ec61Sdh155122 void aps_free(aps, ifs)
696ab25eeb5Syz155240 ap_session_t *aps;
697f4b3ec61Sdh155122 ipf_stack_t *ifs;
698ab25eeb5Syz155240 {
699ab25eeb5Syz155240 	ap_session_t *a, **ap;
700ab25eeb5Syz155240 	aproxy_t *apr;
701ab25eeb5Syz155240 
702ab25eeb5Syz155240 	if (!aps)
703ab25eeb5Syz155240 		return;
704ab25eeb5Syz155240 
705f4b3ec61Sdh155122 	for (ap = &ifs->ifs_ap_sess_list; ((a = *ap) != NULL); ap = &a->aps_next)
706ab25eeb5Syz155240 		if (a == aps) {
707ab25eeb5Syz155240 			*ap = a->aps_next;
708ab25eeb5Syz155240 			break;
709ab25eeb5Syz155240 		}
710ab25eeb5Syz155240 
711ab25eeb5Syz155240 	apr = aps->aps_apr;
712ab25eeb5Syz155240 	if ((apr != NULL) && (apr->apr_del != NULL))
713f4b3ec61Sdh155122 		(*apr->apr_del)(aps, apr->apr_private, ifs);
714ab25eeb5Syz155240 
715ab25eeb5Syz155240 	if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
716ab25eeb5Syz155240 		KFREES(aps->aps_data, aps->aps_psiz);
717ab25eeb5Syz155240 	KFREE(aps);
718ab25eeb5Syz155240 }
719ab25eeb5Syz155240 
720ab25eeb5Syz155240 
721ab25eeb5Syz155240 /*
722ab25eeb5Syz155240  * returns 2 if ack or seq number in TCP header is changed, returns 0 otherwise
723ab25eeb5Syz155240  */
724ab25eeb5Syz155240 static int appr_fixseqack(fin, ip, aps, inc)
725ab25eeb5Syz155240 fr_info_t *fin;
726ab25eeb5Syz155240 ip_t *ip;
727ab25eeb5Syz155240 ap_session_t *aps;
728ab25eeb5Syz155240 int inc;
729ab25eeb5Syz155240 {
730ab25eeb5Syz155240 	int sel, ch = 0, out, nlen;
731ab25eeb5Syz155240 	u_32_t seq1, seq2;
732ab25eeb5Syz155240 	tcphdr_t *tcp;
733ab25eeb5Syz155240 	short inc2;
734ab25eeb5Syz155240 
735ab25eeb5Syz155240 	tcp = (tcphdr_t *)fin->fin_dp;
736ab25eeb5Syz155240 	out = fin->fin_out;
737ab25eeb5Syz155240 	/*
738ab25eeb5Syz155240 	 * ip_len has already been adjusted by 'inc'.
739ab25eeb5Syz155240 	 */
740ab25eeb5Syz155240 	nlen = ip->ip_len;
741ab25eeb5Syz155240 	nlen -= (IP_HL(ip) << 2) + (TCP_OFF(tcp) << 2);
742ab25eeb5Syz155240 
743ab25eeb5Syz155240 	inc2 = inc;
744ab25eeb5Syz155240 	inc = (int)inc2;
745ab25eeb5Syz155240 
746ab25eeb5Syz155240 	if (out != 0) {
747ab25eeb5Syz155240 		seq1 = (u_32_t)ntohl(tcp->th_seq);
748ab25eeb5Syz155240 		sel = aps->aps_sel[out];
749ab25eeb5Syz155240 
750ab25eeb5Syz155240 		/* switch to other set ? */
751ab25eeb5Syz155240 		if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
752ab25eeb5Syz155240 		    (seq1 > aps->aps_seqmin[!sel])) {
753ab25eeb5Syz155240 			if (ipf_proxy_debug > 7)
754ab25eeb5Syz155240 				printf("proxy out switch set seq %d -> %d %x > %x\n",
755ab25eeb5Syz155240 					sel, !sel, seq1,
756ab25eeb5Syz155240 					aps->aps_seqmin[!sel]);
757ab25eeb5Syz155240 			sel = aps->aps_sel[out] = !sel;
758ab25eeb5Syz155240 		}
759ab25eeb5Syz155240 
760ab25eeb5Syz155240 		if (aps->aps_seqoff[sel]) {
761ab25eeb5Syz155240 			seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
762ab25eeb5Syz155240 			if (seq1 > seq2) {
763ab25eeb5Syz155240 				seq2 = aps->aps_seqoff[sel];
764ab25eeb5Syz155240 				seq1 += seq2;
765ab25eeb5Syz155240 				tcp->th_seq = htonl(seq1);
766ab25eeb5Syz155240 				ch = 1;
767ab25eeb5Syz155240 			}
768ab25eeb5Syz155240 		}
769ab25eeb5Syz155240 
770ab25eeb5Syz155240 		if (inc && (seq1 > aps->aps_seqmin[!sel])) {
771ab25eeb5Syz155240 			aps->aps_seqmin[sel] = seq1 + nlen - 1;
772ab25eeb5Syz155240 			aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
773ab25eeb5Syz155240 			if (ipf_proxy_debug > 7)
774ab25eeb5Syz155240 				printf("proxy seq set %d at %x to %d + %d\n",
775ab25eeb5Syz155240 					sel, aps->aps_seqmin[sel],
776ab25eeb5Syz155240 					aps->aps_seqoff[sel], inc);
777ab25eeb5Syz155240 		}
778ab25eeb5Syz155240 
779ab25eeb5Syz155240 		/***/
780ab25eeb5Syz155240 
781ab25eeb5Syz155240 		seq1 = ntohl(tcp->th_ack);
782ab25eeb5Syz155240 		sel = aps->aps_sel[1 - out];
783ab25eeb5Syz155240 
784ab25eeb5Syz155240 		/* switch to other set ? */
785ab25eeb5Syz155240 		if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
786ab25eeb5Syz155240 		    (seq1 > aps->aps_ackmin[!sel])) {
787ab25eeb5Syz155240 			if (ipf_proxy_debug > 7)
788ab25eeb5Syz155240 				printf("proxy out switch set ack %d -> %d %x > %x\n",
789ab25eeb5Syz155240 					sel, !sel, seq1,
790ab25eeb5Syz155240 					aps->aps_ackmin[!sel]);
791ab25eeb5Syz155240 			sel = aps->aps_sel[1 - out] = !sel;
792ab25eeb5Syz155240 		}
793ab25eeb5Syz155240 
794ab25eeb5Syz155240 		if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
795ab25eeb5Syz155240 			seq2 = aps->aps_ackoff[sel];
796ab25eeb5Syz155240 			tcp->th_ack = htonl(seq1 - seq2);
797ab25eeb5Syz155240 			ch = 1;
798ab25eeb5Syz155240 		}
799ab25eeb5Syz155240 	} else {
800ab25eeb5Syz155240 		seq1 = ntohl(tcp->th_seq);
801ab25eeb5Syz155240 		sel = aps->aps_sel[out];
802ab25eeb5Syz155240 
803ab25eeb5Syz155240 		/* switch to other set ? */
804ab25eeb5Syz155240 		if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
805ab25eeb5Syz155240 		    (seq1 > aps->aps_ackmin[!sel])) {
806ab25eeb5Syz155240 			if (ipf_proxy_debug > 7)
807ab25eeb5Syz155240 				printf("proxy in switch set ack %d -> %d %x > %x\n",
808ab25eeb5Syz155240 					sel, !sel, seq1, aps->aps_ackmin[!sel]);
809ab25eeb5Syz155240 			sel = aps->aps_sel[out] = !sel;
810ab25eeb5Syz155240 		}
811ab25eeb5Syz155240 
812ab25eeb5Syz155240 		if (aps->aps_ackoff[sel]) {
813ab25eeb5Syz155240 			seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
814ab25eeb5Syz155240 			if (seq1 > seq2) {
815ab25eeb5Syz155240 				seq2 = aps->aps_ackoff[sel];
816ab25eeb5Syz155240 				seq1 += seq2;
817ab25eeb5Syz155240 				tcp->th_seq = htonl(seq1);
818ab25eeb5Syz155240 				ch = 1;
819ab25eeb5Syz155240 			}
820ab25eeb5Syz155240 		}
821ab25eeb5Syz155240 
822ab25eeb5Syz155240 		if (inc && (seq1 > aps->aps_ackmin[!sel])) {
823ab25eeb5Syz155240 			aps->aps_ackmin[!sel] = seq1 + nlen - 1;
824ab25eeb5Syz155240 			aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
825ab25eeb5Syz155240 
826ab25eeb5Syz155240 			if (ipf_proxy_debug > 7)
827ab25eeb5Syz155240 				printf("proxy ack set %d at %x to %d + %d\n",
828ab25eeb5Syz155240 					!sel, aps->aps_seqmin[!sel],
829ab25eeb5Syz155240 					aps->aps_seqoff[sel], inc);
830ab25eeb5Syz155240 		}
831ab25eeb5Syz155240 
832ab25eeb5Syz155240 		/***/
833ab25eeb5Syz155240 
834ab25eeb5Syz155240 		seq1 = ntohl(tcp->th_ack);
835ab25eeb5Syz155240 		sel = aps->aps_sel[1 - out];
836ab25eeb5Syz155240 
837ab25eeb5Syz155240 		/* switch to other set ? */
838ab25eeb5Syz155240 		if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
839ab25eeb5Syz155240 		    (seq1 > aps->aps_seqmin[!sel])) {
840ab25eeb5Syz155240 			if (ipf_proxy_debug > 7)
841ab25eeb5Syz155240 				printf("proxy in switch set seq %d -> %d %x > %x\n",
842ab25eeb5Syz155240 					sel, !sel, seq1, aps->aps_seqmin[!sel]);
843ab25eeb5Syz155240 			sel = aps->aps_sel[1 - out] = !sel;
844ab25eeb5Syz155240 		}
845ab25eeb5Syz155240 
846ab25eeb5Syz155240 		if (aps->aps_seqoff[sel] != 0) {
847ab25eeb5Syz155240 			if (ipf_proxy_debug > 7)
848ab25eeb5Syz155240 				printf("sel %d seqoff %d seq1 %x seqmin %x\n",
849ab25eeb5Syz155240 					sel, aps->aps_seqoff[sel], seq1,
850ab25eeb5Syz155240 					aps->aps_seqmin[sel]);
851ab25eeb5Syz155240 			if (seq1 > aps->aps_seqmin[sel]) {
852ab25eeb5Syz155240 				seq2 = aps->aps_seqoff[sel];
853ab25eeb5Syz155240 				tcp->th_ack = htonl(seq1 - seq2);
854ab25eeb5Syz155240 				ch = 1;
855ab25eeb5Syz155240 			}
856ab25eeb5Syz155240 		}
857ab25eeb5Syz155240 	}
858ab25eeb5Syz155240 
859ab25eeb5Syz155240 	if (ipf_proxy_debug > 8)
860ab25eeb5Syz155240 		printf("appr_fixseqack: seq %x ack %x\n",
861ab25eeb5Syz155240 			ntohl(tcp->th_seq), ntohl(tcp->th_ack));
862ab25eeb5Syz155240 	return ch ? 2 : 0;
863ab25eeb5Syz155240 }
864ab25eeb5Syz155240 
865ab25eeb5Syz155240 
866ab25eeb5Syz155240 /*
867ab25eeb5Syz155240  * Initialise hook for kernel application proxies.
868ab25eeb5Syz155240  * Call the initialise routine for all the compiled in kernel proxies.
869ab25eeb5Syz155240  */
870f4b3ec61Sdh155122 int appr_init(ifs)
871f4b3ec61Sdh155122 ipf_stack_t *ifs;
872ab25eeb5Syz155240 {
873ab25eeb5Syz155240 	aproxy_t *ap;
874ab25eeb5Syz155240 	int err =  0;
875ab25eeb5Syz155240 
876f4b3ec61Sdh155122 	/* Since the refcnt is used we make a copy of lcl_ap_proxies */
877f4b3ec61Sdh155122 	KMALLOCS(ifs->ifs_ap_proxies, aproxy_t *, sizeof (lcl_ap_proxies));
878f4b3ec61Sdh155122 	bcopy(lcl_ap_proxies, ifs->ifs_ap_proxies, sizeof (lcl_ap_proxies));
879f4b3ec61Sdh155122 
880f4b3ec61Sdh155122 	for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++) {
881ab25eeb5Syz155240 		if (ap->apr_init != NULL) {
882f4b3ec61Sdh155122 			err = (*ap->apr_init)(&ap->apr_private, ifs);
883ab25eeb5Syz155240 			if (err != 0)
884ab25eeb5Syz155240 				break;
885ab25eeb5Syz155240 		}
886ab25eeb5Syz155240 	}
887f4b3ec61Sdh155122 	return 0;
888ab25eeb5Syz155240 }
889ab25eeb5Syz155240 
890ab25eeb5Syz155240 
891ab25eeb5Syz155240 /*
892ab25eeb5Syz155240  * Unload hook for kernel application proxies.
893ab25eeb5Syz155240  * Call the finialise routine for all the compiled in kernel proxies.
894ab25eeb5Syz155240  */
895f4b3ec61Sdh155122 void appr_unload(ifs)
896f4b3ec61Sdh155122 ipf_stack_t *ifs;
897ab25eeb5Syz155240 {
898ab25eeb5Syz155240 	aproxy_t *ap;
899f4b3ec61Sdh155122 	if(ifs->ifs_ap_proxies == NULL)
900f4b3ec61Sdh155122 		return;
901ab25eeb5Syz155240 
902f4b3ec61Sdh155122 	for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++)
903ab25eeb5Syz155240 		if (ap->apr_fini != NULL)
904f4b3ec61Sdh155122 			(*ap->apr_fini)(&ap->apr_private, ifs);
905f4b3ec61Sdh155122 	for (ap = ifs->ifs_ap_proxylist; ap; ap = ap->apr_next)
906ab25eeb5Syz155240 		if (ap->apr_fini != NULL)
907f4b3ec61Sdh155122 			(*ap->apr_fini)(&ap->apr_private, ifs);
908f4b3ec61Sdh155122 
909f4b3ec61Sdh155122 	KFREES(ifs->ifs_ap_proxies, sizeof (lcl_ap_proxies));
910f4b3ec61Sdh155122 	ifs->ifs_ap_proxies = NULL;
911ab25eeb5Syz155240 }
912