1caf43b02SWarner Losh /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro F. Giffuni * 482cd038dSYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 582cd038dSYoshinobu Inoue * All rights reserved. 682cd038dSYoshinobu Inoue * 782cd038dSYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 882cd038dSYoshinobu Inoue * modification, are permitted provided that the following conditions 982cd038dSYoshinobu Inoue * are met: 1082cd038dSYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 1182cd038dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 1282cd038dSYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 1382cd038dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 1482cd038dSYoshinobu Inoue * documentation and/or other materials provided with the distribution. 1582cd038dSYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 1682cd038dSYoshinobu Inoue * may be used to endorse or promote products derived from this software 1782cd038dSYoshinobu Inoue * without specific prior written permission. 1882cd038dSYoshinobu Inoue * 1982cd038dSYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2082cd038dSYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2182cd038dSYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2282cd038dSYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2382cd038dSYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2482cd038dSYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2582cd038dSYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2682cd038dSYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2782cd038dSYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2882cd038dSYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2982cd038dSYoshinobu Inoue * SUCH DAMAGE. 30b48287a3SDavid E. O'Brien * 31b48287a3SDavid E. O'Brien * $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $ 3282cd038dSYoshinobu Inoue */ 3382cd038dSYoshinobu Inoue 34b48287a3SDavid E. O'Brien #include <sys/cdefs.h> 35b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$"); 36b48287a3SDavid E. O'Brien 3782cd038dSYoshinobu Inoue #include <sys/param.h> 3882cd038dSYoshinobu Inoue #include <sys/systm.h> 3982cd038dSYoshinobu Inoue #include <sys/malloc.h> 4082cd038dSYoshinobu Inoue #include <sys/socket.h> 4182cd038dSYoshinobu Inoue #include <sys/sockio.h> 4276ca6f88SJamie Gritton #include <sys/jail.h> 4382cd038dSYoshinobu Inoue #include <sys/kernel.h> 44cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 45530c0060SRobert Watson #include <sys/proc.h> 46cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h> 4733841545SHajimu UMEMOTO #include <sys/syslog.h> 4882cd038dSYoshinobu Inoue #include <sys/md5.h> 4982cd038dSYoshinobu Inoue 5082cd038dSYoshinobu Inoue #include <net/if.h> 5176039bc8SGleb Smirnoff #include <net/if_var.h> 5282cd038dSYoshinobu Inoue #include <net/if_dl.h> 5382cd038dSYoshinobu Inoue #include <net/if_types.h> 5482cd038dSYoshinobu Inoue #include <net/route.h> 554b79449eSBjoern A. Zeeb #include <net/vnet.h> 5682cd038dSYoshinobu Inoue 5782cd038dSYoshinobu Inoue #include <netinet/in.h> 5882cd038dSYoshinobu Inoue #include <netinet/in_var.h> 5982cd038dSYoshinobu Inoue #include <netinet/if_ether.h> 60e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h> 61eddfbb76SRobert Watson #include <netinet/ip_var.h> 62eddfbb76SRobert Watson #include <netinet/udp.h> 63eddfbb76SRobert Watson #include <netinet/udp_var.h> 6482cd038dSYoshinobu Inoue 65686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 6682cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h> 6733841545SHajimu UMEMOTO #include <netinet6/in6_var.h> 68e43cc4aeSHajimu UMEMOTO #include <netinet6/in6_pcb.h> 6982cd038dSYoshinobu Inoue #include <netinet6/in6_ifattach.h> 7082cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h> 7182cd038dSYoshinobu Inoue #include <netinet6/nd6.h> 7233cde130SBruce M Simpson #include <netinet6/mld6_var.h> 73686cdd19SJun-ichiro itojun Hagino #include <netinet6/scope6_var.h> 7482cd038dSYoshinobu Inoue 7582cea7e6SBjoern A. Zeeb VNET_DEFINE(unsigned long, in6_maxmtu) = 0; 76eddfbb76SRobert Watson 7782cea7e6SBjoern A. Zeeb #ifdef IP6_AUTO_LINKLOCAL 7882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL; 7982cea7e6SBjoern A. Zeeb #else 8082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_auto_linklocal) = 1; /* enabled by default */ 8182cea7e6SBjoern A. Zeeb #endif 8282cea7e6SBjoern A. Zeeb 8382cea7e6SBjoern A. Zeeb VNET_DEFINE(struct callout, in6_tmpaddrtimer_ch); 841e77c105SRobert Watson #define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch) 85eddfbb76SRobert Watson 86eddfbb76SRobert Watson VNET_DECLARE(struct inpcbinfo, ripcbinfo); 871e77c105SRobert Watson #define V_ripcbinfo VNET(ripcbinfo) 88e43cc4aeSHajimu UMEMOTO 899233d8f3SDavid E. O'Brien static int get_rand_ifid(struct ifnet *, struct in6_addr *); 909233d8f3SDavid E. O'Brien static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *); 919233d8f3SDavid E. O'Brien static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *); 929233d8f3SDavid E. O'Brien static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *); 939233d8f3SDavid E. O'Brien static int in6_ifattach_loopback(struct ifnet *); 949233d8f3SDavid E. O'Brien static void in6_purgemaddrs(struct ifnet *); 9582cd038dSYoshinobu Inoue 96686cdd19SJun-ichiro itojun Hagino #define EUI64_GBIT 0x01 97686cdd19SJun-ichiro itojun Hagino #define EUI64_UBIT 0x02 98686cdd19SJun-ichiro itojun Hagino #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) 99686cdd19SJun-ichiro itojun Hagino #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 100686cdd19SJun-ichiro itojun Hagino #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6)) 101686cdd19SJun-ichiro itojun Hagino #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT) 102686cdd19SJun-ichiro itojun Hagino #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6)) 10382cd038dSYoshinobu Inoue 104686cdd19SJun-ichiro itojun Hagino #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6)) 105686cdd19SJun-ichiro itojun Hagino #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6)) 10682cd038dSYoshinobu Inoue 10782cd038dSYoshinobu Inoue /* 10882cd038dSYoshinobu Inoue * Generate a last-resort interface identifier, when the machine has no 10982cd038dSYoshinobu Inoue * IEEE802/EUI64 address sources. 110686cdd19SJun-ichiro itojun Hagino * The goal here is to get an interface identifier that is 111686cdd19SJun-ichiro itojun Hagino * (1) random enough and (2) does not change across reboot. 112686cdd19SJun-ichiro itojun Hagino * We currently use MD5(hostname) for it. 1131272577eSXin LI * 1141272577eSXin LI * in6 - upper 64bits are preserved 11582cd038dSYoshinobu Inoue */ 11682cd038dSYoshinobu Inoue static int 1171272577eSXin LI get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6) 11882cd038dSYoshinobu Inoue { 11982cd038dSYoshinobu Inoue MD5_CTX ctxt; 12076ca6f88SJamie Gritton struct prison *pr; 12182cd038dSYoshinobu Inoue u_int8_t digest[16]; 1224f7d1876SRobert Watson int hostnamelen; 12382cd038dSYoshinobu Inoue 12476ca6f88SJamie Gritton pr = curthread->td_ucred->cr_prison; 12576ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 126c1f19219SJamie Gritton hostnamelen = strlen(pr->pr_hostname); 127686cdd19SJun-ichiro itojun Hagino #if 0 128686cdd19SJun-ichiro itojun Hagino /* we need at least several letters as seed for ifid */ 12976ca6f88SJamie Gritton if (hostnamelen < 3) { 13076ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 131686cdd19SJun-ichiro itojun Hagino return -1; 13276ca6f88SJamie Gritton } 133686cdd19SJun-ichiro itojun Hagino #endif 134686cdd19SJun-ichiro itojun Hagino 13582cd038dSYoshinobu Inoue /* generate 8 bytes of pseudo-random value. */ 13682cd038dSYoshinobu Inoue bzero(&ctxt, sizeof(ctxt)); 13782cd038dSYoshinobu Inoue MD5Init(&ctxt); 138c1f19219SJamie Gritton MD5Update(&ctxt, pr->pr_hostname, hostnamelen); 13976ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 14082cd038dSYoshinobu Inoue MD5Final(digest, &ctxt); 14182cd038dSYoshinobu Inoue 142686cdd19SJun-ichiro itojun Hagino /* assumes sizeof(digest) > sizeof(ifid) */ 143686cdd19SJun-ichiro itojun Hagino bcopy(digest, &in6->s6_addr[8], 8); 14482cd038dSYoshinobu Inoue 14582cd038dSYoshinobu Inoue /* make sure to set "u" bit to local, and "g" bit to individual. */ 146686cdd19SJun-ichiro itojun Hagino in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 147686cdd19SJun-ichiro itojun Hagino in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 148686cdd19SJun-ichiro itojun Hagino 149686cdd19SJun-ichiro itojun Hagino /* convert EUI64 into IPv6 interface identifier */ 150686cdd19SJun-ichiro itojun Hagino EUI64_TO_IFID(in6); 15182cd038dSYoshinobu Inoue 15282cd038dSYoshinobu Inoue return 0; 15382cd038dSYoshinobu Inoue } 15482cd038dSYoshinobu Inoue 15533841545SHajimu UMEMOTO static int 1561272577eSXin LI generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret) 15733841545SHajimu UMEMOTO { 15833841545SHajimu UMEMOTO MD5_CTX ctxt; 15933841545SHajimu UMEMOTO u_int8_t seed[16], digest[16], nullbuf[8]; 16033841545SHajimu UMEMOTO u_int32_t val32; 16133841545SHajimu UMEMOTO 1621272577eSXin LI /* If there's no history, start with a random seed. */ 16333841545SHajimu UMEMOTO bzero(nullbuf, sizeof(nullbuf)); 16433841545SHajimu UMEMOTO if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) { 16533841545SHajimu UMEMOTO int i; 16633841545SHajimu UMEMOTO 16733841545SHajimu UMEMOTO for (i = 0; i < 2; i++) { 168a02e1e2bSHajimu UMEMOTO val32 = arc4random(); 16933841545SHajimu UMEMOTO bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32)); 17033841545SHajimu UMEMOTO } 1717efe5d92SHajimu UMEMOTO } else 17233841545SHajimu UMEMOTO bcopy(seed0, seed, 8); 17333841545SHajimu UMEMOTO 17433841545SHajimu UMEMOTO /* copy the right-most 64-bits of the given address */ 17533841545SHajimu UMEMOTO /* XXX assumption on the size of IFID */ 17633841545SHajimu UMEMOTO bcopy(seed1, &seed[8], 8); 17733841545SHajimu UMEMOTO 17833841545SHajimu UMEMOTO if (0) { /* for debugging purposes only */ 17933841545SHajimu UMEMOTO int i; 18033841545SHajimu UMEMOTO 18133841545SHajimu UMEMOTO printf("generate_tmp_ifid: new randomized ID from: "); 18233841545SHajimu UMEMOTO for (i = 0; i < 16; i++) 18333841545SHajimu UMEMOTO printf("%02x", seed[i]); 18433841545SHajimu UMEMOTO printf(" "); 18533841545SHajimu UMEMOTO } 18633841545SHajimu UMEMOTO 18733841545SHajimu UMEMOTO /* generate 16 bytes of pseudo-random value. */ 18833841545SHajimu UMEMOTO bzero(&ctxt, sizeof(ctxt)); 18933841545SHajimu UMEMOTO MD5Init(&ctxt); 19033841545SHajimu UMEMOTO MD5Update(&ctxt, seed, sizeof(seed)); 19133841545SHajimu UMEMOTO MD5Final(digest, &ctxt); 19233841545SHajimu UMEMOTO 19333841545SHajimu UMEMOTO /* 19433841545SHajimu UMEMOTO * RFC 3041 3.2.1. (3) 19533841545SHajimu UMEMOTO * Take the left-most 64-bits of the MD5 digest and set bit 6 (the 19633841545SHajimu UMEMOTO * left-most bit is numbered 0) to zero. 19733841545SHajimu UMEMOTO */ 19833841545SHajimu UMEMOTO bcopy(digest, ret, 8); 19933841545SHajimu UMEMOTO ret[0] &= ~EUI64_UBIT; 20033841545SHajimu UMEMOTO 20133841545SHajimu UMEMOTO /* 20233841545SHajimu UMEMOTO * XXX: we'd like to ensure that the generated value is not zero 20333841545SHajimu UMEMOTO * for simplicity. If the caclculated digest happens to be zero, 20433841545SHajimu UMEMOTO * use a random non-zero value as the last resort. 20533841545SHajimu UMEMOTO */ 20633841545SHajimu UMEMOTO if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) { 2072ce62dceSSUZUKI Shinsuke nd6log((LOG_INFO, 2082ce62dceSSUZUKI Shinsuke "generate_tmp_ifid: computed MD5 value is zero.\n")); 20933841545SHajimu UMEMOTO 210a02e1e2bSHajimu UMEMOTO val32 = arc4random(); 21133841545SHajimu UMEMOTO val32 = 1 + (val32 % (0xffffffff - 1)); 21233841545SHajimu UMEMOTO } 21333841545SHajimu UMEMOTO 21433841545SHajimu UMEMOTO /* 21533841545SHajimu UMEMOTO * RFC 3041 3.2.1. (4) 21633841545SHajimu UMEMOTO * Take the rightmost 64-bits of the MD5 digest and save them in 21733841545SHajimu UMEMOTO * stable storage as the history value to be used in the next 21833841545SHajimu UMEMOTO * iteration of the algorithm. 21933841545SHajimu UMEMOTO */ 22033841545SHajimu UMEMOTO bcopy(&digest[8], seed0, 8); 22133841545SHajimu UMEMOTO 22233841545SHajimu UMEMOTO if (0) { /* for debugging purposes only */ 22333841545SHajimu UMEMOTO int i; 22433841545SHajimu UMEMOTO 22533841545SHajimu UMEMOTO printf("to: "); 22633841545SHajimu UMEMOTO for (i = 0; i < 16; i++) 22733841545SHajimu UMEMOTO printf("%02x", digest[i]); 22833841545SHajimu UMEMOTO printf("\n"); 22933841545SHajimu UMEMOTO } 23033841545SHajimu UMEMOTO 23133841545SHajimu UMEMOTO return 0; 23233841545SHajimu UMEMOTO } 23333841545SHajimu UMEMOTO 23482cd038dSYoshinobu Inoue /* 235686cdd19SJun-ichiro itojun Hagino * Get interface identifier for the specified interface. 236686cdd19SJun-ichiro itojun Hagino * XXX assumes single sockaddr_dl (AF_LINK address) per an interface 2371272577eSXin LI * 2381272577eSXin LI * in6 - upper 64bits are preserved 23982cd038dSYoshinobu Inoue */ 240d28bde66SSUZUKI Shinsuke int 2411272577eSXin LI in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6) 24282cd038dSYoshinobu Inoue { 243686cdd19SJun-ichiro itojun Hagino struct ifaddr *ifa; 244686cdd19SJun-ichiro itojun Hagino struct sockaddr_dl *sdl; 245686cdd19SJun-ichiro itojun Hagino u_int8_t *addr; 246686cdd19SJun-ichiro itojun Hagino size_t addrlen; 247686cdd19SJun-ichiro itojun Hagino static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 248686cdd19SJun-ichiro itojun Hagino static u_int8_t allone[8] = 249686cdd19SJun-ichiro itojun Hagino { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 250686cdd19SJun-ichiro itojun Hagino 251137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 252c4dd3fe1SRobert Watson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 25382cd038dSYoshinobu Inoue if (ifa->ifa_addr->sa_family != AF_LINK) 25482cd038dSYoshinobu Inoue continue; 25582cd038dSYoshinobu Inoue sdl = (struct sockaddr_dl *)ifa->ifa_addr; 25682cd038dSYoshinobu Inoue if (sdl == NULL) 25782cd038dSYoshinobu Inoue continue; 25882cd038dSYoshinobu Inoue if (sdl->sdl_alen == 0) 25982cd038dSYoshinobu Inoue continue; 260686cdd19SJun-ichiro itojun Hagino 261686cdd19SJun-ichiro itojun Hagino goto found; 262686cdd19SJun-ichiro itojun Hagino } 263137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 264686cdd19SJun-ichiro itojun Hagino 265686cdd19SJun-ichiro itojun Hagino return -1; 266686cdd19SJun-ichiro itojun Hagino 267686cdd19SJun-ichiro itojun Hagino found: 2688c0fec80SRobert Watson IF_ADDR_LOCK_ASSERT(ifp); 269686cdd19SJun-ichiro itojun Hagino addr = LLADDR(sdl); 270686cdd19SJun-ichiro itojun Hagino addrlen = sdl->sdl_alen; 271686cdd19SJun-ichiro itojun Hagino 272686cdd19SJun-ichiro itojun Hagino /* get EUI64 */ 27382cd038dSYoshinobu Inoue switch (ifp->if_type) { 274af805644SHiroki Sato case IFT_BRIDGE: 27582cd038dSYoshinobu Inoue case IFT_ETHER: 276e9a23b55SJohn Hay case IFT_L2VLAN: 27782cd038dSYoshinobu Inoue case IFT_FDDI: 2782049fdeeSMatthew N. Dodd case IFT_ISO88025: 27982cd038dSYoshinobu Inoue case IFT_ATM: 28033841545SHajimu UMEMOTO case IFT_IEEE1394: 28182cd038dSYoshinobu Inoue /* IEEE802/EUI64 cases - what others? */ 28233841545SHajimu UMEMOTO /* IEEE1394 uses 16byte length address starting with EUI64 */ 28333841545SHajimu UMEMOTO if (addrlen > 8) 28433841545SHajimu UMEMOTO addrlen = 8; 285686cdd19SJun-ichiro itojun Hagino 286686cdd19SJun-ichiro itojun Hagino /* look at IEEE802/EUI64 only */ 2871e6a4139SRobert Watson if (addrlen != 8 && addrlen != 6) { 288137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 289686cdd19SJun-ichiro itojun Hagino return -1; 2901e6a4139SRobert Watson } 291686cdd19SJun-ichiro itojun Hagino 29282cd038dSYoshinobu Inoue /* 293686cdd19SJun-ichiro itojun Hagino * check for invalid MAC address - on bsdi, we see it a lot 294686cdd19SJun-ichiro itojun Hagino * since wildboar configures all-zero MAC on pccard before 295686cdd19SJun-ichiro itojun Hagino * card insertion. 29682cd038dSYoshinobu Inoue */ 2971e6a4139SRobert Watson if (bcmp(addr, allzero, addrlen) == 0) { 298137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 299686cdd19SJun-ichiro itojun Hagino return -1; 3001e6a4139SRobert Watson } 3011e6a4139SRobert Watson if (bcmp(addr, allone, addrlen) == 0) { 302137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 303686cdd19SJun-ichiro itojun Hagino return -1; 3041e6a4139SRobert Watson } 305686cdd19SJun-ichiro itojun Hagino 306686cdd19SJun-ichiro itojun Hagino /* make EUI64 address */ 307686cdd19SJun-ichiro itojun Hagino if (addrlen == 8) 308686cdd19SJun-ichiro itojun Hagino bcopy(addr, &in6->s6_addr[8], 8); 309686cdd19SJun-ichiro itojun Hagino else if (addrlen == 6) { 310686cdd19SJun-ichiro itojun Hagino in6->s6_addr[8] = addr[0]; 311686cdd19SJun-ichiro itojun Hagino in6->s6_addr[9] = addr[1]; 312686cdd19SJun-ichiro itojun Hagino in6->s6_addr[10] = addr[2]; 313686cdd19SJun-ichiro itojun Hagino in6->s6_addr[11] = 0xff; 314686cdd19SJun-ichiro itojun Hagino in6->s6_addr[12] = 0xfe; 315686cdd19SJun-ichiro itojun Hagino in6->s6_addr[13] = addr[3]; 316686cdd19SJun-ichiro itojun Hagino in6->s6_addr[14] = addr[4]; 317686cdd19SJun-ichiro itojun Hagino in6->s6_addr[15] = addr[5]; 318686cdd19SJun-ichiro itojun Hagino } 31982cd038dSYoshinobu Inoue break; 320686cdd19SJun-ichiro itojun Hagino 32182cd038dSYoshinobu Inoue case IFT_ARCNET: 3221e6a4139SRobert Watson if (addrlen != 1) { 323137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 324686cdd19SJun-ichiro itojun Hagino return -1; 3251e6a4139SRobert Watson } 3261e6a4139SRobert Watson if (!addr[0]) { 327137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 328686cdd19SJun-ichiro itojun Hagino return -1; 3291e6a4139SRobert Watson } 330686cdd19SJun-ichiro itojun Hagino 331686cdd19SJun-ichiro itojun Hagino bzero(&in6->s6_addr[8], 8); 332686cdd19SJun-ichiro itojun Hagino in6->s6_addr[15] = addr[0]; 333686cdd19SJun-ichiro itojun Hagino 33482cd038dSYoshinobu Inoue /* 335686cdd19SJun-ichiro itojun Hagino * due to insufficient bitwidth, we mark it local. 33682cd038dSYoshinobu Inoue */ 337686cdd19SJun-ichiro itojun Hagino in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */ 338686cdd19SJun-ichiro itojun Hagino in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */ 33982cd038dSYoshinobu Inoue break; 340686cdd19SJun-ichiro itojun Hagino 341686cdd19SJun-ichiro itojun Hagino case IFT_GIF: 342686cdd19SJun-ichiro itojun Hagino case IFT_STF: 343686cdd19SJun-ichiro itojun Hagino /* 34433841545SHajimu UMEMOTO * RFC2893 says: "SHOULD use IPv4 address as ifid source". 345686cdd19SJun-ichiro itojun Hagino * however, IPv4 address is not very suitable as unique 346686cdd19SJun-ichiro itojun Hagino * identifier source (can be renumbered). 347686cdd19SJun-ichiro itojun Hagino * we don't do this. 348686cdd19SJun-ichiro itojun Hagino */ 349137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 350686cdd19SJun-ichiro itojun Hagino return -1; 35182cd038dSYoshinobu Inoue 352686cdd19SJun-ichiro itojun Hagino default: 353137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 354686cdd19SJun-ichiro itojun Hagino return -1; 355686cdd19SJun-ichiro itojun Hagino } 35682cd038dSYoshinobu Inoue 357686cdd19SJun-ichiro itojun Hagino /* sanity check: g bit must not indicate "group" */ 3581e6a4139SRobert Watson if (EUI64_GROUP(in6)) { 359137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 360686cdd19SJun-ichiro itojun Hagino return -1; 3611e6a4139SRobert Watson } 36282cd038dSYoshinobu Inoue 363686cdd19SJun-ichiro itojun Hagino /* convert EUI64 into IPv6 interface identifier */ 364686cdd19SJun-ichiro itojun Hagino EUI64_TO_IFID(in6); 365686cdd19SJun-ichiro itojun Hagino 366686cdd19SJun-ichiro itojun Hagino /* 367686cdd19SJun-ichiro itojun Hagino * sanity check: ifid must not be all zero, avoid conflict with 368686cdd19SJun-ichiro itojun Hagino * subnet router anycast 369686cdd19SJun-ichiro itojun Hagino */ 370686cdd19SJun-ichiro itojun Hagino if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 && 371686cdd19SJun-ichiro itojun Hagino bcmp(&in6->s6_addr[9], allzero, 7) == 0) { 372137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 373686cdd19SJun-ichiro itojun Hagino return -1; 374686cdd19SJun-ichiro itojun Hagino } 37582cd038dSYoshinobu Inoue 376137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 37782cd038dSYoshinobu Inoue return 0; 378686cdd19SJun-ichiro itojun Hagino } 379686cdd19SJun-ichiro itojun Hagino 380686cdd19SJun-ichiro itojun Hagino /* 381686cdd19SJun-ichiro itojun Hagino * Get interface identifier for the specified interface. If it is not 382686cdd19SJun-ichiro itojun Hagino * available on ifp0, borrow interface identifier from other information 383686cdd19SJun-ichiro itojun Hagino * sources. 3841272577eSXin LI * 3851272577eSXin LI * altifp - secondary EUI64 source 386686cdd19SJun-ichiro itojun Hagino */ 387686cdd19SJun-ichiro itojun Hagino static int 3881272577eSXin LI get_ifid(struct ifnet *ifp0, struct ifnet *altifp, 3891272577eSXin LI struct in6_addr *in6) 390686cdd19SJun-ichiro itojun Hagino { 391686cdd19SJun-ichiro itojun Hagino struct ifnet *ifp; 392686cdd19SJun-ichiro itojun Hagino 393686cdd19SJun-ichiro itojun Hagino /* first, try to get it from the interface itself */ 394d28bde66SSUZUKI Shinsuke if (in6_get_hw_ifid(ifp0, in6) == 0) { 39533841545SHajimu UMEMOTO nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n", 39633841545SHajimu UMEMOTO if_name(ifp0))); 397686cdd19SJun-ichiro itojun Hagino goto success; 398686cdd19SJun-ichiro itojun Hagino } 399686cdd19SJun-ichiro itojun Hagino 400686cdd19SJun-ichiro itojun Hagino /* try secondary EUI64 source. this basically is for ATM PVC */ 401d28bde66SSUZUKI Shinsuke if (altifp && in6_get_hw_ifid(altifp, in6) == 0) { 40233841545SHajimu UMEMOTO nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n", 40333841545SHajimu UMEMOTO if_name(ifp0), if_name(altifp))); 404686cdd19SJun-ichiro itojun Hagino goto success; 405686cdd19SJun-ichiro itojun Hagino } 406686cdd19SJun-ichiro itojun Hagino 407686cdd19SJun-ichiro itojun Hagino /* next, try to get it from some other hardware interface */ 40877dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 409e6abaf91SGleb Smirnoff TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 410686cdd19SJun-ichiro itojun Hagino if (ifp == ifp0) 411686cdd19SJun-ichiro itojun Hagino continue; 412d28bde66SSUZUKI Shinsuke if (in6_get_hw_ifid(ifp, in6) != 0) 413686cdd19SJun-ichiro itojun Hagino continue; 414686cdd19SJun-ichiro itojun Hagino 415686cdd19SJun-ichiro itojun Hagino /* 416686cdd19SJun-ichiro itojun Hagino * to borrow ifid from other interface, ifid needs to be 417686cdd19SJun-ichiro itojun Hagino * globally unique 418686cdd19SJun-ichiro itojun Hagino */ 419686cdd19SJun-ichiro itojun Hagino if (IFID_UNIVERSAL(in6)) { 42033841545SHajimu UMEMOTO nd6log((LOG_DEBUG, 42133841545SHajimu UMEMOTO "%s: borrow interface identifier from %s\n", 42233841545SHajimu UMEMOTO if_name(ifp0), if_name(ifp))); 42377dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 424686cdd19SJun-ichiro itojun Hagino goto success; 425686cdd19SJun-ichiro itojun Hagino } 426686cdd19SJun-ichiro itojun Hagino } 42777dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 428686cdd19SJun-ichiro itojun Hagino 429686cdd19SJun-ichiro itojun Hagino /* last resort: get from random number source */ 430686cdd19SJun-ichiro itojun Hagino if (get_rand_ifid(ifp, in6) == 0) { 43133841545SHajimu UMEMOTO nd6log((LOG_DEBUG, 43233841545SHajimu UMEMOTO "%s: interface identifier generated by random number\n", 43333841545SHajimu UMEMOTO if_name(ifp0))); 434686cdd19SJun-ichiro itojun Hagino goto success; 435686cdd19SJun-ichiro itojun Hagino } 436686cdd19SJun-ichiro itojun Hagino 437de9c893eSJun-ichiro itojun Hagino printf("%s: failed to get interface identifier\n", if_name(ifp0)); 438686cdd19SJun-ichiro itojun Hagino return -1; 439686cdd19SJun-ichiro itojun Hagino 440686cdd19SJun-ichiro itojun Hagino success: 4417efe5d92SHajimu UMEMOTO nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", 4427efe5d92SHajimu UMEMOTO if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10], 4437efe5d92SHajimu UMEMOTO in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13], 44433841545SHajimu UMEMOTO in6->s6_addr[14], in6->s6_addr[15])); 445686cdd19SJun-ichiro itojun Hagino return 0; 446686cdd19SJun-ichiro itojun Hagino } 447686cdd19SJun-ichiro itojun Hagino 4481272577eSXin LI /* 4491272577eSXin LI * altifp - secondary EUI64 source 4501272577eSXin LI */ 451686cdd19SJun-ichiro itojun Hagino static int 4521272577eSXin LI in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp) 453686cdd19SJun-ichiro itojun Hagino { 454686cdd19SJun-ichiro itojun Hagino struct in6_ifaddr *ia; 45533841545SHajimu UMEMOTO struct in6_aliasreq ifra; 456743eee66SSUZUKI Shinsuke struct nd_prefixctl pr0; 457d748f7efSMark Johnston struct nd_prefix *pr; 4581dc8f6a8SAlexander V. Chernikov int error; 459686cdd19SJun-ichiro itojun Hagino 460686cdd19SJun-ichiro itojun Hagino /* 46133841545SHajimu UMEMOTO * configure link-local address. 462686cdd19SJun-ichiro itojun Hagino */ 4639080e7d0SAlexander V. Chernikov in6_prepare_ifra(&ifra, NULL, &in6mask64); 464686cdd19SJun-ichiro itojun Hagino 465a1f7e5f8SHajimu UMEMOTO ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000); 46633841545SHajimu UMEMOTO ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0; 46733841545SHajimu UMEMOTO if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 46833841545SHajimu UMEMOTO ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0; 46933841545SHajimu UMEMOTO ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1); 47033841545SHajimu UMEMOTO } else { 47133841545SHajimu UMEMOTO if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) { 47233841545SHajimu UMEMOTO nd6log((LOG_ERR, 47333841545SHajimu UMEMOTO "%s: no ifid available\n", if_name(ifp))); 4747efe5d92SHajimu UMEMOTO return (-1); 47533841545SHajimu UMEMOTO } 47633841545SHajimu UMEMOTO } 477a1f7e5f8SHajimu UMEMOTO if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL)) 478a1f7e5f8SHajimu UMEMOTO return (-1); 47933841545SHajimu UMEMOTO 48033841545SHajimu UMEMOTO /* link-local addresses should NEVER expire. */ 48133841545SHajimu UMEMOTO ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 48233841545SHajimu UMEMOTO ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 483686cdd19SJun-ichiro itojun Hagino 48433841545SHajimu UMEMOTO /* 48533841545SHajimu UMEMOTO * Now call in6_update_ifa() to do a bunch of procedures to configure 4862ce62dceSSUZUKI Shinsuke * a link-local address. We can set the 3rd argument to NULL, because 48788ff5695SSUZUKI Shinsuke * we know there's no other link-local address on the interface 48888ff5695SSUZUKI Shinsuke * and therefore we are adding one (instead of updating one). 48933841545SHajimu UMEMOTO */ 490743eee66SSUZUKI Shinsuke if ((error = in6_update_ifa(ifp, &ifra, NULL, 491743eee66SSUZUKI Shinsuke IN6_IFAUPDATE_DADDELAY)) != 0) { 49233841545SHajimu UMEMOTO /* 49333841545SHajimu UMEMOTO * XXX: When the interface does not support IPv6, this call 49433841545SHajimu UMEMOTO * would fail in the SIOCSIFADDR ioctl. I believe the 49533841545SHajimu UMEMOTO * notification is rather confusing in this case, so just 4967efe5d92SHajimu UMEMOTO * suppress it. (jinmei@kame.net 20010130) 49733841545SHajimu UMEMOTO */ 49833841545SHajimu UMEMOTO if (error != EAFNOSUPPORT) 4992ce62dceSSUZUKI Shinsuke nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to " 50033841545SHajimu UMEMOTO "configure a link-local address on %s " 50133841545SHajimu UMEMOTO "(errno=%d)\n", 5022ce62dceSSUZUKI Shinsuke if_name(ifp), error)); 50333841545SHajimu UMEMOTO return (-1); 504686cdd19SJun-ichiro itojun Hagino } 50533841545SHajimu UMEMOTO 50633841545SHajimu UMEMOTO ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */ 507f67e13d6SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia == NULL, ifp=%p", __func__, ifp)); 508f67e13d6SBjoern A. Zeeb 5098c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 510686cdd19SJun-ichiro itojun Hagino 51133841545SHajimu UMEMOTO /* 5127efe5d92SHajimu UMEMOTO * Make the link-local prefix (fe80::%link/64) as on-link. 51333841545SHajimu UMEMOTO * Since we'd like to manage prefixes separately from addresses, 51433841545SHajimu UMEMOTO * we make an ND6 prefix structure for the link-local prefix, 51533841545SHajimu UMEMOTO * and add it to the prefix list as a never-expire prefix. 51633841545SHajimu UMEMOTO * XXX: this change might affect some existing code base... 51733841545SHajimu UMEMOTO */ 51833841545SHajimu UMEMOTO bzero(&pr0, sizeof(pr0)); 51933841545SHajimu UMEMOTO pr0.ndpr_ifp = ifp; 52033841545SHajimu UMEMOTO /* this should be 64 at this moment. */ 52133841545SHajimu UMEMOTO pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL); 52233841545SHajimu UMEMOTO pr0.ndpr_prefix = ifra.ifra_addr; 52333841545SHajimu UMEMOTO /* apply the mask for safety. (nd6_prelist_add will apply it again) */ 5241dc8f6a8SAlexander V. Chernikov IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, &in6mask64); 52533841545SHajimu UMEMOTO /* 52633841545SHajimu UMEMOTO * Initialize parameters. The link-local prefix must always be 52733841545SHajimu UMEMOTO * on-link, and its lifetimes never expire. 52833841545SHajimu UMEMOTO */ 52933841545SHajimu UMEMOTO pr0.ndpr_raf_onlink = 1; 53033841545SHajimu UMEMOTO pr0.ndpr_raf_auto = 1; /* probably meaningless */ 53133841545SHajimu UMEMOTO pr0.ndpr_vltime = ND6_INFINITE_LIFETIME; 53233841545SHajimu UMEMOTO pr0.ndpr_pltime = ND6_INFINITE_LIFETIME; 53333841545SHajimu UMEMOTO /* 53433841545SHajimu UMEMOTO * Since there is no other link-local addresses, nd6_prefix_lookup() 53533841545SHajimu UMEMOTO * probably returns NULL. However, we cannot always expect the result. 53633841545SHajimu UMEMOTO * For example, if we first remove the (only) existing link-local 53733841545SHajimu UMEMOTO * address, and then reconfigure another one, the prefix is still 53833841545SHajimu UMEMOTO * valid with referring to the old link-local address. 53933841545SHajimu UMEMOTO */ 540d748f7efSMark Johnston if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 54133841545SHajimu UMEMOTO if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0) 54233841545SHajimu UMEMOTO return (error); 543d748f7efSMark Johnston } else 544d748f7efSMark Johnston nd6_prefix_rele(pr); 545686cdd19SJun-ichiro itojun Hagino 546686cdd19SJun-ichiro itojun Hagino return 0; 547686cdd19SJun-ichiro itojun Hagino } 548686cdd19SJun-ichiro itojun Hagino 5491272577eSXin LI /* 5501272577eSXin LI * ifp - must be IFT_LOOP 5511272577eSXin LI */ 552686cdd19SJun-ichiro itojun Hagino static int 5531272577eSXin LI in6_ifattach_loopback(struct ifnet *ifp) 554686cdd19SJun-ichiro itojun Hagino { 55533841545SHajimu UMEMOTO struct in6_aliasreq ifra; 55633841545SHajimu UMEMOTO int error; 55733841545SHajimu UMEMOTO 5589080e7d0SAlexander V. Chernikov in6_prepare_ifra(&ifra, &in6addr_loopback, &in6mask128); 559686cdd19SJun-ichiro itojun Hagino 560686cdd19SJun-ichiro itojun Hagino /* 561686cdd19SJun-ichiro itojun Hagino * Always initialize ia_dstaddr (= broadcast address) to loopback 56233841545SHajimu UMEMOTO * address. Follows IPv4 practice - see in_ifinit(). 563686cdd19SJun-ichiro itojun Hagino */ 56433841545SHajimu UMEMOTO ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6); 56533841545SHajimu UMEMOTO ifra.ifra_dstaddr.sin6_family = AF_INET6; 56633841545SHajimu UMEMOTO ifra.ifra_dstaddr.sin6_addr = in6addr_loopback; 567686cdd19SJun-ichiro itojun Hagino 56833841545SHajimu UMEMOTO /* the loopback address should NEVER expire. */ 56933841545SHajimu UMEMOTO ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 57033841545SHajimu UMEMOTO ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 571686cdd19SJun-ichiro itojun Hagino 57233841545SHajimu UMEMOTO /* 57388ff5695SSUZUKI Shinsuke * We are sure that this is a newly assigned address, so we can set 57488ff5695SSUZUKI Shinsuke * NULL to the 3rd arg. 57533841545SHajimu UMEMOTO */ 576743eee66SSUZUKI Shinsuke if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) { 5772ce62dceSSUZUKI Shinsuke nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure " 57833841545SHajimu UMEMOTO "the loopback address on %s (errno=%d)\n", 5792ce62dceSSUZUKI Shinsuke if_name(ifp), error)); 58033841545SHajimu UMEMOTO return (-1); 581686cdd19SJun-ichiro itojun Hagino } 582686cdd19SJun-ichiro itojun Hagino 583686cdd19SJun-ichiro itojun Hagino return 0; 584686cdd19SJun-ichiro itojun Hagino } 585686cdd19SJun-ichiro itojun Hagino 586686cdd19SJun-ichiro itojun Hagino /* 587686cdd19SJun-ichiro itojun Hagino * compute NI group address, based on the current hostname setting. 5885df1b6b5SHiroki Sato * see RFC 4620. 589686cdd19SJun-ichiro itojun Hagino * 590686cdd19SJun-ichiro itojun Hagino * when ifp == NULL, the caller is responsible for filling scopeid. 5915df1b6b5SHiroki Sato * 5925df1b6b5SHiroki Sato * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address 5935df1b6b5SHiroki Sato * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620. 594686cdd19SJun-ichiro itojun Hagino */ 5955df1b6b5SHiroki Sato static int 5965df1b6b5SHiroki Sato in6_nigroup0(struct ifnet *ifp, const char *name, int namelen, 5975df1b6b5SHiroki Sato struct in6_addr *in6, int oldmcprefix) 598686cdd19SJun-ichiro itojun Hagino { 59976ca6f88SJamie Gritton struct prison *pr; 600686cdd19SJun-ichiro itojun Hagino const char *p; 60133841545SHajimu UMEMOTO u_char *q; 602686cdd19SJun-ichiro itojun Hagino MD5_CTX ctxt; 603686cdd19SJun-ichiro itojun Hagino u_int8_t digest[16]; 604686cdd19SJun-ichiro itojun Hagino char l; 60533841545SHajimu UMEMOTO char n[64]; /* a single label must not exceed 63 chars */ 606686cdd19SJun-ichiro itojun Hagino 6070304c731SJamie Gritton /* 6080304c731SJamie Gritton * If no name is given and namelen is -1, 6090304c731SJamie Gritton * we try to do the hostname lookup ourselves. 6100304c731SJamie Gritton */ 6110304c731SJamie Gritton if (!name && namelen == -1) { 61276ca6f88SJamie Gritton pr = curthread->td_ucred->cr_prison; 61376ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 614c1f19219SJamie Gritton name = pr->pr_hostname; 6150304c731SJamie Gritton namelen = strlen(name); 6160304c731SJamie Gritton } else 61776ca6f88SJamie Gritton pr = NULL; 6180304c731SJamie Gritton if (!name || !namelen) { 61976ca6f88SJamie Gritton if (pr != NULL) 62076ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 621686cdd19SJun-ichiro itojun Hagino return -1; 6220304c731SJamie Gritton } 623686cdd19SJun-ichiro itojun Hagino 624686cdd19SJun-ichiro itojun Hagino p = name; 625686cdd19SJun-ichiro itojun Hagino while (p && *p && *p != '.' && p - name < namelen) 626686cdd19SJun-ichiro itojun Hagino p++; 6270304c731SJamie Gritton if (p == name || p - name > sizeof(n) - 1) { 62876ca6f88SJamie Gritton if (pr != NULL) 62976ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 630686cdd19SJun-ichiro itojun Hagino return -1; /* label too long */ 6310304c731SJamie Gritton } 632686cdd19SJun-ichiro itojun Hagino l = p - name; 63333841545SHajimu UMEMOTO strncpy(n, name, l); 63476ca6f88SJamie Gritton if (pr != NULL) 63576ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 63633841545SHajimu UMEMOTO n[(int)l] = '\0'; 63733841545SHajimu UMEMOTO for (q = n; *q; q++) { 63833841545SHajimu UMEMOTO if ('A' <= *q && *q <= 'Z') 63933841545SHajimu UMEMOTO *q = *q - 'A' + 'a'; 64033841545SHajimu UMEMOTO } 641686cdd19SJun-ichiro itojun Hagino 6425df1b6b5SHiroki Sato /* generate 16 bytes of pseudo-random value. */ 643686cdd19SJun-ichiro itojun Hagino bzero(&ctxt, sizeof(ctxt)); 644686cdd19SJun-ichiro itojun Hagino MD5Init(&ctxt); 645686cdd19SJun-ichiro itojun Hagino MD5Update(&ctxt, &l, sizeof(l)); 64633841545SHajimu UMEMOTO MD5Update(&ctxt, n, l); 647686cdd19SJun-ichiro itojun Hagino MD5Final(digest, &ctxt); 648686cdd19SJun-ichiro itojun Hagino 649686cdd19SJun-ichiro itojun Hagino bzero(in6, sizeof(*in6)); 6502ce62dceSSUZUKI Shinsuke in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL; 651686cdd19SJun-ichiro itojun Hagino in6->s6_addr8[11] = 2; 6525df1b6b5SHiroki Sato if (oldmcprefix == 0) { 6535df1b6b5SHiroki Sato in6->s6_addr8[12] = 0xff; 6545df1b6b5SHiroki Sato /* Copy the first 24 bits of 128-bit hash into the address. */ 6555df1b6b5SHiroki Sato bcopy(digest, &in6->s6_addr8[13], 3); 6565df1b6b5SHiroki Sato } else { 6575df1b6b5SHiroki Sato /* Copy the first 32 bits of 128-bit hash into the address. */ 658686cdd19SJun-ichiro itojun Hagino bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3])); 6595df1b6b5SHiroki Sato } 660a1f7e5f8SHajimu UMEMOTO if (in6_setscope(in6, ifp, NULL)) 661a1f7e5f8SHajimu UMEMOTO return (-1); /* XXX: should not fail */ 662686cdd19SJun-ichiro itojun Hagino 663686cdd19SJun-ichiro itojun Hagino return 0; 664686cdd19SJun-ichiro itojun Hagino } 665686cdd19SJun-ichiro itojun Hagino 6665df1b6b5SHiroki Sato int 6675df1b6b5SHiroki Sato in6_nigroup(struct ifnet *ifp, const char *name, int namelen, 6685df1b6b5SHiroki Sato struct in6_addr *in6) 6695df1b6b5SHiroki Sato { 6705df1b6b5SHiroki Sato 6715df1b6b5SHiroki Sato return (in6_nigroup0(ifp, name, namelen, in6, 0)); 6725df1b6b5SHiroki Sato } 6735df1b6b5SHiroki Sato 6745df1b6b5SHiroki Sato int 6755df1b6b5SHiroki Sato in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen, 6765df1b6b5SHiroki Sato struct in6_addr *in6) 6775df1b6b5SHiroki Sato { 6785df1b6b5SHiroki Sato 6795df1b6b5SHiroki Sato return (in6_nigroup0(ifp, name, namelen, in6, 1)); 6805df1b6b5SHiroki Sato } 6815df1b6b5SHiroki Sato 68282cd038dSYoshinobu Inoue /* 683686cdd19SJun-ichiro itojun Hagino * XXX multiple loopback interface needs more care. for instance, 684686cdd19SJun-ichiro itojun Hagino * nodelocal address needs to be configured onto only one of them. 685686cdd19SJun-ichiro itojun Hagino * XXX multiple link-local address case 6861272577eSXin LI * 6871272577eSXin LI * altifp - secondary EUI64 source 68882cd038dSYoshinobu Inoue */ 68982cd038dSYoshinobu Inoue void 6901272577eSXin LI in6_ifattach(struct ifnet *ifp, struct ifnet *altifp) 69182cd038dSYoshinobu Inoue { 692686cdd19SJun-ichiro itojun Hagino struct in6_ifaddr *ia; 693686cdd19SJun-ichiro itojun Hagino struct in6_addr in6; 69433841545SHajimu UMEMOTO 6950de0dd9bSHiroki Sato if (ifp->if_afdata[AF_INET6] == NULL) 696cc5934f5SMax Laier return; 69782cd038dSYoshinobu Inoue /* 698686cdd19SJun-ichiro itojun Hagino * quirks based on interface type 69982cd038dSYoshinobu Inoue */ 700686cdd19SJun-ichiro itojun Hagino switch (ifp->if_type) { 701686cdd19SJun-ichiro itojun Hagino case IFT_STF: 70282cd038dSYoshinobu Inoue /* 70388ff5695SSUZUKI Shinsuke * 6to4 interface is a very special kind of beast. 70488ff5695SSUZUKI Shinsuke * no multicast, no linklocal. RFC2529 specifies how to make 70588ff5695SSUZUKI Shinsuke * linklocals for 6to4 interface, but there's no use and 70688ff5695SSUZUKI Shinsuke * it is rather harmful to have one. 70782cd038dSYoshinobu Inoue */ 708af805644SHiroki Sato ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL; 709af805644SHiroki Sato break; 71082cd038dSYoshinobu Inoue default: 71182cd038dSYoshinobu Inoue break; 71282cd038dSYoshinobu Inoue } 71382cd038dSYoshinobu Inoue 714686cdd19SJun-ichiro itojun Hagino /* 715686cdd19SJun-ichiro itojun Hagino * usually, we require multicast capability to the interface 716686cdd19SJun-ichiro itojun Hagino */ 717686cdd19SJun-ichiro itojun Hagino if ((ifp->if_flags & IFF_MULTICAST) == 0) { 7182ce62dceSSUZUKI Shinsuke nd6log((LOG_INFO, "in6_ifattach: " 71933841545SHajimu UMEMOTO "%s is not multicast capable, IPv6 not enabled\n", 7202ce62dceSSUZUKI Shinsuke if_name(ifp))); 72182cd038dSYoshinobu Inoue return; 72282cd038dSYoshinobu Inoue } 723686cdd19SJun-ichiro itojun Hagino 724686cdd19SJun-ichiro itojun Hagino /* 72533841545SHajimu UMEMOTO * assign loopback address for loopback interface. 72633841545SHajimu UMEMOTO * XXX multiple loopback interface case. 727686cdd19SJun-ichiro itojun Hagino */ 72833841545SHajimu UMEMOTO if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 7298c0fec80SRobert Watson struct ifaddr *ifa; 7308c0fec80SRobert Watson 731686cdd19SJun-ichiro itojun Hagino in6 = in6addr_loopback; 7328c0fec80SRobert Watson ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &in6); 7338c0fec80SRobert Watson if (ifa == NULL) { 734686cdd19SJun-ichiro itojun Hagino if (in6_ifattach_loopback(ifp) != 0) 735686cdd19SJun-ichiro itojun Hagino return; 7368c0fec80SRobert Watson } else 7378c0fec80SRobert Watson ifa_free(ifa); 738686cdd19SJun-ichiro itojun Hagino } 739686cdd19SJun-ichiro itojun Hagino 74033841545SHajimu UMEMOTO /* 74133841545SHajimu UMEMOTO * assign a link-local address, if there's none. 74233841545SHajimu UMEMOTO */ 743af805644SHiroki Sato if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 744a283298cSHiroki Sato ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) { 745a283298cSHiroki Sato int error; 746a283298cSHiroki Sato 74733841545SHajimu UMEMOTO ia = in6ifa_ifpforlinklocal(ifp, 0); 74833841545SHajimu UMEMOTO if (ia == NULL) { 749a283298cSHiroki Sato error = in6_ifattach_linklocal(ifp, altifp); 75027f13d5dSHiroki Sato #if 0 751a283298cSHiroki Sato if (error) 752a283298cSHiroki Sato log(LOG_NOTICE, "in6_ifattach_linklocal: " 753a283298cSHiroki Sato "failed to add a link-local addr to %s\n", 754a283298cSHiroki Sato if_name(ifp)); 75527f13d5dSHiroki Sato #endif 7568c0fec80SRobert Watson } else 7578c0fec80SRobert Watson ifa_free(&ia->ia_ifa); 75833841545SHajimu UMEMOTO } 75933841545SHajimu UMEMOTO 76082cd038dSYoshinobu Inoue /* update dynamically. */ 761603724d3SBjoern A. Zeeb if (V_in6_maxmtu < ifp->if_mtu) 762603724d3SBjoern A. Zeeb V_in6_maxmtu = ifp->if_mtu; 76382cd038dSYoshinobu Inoue } 76482cd038dSYoshinobu Inoue 765686cdd19SJun-ichiro itojun Hagino /* 766686cdd19SJun-ichiro itojun Hagino * NOTE: in6_ifdetach() does not support loopback if at this moment. 76789856f7eSBjoern A. Zeeb * 76889856f7eSBjoern A. Zeeb * When shutting down a VNET we clean up layers top-down. In that case 76989856f7eSBjoern A. Zeeb * upper layer protocols (ulp) are cleaned up already and locks are destroyed 77089856f7eSBjoern A. Zeeb * and we must not call into these cleanup functions anymore, thus purgeulp 77189856f7eSBjoern A. Zeeb * is set to 0 in that case by in6_ifdetach_destroy(). 77289856f7eSBjoern A. Zeeb * The normal case of destroying a (cloned) interface still needs to cleanup 77389856f7eSBjoern A. Zeeb * everything related to the interface and will have purgeulp set to 1. 774686cdd19SJun-ichiro itojun Hagino */ 77589856f7eSBjoern A. Zeeb static void 77689856f7eSBjoern A. Zeeb _in6_ifdetach(struct ifnet *ifp, int purgeulp) 77782cd038dSYoshinobu Inoue { 778686cdd19SJun-ichiro itojun Hagino struct ifaddr *ifa, *next; 77982cd038dSYoshinobu Inoue 7806378e1f3SHiroki Sato if (ifp->if_afdata[AF_INET6] == NULL) 7816378e1f3SHiroki Sato return; 7826378e1f3SHiroki Sato 78389856f7eSBjoern A. Zeeb /* 78494a43496SAndrey V. Elsukov * nuke any of IPv6 addresses we have 78594a43496SAndrey V. Elsukov * XXX: all addresses should be already removed 78694a43496SAndrey V. Elsukov */ 787c4dd3fe1SRobert Watson TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 788686cdd19SJun-ichiro itojun Hagino if (ifa->ifa_addr->sa_family != AF_INET6) 789686cdd19SJun-ichiro itojun Hagino continue; 79033841545SHajimu UMEMOTO in6_purgeaddr(ifa); 791686cdd19SJun-ichiro itojun Hagino } 79289856f7eSBjoern A. Zeeb if (purgeulp) { 793603724d3SBjoern A. Zeeb in6_pcbpurgeif0(&V_udbinfo, ifp); 794e06e816fSKevin Lo in6_pcbpurgeif0(&V_ulitecbinfo, ifp); 795603724d3SBjoern A. Zeeb in6_pcbpurgeif0(&V_ripcbinfo, ifp); 79689856f7eSBjoern A. Zeeb } 79771498f30SBruce M Simpson /* leave from all multicast groups joined */ 79871498f30SBruce M Simpson in6_purgemaddrs(ifp); 799686cdd19SJun-ichiro itojun Hagino 80033841545SHajimu UMEMOTO /* 801d748f7efSMark Johnston * Remove neighbor management table. 802d748f7efSMark Johnston * Enabling the nd6_purge will panic on vmove for interfaces on VNET 803d748f7efSMark Johnston * teardown as the IPv6 layer is cleaned up already and the locks 804d748f7efSMark Johnston * are destroyed. 80533841545SHajimu UMEMOTO */ 80689856f7eSBjoern A. Zeeb if (purgeulp) 807686cdd19SJun-ichiro itojun Hagino nd6_purge(ifp); 8083f02eaf3SDon Lewis } 80933841545SHajimu UMEMOTO 81089856f7eSBjoern A. Zeeb void 81189856f7eSBjoern A. Zeeb in6_ifdetach(struct ifnet *ifp) 81289856f7eSBjoern A. Zeeb { 81389856f7eSBjoern A. Zeeb 81489856f7eSBjoern A. Zeeb _in6_ifdetach(ifp, 1); 81589856f7eSBjoern A. Zeeb } 81689856f7eSBjoern A. Zeeb 81789856f7eSBjoern A. Zeeb void 81889856f7eSBjoern A. Zeeb in6_ifdetach_destroy(struct ifnet *ifp) 81989856f7eSBjoern A. Zeeb { 82089856f7eSBjoern A. Zeeb 82189856f7eSBjoern A. Zeeb _in6_ifdetach(ifp, 0); 82289856f7eSBjoern A. Zeeb } 82389856f7eSBjoern A. Zeeb 824743eee66SSUZUKI Shinsuke int 8251272577eSXin LI in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf, 8261272577eSXin LI const u_int8_t *baseid, int generate) 82733841545SHajimu UMEMOTO { 82833841545SHajimu UMEMOTO u_int8_t nullbuf[8]; 82931b1bfe1SHajimu UMEMOTO struct nd_ifinfo *ndi = ND_IFINFO(ifp); 83033841545SHajimu UMEMOTO 83133841545SHajimu UMEMOTO bzero(nullbuf, sizeof(nullbuf)); 83233841545SHajimu UMEMOTO if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) { 83333841545SHajimu UMEMOTO /* we've never created a random ID. Create a new one. */ 83433841545SHajimu UMEMOTO generate = 1; 83533841545SHajimu UMEMOTO } 83633841545SHajimu UMEMOTO 83733841545SHajimu UMEMOTO if (generate) { 83833841545SHajimu UMEMOTO bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1)); 83933841545SHajimu UMEMOTO 84033841545SHajimu UMEMOTO /* generate_tmp_ifid will update seedn and buf */ 84133841545SHajimu UMEMOTO (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1, 84233841545SHajimu UMEMOTO ndi->randomid); 84333841545SHajimu UMEMOTO } 84433841545SHajimu UMEMOTO bcopy(ndi->randomid, retbuf, 8); 845743eee66SSUZUKI Shinsuke 846743eee66SSUZUKI Shinsuke return (0); 84733841545SHajimu UMEMOTO } 84833841545SHajimu UMEMOTO 84933841545SHajimu UMEMOTO void 850f6dfe47aSMarko Zec in6_tmpaddrtimer(void *arg) 85133841545SHajimu UMEMOTO { 852f6dfe47aSMarko Zec CURVNET_SET((struct vnet *) arg); 85333841545SHajimu UMEMOTO struct nd_ifinfo *ndi; 85433841545SHajimu UMEMOTO u_int8_t nullbuf[8]; 85531b1bfe1SHajimu UMEMOTO struct ifnet *ifp; 85633841545SHajimu UMEMOTO 857603724d3SBjoern A. Zeeb callout_reset(&V_in6_tmpaddrtimer_ch, 858603724d3SBjoern A. Zeeb (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor - 859f6dfe47aSMarko Zec V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet); 86033841545SHajimu UMEMOTO 86133841545SHajimu UMEMOTO bzero(nullbuf, sizeof(nullbuf)); 862e6abaf91SGleb Smirnoff TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 86341541ebfSHiroki Sato if (ifp->if_afdata[AF_INET6] == NULL) 86441541ebfSHiroki Sato continue; 86531b1bfe1SHajimu UMEMOTO ndi = ND_IFINFO(ifp); 86633841545SHajimu UMEMOTO if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) { 86733841545SHajimu UMEMOTO /* 86833841545SHajimu UMEMOTO * We've been generating a random ID on this interface. 86933841545SHajimu UMEMOTO * Create a new one. 87033841545SHajimu UMEMOTO */ 87133841545SHajimu UMEMOTO (void)generate_tmp_ifid(ndi->randomseed0, 8727efe5d92SHajimu UMEMOTO ndi->randomseed1, ndi->randomid); 87333841545SHajimu UMEMOTO } 87433841545SHajimu UMEMOTO } 87533841545SHajimu UMEMOTO 876f6dfe47aSMarko Zec CURVNET_RESTORE(); 87733841545SHajimu UMEMOTO } 87871498f30SBruce M Simpson 87971498f30SBruce M Simpson static void 8801272577eSXin LI in6_purgemaddrs(struct ifnet *ifp) 88171498f30SBruce M Simpson { 88233cde130SBruce M Simpson LIST_HEAD(,in6_multi) purgeinms; 88333cde130SBruce M Simpson struct in6_multi *inm, *tinm; 88433cde130SBruce M Simpson struct ifmultiaddr *ifma; 88571498f30SBruce M Simpson 88633cde130SBruce M Simpson LIST_INIT(&purgeinms); 88733cde130SBruce M Simpson IN6_MULTI_LOCK(); 88833cde130SBruce M Simpson 88933cde130SBruce M Simpson /* 89033cde130SBruce M Simpson * Extract list of in6_multi associated with the detaching ifp 89133cde130SBruce M Simpson * which the PF_INET6 layer is about to release. 89233cde130SBruce M Simpson * We need to do this as IF_ADDR_LOCK() may be re-acquired 89333cde130SBruce M Simpson * by code further down. 89433cde130SBruce M Simpson */ 895137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 89633cde130SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 89733cde130SBruce M Simpson if (ifma->ifma_addr->sa_family != AF_INET6 || 89833cde130SBruce M Simpson ifma->ifma_protospec == NULL) 89933cde130SBruce M Simpson continue; 90033cde130SBruce M Simpson inm = (struct in6_multi *)ifma->ifma_protospec; 90133cde130SBruce M Simpson LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry); 90271498f30SBruce M Simpson } 903137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 90433cde130SBruce M Simpson 90533cde130SBruce M Simpson LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) { 90633cde130SBruce M Simpson LIST_REMOVE(inm, in6m_entry); 90733cde130SBruce M Simpson in6m_release_locked(inm); 90833cde130SBruce M Simpson } 90933cde130SBruce M Simpson mld_ifdetach(ifp); 91033cde130SBruce M Simpson 91133cde130SBruce M Simpson IN6_MULTI_UNLOCK(); 91271498f30SBruce M Simpson } 9139901091eSBjoern A. Zeeb 9149901091eSBjoern A. Zeeb void 9159901091eSBjoern A. Zeeb in6_ifattach_destroy(void) 9169901091eSBjoern A. Zeeb { 9179901091eSBjoern A. Zeeb 9189901091eSBjoern A. Zeeb callout_drain(&V_in6_tmpaddrtimer_ch); 9199901091eSBjoern A. Zeeb } 9209901091eSBjoern A. Zeeb 9219901091eSBjoern A. Zeeb static void 9229901091eSBjoern A. Zeeb in6_ifattach_init(void *dummy) 9239901091eSBjoern A. Zeeb { 9249901091eSBjoern A. Zeeb 9259901091eSBjoern A. Zeeb /* Timer for regeneranation of temporary addresses randomize ID. */ 9269901091eSBjoern A. Zeeb callout_init(&V_in6_tmpaddrtimer_ch, 0); 9279901091eSBjoern A. Zeeb callout_reset(&V_in6_tmpaddrtimer_ch, 9289901091eSBjoern A. Zeeb (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor - 9299901091eSBjoern A. Zeeb V_ip6_temp_regen_advance) * hz, 9309901091eSBjoern A. Zeeb in6_tmpaddrtimer, curvnet); 9319901091eSBjoern A. Zeeb } 9329901091eSBjoern A. Zeeb 9339901091eSBjoern A. Zeeb /* 9349901091eSBjoern A. Zeeb * Cheat. 9359901091eSBjoern A. Zeeb * This must be after route_init(), which is now SI_ORDER_THIRD. 9369901091eSBjoern A. Zeeb */ 9379901091eSBjoern A. Zeeb SYSINIT(in6_ifattach_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 9389901091eSBjoern A. Zeeb in6_ifattach_init, NULL); 939