2000-05-03 15:12:03 +04:00
|
|
|
/* $NetBSD: ip_frag.c,v 1.18 2000/05/03 11:12:07 veego Exp $ */
|
1997-01-06 00:32:18 +03:00
|
|
|
|
1997-01-05 16:47:59 +03:00
|
|
|
/*
|
2000-05-03 15:12:03 +04:00
|
|
|
* Copyright (C) 1993-2000 by Darren Reed.
|
1997-01-05 16:47:59 +03: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__)
|
2000-05-03 15:12:03 +04:00
|
|
|
static const char rcsid[] = "$NetBSD: ip_frag.c,v 1.18 2000/05/03 11:12:07 veego Exp $";
|
1999-02-02 22:57:30 +03:00
|
|
|
#else
|
2000-05-03 15:12:03 +04:00
|
|
|
static const char sccsid[] = "@(#)ip_frag.c 1.11 3/24/96 (C) 1993-2000 Darren Reed";
|
|
|
|
static const char rcsid[] = "@(#)Id: ip_frag.c,v 2.10.2.1 2000/04/28 14:56:51 darrenr Exp";
|
1997-01-05 16:47:59 +03:00
|
|
|
#endif
|
1999-02-02 22:57:30 +03:00
|
|
|
#endif
|
1997-01-05 16:47:59 +03:00
|
|
|
|
1999-12-12 14:11:15 +03:00
|
|
|
#if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL)
|
|
|
|
# define _KERNEL
|
|
|
|
#endif
|
|
|
|
|
1997-01-05 16:47:59 +03:00
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
1997-05-25 16:40:11 +04:00
|
|
|
#include <sys/time.h>
|
1997-01-05 16:47:59 +03:00
|
|
|
#include <sys/file.h>
|
1998-11-22 18:17:18 +03:00
|
|
|
#if !defined(_KERNEL) && !defined(KERNEL)
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <stdlib.h>
|
|
|
|
#endif
|
2000-05-03 15:12:03 +04:00
|
|
|
#if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
|
1999-12-12 14:11:15 +03:00
|
|
|
# include <sys/filio.h>
|
|
|
|
# include <sys/fcntl.h>
|
1997-05-25 16:40:11 +04:00
|
|
|
#else
|
1999-12-12 14:11:15 +03:00
|
|
|
# include <sys/ioctl.h>
|
1997-05-25 16:40:11 +04:00
|
|
|
#endif
|
1997-01-05 16:47:59 +03:00
|
|
|
#include <sys/uio.h>
|
1997-11-14 15:40:06 +03:00
|
|
|
#ifndef linux
|
1999-12-12 14:11:15 +03:00
|
|
|
# include <sys/protosw.h>
|
1997-11-14 15:40:06 +03:00
|
|
|
#endif
|
1997-01-05 16:47:59 +03:00
|
|
|
#include <sys/socket.h>
|
1997-11-14 15:40:06 +03:00
|
|
|
#if defined(_KERNEL) && !defined(linux)
|
1997-01-05 16:47:59 +03:00
|
|
|
# include <sys/systm.h>
|
|
|
|
#endif
|
|
|
|
#if !defined(__SVR4) && !defined(__svr4__)
|
1999-12-12 14:11:15 +03:00
|
|
|
# if defined(_KERNEL) && !defined(__sgi)
|
|
|
|
# include <sys/kernel.h>
|
|
|
|
# endif
|
1997-11-14 15:40:06 +03:00
|
|
|
# ifndef linux
|
|
|
|
# include <sys/mbuf.h>
|
|
|
|
# endif
|
1997-01-05 16:47:59 +03:00
|
|
|
#else
|
|
|
|
# include <sys/byteorder.h>
|
1999-12-12 14:11:15 +03:00
|
|
|
# ifdef _KERNEL
|
|
|
|
# include <sys/dditypes.h>
|
|
|
|
# endif
|
1997-01-05 16:47:59 +03:00
|
|
|
# include <sys/stream.h>
|
|
|
|
# include <sys/kmem.h>
|
|
|
|
#endif
|
|
|
|
#include <net/if.h>
|
|
|
|
#ifdef sun
|
1999-12-12 14:11:15 +03:00
|
|
|
# include <net/af.h>
|
1997-01-05 16:47:59 +03:00
|
|
|
#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
|
1999-12-12 14:11:15 +03:00
|
|
|
# include <netinet/ip_var.h>
|
1997-11-14 15:40:06 +03:00
|
|
|
#endif
|
1997-01-05 16:47:59 +03:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
1997-05-25 16:40:11 +04:00
|
|
|
#include "netinet/ip_compat.h"
|
1997-11-14 15:40:06 +03:00
|
|
|
#include <netinet/tcpip.h>
|
1997-05-25 16:40:11 +04:00
|
|
|
#include "netinet/ip_fil.h"
|
|
|
|
#include "netinet/ip_proxy.h"
|
|
|
|
#include "netinet/ip_nat.h"
|
|
|
|
#include "netinet/ip_frag.h"
|
|
|
|
#include "netinet/ip_state.h"
|
1997-07-05 09:38:14 +04:00
|
|
|
#include "netinet/ip_auth.h"
|
1999-12-12 14:11:15 +03:00
|
|
|
#if (__FreeBSD_version >= 300000)
|
|
|
|
# include <sys/malloc.h>
|
|
|
|
# if (defined(KERNEL) || defined(_KERNEL))
|
|
|
|
# ifndef IPFILTER_LKM
|
|
|
|
# include <sys/libkern.h>
|
|
|
|
# include <sys/systm.h>
|
|
|
|
# endif
|
|
|
|
extern struct callout_handle ipfr_slowtimer_ch;
|
|
|
|
# endif
|
|
|
|
#endif
|
2000-03-23 10:01:25 +03:00
|
|
|
#if defined(__NetBSD__)
|
2000-03-25 01:40:11 +03:00
|
|
|
#include <sys/callout.h>
|
2000-03-23 10:01:25 +03:00
|
|
|
extern struct callout ipfr_slowtimer_ch;
|
|
|
|
#endif
|
1999-12-12 14:11:15 +03:00
|
|
|
|
1997-01-05 16:47:59 +03:00
|
|
|
|
2000-05-03 15:12:03 +04:00
|
|
|
static ipfr_t *ipfr_heads[IPFT_SIZE];
|
|
|
|
static ipfr_t *ipfr_nattab[IPFT_SIZE];
|
|
|
|
static ipfrstat_t ipfr_stats;
|
|
|
|
static int ipfr_inuse = 0;
|
|
|
|
|
|
|
|
int fr_ipfrttl = 120; /* 60 seconds */
|
|
|
|
int fr_frag_lock = 0;
|
|
|
|
|
1997-01-05 16:47:59 +03:00
|
|
|
#ifdef _KERNEL
|
1999-12-12 14:11:15 +03:00
|
|
|
# if SOLARIS2 >= 7
|
|
|
|
extern timeout_id_t ipfr_timer_id;
|
|
|
|
# else
|
1997-01-05 16:47:59 +03:00
|
|
|
extern int ipfr_timer_id;
|
1999-12-12 14:11:15 +03:00
|
|
|
# endif
|
1997-01-05 16:47:59 +03:00
|
|
|
#endif
|
1997-10-30 19:08:54 +03:00
|
|
|
#if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
|
1999-12-12 14:11:15 +03:00
|
|
|
extern KRWLOCK_T ipf_frag, ipf_natfrag, ipf_nat, ipf_mutex;
|
|
|
|
# if SOLARIS
|
|
|
|
extern KRWLOCK_T ipf_solaris;
|
|
|
|
# else
|
|
|
|
KRWLOCK_T ipf_solaris;
|
|
|
|
# endif
|
1998-07-12 19:23:59 +04:00
|
|
|
extern kmutex_t ipf_rw;
|
1997-01-05 16:47:59 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-12-12 14:11:15 +03:00
|
|
|
static ipfr_t *ipfr_new __P((ip_t *, fr_info_t *, u_int, ipfr_t **));
|
1997-05-25 16:40:11 +04:00
|
|
|
static ipfr_t *ipfr_lookup __P((ip_t *, fr_info_t *, ipfr_t **));
|
1999-12-12 14:11:15 +03:00
|
|
|
static void ipfr_delete __P((ipfr_t *));
|
1997-05-25 16:40:11 +04:00
|
|
|
|
|
|
|
|
1997-01-05 16:47:59 +03:00
|
|
|
ipfrstat_t *ipfr_fragstats()
|
|
|
|
{
|
|
|
|
ipfr_stats.ifs_table = ipfr_heads;
|
1997-05-25 16:40:11 +04:00
|
|
|
ipfr_stats.ifs_nattab = ipfr_nattab;
|
1997-01-05 16:47:59 +03:00
|
|
|
ipfr_stats.ifs_inuse = ipfr_inuse;
|
|
|
|
return &ipfr_stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add a new entry to the fragment cache, registering it as having come
|
|
|
|
* through this box, with the result of the filter operation.
|
|
|
|
*/
|
1997-05-25 16:40:11 +04:00
|
|
|
static ipfr_t *ipfr_new(ip, fin, pass, table)
|
1997-01-05 16:47:59 +03:00
|
|
|
ip_t *ip;
|
|
|
|
fr_info_t *fin;
|
1999-12-12 14:11:15 +03:00
|
|
|
u_int pass;
|
1997-05-25 16:40:11 +04:00
|
|
|
ipfr_t *table[];
|
1997-01-05 16:47:59 +03:00
|
|
|
{
|
1999-12-12 14:11:15 +03:00
|
|
|
ipfr_t **fp, *fra, frag;
|
1997-01-05 16:47:59 +03:00
|
|
|
u_int idx;
|
|
|
|
|
|
|
|
frag.ipfr_p = ip->ip_p;
|
|
|
|
idx = ip->ip_p;
|
|
|
|
frag.ipfr_id = ip->ip_id;
|
|
|
|
idx += ip->ip_id;
|
|
|
|
frag.ipfr_tos = ip->ip_tos;
|
|
|
|
frag.ipfr_src.s_addr = ip->ip_src.s_addr;
|
|
|
|
idx += ip->ip_src.s_addr;
|
|
|
|
frag.ipfr_dst.s_addr = ip->ip_dst.s_addr;
|
|
|
|
idx += ip->ip_dst.s_addr;
|
|
|
|
idx *= 127;
|
|
|
|
idx %= IPFT_SIZE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first, make sure it isn't already there...
|
|
|
|
*/
|
1999-12-12 14:11:15 +03:00
|
|
|
for (fp = &table[idx]; (fra = *fp); fp = &fra->ipfr_next)
|
|
|
|
if (!bcmp((char *)&frag.ipfr_src, (char *)&fra->ipfr_src,
|
1997-01-05 16:47:59 +03:00
|
|
|
IPFR_CMPSZ)) {
|
2000-05-03 15:12:03 +04:00
|
|
|
ATOMIC_INCL(ipfr_stats.ifs_exists);
|
1997-05-25 16:40:11 +04:00
|
|
|
return NULL;
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
|
|
|
|
1997-05-25 16:40:11 +04:00
|
|
|
/*
|
|
|
|
* allocate some memory, if possible, if not, just record that we
|
|
|
|
* failed to do so.
|
|
|
|
*/
|
1999-12-12 14:11:15 +03:00
|
|
|
KMALLOC(fra, ipfr_t *);
|
|
|
|
if (fra == NULL) {
|
2000-05-03 15:12:03 +04:00
|
|
|
ATOMIC_INCL(ipfr_stats.ifs_nomem);
|
1997-05-25 16:40:11 +04:00
|
|
|
return NULL;
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
1997-05-25 16:40:11 +04:00
|
|
|
|
1999-12-12 14:11:15 +03:00
|
|
|
if ((fra->ipfr_rule = fin->fin_fr) != NULL) {
|
2000-05-03 15:12:03 +04:00
|
|
|
ATOMIC_INC32(fin->fin_fr->fr_ref);
|
1999-12-12 14:11:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-05-25 16:40:11 +04:00
|
|
|
/*
|
|
|
|
* Instert the fragment into the fragment table, copy the struct used
|
|
|
|
* in the search using bcopy rather than reassign each field.
|
|
|
|
* Set the ttl to the default and mask out logging from "pass"
|
|
|
|
*/
|
1999-12-12 14:11:15 +03:00
|
|
|
if ((fra->ipfr_next = table[idx]))
|
|
|
|
table[idx]->ipfr_prev = fra;
|
|
|
|
fra->ipfr_prev = NULL;
|
|
|
|
fra->ipfr_data = NULL;
|
|
|
|
table[idx] = fra;
|
|
|
|
bcopy((char *)&frag.ipfr_src, (char *)&fra->ipfr_src, IPFR_CMPSZ);
|
|
|
|
fra->ipfr_ttl = fr_ipfrttl;
|
1997-05-25 16:40:11 +04:00
|
|
|
/*
|
|
|
|
* Compute the offset of the expected start of the next packet.
|
|
|
|
*/
|
1999-12-12 14:11:15 +03:00
|
|
|
fra->ipfr_off = (ip->ip_off & IP_OFFMASK) + (fin->fin_dlen >> 3);
|
2000-05-03 15:12:03 +04:00
|
|
|
ATOMIC_INCL(ipfr_stats.ifs_new);
|
|
|
|
ATOMIC_INC32(ipfr_inuse);
|
1999-12-12 14:11:15 +03:00
|
|
|
return fra;
|
1997-05-25 16:40:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ipfr_newfrag(ip, fin, pass)
|
|
|
|
ip_t *ip;
|
|
|
|
fr_info_t *fin;
|
1999-12-12 14:11:15 +03:00
|
|
|
u_int pass;
|
1997-05-25 16:40:11 +04:00
|
|
|
{
|
|
|
|
ipfr_t *ipf;
|
|
|
|
|
2000-05-03 15:12:03 +04:00
|
|
|
if ((ip->ip_v != 4) || (fr_frag_lock))
|
|
|
|
return NULL;
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_frag);
|
1997-05-25 16:40:11 +04:00
|
|
|
ipf = ipfr_new(ip, fin, pass, ipfr_heads);
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_frag);
|
1997-05-25 16:40:11 +04:00
|
|
|
return ipf ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ipfr_nat_newfrag(ip, fin, pass, nat)
|
|
|
|
ip_t *ip;
|
|
|
|
fr_info_t *fin;
|
1999-12-12 14:11:15 +03:00
|
|
|
u_int pass;
|
1997-05-25 16:40:11 +04:00
|
|
|
nat_t *nat;
|
|
|
|
{
|
|
|
|
ipfr_t *ipf;
|
|
|
|
|
2000-05-03 15:12:03 +04:00
|
|
|
if ((ip->ip_v != 4) || (fr_frag_lock))
|
|
|
|
return NULL;
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_natfrag);
|
1998-11-22 18:17:18 +03:00
|
|
|
ipf = ipfr_new(ip, fin, pass, ipfr_nattab);
|
|
|
|
if (ipf != NULL) {
|
1997-05-25 16:40:11 +04:00
|
|
|
ipf->ipfr_data = nat;
|
1997-10-30 19:08:54 +03:00
|
|
|
nat->nat_data = ipf;
|
1997-05-25 16:40:11 +04:00
|
|
|
}
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_natfrag);
|
1997-05-25 16:40:11 +04:00
|
|
|
return ipf ? 0 : -1;
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check the fragment cache to see if there is already a record of this packet
|
|
|
|
* with its filter result known.
|
|
|
|
*/
|
1997-05-25 16:40:11 +04:00
|
|
|
static ipfr_t *ipfr_lookup(ip, fin, table)
|
1997-01-05 16:47:59 +03:00
|
|
|
ip_t *ip;
|
|
|
|
fr_info_t *fin;
|
1997-05-25 16:40:11 +04:00
|
|
|
ipfr_t *table[];
|
1997-01-05 16:47:59 +03:00
|
|
|
{
|
|
|
|
ipfr_t *f, frag;
|
|
|
|
u_int idx;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For fragments, we record protocol, packet id, TOS and both IP#'s
|
|
|
|
* (these should all be the same for all fragments of a packet).
|
1997-05-25 16:40:11 +04:00
|
|
|
*
|
|
|
|
* build up a hash value to index the table with.
|
1997-01-05 16:47:59 +03:00
|
|
|
*/
|
|
|
|
frag.ipfr_p = ip->ip_p;
|
|
|
|
idx = ip->ip_p;
|
|
|
|
frag.ipfr_id = ip->ip_id;
|
|
|
|
idx += ip->ip_id;
|
|
|
|
frag.ipfr_tos = ip->ip_tos;
|
|
|
|
frag.ipfr_src.s_addr = ip->ip_src.s_addr;
|
|
|
|
idx += ip->ip_src.s_addr;
|
|
|
|
frag.ipfr_dst.s_addr = ip->ip_dst.s_addr;
|
|
|
|
idx += ip->ip_dst.s_addr;
|
|
|
|
idx *= 127;
|
|
|
|
idx %= IPFT_SIZE;
|
|
|
|
|
1997-05-25 16:40:11 +04:00
|
|
|
/*
|
|
|
|
* check the table, careful to only compare the right amount of data
|
|
|
|
*/
|
|
|
|
for (f = table[idx]; f; f = f->ipfr_next)
|
1997-01-05 16:47:59 +03:00
|
|
|
if (!bcmp((char *)&frag.ipfr_src, (char *)&f->ipfr_src,
|
|
|
|
IPFR_CMPSZ)) {
|
|
|
|
u_short atoff, off;
|
|
|
|
|
1997-05-25 16:40:11 +04:00
|
|
|
if (f != table[idx]) {
|
1997-01-05 16:47:59 +03:00
|
|
|
/*
|
|
|
|
* move fragment info. to the top of the list
|
|
|
|
* to speed up searches.
|
|
|
|
*/
|
|
|
|
if ((f->ipfr_prev->ipfr_next = f->ipfr_next))
|
|
|
|
f->ipfr_next->ipfr_prev = f->ipfr_prev;
|
1997-05-25 16:40:11 +04:00
|
|
|
f->ipfr_next = table[idx];
|
|
|
|
table[idx]->ipfr_prev = f;
|
1997-01-05 16:47:59 +03:00
|
|
|
f->ipfr_prev = NULL;
|
1997-05-25 16:40:11 +04:00
|
|
|
table[idx] = f;
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
1999-12-12 14:11:15 +03:00
|
|
|
off = ip->ip_off & IP_OFFMASK;
|
1997-10-30 19:08:54 +03:00
|
|
|
atoff = off + (fin->fin_dlen >> 3);
|
1997-01-05 16:47:59 +03:00
|
|
|
/*
|
|
|
|
* If we've follwed the fragments, and this is the
|
|
|
|
* last (in order), shrink expiration time.
|
|
|
|
*/
|
1999-12-12 14:11:15 +03:00
|
|
|
if (off == f->ipfr_off) {
|
|
|
|
if (!(ip->ip_off & IP_MF))
|
1997-01-05 16:47:59 +03:00
|
|
|
f->ipfr_ttl = 1;
|
|
|
|
else
|
1997-10-30 19:08:54 +03:00
|
|
|
f->ipfr_off = atoff;
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
2000-05-03 15:12:03 +04:00
|
|
|
ATOMIC_INCL(ipfr_stats.ifs_hits);
|
1997-05-25 16:40:11 +04:00
|
|
|
return f;
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
1997-05-25 16:40:11 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1997-07-05 09:38:14 +04:00
|
|
|
* functional interface for NAT lookups of the NAT fragment cache
|
1997-05-25 16:40:11 +04:00
|
|
|
*/
|
|
|
|
nat_t *ipfr_nat_knownfrag(ip, fin)
|
|
|
|
ip_t *ip;
|
|
|
|
fr_info_t *fin;
|
|
|
|
{
|
|
|
|
nat_t *nat;
|
|
|
|
ipfr_t *ipf;
|
|
|
|
|
2000-05-03 15:12:03 +04:00
|
|
|
if ((ip->ip_v != 4) || (fr_frag_lock))
|
|
|
|
return NULL;
|
1998-07-12 19:23:59 +04:00
|
|
|
READ_ENTER(&ipf_natfrag);
|
1997-07-05 09:38:14 +04:00
|
|
|
ipf = ipfr_lookup(ip, fin, ipfr_nattab);
|
1998-11-22 18:17:18 +03:00
|
|
|
if (ipf != NULL) {
|
1997-10-30 19:08:54 +03:00
|
|
|
nat = ipf->ipfr_data;
|
|
|
|
/*
|
|
|
|
* This is the last fragment for this packet.
|
|
|
|
*/
|
1998-11-22 18:17:18 +03:00
|
|
|
if ((ipf->ipfr_ttl == 1) && (nat != NULL)) {
|
1997-10-30 19:08:54 +03:00
|
|
|
nat->nat_data = NULL;
|
|
|
|
ipf->ipfr_data = NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
nat = NULL;
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_natfrag);
|
1997-05-25 16:40:11 +04:00
|
|
|
return nat;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1997-07-05 09:38:14 +04:00
|
|
|
* functional interface for normal lookups of the fragment cache
|
1997-05-25 16:40:11 +04:00
|
|
|
*/
|
1999-12-12 14:11:15 +03:00
|
|
|
frentry_t *ipfr_knownfrag(ip, fin)
|
1997-05-25 16:40:11 +04:00
|
|
|
ip_t *ip;
|
|
|
|
fr_info_t *fin;
|
|
|
|
{
|
1999-12-12 14:11:15 +03:00
|
|
|
frentry_t *fr = NULL;
|
|
|
|
ipfr_t *fra;
|
1997-05-25 16:40:11 +04:00
|
|
|
|
2000-05-03 15:12:03 +04:00
|
|
|
if ((ip->ip_v != 4) || (fr_frag_lock))
|
|
|
|
return NULL;
|
1998-07-12 19:23:59 +04:00
|
|
|
READ_ENTER(&ipf_frag);
|
1999-12-12 14:11:15 +03:00
|
|
|
fra = ipfr_lookup(ip, fin, ipfr_heads);
|
|
|
|
if (fra != NULL)
|
|
|
|
fr = fra->ipfr_rule;
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_frag);
|
1999-12-12 14:11:15 +03:00
|
|
|
return fr;
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-10-30 19:08:54 +03:00
|
|
|
/*
|
|
|
|
* forget any references to this external object.
|
|
|
|
*/
|
|
|
|
void ipfr_forget(nat)
|
|
|
|
void *nat;
|
|
|
|
{
|
|
|
|
ipfr_t *fr;
|
|
|
|
int idx;
|
|
|
|
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_natfrag);
|
1997-10-30 19:08:54 +03:00
|
|
|
for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
|
|
|
|
for (fr = ipfr_heads[idx]; fr; fr = fr->ipfr_next)
|
|
|
|
if (fr->ipfr_data == nat)
|
|
|
|
fr->ipfr_data = NULL;
|
|
|
|
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_natfrag);
|
1997-10-30 19:08:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-12 14:11:15 +03:00
|
|
|
static void ipfr_delete(fra)
|
|
|
|
ipfr_t *fra;
|
|
|
|
{
|
|
|
|
frentry_t *fr;
|
|
|
|
|
|
|
|
fr = fra->ipfr_rule;
|
|
|
|
if (fr != NULL) {
|
2000-05-03 15:12:03 +04:00
|
|
|
ATOMIC_DEC32(fr->fr_ref);
|
1999-12-12 14:11:15 +03:00
|
|
|
if (fr->fr_ref == 0)
|
|
|
|
KFREE(fr);
|
|
|
|
}
|
|
|
|
if (fra->ipfr_prev)
|
|
|
|
fra->ipfr_prev->ipfr_next = fra->ipfr_next;
|
|
|
|
if (fra->ipfr_next)
|
|
|
|
fra->ipfr_next->ipfr_prev = fra->ipfr_prev;
|
|
|
|
KFREE(fra);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-01-05 16:47:59 +03:00
|
|
|
/*
|
|
|
|
* Free memory in use by fragment state info. kept.
|
|
|
|
*/
|
|
|
|
void ipfr_unload()
|
|
|
|
{
|
1999-12-12 14:11:15 +03:00
|
|
|
ipfr_t **fp, *fra;
|
1997-05-25 16:40:11 +04:00
|
|
|
nat_t *nat;
|
1997-01-05 16:47:59 +03:00
|
|
|
int idx;
|
|
|
|
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_frag);
|
1997-01-05 16:47:59 +03:00
|
|
|
for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
|
1999-12-12 14:11:15 +03:00
|
|
|
for (fp = &ipfr_heads[idx]; (fra = *fp); ) {
|
|
|
|
*fp = fra->ipfr_next;
|
|
|
|
ipfr_delete(fra);
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_frag);
|
1997-05-25 16:40:11 +04:00
|
|
|
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_nat);
|
|
|
|
WRITE_ENTER(&ipf_natfrag);
|
1997-05-25 16:40:11 +04:00
|
|
|
for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
|
1999-12-12 14:11:15 +03:00
|
|
|
for (fp = &ipfr_nattab[idx]; (fra = *fp); ) {
|
|
|
|
*fp = fra->ipfr_next;
|
|
|
|
nat = fra->ipfr_data;
|
1998-11-22 18:17:18 +03:00
|
|
|
if (nat != NULL) {
|
1999-12-12 14:11:15 +03:00
|
|
|
if (nat->nat_data == fra)
|
1997-10-30 19:08:54 +03:00
|
|
|
nat->nat_data = NULL;
|
1997-05-25 16:40:11 +04:00
|
|
|
}
|
1999-12-12 14:11:15 +03:00
|
|
|
ipfr_delete(fra);
|
1997-05-25 16:40:11 +04:00
|
|
|
}
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_natfrag);
|
|
|
|
RWLOCK_EXIT(&ipf_nat);
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _KERNEL
|
2000-05-03 15:12:03 +04:00
|
|
|
void ipfr_fragexpire()
|
1997-01-05 16:47:59 +03:00
|
|
|
{
|
1999-12-12 14:11:15 +03:00
|
|
|
ipfr_t **fp, *fra;
|
1997-05-25 16:40:11 +04:00
|
|
|
nat_t *nat;
|
1999-12-12 14:11:15 +03:00
|
|
|
int idx;
|
|
|
|
#if defined(_KERNEL)
|
|
|
|
# if !SOLARIS
|
|
|
|
int s;
|
|
|
|
# endif
|
|
|
|
#endif
|
1997-01-05 16:47:59 +03:00
|
|
|
|
2000-05-03 15:12:03 +04:00
|
|
|
if (fr_frag_lock)
|
|
|
|
return;
|
1997-10-30 19:08:54 +03:00
|
|
|
|
|
|
|
SPL_NET(s);
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_frag);
|
1997-01-05 16:47:59 +03:00
|
|
|
|
1997-05-25 16:40:11 +04:00
|
|
|
/*
|
|
|
|
* Go through the entire table, looking for entries to expire,
|
|
|
|
* decreasing the ttl by one for each entry. If it reaches 0,
|
|
|
|
* remove it from the chain and free it.
|
|
|
|
*/
|
1997-01-05 16:47:59 +03:00
|
|
|
for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
|
1999-12-12 14:11:15 +03:00
|
|
|
for (fp = &ipfr_heads[idx]; (fra = *fp); ) {
|
|
|
|
--fra->ipfr_ttl;
|
|
|
|
if (fra->ipfr_ttl == 0) {
|
|
|
|
*fp = fra->ipfr_next;
|
|
|
|
ipfr_delete(fra);
|
2000-05-03 15:12:03 +04:00
|
|
|
ATOMIC_INCL(ipfr_stats.ifs_expire);
|
|
|
|
ATOMIC_DEC32(ipfr_inuse);
|
1997-01-05 16:47:59 +03:00
|
|
|
} else
|
1999-12-12 14:11:15 +03:00
|
|
|
fp = &fra->ipfr_next;
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_frag);
|
1997-05-25 16:40:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Same again for the NAT table, except that if the structure also
|
|
|
|
* still points to a NAT structure, and the NAT structure points back
|
|
|
|
* at the one to be free'd, NULL the reference from the NAT struct.
|
|
|
|
* NOTE: We need to grab both mutex's early, and in this order so as
|
|
|
|
* to prevent a deadlock if both try to expire at the same time.
|
|
|
|
*/
|
1998-07-12 19:23:59 +04:00
|
|
|
WRITE_ENTER(&ipf_nat);
|
|
|
|
WRITE_ENTER(&ipf_natfrag);
|
1997-05-25 16:40:11 +04:00
|
|
|
for (idx = IPFT_SIZE - 1; idx >= 0; idx--)
|
1999-12-12 14:11:15 +03:00
|
|
|
for (fp = &ipfr_nattab[idx]; (fra = *fp); ) {
|
|
|
|
--fra->ipfr_ttl;
|
|
|
|
if (fra->ipfr_ttl == 0) {
|
2000-05-03 15:12:03 +04:00
|
|
|
ATOMIC_INCL(ipfr_stats.ifs_expire);
|
|
|
|
ATOMIC_DEC32(ipfr_inuse);
|
1999-12-12 14:11:15 +03:00
|
|
|
nat = fra->ipfr_data;
|
1998-11-22 18:17:18 +03:00
|
|
|
if (nat != NULL) {
|
1999-12-12 14:11:15 +03:00
|
|
|
if (nat->nat_data == fra)
|
1997-10-30 19:08:54 +03:00
|
|
|
nat->nat_data = NULL;
|
1997-05-25 16:40:11 +04:00
|
|
|
}
|
1999-12-12 14:11:15 +03:00
|
|
|
*fp = fra->ipfr_next;
|
|
|
|
ipfr_delete(fra);
|
1997-05-25 16:40:11 +04:00
|
|
|
} else
|
1999-12-12 14:11:15 +03:00
|
|
|
fp = &fra->ipfr_next;
|
1997-05-25 16:40:11 +04:00
|
|
|
}
|
1998-07-12 19:23:59 +04:00
|
|
|
RWLOCK_EXIT(&ipf_natfrag);
|
|
|
|
RWLOCK_EXIT(&ipf_nat);
|
1997-10-30 19:08:54 +03:00
|
|
|
SPL_X(s);
|
2000-05-03 15:12:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Slowly expire held state for fragments. Timeouts are set * in expectation
|
|
|
|
* of this being called twice per second.
|
|
|
|
*/
|
|
|
|
# if (BSD >= 199306) || SOLARIS || defined(__sgi)
|
|
|
|
# if defined(SOLARIS2) && (SOLARIS2 < 7)
|
|
|
|
void ipfr_slowtimer()
|
|
|
|
# else
|
|
|
|
void ipfr_slowtimer __P((void *ptr))
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
int ipfr_slowtimer()
|
|
|
|
# endif
|
|
|
|
{
|
|
|
|
#if defined(_KERNEL) && SOLARIS
|
|
|
|
extern int fr_running;
|
|
|
|
|
|
|
|
if (fr_running <= 0)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
READ_ENTER(&ipf_solaris);
|
|
|
|
#ifdef __sgi
|
|
|
|
ipfilter_sgi_intfsync();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ipfr_fragexpire();
|
1997-01-05 16:47:59 +03:00
|
|
|
fr_timeoutstate();
|
|
|
|
ip_natexpire();
|
1997-07-05 09:38:14 +04:00
|
|
|
fr_authexpire();
|
|
|
|
# if SOLARIS
|
1997-05-25 16:40:11 +04:00
|
|
|
ipfr_timer_id = timeout(ipfr_slowtimer, NULL, drv_usectohz(500000));
|
1997-01-05 16:47:59 +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)
|
|
|
|
ipfr_slowtimer_ch = timeout(ipfr_slowtimer, NULL, hz/2);
|
|
|
|
# else
|
2000-03-23 10:01:25 +03:00
|
|
|
# if defined(__NetBSD__)
|
|
|
|
callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
|
|
|
|
# else
|
1999-12-12 14:11:15 +03:00
|
|
|
timeout(ipfr_slowtimer, NULL, hz/2);
|
2000-03-23 10:01:25 +03:00
|
|
|
# endif
|
1999-12-12 14:11:15 +03:00
|
|
|
# endif
|
1997-11-14 15:40:06 +03:00
|
|
|
# endif
|
1997-10-30 19:08:54 +03:00
|
|
|
# if (BSD < 199306) && !defined(__sgi)
|
1997-01-05 16:47:59 +03:00
|
|
|
return 0;
|
|
|
|
# endif
|
|
|
|
# endif
|
1999-12-12 14:11:15 +03:00
|
|
|
RWLOCK_EXIT(&ipf_solaris);
|
1997-01-05 16:47:59 +03:00
|
|
|
}
|
|
|
|
#endif /* defined(_KERNEL) */
|