2003-08-15 07:42:00 +04:00
|
|
|
/* $NetBSD: ip_auth.c,v 1.31 2003/08/15 03:42:02 jonathan Exp $ */
|
1997-07-06 09:02:26 +04:00
|
|
|
|
|
|
|
/*
|
2002-01-24 11:21:30 +03:00
|
|
|
* Copyright (C) 1998-2001 by Darren Reed & Guido van Rooij.
|
1997-07-06 09:02:26 +04:00
|
|
|
*
|
2002-01-24 11:21:30 +03:00
|
|
|
* See the IPFILTER.LICENCE file for details on licencing.
|
1997-07-06 09:02:26 +04:00
|
|
|
*/
|
2002-03-14 15:32:36 +03:00
|
|
|
#ifdef __sgi
|
|
|
|
# include <sys/ptimers.h>
|
|
|
|
#endif
|
2001-11-15 12:47:59 +03:00
|
|
|
#include <sys/errno.h>
|
2002-01-24 11:23:40 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
1997-07-06 09:02:26 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/file.h>
|
1998-11-22 18:17:18 +03:00
|
|
|
#if !defined(_KERNEL) && !defined(KERNEL)
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
2000-05-03 15:12:03 +04:00
|
|
|
#if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
|
1997-07-06 09:02:26 +04:00
|
|
|
# include <sys/filio.h>
|
|
|
|
# include <sys/fcntl.h>
|
|
|
|
#else
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
1997-11-14 15:40:06 +03:00
|
|
|
#ifndef linux
|
|
|
|
# include <sys/protosw.h>
|
|
|
|
#endif
|
1997-07-06 09:02:26 +04:00
|
|
|
#include <sys/socket.h>
|
2000-05-03 15:12:03 +04:00
|
|
|
#if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
|
1997-07-06 09:02:26 +04:00
|
|
|
# include <sys/systm.h>
|
|
|
|
#endif
|
|
|
|
#if !defined(__SVR4) && !defined(__svr4__)
|
1997-11-14 15:40:06 +03:00
|
|
|
# ifndef linux
|
|
|
|
# include <sys/mbuf.h>
|
|
|
|
# endif
|
1997-07-06 09:02:26 +04:00
|
|
|
#else
|
|
|
|
# include <sys/filio.h>
|
|
|
|
# include <sys/byteorder.h>
|
1999-12-12 14:11:15 +03:00
|
|
|
# ifdef _KERNEL
|
|
|
|
# include <sys/dditypes.h>
|
|
|
|
# endif
|
1997-07-06 09:02:26 +04:00
|
|
|
# include <sys/stream.h>
|
|
|
|
# include <sys/kmem.h>
|
|
|
|
#endif
|
2000-08-10 01:00:39 +04:00
|
|
|
#if (_BSDI_VERSION >= 199802) || (__FreeBSD_version >= 400000)
|
1999-12-12 14:11:15 +03:00
|
|
|
# include <sys/queue.h>
|
|
|
|
#endif
|
1997-11-14 15:40:06 +03:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(bsdi)
|
1997-07-06 09:02:26 +04:00
|
|
|
# include <machine/cpu.h>
|
|
|
|
#endif
|
|
|
|
#include <net/if.h>
|
|
|
|
#ifdef sun
|
1999-12-12 14:11:15 +03:00
|
|
|
# include <net/af.h>
|
1997-07-06 09:02:26 +04:00
|
|
|
#endif
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#ifndef KERNEL
|
1999-12-12 14:11:15 +03:00
|
|
|
# define KERNEL
|
|
|
|
# define NOT_KERNEL
|
1997-07-06 09:02:26 +04:00
|
|
|
#endif
|
1997-11-14 15:40:06 +03:00
|
|
|
#ifndef linux
|
|
|
|
# include <netinet/ip_var.h>
|
|
|
|
#endif
|
1997-07-06 09:02:26 +04:00
|
|
|
#ifdef NOT_KERNEL
|
1999-12-12 14:11:15 +03:00
|
|
|
# undef KERNEL
|
1997-07-06 09:02:26 +04:00
|
|
|
#endif
|
1997-10-30 19:08:54 +03:00
|
|
|
#ifdef __sgi
|
|
|
|
# ifdef IFF_DRVRLOCK /* IRIX6 */
|
1999-12-12 14:11:15 +03:00
|
|
|
# include <sys/hashing.h>
|
1997-10-30 19:08:54 +03:00
|
|
|
# endif
|
|
|
|
#endif
|
1997-11-14 15:40:06 +03:00
|
|
|
#include <netinet/tcp.h>
|
1997-10-30 19:08:54 +03:00
|
|
|
#if defined(__sgi) && !defined(IFF_DRVRLOCK) /* IRIX < 6 */
|
2002-01-24 11:21:30 +03:00
|
|
|
extern struct ifqueue ipintrq; /* ip packet input queue */
|
1997-10-30 19:08:54 +03:00
|
|
|
#else
|
1997-11-14 15:40:06 +03:00
|
|
|
# ifndef linux
|
1999-12-12 14:11:15 +03:00
|
|
|
# if __FreeBSD_version >= 300000
|
|
|
|
# include <net/if_var.h>
|
|
|
|
# endif
|
1997-11-14 15:40:06 +03:00
|
|
|
# include <netinet/in_var.h>
|
|
|
|
# include <netinet/tcp_fsm.h>
|
|
|
|
# endif
|
1997-10-30 19:08:54 +03:00
|
|
|
#endif
|
1997-07-06 09:02:26 +04:00
|
|
|
#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-07-06 09:02:26 +04:00
|
|
|
#include "netinet/ip_fil.h"
|
|
|
|
#include "netinet/ip_auth.h"
|
1997-11-14 15:40:06 +03:00
|
|
|
#if !SOLARIS && !defined(linux)
|
1997-07-06 09:02:26 +04:00
|
|
|
# include <net/netisr.h>
|
1998-05-17 20:50:15 +04:00
|
|
|
# ifdef __FreeBSD__
|
|
|
|
# include <machine/cpufunc.h>
|
|
|
|
# endif
|
1997-07-06 09:02:26 +04:00
|
|
|
#endif
|
1999-12-12 14:11:15 +03:00
|
|
|
#if (__FreeBSD_version >= 300000)
|
|
|
|
# include <sys/malloc.h>
|
|
|
|
# if (defined(_KERNEL) || defined(KERNEL)) && !defined(IPFILTER_LKM)
|
|
|
|
# include <sys/libkern.h>
|
|
|
|
# include <sys/systm.h>
|
|
|
|
# endif
|
|
|
|
#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>
|
2003-08-15 07:42:00 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ip_auth.c,v 1.31 2003/08/15 03:42:02 jonathan Exp $");
|
2002-01-24 11:23:40 +03:00
|
|
|
#else
|
2002-09-19 12:08:14 +04:00
|
|
|
static const char rcsid[] = "@(#)Id: ip_auth.c,v 2.11.2.20 2002/06/04 14:40:42 darrenr Exp";
|
2002-01-24 11:23:40 +03:00
|
|
|
#endif
|
2002-05-02 21:13:27 +04:00
|
|
|
#endif
|
1997-07-06 09:02:26 +04:00
|
|
|
|
|
|
|
|
1997-10-30 19:08:54 +03:00
|
|
|
#if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
|
2002-01-24 11:21:30 +03:00
|
|
|
extern KRWLOCK_T ipf_auth, ipf_mutex;
|
1998-07-12 19:23:59 +04:00
|
|
|
extern kmutex_t ipf_authmx;
|
1997-10-30 19:08:54 +03:00
|
|
|
# if SOLARIS
|
1997-07-06 09:02:26 +04:00
|
|
|
extern kcondvar_t ipfauthwait;
|
1997-10-30 19:08:54 +03:00
|
|
|
# endif
|
1997-07-06 09:02:26 +04:00
|
|
|
#endif
|
1997-11-14 15:40:06 +03:00
|
|
|
#ifdef linux
|
|
|
|
static struct wait_queue *ipfauthwait = NULL;
|
|
|
|
#endif
|
1997-07-06 09:02:26 +04:00
|
|
|
|
|
|
|
int fr_authsize = FR_NUMAUTH;
|
|
|
|
int fr_authused = 0;
|
|
|
|
int fr_defaultauthage = 600;
|
2000-05-03 15:12:03 +04:00
|
|
|
int fr_auth_lock = 0;
|
1997-07-06 09:02:26 +04:00
|
|
|
fr_authstat_t fr_authstats;
|
2000-05-03 15:12:03 +04:00
|
|
|
static frauth_t fr_auth[FR_NUMAUTH];
|
1997-07-06 09:02:26 +04:00
|
|
|
mb_t *fr_authpkts[FR_NUMAUTH];
|
2000-05-03 15:12:03 +04:00
|
|
|
static int fr_authstart = 0, fr_authend = 0, fr_authnext = 0;
|
|
|
|
static frauthent_t *fae_list = NULL;
|
2002-01-24 11:21:30 +03:00
|
|
|
frentry_t *ipauth = NULL,
|
|
|
|
*fr_authlist = NULL;
|
1997-07-06 09:02:26 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if a packet has authorization. If the packet is found to match an
|
|
|
|
* authorization result and that would result in a feedback loop (i.e. it
|
|
|
|
* will end up returning FR_AUTH) then return FR_BLOCK instead.
|
|
|
|
*/
|
1999-12-12 14:11:15 +03:00
|
|
|
u_32_t fr_checkauth(ip, fin)
|
1997-07-06 09:02:26 +04:00
|
|
|
ip_t *ip;
|
|
|
|
fr_info_t *fin;
|
|
|
|
{
|
|
|
|
u_short id = ip->ip_id;
|
2002-01-24 11:21:30 +03:00
|
|
|
frentry_t *fr;
|
|
|
|
frauth_t *fra;
|
1997-07-06 09:02:26 +04:00
|
|
|
u_32_t pass;
|
|
|
|
int i;
|
|
|
|
|
2002-01-24 11:21:30 +03:00
|
|
|
if (fr_auth_lock || !fr_authused)
|
2000-05-03 15:12:03 +04:00
|
|
|
return 0;
|
|
|
|
|
1998-07-12 19:23:59 +04:00
|
|
|
READ_ENTER(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
for (i = fr_authstart; i != fr_authend; ) {
|
|
|
|
/*
|
|
|
|
* index becomes -2 only after an SIOCAUTHW. Check this in
|
|
|
|
* case the same packet gets sent again and it hasn't yet been
|
|
|
|
* auth'd.
|
|
|
|
*/
|
2002-01-24 11:21:30 +03:00
|
|
|
fra = fr_auth + i;
|
|
|
|
if ((fra->fra_index == -2) && (id == fra->fra_info.fin_id) &&
|
|
|
|
!bcmp((char *)fin, (char *)&fra->fra_info, FI_CSIZE)) {
|
1997-07-06 09:02:26 +04:00
|
|
|
/*
|
|
|
|
* Avoid feedback loop.
|
|
|
|
*/
|
2002-01-24 11:21:30 +03:00
|
|
|
if (!(pass = fra->fra_pass) || (pass & FR_AUTH))
|
1997-07-06 09:02:26 +04:00
|
|
|
pass = FR_BLOCK;
|
2002-01-24 11:21:30 +03:00
|
|
|
/*
|
|
|
|
* Create a dummy rule for the stateful checking to
|
|
|
|
* use and return. Zero out any values we don't
|
|
|
|
* trust from userland!
|
|
|
|
*/
|
|
|
|
if ((pass & FR_KEEPSTATE) || ((pass & FR_KEEPFRAG) &&
|
|
|
|
(fin->fin_fi.fi_fl & FI_FRAG))) {
|
|
|
|
KMALLOC(fr, frentry_t *);
|
|
|
|
if (fr) {
|
|
|
|
bcopy((char *)fra->fra_info.fin_fr,
|
|
|
|
fr, sizeof(*fr));
|
|
|
|
fr->fr_grp = NULL;
|
|
|
|
fr->fr_ifa = fin->fin_ifp;
|
|
|
|
fr->fr_func = NULL;
|
|
|
|
fr->fr_ref = 1;
|
|
|
|
fr->fr_flags = pass;
|
|
|
|
#if BSD >= 199306
|
|
|
|
fr->fr_oifa = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
fr = fra->fra_info.fin_fr;
|
|
|
|
fin->fin_fr = fr;
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
|
|
|
WRITE_ENTER(&ipf_auth);
|
2002-01-24 11:21:30 +03:00
|
|
|
if (fr && fr != fra->fra_info.fin_fr) {
|
|
|
|
fr->fr_next = fr_authlist;
|
|
|
|
fr_authlist = fr;
|
|
|
|
}
|
1997-07-06 09:02:26 +04:00
|
|
|
fr_authstats.fas_hits++;
|
2002-01-24 11:21:30 +03:00
|
|
|
fra->fra_index = -1;
|
1997-07-06 09:02:26 +04:00
|
|
|
fr_authused--;
|
|
|
|
if (i == fr_authstart) {
|
2002-01-24 11:21:30 +03:00
|
|
|
while (fra->fra_index == -1) {
|
1997-07-06 09:02:26 +04:00
|
|
|
i++;
|
2002-01-24 11:21:30 +03:00
|
|
|
fra++;
|
|
|
|
if (i == FR_NUMAUTH) {
|
1997-07-06 09:02:26 +04:00
|
|
|
i = 0;
|
2002-01-24 11:21:30 +03:00
|
|
|
fra = fr_auth;
|
|
|
|
}
|
1997-07-06 09:02:26 +04:00
|
|
|
fr_authstart = i;
|
|
|
|
if (i == fr_authend)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fr_authstart == fr_authend) {
|
|
|
|
fr_authnext = 0;
|
|
|
|
fr_authstart = fr_authend = 0;
|
|
|
|
}
|
|
|
|
}
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
if (i == FR_NUMAUTH)
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
fr_authstats.fas_miss++;
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if we have room in the auth array to hold details for another packet.
|
|
|
|
* If we do, store it and wake up any user programs which are waiting to
|
|
|
|
* hear about these events.
|
|
|
|
*/
|
2000-05-23 10:07:42 +04:00
|
|
|
int fr_newauth(m, fin, ip)
|
1997-07-06 09:02:26 +04:00
|
|
|
mb_t *m;
|
|
|
|
fr_info_t *fin;
|
|
|
|
ip_t *ip;
|
|
|
|
{
|
2000-05-23 10:07:42 +04:00
|
|
|
#if defined(_KERNEL) && SOLARIS
|
|
|
|
qif_t *qif = fin->fin_qif;
|
|
|
|
#endif
|
2002-01-24 11:21:30 +03:00
|
|
|
frauth_t *fra;
|
1997-07-06 09:02:26 +04:00
|
|
|
int i;
|
|
|
|
|
2000-05-03 15:12:03 +04:00
|
|
|
if (fr_auth_lock)
|
|
|
|
return 0;
|
|
|
|
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_auth);
|
1999-12-12 14:11:15 +03:00
|
|
|
if (fr_authstart > fr_authend) {
|
1997-07-06 09:02:26 +04:00
|
|
|
fr_authstats.fas_nospace++;
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
return 0;
|
1999-12-12 14:11:15 +03:00
|
|
|
} else {
|
2002-01-24 11:21:30 +03:00
|
|
|
if (fr_authused == FR_NUMAUTH) {
|
1999-12-12 14:11:15 +03:00
|
|
|
fr_authstats.fas_nospace++;
|
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
|
|
|
return 0;
|
|
|
|
}
|
1997-07-06 09:02:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fr_authstats.fas_added++;
|
|
|
|
fr_authused++;
|
|
|
|
i = fr_authend++;
|
|
|
|
if (fr_authend == FR_NUMAUTH)
|
|
|
|
fr_authend = 0;
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
2002-01-24 11:21:30 +03:00
|
|
|
fra = fr_auth + i;
|
|
|
|
fra->fra_index = i;
|
|
|
|
fra->fra_pass = 0;
|
|
|
|
fra->fra_age = fr_defaultauthage;
|
|
|
|
bcopy((char *)fin, (char *)&fra->fra_info, sizeof(*fin));
|
|
|
|
#if SOLARIS && defined(_KERNEL)
|
|
|
|
# if !defined(sparc)
|
1997-07-06 09:02:26 +04:00
|
|
|
/*
|
|
|
|
* No need to copyback here as we want to undo the changes, not keep
|
|
|
|
* them.
|
|
|
|
*/
|
2000-05-03 15:12:03 +04:00
|
|
|
if ((ip == (ip_t *)m->b_rptr) && (ip->ip_v == 4))
|
1997-07-06 09:02:26 +04:00
|
|
|
{
|
2002-01-24 11:23:40 +03:00
|
|
|
register u_short bo;
|
1997-07-06 09:02:26 +04:00
|
|
|
|
|
|
|
bo = ip->ip_len;
|
|
|
|
ip->ip_len = htons(bo);
|
|
|
|
bo = ip->ip_off;
|
|
|
|
ip->ip_off = htons(bo);
|
|
|
|
}
|
2002-01-24 11:21:30 +03:00
|
|
|
# endif
|
1997-07-06 09:02:26 +04:00
|
|
|
m->b_rptr -= qif->qf_off;
|
|
|
|
fr_authpkts[i] = *(mblk_t **)fin->fin_mp;
|
2002-01-24 11:21:30 +03:00
|
|
|
fra->fra_q = qif->qf_q;
|
1997-07-06 09:02:26 +04:00
|
|
|
cv_signal(&ipfauthwait);
|
|
|
|
#else
|
2002-01-24 11:21:30 +03:00
|
|
|
# if defined(BSD) && !defined(sparc) && (BSD >= 199306)
|
|
|
|
if (!fin->fin_out) {
|
|
|
|
HTONS(ip->ip_len);
|
|
|
|
HTONS(ip->ip_off);
|
|
|
|
}
|
|
|
|
# endif
|
1997-07-06 09:02:26 +04:00
|
|
|
fr_authpkts[i] = m;
|
|
|
|
WAKEUP(&fr_authnext);
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-24 11:21:30 +03:00
|
|
|
int fr_auth_ioctl(data, mode, cmd, fr, frptr)
|
1997-07-06 09:02:26 +04:00
|
|
|
caddr_t data;
|
2002-01-24 11:21:30 +03:00
|
|
|
int mode;
|
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
|
1997-07-06 09:02:26 +04:00
|
|
|
u_long cmd;
|
|
|
|
#else
|
|
|
|
int cmd;
|
|
|
|
#endif
|
|
|
|
frentry_t *fr, **frptr;
|
|
|
|
{
|
|
|
|
mb_t *m;
|
2000-05-03 15:12:03 +04:00
|
|
|
#if defined(_KERNEL) && !SOLARIS
|
1997-07-06 09:02:26 +04:00
|
|
|
struct ifqueue *ifq;
|
2002-01-24 11:21:30 +03:00
|
|
|
int s;
|
1997-07-06 09:02:26 +04:00
|
|
|
#endif
|
2002-01-24 11:21:30 +03:00
|
|
|
frauth_t auth, *au = &auth, *fra;
|
1997-07-06 09:02:26 +04:00
|
|
|
frauthent_t *fae, **faep;
|
|
|
|
int i, error = 0;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2000-05-03 15:12:03 +04:00
|
|
|
case SIOCSTLCK :
|
|
|
|
error = fr_lock(data, &fr_auth_lock);
|
|
|
|
break;
|
1997-07-06 09:02:26 +04:00
|
|
|
case SIOCINIFR :
|
|
|
|
case SIOCRMIFR :
|
|
|
|
case SIOCADIFR :
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
case SIOCINAFR :
|
2000-05-03 15:12:03 +04:00
|
|
|
error = EINVAL;
|
|
|
|
break;
|
1997-07-06 09:02:26 +04:00
|
|
|
case SIOCRMAFR :
|
|
|
|
case SIOCADAFR :
|
|
|
|
for (faep = &fae_list; (fae = *faep); )
|
|
|
|
if (&fae->fae_fr == fr)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
faep = &fae->fae_next;
|
|
|
|
if (cmd == SIOCRMAFR) {
|
2002-01-24 11:21:30 +03:00
|
|
|
if (!fr || !frptr)
|
|
|
|
error = EINVAL;
|
|
|
|
else if (!fae)
|
1997-07-06 09:02:26 +04:00
|
|
|
error = ESRCH;
|
|
|
|
else {
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_auth);
|
2002-01-24 11:21:30 +03:00
|
|
|
SPL_NET(s);
|
1997-07-06 09:02:26 +04:00
|
|
|
*faep = fae->fae_next;
|
|
|
|
*frptr = fr->fr_next;
|
2002-01-24 11:21:30 +03:00
|
|
|
SPL_X(s);
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
1997-07-08 02:21:21 +04:00
|
|
|
KFREE(fae);
|
1997-07-06 09:02:26 +04:00
|
|
|
}
|
2002-01-24 11:21:30 +03:00
|
|
|
} else if (fr && frptr) {
|
1999-12-12 14:11:15 +03:00
|
|
|
KMALLOC(fae, frauthent_t *);
|
1997-07-06 09:02:26 +04:00
|
|
|
if (fae != NULL) {
|
2000-05-03 15:12:03 +04:00
|
|
|
bcopy((char *)fr, (char *)&fae->fae_fr,
|
|
|
|
sizeof(*fr));
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_auth);
|
2002-01-24 11:21:30 +03:00
|
|
|
SPL_NET(s);
|
1999-12-12 14:11:15 +03:00
|
|
|
fae->fae_age = fr_defaultauthage;
|
1997-07-06 09:02:26 +04:00
|
|
|
fae->fae_fr.fr_hits = 0;
|
|
|
|
fae->fae_fr.fr_next = *frptr;
|
|
|
|
*frptr = &fae->fae_fr;
|
|
|
|
fae->fae_next = *faep;
|
|
|
|
*faep = fae;
|
1999-12-12 14:11:15 +03:00
|
|
|
ipauth = &fae_list->fae_fr;
|
2002-01-24 11:21:30 +03:00
|
|
|
SPL_X(s);
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
} else
|
|
|
|
error = ENOMEM;
|
2002-01-24 11:21:30 +03:00
|
|
|
} else
|
|
|
|
error = EINVAL;
|
1997-07-06 09:02:26 +04:00
|
|
|
break;
|
|
|
|
case SIOCATHST:
|
1999-12-12 14:11:15 +03:00
|
|
|
fr_authstats.fas_faelist = fae_list;
|
2000-05-03 15:12:03 +04:00
|
|
|
error = IWCOPYPTR((char *)&fr_authstats, data,
|
|
|
|
sizeof(fr_authstats));
|
1997-07-06 09:02:26 +04:00
|
|
|
break;
|
|
|
|
case SIOCAUTHW:
|
2002-01-24 11:21:30 +03:00
|
|
|
if (!(mode & FWRITE)) {
|
|
|
|
error = EPERM;
|
|
|
|
break;
|
|
|
|
}
|
1997-07-06 09:02:26 +04:00
|
|
|
fr_authioctlloop:
|
1998-07-12 19:23:59 +04:00
|
|
|
READ_ENTER(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
if ((fr_authnext != fr_authend) && fr_authpkts[fr_authnext]) {
|
2000-05-03 15:12:03 +04:00
|
|
|
error = IWCOPYPTR((char *)&fr_auth[fr_authnext], data,
|
2001-03-26 10:11:46 +04:00
|
|
|
sizeof(frauth_t));
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
2000-05-03 15:12:03 +04:00
|
|
|
if (error)
|
|
|
|
break;
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_auth);
|
2002-01-24 11:21:30 +03:00
|
|
|
SPL_NET(s);
|
1998-07-12 19:23:59 +04:00
|
|
|
fr_authnext++;
|
1997-07-06 09:02:26 +04:00
|
|
|
if (fr_authnext == FR_NUMAUTH)
|
|
|
|
fr_authnext = 0;
|
2002-01-24 11:21:30 +03:00
|
|
|
SPL_X(s);
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2002-05-02 21:11:37 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
#ifdef _KERNEL
|
|
|
|
# if SOLARIS
|
1998-07-12 19:23:59 +04:00
|
|
|
mutex_enter(&ipf_authmx);
|
|
|
|
if (!cv_wait_sig(&ipfauthwait, &ipf_authmx)) {
|
|
|
|
mutex_exit(&ipf_authmx);
|
1997-07-06 09:02:26 +04:00
|
|
|
return EINTR;
|
|
|
|
}
|
1998-07-12 19:23:59 +04:00
|
|
|
mutex_exit(&ipf_authmx);
|
1997-07-06 09:02:26 +04:00
|
|
|
# else
|
|
|
|
error = SLEEP(&fr_authnext, "fr_authnext");
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
if (!error)
|
|
|
|
goto fr_authioctlloop;
|
|
|
|
break;
|
|
|
|
case SIOCAUTHR:
|
2002-01-24 11:21:30 +03:00
|
|
|
if (!(mode & FWRITE)) {
|
|
|
|
error = EPERM;
|
|
|
|
break;
|
|
|
|
}
|
2000-05-03 15:12:03 +04:00
|
|
|
error = IRCOPYPTR(data, (caddr_t)&auth, sizeof(auth));
|
|
|
|
if (error)
|
|
|
|
return error;
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_auth);
|
2002-01-24 11:21:30 +03:00
|
|
|
SPL_NET(s);
|
1997-07-06 09:02:26 +04:00
|
|
|
i = au->fra_index;
|
2002-01-24 11:21:30 +03:00
|
|
|
fra = fr_auth + i;
|
1997-07-06 09:02:26 +04:00
|
|
|
if ((i < 0) || (i > FR_NUMAUTH) ||
|
2002-01-24 11:21:30 +03:00
|
|
|
(fra->fra_info.fin_id != au->fra_info.fin_id)) {
|
|
|
|
SPL_X(s);
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
m = fr_authpkts[i];
|
2002-01-24 11:21:30 +03:00
|
|
|
fra->fra_index = -2;
|
|
|
|
fra->fra_pass = au->fra_pass;
|
1997-07-06 09:02:26 +04:00
|
|
|
fr_authpkts[i] = NULL;
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
2002-01-24 11:21:30 +03:00
|
|
|
#ifdef _KERNEL
|
1997-07-06 09:02:26 +04:00
|
|
|
if (m && au->fra_info.fin_out) {
|
2002-01-24 11:21:30 +03:00
|
|
|
# if SOLARIS
|
2002-05-02 21:11:37 +04:00
|
|
|
error = (fr_qout(fra->fra_q, m) == 0) ? EINVAL : 0;
|
2002-01-24 11:21:30 +03:00
|
|
|
# else /* SOLARIS */
|
|
|
|
struct route ro;
|
|
|
|
|
|
|
|
bzero((char *)&ro, sizeof(ro));
|
|
|
|
# if ((_BSDI_VERSION >= 199802) && (_BSDI_VERSION < 200005)) || \
|
2002-03-14 15:32:36 +03:00
|
|
|
defined(__OpenBSD__) || (defined(IRIX) && (IRIX >= 605))
|
2002-01-24 11:21:30 +03:00
|
|
|
error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL,
|
1999-12-12 14:11:15 +03:00
|
|
|
NULL);
|
2002-01-24 11:21:30 +03:00
|
|
|
# else
|
2003-08-15 07:42:00 +04:00
|
|
|
error = ip_output(m, NULL, &ro, IP_FORWARDING,
|
|
|
|
(struct ip_moptions *)0, (struct inpcb *)0);
|
2002-01-24 11:21:30 +03:00
|
|
|
# endif
|
|
|
|
if (ro.ro_rt) {
|
|
|
|
RTFREE(ro.ro_rt);
|
|
|
|
}
|
|
|
|
# endif /* SOLARIS */
|
1997-07-06 09:02:26 +04:00
|
|
|
if (error)
|
|
|
|
fr_authstats.fas_sendfail++;
|
|
|
|
else
|
|
|
|
fr_authstats.fas_sendok++;
|
|
|
|
} else if (m) {
|
|
|
|
# if SOLARIS
|
2002-05-02 21:11:37 +04:00
|
|
|
error = (fr_qin(fra->fra_q, m) == 0) ? EINVAL : 0;
|
1997-07-06 09:02:26 +04:00
|
|
|
# else /* SOLARIS */
|
|
|
|
ifq = &ipintrq;
|
|
|
|
if (IF_QFULL(ifq)) {
|
|
|
|
IF_DROP(ifq);
|
|
|
|
m_freem(m);
|
|
|
|
error = ENOBUFS;
|
|
|
|
} else {
|
|
|
|
IF_ENQUEUE(ifq, m);
|
2002-03-14 15:32:36 +03:00
|
|
|
# if IRIX < 605
|
1997-07-06 09:02:26 +04:00
|
|
|
schednetisr(NETISR_IP);
|
2002-03-14 15:32:36 +03:00
|
|
|
# endif
|
1997-07-06 09:02:26 +04:00
|
|
|
}
|
|
|
|
# endif /* SOLARIS */
|
|
|
|
if (error)
|
|
|
|
fr_authstats.fas_quefail++;
|
|
|
|
else
|
|
|
|
fr_authstats.fas_queok++;
|
|
|
|
} else
|
|
|
|
error = EINVAL;
|
|
|
|
# if SOLARIS
|
|
|
|
if (error)
|
|
|
|
error = EINVAL;
|
|
|
|
# else
|
|
|
|
/*
|
|
|
|
* If we experience an error which will result in the packet
|
|
|
|
* not being processed, make sure we advance to the next one.
|
2002-09-19 12:12:43 +04:00
|
|
|
*/
|
1997-07-06 09:02:26 +04:00
|
|
|
if (error == ENOBUFS) {
|
|
|
|
fr_authused--;
|
2002-01-24 11:21:30 +03:00
|
|
|
fra->fra_index = -1;
|
|
|
|
fra->fra_pass = 0;
|
1997-07-06 09:02:26 +04:00
|
|
|
if (i == fr_authstart) {
|
2002-01-24 11:21:30 +03:00
|
|
|
while (fra->fra_index == -1) {
|
1997-07-06 09:02:26 +04:00
|
|
|
i++;
|
|
|
|
if (i == FR_NUMAUTH)
|
|
|
|
i = 0;
|
|
|
|
fr_authstart = i;
|
|
|
|
if (i == fr_authend)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fr_authstart == fr_authend) {
|
|
|
|
fr_authnext = 0;
|
|
|
|
fr_authstart = fr_authend = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#endif /* _KERNEL */
|
2002-01-24 11:21:30 +03:00
|
|
|
SPL_X(s);
|
1997-07-06 09:02:26 +04:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free all network buffer memory used to keep saved packets.
|
|
|
|
*/
|
|
|
|
void fr_authunload()
|
|
|
|
{
|
2002-01-24 11:21:30 +03:00
|
|
|
register int i;
|
|
|
|
register frauthent_t *fae, **faep;
|
|
|
|
frentry_t *fr, **frp;
|
1997-07-06 09:02:26 +04:00
|
|
|
mb_t *m;
|
|
|
|
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
for (i = 0; i < FR_NUMAUTH; i++) {
|
|
|
|
if ((m = fr_authpkts[i])) {
|
|
|
|
FREE_MB_T(m);
|
|
|
|
fr_authpkts[i] = NULL;
|
|
|
|
fr_auth[i].fra_index = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (faep = &fae_list; (fae = *faep); ) {
|
|
|
|
*faep = fae->fae_next;
|
|
|
|
KFREE(fae);
|
|
|
|
}
|
1999-12-12 14:11:15 +03:00
|
|
|
ipauth = NULL;
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
2002-01-24 11:21:30 +03:00
|
|
|
|
|
|
|
if (fr_authlist) {
|
|
|
|
/*
|
|
|
|
* We *MuST* reget ipf_auth because otherwise we won't get the
|
|
|
|
* locks in the right order and risk deadlock.
|
|
|
|
* We need ipf_mutex here to prevent a rule from using it
|
|
|
|
* inside fr_check().
|
|
|
|
*/
|
|
|
|
WRITE_ENTER(&ipf_mutex);
|
|
|
|
WRITE_ENTER(&ipf_auth);
|
|
|
|
for (frp = &fr_authlist; (fr = *frp); ) {
|
|
|
|
if (fr->fr_ref == 1) {
|
|
|
|
*frp = fr->fr_next;
|
|
|
|
KFREE(fr);
|
|
|
|
} else
|
|
|
|
frp = &fr->fr_next;
|
|
|
|
}
|
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
|
|
|
RWLOCK_EXIT(&ipf_mutex);
|
|
|
|
}
|
1997-07-06 09:02:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Slowly expire held auth records. Timeouts are set
|
|
|
|
* in expectation of this being called twice per second.
|
|
|
|
*/
|
|
|
|
void fr_authexpire()
|
|
|
|
{
|
2002-01-24 11:21:30 +03:00
|
|
|
register int i;
|
|
|
|
register frauth_t *fra;
|
|
|
|
register frauthent_t *fae, **faep;
|
|
|
|
register frentry_t *fr, **frp;
|
1997-07-06 09:02:26 +04:00
|
|
|
mb_t *m;
|
2002-01-24 11:21:30 +03:00
|
|
|
#if !SOLARIS && defined(_KERNEL)
|
1997-07-06 09:02:26 +04:00
|
|
|
int s;
|
|
|
|
#endif
|
|
|
|
|
2000-05-03 15:12:03 +04:00
|
|
|
if (fr_auth_lock)
|
|
|
|
return;
|
|
|
|
|
1997-10-30 19:08:54 +03:00
|
|
|
SPL_NET(s);
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_auth);
|
1997-07-06 09:02:26 +04:00
|
|
|
for (i = 0, fra = fr_auth; i < FR_NUMAUTH; i++, fra++) {
|
|
|
|
if ((!--fra->fra_age) && (m = fr_authpkts[i])) {
|
|
|
|
FREE_MB_T(m);
|
|
|
|
fr_authpkts[i] = NULL;
|
|
|
|
fr_auth[i].fra_index = -1;
|
|
|
|
fr_authstats.fas_expire++;
|
|
|
|
fr_authused--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (faep = &fae_list; (fae = *faep); ) {
|
1999-12-12 14:11:15 +03:00
|
|
|
if (!--fae->fae_age) {
|
1997-07-06 09:02:26 +04:00
|
|
|
*faep = fae->fae_next;
|
|
|
|
KFREE(fae);
|
|
|
|
fr_authstats.fas_expire++;
|
|
|
|
} else
|
|
|
|
faep = &fae->fae_next;
|
|
|
|
}
|
2002-09-19 12:08:14 +04:00
|
|
|
if (fae_list != NULL)
|
|
|
|
ipauth = &fae_list->fae_fr;
|
|
|
|
else
|
|
|
|
ipauth = NULL;
|
2002-01-24 11:21:30 +03:00
|
|
|
|
|
|
|
for (frp = &fr_authlist; (fr = *frp); ) {
|
|
|
|
if (fr->fr_ref == 1) {
|
|
|
|
*frp = fr->fr_next;
|
|
|
|
KFREE(fr);
|
|
|
|
} else
|
|
|
|
frp = &fr->fr_next;
|
|
|
|
}
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_auth);
|
1997-10-30 19:08:54 +03:00
|
|
|
SPL_X(s);
|
1997-07-06 09:02:26 +04:00
|
|
|
}
|