xref: /netbsd-src/sys/net/if_llatbl.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: if_llatbl.c,v 1.31 2019/09/25 09:53:37 ozaki-r Exp $	*/
2 /*
3  * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
4  * Copyright (c) 2004-2008 Qing Li. All rights reserved.
5  * Copyright (c) 2008 Kip Macy. All rights reserved.
6  * Copyright (c) 2015 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 #include <sys/cdefs.h>
31 
32 #ifdef _KERNEL_OPT
33 #include "opt_ddb.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_net_mpsafe.h"
37 #endif
38 
39 #include "arp.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/syslog.h>
46 #include <sys/sysctl.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/rwlock.h>
53 
54 #ifdef DDB
55 #include <ddb/ddb.h>
56 #endif
57 
58 #include <netinet/in.h>
59 #include <net/if_llatbl.h>
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/route.h>
63 #include <netinet/if_inarp.h>
64 #include <netinet/in_var.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet6/nd6.h>
67 
68 static SLIST_HEAD(, lltable) lltables;
69 krwlock_t lltable_rwlock;
70 static struct pool llentry_pool;
71 
72 static void lltable_unlink(struct lltable *llt);
73 static void llentries_unlink(struct lltable *llt, struct llentries *head);
74 
75 static void htable_unlink_entry(struct llentry *lle);
76 static void htable_link_entry(struct lltable *llt, struct llentry *lle);
77 static int htable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
78     void *farg);
79 
80 int
81 lltable_dump_entry(struct lltable *llt, struct llentry *lle,
82     struct rt_walkarg *w, struct sockaddr *sa)
83 {
84 #define RTF_LLINFO	0x400
85 #define RTF_CLONED	0x2000
86 	struct ifnet *ifp = llt->llt_ifp;
87 	int error;
88 	void *a;
89 	struct sockaddr_dl sdl;
90 	int size;
91 	struct rt_addrinfo info;
92 
93 	memset(&info, 0, sizeof(info));
94 	info.rti_info[RTAX_DST] = sa;
95 
96 	a = (lle->la_flags & LLE_VALID) == LLE_VALID ? &lle->ll_addr : NULL;
97 	if (sockaddr_dl_init(&sdl, sizeof(sdl), ifp->if_index, ifp->if_type,
98 	    NULL, 0, a, ifp->if_addrlen) == NULL)
99 		return EINVAL;
100 
101 	info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
102 	if (sa->sa_family == AF_INET && lle->la_flags & LLE_PUB) {
103 		struct sockaddr_inarp *sin;
104 		sin = (struct sockaddr_inarp *)sa;
105 		sin->sin_other = SIN_PROXY;
106 	}
107 	if ((error = rt_msg3(RTM_GET, &info, 0, w, &size)))
108 		return error;
109 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
110 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
111 
112 		/* Need to copy by myself */
113 		rtm->rtm_index = ifp->if_index;
114 		rtm->rtm_rmx.rmx_mtu = 0;
115 		rtm->rtm_rmx.rmx_expire = (lle->la_flags & LLE_STATIC) ? 0 :
116 		    time_mono_to_wall(lle->la_expire);
117 		rtm->rtm_flags = RTF_UP;
118 		rtm->rtm_flags |= RTF_HOST; /* For ndp */
119 		/* For backward compatibility */
120 		rtm->rtm_flags |= RTF_LLINFO | RTF_CLONED;
121 		rtm->rtm_flags |= (lle->la_flags & LLE_STATIC) ? RTF_STATIC : 0;
122 		if (lle->la_flags & LLE_PUB)
123 			rtm->rtm_flags |= RTF_ANNOUNCE;
124 		rtm->rtm_addrs = info.rti_addrs;
125 		if ((error = copyout(rtm, w->w_where, size)) != 0)
126 			w->w_where = NULL;
127 		else
128 			w->w_where = (char *)w->w_where + size;
129 	}
130 
131 	return error;
132 #undef RTF_LLINFO
133 #undef RTF_CLONED
134 }
135 
136 /*
137  * Dump lle state for a specific address family.
138  */
139 static int
140 lltable_dump_af(struct lltable *llt, struct rt_walkarg *w)
141 {
142 	int error;
143 
144 	LLTABLE_LOCK_ASSERT();
145 
146 	if (llt->llt_ifp->if_flags & IFF_LOOPBACK)
147 		return (0);
148 	error = 0;
149 
150 	IF_AFDATA_RLOCK(llt->llt_ifp);
151 	error = lltable_foreach_lle(llt,
152 	    (llt_foreach_cb_t *)llt->llt_dump_entry, w);
153 	IF_AFDATA_RUNLOCK(llt->llt_ifp);
154 
155 	return (error);
156 }
157 
158 /*
159  * Dump arp state for a specific address family.
160  */
161 int
162 lltable_sysctl_dump(int af, struct rt_walkarg *w)
163 {
164 	struct lltable *llt;
165 	int error = 0;
166 
167 	LLTABLE_RLOCK();
168 	SLIST_FOREACH(llt, &lltables, llt_link) {
169 		if (llt->llt_af == af) {
170 			error = lltable_dump_af(llt, w);
171 			if (error != 0)
172 				goto done;
173 		}
174 	}
175 done:
176 	LLTABLE_RUNLOCK();
177 	return (error);
178 }
179 
180 /*
181  * Common function helpers for chained hash table.
182  */
183 
184 /*
185  * Runs specified callback for each entry in @llt.
186  * Caller does the locking.
187  *
188  */
189 static int
190 htable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg)
191 {
192 	struct llentry *lle, *next;
193 	int i, error;
194 
195 	error = 0;
196 
197 	for (i = 0; i < llt->llt_hsize; i++) {
198 		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
199 			error = f(llt, lle, farg);
200 			if (error != 0)
201 				break;
202 		}
203 	}
204 
205 	return (error);
206 }
207 
208 static void
209 htable_link_entry(struct lltable *llt, struct llentry *lle)
210 {
211 	struct llentries *lleh;
212 	uint32_t hashidx;
213 
214 	if ((lle->la_flags & LLE_LINKED) != 0)
215 		return;
216 
217 	IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
218 
219 	hashidx = llt->llt_hash(lle, llt->llt_hsize);
220 	lleh = &llt->lle_head[hashidx];
221 
222 	lle->lle_tbl  = llt;
223 	lle->lle_head = lleh;
224 	lle->la_flags |= LLE_LINKED;
225 	LIST_INSERT_HEAD(lleh, lle, lle_next);
226 
227 	llt->llt_lle_count++;
228 }
229 
230 static void
231 htable_unlink_entry(struct llentry *lle)
232 {
233 
234 	if ((lle->la_flags & LLE_LINKED) != 0) {
235 		IF_AFDATA_WLOCK_ASSERT(lle->lle_tbl->llt_ifp);
236 		LIST_REMOVE(lle, lle_next);
237 		lle->la_flags &= ~(LLE_VALID | LLE_LINKED);
238 #if 0
239 		lle->lle_tbl = NULL;
240 		lle->lle_head = NULL;
241 #endif
242 		KASSERTMSG(lle->lle_tbl->llt_lle_count != 0,
243 		    "llt_lle_count=%u", lle->lle_tbl->llt_lle_count);
244 		lle->lle_tbl->llt_lle_count--;
245 	}
246 }
247 
248 struct prefix_match_data {
249 	const struct sockaddr *prefix;
250 	const struct sockaddr *mask;
251 	struct llentries dchain;
252 	u_int flags;
253 };
254 
255 static int
256 htable_prefix_free_cb(struct lltable *llt, struct llentry *lle, void *farg)
257 {
258 	struct prefix_match_data *pmd;
259 
260 	pmd = (struct prefix_match_data *)farg;
261 
262 	if (llt->llt_match_prefix(pmd->prefix, pmd->mask, pmd->flags, lle)) {
263 		LLE_WLOCK(lle);
264 		LIST_INSERT_HEAD(&pmd->dchain, lle, lle_chain);
265 	}
266 
267 	return (0);
268 }
269 
270 static void
271 htable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
272     const struct sockaddr *mask, u_int flags)
273 {
274 	struct llentry *lle, *next;
275 	struct prefix_match_data pmd;
276 
277 	memset(&pmd, 0, sizeof(pmd));
278 	pmd.prefix = prefix;
279 	pmd.mask = mask;
280 	pmd.flags = flags;
281 	LIST_INIT(&pmd.dchain);
282 
283 	IF_AFDATA_WLOCK(llt->llt_ifp);
284 	/* Push matching lles to chain */
285 	lltable_foreach_lle(llt, htable_prefix_free_cb, &pmd);
286 
287 	llentries_unlink(llt, &pmd.dchain);
288 	IF_AFDATA_WUNLOCK(llt->llt_ifp);
289 
290 	LIST_FOREACH_SAFE(lle, &pmd.dchain, lle_chain, next)
291 		llt->llt_free_entry(llt, lle);
292 }
293 
294 static void
295 htable_free_tbl(struct lltable *llt)
296 {
297 
298 	free(llt->lle_head, M_LLTABLE);
299 	free(llt, M_LLTABLE);
300 }
301 
302 static void
303 llentries_unlink(struct lltable *llt, struct llentries *head)
304 {
305 	struct llentry *lle, *next;
306 
307 	LIST_FOREACH_SAFE(lle, head, lle_chain, next)
308 		llt->llt_unlink_entry(lle);
309 }
310 
311 /*
312  * Helper function used to drop all mbufs in hold queue.
313  *
314  * Returns the number of held packets, if any, that were dropped.
315  */
316 size_t
317 lltable_drop_entry_queue(struct llentry *lle)
318 {
319 	size_t pkts_dropped;
320 	struct mbuf *next;
321 
322 	LLE_WLOCK_ASSERT(lle);
323 
324 	pkts_dropped = 0;
325 	while ((lle->la_numheld > 0) && (lle->la_hold != NULL)) {
326 		next = lle->la_hold->m_nextpkt;
327 		m_freem(lle->la_hold);
328 		lle->la_hold = next;
329 		lle->la_numheld--;
330 		pkts_dropped++;
331 	}
332 
333 	KASSERTMSG(lle->la_numheld == 0,
334 		"la_numheld %d > 0, pkts_droped %zd",
335 		 lle->la_numheld, pkts_dropped);
336 
337 	return (pkts_dropped);
338 }
339 
340 struct llentry *
341 llentry_pool_get(int flags)
342 {
343 	struct llentry *lle;
344 
345 	lle = pool_get(&llentry_pool, flags);
346 	if (lle != NULL)
347 		memset(lle, 0, sizeof(*lle));
348 	return lle;
349 }
350 
351 void
352 llentry_pool_put(struct llentry *lle)
353 {
354 
355 	pool_put(&llentry_pool, lle);
356 }
357 
358 /*
359  * Deletes an address from the address table.
360  * This function is called by the timer functions
361  * such as arptimer() and nd6_llinfo_timer(), and
362  * the caller does the locking.
363  *
364  * Returns the number of held packets, if any, that were dropped.
365  */
366 size_t
367 llentry_free(struct llentry *lle)
368 {
369 	struct lltable *llt;
370 	size_t pkts_dropped;
371 
372 	LLE_WLOCK_ASSERT(lle);
373 
374 	lle->la_flags |= LLE_DELETED;
375 
376 	if ((lle->la_flags & LLE_LINKED) != 0) {
377 		llt = lle->lle_tbl;
378 
379 		IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
380 		llt->llt_unlink_entry(lle);
381 	}
382 
383 	/*
384 	 * Stop a pending callout if one exists.  If we cancel one, we have to
385 	 * remove a reference to avoid a leak.  callout_pending is required to
386 	 * to exclude the case that the callout has never been scheduled.
387 	 */
388 	/* XXX once softnet_lock goes away, we should use callout_halt */
389 	if (callout_pending(&lle->la_timer)) {
390 		bool expired = callout_stop(&lle->la_timer);
391 		if (!expired)
392 			LLE_REMREF(lle);
393 	}
394 
395 	pkts_dropped = lltable_drop_entry_queue(lle);
396 
397 	LLE_FREE_LOCKED(lle);
398 
399 	return (pkts_dropped);
400 }
401 
402 /*
403  * (al)locate an llentry for address dst (equivalent to rtalloc for new-arp).
404  *
405  * If found the llentry * is returned referenced and unlocked.
406  */
407 struct llentry *
408 llentry_alloc(struct ifnet *ifp, struct lltable *lt,
409     struct sockaddr_storage *dst)
410 {
411 	struct llentry *la;
412 
413 	IF_AFDATA_RLOCK(ifp);
414 	la = lla_lookup(lt, LLE_EXCLUSIVE, (struct sockaddr *)dst);
415 	IF_AFDATA_RUNLOCK(ifp);
416 	if ((la == NULL) && (ifp->if_flags & IFF_NOARP) == 0) {
417 		IF_AFDATA_WLOCK(ifp);
418 		la = lla_create(lt, 0, (struct sockaddr *)dst, NULL /* XXX */);
419 		IF_AFDATA_WUNLOCK(ifp);
420 	}
421 
422 	if (la != NULL) {
423 		LLE_ADDREF(la);
424 		LLE_WUNLOCK(la);
425 	}
426 
427 	return (la);
428 }
429 
430 /*
431  * Free all entries from given table and free itself.
432  */
433 
434 static int
435 lltable_free_cb(struct lltable *llt, struct llentry *lle, void *farg)
436 {
437 	struct llentries *dchain;
438 
439 	dchain = (struct llentries *)farg;
440 
441 	LLE_WLOCK(lle);
442 	LIST_INSERT_HEAD(dchain, lle, lle_chain);
443 
444 	return (0);
445 }
446 
447 /*
448  * Free all entries from given table.
449  */
450 void
451 lltable_purge_entries(struct lltable *llt)
452 {
453 	struct llentry *lle, *next;
454 	struct llentries dchain;
455 
456 	KASSERTMSG(llt != NULL, "llt is NULL");
457 
458 	LIST_INIT(&dchain);
459 	IF_AFDATA_WLOCK(llt->llt_ifp);
460 	/* Push all lles to @dchain */
461 	lltable_foreach_lle(llt, lltable_free_cb, &dchain);
462 	llentries_unlink(llt, &dchain);
463 	IF_AFDATA_WUNLOCK(llt->llt_ifp);
464 
465 	LIST_FOREACH_SAFE(lle, &dchain, lle_chain, next)
466 		(void)llentry_free(lle);
467 }
468 
469 /*
470  * Free all entries from given table and free itself.
471  */
472 void
473 lltable_free(struct lltable *llt)
474 {
475 
476 	KASSERTMSG(llt != NULL, "llt is NULL");
477 
478 	lltable_unlink(llt);
479 	lltable_purge_entries(llt);
480 	llt->llt_free_tbl(llt);
481 }
482 
483 void
484 lltable_drain(int af)
485 {
486 	struct lltable	*llt;
487 	struct llentry	*lle;
488 	register int i;
489 
490 	LLTABLE_RLOCK();
491 	SLIST_FOREACH(llt, &lltables, llt_link) {
492 		if (llt->llt_af != af)
493 			continue;
494 
495 		for (i=0; i < llt->llt_hsize; i++) {
496 			LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
497 				LLE_WLOCK(lle);
498 				lltable_drop_entry_queue(lle);
499 				LLE_WUNLOCK(lle);
500 			}
501 		}
502 	}
503 	LLTABLE_RUNLOCK();
504 }
505 
506 void
507 lltable_prefix_free(const int af, const struct sockaddr *prefix,
508     const struct sockaddr *mask, const u_int flags)
509 {
510 	struct lltable *llt;
511 
512 	LLTABLE_RLOCK();
513 	SLIST_FOREACH(llt, &lltables, llt_link) {
514 		if (llt->llt_af != af)
515 			continue;
516 
517 		llt->llt_prefix_free(llt, prefix, mask, flags);
518 	}
519 	LLTABLE_RUNLOCK();
520 }
521 
522 struct lltable *
523 lltable_allocate_htbl(uint32_t hsize)
524 {
525 	struct lltable *llt;
526 	int i;
527 
528 	llt = malloc(sizeof(struct lltable), M_LLTABLE, M_WAITOK | M_ZERO);
529 	llt->llt_hsize = hsize;
530 	llt->lle_head = malloc(sizeof(struct llentries) * hsize,
531 	    M_LLTABLE, M_WAITOK | M_ZERO);
532 
533 	for (i = 0; i < llt->llt_hsize; i++)
534 		LIST_INIT(&llt->lle_head[i]);
535 
536 	/* Set some default callbacks */
537 	llt->llt_link_entry = htable_link_entry;
538 	llt->llt_unlink_entry = htable_unlink_entry;
539 	llt->llt_prefix_free = htable_prefix_free;
540 	llt->llt_foreach_entry = htable_foreach_lle;
541 
542 	llt->llt_free_tbl = htable_free_tbl;
543 
544 	return (llt);
545 }
546 
547 /*
548  * Links lltable to global llt list.
549  */
550 void
551 lltable_link(struct lltable *llt)
552 {
553 
554 	LLTABLE_WLOCK();
555 	SLIST_INSERT_HEAD(&lltables, llt, llt_link);
556 	LLTABLE_WUNLOCK();
557 }
558 
559 static void
560 lltable_unlink(struct lltable *llt)
561 {
562 
563 	LLTABLE_WLOCK();
564 	SLIST_REMOVE(&lltables, llt, lltable, llt_link);
565 	LLTABLE_WUNLOCK();
566 
567 }
568 
569 /*
570  * External methods used by lltable consumers
571  */
572 
573 int
574 lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg)
575 {
576 
577 	return (llt->llt_foreach_entry(llt, f, farg));
578 }
579 
580 void
581 lltable_link_entry(struct lltable *llt, struct llentry *lle)
582 {
583 
584 	llt->llt_link_entry(llt, lle);
585 }
586 
587 void
588 lltable_unlink_entry(struct lltable *llt, struct llentry *lle)
589 {
590 
591 	llt->llt_unlink_entry(lle);
592 }
593 
594 void
595 lltable_free_entry(struct lltable *llt, struct llentry *lle)
596 {
597 
598 	llt->llt_free_entry(llt, lle);
599 }
600 
601 void
602 lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
603 {
604 	struct lltable *llt;
605 
606 	llt = lle->lle_tbl;
607 	llt->llt_fill_sa_entry(lle, sa);
608 }
609 
610 struct ifnet *
611 lltable_get_ifp(const struct lltable *llt)
612 {
613 
614 	return (llt->llt_ifp);
615 }
616 
617 int
618 lltable_get_af(const struct lltable *llt)
619 {
620 
621 	return (llt->llt_af);
622 }
623 
624 /*
625  * Called in route_output when rtm_flags contains RTF_LLDATA.
626  */
627 int
628 lla_rt_output(const u_char rtm_type, const int rtm_flags, const time_t rtm_expire,
629     struct rt_addrinfo *info, int sdl_index)
630 {
631 	const struct sockaddr_dl *dl = satocsdl(info->rti_info[RTAX_GATEWAY]);
632 	const struct sockaddr *dst = info->rti_info[RTAX_DST];
633 	struct ifnet *ifp;
634 	struct lltable *llt;
635 	struct llentry *lle;
636 	u_int laflags;
637 	int error;
638 	struct psref psref;
639 	int bound;
640 
641 	KASSERTMSG(dl != NULL && dl->sdl_family == AF_LINK, "invalid dl");
642 
643 	bound = curlwp_bind();
644 	if (sdl_index != 0)
645 		ifp = if_get_byindex(sdl_index, &psref);
646 	else
647 		ifp = if_get_byindex(dl->sdl_index, &psref);
648 	if (ifp == NULL) {
649 		curlwp_bindx(bound);
650 		log(LOG_INFO, "%s: invalid ifp (sdl_index %d)\n",
651 		    __func__, sdl_index != 0 ? sdl_index : dl->sdl_index);
652 		return EINVAL;
653 	}
654 
655 	/* XXX linked list may be too expensive */
656 	LLTABLE_RLOCK();
657 	SLIST_FOREACH(llt, &lltables, llt_link) {
658 		if (llt->llt_af == dst->sa_family &&
659 		    llt->llt_ifp == ifp)
660 			break;
661 	}
662 	LLTABLE_RUNLOCK();
663 	KASSERTMSG(llt != NULL, "Yep, ugly hacks are bad");
664 
665 	error = 0;
666 
667 	switch (rtm_type) {
668 	case RTM_ADD: {
669 		struct rtentry *rt;
670 
671 		/* Never call rtalloc1 with IF_AFDATA_WLOCK */
672 		rt = rtalloc1(dst, 0);
673 
674 		/* Add static LLE */
675 		IF_AFDATA_WLOCK(ifp);
676 		lle = lla_lookup(llt, LLE_EXCLUSIVE, dst);
677 
678 		/* Cannot overwrite an existing static entry */
679 		if (lle != NULL &&
680 		    (lle->la_flags & LLE_STATIC || lle->la_expire == 0)) {
681 			LLE_RUNLOCK(lle);
682 			IF_AFDATA_WUNLOCK(ifp);
683 			if (rt != NULL)
684 				rt_unref(rt);
685 			error = EEXIST;
686 			goto out;
687 		}
688 
689 		/*
690 		 * We can't overwrite an existing entry to avoid race
691 		 * conditions so remove it first.
692 		 */
693 		if (lle != NULL) {
694 #if defined(INET) && NARP > 0
695 			size_t pkts_dropped = llentry_free(lle);
696 			if (dst->sa_family == AF_INET) {
697 				arp_stat_add(ARP_STAT_DFRDROPPED,
698 				    (uint64_t)pkts_dropped);
699 			}
700 #else
701 			(void) llentry_free(lle);
702 #endif
703 		}
704 
705 		lle = lla_create(llt, 0, dst, rt);
706 		if (lle == NULL) {
707 			IF_AFDATA_WUNLOCK(ifp);
708 			if (rt != NULL)
709 				rt_unref(rt);
710 			error = ENOMEM;
711 			goto out;
712 		}
713 
714 		KASSERT(ifp->if_addrlen <= sizeof(lle->ll_addr));
715 		memcpy(&lle->ll_addr, CLLADDR(dl), ifp->if_addrlen);
716 		if ((rtm_flags & RTF_ANNOUNCE))
717 			lle->la_flags |= LLE_PUB;
718 		lle->la_flags |= LLE_VALID;
719 #ifdef INET6
720 		/*
721 		 * ND6
722 		 */
723 		if (dst->sa_family == AF_INET6)
724 			lle->ln_state = ND6_LLINFO_REACHABLE;
725 #endif
726 		/*
727 		 * NB: arp and ndp always set (RTF_STATIC | RTF_HOST)
728 		 */
729 
730 		if (rtm_expire == 0) {
731 			lle->la_flags |= LLE_STATIC;
732 			lle->la_expire = 0;
733 		} else
734 			lle->la_expire = rtm_expire;
735 		laflags = lle->la_flags;
736 		LLE_WUNLOCK(lle);
737 		IF_AFDATA_WUNLOCK(ifp);
738 		if (rt != NULL)
739 			rt_unref(rt);
740 #if defined(INET) && NARP > 0
741 		/* gratuitous ARP */
742 		if ((laflags & LLE_PUB) && dst->sa_family == AF_INET) {
743 			const struct sockaddr_in *sin;
744 			struct in_ifaddr *ia;
745 			struct psref _psref;
746 
747 			sin = satocsin(dst);
748 			ia = in_get_ia_on_iface_psref(sin->sin_addr,
749 			    ifp, &_psref);
750 			if (ia != NULL) {
751 				arpannounce(ifp, &ia->ia_ifa, CLLADDR(dl));
752 				ia4_release(ia, &_psref);
753 			}
754 		}
755 #else
756 		(void)laflags;
757 #endif
758 		break;
759 	    }
760 
761 	case RTM_DELETE:
762 		IF_AFDATA_WLOCK(ifp);
763 		error = lla_delete(llt, 0, dst);
764 		IF_AFDATA_WUNLOCK(ifp);
765 		error = (error == 0 ? 0 : ENOENT);
766 		break;
767 
768 	default:
769 		error = EINVAL;
770 	}
771 
772 out:
773 	if_put(ifp, &psref);
774 	curlwp_bindx(bound);
775 	return (error);
776 }
777 
778 void
779 lltableinit(void)
780 {
781 
782 	SLIST_INIT(&lltables);
783 	rw_init(&lltable_rwlock);
784 
785 	pool_init(&llentry_pool, sizeof(struct llentry), 0, 0, 0, "llentrypl",
786 	    NULL, IPL_SOFTNET);
787 }
788 
789 #ifdef __FreeBSD__
790 #ifdef DDB
791 struct llentry_sa {
792 	struct llentry		base;
793 	struct sockaddr		l3_addr;
794 };
795 
796 static void
797 llatbl_lle_show(struct llentry_sa *la)
798 {
799 	struct llentry *lle;
800 	uint8_t octet[6];
801 
802 	lle = &la->base;
803 	db_printf("lle=%p\n", lle);
804 	db_printf(" lle_next=%p\n", lle->lle_next.le_next);
805 	db_printf(" lle_lock=%p\n", &lle->lle_lock);
806 	db_printf(" lle_tbl=%p\n", lle->lle_tbl);
807 	db_printf(" lle_head=%p\n", lle->lle_head);
808 	db_printf(" la_hold=%p\n", lle->la_hold);
809 	db_printf(" la_numheld=%d\n", lle->la_numheld);
810 	db_printf(" la_expire=%ju\n", (uintmax_t)lle->la_expire);
811 	db_printf(" la_flags=0x%04x\n", lle->la_flags);
812 	db_printf(" la_asked=%u\n", lle->la_asked);
813 	db_printf(" la_preempt=%u\n", lle->la_preempt);
814 	db_printf(" ln_byhint=%u\n", lle->ln_byhint);
815 	db_printf(" ln_state=%d\n", lle->ln_state);
816 	db_printf(" ln_router=%u\n", lle->ln_router);
817 	db_printf(" ln_ntick=%ju\n", (uintmax_t)lle->ln_ntick);
818 	db_printf(" lle_refcnt=%d\n", lle->lle_refcnt);
819 	memcopy(octet, &lle->ll_addr.mac16, sizeof(octet));
820 	db_printf(" ll_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
821 	    octet[0], octet[1], octet[2], octet[3], octet[4], octet[5]);
822 	db_printf(" lle_timer=%p\n", &lle->lle_timer);
823 
824 	switch (la->l3_addr.sa_family) {
825 #ifdef INET
826 	case AF_INET:
827 	{
828 		struct sockaddr_in *sin;
829 		char l3s[INET_ADDRSTRLEN];
830 
831 		sin = (struct sockaddr_in *)&la->l3_addr;
832 		inet_ntoa_r(sin->sin_addr, l3s);
833 		db_printf(" l3_addr=%s\n", l3s);
834 		break;
835 	}
836 #endif
837 #ifdef INET6
838 	case AF_INET6:
839 	{
840 		struct sockaddr_in6 *sin6;
841 		char l3s[INET6_ADDRSTRLEN];
842 
843 		sin6 = (struct sockaddr_in6 *)&la->l3_addr;
844 		IN6_PRINT(l3s, &sin6->sin6_addr);
845 		db_printf(" l3_addr=%s\n", l3s);
846 		break;
847 	}
848 #endif
849 	default:
850 		db_printf(" l3_addr=N/A (af=%d)\n", la->l3_addr.sa_family);
851 		break;
852 	}
853 }
854 
855 DB_SHOW_COMMAND(llentry, db_show_llentry)
856 {
857 
858 	if (!have_addr) {
859 		db_printf("usage: show llentry <struct llentry *>\n");
860 		return;
861 	}
862 
863 	llatbl_lle_show((struct llentry_sa *)addr);
864 }
865 
866 static void
867 llatbl_llt_show(struct lltable *llt)
868 {
869 	int i;
870 	struct llentry *lle;
871 
872 	db_printf("llt=%p llt_af=%d llt_ifp=%p\n",
873 	    llt, llt->llt_af, llt->llt_ifp);
874 
875 	for (i = 0; i < llt->llt_hsize; i++) {
876 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
877 
878 			llatbl_lle_show((struct llentry_sa *)lle);
879 			if (db_pager_quit)
880 				return;
881 		}
882 	}
883 }
884 
885 DB_SHOW_COMMAND(lltable, db_show_lltable)
886 {
887 
888 	if (!have_addr) {
889 		db_printf("usage: show lltable <struct lltable *>\n");
890 		return;
891 	}
892 
893 	llatbl_llt_show((struct lltable *)addr);
894 }
895 
896 DB_SHOW_ALL_COMMAND(lltables, db_show_all_lltables)
897 {
898 	VNET_ITERATOR_DECL(vnet_iter);
899 	struct lltable *llt;
900 
901 	VNET_FOREACH(vnet_iter) {
902 		CURVNET_SET_QUIET(vnet_iter);
903 #ifdef VIMAGE
904 		db_printf("vnet=%p\n", curvnet);
905 #endif
906 		SLIST_FOREACH(llt, &lltables, llt_link) {
907 			db_printf("llt=%p llt_af=%d llt_ifp=%p(%s)\n",
908 			    llt, llt->llt_af, llt->llt_ifp,
909 			    (llt->llt_ifp != NULL) ?
910 				llt->llt_ifp->if_xname : "?");
911 			if (have_addr && addr != 0) /* verbose */
912 				llatbl_llt_show(llt);
913 			if (db_pager_quit) {
914 				CURVNET_RESTORE();
915 				return;
916 			}
917 		}
918 		CURVNET_RESTORE();
919 	}
920 }
921 #endif /* DDB */
922 #endif /* __FreeBSD__ */
923