moved from sys/netinet.

This commit is contained in:
christos 2004-10-02 07:51:53 +00:00
parent 722688d056
commit d0905be2d3
3 changed files with 697 additions and 0 deletions

302
sys/dist/ipf/netinet/ip_h323_pxy.c vendored Normal file
View File

@ -0,0 +1,302 @@
/* $NetBSD: ip_h323_pxy.c,v 1.1 2004/10/02 07:51:53 christos Exp $ */
/*
* Copyright 2001, QNX Software Systems Ltd. All Rights Reserved
*
* This source code has been published by QNX Software Systems Ltd. (QSSL).
* However, any use, reproduction, modification, distribution or transfer of
* this software, or any software which includes or is based upon any of this
* code, is only permitted under the terms of the QNX Open Community License
* version 1.0 (see licensing.qnx.com for details) or as otherwise expressly
* authorized by a written license agreement from QSSL. For more information,
* please email licensing@qnx.com.
*
* For more details, see QNX_OCL.txt provided with this distribution.
*/
/*
* Simple H.323 proxy
*
* by xtang@canada.com
* ported to ipfilter 3.4.20 by Michael Grant mg-ipf@grant.org
*/
#if __FreeBSD_version >= 220000 && defined(_KERNEL)
# include <sys/fcntl.h>
# include <sys/filio.h>
#else
# ifndef linux
# include <sys/ioctl.h>
# endif
#endif
#ifdef _KERNEL_OPT
#include "opt_ipfilter_log.h"
#endif
__KERNEL_RCSID(1, "$NetBSD: ip_h323_pxy.c,v 1.1 2004/10/02 07:51:53 christos Exp $");
#define IPF_H323_PROXY
int ippr_h323_init __P((void));
void ippr_h323_fini __P((void));
int ippr_h323_new __P((fr_info_t *, ap_session_t *, nat_t *));
void ippr_h323_del __P((ap_session_t *));
int ippr_h323_out __P((fr_info_t *, ap_session_t *, nat_t *));
int ippr_h323_in __P((fr_info_t *, ap_session_t *, nat_t *));
int ippr_h245_new __P((fr_info_t *, ap_session_t *, nat_t *));
int ippr_h245_out __P((fr_info_t *, ap_session_t *, nat_t *));
int ippr_h245_in __P((fr_info_t *, ap_session_t *, nat_t *));
static frentry_t h323_fr;
int h323_proxy_init = 0;
static int find_port __P((int, caddr_t, int datlen, int *, u_short *));
static int find_port(ipaddr, data, datlen, off, port)
int ipaddr;
caddr_t data;
int datlen, *off;
unsigned short *port;
{
u_32_t addr, netaddr;
u_char *dp;
int offset;
if (datlen < 6)
return -1;
*port = 0;
offset = *off;
dp = (u_char *)data;
netaddr = ntohl(ipaddr);
for (offset = 0; offset <= datlen - 6; offset++, dp++) {
addr = (dp[0] << 24) | (dp[1] << 16) | (dp[2] << 8) | dp[3];
if (netaddr == addr)
{
*port = (*(dp + 4) << 8) | *(dp + 5);
break;
}
}
*off = offset;
return (offset > datlen - 6) ? -1 : 0;
}
/*
* Initialize local structures.
*/
int ippr_h323_init()
{
bzero((char *)&h323_fr, sizeof(h323_fr));
h323_fr.fr_ref = 1;
h323_fr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
MUTEX_INIT(&h323_fr.fr_lock, "H323 proxy rule lock");
h323_proxy_init = 1;
return 0;
}
void ippr_h323_fini()
{
if (h323_proxy_init == 1) {
MUTEX_DESTROY(&h323_fr.fr_lock);
h323_proxy_init = 0;
}
}
int ippr_h323_new(fin, aps, nat)
fr_info_t *fin;
ap_session_t *aps;
nat_t *nat;
{
fin = fin; /* LINT */
nat = nat; /* LINT */
aps->aps_data = NULL;
aps->aps_psiz = 0;
return 0;
}
void ippr_h323_del(aps)
ap_session_t *aps;
{
int i;
ipnat_t *ipn;
if (aps->aps_data) {
for (i = 0, ipn = aps->aps_data;
i < (aps->aps_psiz / sizeof(ipnat_t));
i++, ipn = (ipnat_t *)((char *)ipn + sizeof(*ipn)))
{
/*
* Check the comment in ippr_h323_in() function,
* just above fr_nat_ioctl() call.
* We are lucky here because this function is not
* called with ipf_nat locked.
*/
if (fr_nat_ioctl((caddr_t)ipn, SIOCRMNAT, NAT_SYSSPACE|
NAT_LOCKHELD|FWRITE) == -1) {
/*EMPTY*/;
/* log the error */
}
}
KFREES(aps->aps_data, aps->aps_psiz);
/* avoid double free */
aps->aps_data = NULL;
aps->aps_psiz = 0;
}
return;
}
int ippr_h323_in(fin, aps, nat)
fr_info_t *fin;
ap_session_t *aps;
nat_t *nat;
{
int ipaddr, off, datlen;
unsigned short port;
caddr_t data;
tcphdr_t *tcp;
ip_t *ip;
ip = fin->fin_ip;
tcp = (tcphdr_t *)fin->fin_dp;
ipaddr = ip->ip_src.s_addr;
data = (caddr_t)tcp + (TCP_OFF(tcp) << 2);
datlen = fin->fin_dlen - (TCP_OFF(tcp) << 2);
if (find_port(ipaddr, data, datlen, &off, &port) == 0) {
ipnat_t *ipn;
char *newarray;
/* setup a nat rule to set a h245 proxy on tcp-port "port"
* it's like:
* map <if> <inter_ip>/<mask> -> <gate_ip>/<mask> proxy port <port> <port>/tcp
*/
KMALLOCS(newarray, char *, aps->aps_psiz + sizeof(*ipn));
if (newarray == NULL) {
return -1;
}
ipn = (ipnat_t *)&newarray[aps->aps_psiz];
bcopy((caddr_t)nat->nat_ptr, (caddr_t)ipn, sizeof(ipnat_t));
(void) strncpy(ipn->in_plabel, "h245", APR_LABELLEN);
ipn->in_inip = nat->nat_inip.s_addr;
ipn->in_inmsk = 0xffffffff;
ipn->in_dport = htons(port);
/*
* we got a problem here. we need to call fr_nat_ioctl() to add
* the h245 proxy rule, but since we already hold (READ locked)
* the nat table rwlock (ipf_nat), if we go into fr_nat_ioctl(),
* it will try to WRITE lock it. This will causing dead lock
* on RTP.
*
* The quick & dirty solution here is release the read lock,
* call fr_nat_ioctl() and re-lock it.
* A (maybe better) solution is do a UPGRADE(), and instead
* of calling fr_nat_ioctl(), we add the nat rule ourself.
*/
RWLOCK_EXIT(&ipf_nat);
if (fr_nat_ioctl((caddr_t)ipn, SIOCADNAT,
NAT_SYSSPACE|FWRITE) == -1) {
READ_ENTER(&ipf_nat);
return -1;
}
READ_ENTER(&ipf_nat);
if (aps->aps_data != NULL && aps->aps_psiz > 0) {
bcopy(aps->aps_data, newarray, aps->aps_psiz);
KFREES(aps->aps_data, aps->aps_psiz);
}
aps->aps_data = newarray;
aps->aps_psiz += sizeof(*ipn);
}
return 0;
}
int ippr_h245_new(fin, aps, nat)
fr_info_t *fin;
ap_session_t *aps;
nat_t *nat;
{
fin = fin; /* LINT */
nat = nat; /* LINT */
aps->aps_data = NULL;
aps->aps_psiz = 0;
return 0;
}
int ippr_h245_out(fin, aps, nat)
fr_info_t *fin;
ap_session_t *aps;
nat_t *nat;
{
int ipaddr, off, datlen;
tcphdr_t *tcp;
caddr_t data;
u_short port;
ip_t *ip;
aps = aps; /* LINT */
ip = fin->fin_ip;
tcp = (tcphdr_t *)fin->fin_dp;
ipaddr = nat->nat_inip.s_addr;
data = (caddr_t)tcp + (TCP_OFF(tcp) << 2);
datlen = ip->ip_len - fin->fin_hlen - (TCP_OFF(tcp) << 2);
if (find_port(ipaddr, data, datlen, &off, &port) == 0) {
fr_info_t fi;
nat_t *nat2;
/* port = htons(port); */
nat2 = nat_outlookup(fin->fin_ifp, IPN_UDP, IPPROTO_UDP,
ip->ip_src, ip->ip_dst);
if (nat2 == NULL) {
struct ip newip;
struct udphdr udp;
bcopy((caddr_t)ip, (caddr_t)&newip, sizeof(newip));
newip.ip_len = fin->fin_hlen + sizeof(udp);
newip.ip_p = IPPROTO_UDP;
newip.ip_src = nat->nat_inip;
bzero((char *)&udp, sizeof(udp));
udp.uh_sport = port;
bcopy((caddr_t)fin, (caddr_t)&fi, sizeof(fi));
fi.fin_fi.fi_p = IPPROTO_UDP;
fi.fin_data[0] = port;
fi.fin_data[1] = 0;
fi.fin_dp = (char *)&udp;
nat2 = nat_new(&fi, nat->nat_ptr, NULL,
NAT_SLAVE|IPN_UDP|SI_W_DPORT,
NAT_OUTBOUND);
if (nat2 != NULL) {
(void) nat_proto(&fi, nat2, IPN_UDP);
nat_update(&fi, nat2, nat2->nat_ptr);
nat2->nat_ptr->in_hits++;
#ifdef IPFILTER_LOG
nat_log(nat2, (u_int)(nat->nat_ptr->in_redir));
#endif
bcopy((caddr_t)&ip->ip_src.s_addr,
data + off, 4);
bcopy((caddr_t)&nat2->nat_outport,
data + off + 4, 2);
}
}
}
return 0;
}

