-/* $KAME: in6_ifattach.c,v 1.41 2000/03/16 07:05:34 jinmei Exp $ */
+/* $FreeBSD: src/sys/netinet6/in6_ifattach.c,v 1.8 2002/04/19 04:46:22 suz Exp $ */
+/* $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <sys/md5.h>
+#include <kern/lock.h>
#include <net/if.h>
#include <net/if_dl.h>
size_t in6_ifstatmax = 0;
size_t icmp6_ifstatmax = 0;
unsigned long in6_maxmtu = 0;
+extern lck_mtx_t *nd6_mutex;
#if IP6_AUTO_LINKLOCAL
int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL;
int ip6_auto_linklocal = 1; /* enable by default */
#endif
-#ifndef __APPLE__
-struct callout in6_tmpaddrtimer_ch;
-#endif
extern struct inpcbinfo udbinfo;
extern struct inpcbinfo ripcbinfo;
+extern lck_mtx_t *rt_mtx;
-static int get_rand_ifid __P((struct ifnet *, struct in6_addr *));
-static int generate_tmp_ifid __P((u_int8_t *, const u_int8_t *, u_int8_t *));
-static int get_hw_ifid __P((struct ifnet *, struct in6_addr *));
-static int get_ifid __P((struct ifnet *, struct ifnet *, struct in6_addr *));
-static int in6_ifattach_linklocal __P((struct ifnet *, struct ifnet *));
-static int in6_ifattach_loopback __P((struct ifnet *));
+static int get_rand_ifid(struct ifnet *, struct in6_addr *);
+static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
+static int get_hw_ifid(struct ifnet *, struct in6_addr *);
+static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
+static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *, struct in6_aliasreq *);
+static int in6_ifattach_loopback(struct ifnet *);
#define EUI64_GBIT 0x01
#define EUI64_UBIT 0x02
* We currently use MD5(hostname) for it.
*/
static int
-get_rand_ifid(ifp, in6)
- struct ifnet *ifp;
- struct in6_addr *in6; /*upper 64bits are preserved */
+get_rand_ifid(
+ struct ifnet *ifp,
+ struct in6_addr *in6) /* upper 64bits are preserved */
{
MD5_CTX ctxt;
u_int8_t digest[16];
}
static int
-generate_tmp_ifid(seed0, seed1, ret)
- u_int8_t *seed0, *ret;
- const u_int8_t *seed1;
+generate_tmp_ifid(
+ u_int8_t *seed0,
+ const u_int8_t *seed1,
+ u_int8_t *ret)
{
MD5_CTX ctxt;
u_int8_t seed[16], digest[16], nullbuf[8];
val32 = random() ^ tv.tv_usec;
bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
}
- } else
+ } else {
bcopy(seed0, seed, 8);
+ }
/* copy the right-most 64-bits of the given address */
/* XXX assumption on the size of IFID */
* XXX assumes single sockaddr_dl (AF_LINK address) per an interface
*/
static int
-get_hw_ifid(ifp, in6)
- struct ifnet *ifp;
- struct in6_addr *in6; /*upper 64bits are preserved */
+get_hw_ifid(
+ struct ifnet *ifp,
+ struct in6_addr *in6) /* upper 64bits are preserved */
{
struct ifaddr *ifa;
struct sockaddr_dl *sdl;
static u_int8_t allone[8] =
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
+ /* Why doesn't this code use ifnet_addrs? */
+ ifnet_lock_shared(ifp);
for (ifa = ifp->if_addrlist.tqh_first;
ifa;
ifa = ifa->ifa_list.tqe_next)
goto found;
}
+ ifnet_lock_done(ifp);
return -1;
found:
+ ifnet_lock_done(ifp);
addr = LLADDR(sdl);
addrlen = sdl->sdl_alen;
case IFT_FDDI:
case IFT_ATM:
case IFT_IEEE1394:
+ case IFT_L2VLAN:
+ case IFT_IEEE8023ADLAG:
#if IFT_IEEE80211
case IFT_IEEE80211:
#endif
* sources.
*/
static int
-get_ifid(ifp0, altifp, in6)
- struct ifnet *ifp0;
- struct ifnet *altifp; /*secondary EUI64 source*/
- struct in6_addr *in6;
+get_ifid(
+ struct ifnet *ifp0,
+ struct ifnet *altifp, /* secondary EUI64 source */
+ struct in6_addr *in6)
{
struct ifnet *ifp;
}
/* next, try to get it from some other hardware interface */
- for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
- {
+ ifnet_head_lock_shared();
+ TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
if (ifp == ifp0)
continue;
if (get_hw_ifid(ifp, in6) != 0)
nd6log((LOG_DEBUG,
"%s: borrow interface identifier from %s\n",
if_name(ifp0), if_name(ifp)));
+ ifnet_head_done();
goto success;
}
}
+ ifnet_head_done();
/* last resort: get from random number source */
if (get_rand_ifid(ifp, in6) == 0) {
}
static int
-in6_ifattach_linklocal(ifp, altifp)
- struct ifnet *ifp;
- struct ifnet *altifp; /* secondary EUI64 source */
+in6_ifattach_linklocal(
+ struct ifnet *ifp,
+ struct ifnet *altifp, /* secondary EUI64 source */
+ struct in6_aliasreq *ifra_passed)
{
struct in6_ifaddr *ia;
struct in6_aliasreq ifra;
*/
bzero(&ifra, sizeof(ifra));
+ dlil_plumb_protocol(PF_INET6, ifp);
+
/*
* in6_update_ifa() does not use ifra_name, but we accurately set it
* for safety.
*/
strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
- ifra.ifra_addr.sin6_family = AF_INET6;
- ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
- ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
+ if (ifp->if_type == IFT_PPP && ifra_passed != NULL) /* PPP provided both addresses for us */
+ bcopy(&ifra_passed->ifra_addr, &(ifra.ifra_addr), sizeof(struct sockaddr_in6));
+ else {
+ ifra.ifra_addr.sin6_family = AF_INET6;
+ ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
+ ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
#if SCOPEDROUTING
- ifra.ifra_addr.sin6_addr.s6_addr16[1] = 0
+ ifra.ifra_addr.sin6_addr.s6_addr16[1] = 0
#else
- ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); /* XXX */
+ ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); /* XXX */
#endif
- ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
- if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
- ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
- ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
- } else {
- if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
- nd6log((LOG_ERR,
- "%s: no ifid available\n", if_name(ifp)));
- return -1;
+ ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
+ if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
+ ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
+ ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
+ } else {
+ if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
+ nd6log((LOG_ERR,
+ " %s: no ifid available\n", if_name(ifp)));
+ return -1;
+ }
}
- }
#if SCOPEDROUTING
- ifra.ifra_addr.sin6_scope_id =
- in6_addr2scopeid(ifp, &ifra.ifra_addr.sin6_addr);
+ ifra.ifra_addr.sin6_scope_id =
+ in6_addr2scopeid(ifp, &ifra.ifra_addr.sin6_addr);
#endif
-
+ }
ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
ifra.ifra_prefixmask.sin6_family = AF_INET6;
ifra.ifra_prefixmask.sin6_addr = in6mask64;
/*
* Do not let in6_update_ifa() do DAD, since we need a random delay
- * before sending an NS at the first time the inteface becomes up.
+ * before sending an NS at the first time the interface becomes up.
* Instead, in6_if_up() will start DAD with a proper random delay.
*/
ifra.ifra_flags |= IN6_IFF_NODAD;
/*
* Now call in6_update_ifa() to do a bunch of procedures to configure
* a link-local address. We can set NULL to the 3rd argument, because
- * we know there's no other link-local address on the interface.
+ * we know there's no other link-local address on the interface
+ * and therefore we are adding one (instead of updating one).
*/
if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
/*
}
static int
-in6_ifattach_loopback(ifp)
- struct ifnet *ifp; /* must be IFT_LOOP */
+in6_ifattach_loopback(
+ struct ifnet *ifp) /* must be IFT_LOOP */
{
struct in6_aliasreq ifra;
int error;
ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
- /* we don't need to perfrom DAD on loopback interfaces. */
+ /* we don't need to perform DAD on loopback interfaces. */
ifra.ifra_flags |= IN6_IFF_NODAD;
/* skip registration to the prefix list. XXX should be temporary. */
ifra.ifra_flags |= IN6_IFF_NOPFX;
/*
- * We can set NULL to the 3rd arg. See comments in
- * in6_ifattach_linklocal().
+ * We are sure that this is a newly assigned address, so we can set
+ * NULL to the 3rd arg.
*/
if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
log(LOG_ERR, "in6_ifattach_loopback: failed to configure "
* when ifp == NULL, the caller is responsible for filling scopeid.
*/
int
-in6_nigroup(ifp, name, namelen, in6)
- struct ifnet *ifp;
- const char *name;
- int namelen;
- struct in6_addr *in6;
+in6_nigroup(
+ struct ifnet *ifp,
+ const char *name,
+ int namelen,
+ struct in6_addr *in6)
{
const char *p;
u_char *q;
while (p && *p && *p != '.' && p - name < namelen)
p++;
if (p - name > sizeof(n) - 1)
- return -1; /*label too long*/
+ return -1; /* label too long */
l = p - name;
strncpy(n, name, l);
n[(int)l] = '\0';
}
void
-in6_nigroup_attach(name, namelen)
- const char *name;
- int namelen;
+in6_nigroup_attach(
+ const char *name,
+ int namelen)
{
struct ifnet *ifp;
struct sockaddr_in6 mltaddr;
if (in6_nigroup(NULL, name, namelen, &mltaddr.sin6_addr) != 0)
return;
- for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
- {
+ ifnet_head_lock_shared();
+ TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
+ ifnet_lock_shared(ifp);
IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
+ ifnet_lock_done(ifp);
if (!in6m) {
- if (!in6_addmulti(&mltaddr.sin6_addr, ifp, &error)) {
+ if (!in6_addmulti(&mltaddr.sin6_addr, ifp, &error, 0)) {
nd6log((LOG_ERR, "%s: failed to join %s "
"(errno=%d)\n", if_name(ifp),
ip6_sprintf(&mltaddr.sin6_addr),
}
}
}
+ ifnet_head_done();
}
void
-in6_nigroup_detach(name, namelen)
- const char *name;
- int namelen;
+in6_nigroup_detach(
+ const char *name,
+ int namelen)
{
struct ifnet *ifp;
struct sockaddr_in6 mltaddr;
if (in6_nigroup(NULL, name, namelen, &mltaddr.sin6_addr) != 0)
return;
- for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
- {
+ ifnet_head_lock_shared();
+ TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
+ ifnet_lock_shared(ifp);
IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
+ ifnet_lock_done(ifp);
if (in6m)
- in6_delmulti(in6m);
+ in6_delmulti(in6m, 0);
}
+ ifnet_head_done();
}
/*
* XXX multiple link-local address case
*/
void
-in6_ifattach(ifp, altifp)
- struct ifnet *ifp;
- struct ifnet *altifp; /* secondary EUI64 source */
+in6_ifattach(
+ struct ifnet *ifp,
+ struct ifnet *altifp, /* secondary EUI64 source */
+ struct in6_aliasreq *ifra)
{
static size_t if_indexlim = 8;
struct in6_ifaddr *ia;
struct in6_addr in6;
- u_long dl_tag;
-
- switch (ifp->if_type) {
-#if IFT_BRIDGE /*OpenBSD 2.8*/
- /* some of the interfaces are inherently not IPv6 capable */
- case IFT_BRIDGE:
- return;
-#endif
-#ifdef __APPLE__
- case IFT_ETHER:
- dl_tag = ether_attach_inet6(ifp);
- break;
-
- case IFT_LOOP:
- dl_tag = lo_attach_inet6(ifp);
-#if NGIF > 0
- case IFT_GIF:
- dl_tag = gif_attach_proto_family(ifp, PF_INET6);
- break;
-#endif
-#if NSTF > 0
- case IFT_STF:
- dl_tag = stf_attach_inet6(ifp);
- break;
-#endif
-#endif
- }
/*
* We have some arrays that should be indexed by if_index.
icmp6_ifstatmax = if_indexlim;
}
+ /* initialize NDP variables */
+ nd6_ifattach(ifp);
+
/* initialize scope identifiers */
scope6_ifattach(ifp);
#if IFT_STF
case IFT_STF:
/*
- * 6to4 interface is a very speical kind of beast.
- * no multicast, no linklocal (based on 03 draft).
+ * 6to4 interface is a very special kind of beast.
+ * no multicast, no linklocal. RFC2529 specifies how to make
+ * linklocals for 6to4 interface, but there's no use and
+ * it is rather harmful to have one.
*/
goto statinit;
#endif
return;
}
- /* initialize NDP variables */
- nd6_ifattach(ifp);
-
/*
* assign loopback address for loopback interface.
* XXX multiple loopback interface case.
*/
if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
+ struct in6_ifaddr *ia6 = NULL;
in6 = in6addr_loopback;
- if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
+ if ((ia6 = in6ifa_ifpwithaddr(ifp, &in6)) == NULL) {
if (in6_ifattach_loopback(ifp) != 0)
return;
}
+ else {
+ ifafree(&ia6->ia_ifa);
+ }
}
/*
if (ip6_auto_linklocal) {
ia = in6ifa_ifpforlinklocal(ifp, 0);
if (ia == NULL) {
- if (in6_ifattach_linklocal(ifp, altifp) == 0) {
+ if (in6_ifattach_linklocal(ifp, altifp, ifra) == 0) {
/* linklocal address assigned */
} else {
+ log(LOG_INFO, "in6_ifattach: "
+ "%s failed to attach a linklocal address.\n",
+ if_name(ifp));
/* failed to assign linklocal address. bark? */
}
}
_MALLOC(sizeof(struct icmp6_ifstat), M_IFADDR, M_WAITOK);
bzero(icmp6_ifstat[ifp->if_index], sizeof(struct icmp6_ifstat));
}
-
}
/*
* from the ifnet list in bsdi.
*/
void
-in6_ifdetach(ifp)
- struct ifnet *ifp;
+in6_ifdetach(
+ struct ifnet *ifp)
{
- struct in6_ifaddr *ia, *oia;
+ struct in6_ifaddr *ia, *oia, *nia;
struct ifaddr *ifa, *next;
struct rtentry *rt;
short rtflags;
nd6_purge(ifp);
/* nuke any of IPv6 addresses we have */
- for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
- {
- next = ifa->ifa_list.tqe_next;
- if (ifa->ifa_addr->sa_family != AF_INET6)
+
+ lck_mtx_lock(nd6_mutex);
+ for (ia = in6_ifaddrs; ia != NULL; ia = nia) {
+ nia = ia->ia_next;
+ if (ia->ia_ifa.ifa_ifp != ifp)
continue;
- in6_purgeaddr(ifa);
+ in6_purgeaddr(&ia->ia_ifa, 1);
}
+ lck_mtx_unlock(nd6_mutex);
+
+ ifnet_lock_exclusive(ifp);
/* undo everything done by in6_ifattach(), just in case */
for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
ia = (struct in6_ifaddr *)ifa;
/* remove from the routing table */
+ lck_mtx_lock(rt_mtx);
if ((ia->ia_flags & IFA_ROUTE)
- && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
+ && (rt = rtalloc1_locked((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
rtflags = rt->rt_flags;
- rtfree(rt);
- rtrequest(RTM_DELETE,
+ rtfree_locked(rt);
+ rtrequest_locked(RTM_DELETE,
(struct sockaddr *)&ia->ia_addr,
(struct sockaddr *)&ia->ia_addr,
(struct sockaddr *)&ia->ia_prefixmask,
rtflags, (struct rtentry **)0);
}
+ lck_mtx_unlock(rt_mtx);
/* remove from the linked list */
- TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
+ if_detach_ifa(ifp, &ia->ia_ifa);
ifafree(&ia->ia_ifa);
/* also remove from the IPv6 address chain(itojun&jinmei) */
oia = ia;
- if (oia == (ia = in6_ifaddr))
- in6_ifaddr = ia->ia_next;
+ lck_mtx_lock(nd6_mutex);
+ if (oia == (ia = in6_ifaddrs))
+ in6_ifaddrs = ia->ia_next;
else {
while (ia->ia_next && (ia->ia_next != oia))
ia = ia->ia_next;
"list\n", if_name(ifp)));
}
}
+ lck_mtx_unlock(nd6_mutex);
- IFAFREE(&oia->ia_ifa);
- }
-
- /* leave from all multicast groups joined */
- in6_pcbpurgeif0(LIST_FIRST(udbinfo.listhead), ifp);
- in6_pcbpurgeif0(LIST_FIRST(ripcbinfo.listhead), ifp);
- for (in6m = LIST_FIRST(&in6_multihead); in6m; in6m = in6m_next) {
- in6m_next = LIST_NEXT(in6m, in6m_entry);
- if (in6m->in6m_ifp != ifp)
- continue;
- in6_delmulti(in6m);
- in6m = NULL;
+ ifafree(&oia->ia_ifa);
}
+ ifnet_lock_done(ifp);
/*
* remove neighbor management table. we call it twice just to make
sin6.sin6_family = AF_INET6;
sin6.sin6_addr = in6addr_linklocal_allnodes;
sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
- rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
+ lck_mtx_lock(rt_mtx);
+ rt = rtalloc1_locked((struct sockaddr *)&sin6, 0, 0UL);
if (rt && rt->rt_ifp == ifp) {
- rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
+ rtrequest_locked(RTM_DELETE, (struct sockaddr *)rt_key(rt),
rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
- rtfree(rt);
+ rtfree_locked(rt);
}
+ lck_mtx_unlock(rt_mtx);
}
void
-in6_get_tmpifid(ifp, retbuf, baseid, generate)
- struct ifnet *ifp;
- u_int8_t *retbuf;
- const u_int8_t *baseid;
- int generate;
+in6_get_tmpifid(
+ struct ifnet *ifp,
+ u_int8_t *retbuf,
+ const u_int8_t *baseid,
+ int generate)
{
u_int8_t nullbuf[8];
struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
bcopy(ndi->randomid, retbuf, 8);
}
-void
-in6_tmpaddrtimer_funneled(void *ignored_arg)
-{
-#ifdef __APPLE__
- boolean_t funnel_state;
- funnel_state = thread_funnel_set(network_flock, TRUE);
-#endif
- in6_tmpaddrtimer(ignored_arg);
-#ifdef __APPLE__
- (void) thread_funnel_set(network_flock, FALSE);
-#endif
-}
-
+extern size_t nd_ifinfo_indexlim;
+extern int ip6_use_tempaddr;
void
-in6_tmpaddrtimer(ignored_arg)
- void *ignored_arg;
+in6_tmpaddrtimer(
+ void *ignored_arg)
{
int i;
struct nd_ifinfo *ndi;
u_int8_t nullbuf[8];
int s = splnet();
- timeout(in6_tmpaddrtimer_funneled, (caddr_t)0,
+ timeout(in6_tmpaddrtimer, (caddr_t)0,
(ip6_temp_preferred_lifetime - ip6_desync_factor -
ip6_temp_regen_advance) * hz);
- bzero(nullbuf, sizeof(nullbuf));
- for (i = 1; i < if_index + 1; i++) {
- ndi = &nd_ifinfo[i];
- if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
- /*
- * We've been generating a random ID on this interface.
- * Create a new one.
- */
- (void)generate_tmp_ifid(ndi->randomseed0,
- ndi->randomseed1,
- ndi->randomid);
+ if (ip6_use_tempaddr) {
+
+ bzero(nullbuf, sizeof(nullbuf));
+ for (i = 1; i < nd_ifinfo_indexlim + 1; i++) {
+ ndi = &nd_ifinfo[i];
+ if (ndi->flags != ND6_IFF_PERFORMNUD)
+ continue;
+ if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
+ /*
+ * We've been generating a random ID on this interface.
+ * Create a new one.
+ */
+ (void)generate_tmp_ifid(ndi->randomseed0,
+ ndi->randomseed1,
+ ndi->randomid);
+ }
}
}