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