124
sys/dist/ipf/netinet/ip_netbios_pxy.c vendored Normal file
View File

@ -0,0 +1,124 @@
/* $NetBSD: ip_netbios_pxy.c,v 1.1 2004/10/02 07:51:53 christos Exp $ */
/*
* Simple netbios-dgm transparent proxy for in-kernel use.
* For use with the NAT code.
* Id: ip_netbios_pxy.c,v 2.8 2003/12/01 02:52:16 darrenr Exp
*/
/*-
* Copyright (c) 2002-2003 Paul J. Ledbetter III
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Id: ip_netbios_pxy.c,v 2.8 2003/12/01 02:52:16 darrenr Exp
*/
__KERNEL_RCSID(1, "$NetBSD: ip_netbios_pxy.c,v 1.1 2004/10/02 07:51:53 christos Exp $");
#define IPF_NETBIOS_PROXY
int ippr_netbios_init __P((void));
void ippr_netbios_fini __P((void));
int ippr_netbios_out __P((fr_info_t *, ap_session_t *, nat_t *));
static frentry_t netbiosfr;
int netbios_proxy_init = 0;
/*
* Initialize local structures.
*/
int ippr_netbios_init()
{
bzero((char *)&netbiosfr, sizeof(netbiosfr));
netbiosfr.fr_ref = 1;
netbiosfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
MUTEX_INIT(&netbiosfr.fr_lock, "NETBIOS proxy rule lock");
netbios_proxy_init = 1;
return 0;
}
void ippr_netbios_fini()
{
if (netbios_proxy_init == 1) {
MUTEX_DESTROY(&netbiosfr.fr_lock);
netbios_proxy_init = 0;
}
}
int ippr_netbios_out(fin, aps, nat)
fr_info_t *fin;
ap_session_t *aps;
nat_t *nat;
{
char dgmbuf[6];
int off, dlen;
udphdr_t *udp;
ip_t *ip;
mb_t *m;
aps = aps; /* LINT */
nat = nat; /* LINT */
ip = fin->fin_ip;
m = *(mb_t **)fin->fin_mp;
off = fin->fin_hlen + sizeof(udphdr_t);
dlen = M_LEN(m);
dlen -= off;
/*
* no net bios datagram could possibly be shorter than this
*/
if (dlen < 11)
return 0;
udp = (udphdr_t *)fin->fin_dp;
/*
* move past the
* ip header;
* udp header;
* 4 bytes into the net bios dgm header.
* According to rfc1002, this should be the exact location of
* the source address/port
*/
off += 4;
/* Copy NATed source Address/port*/
dgmbuf[0] = (char)((ip->ip_src.s_addr ) &0xFF);
dgmbuf[1] = (char)((ip->ip_src.s_addr >> 8) &0xFF);
dgmbuf[2] = (char)((ip->ip_src.s_addr >> 16)&0xFF);
dgmbuf[3] = (char)((ip->ip_src.s_addr >> 24)&0xFF);
dgmbuf[4] = (char)((udp->uh_sport )&0xFF);
dgmbuf[5] = (char)((udp->uh_sport >> 8)&0xFF);
/* replace data in packet */
COPYBACK(m, off, sizeof(dgmbuf), dgmbuf);
return 0;
}

