4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 #if defined(KERNEL) || defined(_KERNEL)
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
19 # include <sys/fcntl.h>
20 #if !defined(_KERNEL) && !defined(__KERNEL__)
29 # include <sys/protosw.h>
30 #include <sys/socket.h>
33 # include <sys/ctype.h>
35 # include <sys/systm.h>
37 # include <sys/mbuf.h>
40 #if defined(_KERNEL) && defined(__FreeBSD__)
41 # include <sys/filio.h>
42 # include <sys/fcntl.h>
44 # include <sys/ioctl.h>
47 # include <sys/byteorder.h>
49 # include <sys/dditypes.h>
51 # include <sys/stream.h>
52 # include <sys/kmem.h>
55 # include <sys/queue.h>
58 #if defined(__FreeBSD__) && defined(_KERNEL)
61 #define CURVNET_SET(arg)
62 #define CURVNET_RESTORE()
63 #define VNET_DEFINE(_t, _v) _t _v
64 #define VNET_DECLARE(_t, _v) extern _t _v
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 # include <netinet/ip_var.h>
74 #include <netinet/tcp.h>
75 #include <netinet/udp.h>
76 #include <netinet/ip_icmp.h>
77 #include "netinet/ip_compat.h"
78 #include <netinet/tcpip.h>
79 #include "netinet/ip_fil.h"
80 #include "netinet/ip_nat.h"
81 #include "netinet/ip_state.h"
82 #include "netinet/ip_proxy.h"
83 #if defined(__FreeBSD__)
84 # include <sys/malloc.h>
89 #include "netinet/ip_ftp_pxy.c"
90 #include "netinet/ip_tftp_pxy.c"
91 #include "netinet/ip_rcmd_pxy.c"
92 #include "netinet/ip_pptp_pxy.c"
94 # include "netinet/ip_irc_pxy.c"
95 # include "netinet/ip_raudio_pxy.c"
96 # include "netinet/ip_netbios_pxy.c"
98 #include "netinet/ip_ipsec_pxy.c"
99 #include "netinet/ip_rpcb_pxy.c"
102 static const char rcsid[] = "@(#)$Id$";
105 #define AP_SESS_SIZE 53
107 static int ipf_proxy_fixseqack(fr_info_t *, ip_t *, ap_session_t *, int );
108 static aproxy_t *ipf_proxy_create_clone(ipf_main_softc_t *, aproxy_t *);
110 typedef struct ipf_proxy_softc_s {
112 int ips_proxy_session_size;
113 ap_session_t **ips_sess_tab;
114 ap_session_t *ips_sess_list;
115 aproxy_t *ips_proxies;
117 ipftuneable_t *ipf_proxy_tune;
120 static ipftuneable_t ipf_proxy_tuneables[] = {
121 { { (void *)offsetof(ipf_proxy_softc_t, ips_proxy_debug) },
122 "proxy_debug", 0, 0x1f,
123 stsizeof(ipf_proxy_softc_t, ips_proxy_debug),
125 { { NULL }, NULL, 0, 0,
130 static aproxy_t *ap_proxylist = NULL;
131 static aproxy_t ips_proxies[] = {
133 { NULL, NULL, "ftp", (char)IPPROTO_TCP, 0, 0, 0,
134 ipf_p_ftp_main_load, ipf_p_ftp_main_unload,
135 ipf_p_ftp_soft_create, ipf_p_ftp_soft_destroy,
137 ipf_p_ftp_new, ipf_p_ftp_del, ipf_p_ftp_in, ipf_p_ftp_out, NULL,
138 NULL, NULL, NULL, NULL },
140 #ifdef IPF_TFTP_PROXY
141 { NULL, NULL, "tftp", (char)IPPROTO_UDP, 0, 0, 0,
142 ipf_p_tftp_main_load, ipf_p_tftp_main_unload,
143 ipf_p_tftp_soft_create, ipf_p_tftp_soft_destroy,
145 ipf_p_tftp_new, ipf_p_tftp_del,
146 ipf_p_tftp_in, ipf_p_tftp_out, NULL,
147 NULL, NULL, NULL, NULL },
150 { NULL, NULL, "irc", (char)IPPROTO_TCP, 0, 0, 0,
151 ipf_p_irc_main_load, ipf_p_irc_main_unload,
154 ipf_p_irc_new, NULL, NULL, ipf_p_irc_out, NULL,
155 NULL, NULL, NULL, NULL },
157 #ifdef IPF_RCMD_PROXY
158 { NULL, NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, 0,
159 ipf_p_rcmd_main_load, ipf_p_rcmd_main_unload,
162 ipf_p_rcmd_new, ipf_p_rcmd_del,
163 ipf_p_rcmd_in, ipf_p_rcmd_out, NULL,
164 NULL, NULL, NULL, NULL },
166 #ifdef IPF_RAUDIO_PROXY
167 { NULL, NULL, "raudio", (char)IPPROTO_TCP, 0, 0, 0,
168 ipf_p_raudio_main_load, ipf_p_raudio_main_unload,
171 ipf_p_raudio_new, NULL, ipf_p_raudio_in, ipf_p_raudio_out, NULL,
172 NULL, NULL, NULL, NULL },
174 #ifdef IPF_MSNRPC_PROXY
175 { NULL, NULL, "msnrpc", (char)IPPROTO_TCP, 0, 0, 0,
176 ipf_p_msnrpc_init, ipf_p_msnrpc_fini,
179 ipf_p_msnrpc_new, NULL, ipf_p_msnrpc_in, ipf_p_msnrpc_out, NULL,
180 NULL, NULL, NULL, NULL },
182 #ifdef IPF_NETBIOS_PROXY
183 { NULL, NULL, "netbios", (char)IPPROTO_UDP, 0, 0, 0,
184 ipf_p_netbios_main_load, ipf_p_netbios_main_unload,
187 NULL, NULL, NULL, ipf_p_netbios_out, NULL,
188 NULL, NULL, NULL, NULL },
190 #ifdef IPF_IPSEC_PROXY
191 { NULL, NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, 0,
193 ipf_p_ipsec_soft_create, ipf_p_ipsec_soft_destroy,
194 ipf_p_ipsec_soft_init, ipf_p_ipsec_soft_fini,
195 ipf_p_ipsec_new, ipf_p_ipsec_del,
196 ipf_p_ipsec_inout, ipf_p_ipsec_inout, ipf_p_ipsec_match,
197 NULL, NULL, NULL, NULL },
200 { NULL, NULL, "dns", (char)IPPROTO_UDP, 0, 0, 0,
202 ipf_p_dns_soft_create, ipf_p_dns_soft_destroy,
204 ipf_p_dns_new, ipf_p_ipsec_del,
205 ipf_p_dns_inout, ipf_p_dns_inout, ipf_p_dns_match,
206 ipf_p_dns_ctl, NULL, NULL, NULL },
208 #ifdef IPF_PPTP_PROXY
209 { NULL, NULL, "pptp", (char)IPPROTO_TCP, 0, 0, 0,
210 ipf_p_pptp_main_load, ipf_p_pptp_main_unload,
213 ipf_p_pptp_new, ipf_p_pptp_del,
214 ipf_p_pptp_inout, ipf_p_pptp_inout, NULL,
215 NULL, NULL, NULL, NULL },
217 #ifdef IPF_RPCB_PROXY
219 { NULL, NULL, "rpcbt", (char)IPPROTO_TCP, 0, 0, 0,
223 ipf_p_rpcb_new, ipf_p_rpcb_del,
224 ipf_p_rpcb_in, ipf_p_rpcb_out, NULL,
225 NULL, NULL, NULL, NULL },
227 { NULL, NULL, "rpcbu", (char)IPPROTO_UDP, 0, 0, 0,
228 ipf_p_rpcb_main_load, ipf_p_rpcb_main_unload,
231 ipf_p_rpcb_new, ipf_p_rpcb_del,
232 ipf_p_rpcb_in, ipf_p_rpcb_out, NULL,
233 NULL, NULL, NULL, NULL },
235 { NULL, NULL, "", '\0', 0, 0, 0,
241 NULL, NULL, NULL, NULL }
245 /* ------------------------------------------------------------------------ */
246 /* Function: ipf_proxy_main_load */
247 /* Returns: int - 0 == success, else failure. */
248 /* Parameters: Nil */
250 /* Initialise hook for kernel application proxies. */
251 /* Call the initialise routine for all the compiled in kernel proxies. */
252 /* ------------------------------------------------------------------------ */
254 ipf_proxy_main_load()
258 for (ap = ips_proxies; ap->apr_p; ap++) {
259 if (ap->apr_load != NULL)
266 /* ------------------------------------------------------------------------ */
267 /* Function: ipf_proxy_main_unload */
268 /* Returns: int - 0 == success, else failure. */
269 /* Parameters: Nil */
271 /* Unload hook for kernel application proxies. */
272 /* Call the finialise routine for all the compiled in kernel proxies. */
273 /* ------------------------------------------------------------------------ */
275 ipf_proxy_main_unload()
279 for (ap = ips_proxies; ap->apr_p; ap++)
280 if (ap->apr_unload != NULL)
282 for (ap = ap_proxylist; ap; ap = ap->apr_next)
283 if (ap->apr_unload != NULL)
290 /* ------------------------------------------------------------------------ */
291 /* Function: ipf_proxy_soft_create */
292 /* Returns: void * - */
293 /* Parameters: softc(I) - pointer to soft context main structure */
295 /* Build the structure to hold all of the run time data to support proxies. */
296 /* ------------------------------------------------------------------------ */
298 ipf_proxy_soft_create(softc)
299 ipf_main_softc_t *softc;
301 ipf_proxy_softc_t *softp;
306 KMALLOC(softp, ipf_proxy_softc_t *);
310 bzero((char *)softp, sizeof(*softp));
313 softp->ips_proxy_debug = 0;
315 softp->ips_proxy_debug = 2;
317 softp->ips_proxy_session_size = AP_SESS_SIZE;
319 softp->ipf_proxy_tune = ipf_tune_array_copy(softp,
320 sizeof(ipf_proxy_tuneables),
321 ipf_proxy_tuneables);
322 if (softp->ipf_proxy_tune == NULL) {
323 ipf_proxy_soft_destroy(softc, softp);
326 if (ipf_tune_array_link(softc, softp->ipf_proxy_tune) == -1) {
327 ipf_proxy_soft_destroy(softc, softp);
332 for (ap = ips_proxies; ap->apr_p; ap++) {
333 apn = ipf_proxy_create_clone(softc, ap);
337 last->apr_next = apn;
339 softp->ips_proxies = apn;
342 for (ap = ips_proxies; ap != NULL; ap = ap->apr_next) {
343 apn = ipf_proxy_create_clone(softc, ap);
347 last->apr_next = apn;
349 softp->ips_proxies = apn;
355 ipf_proxy_soft_destroy(softc, softp);
360 /* ------------------------------------------------------------------------ */
361 /* Function: ipf_proxy_soft_create */
362 /* Returns: void * - */
363 /* Parameters: softc(I) - pointer to soft context main structure */
364 /* orig(I) - pointer to proxy definition to copy */
366 /* This function clones a proxy definition given by orig and returns a */
367 /* a pointer to that copy. */
368 /* ------------------------------------------------------------------------ */
370 ipf_proxy_create_clone(softc, orig)
371 ipf_main_softc_t *softc;
376 KMALLOC(apn, aproxy_t *);
380 bcopy((char *)orig, (char *)apn, sizeof(*apn));
381 apn->apr_next = NULL;
382 apn->apr_soft = NULL;
384 if (apn->apr_create != NULL) {
385 apn->apr_soft = (*apn->apr_create)(softc);
386 if (apn->apr_soft == NULL) {
392 apn->apr_parent = orig;
399 /* ------------------------------------------------------------------------ */
400 /* Function: ipf_proxy_soft_create */
401 /* Returns: int - 0 == success, else failure. */
402 /* Parameters: softc(I) - pointer to soft context main structure */
403 /* arg(I) - pointer to proxy contect data */
405 /* Initialise the proxy context and walk through each of the proxies and */
406 /* call its initialisation function. This allows for proxies to do any */
407 /* local setup prior to actual use. */
408 /* ------------------------------------------------------------------------ */
410 ipf_proxy_soft_init(softc, arg)
411 ipf_main_softc_t *softc;
414 ipf_proxy_softc_t *softp;
420 size = softp->ips_proxy_session_size * sizeof(ap_session_t *);
422 KMALLOCS(softp->ips_sess_tab, ap_session_t **, size);
424 if (softp->ips_sess_tab == NULL)
427 bzero(softp->ips_sess_tab, size);
429 for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next) {
430 if (ap->apr_init != NULL) {
431 err = (*ap->apr_init)(softc, ap->apr_soft);
436 softp->ips_init_run = 1;
442 /* ------------------------------------------------------------------------ */
443 /* Function: ipf_proxy_soft_create */
444 /* Returns: int - 0 == success, else failure. */
445 /* Parameters: softc(I) - pointer to soft context main structure */
446 /* arg(I) - pointer to proxy contect data */
448 /* This function should always succeed. It is responsible for ensuring that */
449 /* the proxy context can be safely called when ipf_proxy_soft_destroy is */
450 /* called and suring all of the proxies have similarly been instructed. */
451 /* ------------------------------------------------------------------------ */
453 ipf_proxy_soft_fini(softc, arg)
454 ipf_main_softc_t *softc;
457 ipf_proxy_softc_t *softp = arg;
460 for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next) {
461 if (ap->apr_fini != NULL) {
462 (*ap->apr_fini)(softc, ap->apr_soft);
466 if (softp->ips_sess_tab != NULL) {
467 KFREES(softp->ips_sess_tab,
468 softp->ips_proxy_session_size * sizeof(ap_session_t *));
469 softp->ips_sess_tab = NULL;
471 softp->ips_init_run = 0;
477 /* ------------------------------------------------------------------------ */
478 /* Function: ipf_proxy_soft_destroy */
480 /* Parameters: softc(I) - pointer to soft context main structure */
481 /* arg(I) - pointer to proxy contect data */
483 /* Free up all of the local data structures allocated during creation. */
484 /* ------------------------------------------------------------------------ */
486 ipf_proxy_soft_destroy(softc, arg)
487 ipf_main_softc_t *softc;
490 ipf_proxy_softc_t *softp = arg;
493 while ((ap = softp->ips_proxies) != NULL) {
494 softp->ips_proxies = ap->apr_next;
495 if (ap->apr_destroy != NULL)
496 (*ap->apr_destroy)(softc, ap->apr_soft);
497 ap->apr_parent->apr_clones--;
501 if (softp->ipf_proxy_tune != NULL) {
502 ipf_tune_array_unlink(softc, softp->ipf_proxy_tune);
503 KFREES(softp->ipf_proxy_tune, sizeof(ipf_proxy_tuneables));
504 softp->ipf_proxy_tune = NULL;
511 /* ------------------------------------------------------------------------ */
512 /* Function: ipf_proxy_flush */
514 /* Parameters: arg(I) - pointer to proxy contect data */
515 /* how(I) - indicates the type of flush operation */
517 /* Walk through all of the proxies and pass on the flush command as either */
518 /* a flush or a clear. */
519 /* ------------------------------------------------------------------------ */
521 ipf_proxy_flush(arg, how)
525 ipf_proxy_softc_t *softp = arg;
531 for (ap = softp->ips_proxies; ap; ap = ap->apr_next)
532 if (ap->apr_flush != NULL)
533 (*ap->apr_flush)(ap, how);
536 for (ap = softp->ips_proxies; ap; ap = ap->apr_next)
537 if (ap->apr_clear != NULL)
538 (*ap->apr_clear)(ap);
546 /* ------------------------------------------------------------------------ */
547 /* Function: ipf_proxy_add */
548 /* Returns: int - 0 == success, else failure. */
549 /* Parameters: ap(I) - pointer to proxy structure */
551 /* Dynamically add a new kernel proxy. Ensure that it is unique in the */
552 /* collection compiled in and dynamically added. */
553 /* ------------------------------------------------------------------------ */
555 ipf_proxy_add(arg, ap)
559 ipf_proxy_softc_t *softp = arg;
563 for (a = ips_proxies; a->apr_p; a++)
564 if ((a->apr_p == ap->apr_p) &&
565 !strncmp(a->apr_label, ap->apr_label,
566 sizeof(ap->apr_label))) {
567 if (softp->ips_proxy_debug & 0x01)
568 printf("ipf_proxy_add: %s/%d present (B)\n",
569 a->apr_label, a->apr_p);
573 for (a = ap_proxylist; (a != NULL); a = a->apr_next)
574 if ((a->apr_p == ap->apr_p) &&
575 !strncmp(a->apr_label, ap->apr_label,
576 sizeof(ap->apr_label))) {
577 if (softp->ips_proxy_debug & 0x01)
578 printf("ipf_proxy_add: %s/%d present (D)\n",
579 a->apr_label, a->apr_p);
582 ap->apr_next = ap_proxylist;
584 if (ap->apr_load != NULL)
590 /* ------------------------------------------------------------------------ */
591 /* Function: ipf_proxy_ctl */
592 /* Returns: int - 0 == success, else error */
593 /* Parameters: softc(I) - pointer to soft context main structure */
594 /* arg(I) - pointer to proxy context */
595 /* ctl(I) - pointer to proxy control structure */
597 /* Check to see if the proxy this control request has come through for */
598 /* exists, and if it does and it has a control function then invoke that */
599 /* control function. */
600 /* ------------------------------------------------------------------------ */
602 ipf_proxy_ctl(softc, arg, ctl)
603 ipf_main_softc_t *softc;
607 ipf_proxy_softc_t *softp = arg;
611 a = ipf_proxy_lookup(arg, ctl->apc_p, ctl->apc_label);
613 if (softp->ips_proxy_debug & 0x01)
614 printf("ipf_proxy_ctl: can't find %s/%d\n",
615 ctl->apc_label, ctl->apc_p);
618 } else if (a->apr_ctl == NULL) {
619 if (softp->ips_proxy_debug & 0x01)
620 printf("ipf_proxy_ctl: no ctl function for %s/%d\n",
621 ctl->apc_label, ctl->apc_p);
625 error = (*a->apr_ctl)(softc, a->apr_soft, ctl);
626 if ((error != 0) && (softp->ips_proxy_debug & 0x02))
627 printf("ipf_proxy_ctl: %s/%d ctl error %d\n",
628 a->apr_label, a->apr_p, error);
634 /* ------------------------------------------------------------------------ */
635 /* Function: ipf_proxy_del */
636 /* Returns: int - 0 == success, else failure. */
637 /* Parameters: ap(I) - pointer to proxy structure */
639 /* Delete a proxy that has been added dynamically from those available. */
640 /* If it is in use, return 1 (do not destroy NOW), not in use 0 or -1 */
641 /* if it cannot be matched. */
642 /* ------------------------------------------------------------------------ */
649 for (app = &ap_proxylist; ((a = *app) != NULL); app = &a->apr_next) {
651 a->apr_flags |= APR_DELETE;
652 if (ap->apr_ref == 0 && ap->apr_clones == 0) {
664 /* ------------------------------------------------------------------------ */
665 /* Function: ipf_proxy_ok */
666 /* Returns: int - 1 == good match else not. */
667 /* Parameters: fin(I) - pointer to packet information */
668 /* tcp(I) - pointer to TCP/UDP header */
669 /* nat(I) - pointer to current NAT session */
671 /* This function extends the NAT matching to ensure that a packet that has */
672 /* arrived matches the proxy information attached to the NAT rule. Notably, */
673 /* if the proxy is scheduled to be deleted then packets will not match the */
674 /* rule even if the rule is still active. */
675 /* ------------------------------------------------------------------------ */
677 ipf_proxy_ok(fin, tcp, np)
682 aproxy_t *apr = np->in_apr;
683 u_short dport = np->in_odport;
685 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
686 (fin->fin_p != apr->apr_p))
688 if ((tcp == NULL) && dport)
694 /* ------------------------------------------------------------------------ */
695 /* Function: ipf_proxy_ioctl */
696 /* Returns: int - 0 == success, else error */
697 /* Parameters: softc(I) - pointer to soft context main structure */
698 /* data(I) - pointer to ioctl data */
699 /* cmd(I) - ioctl command */
700 /* mode(I) - mode bits for device */
701 /* ctx(I) - pointer to context information */
703 /* ------------------------------------------------------------------------ */
705 ipf_proxy_ioctl(softc, data, cmd, mode, ctx)
706 ipf_main_softc_t *softc;
716 mode = mode; /* LINT */
721 error = ipf_inobj(softc, data, NULL, &ctl, IPFOBJ_PROXYCTL);
727 if (ctl.apc_dsize > 0) {
728 KMALLOCS(ptr, caddr_t, ctl.apc_dsize);
733 error = copyinptr(softc, ctl.apc_data, ptr,
744 error = ipf_proxy_ctl(softc, softc->ipf_proxy_soft,
747 if ((error != 0) && (ptr != NULL)) {
748 KFREES(ptr, ctl.apc_dsize);
760 /* ------------------------------------------------------------------------ */
761 /* Function: ipf_proxy_match */
762 /* Returns: int - 0 == success, else error */
763 /* Parameters: fin(I) - pointer to packet information */
764 /* nat(I) - pointer to current NAT session */
766 /* If a proxy has a match function, call that to do extended packet */
767 /* matching. Whilst other parts of the NAT code are rather lenient when it */
768 /* comes to the quality of the packet that it will transform, the proxy */
769 /* matching is not because they need to work with data, not just headers. */
770 /* ------------------------------------------------------------------------ */
772 ipf_proxy_match(fin, nat)
776 ipf_main_softc_t *softc = fin->fin_main_soft;
777 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
783 if (softp->ips_proxy_debug & 0x04)
784 printf("ipf_proxy_match(%lx,%lx) aps %lx ptr %lx\n",
785 (u_long)fin, (u_long)nat, (u_long)nat->nat_aps,
788 if ((fin->fin_flx & (FI_SHORT|FI_BAD)) != 0) {
789 if (softp->ips_proxy_debug & 0x08)
790 printf("ipf_proxy_match: flx 0x%x (BAD|SHORT)\n",
796 if ((apr == NULL) || (apr->apr_flags & APR_DELETE)) {
797 if (softp->ips_proxy_debug & 0x08)
798 printf("ipf_proxy_match:apr %lx apr_flags 0x%x\n",
799 (u_long)apr, apr ? apr->apr_flags : 0);
803 if (apr->apr_match != NULL) {
804 result = (*apr->apr_match)(fin, nat->nat_aps, nat);
806 if (softp->ips_proxy_debug & 0x08)
807 printf("ipf_proxy_match: result %d\n", result);
815 /* ------------------------------------------------------------------------ */
816 /* Function: ipf_proxy_new */
817 /* Returns: int - 0 == success, else error */
818 /* Parameters: fin(I) - pointer to packet information */
819 /* nat(I) - pointer to current NAT session */
821 /* Allocate a new application proxy structure and fill it in with the */
822 /* relevant details. call the init function once complete, prior to */
824 /* ------------------------------------------------------------------------ */
826 ipf_proxy_new(fin, nat)
830 ipf_main_softc_t *softc = fin->fin_main_soft;
831 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
832 register ap_session_t *aps;
835 if (softp->ips_proxy_debug & 0x04)
836 printf("ipf_proxy_new(%lx,%lx) \n", (u_long)fin, (u_long)nat);
838 if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL)) {
839 if (softp->ips_proxy_debug & 0x08)
840 printf("ipf_proxy_new: nat_ptr %lx nat_aps %lx\n",
841 (u_long)nat->nat_ptr, (u_long)nat->nat_aps);
845 apr = nat->nat_ptr->in_apr;
847 if ((apr->apr_flags & APR_DELETE) ||
848 (fin->fin_p != apr->apr_p)) {
849 if (softp->ips_proxy_debug & 0x08)
850 printf("ipf_proxy_new: apr_flags 0x%x p %d/%d\n",
851 apr->apr_flags, fin->fin_p, apr->apr_p);
855 KMALLOC(aps, ap_session_t *);
857 if (softp->ips_proxy_debug & 0x08)
858 printf("ipf_proxy_new: malloc failed (%lu)\n",
859 (u_long)sizeof(ap_session_t));
863 bzero((char *)aps, sizeof(*aps));
864 aps->aps_data = NULL;
867 if (apr->apr_new != NULL)
868 if ((*apr->apr_new)(apr->apr_soft, fin, aps, nat) == -1) {
869 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
870 KFREES(aps->aps_data, aps->aps_psiz);
873 if (softp->ips_proxy_debug & 0x08)
874 printf("ipf_proxy_new: new(%lx) failed\n",
875 (u_long)apr->apr_new);
879 aps->aps_next = softp->ips_sess_list;
880 softp->ips_sess_list = aps;
887 /* ------------------------------------------------------------------------ */
888 /* Function: ipf_proxy_check */
889 /* Returns: int - -1 == error, 0 == success */
890 /* Parameters: fin(I) - pointer to packet information */
891 /* nat(I) - pointer to current NAT session */
893 /* Check to see if a packet should be passed through an active proxy */
894 /* routine if one has been setup for it. We don't need to check the */
895 /* checksum here if IPFILTER_CKSUM is defined because if it is, a failed */
896 /* check causes FI_BAD to be set. */
897 /* ------------------------------------------------------------------------ */
899 ipf_proxy_check(fin, nat)
903 ipf_main_softc_t *softc = fin->fin_main_soft;
904 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
905 #if SOLARIS && defined(_KERNEL) && defined(ICK_VALID)
908 tcphdr_t *tcp = NULL;
909 udphdr_t *udp = NULL;
917 #if !defined(_KERNEL) || SOLARIS
921 if (fin->fin_flx & FI_BAD) {
922 if (softp->ips_proxy_debug & 0x08)
923 printf("ipf_proxy_check: flx 0x%x (BAD)\n",
928 #ifndef IPFILTER_CKSUM
929 if ((fin->fin_out == 0) && (ipf_checkl4sum(fin) == -1)) {
930 if (softp->ips_proxy_debug & 0x08)
931 printf("ipf_proxy_check: l4 checksum failure %d\n",
933 if (fin->fin_p == IPPROTO_TCP)
934 softc->ipf_stats[fin->fin_out].fr_tcpbad++;
942 * If there is data in this packet to be proxied then try and
943 * get it all into the one buffer, else drop it.
945 #if SOLARIS || defined(HAVE_M_PULLDOWN)
946 if ((fin->fin_dlen > 0) && !(fin->fin_flx & FI_COALESCE))
947 if (ipf_coalesce(fin) == -1) {
948 if (softp->ips_proxy_debug & 0x08)
949 printf("ipf_proxy_check: %s %x\n",
950 "coalesce failed", fin->fin_flx);
955 if (fin->fin_cksum > FI_CK_SUMOK)
963 tcp = (tcphdr_t *)fin->fin_dp;
964 #if SOLARIS && defined(_KERNEL) && defined(ICK_VALID)
966 if (dohwcksum && (m->b_ick_flag == ICK_VALID))
971 udp = (udphdr_t *)fin->fin_dp;
979 if (fin->fin_out != 0) {
980 if (apr->apr_outpkt != NULL)
981 err = (*apr->apr_outpkt)(apr->apr_soft, fin,
984 if (apr->apr_inpkt != NULL)
985 err = (*apr->apr_inpkt)(apr->apr_soft, fin,
990 if (((softp->ips_proxy_debug & 0x08) && (rv != 0)) ||
991 (softp->ips_proxy_debug & 0x04))
992 printf("ipf_proxy_check: out %d err %x rv %d\n",
993 fin->fin_out, err, rv);
998 ipf_proxy_deref(apr);
1004 * If err != 0 then the data size of the packet has changed
1005 * so we need to recalculate the header checksums for the
1008 adjlen = APR_INC(err);
1009 #if !defined(_KERNEL) || SOLARIS
1010 s1 = LONG_SUM(fin->fin_plen - adjlen);
1011 s2 = LONG_SUM(fin->fin_plen);
1012 CALC_SUMD(s1, s2, sd);
1013 if ((err != 0) && (fin->fin_cksum < FI_CK_L4PART) &&
1015 ipf_fix_outcksum(0, &ip->ip_sum, sd, 0);
1017 if (fin->fin_flx & FI_DOCKSUM)
1021 * For TCP packets, we may need to adjust the sequence and
1022 * acknowledgement numbers to reflect changes in size of the
1025 * For both TCP and UDP, recalculate the layer 4 checksum,
1026 * regardless, as we can't tell (here) if data has been
1030 err = ipf_proxy_fixseqack(fin, ip, aps, adjlen);
1031 if (fin->fin_cksum == FI_CK_L4PART) {
1032 u_short sum = ntohs(tcp->th_sum);
1034 tcp->th_sum = htons(sum);
1035 } else if (fin->fin_cksum < FI_CK_L4PART) {
1036 tcp->th_sum = fr_cksum(fin, ip,
1039 } else if ((udp != NULL) && (udp->uh_sum != 0)) {
1040 if (fin->fin_cksum == FI_CK_L4PART) {
1041 u_short sum = ntohs(udp->uh_sum);
1043 udp->uh_sum = htons(sum);
1045 udp->uh_sum = fr_cksum(fin, ip,
1049 aps->aps_bytes += fin->fin_plen;
1057 /* ------------------------------------------------------------------------ */
1058 /* Function: ipf_proxy_lookup */
1059 /* Returns: int - -1 == error, 0 == success */
1060 /* Parameters: arg(I) - pointer to proxy context information */
1061 /* pr(I) - protocol number for proxy */
1062 /* name(I) - proxy name */
1064 /* Search for an proxy by the protocol it is being used with and its name. */
1065 /* ------------------------------------------------------------------------ */
1067 ipf_proxy_lookup(arg, pr, name)
1072 ipf_proxy_softc_t *softp = arg;
1075 if (softp->ips_proxy_debug & 0x04)
1076 printf("ipf_proxy_lookup(%d,%s)\n", pr, name);
1078 for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next)
1079 if ((ap->apr_p == pr) &&
1080 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
1085 if (softp->ips_proxy_debug & 0x08)
1086 printf("ipf_proxy_lookup: failed for %d/%s\n", pr, name);
1091 /* ------------------------------------------------------------------------ */
1092 /* Function: ipf_proxy_deref */
1094 /* Parameters: ap(I) - pointer to proxy structure */
1096 /* Drop the reference counter associated with the proxy. */
1097 /* ------------------------------------------------------------------------ */
1106 /* ------------------------------------------------------------------------ */
1107 /* Function: ipf_proxy_free */
1109 /* Parameters: softc(I) - pointer to soft context main structure */
1110 /* aps(I) - pointer to current proxy session */
1111 /* Locks Held: ipf_nat_new, ipf_nat(W) */
1113 /* Free up proxy session information allocated to be used with a NAT */
1115 /* ------------------------------------------------------------------------ */
1117 ipf_proxy_free(softc, aps)
1118 ipf_main_softc_t *softc;
1121 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
1122 ap_session_t *a, **ap;
1128 for (ap = &softp->ips_sess_list; ((a = *ap) != NULL); ap = &a->aps_next)
1135 if ((apr != NULL) && (apr->apr_del != NULL))
1136 (*apr->apr_del)(softc, aps);
1138 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
1139 KFREES(aps->aps_data, aps->aps_psiz);
1144 /* ------------------------------------------------------------------------ */
1145 /* Function: ipf_proxy_fixseqack */
1146 /* Returns: int - 2 if TCP ack/seq is changed, else 0 */
1147 /* Parameters: fin(I) - pointer to packet information */
1148 /* ip(I) - pointer to IP header */
1149 /* nat(I) - pointer to current NAT session */
1150 /* inc(I) - delta to apply to TCP sequence numbering */
1152 /* Adjust the TCP sequence/acknowledge numbers in the TCP header based on */
1153 /* whether or not the new header is past the point at which an adjustment */
1154 /* occurred. This might happen because of (say) an FTP string being changed */
1155 /* and the new string being a different length to the old. */
1156 /* ------------------------------------------------------------------------ */
1158 ipf_proxy_fixseqack(fin, ip, aps, inc)
1164 ipf_main_softc_t *softc = fin->fin_main_soft;
1165 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
1166 int sel, ch = 0, out, nlen;
1171 tcp = (tcphdr_t *)fin->fin_dp;
1174 * ip_len has already been adjusted by 'inc'.
1176 nlen = fin->fin_dlen;
1177 nlen -= (TCP_OFF(tcp) << 2);
1183 seq1 = (u_32_t)ntohl(tcp->th_seq);
1184 sel = aps->aps_sel[out];
1186 /* switch to other set ? */
1187 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
1188 (seq1 > aps->aps_seqmin[!sel])) {
1189 if (softp->ips_proxy_debug & 0x10)
1190 printf("proxy out switch set seq %d -> %d %x > %x\n",
1192 aps->aps_seqmin[!sel]);
1193 sel = aps->aps_sel[out] = !sel;
1196 if (aps->aps_seqoff[sel]) {
1197 seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
1199 seq2 = aps->aps_seqoff[sel];
1201 tcp->th_seq = htonl(seq1);
1206 if (inc && (seq1 > aps->aps_seqmin[!sel])) {
1207 aps->aps_seqmin[sel] = seq1 + nlen - 1;
1208 aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
1209 if (softp->ips_proxy_debug & 0x10)
1210 printf("proxy seq set %d at %x to %d + %d\n",
1211 sel, aps->aps_seqmin[sel],
1212 aps->aps_seqoff[sel], inc);
1217 seq1 = ntohl(tcp->th_ack);
1218 sel = aps->aps_sel[1 - out];
1220 /* switch to other set ? */
1221 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
1222 (seq1 > aps->aps_ackmin[!sel])) {
1223 if (softp->ips_proxy_debug & 0x10)
1224 printf("proxy out switch set ack %d -> %d %x > %x\n",
1226 aps->aps_ackmin[!sel]);
1227 sel = aps->aps_sel[1 - out] = !sel;
1230 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
1231 seq2 = aps->aps_ackoff[sel];
1232 tcp->th_ack = htonl(seq1 - seq2);
1236 seq1 = ntohl(tcp->th_seq);
1237 sel = aps->aps_sel[out];
1239 /* switch to other set ? */
1240 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
1241 (seq1 > aps->aps_ackmin[!sel])) {
1242 if (softp->ips_proxy_debug & 0x10)
1243 printf("proxy in switch set ack %d -> %d %x > %x\n",
1244 sel, !sel, seq1, aps->aps_ackmin[!sel]);
1245 sel = aps->aps_sel[out] = !sel;
1248 if (aps->aps_ackoff[sel]) {
1249 seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
1251 seq2 = aps->aps_ackoff[sel];
1253 tcp->th_seq = htonl(seq1);
1258 if (inc && (seq1 > aps->aps_ackmin[!sel])) {
1259 aps->aps_ackmin[!sel] = seq1 + nlen - 1;
1260 aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
1262 if (softp->ips_proxy_debug & 0x10)
1263 printf("proxy ack set %d at %x to %d + %d\n",
1264 !sel, aps->aps_seqmin[!sel],
1265 aps->aps_seqoff[sel], inc);
1270 seq1 = ntohl(tcp->th_ack);
1271 sel = aps->aps_sel[1 - out];
1273 /* switch to other set ? */
1274 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
1275 (seq1 > aps->aps_seqmin[!sel])) {
1276 if (softp->ips_proxy_debug & 0x10)
1277 printf("proxy in switch set seq %d -> %d %x > %x\n",
1278 sel, !sel, seq1, aps->aps_seqmin[!sel]);
1279 sel = aps->aps_sel[1 - out] = !sel;
1282 if (aps->aps_seqoff[sel] != 0) {
1283 if (softp->ips_proxy_debug & 0x10)
1284 printf("sel %d seqoff %d seq1 %x seqmin %x\n",
1285 sel, aps->aps_seqoff[sel], seq1,
1286 aps->aps_seqmin[sel]);
1287 if (seq1 > aps->aps_seqmin[sel]) {
1288 seq2 = aps->aps_seqoff[sel];
1289 tcp->th_ack = htonl(seq1 - seq2);
1295 if (softp->ips_proxy_debug & 0x10)
1296 printf("ipf_proxy_fixseqack: seq %u ack %u\n",
1297 (u_32_t)ntohl(tcp->th_seq), (u_32_t)ntohl(tcp->th_ack));
1302 /* ------------------------------------------------------------------------ */
1303 /* Function: ipf_proxy_rule_rev */
1304 /* Returns: ipnat_t * - NULL = failure, else pointer to new rule */
1305 /* Parameters: nat(I) - pointer to NAT session to create rule from */
1307 /* This function creates a NAT rule that is based upon the reverse packet */
1308 /* flow associated with this NAT session. Thus if this NAT session was */
1309 /* created with a map rule then this function will create a rdr rule. */
1310 /* Only address fields and network interfaces are assigned in this function */
1311 /* and the address fields are formed such that an exact is required. If the */
1312 /* original rule had a netmask, that is not replicated here not is it */
1313 /* desired. The ultimate goal here is to create a NAT rule to support a NAT */
1314 /* session being created that does not have a user configured rule. The */
1315 /* classic example is supporting the FTP proxy, where a data channel needs */
1316 /* to be setup, based on the addresses used for the control connection. In */
1317 /* that case, this function is used to handle creating NAT rules to support */
1318 /* data connections with the PORT and EPRT commands. */
1319 /* ------------------------------------------------------------------------ */
1321 ipf_proxy_rule_rev(nat)
1329 size = old->in_size;
1331 KMALLOCS(ipn, ipnat_t *, size);
1335 bzero((char *)ipn, size);
1341 ipn->in_size = size;
1342 ipn->in_pr[0] = old->in_pr[1];
1343 ipn->in_pr[1] = old->in_pr[0];
1344 ipn->in_v[0] = old->in_v[1];
1345 ipn->in_v[1] = old->in_v[0];
1346 ipn->in_ifps[0] = old->in_ifps[1];
1347 ipn->in_ifps[1] = old->in_ifps[0];
1348 ipn->in_flags = (old->in_flags | IPN_PROXYRULE);
1350 ipn->in_nsrcip6 = nat->nat_odst6;
1351 ipn->in_osrcip6 = nat->nat_ndst6;
1353 if ((old->in_redir & NAT_REDIRECT) != 0) {
1354 ipn->in_redir = NAT_MAP;
1355 if (ipn->in_v[0] == 4) {
1356 ipn->in_snip = ntohl(nat->nat_odstaddr);
1357 ipn->in_dnip = ntohl(nat->nat_nsrcaddr);
1360 ipn->in_snip6 = nat->nat_odst6;
1361 ipn->in_dnip6 = nat->nat_nsrc6;
1364 ipn->in_ndstip6 = nat->nat_nsrc6;
1365 ipn->in_odstip6 = nat->nat_osrc6;
1367 ipn->in_redir = NAT_REDIRECT;
1368 if (ipn->in_v[0] == 4) {
1369 ipn->in_snip = ntohl(nat->nat_odstaddr);
1370 ipn->in_dnip = ntohl(nat->nat_osrcaddr);
1373 ipn->in_snip6 = nat->nat_odst6;
1374 ipn->in_dnip6 = nat->nat_osrc6;
1377 ipn->in_ndstip6 = nat->nat_osrc6;
1378 ipn->in_odstip6 = nat->nat_nsrc6;
1381 IP6_SETONES(&ipn->in_osrcmsk6);
1382 IP6_SETONES(&ipn->in_nsrcmsk6);
1383 IP6_SETONES(&ipn->in_odstmsk6);
1384 IP6_SETONES(&ipn->in_ndstmsk6);
1386 ipn->in_namelen = old->in_namelen;
1387 ipn->in_ifnames[0] = old->in_ifnames[1];
1388 ipn->in_ifnames[1] = old->in_ifnames[0];
1389 bcopy(old->in_names, ipn->in_names, ipn->in_namelen);
1390 MUTEX_INIT(&ipn->in_lock, "ipnat rev rule lock");
1396 /* ------------------------------------------------------------------------ */
1397 /* Function: ipf_proxy_rule_fwd */
1398 /* Returns: ipnat_t * - NULL = failure, else pointer to new rule */
1399 /* Parameters: nat(I) - pointer to NAT session to create rule from */
1401 /* The purpose and rationale of this function is much the same as the above */
1402 /* function, ipf_proxy_rule_rev, except that a rule is created that matches */
1403 /* the same direction as that of the existing NAT session. Thus if this NAT */
1404 /* session was created with a map rule then this function will also create */
1405 /* a data structure to represent a map rule. Whereas ipf_proxy_rule_rev is */
1406 /* used to support PORT/EPRT, this function supports PASV/EPSV. */
1407 /* ------------------------------------------------------------------------ */
1409 ipf_proxy_rule_fwd(nat)
1417 size = old->in_size;
1419 KMALLOCS(ipn, ipnat_t *, size);
1423 bzero((char *)ipn, size);
1429 ipn->in_size = size;
1430 ipn->in_pr[0] = old->in_pr[0];
1431 ipn->in_pr[1] = old->in_pr[1];
1432 ipn->in_v[0] = old->in_v[0];
1433 ipn->in_v[1] = old->in_v[1];
1434 ipn->in_ifps[0] = nat->nat_ifps[0];
1435 ipn->in_ifps[1] = nat->nat_ifps[1];
1436 ipn->in_flags = (old->in_flags | IPN_PROXYRULE);
1438 ipn->in_nsrcip6 = nat->nat_nsrc6;
1439 ipn->in_osrcip6 = nat->nat_osrc6;
1440 ipn->in_ndstip6 = nat->nat_ndst6;
1441 ipn->in_odstip6 = nat->nat_odst6;
1442 ipn->in_redir = old->in_redir;
1444 if (ipn->in_v[0] == 4) {
1445 ipn->in_snip = ntohl(nat->nat_nsrcaddr);
1446 ipn->in_dnip = ntohl(nat->nat_ndstaddr);
1449 ipn->in_snip6 = nat->nat_nsrc6;
1450 ipn->in_dnip6 = nat->nat_ndst6;
1454 IP6_SETONES(&ipn->in_osrcmsk6);
1455 IP6_SETONES(&ipn->in_nsrcmsk6);
1456 IP6_SETONES(&ipn->in_odstmsk6);
1457 IP6_SETONES(&ipn->in_ndstmsk6);
1459 ipn->in_namelen = old->in_namelen;
1460 ipn->in_ifnames[0] = old->in_ifnames[0];
1461 ipn->in_ifnames[1] = old->in_ifnames[1];
1462 bcopy(old->in_names, ipn->in_names, ipn->in_namelen);
1463 MUTEX_INIT(&ipn->in_lock, "ipnat fwd rule lock");