2008-01-05 02:28:07 +03:00
|
|
|
|
/* $NetBSD: ip_flow.c,v 1.51 2008/01/04 23:28:07 dyoung 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2001-11-13 03:32:34 +03:00
|
|
|
|
#include <sys/cdefs.h>
|
2008-01-05 02:28:07 +03:00
|
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.51 2008/01/04 23:28:07 dyoung Exp $");
|
2001-11-13 03:32:34 +03:00
|
|
|
|
|
1998-04-30 01:37:52 +04:00
|
|
|
|
#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>
|
1998-10-08 05:41:45 +04:00
|
|
|
|
#include <sys/pool.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>
|
|
|
|
|
|
2007-03-26 04:29:15 +04:00
|
|
|
|
/*
|
|
|
|
|
* Similar code is very well commented in netinet6/ip6_flow.c
|
|
|
|
|
*/
|
|
|
|
|
|
2007-03-12 21:18:22 +03:00
|
|
|
|
POOL_INIT(ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl", NULL,
|
|
|
|
|
IPL_NET);
|
1998-10-08 05:41:45 +04:00
|
|
|
|
|
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)
|
2007-03-26 00:12:20 +04:00
|
|
|
|
#define IPFLOW_DEFAULT_HASHSIZE (1 << IPFLOW_HASHBITS)
|
1998-06-02 19:48:03 +04:00
|
|
|
|
|
2007-03-26 00:12:20 +04:00
|
|
|
|
static struct ipflowhead *ipflowtable = NULL;
|
1998-06-02 19:48:03 +04:00
|
|
|
|
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); \
|
2002-11-02 10:20:42 +03:00
|
|
|
|
} while (/*CONSTCOND*/ 0)
|
1998-06-02 19:48:03 +04:00
|
|
|
|
|
|
|
|
|
#define IPFLOW_REMOVE(ipf) \
|
|
|
|
|
do { \
|
|
|
|
|
LIST_REMOVE((ipf), ipf_hash); \
|
|
|
|
|
LIST_REMOVE((ipf), ipf_list); \
|
2002-11-02 10:20:42 +03:00
|
|
|
|
} while (/*CONSTCOND*/ 0)
|
1998-06-02 19:48:03 +04:00
|
|
|
|
|
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;
|
2007-03-26 00:12:20 +04:00
|
|
|
|
int ip_hashsize = IPFLOW_DEFAULT_HASHSIZE;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
|
2007-04-05 22:11:47 +04:00
|
|
|
|
static size_t
|
2008-01-05 02:28:07 +03:00
|
|
|
|
ipflow_hash(const struct ip *ip)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
{
|
2007-04-05 22:11:47 +04:00
|
|
|
|
size_t hash = ip->ip_tos;
|
|
|
|
|
size_t idx;
|
|
|
|
|
|
|
|
|
|
for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS) {
|
|
|
|
|
hash += (ip->ip_dst.s_addr >> (32 - idx)) +
|
|
|
|
|
(ip->ip_src.s_addr >> idx);
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-26 00:12:20 +04:00
|
|
|
|
return hash & (ip_hashsize-1);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct ipflow *
|
2008-01-05 02:28:07 +03:00
|
|
|
|
ipflow_lookup(const struct ip *ip)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
{
|
2007-04-05 22:11:47 +04:00
|
|
|
|
size_t hash;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
struct ipflow *ipf;
|
|
|
|
|
|
2007-04-05 22:11:47 +04:00
|
|
|
|
hash = ipflow_hash(ip);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
|
2005-10-17 23:51:24 +04:00
|
|
|
|
LIST_FOREACH(ipf, &ipflowtable[hash], ipf_hash) {
|
1998-04-30 01:37:52 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
return ipf;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-26 00:12:20 +04:00
|
|
|
|
int
|
|
|
|
|
ipflow_init(int table_size)
|
1998-10-08 05:41:45 +04:00
|
|
|
|
{
|
2007-03-26 00:12:20 +04:00
|
|
|
|
struct ipflowhead *new_table;
|
2007-04-05 22:11:47 +04:00
|
|
|
|
size_t i;
|
1998-10-08 05:41:45 +04:00
|
|
|
|
|
2007-03-26 00:12:20 +04:00
|
|
|
|
new_table = (struct ipflowhead *)malloc(sizeof(struct ipflowhead) *
|
|
|
|
|
table_size, M_RTABLE, M_NOWAIT);
|
|
|
|
|
|
|
|
|
|
if (new_table == NULL)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
if (ipflowtable != NULL)
|
|
|
|
|
free(ipflowtable, M_RTABLE);
|
|
|
|
|
|
|
|
|
|
ipflowtable = new_table;
|
|
|
|
|
ip_hashsize = table_size;
|
|
|
|
|
|
1998-10-08 05:41:45 +04:00
|
|
|
|
LIST_INIT(&ipflowlist);
|
2007-03-26 00:12:20 +04:00
|
|
|
|
for (i = 0; i < ip_hashsize; i++)
|
1998-10-08 05:41:45 +04:00
|
|
|
|
LIST_INIT(&ipflowtable[i]);
|
2007-03-26 00:12:20 +04:00
|
|
|
|
|
|
|
|
|
return 0;
|
1998-10-08 05:41:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
1998-04-30 01:37:52 +04:00
|
|
|
|
int
|
2005-02-04 01:43:34 +03:00
|
|
|
|
ipflow_fastforward(struct mbuf *m)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
{
|
2008-01-05 02:28:07 +03:00
|
|
|
|
struct ip *ip;
|
|
|
|
|
struct ip ip_store;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
struct ipflow *ipf;
|
|
|
|
|
struct rtentry *rt;
|
KNF: de-__P, bzero -> memset, bcmp -> memcmp. Remove extraneous
parentheses in return statements.
Cosmetic: don't open-code TAILQ_FOREACH().
Cosmetic: change types of variables to avoid oodles of casts: in
in6_src.c, avoid casts by changing several route_in6 pointers
to struct route pointers. Remove unnecessary casts to caddr_t
elsewhere.
Pave the way for eliminating address family-specific route caches:
soon, struct route will not embed a sockaddr, but it will hold
a reference to an external sockaddr, instead. We will set the
destination sockaddr using rtcache_setdst(). (I created a stub
for it, but it isn't used anywhere, yet.) rtcache_free() will
free the sockaddr. I have extracted from rtcache_free() a helper
subroutine, rtcache_clear(). rtcache_clear() will "forget" a
cached route, but it will not forget the destination by releasing
the sockaddr. I use rtcache_clear() instead of rtcache_free()
in rtcache_update(), because rtcache_update() is not supposed
to forget the destination.
Constify:
1 Introduce const accessor for route->ro_dst, rtcache_getdst().
2 Constify the 'dst' argument to ifnet->if_output(). This
led me to constify a lot of code called by output routines.
3 Constify the sockaddr argument to protosw->pr_ctlinput. This
led me to constify a lot of code called by ctlinput routines.
4 Introduce const macros for converting from a generic sockaddr
to family-specific sockaddrs, e.g., sockaddr_in: satocsin6,
satocsin, et cetera.
2007-02-18 01:34:07 +03:00
|
|
|
|
const 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;
|
2002-06-09 20:33:36 +04:00
|
|
|
|
|
1998-04-30 01:37:52 +04:00
|
|
|
|
/*
|
|
|
|
|
* IP header with no option and valid version and length
|
|
|
|
|
*/
|
2008-01-05 02:28:07 +03:00
|
|
|
|
if (IP_HDR_ALIGNED_P(mtod(m, const void *)))
|
Changes to allow the IPv4 and IPv6 layers to align headers themseves,
as necessary:
* Implement a new mbuf utility routine, m_copyup(), is is like
m_pullup(), except that it always prepends and copies, rather
than only doing so if the desired length is larger than m->m_len.
m_copyup() also allows an offset into the destination mbuf, which
allows space for packet headers, in the forwarding case.
* Add *_HDR_ALIGNED_P() macros for IP, IPv6, ICMP, and IGMP. These
macros expand to 1 if __NO_STRICT_ALIGNMENT is defined, so that
architectures which do not have strict alignment constraints don't
pay for the test or visit the new align-if-needed path.
* Use the new macros to check if a header needs to be aligned, or to
assert that it already is, as appropriate.
Note: This code is still somewhat experimental. However, the new
code path won't be visited if individual device drivers continue
to guarantee that packets are delivered to layer 3 already properly
aligned (which are rules that are already in use).
2002-07-01 02:40:32 +04:00
|
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
|
else {
|
2008-01-05 02:28:07 +03:00
|
|
|
|
memcpy(&ip_store, mtod(m, const void *), sizeof(ip_store));
|
Changes to allow the IPv4 and IPv6 layers to align headers themseves,
as necessary:
* Implement a new mbuf utility routine, m_copyup(), is is like
m_pullup(), except that it always prepends and copies, rather
than only doing so if the desired length is larger than m->m_len.
m_copyup() also allows an offset into the destination mbuf, which
allows space for packet headers, in the forwarding case.
* Add *_HDR_ALIGNED_P() macros for IP, IPv6, ICMP, and IGMP. These
macros expand to 1 if __NO_STRICT_ALIGNMENT is defined, so that
architectures which do not have strict alignment constraints don't
pay for the test or visit the new align-if-needed path.
* Use the new macros to check if a header needs to be aligned, or to
assert that it already is, as appropriate.
Note: This code is still somewhat experimental. However, the new
code path won't be visited if individual device drivers continue
to guarantee that packets are delivered to layer 3 already properly
aligned (which are rules that are already in use).
2002-07-01 02:40:32 +04:00
|
|
|
|
ip = &ip_store;
|
|
|
|
|
}
|
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 &
|
2001-09-17 21:26:59 +04:00
|
|
|
|
((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
|
2001-06-02 20:17:09 +04:00
|
|
|
|
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?
|
|
|
|
|
*/
|
2008-01-05 02:26:44 +03:00
|
|
|
|
if ((rt = rtcache_validate(&ipf->ipf_ro)) == NULL ||
|
2007-08-20 23:42:34 +04:00
|
|
|
|
(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.
|
2002-06-09 20:33:36 +04:00
|
|
|
|
*
|
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
|
2005-12-25 02:43:17 +03:00
|
|
|
|
* 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
|
|
|
|
|
Changes to allow the IPv4 and IPv6 layers to align headers themseves,
as necessary:
* Implement a new mbuf utility routine, m_copyup(), is is like
m_pullup(), except that it always prepends and copies, rather
than only doing so if the desired length is larger than m->m_len.
m_copyup() also allows an offset into the destination mbuf, which
allows space for packet headers, in the forwarding case.
* Add *_HDR_ALIGNED_P() macros for IP, IPv6, ICMP, and IGMP. These
macros expand to 1 if __NO_STRICT_ALIGNMENT is defined, so that
architectures which do not have strict alignment constraints don't
pay for the test or visit the new align-if-needed path.
* Use the new macros to check if a header needs to be aligned, or to
assert that it already is, as appropriate.
Note: This code is still somewhat experimental. However, the new
code path won't be visited if individual device drivers continue
to guarantee that packets are delivered to layer 3 already properly
aligned (which are rules that are already in use).
2002-07-01 02:40:32 +04:00
|
|
|
|
/*
|
|
|
|
|
* Done modifying the header; copy it back, if necessary.
|
2008-01-05 02:28:07 +03:00
|
|
|
|
*
|
|
|
|
|
* XXX Use m_copyback_cow(9) here? --dyoung
|
Changes to allow the IPv4 and IPv6 layers to align headers themseves,
as necessary:
* Implement a new mbuf utility routine, m_copyup(), is is like
m_pullup(), except that it always prepends and copies, rather
than only doing so if the desired length is larger than m->m_len.
m_copyup() also allows an offset into the destination mbuf, which
allows space for packet headers, in the forwarding case.
* Add *_HDR_ALIGNED_P() macros for IP, IPv6, ICMP, and IGMP. These
macros expand to 1 if __NO_STRICT_ALIGNMENT is defined, so that
architectures which do not have strict alignment constraints don't
pay for the test or visit the new align-if-needed path.
* Use the new macros to check if a header needs to be aligned, or to
assert that it already is, as appropriate.
Note: This code is still somewhat experimental. However, the new
code path won't be visited if individual device drivers continue
to guarantee that packets are delivered to layer 3 already properly
aligned (which are rules that are already in use).
2002-07-01 02:40:32 +04:00
|
|
|
|
*/
|
2007-03-04 08:59:00 +03:00
|
|
|
|
if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0)
|
|
|
|
|
memcpy(mtod(m, void *), &ip_store, sizeof(ip_store));
|
Changes to allow the IPv4 and IPv6 layers to align headers themseves,
as necessary:
* Implement a new mbuf utility routine, m_copyup(), is is like
m_pullup(), except that it always prepends and copies, rather
than only doing so if the desired length is larger than m->m_len.
m_copyup() also allows an offset into the destination mbuf, which
allows space for packet headers, in the forwarding case.
* Add *_HDR_ALIGNED_P() macros for IP, IPv6, ICMP, and IGMP. These
macros expand to 1 if __NO_STRICT_ALIGNMENT is defined, so that
architectures which do not have strict alignment constraints don't
pay for the test or visit the new align-if-needed path.
* Use the new macros to check if a header needs to be aligned, or to
assert that it already is, as appropriate.
Note: This code is still somewhat experimental. However, the new
code path won't be visited if individual device drivers continue
to guarantee that packets are delivered to layer 3 already properly
aligned (which are rules that are already in use).
2002-07-01 02:40:32 +04:00
|
|
|
|
|
1998-06-10 04:47:57 +04:00
|
|
|
|
/*
|
2002-06-09 20:33:36 +04:00
|
|
|
|
* Trim the packet in case it's too long..
|
1998-06-10 04:47:57 +04:00
|
|
|
|
*/
|
|
|
|
|
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
|
KNF: de-__P, bzero -> memset, bcmp -> memcmp. Remove extraneous
parentheses in return statements.
Cosmetic: don't open-code TAILQ_FOREACH().
Cosmetic: change types of variables to avoid oodles of casts: in
in6_src.c, avoid casts by changing several route_in6 pointers
to struct route pointers. Remove unnecessary casts to caddr_t
elsewhere.
Pave the way for eliminating address family-specific route caches:
soon, struct route will not embed a sockaddr, but it will hold
a reference to an external sockaddr, instead. We will set the
destination sockaddr using rtcache_setdst(). (I created a stub
for it, but it isn't used anywhere, yet.) rtcache_free() will
free the sockaddr. I have extracted from rtcache_free() a helper
subroutine, rtcache_clear(). rtcache_clear() will "forget" a
cached route, but it will not forget the destination by releasing
the sockaddr. I use rtcache_clear() instead of rtcache_free()
in rtcache_update(), because rtcache_update() is not supposed
to forget the destination.
Constify:
1 Introduce const accessor for route->ro_dst, rtcache_getdst().
2 Constify the 'dst' argument to ifnet->if_output(). This
led me to constify a lot of code called by output routines.
3 Constify the sockaddr argument to protosw->pr_ctlinput. This
led me to constify a lot of code called by ctlinput routines.
4 Introduce const macros for converting from a generic sockaddr
to family-specific sockaddrs, e.g., sockaddr_in: satocsin6,
satocsin, et cetera.
2007-02-18 01:34:07 +03:00
|
|
|
|
dst = rtcache_getdst(&ipf->ipf_ro);
|
2000-06-30 23:43:53 +04:00
|
|
|
|
|
|
|
|
|
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
|
2005-02-04 01:43:34 +03:00
|
|
|
|
ipflow_addstats(struct ipflow *ipf)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
{
|
2007-12-20 22:53:29 +03:00
|
|
|
|
struct rtentry *rt;
|
|
|
|
|
|
2008-01-05 02:26:44 +03:00
|
|
|
|
if ((rt = rtcache_validate(&ipf->ipf_ro)) != NULL)
|
2007-12-20 22:53:29 +03:00
|
|
|
|
rt->rt_use += ipf->ipf_uses;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
|
2006-09-02 16:41:01 +04:00
|
|
|
|
ipstat.ips_total += ipf->ipf_uses;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
ipstat.ips_forward += ipf->ipf_uses;
|
|
|
|
|
ipstat.ips_fastforward += ipf->ipf_uses;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2005-02-04 01:43:34 +03:00
|
|
|
|
ipflow_free(struct ipflow *ipf)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
{
|
|
|
|
|
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);
|
2006-12-16 00:18:52 +03:00
|
|
|
|
rtcache_free(&ipf->ipf_ro);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
ipflow_inuse--;
|
2006-10-05 21:35:19 +04:00
|
|
|
|
s = splnet();
|
1998-10-08 05:41:45 +04:00
|
|
|
|
pool_put(&ipflow_pool, ipf);
|
2006-10-05 21:35:19 +04:00
|
|
|
|
splx(s);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
1998-05-04 23:24:53 +04:00
|
|
|
|
struct ipflow *
|
2005-02-04 01:43:34 +03:00
|
|
|
|
ipflow_reap(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.
|
|
|
|
|
*/
|
2008-01-05 02:26:44 +03:00
|
|
|
|
if (rtcache_validate(&ipf->ipf_ro) == NULL)
|
1998-06-02 19:48:03 +04:00
|
|
|
|
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);
|
2006-12-16 00:18:52 +03:00
|
|
|
|
rtcache_free(&ipf->ipf_ro);
|
1998-05-04 23:24:53 +04:00
|
|
|
|
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
|
2005-02-04 01:43:34 +03:00
|
|
|
|
ipflow_slowtimo(void)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
{
|
2007-12-20 22:53:29 +03:00
|
|
|
|
struct rtentry *rt;
|
1998-06-02 19:48:03 +04:00
|
|
|
|
struct ipflow *ipf, *next_ipf;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
|
2005-10-17 23:51:24 +04:00
|
|
|
|
for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
|
1998-06-02 19:48:03 +04:00
|
|
|
|
next_ipf = LIST_NEXT(ipf, ipf_list);
|
Here are various changes designed to protect against bad IPv4
routing caused by stale route caches (struct route). Route caches
are sprinkled throughout PCBs, the IP fast-forwarding table, and
IP tunnel interfaces (gre, gif, stf).
Stale IPv6 and ISO route caches will be treated by separate patches.
Thank you to Christoph Badura for suggesting the general approach
to invalidating route caches that I take here.
Here are the details:
Add hooks to struct domain for tracking and for invalidating each
domain's route caches: dom_rtcache, dom_rtflush, and dom_rtflushall.
Introduce helper subroutines, rtflush(ro) for invalidating a route
cache, rtflushall(family) for invalidating all route caches in a
routing domain, and rtcache(ro) for notifying the domain of a new
cached route.
Chain together all IPv4 route caches where ro_rt != NULL. Provide
in_rtcache() for adding a route to the chain. Provide in_rtflush()
and in_rtflushall() for invalidating IPv4 route caches. In
in_rtflush(), set ro_rt to NULL, and remove the route from the
chain. In in_rtflushall(), walk the chain and remove every route
cache.
In rtrequest1(), call rtflushall() to invalidate route caches when
a route is added.
In gif(4), discard the workaround for stale caches that involves
expiring them every so often.
Replace the pattern 'RTFREE(ro->ro_rt); ro->ro_rt = NULL;' with a
call to rtflush(ro).
Update ipflow_fastforward() and all other users of route caches so
that they expect a cached route, ro->ro_rt, to turn to NULL.
Take care when moving a 'struct route' to rtflush() the source and
to rtcache() the destination.
In domain initializers, use .dom_xxx tags.
KNF here and there.
2006-12-09 08:33:04 +03:00
|
|
|
|
if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
|
2008-01-05 02:26:44 +03:00
|
|
|
|
(rt = rtcache_validate(&ipf->ipf_ro)) == NULL) {
|
1998-06-02 19:48:03 +04:00
|
|
|
|
ipflow_free(ipf);
|
|
|
|
|
} else {
|
|
|
|
|
ipf->ipf_last_uses = ipf->ipf_uses;
|
2007-12-20 22:53:29 +03:00
|
|
|
|
rt->rt_use += ipf->ipf_uses;
|
2006-09-02 16:41:01 +04:00
|
|
|
|
ipstat.ips_total += ipf->ipf_uses;
|
1998-06-02 19:48:03 +04:00
|
|
|
|
ipstat.ips_forward += ipf->ipf_uses;
|
|
|
|
|
ipstat.ips_fastforward += ipf->ipf_uses;
|
|
|
|
|
ipf->ipf_uses = 0;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2005-02-04 01:43:34 +03:00
|
|
|
|
ipflow_create(const struct route *ro, struct mbuf *m)
|
1998-04-30 01:37:52 +04:00
|
|
|
|
{
|
2008-01-05 02:28:07 +03:00
|
|
|
|
const struct ip *const ip = mtod(m, const struct ip *);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
struct ipflow *ipf;
|
2007-04-05 22:11:47 +04:00
|
|
|
|
size_t hash;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
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 {
|
2006-10-06 07:20:47 +04:00
|
|
|
|
s = splnet();
|
1998-10-08 05:41:45 +04:00
|
|
|
|
ipf = pool_get(&ipflow_pool, PR_NOWAIT);
|
2006-10-05 21:35:19 +04:00
|
|
|
|
splx(s);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
if (ipf == NULL)
|
|
|
|
|
return;
|
|
|
|
|
ipflow_inuse++;
|
|
|
|
|
}
|
2007-01-26 22:12:21 +03:00
|
|
|
|
memset(ipf, 0, sizeof(*ipf));
|
1998-04-30 01:37:52 +04:00
|
|
|
|
} 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);
|
2006-12-16 00:18:52 +03:00
|
|
|
|
rtcache_free(&ipf->ipf_ro);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
ipf->ipf_uses = ipf->ipf_last_uses = 0;
|
|
|
|
|
ipf->ipf_errors = ipf->ipf_dropped = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Fill in the updated information.
|
|
|
|
|
*/
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
|
rtcache_copy(&ipf->ipf_ro, ro);
|
1998-04-30 01:37:52 +04:00
|
|
|
|
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);
|
2006-06-08 02:33:33 +04:00
|
|
|
|
ipf->ipf_start = time_uptime;
|
1998-04-30 01:37:52 +04:00
|
|
|
|
/*
|
|
|
|
|
* Insert into the approriate bucket of the flow table.
|
|
|
|
|
*/
|
2007-04-05 22:11:47 +04:00
|
|
|
|
hash = ipflow_hash(ip);
|
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);
|
|
|
|
|
}
|
2003-12-13 00:17:59 +03:00
|
|
|
|
|
2007-03-26 00:12:20 +04:00
|
|
|
|
int
|
|
|
|
|
ipflow_invalidate_all(int new_size)
|
2003-12-13 00:17:59 +03:00
|
|
|
|
{
|
|
|
|
|
struct ipflow *ipf, *next_ipf;
|
2007-03-26 00:12:20 +04:00
|
|
|
|
int s, error;
|
2003-12-13 00:17:59 +03:00
|
|
|
|
|
2007-03-26 00:12:20 +04:00
|
|
|
|
error = 0;
|
2003-12-13 00:17:59 +03:00
|
|
|
|
s = splnet();
|
|
|
|
|
for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
|
|
|
|
|
next_ipf = LIST_NEXT(ipf, ipf_list);
|
|
|
|
|
ipflow_free(ipf);
|
|
|
|
|
}
|
2007-03-26 00:12:20 +04:00
|
|
|
|
|
|
|
|
|
if (new_size)
|
|
|
|
|
error = ipflow_init(new_size);
|
2003-12-13 00:17:59 +03:00
|
|
|
|
splx(s);
|
2007-03-26 00:12:20 +04:00
|
|
|
|
|
|
|
|
|
return error;
|
2003-12-13 00:17:59 +03:00
|
|
|
|
}
|