1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1980, 1986, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)if.c 8.3 (Berkeley) 1/4/94 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 375591b823SEivind Eklund #include "opt_compat.h" 38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 390d0f9d1eSYoshinobu Inoue #include "opt_inet.h" 405591b823SEivind Eklund 41df8bae1dSRodney W. Grimes #include <sys/param.h> 424d1d4912SBruce Evans #include <sys/malloc.h> 43df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 44df8bae1dSRodney W. Grimes #include <sys/systm.h> 45df8bae1dSRodney W. Grimes #include <sys/proc.h> 46df8bae1dSRodney W. Grimes #include <sys/socket.h> 47df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 48df8bae1dSRodney W. Grimes #include <sys/protosw.h> 49df8bae1dSRodney W. Grimes #include <sys/kernel.h> 5051a53488SBruce Evans #include <sys/sockio.h> 51963e4c2aSGarrett Wollman #include <sys/syslog.h> 52602d513cSGarrett Wollman #include <sys/sysctl.h> 5391421ba2SRobert Watson #include <sys/jail.h> 54df8bae1dSRodney W. Grimes 55df8bae1dSRodney W. Grimes #include <net/if.h> 56b106252cSBill Paul #include <net/if_arp.h> 57df8bae1dSRodney W. Grimes #include <net/if_dl.h> 5866ce51ceSArchie Cobbs #include <net/if_types.h> 599448326fSPoul-Henning Kamp #include <net/radix.h> 605500d3beSWarner Losh #include <net/route.h> 61df8bae1dSRodney W. Grimes 620d0f9d1eSYoshinobu Inoue #if defined(INET) || defined(INET6) 6382cd038dSYoshinobu Inoue /*XXX*/ 6482cd038dSYoshinobu Inoue #include <netinet/in.h> 650d0f9d1eSYoshinobu Inoue #include <netinet/in_var.h> 663411310dSYoshinobu Inoue #ifdef INET6 67978ee2edSJun-ichiro itojun Hagino #include <netinet6/in6_var.h> 68978ee2edSJun-ichiro itojun Hagino #include <netinet6/in6_ifattach.h> 693411310dSYoshinobu Inoue #endif 7082cd038dSYoshinobu Inoue #endif 7182cd038dSYoshinobu Inoue 722b14f991SJulian Elischer /* 732b14f991SJulian Elischer * System initialization 742b14f991SJulian Elischer */ 752b14f991SJulian Elischer 76ecbb00a2SDoug Rabson static int ifconf __P((u_long, caddr_t)); 774590fd3aSDavid Greenman static void ifinit __P((void *)); 783bda9f9bSPoul-Henning Kamp static void if_qflush __P((struct ifqueue *)); 793bda9f9bSPoul-Henning Kamp static void if_slowtimo __P((void *)); 803bda9f9bSPoul-Henning Kamp static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *)); 815500d3beSWarner Losh static int if_rtdel __P((struct radix_node *, void *)); 823bda9f9bSPoul-Henning Kamp 832b14f991SJulian Elischer SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL) 842b14f991SJulian Elischer 85a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 86a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 872b14f991SJulian Elischer 88df8bae1dSRodney W. Grimes int ifqmaxlen = IFQ_MAXLEN; 8929412182SGarrett Wollman struct ifnethead ifnet; /* depend on static init XXX */ 90df8bae1dSRodney W. Grimes 9182cd038dSYoshinobu Inoue #ifdef INET6 9282cd038dSYoshinobu Inoue /* 9382cd038dSYoshinobu Inoue * XXX: declare here to avoid to include many inet6 related files.. 9482cd038dSYoshinobu Inoue * should be more generalized? 9582cd038dSYoshinobu Inoue */ 9682cd038dSYoshinobu Inoue extern void nd6_setmtu __P((struct ifnet *)); 9782cd038dSYoshinobu Inoue #endif 9882cd038dSYoshinobu Inoue 99df8bae1dSRodney W. Grimes /* 100df8bae1dSRodney W. Grimes * Network interface utility routines. 101df8bae1dSRodney W. Grimes * 102df8bae1dSRodney W. Grimes * Routines with ifa_ifwith* names take sockaddr *'s as 103df8bae1dSRodney W. Grimes * parameters. 104df8bae1dSRodney W. Grimes */ 1052b14f991SJulian Elischer /* ARGSUSED*/ 106df8bae1dSRodney W. Grimes void 10727501cb6SBruce Evans ifinit(dummy) 10827501cb6SBruce Evans void *dummy; 109df8bae1dSRodney W. Grimes { 1108ba5bdaeSPeter Wemm struct ifnet *ifp; 1118ba5bdaeSPeter Wemm int s; 112df8bae1dSRodney W. Grimes 1138ba5bdaeSPeter Wemm s = splimp(); 114fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) { 115e0ea20bcSPoul-Henning Kamp if (ifp->if_snd.ifq_maxlen == 0) { 116e0ea20bcSPoul-Henning Kamp printf("%s%d XXX: driver didn't set ifq_maxlen\n", 117e0ea20bcSPoul-Henning Kamp ifp->if_name, ifp->if_unit); 118df8bae1dSRodney W. Grimes ifp->if_snd.ifq_maxlen = ifqmaxlen; 119e0ea20bcSPoul-Henning Kamp } 1205e980e22SJohn Baldwin if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) { 121df5e1987SJonathan Lemon printf("%s%d XXX: driver didn't initialize queue mtx\n", 122df5e1987SJonathan Lemon ifp->if_name, ifp->if_unit); 123df5e1987SJonathan Lemon mtx_init(&ifp->if_snd.ifq_mtx, "unknown", MTX_DEF); 124df5e1987SJonathan Lemon } 125df5e1987SJonathan Lemon } 1268ba5bdaeSPeter Wemm splx(s); 127df8bae1dSRodney W. Grimes if_slowtimo(0); 128df8bae1dSRodney W. Grimes } 129df8bae1dSRodney W. Grimes 130bbd17bf8SGarrett Wollman int if_index = 0; 131bbd17bf8SGarrett Wollman struct ifaddr **ifnet_addrs; 13282cd038dSYoshinobu Inoue struct ifnet **ifindex2ifnet = NULL; 1333bda9f9bSPoul-Henning Kamp 134df8bae1dSRodney W. Grimes 135df8bae1dSRodney W. Grimes /* 136df8bae1dSRodney W. Grimes * Attach an interface to the 137df8bae1dSRodney W. Grimes * list of "active" interfaces. 138df8bae1dSRodney W. Grimes */ 139df8bae1dSRodney W. Grimes void 140df8bae1dSRodney W. Grimes if_attach(ifp) 141df8bae1dSRodney W. Grimes struct ifnet *ifp; 142df8bae1dSRodney W. Grimes { 143df8bae1dSRodney W. Grimes unsigned socksize, ifasize; 1441ce9bf88SPoul-Henning Kamp int namelen, masklen; 1451ce9bf88SPoul-Henning Kamp char workbuf[64]; 146df8bae1dSRodney W. Grimes register struct sockaddr_dl *sdl; 147df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 148df8bae1dSRodney W. Grimes static int if_indexlim = 8; 14929412182SGarrett Wollman static int inited; 150f23b4c91SGarrett Wollman 15129412182SGarrett Wollman if (!inited) { 15229412182SGarrett Wollman TAILQ_INIT(&ifnet); 15329412182SGarrett Wollman inited = 1; 15429412182SGarrett Wollman } 155df8bae1dSRodney W. Grimes 15629412182SGarrett Wollman TAILQ_INSERT_TAIL(&ifnet, ifp, if_link); 157df8bae1dSRodney W. Grimes ifp->if_index = ++if_index; 15859562606SGarrett Wollman /* 15959562606SGarrett Wollman * XXX - 16059562606SGarrett Wollman * The old code would work if the interface passed a pre-existing 16159562606SGarrett Wollman * chain of ifaddrs to this code. We don't trust our callers to 16259562606SGarrett Wollman * properly initialize the tailq, however, so we no longer allow 16359562606SGarrett Wollman * this unlikely case. 16459562606SGarrett Wollman */ 16559562606SGarrett Wollman TAILQ_INIT(&ifp->if_addrhead); 16682cd038dSYoshinobu Inoue TAILQ_INIT(&ifp->if_prefixhead); 1676817526dSPoul-Henning Kamp TAILQ_INIT(&ifp->if_multiaddrs); 16898b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 169df8bae1dSRodney W. Grimes if (ifnet_addrs == 0 || if_index >= if_indexlim) { 170df8bae1dSRodney W. Grimes unsigned n = (if_indexlim <<= 1) * sizeof(ifa); 1717cc0979fSDavid Malone caddr_t q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO); 172df8bae1dSRodney W. Grimes if (ifnet_addrs) { 173df8bae1dSRodney W. Grimes bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2); 174df8bae1dSRodney W. Grimes free((caddr_t)ifnet_addrs, M_IFADDR); 175df8bae1dSRodney W. Grimes } 17682cd038dSYoshinobu Inoue ifnet_addrs = (struct ifaddr **)q; 17782cd038dSYoshinobu Inoue 17882cd038dSYoshinobu Inoue /* grow ifindex2ifnet */ 17982cd038dSYoshinobu Inoue n = if_indexlim * sizeof(struct ifnet *); 1807cc0979fSDavid Malone q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO); 18182cd038dSYoshinobu Inoue if (ifindex2ifnet) { 18282cd038dSYoshinobu Inoue bcopy((caddr_t)ifindex2ifnet, q, n/2); 18382cd038dSYoshinobu Inoue free((caddr_t)ifindex2ifnet, M_IFADDR); 184df8bae1dSRodney W. Grimes } 18582cd038dSYoshinobu Inoue ifindex2ifnet = (struct ifnet **)q; 18682cd038dSYoshinobu Inoue } 18782cd038dSYoshinobu Inoue 18882cd038dSYoshinobu Inoue ifindex2ifnet[if_index] = ifp; 18982cd038dSYoshinobu Inoue 190df5e1987SJonathan Lemon mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, MTX_DEF); 191df5e1987SJonathan Lemon 192df8bae1dSRodney W. Grimes /* 193df8bae1dSRodney W. Grimes * create a Link Level name for this device 194df8bae1dSRodney W. Grimes */ 1952127f260SArchie Cobbs namelen = snprintf(workbuf, sizeof(workbuf), 1962127f260SArchie Cobbs "%s%d", ifp->if_name, ifp->if_unit); 197df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) 1981ce9bf88SPoul-Henning Kamp masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 199df8bae1dSRodney W. Grimes socksize = masklen + ifp->if_addrlen; 200df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 201df8bae1dSRodney W. Grimes if (socksize < sizeof(*sdl)) 202df8bae1dSRodney W. Grimes socksize = sizeof(*sdl); 2038a261b8fSDoug Rabson socksize = ROUNDUP(socksize); 204df8bae1dSRodney W. Grimes ifasize = sizeof(*ifa) + 2 * socksize; 2057cc0979fSDavid Malone ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 2069448326fSPoul-Henning Kamp if (ifa) { 207df8bae1dSRodney W. Grimes sdl = (struct sockaddr_dl *)(ifa + 1); 208df8bae1dSRodney W. Grimes sdl->sdl_len = socksize; 209df8bae1dSRodney W. Grimes sdl->sdl_family = AF_LINK; 2101ce9bf88SPoul-Henning Kamp bcopy(workbuf, sdl->sdl_data, namelen); 2111ce9bf88SPoul-Henning Kamp sdl->sdl_nlen = namelen; 212df8bae1dSRodney W. Grimes sdl->sdl_index = ifp->if_index; 213df8bae1dSRodney W. Grimes sdl->sdl_type = ifp->if_type; 214df8bae1dSRodney W. Grimes ifnet_addrs[if_index - 1] = ifa; 215df8bae1dSRodney W. Grimes ifa->ifa_ifp = ifp; 216df8bae1dSRodney W. Grimes ifa->ifa_rtrequest = link_rtrequest; 217df8bae1dSRodney W. Grimes ifa->ifa_addr = (struct sockaddr *)sdl; 218df8bae1dSRodney W. Grimes sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 219df8bae1dSRodney W. Grimes ifa->ifa_netmask = (struct sockaddr *)sdl; 220df8bae1dSRodney W. Grimes sdl->sdl_len = masklen; 221df8bae1dSRodney W. Grimes while (namelen != 0) 222df8bae1dSRodney W. Grimes sdl->sdl_data[--namelen] = 0xff; 22359562606SGarrett Wollman TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 224df8bae1dSRodney W. Grimes } 225df8bae1dSRodney W. Grimes } 2266182fdbdSPeter Wemm 2276182fdbdSPeter Wemm /* 2286182fdbdSPeter Wemm * Detach an interface, removing it from the 2296182fdbdSPeter Wemm * list of "active" interfaces. 2306182fdbdSPeter Wemm */ 2316182fdbdSPeter Wemm void 2326182fdbdSPeter Wemm if_detach(ifp) 2336182fdbdSPeter Wemm struct ifnet *ifp; 2346182fdbdSPeter Wemm { 2356182fdbdSPeter Wemm struct ifaddr *ifa; 2365500d3beSWarner Losh struct radix_node_head *rnh; 2375500d3beSWarner Losh int s; 2385500d3beSWarner Losh int i; 2396182fdbdSPeter Wemm 2406182fdbdSPeter Wemm /* 2416182fdbdSPeter Wemm * Remove routes and flush queues. 2426182fdbdSPeter Wemm */ 2435500d3beSWarner Losh s = splnet(); 2446182fdbdSPeter Wemm if_down(ifp); 2456182fdbdSPeter Wemm 2466182fdbdSPeter Wemm /* 2476182fdbdSPeter Wemm * Remove address from ifnet_addrs[] and maybe decrement if_index. 2486182fdbdSPeter Wemm * Clean up all addresses. 2496182fdbdSPeter Wemm */ 250aa6be122SWarner Losh ifnet_addrs[ifp->if_index - 1] = 0; 251aa6be122SWarner Losh while (if_index > 0 && ifnet_addrs[if_index - 1] == 0) 2526182fdbdSPeter Wemm if_index--; 2536182fdbdSPeter Wemm 2546182fdbdSPeter Wemm for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 2556182fdbdSPeter Wemm ifa = TAILQ_FIRST(&ifp->if_addrhead)) { 2560d0f9d1eSYoshinobu Inoue #ifdef INET 257aa6be122SWarner Losh /* XXX: Ugly!! ad hoc just for INET */ 258aa6be122SWarner Losh if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 259aa6be122SWarner Losh struct ifaliasreq ifr; 260aa6be122SWarner Losh 261aa6be122SWarner Losh bzero(&ifr, sizeof(ifr)); 262aa6be122SWarner Losh ifr.ifra_addr = *ifa->ifa_addr; 263aa6be122SWarner Losh if (ifa->ifa_dstaddr) 264aa6be122SWarner Losh ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 265aa6be122SWarner Losh if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 266aa6be122SWarner Losh NULL) == 0) 267aa6be122SWarner Losh continue; 268aa6be122SWarner Losh } 2690d0f9d1eSYoshinobu Inoue #endif /* INET */ 2700d0f9d1eSYoshinobu Inoue #ifdef INET6 2710d0f9d1eSYoshinobu Inoue if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { 272978ee2edSJun-ichiro itojun Hagino in6_purgeaddr(ifa, ifp); 273978ee2edSJun-ichiro itojun Hagino /* ifp_addrhead is already updated */ 2740d0f9d1eSYoshinobu Inoue continue; 2750d0f9d1eSYoshinobu Inoue } 2760d0f9d1eSYoshinobu Inoue #endif /* INET6 */ 2776182fdbdSPeter Wemm TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 2786182fdbdSPeter Wemm IFAFREE(ifa); 2796182fdbdSPeter Wemm } 2806182fdbdSPeter Wemm 2815500d3beSWarner Losh /* 2825500d3beSWarner Losh * Delete all remaining routes using this interface 2835500d3beSWarner Losh * Unfortuneatly the only way to do this is to slog through 2845500d3beSWarner Losh * the entire routing table looking for routes which point 2855500d3beSWarner Losh * to this interface...oh well... 2865500d3beSWarner Losh */ 2875500d3beSWarner Losh for (i = 1; i <= AF_MAX; i++) { 2885500d3beSWarner Losh if ((rnh = rt_tables[i]) == NULL) 2895500d3beSWarner Losh continue; 2905500d3beSWarner Losh (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 2915500d3beSWarner Losh } 2925500d3beSWarner Losh 293978ee2edSJun-ichiro itojun Hagino #ifdef INET6 294978ee2edSJun-ichiro itojun Hagino /* nuke all IPv6 kernel structs related to ifp */ 295978ee2edSJun-ichiro itojun Hagino in6_ifdetach(ifp); 296978ee2edSJun-ichiro itojun Hagino #endif 297978ee2edSJun-ichiro itojun Hagino 2986182fdbdSPeter Wemm TAILQ_REMOVE(&ifnet, ifp, if_link); 299df5e1987SJonathan Lemon mtx_destroy(&ifp->if_snd.ifq_mtx); 3005500d3beSWarner Losh splx(s); 3015500d3beSWarner Losh } 3025500d3beSWarner Losh 3035500d3beSWarner Losh /* 3045500d3beSWarner Losh * Delete Routes for a Network Interface 3055500d3beSWarner Losh * 3065500d3beSWarner Losh * Called for each routing entry via the rnh->rnh_walktree() call above 3075500d3beSWarner Losh * to delete all route entries referencing a detaching network interface. 3085500d3beSWarner Losh * 3095500d3beSWarner Losh * Arguments: 3105500d3beSWarner Losh * rn pointer to node in the routing table 3115500d3beSWarner Losh * arg argument passed to rnh->rnh_walktree() - detaching interface 3125500d3beSWarner Losh * 3135500d3beSWarner Losh * Returns: 3145500d3beSWarner Losh * 0 successful 3155500d3beSWarner Losh * errno failed - reason indicated 3165500d3beSWarner Losh * 3175500d3beSWarner Losh */ 3185500d3beSWarner Losh static int 3195500d3beSWarner Losh if_rtdel(rn, arg) 3205500d3beSWarner Losh struct radix_node *rn; 3215500d3beSWarner Losh void *arg; 3225500d3beSWarner Losh { 3235500d3beSWarner Losh struct rtentry *rt = (struct rtentry *)rn; 3245500d3beSWarner Losh struct ifnet *ifp = arg; 3255500d3beSWarner Losh int err; 3265500d3beSWarner Losh 3275500d3beSWarner Losh if (rt->rt_ifp == ifp) { 3285500d3beSWarner Losh 3295500d3beSWarner Losh /* 3305500d3beSWarner Losh * Protect (sorta) against walktree recursion problems 3315500d3beSWarner Losh * with cloned routes 3325500d3beSWarner Losh */ 3335500d3beSWarner Losh if ((rt->rt_flags & RTF_UP) == 0) 3345500d3beSWarner Losh return (0); 3355500d3beSWarner Losh 3365500d3beSWarner Losh err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 3375500d3beSWarner Losh rt_mask(rt), rt->rt_flags, 3385500d3beSWarner Losh (struct rtentry **) NULL); 3395500d3beSWarner Losh if (err) { 3405500d3beSWarner Losh log(LOG_WARNING, "if_rtdel: error %d\n", err); 3415500d3beSWarner Losh } 3425500d3beSWarner Losh } 3435500d3beSWarner Losh 3445500d3beSWarner Losh return (0); 3456182fdbdSPeter Wemm } 3466182fdbdSPeter Wemm 347df8bae1dSRodney W. Grimes /* 348df8bae1dSRodney W. Grimes * Locate an interface based on a complete address. 349df8bae1dSRodney W. Grimes */ 350df8bae1dSRodney W. Grimes /*ARGSUSED*/ 351df8bae1dSRodney W. Grimes struct ifaddr * 352df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr) 353df8bae1dSRodney W. Grimes register struct sockaddr *addr; 354df8bae1dSRodney W. Grimes { 355df8bae1dSRodney W. Grimes register struct ifnet *ifp; 356df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 357df8bae1dSRodney W. Grimes 358df8bae1dSRodney W. Grimes #define equal(a1, a2) \ 359df8bae1dSRodney W. Grimes (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) 360fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) 36137d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 362df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != addr->sa_family) 363df8bae1dSRodney W. Grimes continue; 364df8bae1dSRodney W. Grimes if (equal(addr, ifa->ifa_addr)) 365df8bae1dSRodney W. Grimes return (ifa); 366df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr && 36782cd038dSYoshinobu Inoue /* IP6 doesn't have broadcast */ 36882cd038dSYoshinobu Inoue ifa->ifa_broadaddr->sa_len != 0 && 369df8bae1dSRodney W. Grimes equal(ifa->ifa_broadaddr, addr)) 370df8bae1dSRodney W. Grimes return (ifa); 371df8bae1dSRodney W. Grimes } 372df8bae1dSRodney W. Grimes return ((struct ifaddr *)0); 373df8bae1dSRodney W. Grimes } 374df8bae1dSRodney W. Grimes /* 375df8bae1dSRodney W. Grimes * Locate the point to point interface with a given destination address. 376df8bae1dSRodney W. Grimes */ 377df8bae1dSRodney W. Grimes /*ARGSUSED*/ 378df8bae1dSRodney W. Grimes struct ifaddr * 379df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr) 380df8bae1dSRodney W. Grimes register struct sockaddr *addr; 381df8bae1dSRodney W. Grimes { 382df8bae1dSRodney W. Grimes register struct ifnet *ifp; 383df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 384df8bae1dSRodney W. Grimes 385fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) 386df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_POINTOPOINT) 38737d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 388df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != addr->sa_family) 389df8bae1dSRodney W. Grimes continue; 39055088a1cSDavid Greenman if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)) 391df8bae1dSRodney W. Grimes return (ifa); 392df8bae1dSRodney W. Grimes } 393df8bae1dSRodney W. Grimes return ((struct ifaddr *)0); 394df8bae1dSRodney W. Grimes } 395df8bae1dSRodney W. Grimes 396df8bae1dSRodney W. Grimes /* 397df8bae1dSRodney W. Grimes * Find an interface on a specific network. If many, choice 398df8bae1dSRodney W. Grimes * is most specific found. 399df8bae1dSRodney W. Grimes */ 400df8bae1dSRodney W. Grimes struct ifaddr * 401df8bae1dSRodney W. Grimes ifa_ifwithnet(addr) 402df8bae1dSRodney W. Grimes struct sockaddr *addr; 403df8bae1dSRodney W. Grimes { 404df8bae1dSRodney W. Grimes register struct ifnet *ifp; 405df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 406df8bae1dSRodney W. Grimes struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 407df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 408df8bae1dSRodney W. Grimes char *addr_data = addr->sa_data, *cplim; 409df8bae1dSRodney W. Grimes 4107e2a6151SJulian Elischer /* 4117e2a6151SJulian Elischer * AF_LINK addresses can be looked up directly by their index number, 4127e2a6151SJulian Elischer * so do that if we can. 4137e2a6151SJulian Elischer */ 414df8bae1dSRodney W. Grimes if (af == AF_LINK) { 415df8bae1dSRodney W. Grimes register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 416df8bae1dSRodney W. Grimes if (sdl->sdl_index && sdl->sdl_index <= if_index) 417df8bae1dSRodney W. Grimes return (ifnet_addrs[sdl->sdl_index - 1]); 418df8bae1dSRodney W. Grimes } 4197e2a6151SJulian Elischer 4207e2a6151SJulian Elischer /* 4217e2a6151SJulian Elischer * Scan though each interface, looking for ones that have 4227e2a6151SJulian Elischer * addresses in this address family. 4237e2a6151SJulian Elischer */ 424fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) { 42537d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 426df8bae1dSRodney W. Grimes register char *cp, *cp2, *cp3; 427df8bae1dSRodney W. Grimes 428523a02aaSDavid Greenman if (ifa->ifa_addr->sa_family != af) 429df8bae1dSRodney W. Grimes next: continue; 43082cd038dSYoshinobu Inoue if ( 43182cd038dSYoshinobu Inoue #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */ 43282cd038dSYoshinobu Inoue addr->sa_family != AF_INET6 && 43382cd038dSYoshinobu Inoue #endif 43482cd038dSYoshinobu Inoue ifp->if_flags & IFF_POINTOPOINT) { 4357e2a6151SJulian Elischer /* 4367e2a6151SJulian Elischer * This is a bit broken as it doesn't 4377e2a6151SJulian Elischer * take into account that the remote end may 4387e2a6151SJulian Elischer * be a single node in the network we are 4397e2a6151SJulian Elischer * looking for. 4407e2a6151SJulian Elischer * The trouble is that we don't know the 4417e2a6151SJulian Elischer * netmask for the remote end. 4427e2a6151SJulian Elischer */ 443fcd6781aSGarrett Wollman if (ifa->ifa_dstaddr != 0 444fcd6781aSGarrett Wollman && equal(addr, ifa->ifa_dstaddr)) 445b2af64fdSDavid Greenman return (ifa); 4463740e2adSDavid Greenman } else { 4477e2a6151SJulian Elischer /* 4487ed8f465SJulian Elischer * if we have a special address handler, 4497ed8f465SJulian Elischer * then use it instead of the generic one. 4507ed8f465SJulian Elischer */ 4517ed8f465SJulian Elischer if (ifa->ifa_claim_addr) { 4527ed8f465SJulian Elischer if ((*ifa->ifa_claim_addr)(ifa, addr)) { 4537ed8f465SJulian Elischer return (ifa); 4547ed8f465SJulian Elischer } else { 4557ed8f465SJulian Elischer continue; 4567ed8f465SJulian Elischer } 4577ed8f465SJulian Elischer } 4587ed8f465SJulian Elischer 4597ed8f465SJulian Elischer /* 4607e2a6151SJulian Elischer * Scan all the bits in the ifa's address. 4617e2a6151SJulian Elischer * If a bit dissagrees with what we are 4627e2a6151SJulian Elischer * looking for, mask it with the netmask 4637e2a6151SJulian Elischer * to see if it really matters. 4647e2a6151SJulian Elischer * (A byte at a time) 4657e2a6151SJulian Elischer */ 466523a02aaSDavid Greenman if (ifa->ifa_netmask == 0) 467523a02aaSDavid Greenman continue; 468df8bae1dSRodney W. Grimes cp = addr_data; 469df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 470df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 4717e2a6151SJulian Elischer cplim = ifa->ifa_netmask->sa_len 4727e2a6151SJulian Elischer + (char *)ifa->ifa_netmask; 473df8bae1dSRodney W. Grimes while (cp3 < cplim) 474df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3++) 4757e2a6151SJulian Elischer goto next; /* next address! */ 4767e2a6151SJulian Elischer /* 4777e2a6151SJulian Elischer * If the netmask of what we just found 4787e2a6151SJulian Elischer * is more specific than what we had before 4797e2a6151SJulian Elischer * (if we had one) then remember the new one 4807e2a6151SJulian Elischer * before continuing to search 4817e2a6151SJulian Elischer * for an even better one. 4827e2a6151SJulian Elischer */ 483df8bae1dSRodney W. Grimes if (ifa_maybe == 0 || 484df8bae1dSRodney W. Grimes rn_refines((caddr_t)ifa->ifa_netmask, 485df8bae1dSRodney W. Grimes (caddr_t)ifa_maybe->ifa_netmask)) 486df8bae1dSRodney W. Grimes ifa_maybe = ifa; 487df8bae1dSRodney W. Grimes } 488b2af64fdSDavid Greenman } 489b2af64fdSDavid Greenman } 490df8bae1dSRodney W. Grimes return (ifa_maybe); 491df8bae1dSRodney W. Grimes } 492df8bae1dSRodney W. Grimes 493df8bae1dSRodney W. Grimes /* 494df8bae1dSRodney W. Grimes * Find an interface address specific to an interface best matching 495df8bae1dSRodney W. Grimes * a given address. 496df8bae1dSRodney W. Grimes */ 497df8bae1dSRodney W. Grimes struct ifaddr * 498df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp) 499df8bae1dSRodney W. Grimes struct sockaddr *addr; 500df8bae1dSRodney W. Grimes register struct ifnet *ifp; 501df8bae1dSRodney W. Grimes { 502df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 503df8bae1dSRodney W. Grimes register char *cp, *cp2, *cp3; 504df8bae1dSRodney W. Grimes register char *cplim; 505df8bae1dSRodney W. Grimes struct ifaddr *ifa_maybe = 0; 506df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 507df8bae1dSRodney W. Grimes 508df8bae1dSRodney W. Grimes if (af >= AF_MAX) 509df8bae1dSRodney W. Grimes return (0); 51037d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 511df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != af) 512df8bae1dSRodney W. Grimes continue; 513381dd1d2SJulian Elischer if (ifa_maybe == 0) 514df8bae1dSRodney W. Grimes ifa_maybe = ifa; 515df8bae1dSRodney W. Grimes if (ifa->ifa_netmask == 0) { 516df8bae1dSRodney W. Grimes if (equal(addr, ifa->ifa_addr) || 517df8bae1dSRodney W. Grimes (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 518df8bae1dSRodney W. Grimes return (ifa); 519df8bae1dSRodney W. Grimes continue; 520df8bae1dSRodney W. Grimes } 521b2af64fdSDavid Greenman if (ifp->if_flags & IFF_POINTOPOINT) { 522b2af64fdSDavid Greenman if (equal(addr, ifa->ifa_dstaddr)) 523b2af64fdSDavid Greenman return (ifa); 5243740e2adSDavid Greenman } else { 525df8bae1dSRodney W. Grimes cp = addr->sa_data; 526df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 527df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 528df8bae1dSRodney W. Grimes cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 529df8bae1dSRodney W. Grimes for (; cp3 < cplim; cp3++) 530df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3) 531df8bae1dSRodney W. Grimes break; 532df8bae1dSRodney W. Grimes if (cp3 == cplim) 533df8bae1dSRodney W. Grimes return (ifa); 534df8bae1dSRodney W. Grimes } 535b2af64fdSDavid Greenman } 536df8bae1dSRodney W. Grimes return (ifa_maybe); 537df8bae1dSRodney W. Grimes } 538df8bae1dSRodney W. Grimes 539df8bae1dSRodney W. Grimes #include <net/route.h> 540df8bae1dSRodney W. Grimes 541df8bae1dSRodney W. Grimes /* 542df8bae1dSRodney W. Grimes * Default action when installing a route with a Link Level gateway. 543df8bae1dSRodney W. Grimes * Lookup an appropriate real ifa to point to. 544df8bae1dSRodney W. Grimes * This should be moved to /sys/net/link.c eventually. 545df8bae1dSRodney W. Grimes */ 5463bda9f9bSPoul-Henning Kamp static void 547df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa) 548df8bae1dSRodney W. Grimes int cmd; 549df8bae1dSRodney W. Grimes register struct rtentry *rt; 550df8bae1dSRodney W. Grimes struct sockaddr *sa; 551df8bae1dSRodney W. Grimes { 552df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 553df8bae1dSRodney W. Grimes struct sockaddr *dst; 554df8bae1dSRodney W. Grimes struct ifnet *ifp; 555df8bae1dSRodney W. Grimes 556df8bae1dSRodney W. Grimes if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 557df8bae1dSRodney W. Grimes ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 558df8bae1dSRodney W. Grimes return; 5599448326fSPoul-Henning Kamp ifa = ifaof_ifpforaddr(dst, ifp); 5609448326fSPoul-Henning Kamp if (ifa) { 561df8bae1dSRodney W. Grimes IFAFREE(rt->rt_ifa); 562df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 563df8bae1dSRodney W. Grimes ifa->ifa_refcnt++; 564df8bae1dSRodney W. Grimes if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 565df8bae1dSRodney W. Grimes ifa->ifa_rtrequest(cmd, rt, sa); 566df8bae1dSRodney W. Grimes } 567df8bae1dSRodney W. Grimes } 568df8bae1dSRodney W. Grimes 569df8bae1dSRodney W. Grimes /* 570df8bae1dSRodney W. Grimes * Mark an interface down and notify protocols of 571df8bae1dSRodney W. Grimes * the transition. 572df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 573df8bae1dSRodney W. Grimes */ 574df8bae1dSRodney W. Grimes void 575e8c2601dSPoul-Henning Kamp if_unroute(ifp, flag, fam) 576df8bae1dSRodney W. Grimes register struct ifnet *ifp; 577e8c2601dSPoul-Henning Kamp int flag, fam; 578df8bae1dSRodney W. Grimes { 579df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 580df8bae1dSRodney W. Grimes 581e8c2601dSPoul-Henning Kamp ifp->if_flags &= ~flag; 58298b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 583e8c2601dSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 584e8c2601dSPoul-Henning Kamp if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 585df8bae1dSRodney W. Grimes pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 586df8bae1dSRodney W. Grimes if_qflush(&ifp->if_snd); 587df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes 590df8bae1dSRodney W. Grimes /* 591df8bae1dSRodney W. Grimes * Mark an interface up and notify protocols of 592df8bae1dSRodney W. Grimes * the transition. 593df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 594df8bae1dSRodney W. Grimes */ 595df8bae1dSRodney W. Grimes void 596e8c2601dSPoul-Henning Kamp if_route(ifp, flag, fam) 597df8bae1dSRodney W. Grimes register struct ifnet *ifp; 598e8c2601dSPoul-Henning Kamp int flag, fam; 599df8bae1dSRodney W. Grimes { 600176395b2SGarrett Wollman register struct ifaddr *ifa; 601df8bae1dSRodney W. Grimes 602e8c2601dSPoul-Henning Kamp ifp->if_flags |= flag; 60398b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 604e8c2601dSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 605e8c2601dSPoul-Henning Kamp if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 606df8bae1dSRodney W. Grimes pfctlinput(PRC_IFUP, ifa->ifa_addr); 607df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 60882cd038dSYoshinobu Inoue #ifdef INET6 60982cd038dSYoshinobu Inoue in6_if_up(ifp); 61082cd038dSYoshinobu Inoue #endif 611df8bae1dSRodney W. Grimes } 612df8bae1dSRodney W. Grimes 613df8bae1dSRodney W. Grimes /* 614e8c2601dSPoul-Henning Kamp * Mark an interface down and notify protocols of 615e8c2601dSPoul-Henning Kamp * the transition. 616e8c2601dSPoul-Henning Kamp * NOTE: must be called at splnet or eqivalent. 617e8c2601dSPoul-Henning Kamp */ 618e8c2601dSPoul-Henning Kamp void 619e8c2601dSPoul-Henning Kamp if_down(ifp) 620e8c2601dSPoul-Henning Kamp register struct ifnet *ifp; 621e8c2601dSPoul-Henning Kamp { 622e8c2601dSPoul-Henning Kamp 623e8c2601dSPoul-Henning Kamp if_unroute(ifp, IFF_UP, AF_UNSPEC); 624e8c2601dSPoul-Henning Kamp } 625e8c2601dSPoul-Henning Kamp 626e8c2601dSPoul-Henning Kamp /* 627e8c2601dSPoul-Henning Kamp * Mark an interface up and notify protocols of 628e8c2601dSPoul-Henning Kamp * the transition. 629e8c2601dSPoul-Henning Kamp * NOTE: must be called at splnet or eqivalent. 630e8c2601dSPoul-Henning Kamp */ 631e8c2601dSPoul-Henning Kamp void 632e8c2601dSPoul-Henning Kamp if_up(ifp) 633e8c2601dSPoul-Henning Kamp register struct ifnet *ifp; 634e8c2601dSPoul-Henning Kamp { 635e8c2601dSPoul-Henning Kamp 636e8c2601dSPoul-Henning Kamp if_route(ifp, IFF_UP, AF_UNSPEC); 637e8c2601dSPoul-Henning Kamp } 638e8c2601dSPoul-Henning Kamp 639e8c2601dSPoul-Henning Kamp /* 640df8bae1dSRodney W. Grimes * Flush an interface queue. 641df8bae1dSRodney W. Grimes */ 6423bda9f9bSPoul-Henning Kamp static void 643df8bae1dSRodney W. Grimes if_qflush(ifq) 644df8bae1dSRodney W. Grimes register struct ifqueue *ifq; 645df8bae1dSRodney W. Grimes { 646df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 647df8bae1dSRodney W. Grimes 648df8bae1dSRodney W. Grimes n = ifq->ifq_head; 6499448326fSPoul-Henning Kamp while ((m = n) != 0) { 650df8bae1dSRodney W. Grimes n = m->m_act; 651df8bae1dSRodney W. Grimes m_freem(m); 652df8bae1dSRodney W. Grimes } 653df8bae1dSRodney W. Grimes ifq->ifq_head = 0; 654df8bae1dSRodney W. Grimes ifq->ifq_tail = 0; 655df8bae1dSRodney W. Grimes ifq->ifq_len = 0; 656df8bae1dSRodney W. Grimes } 657df8bae1dSRodney W. Grimes 658df8bae1dSRodney W. Grimes /* 659df8bae1dSRodney W. Grimes * Handle interface watchdog timer routines. Called 660df8bae1dSRodney W. Grimes * from softclock, we decrement timers (if set) and 661df8bae1dSRodney W. Grimes * call the appropriate interface routine on expiration. 662df8bae1dSRodney W. Grimes */ 6633bda9f9bSPoul-Henning Kamp static void 664df8bae1dSRodney W. Grimes if_slowtimo(arg) 665df8bae1dSRodney W. Grimes void *arg; 666df8bae1dSRodney W. Grimes { 667df8bae1dSRodney W. Grimes register struct ifnet *ifp; 668df8bae1dSRodney W. Grimes int s = splimp(); 669df8bae1dSRodney W. Grimes 670fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) { 671df8bae1dSRodney W. Grimes if (ifp->if_timer == 0 || --ifp->if_timer) 672df8bae1dSRodney W. Grimes continue; 673df8bae1dSRodney W. Grimes if (ifp->if_watchdog) 6744a5f1499SDavid Greenman (*ifp->if_watchdog)(ifp); 675df8bae1dSRodney W. Grimes } 676df8bae1dSRodney W. Grimes splx(s); 677df8bae1dSRodney W. Grimes timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 678df8bae1dSRodney W. Grimes } 679df8bae1dSRodney W. Grimes 680df8bae1dSRodney W. Grimes /* 681df8bae1dSRodney W. Grimes * Map interface name to 682df8bae1dSRodney W. Grimes * interface structure pointer. 683df8bae1dSRodney W. Grimes */ 684df8bae1dSRodney W. Grimes struct ifnet * 6858b7805e4SBoris Popov ifunit(char *name) 686df8bae1dSRodney W. Grimes { 68701f0fef3SJulian Elischer char namebuf[IFNAMSIZ + 1]; 6888b7805e4SBoris Popov char *cp; 6898b7805e4SBoris Popov struct ifnet *ifp; 690df8bae1dSRodney W. Grimes int unit; 6918b7805e4SBoris Popov unsigned len, m; 6928b7805e4SBoris Popov char c; 693df8bae1dSRodney W. Grimes 6948b7805e4SBoris Popov len = strlen(name); 6958b7805e4SBoris Popov if (len < 2 || len > IFNAMSIZ) 6968b7805e4SBoris Popov return NULL; 6978b7805e4SBoris Popov cp = name + len - 1; 6988b7805e4SBoris Popov c = *cp; 6998b7805e4SBoris Popov if (c < '0' || c > '9') 7008b7805e4SBoris Popov return NULL; /* trailing garbage */ 7018b7805e4SBoris Popov unit = 0; 7028b7805e4SBoris Popov m = 1; 7038b7805e4SBoris Popov do { 7048b7805e4SBoris Popov if (cp == name) 7058b7805e4SBoris Popov return NULL; /* no interface name */ 7068b7805e4SBoris Popov unit += (c - '0') * m; 7078b7805e4SBoris Popov if (unit > 1000000) 7088b7805e4SBoris Popov return NULL; /* number is unreasonable */ 7098b7805e4SBoris Popov m *= 10; 7108b7805e4SBoris Popov c = *--cp; 7118b7805e4SBoris Popov } while (c >= '0' && c <= '9'); 712df8bae1dSRodney W. Grimes len = cp - name + 1; 7138b7805e4SBoris Popov bcopy(name, namebuf, len); 7148b7805e4SBoris Popov namebuf[len] = '\0'; 71501f0fef3SJulian Elischer /* 71601f0fef3SJulian Elischer * Now search all the interfaces for this name/number 71701f0fef3SJulian Elischer */ 718fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(ifp, &ifnet, if_link) { 7198b7805e4SBoris Popov if (strcmp(ifp->if_name, namebuf)) 720df8bae1dSRodney W. Grimes continue; 721df8bae1dSRodney W. Grimes if (unit == ifp->if_unit) 722df8bae1dSRodney W. Grimes break; 723df8bae1dSRodney W. Grimes } 724df8bae1dSRodney W. Grimes return (ifp); 725df8bae1dSRodney W. Grimes } 726df8bae1dSRodney W. Grimes 72782cd038dSYoshinobu Inoue 72882cd038dSYoshinobu Inoue /* 72982cd038dSYoshinobu Inoue * Map interface name in a sockaddr_dl to 73082cd038dSYoshinobu Inoue * interface structure pointer. 73182cd038dSYoshinobu Inoue */ 73282cd038dSYoshinobu Inoue struct ifnet * 73382cd038dSYoshinobu Inoue if_withname(sa) 73482cd038dSYoshinobu Inoue struct sockaddr *sa; 73582cd038dSYoshinobu Inoue { 73682cd038dSYoshinobu Inoue char ifname[IFNAMSIZ+1]; 73782cd038dSYoshinobu Inoue struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; 73882cd038dSYoshinobu Inoue 73982cd038dSYoshinobu Inoue if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) || 74082cd038dSYoshinobu Inoue (sdl->sdl_nlen > IFNAMSIZ) ) 74182cd038dSYoshinobu Inoue return NULL; 74282cd038dSYoshinobu Inoue 74382cd038dSYoshinobu Inoue /* 74482cd038dSYoshinobu Inoue * ifunit wants a null-terminated name. It may not be null-terminated 74582cd038dSYoshinobu Inoue * in the sockaddr. We don't want to change the caller's sockaddr, 74682cd038dSYoshinobu Inoue * and there might not be room to put the trailing null anyway, so we 74782cd038dSYoshinobu Inoue * make a local copy that we know we can null terminate safely. 74882cd038dSYoshinobu Inoue */ 74982cd038dSYoshinobu Inoue 75082cd038dSYoshinobu Inoue bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen); 75182cd038dSYoshinobu Inoue ifname[sdl->sdl_nlen] = '\0'; 75282cd038dSYoshinobu Inoue return ifunit(ifname); 75382cd038dSYoshinobu Inoue } 75482cd038dSYoshinobu Inoue 75582cd038dSYoshinobu Inoue 756df8bae1dSRodney W. Grimes /* 757df8bae1dSRodney W. Grimes * Interface ioctls. 758df8bae1dSRodney W. Grimes */ 759df8bae1dSRodney W. Grimes int 760df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p) 761df8bae1dSRodney W. Grimes struct socket *so; 762ecbb00a2SDoug Rabson u_long cmd; 763df8bae1dSRodney W. Grimes caddr_t data; 764df8bae1dSRodney W. Grimes struct proc *p; 765df8bae1dSRodney W. Grimes { 766df8bae1dSRodney W. Grimes register struct ifnet *ifp; 767df8bae1dSRodney W. Grimes register struct ifreq *ifr; 768413dd0baSPoul-Henning Kamp struct ifstat *ifs; 769df8bae1dSRodney W. Grimes int error; 77082cd038dSYoshinobu Inoue short oif_flags; 771df8bae1dSRodney W. Grimes 772df8bae1dSRodney W. Grimes switch (cmd) { 773df8bae1dSRodney W. Grimes 774df8bae1dSRodney W. Grimes case SIOCGIFCONF: 775df8bae1dSRodney W. Grimes case OSIOCGIFCONF: 776df8bae1dSRodney W. Grimes return (ifconf(cmd, data)); 777df8bae1dSRodney W. Grimes } 778df8bae1dSRodney W. Grimes ifr = (struct ifreq *)data; 779df8bae1dSRodney W. Grimes ifp = ifunit(ifr->ifr_name); 780df8bae1dSRodney W. Grimes if (ifp == 0) 781df8bae1dSRodney W. Grimes return (ENXIO); 782df8bae1dSRodney W. Grimes switch (cmd) { 783df8bae1dSRodney W. Grimes 784df8bae1dSRodney W. Grimes case SIOCGIFFLAGS: 785df8bae1dSRodney W. Grimes ifr->ifr_flags = ifp->if_flags; 786df8bae1dSRodney W. Grimes break; 787df8bae1dSRodney W. Grimes 788df8bae1dSRodney W. Grimes case SIOCGIFMETRIC: 789df8bae1dSRodney W. Grimes ifr->ifr_metric = ifp->if_metric; 790df8bae1dSRodney W. Grimes break; 791df8bae1dSRodney W. Grimes 792a7028af7SDavid Greenman case SIOCGIFMTU: 793a7028af7SDavid Greenman ifr->ifr_mtu = ifp->if_mtu; 794a7028af7SDavid Greenman break; 795a7028af7SDavid Greenman 796074c4a4eSGarrett Wollman case SIOCGIFPHYS: 797074c4a4eSGarrett Wollman ifr->ifr_phys = ifp->if_physical; 798074c4a4eSGarrett Wollman break; 799074c4a4eSGarrett Wollman 800df8bae1dSRodney W. Grimes case SIOCSIFFLAGS: 801f711d546SPoul-Henning Kamp error = suser(p); 8029448326fSPoul-Henning Kamp if (error) 803df8bae1dSRodney W. Grimes return (error); 8044add131eSPoul-Henning Kamp ifr->ifr_prevflags = ifp->if_flags; 805cf4b9371SPoul-Henning Kamp if (ifp->if_flags & IFF_SMART) { 806cf4b9371SPoul-Henning Kamp /* Smart drivers twiddle their own routes */ 8072f55ead7SPoul-Henning Kamp } else if (ifp->if_flags & IFF_UP && 808cf4b9371SPoul-Henning Kamp (ifr->ifr_flags & IFF_UP) == 0) { 809df8bae1dSRodney W. Grimes int s = splimp(); 810df8bae1dSRodney W. Grimes if_down(ifp); 811df8bae1dSRodney W. Grimes splx(s); 812cf4b9371SPoul-Henning Kamp } else if (ifr->ifr_flags & IFF_UP && 813cf4b9371SPoul-Henning Kamp (ifp->if_flags & IFF_UP) == 0) { 814df8bae1dSRodney W. Grimes int s = splimp(); 815df8bae1dSRodney W. Grimes if_up(ifp); 816df8bae1dSRodney W. Grimes splx(s); 817df8bae1dSRodney W. Grimes } 818df8bae1dSRodney W. Grimes ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 819df8bae1dSRodney W. Grimes (ifr->ifr_flags &~ IFF_CANTCHANGE); 820df8bae1dSRodney W. Grimes if (ifp->if_ioctl) 821df8bae1dSRodney W. Grimes (void) (*ifp->if_ioctl)(ifp, cmd, data); 82298b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 823df8bae1dSRodney W. Grimes break; 824df8bae1dSRodney W. Grimes 825df8bae1dSRodney W. Grimes case SIOCSIFMETRIC: 826f711d546SPoul-Henning Kamp error = suser(p); 8279448326fSPoul-Henning Kamp if (error) 828df8bae1dSRodney W. Grimes return (error); 829df8bae1dSRodney W. Grimes ifp->if_metric = ifr->ifr_metric; 83098b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 831df8bae1dSRodney W. Grimes break; 832df8bae1dSRodney W. Grimes 833074c4a4eSGarrett Wollman case SIOCSIFPHYS: 834f711d546SPoul-Henning Kamp error = suser(p); 835e39a0280SGary Palmer if (error) 836e39a0280SGary Palmer return error; 837e39a0280SGary Palmer if (!ifp->if_ioctl) 838e39a0280SGary Palmer return EOPNOTSUPP; 839e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 840e39a0280SGary Palmer if (error == 0) 84198b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 842e39a0280SGary Palmer return(error); 843074c4a4eSGarrett Wollman 844a7028af7SDavid Greenman case SIOCSIFMTU: 84582cd038dSYoshinobu Inoue { 84682cd038dSYoshinobu Inoue u_long oldmtu = ifp->if_mtu; 84782cd038dSYoshinobu Inoue 848f711d546SPoul-Henning Kamp error = suser(p); 8499448326fSPoul-Henning Kamp if (error) 850a7028af7SDavid Greenman return (error); 851a7028af7SDavid Greenman if (ifp->if_ioctl == NULL) 852a7028af7SDavid Greenman return (EOPNOTSUPP); 853aab3beeeSBrian Somers if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 85475ee03cbSDavid Greenman return (EINVAL); 855e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 85648f71763SRuslan Ermilov if (error == 0) { 85798b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 85848f71763SRuslan Ermilov rt_ifmsg(ifp); 85948f71763SRuslan Ermilov } 86082cd038dSYoshinobu Inoue /* 86182cd038dSYoshinobu Inoue * If the link MTU changed, do network layer specific procedure. 86282cd038dSYoshinobu Inoue */ 86382cd038dSYoshinobu Inoue if (ifp->if_mtu != oldmtu) { 86482cd038dSYoshinobu Inoue #ifdef INET6 86582cd038dSYoshinobu Inoue nd6_setmtu(ifp); 86682cd038dSYoshinobu Inoue #endif 86782cd038dSYoshinobu Inoue } 868e39a0280SGary Palmer return (error); 86982cd038dSYoshinobu Inoue } 870a7028af7SDavid Greenman 871df8bae1dSRodney W. Grimes case SIOCADDMULTI: 872df8bae1dSRodney W. Grimes case SIOCDELMULTI: 873f711d546SPoul-Henning Kamp error = suser(p); 8749448326fSPoul-Henning Kamp if (error) 875df8bae1dSRodney W. Grimes return (error); 876477180fbSGarrett Wollman 877477180fbSGarrett Wollman /* Don't allow group membership on non-multicast interfaces. */ 878477180fbSGarrett Wollman if ((ifp->if_flags & IFF_MULTICAST) == 0) 879477180fbSGarrett Wollman return EOPNOTSUPP; 880477180fbSGarrett Wollman 881477180fbSGarrett Wollman /* Don't let users screw up protocols' entries. */ 882477180fbSGarrett Wollman if (ifr->ifr_addr.sa_family != AF_LINK) 883477180fbSGarrett Wollman return EINVAL; 884477180fbSGarrett Wollman 885477180fbSGarrett Wollman if (cmd == SIOCADDMULTI) { 886477180fbSGarrett Wollman struct ifmultiaddr *ifma; 887477180fbSGarrett Wollman error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 888477180fbSGarrett Wollman } else { 889477180fbSGarrett Wollman error = if_delmulti(ifp, &ifr->ifr_addr); 890477180fbSGarrett Wollman } 891e39a0280SGary Palmer if (error == 0) 89298b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 893477180fbSGarrett Wollman return error; 894df8bae1dSRodney W. Grimes 89541b3e8e5SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 89641b3e8e5SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 89741b3e8e5SJun-ichiro itojun Hagino #ifdef INET6 89841b3e8e5SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 89941b3e8e5SJun-ichiro itojun Hagino #endif 900a912e453SPeter Wemm case SIOCSIFMEDIA: 901d7189ec6SJoerg Wunsch case SIOCSIFGENERIC: 902f711d546SPoul-Henning Kamp error = suser(p); 903a912e453SPeter Wemm if (error) 904a912e453SPeter Wemm return (error); 905a912e453SPeter Wemm if (ifp->if_ioctl == 0) 906a912e453SPeter Wemm return (EOPNOTSUPP); 907a912e453SPeter Wemm error = (*ifp->if_ioctl)(ifp, cmd, data); 908a912e453SPeter Wemm if (error == 0) 90998b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 910a912e453SPeter Wemm return error; 911a912e453SPeter Wemm 912413dd0baSPoul-Henning Kamp case SIOCGIFSTATUS: 913413dd0baSPoul-Henning Kamp ifs = (struct ifstat *)data; 914413dd0baSPoul-Henning Kamp ifs->ascii[0] = '\0'; 915413dd0baSPoul-Henning Kamp 916a912e453SPeter Wemm case SIOCGIFMEDIA: 917d7189ec6SJoerg Wunsch case SIOCGIFGENERIC: 918a912e453SPeter Wemm if (ifp->if_ioctl == 0) 919a912e453SPeter Wemm return (EOPNOTSUPP); 920a912e453SPeter Wemm return ((*ifp->if_ioctl)(ifp, cmd, data)); 921a912e453SPeter Wemm 922b106252cSBill Paul case SIOCSIFLLADDR: 923b106252cSBill Paul error = suser(p); 924b106252cSBill Paul if (error) 925b106252cSBill Paul return (error); 92666ce51ceSArchie Cobbs return if_setlladdr(ifp, 92766ce51ceSArchie Cobbs ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 92866ce51ceSArchie Cobbs 929df8bae1dSRodney W. Grimes default: 93082cd038dSYoshinobu Inoue oif_flags = ifp->if_flags; 931df8bae1dSRodney W. Grimes if (so->so_proto == 0) 932df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 933df8bae1dSRodney W. Grimes #ifndef COMPAT_43 93482cd038dSYoshinobu Inoue error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 9352c37256eSGarrett Wollman data, 936bc6d9b80SJoerg Wunsch ifp, p)); 937df8bae1dSRodney W. Grimes #else 938df8bae1dSRodney W. Grimes { 939df8bae1dSRodney W. Grimes int ocmd = cmd; 940df8bae1dSRodney W. Grimes 941df8bae1dSRodney W. Grimes switch (cmd) { 942df8bae1dSRodney W. Grimes 943df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 944df8bae1dSRodney W. Grimes case SIOCSIFADDR: 945df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 946df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 947df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN 948df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_family == 0 && 949df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len < 16) { 950df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 951df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 952df8bae1dSRodney W. Grimes } 953df8bae1dSRodney W. Grimes #else 954df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_len == 0) 955df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 956df8bae1dSRodney W. Grimes #endif 957df8bae1dSRodney W. Grimes break; 958df8bae1dSRodney W. Grimes 959df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 960df8bae1dSRodney W. Grimes cmd = SIOCGIFADDR; 961df8bae1dSRodney W. Grimes break; 962df8bae1dSRodney W. Grimes 963df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 964df8bae1dSRodney W. Grimes cmd = SIOCGIFDSTADDR; 965df8bae1dSRodney W. Grimes break; 966df8bae1dSRodney W. Grimes 967df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 968df8bae1dSRodney W. Grimes cmd = SIOCGIFBRDADDR; 969df8bae1dSRodney W. Grimes break; 970df8bae1dSRodney W. Grimes 971df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 972df8bae1dSRodney W. Grimes cmd = SIOCGIFNETMASK; 973df8bae1dSRodney W. Grimes } 9742c37256eSGarrett Wollman error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 9752c37256eSGarrett Wollman cmd, 9762c37256eSGarrett Wollman data, 977a29f300eSGarrett Wollman ifp, p)); 978df8bae1dSRodney W. Grimes switch (ocmd) { 979df8bae1dSRodney W. Grimes 980df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 981df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 982df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 983df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 984df8bae1dSRodney W. Grimes *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 98582cd038dSYoshinobu Inoue 98682cd038dSYoshinobu Inoue } 98782cd038dSYoshinobu Inoue } 98882cd038dSYoshinobu Inoue #endif /* COMPAT_43 */ 98982cd038dSYoshinobu Inoue 99082cd038dSYoshinobu Inoue if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 99182cd038dSYoshinobu Inoue #ifdef INET6 9923411310dSYoshinobu Inoue DELAY(100);/* XXX: temporal workaround for fxp issue*/ 99382cd038dSYoshinobu Inoue if (ifp->if_flags & IFF_UP) { 99482cd038dSYoshinobu Inoue int s = splimp(); 99582cd038dSYoshinobu Inoue in6_if_up(ifp); 99682cd038dSYoshinobu Inoue splx(s); 99782cd038dSYoshinobu Inoue } 99882cd038dSYoshinobu Inoue #endif 999df8bae1dSRodney W. Grimes } 1000df8bae1dSRodney W. Grimes return (error); 1001df8bae1dSRodney W. Grimes 1002df8bae1dSRodney W. Grimes } 1003df8bae1dSRodney W. Grimes return (0); 1004df8bae1dSRodney W. Grimes } 1005df8bae1dSRodney W. Grimes 1006df8bae1dSRodney W. Grimes /* 1007963e4c2aSGarrett Wollman * Set/clear promiscuous mode on interface ifp based on the truth value 1008963e4c2aSGarrett Wollman * of pswitch. The calls are reference counted so that only the first 1009963e4c2aSGarrett Wollman * "on" request actually has an effect, as does the final "off" request. 1010963e4c2aSGarrett Wollman * Results are undefined if the "off" and "on" requests are not matched. 1011963e4c2aSGarrett Wollman */ 1012963e4c2aSGarrett Wollman int 1013963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch) 1014963e4c2aSGarrett Wollman struct ifnet *ifp; 1015963e4c2aSGarrett Wollman int pswitch; 1016963e4c2aSGarrett Wollman { 1017963e4c2aSGarrett Wollman struct ifreq ifr; 10184a26224cSGarrett Wollman int error; 10194f3c11a6SBill Fenner int oldflags, oldpcount; 1020963e4c2aSGarrett Wollman 10214f3c11a6SBill Fenner oldpcount = ifp->if_pcount; 10222c514a31SBrian Somers oldflags = ifp->if_flags; 1023963e4c2aSGarrett Wollman if (pswitch) { 1024963e4c2aSGarrett Wollman /* 1025963e4c2aSGarrett Wollman * If the device is not configured up, we cannot put it in 1026963e4c2aSGarrett Wollman * promiscuous mode. 1027963e4c2aSGarrett Wollman */ 1028963e4c2aSGarrett Wollman if ((ifp->if_flags & IFF_UP) == 0) 1029963e4c2aSGarrett Wollman return (ENETDOWN); 1030963e4c2aSGarrett Wollman if (ifp->if_pcount++ != 0) 1031963e4c2aSGarrett Wollman return (0); 1032963e4c2aSGarrett Wollman ifp->if_flags |= IFF_PROMISC; 1033963e4c2aSGarrett Wollman } else { 1034963e4c2aSGarrett Wollman if (--ifp->if_pcount > 0) 1035963e4c2aSGarrett Wollman return (0); 1036963e4c2aSGarrett Wollman ifp->if_flags &= ~IFF_PROMISC; 1037963e4c2aSGarrett Wollman } 1038963e4c2aSGarrett Wollman ifr.ifr_flags = ifp->if_flags; 10394a26224cSGarrett Wollman error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 10404f3c11a6SBill Fenner if (error == 0) { 10414f3c11a6SBill Fenner log(LOG_INFO, "%s%d: promiscuous mode %s\n", 10424f3c11a6SBill Fenner ifp->if_name, ifp->if_unit, 10434f3c11a6SBill Fenner (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 10444a26224cSGarrett Wollman rt_ifmsg(ifp); 10454f3c11a6SBill Fenner } else { 10464f3c11a6SBill Fenner ifp->if_pcount = oldpcount; 10472c514a31SBrian Somers ifp->if_flags = oldflags; 10484f3c11a6SBill Fenner } 10494a26224cSGarrett Wollman return error; 1050963e4c2aSGarrett Wollman } 1051963e4c2aSGarrett Wollman 1052963e4c2aSGarrett Wollman /* 1053df8bae1dSRodney W. Grimes * Return interface configuration 1054df8bae1dSRodney W. Grimes * of system. List may be used 1055df8bae1dSRodney W. Grimes * in later ioctl's (above) to get 1056df8bae1dSRodney W. Grimes * other information. 1057df8bae1dSRodney W. Grimes */ 1058df8bae1dSRodney W. Grimes /*ARGSUSED*/ 10593bda9f9bSPoul-Henning Kamp static int 1060df8bae1dSRodney W. Grimes ifconf(cmd, data) 1061ecbb00a2SDoug Rabson u_long cmd; 1062df8bae1dSRodney W. Grimes caddr_t data; 1063df8bae1dSRodney W. Grimes { 1064df8bae1dSRodney W. Grimes register struct ifconf *ifc = (struct ifconf *)data; 106522f29826SPoul-Henning Kamp register struct ifnet *ifp = TAILQ_FIRST(&ifnet); 1066df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 1067df8bae1dSRodney W. Grimes struct ifreq ifr, *ifrp; 1068df8bae1dSRodney W. Grimes int space = ifc->ifc_len, error = 0; 1069df8bae1dSRodney W. Grimes 1070df8bae1dSRodney W. Grimes ifrp = ifc->ifc_req; 107122f29826SPoul-Henning Kamp for (; space > sizeof (ifr) && ifp; ifp = TAILQ_NEXT(ifp, if_link)) { 10721ce9bf88SPoul-Henning Kamp char workbuf[64]; 107375c13541SPoul-Henning Kamp int ifnlen, addrs; 10742624cf89SGarrett Wollman 10752127f260SArchie Cobbs ifnlen = snprintf(workbuf, sizeof(workbuf), 10762127f260SArchie Cobbs "%s%d", ifp->if_name, ifp->if_unit); 10771ce9bf88SPoul-Henning Kamp if(ifnlen + 1 > sizeof ifr.ifr_name) { 10782624cf89SGarrett Wollman error = ENAMETOOLONG; 1079b3f1e629SGuido van Rooij break; 10802624cf89SGarrett Wollman } else { 10811ce9bf88SPoul-Henning Kamp strcpy(ifr.ifr_name, workbuf); 10822624cf89SGarrett Wollman } 10832624cf89SGarrett Wollman 108475c13541SPoul-Henning Kamp addrs = 0; 108522f29826SPoul-Henning Kamp ifa = TAILQ_FIRST(&ifp->if_addrhead); 108659562606SGarrett Wollman for ( ; space > sizeof (ifr) && ifa; 108722f29826SPoul-Henning Kamp ifa = TAILQ_NEXT(ifa, ifa_link)) { 1088df8bae1dSRodney W. Grimes register struct sockaddr *sa = ifa->ifa_addr; 108991421ba2SRobert Watson if (jailed(curproc->p_ucred) && 109091421ba2SRobert Watson prison_if(curproc->p_ucred, sa)) 109175c13541SPoul-Henning Kamp continue; 109275c13541SPoul-Henning Kamp addrs++; 1093df8bae1dSRodney W. Grimes #ifdef COMPAT_43 1094df8bae1dSRodney W. Grimes if (cmd == OSIOCGIFCONF) { 1095df8bae1dSRodney W. Grimes struct osockaddr *osa = 1096df8bae1dSRodney W. Grimes (struct osockaddr *)&ifr.ifr_addr; 1097df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 1098df8bae1dSRodney W. Grimes osa->sa_family = sa->sa_family; 1099df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1100df8bae1dSRodney W. Grimes sizeof (ifr)); 1101df8bae1dSRodney W. Grimes ifrp++; 1102df8bae1dSRodney W. Grimes } else 1103df8bae1dSRodney W. Grimes #endif 1104df8bae1dSRodney W. Grimes if (sa->sa_len <= sizeof(*sa)) { 1105df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 1106df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1107df8bae1dSRodney W. Grimes sizeof (ifr)); 1108df8bae1dSRodney W. Grimes ifrp++; 1109df8bae1dSRodney W. Grimes } else { 1110d91a068eSGuido van Rooij if (space < sizeof (ifr) + sa->sa_len - 1111d91a068eSGuido van Rooij sizeof(*sa)) 1112b3f1e629SGuido van Rooij break; 1113df8bae1dSRodney W. Grimes space -= sa->sa_len - sizeof(*sa); 1114df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1115df8bae1dSRodney W. Grimes sizeof (ifr.ifr_name)); 1116df8bae1dSRodney W. Grimes if (error == 0) 1117df8bae1dSRodney W. Grimes error = copyout((caddr_t)sa, 1118df8bae1dSRodney W. Grimes (caddr_t)&ifrp->ifr_addr, sa->sa_len); 1119df8bae1dSRodney W. Grimes ifrp = (struct ifreq *) 1120df8bae1dSRodney W. Grimes (sa->sa_len + (caddr_t)&ifrp->ifr_addr); 1121df8bae1dSRodney W. Grimes } 1122df8bae1dSRodney W. Grimes if (error) 1123df8bae1dSRodney W. Grimes break; 1124df8bae1dSRodney W. Grimes space -= sizeof (ifr); 1125df8bae1dSRodney W. Grimes } 1126b3f1e629SGuido van Rooij if (error) 1127b3f1e629SGuido van Rooij break; 112875c13541SPoul-Henning Kamp if (!addrs) { 112975c13541SPoul-Henning Kamp bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 113075c13541SPoul-Henning Kamp error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 113175c13541SPoul-Henning Kamp sizeof (ifr)); 113275c13541SPoul-Henning Kamp if (error) 113375c13541SPoul-Henning Kamp break; 1134b3f1e629SGuido van Rooij space -= sizeof (ifr); 1135b3f1e629SGuido van Rooij ifrp++; 113675c13541SPoul-Henning Kamp } 1137df8bae1dSRodney W. Grimes } 1138df8bae1dSRodney W. Grimes ifc->ifc_len -= space; 1139df8bae1dSRodney W. Grimes return (error); 1140df8bae1dSRodney W. Grimes } 1141df8bae1dSRodney W. Grimes 11421158dfb7SGarrett Wollman /* 11431158dfb7SGarrett Wollman * Just like if_promisc(), but for all-multicast-reception mode. 11441158dfb7SGarrett Wollman */ 11451158dfb7SGarrett Wollman int 11461158dfb7SGarrett Wollman if_allmulti(ifp, onswitch) 11471158dfb7SGarrett Wollman struct ifnet *ifp; 11481158dfb7SGarrett Wollman int onswitch; 11491158dfb7SGarrett Wollman { 11501158dfb7SGarrett Wollman int error = 0; 11511158dfb7SGarrett Wollman int s = splimp(); 11521158dfb7SGarrett Wollman 11531158dfb7SGarrett Wollman if (onswitch) { 11541158dfb7SGarrett Wollman if (ifp->if_amcount++ == 0) { 11551158dfb7SGarrett Wollman ifp->if_flags |= IFF_ALLMULTI; 11561158dfb7SGarrett Wollman error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 11571158dfb7SGarrett Wollman } 11581158dfb7SGarrett Wollman } else { 11591158dfb7SGarrett Wollman if (ifp->if_amcount > 1) { 11601158dfb7SGarrett Wollman ifp->if_amcount--; 11611158dfb7SGarrett Wollman } else { 11621158dfb7SGarrett Wollman ifp->if_amcount = 0; 11631158dfb7SGarrett Wollman ifp->if_flags &= ~IFF_ALLMULTI; 11641158dfb7SGarrett Wollman error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 11651158dfb7SGarrett Wollman } 11661158dfb7SGarrett Wollman } 11671158dfb7SGarrett Wollman splx(s); 11684a26224cSGarrett Wollman 11694a26224cSGarrett Wollman if (error == 0) 11704a26224cSGarrett Wollman rt_ifmsg(ifp); 11711158dfb7SGarrett Wollman return error; 11721158dfb7SGarrett Wollman } 11731158dfb7SGarrett Wollman 11741158dfb7SGarrett Wollman /* 11751158dfb7SGarrett Wollman * Add a multicast listenership to the interface in question. 11761158dfb7SGarrett Wollman * The link layer provides a routine which converts 11771158dfb7SGarrett Wollman */ 11781158dfb7SGarrett Wollman int 1179373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma) 11801158dfb7SGarrett Wollman struct ifnet *ifp; /* interface to manipulate */ 11811158dfb7SGarrett Wollman struct sockaddr *sa; /* address to add */ 1182b2053118SGarrett Wollman struct ifmultiaddr **retifma; 11831158dfb7SGarrett Wollman { 11841158dfb7SGarrett Wollman struct sockaddr *llsa, *dupsa; 11851158dfb7SGarrett Wollman int error, s; 11861158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 11871158dfb7SGarrett Wollman 118857af7922SJulian Elischer /* 118957af7922SJulian Elischer * If the matching multicast address already exists 119057af7922SJulian Elischer * then don't add a new one, just add a reference 119157af7922SJulian Elischer */ 11926817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 119357af7922SJulian Elischer if (equal(sa, ifma->ifma_addr)) { 11941158dfb7SGarrett Wollman ifma->ifma_refcount++; 119557af7922SJulian Elischer if (retifma) 119657af7922SJulian Elischer *retifma = ifma; 11971158dfb7SGarrett Wollman return 0; 11981158dfb7SGarrett Wollman } 119957af7922SJulian Elischer } 12001158dfb7SGarrett Wollman 12011158dfb7SGarrett Wollman /* 12021158dfb7SGarrett Wollman * Give the link layer a chance to accept/reject it, and also 12031158dfb7SGarrett Wollman * find out which AF_LINK address this maps to, if it isn't one 12041158dfb7SGarrett Wollman * already. 12051158dfb7SGarrett Wollman */ 12061158dfb7SGarrett Wollman if (ifp->if_resolvemulti) { 12071158dfb7SGarrett Wollman error = ifp->if_resolvemulti(ifp, &llsa, sa); 12081158dfb7SGarrett Wollman if (error) return error; 12091158dfb7SGarrett Wollman } else { 12101158dfb7SGarrett Wollman llsa = 0; 12111158dfb7SGarrett Wollman } 12121158dfb7SGarrett Wollman 12131158dfb7SGarrett Wollman MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); 12141158dfb7SGarrett Wollman MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); 12151158dfb7SGarrett Wollman bcopy(sa, dupsa, sa->sa_len); 12161158dfb7SGarrett Wollman 12171158dfb7SGarrett Wollman ifma->ifma_addr = dupsa; 12181158dfb7SGarrett Wollman ifma->ifma_lladdr = llsa; 12191158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 12201158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 1221373f88edSGarrett Wollman ifma->ifma_protospec = 0; 1222477180fbSGarrett Wollman rt_newmaddrmsg(RTM_NEWMADDR, ifma); 1223373f88edSGarrett Wollman 12241158dfb7SGarrett Wollman /* 12251158dfb7SGarrett Wollman * Some network interfaces can scan the address list at 12261158dfb7SGarrett Wollman * interrupt time; lock them out. 12271158dfb7SGarrett Wollman */ 12281158dfb7SGarrett Wollman s = splimp(); 12296817526dSPoul-Henning Kamp TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 12301158dfb7SGarrett Wollman splx(s); 1231373f88edSGarrett Wollman *retifma = ifma; 12321158dfb7SGarrett Wollman 12331158dfb7SGarrett Wollman if (llsa != 0) { 12346817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 12351158dfb7SGarrett Wollman if (equal(ifma->ifma_addr, llsa)) 12361158dfb7SGarrett Wollman break; 12371158dfb7SGarrett Wollman } 12381158dfb7SGarrett Wollman if (ifma) { 12391158dfb7SGarrett Wollman ifma->ifma_refcount++; 12401158dfb7SGarrett Wollman } else { 12411158dfb7SGarrett Wollman MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, 12421158dfb7SGarrett Wollman M_IFMADDR, M_WAITOK); 1243477180fbSGarrett Wollman MALLOC(dupsa, struct sockaddr *, llsa->sa_len, 1244477180fbSGarrett Wollman M_IFMADDR, M_WAITOK); 1245477180fbSGarrett Wollman bcopy(llsa, dupsa, llsa->sa_len); 1246477180fbSGarrett Wollman ifma->ifma_addr = dupsa; 12471158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 12481158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 12491158dfb7SGarrett Wollman s = splimp(); 12506817526dSPoul-Henning Kamp TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 12511158dfb7SGarrett Wollman splx(s); 12521158dfb7SGarrett Wollman } 125357af7922SJulian Elischer } 12541158dfb7SGarrett Wollman /* 12551158dfb7SGarrett Wollman * We are certain we have added something, so call down to the 12561158dfb7SGarrett Wollman * interface to let them know about it. 12571158dfb7SGarrett Wollman */ 12581158dfb7SGarrett Wollman s = splimp(); 12591158dfb7SGarrett Wollman ifp->if_ioctl(ifp, SIOCADDMULTI, 0); 12601158dfb7SGarrett Wollman splx(s); 12611158dfb7SGarrett Wollman 12621158dfb7SGarrett Wollman return 0; 12631158dfb7SGarrett Wollman } 12641158dfb7SGarrett Wollman 12651158dfb7SGarrett Wollman /* 12661158dfb7SGarrett Wollman * Remove a reference to a multicast address on this interface. Yell 12671158dfb7SGarrett Wollman * if the request does not match an existing membership. 12681158dfb7SGarrett Wollman */ 12691158dfb7SGarrett Wollman int 12701158dfb7SGarrett Wollman if_delmulti(ifp, sa) 12711158dfb7SGarrett Wollman struct ifnet *ifp; 12721158dfb7SGarrett Wollman struct sockaddr *sa; 12731158dfb7SGarrett Wollman { 12741158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 12751158dfb7SGarrett Wollman int s; 12761158dfb7SGarrett Wollman 12776817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 12781158dfb7SGarrett Wollman if (equal(sa, ifma->ifma_addr)) 12791158dfb7SGarrett Wollman break; 12801158dfb7SGarrett Wollman if (ifma == 0) 12811158dfb7SGarrett Wollman return ENOENT; 12821158dfb7SGarrett Wollman 12831158dfb7SGarrett Wollman if (ifma->ifma_refcount > 1) { 12841158dfb7SGarrett Wollman ifma->ifma_refcount--; 12851158dfb7SGarrett Wollman return 0; 12861158dfb7SGarrett Wollman } 12871158dfb7SGarrett Wollman 1288477180fbSGarrett Wollman rt_newmaddrmsg(RTM_DELMADDR, ifma); 12891158dfb7SGarrett Wollman sa = ifma->ifma_lladdr; 12901158dfb7SGarrett Wollman s = splimp(); 12916817526dSPoul-Henning Kamp TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 1292ccb7cc8dSYaroslav Tykhiy /* 1293ccb7cc8dSYaroslav Tykhiy * Make sure the interface driver is notified 1294ccb7cc8dSYaroslav Tykhiy * in the case of a link layer mcast group being left. 1295ccb7cc8dSYaroslav Tykhiy */ 1296ccb7cc8dSYaroslav Tykhiy if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) 1297ccb7cc8dSYaroslav Tykhiy ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 12981158dfb7SGarrett Wollman splx(s); 12991158dfb7SGarrett Wollman free(ifma->ifma_addr, M_IFMADDR); 13001158dfb7SGarrett Wollman free(ifma, M_IFMADDR); 13011158dfb7SGarrett Wollman if (sa == 0) 13021158dfb7SGarrett Wollman return 0; 13031158dfb7SGarrett Wollman 13041158dfb7SGarrett Wollman /* 13051158dfb7SGarrett Wollman * Now look for the link-layer address which corresponds to 13061158dfb7SGarrett Wollman * this network address. It had been squirreled away in 13071158dfb7SGarrett Wollman * ifma->ifma_lladdr for this purpose (so we don't have 13081158dfb7SGarrett Wollman * to call ifp->if_resolvemulti() again), and we saved that 13091158dfb7SGarrett Wollman * value in sa above. If some nasty deleted the 13101158dfb7SGarrett Wollman * link-layer address out from underneath us, we can deal because 13111158dfb7SGarrett Wollman * the address we stored was is not the same as the one which was 13121158dfb7SGarrett Wollman * in the record for the link-layer address. (So we don't complain 13131158dfb7SGarrett Wollman * in that case.) 13141158dfb7SGarrett Wollman */ 13156817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 13161158dfb7SGarrett Wollman if (equal(sa, ifma->ifma_addr)) 13171158dfb7SGarrett Wollman break; 13181158dfb7SGarrett Wollman if (ifma == 0) 13191158dfb7SGarrett Wollman return 0; 13201158dfb7SGarrett Wollman 13211158dfb7SGarrett Wollman if (ifma->ifma_refcount > 1) { 13221158dfb7SGarrett Wollman ifma->ifma_refcount--; 13231158dfb7SGarrett Wollman return 0; 13241158dfb7SGarrett Wollman } 13251158dfb7SGarrett Wollman 13261158dfb7SGarrett Wollman s = splimp(); 13276817526dSPoul-Henning Kamp TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 1328c7323482SBill Paul ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 13291158dfb7SGarrett Wollman splx(s); 13301158dfb7SGarrett Wollman free(ifma->ifma_addr, M_IFMADDR); 13311158dfb7SGarrett Wollman free(sa, M_IFMADDR); 13321158dfb7SGarrett Wollman free(ifma, M_IFMADDR); 13331158dfb7SGarrett Wollman 13341158dfb7SGarrett Wollman return 0; 13351158dfb7SGarrett Wollman } 13361158dfb7SGarrett Wollman 133766ce51ceSArchie Cobbs /* 133866ce51ceSArchie Cobbs * Set the link layer address on an interface. 133966ce51ceSArchie Cobbs * 134066ce51ceSArchie Cobbs * At this time we only support certain types of interfaces, 134166ce51ceSArchie Cobbs * and we don't allow the length of the address to change. 134266ce51ceSArchie Cobbs */ 134366ce51ceSArchie Cobbs int 134466ce51ceSArchie Cobbs if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 134566ce51ceSArchie Cobbs { 134666ce51ceSArchie Cobbs struct sockaddr_dl *sdl; 134766ce51ceSArchie Cobbs struct ifaddr *ifa; 134866ce51ceSArchie Cobbs 134966ce51ceSArchie Cobbs ifa = ifnet_addrs[ifp->if_index - 1]; 135066ce51ceSArchie Cobbs if (ifa == NULL) 135166ce51ceSArchie Cobbs return (EINVAL); 135266ce51ceSArchie Cobbs sdl = (struct sockaddr_dl *)ifa->ifa_addr; 135366ce51ceSArchie Cobbs if (sdl == NULL) 135466ce51ceSArchie Cobbs return (EINVAL); 135566ce51ceSArchie Cobbs if (len != sdl->sdl_alen) /* don't allow length to change */ 135666ce51ceSArchie Cobbs return (EINVAL); 135766ce51ceSArchie Cobbs switch (ifp->if_type) { 135866ce51ceSArchie Cobbs case IFT_ETHER: /* these types use struct arpcom */ 135966ce51ceSArchie Cobbs case IFT_FDDI: 136066ce51ceSArchie Cobbs case IFT_XETHER: 136166ce51ceSArchie Cobbs case IFT_ISO88025: 1362b7bffa71SYaroslav Tykhiy case IFT_L2VLAN: 136366ce51ceSArchie Cobbs bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len); 136466ce51ceSArchie Cobbs bcopy(lladdr, LLADDR(sdl), len); 136566ce51ceSArchie Cobbs break; 136666ce51ceSArchie Cobbs default: 136766ce51ceSArchie Cobbs return (ENODEV); 136866ce51ceSArchie Cobbs } 136966ce51ceSArchie Cobbs /* 137066ce51ceSArchie Cobbs * If the interface is already up, we need 137166ce51ceSArchie Cobbs * to re-init it in order to reprogram its 137266ce51ceSArchie Cobbs * address filter. 137366ce51ceSArchie Cobbs */ 137466ce51ceSArchie Cobbs if ((ifp->if_flags & IFF_UP) != 0) { 137566ce51ceSArchie Cobbs ifp->if_flags &= ~IFF_UP; 137666ce51ceSArchie Cobbs (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL); 137766ce51ceSArchie Cobbs ifp->if_flags |= IFF_UP; 137866ce51ceSArchie Cobbs (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL); 137966ce51ceSArchie Cobbs } 138066ce51ceSArchie Cobbs return (0); 138166ce51ceSArchie Cobbs } 138266ce51ceSArchie Cobbs 1383373f88edSGarrett Wollman struct ifmultiaddr * 1384373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp) 1385373f88edSGarrett Wollman struct sockaddr *sa; 1386373f88edSGarrett Wollman struct ifnet *ifp; 1387373f88edSGarrett Wollman { 1388373f88edSGarrett Wollman struct ifmultiaddr *ifma; 1389373f88edSGarrett Wollman 13906817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1391373f88edSGarrett Wollman if (equal(ifma->ifma_addr, sa)) 1392373f88edSGarrett Wollman break; 1393373f88edSGarrett Wollman 1394373f88edSGarrett Wollman return ifma; 1395373f88edSGarrett Wollman } 1396373f88edSGarrett Wollman 1397602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 13982c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 1399