2008-01-10 11:06:11 +03:00
|
|
|
/* $NetBSD: in6_pcb.c,v 1.92 2008/01/10 08:06:11 dyoung Exp $ */
|
2001-02-11 09:49:49 +03:00
|
|
|
/* $KAME: in6_pcb.c,v 1.84 2001/02/08 18:02:08 itojun Exp $ */
|
1999-07-04 01:24:45 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-06-03 18:36:32 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* 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. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2000-06-03 18:36:32 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT 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 PROJECT 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1999-06-28 10:36:47 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
|
|
|
*
|
|
|
|
* @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
|
|
|
|
*/
|
|
|
|
|
2001-11-13 03:56:55 +03:00
|
|
|
#include <sys/cdefs.h>
|
2008-01-10 11:06:11 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.92 2008/01/10 08:06:11 dyoung Exp $");
|
2001-11-13 03:56:55 +03:00
|
|
|
|
2002-11-06 00:46:42 +03:00
|
|
|
#include "opt_inet.h"
|
1999-07-10 02:57:15 +04:00
|
|
|
#include "opt_ipsec.h"
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/proc.h>
|
2006-05-15 01:19:33 +04:00
|
|
|
#include <sys/kauth.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/in_pcb.h>
|
2000-02-06 15:49:37 +03:00
|
|
|
#include <netinet/ip6.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netinet6/ip6_var.h>
|
2000-01-06 09:41:18 +03:00
|
|
|
#include <netinet6/in6_pcb.h>
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
#include <netinet6/scope6_var.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netinet6/nd6.h>
|
|
|
|
|
1999-07-17 11:07:08 +04:00
|
|
|
#include "faith.h"
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#ifdef IPSEC
|
|
|
|
#include <netinet6/ipsec.h>
|
|
|
|
#include <netkey/key.h>
|
|
|
|
#endif /* IPSEC */
|
|
|
|
|
2004-04-26 05:53:59 +04:00
|
|
|
#ifdef FAST_IPSEC
|
|
|
|
#include <netipsec/ipsec.h>
|
|
|
|
#include <netipsec/ipsec6.h>
|
|
|
|
#include <netipsec/key.h>
|
|
|
|
#endif /* FAST_IPSEC */
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6_addr zeroin6_addr;
|
|
|
|
|
2003-11-05 04:20:56 +03:00
|
|
|
#define IN6PCBHASH_PORT(table, lport) \
|
|
|
|
&(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
|
2003-09-04 13:16:57 +04:00
|
|
|
#define IN6PCBHASH_BIND(table, laddr, lport) \
|
|
|
|
&(table)->inpt_bindhashtbl[ \
|
|
|
|
(((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
|
|
|
|
(laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + ntohs(lport)) & \
|
|
|
|
(table)->inpt_bindhash]
|
|
|
|
#define IN6PCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
|
|
|
|
&(table)->inpt_bindhashtbl[ \
|
|
|
|
((((faddr)->s6_addr32[0] ^ (faddr)->s6_addr32[1] ^ \
|
|
|
|
(faddr)->s6_addr32[2] ^ (faddr)->s6_addr32[3]) + ntohs(fport)) + \
|
|
|
|
(((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
|
|
|
|
(laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + \
|
|
|
|
ntohs(lport))) & (table)->inpt_bindhash]
|
|
|
|
|
2000-08-26 15:03:45 +04:00
|
|
|
int ip6_anonportmin = IPV6PORT_ANONMIN;
|
|
|
|
int ip6_anonportmax = IPV6PORT_ANONMAX;
|
|
|
|
int ip6_lowportmin = IPV6PORT_RESERVEDMIN;
|
|
|
|
int ip6_lowportmax = IPV6PORT_RESERVEDMAX;
|
|
|
|
|
2007-03-12 21:18:22 +03:00
|
|
|
POOL_INIT(in6pcb_pool, sizeof(struct in6pcb), 0, 0, 0, "in6pcbpl", NULL,
|
|
|
|
IPL_SOFTNET);
|
2003-09-04 13:16:57 +04:00
|
|
|
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
|
2003-09-04 13:16:57 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
in_pcbinit(table, bindhashsize, connecthashsize);
|
|
|
|
table->inpt_lastport = (u_int16_t)ip6_anonportmax;
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
int
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcballoc(struct socket *so, void *v)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-09-04 13:16:57 +04:00
|
|
|
struct inpcbtable *table = v;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6pcb *in6p;
|
2002-03-21 05:11:39 +03:00
|
|
|
int s;
|
2004-04-26 05:53:59 +04:00
|
|
|
#if defined(IPSEC) || defined(FAST_IPSEC)
|
2001-07-26 03:28:02 +04:00
|
|
|
int error;
|
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2006-10-05 21:35:19 +04:00
|
|
|
s = splnet();
|
2003-09-04 13:16:57 +04:00
|
|
|
in6p = pool_get(&in6pcb_pool, PR_NOWAIT);
|
2006-10-05 21:35:19 +04:00
|
|
|
splx(s);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (in6p == NULL)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (ENOBUFS);
|
2007-03-04 08:59:00 +03:00
|
|
|
bzero((void *)in6p, sizeof(*in6p));
|
2003-09-04 13:16:57 +04:00
|
|
|
in6p->in6p_af = AF_INET6;
|
|
|
|
in6p->in6p_table = table;
|
1999-06-28 10:36:47 +04:00
|
|
|
in6p->in6p_socket = so;
|
|
|
|
in6p->in6p_hops = -1; /* use kernel default */
|
|
|
|
in6p->in6p_icmp6filt = NULL;
|
2004-04-26 05:53:59 +04:00
|
|
|
#if defined(IPSEC) || defined(FAST_IPSEC)
|
2002-06-11 23:39:59 +04:00
|
|
|
error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp);
|
2001-07-26 03:28:02 +04:00
|
|
|
if (error != 0) {
|
2006-10-05 21:35:19 +04:00
|
|
|
s = splnet();
|
2003-09-04 13:16:57 +04:00
|
|
|
pool_put(&in6pcb_pool, in6p);
|
2006-10-05 21:35:19 +04:00
|
|
|
splx(s);
|
2001-07-26 03:28:02 +04:00
|
|
|
return error;
|
|
|
|
}
|
2001-10-24 10:36:37 +04:00
|
|
|
#endif /* IPSEC */
|
2002-03-21 05:11:39 +03:00
|
|
|
s = splnet();
|
2003-09-04 13:16:57 +04:00
|
|
|
CIRCLEQ_INSERT_HEAD(&table->inpt_queue, (struct inpcb_hdr*)in6p,
|
|
|
|
inph_queue);
|
2003-11-05 04:20:56 +03:00
|
|
|
LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
|
|
|
|
&in6p->in6p_head, inph_lhash);
|
2003-09-04 13:16:57 +04:00
|
|
|
in6_pcbstate(in6p, IN6P_ATTACHED);
|
2002-03-21 05:11:39 +03:00
|
|
|
splx(s);
|
2001-10-15 13:51:15 +04:00
|
|
|
if (ip6_v6only)
|
|
|
|
in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
|
2007-03-04 08:59:00 +03:00
|
|
|
so->so_pcb = (void *)in6p;
|
2002-09-11 06:46:42 +04:00
|
|
|
return (0);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbbind(void *v, struct mbuf *nam, struct lwp *l)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-09-04 13:16:57 +04:00
|
|
|
struct in6pcb *in6p = v;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct socket *so = in6p->in6p_socket;
|
2003-09-04 13:16:57 +04:00
|
|
|
struct inpcbtable *table = in6p->in6p_table;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
|
2000-02-03 16:17:39 +03:00
|
|
|
u_int16_t lport = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return (EINVAL);
|
|
|
|
|
1999-12-13 18:17:17 +03:00
|
|
|
if (in6p->in6p_lport || !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
|
|
|
|
((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
|
|
|
|
(so->so_options & SO_ACCEPTCONN) == 0))
|
2003-09-04 13:16:57 +04:00
|
|
|
wild = 1;
|
1999-06-28 10:36:47 +04:00
|
|
|
if (nam) {
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
int error;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
sin6 = mtod(nam, struct sockaddr_in6 *);
|
|
|
|
if (nam->m_len != sizeof(*sin6))
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* We should check the family, but old programs
|
|
|
|
* incorrectly fail to intialize it.
|
|
|
|
*/
|
|
|
|
if (sin6->sin6_family != AF_INET6)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EAFNOSUPPORT);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
#ifndef INET
|
2000-05-29 04:03:18 +04:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EADDRNOTAVAIL);
|
2003-09-04 13:16:57 +04:00
|
|
|
#endif
|
2000-05-29 04:03:18 +04:00
|
|
|
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
|
|
|
|
return (error);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
lport = sin6->sin6_port;
|
|
|
|
if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
|
|
|
|
/*
|
|
|
|
* Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
|
|
|
|
* allow compepte duplication of binding if
|
|
|
|
* SO_REUSEPORT is set, or if SO_REUSEADDR is set
|
|
|
|
* and a multicast address is bound on both
|
|
|
|
* new and duplicated sockets.
|
|
|
|
*/
|
|
|
|
if (so->so_options & SO_REUSEADDR)
|
|
|
|
reuseport = SO_REUSEADDR|SO_REUSEPORT;
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
} else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
|
2003-09-04 13:16:57 +04:00
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
if (sin6->sin6_addr.s6_addr32[3]) {
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
|
|
|
bzero(&sin, sizeof(sin));
|
|
|
|
sin.sin_len = sizeof(sin);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
bcopy(&sin6->sin6_addr.s6_addr32[3],
|
|
|
|
&sin.sin_addr, sizeof(sin.sin_addr));
|
|
|
|
if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
|
|
|
|
return EADDRNOTAVAIL;
|
|
|
|
}
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
|
1999-06-28 10:36:47 +04:00
|
|
|
struct ifaddr *ia = NULL;
|
|
|
|
|
|
|
|
sin6->sin6_port = 0; /* yech... */
|
2001-05-11 22:38:03 +04:00
|
|
|
if ((in6p->in6p_flags & IN6P_FAITH) == 0 &&
|
|
|
|
(ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EADDRNOTAVAIL);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
2001-12-21 11:54:52 +03:00
|
|
|
* bind to an anycast address might accidentally
|
|
|
|
* cause sending a packet with an anycast source
|
|
|
|
* address, so we forbid it.
|
2002-08-21 02:06:04 +04:00
|
|
|
*
|
|
|
|
* We should allow to bind to a deprecated address,
|
|
|
|
* since the application dare to use it.
|
|
|
|
* But, can we assume that they are careful enough
|
|
|
|
* to check if the address is deprecated or not?
|
|
|
|
* Maybe, as a safeguard, we should have a setsockopt
|
|
|
|
* flag to control the bind(2) behavior against
|
|
|
|
* deprecated addresses (default: forbid bind(2)).
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2000-03-02 09:42:52 +03:00
|
|
|
if (ia &&
|
|
|
|
((struct in6_ifaddr *)ia)->ia6_flags &
|
2002-08-21 02:06:04 +04:00
|
|
|
(IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED))
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EADDRNOTAVAIL);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
if (lport) {
|
2000-01-26 20:06:36 +03:00
|
|
|
#ifndef IPNOPRIVPORTS
|
2000-07-07 19:54:16 +04:00
|
|
|
int priv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: all operating systems use suser() for
|
|
|
|
* privilege check! do not rewrite it into SS_PRIV.
|
|
|
|
*/
|
2006-07-24 02:06:03 +04:00
|
|
|
priv = (l && !kauth_authorize_generic(l->l_cred,
|
2007-01-04 22:07:03 +03:00
|
|
|
KAUTH_GENERIC_ISSUSER, NULL)) ? 1 : 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
/* GROSS */
|
2000-07-07 19:54:16 +04:00
|
|
|
if (ntohs(lport) < IPV6PORT_RESERVED && !priv)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EACCES);
|
2000-01-26 20:06:36 +03:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
|
2003-09-04 13:16:57 +04:00
|
|
|
#ifdef INET
|
1999-06-28 10:36:47 +04:00
|
|
|
struct inpcb *t;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
t = in_pcblookup_port(table,
|
|
|
|
*(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
|
|
|
|
lport, wild);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (t && (reuseport & t->inp_socket->so_options) == 0)
|
2003-09-04 13:16:57 +04:00
|
|
|
return (EADDRINUSE);
|
|
|
|
#else
|
|
|
|
return (EADDRNOTAVAIL);
|
1999-06-28 10:36:47 +04:00
|
|
|
#endif
|
2003-09-04 13:16:57 +04:00
|
|
|
}
|
|
|
|
|
2001-10-16 08:57:38 +04:00
|
|
|
{
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6pcb *t;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
t = in6_pcblookup_port(table, &sin6->sin6_addr,
|
|
|
|
lport, wild);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (t && (reuseport & t->in6p_socket->so_options) == 0)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EADDRINUSE);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
in6p->in6p_laddr = sin6->sin6_addr;
|
|
|
|
}
|
1999-12-13 18:17:17 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
if (lport == 0) {
|
1999-12-13 18:17:17 +03:00
|
|
|
int e;
|
2006-07-24 02:06:03 +04:00
|
|
|
e = in6_pcbsetport(&in6p->in6p_laddr, in6p, l);
|
2003-09-04 13:16:57 +04:00
|
|
|
if (e != 0)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (e);
|
2003-09-04 13:16:57 +04:00
|
|
|
} else {
|
1999-12-13 18:17:17 +03:00
|
|
|
in6p->in6p_lport = lport;
|
2003-09-04 13:16:57 +04:00
|
|
|
in6_pcbstate(in6p, IN6P_BOUND);
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-11-05 04:20:56 +03:00
|
|
|
LIST_REMOVE(&in6p->in6p_head, inph_lhash);
|
|
|
|
LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
|
|
|
|
&in6p->in6p_head, inph_lhash);
|
|
|
|
|
2003-09-06 07:12:51 +04:00
|
|
|
#if 0
|
|
|
|
in6p->in6p_flowinfo = 0; /* XXX */
|
|
|
|
#endif
|
2002-09-11 06:46:42 +04:00
|
|
|
return (0);
|
1999-12-13 18:17:17 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect from a socket to a specified address.
|
|
|
|
* Both address and port must be specified in argument sin6.
|
|
|
|
* If don't have a local address for this socket yet,
|
|
|
|
* then pick one.
|
|
|
|
*/
|
|
|
|
int
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbconnect(void *v, struct mbuf *nam, struct lwp *l)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2007-12-20 22:53:29 +03:00
|
|
|
struct rtentry *rt;
|
2003-09-04 13:16:57 +04:00
|
|
|
struct in6pcb *in6p = v;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6_addr *in6a = NULL;
|
|
|
|
struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
|
|
|
|
struct ifnet *ifp = NULL; /* outgoing interface */
|
|
|
|
int error = 0;
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
int scope_ambiguous = 0;
|
2000-10-02 07:55:41 +04:00
|
|
|
#ifdef INET
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6_addr mapped;
|
2000-10-02 07:55:41 +04:00
|
|
|
#endif
|
2000-06-08 17:51:33 +04:00
|
|
|
struct sockaddr_in6 tmp;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
(void)&in6a; /* XXX fool gcc */
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return (EINVAL);
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
if (nam->m_len != sizeof(*sin6))
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (sin6->sin6_family != AF_INET6)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EAFNOSUPPORT);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (sin6->sin6_port == 0)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EADDRNOTAVAIL);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
|
|
|
|
scope_ambiguous = 1;
|
|
|
|
if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
|
|
|
|
return(error);
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* sanity check for mapped address case */
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
|
2001-10-15 13:51:15 +04:00
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
return EINVAL;
|
1999-06-28 10:36:47 +04:00
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
|
|
|
|
in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
|
|
|
|
if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
|
|
|
|
return EINVAL;
|
2001-10-15 13:51:15 +04:00
|
|
|
} else
|
|
|
|
{
|
1999-06-28 10:36:47 +04:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2000-06-08 17:51:33 +04:00
|
|
|
/* protect *sin6 from overwrites */
|
|
|
|
tmp = *sin6;
|
|
|
|
sin6 = &tmp;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* Source address selection. */
|
2003-09-04 13:16:57 +04:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
|
|
|
|
in6p->in6p_laddr.s6_addr32[3] == 0) {
|
2000-10-02 07:55:41 +04:00
|
|
|
#ifdef INET
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sockaddr_in sin, *sinp;
|
|
|
|
|
|
|
|
bzero(&sin, sizeof(sin));
|
|
|
|
sin.sin_len = sizeof(sin);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
|
|
|
|
sizeof(sin.sin_addr));
|
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
|
|
|
sinp = in_selectsrc(&sin, &in6p->in6p_route,
|
1999-06-28 10:36:47 +04:00
|
|
|
in6p->in6p_socket->so_options, NULL, &error);
|
|
|
|
if (sinp == 0) {
|
|
|
|
if (error == 0)
|
|
|
|
error = EADDRNOTAVAIL;
|
2002-09-11 06:46:42 +04:00
|
|
|
return (error);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
bzero(&mapped, sizeof(mapped));
|
|
|
|
mapped.s6_addr16[5] = htons(0xffff);
|
|
|
|
bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
|
|
|
|
in6a = &mapped;
|
2000-10-02 07:55:41 +04:00
|
|
|
#else
|
|
|
|
return EADDRNOTAVAIL;
|
|
|
|
#endif
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
} else {
|
1999-12-13 18:17:17 +03:00
|
|
|
/*
|
|
|
|
* XXX: in6_selectsrc might replace the bound local address
|
|
|
|
* with the address specified by setsockopt(IPV6_PKTINFO).
|
|
|
|
* Is it the intended behavior?
|
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
|
1999-12-13 18:17:17 +03:00
|
|
|
in6p->in6p_moptions,
|
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
|
|
|
&in6p->in6p_route,
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
&in6p->in6p_laddr, &ifp, &error);
|
|
|
|
if (ifp && scope_ambiguous &&
|
|
|
|
(error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
|
|
|
|
return(error);
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
if (in6a == 0) {
|
|
|
|
if (error == 0)
|
|
|
|
error = EADDRNOTAVAIL;
|
2002-09-11 06:46:42 +04:00
|
|
|
return (error);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
2007-12-20 22:53:29 +03:00
|
|
|
if (ifp == NULL && (rt = rtcache_getrt(&in6p->in6p_route)) != NULL)
|
|
|
|
ifp = rt->rt_ifp;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
1999-12-13 18:17:17 +03:00
|
|
|
in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr,
|
|
|
|
sin6->sin6_port,
|
|
|
|
IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr,
|
|
|
|
in6p->in6p_lport, 0))
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EADDRINUSE);
|
2003-09-04 13:16:57 +04:00
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
|
|
|
|
(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
|
|
|
|
in6p->in6p_laddr.s6_addr32[3] == 0))
|
2001-10-15 13:51:15 +04:00
|
|
|
{
|
2000-07-07 19:54:16 +04:00
|
|
|
if (in6p->in6p_lport == 0) {
|
2006-07-24 02:06:03 +04:00
|
|
|
error = in6_pcbbind(in6p, (struct mbuf *)0, l);
|
2005-11-15 21:39:46 +03:00
|
|
|
if (error != 0)
|
|
|
|
return error;
|
2000-07-07 19:54:16 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
in6p->in6p_laddr = *in6a;
|
|
|
|
}
|
|
|
|
in6p->in6p_faddr = sin6->sin6_addr;
|
|
|
|
in6p->in6p_fport = sin6->sin6_port;
|
2003-09-04 13:16:57 +04:00
|
|
|
in6_pcbstate(in6p, IN6P_CONNECTED);
|
2003-09-06 07:12:51 +04:00
|
|
|
in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
|
|
|
|
if (ip6_auto_flowlabel)
|
|
|
|
in6p->in6p_flowinfo |=
|
2003-09-06 07:36:30 +04:00
|
|
|
(htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
|
2004-04-26 05:53:59 +04:00
|
|
|
#if defined(IPSEC) || defined(FAST_IPSEC)
|
2001-08-06 14:25:00 +04:00
|
|
|
if (in6p->in6p_socket->so_type == SOCK_STREAM)
|
|
|
|
ipsec_pcbconn(in6p->in6p_sp);
|
|
|
|
#endif
|
2002-09-11 06:46:42 +04:00
|
|
|
return (0);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbdisconnect(struct in6pcb *in6p)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2007-03-04 08:59:00 +03:00
|
|
|
bzero((void *)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
|
1999-06-28 10:36:47 +04:00
|
|
|
in6p->in6p_fport = 0;
|
2003-09-04 13:16:57 +04:00
|
|
|
in6_pcbstate(in6p, IN6P_BOUND);
|
2003-09-06 07:12:51 +04:00
|
|
|
in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
|
2004-04-26 05:53:59 +04:00
|
|
|
#if defined(IPSEC) || defined(FAST_IPSEC)
|
2001-08-06 14:25:00 +04:00
|
|
|
ipsec_pcbdisconn(in6p->in6p_sp);
|
|
|
|
#endif
|
2004-01-13 09:17:14 +03:00
|
|
|
if (in6p->in6p_socket->so_state & SS_NOFDREF)
|
|
|
|
in6_pcbdetach(in6p);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbdetach(struct in6pcb *in6p)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct socket *so = in6p->in6p_socket;
|
2002-03-21 05:11:39 +03:00
|
|
|
int s;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
2004-04-26 05:53:59 +04:00
|
|
|
#if defined(IPSEC) || defined(FAST_IPSEC)
|
1999-06-28 10:36:47 +04:00
|
|
|
ipsec6_delete_pcbpolicy(in6p);
|
|
|
|
#endif /* IPSEC */
|
2004-06-24 20:49:51 +04:00
|
|
|
so->so_pcb = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
sofree(so);
|
|
|
|
if (in6p->in6p_options)
|
|
|
|
m_freem(in6p->in6p_options);
|
|
|
|
if (in6p->in6p_outputopts) {
|
2006-12-16 00:18:52 +03:00
|
|
|
if (in6p->in6p_outputopts->ip6po_rthdr != NULL)
|
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_free(&in6p->in6p_outputopts->ip6po_route);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (in6p->in6p_outputopts->ip6po_m)
|
|
|
|
(void)m_free(in6p->in6p_outputopts->ip6po_m);
|
|
|
|
free(in6p->in6p_outputopts, M_IP6OPT);
|
|
|
|
}
|
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_free(&in6p->in6p_route);
|
1999-06-28 10:36:47 +04:00
|
|
|
ip6_freemoptions(in6p->in6p_moptions);
|
2002-03-21 05:11:39 +03:00
|
|
|
s = splnet();
|
2003-09-04 13:16:57 +04:00
|
|
|
in6_pcbstate(in6p, IN6P_ATTACHED);
|
2003-11-05 04:20:56 +03:00
|
|
|
LIST_REMOVE(&in6p->in6p_head, inph_lhash);
|
2003-09-04 13:16:57 +04:00
|
|
|
CIRCLEQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head,
|
|
|
|
inph_queue);
|
|
|
|
pool_put(&in6pcb_pool, in6p);
|
2006-10-05 21:35:19 +04:00
|
|
|
splx(s);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_setsockaddr(struct in6pcb *in6p, struct mbuf *nam)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
nam->m_len = sizeof(*sin6);
|
|
|
|
sin6 = mtod(nam, struct sockaddr_in6 *);
|
2007-11-10 03:14:31 +03:00
|
|
|
sockaddr_in6_init(sin6, &in6p->in6p_laddr, in6p->in6p_lport, 0, 0);
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_setpeeraddr(struct in6pcb *in6p, struct mbuf *nam)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
nam->m_len = sizeof(*sin6);
|
|
|
|
sin6 = mtod(nam, struct sockaddr_in6 *);
|
2007-11-10 03:14:31 +03:00
|
|
|
sockaddr_in6_init(sin6, &in6p->in6p_faddr, in6p->in6p_fport, 0, 0);
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pass some notification to all connections of a protocol
|
|
|
|
* associated with address dst. The local address and/or port numbers
|
|
|
|
* may be specified to limit the search. The "usual action" will be
|
|
|
|
* taken, depending on the ctlinput cmd. The caller must filter any
|
|
|
|
* cmds that are uninteresting (e.g., no error in the map).
|
|
|
|
* Call the protocol specific routine (if any) to report
|
|
|
|
* any errors for each matching socket.
|
|
|
|
*
|
1999-07-04 06:01:15 +04:00
|
|
|
* Must be called at splsoftnet.
|
2001-02-11 09:49:49 +03:00
|
|
|
*
|
|
|
|
* Note: src (4th arg) carries the flowlabel value on the original IPv6
|
|
|
|
* header, in sin6_flowinfo member.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
int
|
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
|
|
|
in6_pcbnotify(struct inpcbtable *table, const struct sockaddr *dst,
|
|
|
|
u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd,
|
|
|
|
void *cmdarg, void (*notify)(struct in6pcb *, int))
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2007-12-20 22:53:29 +03:00
|
|
|
struct rtentry *rt;
|
2000-03-02 09:42:52 +03:00
|
|
|
struct in6pcb *in6p, *nin6p;
|
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
|
|
|
struct sockaddr_in6 sa6_src;
|
|
|
|
const struct sockaddr_in6 *sa6_dst;
|
2000-02-03 16:17:39 +03:00
|
|
|
u_int16_t fport = fport_arg, lport = lport_arg;
|
1999-06-28 10:36:47 +04:00
|
|
|
int errno;
|
|
|
|
int nmatch = 0;
|
2001-02-11 09:49:49 +03:00
|
|
|
u_int32_t flowinfo;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-30 04:01:18 +04:00
|
|
|
if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
2001-02-11 09:49:49 +03:00
|
|
|
|
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
|
|
|
sa6_dst = (const struct sockaddr_in6 *)dst;
|
2001-02-11 09:49:49 +03:00
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
|
|
|
|
2001-02-11 09:49:49 +03:00
|
|
|
/*
|
|
|
|
* note that src can be NULL when we get notify by local fragmentation.
|
|
|
|
*/
|
2005-05-30 01:43:51 +04:00
|
|
|
sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
|
2001-02-11 09:49:49 +03:00
|
|
|
flowinfo = sa6_src.sin6_flowinfo;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Redirects go to all references to the destination,
|
2000-03-02 09:42:52 +03:00
|
|
|
* and use in6_rtchange to invalidate the route cache.
|
|
|
|
* Dead host indications: also use in6_rtchange to invalidate
|
|
|
|
* the cache, and deliver the error to all the sockets.
|
1999-06-28 10:36:47 +04:00
|
|
|
* Otherwise, if we have knowledge of the local port and address,
|
|
|
|
* deliver only to that socket.
|
|
|
|
*/
|
|
|
|
if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
|
|
|
|
fport = 0;
|
|
|
|
lport = 0;
|
2007-03-04 08:59:00 +03:00
|
|
|
bzero((void *)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
|
2000-03-02 09:42:52 +03:00
|
|
|
|
2001-02-11 09:49:49 +03:00
|
|
|
if (cmd != PRC_HOSTDEAD)
|
|
|
|
notify = in6_rtchange;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-03-02 09:42:52 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
errno = inet6ctlerrmap[cmd];
|
2003-09-04 13:16:57 +04:00
|
|
|
for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue);
|
|
|
|
in6p != (void *)&table->inpt_queue;
|
|
|
|
in6p = nin6p) {
|
|
|
|
nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue);
|
|
|
|
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
2000-03-02 09:42:52 +03:00
|
|
|
|
2001-02-11 09:49:49 +03:00
|
|
|
/*
|
|
|
|
* Under the following condition, notify of redirects
|
|
|
|
* to the pcb, without making address matches against inpcb.
|
|
|
|
* - redirect notification is arrived.
|
|
|
|
* - the inpcb is unconnected.
|
|
|
|
* - the inpcb is caching !RTF_HOST routing entry.
|
|
|
|
* - the ICMPv6 notification is from the gateway cached in the
|
|
|
|
* inpcb. i.e. ICMPv6 notification is from nexthop gateway
|
|
|
|
* the inpcb used very recently.
|
|
|
|
*
|
|
|
|
* This is to improve interaction between netbsd/openbsd
|
|
|
|
* redirect handling code, and inpcb route cache code.
|
|
|
|
* without the clause, !RTF_HOST routing entry (which carries
|
|
|
|
* gateway used by inpcb right before the ICMPv6 redirect)
|
|
|
|
* will be cached forever in unconnected inpcb.
|
|
|
|
*
|
|
|
|
* There still is a question regarding to what is TRT:
|
|
|
|
* - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
|
|
|
|
* generated on packet output. inpcb will always cache
|
|
|
|
* RTF_HOST routing entry so there's no need for the clause
|
|
|
|
* (ICMPv6 redirect will update RTF_HOST routing entry,
|
|
|
|
* and inpcb is caching it already).
|
|
|
|
* However, bsdi/freebsd are vulnerable to local DoS attacks
|
|
|
|
* due to the cloned routing entries.
|
|
|
|
* - Specwise, "destination cache" is mentioned in RFC2461.
|
|
|
|
* Jinmei says that it implies bsdi/freebsd behavior, itojun
|
|
|
|
* is not really convinced.
|
|
|
|
* - Having hiwat/lowat on # of cloned host route (redirect/
|
|
|
|
* pmtud) may be a good idea. netbsd/openbsd has it. see
|
|
|
|
* icmp6_mtudisc_update().
|
|
|
|
*/
|
|
|
|
if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
|
|
|
|
IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
|
2007-12-20 22:53:29 +03:00
|
|
|
(rt = rtcache_getrt(&in6p->in6p_route)) != NULL &&
|
|
|
|
!(rt->rt_flags & RTF_HOST)) {
|
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_in6 *dst6;
|
2001-02-11 09:49:49 +03:00
|
|
|
|
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
|
|
|
dst6 = (const struct sockaddr_in6 *)
|
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_getdst(&in6p->in6p_route);
|
|
|
|
if (dst6 == NULL)
|
|
|
|
;
|
|
|
|
else if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
|
2001-02-11 09:49:49 +03:00
|
|
|
&sa6_dst->sin6_addr))
|
|
|
|
goto do_notify;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-03-02 09:42:52 +03:00
|
|
|
|
2006-05-05 04:03:21 +04:00
|
|
|
/*
|
|
|
|
* If the error designates a new path MTU for a destination
|
|
|
|
* and the application (associated with this socket) wanted to
|
|
|
|
* know the value, notify. Note that we notify for all
|
|
|
|
* disconnected sockets if the corresponding application
|
|
|
|
* wanted. This is because some UDP applications keep sending
|
|
|
|
* sockets disconnected.
|
|
|
|
* XXX: should we avoid to notify the value to TCP sockets?
|
|
|
|
*/
|
|
|
|
if (cmd == PRC_MSGSIZE && (in6p->in6p_flags & IN6P_MTU) != 0 &&
|
|
|
|
(IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) ||
|
|
|
|
IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &sa6_dst->sin6_addr))) {
|
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
|
|
|
ip6_notify_pmtu(in6p, (const struct sockaddr_in6 *)dst,
|
2006-05-05 04:03:21 +04:00
|
|
|
(u_int32_t *)cmdarg);
|
|
|
|
}
|
|
|
|
|
2001-02-11 09:49:49 +03:00
|
|
|
/*
|
|
|
|
* Detect if we should notify the error. If no source and
|
2001-10-16 08:57:38 +04:00
|
|
|
* destination ports are specified, but non-zero flowinfo and
|
2001-02-11 09:49:49 +03:00
|
|
|
* local address match, notify the error. This is the case
|
|
|
|
* when the error is delivered with an encrypted buffer
|
|
|
|
* by ESP. Otherwise, just compare addresses and ports
|
|
|
|
* as usual.
|
|
|
|
*/
|
|
|
|
if (lport == 0 && fport == 0 && flowinfo &&
|
|
|
|
in6p->in6p_socket != NULL &&
|
|
|
|
flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
|
|
|
|
IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
|
|
|
|
goto do_notify;
|
|
|
|
else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
|
|
|
|
&sa6_dst->sin6_addr) ||
|
2000-03-02 09:42:52 +03:00
|
|
|
in6p->in6p_socket == 0 ||
|
|
|
|
(lport && in6p->in6p_lport != lport) ||
|
2001-02-11 09:49:49 +03:00
|
|
|
(!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
|
|
|
|
!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
|
|
|
|
&sa6_src.sin6_addr)) ||
|
2000-03-02 09:42:52 +03:00
|
|
|
(fport && in6p->in6p_fport != fport))
|
|
|
|
continue;
|
|
|
|
|
2001-02-11 09:49:49 +03:00
|
|
|
do_notify:
|
|
|
|
if (notify)
|
|
|
|
(*notify)(in6p, errno);
|
1999-06-28 10:36:47 +04:00
|
|
|
nmatch++;
|
|
|
|
}
|
|
|
|
return nmatch;
|
|
|
|
}
|
|
|
|
|
2000-02-03 02:28:08 +03:00
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
|
2000-02-03 02:28:08 +03:00
|
|
|
{
|
|
|
|
struct in6pcb *in6p, *nin6p;
|
2001-06-27 19:53:14 +04:00
|
|
|
struct ip6_moptions *im6o;
|
|
|
|
struct in6_multi_mship *imm, *nimm;
|
2000-02-03 02:28:08 +03:00
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue);
|
|
|
|
in6p != (void *)&table->inpt_queue;
|
|
|
|
in6p = nin6p) {
|
|
|
|
nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue);
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
2001-06-27 19:53:14 +04:00
|
|
|
im6o = in6p->in6p_moptions;
|
|
|
|
if (im6o) {
|
|
|
|
/*
|
|
|
|
* Unselect the outgoing interface if it is being
|
|
|
|
* detached.
|
|
|
|
*/
|
|
|
|
if (im6o->im6o_multicast_ifp == ifp)
|
|
|
|
im6o->im6o_multicast_ifp = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Drop multicast group membership if we joined
|
|
|
|
* through the interface being detached.
|
|
|
|
* XXX controversial - is it really legal for kernel
|
|
|
|
* to force this?
|
|
|
|
*/
|
|
|
|
for (imm = im6o->im6o_memberships.lh_first;
|
|
|
|
imm != NULL; imm = nimm) {
|
|
|
|
nimm = imm->i6mm_chain.le_next;
|
|
|
|
if (imm->i6mm_maddr->in6m_ifp == ifp) {
|
|
|
|
LIST_REMOVE(imm, i6mm_chain);
|
2001-12-21 11:54:52 +03:00
|
|
|
in6_leavegroup(imm);
|
2001-06-27 19:53:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-02-03 02:28:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-02 19:25:34 +04:00
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
|
2001-07-02 19:25:34 +04:00
|
|
|
{
|
2007-12-20 22:53:29 +03:00
|
|
|
struct rtentry *rt;
|
2001-07-02 19:25:34 +04:00
|
|
|
struct in6pcb *in6p, *nin6p;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue);
|
|
|
|
in6p != (void *)&table->inpt_queue;
|
|
|
|
in6p = nin6p) {
|
|
|
|
nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue);
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
2007-12-20 22:53:29 +03:00
|
|
|
if ((rt = rtcache_getrt(&in6p->in6p_route)) != NULL &&
|
|
|
|
rt->rt_ifp == ifp)
|
2001-07-02 19:25:34 +04:00
|
|
|
in6_rtchange(in6p, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Check for alternatives when higher level complains
|
|
|
|
* about service problems. For now, invalidate cached
|
|
|
|
* routing information. If the route was created dynamically
|
|
|
|
* (by a redirect), time to try a default gateway again.
|
|
|
|
*/
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_losing(struct in6pcb *in6p)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct rtentry *rt;
|
|
|
|
struct rt_addrinfo info;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
2007-12-20 22:53:29 +03:00
|
|
|
if ((rt = rtcache_getrt(&in6p->in6p_route)) != NULL) {
|
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
|
|
|
memset(&info, 0, sizeof(info));
|
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
|
|
|
info.rti_info[RTAX_DST] = rtcache_getdst(&in6p->in6p_route);
|
1999-06-28 10:36:47 +04:00
|
|
|
info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
|
|
|
|
info.rti_info[RTAX_NETMASK] = rt_mask(rt);
|
|
|
|
rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
|
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
|
|
|
if (rt->rt_flags & RTF_DYNAMIC) {
|
Take steps to hide the radix_node implementation of the forwarding table
from the forwarding table's users:
Introduce rt_walktree() for walking the routing table and
applying a function to each rtentry. Replace most
rn_walktree() calls with it.
Use rt_getkey()/rt_setkey() to get/set a route's destination.
Keep a pointer to the sockaddr key in the rtentry, so that
rtentry users do not have to grovel in the radix_node for
the key.
Add a RTM_GET method to rtrequest. Use that instead of
radix_node lookups in, e.g., carp(4).
Add sys/net/link_proto.c, which supplies sockaddr routines for
link-layer socket addresses (sockaddr_dl).
Cosmetic:
Constify. KNF. Stop open-coding LIST_FOREACH, TAILQ_FOREACH,
et cetera. Use NULL instead of 0 for null pointers. Use
__arraycount(). Reduce gratuitous parenthesization.
Stop using variadic arguments for rip6_output(), it is
unnecessary.
Remove the unnecessary rtentry member rt_genmask and the
code to maintain it, since nothing actually used it.
Make rt_maskedcopy() easier to read by using meaningful variable
names.
Extract a subroutine intern_netmask() for looking up a netmask in
the masks table.
Start converting backslash-ridden IPv6 macros in
sys/netinet6/in6_var.h into inline subroutines that one
can read without special eyeglasses.
One functional change: when the kernel serves an RTM_GET, RTM_LOCK,
or RTM_CHANGE request, it applies the netmask (if supplied) to a
destination before searching for it in the forwarding table.
I have changed sys/netinet/ip_carp.c, carp_setroute(), to remove
the unlawful radix_node knowledge.
Apart from the changes to carp(4), netiso, ATM, and strip(4), I
have run the changes on three nodes in my wireless routing testbed,
which involves IPv4 + IPv6 dynamic routing acrobatics, and it's
working beautifully so far.
2007-07-20 00:48:52 +04:00
|
|
|
(void)rtrequest(RTM_DELETE, rt_getkey(rt),
|
|
|
|
rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
|
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
|
|
|
}
|
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_free(&in6p->in6p_route);
|
2006-12-08 19:06:22 +03:00
|
|
|
/*
|
|
|
|
* A new route can be allocated
|
|
|
|
* the next time output is attempted.
|
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
* After a routing change, flush old routing. A new route can be
|
|
|
|
* allocated the next time output is attempted.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
void
|
2006-11-16 04:32:37 +03:00
|
|
|
in6_rtchange(struct in6pcb *in6p, int errno)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
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_free(&in6p->in6p_route);
|
2006-12-16 00:18:52 +03:00
|
|
|
/*
|
|
|
|
* A new route can be allocated the next time
|
|
|
|
* output is attempted.
|
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct in6pcb *
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcblookup_port(struct inpcbtable *table, struct in6_addr *laddr6,
|
|
|
|
u_int lport_arg, int lookup_wildcard)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-11-05 04:20:56 +03:00
|
|
|
struct inpcbhead *head;
|
2003-09-04 13:16:57 +04:00
|
|
|
struct inpcb_hdr *inph;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6pcb *in6p, *match = 0;
|
|
|
|
int matchwild = 3, wildcard;
|
2003-09-04 13:16:57 +04:00
|
|
|
u_int16_t lport = lport_arg;
|
|
|
|
|
2003-11-05 04:20:56 +03:00
|
|
|
head = IN6PCBHASH_PORT(table, lport);
|
|
|
|
LIST_FOREACH(inph, head, inph_lhash) {
|
2003-09-04 13:16:57 +04:00
|
|
|
in6p = (struct in6pcb *)inph;
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
if (in6p->in6p_lport != lport)
|
|
|
|
continue;
|
|
|
|
wildcard = 0;
|
2003-09-04 13:16:57 +04:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
|
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
2000-01-06 09:41:18 +03:00
|
|
|
}
|
2003-09-04 13:16:57 +04:00
|
|
|
if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
|
|
|
|
wildcard++;
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
|
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
continue;
|
2000-01-06 09:41:18 +03:00
|
|
|
if (!IN6_IS_ADDR_V4MAPPED(laddr6))
|
|
|
|
continue;
|
2003-09-04 13:16:57 +04:00
|
|
|
|
|
|
|
/* duplicate of IPv4 logic */
|
|
|
|
wildcard = 0;
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) &&
|
|
|
|
in6p->in6p_faddr.s6_addr32[3])
|
2000-01-06 09:41:18 +03:00
|
|
|
wildcard++;
|
2003-09-04 13:16:57 +04:00
|
|
|
if (!in6p->in6p_laddr.s6_addr32[3]) {
|
|
|
|
if (laddr6->s6_addr32[3])
|
|
|
|
wildcard++;
|
|
|
|
} else {
|
|
|
|
if (!laddr6->s6_addr32[3])
|
|
|
|
wildcard++;
|
|
|
|
else {
|
|
|
|
if (in6p->in6p_laddr.s6_addr32[3] !=
|
|
|
|
laddr6->s6_addr32[3])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
|
2000-01-06 09:41:18 +03:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
|
2003-09-04 13:16:57 +04:00
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
2000-01-06 09:41:18 +03:00
|
|
|
continue;
|
2003-09-04 13:16:57 +04:00
|
|
|
}
|
|
|
|
if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
|
2000-01-06 09:41:18 +03:00
|
|
|
wildcard++;
|
2003-09-04 13:16:57 +04:00
|
|
|
} else {
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
|
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
2000-01-06 09:41:18 +03:00
|
|
|
continue;
|
2003-09-04 13:16:57 +04:00
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
|
1999-06-28 10:36:47 +04:00
|
|
|
wildcard++;
|
2003-09-04 13:16:57 +04:00
|
|
|
else {
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
|
|
|
|
laddr6))
|
|
|
|
continue;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2003-09-04 13:16:57 +04:00
|
|
|
if (wildcard && !lookup_wildcard)
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
|
|
|
if (wildcard < matchwild) {
|
|
|
|
match = in6p;
|
|
|
|
matchwild = wildcard;
|
|
|
|
if (matchwild == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-09-11 06:46:42 +04:00
|
|
|
return (match);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2003-09-04 13:16:57 +04:00
|
|
|
#undef continue
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-08-13 08:59:34 +04:00
|
|
|
/*
|
|
|
|
* WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to
|
|
|
|
* IPv4 mapped address.
|
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
struct rtentry *
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbrtentry(struct in6pcb *in6p)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2007-12-20 22:53:29 +03:00
|
|
|
struct rtentry *rt;
|
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
|
|
|
struct route *ro;
|
2007-11-22 00:18:25 +03:00
|
|
|
union {
|
|
|
|
const struct sockaddr *sa;
|
|
|
|
const struct sockaddr_in6 *sa6;
|
|
|
|
#ifdef INET
|
|
|
|
const struct sockaddr_in *sa4;
|
|
|
|
#endif
|
|
|
|
} cdst;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
ro = &in6p->in6p_route;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return (NULL);
|
|
|
|
|
2007-11-22 00:18:25 +03:00
|
|
|
cdst.sa = rtcache_getdst(ro);
|
|
|
|
if (cdst.sa == NULL)
|
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
|
|
|
;
|
2007-11-22 00:18:25 +03:00
|
|
|
#ifdef INET
|
|
|
|
else if (cdst.sa->sa_family == AF_INET) {
|
|
|
|
KASSERT(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr));
|
|
|
|
if (cdst.sa4->sin_addr.s_addr != in6p->in6p_faddr.s6_addr32[3])
|
|
|
|
rtcache_free(ro);
|
|
|
|
else
|
|
|
|
rtcache_check(ro);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&cdst.sa6->sin6_addr,
|
|
|
|
&in6p->in6p_faddr))
|
|
|
|
rtcache_free(ro);
|
|
|
|
else
|
|
|
|
rtcache_check(ro);
|
|
|
|
}
|
2007-12-20 22:53:29 +03:00
|
|
|
rt = rtcache_getrt(ro);
|
2003-08-13 08:59:34 +04:00
|
|
|
#ifdef INET
|
2007-12-20 22:53:29 +03:00
|
|
|
if (rt == NULL && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
|
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
|
|
|
union {
|
|
|
|
struct sockaddr dst;
|
|
|
|
struct sockaddr_in dst4;
|
|
|
|
} u;
|
|
|
|
struct in_addr addr;
|
2003-08-13 08:59:34 +04:00
|
|
|
|
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
|
|
|
addr.s_addr = in6p->in6p_faddr.s6_addr32[3];
|
|
|
|
|
|
|
|
sockaddr_in_init(&u.dst4, &addr, 0);
|
|
|
|
rtcache_setdst(ro, &u.dst);
|
|
|
|
|
2008-01-10 11:06:11 +03:00
|
|
|
rt = rtcache_init(ro);
|
2003-08-13 08:59:34 +04:00
|
|
|
} else
|
|
|
|
#endif
|
2007-12-20 22:53:29 +03:00
|
|
|
if (rt == NULL && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
|
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
|
|
|
union {
|
|
|
|
struct sockaddr dst;
|
|
|
|
struct sockaddr_in6 dst6;
|
|
|
|
} u;
|
|
|
|
|
|
|
|
sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0);
|
|
|
|
rtcache_setdst(ro, &u.dst);
|
|
|
|
|
2008-01-10 11:06:11 +03:00
|
|
|
rt = rtcache_init(ro);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2008-01-10 11:06:11 +03:00
|
|
|
return rt;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct in6pcb *
|
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
|
|
|
in6_pcblookup_connect(struct inpcbtable *table, const struct in6_addr *faddr6,
|
2006-10-12 05:30:41 +04:00
|
|
|
u_int fport_arg, const struct in6_addr *laddr6, u_int lport_arg,
|
2006-11-16 04:32:37 +03:00
|
|
|
int faith)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-09-04 13:16:57 +04:00
|
|
|
struct inpcbhead *head;
|
|
|
|
struct inpcb_hdr *inph;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6pcb *in6p;
|
2000-02-03 16:17:39 +03:00
|
|
|
u_int16_t fport = fport_arg, lport = lport_arg;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport);
|
|
|
|
LIST_FOREACH(inph, head, inph_hash) {
|
|
|
|
in6p = (struct in6pcb *)inph;
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* find exact match on both source and dest */
|
|
|
|
if (in6p->in6p_fport != fport)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_lport != lport)
|
|
|
|
continue;
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
|
|
|
|
continue;
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
|
|
|
|
continue;
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
|
|
|
|
continue;
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
|
|
|
|
continue;
|
2001-10-15 13:51:15 +04:00
|
|
|
if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
|
|
|
|
IN6_IS_ADDR_V4MAPPED(faddr6)) &&
|
|
|
|
(in6p->in6p_flags & IN6P_IPV6_V6ONLY))
|
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
return in6p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct in6pcb *
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcblookup_bind(struct inpcbtable *table, const struct in6_addr *laddr6,
|
|
|
|
u_int lport_arg, int faith)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-09-04 13:16:57 +04:00
|
|
|
struct inpcbhead *head;
|
|
|
|
struct inpcb_hdr *inph;
|
|
|
|
struct in6pcb *in6p;
|
2000-02-03 16:17:39 +03:00
|
|
|
u_int16_t lport = lport_arg;
|
2004-03-29 08:59:02 +04:00
|
|
|
#ifdef INET
|
2003-09-04 13:16:57 +04:00
|
|
|
struct in6_addr zero_mapped;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
head = IN6PCBHASH_BIND(table, laddr6, lport);
|
|
|
|
LIST_FOREACH(inph, head, inph_hash) {
|
|
|
|
in6p = (struct in6pcb *)inph;
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
1999-07-17 11:07:08 +04:00
|
|
|
if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
|
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
if (in6p->in6p_fport != 0)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_lport != lport)
|
|
|
|
continue;
|
2003-09-04 13:16:57 +04:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
|
|
|
|
(in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
continue;
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
#ifdef INET
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
|
|
|
|
memset(&zero_mapped, 0, sizeof(zero_mapped));
|
|
|
|
zero_mapped.s6_addr16[5] = 0xffff;
|
|
|
|
head = IN6PCBHASH_BIND(table, &zero_mapped, lport);
|
|
|
|
LIST_FOREACH(inph, head, inph_hash) {
|
|
|
|
in6p = (struct in6pcb *)inph;
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_fport != 0)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_lport != lport)
|
|
|
|
continue;
|
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
continue;
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped))
|
|
|
|
goto out;
|
2000-01-06 09:41:18 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2003-09-04 13:16:57 +04:00
|
|
|
#endif
|
|
|
|
head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport);
|
|
|
|
LIST_FOREACH(inph, head, inph_hash) {
|
|
|
|
in6p = (struct in6pcb *)inph;
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_fport != 0)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_lport != lport)
|
|
|
|
continue;
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
|
|
|
|
(in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
continue;
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
out:
|
|
|
|
inph = &in6p->in6p_head;
|
|
|
|
if (inph != LIST_FIRST(head)) {
|
|
|
|
LIST_REMOVE(inph, inph_hash);
|
|
|
|
LIST_INSERT_HEAD(head, inph, inph_hash);
|
|
|
|
}
|
|
|
|
return in6p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbstate(struct in6pcb *in6p, int state)
|
2003-09-04 13:16:57 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (in6p->in6p_state > IN6P_ATTACHED)
|
|
|
|
LIST_REMOVE(&in6p->in6p_head, inph_hash);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case IN6P_BOUND:
|
|
|
|
LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table,
|
|
|
|
&in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
|
|
|
|
inph_hash);
|
|
|
|
break;
|
|
|
|
case IN6P_CONNECTED:
|
|
|
|
LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table,
|
|
|
|
&in6p->in6p_faddr, in6p->in6p_fport,
|
|
|
|
&in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
|
|
|
|
inph_hash);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
in6p->in6p_state = state;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|