12393Syz155240 /* 22393Syz155240 * Copyright (C) 1997-2003 by Darren Reed. 32393Syz155240 * 42393Syz155240 * See the IPFILTER.LICENCE file for details on licencing. 52393Syz155240 * 6*3448Sdh155122 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 72393Syz155240 * Use is subject to license terms. 82393Syz155240 */ 92393Syz155240 102393Syz155240 #pragma ident "%Z%%M% %I% %E% SMI" 112393Syz155240 122393Syz155240 #if defined(KERNEL) || defined(_KERNEL) 132393Syz155240 # undef KERNEL 142393Syz155240 # undef _KERNEL 152393Syz155240 # define KERNEL 1 162393Syz155240 # define _KERNEL 1 172393Syz155240 #endif 182393Syz155240 #include <sys/errno.h> 192393Syz155240 #include <sys/types.h> 202393Syz155240 #include <sys/param.h> 212393Syz155240 #include <sys/time.h> 222393Syz155240 #include <sys/file.h> 232393Syz155240 #if !defined(AIX) 242393Syz155240 # include <sys/fcntl.h> 252393Syz155240 #endif 262393Syz155240 #if !defined(_KERNEL) && !defined(__KERNEL__) 272393Syz155240 # include <stdio.h> 282393Syz155240 # include <string.h> 292393Syz155240 # include <stdlib.h> 302393Syz155240 # include <ctype.h> 312393Syz155240 # define _KERNEL 322393Syz155240 # ifdef __OpenBSD__ 332393Syz155240 struct file; 342393Syz155240 # endif 352393Syz155240 # include <sys/uio.h> 362393Syz155240 # undef _KERNEL 372393Syz155240 #endif 382393Syz155240 #if !defined(linux) 392393Syz155240 # include <sys/protosw.h> 402393Syz155240 #endif 412393Syz155240 #include <sys/socket.h> 422393Syz155240 #if defined(_KERNEL) 432393Syz155240 # if !defined(__NetBSD__) && !defined(sun) && !defined(__osf__) && \ 442393Syz155240 !defined(__OpenBSD__) && !defined(__hpux) && !defined(__sgi) && \ 452393Syz155240 !defined(AIX) 462393Syz155240 # include <sys/ctype.h> 472393Syz155240 # endif 482393Syz155240 # include <sys/systm.h> 492393Syz155240 # if !defined(__SVR4) && !defined(__svr4__) 502393Syz155240 # include <sys/mbuf.h> 512393Syz155240 # endif 522393Syz155240 #endif 532393Syz155240 #if defined(_KERNEL) && (__FreeBSD_version >= 220000) 542393Syz155240 # include <sys/filio.h> 552393Syz155240 # include <sys/fcntl.h> 562393Syz155240 # if (__FreeBSD_version >= 300000) && !defined(IPFILTER_LKM) 572393Syz155240 # include "opt_ipfilter.h" 582393Syz155240 # endif 592393Syz155240 #else 602393Syz155240 # include <sys/ioctl.h> 612393Syz155240 #endif 622393Syz155240 #if defined(__SVR4) || defined(__svr4__) 632393Syz155240 # include <sys/byteorder.h> 642393Syz155240 # ifdef _KERNEL 652393Syz155240 # include <sys/dditypes.h> 662393Syz155240 # endif 672393Syz155240 # include <sys/stream.h> 682393Syz155240 # include <sys/kmem.h> 692393Syz155240 #endif 702393Syz155240 #if __FreeBSD__ > 2 712393Syz155240 # include <sys/queue.h> 722393Syz155240 #endif 732393Syz155240 #include <net/if.h> 742393Syz155240 #ifdef sun 752393Syz155240 # include <net/af.h> 762393Syz155240 #endif 772393Syz155240 #include <net/route.h> 782393Syz155240 #include <netinet/in.h> 792393Syz155240 #include <netinet/in_systm.h> 802393Syz155240 #include <netinet/ip.h> 812393Syz155240 #ifndef linux 822393Syz155240 # include <netinet/ip_var.h> 832393Syz155240 #endif 842393Syz155240 #include <netinet/tcp.h> 852393Syz155240 #include <netinet/udp.h> 862393Syz155240 #include <netinet/ip_icmp.h> 872393Syz155240 #include "netinet/ip_compat.h" 882393Syz155240 #include <netinet/tcpip.h> 89*3448Sdh155122 #include "netinet/ipf_stack.h" 902393Syz155240 #include "netinet/ip_fil.h" 912393Syz155240 #include "netinet/ip_nat.h" 922393Syz155240 #include "netinet/ip_state.h" 932393Syz155240 #include "netinet/ip_proxy.h" 942393Syz155240 #if (__FreeBSD_version >= 300000) 952393Syz155240 # include <sys/malloc.h> 962393Syz155240 #endif 972393Syz155240 982393Syz155240 #include "netinet/ip_ftp_pxy.c" 992393Syz155240 #include "netinet/ip_rcmd_pxy.c" 1002393Syz155240 # include "netinet/ip_pptp_pxy.c" 1012393Syz155240 #if defined(_KERNEL) 1022393Syz155240 # include "netinet/ip_irc_pxy.c" 1032393Syz155240 # include "netinet/ip_raudio_pxy.c" 1042393Syz155240 # include "netinet/ip_h323_pxy.c" 1052393Syz155240 # include "netinet/ip_netbios_pxy.c" 1062393Syz155240 #endif 1072393Syz155240 #include "netinet/ip_ipsec_pxy.c" 1082393Syz155240 #include "netinet/ip_rpcb_pxy.c" 1092393Syz155240 1102393Syz155240 /* END OF INCLUDES */ 1112393Syz155240 1122393Syz155240 #if !defined(lint) 1132393Syz155240 static const char rcsid[] = "@(#)$Id: ip_proxy.c,v 2.62.2.14 2005/06/18 02:41:33 darrenr Exp $"; 1142393Syz155240 #endif 1152393Syz155240 1162393Syz155240 static int appr_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int )); 1172393Syz155240 1182393Syz155240 #define AP_SESS_SIZE 53 1192393Syz155240 1202393Syz155240 #if defined(_KERNEL) 1212393Syz155240 int ipf_proxy_debug = 0; 1222393Syz155240 #else 1232393Syz155240 int ipf_proxy_debug = 2; 1242393Syz155240 #endif 125*3448Sdh155122 126*3448Sdh155122 static aproxy_t lcl_ap_proxies[] = { 1272393Syz155240 #ifdef IPF_FTP_PROXY 128*3448Sdh155122 { NULL, "ftp", (char)IPPROTO_TCP, 0, 0, NULL, ippr_ftp_init, ippr_ftp_fini, 1292393Syz155240 ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL }, 1302393Syz155240 #endif 1312393Syz155240 #ifdef IPF_IRC_PROXY 132*3448Sdh155122 { NULL, "irc", (char)IPPROTO_TCP, 0, 0, NULL, ippr_irc_init, ippr_irc_fini, 1332393Syz155240 ippr_irc_new, NULL, NULL, ippr_irc_out, NULL, NULL }, 1342393Syz155240 #endif 1352393Syz155240 #ifdef IPF_RCMD_PROXY 136*3448Sdh155122 { NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, NULL, ippr_rcmd_init, ippr_rcmd_fini, 1372393Syz155240 ippr_rcmd_new, NULL, ippr_rcmd_in, ippr_rcmd_out, NULL, NULL }, 1382393Syz155240 #endif 1392393Syz155240 #ifdef IPF_RAUDIO_PROXY 140*3448Sdh155122 { NULL, "raudio", (char)IPPROTO_TCP, 0, 0, NULL, ippr_raudio_init, ippr_raudio_fini, 1412393Syz155240 ippr_raudio_new, NULL, ippr_raudio_in, ippr_raudio_out, NULL, NULL }, 1422393Syz155240 #endif 1432393Syz155240 #ifdef IPF_MSNRPC_PROXY 144*3448Sdh155122 { NULL, "msnrpc", (char)IPPROTO_TCP, 0, 0, NULL, ippr_msnrpc_init, ippr_msnrpc_fini, 1452393Syz155240 ippr_msnrpc_new, NULL, ippr_msnrpc_in, ippr_msnrpc_out, NULL, NULL }, 1462393Syz155240 #endif 1472393Syz155240 #ifdef IPF_NETBIOS_PROXY 148*3448Sdh155122 { NULL, "netbios", (char)IPPROTO_UDP, 0, 0, NULL, ippr_netbios_init, ippr_netbios_fini, 1492393Syz155240 NULL, NULL, NULL, ippr_netbios_out, NULL, NULL }, 1502393Syz155240 #endif 1512393Syz155240 #ifdef IPF_IPSEC_PROXY 152*3448Sdh155122 { NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, NULL, 1532393Syz155240 ippr_ipsec_init, ippr_ipsec_fini, ippr_ipsec_new, ippr_ipsec_del, 1542393Syz155240 ippr_ipsec_inout, ippr_ipsec_inout, ippr_ipsec_match, NULL }, 1552393Syz155240 #endif 1562393Syz155240 #ifdef IPF_PPTP_PROXY 157*3448Sdh155122 { NULL, "pptp", (char)IPPROTO_TCP, 0, 0, NULL, 1582393Syz155240 ippr_pptp_init, ippr_pptp_fini, ippr_pptp_new, ippr_pptp_del, 1592393Syz155240 ippr_pptp_inout, ippr_pptp_inout, NULL, NULL }, 1602393Syz155240 #endif 1612393Syz155240 #ifdef IPF_H323_PROXY 162*3448Sdh155122 { NULL, "h323", (char)IPPROTO_TCP, 0, 0, NULL, ippr_h323_init, ippr_h323_fini, 1632393Syz155240 ippr_h323_new, ippr_h323_del, ippr_h323_in, NULL, NULL }, 164*3448Sdh155122 { NULL, "h245", (char)IPPROTO_TCP, 0, 0, NULL, NULL, NULL, 1652393Syz155240 ippr_h245_new, NULL, NULL, ippr_h245_out, NULL }, 1662393Syz155240 #endif 1672393Syz155240 #ifdef IPF_RPCB_PROXY 1682393Syz155240 # if 0 169*3448Sdh155122 { NULL, "rpcbt", (char)IPPROTO_TCP, 0, 0, NULL, 1702393Syz155240 ippr_rpcb_init, ippr_rpcb_fini, ippr_rpcb_new, ippr_rpcb_del, 1712393Syz155240 ippr_rpcb_in, ippr_rpcb_out, NULL, NULL }, 1722393Syz155240 # endif 173*3448Sdh155122 { NULL, "rpcbu", (char)IPPROTO_UDP, 0, 0, NULL, 1742393Syz155240 ippr_rpcb_init, ippr_rpcb_fini, ippr_rpcb_new, ippr_rpcb_del, 1752393Syz155240 ippr_rpcb_in, ippr_rpcb_out, NULL, NULL }, 1762393Syz155240 #endif 177*3448Sdh155122 { NULL, "", '\0', 0, 0, NULL, NULL, NULL, NULL, NULL } 1782393Syz155240 }; 1792393Syz155240 1802393Syz155240 /* 1812393Syz155240 * Dynamically add a new kernel proxy. Ensure that it is unique in the 1822393Syz155240 * collection compiled in and dynamically added. 1832393Syz155240 */ 184*3448Sdh155122 int appr_add(ap, ifs) 1852393Syz155240 aproxy_t *ap; 186*3448Sdh155122 ipf_stack_t *ifs; 1872393Syz155240 { 1882393Syz155240 aproxy_t *a; 1892393Syz155240 190*3448Sdh155122 for (a = ifs->ifs_ap_proxies; a->apr_p; a++) 1912393Syz155240 if ((a->apr_p == ap->apr_p) && 1922393Syz155240 !strncmp(a->apr_label, ap->apr_label, 1932393Syz155240 sizeof(ap->apr_label))) { 1942393Syz155240 if (ipf_proxy_debug > 1) 1952393Syz155240 printf("appr_add: %s/%d already present (B)\n", 1962393Syz155240 a->apr_label, a->apr_p); 1972393Syz155240 return -1; 1982393Syz155240 } 1992393Syz155240 200*3448Sdh155122 for (a = ifs->ifs_ap_proxylist; a->apr_p; a = a->apr_next) 2012393Syz155240 if ((a->apr_p == ap->apr_p) && 2022393Syz155240 !strncmp(a->apr_label, ap->apr_label, 2032393Syz155240 sizeof(ap->apr_label))) { 2042393Syz155240 if (ipf_proxy_debug > 1) 2052393Syz155240 printf("appr_add: %s/%d already present (D)\n", 2062393Syz155240 a->apr_label, a->apr_p); 2072393Syz155240 return -1; 2082393Syz155240 } 209*3448Sdh155122 ap->apr_next = ifs->ifs_ap_proxylist; 210*3448Sdh155122 ifs->ifs_ap_proxylist = ap; 2112393Syz155240 if (ap->apr_init != NULL) 212*3448Sdh155122 return (*ap->apr_init)(&ap->apr_private, ifs); 213*3448Sdh155122 2142393Syz155240 return 0; 2152393Syz155240 } 2162393Syz155240 2172393Syz155240 2182393Syz155240 /* 2192393Syz155240 * Check to see if the proxy this control request has come through for 2202393Syz155240 * exists, and if it does and it has a control function then invoke that 2212393Syz155240 * control function. 2222393Syz155240 */ 223*3448Sdh155122 int appr_ctl(ctl, ifs) 2242393Syz155240 ap_ctl_t *ctl; 225*3448Sdh155122 ipf_stack_t *ifs; 2262393Syz155240 { 2272393Syz155240 aproxy_t *a; 2282393Syz155240 int error; 2292393Syz155240 230*3448Sdh155122 a = appr_lookup(ctl->apc_p, ctl->apc_label, ifs); 2312393Syz155240 if (a == NULL) { 2322393Syz155240 if (ipf_proxy_debug > 1) 2332393Syz155240 printf("appr_ctl: can't find %s/%d\n", 2342393Syz155240 ctl->apc_label, ctl->apc_p); 2352393Syz155240 error = ESRCH; 2362393Syz155240 } else if (a->apr_ctl == NULL) { 2372393Syz155240 if (ipf_proxy_debug > 1) 2382393Syz155240 printf("appr_ctl: no ctl function for %s/%d\n", 2392393Syz155240 ctl->apc_label, ctl->apc_p); 2402393Syz155240 error = ENXIO; 2412393Syz155240 } else { 242*3448Sdh155122 error = (*a->apr_ctl)(a, ctl, a->apr_private); 2432393Syz155240 if ((error != 0) && (ipf_proxy_debug > 1)) 2442393Syz155240 printf("appr_ctl: %s/%d ctl error %d\n", 2452393Syz155240 a->apr_label, a->apr_p, error); 2462393Syz155240 } 2472393Syz155240 return error; 2482393Syz155240 } 2492393Syz155240 2502393Syz155240 2512393Syz155240 /* 2522393Syz155240 * Delete a proxy that has been added dynamically from those available. 2532393Syz155240 * If it is in use, return 1 (do not destroy NOW), not in use 0 or -1 2542393Syz155240 * if it cannot be matched. 2552393Syz155240 */ 256*3448Sdh155122 int appr_del(ap, ifs) 2572393Syz155240 aproxy_t *ap; 258*3448Sdh155122 ipf_stack_t *ifs; 2592393Syz155240 { 2602393Syz155240 aproxy_t *a, **app; 2612393Syz155240 262*3448Sdh155122 for (app = &ifs->ifs_ap_proxylist; ((a = *app) != NULL); 263*3448Sdh155122 app = &a->apr_next) 2642393Syz155240 if (a == ap) { 2652393Syz155240 a->apr_flags |= APR_DELETE; 2662393Syz155240 *app = a->apr_next; 2672393Syz155240 if (ap->apr_ref != 0) { 2682393Syz155240 if (ipf_proxy_debug > 2) 2692393Syz155240 printf("appr_del: orphaning %s/%d\n", 2702393Syz155240 ap->apr_label, ap->apr_p); 2712393Syz155240 return 1; 2722393Syz155240 } 2732393Syz155240 return 0; 2742393Syz155240 } 2752393Syz155240 if (ipf_proxy_debug > 1) 2762393Syz155240 printf("appr_del: proxy %lx not found\n", (u_long)ap); 2772393Syz155240 return -1; 2782393Syz155240 } 2792393Syz155240 2802393Syz155240 2812393Syz155240 /* 2822393Syz155240 * Return 1 if the packet is a good match against a proxy, else 0. 2832393Syz155240 */ 2842393Syz155240 int appr_ok(fin, tcp, nat) 2852393Syz155240 fr_info_t *fin; 2862393Syz155240 tcphdr_t *tcp; 2872393Syz155240 ipnat_t *nat; 2882393Syz155240 { 2892393Syz155240 aproxy_t *apr = nat->in_apr; 2902393Syz155240 u_short dport = nat->in_dport; 2912393Syz155240 2922393Syz155240 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) || 2932393Syz155240 (fin->fin_p != apr->apr_p)) 2942393Syz155240 return 0; 2952393Syz155240 if ((tcp == NULL) && dport) 2962393Syz155240 return 0; 2972393Syz155240 return 1; 2982393Syz155240 } 2992393Syz155240 3002393Syz155240 301*3448Sdh155122 int appr_ioctl(data, cmd, mode, ifs) 3022393Syz155240 caddr_t data; 3032393Syz155240 ioctlcmd_t cmd; 3042393Syz155240 int mode; 305*3448Sdh155122 ipf_stack_t *ifs; 3062393Syz155240 { 3072393Syz155240 ap_ctl_t ctl; 3082393Syz155240 caddr_t ptr; 3092393Syz155240 int error; 3102393Syz155240 3112393Syz155240 mode = mode; /* LINT */ 3122393Syz155240 3132393Syz155240 switch (cmd) 3142393Syz155240 { 3152393Syz155240 case SIOCPROXY : 3162393Syz155240 (void) BCOPYIN(data, &ctl, sizeof(ctl)); 3172393Syz155240 ptr = NULL; 3182393Syz155240 3192393Syz155240 if (ctl.apc_dsize > 0) { 3202393Syz155240 KMALLOCS(ptr, caddr_t, ctl.apc_dsize); 3212393Syz155240 if (ptr == NULL) 3222393Syz155240 error = ENOMEM; 3232393Syz155240 else { 3242393Syz155240 error = copyinptr(ctl.apc_data, ptr, 3252393Syz155240 ctl.apc_dsize); 3262393Syz155240 if (error == 0) 3272393Syz155240 ctl.apc_data = ptr; 3282393Syz155240 } 3292393Syz155240 } else { 3302393Syz155240 ctl.apc_data = NULL; 3312393Syz155240 error = 0; 3322393Syz155240 } 3332393Syz155240 3342393Syz155240 if (error == 0) 335*3448Sdh155122 error = appr_ctl(&ctl, ifs); 3362393Syz155240 3372393Syz155240 if ((ctl.apc_dsize > 0) && (ptr != NULL) && 3382393Syz155240 (ctl.apc_data == ptr)) { 3392393Syz155240 KFREES(ptr, ctl.apc_dsize); 3402393Syz155240 } 3412393Syz155240 break; 3422393Syz155240 3432393Syz155240 default : 3442393Syz155240 error = EINVAL; 3452393Syz155240 } 3462393Syz155240 return error; 3472393Syz155240 } 3482393Syz155240 3492393Syz155240 3502393Syz155240 /* 3512393Syz155240 * If a proxy has a match function, call that to do extended packet 3522393Syz155240 * matching. 3532393Syz155240 */ 3542393Syz155240 int appr_match(fin, nat) 3552393Syz155240 fr_info_t *fin; 3562393Syz155240 nat_t *nat; 3572393Syz155240 { 3582393Syz155240 aproxy_t *apr; 3592393Syz155240 ipnat_t *ipn; 3602393Syz155240 int result; 3612393Syz155240 3622393Syz155240 ipn = nat->nat_ptr; 3632393Syz155240 if (ipf_proxy_debug > 8) 3642393Syz155240 printf("appr_match(%lx,%lx) aps %lx ptr %lx\n", 3652393Syz155240 (u_long)fin, (u_long)nat, (u_long)nat->nat_aps, 3662393Syz155240 (u_long)ipn); 3672393Syz155240 3682393Syz155240 if ((fin->fin_flx & (FI_SHORT|FI_BAD)) != 0) { 3692393Syz155240 if (ipf_proxy_debug > 0) 3702393Syz155240 printf("appr_match: flx 0x%x (BAD|SHORT)\n", 3712393Syz155240 fin->fin_flx); 3722393Syz155240 return -1; 3732393Syz155240 } 3742393Syz155240 3752393Syz155240 apr = ipn->in_apr; 3762393Syz155240 if ((apr == NULL) || (apr->apr_flags & APR_DELETE)) { 3772393Syz155240 if (ipf_proxy_debug > 0) 3782393Syz155240 printf("appr_match:apr %lx apr_flags 0x%x\n", 3792393Syz155240 (u_long)apr, apr ? apr->apr_flags : 0); 3802393Syz155240 return -1; 3812393Syz155240 } 3822393Syz155240 3832393Syz155240 if (apr->apr_match != NULL) { 384*3448Sdh155122 result = (*apr->apr_match)(fin, nat->nat_aps, nat, apr->apr_private); 3852393Syz155240 if (result != 0) { 3862393Syz155240 if (ipf_proxy_debug > 4) 3872393Syz155240 printf("appr_match: result %d\n", result); 3882393Syz155240 return -1; 3892393Syz155240 } 3902393Syz155240 } 3912393Syz155240 return 0; 3922393Syz155240 } 3932393Syz155240 3942393Syz155240 3952393Syz155240 /* 3962393Syz155240 * Allocate a new application proxy structure and fill it in with the 3972393Syz155240 * relevant details. call the init function once complete, prior to 3982393Syz155240 * returning. 3992393Syz155240 */ 4002393Syz155240 int appr_new(fin, nat) 4012393Syz155240 fr_info_t *fin; 4022393Syz155240 nat_t *nat; 4032393Syz155240 { 4042393Syz155240 register ap_session_t *aps; 4052393Syz155240 aproxy_t *apr; 406*3448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 4072393Syz155240 4082393Syz155240 if (ipf_proxy_debug > 8) 4092393Syz155240 printf("appr_new(%lx,%lx) \n", (u_long)fin, (u_long)nat); 4102393Syz155240 4112393Syz155240 if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL)) { 4122393Syz155240 if (ipf_proxy_debug > 0) 4132393Syz155240 printf("appr_new: nat_ptr %lx nat_aps %lx\n", 4142393Syz155240 (u_long)nat->nat_ptr, (u_long)nat->nat_aps); 4152393Syz155240 return -1; 4162393Syz155240 } 4172393Syz155240 4182393Syz155240 apr = nat->nat_ptr->in_apr; 4192393Syz155240 4202393Syz155240 if ((apr->apr_flags & APR_DELETE) || 4212393Syz155240 (fin->fin_p != apr->apr_p)) { 4222393Syz155240 if (ipf_proxy_debug > 2) 4232393Syz155240 printf("appr_new: apr_flags 0x%x p %d/%d\n", 4242393Syz155240 apr->apr_flags, fin->fin_p, apr->apr_p); 4252393Syz155240 return -1; 4262393Syz155240 } 4272393Syz155240 4282393Syz155240 KMALLOC(aps, ap_session_t *); 4292393Syz155240 if (!aps) { 4302393Syz155240 if (ipf_proxy_debug > 0) 4312393Syz155240 printf("appr_new: malloc failed (%lu)\n", 4322393Syz155240 (u_long)sizeof(ap_session_t)); 4332393Syz155240 return -1; 4342393Syz155240 } 4352393Syz155240 4362393Syz155240 bzero((char *)aps, sizeof(*aps)); 4372393Syz155240 aps->aps_p = fin->fin_p; 4382393Syz155240 aps->aps_data = NULL; 4392393Syz155240 aps->aps_apr = apr; 4402393Syz155240 aps->aps_psiz = 0; 4412393Syz155240 if (apr->apr_new != NULL) 442*3448Sdh155122 if ((*apr->apr_new)(fin, aps, nat, apr->apr_private) == -1) { 4432393Syz155240 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) { 4442393Syz155240 KFREES(aps->aps_data, aps->aps_psiz); 4452393Syz155240 } 4462393Syz155240 KFREE(aps); 4472393Syz155240 if (ipf_proxy_debug > 2) 4482393Syz155240 printf("appr_new: new(%lx) failed\n", 4492393Syz155240 (u_long)apr->apr_new); 4502393Syz155240 return -1; 4512393Syz155240 } 4522393Syz155240 aps->aps_nat = nat; 453*3448Sdh155122 aps->aps_next = ifs->ifs_ap_sess_list; 454*3448Sdh155122 ifs->ifs_ap_sess_list = aps; 4552393Syz155240 nat->nat_aps = aps; 4562393Syz155240 4572393Syz155240 return 0; 4582393Syz155240 } 4592393Syz155240 4602393Syz155240 4612393Syz155240 /* 4622393Syz155240 * Check to see if a packet should be passed through an active proxy routine 4632393Syz155240 * if one has been setup for it. We don't need to check the checksum here if 4642393Syz155240 * IPFILTER_CKSUM is defined because if it is, a failed check causes FI_BAD 4652393Syz155240 * to be set. 4662393Syz155240 */ 4672393Syz155240 int appr_check(fin, nat) 4682393Syz155240 fr_info_t *fin; 4692393Syz155240 nat_t *nat; 4702393Syz155240 { 4712958Sdr146992 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_VALID) 4722393Syz155240 mb_t *m; 4732393Syz155240 int dosum = 1; 4742393Syz155240 #endif 4752393Syz155240 tcphdr_t *tcp = NULL; 4762393Syz155240 udphdr_t *udp = NULL; 4772393Syz155240 ap_session_t *aps; 4782393Syz155240 aproxy_t *apr; 4792393Syz155240 ip_t *ip; 4802393Syz155240 short rv; 4812393Syz155240 int err; 4822393Syz155240 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) 4832393Syz155240 u_32_t s1, s2, sd; 4842393Syz155240 #endif 485*3448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs; 486*3448Sdh155122 4872958Sdr146992 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) 4882958Sdr146992 net_data_t net_data_p; 4892958Sdr146992 if (fin->fin_v == 4) 490*3448Sdh155122 net_data_p = ifs->ifs_ipf_ipv4; 4912958Sdr146992 else 492*3448Sdh155122 net_data_p = ifs->ifs_ipf_ipv6; 4932958Sdr146992 #endif 4942393Syz155240 4952393Syz155240 if (fin->fin_flx & FI_BAD) { 4962393Syz155240 if (ipf_proxy_debug > 0) 4972393Syz155240 printf("appr_check: flx 0x%x (BAD)\n", fin->fin_flx); 4982393Syz155240 return -1; 4992393Syz155240 } 5002393Syz155240 5012393Syz155240 #ifndef IPFILTER_CKSUM 5022393Syz155240 if ((fin->fin_out == 0) && (fr_checkl4sum(fin) == -1)) { 5032393Syz155240 if (ipf_proxy_debug > 0) 5042393Syz155240 printf("appr_check: l4 checksum failure %d\n", 5052393Syz155240 fin->fin_p); 5062393Syz155240 if (fin->fin_p == IPPROTO_TCP) 507*3448Sdh155122 ifs->ifs_frstats[fin->fin_out].fr_tcpbad++; 5082393Syz155240 return -1; 5092393Syz155240 } 5102393Syz155240 #endif 5112393Syz155240 5122393Syz155240 aps = nat->nat_aps; 5132393Syz155240 if ((aps != NULL) && (aps->aps_p == fin->fin_p)) { 5142393Syz155240 /* 5152393Syz155240 * If there is data in this packet to be proxied then try and 5162393Syz155240 * get it all into the one buffer, else drop it. 5172393Syz155240 */ 5182393Syz155240 #if defined(MENTAT) || defined(HAVE_M_PULLDOWN) 5192393Syz155240 if ((fin->fin_dlen > 0) && !(fin->fin_flx & FI_COALESCE)) 5202393Syz155240 if (fr_coalesce(fin) == -1) { 5212393Syz155240 if (ipf_proxy_debug > 0) 5222393Syz155240 printf("appr_check: fr_coalesce failed %x\n", fin->fin_flx); 5232393Syz155240 return -1; 5242393Syz155240 } 5252393Syz155240 #endif 5262393Syz155240 ip = fin->fin_ip; 5272393Syz155240 5282393Syz155240 switch (fin->fin_p) 5292393Syz155240 { 5302393Syz155240 case IPPROTO_TCP : 5312393Syz155240 tcp = (tcphdr_t *)fin->fin_dp; 5322393Syz155240 5332393Syz155240 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_VALID) 5342393Syz155240 m = fin->fin_qfm; 5352393Syz155240 if (dohwcksum && (m->b_ick_flag == ICK_VALID)) 5362393Syz155240 dosum = 0; 5372393Syz155240 #endif 5382393Syz155240 /* 5392393Syz155240 * Don't bother the proxy with these...or in fact, 5402393Syz155240 * should we free up proxy stuff when seen? 5412393Syz155240 */ 5422393Syz155240 if ((fin->fin_tcpf & TH_RST) != 0) 5432393Syz155240 break; 5442393Syz155240 /*FALLTHROUGH*/ 5452393Syz155240 case IPPROTO_UDP : 5462393Syz155240 udp = (udphdr_t *)fin->fin_dp; 5472393Syz155240 break; 5482393Syz155240 default : 5492393Syz155240 break; 5502393Syz155240 } 5512393Syz155240 5522393Syz155240 apr = aps->aps_apr; 5532393Syz155240 err = 0; 5542393Syz155240 if (fin->fin_out != 0) { 5552393Syz155240 if (apr->apr_outpkt != NULL) 556*3448Sdh155122 err = (*apr->apr_outpkt)(fin, aps, nat, apr->apr_private); 5572393Syz155240 } else { 5582393Syz155240 if (apr->apr_inpkt != NULL) 559*3448Sdh155122 err = (*apr->apr_inpkt)(fin, aps, nat, apr->apr_private); 5602393Syz155240 } 5612393Syz155240 5622393Syz155240 rv = APR_EXIT(err); 5632393Syz155240 if (((ipf_proxy_debug > 0) && (rv != 0)) || 5642393Syz155240 (ipf_proxy_debug > 8)) 5652393Syz155240 printf("appr_check: out %d err %x rv %d\n", 5662393Syz155240 fin->fin_out, err, rv); 5672393Syz155240 if (rv == 1) 5682393Syz155240 return -1; 5692393Syz155240 5702393Syz155240 if (rv == 2) { 5712393Syz155240 appr_free(apr); 5722393Syz155240 nat->nat_aps = NULL; 5732393Syz155240 return -1; 5742393Syz155240 } 5752393Syz155240 5762393Syz155240 /* 5772393Syz155240 * If err != 0 then the data size of the packet has changed 5782393Syz155240 * so we need to recalculate the header checksums for the 5792393Syz155240 * packet. 5802958Sdr146992 * inbound packets always need to be adjusted. 5812393Syz155240 */ 5822393Syz155240 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) 5832958Sdr146992 if (err != 0 && (!fin->fin_out || 5842958Sdr146992 !NET_IS_HCK_L3_FULL(net_data_p, fin->fin_m))) { 5852393Syz155240 short adjlen = err & 0xffff; 5862393Syz155240 5872393Syz155240 s1 = LONG_SUM(ip->ip_len - adjlen); 5882393Syz155240 s2 = LONG_SUM(ip->ip_len); 5892393Syz155240 CALC_SUMD(s1, s2, sd); 5902958Sdr146992 fix_outcksum(&ip->ip_sum, sd); 5912393Syz155240 } 5922393Syz155240 #endif 5932393Syz155240 5942393Syz155240 /* 5952393Syz155240 * For TCP packets, we may need to adjust the sequence and 5962393Syz155240 * acknowledgement numbers to reflect changes in size of the 5972393Syz155240 * data stream. 5982393Syz155240 * 5992393Syz155240 * For both TCP and UDP, recalculate the layer 4 checksum, 6002393Syz155240 * regardless, as we can't tell (here) if data has been 6012393Syz155240 * changed or not. 6022393Syz155240 */ 6032393Syz155240 if (tcp != NULL) { 6042393Syz155240 err = appr_fixseqack(fin, ip, aps, APR_INC(err)); 6052393Syz155240 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) 6062958Sdr146992 if (!fin->fin_out || 6072958Sdr146992 !NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m) && 6082958Sdr146992 !NET_IS_HCK_L4_PART(net_data_p, fin->fin_m)) 6092393Syz155240 tcp->th_sum = fr_cksum(fin->fin_qfm, ip, 6102393Syz155240 IPPROTO_TCP, tcp); 6112393Syz155240 #else 6122393Syz155240 tcp->th_sum = fr_cksum(fin->fin_m, ip, 6132393Syz155240 IPPROTO_TCP, tcp); 6142393Syz155240 #endif 6152393Syz155240 } else if ((udp != NULL) && (udp->uh_sum != 0)) { 6162393Syz155240 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) 6172958Sdr146992 if (!fin->fin_out || 6182958Sdr146992 !NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m) && 6192958Sdr146992 !NET_IS_HCK_L4_PART(net_data_p, fin->fin_m)) 6202393Syz155240 udp->uh_sum = fr_cksum(fin->fin_qfm, ip, 6212393Syz155240 IPPROTO_UDP, udp); 6222393Syz155240 #else 6232393Syz155240 udp->uh_sum = fr_cksum(fin->fin_m, ip, 6242393Syz155240 IPPROTO_UDP, udp); 6252393Syz155240 #endif 6262393Syz155240 } 6272393Syz155240 aps->aps_bytes += fin->fin_plen; 6282393Syz155240 aps->aps_pkts++; 6292393Syz155240 return 1; 6302393Syz155240 } 6312393Syz155240 return 0; 6322393Syz155240 } 6332393Syz155240 6342393Syz155240 6352393Syz155240 /* 6362393Syz155240 * Search for an proxy by the protocol it is being used with and its name. 6372393Syz155240 */ 638*3448Sdh155122 aproxy_t *appr_lookup(pr, name, ifs) 6392393Syz155240 u_int pr; 6402393Syz155240 char *name; 641*3448Sdh155122 ipf_stack_t *ifs; 6422393Syz155240 { 6432393Syz155240 aproxy_t *ap; 6442393Syz155240 6452393Syz155240 if (ipf_proxy_debug > 8) 6462393Syz155240 printf("appr_lookup(%d,%s)\n", pr, name); 6472393Syz155240 648*3448Sdh155122 for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++) 6492393Syz155240 if ((ap->apr_p == pr) && 6502393Syz155240 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) { 6512393Syz155240 ap->apr_ref++; 6522393Syz155240 return ap; 6532393Syz155240 } 6542393Syz155240 655*3448Sdh155122 for (ap = ifs->ifs_ap_proxylist; ap; ap = ap->apr_next) 6562393Syz155240 if ((ap->apr_p == pr) && 6572393Syz155240 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) { 6582393Syz155240 ap->apr_ref++; 6592393Syz155240 return ap; 6602393Syz155240 } 6612393Syz155240 if (ipf_proxy_debug > 2) 6622393Syz155240 printf("appr_lookup: failed for %d/%s\n", pr, name); 6632393Syz155240 return NULL; 6642393Syz155240 } 6652393Syz155240 6662393Syz155240 6672393Syz155240 void appr_free(ap) 6682393Syz155240 aproxy_t *ap; 6692393Syz155240 { 6702393Syz155240 ap->apr_ref--; 6712393Syz155240 } 6722393Syz155240 6732393Syz155240 674*3448Sdh155122 void aps_free(aps, ifs) 6752393Syz155240 ap_session_t *aps; 676*3448Sdh155122 ipf_stack_t *ifs; 6772393Syz155240 { 6782393Syz155240 ap_session_t *a, **ap; 6792393Syz155240 aproxy_t *apr; 6802393Syz155240 6812393Syz155240 if (!aps) 6822393Syz155240 return; 6832393Syz155240 684*3448Sdh155122 for (ap = &ifs->ifs_ap_sess_list; ((a = *ap) != NULL); ap = &a->aps_next) 6852393Syz155240 if (a == aps) { 6862393Syz155240 *ap = a->aps_next; 6872393Syz155240 break; 6882393Syz155240 } 6892393Syz155240 6902393Syz155240 apr = aps->aps_apr; 6912393Syz155240 if ((apr != NULL) && (apr->apr_del != NULL)) 692*3448Sdh155122 (*apr->apr_del)(aps, apr->apr_private, ifs); 6932393Syz155240 6942393Syz155240 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) 6952393Syz155240 KFREES(aps->aps_data, aps->aps_psiz); 6962393Syz155240 KFREE(aps); 6972393Syz155240 } 6982393Syz155240 6992393Syz155240 7002393Syz155240 /* 7012393Syz155240 * returns 2 if ack or seq number in TCP header is changed, returns 0 otherwise 7022393Syz155240 */ 7032393Syz155240 static int appr_fixseqack(fin, ip, aps, inc) 7042393Syz155240 fr_info_t *fin; 7052393Syz155240 ip_t *ip; 7062393Syz155240 ap_session_t *aps; 7072393Syz155240 int inc; 7082393Syz155240 { 7092393Syz155240 int sel, ch = 0, out, nlen; 7102393Syz155240 u_32_t seq1, seq2; 7112393Syz155240 tcphdr_t *tcp; 7122393Syz155240 short inc2; 7132393Syz155240 7142393Syz155240 tcp = (tcphdr_t *)fin->fin_dp; 7152393Syz155240 out = fin->fin_out; 7162393Syz155240 /* 7172393Syz155240 * ip_len has already been adjusted by 'inc'. 7182393Syz155240 */ 7192393Syz155240 nlen = ip->ip_len; 7202393Syz155240 nlen -= (IP_HL(ip) << 2) + (TCP_OFF(tcp) << 2); 7212393Syz155240 7222393Syz155240 inc2 = inc; 7232393Syz155240 inc = (int)inc2; 7242393Syz155240 7252393Syz155240 if (out != 0) { 7262393Syz155240 seq1 = (u_32_t)ntohl(tcp->th_seq); 7272393Syz155240 sel = aps->aps_sel[out]; 7282393Syz155240 7292393Syz155240 /* switch to other set ? */ 7302393Syz155240 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) && 7312393Syz155240 (seq1 > aps->aps_seqmin[!sel])) { 7322393Syz155240 if (ipf_proxy_debug > 7) 7332393Syz155240 printf("proxy out switch set seq %d -> %d %x > %x\n", 7342393Syz155240 sel, !sel, seq1, 7352393Syz155240 aps->aps_seqmin[!sel]); 7362393Syz155240 sel = aps->aps_sel[out] = !sel; 7372393Syz155240 } 7382393Syz155240 7392393Syz155240 if (aps->aps_seqoff[sel]) { 7402393Syz155240 seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel]; 7412393Syz155240 if (seq1 > seq2) { 7422393Syz155240 seq2 = aps->aps_seqoff[sel]; 7432393Syz155240 seq1 += seq2; 7442393Syz155240 tcp->th_seq = htonl(seq1); 7452393Syz155240 ch = 1; 7462393Syz155240 } 7472393Syz155240 } 7482393Syz155240 7492393Syz155240 if (inc && (seq1 > aps->aps_seqmin[!sel])) { 7502393Syz155240 aps->aps_seqmin[sel] = seq1 + nlen - 1; 7512393Syz155240 aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc; 7522393Syz155240 if (ipf_proxy_debug > 7) 7532393Syz155240 printf("proxy seq set %d at %x to %d + %d\n", 7542393Syz155240 sel, aps->aps_seqmin[sel], 7552393Syz155240 aps->aps_seqoff[sel], inc); 7562393Syz155240 } 7572393Syz155240 7582393Syz155240 /***/ 7592393Syz155240 7602393Syz155240 seq1 = ntohl(tcp->th_ack); 7612393Syz155240 sel = aps->aps_sel[1 - out]; 7622393Syz155240 7632393Syz155240 /* switch to other set ? */ 7642393Syz155240 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) && 7652393Syz155240 (seq1 > aps->aps_ackmin[!sel])) { 7662393Syz155240 if (ipf_proxy_debug > 7) 7672393Syz155240 printf("proxy out switch set ack %d -> %d %x > %x\n", 7682393Syz155240 sel, !sel, seq1, 7692393Syz155240 aps->aps_ackmin[!sel]); 7702393Syz155240 sel = aps->aps_sel[1 - out] = !sel; 7712393Syz155240 } 7722393Syz155240 7732393Syz155240 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) { 7742393Syz155240 seq2 = aps->aps_ackoff[sel]; 7752393Syz155240 tcp->th_ack = htonl(seq1 - seq2); 7762393Syz155240 ch = 1; 7772393Syz155240 } 7782393Syz155240 } else { 7792393Syz155240 seq1 = ntohl(tcp->th_seq); 7802393Syz155240 sel = aps->aps_sel[out]; 7812393Syz155240 7822393Syz155240 /* switch to other set ? */ 7832393Syz155240 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) && 7842393Syz155240 (seq1 > aps->aps_ackmin[!sel])) { 7852393Syz155240 if (ipf_proxy_debug > 7) 7862393Syz155240 printf("proxy in switch set ack %d -> %d %x > %x\n", 7872393Syz155240 sel, !sel, seq1, aps->aps_ackmin[!sel]); 7882393Syz155240 sel = aps->aps_sel[out] = !sel; 7892393Syz155240 } 7902393Syz155240 7912393Syz155240 if (aps->aps_ackoff[sel]) { 7922393Syz155240 seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel]; 7932393Syz155240 if (seq1 > seq2) { 7942393Syz155240 seq2 = aps->aps_ackoff[sel]; 7952393Syz155240 seq1 += seq2; 7962393Syz155240 tcp->th_seq = htonl(seq1); 7972393Syz155240 ch = 1; 7982393Syz155240 } 7992393Syz155240 } 8002393Syz155240 8012393Syz155240 if (inc && (seq1 > aps->aps_ackmin[!sel])) { 8022393Syz155240 aps->aps_ackmin[!sel] = seq1 + nlen - 1; 8032393Syz155240 aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc; 8042393Syz155240 8052393Syz155240 if (ipf_proxy_debug > 7) 8062393Syz155240 printf("proxy ack set %d at %x to %d + %d\n", 8072393Syz155240 !sel, aps->aps_seqmin[!sel], 8082393Syz155240 aps->aps_seqoff[sel], inc); 8092393Syz155240 } 8102393Syz155240 8112393Syz155240 /***/ 8122393Syz155240 8132393Syz155240 seq1 = ntohl(tcp->th_ack); 8142393Syz155240 sel = aps->aps_sel[1 - out]; 8152393Syz155240 8162393Syz155240 /* switch to other set ? */ 8172393Syz155240 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) && 8182393Syz155240 (seq1 > aps->aps_seqmin[!sel])) { 8192393Syz155240 if (ipf_proxy_debug > 7) 8202393Syz155240 printf("proxy in switch set seq %d -> %d %x > %x\n", 8212393Syz155240 sel, !sel, seq1, aps->aps_seqmin[!sel]); 8222393Syz155240 sel = aps->aps_sel[1 - out] = !sel; 8232393Syz155240 } 8242393Syz155240 8252393Syz155240 if (aps->aps_seqoff[sel] != 0) { 8262393Syz155240 if (ipf_proxy_debug > 7) 8272393Syz155240 printf("sel %d seqoff %d seq1 %x seqmin %x\n", 8282393Syz155240 sel, aps->aps_seqoff[sel], seq1, 8292393Syz155240 aps->aps_seqmin[sel]); 8302393Syz155240 if (seq1 > aps->aps_seqmin[sel]) { 8312393Syz155240 seq2 = aps->aps_seqoff[sel]; 8322393Syz155240 tcp->th_ack = htonl(seq1 - seq2); 8332393Syz155240 ch = 1; 8342393Syz155240 } 8352393Syz155240 } 8362393Syz155240 } 8372393Syz155240 8382393Syz155240 if (ipf_proxy_debug > 8) 8392393Syz155240 printf("appr_fixseqack: seq %x ack %x\n", 8402393Syz155240 ntohl(tcp->th_seq), ntohl(tcp->th_ack)); 8412393Syz155240 return ch ? 2 : 0; 8422393Syz155240 } 8432393Syz155240 8442393Syz155240 8452393Syz155240 /* 8462393Syz155240 * Initialise hook for kernel application proxies. 8472393Syz155240 * Call the initialise routine for all the compiled in kernel proxies. 8482393Syz155240 */ 849*3448Sdh155122 int appr_init(ifs) 850*3448Sdh155122 ipf_stack_t *ifs; 8512393Syz155240 { 8522393Syz155240 aproxy_t *ap; 853*3448Sdh155122 int err = 0; 8542393Syz155240 855*3448Sdh155122 /* Since the refcnt is used we make a copy of lcl_ap_proxies */ 856*3448Sdh155122 KMALLOCS(ifs->ifs_ap_proxies, aproxy_t *, sizeof (lcl_ap_proxies)); 857*3448Sdh155122 bcopy(lcl_ap_proxies, ifs->ifs_ap_proxies, sizeof (lcl_ap_proxies)); 858*3448Sdh155122 859*3448Sdh155122 for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++) { 8602393Syz155240 if (ap->apr_init != NULL) { 861*3448Sdh155122 err = (*ap->apr_init)(&ap->apr_private, ifs); 8622393Syz155240 if (err != 0) 8632393Syz155240 break; 8642393Syz155240 } 8652393Syz155240 } 866*3448Sdh155122 return 0; 8672393Syz155240 } 8682393Syz155240 8692393Syz155240 8702393Syz155240 /* 8712393Syz155240 * Unload hook for kernel application proxies. 8722393Syz155240 * Call the finialise routine for all the compiled in kernel proxies. 8732393Syz155240 */ 874*3448Sdh155122 void appr_unload(ifs) 875*3448Sdh155122 ipf_stack_t *ifs; 8762393Syz155240 { 8772393Syz155240 aproxy_t *ap; 878*3448Sdh155122 if(ifs->ifs_ap_proxies == NULL) 879*3448Sdh155122 return; 8802393Syz155240 881*3448Sdh155122 for (ap = ifs->ifs_ap_proxies; ap->apr_p; ap++) 8822393Syz155240 if (ap->apr_fini != NULL) 883*3448Sdh155122 (*ap->apr_fini)(&ap->apr_private, ifs); 884*3448Sdh155122 for (ap = ifs->ifs_ap_proxylist; ap; ap = ap->apr_next) 8852393Syz155240 if (ap->apr_fini != NULL) 886*3448Sdh155122 (*ap->apr_fini)(&ap->apr_private, ifs); 887*3448Sdh155122 888*3448Sdh155122 KFREES(ifs->ifs_ap_proxies, sizeof (lcl_ap_proxies)); 889*3448Sdh155122 ifs->ifs_ap_proxies = NULL; 8902393Syz155240 } 891