182f39c91SKip Macy /* 282f39c91SKip Macy * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved. 382f39c91SKip Macy * Copyright (c) 2004-2008 Qing Li. All rights reserved. 482f39c91SKip Macy * Copyright (c) 2008 Kip Macy. All rights reserved. 582f39c91SKip Macy * 682f39c91SKip Macy * Redistribution and use in source and binary forms, with or without 782f39c91SKip Macy * modification, are permitted provided that the following conditions 882f39c91SKip Macy * are met: 982f39c91SKip Macy * 1. Redistributions of source code must retain the above copyright 1082f39c91SKip Macy * notice, this list of conditions and the following disclaimer. 1182f39c91SKip Macy * 2. Redistributions in binary form must reproduce the above copyright 1282f39c91SKip Macy * notice, this list of conditions and the following disclaimer in the 1382f39c91SKip Macy * documentation and/or other materials provided with the distribution. 1482f39c91SKip Macy * 1582f39c91SKip Macy * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1682f39c91SKip Macy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1782f39c91SKip Macy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1882f39c91SKip Macy * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 1982f39c91SKip Macy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2082f39c91SKip Macy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2182f39c91SKip Macy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2282f39c91SKip Macy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2382f39c91SKip Macy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2482f39c91SKip Macy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2582f39c91SKip Macy * SUCH DAMAGE. 2682f39c91SKip Macy */ 2782f39c91SKip Macy #include <sys/cdefs.h> 2882f39c91SKip Macy __FBSDID("$FreeBSD$"); 2982f39c91SKip Macy 3082f39c91SKip Macy #include "opt_inet.h" 3182f39c91SKip Macy #include "opt_inet6.h" 3282f39c91SKip Macy 3382f39c91SKip Macy #include <sys/param.h> 3482f39c91SKip Macy #include <sys/systm.h> 3582f39c91SKip Macy #include <sys/malloc.h> 3682f39c91SKip Macy #include <sys/mbuf.h> 3782f39c91SKip Macy #include <sys/syslog.h> 3882f39c91SKip Macy #include <sys/sysctl.h> 3982f39c91SKip Macy #include <sys/socket.h> 4082f39c91SKip Macy #include <sys/kernel.h> 4182f39c91SKip Macy #include <sys/lock.h> 4282f39c91SKip Macy #include <sys/mutex.h> 4382f39c91SKip Macy #include <sys/rwlock.h> 4482f39c91SKip Macy #include <sys/vimage.h> 4582f39c91SKip Macy 4682f39c91SKip Macy #include <vm/uma.h> 4782f39c91SKip Macy 4882f39c91SKip Macy #include <netinet/in.h> 4982f39c91SKip Macy #include <net/if_llatbl.h> 5082f39c91SKip Macy #include <net/if.h> 5182f39c91SKip Macy #include <net/if_dl.h> 5282f39c91SKip Macy #include <net/if_var.h> 5382f39c91SKip Macy #include <net/route.h> 5482f39c91SKip Macy #include <netinet/if_ether.h> 5582f39c91SKip Macy #include <netinet6/in6_var.h> 5682f39c91SKip Macy #include <netinet6/nd6.h> 5782f39c91SKip Macy 5882f39c91SKip Macy MALLOC_DEFINE(M_LLTABLE, "lltable", "link level address tables"); 5982f39c91SKip Macy 6082f39c91SKip Macy static SLIST_HEAD(, lltable) lltables = SLIST_HEAD_INITIALIZER(lltables); 6182f39c91SKip Macy 6282f39c91SKip Macy extern void arprequest(struct ifnet *, struct in_addr *, struct in_addr *, 6382f39c91SKip Macy u_char *); 6482f39c91SKip Macy 6582f39c91SKip Macy /* 6682f39c91SKip Macy * Dump arp state for a specific address family. 6782f39c91SKip Macy */ 6882f39c91SKip Macy int 6982f39c91SKip Macy lltable_sysctl_dumparp(int af, struct sysctl_req *wr) 7082f39c91SKip Macy { 7182f39c91SKip Macy struct lltable *llt; 7282f39c91SKip Macy int error = 0; 7382f39c91SKip Macy 7482f39c91SKip Macy IFNET_RLOCK(); 7582f39c91SKip Macy SLIST_FOREACH(llt, &lltables, llt_link) { 7682f39c91SKip Macy if (llt->llt_af == af) { 7782f39c91SKip Macy error = llt->llt_dump(llt, wr); 7882f39c91SKip Macy if (error != 0) 7982f39c91SKip Macy goto done; 8082f39c91SKip Macy } 8182f39c91SKip Macy } 8282f39c91SKip Macy done: 8382f39c91SKip Macy IFNET_RUNLOCK(); 8482f39c91SKip Macy return (error); 8582f39c91SKip Macy } 8682f39c91SKip Macy 8782f39c91SKip Macy /* 8882f39c91SKip Macy * Deletes an address from the address table. 8982f39c91SKip Macy * This function is called by the timer functions 9082f39c91SKip Macy * such as arptimer() and nd6_llinfo_timer(), and 9182f39c91SKip Macy * the caller does the locking. 9282f39c91SKip Macy */ 9382f39c91SKip Macy void 9482f39c91SKip Macy llentry_free(struct llentry *lle) 9582f39c91SKip Macy { 9682f39c91SKip Macy 9782f39c91SKip Macy LLE_WLOCK_ASSERT(lle); 9882f39c91SKip Macy LIST_REMOVE(lle, lle_next); 9982f39c91SKip Macy 10082f39c91SKip Macy if (lle->la_hold != NULL) 10182f39c91SKip Macy m_freem(lle->la_hold); 10282f39c91SKip Macy 10382f39c91SKip Macy LLE_FREE_LOCKED(lle); 10482f39c91SKip Macy } 10582f39c91SKip Macy 106c8da95acSKip Macy int 107c8da95acSKip Macy llentry_update(struct llentry **llep, struct lltable *lt, 108c8da95acSKip Macy struct sockaddr *dst, struct ifnet *ifp) 109c8da95acSKip Macy { 110c8da95acSKip Macy struct llentry *la; 111c8da95acSKip Macy 112c8da95acSKip Macy IF_AFDATA_RLOCK(ifp); 113c8da95acSKip Macy la = lla_lookup(lt, LLE_EXCLUSIVE, 114c8da95acSKip Macy (struct sockaddr *)dst); 115c8da95acSKip Macy IF_AFDATA_RUNLOCK(ifp); 116c8da95acSKip Macy if ((la == NULL) && 117c8da95acSKip Macy (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) { 118c8da95acSKip Macy IF_AFDATA_WLOCK(ifp); 119c8da95acSKip Macy la = lla_lookup(lt, 120c8da95acSKip Macy (LLE_CREATE | LLE_EXCLUSIVE), 121c8da95acSKip Macy (struct sockaddr *)dst); 122c8da95acSKip Macy IF_AFDATA_WUNLOCK(ifp); 123c8da95acSKip Macy } 124c8da95acSKip Macy if (la != NULL && (*llep != la)) { 125c8da95acSKip Macy if (*llep != NULL) 126c8da95acSKip Macy LLE_FREE(*llep); 127c8da95acSKip Macy LLE_ADDREF(la); 128c8da95acSKip Macy LLE_WUNLOCK(la); 129c8da95acSKip Macy *llep = la; 130c8da95acSKip Macy } else if (la != NULL) 131c8da95acSKip Macy LLE_WUNLOCK(la); 132c8da95acSKip Macy 133c8da95acSKip Macy if (la == NULL) 134c8da95acSKip Macy return (ENOENT); 135c8da95acSKip Macy 136c8da95acSKip Macy return (0); 137c8da95acSKip Macy } 138c8da95acSKip Macy 13982f39c91SKip Macy /* 14082f39c91SKip Macy * Free all entries from given table and free itself. 14182f39c91SKip Macy * Since lltables collects from all of the intefaces, 14282f39c91SKip Macy * the caller of this function must acquire IFNET_WLOCK(). 14382f39c91SKip Macy */ 14482f39c91SKip Macy void 14582f39c91SKip Macy lltable_free(struct lltable *llt) 14682f39c91SKip Macy { 14782f39c91SKip Macy struct llentry *lle, *next; 14882f39c91SKip Macy int i; 14982f39c91SKip Macy 15082f39c91SKip Macy KASSERT(llt != NULL, ("%s: llt is NULL", __func__)); 15182f39c91SKip Macy 15282f39c91SKip Macy IFNET_WLOCK(); 15382f39c91SKip Macy SLIST_REMOVE(&lltables, llt, lltable, llt_link); 15482f39c91SKip Macy IFNET_WUNLOCK(); 15582f39c91SKip Macy 15682f39c91SKip Macy for (i=0; i < LLTBL_HASHTBL_SIZE; i++) { 15782f39c91SKip Macy LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 15882f39c91SKip Macy 15982f39c91SKip Macy callout_drain(&lle->la_timer); 16082f39c91SKip Macy LLE_WLOCK(lle); 16182f39c91SKip Macy llentry_free(lle); 16282f39c91SKip Macy } 16382f39c91SKip Macy } 16482f39c91SKip Macy 16582f39c91SKip Macy free(llt, M_LLTABLE); 16682f39c91SKip Macy } 16782f39c91SKip Macy 16882f39c91SKip Macy void 16982f39c91SKip Macy lltable_drain(int af) 17082f39c91SKip Macy { 17182f39c91SKip Macy struct lltable *llt; 17282f39c91SKip Macy struct llentry *lle; 17382f39c91SKip Macy register int i; 17482f39c91SKip Macy 17582f39c91SKip Macy IFNET_RLOCK(); 17682f39c91SKip Macy SLIST_FOREACH(llt, &lltables, llt_link) { 17782f39c91SKip Macy if (llt->llt_af != af) 17882f39c91SKip Macy continue; 17982f39c91SKip Macy 18082f39c91SKip Macy for (i=0; i < LLTBL_HASHTBL_SIZE; i++) { 18182f39c91SKip Macy LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 18282f39c91SKip Macy if (lle->la_hold) { 18382f39c91SKip Macy m_freem(lle->la_hold); 18482f39c91SKip Macy lle->la_hold = NULL; 18582f39c91SKip Macy } 18682f39c91SKip Macy } 18782f39c91SKip Macy } 18882f39c91SKip Macy } 18982f39c91SKip Macy IFNET_RUNLOCK(); 19082f39c91SKip Macy } 19182f39c91SKip Macy 19282f39c91SKip Macy /* 19382f39c91SKip Macy * Create a new lltable. 19482f39c91SKip Macy */ 19582f39c91SKip Macy struct lltable * 19682f39c91SKip Macy lltable_init(struct ifnet *ifp, int af) 19782f39c91SKip Macy { 19882f39c91SKip Macy struct lltable *llt; 19982f39c91SKip Macy register int i; 20082f39c91SKip Macy 20182f39c91SKip Macy llt = malloc(sizeof(struct lltable), M_LLTABLE, M_WAITOK); 20282f39c91SKip Macy if (llt == NULL) 20382f39c91SKip Macy return (NULL); 20482f39c91SKip Macy 20582f39c91SKip Macy llt->llt_af = af; 20682f39c91SKip Macy llt->llt_ifp = ifp; 20782f39c91SKip Macy for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) 20882f39c91SKip Macy LIST_INIT(&llt->lle_head[i]); 20982f39c91SKip Macy 21082f39c91SKip Macy IFNET_WLOCK(); 21182f39c91SKip Macy SLIST_INSERT_HEAD(&lltables, llt, llt_link); 21282f39c91SKip Macy IFNET_WUNLOCK(); 21382f39c91SKip Macy 21482f39c91SKip Macy return (llt); 21582f39c91SKip Macy } 21682f39c91SKip Macy 21782f39c91SKip Macy /* 21882f39c91SKip Macy * Called in route_output when adding/deleting a route to an interface. 21982f39c91SKip Macy */ 22082f39c91SKip Macy int 22182f39c91SKip Macy lla_rt_output(struct rt_msghdr *rtm, struct rt_addrinfo *info) 22282f39c91SKip Macy { 22382f39c91SKip Macy struct sockaddr_dl *dl = 22482f39c91SKip Macy (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY]; 22582f39c91SKip Macy struct sockaddr *dst = (struct sockaddr *)info->rti_info[RTAX_DST]; 22682f39c91SKip Macy struct ifnet *ifp; 22782f39c91SKip Macy struct lltable *llt; 22882f39c91SKip Macy struct llentry *lle; 22982f39c91SKip Macy u_int laflags = 0, flags = 0; 23082f39c91SKip Macy int error = 0; 23182f39c91SKip Macy 23282f39c91SKip Macy if (dl == NULL || dl->sdl_family != AF_LINK) { 23382f39c91SKip Macy log(LOG_INFO, "%s: invalid dl\n", __func__); 23482f39c91SKip Macy return EINVAL; 23582f39c91SKip Macy } 23682f39c91SKip Macy ifp = ifnet_byindex(dl->sdl_index); 23782f39c91SKip Macy if (ifp == NULL) { 23882f39c91SKip Macy log(LOG_INFO, "%s: invalid ifp (sdl_index %d)\n", 23982f39c91SKip Macy __func__, dl->sdl_index); 24082f39c91SKip Macy return EINVAL; 24182f39c91SKip Macy } 24282f39c91SKip Macy 24382f39c91SKip Macy switch (rtm->rtm_type) { 24482f39c91SKip Macy case RTM_ADD: 24582f39c91SKip Macy if (rtm->rtm_flags & RTF_ANNOUNCE) { 24682f39c91SKip Macy flags |= LLE_PUB; 24782f39c91SKip Macy #ifdef INET 24882f39c91SKip Macy if (dst->sa_family == AF_INET && 24982f39c91SKip Macy ((struct sockaddr_inarp *)dst)->sin_other != 0) { 25082f39c91SKip Macy struct rtentry *rt = rtalloc1(dst, 0, 0); 25182f39c91SKip Macy if (rt == NULL || !(rt->rt_flags & RTF_HOST)) { 25282f39c91SKip Macy log(LOG_INFO, "%s: RTM_ADD publish " 25382f39c91SKip Macy "(proxy only) is invalid\n", 25482f39c91SKip Macy __func__); 2552e730beaSBjoern A. Zeeb if (rt) 2562e730beaSBjoern A. Zeeb RTFREE_LOCKED(rt); 25782f39c91SKip Macy return EINVAL; 25882f39c91SKip Macy } 2592e730beaSBjoern A. Zeeb RTFREE_LOCKED(rt); 26082f39c91SKip Macy 26182f39c91SKip Macy flags |= LLE_PROXY; 26282f39c91SKip Macy } 26382f39c91SKip Macy #endif 26482f39c91SKip Macy } 26582f39c91SKip Macy flags |= LLE_CREATE; 26682f39c91SKip Macy break; 26782f39c91SKip Macy 26882f39c91SKip Macy case RTM_DELETE: 26982f39c91SKip Macy flags |= LLE_DELETE; 27082f39c91SKip Macy break; 27182f39c91SKip Macy 27282f39c91SKip Macy case RTM_CHANGE: 27382f39c91SKip Macy break; 27482f39c91SKip Macy 27582f39c91SKip Macy default: 27682f39c91SKip Macy return EINVAL; /* XXX not implemented yet */ 27782f39c91SKip Macy } 27882f39c91SKip Macy 27982f39c91SKip Macy /* XXX linked list may be too expensive */ 28082f39c91SKip Macy IFNET_RLOCK(); 28182f39c91SKip Macy SLIST_FOREACH(llt, &lltables, llt_link) { 28282f39c91SKip Macy if (llt->llt_af == dst->sa_family && 28382f39c91SKip Macy llt->llt_ifp == ifp) 28482f39c91SKip Macy break; 28582f39c91SKip Macy } 28682f39c91SKip Macy IFNET_RUNLOCK(); 28782f39c91SKip Macy KASSERT(llt != NULL, ("Yep, ugly hacks are bad\n")); 28882f39c91SKip Macy 28982f39c91SKip Macy if (flags && LLE_CREATE) 29082f39c91SKip Macy flags |= LLE_EXCLUSIVE; 29182f39c91SKip Macy 29282f39c91SKip Macy IF_AFDATA_LOCK(ifp); 29382f39c91SKip Macy lle = lla_lookup(llt, flags, dst); 29482f39c91SKip Macy IF_AFDATA_UNLOCK(ifp); 29582f39c91SKip Macy if (LLE_IS_VALID(lle)) { 29682f39c91SKip Macy if (flags & LLE_CREATE) { 29782f39c91SKip Macy /* 29882f39c91SKip Macy * If we delay the delete, then a subsequent 29982f39c91SKip Macy * "arp add" should look up this entry, reset the 30082f39c91SKip Macy * LLE_DELETED flag, and reset the expiration timer 30182f39c91SKip Macy */ 30282f39c91SKip Macy bcopy(LLADDR(dl), &lle->ll_addr, ifp->if_addrlen); 30382f39c91SKip Macy lle->la_flags |= LLE_VALID; 30482f39c91SKip Macy lle->la_flags &= ~LLE_DELETED; 30582f39c91SKip Macy #ifdef INET6 30682f39c91SKip Macy /* 30782f39c91SKip Macy * ND6 30882f39c91SKip Macy */ 30982f39c91SKip Macy if (dst->sa_family == AF_INET6) 31082f39c91SKip Macy lle->ln_state = ND6_LLINFO_REACHABLE; 31182f39c91SKip Macy #endif 31282f39c91SKip Macy /* 31382f39c91SKip Macy * NB: arp and ndp always set (RTF_STATIC | RTF_HOST) 31482f39c91SKip Macy */ 31582f39c91SKip Macy 31682f39c91SKip Macy if (rtm->rtm_rmx.rmx_expire == 0) { 31782f39c91SKip Macy lle->la_flags |= LLE_STATIC; 31882f39c91SKip Macy lle->la_expire = 0; 31982f39c91SKip Macy } else 32082f39c91SKip Macy lle->la_expire = rtm->rtm_rmx.rmx_expire; 32182f39c91SKip Macy laflags = lle->la_flags; 32282f39c91SKip Macy LLE_WUNLOCK(lle); 32382f39c91SKip Macy #ifdef INET 3247b4d716bSKip Macy /* gratuitous ARP */ 32582f39c91SKip Macy if ((laflags & LLE_PUB) && dst->sa_family == AF_INET) { 32682f39c91SKip Macy arprequest(ifp, 32782f39c91SKip Macy &((struct sockaddr_in *)dst)->sin_addr, 32882f39c91SKip Macy &((struct sockaddr_in *)dst)->sin_addr, 32982f39c91SKip Macy ((laflags & LLE_PROXY) ? 33082f39c91SKip Macy (u_char *)IF_LLADDR(ifp) : 33182f39c91SKip Macy (u_char *)LLADDR(dl))); 33282f39c91SKip Macy } 33382f39c91SKip Macy #endif 33482f39c91SKip Macy } else { 33582f39c91SKip Macy if (flags & LLE_EXCLUSIVE) 33682f39c91SKip Macy LLE_WUNLOCK(lle); 33782f39c91SKip Macy else 33882f39c91SKip Macy LLE_RUNLOCK(lle); 33982f39c91SKip Macy } 34082f39c91SKip Macy } else if ((lle == NULL) && (flags & LLE_DELETE)) 34182f39c91SKip Macy error = EINVAL; 34282f39c91SKip Macy 34382f39c91SKip Macy 34482f39c91SKip Macy return (error); 34582f39c91SKip Macy } 346