xref: /freebsd-13-stable/sys/net/if_llatbl.c (revision 94e75f5e29b645e351cec7ec287aef5b48762fd9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
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.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 #include <sys/cdefs.h>
30 #include "opt_ddb.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/eventhandler.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/syslog.h>
40 #include <sys/sysctl.h>
41 #include <sys/socket.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/rwlock.h>
46 
47 #ifdef DDB
48 #include <ddb/ddb.h>
49 #endif
50 
51 #include <vm/uma.h>
52 
53 #include <netinet/in.h>
54 #include <net/if_llatbl.h>
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_var.h>
58 #include <net/route.h>
59 #include <net/route/route_ctl.h>
60 #include <net/route/route_debug.h>
61 #include <net/vnet.h>
62 #include <netinet/if_ether.h>
63 #include <netinet6/in6_var.h>
64 #include <netinet6/nd6.h>
65 
66 MALLOC_DEFINE(M_LLTABLE, "lltable", "link level address tables");
67 
68 VNET_DEFINE_STATIC(SLIST_HEAD(, lltable), lltables) =
69     SLIST_HEAD_INITIALIZER(lltables);
70 #define	V_lltables	VNET(lltables)
71 
72 static struct rwlock lltable_list_lock;
73 RW_SYSINIT(lltable_list_lock, &lltable_list_lock, "lltable_list_lock");
74 #define	LLTABLE_LIST_RLOCK()		rw_rlock(&lltable_list_lock)
75 #define	LLTABLE_LIST_RUNLOCK()		rw_runlock(&lltable_list_lock)
76 #define	LLTABLE_LIST_WLOCK()		rw_wlock(&lltable_list_lock)
77 #define	LLTABLE_LIST_WUNLOCK()		rw_wunlock(&lltable_list_lock)
78 #define	LLTABLE_LIST_LOCK_ASSERT()	rw_assert(&lltable_list_lock, RA_LOCKED)
79 
80 static void lltable_unlink(struct lltable *llt);
81 static void llentries_unlink(struct lltable *llt, struct llentries *head);
82 
83 /*
84  * Dump lle state for a specific address family.
85  */
86 static int
lltable_dump_af(struct lltable * llt,struct sysctl_req * wr)87 lltable_dump_af(struct lltable *llt, struct sysctl_req *wr)
88 {
89 	struct epoch_tracker et;
90 	int error;
91 
92 	LLTABLE_LIST_LOCK_ASSERT();
93 
94 	if (llt->llt_ifp->if_flags & IFF_LOOPBACK)
95 		return (0);
96 	error = 0;
97 
98 	NET_EPOCH_ENTER(et);
99 	error = lltable_foreach_lle(llt,
100 	    (llt_foreach_cb_t *)llt->llt_dump_entry, wr);
101 	NET_EPOCH_EXIT(et);
102 
103 	return (error);
104 }
105 
106 /*
107  * Dump arp state for a specific address family.
108  */
109 int
lltable_sysctl_dumparp(int af,struct sysctl_req * wr)110 lltable_sysctl_dumparp(int af, struct sysctl_req *wr)
111 {
112 	struct lltable *llt;
113 	int error = 0;
114 
115 	LLTABLE_LIST_RLOCK();
116 	SLIST_FOREACH(llt, &V_lltables, llt_link) {
117 		if (llt->llt_af == af) {
118 			error = lltable_dump_af(llt, wr);
119 			if (error != 0)
120 				goto done;
121 		}
122 	}
123 done:
124 	LLTABLE_LIST_RUNLOCK();
125 	return (error);
126 }
127 
128 /*
129  * Adds a mbuf to hold queue. Drops old packets if the queue is full.
130  *
131  * Returns the number of held packets that were dropped.
132  */
133 size_t
lltable_append_entry_queue(struct llentry * lle,struct mbuf * m,size_t maxheld)134 lltable_append_entry_queue(struct llentry *lle, struct mbuf *m,
135     size_t maxheld)
136 {
137 	size_t pkts_dropped = 0;
138 
139 	LLE_WLOCK_ASSERT(lle);
140 
141 	while (lle->la_numheld >= maxheld && lle->la_hold != NULL) {
142 		struct mbuf *next = lle->la_hold->m_nextpkt;
143 		m_freem(lle->la_hold);
144 		lle->la_hold = next;
145 		lle->la_numheld--;
146 		pkts_dropped++;
147 	}
148 
149 	if (lle->la_hold != NULL) {
150 		struct mbuf *curr = lle->la_hold;
151 		while (curr->m_nextpkt != NULL)
152 			curr = curr->m_nextpkt;
153 		curr->m_nextpkt = m;
154 	} else
155 		lle->la_hold = m;
156 
157 	lle->la_numheld++;
158 
159 	return pkts_dropped;
160 }
161 
162 
163 /*
164  * Common function helpers for chained hash table.
165  */
166 
167 /*
168  * Runs specified callback for each entry in @llt.
169  * Caller does the locking.
170  *
171  */
172 static int
htable_foreach_lle(struct lltable * llt,llt_foreach_cb_t * f,void * farg)173 htable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg)
174 {
175 	struct llentry *lle, *next;
176 	int i, error;
177 
178 	error = 0;
179 
180 	for (i = 0; i < llt->llt_hsize; i++) {
181 		CK_LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
182 			error = f(llt, lle, farg);
183 			if (error != 0)
184 				break;
185 		}
186 	}
187 
188 	return (error);
189 }
190 
191 /*
192  * The htable_[un]link_entry() functions return:
193  * 0 if the entry was (un)linked already and nothing changed,
194  * 1 if the entry was added/removed to/from the table, and
195  * -1 on error (e.g., not being able to add the entry due to limits reached).
196  * While the "unlink" operation should never error, callers of
197  * lltable_link_entry() need to check for errors and handle them.
198  */
199 static int
htable_link_entry(struct lltable * llt,struct llentry * lle)200 htable_link_entry(struct lltable *llt, struct llentry *lle)
201 {
202 	struct llentries *lleh;
203 	uint32_t hashidx;
204 
205 	if ((lle->la_flags & LLE_LINKED) != 0)
206 		return (0);
207 
208 	IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
209 
210 	if (llt->llt_maxentries > 0 &&
211 	    llt->llt_entries >= llt->llt_maxentries)
212 		return (-1);
213 
214 	hashidx = llt->llt_hash(lle, llt->llt_hsize);
215 	lleh = &llt->lle_head[hashidx];
216 
217 	lle->lle_tbl  = llt;
218 	lle->lle_head = lleh;
219 	lle->la_flags |= LLE_LINKED;
220 	CK_LIST_INSERT_HEAD(lleh, lle, lle_next);
221 	llt->llt_entries++;
222 
223 	return (1);
224 }
225 
226 static int
htable_unlink_entry(struct llentry * lle)227 htable_unlink_entry(struct llentry *lle)
228 {
229 	struct lltable *llt;
230 
231 	if ((lle->la_flags & LLE_LINKED) == 0)
232 		return (0);
233 
234 	llt = lle->lle_tbl;
235 	IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
236 	KASSERT(llt->llt_entries > 0, ("%s: lltable %p (%s) entries %d <= 0",
237 	    __func__, llt, if_name(llt->llt_ifp), llt->llt_entries));
238 
239 	CK_LIST_REMOVE(lle, lle_next);
240 	lle->la_flags &= ~(LLE_VALID | LLE_LINKED);
241 #if 0
242 	lle->lle_tbl = NULL;
243 	lle->lle_head = NULL;
244 #endif
245 	llt->llt_entries--;
246 
247 	return (1);
248 }
249 
250 struct prefix_match_data {
251 	const struct sockaddr *addr;
252 	const struct sockaddr *mask;
253 	struct llentries dchain;
254 	u_int flags;
255 };
256 
257 static int
htable_prefix_free_cb(struct lltable * llt,struct llentry * lle,void * farg)258 htable_prefix_free_cb(struct lltable *llt, struct llentry *lle, void *farg)
259 {
260 	struct prefix_match_data *pmd;
261 
262 	pmd = (struct prefix_match_data *)farg;
263 
264 	if (llt->llt_match_prefix(pmd->addr, pmd->mask, pmd->flags, lle)) {
265 		LLE_WLOCK(lle);
266 		CK_LIST_INSERT_HEAD(&pmd->dchain, lle, lle_chain);
267 	}
268 
269 	return (0);
270 }
271 
272 static void
htable_prefix_free(struct lltable * llt,const struct sockaddr * addr,const struct sockaddr * mask,u_int flags)273 htable_prefix_free(struct lltable *llt, const struct sockaddr *addr,
274     const struct sockaddr *mask, u_int flags)
275 {
276 	struct llentry *lle, *next;
277 	struct prefix_match_data pmd;
278 
279 	bzero(&pmd, sizeof(pmd));
280 	pmd.addr = addr;
281 	pmd.mask = mask;
282 	pmd.flags = flags;
283 	CK_LIST_INIT(&pmd.dchain);
284 
285 	IF_AFDATA_WLOCK(llt->llt_ifp);
286 	/* Push matching lles to chain */
287 	lltable_foreach_lle(llt, htable_prefix_free_cb, &pmd);
288 
289 	llentries_unlink(llt, &pmd.dchain);
290 	IF_AFDATA_WUNLOCK(llt->llt_ifp);
291 
292 	CK_LIST_FOREACH_SAFE(lle, &pmd.dchain, lle_chain, next)
293 		lltable_free_entry(llt, lle);
294 }
295 
296 static void
htable_free_tbl(struct lltable * llt)297 htable_free_tbl(struct lltable *llt)
298 {
299 
300 	free(llt->lle_head, M_LLTABLE);
301 	free(llt, M_LLTABLE);
302 }
303 
304 static void
llentries_unlink(struct lltable * llt,struct llentries * head)305 llentries_unlink(struct lltable *llt, struct llentries *head)
306 {
307 	struct llentry *lle, *next;
308 
309 	CK_LIST_FOREACH_SAFE(lle, head, lle_chain, next)
310 		llt->llt_unlink_entry(lle);
311 }
312 
313 /*
314  * Helper function used to drop all mbufs in hold queue.
315  *
316  * Returns the number of held packets, if any, that were dropped.
317  */
318 size_t
lltable_drop_entry_queue(struct llentry * lle)319 lltable_drop_entry_queue(struct llentry *lle)
320 {
321 	size_t pkts_dropped = 0;
322 
323 	LLE_WLOCK_ASSERT(lle);
324 
325 	while (lle->la_hold != NULL) {
326 		struct mbuf *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 	KASSERT(lle->la_numheld == 0,
334 		("%s: la_numheld %d > 0, pkts_dropped %zd", __func__,
335 		 lle->la_numheld, pkts_dropped));
336 
337 	return (pkts_dropped);
338 }
339 
340 void
lltable_set_entry_addr(struct ifnet * ifp,struct llentry * lle,const char * linkhdr,size_t linkhdrsize,int lladdr_off)341 lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
342     const char *linkhdr, size_t linkhdrsize, int lladdr_off)
343 {
344 
345 	memcpy(lle->r_linkdata, linkhdr, linkhdrsize);
346 	lle->r_hdrlen = linkhdrsize;
347 	lle->ll_addr = &lle->r_linkdata[lladdr_off];
348 	lle->la_flags |= LLE_VALID;
349 	lle->r_flags |= RLLE_VALID;
350 }
351 
352 /*
353  * Acquires lltable write lock.
354  *
355  * Returns true on success, with both lltable and lle lock held.
356  * On failure, false is returned and lle wlock is still held.
357  */
358 bool
lltable_acquire_wlock(struct ifnet * ifp,struct llentry * lle)359 lltable_acquire_wlock(struct ifnet *ifp, struct llentry *lle)
360 {
361 	NET_EPOCH_ASSERT();
362 
363 	/* Perform real LLE update */
364 	/* use afdata WLOCK to update fields */
365 	LLE_WUNLOCK(lle);
366 	IF_AFDATA_WLOCK(ifp);
367 	LLE_WLOCK(lle);
368 
369 	/*
370 	 * Since we droppped LLE lock, other thread might have deleted
371 	 * this lle. Check and return
372 	 */
373 	if ((lle->la_flags & LLE_DELETED) != 0) {
374 		IF_AFDATA_WUNLOCK(ifp);
375 		return (false);
376 	}
377 
378 	return (true);
379 }
380 
381 /*
382  * Tries to update @lle link-level address.
383  * Since update requires AFDATA WLOCK, function
384  * drops @lle lock, acquires AFDATA lock and then acquires
385  * @lle lock to maintain lock order.
386  *
387  * Returns 1 on success.
388  */
389 int
lltable_try_set_entry_addr(struct ifnet * ifp,struct llentry * lle,const char * linkhdr,size_t linkhdrsize,int lladdr_off)390 lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
391     const char *linkhdr, size_t linkhdrsize, int lladdr_off)
392 {
393 
394 	if (!lltable_acquire_wlock(ifp, lle))
395 		return (0);
396 
397 	/* Update data */
398 	lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize, lladdr_off);
399 
400 	IF_AFDATA_WUNLOCK(ifp);
401 
402 	return (1);
403 }
404 
405  /*
406  * Helper function used to pre-compute full/partial link-layer
407  * header data suitable for feeding into if_output().
408  */
409 int
lltable_calc_llheader(struct ifnet * ifp,int family,char * lladdr,char * buf,size_t * bufsize,int * lladdr_off)410 lltable_calc_llheader(struct ifnet *ifp, int family, char *lladdr,
411     char *buf, size_t *bufsize, int *lladdr_off)
412 {
413 	struct if_encap_req ereq;
414 	int error;
415 
416 	bzero(buf, *bufsize);
417 	bzero(&ereq, sizeof(ereq));
418 	ereq.buf = buf;
419 	ereq.bufsize = *bufsize;
420 	ereq.rtype = IFENCAP_LL;
421 	ereq.family = family;
422 	ereq.lladdr = lladdr;
423 	ereq.lladdr_len = ifp->if_addrlen;
424 	error = ifp->if_requestencap(ifp, &ereq);
425 	if (error == 0) {
426 		*bufsize = ereq.bufsize;
427 		*lladdr_off = ereq.lladdr_off;
428 	}
429 
430 	return (error);
431 }
432 
433 /*
434  * Searches for the child entry matching @family inside @lle.
435  * Returns the entry or NULL.
436  */
437 struct llentry *
llentry_lookup_family(struct llentry * lle,int family)438 llentry_lookup_family(struct llentry *lle, int family)
439 {
440 	struct llentry *child_lle;
441 
442 	if (lle == NULL)
443 		return (NULL);
444 
445 	CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
446 		if (child_lle->r_family == family)
447 			return (child_lle);
448 	}
449 
450 	return (NULL);
451 }
452 
453 /*
454  * Retrieves upper protocol family for the llentry.
455  * By default, all "normal" (e.g. upper_family == transport_family)
456  * llentries have r_family set to 0.
457  * Thus, use @default_family in that regard, otherwise use r_family.
458  *
459  * Returns upper protocol family
460  */
461 int
llentry_get_upper_family(const struct llentry * lle,int default_family)462 llentry_get_upper_family(const struct llentry *lle, int default_family)
463 {
464 	return (lle->r_family == 0 ? default_family : lle->r_family);
465 }
466 
467 /*
468  * Prints llentry @lle data into provided buffer.
469  * Example: lle/inet/valid/em0/1.2.3.4
470  *
471  * Returns @buf.
472  */
473 char *
llentry_print_buf(const struct llentry * lle,struct ifnet * ifp,int family,char * buf,size_t bufsize)474 llentry_print_buf(const struct llentry *lle, struct ifnet *ifp, int family,
475     char *buf, size_t bufsize)
476 {
477 #if defined(INET) || defined(INET6)
478 	char abuf[INET6_ADDRSTRLEN];
479 #endif
480 
481 	const char *valid = (lle->r_flags & RLLE_VALID) ? "valid" : "no_l2";
482 	const char *upper_str = rib_print_family(llentry_get_upper_family(lle, family));
483 
484 	switch (family) {
485 #ifdef INET
486 	case AF_INET:
487 		inet_ntop(AF_INET, &lle->r_l3addr.addr4, abuf, sizeof(abuf));
488 		snprintf(buf, bufsize, "lle/%s/%s/%s/%s", upper_str,
489 		    valid, if_name(ifp), abuf);
490 		break;
491 #endif
492 #ifdef INET6
493 	case AF_INET6:
494 		inet_ntop(AF_INET6, &lle->r_l3addr.addr6, abuf, sizeof(abuf));
495 		snprintf(buf, bufsize, "lle/%s/%s/%s/%s", upper_str,
496 		    valid, if_name(ifp), abuf);
497 		break;
498 #endif
499 	default:
500 		snprintf(buf, bufsize, "lle/%s/%s/%s/????", upper_str,
501 		    valid, if_name(ifp));
502 		break;
503 	}
504 
505 	return (buf);
506 }
507 
508 char *
llentry_print_buf_lltable(const struct llentry * lle,char * buf,size_t bufsize)509 llentry_print_buf_lltable(const struct llentry *lle, char *buf, size_t bufsize)
510 {
511 	struct lltable *tbl = lle->lle_tbl;
512 
513 	return (llentry_print_buf(lle, lltable_get_ifp(tbl), lltable_get_af(tbl), buf, bufsize));
514 }
515 
516 /*
517  * Requests feedback from the datapath.
518  * First packet using @lle should result in
519  * setting r_skip_req back to 0 and updating
520  * lle_hittime to the current time_uptime.
521  */
522 void
llentry_request_feedback(struct llentry * lle)523 llentry_request_feedback(struct llentry *lle)
524 {
525 	struct llentry *child_lle;
526 
527 	LLE_REQ_LOCK(lle);
528 	lle->r_skip_req = 1;
529 	LLE_REQ_UNLOCK(lle);
530 
531 	CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
532 		LLE_REQ_LOCK(child_lle);
533 		child_lle->r_skip_req = 1;
534 		LLE_REQ_UNLOCK(child_lle);
535 	}
536 }
537 
538 /*
539  * Updates the lle state to mark it has been used
540  * and record the time.
541  * Used by the llentry_provide_feedback() wrapper.
542  */
543 void
llentry_mark_used(struct llentry * lle)544 llentry_mark_used(struct llentry *lle)
545 {
546 	LLE_REQ_LOCK(lle);
547 	lle->r_skip_req = 0;
548 	lle->lle_hittime = time_uptime;
549 	LLE_REQ_UNLOCK(lle);
550 }
551 
552 /*
553  * Fetches the time when lle was used.
554  * Return 0 if the entry was not used, relevant time_uptime
555  *  otherwise.
556  */
557 static time_t
llentry_get_hittime_raw(struct llentry * lle)558 llentry_get_hittime_raw(struct llentry *lle)
559 {
560 	time_t lle_hittime = 0;
561 
562 	LLE_REQ_LOCK(lle);
563 	if ((lle->r_skip_req == 0) && (lle_hittime < lle->lle_hittime))
564 		lle_hittime = lle->lle_hittime;
565 	LLE_REQ_UNLOCK(lle);
566 
567 	return (lle_hittime);
568 }
569 
570 time_t
llentry_get_hittime(struct llentry * lle)571 llentry_get_hittime(struct llentry *lle)
572 {
573 	time_t lle_hittime = 0;
574 	struct llentry *child_lle;
575 
576 	lle_hittime = llentry_get_hittime_raw(lle);
577 
578 	CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
579 		time_t hittime = llentry_get_hittime_raw(child_lle);
580 		if (hittime > lle_hittime)
581 			lle_hittime = hittime;
582 	}
583 
584 	return (lle_hittime);
585 }
586 
587 /*
588  * Update link-layer header for given @lle after
589  * interface lladdr was changed.
590  */
591 static int
llentry_update_ifaddr(struct lltable * llt,struct llentry * lle,void * farg)592 llentry_update_ifaddr(struct lltable *llt, struct llentry *lle, void *farg)
593 {
594 	struct ifnet *ifp;
595 	u_char linkhdr[LLE_MAX_LINKHDR];
596 	size_t linkhdrsize;
597 	u_char *lladdr;
598 	int lladdr_off;
599 
600 	ifp = (struct ifnet *)farg;
601 
602 	lladdr = lle->ll_addr;
603 
604 	LLE_WLOCK(lle);
605 	if ((lle->la_flags & LLE_VALID) == 0) {
606 		LLE_WUNLOCK(lle);
607 		return (0);
608 	}
609 
610 	if ((lle->la_flags & LLE_IFADDR) != 0)
611 		lladdr = IF_LLADDR(ifp);
612 
613 	linkhdrsize = sizeof(linkhdr);
614 	lltable_calc_llheader(ifp, llt->llt_af, lladdr, linkhdr, &linkhdrsize,
615 	    &lladdr_off);
616 	memcpy(lle->r_linkdata, linkhdr, linkhdrsize);
617 	LLE_WUNLOCK(lle);
618 
619 	return (0);
620 }
621 
622 /*
623  * Update all calculated headers for given @llt
624  */
625 void
lltable_update_ifaddr(struct lltable * llt)626 lltable_update_ifaddr(struct lltable *llt)
627 {
628 
629 	if (llt->llt_ifp->if_flags & IFF_LOOPBACK)
630 		return;
631 
632 	IF_AFDATA_WLOCK(llt->llt_ifp);
633 	lltable_foreach_lle(llt, llentry_update_ifaddr, llt->llt_ifp);
634 	IF_AFDATA_WUNLOCK(llt->llt_ifp);
635 }
636 
637 /*
638  *
639  * Performs generic cleanup routines and frees lle.
640  *
641  * Called for non-linked entries, with callouts and
642  * other AF-specific cleanups performed.
643  *
644  * @lle must be passed WLOCK'ed
645  *
646  * Returns the number of held packets, if any, that were dropped.
647  */
648 size_t
llentry_free(struct llentry * lle)649 llentry_free(struct llentry *lle)
650 {
651 	size_t pkts_dropped;
652 
653 	LLE_WLOCK_ASSERT(lle);
654 
655 	KASSERT((lle->la_flags & LLE_LINKED) == 0, ("freeing linked lle"));
656 
657 	pkts_dropped = lltable_drop_entry_queue(lle);
658 
659 	/* cancel timer */
660 	if (callout_stop(&lle->lle_timer) > 0)
661 		LLE_REMREF(lle);
662 	LLE_FREE_LOCKED(lle);
663 
664 	return (pkts_dropped);
665 }
666 
667 /*
668  * Free all entries from given table and free itself.
669  */
670 
671 static int
lltable_free_cb(struct lltable * llt,struct llentry * lle,void * farg)672 lltable_free_cb(struct lltable *llt, struct llentry *lle, void *farg)
673 {
674 	struct llentries *dchain;
675 
676 	dchain = (struct llentries *)farg;
677 
678 	LLE_WLOCK(lle);
679 	CK_LIST_INSERT_HEAD(dchain, lle, lle_chain);
680 
681 	return (0);
682 }
683 
684 /*
685  * Free all entries from given table and free itself.
686  */
687 void
lltable_free(struct lltable * llt)688 lltable_free(struct lltable *llt)
689 {
690 	struct llentry *lle, *next;
691 	struct llentries dchain;
692 
693 	KASSERT(llt != NULL, ("%s: llt is NULL", __func__));
694 
695 	lltable_unlink(llt);
696 
697 	CK_LIST_INIT(&dchain);
698 	IF_AFDATA_WLOCK(llt->llt_ifp);
699 	/* Push all lles to @dchain */
700 	lltable_foreach_lle(llt, lltable_free_cb, &dchain);
701 	llentries_unlink(llt, &dchain);
702 	IF_AFDATA_WUNLOCK(llt->llt_ifp);
703 
704 	CK_LIST_FOREACH_SAFE(lle, &dchain, lle_chain, next) {
705 		llentry_free(lle);
706 	}
707 
708 	KASSERT(llt->llt_entries == 0, ("%s: lltable %p (%s) entries not 0: %d",
709 	    __func__, llt, llt->llt_ifp->if_xname, llt->llt_entries));
710 
711 	llt->llt_free_tbl(llt);
712 }
713 
714 /*
715  * Deletes an address from given lltable.
716  * Used for userland interaction to remove
717  * individual entries. Skips entries added by OS.
718  */
719 int
lltable_delete_addr(struct lltable * llt,u_int flags,const struct sockaddr * l3addr)720 lltable_delete_addr(struct lltable *llt, u_int flags,
721     const struct sockaddr *l3addr)
722 {
723 	struct llentry *lle;
724 	struct ifnet *ifp;
725 
726 	ifp = llt->llt_ifp;
727 	IF_AFDATA_WLOCK(ifp);
728 	lle = lla_lookup(llt, LLE_SF(l3addr->sa_family, LLE_EXCLUSIVE), l3addr);
729 
730 	if (lle == NULL) {
731 		IF_AFDATA_WUNLOCK(ifp);
732 		return (ENOENT);
733 	}
734 	if ((lle->la_flags & LLE_IFADDR) != 0 && (flags & LLE_IFADDR) == 0) {
735 		IF_AFDATA_WUNLOCK(ifp);
736 		LLE_WUNLOCK(lle);
737 		return (EPERM);
738 	}
739 
740 	lltable_unlink_entry(llt, lle);
741 	IF_AFDATA_WUNLOCK(ifp);
742 
743 	llt->llt_delete_entry(llt, lle);
744 
745 	return (0);
746 }
747 
748 void
lltable_prefix_free(int af,struct sockaddr * addr,struct sockaddr * mask,u_int flags)749 lltable_prefix_free(int af, struct sockaddr *addr, struct sockaddr *mask,
750     u_int flags)
751 {
752 	struct lltable *llt;
753 
754 	LLTABLE_LIST_RLOCK();
755 	SLIST_FOREACH(llt, &V_lltables, llt_link) {
756 		if (llt->llt_af != af)
757 			continue;
758 
759 		llt->llt_prefix_free(llt, addr, mask, flags);
760 	}
761 	LLTABLE_LIST_RUNLOCK();
762 }
763 
764 struct lltable *
lltable_allocate_htbl(uint32_t hsize)765 lltable_allocate_htbl(uint32_t hsize)
766 {
767 	struct lltable *llt;
768 	int i;
769 
770 	llt = malloc(sizeof(struct lltable), M_LLTABLE, M_WAITOK | M_ZERO);
771 	llt->llt_hsize = hsize;
772 	llt->lle_head = malloc(sizeof(struct llentries) * hsize,
773 	    M_LLTABLE, M_WAITOK | M_ZERO);
774 
775 	for (i = 0; i < llt->llt_hsize; i++)
776 		CK_LIST_INIT(&llt->lle_head[i]);
777 
778 	/* Set some default callbacks */
779 	llt->llt_link_entry = htable_link_entry;
780 	llt->llt_unlink_entry = htable_unlink_entry;
781 	llt->llt_prefix_free = htable_prefix_free;
782 	llt->llt_foreach_entry = htable_foreach_lle;
783 	llt->llt_free_tbl = htable_free_tbl;
784 
785 	return (llt);
786 }
787 
788 /*
789  * Links lltable to global llt list.
790  */
791 void
lltable_link(struct lltable * llt)792 lltable_link(struct lltable *llt)
793 {
794 
795 	LLTABLE_LIST_WLOCK();
796 	SLIST_INSERT_HEAD(&V_lltables, llt, llt_link);
797 	LLTABLE_LIST_WUNLOCK();
798 }
799 
800 static void
lltable_unlink(struct lltable * llt)801 lltable_unlink(struct lltable *llt)
802 {
803 
804 	LLTABLE_LIST_WLOCK();
805 	SLIST_REMOVE(&V_lltables, llt, lltable, llt_link);
806 	LLTABLE_LIST_WUNLOCK();
807 
808 }
809 
810 /*
811  * Gets interface @ifp lltable for the specified @family
812  */
813 struct lltable *
lltable_get(struct ifnet * ifp,int family)814 lltable_get(struct ifnet *ifp, int family)
815 {
816 	switch (family) {
817 #ifdef INET
818 	case AF_INET:
819 		return (in_lltable_get(ifp));
820 #endif
821 #ifdef INET6
822 	case AF_INET6:
823 		return (in6_lltable_get(ifp));
824 #endif
825 	}
826 
827 	return (NULL);
828 }
829 
830 /*
831  * External methods used by lltable consumers
832  */
833 
834 int
lltable_foreach_lle(struct lltable * llt,llt_foreach_cb_t * f,void * farg)835 lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg)
836 {
837 
838 	return (llt->llt_foreach_entry(llt, f, farg));
839 }
840 
841 struct llentry *
lltable_alloc_entry(struct lltable * llt,u_int flags,const struct sockaddr * l3addr)842 lltable_alloc_entry(struct lltable *llt, u_int flags,
843     const struct sockaddr *l3addr)
844 {
845 
846 	return (llt->llt_alloc_entry(llt, flags, l3addr));
847 }
848 
849 void
lltable_free_entry(struct lltable * llt,struct llentry * lle)850 lltable_free_entry(struct lltable *llt, struct llentry *lle)
851 {
852 
853 	llt->llt_free_entry(llt, lle);
854 }
855 
856 int
lltable_link_entry(struct lltable * llt,struct llentry * lle)857 lltable_link_entry(struct lltable *llt, struct llentry *lle)
858 {
859 
860 	return (llt->llt_link_entry(llt, lle));
861 }
862 
863 void
lltable_link_child_entry(struct llentry * lle,struct llentry * child_lle)864 lltable_link_child_entry(struct llentry *lle, struct llentry *child_lle)
865 {
866 	child_lle->lle_parent = lle;
867 	child_lle->lle_tbl = lle->lle_tbl;
868 	child_lle->la_flags |= LLE_LINKED;
869 	CK_SLIST_INSERT_HEAD(&lle->lle_children, child_lle, lle_child_next);
870 }
871 
872 void
lltable_unlink_child_entry(struct llentry * child_lle)873 lltable_unlink_child_entry(struct llentry *child_lle)
874 {
875 	struct llentry *lle = child_lle->lle_parent;
876 
877 	child_lle->la_flags &= ~LLE_LINKED;
878 	child_lle->lle_parent = NULL;
879 	CK_SLIST_REMOVE(&lle->lle_children, child_lle, llentry, lle_child_next);
880 }
881 
882 int
lltable_unlink_entry(struct lltable * llt,struct llentry * lle)883 lltable_unlink_entry(struct lltable *llt, struct llentry *lle)
884 {
885 
886 	return (llt->llt_unlink_entry(lle));
887 }
888 
889 void
lltable_fill_sa_entry(const struct llentry * lle,struct sockaddr * sa)890 lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
891 {
892 	struct lltable *llt;
893 
894 	llt = lle->lle_tbl;
895 	llt->llt_fill_sa_entry(lle, sa);
896 }
897 
898 struct ifnet *
lltable_get_ifp(const struct lltable * llt)899 lltable_get_ifp(const struct lltable *llt)
900 {
901 
902 	return (llt->llt_ifp);
903 }
904 
905 int
lltable_get_af(const struct lltable * llt)906 lltable_get_af(const struct lltable *llt)
907 {
908 
909 	return (llt->llt_af);
910 }
911 
912 /*
913  * Called in route_output when rtm_flags contains RTF_LLDATA.
914  */
915 int
lla_rt_output(struct rt_msghdr * rtm,struct rt_addrinfo * info)916 lla_rt_output(struct rt_msghdr *rtm, struct rt_addrinfo *info)
917 {
918 	struct sockaddr_dl *dl =
919 	    (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
920 	struct sockaddr *dst = (struct sockaddr *)info->rti_info[RTAX_DST];
921 	struct ifnet *ifp;
922 	struct lltable *llt;
923 	struct llentry *lle, *lle_tmp;
924 	uint8_t linkhdr[LLE_MAX_LINKHDR];
925 	size_t linkhdrsize;
926 	int lladdr_off;
927 	u_int laflags = 0;
928 	int error;
929 
930 	if (dl == NULL || dl->sdl_family != AF_LINK)
931 		return (EINVAL);
932 
933 	/* XXX: should be ntohs() */
934 	ifp = ifnet_byindex(dl->sdl_index);
935 	if (ifp == NULL) {
936 		log(LOG_INFO, "%s: invalid ifp (sdl_index %d)\n",
937 		    __func__, dl->sdl_index);
938 		return EINVAL;
939 	}
940 
941 	llt = lltable_get(ifp, dst->sa_family);
942 
943 	if (llt == NULL)
944 		return (ESRCH);
945 
946 	error = 0;
947 
948 	switch (rtm->rtm_type) {
949 	case RTM_ADD:
950 		/* Add static LLE */
951 		laflags = 0;
952 		if (rtm->rtm_rmx.rmx_expire == 0)
953 			laflags = LLE_STATIC;
954 		lle = lltable_alloc_entry(llt, laflags, dst);
955 		if (lle == NULL)
956 			return (ENOMEM);
957 
958 		linkhdrsize = sizeof(linkhdr);
959 		if (lltable_calc_llheader(ifp, dst->sa_family, LLADDR(dl),
960 		    linkhdr, &linkhdrsize, &lladdr_off) != 0) {
961 			lltable_free_entry(llt, lle);
962 			return (EINVAL);
963 		}
964 		lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
965 		    lladdr_off);
966 		if ((rtm->rtm_flags & RTF_ANNOUNCE))
967 			lle->la_flags |= LLE_PUB;
968 		lle->la_expire = rtm->rtm_rmx.rmx_expire;
969 
970 		laflags = lle->la_flags;
971 
972 		/* Try to link new entry */
973 		lle_tmp = NULL;
974 		IF_AFDATA_WLOCK(ifp);
975 		LLE_WLOCK(lle);
976 		lle_tmp = lla_lookup(llt, LLE_EXCLUSIVE, dst);
977 		if (lle_tmp != NULL) {
978 			/* Check if we are trying to replace immutable entry */
979 			if ((lle_tmp->la_flags & LLE_IFADDR) != 0) {
980 				IF_AFDATA_WUNLOCK(ifp);
981 				LLE_WUNLOCK(lle_tmp);
982 				lltable_free_entry(llt, lle);
983 				return (EPERM);
984 			}
985 			/* Unlink existing entry from table */
986 			lltable_unlink_entry(llt, lle_tmp);
987 		}
988 		lltable_link_entry(llt, lle);
989 		IF_AFDATA_WUNLOCK(ifp);
990 
991 		if (lle_tmp != NULL) {
992 			EVENTHANDLER_INVOKE(lle_event, lle_tmp,LLENTRY_EXPIRED);
993 			lltable_free_entry(llt, lle_tmp);
994 		}
995 
996 		/*
997 		 * By invoking LLE handler here we might get
998 		 * two events on static LLE entry insertion
999 		 * in routing socket. However, since we might have
1000 		 * other subscribers we need to generate this event.
1001 		 */
1002 		EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
1003 		LLE_WUNLOCK(lle);
1004 #ifdef INET
1005 		/* gratuitous ARP */
1006 		if ((laflags & LLE_PUB) && dst->sa_family == AF_INET)
1007 			arprequest(ifp,
1008 			    &((struct sockaddr_in *)dst)->sin_addr,
1009 			    &((struct sockaddr_in *)dst)->sin_addr,
1010 			    (u_char *)LLADDR(dl));
1011 #endif
1012 
1013 		break;
1014 
1015 	case RTM_DELETE:
1016 		return (lltable_delete_addr(llt, 0, dst));
1017 
1018 	default:
1019 		error = EINVAL;
1020 	}
1021 
1022 	return (error);
1023 }
1024 
1025 #ifdef DDB
1026 struct llentry_sa {
1027 	struct llentry		base;
1028 	struct sockaddr		l3_addr;
1029 };
1030 
1031 static void
llatbl_lle_show(struct llentry_sa * la)1032 llatbl_lle_show(struct llentry_sa *la)
1033 {
1034 	struct llentry *lle;
1035 	uint8_t octet[6];
1036 
1037 	lle = &la->base;
1038 	db_printf("lle=%p\n", lle);
1039 	db_printf(" lle_next=%p\n", lle->lle_next.cle_next);
1040 	db_printf(" lle_lock=%p\n", &lle->lle_lock);
1041 	db_printf(" lle_tbl=%p\n", lle->lle_tbl);
1042 	db_printf(" lle_head=%p\n", lle->lle_head);
1043 	db_printf(" la_hold=%p\n", lle->la_hold);
1044 	db_printf(" la_numheld=%d\n", lle->la_numheld);
1045 	db_printf(" la_expire=%ju\n", (uintmax_t)lle->la_expire);
1046 	db_printf(" la_flags=0x%04x\n", lle->la_flags);
1047 	db_printf(" la_asked=%u\n", lle->la_asked);
1048 	db_printf(" la_preempt=%u\n", lle->la_preempt);
1049 	db_printf(" ln_state=%d\n", lle->ln_state);
1050 	db_printf(" ln_router=%u\n", lle->ln_router);
1051 	db_printf(" ln_ntick=%ju\n", (uintmax_t)lle->ln_ntick);
1052 	db_printf(" lle_refcnt=%d\n", lle->lle_refcnt);
1053 	bcopy(lle->ll_addr, octet, sizeof(octet));
1054 	db_printf(" ll_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
1055 	    octet[0], octet[1], octet[2], octet[3], octet[4], octet[5]);
1056 	db_printf(" lle_timer=%p\n", &lle->lle_timer);
1057 
1058 	switch (la->l3_addr.sa_family) {
1059 #ifdef INET
1060 	case AF_INET:
1061 	{
1062 		struct sockaddr_in *sin;
1063 		char l3s[INET_ADDRSTRLEN];
1064 
1065 		sin = (struct sockaddr_in *)&la->l3_addr;
1066 		inet_ntoa_r(sin->sin_addr, l3s);
1067 		db_printf(" l3_addr=%s\n", l3s);
1068 		break;
1069 	}
1070 #endif
1071 #ifdef INET6
1072 	case AF_INET6:
1073 	{
1074 		struct sockaddr_in6 *sin6;
1075 		char l3s[INET6_ADDRSTRLEN];
1076 
1077 		sin6 = (struct sockaddr_in6 *)&la->l3_addr;
1078 		ip6_sprintf(l3s, &sin6->sin6_addr);
1079 		db_printf(" l3_addr=%s\n", l3s);
1080 		break;
1081 	}
1082 #endif
1083 	default:
1084 		db_printf(" l3_addr=N/A (af=%d)\n", la->l3_addr.sa_family);
1085 		break;
1086 	}
1087 }
1088 
DB_SHOW_COMMAND(llentry,db_show_llentry)1089 DB_SHOW_COMMAND(llentry, db_show_llentry)
1090 {
1091 
1092 	if (!have_addr) {
1093 		db_printf("usage: show llentry <struct llentry *>\n");
1094 		return;
1095 	}
1096 
1097 	llatbl_lle_show((struct llentry_sa *)addr);
1098 }
1099 
1100 static void
llatbl_llt_show(struct lltable * llt)1101 llatbl_llt_show(struct lltable *llt)
1102 {
1103 	int i;
1104 	struct llentry *lle;
1105 
1106 	db_printf("llt=%p llt_af=%d llt_ifp=%p\n",
1107 	    llt, llt->llt_af, llt->llt_ifp);
1108 
1109 	for (i = 0; i < llt->llt_hsize; i++) {
1110 		CK_LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1111 			llatbl_lle_show((struct llentry_sa *)lle);
1112 			if (db_pager_quit)
1113 				return;
1114 		}
1115 	}
1116 }
1117 
DB_SHOW_COMMAND(lltable,db_show_lltable)1118 DB_SHOW_COMMAND(lltable, db_show_lltable)
1119 {
1120 
1121 	if (!have_addr) {
1122 		db_printf("usage: show lltable <struct lltable *>\n");
1123 		return;
1124 	}
1125 
1126 	llatbl_llt_show((struct lltable *)addr);
1127 }
1128 
DB_SHOW_ALL_COMMAND(lltables,db_show_all_lltables)1129 DB_SHOW_ALL_COMMAND(lltables, db_show_all_lltables)
1130 {
1131 	VNET_ITERATOR_DECL(vnet_iter);
1132 	struct lltable *llt;
1133 
1134 	VNET_FOREACH(vnet_iter) {
1135 		CURVNET_SET_QUIET(vnet_iter);
1136 #ifdef VIMAGE
1137 		db_printf("vnet=%p\n", curvnet);
1138 #endif
1139 		SLIST_FOREACH(llt, &V_lltables, llt_link) {
1140 			db_printf("llt=%p llt_af=%d llt_ifp=%p(%s)\n",
1141 			    llt, llt->llt_af, llt->llt_ifp,
1142 			    (llt->llt_ifp != NULL) ?
1143 				llt->llt_ifp->if_xname : "?");
1144 			if (have_addr && addr != 0) /* verbose */
1145 				llatbl_llt_show(llt);
1146 			if (db_pager_quit) {
1147 				CURVNET_RESTORE();
1148 				return;
1149 			}
1150 		}
1151 		CURVNET_RESTORE();
1152 	}
1153 }
1154 #endif
1155