xref: /illumos-gate/usr/src/uts/common/inet/ipf/ip_proxy.c (revision 381a2a9a387f449fab7d0c7e97c4184c26963abf)
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) && defined(ICK_VALID)
466 	mb_t *m;
467 	int dosum = 1;
468 #endif
469 	tcphdr_t *tcp = NULL;
470 	udphdr_t *udp = NULL;
471 	ap_session_t *aps;
472 	aproxy_t *apr;
473 	ip_t *ip;
474 	short rv;
475 	int err;
476 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi)
477 	u_32_t s1, s2, sd;
478 #endif
479 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
480 	net_data_t net_data_p;
481 	if (fin->fin_v == 4)
482 		net_data_p = ipf_ipv4;
483 	else
484 		net_data_p = ipf_ipv6;
485 #endif
486 
487 	if (fin->fin_flx & FI_BAD) {
488 		if (ipf_proxy_debug > 0)
489 			printf("appr_check: flx 0x%x (BAD)\n", fin->fin_flx);
490 		return -1;
491 	}
492 
493 #ifndef IPFILTER_CKSUM
494 	if ((fin->fin_out == 0) && (fr_checkl4sum(fin) == -1)) {
495 		if (ipf_proxy_debug > 0)
496 			printf("appr_check: l4 checksum failure %d\n",
497 				fin->fin_p);
498 		if (fin->fin_p == IPPROTO_TCP)
499 			frstats[fin->fin_out].fr_tcpbad++;
500 		return -1;
501 	}
502 #endif
503 
504 	aps = nat->nat_aps;
505 	if ((aps != NULL) && (aps->aps_p == fin->fin_p)) {
506 		/*
507 		 * If there is data in this packet to be proxied then try and
508 		 * get it all into the one buffer, else drop it.
509 		 */
510 #if defined(MENTAT) || defined(HAVE_M_PULLDOWN)
511 		if ((fin->fin_dlen > 0) && !(fin->fin_flx & FI_COALESCE))
512 			if (fr_coalesce(fin) == -1) {
513 				if (ipf_proxy_debug > 0)
514 					printf("appr_check: fr_coalesce failed %x\n", fin->fin_flx);
515 				return -1;
516 			}
517 #endif
518 		ip = fin->fin_ip;
519 
520 		switch (fin->fin_p)
521 		{
522 		case IPPROTO_TCP :
523 			tcp = (tcphdr_t *)fin->fin_dp;
524 
525 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_VALID)
526 			m = fin->fin_qfm;
527 			if (dohwcksum && (m->b_ick_flag == ICK_VALID))
528 				dosum = 0;
529 #endif
530 			/*
531 			 * Don't bother the proxy with these...or in fact,
532 			 * should we free up proxy stuff when seen?
533 			 */
534 			if ((fin->fin_tcpf & TH_RST) != 0)
535 				break;
536 			/*FALLTHROUGH*/
537 		case IPPROTO_UDP :
538 			udp = (udphdr_t *)fin->fin_dp;
539 			break;
540 		default :
541 			break;
542 		}
543 
544 		apr = aps->aps_apr;
545 		err = 0;
546 		if (fin->fin_out != 0) {
547 			if (apr->apr_outpkt != NULL)
548 				err = (*apr->apr_outpkt)(fin, aps, nat);
549 		} else {
550 			if (apr->apr_inpkt != NULL)
551 				err = (*apr->apr_inpkt)(fin, aps, nat);
552 		}
553 
554 		rv = APR_EXIT(err);
555 		if (((ipf_proxy_debug > 0) && (rv != 0)) ||
556 		    (ipf_proxy_debug > 8))
557 			printf("appr_check: out %d err %x rv %d\n",
558 				fin->fin_out, err, rv);
559 		if (rv == 1)
560 			return -1;
561 
562 		if (rv == 2) {
563 			appr_free(apr);
564 			nat->nat_aps = NULL;
565 			return -1;
566 		}
567 
568 		/*
569 		 * If err != 0 then the data size of the packet has changed
570 		 * so we need to recalculate the header checksums for the
571 		 * packet.
572 		 * inbound packets always need to be adjusted.
573 		 */
574 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi)
575 		if (err != 0 && (!fin->fin_out ||
576 	    	    !NET_IS_HCK_L3_FULL(net_data_p, fin->fin_m))) {
577 			short adjlen = err & 0xffff;
578 
579 			s1 = LONG_SUM(ip->ip_len - adjlen);
580 			s2 = LONG_SUM(ip->ip_len);
581 			CALC_SUMD(s1, s2, sd);
582 			fix_outcksum(&ip->ip_sum, sd);
583 		}
584 #endif
585 
586 		/*
587 		 * For TCP packets, we may need to adjust the sequence and
588 		 * acknowledgement numbers to reflect changes in size of the
589 		 * data stream.
590 		 *
591 		 * For both TCP and UDP, recalculate the layer 4 checksum,
592 		 * regardless, as we can't tell (here) if data has been
593 		 * changed or not.
594 		 */
595 		if (tcp != NULL) {
596 			err = appr_fixseqack(fin, ip, aps, APR_INC(err));
597 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
598 			if (!fin->fin_out ||
599 			    !NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m) &&
600 			    !NET_IS_HCK_L4_PART(net_data_p, fin->fin_m))
601 				tcp->th_sum = fr_cksum(fin->fin_qfm, ip,
602 						       IPPROTO_TCP, tcp);
603 #else
604 			tcp->th_sum = fr_cksum(fin->fin_m, ip,
605 					       IPPROTO_TCP, tcp);
606 #endif
607 		} else if ((udp != NULL) && (udp->uh_sum != 0)) {
608 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
609 			if (!fin->fin_out ||
610 			    !NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m) &&
611 			    !NET_IS_HCK_L4_PART(net_data_p, fin->fin_m))
612 				udp->uh_sum = fr_cksum(fin->fin_qfm, ip,
613 						       IPPROTO_UDP, udp);
614 #else
615 			udp->uh_sum = fr_cksum(fin->fin_m, ip,
616 					       IPPROTO_UDP, udp);
617 #endif
618 		}
619 		aps->aps_bytes += fin->fin_plen;
620 		aps->aps_pkts++;
621 		return 1;
622 	}
623 	return 0;
624 }
625 
626 
627 /*
628  * Search for an proxy by the protocol it is being used with and its name.
629  */
630 aproxy_t *appr_lookup(pr, name)
631 u_int pr;
632 char *name;
633 {
634 	aproxy_t *ap;
635 
636 	if (ipf_proxy_debug > 8)
637 		printf("appr_lookup(%d,%s)\n", pr, name);
638 
639 	for (ap = ap_proxies; ap->apr_p; ap++)
640 		if ((ap->apr_p == pr) &&
641 		    !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
642 			ap->apr_ref++;
643 			return ap;
644 		}
645 
646 	for (ap = ap_proxylist; ap; ap = ap->apr_next)
647 		if ((ap->apr_p == pr) &&
648 		    !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
649 			ap->apr_ref++;
650 			return ap;
651 		}
652 	if (ipf_proxy_debug > 2)
653 		printf("appr_lookup: failed for %d/%s\n", pr, name);
654 	return NULL;
655 }
656 
657 
658 void appr_free(ap)
659 aproxy_t *ap;
660 {
661 	ap->apr_ref--;
662 }
663 
664 
665 void aps_free(aps)
666 ap_session_t *aps;
667 {
668 	ap_session_t *a, **ap;
669 	aproxy_t *apr;
670 
671 	if (!aps)
672 		return;
673 
674 	for (ap = &ap_sess_list; ((a = *ap) != NULL); ap = &a->aps_next)
675 		if (a == aps) {
676 			*ap = a->aps_next;
677 			break;
678 		}
679 
680 	apr = aps->aps_apr;
681 	if ((apr != NULL) && (apr->apr_del != NULL))
682 		(*apr->apr_del)(aps);
683 
684 	if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
685 		KFREES(aps->aps_data, aps->aps_psiz);
686 	KFREE(aps);
687 }
688 
689 
690 /*
691  * returns 2 if ack or seq number in TCP header is changed, returns 0 otherwise
692  */
693 static int appr_fixseqack(fin, ip, aps, inc)
694 fr_info_t *fin;
695 ip_t *ip;
696 ap_session_t *aps;
697 int inc;
698 {
699 	int sel, ch = 0, out, nlen;
700 	u_32_t seq1, seq2;
701 	tcphdr_t *tcp;
702 	short inc2;
703 
704 	tcp = (tcphdr_t *)fin->fin_dp;
705 	out = fin->fin_out;
706 	/*
707 	 * ip_len has already been adjusted by 'inc'.
708 	 */
709 	nlen = ip->ip_len;
710 	nlen -= (IP_HL(ip) << 2) + (TCP_OFF(tcp) << 2);
711 
712 	inc2 = inc;
713 	inc = (int)inc2;
714 
715 	if (out != 0) {
716 		seq1 = (u_32_t)ntohl(tcp->th_seq);
717 		sel = aps->aps_sel[out];
718 
719 		/* switch to other set ? */
720 		if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
721 		    (seq1 > aps->aps_seqmin[!sel])) {
722 			if (ipf_proxy_debug > 7)
723 				printf("proxy out switch set seq %d -> %d %x > %x\n",
724 					sel, !sel, seq1,
725 					aps->aps_seqmin[!sel]);
726 			sel = aps->aps_sel[out] = !sel;
727 		}
728 
729 		if (aps->aps_seqoff[sel]) {
730 			seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
731 			if (seq1 > seq2) {
732 				seq2 = aps->aps_seqoff[sel];
733 				seq1 += seq2;
734 				tcp->th_seq = htonl(seq1);
735 				ch = 1;
736 			}
737 		}
738 
739 		if (inc && (seq1 > aps->aps_seqmin[!sel])) {
740 			aps->aps_seqmin[sel] = seq1 + nlen - 1;
741 			aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
742 			if (ipf_proxy_debug > 7)
743 				printf("proxy seq set %d at %x to %d + %d\n",
744 					sel, aps->aps_seqmin[sel],
745 					aps->aps_seqoff[sel], inc);
746 		}
747 
748 		/***/
749 
750 		seq1 = ntohl(tcp->th_ack);
751 		sel = aps->aps_sel[1 - out];
752 
753 		/* switch to other set ? */
754 		if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
755 		    (seq1 > aps->aps_ackmin[!sel])) {
756 			if (ipf_proxy_debug > 7)
757 				printf("proxy out switch set ack %d -> %d %x > %x\n",
758 					sel, !sel, seq1,
759 					aps->aps_ackmin[!sel]);
760 			sel = aps->aps_sel[1 - out] = !sel;
761 		}
762 
763 		if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
764 			seq2 = aps->aps_ackoff[sel];
765 			tcp->th_ack = htonl(seq1 - seq2);
766 			ch = 1;
767 		}
768 	} else {
769 		seq1 = ntohl(tcp->th_seq);
770 		sel = aps->aps_sel[out];
771 
772 		/* switch to other set ? */
773 		if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
774 		    (seq1 > aps->aps_ackmin[!sel])) {
775 			if (ipf_proxy_debug > 7)
776 				printf("proxy in switch set ack %d -> %d %x > %x\n",
777 					sel, !sel, seq1, aps->aps_ackmin[!sel]);
778 			sel = aps->aps_sel[out] = !sel;
779 		}
780 
781 		if (aps->aps_ackoff[sel]) {
782 			seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
783 			if (seq1 > seq2) {
784 				seq2 = aps->aps_ackoff[sel];
785 				seq1 += seq2;
786 				tcp->th_seq = htonl(seq1);
787 				ch = 1;
788 			}
789 		}
790 
791 		if (inc && (seq1 > aps->aps_ackmin[!sel])) {
792 			aps->aps_ackmin[!sel] = seq1 + nlen - 1;
793 			aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
794 
795 			if (ipf_proxy_debug > 7)
796 				printf("proxy ack set %d at %x to %d + %d\n",
797 					!sel, aps->aps_seqmin[!sel],
798 					aps->aps_seqoff[sel], inc);
799 		}
800 
801 		/***/
802 
803 		seq1 = ntohl(tcp->th_ack);
804 		sel = aps->aps_sel[1 - out];
805 
806 		/* switch to other set ? */
807 		if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
808 		    (seq1 > aps->aps_seqmin[!sel])) {
809 			if (ipf_proxy_debug > 7)
810 				printf("proxy in switch set seq %d -> %d %x > %x\n",
811 					sel, !sel, seq1, aps->aps_seqmin[!sel]);
812 			sel = aps->aps_sel[1 - out] = !sel;
813 		}
814 
815 		if (aps->aps_seqoff[sel] != 0) {
816 			if (ipf_proxy_debug > 7)
817 				printf("sel %d seqoff %d seq1 %x seqmin %x\n",
818 					sel, aps->aps_seqoff[sel], seq1,
819 					aps->aps_seqmin[sel]);
820 			if (seq1 > aps->aps_seqmin[sel]) {
821 				seq2 = aps->aps_seqoff[sel];
822 				tcp->th_ack = htonl(seq1 - seq2);
823 				ch = 1;
824 			}
825 		}
826 	}
827 
828 	if (ipf_proxy_debug > 8)
829 		printf("appr_fixseqack: seq %x ack %x\n",
830 			ntohl(tcp->th_seq), ntohl(tcp->th_ack));
831 	return ch ? 2 : 0;
832 }
833 
834 
835 /*
836  * Initialise hook for kernel application proxies.
837  * Call the initialise routine for all the compiled in kernel proxies.
838  */
839 int appr_init()
840 {
841 	aproxy_t *ap;
842 	int err = 0;
843 
844 	for (ap = ap_proxies; ap->apr_p; ap++) {
845 		if (ap->apr_init != NULL) {
846 			err = (*ap->apr_init)();
847 			if (err != 0)
848 				break;
849 		}
850 	}
851 	return err;
852 }
853 
854 
855 /*
856  * Unload hook for kernel application proxies.
857  * Call the finialise routine for all the compiled in kernel proxies.
858  */
859 void appr_unload()
860 {
861 	aproxy_t *ap;
862 
863 	for (ap = ap_proxies; ap->apr_p; ap++)
864 		if (ap->apr_fini != NULL)
865 			(*ap->apr_fini)();
866 	for (ap = ap_proxylist; ap; ap = ap->apr_next)
867 		if (ap->apr_fini != NULL)
868 			(*ap->apr_fini)();
869 }
870