3 * Copyright (C) 2012 by Darren Reed.
5 * See the IPFILTER.LICENCE file for details on licencing.
7 #if defined(KERNEL) || defined(_KERNEL)
13 #include <sys/errno.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
18 # include <sys/fcntl.h>
19 #if !defined(_KERNEL) && !defined(__KERNEL__)
28 # include <sys/protosw.h>
29 #include <sys/socket.h>
32 # include <sys/ctype.h>
34 # include <sys/systm.h>
36 # include <sys/mbuf.h>
39 #if defined(_KERNEL) && defined(__FreeBSD__)
40 # include <sys/filio.h>
41 # include <sys/fcntl.h>
43 # include <sys/ioctl.h>
46 # include <sys/byteorder.h>
48 # include <sys/dditypes.h>
50 # include <sys/stream.h>
51 # include <sys/kmem.h>
54 # include <sys/queue.h>
57 #if defined(__FreeBSD__) && defined(_KERNEL)
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
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>
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"
93 # include "netinet/ip_irc_pxy.c"
94 # include "netinet/ip_raudio_pxy.c"
95 # include "netinet/ip_netbios_pxy.c"
97 #include "netinet/ip_ipsec_pxy.c"
98 #include "netinet/ip_rpcb_pxy.c"
101 static const char rcsid[] = "@(#)$Id$";
104 #define AP_SESS_SIZE 53
106 static int ipf_proxy_fixseqack(fr_info_t *, ip_t *, ap_session_t *, int );
107 static aproxy_t *ipf_proxy_create_clone(ipf_main_softc_t *, aproxy_t *);
109 typedef struct ipf_proxy_softc_s {
111 int ips_proxy_session_size;
112 ap_session_t **ips_sess_tab;
113 ap_session_t *ips_sess_list;
114 aproxy_t *ips_proxies;
116 ipftuneable_t *ipf_proxy_tune;
119 static ipftuneable_t ipf_proxy_tuneables[] = {
120 { { (void *)offsetof(ipf_proxy_softc_t, ips_proxy_debug) },
121 "proxy_debug", 0, 0x1f,
122 stsizeof(ipf_proxy_softc_t, ips_proxy_debug),
124 { { NULL }, NULL, 0, 0,
129 static aproxy_t *ap_proxylist = NULL;
130 static aproxy_t ips_proxies[] = {
132 { NULL, NULL, "ftp", (char)IPPROTO_TCP, 0, 0, 0,
133 ipf_p_ftp_main_load, ipf_p_ftp_main_unload,
134 ipf_p_ftp_soft_create, ipf_p_ftp_soft_destroy,
136 ipf_p_ftp_new, ipf_p_ftp_del, ipf_p_ftp_in, ipf_p_ftp_out, NULL,
137 NULL, NULL, NULL, NULL },
139 #ifdef IPF_TFTP_PROXY
140 { NULL, NULL, "tftp", (char)IPPROTO_UDP, 0, 0, 0,
141 ipf_p_tftp_main_load, ipf_p_tftp_main_unload,
142 ipf_p_tftp_soft_create, ipf_p_tftp_soft_destroy,
144 ipf_p_tftp_new, ipf_p_tftp_del,
145 ipf_p_tftp_in, ipf_p_tftp_out, NULL,
146 NULL, NULL, NULL, NULL },
149 { NULL, NULL, "irc", (char)IPPROTO_TCP, 0, 0, 0,
150 ipf_p_irc_main_load, ipf_p_irc_main_unload,
153 ipf_p_irc_new, NULL, NULL, ipf_p_irc_out, NULL,
154 NULL, NULL, NULL, NULL },
156 #ifdef IPF_RCMD_PROXY
157 { NULL, NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, 0,
158 ipf_p_rcmd_main_load, ipf_p_rcmd_main_unload,
161 ipf_p_rcmd_new, ipf_p_rcmd_del,
162 ipf_p_rcmd_in, ipf_p_rcmd_out, NULL,
163 NULL, NULL, NULL, NULL },
165 #ifdef IPF_RAUDIO_PROXY
166 { NULL, NULL, "raudio", (char)IPPROTO_TCP, 0, 0, 0,
167 ipf_p_raudio_main_load, ipf_p_raudio_main_unload,
170 ipf_p_raudio_new, NULL, ipf_p_raudio_in, ipf_p_raudio_out, NULL,
171 NULL, NULL, NULL, NULL },
173 #ifdef IPF_MSNRPC_PROXY
174 { NULL, NULL, "msnrpc", (char)IPPROTO_TCP, 0, 0, 0,
175 ipf_p_msnrpc_init, ipf_p_msnrpc_fini,
178 ipf_p_msnrpc_new, NULL, ipf_p_msnrpc_in, ipf_p_msnrpc_out, NULL,
179 NULL, NULL, NULL, NULL },
181 #ifdef IPF_NETBIOS_PROXY
182 { NULL, NULL, "netbios", (char)IPPROTO_UDP, 0, 0, 0,
183 ipf_p_netbios_main_load, ipf_p_netbios_main_unload,
186 NULL, NULL, NULL, ipf_p_netbios_out, NULL,
187 NULL, NULL, NULL, NULL },
189 #ifdef IPF_IPSEC_PROXY
190 { NULL, NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, 0,
192 ipf_p_ipsec_soft_create, ipf_p_ipsec_soft_destroy,
193 ipf_p_ipsec_soft_init, ipf_p_ipsec_soft_fini,
194 ipf_p_ipsec_new, ipf_p_ipsec_del,
195 ipf_p_ipsec_inout, ipf_p_ipsec_inout, ipf_p_ipsec_match,
196 NULL, NULL, NULL, NULL },
199 { NULL, NULL, "dns", (char)IPPROTO_UDP, 0, 0, 0,
201 ipf_p_dns_soft_create, ipf_p_dns_soft_destroy,
203 ipf_p_dns_new, ipf_p_ipsec_del,
204 ipf_p_dns_inout, ipf_p_dns_inout, ipf_p_dns_match,
205 ipf_p_dns_ctl, NULL, NULL, NULL },
207 #ifdef IPF_PPTP_PROXY
208 { NULL, NULL, "pptp", (char)IPPROTO_TCP, 0, 0, 0,
209 ipf_p_pptp_main_load, ipf_p_pptp_main_unload,
212 ipf_p_pptp_new, ipf_p_pptp_del,
213 ipf_p_pptp_inout, ipf_p_pptp_inout, NULL,
214 NULL, NULL, NULL, NULL },
216 #ifdef IPF_RPCB_PROXY
218 { NULL, NULL, "rpcbt", (char)IPPROTO_TCP, 0, 0, 0,
222 ipf_p_rpcb_new, ipf_p_rpcb_del,
223 ipf_p_rpcb_in, ipf_p_rpcb_out, NULL,
224 NULL, NULL, NULL, NULL },
226 { NULL, NULL, "rpcbu", (char)IPPROTO_UDP, 0, 0, 0,
227 ipf_p_rpcb_main_load, ipf_p_rpcb_main_unload,
230 ipf_p_rpcb_new, ipf_p_rpcb_del,
231 ipf_p_rpcb_in, ipf_p_rpcb_out, NULL,
232 NULL, NULL, NULL, NULL },
234 { NULL, NULL, "", '\0', 0, 0, 0,
240 NULL, NULL, NULL, NULL }
244 /* ------------------------------------------------------------------------ */
245 /* Function: ipf_proxy_main_load */
246 /* Returns: int - 0 == success, else failure. */
247 /* Parameters: Nil */
249 /* Initialise hook for kernel application proxies. */
250 /* Call the initialise routine for all the compiled in kernel proxies. */
251 /* ------------------------------------------------------------------------ */
253 ipf_proxy_main_load(void)
257 for (ap = ips_proxies; ap->apr_p; ap++) {
258 if (ap->apr_load != NULL)
265 /* ------------------------------------------------------------------------ */
266 /* Function: ipf_proxy_main_unload */
267 /* Returns: int - 0 == success, else failure. */
268 /* Parameters: Nil */
270 /* Unload hook for kernel application proxies. */
271 /* Call the finialise routine for all the compiled in kernel proxies. */
272 /* ------------------------------------------------------------------------ */
274 ipf_proxy_main_unload(void)
278 for (ap = ips_proxies; ap->apr_p; ap++)
279 if (ap->apr_unload != NULL)
281 for (ap = ap_proxylist; ap; ap = ap->apr_next)
282 if (ap->apr_unload != NULL)
289 /* ------------------------------------------------------------------------ */
290 /* Function: ipf_proxy_soft_create */
291 /* Returns: void * - */
292 /* Parameters: softc(I) - pointer to soft context main structure */
294 /* Build the structure to hold all of the run time data to support proxies. */
295 /* ------------------------------------------------------------------------ */
297 ipf_proxy_soft_create(ipf_main_softc_t *softc)
299 ipf_proxy_softc_t *softp;
304 KMALLOC(softp, ipf_proxy_softc_t *);
308 bzero((char *)softp, sizeof(*softp));
311 softp->ips_proxy_debug = 0;
313 softp->ips_proxy_debug = 2;
315 softp->ips_proxy_session_size = AP_SESS_SIZE;
317 softp->ipf_proxy_tune = ipf_tune_array_copy(softp,
318 sizeof(ipf_proxy_tuneables),
319 ipf_proxy_tuneables);
320 if (softp->ipf_proxy_tune == NULL) {
321 ipf_proxy_soft_destroy(softc, softp);
324 if (ipf_tune_array_link(softc, softp->ipf_proxy_tune) == -1) {
325 ipf_proxy_soft_destroy(softc, softp);
330 for (ap = ips_proxies; ap->apr_p; ap++) {
331 apn = ipf_proxy_create_clone(softc, ap);
335 last->apr_next = apn;
337 softp->ips_proxies = apn;
340 for (ap = ips_proxies; ap != NULL; ap = ap->apr_next) {
341 apn = ipf_proxy_create_clone(softc, ap);
345 last->apr_next = apn;
347 softp->ips_proxies = apn;
353 ipf_proxy_soft_destroy(softc, softp);
358 /* ------------------------------------------------------------------------ */
359 /* Function: ipf_proxy_soft_create */
360 /* Returns: void * - */
361 /* Parameters: softc(I) - pointer to soft context main structure */
362 /* orig(I) - pointer to proxy definition to copy */
364 /* This function clones a proxy definition given by orig and returns a */
365 /* a pointer to that copy. */
366 /* ------------------------------------------------------------------------ */
368 ipf_proxy_create_clone(ipf_main_softc_t *softc, aproxy_t *orig)
372 KMALLOC(apn, aproxy_t *);
376 bcopy((char *)orig, (char *)apn, sizeof(*apn));
377 apn->apr_next = NULL;
378 apn->apr_soft = NULL;
380 if (apn->apr_create != NULL) {
381 apn->apr_soft = (*apn->apr_create)(softc);
382 if (apn->apr_soft == NULL) {
388 apn->apr_parent = orig;
395 /* ------------------------------------------------------------------------ */
396 /* Function: ipf_proxy_soft_create */
397 /* Returns: int - 0 == success, else failure. */
398 /* Parameters: softc(I) - pointer to soft context main structure */
399 /* arg(I) - pointer to proxy contect data */
401 /* Initialise the proxy context and walk through each of the proxies and */
402 /* call its initialisation function. This allows for proxies to do any */
403 /* local setup prior to actual use. */
404 /* ------------------------------------------------------------------------ */
406 ipf_proxy_soft_init(ipf_main_softc_t *softc, void *arg)
408 ipf_proxy_softc_t *softp;
414 size = softp->ips_proxy_session_size * sizeof(ap_session_t *);
416 KMALLOCS(softp->ips_sess_tab, ap_session_t **, size);
418 if (softp->ips_sess_tab == NULL)
421 bzero(softp->ips_sess_tab, size);
423 for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next) {
424 if (ap->apr_init != NULL) {
425 err = (*ap->apr_init)(softc, ap->apr_soft);
430 softp->ips_init_run = 1;
436 /* ------------------------------------------------------------------------ */
437 /* Function: ipf_proxy_soft_create */
438 /* Returns: int - 0 == success, else failure. */
439 /* Parameters: softc(I) - pointer to soft context main structure */
440 /* arg(I) - pointer to proxy contect data */
442 /* This function should always succeed. It is responsible for ensuring that */
443 /* the proxy context can be safely called when ipf_proxy_soft_destroy is */
444 /* called and suring all of the proxies have similarly been instructed. */
445 /* ------------------------------------------------------------------------ */
447 ipf_proxy_soft_fini(ipf_main_softc_t *softc, void *arg)
449 ipf_proxy_softc_t *softp = arg;
452 for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next) {
453 if (ap->apr_fini != NULL) {
454 (*ap->apr_fini)(softc, ap->apr_soft);
458 if (softp->ips_sess_tab != NULL) {
459 KFREES(softp->ips_sess_tab,
460 softp->ips_proxy_session_size * sizeof(ap_session_t *));
461 softp->ips_sess_tab = NULL;
463 softp->ips_init_run = 0;
469 /* ------------------------------------------------------------------------ */
470 /* Function: ipf_proxy_soft_destroy */
472 /* Parameters: softc(I) - pointer to soft context main structure */
473 /* arg(I) - pointer to proxy contect data */
475 /* Free up all of the local data structures allocated during creation. */
476 /* ------------------------------------------------------------------------ */
478 ipf_proxy_soft_destroy(ipf_main_softc_t *softc, void *arg)
480 ipf_proxy_softc_t *softp = arg;
483 while ((ap = softp->ips_proxies) != NULL) {
484 softp->ips_proxies = ap->apr_next;
485 if (ap->apr_destroy != NULL)
486 (*ap->apr_destroy)(softc, ap->apr_soft);
487 ap->apr_parent->apr_clones--;
491 if (softp->ipf_proxy_tune != NULL) {
492 ipf_tune_array_unlink(softc, softp->ipf_proxy_tune);
493 KFREES(softp->ipf_proxy_tune, sizeof(ipf_proxy_tuneables));
494 softp->ipf_proxy_tune = NULL;
501 /* ------------------------------------------------------------------------ */
502 /* Function: ipf_proxy_flush */
504 /* Parameters: arg(I) - pointer to proxy contect data */
505 /* how(I) - indicates the type of flush operation */
507 /* Walk through all of the proxies and pass on the flush command as either */
508 /* a flush or a clear. */
509 /* ------------------------------------------------------------------------ */
511 ipf_proxy_flush(void *arg, int how)
513 ipf_proxy_softc_t *softp = arg;
519 for (ap = softp->ips_proxies; ap; ap = ap->apr_next)
520 if (ap->apr_flush != NULL)
521 (*ap->apr_flush)(ap, how);
524 for (ap = softp->ips_proxies; ap; ap = ap->apr_next)
525 if (ap->apr_clear != NULL)
526 (*ap->apr_clear)(ap);
534 /* ------------------------------------------------------------------------ */
535 /* Function: ipf_proxy_add */
536 /* Returns: int - 0 == success, else failure. */
537 /* Parameters: ap(I) - pointer to proxy structure */
539 /* Dynamically add a new kernel proxy. Ensure that it is unique in the */
540 /* collection compiled in and dynamically added. */
541 /* ------------------------------------------------------------------------ */
543 ipf_proxy_add(void *arg, aproxy_t *ap)
545 ipf_proxy_softc_t *softp = arg;
549 for (a = ips_proxies; a->apr_p; a++)
550 if ((a->apr_p == ap->apr_p) &&
551 !strncmp(a->apr_label, ap->apr_label,
552 sizeof(ap->apr_label))) {
553 if (softp->ips_proxy_debug & 0x01)
554 printf("ipf_proxy_add: %s/%d present (B)\n",
555 a->apr_label, a->apr_p);
559 for (a = ap_proxylist; (a != NULL); a = a->apr_next)
560 if ((a->apr_p == ap->apr_p) &&
561 !strncmp(a->apr_label, ap->apr_label,
562 sizeof(ap->apr_label))) {
563 if (softp->ips_proxy_debug & 0x01)
564 printf("ipf_proxy_add: %s/%d present (D)\n",
565 a->apr_label, a->apr_p);
568 ap->apr_next = ap_proxylist;
570 if (ap->apr_load != NULL)
576 /* ------------------------------------------------------------------------ */
577 /* Function: ipf_proxy_ctl */
578 /* Returns: int - 0 == success, else error */
579 /* Parameters: softc(I) - pointer to soft context main structure */
580 /* arg(I) - pointer to proxy context */
581 /* ctl(I) - pointer to proxy control structure */
583 /* Check to see if the proxy this control request has come through for */
584 /* exists, and if it does and it has a control function then invoke that */
585 /* control function. */
586 /* ------------------------------------------------------------------------ */
588 ipf_proxy_ctl(ipf_main_softc_t *softc, void *arg, ap_ctl_t *ctl)
590 ipf_proxy_softc_t *softp = arg;
594 a = ipf_proxy_lookup(arg, ctl->apc_p, ctl->apc_label);
596 if (softp->ips_proxy_debug & 0x01)
597 printf("ipf_proxy_ctl: can't find %s/%d\n",
598 ctl->apc_label, ctl->apc_p);
601 } else if (a->apr_ctl == NULL) {
602 if (softp->ips_proxy_debug & 0x01)
603 printf("ipf_proxy_ctl: no ctl function for %s/%d\n",
604 ctl->apc_label, ctl->apc_p);
608 error = (*a->apr_ctl)(softc, a->apr_soft, ctl);
609 if ((error != 0) && (softp->ips_proxy_debug & 0x02))
610 printf("ipf_proxy_ctl: %s/%d ctl error %d\n",
611 a->apr_label, a->apr_p, error);
617 /* ------------------------------------------------------------------------ */
618 /* Function: ipf_proxy_del */
619 /* Returns: int - 0 == success, else failure. */
620 /* Parameters: ap(I) - pointer to proxy structure */
622 /* Delete a proxy that has been added dynamically from those available. */
623 /* If it is in use, return 1 (do not destroy NOW), not in use 0 or -1 */
624 /* if it cannot be matched. */
625 /* ------------------------------------------------------------------------ */
627 ipf_proxy_del(aproxy_t *ap)
631 for (app = &ap_proxylist; ((a = *app) != NULL); app = &a->apr_next) {
633 a->apr_flags |= APR_DELETE;
634 if (ap->apr_ref == 0 && ap->apr_clones == 0) {
646 /* ------------------------------------------------------------------------ */
647 /* Function: ipf_proxy_ok */
648 /* Returns: int - 1 == good match else not. */
649 /* Parameters: fin(I) - pointer to packet information */
650 /* tcp(I) - pointer to TCP/UDP header */
651 /* nat(I) - pointer to current NAT session */
653 /* This function extends the NAT matching to ensure that a packet that has */
654 /* arrived matches the proxy information attached to the NAT rule. Notably, */
655 /* if the proxy is scheduled to be deleted then packets will not match the */
656 /* rule even if the rule is still active. */
657 /* ------------------------------------------------------------------------ */
659 ipf_proxy_ok(fr_info_t *fin, tcphdr_t *tcp, ipnat_t *np)
661 aproxy_t *apr = np->in_apr;
662 u_short dport = np->in_odport;
664 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
665 (fin->fin_p != apr->apr_p))
667 if ((tcp == NULL) && dport)
673 /* ------------------------------------------------------------------------ */
674 /* Function: ipf_proxy_ioctl */
675 /* Returns: int - 0 == success, else error */
676 /* Parameters: softc(I) - pointer to soft context main structure */
677 /* data(I) - pointer to ioctl data */
678 /* cmd(I) - ioctl command */
679 /* mode(I) - mode bits for device */
680 /* ctx(I) - pointer to context information */
682 /* ------------------------------------------------------------------------ */
684 ipf_proxy_ioctl(ipf_main_softc_t *softc, caddr_t data, ioctlcmd_t cmd,
691 mode = mode; /* LINT */
696 error = ipf_inobj(softc, data, NULL, &ctl, IPFOBJ_PROXYCTL);
702 if (ctl.apc_dsize > 0) {
703 KMALLOCS(ptr, caddr_t, ctl.apc_dsize);
708 error = copyinptr(softc, ctl.apc_data, ptr,
719 error = ipf_proxy_ctl(softc, softc->ipf_proxy_soft,
722 if ((error != 0) && (ptr != NULL)) {
723 KFREES(ptr, ctl.apc_dsize);
735 /* ------------------------------------------------------------------------ */
736 /* Function: ipf_proxy_match */
737 /* Returns: int - 0 == success, else error */
738 /* Parameters: fin(I) - pointer to packet information */
739 /* nat(I) - pointer to current NAT session */
741 /* If a proxy has a match function, call that to do extended packet */
742 /* matching. Whilst other parts of the NAT code are rather lenient when it */
743 /* comes to the quality of the packet that it will transform, the proxy */
744 /* matching is not because they need to work with data, not just headers. */
745 /* ------------------------------------------------------------------------ */
747 ipf_proxy_match(fr_info_t *fin, nat_t *nat)
749 ipf_main_softc_t *softc = fin->fin_main_soft;
750 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
756 if (softp->ips_proxy_debug & 0x04)
757 printf("ipf_proxy_match(%lx,%lx) aps %lx ptr %lx\n",
758 (u_long)fin, (u_long)nat, (u_long)nat->nat_aps,
761 if ((fin->fin_flx & (FI_SHORT|FI_BAD)) != 0) {
762 if (softp->ips_proxy_debug & 0x08)
763 printf("ipf_proxy_match: flx 0x%x (BAD|SHORT)\n",
769 if ((apr == NULL) || (apr->apr_flags & APR_DELETE)) {
770 if (softp->ips_proxy_debug & 0x08)
771 printf("ipf_proxy_match:apr %lx apr_flags 0x%x\n",
772 (u_long)apr, apr ? apr->apr_flags : 0);
776 if (apr->apr_match != NULL) {
777 result = (*apr->apr_match)(fin, nat->nat_aps, nat);
779 if (softp->ips_proxy_debug & 0x08)
780 printf("ipf_proxy_match: result %d\n", result);
788 /* ------------------------------------------------------------------------ */
789 /* Function: ipf_proxy_new */
790 /* Returns: int - 0 == success, else error */
791 /* Parameters: fin(I) - pointer to packet information */
792 /* nat(I) - pointer to current NAT session */
794 /* Allocate a new application proxy structure and fill it in with the */
795 /* relevant details. call the init function once complete, prior to */
797 /* ------------------------------------------------------------------------ */
799 ipf_proxy_new(fr_info_t *fin, nat_t *nat)
801 ipf_main_softc_t *softc = fin->fin_main_soft;
802 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
803 register ap_session_t *aps;
806 if (softp->ips_proxy_debug & 0x04)
807 printf("ipf_proxy_new(%lx,%lx) \n", (u_long)fin, (u_long)nat);
809 if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL)) {
810 if (softp->ips_proxy_debug & 0x08)
811 printf("ipf_proxy_new: nat_ptr %lx nat_aps %lx\n",
812 (u_long)nat->nat_ptr, (u_long)nat->nat_aps);
816 apr = nat->nat_ptr->in_apr;
818 if ((apr->apr_flags & APR_DELETE) ||
819 (fin->fin_p != apr->apr_p)) {
820 if (softp->ips_proxy_debug & 0x08)
821 printf("ipf_proxy_new: apr_flags 0x%x p %d/%d\n",
822 apr->apr_flags, fin->fin_p, apr->apr_p);
826 KMALLOC(aps, ap_session_t *);
828 if (softp->ips_proxy_debug & 0x08)
829 printf("ipf_proxy_new: malloc failed (%lu)\n",
830 (u_long)sizeof(ap_session_t));
834 bzero((char *)aps, sizeof(*aps));
835 aps->aps_data = NULL;
838 if (apr->apr_new != NULL)
839 if ((*apr->apr_new)(apr->apr_soft, fin, aps, nat) == -1) {
840 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
841 KFREES(aps->aps_data, aps->aps_psiz);
844 if (softp->ips_proxy_debug & 0x08)
845 printf("ipf_proxy_new: new(%lx) failed\n",
846 (u_long)apr->apr_new);
850 aps->aps_next = softp->ips_sess_list;
851 softp->ips_sess_list = aps;
858 /* ------------------------------------------------------------------------ */
859 /* Function: ipf_proxy_check */
860 /* Returns: int - -1 == error, 1 == success */
861 /* Parameters: fin(I) - pointer to packet information */
862 /* nat(I) - pointer to current NAT session */
864 /* Check to see if a packet should be passed through an active proxy */
865 /* routine if one has been setup for it. We don't need to check the */
866 /* checksum here if IPFILTER_CKSUM is defined because if it is, a failed */
867 /* check causes FI_BAD to be set. */
868 /* ------------------------------------------------------------------------ */
870 ipf_proxy_check(fr_info_t *fin, nat_t *nat)
872 ipf_main_softc_t *softc = fin->fin_main_soft;
873 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
874 #if SOLARIS && defined(_KERNEL) && defined(ICK_VALID)
877 tcphdr_t *tcp = NULL;
878 udphdr_t *udp = NULL;
886 #if !defined(_KERNEL) || SOLARIS || defined(__FreeBSD__)
890 if (fin->fin_flx & FI_BAD) {
891 if (softp->ips_proxy_debug & 0x08)
892 printf("ipf_proxy_check: flx 0x%x (BAD)\n",
897 #ifndef IPFILTER_CKSUM
898 if ((fin->fin_out == 0) && (ipf_checkl4sum(fin) == -1)) {
899 if (softp->ips_proxy_debug & 0x08)
900 printf("ipf_proxy_check: l4 checksum failure %d\n",
902 if (fin->fin_p == IPPROTO_TCP)
903 softc->ipf_stats[fin->fin_out].fr_tcpbad++;
911 * If there is data in this packet to be proxied then try and
912 * get it all into the one buffer, else drop it.
914 #if SOLARIS || defined(HAVE_M_PULLDOWN)
915 if ((fin->fin_dlen > 0) && !(fin->fin_flx & FI_COALESCE))
916 if (ipf_coalesce(fin) == -1) {
917 if (softp->ips_proxy_debug & 0x08)
918 printf("ipf_proxy_check: %s %x\n",
919 "coalesce failed", fin->fin_flx);
924 if (fin->fin_cksum > FI_CK_SUMOK)
932 tcp = (tcphdr_t *)fin->fin_dp;
933 #if SOLARIS && defined(_KERNEL) && defined(ICK_VALID)
935 if (dohwcksum && (m->b_ick_flag == ICK_VALID))
940 udp = (udphdr_t *)fin->fin_dp;
948 if (fin->fin_out != 0) {
949 if (apr->apr_outpkt != NULL)
950 err = (*apr->apr_outpkt)(apr->apr_soft, fin,
953 if (apr->apr_inpkt != NULL)
954 err = (*apr->apr_inpkt)(apr->apr_soft, fin,
959 if (((softp->ips_proxy_debug & 0x08) && (rv != 0)) ||
960 (softp->ips_proxy_debug & 0x04))
961 printf("ipf_proxy_check: out %d err %x rv %d\n",
962 fin->fin_out, err, rv);
967 ipf_proxy_deref(apr);
973 * If err != 0 then the data size of the packet has changed
974 * so we need to recalculate the header checksums for the
977 adjlen = APR_INC(err);
978 #if !defined(_KERNEL) || SOLARIS || defined(__FreeBSD__)
979 s1 = LONG_SUM(fin->fin_plen - adjlen);
980 s2 = LONG_SUM(fin->fin_plen);
981 CALC_SUMD(s1, s2, sd);
982 if ((err != 0) && (fin->fin_cksum < FI_CK_L4PART) &&
984 ipf_fix_outcksum(0, &ip->ip_sum, sd, 0);
986 if (fin->fin_flx & FI_DOCKSUM)
990 * For TCP packets, we may need to adjust the sequence and
991 * acknowledgement numbers to reflect changes in size of the
994 * For both TCP and UDP, recalculate the layer 4 checksum,
995 * regardless, as we can't tell (here) if data has been
999 err = ipf_proxy_fixseqack(fin, ip, aps, adjlen);
1000 if (fin->fin_cksum == FI_CK_L4PART) {
1001 u_short sum = ntohs(tcp->th_sum);
1003 tcp->th_sum = htons(sum);
1004 } else if (fin->fin_cksum < FI_CK_L4PART) {
1005 tcp->th_sum = fr_cksum(fin, ip,
1008 } else if ((udp != NULL) && (udp->uh_sum != 0)) {
1009 if (fin->fin_cksum == FI_CK_L4PART) {
1010 u_short sum = ntohs(udp->uh_sum);
1012 udp->uh_sum = htons(sum);
1014 udp->uh_sum = fr_cksum(fin, ip,
1018 aps->aps_bytes += fin->fin_plen;
1025 /* ------------------------------------------------------------------------ */
1026 /* Function: ipf_proxy_lookup */
1027 /* Returns: int - -1 == error, 0 == success */
1028 /* Parameters: arg(I) - pointer to proxy context information */
1029 /* pr(I) - protocol number for proxy */
1030 /* name(I) - proxy name */
1032 /* Search for a proxy by the protocol being used and by its name. */
1033 /* ------------------------------------------------------------------------ */
1035 ipf_proxy_lookup(void *arg, u_int pr, char *name)
1037 ipf_proxy_softc_t *softp = arg;
1040 if (softp->ips_proxy_debug & 0x04)
1041 printf("ipf_proxy_lookup(%d,%s)\n", pr, name);
1043 for (ap = softp->ips_proxies; ap != NULL; ap = ap->apr_next)
1044 if ((ap->apr_p == pr) &&
1045 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
1050 if (softp->ips_proxy_debug & 0x08)
1051 printf("ipf_proxy_lookup: failed for %d/%s\n", pr, name);
1056 /* ------------------------------------------------------------------------ */
1057 /* Function: ipf_proxy_deref */
1059 /* Parameters: ap(I) - pointer to proxy structure */
1061 /* Drop the reference counter associated with the proxy. */
1062 /* ------------------------------------------------------------------------ */
1064 ipf_proxy_deref(aproxy_t *ap)
1070 /* ------------------------------------------------------------------------ */
1071 /* Function: ipf_proxy_free */
1073 /* Parameters: softc(I) - pointer to soft context main structure */
1074 /* aps(I) - pointer to current proxy session */
1075 /* Locks Held: ipf_nat_new, ipf_nat(W) */
1077 /* Free up proxy session information allocated to be used with a NAT */
1079 /* ------------------------------------------------------------------------ */
1081 ipf_proxy_free(ipf_main_softc_t *softc, ap_session_t *aps)
1083 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
1084 ap_session_t *a, **ap;
1090 for (ap = &softp->ips_sess_list; ((a = *ap) != NULL); ap = &a->aps_next)
1097 if ((apr != NULL) && (apr->apr_del != NULL))
1098 (*apr->apr_del)(softc, aps);
1100 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
1101 KFREES(aps->aps_data, aps->aps_psiz);
1106 /* ------------------------------------------------------------------------ */
1107 /* Function: ipf_proxy_fixseqack */
1108 /* Returns: int - 2 if TCP ack/seq is changed, else 0 */
1109 /* Parameters: fin(I) - pointer to packet information */
1110 /* ip(I) - pointer to IP header */
1111 /* nat(I) - pointer to current NAT session */
1112 /* inc(I) - delta to apply to TCP sequence numbering */
1114 /* Adjust the TCP sequence/acknowledge numbers in the TCP header based on */
1115 /* whether or not the new header is past the point at which an adjustment */
1116 /* occurred. This might happen because of (say) an FTP string being changed */
1117 /* and the new string being a different length to the old. */
1118 /* ------------------------------------------------------------------------ */
1120 ipf_proxy_fixseqack(fr_info_t *fin, ip_t *ip, ap_session_t *aps, int inc)
1122 ipf_main_softc_t *softc = fin->fin_main_soft;
1123 ipf_proxy_softc_t *softp = softc->ipf_proxy_soft;
1124 int sel, ch = 0, out, nlen;
1129 tcp = (tcphdr_t *)fin->fin_dp;
1132 * ip_len has already been adjusted by 'inc'.
1134 nlen = fin->fin_dlen;
1135 nlen -= (TCP_OFF(tcp) << 2);
1141 seq1 = (u_32_t)ntohl(tcp->th_seq);
1142 sel = aps->aps_sel[out];
1144 /* switch to other set ? */
1145 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
1146 (seq1 > aps->aps_seqmin[!sel])) {
1147 if (softp->ips_proxy_debug & 0x10)
1148 printf("proxy out switch set seq %d -> %d %x > %x\n",
1150 aps->aps_seqmin[!sel]);
1151 sel = aps->aps_sel[out] = !sel;
1154 if (aps->aps_seqoff[sel]) {
1155 seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
1157 seq2 = aps->aps_seqoff[sel];
1159 tcp->th_seq = htonl(seq1);
1164 if (inc && (seq1 > aps->aps_seqmin[!sel])) {
1165 aps->aps_seqmin[sel] = seq1 + nlen - 1;
1166 aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
1167 if (softp->ips_proxy_debug & 0x10)
1168 printf("proxy seq set %d at %x to %d + %d\n",
1169 sel, aps->aps_seqmin[sel],
1170 aps->aps_seqoff[sel], inc);
1175 seq1 = ntohl(tcp->th_ack);
1176 sel = aps->aps_sel[1 - out];
1178 /* switch to other set ? */
1179 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
1180 (seq1 > aps->aps_ackmin[!sel])) {
1181 if (softp->ips_proxy_debug & 0x10)
1182 printf("proxy out switch set ack %d -> %d %x > %x\n",
1184 aps->aps_ackmin[!sel]);
1185 sel = aps->aps_sel[1 - out] = !sel;
1188 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
1189 seq2 = aps->aps_ackoff[sel];
1190 tcp->th_ack = htonl(seq1 - seq2);
1194 seq1 = ntohl(tcp->th_seq);
1195 sel = aps->aps_sel[out];
1197 /* switch to other set ? */
1198 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
1199 (seq1 > aps->aps_ackmin[!sel])) {
1200 if (softp->ips_proxy_debug & 0x10)
1201 printf("proxy in switch set ack %d -> %d %x > %x\n",
1202 sel, !sel, seq1, aps->aps_ackmin[!sel]);
1203 sel = aps->aps_sel[out] = !sel;
1206 if (aps->aps_ackoff[sel]) {
1207 seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
1209 seq2 = aps->aps_ackoff[sel];
1211 tcp->th_seq = htonl(seq1);
1216 if (inc && (seq1 > aps->aps_ackmin[!sel])) {
1217 aps->aps_ackmin[!sel] = seq1 + nlen - 1;
1218 aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
1220 if (softp->ips_proxy_debug & 0x10)
1221 printf("proxy ack set %d at %x to %d + %d\n",
1222 !sel, aps->aps_seqmin[!sel],
1223 aps->aps_seqoff[sel], inc);
1228 seq1 = ntohl(tcp->th_ack);
1229 sel = aps->aps_sel[1 - out];
1231 /* switch to other set ? */
1232 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
1233 (seq1 > aps->aps_seqmin[!sel])) {
1234 if (softp->ips_proxy_debug & 0x10)
1235 printf("proxy in switch set seq %d -> %d %x > %x\n",
1236 sel, !sel, seq1, aps->aps_seqmin[!sel]);
1237 sel = aps->aps_sel[1 - out] = !sel;
1240 if (aps->aps_seqoff[sel] != 0) {
1241 if (softp->ips_proxy_debug & 0x10)
1242 printf("sel %d seqoff %d seq1 %x seqmin %x\n",
1243 sel, aps->aps_seqoff[sel], seq1,
1244 aps->aps_seqmin[sel]);
1245 if (seq1 > aps->aps_seqmin[sel]) {
1246 seq2 = aps->aps_seqoff[sel];
1247 tcp->th_ack = htonl(seq1 - seq2);
1253 if (softp->ips_proxy_debug & 0x10)
1254 printf("ipf_proxy_fixseqack: seq %u ack %u\n",
1255 (u_32_t)ntohl(tcp->th_seq), (u_32_t)ntohl(tcp->th_ack));
1256 return (ch ? 2 : 0);
1260 /* ------------------------------------------------------------------------ */
1261 /* Function: ipf_proxy_rule_rev */
1262 /* Returns: ipnat_t * - NULL = failure, else pointer to new rule */
1263 /* Parameters: nat(I) - pointer to NAT session to create rule from */
1265 /* This function creates a NAT rule that is based upon the reverse packet */
1266 /* flow associated with this NAT session. Thus if this NAT session was */
1267 /* created with a map rule then this function will create a rdr rule. */
1268 /* Only address fields and network interfaces are assigned in this function */
1269 /* and the address fields are formed such that an exact is required. If the */
1270 /* original rule had a netmask, that is not replicated here not is it */
1271 /* desired. The ultimate goal here is to create a NAT rule to support a NAT */
1272 /* session being created that does not have a user configured rule. The */
1273 /* classic example is supporting the FTP proxy, where a data channel needs */
1274 /* to be setup, based on the addresses used for the control connection. In */
1275 /* that case, this function is used to handle creating NAT rules to support */
1276 /* data connections with the PORT and EPRT commands. */
1277 /* ------------------------------------------------------------------------ */
1279 ipf_proxy_rule_rev(nat_t *nat)
1286 size = old->in_size;
1288 KMALLOCS(ipn, ipnat_t *, size);
1292 bzero((char *)ipn, size);
1298 ipn->in_size = size;
1299 ipn->in_pr[0] = old->in_pr[1];
1300 ipn->in_pr[1] = old->in_pr[0];
1301 ipn->in_v[0] = old->in_v[1];
1302 ipn->in_v[1] = old->in_v[0];
1303 ipn->in_ifps[0] = old->in_ifps[1];
1304 ipn->in_ifps[1] = old->in_ifps[0];
1305 ipn->in_flags = (old->in_flags | IPN_PROXYRULE);
1307 ipn->in_nsrcip6 = nat->nat_odst6;
1308 ipn->in_osrcip6 = nat->nat_ndst6;
1310 if ((old->in_redir & NAT_REDIRECT) != 0) {
1311 ipn->in_redir = NAT_MAP;
1312 if (ipn->in_v[0] == 4) {
1313 ipn->in_snip = ntohl(nat->nat_odstaddr);
1314 ipn->in_dnip = ntohl(nat->nat_nsrcaddr);
1317 ipn->in_snip6 = nat->nat_odst6;
1318 ipn->in_dnip6 = nat->nat_nsrc6;
1321 ipn->in_ndstip6 = nat->nat_nsrc6;
1322 ipn->in_odstip6 = nat->nat_osrc6;
1324 ipn->in_redir = NAT_REDIRECT;
1325 if (ipn->in_v[0] == 4) {
1326 ipn->in_snip = ntohl(nat->nat_odstaddr);
1327 ipn->in_dnip = ntohl(nat->nat_osrcaddr);
1330 ipn->in_snip6 = nat->nat_odst6;
1331 ipn->in_dnip6 = nat->nat_osrc6;
1334 ipn->in_ndstip6 = nat->nat_osrc6;
1335 ipn->in_odstip6 = nat->nat_nsrc6;
1338 IP6_SETONES(&ipn->in_osrcmsk6);
1339 IP6_SETONES(&ipn->in_nsrcmsk6);
1340 IP6_SETONES(&ipn->in_odstmsk6);
1341 IP6_SETONES(&ipn->in_ndstmsk6);
1343 ipn->in_namelen = old->in_namelen;
1344 ipn->in_ifnames[0] = old->in_ifnames[1];
1345 ipn->in_ifnames[1] = old->in_ifnames[0];
1346 bcopy(old->in_names, ipn->in_names, ipn->in_namelen);
1347 MUTEX_INIT(&ipn->in_lock, "ipnat rev rule lock");
1353 /* ------------------------------------------------------------------------ */
1354 /* Function: ipf_proxy_rule_fwd */
1355 /* Returns: ipnat_t * - NULL = failure, else pointer to new rule */
1356 /* Parameters: nat(I) - pointer to NAT session to create rule from */
1358 /* The purpose and rationale of this function is much the same as the above */
1359 /* function, ipf_proxy_rule_rev, except that a rule is created that matches */
1360 /* the same direction as that of the existing NAT session. Thus if this NAT */
1361 /* session was created with a map rule then this function will also create */
1362 /* a data structure to represent a map rule. Whereas ipf_proxy_rule_rev is */
1363 /* used to support PORT/EPRT, this function supports PASV/EPSV. */
1364 /* ------------------------------------------------------------------------ */
1366 ipf_proxy_rule_fwd(nat_t *nat)
1373 size = old->in_size;
1375 KMALLOCS(ipn, ipnat_t *, size);
1379 bzero((char *)ipn, size);
1385 ipn->in_size = size;
1386 ipn->in_pr[0] = old->in_pr[0];
1387 ipn->in_pr[1] = old->in_pr[1];
1388 ipn->in_v[0] = old->in_v[0];
1389 ipn->in_v[1] = old->in_v[1];
1390 ipn->in_ifps[0] = nat->nat_ifps[0];
1391 ipn->in_ifps[1] = nat->nat_ifps[1];
1392 ipn->in_flags = (old->in_flags | IPN_PROXYRULE);
1394 ipn->in_nsrcip6 = nat->nat_nsrc6;
1395 ipn->in_osrcip6 = nat->nat_osrc6;
1396 ipn->in_ndstip6 = nat->nat_ndst6;
1397 ipn->in_odstip6 = nat->nat_odst6;
1398 ipn->in_redir = old->in_redir;
1400 if (ipn->in_v[0] == 4) {
1401 ipn->in_snip = ntohl(nat->nat_nsrcaddr);
1402 ipn->in_dnip = ntohl(nat->nat_ndstaddr);
1405 ipn->in_snip6 = nat->nat_nsrc6;
1406 ipn->in_dnip6 = nat->nat_ndst6;
1410 IP6_SETONES(&ipn->in_osrcmsk6);
1411 IP6_SETONES(&ipn->in_nsrcmsk6);
1412 IP6_SETONES(&ipn->in_odstmsk6);
1413 IP6_SETONES(&ipn->in_ndstmsk6);
1415 ipn->in_namelen = old->in_namelen;
1416 ipn->in_ifnames[0] = old->in_ifnames[0];
1417 ipn->in_ifnames[1] = old->in_ifnames[1];
1418 bcopy(old->in_names, ipn->in_names, ipn->in_namelen);
1419 MUTEX_INIT(&ipn->in_lock, "ipnat fwd rule lock");