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 4582f39c91SKip Macy #include <vm/uma.h> 4682f39c91SKip Macy 4782f39c91SKip Macy #include <netinet/in.h> 4882f39c91SKip Macy #include <net/if_llatbl.h> 4982f39c91SKip Macy #include <net/if.h> 5082f39c91SKip Macy #include <net/if_dl.h> 5182f39c91SKip Macy #include <net/if_var.h> 5282f39c91SKip Macy #include <net/route.h> 53530c0060SRobert Watson #include <net/vnet.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 60989e0411SMarko Zec static VNET_DEFINE(SLIST_HEAD(, lltable), lltables); 61989e0411SMarko Zec #define V_lltables VNET(lltables) 6282f39c91SKip Macy 6382f39c91SKip Macy extern void arprequest(struct ifnet *, struct in_addr *, struct in_addr *, 6482f39c91SKip Macy u_char *); 6582f39c91SKip Macy 66989e0411SMarko Zec static void vnet_lltable_init(void); 67989e0411SMarko Zec 68dc56e98fSRobert Watson struct rwlock lltable_rwlock; 69dc56e98fSRobert Watson RW_SYSINIT(lltable_rwlock, &lltable_rwlock, "lltable_rwlock"); 70dc56e98fSRobert Watson 7182f39c91SKip Macy /* 7282f39c91SKip Macy * Dump arp state for a specific address family. 7382f39c91SKip Macy */ 7482f39c91SKip Macy int 7582f39c91SKip Macy lltable_sysctl_dumparp(int af, struct sysctl_req *wr) 7682f39c91SKip Macy { 7782f39c91SKip Macy struct lltable *llt; 7882f39c91SKip Macy int error = 0; 7982f39c91SKip Macy 80dc56e98fSRobert Watson LLTABLE_RLOCK(); 81989e0411SMarko Zec SLIST_FOREACH(llt, &V_lltables, llt_link) { 8282f39c91SKip Macy if (llt->llt_af == af) { 8382f39c91SKip Macy error = llt->llt_dump(llt, wr); 8482f39c91SKip Macy if (error != 0) 8582f39c91SKip Macy goto done; 8682f39c91SKip Macy } 8782f39c91SKip Macy } 8882f39c91SKip Macy done: 89dc56e98fSRobert Watson LLTABLE_RUNLOCK(); 9082f39c91SKip Macy return (error); 9182f39c91SKip Macy } 9282f39c91SKip Macy 9382f39c91SKip Macy /* 9482f39c91SKip Macy * Deletes an address from the address table. 9582f39c91SKip Macy * This function is called by the timer functions 9682f39c91SKip Macy * such as arptimer() and nd6_llinfo_timer(), and 9782f39c91SKip Macy * the caller does the locking. 9882f39c91SKip Macy */ 9982f39c91SKip Macy void 10082f39c91SKip Macy llentry_free(struct llentry *lle) 10182f39c91SKip Macy { 10282f39c91SKip Macy 10382f39c91SKip Macy LLE_WLOCK_ASSERT(lle); 10482f39c91SKip Macy LIST_REMOVE(lle, lle_next); 10582f39c91SKip Macy 10682f39c91SKip Macy if (lle->la_hold != NULL) 10782f39c91SKip Macy m_freem(lle->la_hold); 10882f39c91SKip Macy 10982f39c91SKip Macy LLE_FREE_LOCKED(lle); 11082f39c91SKip Macy } 11182f39c91SKip Macy 112adfc35ffSKip Macy /* 113adfc35ffSKip Macy * Update an llentry for address dst (equivalent to rtalloc for new-arp) 114adfc35ffSKip Macy * Caller must pass in a valid struct llentry * 115e94ba2ceSKip Macy * 116e94ba2ceSKip Macy * if found the llentry * is returned referenced and unlocked 117adfc35ffSKip Macy */ 118c8da95acSKip Macy int 119c8da95acSKip Macy llentry_update(struct llentry **llep, struct lltable *lt, 120c8da95acSKip Macy struct sockaddr *dst, struct ifnet *ifp) 121c8da95acSKip Macy { 122c8da95acSKip Macy struct llentry *la; 123c8da95acSKip Macy 124c8da95acSKip Macy IF_AFDATA_RLOCK(ifp); 125c8da95acSKip Macy la = lla_lookup(lt, LLE_EXCLUSIVE, 126c8da95acSKip Macy (struct sockaddr *)dst); 127c8da95acSKip Macy IF_AFDATA_RUNLOCK(ifp); 128c8da95acSKip Macy if ((la == NULL) && 129c8da95acSKip Macy (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) { 130c8da95acSKip Macy IF_AFDATA_WLOCK(ifp); 131c8da95acSKip Macy la = lla_lookup(lt, 132c8da95acSKip Macy (LLE_CREATE | LLE_EXCLUSIVE), 133c8da95acSKip Macy (struct sockaddr *)dst); 134c8da95acSKip Macy IF_AFDATA_WUNLOCK(ifp); 135c8da95acSKip Macy } 136c8da95acSKip Macy if (la != NULL && (*llep != la)) { 137c8da95acSKip Macy if (*llep != NULL) 138c8da95acSKip Macy LLE_FREE(*llep); 139c8da95acSKip Macy LLE_ADDREF(la); 140c8da95acSKip Macy LLE_WUNLOCK(la); 141c8da95acSKip Macy *llep = la; 142c8da95acSKip Macy } else if (la != NULL) 143c8da95acSKip Macy LLE_WUNLOCK(la); 144c8da95acSKip Macy 145c8da95acSKip Macy if (la == NULL) 146c8da95acSKip Macy return (ENOENT); 147c8da95acSKip Macy 148c8da95acSKip Macy return (0); 149c8da95acSKip Macy } 150c8da95acSKip Macy 15182f39c91SKip Macy /* 15282f39c91SKip Macy * Free all entries from given table and free itself. 15382f39c91SKip Macy */ 15482f39c91SKip Macy void 15582f39c91SKip Macy lltable_free(struct lltable *llt) 15682f39c91SKip Macy { 15782f39c91SKip Macy struct llentry *lle, *next; 15882f39c91SKip Macy int i; 15982f39c91SKip Macy 16082f39c91SKip Macy KASSERT(llt != NULL, ("%s: llt is NULL", __func__)); 16182f39c91SKip Macy 162dc56e98fSRobert Watson LLTABLE_WLOCK(); 163989e0411SMarko Zec SLIST_REMOVE(&V_lltables, llt, lltable, llt_link); 164dc56e98fSRobert Watson LLTABLE_WUNLOCK(); 16582f39c91SKip Macy 16682f39c91SKip Macy for (i=0; i < LLTBL_HASHTBL_SIZE; i++) { 16782f39c91SKip Macy LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 16882f39c91SKip Macy 16982f39c91SKip Macy callout_drain(&lle->la_timer); 17082f39c91SKip Macy LLE_WLOCK(lle); 17182f39c91SKip Macy llentry_free(lle); 17282f39c91SKip Macy } 17382f39c91SKip Macy } 17482f39c91SKip Macy 17582f39c91SKip Macy free(llt, M_LLTABLE); 17682f39c91SKip Macy } 17782f39c91SKip Macy 17882f39c91SKip Macy void 17982f39c91SKip Macy lltable_drain(int af) 18082f39c91SKip Macy { 18182f39c91SKip Macy struct lltable *llt; 18282f39c91SKip Macy struct llentry *lle; 18382f39c91SKip Macy register int i; 18482f39c91SKip Macy 185dc56e98fSRobert Watson LLTABLE_RLOCK(); 186989e0411SMarko Zec SLIST_FOREACH(llt, &V_lltables, llt_link) { 18782f39c91SKip Macy if (llt->llt_af != af) 18882f39c91SKip Macy continue; 18982f39c91SKip Macy 19082f39c91SKip Macy for (i=0; i < LLTBL_HASHTBL_SIZE; i++) { 19182f39c91SKip Macy LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 19282f39c91SKip Macy if (lle->la_hold) { 19382f39c91SKip Macy m_freem(lle->la_hold); 19482f39c91SKip Macy lle->la_hold = NULL; 19582f39c91SKip Macy } 19682f39c91SKip Macy } 19782f39c91SKip Macy } 19882f39c91SKip Macy } 199dc56e98fSRobert Watson LLTABLE_RUNLOCK(); 20082f39c91SKip Macy } 20182f39c91SKip Macy 202c9d763bfSQing Li void 203c9d763bfSQing Li lltable_prefix_free(int af, struct sockaddr *prefix, struct sockaddr *mask) 204c9d763bfSQing Li { 205c9d763bfSQing Li struct lltable *llt; 206c9d763bfSQing Li 207dc56e98fSRobert Watson LLTABLE_RLOCK(); 208989e0411SMarko Zec SLIST_FOREACH(llt, &V_lltables, llt_link) { 209c9d763bfSQing Li if (llt->llt_af != af) 210c9d763bfSQing Li continue; 211c9d763bfSQing Li 212c9d763bfSQing Li llt->llt_prefix_free(llt, prefix, mask); 213c9d763bfSQing Li } 214dc56e98fSRobert Watson LLTABLE_RUNLOCK(); 215c9d763bfSQing Li } 216c9d763bfSQing Li 217c9d763bfSQing Li 218c9d763bfSQing Li 21982f39c91SKip Macy /* 22082f39c91SKip Macy * Create a new lltable. 22182f39c91SKip Macy */ 22282f39c91SKip Macy struct lltable * 22382f39c91SKip Macy lltable_init(struct ifnet *ifp, int af) 22482f39c91SKip Macy { 22582f39c91SKip Macy struct lltable *llt; 22682f39c91SKip Macy register int i; 22782f39c91SKip Macy 22882f39c91SKip Macy llt = malloc(sizeof(struct lltable), M_LLTABLE, M_WAITOK); 22982f39c91SKip Macy if (llt == NULL) 23082f39c91SKip Macy return (NULL); 23182f39c91SKip Macy 23282f39c91SKip Macy llt->llt_af = af; 23382f39c91SKip Macy llt->llt_ifp = ifp; 23482f39c91SKip Macy for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) 23582f39c91SKip Macy LIST_INIT(&llt->lle_head[i]); 23682f39c91SKip Macy 237dc56e98fSRobert Watson LLTABLE_WLOCK(); 238989e0411SMarko Zec SLIST_INSERT_HEAD(&V_lltables, llt, llt_link); 239dc56e98fSRobert Watson LLTABLE_WUNLOCK(); 24082f39c91SKip Macy 24182f39c91SKip Macy return (llt); 24282f39c91SKip Macy } 24382f39c91SKip Macy 24482f39c91SKip Macy /* 24582f39c91SKip Macy * Called in route_output when adding/deleting a route to an interface. 24682f39c91SKip Macy */ 24782f39c91SKip Macy int 24882f39c91SKip Macy lla_rt_output(struct rt_msghdr *rtm, struct rt_addrinfo *info) 24982f39c91SKip Macy { 25082f39c91SKip Macy struct sockaddr_dl *dl = 25182f39c91SKip Macy (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY]; 25282f39c91SKip Macy struct sockaddr *dst = (struct sockaddr *)info->rti_info[RTAX_DST]; 25382f39c91SKip Macy struct ifnet *ifp; 25482f39c91SKip Macy struct lltable *llt; 25582f39c91SKip Macy struct llentry *lle; 25682f39c91SKip Macy u_int laflags = 0, flags = 0; 25782f39c91SKip Macy int error = 0; 25882f39c91SKip Macy 25982f39c91SKip Macy if (dl == NULL || dl->sdl_family != AF_LINK) { 26082f39c91SKip Macy log(LOG_INFO, "%s: invalid dl\n", __func__); 26182f39c91SKip Macy return EINVAL; 26282f39c91SKip Macy } 26382f39c91SKip Macy ifp = ifnet_byindex(dl->sdl_index); 26482f39c91SKip Macy if (ifp == NULL) { 26582f39c91SKip Macy log(LOG_INFO, "%s: invalid ifp (sdl_index %d)\n", 26682f39c91SKip Macy __func__, dl->sdl_index); 26782f39c91SKip Macy return EINVAL; 26882f39c91SKip Macy } 26982f39c91SKip Macy 27082f39c91SKip Macy switch (rtm->rtm_type) { 27182f39c91SKip Macy case RTM_ADD: 27282f39c91SKip Macy if (rtm->rtm_flags & RTF_ANNOUNCE) { 27382f39c91SKip Macy flags |= LLE_PUB; 27482f39c91SKip Macy #ifdef INET 27582f39c91SKip Macy if (dst->sa_family == AF_INET && 27682f39c91SKip Macy ((struct sockaddr_inarp *)dst)->sin_other != 0) { 27782f39c91SKip Macy struct rtentry *rt = rtalloc1(dst, 0, 0); 27882f39c91SKip Macy if (rt == NULL || !(rt->rt_flags & RTF_HOST)) { 27982f39c91SKip Macy log(LOG_INFO, "%s: RTM_ADD publish " 28082f39c91SKip Macy "(proxy only) is invalid\n", 28182f39c91SKip Macy __func__); 2822e730beaSBjoern A. Zeeb if (rt) 2832e730beaSBjoern A. Zeeb RTFREE_LOCKED(rt); 28482f39c91SKip Macy return EINVAL; 28582f39c91SKip Macy } 2862e730beaSBjoern A. Zeeb RTFREE_LOCKED(rt); 28782f39c91SKip Macy 28882f39c91SKip Macy flags |= LLE_PROXY; 28982f39c91SKip Macy } 29082f39c91SKip Macy #endif 29182f39c91SKip Macy } 29282f39c91SKip Macy flags |= LLE_CREATE; 29382f39c91SKip Macy break; 29482f39c91SKip Macy 29582f39c91SKip Macy case RTM_DELETE: 29682f39c91SKip Macy flags |= LLE_DELETE; 29782f39c91SKip Macy break; 29882f39c91SKip Macy 29982f39c91SKip Macy case RTM_CHANGE: 30082f39c91SKip Macy break; 30182f39c91SKip Macy 30282f39c91SKip Macy default: 30382f39c91SKip Macy return EINVAL; /* XXX not implemented yet */ 30482f39c91SKip Macy } 30582f39c91SKip Macy 30682f39c91SKip Macy /* XXX linked list may be too expensive */ 307dc56e98fSRobert Watson LLTABLE_RLOCK(); 308989e0411SMarko Zec SLIST_FOREACH(llt, &V_lltables, llt_link) { 30982f39c91SKip Macy if (llt->llt_af == dst->sa_family && 31082f39c91SKip Macy llt->llt_ifp == ifp) 31182f39c91SKip Macy break; 31282f39c91SKip Macy } 313dc56e98fSRobert Watson LLTABLE_RUNLOCK(); 31482f39c91SKip Macy KASSERT(llt != NULL, ("Yep, ugly hacks are bad\n")); 31582f39c91SKip Macy 31682f39c91SKip Macy if (flags && LLE_CREATE) 31782f39c91SKip Macy flags |= LLE_EXCLUSIVE; 31882f39c91SKip Macy 31982f39c91SKip Macy IF_AFDATA_LOCK(ifp); 32082f39c91SKip Macy lle = lla_lookup(llt, flags, dst); 32182f39c91SKip Macy IF_AFDATA_UNLOCK(ifp); 32282f39c91SKip Macy if (LLE_IS_VALID(lle)) { 32382f39c91SKip Macy if (flags & LLE_CREATE) { 32482f39c91SKip Macy /* 32582f39c91SKip Macy * If we delay the delete, then a subsequent 32682f39c91SKip Macy * "arp add" should look up this entry, reset the 32782f39c91SKip Macy * LLE_DELETED flag, and reset the expiration timer 32882f39c91SKip Macy */ 32982f39c91SKip Macy bcopy(LLADDR(dl), &lle->ll_addr, ifp->if_addrlen); 33082f39c91SKip Macy lle->la_flags |= LLE_VALID; 33182f39c91SKip Macy lle->la_flags &= ~LLE_DELETED; 33282f39c91SKip Macy #ifdef INET6 33382f39c91SKip Macy /* 33482f39c91SKip Macy * ND6 33582f39c91SKip Macy */ 33682f39c91SKip Macy if (dst->sa_family == AF_INET6) 33782f39c91SKip Macy lle->ln_state = ND6_LLINFO_REACHABLE; 33882f39c91SKip Macy #endif 33982f39c91SKip Macy /* 34082f39c91SKip Macy * NB: arp and ndp always set (RTF_STATIC | RTF_HOST) 34182f39c91SKip Macy */ 34282f39c91SKip Macy 34382f39c91SKip Macy if (rtm->rtm_rmx.rmx_expire == 0) { 34482f39c91SKip Macy lle->la_flags |= LLE_STATIC; 34582f39c91SKip Macy lle->la_expire = 0; 34682f39c91SKip Macy } else 34782f39c91SKip Macy lle->la_expire = rtm->rtm_rmx.rmx_expire; 34882f39c91SKip Macy laflags = lle->la_flags; 34982f39c91SKip Macy LLE_WUNLOCK(lle); 35082f39c91SKip Macy #ifdef INET 3517b4d716bSKip Macy /* gratuitous ARP */ 35282f39c91SKip Macy if ((laflags & LLE_PUB) && dst->sa_family == AF_INET) { 35382f39c91SKip Macy arprequest(ifp, 35482f39c91SKip Macy &((struct sockaddr_in *)dst)->sin_addr, 35582f39c91SKip Macy &((struct sockaddr_in *)dst)->sin_addr, 35682f39c91SKip Macy ((laflags & LLE_PROXY) ? 35782f39c91SKip Macy (u_char *)IF_LLADDR(ifp) : 35882f39c91SKip Macy (u_char *)LLADDR(dl))); 35982f39c91SKip Macy } 36082f39c91SKip Macy #endif 36182f39c91SKip Macy } else { 36282f39c91SKip Macy if (flags & LLE_EXCLUSIVE) 36382f39c91SKip Macy LLE_WUNLOCK(lle); 36482f39c91SKip Macy else 36582f39c91SKip Macy LLE_RUNLOCK(lle); 36682f39c91SKip Macy } 36782f39c91SKip Macy } else if ((lle == NULL) && (flags & LLE_DELETE)) 36882f39c91SKip Macy error = EINVAL; 36982f39c91SKip Macy 37082f39c91SKip Macy 37182f39c91SKip Macy return (error); 37282f39c91SKip Macy } 373989e0411SMarko Zec 374989e0411SMarko Zec static void 375989e0411SMarko Zec vnet_lltable_init() 376989e0411SMarko Zec { 377989e0411SMarko Zec 378989e0411SMarko Zec SLIST_INIT(&V_lltables); 379989e0411SMarko Zec } 38038d61195SMarko Zec VNET_SYSINIT(vnet_lltable_init, SI_SUB_PSEUDO, SI_ORDER_FIRST, 38138d61195SMarko Zec vnet_lltable_init, NULL); 382989e0411SMarko Zec 383