1999-08-24 20:10:35 +04:00
|
|
|
/* $NetBSD: ip_proxy.c,v 1.18 1999/08/24 16:10:35 bouyer Exp $ */
|
1997-05-28 04:17:11 +04:00
|
|
|
|
1997-05-26 19:18:15 +04:00
|
|
|
/*
|
1998-11-22 18:17:18 +03:00
|
|
|
* Copyright (C) 1997-1998 by Darren Reed.
|
1997-05-26 19:18:15 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that this notice is preserved and due credit is given
|
|
|
|
* to the original author and the contributors.
|
|
|
|
*/
|
1997-10-30 19:08:54 +03:00
|
|
|
#if !defined(lint)
|
1999-02-02 22:57:30 +03:00
|
|
|
#if defined(__NetBSD__)
|
1999-08-24 20:10:35 +04:00
|
|
|
static const char rcsid[] = "$NetBSD: ip_proxy.c,v 1.18 1999/08/24 16:10:35 bouyer Exp $";
|
1999-02-02 22:57:30 +03:00
|
|
|
#else
|
1998-11-22 18:17:18 +03:00
|
|
|
static const char rcsid[] = "@(#)Id: ip_proxy.c,v 2.0.2.11.2.15 1998/11/22 01:50:29 darrenr Exp ";
|
1997-05-26 19:18:15 +04:00
|
|
|
#endif
|
1999-02-02 22:57:30 +03:00
|
|
|
#endif
|
1997-05-26 19:18:15 +04:00
|
|
|
|
|
|
|
#if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL)
|
|
|
|
# define _KERNEL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/uio.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
|
1997-05-26 19:18:15 +04:00
|
|
|
#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
|
1997-05-26 19:18:15 +04:00
|
|
|
#endif
|
|
|
|
#if !defined(__SVR4) && !defined(__svr4__)
|
1997-11-14 15:40:06 +03:00
|
|
|
# ifndef linux
|
|
|
|
# include <sys/mbuf.h>
|
|
|
|
# endif
|
1997-05-26 19:18:15 +04:00
|
|
|
#else
|
|
|
|
# include <sys/byteorder.h>
|
|
|
|
# include <sys/dditypes.h>
|
|
|
|
# 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
|
1997-05-26 19:18:15 +04:00
|
|
|
#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>
|
1997-05-26 19:18:15 +04:00
|
|
|
#include "netinet/ip_fil.h"
|
|
|
|
#include "netinet/ip_proxy.h"
|
|
|
|
#include "netinet/ip_nat.h"
|
|
|
|
#include "netinet/ip_state.h"
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
|
|
|
#endif
|
|
|
|
|
1998-11-22 18:17:18 +03:00
|
|
|
static ap_session_t *ap_new_session __P((aproxy_t *, ip_t *,
|
1997-05-26 19:18:15 +04:00
|
|
|
fr_info_t *, nat_t *));
|
1998-11-22 18:17:18 +03:00
|
|
|
int ap_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int inc));
|
1998-07-12 19:23:59 +04:00
|
|
|
|
1997-05-26 19:18:15 +04:00
|
|
|
|
|
|
|
#define AP_SESS_SIZE 53
|
|
|
|
|
1997-11-14 15:40:06 +03:00
|
|
|
#if defined(_KERNEL) && !defined(linux)
|
1997-05-26 19:18:15 +04:00
|
|
|
#include "netinet/ip_ftp_pxy.c"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ap_session_t *ap_sess_tab[AP_SESS_SIZE];
|
1998-11-22 18:17:18 +03:00
|
|
|
ap_session_t *ap_sess_list = NULL;
|
1997-05-26 19:18:15 +04:00
|
|
|
aproxy_t ap_proxies[] = {
|
|
|
|
#ifdef IPF_FTP_PROXY
|
|
|
|
{ "ftp", (char)IPPROTO_TCP, 0, 0, ippr_ftp_init, ippr_ftp_in, ippr_ftp_out },
|
|
|
|
#endif
|
|
|
|
{ "", '\0', 0, 0, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int ap_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;
|
|
|
|
|
|
|
|
if (!apr || (apr && (apr->apr_flags & APR_DELETE)) ||
|
|
|
|
(ip->ip_p != apr->apr_p))
|
|
|
|
return 0;
|
|
|
|
if ((tcp && (tcp->th_dport != dport)) || (!tcp && dport))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-10-30 19:08:54 +03:00
|
|
|
/*
|
|
|
|
* Allocate a new application proxy structure and fill it in with the
|
|
|
|
* relevant details. call the init function once complete, prior to
|
|
|
|
* returning.
|
|
|
|
*/
|
1998-11-22 18:17:18 +03:00
|
|
|
static ap_session_t *ap_new_session(apr, ip, fin, nat)
|
1997-05-26 19:18:15 +04:00
|
|
|
aproxy_t *apr;
|
|
|
|
ip_t *ip;
|
|
|
|
fr_info_t *fin;
|
|
|
|
nat_t *nat;
|
|
|
|
{
|
|
|
|
register ap_session_t *aps;
|
1998-11-22 18:17:18 +03:00
|
|
|
tcphdr_t *tcp;
|
1997-05-26 19:18:15 +04:00
|
|
|
u_short dport;
|
1998-11-22 18:17:18 +03:00
|
|
|
u_int hv;
|
1997-05-26 19:18:15 +04:00
|
|
|
|
|
|
|
if (!apr || (apr && (apr->apr_flags & APR_DELETE)) ||
|
|
|
|
(ip->ip_p != apr->apr_p))
|
|
|
|
return NULL;
|
1998-11-22 18:17:18 +03:00
|
|
|
|
|
|
|
if (!(fin->fin_fi.fi_fl & FI_TCPUDP))
|
|
|
|
tcp = NULL;
|
|
|
|
else
|
|
|
|
tcp = (tcphdr_t *)fin->fin_dp;
|
1997-05-26 19:18:15 +04:00
|
|
|
dport = nat->nat_ptr->in_dport;
|
1998-11-22 18:17:18 +03:00
|
|
|
|
1997-05-26 19:18:15 +04:00
|
|
|
if ((tcp && (tcp->th_dport != dport)) || (!tcp && dport))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
hv = ip->ip_src.s_addr ^ ip->ip_dst.s_addr;
|
|
|
|
hv *= 651733;
|
|
|
|
if (tcp) {
|
|
|
|
hv ^= (tcp->th_sport + tcp->th_dport);
|
|
|
|
hv *= 5;
|
|
|
|
}
|
|
|
|
hv %= AP_SESS_SIZE;
|
|
|
|
|
|
|
|
KMALLOC(aps, ap_session_t *, sizeof(*aps));
|
|
|
|
if (!aps)
|
|
|
|
return NULL;
|
1997-10-30 19:08:54 +03:00
|
|
|
bzero((char *)aps, sizeof(*aps));
|
1997-05-26 19:18:15 +04:00
|
|
|
aps->aps_apr = apr;
|
|
|
|
aps->aps_src = ip->ip_src;
|
|
|
|
aps->aps_dst = ip->ip_dst;
|
|
|
|
aps->aps_p = ip->ip_p;
|
|
|
|
if (tcp) {
|
|
|
|
aps->aps_sport = tcp->th_sport;
|
|
|
|
aps->aps_dport = tcp->th_dport;
|
|
|
|
}
|
|
|
|
aps->aps_data = NULL;
|
|
|
|
aps->aps_psiz = 0;
|
1998-11-22 18:17:18 +03:00
|
|
|
aps->aps_hnext = ap_sess_tab[hv];
|
|
|
|
aps->aps_next = ap_sess_list;
|
|
|
|
ap_sess_list = aps;
|
|
|
|
aps->aps_nat = nat;
|
|
|
|
aps->aps_hv = hv;
|
|
|
|
nat->nat_aps = aps;
|
1997-05-26 19:18:15 +04:00
|
|
|
ap_sess_tab[hv] = aps;
|
1998-11-22 18:17:18 +03:00
|
|
|
(void) (*apr->apr_init)(fin, ip, aps, nat);
|
1997-05-26 19:18:15 +04:00
|
|
|
return aps;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-10-30 19:08:54 +03:00
|
|
|
/*
|
|
|
|
* check to see if a packet should be passed through an active proxy routine
|
|
|
|
* if one has been setup for it.
|
|
|
|
*/
|
1998-11-22 18:17:18 +03:00
|
|
|
int ap_check(ip, fin, nat)
|
1997-05-26 19:18:15 +04:00
|
|
|
ip_t *ip;
|
|
|
|
fr_info_t *fin;
|
|
|
|
nat_t *nat;
|
|
|
|
{
|
|
|
|
ap_session_t *aps;
|
|
|
|
aproxy_t *apr;
|
1998-11-22 18:17:18 +03:00
|
|
|
tcphdr_t *tcp = NULL;
|
1998-07-12 19:23:59 +04:00
|
|
|
u_32_t sum;
|
1997-05-26 19:18:15 +04:00
|
|
|
int err;
|
|
|
|
|
1999-08-24 20:10:35 +04:00
|
|
|
if ((aps = nat->nat_aps) == NULL)
|
|
|
|
aps = ap_new_session(nat->nat_ptr->in_apr, ip, fin, nat);
|
|
|
|
if ((aps != NULL) && (aps->aps_p == ip->ip_p)) {
|
1997-10-30 19:08:54 +03:00
|
|
|
if (ip->ip_p == IPPROTO_TCP) {
|
1998-11-22 18:17:18 +03:00
|
|
|
tcp = (tcphdr_t *)fin->fin_dp;
|
1997-10-30 19:08:54 +03:00
|
|
|
/*
|
|
|
|
* verify that the checksum is correct. If not, then
|
|
|
|
* don't do anything with this packet.
|
|
|
|
*/
|
1998-07-12 19:23:59 +04:00
|
|
|
#if SOLARIS && defined(_KERNEL)
|
|
|
|
sum = fr_tcpsum(fin->fin_qfm, ip, tcp, ip->ip_len);
|
|
|
|
#else
|
|
|
|
sum = fr_tcpsum(*(mb_t **)fin->fin_mp,
|
|
|
|
ip, tcp, ip->ip_len);
|
|
|
|
#endif
|
1999-01-23 11:50:52 +03:00
|
|
|
if (sum != 0) {
|
1997-11-14 15:40:06 +03:00
|
|
|
frstats[fin->fin_out].fr_tcpbad++;
|
1997-10-30 19:08:54 +03:00
|
|
|
return -1;
|
1997-11-14 15:40:06 +03:00
|
|
|
}
|
1997-10-30 19:08:54 +03:00
|
|
|
}
|
|
|
|
|
1997-05-26 19:18:15 +04:00
|
|
|
apr = aps->aps_apr;
|
|
|
|
err = 0;
|
|
|
|
if (fin->fin_out) {
|
|
|
|
if (apr->apr_outpkt)
|
1998-11-22 18:17:18 +03:00
|
|
|
err = (*apr->apr_outpkt)(fin, ip, aps, nat);
|
1997-05-26 19:18:15 +04:00
|
|
|
} else {
|
|
|
|
if (apr->apr_inpkt)
|
1998-11-22 18:17:18 +03:00
|
|
|
err = (*apr->apr_inpkt)(fin, ip, aps, nat);
|
1997-05-26 19:18:15 +04:00
|
|
|
}
|
1998-11-22 18:17:18 +03:00
|
|
|
|
|
|
|
if (tcp != NULL) {
|
|
|
|
err = ap_fixseqack(fin, ip, aps, err);
|
1999-01-23 11:50:52 +03:00
|
|
|
tcp->th_sum = 0;
|
1998-07-12 19:23:59 +04:00
|
|
|
#if SOLARIS && defined(_KERNEL)
|
1998-11-22 18:17:18 +03:00
|
|
|
tcp->th_sum = fr_tcpsum(fin->fin_qfm, ip, tcp,
|
|
|
|
ip->ip_len);
|
1998-07-12 19:23:59 +04:00
|
|
|
#else
|
1997-11-25 06:14:11 +03:00
|
|
|
tcp->th_sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip,
|
1998-05-17 20:50:15 +04:00
|
|
|
tcp, ip->ip_len);
|
1998-07-12 19:23:59 +04:00
|
|
|
#endif
|
1997-05-26 19:18:15 +04:00
|
|
|
}
|
1998-11-22 18:17:18 +03:00
|
|
|
aps->aps_bytes += ip->ip_len;
|
|
|
|
aps->aps_pkts++;
|
|
|
|
return 2;
|
1997-05-26 19:18:15 +04:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
aproxy_t *ap_match(pr, name)
|
1997-07-05 09:38:14 +04:00
|
|
|
u_char pr;
|
1997-05-26 19:18:15 +04:00
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
aproxy_t *ap;
|
|
|
|
|
|
|
|
for (ap = ap_proxies; ap->apr_p; ap++)
|
|
|
|
if ((ap->apr_p == pr) &&
|
1997-10-30 19:08:54 +03:00
|
|
|
!strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
|
|
|
|
ap->apr_ref++;
|
1997-05-26 19:18:15 +04:00
|
|
|
return ap;
|
1997-10-30 19:08:54 +03:00
|
|
|
}
|
1997-05-26 19:18:15 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ap_free(ap)
|
|
|
|
aproxy_t *ap;
|
|
|
|
{
|
1997-10-30 19:08:54 +03:00
|
|
|
ap->apr_ref--;
|
1997-05-26 19:18:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void aps_free(aps)
|
|
|
|
ap_session_t *aps;
|
|
|
|
{
|
1998-11-22 18:17:18 +03:00
|
|
|
ap_session_t *a, **ap;
|
|
|
|
u_int hv;
|
1997-05-26 19:18:15 +04:00
|
|
|
|
1998-11-22 18:17:18 +03:00
|
|
|
if (!aps)
|
|
|
|
return;
|
1997-05-26 19:18:15 +04:00
|
|
|
|
1998-11-22 18:17:18 +03:00
|
|
|
hv = aps->aps_hv;
|
1997-05-26 19:18:15 +04:00
|
|
|
|
1998-11-22 18:17:18 +03:00
|
|
|
for (ap = ap_sess_tab + hv; (a = *ap); ap = &a->aps_hnext)
|
|
|
|
if (a == aps) {
|
|
|
|
*ap = a->aps_hnext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ap = &ap_sess_list; (a = *ap); ap = &a->aps_next)
|
|
|
|
if (a == aps) {
|
|
|
|
*ap = a->aps_next;
|
|
|
|
break;
|
1997-05-26 19:18:15 +04:00
|
|
|
}
|
1998-11-22 18:17:18 +03:00
|
|
|
|
|
|
|
if (a) {
|
|
|
|
if (aps->aps_data && aps->aps_psiz)
|
|
|
|
KFREES(aps->aps_data, aps->aps_psiz);
|
|
|
|
KFREE(aps);
|
|
|
|
}
|
1997-05-26 19:18:15 +04:00
|
|
|
}
|
1997-11-25 06:14:11 +03:00
|
|
|
|
|
|
|
|
1998-11-22 18:17:18 +03:00
|
|
|
int ap_fixseqack(fin, ip, aps, inc)
|
|
|
|
fr_info_t *fin;
|
|
|
|
ip_t *ip;
|
|
|
|
ap_session_t *aps;
|
|
|
|
int inc;
|
1997-11-25 06:14:11 +03:00
|
|
|
{
|
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;
|
1997-11-25 06:14:11 +03:00
|
|
|
}
|
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;
|
1997-11-25 06:14:11 +03:00
|
|
|
}
|