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/if_private.h>
59 #include <net/route.h>
60 #include <net/route/route_ctl.h>
61 #include <net/route/route_debug.h>
62 #include <net/vnet.h>
63 #include <netinet/if_ether.h>
64 #include <netinet6/in6_var.h>
65 #include <netinet6/nd6.h>
66
67 MALLOC_DEFINE(M_LLTABLE, "lltable", "link level address tables");
68
69 VNET_DEFINE_STATIC(SLIST_HEAD(, lltable), lltables) =
70 SLIST_HEAD_INITIALIZER(lltables);
71 #define V_lltables VNET(lltables)
72
73 static struct rwlock lltable_list_lock;
74 RW_SYSINIT(lltable_list_lock, &lltable_list_lock, "lltable_list_lock");
75 #define LLTABLE_LIST_RLOCK() rw_rlock(&lltable_list_lock)
76 #define LLTABLE_LIST_RUNLOCK() rw_runlock(&lltable_list_lock)
77 #define LLTABLE_LIST_WLOCK() rw_wlock(&lltable_list_lock)
78 #define LLTABLE_LIST_WUNLOCK() rw_wunlock(&lltable_list_lock)
79 #define LLTABLE_LIST_LOCK_ASSERT() rw_assert(&lltable_list_lock, RA_LOCKED)
80
81 static void lltable_unlink(struct lltable *llt);
82 static void llentries_unlink(struct lltable *llt, struct llentries *head);
83
84 /*
85 * Dump lle state for a specific address family.
86 */
87 static int
lltable_dump_af(struct lltable * llt,struct sysctl_req * wr)88 lltable_dump_af(struct lltable *llt, struct sysctl_req *wr)
89 {
90 struct epoch_tracker et;
91 int error;
92
93 LLTABLE_LIST_LOCK_ASSERT();
94
95 if (llt->llt_ifp->if_flags & IFF_LOOPBACK)
96 return (0);
97 error = 0;
98
99 NET_EPOCH_ENTER(et);
100 error = lltable_foreach_lle(llt,
101 (llt_foreach_cb_t *)llt->llt_dump_entry, wr);
102 NET_EPOCH_EXIT(et);
103
104 return (error);
105 }
106
107 /*
108 * Dump arp state for a specific address family.
109 */
110 int
lltable_sysctl_dumparp(int af,struct sysctl_req * wr)111 lltable_sysctl_dumparp(int af, struct sysctl_req *wr)
112 {
113 struct lltable *llt;
114 int error = 0;
115
116 LLTABLE_LIST_RLOCK();
117 SLIST_FOREACH(llt, &V_lltables, llt_link) {
118 if (llt->llt_af == af) {
119 error = lltable_dump_af(llt, wr);
120 if (error != 0)
121 goto done;
122 }
123 }
124 done:
125 LLTABLE_LIST_RUNLOCK();
126 return (error);
127 }
128
129 /*
130 * Adds a mbuf to hold queue. Drops old packets if the queue is full.
131 *
132 * Returns the number of held packets that were dropped.
133 */
134 size_t
lltable_append_entry_queue(struct llentry * lle,struct mbuf * m,size_t maxheld)135 lltable_append_entry_queue(struct llentry *lle, struct mbuf *m,
136 size_t maxheld)
137 {
138 size_t pkts_dropped = 0;
139
140 LLE_WLOCK_ASSERT(lle);
141
142 while (lle->la_numheld >= maxheld && lle->la_hold != NULL) {
143 struct mbuf *next = lle->la_hold->m_nextpkt;
144 m_freem(lle->la_hold);
145 lle->la_hold = next;
146 lle->la_numheld--;
147 pkts_dropped++;
148 }
149
150 if (lle->la_hold != NULL) {
151 struct mbuf *curr = lle->la_hold;
152 while (curr->m_nextpkt != NULL)
153 curr = curr->m_nextpkt;
154 curr->m_nextpkt = m;
155 } else
156 lle->la_hold = m;
157
158 lle->la_numheld++;
159
160 return pkts_dropped;
161 }
162
163
164 /*
165 * Common function helpers for chained hash table.
166 */
167
168 /*
169 * Runs specified callback for each entry in @llt.
170 * Caller does the locking.
171 *
172 */
173 static int
htable_foreach_lle(struct lltable * llt,llt_foreach_cb_t * f,void * farg)174 htable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg)
175 {
176 struct llentry *lle, *next;
177 int i, error;
178
179 error = 0;
180
181 for (i = 0; i < llt->llt_hsize; i++) {
182 CK_LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
183 error = f(llt, lle, farg);
184 if (error != 0)
185 break;
186 }
187 }
188
189 return (error);
190 }
191
192 /*
193 * The htable_[un]link_entry() functions return:
194 * 0 if the entry was (un)linked already and nothing changed,
195 * 1 if the entry was added/removed to/from the table, and
196 * -1 on error (e.g., not being able to add the entry due to limits reached).
197 * While the "unlink" operation should never error, callers of
198 * lltable_link_entry() need to check for errors and handle them.
199 */
200 static int
htable_link_entry(struct lltable * llt,struct llentry * lle)201 htable_link_entry(struct lltable *llt, struct llentry *lle)
202 {
203 struct llentries *lleh;
204 uint32_t hashidx;
205
206 if ((lle->la_flags & LLE_LINKED) != 0)
207 return (0);
208
209 LLTABLE_LOCK_ASSERT(llt);
210
211 if (llt->llt_maxentries > 0 &&
212 llt->llt_entries >= llt->llt_maxentries)
213 return (-1);
214
215 hashidx = llt->llt_hash(lle, llt->llt_hsize);
216 lleh = &llt->lle_head[hashidx];
217
218 lle->lle_tbl = llt;
219 lle->lle_head = lleh;
220 lle->la_flags |= LLE_LINKED;
221 CK_LIST_INSERT_HEAD(lleh, lle, lle_next);
222 llt->llt_entries++;
223
224 return (1);
225 }
226
227 static int
htable_unlink_entry(struct llentry * lle)228 htable_unlink_entry(struct llentry *lle)
229 {
230 struct lltable *llt;
231
232 if ((lle->la_flags & LLE_LINKED) == 0)
233 return (0);
234
235 llt = lle->lle_tbl;
236 LLTABLE_LOCK_ASSERT(llt);
237 KASSERT(llt->llt_entries > 0, ("%s: lltable %p (%s) entries %d <= 0",
238 __func__, llt, if_name(llt->llt_ifp), llt->llt_entries));
239
240 CK_LIST_REMOVE(lle, lle_next);
241 lle->la_flags &= ~(LLE_VALID | LLE_LINKED);
242 #if 0
243 lle->lle_tbl = NULL;
244 lle->lle_head = NULL;
245 #endif
246 llt->llt_entries--;
247
248 return (1);
249 }
250
251 struct prefix_match_data {
252 const struct sockaddr *addr;
253 const struct sockaddr *mask;
254 struct llentries dchain;
255 u_int flags;
256 };
257
258 static int
htable_prefix_free_cb(struct lltable * llt,struct llentry * lle,void * farg)259 htable_prefix_free_cb(struct lltable *llt, struct llentry *lle, void *farg)
260 {
261 struct prefix_match_data *pmd;
262
263 pmd = (struct prefix_match_data *)farg;
264
265 if (llt->llt_match_prefix(pmd->addr, pmd->mask, pmd->flags, lle)) {
266 LLE_WLOCK(lle);
267 CK_LIST_INSERT_HEAD(&pmd->dchain, lle, lle_chain);
268 }
269
270 return (0);
271 }
272
273 static void
htable_prefix_free(struct lltable * llt,const struct sockaddr * addr,const struct sockaddr * mask,u_int flags)274 htable_prefix_free(struct lltable *llt, const struct sockaddr *addr,
275 const struct sockaddr *mask, u_int flags)
276 {
277 struct llentry *lle, *next;
278 struct prefix_match_data pmd;
279
280 bzero(&pmd, sizeof(pmd));
281 pmd.addr = addr;
282 pmd.mask = mask;
283 pmd.flags = flags;
284 CK_LIST_INIT(&pmd.dchain);
285
286 LLTABLE_LOCK(llt);
287 /* Push matching lles to chain */
288 lltable_foreach_lle(llt, htable_prefix_free_cb, &pmd);
289
290 llentries_unlink(llt, &pmd.dchain);
291 LLTABLE_UNLOCK(llt);
292
293 CK_LIST_FOREACH_SAFE(lle, &pmd.dchain, lle_chain, next)
294 lltable_free_entry(llt, lle);
295 }
296
297 static void
htable_free_tbl(struct lltable * llt)298 htable_free_tbl(struct lltable *llt)
299 {
300
301 mtx_destroy(&llt->llt_lock);
302 free(llt->lle_head, M_LLTABLE);
303 free(llt, M_LLTABLE);
304 }
305
306 static void
llentries_unlink(struct lltable * llt,struct llentries * head)307 llentries_unlink(struct lltable *llt, struct llentries *head)
308 {
309 struct llentry *lle, *next;
310
311 CK_LIST_FOREACH_SAFE(lle, head, lle_chain, next)
312 llt->llt_unlink_entry(lle);
313 }
314
315 /*
316 * Helper function used to drop all mbufs in hold queue.
317 *
318 * Returns the number of held packets, if any, that were dropped.
319 */
320 size_t
lltable_drop_entry_queue(struct llentry * lle)321 lltable_drop_entry_queue(struct llentry *lle)
322 {
323 size_t pkts_dropped = 0;
324
325 LLE_WLOCK_ASSERT(lle);
326
327 while (lle->la_hold != NULL) {
328 struct mbuf *next = lle->la_hold->m_nextpkt;
329 m_freem(lle->la_hold);
330 lle->la_hold = next;
331 lle->la_numheld--;
332 pkts_dropped++;
333 }
334
335 KASSERT(lle->la_numheld == 0,
336 ("%s: la_numheld %d > 0, pkts_dropped %zd", __func__,
337 lle->la_numheld, pkts_dropped));
338
339 return (pkts_dropped);
340 }
341
342 void
lltable_set_entry_addr(struct ifnet * ifp,struct llentry * lle,const char * linkhdr,size_t linkhdrsize,int lladdr_off)343 lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
344 const char *linkhdr, size_t linkhdrsize, int lladdr_off)
345 {
346
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;
352 }
353
354 /*
355 * Acquires lltable write lock.
356 *
357 * Returns true on success, with both lltable and lle lock held.
358 * On failure, false is returned and lle wlock is still held.
359 */
360 bool
lltable_trylock(struct llentry * lle)361 lltable_trylock(struct llentry *lle)
362 {
363 NET_EPOCH_ASSERT();
364
365 LLE_WUNLOCK(lle);
366 LLTABLE_LOCK(lle->lle_tbl);
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 LLTABLE_UNLOCK(lle->lle_tbl);
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_trylock(lle))
395 return (0);
396
397 /* Update data */
398 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize, lladdr_off);
399
400 LLTABLE_UNLOCK(lle->lle_tbl);
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 LLTABLE_LOCK(llt);
633 lltable_foreach_lle(llt, llentry_update_ifaddr, llt->llt_ifp);
634 LLTABLE_UNLOCK(llt);
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 LLTABLE_LOCK(llt);
699 /* Push all lles to @dchain */
700 lltable_foreach_lle(llt, lltable_free_cb, &dchain);
701 llentries_unlink(llt, &dchain);
702 LLTABLE_UNLOCK(llt);
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
725 LLTABLE_LOCK(llt);
726 lle = lla_lookup(llt, LLE_SF(l3addr->sa_family, LLE_EXCLUSIVE), l3addr);
727
728 if (lle == NULL) {
729 LLTABLE_UNLOCK(llt);
730 return (ENOENT);
731 }
732 if ((lle->la_flags & LLE_IFADDR) != 0 && (flags & LLE_IFADDR) == 0) {
733 LLTABLE_UNLOCK(llt);
734 LLE_WUNLOCK(lle);
735 return (EPERM);
736 }
737
738 lltable_unlink_entry(llt, lle);
739 LLTABLE_UNLOCK(llt);
740
741 llt->llt_delete_entry(llt, lle);
742
743 return (0);
744 }
745
746 void
lltable_prefix_free(int af,struct sockaddr * addr,struct sockaddr * mask,u_int flags)747 lltable_prefix_free(int af, struct sockaddr *addr, struct sockaddr *mask,
748 u_int flags)
749 {
750 struct lltable *llt;
751
752 LLTABLE_LIST_RLOCK();
753 SLIST_FOREACH(llt, &V_lltables, llt_link) {
754 if (llt->llt_af != af)
755 continue;
756
757 llt->llt_prefix_free(llt, addr, mask, flags);
758 }
759 LLTABLE_LIST_RUNLOCK();
760 }
761
762 /*
763 * Delete llentries that func() returns true.
764 */
765 struct lle_match_data {
766 struct llentries dchain;
767 llt_match_cb_t *func;
768 void *farg;
769 };
770
771 static int
lltable_delete_conditional_cb(struct lltable * llt,struct llentry * lle,void * farg)772 lltable_delete_conditional_cb(struct lltable *llt, struct llentry *lle,
773 void *farg)
774 {
775 struct lle_match_data *lmd;
776
777 lmd = (struct lle_match_data *)farg;
778 if (lmd->func(llt, lle, lmd->farg)) {
779 LLE_WLOCK(lle);
780 CK_LIST_INSERT_HEAD(&lmd->dchain, lle, lle_chain);
781 }
782
783 return (0);
784 }
785
786 void
lltable_delete_conditional(struct lltable * llt,llt_match_cb_t * func,void * farg)787 lltable_delete_conditional(struct lltable *llt, llt_match_cb_t *func,
788 void *farg)
789 {
790 struct llentry *lle, *next;
791 struct lle_match_data lmd;
792
793 bzero(&lmd, sizeof(lmd));
794 CK_LIST_INIT(&lmd.dchain);
795 lmd.func = func;
796 lmd.farg = farg;
797
798 LLTABLE_LOCK(llt);
799 lltable_foreach_lle(llt, lltable_delete_conditional_cb, &lmd);
800 llentries_unlink(llt, &lmd.dchain);
801 LLTABLE_UNLOCK(llt);
802
803 CK_LIST_FOREACH_SAFE(lle, &lmd.dchain, lle_chain, next)
804 llt->llt_delete_entry(llt, lle);
805 }
806
807 struct lltable *
lltable_allocate_htbl(uint32_t hsize)808 lltable_allocate_htbl(uint32_t hsize)
809 {
810 struct lltable *llt;
811 int i;
812
813 llt = malloc(sizeof(struct lltable), M_LLTABLE, M_WAITOK | M_ZERO);
814 llt->llt_hsize = hsize;
815 llt->lle_head = malloc(sizeof(struct llentries) * hsize,
816 M_LLTABLE, M_WAITOK | M_ZERO);
817 mtx_init(&llt->llt_lock, "lltable", NULL, MTX_DEF);
818
819 for (i = 0; i < llt->llt_hsize; i++)
820 CK_LIST_INIT(&llt->lle_head[i]);
821
822 /* Set some default callbacks */
823 llt->llt_link_entry = htable_link_entry;
824 llt->llt_unlink_entry = htable_unlink_entry;
825 llt->llt_prefix_free = htable_prefix_free;
826 llt->llt_foreach_entry = htable_foreach_lle;
827 llt->llt_free_tbl = htable_free_tbl;
828
829 return (llt);
830 }
831
832 /*
833 * Links lltable to global llt list.
834 */
835 void
lltable_link(struct lltable * llt)836 lltable_link(struct lltable *llt)
837 {
838
839 LLTABLE_LIST_WLOCK();
840 SLIST_INSERT_HEAD(&V_lltables, llt, llt_link);
841 LLTABLE_LIST_WUNLOCK();
842 }
843
844 static void
lltable_unlink(struct lltable * llt)845 lltable_unlink(struct lltable *llt)
846 {
847
848 LLTABLE_LIST_WLOCK();
849 SLIST_REMOVE(&V_lltables, llt, lltable, llt_link);
850 LLTABLE_LIST_WUNLOCK();
851
852 }
853
854 /*
855 * Gets interface @ifp lltable for the specified @family
856 */
857 struct lltable *
lltable_get(struct ifnet * ifp,int family)858 lltable_get(struct ifnet *ifp, int family)
859 {
860 switch (family) {
861 #ifdef INET
862 case AF_INET:
863 return (in_lltable_get(ifp));
864 #endif
865 #ifdef INET6
866 case AF_INET6:
867 return (in6_lltable_get(ifp));
868 #endif
869 }
870
871 return (NULL);
872 }
873
874 /*
875 * External methods used by lltable consumers
876 */
877
878 int
lltable_foreach_lle(struct lltable * llt,llt_foreach_cb_t * f,void * farg)879 lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg)
880 {
881
882 return (llt->llt_foreach_entry(llt, f, farg));
883 }
884
885 struct llentry *
lltable_alloc_entry(struct lltable * llt,u_int flags,const struct sockaddr * l3addr)886 lltable_alloc_entry(struct lltable *llt, u_int flags,
887 const struct sockaddr *l3addr)
888 {
889
890 return (llt->llt_alloc_entry(llt, flags, l3addr));
891 }
892
893 void
lltable_free_entry(struct lltable * llt,struct llentry * lle)894 lltable_free_entry(struct lltable *llt, struct llentry *lle)
895 {
896
897 llt->llt_free_entry(llt, lle);
898 }
899
900 int
lltable_link_entry(struct lltable * llt,struct llentry * lle)901 lltable_link_entry(struct lltable *llt, struct llentry *lle)
902 {
903 int error = llt->llt_link_entry(llt, lle);
904
905 if (error == 0 && (lle->la_flags & LLE_PUB) != 0)
906 llt->llt_flags |= LLT_ADDEDPROXY;
907
908 return (error);
909 }
910
911 void
lltable_link_child_entry(struct llentry * lle,struct llentry * child_lle)912 lltable_link_child_entry(struct llentry *lle, struct llentry *child_lle)
913 {
914 child_lle->lle_parent = lle;
915 child_lle->lle_tbl = lle->lle_tbl;
916 child_lle->la_flags |= LLE_LINKED;
917 CK_SLIST_INSERT_HEAD(&lle->lle_children, child_lle, lle_child_next);
918 }
919
920 void
lltable_unlink_child_entry(struct llentry * child_lle)921 lltable_unlink_child_entry(struct llentry *child_lle)
922 {
923 struct llentry *lle = child_lle->lle_parent;
924
925 child_lle->la_flags &= ~LLE_LINKED;
926 child_lle->lle_parent = NULL;
927 CK_SLIST_REMOVE(&lle->lle_children, child_lle, llentry, lle_child_next);
928 }
929
930 int
lltable_unlink_entry(struct lltable * llt,struct llentry * lle)931 lltable_unlink_entry(struct lltable *llt, struct llentry *lle)
932 {
933
934 return (llt->llt_unlink_entry(lle));
935 }
936
937 void
lltable_fill_sa_entry(const struct llentry * lle,struct sockaddr * sa)938 lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
939 {
940 struct lltable *llt;
941
942 llt = lle->lle_tbl;
943 llt->llt_fill_sa_entry(lle, sa);
944 }
945
946 struct ifnet *
lltable_get_ifp(const struct lltable * llt)947 lltable_get_ifp(const struct lltable *llt)
948 {
949
950 return (llt->llt_ifp);
951 }
952
953 int
lltable_get_af(const struct lltable * llt)954 lltable_get_af(const struct lltable *llt)
955 {
956
957 return (llt->llt_af);
958 }
959
960 /*
961 * Called in route_output when rtm_flags contains RTF_LLDATA.
962 */
963 int
lla_rt_output(struct rt_msghdr * rtm,struct rt_addrinfo * info)964 lla_rt_output(struct rt_msghdr *rtm, struct rt_addrinfo *info)
965 {
966 struct sockaddr_dl *dl =
967 (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
968 struct sockaddr *dst = (struct sockaddr *)info->rti_info[RTAX_DST];
969 struct ifnet *ifp;
970 struct lltable *llt;
971 struct llentry *lle, *lle_tmp;
972 uint8_t linkhdr[LLE_MAX_LINKHDR];
973 size_t linkhdrsize;
974 int lladdr_off;
975 u_int laflags = 0;
976 int error;
977
978 if (dl == NULL || dl->sdl_family != AF_LINK)
979 return (EINVAL);
980
981 /* XXX: should be ntohs() */
982 ifp = ifnet_byindex(dl->sdl_index);
983 if (ifp == NULL) {
984 log(LOG_INFO, "%s: invalid ifp (sdl_index %d)\n",
985 __func__, dl->sdl_index);
986 return EINVAL;
987 }
988
989 llt = lltable_get(ifp, dst->sa_family);
990
991 if (llt == NULL)
992 return (ESRCH);
993
994 error = 0;
995
996 switch (rtm->rtm_type) {
997 case RTM_ADD:
998 /* Add static LLE */
999 laflags = 0;
1000 if (rtm->rtm_rmx.rmx_expire == 0)
1001 laflags = LLE_STATIC;
1002 lle = lltable_alloc_entry(llt, laflags, dst);
1003 if (lle == NULL)
1004 return (ENOMEM);
1005
1006 linkhdrsize = sizeof(linkhdr);
1007 if (lltable_calc_llheader(ifp, dst->sa_family, LLADDR(dl),
1008 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1009 lltable_free_entry(llt, lle);
1010 return (EINVAL);
1011 }
1012 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1013 lladdr_off);
1014 if ((rtm->rtm_flags & RTF_ANNOUNCE))
1015 lle->la_flags |= LLE_PUB;
1016 lle->la_expire = rtm->rtm_rmx.rmx_expire;
1017
1018 laflags = lle->la_flags;
1019
1020 /* Try to link new entry */
1021 lle_tmp = NULL;
1022 LLTABLE_LOCK(llt);
1023 LLE_WLOCK(lle);
1024 lle_tmp = lla_lookup(llt, LLE_EXCLUSIVE, dst);
1025 if (lle_tmp != NULL) {
1026 /* Check if we are trying to replace immutable entry */
1027 if ((lle_tmp->la_flags & LLE_IFADDR) != 0) {
1028 LLTABLE_UNLOCK(llt);
1029 LLE_WUNLOCK(lle_tmp);
1030 lltable_free_entry(llt, lle);
1031 return (EPERM);
1032 }
1033 /* Unlink existing entry from table */
1034 lltable_unlink_entry(llt, lle_tmp);
1035 }
1036 lltable_link_entry(llt, lle);
1037 LLTABLE_UNLOCK(llt);
1038
1039 if (lle_tmp != NULL) {
1040 EVENTHANDLER_INVOKE(lle_event, lle_tmp,LLENTRY_EXPIRED);
1041 lltable_free_entry(llt, lle_tmp);
1042 }
1043
1044 /*
1045 * By invoking LLE handler here we might get
1046 * two events on static LLE entry insertion
1047 * in routing socket. However, since we might have
1048 * other subscribers we need to generate this event.
1049 */
1050 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
1051 LLE_WUNLOCK(lle);
1052 llt->llt_post_resolved(llt, lle);
1053 break;
1054
1055 case RTM_DELETE:
1056 return (lltable_delete_addr(llt, 0, dst));
1057
1058 default:
1059 error = EINVAL;
1060 }
1061
1062 return (error);
1063 }
1064
1065 #ifdef DDB
1066 static void
llatbl_lle_show(struct llentry * lle)1067 llatbl_lle_show(struct llentry *lle)
1068 {
1069 uint8_t octet[6];
1070 sa_family_t af = AF_UNSPEC;
1071 char l3_addr_fmt[] = " l3_addr=%s (af=%d)\n";
1072
1073 db_printf("lle=%p\n", lle);
1074 db_printf(" lle_next=%p\n", lle->lle_next.cle_next);
1075 db_printf(" lle_lock=%p\n", &lle->lle_lock);
1076 db_printf(" lle_tbl=%p\n", lle->lle_tbl);
1077 db_printf(" lle_head=%p\n", lle->lle_head);
1078 db_printf(" la_hold=%p\n", lle->la_hold);
1079 db_printf(" la_numheld=%d\n", lle->la_numheld);
1080 db_printf(" la_expire=%ju\n", (uintmax_t)lle->la_expire);
1081 db_printf(" la_flags=0x%04x\n", lle->la_flags);
1082 db_printf(" la_asked=%u\n", lle->la_asked);
1083 db_printf(" la_preempt=%u\n", lle->la_preempt);
1084 db_printf(" ln_state=%d\n", lle->ln_state);
1085 db_printf(" ln_router=%u\n", lle->ln_router);
1086 db_printf(" ln_ntick=%ju\n", (uintmax_t)lle->ln_ntick);
1087 db_printf(" lle_refcnt=%d\n", lle->lle_refcnt);
1088 bcopy(lle->ll_addr, octet, sizeof(octet));
1089 db_printf(" ll_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
1090 octet[0], octet[1], octet[2], octet[3], octet[4], octet[5]);
1091 db_printf(" lle_timer=%p\n", &lle->lle_timer);
1092
1093 if (lle->lle_tbl) {
1094 af = lle->lle_tbl->llt_af;
1095 }
1096
1097 switch (af) {
1098 #ifdef INET
1099 case AF_INET:
1100 {
1101 struct sockaddr_in sin;
1102 char l3s[INET_ADDRSTRLEN];
1103
1104 lltable_fill_sa_entry(lle, (struct sockaddr *)&sin);
1105 (void) inet_ntop(af, &sin.sin_addr, l3s, sizeof(l3s));
1106 db_printf(l3_addr_fmt, l3s, af);
1107 break;
1108 }
1109 #endif
1110 #ifdef INET6
1111 case AF_INET6:
1112 {
1113 struct sockaddr_in6 sin6;
1114 char l3s[INET6_ADDRSTRLEN];
1115
1116 lltable_fill_sa_entry(lle, (struct sockaddr *)&sin6);
1117 (void) inet_ntop(af, &sin6.sin6_addr, l3s, sizeof(l3s));
1118 db_printf(l3_addr_fmt, l3s, af);
1119 break;
1120 }
1121 #endif
1122 default:
1123 db_printf(l3_addr_fmt, "N/A", af);
1124 break;
1125 }
1126 }
1127
DB_SHOW_COMMAND(llentry,db_show_llentry)1128 DB_SHOW_COMMAND(llentry, db_show_llentry)
1129 {
1130
1131 if (!have_addr) {
1132 db_printf("usage: show llentry <struct llentry *>\n");
1133 return;
1134 }
1135
1136 llatbl_lle_show((struct llentry *)addr);
1137 }
1138
1139 static void
llatbl_llt_show(struct lltable * llt)1140 llatbl_llt_show(struct lltable *llt)
1141 {
1142 int i;
1143 struct llentry *lle;
1144
1145 db_printf("llt=%p llt_af=%d llt_ifp=%p\n",
1146 llt, llt->llt_af, llt->llt_ifp);
1147
1148 for (i = 0; i < llt->llt_hsize; i++) {
1149 CK_LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1150 llatbl_lle_show(lle);
1151 if (db_pager_quit)
1152 return;
1153 }
1154 }
1155 }
1156
DB_SHOW_COMMAND(lltable,db_show_lltable)1157 DB_SHOW_COMMAND(lltable, db_show_lltable)
1158 {
1159
1160 if (!have_addr) {
1161 db_printf("usage: show lltable <struct lltable *>\n");
1162 return;
1163 }
1164
1165 llatbl_llt_show((struct lltable *)addr);
1166 }
1167
DB_SHOW_ALL_COMMAND(lltables,db_show_all_lltables)1168 DB_SHOW_ALL_COMMAND(lltables, db_show_all_lltables)
1169 {
1170 VNET_ITERATOR_DECL(vnet_iter);
1171 struct lltable *llt;
1172
1173 VNET_FOREACH(vnet_iter) {
1174 CURVNET_SET_QUIET(vnet_iter);
1175 #ifdef VIMAGE
1176 db_printf("vnet=%p\n", curvnet);
1177 #endif
1178 SLIST_FOREACH(llt, &V_lltables, llt_link) {
1179 db_printf("llt=%p llt_af=%d llt_ifp=%p(%s)\n",
1180 llt, llt->llt_af, llt->llt_ifp,
1181 (llt->llt_ifp != NULL) ?
1182 llt->llt_ifp->if_xname : "?");
1183 if (have_addr && addr != 0) /* verbose */
1184 llatbl_llt_show(llt);
1185 if (db_pager_quit) {
1186 CURVNET_RESTORE();
1187 return;
1188 }
1189 }
1190 CURVNET_RESTORE();
1191 }
1192 }
1193 #endif
1194