271
sys/dist/ipf/netinet/ip_pptp_pxy.c vendored Normal file
View File

@ -0,0 +1,271 @@
/* $NetBSD: ip_pptp_pxy.c,v 1.1 2004/10/02 07:51:53 christos Exp $ */
/*
* Copyright (C) 2002-2003 by Darren Reed
*
* Simple PPTP transparent proxy for in-kernel use. For use with the NAT
* code.
*
* Id: ip_pptp_pxy.c,v 2.10.2.5 2004/06/07 14:20:05 darrenr Exp
*
*/
#define IPF_PPTP_PROXY
typedef struct pptp_pxy {
ipnat_t pptp_rule;
nat_t *pptp_nat;
ipstate_t *pptp_state;
int pptp_seencookie;
u_32_t pptp_cookie;
} pptp_pxy_t;
int ippr_pptp_init __P((void));
void ippr_pptp_fini __P((void));
int ippr_pptp_new __P((fr_info_t *, ap_session_t *, nat_t *));
void ippr_pptp_del __P((ap_session_t *));
int ippr_pptp_inout __P((fr_info_t *, ap_session_t *, nat_t *));
int ippr_pptp_match __P((fr_info_t *, ap_session_t *, nat_t *));
static frentry_t pptpfr;
int pptp_proxy_init = 0;
/*
* PPTP application proxy initialization.
*/
int ippr_pptp_init()
{
bzero((char *)&pptpfr, sizeof(pptpfr));
pptpfr.fr_ref = 1;
pptpfr.fr_flags = FR_OUTQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
MUTEX_INIT(&pptpfr.fr_lock, "PPTP proxy rule lock");
pptp_proxy_init = 1;
return 0;
}
void ippr_pptp_fini()
{
if (pptp_proxy_init == 1) {
MUTEX_DESTROY(&pptpfr.fr_lock);
pptp_proxy_init = 0;
}
}
/*
* Setup for a new PPTP proxy.
*/
int ippr_pptp_new(fin, aps, nat)
fr_info_t *fin;
ap_session_t *aps;
nat_t *nat;
{
pptp_pxy_t *pptp;
fr_info_t fi;
ipnat_t *ipn;
nat_t *nat2;
int p, off;
ip_t *ip;
ip = fin->fin_ip;
off = fin->fin_hlen + sizeof(udphdr_t);
if (nat_outlookup(fin, 0, IPPROTO_GRE, nat->nat_inip,
ip->ip_dst) != NULL)
return -1;
aps->aps_psiz = sizeof(*pptp);
KMALLOCS(aps->aps_data, pptp_pxy_t *, sizeof(*pptp));
if (aps->aps_data == NULL)
return -1;
ip = fin->fin_ip;
pptp = aps->aps_data;
bzero((char *)pptp, sizeof(*pptp));
/*
* Create NAT rule against which the tunnel/transport mapping is
* created. This is required because the current NAT rule does not
* describe GRE but TCP instead.
*/
ipn = &pptp->pptp_rule;
ipn->in_ifps[0] = fin->fin_ifp;
ipn->in_apr = NULL;
ipn->in_use = 1;
ipn->in_hits = 1;
ipn->in_nip = ntohl(nat->nat_outip.s_addr);
ipn->in_ippip = 1;
ipn->in_inip = nat->nat_inip.s_addr;
ipn->in_inmsk = 0xffffffff;
ipn->in_outip = fin->fin_saddr;
ipn->in_outmsk = nat->nat_outip.s_addr;
ipn->in_srcip = fin->fin_saddr;
ipn->in_srcmsk = 0xffffffff;
ipn->in_redir = NAT_MAP;
bcopy(nat->nat_ptr->in_ifnames[0], ipn->in_ifnames[0],
sizeof(ipn->in_ifnames[0]));
ipn->in_p = IPPROTO_GRE;
bcopy((char *)fin, (char *)&fi, sizeof(fi));
fi.fin_fi.fi_p = IPPROTO_GRE;
fi.fin_fr = &pptpfr;
fi.fin_data[0] = 0;
fi.fin_data[1] = 0;
p = ip->ip_p;
ip->ip_p = IPPROTO_GRE;
fi.fin_flx &= ~FI_TCPUDP;
fi.fin_flx |= FI_IGNORE;
nat2 = nat_new(&fi, ipn, &pptp->pptp_nat, 0, NAT_OUTBOUND);
pptp->pptp_nat = nat2;
if (nat2 != NULL) {
(void) nat_proto(&fi, nat2, 0);
nat_update(&fi, nat2, nat2->nat_ptr);
fi.fin_data[0] = 0;
fi.fin_data[1] = 0;
pptp->pptp_state = fr_addstate(&fi, &pptp->pptp_state, 0);
}
ip->ip_p = p;
return 0;
}
/*
* For outgoing PPTP packets. refresh timeouts for NAT & state entries, if
* we can. If they have disappeared, recreate them.
*/
int ippr_pptp_inout(fin, aps, nat)
fr_info_t *fin;
ap_session_t *aps;
nat_t *nat;
{
pptp_pxy_t *pptp;
fr_info_t fi;
nat_t *nat2;
ip_t *ip;
int p;
if ((fin->fin_out == 1) && (nat->nat_dir == NAT_INBOUND))
return 0;
if ((fin->fin_out == 0) && (nat->nat_dir == NAT_OUTBOUND))
return 0;
pptp = aps->aps_data;
if (pptp != NULL) {
ip = fin->fin_ip;
p = ip->ip_p;
if ((pptp->pptp_nat == NULL) || (pptp->pptp_state == NULL)) {
bcopy((char *)fin, (char *)&fi, sizeof(fi));
fi.fin_fi.fi_p = IPPROTO_GRE;
fi.fin_fr = &pptpfr;
fi.fin_data[0] = 0;
fi.fin_data[1] = 0;
ip->ip_p = IPPROTO_GRE;
fi.fin_flx &= ~FI_TCPUDP;
fi.fin_flx |= FI_IGNORE;
}
/*
* Update NAT timeout/create NAT if missing.
*/
if (pptp->pptp_nat != NULL)
fr_queueback(&pptp->pptp_nat->nat_tqe);
else {
nat2 = nat_new(&fi, &pptp->pptp_rule, &pptp->pptp_nat,
NAT_SLAVE, nat->nat_dir);
pptp->pptp_nat = nat2;
if (nat2 != NULL) {
(void) nat_proto(&fi, nat2, 0);
nat_update(&fi, nat2, nat2->nat_ptr);
}
}
/*
* Update state timeout/create state if missing.
*/
READ_ENTER(&ipf_state);
if (pptp->pptp_state != NULL) {
fr_queueback(&pptp->pptp_state->is_sti);
RWLOCK_EXIT(&ipf_state);
} else {
RWLOCK_EXIT(&ipf_state);
fi.fin_data[0] = 0;
fi.fin_data[1] = 0;
pptp->pptp_state = fr_addstate(&fi, &pptp->pptp_state,
0);
}
ip->ip_p = p;
}
return 0;
}
/*
* clean up after ourselves.
*/
void ippr_pptp_del(aps)
ap_session_t *aps;
{
pptp_pxy_t *pptp;
pptp = aps->aps_data;
if (pptp != NULL) {
/*
* Don't bother changing any of the NAT structure details,
* *_del() is on a callback from aps_free(), from nat_delete()
*/
READ_ENTER(&ipf_state);
if (pptp->pptp_state != NULL) {
pptp->pptp_state->is_die = fr_ticks + 1;
pptp->pptp_state->is_me = NULL;
fr_queuefront(&pptp->pptp_state->is_sti);
}
RWLOCK_EXIT(&ipf_state);
pptp->pptp_state = NULL;
pptp->pptp_nat = NULL;
}
}
int ippr_pptp_match(fin, aps, nat)
fr_info_t *fin;
ap_session_t *aps;
nat_t *nat;
{
pptp_pxy_t *pptp;
tcphdr_t *tcp;
u_32_t cookie;
pptp = aps->aps_data;
tcp = fin->fin_dp;
if ((pptp != NULL) && (fin->fin_dlen - (TCP_OFF(tcp) << 2) >= 8)) {
u_char *cs;
cs = (u_char *)tcp + (TCP_OFF(tcp) << 2) + 4;
if (pptp->pptp_seencookie == 0) {
pptp->pptp_seencookie = 1;
pptp->pptp_cookie = (cs[0] << 24) | (cs[1] << 16) |
(cs[2] << 8) | cs[3];
} else {
cookie = (cs[0] << 24) | (cs[1] << 16) |
(cs[2] << 8) | cs[3];
if (cookie != pptp->pptp_cookie)
return -1;
}
}
return 0;
}