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>
20 # include <sys/fcntl.h>
22 #if !defined(_KERNEL) && !defined(__KERNEL__)
35 # include <sys/protosw.h>
37 #include <sys/socket.h>
39 # if !defined(__NetBSD__) && !defined(sun) && !defined(__osf__) && \
40 !defined(__OpenBSD__) && !defined(__hpux) && !defined(__sgi) && \
42 # include <sys/ctype.h>
44 # include <sys/systm.h>
45 # if !defined(__SVR4) && !defined(__svr4__)
46 # include <sys/mbuf.h>
49 #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
50 # include <sys/filio.h>
51 # include <sys/fcntl.h>
53 # include <sys/ioctl.h>
55 #if defined(__SVR4) || defined(__svr4__)
56 # include <sys/byteorder.h>
58 # include <sys/dditypes.h>
60 # include <sys/stream.h>
61 # include <sys/kmem.h>
63 #if __FreeBSD_version >= 300000
64 # include <sys/queue.h>
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
74 # include <netinet/ip_var.h>
76 #include <netinet/tcp.h>
77 #include <netinet/udp.h>
78 #include <netinet/ip_icmp.h>
79 #include "netinet/ip_compat.h"
80 #include <netinet/tcpip.h>
81 #include "netinet/ip_fil.h"
82 #include "netinet/ip_nat.h"
83 #include "netinet/ip_state.h"
84 #include "netinet/ip_proxy.h"
85 #if (__FreeBSD_version >= 300000)
86 # include <sys/malloc.h>
91 #include "netinet/ip_ftp_pxy.c"
92 #include "netinet/ip_tftp_pxy.c"
93 #include "netinet/ip_rcmd_pxy.c"
94 #include "netinet/ip_pptp_pxy.c"
96 # include "netinet/ip_irc_pxy.c"
97 # include "netinet/ip_raudio_pxy.c"
98 # include "netinet/ip_netbios_pxy.c"
100 #include "netinet/ip_ipsec_pxy.c"
101 #include "netinet/ip_rpcb_pxy.c"
104 static const char rcsid[] = "@(#)$Id$";
107 #define AP_SESS_SIZE 53
109 static int ipf_proxy_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int ));
110 static aproxy_t *ipf_proxy_create_clone __P((ipf_main_softc_t *, aproxy_t *));
112 typedef struct ipf_proxy_softc_s {
114 int ips_proxy_session_size;
115 ap_session_t **ips_sess_tab;
116 ap_session_t *ips_sess_list;
117 aproxy_t *ips_proxies;
119 ipftuneable_t *ipf_proxy_tune;
122 static ipftuneable_t ipf_proxy_tuneables[] = {
123 { { (void *)offsetof(ipf_proxy_softc_t, ips_proxy_debug) },
124 "proxy_debug", 0, 0x1f,
125 stsizeof(ipf_proxy_softc_t, ips_proxy_debug),
127 { { NULL }, NULL, 0, 0,
132 static aproxy_t *ap_proxylist = NULL;
133 static aproxy_t ips_proxies[] = {
135 { NULL, NULL, "ftp", (char)IPPROTO_TCP, 0, 0, 0,
136 ipf_p_ftp_main_load, ipf_p_ftp_main_unload,
137 ipf_p_ftp_soft_create, ipf_p_ftp_soft_destroy,
139 ipf_p_ftp_new, ipf_p_ftp_del, ipf_p_ftp_in, ipf_p_ftp_out, NULL,
140 NULL, NULL, NULL, NULL },
142 #ifdef IPF_TFTP_PROXY
143 { NULL, NULL, "tftp", (char)IPPROTO_UDP, 0, 0, 0,
144 ipf_p_tftp_main_load, ipf_p_tftp_main_unload,
145 ipf_p_tftp_soft_create, ipf_p_tftp_soft_destroy,
147 ipf_p_tftp_new, ipf_p_tftp_del,
148 ipf_p_tftp_in, ipf_p_tftp_out, NULL,
149 NULL, NULL, NULL, NULL },
152 { NULL, NULL, "irc", (char)IPPROTO_TCP, 0, 0, 0,
153 ipf_p_irc_main_load, ipf_p_irc_main_unload,
156 ipf_p_irc_new, NULL, NULL, ipf_p_irc_out, NULL,
157 NULL, NULL, NULL, NULL },
159 #ifdef IPF_RCMD_PROXY
160 { NULL, NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, 0,
161 ipf_p_rcmd_main_load, ipf_p_rcmd_main_unload,
164 ipf_p_rcmd_new, ipf_p_rcmd_del,
165 ipf_p_rcmd_in, ipf_p_rcmd_out, NULL,
166 NULL, NULL, NULL, NULL },
168 #ifdef IPF_RAUDIO_PROXY
169 { NULL, NULL, "raudio", (char)IPPROTO_TCP, 0, 0, 0,
170 ipf_p_raudio_main_load, ipf_p_raudio_main_unload,
173 ipf_p_raudio_new, NULL, ipf_p_raudio_in, ipf_p_raudio_out, NULL,
174 NULL, NULL, NULL, NULL },
176 #ifdef IPF_MSNRPC_PROXY
177 { NULL, NULL, "msnrpc", (char)IPPROTO_TCP, 0, 0, 0,
178 ipf_p_msnrpc_init, ipf_p_msnrpc_fini,
181 ipf_p_msnrpc_new, NULL, ipf_p_msnrpc_in, ipf_p_msnrpc_out, NULL,
182 NULL, NULL, NULL, NULL },
184 #ifdef IPF_NETBIOS_PROXY
185 { NULL, NULL, "netbios", (char)IPPROTO_UDP, 0, 0, 0,
186 ipf_p_netbios_main_load, ipf_p_netbios_main_unload,
189 NULL, NULL, NULL, ipf_p_netbios_out, NULL,
190 NULL, NULL, NULL, NULL },
192 #ifdef IPF_IPSEC_PROXY
193 { NULL, NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, 0,
195 ipf_p_ipsec_soft_create, ipf_p_ipsec_soft_destroy,
196 ipf_p_ipsec_soft_init, ipf_p_ipsec_soft_fini,
197 ipf_p_ipsec_new, ipf_p_ipsec_del,
198 ipf_p_ipsec_inout, ipf_p_ipsec_inout, ipf_p_ipsec_match,
199 NULL, NULL, NULL, NULL },
202 { NULL, NULL, "dns", (char)IPPROTO_UDP, 0, 0, 0,
204 ipf_p_dns_soft_create, ipf_p_dns_soft_destroy,
206 ipf_p_dns_new, ipf_p_ipsec_del,
207 ipf_p_dns_inout, ipf_p_dns_inout, ipf_p_dns_match,
208 ipf_p_dns_ctl, NULL, NULL, NULL },
210 #ifdef IPF_PPTP_PROXY
211 { NULL, NULL, "pptp", (char)IPPROTO_TCP, 0, 0, 0,
212 ipf_p_pptp_main_load, ipf_p_pptp_main_unload,
215 ipf_p_pptp_new, ipf_p_pptp_del,
216 ipf_p_pptp_inout, ipf_p_pptp_inout, NULL,
217 NULL, NULL, NULL, NULL },
219 #ifdef IPF_RPCB_PROXY
221 { NULL, NULL, "rpcbt", (char)IPPROTO_TCP, 0, 0, 0,
225 ipf_p_rpcb_new, ipf_p_rpcb_del,
226 ipf_p_rpcb_in, ipf_p_rpcb_out, NULL,
227 NULL, NULL, NULL, NULL },
229 { NULL, NULL, "rpcbu", (char)IPPROTO_UDP, 0, 0, 0,
230 ipf_p_rpcb_main_load, ipf_p_rpcb_main_unload,
233 ipf_p_rpcb_new, ipf_p_rpcb_del,
234 ipf_p_rpcb_in, ipf_p_rpcb_out, NULL,
235 NULL, NULL, NULL, NULL },
237 { NULL, NULL, "", '\0', 0, 0, 0,
243 NULL, NULL, NULL, NULL }
247 /* ------------------------------------------------------------------------ */
248 /* Function: ipf_proxy_main_load */
249 /* Returns: int - 0 == success, else failure. */
250 /* Parameters: Nil */
252 /* Initialise hook for kernel application proxies. */
253 /* Call the initialise routine for all the compiled in kernel proxies. */
254 /* ------------------------------------------------------------------------ */
256 ipf_proxy_main_load()
260 for (ap = ips_proxies; ap->apr_p; ap++) {
261 if (ap->apr_load != NULL)
268 /* ------------------------------------------------------------------------ */
269 /* Function: ipf_proxy_main_unload */
270 /* Returns: int - 0 == success, else failure. */
271 /* Parameters: Nil */
273 /* Unload hook for kernel application proxies. */
274 /* Call the finialise routine for all the compiled in kernel proxies. */
275 /* ------------------------------------------------------------------------ */
277 ipf_proxy_main_unload()
281 for (ap = ips_proxies; ap->apr_p; ap++)
282 if (ap->apr_unload != NULL)
284 for (ap = ap_proxylist; ap; ap = ap->apr_next)
285 if (ap->apr_unload != NULL)
292 /* ------------------------------------------------------------------------ */
293 /* Function: ipf_proxy_soft_create */
294 /* Returns: void * - */
295 /* Parameters: softc(I) - pointer to soft context main structure */
297 /* Build the structure to hold all of the run time data to support proxies. */
298 /* ------------------------------------------------------------------------ */
300 ipf_proxy_soft_create(softc)
301 ipf_main_softc_t *softc;
303 ipf_proxy_softc_t *softp;
308 KMALLOC(softp, ipf_proxy_softc_t *);
312 bzero((char *)softp, sizeof(*softp));
315 softp->ips_proxy_debug = 0;
317 softp->ips_proxy_debug = 2;
319 softp->ips_proxy_session_size = AP_SESS_SIZE;
321 softp->ipf_proxy_tune = ipf_tune_array_copy(softp,
322 sizeof(ipf_proxy_tuneables),
323 ipf_proxy_tuneables);
324 if (softp->ipf_proxy_tune == NULL) {
325 ipf_proxy_soft_destroy(softc, softp);
328 if (ipf_tune_array_link(softc, softp->ipf_proxy_tune) == -1) {
329 ipf_proxy_soft_destroy(softc, softp);
334 for (ap = ips_proxies; ap->apr_p; ap++) {
335 apn = ipf_proxy_create_clone(softc, ap);
339 last->apr_next = apn;
341 softp->ips_proxies = apn;
344 for (ap = ips_proxies; ap != NULL; ap = ap->apr_next) {
345 apn = ipf_proxy_create_clone(softc, ap);
349 last->apr_next = apn;
351 softp->ips_proxies = apn;
357 ipf_proxy_soft_destroy(softc, softp);
362 /* ------------------------------------------------------------------------ */
363 /* Function: ipf_proxy_soft_create */
364 /* Returns: void * - */
365 /* Parameters: softc(I) - pointer to soft context main structure */
366 /* orig(I) - pointer to proxy definition to copy */
368 /* This function clones a proxy definition given by orig and returns a */
369 /* a pointer to that copy. */
370 /* ------------------------------------------------------------------------ */
372 ipf_proxy_create_clone(softc, orig)
373 ipf_main_softc_t *softc;
378 KMALLOC(apn, aproxy_t *);
382 bcopy((char *)orig, (char *)apn, sizeof(*apn));
383 apn->apr_next = NULL;
384 apn->apr_soft = NULL;
386 if (apn->apr_create != NULL) {
387 apn->apr_soft = (*apn->apr_create)(softc);
388 if (apn->apr_soft == NULL) {
394 apn->apr_parent = orig;
401 /* ------------------------------------------------------------------------ */
402 /* Function: ipf_proxy_soft_create */
403 /* Returns: int - 0 == success, else failure. */
404 /* Parameters: softc(I) - pointer to soft context main structure */
405 /* arg(I) - pointer to proxy contect data */
407 /* Initialise the proxy context and walk through each of the proxies and */
408 /* call its initialisation function. This allows for proxies to do any */
409 /* local setup prior to actual use. */
410 /* ------------------------------------------------------------------------ */
412 ipf_proxy_soft_init(softc, arg)
413 ipf_main_softc_t *softc;
416 ipf_proxy_softc_t *softp;
422 size = softp->ips_proxy_session_size * sizeof(ap_session_t *);
424 KMALLOCS(softp->ips_sess_tab, ap_session_t **, size);
426 if (softp->ips_sess_tab == NULL)
429 bzero(softp->ips_sess_tab, size);
431 for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next) {
432 if (ap->apr_init != NULL) {
433 err = (*ap->apr_init)(softc, ap->apr_soft);
438 softp->ips_init_run = 1;
444 /* ------------------------------------------------------------------------ */
445 /* Function: ipf_proxy_soft_create */
446 /* Returns: int - 0 == success, else failure. */
447 /* Parameters: softc(I) - pointer to soft context main structure */
448 /* arg(I) - pointer to proxy contect data */
450 /* This function should always succeed. It is responsible for ensuring that */
451 /* the proxy context can be safely called when ipf_proxy_soft_destroy is */
452 /* called and suring all of the proxies have similarly been instructed. */
453 /* ------------------------------------------------------------------------ */
455 ipf_proxy_soft_fini(softc, arg)
456 ipf_main_softc_t *softc;
459 ipf_proxy_softc_t *softp = arg;
462 for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next) {
463 if (ap->apr_fini != NULL) {
464 (*ap->apr_fini)(softc, ap->apr_soft);
468 if (softp->ips_sess_tab != NULL) {
469 KFREES(softp->ips_sess_tab,
470 softp->ips_proxy_session_size * sizeof(ap_session_t *));
471 softp->ips_sess_tab = NULL;
473 softp->ips_init_run = 0;
479 /* ------------------------------------------------------------------------ */
480 /* Function: ipf_proxy_soft_destroy */
482 /* Parameters: softc(I) - pointer to soft context main structure */
483 /* arg(I) - pointer to proxy contect data */
485 /* Free up all of the local data structures allocated during creation. */
486 /* ------------------------------------------------------------------------ */
488 ipf_proxy_soft_destroy(softc, arg)
489 ipf_main_softc_t *softc;
492 ipf_proxy_softc_t *softp = arg;
495 while ((ap = softp->ips_proxies) != NULL) {
496 softp->ips_proxies = ap->apr_next;
497 if (ap->apr_destroy != NULL)
498 (*ap->apr_destroy)(softc, ap->apr_soft);
499 ap->apr_parent->apr_clones--;
503 if (softp->ipf_proxy_tune != NULL) {
504 ipf_tune_array_unlink(softc, softp->ipf_proxy_tune);
505 KFREES(softp->ipf_proxy_tune, sizeof(ipf_proxy_tuneables));
506 softp->ipf_proxy_tune = NULL;
513 /* ------------------------------------------------------------------------ */
514 /* Function: ipf_proxy_flush */
516 /* Parameters: arg(I) - pointer to proxy contect data */
517 /* how(I) - indicates the type of flush operation */
519 /* Walk through all of the proxies and pass on the flush command as either */
520 /* a flush or a clear. */
521 /* ------------------------------------------------------------------------ */
523 ipf_proxy_flush(arg, how)
527 ipf_proxy_softc_t *softp = arg;
533 for (ap = softp->ips_proxies; ap; ap = ap->apr_next)
534 if (ap->apr_flush != NULL)
535 (*ap->apr_flush)(ap, how);
538 for (ap = softp->ips_proxies; ap; ap = ap->apr_next)
539 if (ap->apr_clear != NULL)
540 (*ap->apr_clear)(ap);
548 /* ------------------------------------------------------------------------ */
549 /* Function: ipf_proxy_add */
550 /* Returns: int - 0 == success, else failure. */
551 /* Parameters: ap(I) - pointer to proxy structure */
553 /* Dynamically add a new kernel proxy. Ensure that it is unique in the */
554 /* collection compiled in and dynamically added. */
555 /* ------------------------------------------------------------------------ */
557 ipf_proxy_add(arg, ap)
561 ipf_proxy_softc_t *softp = arg;
565 for (a = ips_proxies; a->apr_p; a++)
566 if ((a->apr_p == ap->apr_p) &&
567 !strncmp(a->apr_label, ap->apr_label,
568 sizeof(ap->apr_label))) {
569 if (softp->ips_proxy_debug & 0x01)
570 printf("ipf_proxy_add: %s/%d present (B)\n",
571 a->apr_label, a->apr_p);
575 for (a = ap_proxylist; (a != NULL); a = a->apr_next)
576 if ((a->apr_p == ap->apr_p) &&
577 !strncmp(a->apr_label, ap->apr_label,
578 sizeof(ap->apr_label))) {
579 if (softp->ips_proxy_debug & 0x01)
580 printf("ipf_proxy_add: %s/%d present (D)\n",
581 a->apr_label, a->apr_p);
584 ap->apr_next = ap_proxylist;
586 if (ap->apr_load != NULL)
592 /* ------------------------------------------------------------------------ */
593 /* Function: ipf_proxy_ctl */
594 /* Returns: int - 0 == success, else error */
595 /* Parameters: softc(I) - pointer to soft context main structure */
596 /* arg(I) - pointer to proxy context */
597 /* ctl(I) - pointer to proxy control structure */
599 /* Check to see if the proxy this control request has come through for */
600 /* exists, and if it does and it has a control function then invoke that */
601 /* control function. */
602 /* ------------------------------------------------------------------------ */
604 ipf_proxy_ctl(softc, arg, ctl)
605 ipf_main_softc_t *softc;
609 ipf_proxy_softc_t *softp = arg;
613 a = ipf_proxy_lookup(arg, ctl->apc_p, ctl->apc_label);
615 if (softp->ips_proxy_debug & 0x01)
616 printf("ipf_proxy_ctl: can't find %s/%d\n",
617 ctl->apc_label, ctl->apc_p);
620 } else if (a->apr_ctl == NULL) {
621 if (softp->ips_proxy_debug & 0x01)
622 printf("ipf_proxy_ctl: no ctl function for %s/%d\n",
623 ctl->apc_label, ctl->apc_p);
627 error = (*a->apr_ctl)(softc, a->apr_soft, ctl);
628 if ((error != 0) && (softp->ips_proxy_debug & 0x02))
629 printf("ipf_proxy_ctl: %s/%d ctl error %d\n",
630 a->apr_label, a->apr_p, error);
636 /* ------------------------------------------------------------------------ */
637 /* Function: ipf_proxy_del */
638 /* Returns: int - 0 == success, else failure. */
639 /* Parameters: ap(I) - pointer to proxy structure */
641 /* Delete a proxy that has been added dynamically from those available. */
642 /* If it is in use, return 1 (do not destroy NOW), not in use 0 or -1 */
643 /* if it cannot be matched. */
644 /* ------------------------------------------------------------------------ */
651 for (app = &ap_proxylist; ((a = *app) != NULL); app = &a->apr_next) {
653 a->apr_flags |= APR_DELETE;
654 if (ap->apr_ref == 0 && ap->apr_clones == 0) {
666 /* ------------------------------------------------------------------------ */
667 /* Function: ipf_proxy_ok */
668 /* Returns: int - 1 == good match else not. */
669 /* Parameters: fin(I) - pointer to packet information */
670 /* tcp(I) - pointer to TCP/UDP header */
671 /* nat(I) - pointer to current NAT session */
673 /* This function extends the NAT matching to ensure that a packet that has */
674 /* arrived matches the proxy information attached to the NAT rule. Notably, */
675 /* if the proxy is scheduled to be deleted then packets will not match the */
676 /* rule even if the rule is still active. */
677 /* ------------------------------------------------------------------------ */
679 ipf_proxy_ok(fin, tcp, np)
684 aproxy_t *apr = np->in_apr;
685 u_short dport = np->in_odport;
687 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
688 (fin->fin_p != apr->apr_p))
690 if ((tcp == NULL) && dport)
696 /* ------------------------------------------------------------------------ */
697 /* Function: ipf_proxy_ioctl */
698 /* Returns: int - 0 == success, else error */
699 /* Parameters: softc(I) - pointer to soft context main structure */
700 /* data(I) - pointer to ioctl data */
701 /* cmd(I) - ioctl command */
702 /* mode(I) - mode bits for device */
703 /* ctx(I) - pointer to context information */
705 /* ------------------------------------------------------------------------ */
707 ipf_proxy_ioctl(softc, data, cmd, mode, ctx)
708 ipf_main_softc_t *softc;
718 mode = mode; /* LINT */
723 error = ipf_inobj(softc, data, NULL, &ctl, IPFOBJ_PROXYCTL);
729 if (ctl.apc_dsize > 0) {
730 KMALLOCS(ptr, caddr_t, ctl.apc_dsize);
735 error = copyinptr(softc, ctl.apc_data, ptr,
746 error = ipf_proxy_ctl(softc, softc->ipf_proxy_soft,
749 if ((error != 0) && (ptr != NULL)) {
750 KFREES(ptr, ctl.apc_dsize);
762 /* ------------------------------------------------------------------------ */
763 /* Function: ipf_proxy_match */
764 /* Returns: int - 0 == success, else error */
765 /* Parameters: fin(I) - pointer to packet information */
766 /* nat(I) - pointer to current NAT session */
768 /* If a proxy has a match function, call that to do extended packet */
769 /* matching. Whilst other parts of the NAT code are rather lenient when it */
770 /* comes to the quality of the packet that it will transform, the proxy */
771 /* matching is not because they need to work with data, not just headers. */
772 /* ------------------------------------------------------------------------ */
774 ipf_proxy_match(fin, nat)
778 ipf_main_softc_t *softc = fin->fin_main_soft;
779 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
785 if (softp->ips_proxy_debug & 0x04)
786 printf("ipf_proxy_match(%lx,%lx) aps %lx ptr %lx\n",
787 (u_long)fin, (u_long)nat, (u_long)nat->nat_aps,
790 if ((fin->fin_flx & (FI_SHORT|FI_BAD)) != 0) {
791 if (softp->ips_proxy_debug & 0x08)
792 printf("ipf_proxy_match: flx 0x%x (BAD|SHORT)\n",
798 if ((apr == NULL) || (apr->apr_flags & APR_DELETE)) {
799 if (softp->ips_proxy_debug & 0x08)
800 printf("ipf_proxy_match:apr %lx apr_flags 0x%x\n",
801 (u_long)apr, apr ? apr->apr_flags : 0);
805 if (apr->apr_match != NULL) {
806 result = (*apr->apr_match)(fin, nat->nat_aps, nat);
808 if (softp->ips_proxy_debug & 0x08)
809 printf("ipf_proxy_match: result %d\n", result);
817 /* ------------------------------------------------------------------------ */
818 /* Function: ipf_proxy_new */
819 /* Returns: int - 0 == success, else error */
820 /* Parameters: fin(I) - pointer to packet information */
821 /* nat(I) - pointer to current NAT session */
823 /* Allocate a new application proxy structure and fill it in with the */
824 /* relevant details. call the init function once complete, prior to */
826 /* ------------------------------------------------------------------------ */
828 ipf_proxy_new(fin, nat)
832 ipf_main_softc_t *softc = fin->fin_main_soft;
833 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
834 register ap_session_t *aps;
837 if (softp->ips_proxy_debug & 0x04)
838 printf("ipf_proxy_new(%lx,%lx) \n", (u_long)fin, (u_long)nat);
840 if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL)) {
841 if (softp->ips_proxy_debug & 0x08)
842 printf("ipf_proxy_new: nat_ptr %lx nat_aps %lx\n",
843 (u_long)nat->nat_ptr, (u_long)nat->nat_aps);
847 apr = nat->nat_ptr->in_apr;
849 if ((apr->apr_flags & APR_DELETE) ||
850 (fin->fin_p != apr->apr_p)) {
851 if (softp->ips_proxy_debug & 0x08)
852 printf("ipf_proxy_new: apr_flags 0x%x p %d/%d\n",
853 apr->apr_flags, fin->fin_p, apr->apr_p);
857 KMALLOC(aps, ap_session_t *);
859 if (softp->ips_proxy_debug & 0x08)
860 printf("ipf_proxy_new: malloc failed (%lu)\n",
861 (u_long)sizeof(ap_session_t));
865 bzero((char *)aps, sizeof(*aps));
866 aps->aps_data = NULL;
869 if (apr->apr_new != NULL)
870 if ((*apr->apr_new)(apr->apr_soft, fin, aps, nat) == -1) {
871 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
872 KFREES(aps->aps_data, aps->aps_psiz);
875 if (softp->ips_proxy_debug & 0x08)
876 printf("ipf_proxy_new: new(%lx) failed\n",
877 (u_long)apr->apr_new);
881 aps->aps_next = softp->ips_sess_list;
882 softp->ips_sess_list = aps;
889 /* ------------------------------------------------------------------------ */
890 /* Function: ipf_proxy_check */
891 /* Returns: int - -1 == error, 0 == success */
892 /* Parameters: fin(I) - pointer to packet information */
893 /* nat(I) - pointer to current NAT session */
895 /* Check to see if a packet should be passed through an active proxy */
896 /* routine if one has been setup for it. We don't need to check the */
897 /* checksum here if IPFILTER_CKSUM is defined because if it is, a failed */
898 /* check causes FI_BAD to be set. */
899 /* ------------------------------------------------------------------------ */
901 ipf_proxy_check(fin, nat)
905 ipf_main_softc_t *softc = fin->fin_main_soft;
906 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
907 #if SOLARIS && defined(_KERNEL) && defined(ICK_VALID)
910 tcphdr_t *tcp = NULL;
911 udphdr_t *udp = NULL;
919 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi)
923 if (fin->fin_flx & FI_BAD) {
924 if (softp->ips_proxy_debug & 0x08)
925 printf("ipf_proxy_check: flx 0x%x (BAD)\n",
930 #ifndef IPFILTER_CKSUM
931 if ((fin->fin_out == 0) && (ipf_checkl4sum(fin) == -1)) {
932 if (softp->ips_proxy_debug & 0x08)
933 printf("ipf_proxy_check: l4 checksum failure %d\n",
935 if (fin->fin_p == IPPROTO_TCP)
936 softc->ipf_stats[fin->fin_out].fr_tcpbad++;
944 * If there is data in this packet to be proxied then try and
945 * get it all into the one buffer, else drop it.
947 #if defined(MENTAT) || defined(HAVE_M_PULLDOWN)
948 if ((fin->fin_dlen > 0) && !(fin->fin_flx & FI_COALESCE))
949 if (ipf_coalesce(fin) == -1) {
950 if (softp->ips_proxy_debug & 0x08)
951 printf("ipf_proxy_check: %s %x\n",
952 "coalesce failed", fin->fin_flx);
957 if (fin->fin_cksum > FI_CK_SUMOK)
965 tcp = (tcphdr_t *)fin->fin_dp;
966 #if SOLARIS && defined(_KERNEL) && defined(ICK_VALID)
968 if (dohwcksum && (m->b_ick_flag == ICK_VALID))
973 udp = (udphdr_t *)fin->fin_dp;
981 if (fin->fin_out != 0) {
982 if (apr->apr_outpkt != NULL)
983 err = (*apr->apr_outpkt)(apr->apr_soft, fin,
986 if (apr->apr_inpkt != NULL)
987 err = (*apr->apr_inpkt)(apr->apr_soft, fin,
992 if (((softp->ips_proxy_debug & 0x08) && (rv != 0)) ||
993 (softp->ips_proxy_debug & 0x04))
994 printf("ipf_proxy_check: out %d err %x rv %d\n",
995 fin->fin_out, err, rv);
1000 ipf_proxy_deref(apr);
1001 nat->nat_aps = NULL;
1006 * If err != 0 then the data size of the packet has changed
1007 * so we need to recalculate the header checksums for the
1010 adjlen = APR_INC(err);
1011 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi)
1012 s1 = LONG_SUM(fin->fin_plen - adjlen);
1013 s2 = LONG_SUM(fin->fin_plen);
1014 CALC_SUMD(s1, s2, sd);
1015 if ((err != 0) && (fin->fin_cksum < FI_CK_L4PART) &&
1017 ipf_fix_outcksum(0, &ip->ip_sum, sd, 0);
1019 if (fin->fin_flx & FI_DOCKSUM)
1023 * For TCP packets, we may need to adjust the sequence and
1024 * acknowledgement numbers to reflect changes in size of the
1027 * For both TCP and UDP, recalculate the layer 4 checksum,
1028 * regardless, as we can't tell (here) if data has been
1032 err = ipf_proxy_fixseqack(fin, ip, aps, adjlen);
1033 if (fin->fin_cksum == FI_CK_L4PART) {
1034 u_short sum = ntohs(tcp->th_sum);
1036 tcp->th_sum = htons(sum);
1037 } else if (fin->fin_cksum < FI_CK_L4PART) {
1038 tcp->th_sum = fr_cksum(fin, ip,
1041 } else if ((udp != NULL) && (udp->uh_sum != 0)) {
1042 if (fin->fin_cksum == FI_CK_L4PART) {
1043 u_short sum = ntohs(udp->uh_sum);
1045 udp->uh_sum = htons(sum);
1047 udp->uh_sum = fr_cksum(fin, ip,
1051 aps->aps_bytes += fin->fin_plen;
1059 /* ------------------------------------------------------------------------ */
1060 /* Function: ipf_proxy_lookup */
1061 /* Returns: int - -1 == error, 0 == success */
1062 /* Parameters: arg(I) - pointer to proxy context information */
1063 /* pr(I) - protocol number for proxy */
1064 /* name(I) - proxy name */
1066 /* Search for an proxy by the protocol it is being used with and its name. */
1067 /* ------------------------------------------------------------------------ */
1069 ipf_proxy_lookup(arg, pr, name)
1074 ipf_proxy_softc_t *softp = arg;
1077 if (softp->ips_proxy_debug & 0x04)
1078 printf("ipf_proxy_lookup(%d,%s)\n", pr, name);
1080 for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next)
1081 if ((ap->apr_p == pr) &&
1082 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
1087 if (softp->ips_proxy_debug & 0x08)
1088 printf("ipf_proxy_lookup: failed for %d/%s\n", pr, name);
1093 /* ------------------------------------------------------------------------ */
1094 /* Function: ipf_proxy_deref */
1096 /* Parameters: ap(I) - pointer to proxy structure */
1098 /* Drop the reference counter associated with the proxy. */
1099 /* ------------------------------------------------------------------------ */
1108 /* ------------------------------------------------------------------------ */
1109 /* Function: ipf_proxy_free */
1111 /* Parameters: softc(I) - pointer to soft context main structure */
1112 /* aps(I) - pointer to current proxy session */
1113 /* Locks Held: ipf_nat_new, ipf_nat(W) */
1115 /* Free up proxy session information allocated to be used with a NAT */
1117 /* ------------------------------------------------------------------------ */
1119 ipf_proxy_free(softc, aps)
1120 ipf_main_softc_t *softc;
1123 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
1124 ap_session_t *a, **ap;
1130 for (ap = &softp->ips_sess_list; ((a = *ap) != NULL); ap = &a->aps_next)
1137 if ((apr != NULL) && (apr->apr_del != NULL))
1138 (*apr->apr_del)(softc, aps);
1140 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
1141 KFREES(aps->aps_data, aps->aps_psiz);
1146 /* ------------------------------------------------------------------------ */
1147 /* Function: ipf_proxy_fixseqack */
1148 /* Returns: int - 2 if TCP ack/seq is changed, else 0 */
1149 /* Parameters: fin(I) - pointer to packet information */
1150 /* ip(I) - pointer to IP header */
1151 /* nat(I) - pointer to current NAT session */
1152 /* inc(I) - delta to apply to TCP sequence numbering */
1154 /* Adjust the TCP sequence/acknowledge numbers in the TCP header based on */
1155 /* whether or not the new header is past the point at which an adjustment */
1156 /* occurred. This might happen because of (say) an FTP string being changed */
1157 /* and the new string being a different length to the old. */
1158 /* ------------------------------------------------------------------------ */
1160 ipf_proxy_fixseqack(fin, ip, aps, inc)
1166 ipf_main_softc_t *softc = fin->fin_main_soft;
1167 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
1168 int sel, ch = 0, out, nlen;
1173 tcp = (tcphdr_t *)fin->fin_dp;
1176 * ip_len has already been adjusted by 'inc'.
1178 nlen = fin->fin_dlen;
1179 nlen -= (TCP_OFF(tcp) << 2);
1185 seq1 = (u_32_t)ntohl(tcp->th_seq);
1186 sel = aps->aps_sel[out];
1188 /* switch to other set ? */
1189 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
1190 (seq1 > aps->aps_seqmin[!sel])) {
1191 if (softp->ips_proxy_debug & 0x10)
1192 printf("proxy out switch set seq %d -> %d %x > %x\n",
1194 aps->aps_seqmin[!sel]);
1195 sel = aps->aps_sel[out] = !sel;
1198 if (aps->aps_seqoff[sel]) {
1199 seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
1201 seq2 = aps->aps_seqoff[sel];
1203 tcp->th_seq = htonl(seq1);
1208 if (inc && (seq1 > aps->aps_seqmin[!sel])) {
1209 aps->aps_seqmin[sel] = seq1 + nlen - 1;
1210 aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
1211 if (softp->ips_proxy_debug & 0x10)
1212 printf("proxy seq set %d at %x to %d + %d\n",
1213 sel, aps->aps_seqmin[sel],
1214 aps->aps_seqoff[sel], inc);
1219 seq1 = ntohl(tcp->th_ack);
1220 sel = aps->aps_sel[1 - out];
1222 /* switch to other set ? */
1223 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
1224 (seq1 > aps->aps_ackmin[!sel])) {
1225 if (softp->ips_proxy_debug & 0x10)
1226 printf("proxy out switch set ack %d -> %d %x > %x\n",
1228 aps->aps_ackmin[!sel]);
1229 sel = aps->aps_sel[1 - out] = !sel;
1232 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
1233 seq2 = aps->aps_ackoff[sel];
1234 tcp->th_ack = htonl(seq1 - seq2);
1238 seq1 = ntohl(tcp->th_seq);
1239 sel = aps->aps_sel[out];
1241 /* switch to other set ? */
1242 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
1243 (seq1 > aps->aps_ackmin[!sel])) {
1244 if (softp->ips_proxy_debug & 0x10)
1245 printf("proxy in switch set ack %d -> %d %x > %x\n",
1246 sel, !sel, seq1, aps->aps_ackmin[!sel]);
1247 sel = aps->aps_sel[out] = !sel;
1250 if (aps->aps_ackoff[sel]) {
1251 seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
1253 seq2 = aps->aps_ackoff[sel];
1255 tcp->th_seq = htonl(seq1);
1260 if (inc && (seq1 > aps->aps_ackmin[!sel])) {
1261 aps->aps_ackmin[!sel] = seq1 + nlen - 1;
1262 aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
1264 if (softp->ips_proxy_debug & 0x10)
1265 printf("proxy ack set %d at %x to %d + %d\n",
1266 !sel, aps->aps_seqmin[!sel],
1267 aps->aps_seqoff[sel], inc);
1272 seq1 = ntohl(tcp->th_ack);
1273 sel = aps->aps_sel[1 - out];
1275 /* switch to other set ? */
1276 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
1277 (seq1 > aps->aps_seqmin[!sel])) {
1278 if (softp->ips_proxy_debug & 0x10)
1279 printf("proxy in switch set seq %d -> %d %x > %x\n",
1280 sel, !sel, seq1, aps->aps_seqmin[!sel]);
1281 sel = aps->aps_sel[1 - out] = !sel;
1284 if (aps->aps_seqoff[sel] != 0) {
1285 if (softp->ips_proxy_debug & 0x10)
1286 printf("sel %d seqoff %d seq1 %x seqmin %x\n",
1287 sel, aps->aps_seqoff[sel], seq1,
1288 aps->aps_seqmin[sel]);
1289 if (seq1 > aps->aps_seqmin[sel]) {
1290 seq2 = aps->aps_seqoff[sel];
1291 tcp->th_ack = htonl(seq1 - seq2);
1297 if (softp->ips_proxy_debug & 0x10)
1298 printf("ipf_proxy_fixseqack: seq %u ack %u\n",
1299 (u_32_t)ntohl(tcp->th_seq), (u_32_t)ntohl(tcp->th_ack));
1304 /* ------------------------------------------------------------------------ */
1305 /* Function: ipf_proxy_rule_rev */
1306 /* Returns: ipnat_t * - NULL = failure, else pointer to new rule */
1307 /* Parameters: nat(I) - pointer to NAT session to create rule from */
1309 /* This function creates a NAT rule that is based upon the reverse packet */
1310 /* flow associated with this NAT session. Thus if this NAT session was */
1311 /* created with a map rule then this function will create a rdr rule. */
1312 /* Only address fields and network interfaces are assigned in this function */
1313 /* and the address fields are formed such that an exact is required. If the */
1314 /* original rule had a netmask, that is not replicated here not is it */
1315 /* desired. The ultimate goal here is to create a NAT rule to support a NAT */
1316 /* session being created that does not have a user configured rule. The */
1317 /* classic example is supporting the FTP proxy, where a data channel needs */
1318 /* to be setup, based on the addresses used for the control connection. In */
1319 /* that case, this function is used to handle creating NAT rules to support */
1320 /* data connections with the PORT and EPRT commands. */
1321 /* ------------------------------------------------------------------------ */
1323 ipf_proxy_rule_rev(nat)
1331 size = old->in_size;
1333 KMALLOCS(ipn, ipnat_t *, size);
1337 bzero((char *)ipn, size);
1343 ipn->in_size = size;
1344 ipn->in_pr[0] = old->in_pr[1];
1345 ipn->in_pr[1] = old->in_pr[0];
1346 ipn->in_v[0] = old->in_v[1];
1347 ipn->in_v[1] = old->in_v[0];
1348 ipn->in_ifps[0] = old->in_ifps[1];
1349 ipn->in_ifps[1] = old->in_ifps[0];
1350 ipn->in_flags = (old->in_flags | IPN_PROXYRULE);
1352 ipn->in_nsrcip6 = nat->nat_odst6;
1353 ipn->in_osrcip6 = nat->nat_ndst6;
1355 if ((old->in_redir & NAT_REDIRECT) != 0) {
1356 ipn->in_redir = NAT_MAP;
1357 if (ipn->in_v[0] == 4) {
1358 ipn->in_snip = ntohl(nat->nat_odstaddr);
1359 ipn->in_dnip = ntohl(nat->nat_nsrcaddr);
1362 ipn->in_snip6 = nat->nat_odst6;
1363 ipn->in_dnip6 = nat->nat_nsrc6;
1366 ipn->in_ndstip6 = nat->nat_nsrc6;
1367 ipn->in_odstip6 = nat->nat_osrc6;
1369 ipn->in_redir = NAT_REDIRECT;
1370 if (ipn->in_v[0] == 4) {
1371 ipn->in_snip = ntohl(nat->nat_odstaddr);
1372 ipn->in_dnip = ntohl(nat->nat_osrcaddr);
1375 ipn->in_snip6 = nat->nat_odst6;
1376 ipn->in_dnip6 = nat->nat_osrc6;
1379 ipn->in_ndstip6 = nat->nat_osrc6;
1380 ipn->in_odstip6 = nat->nat_nsrc6;
1383 IP6_SETONES(&ipn->in_osrcmsk6);
1384 IP6_SETONES(&ipn->in_nsrcmsk6);
1385 IP6_SETONES(&ipn->in_odstmsk6);
1386 IP6_SETONES(&ipn->in_ndstmsk6);
1388 ipn->in_namelen = old->in_namelen;
1389 ipn->in_ifnames[0] = old->in_ifnames[1];
1390 ipn->in_ifnames[1] = old->in_ifnames[0];
1391 bcopy(old->in_names, ipn->in_names, ipn->in_namelen);
1392 MUTEX_INIT(&ipn->in_lock, "ipnat rev rule lock");
1398 /* ------------------------------------------------------------------------ */
1399 /* Function: ipf_proxy_rule_fwd */
1400 /* Returns: ipnat_t * - NULL = failure, else pointer to new rule */
1401 /* Parameters: nat(I) - pointer to NAT session to create rule from */
1403 /* The purpose and rationale of this function is much the same as the above */
1404 /* function, ipf_proxy_rule_rev, except that a rule is created that matches */
1405 /* the same direction as that of the existing NAT session. Thus if this NAT */
1406 /* session was created with a map rule then this function will also create */
1407 /* a data structure to represent a map rule. Whereas ipf_proxy_rule_rev is */
1408 /* used to support PORT/EPRT, this function supports PASV/EPSV. */
1409 /* ------------------------------------------------------------------------ */
1411 ipf_proxy_rule_fwd(nat)
1419 size = old->in_size;
1421 KMALLOCS(ipn, ipnat_t *, size);
1425 bzero((char *)ipn, size);
1431 ipn->in_size = size;
1432 ipn->in_pr[0] = old->in_pr[0];
1433 ipn->in_pr[1] = old->in_pr[1];
1434 ipn->in_v[0] = old->in_v[0];
1435 ipn->in_v[1] = old->in_v[1];
1436 ipn->in_ifps[0] = nat->nat_ifps[0];
1437 ipn->in_ifps[1] = nat->nat_ifps[1];
1438 ipn->in_flags = (old->in_flags | IPN_PROXYRULE);
1440 ipn->in_nsrcip6 = nat->nat_nsrc6;
1441 ipn->in_osrcip6 = nat->nat_osrc6;
1442 ipn->in_ndstip6 = nat->nat_ndst6;
1443 ipn->in_odstip6 = nat->nat_odst6;
1444 ipn->in_redir = old->in_redir;
1446 if (ipn->in_v[0] == 4) {
1447 ipn->in_snip = ntohl(nat->nat_nsrcaddr);
1448 ipn->in_dnip = ntohl(nat->nat_ndstaddr);
1451 ipn->in_snip6 = nat->nat_nsrc6;
1452 ipn->in_dnip6 = nat->nat_ndst6;
1456 IP6_SETONES(&ipn->in_osrcmsk6);
1457 IP6_SETONES(&ipn->in_nsrcmsk6);
1458 IP6_SETONES(&ipn->in_odstmsk6);
1459 IP6_SETONES(&ipn->in_ndstmsk6);
1461 ipn->in_namelen = old->in_namelen;
1462 ipn->in_ifnames[0] = old->in_ifnames[0];
1463 ipn->in_ifnames[1] = old->in_ifnames[1];
1464 bcopy(old->in_names, ipn->in_names, ipn->in_namelen);
1465 MUTEX_INIT(&ipn->in_lock, "ipnat fwd rule lock");