2001-06-12 19:17:10 +04:00
|
|
|
|
/* $NetBSD: ip_flow.c,v 1.19 2001/06/12 15:17:28 wiz Exp $ */
|
1998-04-30 01:37:52 +04:00
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
|
* by the 3am Software Foundry ("3am"). It was developed by Matt Thomas.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
#include <sys/systm.h>
|
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
|
#include <sys/domain.h>
|
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
|
#include <sys/errno.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
|
#include <sys/proc.h>
|
1998-10-08 05:41:45 +04:00
|
|
|
|
#include <sys/pool.h>
|
1998-04-30 01:37:52 +04:00
|
|
|
|
|
2000-06-28 07:01:16 +04:00
|
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
|
1998-04-30 01:37:52 +04:00
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
|
#include <net/route.h>
|
|
|
|
|
#include <net/pfil.h>
|
|
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
|
|
1998-10-08 05:41:45 +04:00
|
|
|
|
struct pool ipflow_pool;
|
|
|
|
|
|
1998-06-02 19:48:03 +04:00
|
|
|
|
LIST_HEAD(ipflowhead, ipflow);
|
|
|
|
|
|
1998-04-30 01:37:52 +04:00
|
|
|
|
#define IPFLOW_TIMER (5 * PR_SLOWHZ)
|
|
|
|
|
#define IPFLOW_HASHSIZE (1 << IPFLOW_HASHBITS)
|
1998-06-02 19:48:03 +04:00
|
|
|
|
|
|
|
|
|
static struct ipflowhead ipflowtable[IPFLOW_HASHSIZE];
|
|
|
|
|
static struct ipflowhead ipflowlist;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
static int ipflow_inuse;
|
1998-06-02 19:48:03 +04:00
|
|
|
|
|
|
|
|
|
#define IPFLOW_INSERT(bucket, ipf) \
|
|
|
|
|
do { \
|
|
|
|
|
LIST_INSERT_HEAD((bucket), (ipf), ipf_hash); \
|
|
|
|
|
LIST_INSERT_HEAD(&ipflowlist, (ipf), ipf_list); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
#define IPFLOW_REMOVE(ipf) \
|
|
|
|
|
do { \
|
|
|
|
|
LIST_REMOVE((ipf), ipf_hash); \
|
|
|
|
|
LIST_REMOVE((ipf), ipf_list); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
1998-05-04 23:24:53 +04:00
|
|
|
|
#ifndef IPFLOW_MAX
|
1998-04-30 01:37:52 +04:00
|
|
|
|
#define IPFLOW_MAX 256
|
1998-05-04 23:24:53 +04:00
|
|
|
|
#endif
|
|
|
|
|
int ip_maxflows = IPFLOW_MAX;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
|
ipflow_hash(
|
|
|
|
|
struct in_addr dst,
|
|
|
|
|
struct in_addr src,
|
|
|
|
|
unsigned tos)
|
|
|
|
|
{
|
|
|
|
|
unsigned hash = tos;
|
|
|
|
|
int idx;
|
|
|
|
|
for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
|
|
|
|
|
hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
|
|
|
|
|
return hash & (IPFLOW_HASHSIZE-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct ipflow *
|
|
|
|
|
ipflow_lookup(
|
|
|
|
|
const struct ip *ip)
|
|
|
|
|
{
|
|
|
|
|
unsigned hash;
|
|
|
|
|
struct ipflow *ipf;
|
|
|
|
|
|
|
|
|
|
hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
|
|
|
|
|
|
1998-06-02 19:48:03 +04:00
|
|
|
|
ipf = LIST_FIRST(&ipflowtable[hash]);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
while (ipf != NULL) {
|
|
|
|
|
if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
|
|
|
|
|
&& ip->ip_src.s_addr == ipf->ipf_src.s_addr
|
|
|
|
|
&& ip->ip_tos == ipf->ipf_tos)
|
|
|
|
|
break;
|
1998-06-02 19:48:03 +04:00
|
|
|
|
ipf = LIST_NEXT(ipf, ipf_hash);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
}
|
|
|
|
|
return ipf;
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-08 05:41:45 +04:00
|
|
|
|
void
|
|
|
|
|
ipflow_init()
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
pool_init(&ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl",
|
|
|
|
|
0, NULL, NULL, M_IPFLOW);
|
|
|
|
|
|
|
|
|
|
LIST_INIT(&ipflowlist);
|
|
|
|
|
for (i = 0; i < IPFLOW_HASHSIZE; i++)
|
|
|
|
|
LIST_INIT(&ipflowtable[i]);
|
|
|
|
|
}
|
|
|
|
|
|
1998-04-30 01:37:52 +04:00
|
|
|
|
int
|
|
|
|
|
ipflow_fastforward(
|
|
|
|
|
struct mbuf *m)
|
|
|
|
|
{
|
|
|
|
|
struct ip *ip;
|
|
|
|
|
struct ipflow *ipf;
|
|
|
|
|
struct rtentry *rt;
|
2000-06-30 23:43:53 +04:00
|
|
|
|
struct sockaddr *dst;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
int error;
|
1998-06-10 04:47:57 +04:00
|
|
|
|
int iplen;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Are we forwarding packets? Big enough for an IP packet?
|
|
|
|
|
*/
|
1998-05-04 23:24:53 +04:00
|
|
|
|
if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
|
1998-04-30 01:37:52 +04:00
|
|
|
|
return 0;
|
1999-10-18 03:38:45 +04:00
|
|
|
|
|
|
|
|
|
/*
|
2001-06-12 19:17:10 +04:00
|
|
|
|
* Was packet received as a link-level multicast or broadcast?
|
1999-10-18 03:38:45 +04:00
|
|
|
|
* If so, don't try to fast forward..
|
|
|
|
|
*/
|
|
|
|
|
if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
1998-04-30 01:37:52 +04:00
|
|
|
|
/*
|
|
|
|
|
* IP header with no option and valid version and length
|
|
|
|
|
*/
|
|
|
|
|
ip = mtod(m, struct ip *);
|
1998-06-10 04:47:57 +04:00
|
|
|
|
iplen = ntohs(ip->ip_len);
|
1998-06-02 19:48:03 +04:00
|
|
|
|
if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
|
1999-03-26 11:51:35 +03:00
|
|
|
|
iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
return 0;
|
|
|
|
|
/*
|
|
|
|
|
* Find a flow.
|
|
|
|
|
*/
|
|
|
|
|
if ((ipf = ipflow_lookup(ip)) == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
1998-05-04 09:46:04 +04:00
|
|
|
|
/*
|
2001-06-02 20:17:09 +04:00
|
|
|
|
* Verify the IP header checksum.
|
1998-05-04 09:46:04 +04:00
|
|
|
|
*/
|
2001-06-02 20:17:09 +04:00
|
|
|
|
switch (m->m_pkthdr.csum_flags &
|
|
|
|
|
((m->m_pkthdr.rcvif->if_csum_flags & M_CSUM_IPv4) |
|
|
|
|
|
M_CSUM_IPv4_BAD)) {
|
|
|
|
|
case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
|
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
|
|
case M_CSUM_IPv4:
|
|
|
|
|
/* Checksum was okay. */
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
/* Must compute it ourselves. */
|
|
|
|
|
if (in_cksum(m, sizeof(struct ip)) != 0)
|
|
|
|
|
return (0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
1998-05-04 09:46:04 +04:00
|
|
|
|
|
1998-04-30 01:37:52 +04:00
|
|
|
|
/*
|
|
|
|
|
* Route and interface still up?
|
|
|
|
|
*/
|
|
|
|
|
rt = ipf->ipf_ro.ro_rt;
|
1998-06-02 19:48:03 +04:00
|
|
|
|
if ((rt->rt_flags & RTF_UP) == 0 ||
|
|
|
|
|
(rt->rt_ifp->if_flags & IFF_UP) == 0)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Packet size OK? TTL?
|
|
|
|
|
*/
|
|
|
|
|
if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2001-06-02 20:17:09 +04:00
|
|
|
|
/*
|
|
|
|
|
* Clear any in-bound checksum flags for this packet.
|
|
|
|
|
*/
|
|
|
|
|
m->m_pkthdr.csum_flags = 0;
|
|
|
|
|
|
1998-04-30 01:37:52 +04:00
|
|
|
|
/*
|
|
|
|
|
* Everything checks out and so we can forward this packet.
|
|
|
|
|
* Modify the TTL and incrementally change the checksum.
|
1999-01-24 16:34:35 +03:00
|
|
|
|
*
|
|
|
|
|
* This method of adding the checksum works on either endian CPU.
|
|
|
|
|
* If htons() is inlined, all the arithmetic is folded; otherwise
|
|
|
|
|
* the htons()s are combined by CSE due to the __const__ attribute.
|
2001-06-02 20:17:09 +04:00
|
|
|
|
*
|
|
|
|
|
* Don't bother using HW checksumming here -- the incremental
|
|
|
|
|
* update is pretty fast.
|
1998-04-30 01:37:52 +04:00
|
|
|
|
*/
|
|
|
|
|
ip->ip_ttl -= IPTTLDEC;
|
1999-01-29 00:29:27 +03:00
|
|
|
|
if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
|
1999-01-25 18:53:29 +03:00
|
|
|
|
ip->ip_sum -= ~htons(IPTTLDEC << 8);
|
1999-01-24 15:57:38 +03:00
|
|
|
|
else
|
1998-05-04 09:46:04 +04:00
|
|
|
|
ip->ip_sum += htons(IPTTLDEC << 8);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
|
1998-06-10 04:47:57 +04:00
|
|
|
|
/*
|
|
|
|
|
* Trim the packet in case it's too long..
|
|
|
|
|
*/
|
|
|
|
|
if (m->m_pkthdr.len > iplen) {
|
|
|
|
|
if (m->m_len == m->m_pkthdr.len) {
|
|
|
|
|
m->m_len = iplen;
|
|
|
|
|
m->m_pkthdr.len = iplen;
|
|
|
|
|
} else
|
|
|
|
|
m_adj(m, iplen - m->m_pkthdr.len);
|
|
|
|
|
}
|
|
|
|
|
|
1998-04-30 01:37:52 +04:00
|
|
|
|
/*
|
|
|
|
|
* Send the packet on it's way. All we can get back is ENOBUFS
|
|
|
|
|
*/
|
|
|
|
|
ipf->ipf_uses++;
|
1998-06-02 19:48:03 +04:00
|
|
|
|
PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
|
2000-06-30 23:43:53 +04:00
|
|
|
|
|
|
|
|
|
if (rt->rt_flags & RTF_GATEWAY)
|
|
|
|
|
dst = rt->rt_gateway;
|
|
|
|
|
else
|
|
|
|
|
dst = &ipf->ipf_ro.ro_dst;
|
|
|
|
|
|
|
|
|
|
if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, dst, rt)) != 0) {
|
1998-04-30 01:37:52 +04:00
|
|
|
|
if (error == ENOBUFS)
|
|
|
|
|
ipf->ipf_dropped++;
|
|
|
|
|
else
|
|
|
|
|
ipf->ipf_errors++;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ipflow_addstats(
|
|
|
|
|
struct ipflow *ipf)
|
|
|
|
|
{
|
|
|
|
|
ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
|
|
|
|
|
ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
|
|
|
|
|
ipstat.ips_forward += ipf->ipf_uses;
|
|
|
|
|
ipstat.ips_fastforward += ipf->ipf_uses;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ipflow_free(
|
|
|
|
|
struct ipflow *ipf)
|
|
|
|
|
{
|
|
|
|
|
int s;
|
|
|
|
|
/*
|
|
|
|
|
* Remove the flow from the hash table (at elevated IPL).
|
|
|
|
|
* Once it's off the list, we can deal with it at normal
|
|
|
|
|
* network IPL.
|
|
|
|
|
*/
|
2001-04-14 03:29:55 +04:00
|
|
|
|
s = splnet();
|
1998-06-02 19:48:03 +04:00
|
|
|
|
IPFLOW_REMOVE(ipf);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
splx(s);
|
|
|
|
|
ipflow_addstats(ipf);
|
|
|
|
|
RTFREE(ipf->ipf_ro.ro_rt);
|
|
|
|
|
ipflow_inuse--;
|
1998-10-08 05:41:45 +04:00
|
|
|
|
pool_put(&ipflow_pool, ipf);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
1998-05-04 23:24:53 +04:00
|
|
|
|
struct ipflow *
|
1998-04-30 01:37:52 +04:00
|
|
|
|
ipflow_reap(
|
1998-05-04 23:24:53 +04:00
|
|
|
|
int just_one)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
{
|
1998-05-04 23:24:53 +04:00
|
|
|
|
while (just_one || ipflow_inuse > ip_maxflows) {
|
|
|
|
|
struct ipflow *ipf, *maybe_ipf = NULL;
|
|
|
|
|
int s;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
|
1998-06-02 19:48:03 +04:00
|
|
|
|
ipf = LIST_FIRST(&ipflowlist);
|
|
|
|
|
while (ipf != NULL) {
|
|
|
|
|
/*
|
|
|
|
|
* If this no longer points to a valid route
|
|
|
|
|
* reclaim it.
|
|
|
|
|
*/
|
|
|
|
|
if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
|
|
|
|
|
goto done;
|
|
|
|
|
/*
|
|
|
|
|
* choose the one that's been least recently
|
|
|
|
|
* used or has had the least uses in the
|
|
|
|
|
* last 1.5 intervals.
|
|
|
|
|
*/
|
|
|
|
|
if (maybe_ipf == NULL ||
|
|
|
|
|
ipf->ipf_timer < maybe_ipf->ipf_timer ||
|
|
|
|
|
(ipf->ipf_timer == maybe_ipf->ipf_timer &&
|
|
|
|
|
ipf->ipf_last_uses + ipf->ipf_uses <
|
|
|
|
|
maybe_ipf->ipf_last_uses +
|
|
|
|
|
maybe_ipf->ipf_uses))
|
|
|
|
|
maybe_ipf = ipf;
|
|
|
|
|
ipf = LIST_NEXT(ipf, ipf_list);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
}
|
1998-05-04 23:24:53 +04:00
|
|
|
|
ipf = maybe_ipf;
|
|
|
|
|
done:
|
|
|
|
|
/*
|
|
|
|
|
* Remove the entry from the flow table.
|
|
|
|
|
*/
|
2001-04-14 03:29:55 +04:00
|
|
|
|
s = splnet();
|
1998-06-02 19:48:03 +04:00
|
|
|
|
IPFLOW_REMOVE(ipf);
|
1998-05-04 23:24:53 +04:00
|
|
|
|
splx(s);
|
|
|
|
|
ipflow_addstats(ipf);
|
|
|
|
|
RTFREE(ipf->ipf_ro.ro_rt);
|
|
|
|
|
if (just_one)
|
|
|
|
|
return ipf;
|
1998-10-08 05:41:45 +04:00
|
|
|
|
pool_put(&ipflow_pool, ipf);
|
1998-05-04 23:24:53 +04:00
|
|
|
|
ipflow_inuse--;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
}
|
1998-05-04 23:24:53 +04:00
|
|
|
|
return NULL;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ipflow_slowtimo(
|
|
|
|
|
void)
|
|
|
|
|
{
|
1998-06-02 19:48:03 +04:00
|
|
|
|
struct ipflow *ipf, *next_ipf;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
|
1998-06-02 19:48:03 +04:00
|
|
|
|
ipf = LIST_FIRST(&ipflowlist);
|
|
|
|
|
while (ipf != NULL) {
|
|
|
|
|
next_ipf = LIST_NEXT(ipf, ipf_list);
|
|
|
|
|
if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer)) {
|
|
|
|
|
ipflow_free(ipf);
|
|
|
|
|
} else {
|
|
|
|
|
ipf->ipf_last_uses = ipf->ipf_uses;
|
|
|
|
|
ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
|
|
|
|
|
ipstat.ips_forward += ipf->ipf_uses;
|
|
|
|
|
ipstat.ips_fastforward += ipf->ipf_uses;
|
|
|
|
|
ipf->ipf_uses = 0;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
}
|
1998-06-02 19:48:03 +04:00
|
|
|
|
ipf = next_ipf;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ipflow_create(
|
|
|
|
|
const struct route *ro,
|
|
|
|
|
struct mbuf *m)
|
|
|
|
|
{
|
|
|
|
|
const struct ip *const ip = mtod(m, struct ip *);
|
|
|
|
|
struct ipflow *ipf;
|
|
|
|
|
unsigned hash;
|
|
|
|
|
int s;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Don't create cache entries for ICMP messages.
|
|
|
|
|
*/
|
1998-05-04 23:24:53 +04:00
|
|
|
|
if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
return;
|
|
|
|
|
/*
|
|
|
|
|
* See if an existing flow struct exists. If so remove it from it's
|
|
|
|
|
* list and free the old route. If not, try to malloc a new one
|
|
|
|
|
* (if we aren't at our limit).
|
|
|
|
|
*/
|
|
|
|
|
ipf = ipflow_lookup(ip);
|
|
|
|
|
if (ipf == NULL) {
|
1998-05-04 23:24:53 +04:00
|
|
|
|
if (ipflow_inuse >= ip_maxflows) {
|
|
|
|
|
ipf = ipflow_reap(1);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
} else {
|
1998-10-08 05:41:45 +04:00
|
|
|
|
ipf = pool_get(&ipflow_pool, PR_NOWAIT);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
if (ipf == NULL)
|
|
|
|
|
return;
|
|
|
|
|
ipflow_inuse++;
|
|
|
|
|
}
|
|
|
|
|
bzero((caddr_t) ipf, sizeof(*ipf));
|
|
|
|
|
} else {
|
2001-04-14 03:29:55 +04:00
|
|
|
|
s = splnet();
|
1998-06-02 19:48:03 +04:00
|
|
|
|
IPFLOW_REMOVE(ipf);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
splx(s);
|
|
|
|
|
ipflow_addstats(ipf);
|
|
|
|
|
RTFREE(ipf->ipf_ro.ro_rt);
|
|
|
|
|
ipf->ipf_uses = ipf->ipf_last_uses = 0;
|
|
|
|
|
ipf->ipf_errors = ipf->ipf_dropped = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Fill in the updated information.
|
|
|
|
|
*/
|
|
|
|
|
ipf->ipf_ro = *ro;
|
|
|
|
|
ro->ro_rt->rt_refcnt++;
|
|
|
|
|
ipf->ipf_dst = ip->ip_dst;
|
|
|
|
|
ipf->ipf_src = ip->ip_src;
|
|
|
|
|
ipf->ipf_tos = ip->ip_tos;
|
1998-06-02 19:48:03 +04:00
|
|
|
|
PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
ipf->ipf_start = time.tv_sec;
|
|
|
|
|
/*
|
|
|
|
|
* Insert into the approriate bucket of the flow table.
|
|
|
|
|
*/
|
|
|
|
|
hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
|
2001-04-14 03:29:55 +04:00
|
|
|
|
s = splnet();
|
1998-06-02 19:48:03 +04:00
|
|
|
|
IPFLOW_INSERT(&ipflowtable[hash], ipf);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
splx(s);
|
|
|
|
|
}
|