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