1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  */
7 #if defined(KERNEL) || defined(_KERNEL)
8 # undef KERNEL
9 # undef _KERNEL
10 # define        KERNEL	1
11 # define        _KERNEL	1
12 #endif
13 #include <sys/errno.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
16 #include <sys/time.h>
17 #include <sys/file.h>
18 # include <sys/fcntl.h>
19 #if !defined(_KERNEL) && !defined(__KERNEL__)
20 # include <stdio.h>
21 # include <string.h>
22 # include <stdlib.h>
23 # include <ctype.h>
24 # define _KERNEL
25 # include <sys/uio.h>
26 # undef _KERNEL
27 #endif
28 # include <sys/protosw.h>
29 #include <sys/socket.h>
30 #if defined(_KERNEL)
31 #ifdef __FreeBSD__
32 #  include <sys/ctype.h>
33 # endif
34 # include <sys/systm.h>
35 # if !defined(__SVR4)
36 #  include <sys/mbuf.h>
37 # endif
38 #endif
39 #if defined(_KERNEL) && defined(__FreeBSD__)
40 # include <sys/filio.h>
41 # include <sys/fcntl.h>
42 #else
43 # include <sys/ioctl.h>
44 #endif
45 #if defined(__SVR4)
46 # include <sys/byteorder.h>
47 # ifdef _KERNEL
48 #  include <sys/dditypes.h>
49 # endif
50 # include <sys/stream.h>
51 # include <sys/kmem.h>
52 #endif
53 #ifdef __FreeBSD__
54 # include <sys/queue.h>
55 #endif
56 #include <net/if.h>
57 #if defined(__FreeBSD__) && defined(_KERNEL)
58 #include <net/vnet.h>
59 #else
60 #define CURVNET_SET(arg)
61 #define CURVNET_RESTORE()
62 #define	VNET_DEFINE(_t, _v)	_t _v
63 #define	VNET_DECLARE(_t, _v)	extern _t _v
64 #define	VNET(arg)	arg
65 #endif
66 #ifdef sun
67 # include <net/af.h>
68 #endif
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 # include <netinet/ip_var.h>
73 #include <netinet/tcp.h>
74 #include <netinet/udp.h>
75 #include <netinet/ip_icmp.h>
76 #include "netinet/ip_compat.h"
77 #include <netinet/tcpip.h>
78 #include "netinet/ip_fil.h"
79 #include "netinet/ip_nat.h"
80 #include "netinet/ip_state.h"
81 #include "netinet/ip_proxy.h"
82 #if defined(__FreeBSD__)
83 # include <sys/malloc.h>
84 #endif
85 
86 /* END OF INCLUDES */
87 
88 #include "netinet/ip_ftp_pxy.c"
89 #include "netinet/ip_tftp_pxy.c"
90 #include "netinet/ip_rcmd_pxy.c"
91 #include "netinet/ip_pptp_pxy.c"
92 #if defined(_KERNEL)
93 # include "netinet/ip_irc_pxy.c"
94 # include "netinet/ip_raudio_pxy.c"
95 # include "netinet/ip_netbios_pxy.c"
96 #endif
97 #include "netinet/ip_ipsec_pxy.c"
98 #include "netinet/ip_rpcb_pxy.c"
99 
100 #if !defined(lint)
101 static const char rcsid[] = "@(#)$Id$";
102 #endif
103 
104 #define	AP_SESS_SIZE	53
105 
106 static int ipf_proxy_fixseqack(fr_info_t *, ip_t *, ap_session_t *, int );
107 static aproxy_t *ipf_proxy_create_clone(ipf_main_softc_t *, aproxy_t *);
108 
109 typedef struct ipf_proxy_softc_s {
110 	int		ips_proxy_debug;
111 	int		ips_proxy_session_size;
112 	ap_session_t	**ips_sess_tab;
113 	ap_session_t	*ips_sess_list;
114 	aproxy_t	*ips_proxies;
115 	int		ips_init_run;
116 	ipftuneable_t	*ipf_proxy_tune;
117 } ipf_proxy_softc_t;
118 
119 static ipftuneable_t ipf_proxy_tuneables[] = {
120 	{ { (void *)offsetof(ipf_proxy_softc_t, ips_proxy_debug) },
121 		"proxy_debug",	0,	0x1f,
122 		stsizeof(ipf_proxy_softc_t, ips_proxy_debug),
123 		0,	NULL,	NULL },
124 	{ { NULL },		NULL,			0,	0,
125 		0,
126 		0,	NULL,	NULL}
127 };
128 
129 static	aproxy_t	*ap_proxylist = NULL;
130 static	aproxy_t	ips_proxies[] = {
131 #ifdef	IPF_FTP_PROXY
132 	{ NULL, NULL, "ftp", (char)IPPROTO_TCP, 0, 0, 0,
133 	  ipf_p_ftp_main_load, ipf_p_ftp_main_unload,
134 	  ipf_p_ftp_soft_create, ipf_p_ftp_soft_destroy,
135 	  NULL, NULL,
136 	  ipf_p_ftp_new, ipf_p_ftp_del, ipf_p_ftp_in, ipf_p_ftp_out, NULL,
137 	  NULL, NULL, NULL, NULL },
138 #endif
139 #ifdef	IPF_TFTP_PROXY
140 	{ NULL, NULL, "tftp", (char)IPPROTO_UDP, 0, 0, 0,
141 	  ipf_p_tftp_main_load, ipf_p_tftp_main_unload,
142 	  ipf_p_tftp_soft_create, ipf_p_tftp_soft_destroy,
143 	  NULL, NULL,
144 	  ipf_p_tftp_new, ipf_p_tftp_del,
145 	  ipf_p_tftp_in, ipf_p_tftp_out, NULL,
146 	  NULL, NULL, NULL, NULL },
147 #endif
148 #ifdef	IPF_IRC_PROXY
149 	{ NULL, NULL, "irc", (char)IPPROTO_TCP, 0, 0, 0,
150 	  ipf_p_irc_main_load, ipf_p_irc_main_unload,
151 	  NULL, NULL,
152 	  NULL, NULL,
153 	  ipf_p_irc_new, NULL, NULL, ipf_p_irc_out, NULL,
154 	  NULL, NULL, NULL, NULL },
155 #endif
156 #ifdef	IPF_RCMD_PROXY
157 	{ NULL, NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, 0,
158 	  ipf_p_rcmd_main_load, ipf_p_rcmd_main_unload,
159 	  NULL, NULL,
160 	  NULL, NULL,
161 	  ipf_p_rcmd_new, ipf_p_rcmd_del,
162 	  ipf_p_rcmd_in, ipf_p_rcmd_out, NULL,
163 	  NULL, NULL, NULL, NULL },
164 #endif
165 #ifdef	IPF_RAUDIO_PROXY
166 	{ NULL, NULL, "raudio", (char)IPPROTO_TCP, 0, 0, 0,
167 	  ipf_p_raudio_main_load, ipf_p_raudio_main_unload,
168 	  NULL, NULL,
169 	  NULL, NULL,
170 	  ipf_p_raudio_new, NULL, ipf_p_raudio_in, ipf_p_raudio_out, NULL,
171 	  NULL, NULL, NULL, NULL },
172 #endif
173 #ifdef	IPF_MSNRPC_PROXY
174 	{ NULL, NULL, "msnrpc", (char)IPPROTO_TCP, 0, 0, 0,
175 	  ipf_p_msnrpc_init, ipf_p_msnrpc_fini,
176 	  NULL, NULL,
177 	  NULL, NULL,
178 	  ipf_p_msnrpc_new, NULL, ipf_p_msnrpc_in, ipf_p_msnrpc_out, NULL,
179 	  NULL, NULL, NULL, NULL },
180 #endif
181 #ifdef	IPF_NETBIOS_PROXY
182 	{ NULL, NULL, "netbios", (char)IPPROTO_UDP, 0, 0, 0,
183 	  ipf_p_netbios_main_load, ipf_p_netbios_main_unload,
184 	  NULL, NULL,
185 	  NULL, NULL,
186 	  NULL, NULL, NULL, ipf_p_netbios_out, NULL,
187 	  NULL, NULL, NULL, NULL },
188 #endif
189 #ifdef	IPF_IPSEC_PROXY
190 	{ NULL, NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, 0,
191 	  NULL, NULL,
192 	  ipf_p_ipsec_soft_create, ipf_p_ipsec_soft_destroy,
193 	  ipf_p_ipsec_soft_init, ipf_p_ipsec_soft_fini,
194 	  ipf_p_ipsec_new, ipf_p_ipsec_del,
195 	  ipf_p_ipsec_inout, ipf_p_ipsec_inout, ipf_p_ipsec_match,
196 	  NULL, NULL, NULL, NULL },
197 #endif
198 #ifdef	IPF_DNS_PROXY
199 	{ NULL, NULL, "dns", (char)IPPROTO_UDP, 0, 0, 0,
200 	  NULL, NULL,
201 	  ipf_p_dns_soft_create, ipf_p_dns_soft_destroy,
202 	  NULL, NULL,
203 	  ipf_p_dns_new, ipf_p_ipsec_del,
204 	  ipf_p_dns_inout, ipf_p_dns_inout, ipf_p_dns_match,
205 	  ipf_p_dns_ctl, NULL, NULL, NULL },
206 #endif
207 #ifdef	IPF_PPTP_PROXY
208 	{ NULL, NULL, "pptp", (char)IPPROTO_TCP, 0, 0, 0,
209 	  ipf_p_pptp_main_load, ipf_p_pptp_main_unload,
210 	  NULL, NULL,
211 	  NULL, NULL,
212 	  ipf_p_pptp_new, ipf_p_pptp_del,
213 	  ipf_p_pptp_inout, ipf_p_pptp_inout, NULL,
214 	  NULL, NULL, NULL, NULL },
215 #endif
216 #ifdef	IPF_RPCB_PROXY
217 # ifndef _KERNEL
218 	{ NULL, NULL, "rpcbt", (char)IPPROTO_TCP, 0, 0, 0,
219 	  NULL, NULL,
220 	  NULL, NULL,
221 	  NULL, NULL,
222 	  ipf_p_rpcb_new, ipf_p_rpcb_del,
223 	  ipf_p_rpcb_in, ipf_p_rpcb_out, NULL,
224 	  NULL, NULL, NULL, NULL },
225 # endif
226 	{ NULL, NULL, "rpcbu", (char)IPPROTO_UDP, 0, 0, 0,
227 	  ipf_p_rpcb_main_load, ipf_p_rpcb_main_unload,
228 	  NULL, NULL,
229 	  NULL, NULL,
230 	  ipf_p_rpcb_new, ipf_p_rpcb_del,
231 	  ipf_p_rpcb_in, ipf_p_rpcb_out, NULL,
232 	  NULL, NULL, NULL, NULL },
233 #endif
234 	{ NULL, NULL, "", '\0', 0, 0, 0,
235 	  NULL, NULL,
236 	  NULL, NULL,
237 	  NULL, NULL,
238 	  NULL, NULL,
239 	  NULL, NULL, NULL,
240 	  NULL, NULL, NULL, NULL }
241 };
242 
243 
244 /* ------------------------------------------------------------------------ */
245 /* Function:    ipf_proxy_main_load                                         */
246 /* Returns:     int    - 0 == success, else failure.                        */
247 /* Parameters:  Nil                                                         */
248 /*                                                                          */
249 /* Initialise hook for kernel application proxies.                          */
250 /* Call the initialise routine for all the compiled in kernel proxies.      */
251 /* ------------------------------------------------------------------------ */
252 int
ipf_proxy_main_load(void)253 ipf_proxy_main_load(void)
254 {
255 	aproxy_t *ap;
256 
257 	for (ap = ips_proxies; ap->apr_p; ap++) {
258 		if (ap->apr_load != NULL)
259 			(*ap->apr_load)();
260 	}
261 	return (0);
262 }
263 
264 
265 /* ------------------------------------------------------------------------ */
266 /* Function:    ipf_proxy_main_unload                                       */
267 /* Returns:     int - 0 == success, else failure.                           */
268 /* Parameters:  Nil                                                         */
269 /*                                                                          */
270 /* Unload hook for kernel application proxies.                              */
271 /* Call the finialise routine for all the compiled in kernel proxies.       */
272 /* ------------------------------------------------------------------------ */
273 int
ipf_proxy_main_unload(void)274 ipf_proxy_main_unload(void)
275 {
276 	aproxy_t *ap;
277 
278 	for (ap = ips_proxies; ap->apr_p; ap++)
279 		if (ap->apr_unload != NULL)
280 			(*ap->apr_unload)();
281 	for (ap = ap_proxylist; ap; ap = ap->apr_next)
282 		if (ap->apr_unload != NULL)
283 			(*ap->apr_unload)();
284 
285 	return (0);
286 }
287 
288 
289 /* ------------------------------------------------------------------------ */
290 /* Function:    ipf_proxy_soft_create                                       */
291 /* Returns:     void *   -                                                  */
292 /* Parameters:  softc(I) - pointer to soft context main structure           */
293 /*                                                                          */
294 /* Build the structure to hold all of the run time data to support proxies. */
295 /* ------------------------------------------------------------------------ */
296 void *
ipf_proxy_soft_create(ipf_main_softc_t * softc)297 ipf_proxy_soft_create(ipf_main_softc_t *softc)
298 {
299 	ipf_proxy_softc_t *softp;
300 	aproxy_t *last;
301 	aproxy_t *apn;
302 	aproxy_t *ap;
303 
304 	KMALLOC(softp, ipf_proxy_softc_t *);
305 	if (softp == NULL)
306 		return (softp);
307 
308 	bzero((char *)softp, sizeof(*softp));
309 
310 #if defined(_KERNEL)
311 	softp->ips_proxy_debug = 0;
312 #else
313 	softp->ips_proxy_debug = 2;
314 #endif
315 	softp->ips_proxy_session_size = AP_SESS_SIZE;
316 
317 	softp->ipf_proxy_tune = ipf_tune_array_copy(softp,
318 						    sizeof(ipf_proxy_tuneables),
319 						    ipf_proxy_tuneables);
320 	if (softp->ipf_proxy_tune == NULL) {
321 		ipf_proxy_soft_destroy(softc, softp);
322 		return (NULL);
323 	}
324 	if (ipf_tune_array_link(softc, softp->ipf_proxy_tune) == -1) {
325 		ipf_proxy_soft_destroy(softc, softp);
326 		return (NULL);
327 	}
328 
329 	last = NULL;
330 	for (ap = ips_proxies; ap->apr_p; ap++) {
331 		apn = ipf_proxy_create_clone(softc, ap);
332 		if (apn == NULL)
333 			goto failed;
334 		if (last != NULL)
335 			last->apr_next = apn;
336 		else
337 			softp->ips_proxies = apn;
338 		last = apn;
339 	}
340 	for (ap = ips_proxies; ap != NULL; ap = ap->apr_next) {
341 		apn = ipf_proxy_create_clone(softc, ap);
342 		if (apn == NULL)
343 			goto failed;
344 		if (last != NULL)
345 			last->apr_next = apn;
346 		else
347 			softp->ips_proxies = apn;
348 		last = apn;
349 	}
350 
351 	return (softp);
352 failed:
353 	ipf_proxy_soft_destroy(softc, softp);
354 	return (NULL);
355 }
356 
357 
358 /* ------------------------------------------------------------------------ */
359 /* Function:    ipf_proxy_soft_create                                       */
360 /* Returns:     void *   -                                                  */
361 /* Parameters:  softc(I) - pointer to soft context main structure           */
362 /*              orig(I)  - pointer to proxy definition to copy              */
363 /*                                                                          */
364 /* This function clones a proxy definition given by orig and returns a      */
365 /* a pointer to that copy.                                                  */
366 /* ------------------------------------------------------------------------ */
367 static aproxy_t *
ipf_proxy_create_clone(ipf_main_softc_t * softc,aproxy_t * orig)368 ipf_proxy_create_clone(ipf_main_softc_t *softc, aproxy_t *orig)
369 {
370 	aproxy_t *apn;
371 
372 	KMALLOC(apn, aproxy_t *);
373 	if (apn == NULL)
374 		return (NULL);
375 
376 	bcopy((char *)orig, (char *)apn, sizeof(*apn));
377 	apn->apr_next = NULL;
378 	apn->apr_soft = NULL;
379 
380 	if (apn->apr_create != NULL) {
381 		apn->apr_soft = (*apn->apr_create)(softc);
382 		if (apn->apr_soft == NULL) {
383 			KFREE(apn);
384 			return (NULL);
385 		}
386 	}
387 
388 	apn->apr_parent = orig;
389 	orig->apr_clones++;
390 
391 	return (apn);
392 }
393 
394 
395 /* ------------------------------------------------------------------------ */
396 /* Function:    ipf_proxy_soft_create                                       */
397 /* Returns:     int      - 0 == success, else failure.                      */
398 /* Parameters:  softc(I) - pointer to soft context main structure           */
399 /*              arg(I)   - pointer to proxy contect data                    */
400 /*                                                                          */
401 /* Initialise the proxy context and walk through each of the proxies and    */
402 /* call its initialisation function. This allows for proxies to do any      */
403 /* local setup prior to actual use.                                         */
404 /* ------------------------------------------------------------------------ */
405 int
ipf_proxy_soft_init(ipf_main_softc_t * softc,void * arg)406 ipf_proxy_soft_init(ipf_main_softc_t *softc, void *arg)
407 {
408 	ipf_proxy_softc_t *softp;
409 	aproxy_t *ap;
410 	u_int size;
411 	int err;
412 
413 	softp = arg;
414 	size = softp->ips_proxy_session_size * sizeof(ap_session_t *);
415 
416 	KMALLOCS(softp->ips_sess_tab, ap_session_t **, size);
417 
418 	if (softp->ips_sess_tab == NULL)
419 		return (-1);
420 
421 	bzero(softp->ips_sess_tab, size);
422 
423 	for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next) {
424 		if (ap->apr_init != NULL) {
425 			err = (*ap->apr_init)(softc, ap->apr_soft);
426 			if (err != 0)
427 				return (-2);
428 		}
429 	}
430 	softp->ips_init_run = 1;
431 
432 	return (0);
433 }
434 
435 
436 /* ------------------------------------------------------------------------ */
437 /* Function:    ipf_proxy_soft_create                                       */
438 /* Returns:     int      - 0 == success, else failure.                      */
439 /* Parameters:  softc(I) - pointer to soft context main structure           */
440 /*              arg(I)   - pointer to proxy contect data                    */
441 /*                                                                          */
442 /* This function should always succeed. It is responsible for ensuring that */
443 /* the proxy context can be safely called when ipf_proxy_soft_destroy is    */
444 /* called and suring all of the proxies have similarly been instructed.     */
445 /* ------------------------------------------------------------------------ */
446 int
ipf_proxy_soft_fini(ipf_main_softc_t * softc,void * arg)447 ipf_proxy_soft_fini(ipf_main_softc_t *softc, void *arg)
448 {
449 	ipf_proxy_softc_t *softp = arg;
450 	aproxy_t *ap;
451 
452 	for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next) {
453 		if (ap->apr_fini != NULL) {
454 			(*ap->apr_fini)(softc, ap->apr_soft);
455 		}
456 	}
457 
458 	if (softp->ips_sess_tab != NULL) {
459 		KFREES(softp->ips_sess_tab,
460 		       softp->ips_proxy_session_size * sizeof(ap_session_t *));
461 		softp->ips_sess_tab = NULL;
462 	}
463 	softp->ips_init_run = 0;
464 
465 	return (0);
466 }
467 
468 
469 /* ------------------------------------------------------------------------ */
470 /* Function:    ipf_proxy_soft_destroy                                      */
471 /* Returns:     Nil                                                         */
472 /* Parameters:  softc(I) - pointer to soft context main structure           */
473 /*              arg(I)   - pointer to proxy contect data                    */
474 /*                                                                          */
475 /* Free up all of the local data structures allocated during creation.      */
476 /* ------------------------------------------------------------------------ */
477 void
ipf_proxy_soft_destroy(ipf_main_softc_t * softc,void * arg)478 ipf_proxy_soft_destroy(ipf_main_softc_t *softc, void *arg)
479 {
480 	ipf_proxy_softc_t *softp = arg;
481 	aproxy_t *ap;
482 
483 	while ((ap = softp->ips_proxies) != NULL) {
484 		softp->ips_proxies = ap->apr_next;
485 		if (ap->apr_destroy != NULL)
486 			(*ap->apr_destroy)(softc, ap->apr_soft);
487 		ap->apr_parent->apr_clones--;
488 		KFREE(ap);
489 	}
490 
491 	if (softp->ipf_proxy_tune != NULL) {
492 		ipf_tune_array_unlink(softc, softp->ipf_proxy_tune);
493 		KFREES(softp->ipf_proxy_tune, sizeof(ipf_proxy_tuneables));
494 		softp->ipf_proxy_tune = NULL;
495 	}
496 
497 	KFREE(softp);
498 }
499 
500 
501 /* ------------------------------------------------------------------------ */
502 /* Function:    ipf_proxy_flush                                             */
503 /* Returns:     Nil                                                         */
504 /* Parameters:  arg(I)   - pointer to proxy contect data                    */
505 /*              how(I)   - indicates the type of flush operation            */
506 /*                                                                          */
507 /* Walk through all of the proxies and pass on the flush command as either  */
508 /* a flush or a clear.                                                      */
509 /* ------------------------------------------------------------------------ */
510 void
ipf_proxy_flush(void * arg,int how)511 ipf_proxy_flush(void *arg, int how)
512 {
513 	ipf_proxy_softc_t *softp = arg;
514 	aproxy_t *ap;
515 
516 	switch (how)
517 	{
518 	case 0 :
519 		for (ap = softp->ips_proxies; ap; ap = ap->apr_next)
520 			if (ap->apr_flush != NULL)
521 				(*ap->apr_flush)(ap, how);
522 		break;
523 	case 1 :
524 		for (ap = softp->ips_proxies; ap; ap = ap->apr_next)
525 			if (ap->apr_clear != NULL)
526 				(*ap->apr_clear)(ap);
527 		break;
528 	default :
529 		break;
530 	}
531 }
532 
533 
534 /* ------------------------------------------------------------------------ */
535 /* Function:    ipf_proxy_add                                               */
536 /* Returns:     int   - 0 == success, else failure.                         */
537 /* Parameters:  ap(I) - pointer to proxy structure                          */
538 /*                                                                          */
539 /* Dynamically add a new kernel proxy.  Ensure that it is unique in the     */
540 /* collection compiled in and dynamically added.                            */
541 /* ------------------------------------------------------------------------ */
542 int
ipf_proxy_add(void * arg,aproxy_t * ap)543 ipf_proxy_add(void *arg, aproxy_t *ap)
544 {
545 	ipf_proxy_softc_t *softp = arg;
546 
547 	aproxy_t *a;
548 
549 	for (a = ips_proxies; a->apr_p; a++)
550 		if ((a->apr_p == ap->apr_p) &&
551 		    !strncmp(a->apr_label, ap->apr_label,
552 			     sizeof(ap->apr_label))) {
553 			if (softp->ips_proxy_debug & 0x01)
554 				printf("ipf_proxy_add: %s/%d present (B)\n",
555 				       a->apr_label, a->apr_p);
556 			return (-1);
557 		}
558 
559 	for (a = ap_proxylist; (a != NULL); a = a->apr_next)
560 		if ((a->apr_p == ap->apr_p) &&
561 		    !strncmp(a->apr_label, ap->apr_label,
562 			     sizeof(ap->apr_label))) {
563 			if (softp->ips_proxy_debug & 0x01)
564 				printf("ipf_proxy_add: %s/%d present (D)\n",
565 				       a->apr_label, a->apr_p);
566 			return (-1);
567 		}
568 	ap->apr_next = ap_proxylist;
569 	ap_proxylist = ap;
570 	if (ap->apr_load != NULL)
571 		(*ap->apr_load)();
572 	return (0);
573 }
574 
575 
576 /* ------------------------------------------------------------------------ */
577 /* Function:    ipf_proxy_ctl                                               */
578 /* Returns:     int    - 0 == success, else error                           */
579 /* Parameters:  softc(I) - pointer to soft context main structure           */
580 /*              arg(I)   - pointer to proxy context                         */
581 /*              ctl(I)   - pointer to proxy control structure               */
582 /*                                                                          */
583 /* Check to see if the proxy this control request has come through for      */
584 /* exists, and if it does and it has a control function then invoke that    */
585 /* control function.                                                        */
586 /* ------------------------------------------------------------------------ */
587 int
ipf_proxy_ctl(ipf_main_softc_t * softc,void * arg,ap_ctl_t * ctl)588 ipf_proxy_ctl(ipf_main_softc_t *softc, void *arg, ap_ctl_t *ctl)
589 {
590 	ipf_proxy_softc_t *softp = arg;
591 	aproxy_t *a;
592 	int error;
593 
594 	a = ipf_proxy_lookup(arg, ctl->apc_p, ctl->apc_label);
595 	if (a == NULL) {
596 		if (softp->ips_proxy_debug & 0x01)
597 			printf("ipf_proxy_ctl: can't find %s/%d\n",
598 				ctl->apc_label, ctl->apc_p);
599 		IPFERROR(80001);
600 		error = ESRCH;
601 	} else if (a->apr_ctl == NULL) {
602 		if (softp->ips_proxy_debug & 0x01)
603 			printf("ipf_proxy_ctl: no ctl function for %s/%d\n",
604 				ctl->apc_label, ctl->apc_p);
605 		IPFERROR(80002);
606 		error = ENXIO;
607 	} else {
608 		error = (*a->apr_ctl)(softc, a->apr_soft, ctl);
609 		if ((error != 0) && (softp->ips_proxy_debug & 0x02))
610 			printf("ipf_proxy_ctl: %s/%d ctl error %d\n",
611 				a->apr_label, a->apr_p, error);
612 	}
613 	return (error);
614 }
615 
616 
617 /* ------------------------------------------------------------------------ */
618 /* Function:    ipf_proxy_del                                               */
619 /* Returns:     int   - 0 == success, else failure.                         */
620 /* Parameters:  ap(I) - pointer to proxy structure                          */
621 /*                                                                          */
622 /* Delete a proxy that has been added dynamically from those available.     */
623 /* If it is in use, return 1 (do not destroy NOW), not in use 0 or -1       */
624 /* if it cannot be matched.                                                 */
625 /* ------------------------------------------------------------------------ */
626 int
ipf_proxy_del(aproxy_t * ap)627 ipf_proxy_del(aproxy_t *ap)
628 {
629 	aproxy_t *a, **app;
630 
631 	for (app = &ap_proxylist; ((a = *app) != NULL); app = &a->apr_next) {
632 		if (a == ap) {
633 			a->apr_flags |= APR_DELETE;
634 			if (ap->apr_ref == 0 && ap->apr_clones == 0) {
635 				*app = a->apr_next;
636 				return (0);
637 			}
638 			return (1);
639 		}
640 	}
641 
642 	return (-1);
643 }
644 
645 
646 /* ------------------------------------------------------------------------ */
647 /* Function:    ipf_proxy_ok                                                */
648 /* Returns:     int    - 1 == good match else not.                          */
649 /* Parameters:  fin(I) - pointer to packet information                      */
650 /*              tcp(I) - pointer to TCP/UDP header                          */
651 /*              nat(I) - pointer to current NAT session                     */
652 /*                                                                          */
653 /* This function extends the NAT matching to ensure that a packet that has  */
654 /* arrived matches the proxy information attached to the NAT rule. Notably, */
655 /* if the proxy is scheduled to be deleted then packets will not match the  */
656 /* rule even if the rule is still active.                                   */
657 /* ------------------------------------------------------------------------ */
658 int
ipf_proxy_ok(fr_info_t * fin,tcphdr_t * tcp,ipnat_t * np)659 ipf_proxy_ok(fr_info_t *fin, tcphdr_t *tcp, ipnat_t *np)
660 {
661 	aproxy_t *apr = np->in_apr;
662 	u_short dport = np->in_odport;
663 
664 	if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
665 	    (fin->fin_p != apr->apr_p))
666 		return (0);
667 	if ((tcp == NULL) && dport)
668 		return (0);
669 	return (1);
670 }
671 
672 
673 /* ------------------------------------------------------------------------ */
674 /* Function:    ipf_proxy_ioctl                                             */
675 /* Returns:     int    - 0 == success, else error                           */
676 /* Parameters:  softc(I) - pointer to soft context main structure           */
677 /*              data(I)  - pointer to ioctl data                            */
678 /*              cmd(I)   - ioctl command                                    */
679 /*              mode(I)  - mode bits for device                             */
680 /*              ctx(I)   - pointer to context information                   */
681 /*                                                                          */
682 /* ------------------------------------------------------------------------ */
683 int
ipf_proxy_ioctl(ipf_main_softc_t * softc,caddr_t data,ioctlcmd_t cmd,int mode,void * ctx)684 ipf_proxy_ioctl(ipf_main_softc_t *softc, caddr_t data, ioctlcmd_t cmd,
685 	int mode, void *ctx)
686 {
687 	ap_ctl_t ctl;
688 	caddr_t ptr;
689 	int error;
690 
691 	mode = mode;	/* LINT */
692 
693 	switch (cmd)
694 	{
695 	case SIOCPROXY :
696 		error = ipf_inobj(softc, data, NULL, &ctl, IPFOBJ_PROXYCTL);
697 		if (error != 0) {
698 			return (error);
699 		}
700 		ptr = NULL;
701 
702 		if (ctl.apc_dsize > 0) {
703 			KMALLOCS(ptr, caddr_t, ctl.apc_dsize);
704 			if (ptr == NULL) {
705 				IPFERROR(80003);
706 				error = ENOMEM;
707 			} else {
708 				error = copyinptr(softc, ctl.apc_data, ptr,
709 						  ctl.apc_dsize);
710 				if (error == 0)
711 					ctl.apc_data = ptr;
712 			}
713 		} else {
714 			ctl.apc_data = NULL;
715 			error = 0;
716 		}
717 
718 		if (error == 0)
719 			error = ipf_proxy_ctl(softc, softc->ipf_proxy_soft,
720 					      &ctl);
721 
722 		if ((error != 0) && (ptr != NULL)) {
723 			KFREES(ptr, ctl.apc_dsize);
724 		}
725 		break;
726 
727 	default :
728 		IPFERROR(80004);
729 		error = EINVAL;
730 	}
731 	return (error);
732 }
733 
734 
735 /* ------------------------------------------------------------------------ */
736 /* Function:    ipf_proxy_match                                             */
737 /* Returns:     int    - 0 == success, else error                           */
738 /* Parameters:  fin(I) - pointer to packet information                      */
739 /*              nat(I) - pointer to current NAT session                     */
740 /*                                                                          */
741 /* If a proxy has a match function, call that to do extended packet         */
742 /* matching. Whilst other parts of the NAT code are rather lenient when it  */
743 /* comes to the quality of the packet that it will transform, the proxy     */
744 /* matching is not because they need to work with data, not just headers.   */
745 /* ------------------------------------------------------------------------ */
746 int
ipf_proxy_match(fr_info_t * fin,nat_t * nat)747 ipf_proxy_match(fr_info_t *fin, nat_t *nat)
748 {
749 	ipf_main_softc_t *softc = fin->fin_main_soft;
750 	ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
751 	aproxy_t *apr;
752 	ipnat_t *ipn;
753 	int result;
754 
755 	ipn = nat->nat_ptr;
756 	if (softp->ips_proxy_debug & 0x04)
757 		printf("ipf_proxy_match(%lx,%lx) aps %lx ptr %lx\n",
758 			(u_long)fin, (u_long)nat, (u_long)nat->nat_aps,
759 			(u_long)ipn);
760 
761 	if ((fin->fin_flx & (FI_SHORT|FI_BAD)) != 0) {
762 		if (softp->ips_proxy_debug & 0x08)
763 			printf("ipf_proxy_match: flx 0x%x (BAD|SHORT)\n",
764 				fin->fin_flx);
765 		return (-1);
766 	}
767 
768 	apr = ipn->in_apr;
769 	if ((apr == NULL) || (apr->apr_flags & APR_DELETE)) {
770 		if (softp->ips_proxy_debug & 0x08)
771 			printf("ipf_proxy_match:apr %lx apr_flags 0x%x\n",
772 				(u_long)apr, apr ? apr->apr_flags : 0);
773 		return (-1);
774 	}
775 
776 	if (apr->apr_match != NULL) {
777 		result = (*apr->apr_match)(fin, nat->nat_aps, nat);
778 		if (result != 0) {
779 			if (softp->ips_proxy_debug & 0x08)
780 				printf("ipf_proxy_match: result %d\n", result);
781 			return (-1);
782 		}
783 	}
784 	return (0);
785 }
786 
787 
788 /* ------------------------------------------------------------------------ */
789 /* Function:    ipf_proxy_new                                               */
790 /* Returns:     int    - 0 == success, else error                           */
791 /* Parameters:  fin(I) - pointer to packet information                      */
792 /*              nat(I) - pointer to current NAT session                     */
793 /*                                                                          */
794 /* Allocate a new application proxy structure and fill it in with the       */
795 /* relevant details.  call the init function once complete, prior to        */
796 /* returning.                                                               */
797 /* ------------------------------------------------------------------------ */
798 int
ipf_proxy_new(fr_info_t * fin,nat_t * nat)799 ipf_proxy_new(fr_info_t *fin, nat_t *nat)
800 {
801 	ipf_main_softc_t *softc = fin->fin_main_soft;
802 	ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
803 	register ap_session_t *aps;
804 	aproxy_t *apr;
805 
806 	if (softp->ips_proxy_debug & 0x04)
807 		printf("ipf_proxy_new(%lx,%lx) \n", (u_long)fin, (u_long)nat);
808 
809 	if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL)) {
810 		if (softp->ips_proxy_debug & 0x08)
811 			printf("ipf_proxy_new: nat_ptr %lx nat_aps %lx\n",
812 				(u_long)nat->nat_ptr, (u_long)nat->nat_aps);
813 		return (-1);
814 	}
815 
816 	apr = nat->nat_ptr->in_apr;
817 
818 	if ((apr->apr_flags & APR_DELETE) ||
819 	    (fin->fin_p != apr->apr_p)) {
820 		if (softp->ips_proxy_debug & 0x08)
821 			printf("ipf_proxy_new: apr_flags 0x%x p %d/%d\n",
822 				apr->apr_flags, fin->fin_p, apr->apr_p);
823 		return (-1);
824 	}
825 
826 	KMALLOC(aps, ap_session_t *);
827 	if (!aps) {
828 		if (softp->ips_proxy_debug & 0x08)
829 			printf("ipf_proxy_new: malloc failed (%lu)\n",
830 				(u_long)sizeof(ap_session_t));
831 		return (-1);
832 	}
833 
834 	bzero((char *)aps, sizeof(*aps));
835 	aps->aps_data = NULL;
836 	aps->aps_apr = apr;
837 	aps->aps_psiz = 0;
838 	if (apr->apr_new != NULL)
839 		if ((*apr->apr_new)(apr->apr_soft, fin, aps, nat) == -1) {
840 			if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
841 				KFREES(aps->aps_data, aps->aps_psiz);
842 			}
843 			KFREE(aps);
844 			if (softp->ips_proxy_debug & 0x08)
845 				printf("ipf_proxy_new: new(%lx) failed\n",
846 					(u_long)apr->apr_new);
847 			return (-1);
848 		}
849 	aps->aps_nat = nat;
850 	aps->aps_next = softp->ips_sess_list;
851 	softp->ips_sess_list = aps;
852 	nat->nat_aps = aps;
853 
854 	return (0);
855 }
856 
857 
858 /* ------------------------------------------------------------------------ */
859 /* Function:    ipf_proxy_check                                             */
860 /* Returns:     int - -1 == error, 1 == success                             */
861 /* Parameters:  fin(I) - pointer to packet information                      */
862 /*              nat(I) - pointer to current NAT session                     */
863 /*                                                                          */
864 /* Check to see if a packet should be passed through an active proxy        */
865 /* routine if one has been setup for it.  We don't need to check the        */
866 /* checksum here if IPFILTER_CKSUM is defined because if it is, a failed    */
867 /* check causes FI_BAD to be set.                                           */
868 /* ------------------------------------------------------------------------ */
869 int
ipf_proxy_check(fr_info_t * fin,nat_t * nat)870 ipf_proxy_check(fr_info_t *fin, nat_t *nat)
871 {
872 	ipf_main_softc_t *softc = fin->fin_main_soft;
873 	ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
874 #if SOLARIS && defined(_KERNEL) && defined(ICK_VALID)
875 	mb_t *m;
876 #endif
877 	tcphdr_t *tcp = NULL;
878 	udphdr_t *udp = NULL;
879 	ap_session_t *aps;
880 	aproxy_t *apr;
881 	short adjlen;
882 	int dosum;
883 	ip_t *ip;
884 	short rv;
885 	int err;
886 #if !defined(_KERNEL) || SOLARIS || defined(__FreeBSD__)
887 	u_32_t s1, s2, sd;
888 #endif
889 
890 	if (fin->fin_flx & FI_BAD) {
891 		if (softp->ips_proxy_debug & 0x08)
892 			printf("ipf_proxy_check: flx 0x%x (BAD)\n",
893 			       fin->fin_flx);
894 		return (-1);
895 	}
896 
897 #ifndef IPFILTER_CKSUM
898 	if ((fin->fin_out == 0) && (ipf_checkl4sum(fin) == -1)) {
899 		if (softp->ips_proxy_debug & 0x08)
900 			printf("ipf_proxy_check: l4 checksum failure %d\n",
901 				fin->fin_p);
902 		if (fin->fin_p == IPPROTO_TCP)
903 			softc->ipf_stats[fin->fin_out].fr_tcpbad++;
904 		return (-1);
905 	}
906 #endif
907 
908 	aps = nat->nat_aps;
909 	if (aps != NULL) {
910 		/*
911 		 * If there is data in this packet to be proxied then try and
912 		 * get it all into the one buffer, else drop it.
913 		 */
914 #if SOLARIS || defined(HAVE_M_PULLDOWN)
915 		if ((fin->fin_dlen > 0) && !(fin->fin_flx & FI_COALESCE))
916 			if (ipf_coalesce(fin) == -1) {
917 				if (softp->ips_proxy_debug & 0x08)
918 					printf("ipf_proxy_check: %s %x\n",
919 					       "coalesce failed", fin->fin_flx);
920 				return (-1);
921 			}
922 #endif
923 		ip = fin->fin_ip;
924 		if (fin->fin_cksum > FI_CK_SUMOK)
925 			dosum = 0;
926 		else
927 			dosum = 1;
928 
929 		switch (fin->fin_p)
930 		{
931 		case IPPROTO_TCP :
932 			tcp = (tcphdr_t *)fin->fin_dp;
933 #if SOLARIS && defined(_KERNEL) && defined(ICK_VALID)
934 			m = fin->fin_qfm;
935 			if (dohwcksum && (m->b_ick_flag == ICK_VALID))
936 				dosum = 0;
937 #endif
938 			break;
939 		case IPPROTO_UDP :
940 			udp = (udphdr_t *)fin->fin_dp;
941 			break;
942 		default :
943 			break;
944 		}
945 
946 		apr = aps->aps_apr;
947 		err = 0;
948 		if (fin->fin_out != 0) {
949 			if (apr->apr_outpkt != NULL)
950 				err = (*apr->apr_outpkt)(apr->apr_soft, fin,
951 							 aps, nat);
952 		} else {
953 			if (apr->apr_inpkt != NULL)
954 				err = (*apr->apr_inpkt)(apr->apr_soft, fin,
955 							aps, nat);
956 		}
957 
958 		rv = APR_EXIT(err);
959 		if (((softp->ips_proxy_debug & 0x08) && (rv != 0)) ||
960 		    (softp->ips_proxy_debug & 0x04))
961 			printf("ipf_proxy_check: out %d err %x rv %d\n",
962 				fin->fin_out, err, rv);
963 		if (rv == 1)
964 			return (-1);
965 
966 		if (rv == 2) {
967 			ipf_proxy_deref(apr);
968 			nat->nat_aps = NULL;
969 			return (-1);
970 		}
971 
972 		/*
973 		 * If err != 0 then the data size of the packet has changed
974 		 * so we need to recalculate the header checksums for the
975 		 * packet.
976 		 */
977 		adjlen = APR_INC(err);
978 #if !defined(_KERNEL) || SOLARIS || defined(__FreeBSD__)
979 		s1 = LONG_SUM(fin->fin_plen - adjlen);
980 		s2 = LONG_SUM(fin->fin_plen);
981 		CALC_SUMD(s1, s2, sd);
982 		if ((err != 0) && (fin->fin_cksum < FI_CK_L4PART) &&
983 		    fin->fin_v == 4)
984 			ipf_fix_outcksum(0, &ip->ip_sum, sd, 0);
985 #endif
986 		if (fin->fin_flx & FI_DOCKSUM)
987 			dosum = 1;
988 
989 		/*
990 		 * For TCP packets, we may need to adjust the sequence and
991 		 * acknowledgement numbers to reflect changes in size of the
992 		 * data stream.
993 		 *
994 		 * For both TCP and UDP, recalculate the layer 4 checksum,
995 		 * regardless, as we can't tell (here) if data has been
996 		 * changed or not.
997 		 */
998 		if (tcp != NULL) {
999 			err = ipf_proxy_fixseqack(fin, ip, aps, adjlen);
1000 			if (fin->fin_cksum == FI_CK_L4PART) {
1001 				u_short sum = ntohs(tcp->th_sum);
1002 				sum += adjlen;
1003 				tcp->th_sum = htons(sum);
1004 			} else if (fin->fin_cksum < FI_CK_L4PART) {
1005 				tcp->th_sum = fr_cksum(fin, ip,
1006 						       IPPROTO_TCP, tcp);
1007 			}
1008 		} else if ((udp != NULL) && (udp->uh_sum != 0)) {
1009 			if (fin->fin_cksum == FI_CK_L4PART) {
1010 				u_short sum = ntohs(udp->uh_sum);
1011 				sum += adjlen;
1012 				udp->uh_sum = htons(sum);
1013 			} else if (dosum) {
1014 				udp->uh_sum = fr_cksum(fin, ip,
1015 						       IPPROTO_UDP, udp);
1016 			}
1017 		}
1018 		aps->aps_bytes += fin->fin_plen;
1019 		aps->aps_pkts++;
1020 	}
1021 	return (1);
1022 }
1023 
1024 
1025 /* ------------------------------------------------------------------------ */
1026 /* Function:    ipf_proxy_lookup                                            */
1027 /* Returns:     int - -1 == error, 0 == success                             */
1028 /* Parameters:  arg(I)  - pointer to proxy context information              */
1029 /*              pr(I)   - protocol number for proxy                         */
1030 /*              name(I) - proxy name                                        */
1031 /*                                                                          */
1032 /* Search for a proxy by the protocol being used and by its name.           */
1033 /* ------------------------------------------------------------------------ */
1034 aproxy_t *
ipf_proxy_lookup(void * arg,u_int pr,char * name)1035 ipf_proxy_lookup(void *arg, u_int pr, char *name)
1036 {
1037 	ipf_proxy_softc_t *softp = arg;
1038 	aproxy_t *ap;
1039 
1040 	if (softp->ips_proxy_debug & 0x04)
1041 		printf("ipf_proxy_lookup(%d,%s)\n", pr, name);
1042 
1043 	for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next)
1044 		if ((ap->apr_p == pr) &&
1045 		    !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
1046 			ap->apr_ref++;
1047 			return (ap);
1048 		}
1049 
1050 	if (softp->ips_proxy_debug & 0x08)
1051 		printf("ipf_proxy_lookup: failed for %d/%s\n", pr, name);
1052 	return (NULL);
1053 }
1054 
1055 
1056 /* ------------------------------------------------------------------------ */
1057 /* Function:    ipf_proxy_deref                                             */
1058 /* Returns:     Nil                                                         */
1059 /* Parameters:  ap(I) - pointer to proxy structure                          */
1060 /*                                                                          */
1061 /* Drop the reference counter associated with the proxy.                    */
1062 /* ------------------------------------------------------------------------ */
1063 void
ipf_proxy_deref(aproxy_t * ap)1064 ipf_proxy_deref(aproxy_t *ap)
1065 {
1066 	ap->apr_ref--;
1067 }
1068 
1069 
1070 /* ------------------------------------------------------------------------ */
1071 /* Function:    ipf_proxy_free                                              */
1072 /* Returns:     Nil                                                         */
1073 /* Parameters:  softc(I) - pointer to soft context main structure           */
1074 /*              aps(I)   - pointer to current proxy session                 */
1075 /* Locks Held:  ipf_nat_new, ipf_nat(W)                                     */
1076 /*                                                                          */
1077 /* Free up proxy session information allocated to be used with a NAT        */
1078 /* session.                                                                 */
1079 /* ------------------------------------------------------------------------ */
1080 void
ipf_proxy_free(ipf_main_softc_t * softc,ap_session_t * aps)1081 ipf_proxy_free(ipf_main_softc_t *softc, ap_session_t *aps)
1082 {
1083 	ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
1084 	ap_session_t *a, **ap;
1085 	aproxy_t *apr;
1086 
1087 	if (!aps)
1088 		return;
1089 
1090 	for (ap = &softp->ips_sess_list; ((a = *ap) != NULL); ap = &a->aps_next)
1091 		if (a == aps) {
1092 			*ap = a->aps_next;
1093 			break;
1094 		}
1095 
1096 	apr = aps->aps_apr;
1097 	if ((apr != NULL) && (apr->apr_del != NULL))
1098 		(*apr->apr_del)(softc, aps);
1099 
1100 	if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
1101 		KFREES(aps->aps_data, aps->aps_psiz);
1102 	KFREE(aps);
1103 }
1104 
1105 
1106 /* ------------------------------------------------------------------------ */
1107 /* Function:    ipf_proxy_fixseqack                                         */
1108 /* Returns:     int    - 2 if TCP ack/seq is changed, else 0                */
1109 /* Parameters:  fin(I) - pointer to packet information                      */
1110 /*              ip(I)  - pointer to IP header                               */
1111 /*              nat(I) - pointer to current NAT session                     */
1112 /*              inc(I) - delta to apply to TCP sequence numbering           */
1113 /*                                                                          */
1114 /* Adjust the TCP sequence/acknowledge numbers in the TCP header based on   */
1115 /* whether or not the new header is past the point at which an adjustment   */
1116 /* occurred. This might happen because of (say) an FTP string being changed */
1117 /* and the new string being a different length to the old.                  */
1118 /* ------------------------------------------------------------------------ */
1119 static int
ipf_proxy_fixseqack(fr_info_t * fin,ip_t * ip,ap_session_t * aps,int inc)1120 ipf_proxy_fixseqack(fr_info_t *fin, ip_t *ip, ap_session_t *aps, int inc)
1121 {
1122 	ipf_main_softc_t *softc = fin->fin_main_soft;
1123 	ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
1124 	int sel, ch = 0, out, nlen;
1125 	u_32_t seq1, seq2;
1126 	tcphdr_t *tcp;
1127 	short inc2;
1128 
1129 	tcp = (tcphdr_t *)fin->fin_dp;
1130 	out = fin->fin_out;
1131 	/*
1132 	 * ip_len has already been adjusted by 'inc'.
1133 	 */
1134 	nlen = fin->fin_dlen;
1135 	nlen -= (TCP_OFF(tcp) << 2);
1136 
1137 	inc2 = inc;
1138 	inc = (int)inc2;
1139 
1140 	if (out != 0) {
1141 		seq1 = (u_32_t)ntohl(tcp->th_seq);
1142 		sel = aps->aps_sel[out];
1143 
1144 		/* switch to other set ? */
1145 		if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
1146 		    (seq1 > aps->aps_seqmin[!sel])) {
1147 			if (softp->ips_proxy_debug & 0x10)
1148 				printf("proxy out switch set seq %d -> %d %x > %x\n",
1149 					sel, !sel, seq1,
1150 					aps->aps_seqmin[!sel]);
1151 			sel = aps->aps_sel[out] = !sel;
1152 		}
1153 
1154 		if (aps->aps_seqoff[sel]) {
1155 			seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
1156 			if (seq1 > seq2) {
1157 				seq2 = aps->aps_seqoff[sel];
1158 				seq1 += seq2;
1159 				tcp->th_seq = htonl(seq1);
1160 				ch = 1;
1161 			}
1162 		}
1163 
1164 		if (inc && (seq1 > aps->aps_seqmin[!sel])) {
1165 			aps->aps_seqmin[sel] = seq1 + nlen - 1;
1166 			aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
1167 			if (softp->ips_proxy_debug & 0x10)
1168 				printf("proxy seq set %d at %x to %d + %d\n",
1169 					sel, aps->aps_seqmin[sel],
1170 					aps->aps_seqoff[sel], inc);
1171 		}
1172 
1173 		/***/
1174 
1175 		seq1 = ntohl(tcp->th_ack);
1176 		sel = aps->aps_sel[1 - out];
1177 
1178 		/* switch to other set ? */
1179 		if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
1180 		    (seq1 > aps->aps_ackmin[!sel])) {
1181 			if (softp->ips_proxy_debug & 0x10)
1182 				printf("proxy out switch set ack %d -> %d %x > %x\n",
1183 					sel, !sel, seq1,
1184 					aps->aps_ackmin[!sel]);
1185 			sel = aps->aps_sel[1 - out] = !sel;
1186 		}
1187 
1188 		if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
1189 			seq2 = aps->aps_ackoff[sel];
1190 			tcp->th_ack = htonl(seq1 - seq2);
1191 			ch = 1;
1192 		}
1193 	} else {
1194 		seq1 = ntohl(tcp->th_seq);
1195 		sel = aps->aps_sel[out];
1196 
1197 		/* switch to other set ? */
1198 		if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
1199 		    (seq1 > aps->aps_ackmin[!sel])) {
1200 			if (softp->ips_proxy_debug & 0x10)
1201 				printf("proxy in switch set ack %d -> %d %x > %x\n",
1202 					sel, !sel, seq1, aps->aps_ackmin[!sel]);
1203 			sel = aps->aps_sel[out] = !sel;
1204 		}
1205 
1206 		if (aps->aps_ackoff[sel]) {
1207 			seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
1208 			if (seq1 > seq2) {
1209 				seq2 = aps->aps_ackoff[sel];
1210 				seq1 += seq2;
1211 				tcp->th_seq = htonl(seq1);
1212 				ch = 1;
1213 			}
1214 		}
1215 
1216 		if (inc && (seq1 > aps->aps_ackmin[!sel])) {
1217 			aps->aps_ackmin[!sel] = seq1 + nlen - 1;
1218 			aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
1219 
1220 			if (softp->ips_proxy_debug & 0x10)
1221 				printf("proxy ack set %d at %x to %d + %d\n",
1222 					!sel, aps->aps_seqmin[!sel],
1223 					aps->aps_seqoff[sel], inc);
1224 		}
1225 
1226 		/***/
1227 
1228 		seq1 = ntohl(tcp->th_ack);
1229 		sel = aps->aps_sel[1 - out];
1230 
1231 		/* switch to other set ? */
1232 		if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
1233 		    (seq1 > aps->aps_seqmin[!sel])) {
1234 			if (softp->ips_proxy_debug & 0x10)
1235 				printf("proxy in switch set seq %d -> %d %x > %x\n",
1236 					sel, !sel, seq1, aps->aps_seqmin[!sel]);
1237 			sel = aps->aps_sel[1 - out] = !sel;
1238 		}
1239 
1240 		if (aps->aps_seqoff[sel] != 0) {
1241 			if (softp->ips_proxy_debug & 0x10)
1242 				printf("sel %d seqoff %d seq1 %x seqmin %x\n",
1243 					sel, aps->aps_seqoff[sel], seq1,
1244 					aps->aps_seqmin[sel]);
1245 			if (seq1 > aps->aps_seqmin[sel]) {
1246 				seq2 = aps->aps_seqoff[sel];
1247 				tcp->th_ack = htonl(seq1 - seq2);
1248 				ch = 1;
1249 			}
1250 		}
1251 	}
1252 
1253 	if (softp->ips_proxy_debug & 0x10)
1254 		printf("ipf_proxy_fixseqack: seq %u ack %u\n",
1255 			(u_32_t)ntohl(tcp->th_seq), (u_32_t)ntohl(tcp->th_ack));
1256 	return (ch ? 2 : 0);
1257 }
1258 
1259 
1260 /* ------------------------------------------------------------------------ */
1261 /* Function:    ipf_proxy_rule_rev                                          */
1262 /* Returns:     ipnat_t * - NULL = failure, else pointer to new rule        */
1263 /* Parameters:  nat(I) - pointer to NAT session to create rule from         */
1264 /*                                                                          */
1265 /* This function creates a NAT rule that is based upon the reverse packet   */
1266 /* flow associated with this NAT session. Thus if this NAT session was      */
1267 /* created with a map rule then this function will create a rdr rule.       */
1268 /* Only address fields and network interfaces are assigned in this function */
1269 /* and the address fields are formed such that an exact is required. If the */
1270 /* original rule had a netmask, that is not replicated here not is it       */
1271 /* desired. The ultimate goal here is to create a NAT rule to support a NAT */
1272 /* session being created that does not have a user configured rule. The     */
1273 /* classic example is supporting the FTP proxy, where a data channel needs  */
1274 /* to be setup, based on the addresses used for the control connection. In  */
1275 /* that case, this function is used to handle creating NAT rules to support */
1276 /* data connections with the PORT and EPRT commands.                        */
1277 /* ------------------------------------------------------------------------ */
1278 ipnat_t *
ipf_proxy_rule_rev(nat_t * nat)1279 ipf_proxy_rule_rev(nat_t *nat)
1280 {
1281 	ipnat_t *old;
1282 	ipnat_t *ipn;
1283 	int size;
1284 
1285 	old = nat->nat_ptr;
1286 	size = old->in_size;
1287 
1288 	KMALLOCS(ipn, ipnat_t *, size);
1289 	if (ipn == NULL)
1290 		return (NULL);
1291 
1292 	bzero((char *)ipn, size);
1293 
1294 	ipn->in_use = 1;
1295 	ipn->in_hits = 1;
1296 	ipn->in_ippip = 1;
1297 	ipn->in_apr = NULL;
1298 	ipn->in_size = size;
1299 	ipn->in_pr[0] = old->in_pr[1];
1300 	ipn->in_pr[1] = old->in_pr[0];
1301 	ipn->in_v[0] = old->in_v[1];
1302 	ipn->in_v[1] = old->in_v[0];
1303 	ipn->in_ifps[0] = old->in_ifps[1];
1304 	ipn->in_ifps[1] = old->in_ifps[0];
1305 	ipn->in_flags = (old->in_flags | IPN_PROXYRULE);
1306 
1307 	ipn->in_nsrcip6 = nat->nat_odst6;
1308 	ipn->in_osrcip6 = nat->nat_ndst6;
1309 
1310 	if ((old->in_redir & NAT_REDIRECT) != 0) {
1311 		ipn->in_redir = NAT_MAP;
1312 		if (ipn->in_v[0] == 4) {
1313 			ipn->in_snip = ntohl(nat->nat_odstaddr);
1314 			ipn->in_dnip = ntohl(nat->nat_nsrcaddr);
1315 #ifdef USE_INET6
1316 		} else {
1317 			ipn->in_snip6 = nat->nat_odst6;
1318 			ipn->in_dnip6 = nat->nat_nsrc6;
1319 #endif
1320 		}
1321 		ipn->in_ndstip6 = nat->nat_nsrc6;
1322 		ipn->in_odstip6 = nat->nat_osrc6;
1323 	} else {
1324 		ipn->in_redir = NAT_REDIRECT;
1325 		if (ipn->in_v[0] == 4) {
1326 			ipn->in_snip = ntohl(nat->nat_odstaddr);
1327 			ipn->in_dnip = ntohl(nat->nat_osrcaddr);
1328 #ifdef USE_INET6
1329 		} else {
1330 			ipn->in_snip6 = nat->nat_odst6;
1331 			ipn->in_dnip6 = nat->nat_osrc6;
1332 #endif
1333 		}
1334 		ipn->in_ndstip6 = nat->nat_osrc6;
1335 		ipn->in_odstip6 = nat->nat_nsrc6;
1336 	}
1337 
1338 	IP6_SETONES(&ipn->in_osrcmsk6);
1339 	IP6_SETONES(&ipn->in_nsrcmsk6);
1340 	IP6_SETONES(&ipn->in_odstmsk6);
1341 	IP6_SETONES(&ipn->in_ndstmsk6);
1342 
1343 	ipn->in_namelen = old->in_namelen;
1344 	ipn->in_ifnames[0] = old->in_ifnames[1];
1345 	ipn->in_ifnames[1] = old->in_ifnames[0];
1346 	bcopy(old->in_names, ipn->in_names, ipn->in_namelen);
1347 	MUTEX_INIT(&ipn->in_lock, "ipnat rev rule lock");
1348 
1349 	return (ipn);
1350 }
1351 
1352 
1353 /* ------------------------------------------------------------------------ */
1354 /* Function:    ipf_proxy_rule_fwd                                          */
1355 /* Returns:     ipnat_t * - NULL = failure, else pointer to new rule        */
1356 /* Parameters:  nat(I) - pointer to NAT session to create rule from         */
1357 /*                                                                          */
1358 /* The purpose and rationale of this function is much the same as the above */
1359 /* function, ipf_proxy_rule_rev, except that a rule is created that matches */
1360 /* the same direction as that of the existing NAT session. Thus if this NAT */
1361 /* session was created with a map rule then this function will also create  */
1362 /* a data structure to represent a map rule. Whereas ipf_proxy_rule_rev is  */
1363 /* used to support PORT/EPRT, this function supports PASV/EPSV.             */
1364 /* ------------------------------------------------------------------------ */
1365 ipnat_t *
ipf_proxy_rule_fwd(nat_t * nat)1366 ipf_proxy_rule_fwd(nat_t *nat)
1367 {
1368 	ipnat_t *old;
1369 	ipnat_t *ipn;
1370 	int size;
1371 
1372 	old = nat->nat_ptr;
1373 	size = old->in_size;
1374 
1375 	KMALLOCS(ipn, ipnat_t *, size);
1376 	if (ipn == NULL)
1377 		return (NULL);
1378 
1379 	bzero((char *)ipn, size);
1380 
1381 	ipn->in_use = 1;
1382 	ipn->in_hits = 1;
1383 	ipn->in_ippip = 1;
1384 	ipn->in_apr = NULL;
1385 	ipn->in_size = size;
1386 	ipn->in_pr[0] = old->in_pr[0];
1387 	ipn->in_pr[1] = old->in_pr[1];
1388 	ipn->in_v[0] = old->in_v[0];
1389 	ipn->in_v[1] = old->in_v[1];
1390 	ipn->in_ifps[0] = nat->nat_ifps[0];
1391 	ipn->in_ifps[1] = nat->nat_ifps[1];
1392 	ipn->in_flags = (old->in_flags | IPN_PROXYRULE);
1393 
1394 	ipn->in_nsrcip6 = nat->nat_nsrc6;
1395 	ipn->in_osrcip6 = nat->nat_osrc6;
1396 	ipn->in_ndstip6 = nat->nat_ndst6;
1397 	ipn->in_odstip6 = nat->nat_odst6;
1398 	ipn->in_redir = old->in_redir;
1399 
1400 	if (ipn->in_v[0] == 4) {
1401 		ipn->in_snip = ntohl(nat->nat_nsrcaddr);
1402 		ipn->in_dnip = ntohl(nat->nat_ndstaddr);
1403 #ifdef USE_INET6
1404 	} else {
1405 		ipn->in_snip6 = nat->nat_nsrc6;
1406 		ipn->in_dnip6 = nat->nat_ndst6;
1407 #endif
1408 	}
1409 
1410 	IP6_SETONES(&ipn->in_osrcmsk6);
1411 	IP6_SETONES(&ipn->in_nsrcmsk6);
1412 	IP6_SETONES(&ipn->in_odstmsk6);
1413 	IP6_SETONES(&ipn->in_ndstmsk6);
1414 
1415 	ipn->in_namelen = old->in_namelen;
1416 	ipn->in_ifnames[0] = old->in_ifnames[0];
1417 	ipn->in_ifnames[1] = old->in_ifnames[1];
1418 	bcopy(old->in_names, ipn->in_names, ipn->in_namelen);
1419 	MUTEX_INIT(&ipn->in_lock, "ipnat fwd rule lock");
1420 
1421 	return (ipn);
1422 }
1423