2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
5 * Copyright (c) 2004-2008 Qing Li. All rights reserved.
6 * Copyright (c) 2008 Kip Macy. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/eventhandler.h>
39 #include <sys/malloc.h>
41 #include <sys/syslog.h>
42 #include <sys/sysctl.h>
43 #include <sys/socket.h>
44 #include <sys/kernel.h>
46 #include <sys/mutex.h>
47 #include <sys/rwlock.h>
55 #include <netinet/in.h>
56 #include <net/if_llatbl.h>
58 #include <net/if_dl.h>
59 #include <net/if_var.h>
60 #include <net/route.h>
61 #include <net/route/route_ctl.h>
62 #include <net/route/route_debug.h>
64 #include <netinet/if_ether.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet6/nd6.h>
68 MALLOC_DEFINE(M_LLTABLE, "lltable", "link level address tables");
70 VNET_DEFINE_STATIC(SLIST_HEAD(, lltable), lltables) =
71 SLIST_HEAD_INITIALIZER(lltables);
72 #define V_lltables VNET(lltables)
74 static struct rwlock lltable_list_lock;
75 RW_SYSINIT(lltable_list_lock, &lltable_list_lock, "lltable_list_lock");
76 #define LLTABLE_LIST_RLOCK() rw_rlock(&lltable_list_lock)
77 #define LLTABLE_LIST_RUNLOCK() rw_runlock(&lltable_list_lock)
78 #define LLTABLE_LIST_WLOCK() rw_wlock(&lltable_list_lock)
79 #define LLTABLE_LIST_WUNLOCK() rw_wunlock(&lltable_list_lock)
80 #define LLTABLE_LIST_LOCK_ASSERT() rw_assert(&lltable_list_lock, RA_LOCKED)
82 static void lltable_unlink(struct lltable *llt);
83 static void llentries_unlink(struct lltable *llt, struct llentries *head);
86 * Dump lle state for a specific address family.
89 lltable_dump_af(struct lltable *llt, struct sysctl_req *wr)
91 struct epoch_tracker et;
94 LLTABLE_LIST_LOCK_ASSERT();
96 if (llt->llt_ifp->if_flags & IFF_LOOPBACK)
101 error = lltable_foreach_lle(llt,
102 (llt_foreach_cb_t *)llt->llt_dump_entry, wr);
109 * Dump arp state for a specific address family.
112 lltable_sysctl_dumparp(int af, struct sysctl_req *wr)
117 LLTABLE_LIST_RLOCK();
118 SLIST_FOREACH(llt, &V_lltables, llt_link) {
119 if (llt->llt_af == af) {
120 error = lltable_dump_af(llt, wr);
126 LLTABLE_LIST_RUNLOCK();
131 * Adds a mbuf to hold queue. Drops old packets if the queue is full.
133 * Returns the number of held packets that were dropped.
136 lltable_append_entry_queue(struct llentry *lle, struct mbuf *m,
139 size_t pkts_dropped = 0;
141 LLE_WLOCK_ASSERT(lle);
143 while (lle->la_numheld >= maxheld && lle->la_hold != NULL) {
144 struct mbuf *next = lle->la_hold->m_nextpkt;
145 m_freem(lle->la_hold);
151 if (lle->la_hold != NULL) {
152 struct mbuf *curr = lle->la_hold;
153 while (curr->m_nextpkt != NULL)
154 curr = curr->m_nextpkt;
166 * Common function helpers for chained hash table.
170 * Runs specified callback for each entry in @llt.
171 * Caller does the locking.
175 htable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg)
177 struct llentry *lle, *next;
182 for (i = 0; i < llt->llt_hsize; i++) {
183 CK_LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
184 error = f(llt, lle, farg);
194 * The htable_[un]link_entry() functions return:
195 * 0 if the entry was (un)linked already and nothing changed,
196 * 1 if the entry was added/removed to/from the table, and
197 * -1 on error (e.g., not being able to add the entry due to limits reached).
198 * While the "unlink" operation should never error, callers of
199 * lltable_link_entry() need to check for errors and handle them.
202 htable_link_entry(struct lltable *llt, struct llentry *lle)
204 struct llentries *lleh;
207 if ((lle->la_flags & LLE_LINKED) != 0)
210 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
212 if (llt->llt_maxentries > 0 &&
213 llt->llt_entries >= llt->llt_maxentries)
216 hashidx = llt->llt_hash(lle, llt->llt_hsize);
217 lleh = &llt->lle_head[hashidx];
220 lle->lle_head = lleh;
221 lle->la_flags |= LLE_LINKED;
222 CK_LIST_INSERT_HEAD(lleh, lle, lle_next);
229 htable_unlink_entry(struct llentry *lle)
233 if ((lle->la_flags & LLE_LINKED) == 0)
237 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
238 KASSERT(llt->llt_entries > 0, ("%s: lltable %p (%s) entries %d <= 0",
239 __func__, llt, if_name(llt->llt_ifp), llt->llt_entries));
241 CK_LIST_REMOVE(lle, lle_next);
242 lle->la_flags &= ~(LLE_VALID | LLE_LINKED);
245 lle->lle_head = NULL;
252 struct prefix_match_data {
253 const struct sockaddr *addr;
254 const struct sockaddr *mask;
255 struct llentries dchain;
260 htable_prefix_free_cb(struct lltable *llt, struct llentry *lle, void *farg)
262 struct prefix_match_data *pmd;
264 pmd = (struct prefix_match_data *)farg;
266 if (llt->llt_match_prefix(pmd->addr, pmd->mask, pmd->flags, lle)) {
268 CK_LIST_INSERT_HEAD(&pmd->dchain, lle, lle_chain);
275 htable_prefix_free(struct lltable *llt, const struct sockaddr *addr,
276 const struct sockaddr *mask, u_int flags)
278 struct llentry *lle, *next;
279 struct prefix_match_data pmd;
281 bzero(&pmd, sizeof(pmd));
285 CK_LIST_INIT(&pmd.dchain);
287 IF_AFDATA_WLOCK(llt->llt_ifp);
288 /* Push matching lles to chain */
289 lltable_foreach_lle(llt, htable_prefix_free_cb, &pmd);
291 llentries_unlink(llt, &pmd.dchain);
292 IF_AFDATA_WUNLOCK(llt->llt_ifp);
294 CK_LIST_FOREACH_SAFE(lle, &pmd.dchain, lle_chain, next)
295 lltable_free_entry(llt, lle);
299 htable_free_tbl(struct lltable *llt)
302 free(llt->lle_head, M_LLTABLE);
303 free(llt, M_LLTABLE);
307 llentries_unlink(struct lltable *llt, struct llentries *head)
309 struct llentry *lle, *next;
311 CK_LIST_FOREACH_SAFE(lle, head, lle_chain, next)
312 llt->llt_unlink_entry(lle);
316 * Helper function used to drop all mbufs in hold queue.
318 * Returns the number of held packets, if any, that were dropped.
321 lltable_drop_entry_queue(struct llentry *lle)
323 size_t pkts_dropped = 0;
325 LLE_WLOCK_ASSERT(lle);
327 while (lle->la_hold != NULL) {
328 struct mbuf *next = lle->la_hold->m_nextpkt;
329 m_freem(lle->la_hold);
335 KASSERT(lle->la_numheld == 0,
336 ("%s: la_numheld %d > 0, pkts_dropped %zd", __func__,
337 lle->la_numheld, pkts_dropped));
339 return (pkts_dropped);
343 lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
344 const char *linkhdr, size_t linkhdrsize, int lladdr_off)
347 memcpy(lle->r_linkdata, linkhdr, linkhdrsize);
348 lle->r_hdrlen = linkhdrsize;
349 lle->ll_addr = &lle->r_linkdata[lladdr_off];
350 lle->la_flags |= LLE_VALID;
351 lle->r_flags |= RLLE_VALID;
355 * Acquires lltable write lock.
357 * Returns true on success, with both lltable and lle lock held.
358 * On failure, false is returned and lle wlock is still held.
361 lltable_acquire_wlock(struct ifnet *ifp, struct llentry *lle)
365 /* Perform real LLE update */
366 /* use afdata WLOCK to update fields */
368 IF_AFDATA_WLOCK(ifp);
372 * Since we droppped LLE lock, other thread might have deleted
373 * this lle. Check and return
375 if ((lle->la_flags & LLE_DELETED) != 0) {
376 IF_AFDATA_WUNLOCK(ifp);
384 * Tries to update @lle link-level address.
385 * Since update requires AFDATA WLOCK, function
386 * drops @lle lock, acquires AFDATA lock and then acquires
387 * @lle lock to maintain lock order.
389 * Returns 1 on success.
392 lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
393 const char *linkhdr, size_t linkhdrsize, int lladdr_off)
396 if (!lltable_acquire_wlock(ifp, lle))
400 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize, lladdr_off);
402 IF_AFDATA_WUNLOCK(ifp);
408 * Helper function used to pre-compute full/partial link-layer
409 * header data suitable for feeding into if_output().
412 lltable_calc_llheader(struct ifnet *ifp, int family, char *lladdr,
413 char *buf, size_t *bufsize, int *lladdr_off)
415 struct if_encap_req ereq;
418 bzero(buf, *bufsize);
419 bzero(&ereq, sizeof(ereq));
421 ereq.bufsize = *bufsize;
422 ereq.rtype = IFENCAP_LL;
423 ereq.family = family;
424 ereq.lladdr = lladdr;
425 ereq.lladdr_len = ifp->if_addrlen;
426 error = ifp->if_requestencap(ifp, &ereq);
428 *bufsize = ereq.bufsize;
429 *lladdr_off = ereq.lladdr_off;
436 * Searches for the child entry matching @family inside @lle.
437 * Returns the entry or NULL.
440 llentry_lookup_family(struct llentry *lle, int family)
442 struct llentry *child_lle;
447 CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
448 if (child_lle->r_family == family)
456 * Retrieves upper protocol family for the llentry.
457 * By default, all "normal" (e.g. upper_family == transport_family)
458 * llentries have r_family set to 0.
459 * Thus, use @default_family in that regard, otherwise use r_family.
461 * Returns upper protocol family
464 llentry_get_upper_family(const struct llentry *lle, int default_family)
466 return (lle->r_family == 0 ? default_family : lle->r_family);
470 * Prints llentry @lle data into provided buffer.
471 * Example: lle/inet/valid/em0/1.2.3.4
476 llentry_print_buf(const struct llentry *lle, struct ifnet *ifp, int family,
477 char *buf, size_t bufsize)
479 #if defined(INET) || defined(INET6)
480 char abuf[INET6_ADDRSTRLEN];
483 const char *valid = (lle->r_flags & RLLE_VALID) ? "valid" : "no_l2";
484 const char *upper_str = rib_print_family(llentry_get_upper_family(lle, family));
489 inet_ntop(AF_INET, &lle->r_l3addr.addr4, abuf, sizeof(abuf));
490 snprintf(buf, bufsize, "lle/%s/%s/%s/%s", upper_str,
491 valid, if_name(ifp), abuf);
496 inet_ntop(AF_INET6, &lle->r_l3addr.addr6, abuf, sizeof(abuf));
497 snprintf(buf, bufsize, "lle/%s/%s/%s/%s", upper_str,
498 valid, if_name(ifp), abuf);
502 snprintf(buf, bufsize, "lle/%s/%s/%s/????", upper_str,
503 valid, if_name(ifp));
511 llentry_print_buf_lltable(const struct llentry *lle, char *buf, size_t bufsize)
513 struct lltable *tbl = lle->lle_tbl;
515 return (llentry_print_buf(lle, lltable_get_ifp(tbl), lltable_get_af(tbl), buf, bufsize));
519 * Requests feedback from the datapath.
520 * First packet using @lle should result in
521 * setting r_skip_req back to 0 and updating
522 * lle_hittime to the current time_uptime.
525 llentry_request_feedback(struct llentry *lle)
527 struct llentry *child_lle;
533 CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
534 LLE_REQ_LOCK(child_lle);
535 child_lle->r_skip_req = 1;
536 LLE_REQ_UNLOCK(child_lle);
541 * Updates the lle state to mark it has been used
542 * and record the time.
543 * Used by the llentry_provide_feedback() wrapper.
546 llentry_mark_used(struct llentry *lle)
550 lle->lle_hittime = time_uptime;
555 * Fetches the time when lle was used.
556 * Return 0 if the entry was not used, relevant time_uptime
560 llentry_get_hittime_raw(struct llentry *lle)
562 time_t lle_hittime = 0;
565 if ((lle->r_skip_req == 0) && (lle_hittime < lle->lle_hittime))
566 lle_hittime = lle->lle_hittime;
569 return (lle_hittime);
573 llentry_get_hittime(struct llentry *lle)
575 time_t lle_hittime = 0;
576 struct llentry *child_lle;
578 lle_hittime = llentry_get_hittime_raw(lle);
580 CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
581 time_t hittime = llentry_get_hittime_raw(child_lle);
582 if (hittime > lle_hittime)
583 lle_hittime = hittime;
586 return (lle_hittime);
590 * Update link-layer header for given @lle after
591 * interface lladdr was changed.
594 llentry_update_ifaddr(struct lltable *llt, struct llentry *lle, void *farg)
597 u_char linkhdr[LLE_MAX_LINKHDR];
602 ifp = (struct ifnet *)farg;
604 lladdr = lle->ll_addr;
607 if ((lle->la_flags & LLE_VALID) == 0) {
612 if ((lle->la_flags & LLE_IFADDR) != 0)
613 lladdr = IF_LLADDR(ifp);
615 linkhdrsize = sizeof(linkhdr);
616 lltable_calc_llheader(ifp, llt->llt_af, lladdr, linkhdr, &linkhdrsize,
618 memcpy(lle->r_linkdata, linkhdr, linkhdrsize);
625 * Update all calculated headers for given @llt
628 lltable_update_ifaddr(struct lltable *llt)
631 if (llt->llt_ifp->if_flags & IFF_LOOPBACK)
634 IF_AFDATA_WLOCK(llt->llt_ifp);
635 lltable_foreach_lle(llt, llentry_update_ifaddr, llt->llt_ifp);
636 IF_AFDATA_WUNLOCK(llt->llt_ifp);
641 * Performs generic cleanup routines and frees lle.
643 * Called for non-linked entries, with callouts and
644 * other AF-specific cleanups performed.
646 * @lle must be passed WLOCK'ed
648 * Returns the number of held packets, if any, that were dropped.
651 llentry_free(struct llentry *lle)
655 LLE_WLOCK_ASSERT(lle);
657 KASSERT((lle->la_flags & LLE_LINKED) == 0, ("freeing linked lle"));
659 pkts_dropped = lltable_drop_entry_queue(lle);
662 if (callout_stop(&lle->lle_timer) > 0)
664 LLE_FREE_LOCKED(lle);
666 return (pkts_dropped);
670 * Free all entries from given table and free itself.
674 lltable_free_cb(struct lltable *llt, struct llentry *lle, void *farg)
676 struct llentries *dchain;
678 dchain = (struct llentries *)farg;
681 CK_LIST_INSERT_HEAD(dchain, lle, lle_chain);
687 * Free all entries from given table and free itself.
690 lltable_free(struct lltable *llt)
692 struct llentry *lle, *next;
693 struct llentries dchain;
695 KASSERT(llt != NULL, ("%s: llt is NULL", __func__));
699 CK_LIST_INIT(&dchain);
700 IF_AFDATA_WLOCK(llt->llt_ifp);
701 /* Push all lles to @dchain */
702 lltable_foreach_lle(llt, lltable_free_cb, &dchain);
703 llentries_unlink(llt, &dchain);
704 IF_AFDATA_WUNLOCK(llt->llt_ifp);
706 CK_LIST_FOREACH_SAFE(lle, &dchain, lle_chain, next) {
710 KASSERT(llt->llt_entries == 0, ("%s: lltable %p (%s) entires not 0: %d",
711 __func__, llt, llt->llt_ifp->if_xname, llt->llt_entries));
713 llt->llt_free_tbl(llt);
717 * Deletes an address from given lltable.
718 * Used for userland interaction to remove
719 * individual entries. Skips entries added by OS.
722 lltable_delete_addr(struct lltable *llt, u_int flags,
723 const struct sockaddr *l3addr)
729 IF_AFDATA_WLOCK(ifp);
730 lle = lla_lookup(llt, LLE_SF(l3addr->sa_family, LLE_EXCLUSIVE), l3addr);
733 IF_AFDATA_WUNLOCK(ifp);
736 if ((lle->la_flags & LLE_IFADDR) != 0 && (flags & LLE_IFADDR) == 0) {
737 IF_AFDATA_WUNLOCK(ifp);
742 lltable_unlink_entry(llt, lle);
743 IF_AFDATA_WUNLOCK(ifp);
745 llt->llt_delete_entry(llt, lle);
751 lltable_prefix_free(int af, struct sockaddr *addr, struct sockaddr *mask,
756 LLTABLE_LIST_RLOCK();
757 SLIST_FOREACH(llt, &V_lltables, llt_link) {
758 if (llt->llt_af != af)
761 llt->llt_prefix_free(llt, addr, mask, flags);
763 LLTABLE_LIST_RUNLOCK();
767 * Delete llentries that func() returns true.
769 struct lle_match_data {
770 struct llentries dchain;
771 llt_match_cb_t *func;
776 lltable_delete_conditional_cb(struct lltable *llt, struct llentry *lle,
779 struct lle_match_data *lmd;
781 lmd = (struct lle_match_data *)farg;
782 if (lmd->func(llt, lle, lmd->farg)) {
784 CK_LIST_INSERT_HEAD(&lmd->dchain, lle, lle_chain);
791 lltable_delete_conditional(struct lltable *llt, llt_match_cb_t *func,
794 struct llentry *lle, *next;
795 struct lle_match_data lmd;
797 bzero(&lmd, sizeof(lmd));
798 CK_LIST_INIT(&lmd.dchain);
802 IF_AFDATA_WLOCK(llt->llt_ifp);
803 lltable_foreach_lle(llt, lltable_delete_conditional_cb, &lmd);
804 llentries_unlink(llt, &lmd.dchain);
805 IF_AFDATA_WUNLOCK(llt->llt_ifp);
807 CK_LIST_FOREACH_SAFE(lle, &lmd.dchain, lle_chain, next)
808 llt->llt_delete_entry(llt, lle);
812 lltable_allocate_htbl(uint32_t hsize)
817 llt = malloc(sizeof(struct lltable), M_LLTABLE, M_WAITOK | M_ZERO);
818 llt->llt_hsize = hsize;
819 llt->lle_head = malloc(sizeof(struct llentries) * hsize,
820 M_LLTABLE, M_WAITOK | M_ZERO);
822 for (i = 0; i < llt->llt_hsize; i++)
823 CK_LIST_INIT(&llt->lle_head[i]);
825 /* Set some default callbacks */
826 llt->llt_link_entry = htable_link_entry;
827 llt->llt_unlink_entry = htable_unlink_entry;
828 llt->llt_prefix_free = htable_prefix_free;
829 llt->llt_foreach_entry = htable_foreach_lle;
830 llt->llt_free_tbl = htable_free_tbl;
836 * Links lltable to global llt list.
839 lltable_link(struct lltable *llt)
842 LLTABLE_LIST_WLOCK();
843 SLIST_INSERT_HEAD(&V_lltables, llt, llt_link);
844 LLTABLE_LIST_WUNLOCK();
848 lltable_unlink(struct lltable *llt)
851 LLTABLE_LIST_WLOCK();
852 SLIST_REMOVE(&V_lltables, llt, lltable, llt_link);
853 LLTABLE_LIST_WUNLOCK();
858 * Gets interface @ifp lltable for the specified @family
861 lltable_get(struct ifnet *ifp, int family)
866 return (in_lltable_get(ifp));
870 return (in6_lltable_get(ifp));
878 * External methods used by lltable consumers
882 lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg)
885 return (llt->llt_foreach_entry(llt, f, farg));
889 lltable_alloc_entry(struct lltable *llt, u_int flags,
890 const struct sockaddr *l3addr)
893 return (llt->llt_alloc_entry(llt, flags, l3addr));
897 lltable_free_entry(struct lltable *llt, struct llentry *lle)
900 llt->llt_free_entry(llt, lle);
904 lltable_link_entry(struct lltable *llt, struct llentry *lle)
907 return (llt->llt_link_entry(llt, lle));
911 lltable_link_child_entry(struct llentry *lle, struct llentry *child_lle)
913 child_lle->lle_parent = lle;
914 child_lle->lle_tbl = lle->lle_tbl;
915 child_lle->la_flags |= LLE_LINKED;
916 CK_SLIST_INSERT_HEAD(&lle->lle_children, child_lle, lle_child_next);
920 lltable_unlink_child_entry(struct llentry *child_lle)
922 struct llentry *lle = child_lle->lle_parent;
924 child_lle->la_flags &= ~LLE_LINKED;
925 child_lle->lle_parent = NULL;
926 CK_SLIST_REMOVE(&lle->lle_children, child_lle, llentry, lle_child_next);
930 lltable_unlink_entry(struct lltable *llt, struct llentry *lle)
933 return (llt->llt_unlink_entry(lle));
937 lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
942 llt->llt_fill_sa_entry(lle, sa);
946 lltable_get_ifp(const struct lltable *llt)
949 return (llt->llt_ifp);
953 lltable_get_af(const struct lltable *llt)
956 return (llt->llt_af);
960 * Called in route_output when rtm_flags contains RTF_LLDATA.
963 lla_rt_output(struct rt_msghdr *rtm, struct rt_addrinfo *info)
965 struct sockaddr_dl *dl =
966 (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
967 struct sockaddr *dst = (struct sockaddr *)info->rti_info[RTAX_DST];
970 struct llentry *lle, *lle_tmp;
971 uint8_t linkhdr[LLE_MAX_LINKHDR];
977 if (dl == NULL || dl->sdl_family != AF_LINK)
980 /* XXX: should be ntohs() */
981 ifp = ifnet_byindex(dl->sdl_index);
983 log(LOG_INFO, "%s: invalid ifp (sdl_index %d)\n",
984 __func__, dl->sdl_index);
988 llt = lltable_get(ifp, dst->sa_family);
995 switch (rtm->rtm_type) {
999 if (rtm->rtm_rmx.rmx_expire == 0)
1000 laflags = LLE_STATIC;
1001 lle = lltable_alloc_entry(llt, laflags, dst);
1005 linkhdrsize = sizeof(linkhdr);
1006 if (lltable_calc_llheader(ifp, dst->sa_family, LLADDR(dl),
1007 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1008 lltable_free_entry(llt, lle);
1011 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1013 if ((rtm->rtm_flags & RTF_ANNOUNCE))
1014 lle->la_flags |= LLE_PUB;
1015 lle->la_expire = rtm->rtm_rmx.rmx_expire;
1017 laflags = lle->la_flags;
1019 /* Try to link new entry */
1021 IF_AFDATA_WLOCK(ifp);
1023 lle_tmp = lla_lookup(llt, LLE_EXCLUSIVE, dst);
1024 if (lle_tmp != NULL) {
1025 /* Check if we are trying to replace immutable entry */
1026 if ((lle_tmp->la_flags & LLE_IFADDR) != 0) {
1027 IF_AFDATA_WUNLOCK(ifp);
1028 LLE_WUNLOCK(lle_tmp);
1029 lltable_free_entry(llt, lle);
1032 /* Unlink existing entry from table */
1033 lltable_unlink_entry(llt, lle_tmp);
1035 lltable_link_entry(llt, lle);
1036 if ((lle->la_flags & LLE_PUB) != 0 &&
1037 (llt->llt_flags & LLT_ADDEDPROXY) == 0)
1038 llt->llt_flags |= LLT_ADDEDPROXY;
1039 IF_AFDATA_WUNLOCK(ifp);
1041 if (lle_tmp != NULL) {
1042 EVENTHANDLER_INVOKE(lle_event, lle_tmp,LLENTRY_EXPIRED);
1043 lltable_free_entry(llt, lle_tmp);
1047 * By invoking LLE handler here we might get
1048 * two events on static LLE entry insertion
1049 * in routing socket. However, since we might have
1050 * other subscribers we need to generate this event.
1052 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
1054 llt->llt_post_resolved(llt, lle);
1058 return (lltable_delete_addr(llt, 0, dst));
1069 struct llentry base;
1070 struct sockaddr l3_addr;
1074 llatbl_lle_show(struct llentry_sa *la)
1076 struct llentry *lle;
1080 db_printf("lle=%p\n", lle);
1081 db_printf(" lle_next=%p\n", lle->lle_next.cle_next);
1082 db_printf(" lle_lock=%p\n", &lle->lle_lock);
1083 db_printf(" lle_tbl=%p\n", lle->lle_tbl);
1084 db_printf(" lle_head=%p\n", lle->lle_head);
1085 db_printf(" la_hold=%p\n", lle->la_hold);
1086 db_printf(" la_numheld=%d\n", lle->la_numheld);
1087 db_printf(" la_expire=%ju\n", (uintmax_t)lle->la_expire);
1088 db_printf(" la_flags=0x%04x\n", lle->la_flags);
1089 db_printf(" la_asked=%u\n", lle->la_asked);
1090 db_printf(" la_preempt=%u\n", lle->la_preempt);
1091 db_printf(" ln_state=%d\n", lle->ln_state);
1092 db_printf(" ln_router=%u\n", lle->ln_router);
1093 db_printf(" ln_ntick=%ju\n", (uintmax_t)lle->ln_ntick);
1094 db_printf(" lle_refcnt=%d\n", lle->lle_refcnt);
1095 bcopy(lle->ll_addr, octet, sizeof(octet));
1096 db_printf(" ll_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
1097 octet[0], octet[1], octet[2], octet[3], octet[4], octet[5]);
1098 db_printf(" lle_timer=%p\n", &lle->lle_timer);
1100 switch (la->l3_addr.sa_family) {
1104 struct sockaddr_in *sin;
1105 char l3s[INET_ADDRSTRLEN];
1107 sin = (struct sockaddr_in *)&la->l3_addr;
1108 inet_ntoa_r(sin->sin_addr, l3s);
1109 db_printf(" l3_addr=%s\n", l3s);
1116 struct sockaddr_in6 *sin6;
1117 char l3s[INET6_ADDRSTRLEN];
1119 sin6 = (struct sockaddr_in6 *)&la->l3_addr;
1120 ip6_sprintf(l3s, &sin6->sin6_addr);
1121 db_printf(" l3_addr=%s\n", l3s);
1126 db_printf(" l3_addr=N/A (af=%d)\n", la->l3_addr.sa_family);
1131 DB_SHOW_COMMAND(llentry, db_show_llentry)
1135 db_printf("usage: show llentry <struct llentry *>\n");
1139 llatbl_lle_show((struct llentry_sa *)addr);
1143 llatbl_llt_show(struct lltable *llt)
1146 struct llentry *lle;
1148 db_printf("llt=%p llt_af=%d llt_ifp=%p\n",
1149 llt, llt->llt_af, llt->llt_ifp);
1151 for (i = 0; i < llt->llt_hsize; i++) {
1152 CK_LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1153 llatbl_lle_show((struct llentry_sa *)lle);
1160 DB_SHOW_COMMAND(lltable, db_show_lltable)
1164 db_printf("usage: show lltable <struct lltable *>\n");
1168 llatbl_llt_show((struct lltable *)addr);
1171 DB_SHOW_ALL_COMMAND(lltables, db_show_all_lltables)
1173 VNET_ITERATOR_DECL(vnet_iter);
1174 struct lltable *llt;
1176 VNET_FOREACH(vnet_iter) {
1177 CURVNET_SET_QUIET(vnet_iter);
1179 db_printf("vnet=%p\n", curvnet);
1181 SLIST_FOREACH(llt, &V_lltables, llt_link) {
1182 db_printf("llt=%p llt_af=%d llt_ifp=%p(%s)\n",
1183 llt, llt->llt_af, llt->llt_ifp,
1184 (llt->llt_ifp != NULL) ?
1185 llt->llt_ifp->if_xname : "?");
1186 if (have_addr && addr != 0) /* verbose */
1187 llatbl_llt_show(llt);
1188 if (db_pager_quit) {