xref: /titanic_41/usr/src/uts/common/inet/ipf/ip_proxy.c (revision 9404882939d18ddd3c94a5bd3da7a0449c195a5d)
1 /*
2  * Copyright (C) 1997-2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright 2007 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/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 		(void) BCOPYIN(data, &ctl, sizeof(ctl));
317 		ptr = NULL;
318 
319 		if (ctl.apc_dsize > 0) {
320 			KMALLOCS(ptr, caddr_t, ctl.apc_dsize);
321 			if (ptr == NULL)
322 				error = ENOMEM;
323 			else {
324 				error = copyinptr(ctl.apc_data, ptr,
325 						  ctl.apc_dsize);
326 				if (error == 0)
327 					ctl.apc_data = ptr;
328 			}
329 		} else {
330 			ctl.apc_data = NULL;
331 			error = 0;
332 		}
333 
334 		if (error == 0)
335 			error = appr_ctl(&ctl, ifs);
336 
337 		if ((ctl.apc_dsize > 0) && (ptr != NULL) &&
338 		    (ctl.apc_data == ptr)) {
339 			KFREES(ptr, ctl.apc_dsize);
340 		}
341 		break;
342 
343 	default :
344 		error = EINVAL;
345 	}
346 	return error;
347 }
348 
349 
350 /*
351  * If a proxy has a match function, call that to do extended packet
352  * matching.
353  */
354 int appr_match(fin, nat)
355 fr_info_t *fin;
356 nat_t *nat;
357 {
358 	aproxy_t *apr;
359 	ipnat_t *ipn;
360 	int result;
361 
362 	ipn = nat->nat_ptr;
363 	if (ipf_proxy_debug > 8)
364 		printf("appr_match(%lx,%lx) aps %lx ptr %lx\n",
365 			(u_long)fin, (u_long)nat, (u_long)nat->nat_aps,
366 			(u_long)ipn);
367 
368 	if ((fin->fin_flx & (FI_SHORT|FI_BAD)) != 0) {
369 		if (ipf_proxy_debug > 0)
370 			printf("appr_match: flx 0x%x (BAD|SHORT)\n",
371 				fin->fin_flx);
372 		return -1;
373 	}
374 
375 	apr = ipn->in_apr;
376 	if ((apr == NULL) || (apr->apr_flags & APR_DELETE)) {
377 		if (ipf_proxy_debug > 0)
378 			printf("appr_match:apr %lx apr_flags 0x%x\n",
379 				(u_long)apr, apr ? apr->apr_flags : 0);
380 		return -1;
381 	}
382 
383 	if (apr->apr_match != NULL) {
384 		result = (*apr->apr_match)(fin, nat->nat_aps, nat, apr->apr_private);
385 		if (result != 0) {
386 			if (ipf_proxy_debug > 4)
387 				printf("appr_match: result %d\n", result);
388 			return -1;
389 		}
390 	}
391 	return 0;
392 }
393 
394 
395 /*
396  * Allocate a new application proxy structure and fill it in with the
397  * relevant details.  call the init function once complete, prior to
398  * returning.
399  */
400 int appr_new(fin, nat)
401 fr_info_t *fin;
402 nat_t *nat;
403 {
404 	register ap_session_t *aps;
405 	aproxy_t *apr;
406 	ipf_stack_t *ifs = fin->fin_ifs;
407 
408 	if (ipf_proxy_debug > 8)
409 		printf("appr_new(%lx,%lx) \n", (u_long)fin, (u_long)nat);
410 
411 	if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL)) {
412 		if (ipf_proxy_debug > 0)
413 			printf("appr_new: nat_ptr %lx nat_aps %lx\n",
414 				(u_long)nat->nat_ptr, (u_long)nat->nat_aps);
415 		return -1;
416 	}
417 
418 	apr = nat->nat_ptr->in_apr;
419 
420 	if ((apr->apr_flags & APR_DELETE) ||
421 	    (fin->fin_p != apr->apr_p)) {
422 		if (ipf_proxy_debug > 2)
423 			printf("appr_new: apr_flags 0x%x p %d/%d\n",
424 				apr->apr_flags, fin->fin_p, apr->apr_p);
425 		return -1;
426 	}
427 
428 	KMALLOC(aps, ap_session_t *);
429 	if (!aps) {
430 		if (ipf_proxy_debug > 0)
431 			printf("appr_new: malloc failed (%lu)\n",
432 				(u_long)sizeof(ap_session_t));
433 		return -1;
434 	}
435 
436 	bzero((char *)aps, sizeof(*aps));
437 	aps->aps_p = fin->fin_p;
438 	aps->aps_data = NULL;
439 	aps->aps_apr = apr;
440 	aps->aps_psiz = 0;
441 	if (apr->apr_new != NULL)
442 		if ((*apr->apr_new)(fin, aps, nat, apr->apr_private) == -1) {
443 			if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
444 				KFREES(aps->aps_data, aps->aps_psiz);
445 			}
446 			KFREE(aps);
447 			if (ipf_proxy_debug > 2)
448 				printf("appr_new: new(%lx) failed\n",
449 					(u_long)apr->apr_new);
450 			return -1;
451 		}
452 	aps->aps_nat = nat;
453 	aps->aps_next = ifs->ifs_ap_sess_list;
454 	ifs->ifs_ap_sess_list = aps;
455 	nat->nat_aps = aps;
456 
457 	return 0;
458 }
459 
460 
461 /*
462  * Check to see if a packet should be passed through an active proxy routine
463  * if one has been setup for it.  We don't need to check the checksum here if
464  * IPFILTER_CKSUM is defined because if it is, a failed check causes FI_BAD
465  * to be set.
466  */
467 int appr_check(fin, nat)
468 fr_info_t *fin;
469 nat_t *nat;
470 {
471 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_VALID)
472 	mb_t *m;
473 	int dosum = 1;
474 #endif
475 	tcphdr_t *tcp = NULL;
476 	udphdr_t *udp = NULL;
477 	ap_session_t *aps;
478 	aproxy_t *apr;
479 	ip_t *ip;
480 	short rv;
481 	int err;
482 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi)
483 	u_32_t s1, s2, sd;
484 #endif
485 	ipf_stack_t *ifs = fin->fin_ifs;
486 
487 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
488 	net_data_t net_data_p;
489 	if (fin->fin_v == 4)
490 		net_data_p = ifs->ifs_ipf_ipv4;
491 	else
492 		net_data_p = ifs->ifs_ipf_ipv6;
493 #endif
494 
495 	if (fin->fin_flx & FI_BAD) {
496 		if (ipf_proxy_debug > 0)
497 			printf("appr_check: flx 0x%x (BAD)\n", fin->fin_flx);
498 		return -1;
499 	}
500 
501 #ifndef IPFILTER_CKSUM
502 	if ((fin->fin_out == 0) && (fr_checkl4sum(fin) == -1)) {
503 		if (ipf_proxy_debug > 0)
504 			printf("appr_check: l4 checksum failure %d\n",
505 				fin->fin_p);
506 		if (fin->fin_p == IPPROTO_TCP)
507 			ifs->ifs_frstats[fin->fin_out].fr_tcpbad++;
508 		return -1;
509 	}
510 #endif
511 
512 	aps = nat->nat_aps;
513 	if ((aps != NULL) && (aps->aps_p == fin->fin_p)) {
514 		/*
515 		 * If there is data in this packet to be proxied then try and
516 		 * get it all into the one buffer, else drop it.
517 		 */
518 #if defined(MENTAT) || defined(HAVE_M_PULLDOWN)
519 		if ((fin->fin_dlen > 0) && !(fin->fin_flx & FI_COALESCE))
520 			if (fr_coalesce(fin) == -1) {
521 				if (ipf_proxy_debug > 0)
522 					printf("appr_check: fr_coalesce failed %x\n", fin->fin_flx);
523 				return -1;
524 			}
525 #endif
526 		ip = fin->fin_ip;
527 
528 		switch (fin->fin_p)
529 		{
530 		case IPPROTO_TCP :
531 			tcp = (tcphdr_t *)fin->fin_dp;
532 
533 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_VALID)
534 			m = fin->fin_qfm;
535 			if (dohwcksum && (m->b_ick_flag == ICK_VALID))
536 				dosum = 0;
537 #endif
538 			/*
539 			 * Don't bother the proxy with these...or in fact,
540 			 * should we free up proxy stuff when seen?
541 			 */
542 			if ((fin->fin_tcpf & TH_RST) != 0)
543 				break;
544 			/*FALLTHROUGH*/
545 		case IPPROTO_UDP :
546 			udp = (udphdr_t *)fin->fin_dp;
547 			break;
548 		default :
549 			break;
550 		}
551 
552 		apr = aps->aps_apr;
553 		err = 0;
554 		if (fin->fin_out != 0) {
555 			if (apr->apr_outpkt != NULL)
556 				err = (*apr->apr_outpkt)(fin, aps, nat, apr->apr_private);
557 		} else {
558 			if (apr->apr_inpkt != NULL)
559 				err = (*apr->apr_inpkt)(fin, aps, nat, apr->apr_private);
560 		}
561 
562 		rv = APR_EXIT(err);
563 		if (((ipf_proxy_debug > 0) && (rv != 0)) ||
564 		    (ipf_proxy_debug > 8))
565 			printf("appr_check: out %d err %x rv %d\n",
566 				fin->fin_out, err, rv);
567 		if (rv == 1)
568 			return -1;
569 
570 		if (rv == 2) {
571 			appr_free(apr);
572 			nat->nat_aps = NULL;
573 			return -1;
574 		}
575 
576 		/*
577 		 * If err != 0 then the data size of the packet has changed
578 		 * so we need to recalculate the header checksums for the
579 		 * packet.
580 		 * inbound packets always need to be adjusted.
581 		 */
582 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi)
583 		if (err != 0 && (!fin->fin_out ||
584 	    	    !NET_IS_HCK_L3_FULL(net_data_p, fin->fin_m))) {
585 			short adjlen = err & 0xffff;
586 
587 			s1 = LONG_SUM(ip->ip_len - adjlen);
588 			s2 = LONG_SUM(ip->ip_len);
589 			CALC_SUMD(s1, s2, sd);
590 			fix_outcksum(&ip->ip_sum, sd);
591 		}
592 #endif
593 
594 		/*
595 		 * For TCP packets, we may need to adjust the sequence and
596 		 * acknowledgement numbers to reflect changes in size of the
597 		 * data stream.
598 		 *
599 		 * For both TCP and UDP, recalculate the layer 4 checksum,
600 		 * regardless, as we can't tell (here) if data has been
601 		 * changed or not.
602 		 */
603 		if (tcp != NULL) {
604 			err = appr_fixseqack(fin, ip, aps, APR_INC(err));
605 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
606 			if (!fin->fin_out ||
607 			    !NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m) &&
608 			    !NET_IS_HCK_L4_PART(net_data_p, fin->fin_m))
609 				tcp->th_sum = fr_cksum(fin->fin_qfm, ip,
610 						       IPPROTO_TCP, tcp);
611 #else
612 			tcp->th_sum = fr_cksum(fin->fin_m, ip,
613 					       IPPROTO_TCP, tcp);
614 #endif
615 		} else if ((udp != NULL) && (udp->uh_sum != 0)) {
616 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
617 			if (!fin->fin_out ||
618 			    !NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m) &&
619 			    !NET_IS_HCK_L4_PART(net_data_p, fin->fin_m))
620 				udp->uh_sum = fr_cksum(fin->fin_qfm, ip,
621 						       IPPROTO_UDP, udp);
622 #else
623 			udp->uh_sum = fr_cksum(fin->fin_m, ip,
624 					       IPPROTO_UDP, udp);
625 #endif
626 		}
627 		aps->aps_bytes += fin->fin_plen;
628 		aps->aps_pkts++;
629 		return 1;
630 	}
631 	return 0;
632 }
633 
634 
635 /*
636  * Search for an proxy by the protocol it is being used with and its name.
637  */
638 aproxy_t *appr_lookup(pr, name, ifs)
639 u_int pr;
640 char *name;
641 ipf_stack_t *ifs;
642 {
643 	aproxy_t *ap;
644 
645 	if (ipf_proxy_debug > 8)
646 		printf("appr_lookup(%d,%s)\n", pr, name);
647 
648 	for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++)
649 		if ((ap->apr_p == pr) &&
650 		    !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
651 			ap->apr_ref++;
652 			return ap;
653 		}
654 
655 	for (ap = ifs->ifs_ap_proxylist; ap; ap = ap->apr_next)
656 		if ((ap->apr_p == pr) &&
657 		    !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
658 			ap->apr_ref++;
659 			return ap;
660 		}
661 	if (ipf_proxy_debug > 2)
662 		printf("appr_lookup: failed for %d/%s\n", pr, name);
663 	return NULL;
664 }
665 
666 
667 void appr_free(ap)
668 aproxy_t *ap;
669 {
670 	ap->apr_ref--;
671 }
672 
673 
674 void aps_free(aps, ifs)
675 ap_session_t *aps;
676 ipf_stack_t *ifs;
677 {
678 	ap_session_t *a, **ap;
679 	aproxy_t *apr;
680 
681 	if (!aps)
682 		return;
683 
684 	for (ap = &ifs->ifs_ap_sess_list; ((a = *ap) != NULL); ap = &a->aps_next)
685 		if (a == aps) {
686 			*ap = a->aps_next;
687 			break;
688 		}
689 
690 	apr = aps->aps_apr;
691 	if ((apr != NULL) && (apr->apr_del != NULL))
692 		(*apr->apr_del)(aps, apr->apr_private, ifs);
693 
694 	if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
695 		KFREES(aps->aps_data, aps->aps_psiz);
696 	KFREE(aps);
697 }
698 
699 
700 /*
701  * returns 2 if ack or seq number in TCP header is changed, returns 0 otherwise
702  */
703 static int appr_fixseqack(fin, ip, aps, inc)
704 fr_info_t *fin;
705 ip_t *ip;
706 ap_session_t *aps;
707 int inc;
708 {
709 	int sel, ch = 0, out, nlen;
710 	u_32_t seq1, seq2;
711 	tcphdr_t *tcp;
712 	short inc2;
713 
714 	tcp = (tcphdr_t *)fin->fin_dp;
715 	out = fin->fin_out;
716 	/*
717 	 * ip_len has already been adjusted by 'inc'.
718 	 */
719 	nlen = ip->ip_len;
720 	nlen -= (IP_HL(ip) << 2) + (TCP_OFF(tcp) << 2);
721 
722 	inc2 = inc;
723 	inc = (int)inc2;
724 
725 	if (out != 0) {
726 		seq1 = (u_32_t)ntohl(tcp->th_seq);
727 		sel = aps->aps_sel[out];
728 
729 		/* switch to other set ? */
730 		if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
731 		    (seq1 > aps->aps_seqmin[!sel])) {
732 			if (ipf_proxy_debug > 7)
733 				printf("proxy out switch set seq %d -> %d %x > %x\n",
734 					sel, !sel, seq1,
735 					aps->aps_seqmin[!sel]);
736 			sel = aps->aps_sel[out] = !sel;
737 		}
738 
739 		if (aps->aps_seqoff[sel]) {
740 			seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
741 			if (seq1 > seq2) {
742 				seq2 = aps->aps_seqoff[sel];
743 				seq1 += seq2;
744 				tcp->th_seq = htonl(seq1);
745 				ch = 1;
746 			}
747 		}
748 
749 		if (inc && (seq1 > aps->aps_seqmin[!sel])) {
750 			aps->aps_seqmin[sel] = seq1 + nlen - 1;
751 			aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
752 			if (ipf_proxy_debug > 7)
753 				printf("proxy seq set %d at %x to %d + %d\n",
754 					sel, aps->aps_seqmin[sel],
755 					aps->aps_seqoff[sel], inc);
756 		}
757 
758 		/***/
759 
760 		seq1 = ntohl(tcp->th_ack);
761 		sel = aps->aps_sel[1 - out];
762 
763 		/* switch to other set ? */
764 		if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
765 		    (seq1 > aps->aps_ackmin[!sel])) {
766 			if (ipf_proxy_debug > 7)
767 				printf("proxy out switch set ack %d -> %d %x > %x\n",
768 					sel, !sel, seq1,
769 					aps->aps_ackmin[!sel]);
770 			sel = aps->aps_sel[1 - out] = !sel;
771 		}
772 
773 		if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
774 			seq2 = aps->aps_ackoff[sel];
775 			tcp->th_ack = htonl(seq1 - seq2);
776 			ch = 1;
777 		}
778 	} else {
779 		seq1 = ntohl(tcp->th_seq);
780 		sel = aps->aps_sel[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 in switch set ack %d -> %d %x > %x\n",
787 					sel, !sel, seq1, aps->aps_ackmin[!sel]);
788 			sel = aps->aps_sel[out] = !sel;
789 		}
790 
791 		if (aps->aps_ackoff[sel]) {
792 			seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
793 			if (seq1 > seq2) {
794 				seq2 = aps->aps_ackoff[sel];
795 				seq1 += seq2;
796 				tcp->th_seq = htonl(seq1);
797 				ch = 1;
798 			}
799 		}
800 
801 		if (inc && (seq1 > aps->aps_ackmin[!sel])) {
802 			aps->aps_ackmin[!sel] = seq1 + nlen - 1;
803 			aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
804 
805 			if (ipf_proxy_debug > 7)
806 				printf("proxy ack set %d at %x to %d + %d\n",
807 					!sel, aps->aps_seqmin[!sel],
808 					aps->aps_seqoff[sel], inc);
809 		}
810 
811 		/***/
812 
813 		seq1 = ntohl(tcp->th_ack);
814 		sel = aps->aps_sel[1 - out];
815 
816 		/* switch to other set ? */
817 		if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
818 		    (seq1 > aps->aps_seqmin[!sel])) {
819 			if (ipf_proxy_debug > 7)
820 				printf("proxy in switch set seq %d -> %d %x > %x\n",
821 					sel, !sel, seq1, aps->aps_seqmin[!sel]);
822 			sel = aps->aps_sel[1 - out] = !sel;
823 		}
824 
825 		if (aps->aps_seqoff[sel] != 0) {
826 			if (ipf_proxy_debug > 7)
827 				printf("sel %d seqoff %d seq1 %x seqmin %x\n",
828 					sel, aps->aps_seqoff[sel], seq1,
829 					aps->aps_seqmin[sel]);
830 			if (seq1 > aps->aps_seqmin[sel]) {
831 				seq2 = aps->aps_seqoff[sel];
832 				tcp->th_ack = htonl(seq1 - seq2);
833 				ch = 1;
834 			}
835 		}
836 	}
837 
838 	if (ipf_proxy_debug > 8)
839 		printf("appr_fixseqack: seq %x ack %x\n",
840 			ntohl(tcp->th_seq), ntohl(tcp->th_ack));
841 	return ch ? 2 : 0;
842 }
843 
844 
845 /*
846  * Initialise hook for kernel application proxies.
847  * Call the initialise routine for all the compiled in kernel proxies.
848  */
849 int appr_init(ifs)
850 ipf_stack_t *ifs;
851 {
852 	aproxy_t *ap;
853 	int err =  0;
854 
855 	/* Since the refcnt is used we make a copy of lcl_ap_proxies */
856 	KMALLOCS(ifs->ifs_ap_proxies, aproxy_t *, sizeof (lcl_ap_proxies));
857 	bcopy(lcl_ap_proxies, ifs->ifs_ap_proxies, sizeof (lcl_ap_proxies));
858 
859 	for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++) {
860 		if (ap->apr_init != NULL) {
861 			err = (*ap->apr_init)(&ap->apr_private, ifs);
862 			if (err != 0)
863 				break;
864 		}
865 	}
866 	return 0;
867 }
868 
869 
870 /*
871  * Unload hook for kernel application proxies.
872  * Call the finialise routine for all the compiled in kernel proxies.
873  */
874 void appr_unload(ifs)
875 ipf_stack_t *ifs;
876 {
877 	aproxy_t *ap;
878 	if(ifs->ifs_ap_proxies == NULL)
879 		return;
880 
881 	for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++)
882 		if (ap->apr_fini != NULL)
883 			(*ap->apr_fini)(&ap->apr_private, ifs);
884 	for (ap = ifs->ifs_ap_proxylist; ap; ap = ap->apr_next)
885 		if (ap->apr_fini != NULL)
886 			(*ap->apr_fini)(&ap->apr_private, ifs);
887 
888 	KFREES(ifs->ifs_ap_proxies, sizeof (lcl_ap_proxies));
889 	ifs->ifs_ap_proxies = NULL;
890 }
891