NetBSD/sys/netinet/ip_proxy.c

554 lines
12 KiB
C
Raw Normal View History

2002-06-09 20:33:36 +04:00
/* $NetBSD: ip_proxy.c,v 1.34 2002/06/09 16:33:42 itojun Exp $ */
/*
2002-01-24 11:21:30 +03:00
* Copyright (C) 1997-2002 by Darren Reed.
*
2002-01-24 11:21:30 +03:00
* See the IPFILTER.LICENCE file for details on licencing.
*/
#if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL)
# define _KERNEL
#endif
2002-03-14 15:32:36 +03:00
#ifdef __sgi
# include <sys/ptimers.h>
#endif
#include <sys/errno.h>
2002-01-24 11:23:40 +03:00
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/file.h>
2002-06-09 20:33:36 +04:00
#if !defined(__FreeBSD_version)
# include <sys/ioctl.h>
1999-12-12 14:11:15 +03:00
#endif
#include <sys/fcntl.h>
1998-11-22 18:17:18 +03:00
#if !defined(_KERNEL) && !defined(KERNEL)
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
#endif
1997-11-14 15:40:06 +03:00
#ifndef linux
# include <sys/protosw.h>
#endif
#include <sys/socket.h>
1997-11-14 15:40:06 +03:00
#if defined(_KERNEL)
# if !defined(linux)
# include <sys/systm.h>
# else
# include <linux/string.h>
# endif
#endif
#if !defined(__SVR4) && !defined(__svr4__)
1997-11-14 15:40:06 +03:00
# ifndef linux
# include <sys/mbuf.h>
# endif
#else
# include <sys/byteorder.h>
1999-12-12 14:11:15 +03:00
# ifdef _KERNEL
# include <sys/dditypes.h>
# endif
# include <sys/stream.h>
# include <sys/kmem.h>
#endif
#if __FreeBSD__ > 2
# include <sys/queue.h>
#endif
#include <net/if.h>
#ifdef sun
# include <net/af.h>
#endif
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
1997-11-14 15:40:06 +03:00
#ifndef linux
# include <netinet/ip_var.h>
#endif
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include "netinet/ip_compat.h"
1997-11-14 15:40:06 +03:00
#include <netinet/tcpip.h>
#include "netinet/ip_fil.h"
#include "netinet/ip_nat.h"
#include "netinet/ip_state.h"
2002-01-24 11:21:30 +03:00
#include "netinet/ip_proxy.h"
1999-12-12 14:11:15 +03:00
#if (__FreeBSD_version >= 300000)
# include <sys/malloc.h>
#endif
2002-01-24 11:21:30 +03:00
#if !defined(lint)
2002-01-24 11:23:40 +03:00
#if defined(__NetBSD__)
#include <sys/cdefs.h>
2002-06-09 20:33:36 +04:00
__KERNEL_RCSID(0, "$NetBSD: ip_proxy.c,v 1.34 2002/06/09 16:33:42 itojun Exp $");
2002-01-24 11:23:40 +03:00
#else
2002-05-02 21:11:37 +04:00
static const char rcsid[] = "@(#)Id: ip_proxy.c,v 2.9.2.22 2002/04/26 10:23:17 darrenr Exp";
2002-01-24 11:21:30 +03:00
#endif
2002-01-24 11:23:40 +03:00
#endif
2002-01-24 11:21:30 +03:00
#if defined(_KERNEL) && (SOLARIS || defined(__sgi))
extern KRWLOCK_T ipf_nat, ipf_state;
#endif
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
1999-12-12 14:11:15 +03:00
static int appr_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int ));
1998-07-12 19:23:59 +04:00
#define AP_SESS_SIZE 53
#include "netinet/ip_ftp_pxy.c"
2002-05-02 21:11:37 +04:00
#if defined(_KERNEL)
1999-12-12 14:11:15 +03:00
#include "netinet/ip_rcmd_pxy.c"
#include "netinet/ip_raudio_pxy.c"
2002-01-24 11:21:30 +03:00
#include "netinet/ip_netbios_pxy.c"
#include "netinet/ip_h323_pxy.c"
#endif
2002-01-24 11:21:30 +03:00
#include "netinet/ip_ipsec_pxy.c"
ap_session_t *ap_sess_tab[AP_SESS_SIZE];
1998-11-22 18:17:18 +03:00
ap_session_t *ap_sess_list = NULL;
2000-05-03 15:12:03 +04:00
aproxy_t *ap_proxylist = NULL;
aproxy_t ap_proxies[] = {
#ifdef IPF_FTP_PROXY
2000-05-03 15:12:03 +04:00
{ NULL, "ftp", (char)IPPROTO_TCP, 0, 0, ippr_ftp_init, NULL,
2002-01-24 11:21:30 +03:00
ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL },
1999-12-12 14:11:15 +03:00
#endif
#ifdef IPF_RCMD_PROXY
2000-05-03 15:12:03 +04:00
{ NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, ippr_rcmd_init, NULL,
2002-01-24 11:21:30 +03:00
ippr_rcmd_new, NULL, NULL, ippr_rcmd_out, NULL },
1999-12-12 14:11:15 +03:00
#endif
#ifdef IPF_RAUDIO_PROXY
2000-05-03 15:12:03 +04:00
{ NULL, "raudio", (char)IPPROTO_TCP, 0, 0, ippr_raudio_init, NULL,
2002-01-24 11:21:30 +03:00
ippr_raudio_new, NULL, ippr_raudio_in, ippr_raudio_out, NULL },
#endif
#ifdef IPF_IPSEC_PROXY
{ NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, ippr_ipsec_init, NULL,
ippr_ipsec_new, ippr_ipsec_del, NULL, ippr_ipsec_out,
ippr_ipsec_match },
#endif
#ifdef IPF_NETBIOS_PROXY
{ NULL, "netbios", (char)IPPROTO_TCP, 0, 0, ippr_netbios_init, NULL,
NULL, NULL, NULL, ippr_netbios_out, NULL },
#endif
2002-03-14 15:32:36 +03:00
#ifdef IPF_H323_PROXY
{ NULL, "h323", (char)IPPROTO_TCP, 0, 0, ippr_h323_init, NULL,
2002-06-09 20:33:36 +04:00
ippr_h323_new, ippr_h323_del, ippr_h323_in, ippr_h323_out, NULL },
2002-03-14 15:32:36 +03:00
{ NULL, "h245", (char)IPPROTO_TCP, 0, 0, ippr_h245_init, NULL,
2002-06-09 20:33:36 +04:00
ippr_h245_new, NULL, NULL, ippr_h245_out, NULL },
#endif
2002-01-24 11:21:30 +03:00
{ NULL, "", '\0', 0, 0, NULL, NULL, NULL }
};
2002-01-24 11:21:30 +03:00
/*
* Dynamically add a new kernel proxy. Ensure that it is unique in the
* collection compiled in and dynamically added.
*/
2000-05-03 15:12:03 +04:00
int appr_add(ap)
aproxy_t *ap;
{
aproxy_t *a;
for (a = ap_proxies; a->apr_p; a++)
if ((a->apr_p == ap->apr_p) &&
!strncmp(a->apr_label, ap->apr_label,
sizeof(ap->apr_label)))
return -1;
2002-01-24 11:21:30 +03:00
for (a = ap_proxylist; a && a->apr_p; a = a->apr_next)
2000-05-03 15:12:03 +04:00
if ((a->apr_p == ap->apr_p) &&
!strncmp(a->apr_label, ap->apr_label,
sizeof(ap->apr_label)))
return -1;
ap->apr_next = ap_proxylist;
ap_proxylist = ap;
return (*ap->apr_init)();
}
2002-01-24 11:21:30 +03:00
/*
* Delete a proxy that has been added dynamically from those available.
* If it is in use, return 1 (do not destroy NOW), not in use 0 or -1
* if it cannot be matched.
*/
2000-05-03 15:12:03 +04:00
int appr_del(ap)
aproxy_t *ap;
{
aproxy_t *a, **app;
for (app = &ap_proxylist; (a = *app); app = &a->apr_next)
if (a == ap) {
2002-01-24 11:21:30 +03:00
a->apr_flags |= APR_DELETE;
*app = a->apr_next;
2000-05-03 15:12:03 +04:00
if (ap->apr_ref != 0)
return 1;
return 0;
}
return -1;
}
2002-01-24 11:21:30 +03:00
/*
* Return 1 if the packet is a good match against a proxy, else 0.
*/
1999-12-12 14:11:15 +03:00
int appr_ok(ip, tcp, nat)
ip_t *ip;
tcphdr_t *tcp;
ipnat_t *nat;
{
aproxy_t *apr = nat->in_apr;
u_short dport = nat->in_dport;
2002-01-24 11:21:30 +03:00
if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
(ip->ip_p != apr->apr_p))
return 0;
2002-01-24 11:21:30 +03:00
if (((tcp != NULL) && (tcp->th_dport != dport)) || (!tcp && dport))
return 0;
return 1;
}
2002-01-24 11:21:30 +03:00
/*
* If a proxy has a match function, call that to do extended packet
* matching.
*/
int appr_match(fin, nat)
fr_info_t *fin;
nat_t *nat;
{
aproxy_t *apr;
ipnat_t *ipn;
ipn = nat->nat_ptr;
if (ipn == NULL)
return -1;
apr = ipn->in_apr;
if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
(nat->nat_aps == NULL))
return -1;
if (apr->apr_match != NULL)
if ((*apr->apr_match)(fin, nat->nat_aps, nat) != 0)
return -1;
return 0;
}
/*
* Allocate a new application proxy structure and fill it in with the
* relevant details. call the init function once complete, prior to
* returning.
*/
2002-01-24 11:21:30 +03:00
int appr_new(fin, ip, nat)
fr_info_t *fin;
2002-01-24 11:21:30 +03:00
ip_t *ip;
nat_t *nat;
{
2002-01-24 11:21:30 +03:00
register ap_session_t *aps;
aproxy_t *apr;
if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL))
return -1;
apr = nat->nat_ptr->in_apr;
1998-11-22 18:17:18 +03:00
1999-12-12 14:11:15 +03:00
if (!apr || (apr->apr_flags & APR_DELETE) || (ip->ip_p != apr->apr_p))
2002-01-24 11:21:30 +03:00
return -1;
1999-12-12 14:11:15 +03:00
KMALLOC(aps, ap_session_t *);
if (!aps)
2002-01-24 11:21:30 +03:00
return -1;
bzero((char *)aps, sizeof(*aps));
aps->aps_p = ip->ip_p;
aps->aps_data = NULL;
1999-12-12 14:11:15 +03:00
aps->aps_apr = apr;
aps->aps_psiz = 0;
1999-12-12 14:11:15 +03:00
if (apr->apr_new != NULL)
2000-05-03 15:12:03 +04:00
if ((*apr->apr_new)(fin, ip, aps, nat) == -1) {
2002-03-14 15:32:36 +03:00
if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
2002-01-24 11:21:30 +03:00
KFREES(aps->aps_data, aps->aps_psiz);
2002-03-14 15:32:36 +03:00
}
2000-05-03 15:12:03 +04:00
KFREE(aps);
2002-01-24 11:21:30 +03:00
return -1;
2000-05-03 15:12:03 +04:00
}
aps->aps_nat = nat;
aps->aps_next = ap_sess_list;
ap_sess_list = aps;
2002-01-24 11:21:30 +03:00
nat->nat_aps = aps;
return 0;
}
/*
* check to see if a packet should be passed through an active proxy routine
* if one has been setup for it.
*/
1999-12-12 14:11:15 +03:00
int appr_check(ip, fin, nat)
ip_t *ip;
fr_info_t *fin;
nat_t *nat;
{
2002-01-24 11:21:30 +03:00
#if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
mb_t *m = fin->fin_qfm;
int dosum = 1;
#endif
tcphdr_t *tcp = NULL;
ap_session_t *aps;
aproxy_t *apr;
1998-07-12 19:23:59 +04:00
u_32_t sum;
short rv;
int err;
1999-12-12 14:11:15 +03:00
aps = nat->nat_aps;
if ((aps != NULL) && (aps->aps_p == ip->ip_p)) {
if (ip->ip_p == IPPROTO_TCP) {
1998-11-22 18:17:18 +03:00
tcp = (tcphdr_t *)fin->fin_dp;
/*
* verify that the checksum is correct. If not, then
* don't do anything with this packet.
*/
2002-01-24 11:21:30 +03:00
#if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
if (dohwcksum && (m->b_ick_flag == ICK_VALID)) {
sum = tcp->th_sum;
dosum = 0;
}
if (dosum)
sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
1998-07-12 19:23:59 +04:00
#else
1999-12-12 14:11:15 +03:00
sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
1998-07-12 19:23:59 +04:00
#endif
1999-12-12 14:11:15 +03:00
if (sum != tcp->th_sum) {
1997-11-14 15:40:06 +03:00
frstats[fin->fin_out].fr_tcpbad++;
return -1;
1997-11-14 15:40:06 +03:00
}
}
apr = aps->aps_apr;
err = 0;
1999-12-12 14:11:15 +03:00
if (fin->fin_out != 0) {
if (apr->apr_outpkt != NULL)
1998-11-22 18:17:18 +03:00
err = (*apr->apr_outpkt)(fin, ip, aps, nat);
} else {
1999-12-12 14:11:15 +03:00
if (apr->apr_inpkt != NULL)
1998-11-22 18:17:18 +03:00
err = (*apr->apr_inpkt)(fin, ip, aps, nat);
}
1998-11-22 18:17:18 +03:00
rv = APR_EXIT(err);
2002-01-24 11:21:30 +03:00
if (rv == 1)
return -1;
if (rv == 2) {
appr_free(apr);
nat->nat_aps = NULL;
return -1;
}
1998-11-22 18:17:18 +03:00
if (tcp != NULL) {
err = appr_fixseqack(fin, ip, aps, APR_INC(err));
2002-01-24 11:21:30 +03:00
#if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
if (dosum)
tcp->th_sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
1998-07-12 19:23:59 +04:00
#else
1999-12-12 14:11:15 +03:00
tcp->th_sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
1998-07-12 19:23:59 +04:00
#endif
}
1998-11-22 18:17:18 +03:00
aps->aps_bytes += ip->ip_len;
aps->aps_pkts++;
return 1;
}
return 0;
}
2002-01-24 11:21:30 +03:00
/*
* Search for an proxy by the protocol it is being used with and its name.
*/
aproxy_t *appr_lookup(pr, name)
1999-12-12 14:11:15 +03:00
u_int pr;
char *name;
{
aproxy_t *ap;
for (ap = ap_proxies; ap->apr_p; ap++)
if ((ap->apr_p == pr) &&
!strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
ap->apr_ref++;
return ap;
}
2000-05-03 15:12:03 +04:00
for (ap = ap_proxylist; ap; ap = ap->apr_next)
if ((ap->apr_p == pr) &&
!strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
ap->apr_ref++;
return ap;
}
return NULL;
}
1999-12-12 14:11:15 +03:00
void appr_free(ap)
aproxy_t *ap;
{
ap->apr_ref--;
}
void aps_free(aps)
ap_session_t *aps;
{
1998-11-22 18:17:18 +03:00
ap_session_t *a, **ap;
2002-01-24 11:21:30 +03:00
aproxy_t *apr;
1998-11-22 18:17:18 +03:00
if (!aps)
return;
1998-11-22 18:17:18 +03:00
for (ap = &ap_sess_list; (a = *ap); ap = &a->aps_next)
if (a == aps) {
*ap = a->aps_next;
break;
}
1998-11-22 18:17:18 +03:00
2002-01-24 11:21:30 +03:00
apr = aps->aps_apr;
if ((apr != NULL) && (apr->apr_del != NULL))
(*apr->apr_del)(aps);
2002-06-09 20:33:36 +04:00
2000-05-03 15:12:03 +04:00
if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
KFREES(aps->aps_data, aps->aps_psiz);
KFREE(aps);
}
1999-12-12 14:11:15 +03:00
static int appr_fixseqack(fin, ip, aps, inc)
1998-11-22 18:17:18 +03:00
fr_info_t *fin;
ip_t *ip;
ap_session_t *aps;
int inc;
{
1998-11-22 18:17:18 +03:00
int sel, ch = 0, out, nlen;
u_32_t seq1, seq2;
tcphdr_t *tcp;
tcp = (tcphdr_t *)fin->fin_dp;
out = fin->fin_out;
nlen = ip->ip_len;
nlen -= (ip->ip_hl << 2) + (tcp->th_off << 2);
if (out != 0) {
seq1 = (u_32_t)ntohl(tcp->th_seq);
sel = aps->aps_sel[out];
/* switch to other set ? */
if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
(seq1 > aps->aps_seqmin[!sel]))
sel = aps->aps_sel[out] = !sel;
if (aps->aps_seqoff[sel]) {
seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
if (seq1 > seq2) {
seq2 = aps->aps_seqoff[sel];
seq1 += seq2;
tcp->th_seq = htonl(seq1);
ch = 1;
}
}
if (inc && (seq1 > aps->aps_seqmin[!sel])) {
aps->aps_seqmin[!sel] = seq1 + nlen - 1;
aps->aps_seqoff[!sel] = aps->aps_seqoff[sel] + inc;
}
/***/
seq1 = ntohl(tcp->th_ack);
sel = aps->aps_sel[1 - out];
/* switch to other set ? */
if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
(seq1 > aps->aps_ackmin[!sel]))
sel = aps->aps_sel[1 - out] = !sel;
if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
seq2 = aps->aps_ackoff[sel];
tcp->th_ack = htonl(seq1 - seq2);
ch = 1;
}
1998-11-22 18:17:18 +03:00
} else {
seq1 = ntohl(tcp->th_seq);
sel = aps->aps_sel[out];
/* switch to other set ? */
if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
(seq1 > aps->aps_ackmin[!sel]))
sel = aps->aps_sel[out] = !sel;
if (aps->aps_ackoff[sel]) {
seq2 = aps->aps_ackmin[sel] -
aps->aps_ackoff[sel];
if (seq1 > seq2) {
seq2 = aps->aps_ackoff[sel];
seq1 += seq2;
tcp->th_seq = htonl(seq1);
ch = 1;
}
}
if (inc && (seq1 > aps->aps_ackmin[!sel])) {
aps->aps_ackmin[!sel] = seq1 + nlen - 1;
aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
}
/***/
seq1 = ntohl(tcp->th_ack);
sel = aps->aps_sel[1 - out];
/* switch to other set ? */
if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
(seq1 > aps->aps_seqmin[!sel]))
sel = aps->aps_sel[1 - out] = !sel;
if (aps->aps_seqoff[sel] && (seq1 > aps->aps_seqmin[sel])) {
seq2 = aps->aps_seqoff[sel];
tcp->th_ack = htonl(seq1 - seq2);
ch = 1;
}
}
return ch ? 2 : 0;
}
1999-12-12 14:11:15 +03:00
2002-01-24 11:21:30 +03:00
/*
* Initialise hook for kernel application proxies.
* Call the initialise routine for all the compiled in kernel proxies.
*/
1999-12-12 14:11:15 +03:00
int appr_init()
{
aproxy_t *ap;
int err = 0;
for (ap = ap_proxies; ap->apr_p; ap++) {
err = (*ap->apr_init)();
if (err != 0)
break;
}
return err;
}
2000-05-03 15:12:03 +04:00
2002-01-24 11:21:30 +03:00
/*
* Unload hook for kernel application proxies.
* Call the finialise routine for all the compiled in kernel proxies.
*/
2000-05-03 15:12:03 +04:00
void appr_unload()
{
aproxy_t *ap;
for (ap = ap_proxies; ap->apr_p; ap++)
if (ap->apr_fini)
(*ap->apr_fini)();
for (ap = ap_proxylist; ap; ap = ap->apr_next)
if (ap->apr_fini)
(*ap->apr_fini)();
}