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