2 * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
3 * Copyright (c) 2004-2008 Qing Li. All rights reserved.
4 * Copyright (c) 2008 Kip Macy. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
37 #include <sys/syslog.h>
38 #include <sys/sysctl.h>
39 #include <sys/socket.h>
40 #include <sys/kernel.h>
42 #include <sys/mutex.h>
43 #include <sys/rwlock.h>
47 #include <netinet/in.h>
48 #include <net/if_llatbl.h>
50 #include <net/if_dl.h>
51 #include <net/if_var.h>
52 #include <net/route.h>
54 #include <netinet/if_ether.h>
55 #include <netinet6/in6_var.h>
56 #include <netinet6/nd6.h>
58 MALLOC_DEFINE(M_LLTABLE, "lltable", "link level address tables");
60 static SLIST_HEAD(, lltable) lltables = SLIST_HEAD_INITIALIZER(lltables);
62 extern void arprequest(struct ifnet *, struct in_addr *, struct in_addr *,
66 * Dump arp state for a specific address family.
69 lltable_sysctl_dumparp(int af, struct sysctl_req *wr)
75 SLIST_FOREACH(llt, &lltables, llt_link) {
76 if (llt->llt_af == af) {
77 error = llt->llt_dump(llt, wr);
88 * Deletes an address from the address table.
89 * This function is called by the timer functions
90 * such as arptimer() and nd6_llinfo_timer(), and
91 * the caller does the locking.
94 llentry_free(struct llentry *lle)
97 LLE_WLOCK_ASSERT(lle);
98 LIST_REMOVE(lle, lle_next);
100 if (lle->la_hold != NULL)
101 m_freem(lle->la_hold);
103 LLE_FREE_LOCKED(lle);
107 * Update an llentry for address dst (equivalent to rtalloc for new-arp)
108 * Caller must pass in a valid struct llentry *
110 * if found the llentry * is returned referenced and unlocked
113 llentry_update(struct llentry **llep, struct lltable *lt,
114 struct sockaddr *dst, struct ifnet *ifp)
118 IF_AFDATA_RLOCK(ifp);
119 la = lla_lookup(lt, LLE_EXCLUSIVE,
120 (struct sockaddr *)dst);
121 IF_AFDATA_RUNLOCK(ifp);
123 (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
124 IF_AFDATA_WLOCK(ifp);
126 (LLE_CREATE | LLE_EXCLUSIVE),
127 (struct sockaddr *)dst);
128 IF_AFDATA_WUNLOCK(ifp);
130 if (la != NULL && (*llep != la)) {
136 } else if (la != NULL)
146 * Free all entries from given table and free itself.
147 * Since lltables collects from all of the intefaces,
148 * the caller of this function must acquire IFNET_WLOCK().
151 lltable_free(struct lltable *llt)
153 struct llentry *lle, *next;
156 KASSERT(llt != NULL, ("%s: llt is NULL", __func__));
159 SLIST_REMOVE(&lltables, llt, lltable, llt_link);
162 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
163 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
165 callout_drain(&lle->la_timer);
171 free(llt, M_LLTABLE);
175 lltable_drain(int af)
182 SLIST_FOREACH(llt, &lltables, llt_link) {
183 if (llt->llt_af != af)
186 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
187 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
189 m_freem(lle->la_hold);
199 lltable_prefix_free(int af, struct sockaddr *prefix, struct sockaddr *mask)
204 SLIST_FOREACH(llt, &lltables, llt_link) {
205 if (llt->llt_af != af)
208 llt->llt_prefix_free(llt, prefix, mask);
216 * Create a new lltable.
219 lltable_init(struct ifnet *ifp, int af)
224 llt = malloc(sizeof(struct lltable), M_LLTABLE, M_WAITOK);
230 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++)
231 LIST_INIT(&llt->lle_head[i]);
234 SLIST_INSERT_HEAD(&lltables, llt, llt_link);
241 * Called in route_output when adding/deleting a route to an interface.
244 lla_rt_output(struct rt_msghdr *rtm, struct rt_addrinfo *info)
246 struct sockaddr_dl *dl =
247 (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
248 struct sockaddr *dst = (struct sockaddr *)info->rti_info[RTAX_DST];
252 u_int laflags = 0, flags = 0;
255 if (dl == NULL || dl->sdl_family != AF_LINK) {
256 log(LOG_INFO, "%s: invalid dl\n", __func__);
259 ifp = ifnet_byindex(dl->sdl_index);
261 log(LOG_INFO, "%s: invalid ifp (sdl_index %d)\n",
262 __func__, dl->sdl_index);
266 switch (rtm->rtm_type) {
268 if (rtm->rtm_flags & RTF_ANNOUNCE) {
271 if (dst->sa_family == AF_INET &&
272 ((struct sockaddr_inarp *)dst)->sin_other != 0) {
273 struct rtentry *rt = rtalloc1(dst, 0, 0);
274 if (rt == NULL || !(rt->rt_flags & RTF_HOST)) {
275 log(LOG_INFO, "%s: RTM_ADD publish "
276 "(proxy only) is invalid\n",
299 return EINVAL; /* XXX not implemented yet */
302 /* XXX linked list may be too expensive */
304 SLIST_FOREACH(llt, &lltables, llt_link) {
305 if (llt->llt_af == dst->sa_family &&
310 KASSERT(llt != NULL, ("Yep, ugly hacks are bad\n"));
312 if (flags && LLE_CREATE)
313 flags |= LLE_EXCLUSIVE;
316 lle = lla_lookup(llt, flags, dst);
317 IF_AFDATA_UNLOCK(ifp);
318 if (LLE_IS_VALID(lle)) {
319 if (flags & LLE_CREATE) {
321 * If we delay the delete, then a subsequent
322 * "arp add" should look up this entry, reset the
323 * LLE_DELETED flag, and reset the expiration timer
325 bcopy(LLADDR(dl), &lle->ll_addr, ifp->if_addrlen);
326 lle->la_flags |= LLE_VALID;
327 lle->la_flags &= ~LLE_DELETED;
332 if (dst->sa_family == AF_INET6)
333 lle->ln_state = ND6_LLINFO_REACHABLE;
336 * NB: arp and ndp always set (RTF_STATIC | RTF_HOST)
339 if (rtm->rtm_rmx.rmx_expire == 0) {
340 lle->la_flags |= LLE_STATIC;
343 lle->la_expire = rtm->rtm_rmx.rmx_expire;
344 laflags = lle->la_flags;
348 if ((laflags & LLE_PUB) && dst->sa_family == AF_INET) {
350 &((struct sockaddr_in *)dst)->sin_addr,
351 &((struct sockaddr_in *)dst)->sin_addr,
352 ((laflags & LLE_PROXY) ?
353 (u_char *)IF_LLADDR(ifp) :
354 (u_char *)LLADDR(dl)));
358 if (flags & LLE_EXCLUSIVE)
363 } else if ((lle == NULL) && (flags & LLE_DELETE))