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 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 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 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 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 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 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp); 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 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 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp); 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 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 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 IF_AFDATA_WLOCK(llt->llt_ifp); 287 /* Push matching lles to chain */ 288 lltable_foreach_lle(llt, htable_prefix_free_cb, &pmd); 289 290 llentries_unlink(llt, &pmd.dchain); 291 IF_AFDATA_WUNLOCK(llt->llt_ifp); 292 293 CK_LIST_FOREACH_SAFE(lle, &pmd.dchain, lle_chain, next) 294 lltable_free_entry(llt, lle); 295 } 296 297 static void 298 htable_free_tbl(struct lltable *llt) 299 { 300 301 free(llt->lle_head, M_LLTABLE); 302 free(llt, M_LLTABLE); 303 } 304 305 static void 306 llentries_unlink(struct lltable *llt, struct llentries *head) 307 { 308 struct llentry *lle, *next; 309 310 CK_LIST_FOREACH_SAFE(lle, head, lle_chain, next) 311 llt->llt_unlink_entry(lle); 312 } 313 314 /* 315 * Helper function used to drop all mbufs in hold queue. 316 * 317 * Returns the number of held packets, if any, that were dropped. 318 */ 319 size_t 320 lltable_drop_entry_queue(struct llentry *lle) 321 { 322 size_t pkts_dropped = 0; 323 324 LLE_WLOCK_ASSERT(lle); 325 326 while (lle->la_hold != NULL) { 327 struct mbuf *next = lle->la_hold->m_nextpkt; 328 m_freem(lle->la_hold); 329 lle->la_hold = next; 330 lle->la_numheld--; 331 pkts_dropped++; 332 } 333 334 KASSERT(lle->la_numheld == 0, 335 ("%s: la_numheld %d > 0, pkts_dropped %zd", __func__, 336 lle->la_numheld, pkts_dropped)); 337 338 return (pkts_dropped); 339 } 340 341 void 342 lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle, 343 const char *linkhdr, size_t linkhdrsize, int lladdr_off) 344 { 345 346 memcpy(lle->r_linkdata, linkhdr, linkhdrsize); 347 lle->r_hdrlen = linkhdrsize; 348 lle->ll_addr = &lle->r_linkdata[lladdr_off]; 349 lle->la_flags |= LLE_VALID; 350 lle->r_flags |= RLLE_VALID; 351 } 352 353 /* 354 * Acquires lltable write lock. 355 * 356 * Returns true on success, with both lltable and lle lock held. 357 * On failure, false is returned and lle wlock is still held. 358 */ 359 bool 360 lltable_acquire_wlock(struct ifnet *ifp, struct llentry *lle) 361 { 362 NET_EPOCH_ASSERT(); 363 364 /* Perform real LLE update */ 365 /* use afdata WLOCK to update fields */ 366 LLE_WUNLOCK(lle); 367 IF_AFDATA_WLOCK(ifp); 368 LLE_WLOCK(lle); 369 370 /* 371 * Since we droppped LLE lock, other thread might have deleted 372 * this lle. Check and return 373 */ 374 if ((lle->la_flags & LLE_DELETED) != 0) { 375 IF_AFDATA_WUNLOCK(ifp); 376 return (false); 377 } 378 379 return (true); 380 } 381 382 /* 383 * Tries to update @lle link-level address. 384 * Since update requires AFDATA WLOCK, function 385 * drops @lle lock, acquires AFDATA lock and then acquires 386 * @lle lock to maintain lock order. 387 * 388 * Returns 1 on success. 389 */ 390 int 391 lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle, 392 const char *linkhdr, size_t linkhdrsize, int lladdr_off) 393 { 394 395 if (!lltable_acquire_wlock(ifp, lle)) 396 return (0); 397 398 /* Update data */ 399 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize, lladdr_off); 400 401 IF_AFDATA_WUNLOCK(ifp); 402 403 return (1); 404 } 405 406 /* 407 * Helper function used to pre-compute full/partial link-layer 408 * header data suitable for feeding into if_output(). 409 */ 410 int 411 lltable_calc_llheader(struct ifnet *ifp, int family, char *lladdr, 412 char *buf, size_t *bufsize, int *lladdr_off) 413 { 414 struct if_encap_req ereq; 415 int error; 416 417 bzero(buf, *bufsize); 418 bzero(&ereq, sizeof(ereq)); 419 ereq.buf = buf; 420 ereq.bufsize = *bufsize; 421 ereq.rtype = IFENCAP_LL; 422 ereq.family = family; 423 ereq.lladdr = lladdr; 424 ereq.lladdr_len = ifp->if_addrlen; 425 error = ifp->if_requestencap(ifp, &ereq); 426 if (error == 0) { 427 *bufsize = ereq.bufsize; 428 *lladdr_off = ereq.lladdr_off; 429 } 430 431 return (error); 432 } 433 434 /* 435 * Searches for the child entry matching @family inside @lle. 436 * Returns the entry or NULL. 437 */ 438 struct llentry * 439 llentry_lookup_family(struct llentry *lle, int family) 440 { 441 struct llentry *child_lle; 442 443 if (lle == NULL) 444 return (NULL); 445 446 CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) { 447 if (child_lle->r_family == family) 448 return (child_lle); 449 } 450 451 return (NULL); 452 } 453 454 /* 455 * Retrieves upper protocol family for the llentry. 456 * By default, all "normal" (e.g. upper_family == transport_family) 457 * llentries have r_family set to 0. 458 * Thus, use @default_family in that regard, otherwise use r_family. 459 * 460 * Returns upper protocol family 461 */ 462 int 463 llentry_get_upper_family(const struct llentry *lle, int default_family) 464 { 465 return (lle->r_family == 0 ? default_family : lle->r_family); 466 } 467 468 /* 469 * Prints llentry @lle data into provided buffer. 470 * Example: lle/inet/valid/em0/1.2.3.4 471 * 472 * Returns @buf. 473 */ 474 char * 475 llentry_print_buf(const struct llentry *lle, struct ifnet *ifp, int family, 476 char *buf, size_t bufsize) 477 { 478 #if defined(INET) || defined(INET6) 479 char abuf[INET6_ADDRSTRLEN]; 480 #endif 481 482 const char *valid = (lle->r_flags & RLLE_VALID) ? "valid" : "no_l2"; 483 const char *upper_str = rib_print_family(llentry_get_upper_family(lle, family)); 484 485 switch (family) { 486 #ifdef INET 487 case AF_INET: 488 inet_ntop(AF_INET, &lle->r_l3addr.addr4, abuf, sizeof(abuf)); 489 snprintf(buf, bufsize, "lle/%s/%s/%s/%s", upper_str, 490 valid, if_name(ifp), abuf); 491 break; 492 #endif 493 #ifdef INET6 494 case AF_INET6: 495 inet_ntop(AF_INET6, &lle->r_l3addr.addr6, abuf, sizeof(abuf)); 496 snprintf(buf, bufsize, "lle/%s/%s/%s/%s", upper_str, 497 valid, if_name(ifp), abuf); 498 break; 499 #endif 500 default: 501 snprintf(buf, bufsize, "lle/%s/%s/%s/????", upper_str, 502 valid, if_name(ifp)); 503 break; 504 } 505 506 return (buf); 507 } 508 509 char * 510 llentry_print_buf_lltable(const struct llentry *lle, char *buf, size_t bufsize) 511 { 512 struct lltable *tbl = lle->lle_tbl; 513 514 return (llentry_print_buf(lle, lltable_get_ifp(tbl), lltable_get_af(tbl), buf, bufsize)); 515 } 516 517 /* 518 * Requests feedback from the datapath. 519 * First packet using @lle should result in 520 * setting r_skip_req back to 0 and updating 521 * lle_hittime to the current time_uptime. 522 */ 523 void 524 llentry_request_feedback(struct llentry *lle) 525 { 526 struct llentry *child_lle; 527 528 LLE_REQ_LOCK(lle); 529 lle->r_skip_req = 1; 530 LLE_REQ_UNLOCK(lle); 531 532 CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) { 533 LLE_REQ_LOCK(child_lle); 534 child_lle->r_skip_req = 1; 535 LLE_REQ_UNLOCK(child_lle); 536 } 537 } 538 539 /* 540 * Updates the lle state to mark it has been used 541 * and record the time. 542 * Used by the llentry_provide_feedback() wrapper. 543 */ 544 void 545 llentry_mark_used(struct llentry *lle) 546 { 547 LLE_REQ_LOCK(lle); 548 lle->r_skip_req = 0; 549 lle->lle_hittime = time_uptime; 550 LLE_REQ_UNLOCK(lle); 551 } 552 553 /* 554 * Fetches the time when lle was used. 555 * Return 0 if the entry was not used, relevant time_uptime 556 * otherwise. 557 */ 558 static time_t 559 llentry_get_hittime_raw(struct llentry *lle) 560 { 561 time_t lle_hittime = 0; 562 563 LLE_REQ_LOCK(lle); 564 if ((lle->r_skip_req == 0) && (lle_hittime < lle->lle_hittime)) 565 lle_hittime = lle->lle_hittime; 566 LLE_REQ_UNLOCK(lle); 567 568 return (lle_hittime); 569 } 570 571 time_t 572 llentry_get_hittime(struct llentry *lle) 573 { 574 time_t lle_hittime = 0; 575 struct llentry *child_lle; 576 577 lle_hittime = llentry_get_hittime_raw(lle); 578 579 CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) { 580 time_t hittime = llentry_get_hittime_raw(child_lle); 581 if (hittime > lle_hittime) 582 lle_hittime = hittime; 583 } 584 585 return (lle_hittime); 586 } 587 588 /* 589 * Update link-layer header for given @lle after 590 * interface lladdr was changed. 591 */ 592 static int 593 llentry_update_ifaddr(struct lltable *llt, struct llentry *lle, void *farg) 594 { 595 struct ifnet *ifp; 596 u_char linkhdr[LLE_MAX_LINKHDR]; 597 size_t linkhdrsize; 598 u_char *lladdr; 599 int lladdr_off; 600 601 ifp = (struct ifnet *)farg; 602 603 lladdr = lle->ll_addr; 604 605 LLE_WLOCK(lle); 606 if ((lle->la_flags & LLE_VALID) == 0) { 607 LLE_WUNLOCK(lle); 608 return (0); 609 } 610 611 if ((lle->la_flags & LLE_IFADDR) != 0) 612 lladdr = IF_LLADDR(ifp); 613 614 linkhdrsize = sizeof(linkhdr); 615 lltable_calc_llheader(ifp, llt->llt_af, lladdr, linkhdr, &linkhdrsize, 616 &lladdr_off); 617 memcpy(lle->r_linkdata, linkhdr, linkhdrsize); 618 LLE_WUNLOCK(lle); 619 620 return (0); 621 } 622 623 /* 624 * Update all calculated headers for given @llt 625 */ 626 void 627 lltable_update_ifaddr(struct lltable *llt) 628 { 629 630 if (llt->llt_ifp->if_flags & IFF_LOOPBACK) 631 return; 632 633 IF_AFDATA_WLOCK(llt->llt_ifp); 634 lltable_foreach_lle(llt, llentry_update_ifaddr, llt->llt_ifp); 635 IF_AFDATA_WUNLOCK(llt->llt_ifp); 636 } 637 638 /* 639 * 640 * Performs generic cleanup routines and frees lle. 641 * 642 * Called for non-linked entries, with callouts and 643 * other AF-specific cleanups performed. 644 * 645 * @lle must be passed WLOCK'ed 646 * 647 * Returns the number of held packets, if any, that were dropped. 648 */ 649 size_t 650 llentry_free(struct llentry *lle) 651 { 652 size_t pkts_dropped; 653 654 LLE_WLOCK_ASSERT(lle); 655 656 KASSERT((lle->la_flags & LLE_LINKED) == 0, ("freeing linked lle")); 657 658 pkts_dropped = lltable_drop_entry_queue(lle); 659 660 /* cancel timer */ 661 if (callout_stop(&lle->lle_timer) > 0) 662 LLE_REMREF(lle); 663 LLE_FREE_LOCKED(lle); 664 665 return (pkts_dropped); 666 } 667 668 /* 669 * Free all entries from given table and free itself. 670 */ 671 672 static int 673 lltable_free_cb(struct lltable *llt, struct llentry *lle, void *farg) 674 { 675 struct llentries *dchain; 676 677 dchain = (struct llentries *)farg; 678 679 LLE_WLOCK(lle); 680 CK_LIST_INSERT_HEAD(dchain, lle, lle_chain); 681 682 return (0); 683 } 684 685 /* 686 * Free all entries from given table and free itself. 687 */ 688 void 689 lltable_free(struct lltable *llt) 690 { 691 struct llentry *lle, *next; 692 struct llentries dchain; 693 694 KASSERT(llt != NULL, ("%s: llt is NULL", __func__)); 695 696 lltable_unlink(llt); 697 698 CK_LIST_INIT(&dchain); 699 IF_AFDATA_WLOCK(llt->llt_ifp); 700 /* Push all lles to @dchain */ 701 lltable_foreach_lle(llt, lltable_free_cb, &dchain); 702 llentries_unlink(llt, &dchain); 703 IF_AFDATA_WUNLOCK(llt->llt_ifp); 704 705 CK_LIST_FOREACH_SAFE(lle, &dchain, lle_chain, next) { 706 llentry_free(lle); 707 } 708 709 KASSERT(llt->llt_entries == 0, ("%s: lltable %p (%s) entries not 0: %d", 710 __func__, llt, llt->llt_ifp->if_xname, llt->llt_entries)); 711 712 llt->llt_free_tbl(llt); 713 } 714 715 /* 716 * Deletes an address from given lltable. 717 * Used for userland interaction to remove 718 * individual entries. Skips entries added by OS. 719 */ 720 int 721 lltable_delete_addr(struct lltable *llt, u_int flags, 722 const struct sockaddr *l3addr) 723 { 724 struct llentry *lle; 725 struct ifnet *ifp; 726 727 ifp = llt->llt_ifp; 728 IF_AFDATA_WLOCK(ifp); 729 lle = lla_lookup(llt, LLE_SF(l3addr->sa_family, LLE_EXCLUSIVE), l3addr); 730 731 if (lle == NULL) { 732 IF_AFDATA_WUNLOCK(ifp); 733 return (ENOENT); 734 } 735 if ((lle->la_flags & LLE_IFADDR) != 0 && (flags & LLE_IFADDR) == 0) { 736 IF_AFDATA_WUNLOCK(ifp); 737 LLE_WUNLOCK(lle); 738 return (EPERM); 739 } 740 741 lltable_unlink_entry(llt, lle); 742 IF_AFDATA_WUNLOCK(ifp); 743 744 llt->llt_delete_entry(llt, lle); 745 746 return (0); 747 } 748 749 void 750 lltable_prefix_free(int af, struct sockaddr *addr, struct sockaddr *mask, 751 u_int flags) 752 { 753 struct lltable *llt; 754 755 LLTABLE_LIST_RLOCK(); 756 SLIST_FOREACH(llt, &V_lltables, llt_link) { 757 if (llt->llt_af != af) 758 continue; 759 760 llt->llt_prefix_free(llt, addr, mask, flags); 761 } 762 LLTABLE_LIST_RUNLOCK(); 763 } 764 765 /* 766 * Delete llentries that func() returns true. 767 */ 768 struct lle_match_data { 769 struct llentries dchain; 770 llt_match_cb_t *func; 771 void *farg; 772 }; 773 774 static int 775 lltable_delete_conditional_cb(struct lltable *llt, struct llentry *lle, 776 void *farg) 777 { 778 struct lle_match_data *lmd; 779 780 lmd = (struct lle_match_data *)farg; 781 if (lmd->func(llt, lle, lmd->farg)) { 782 LLE_WLOCK(lle); 783 CK_LIST_INSERT_HEAD(&lmd->dchain, lle, lle_chain); 784 } 785 786 return (0); 787 } 788 789 void 790 lltable_delete_conditional(struct lltable *llt, llt_match_cb_t *func, 791 void *farg) 792 { 793 struct llentry *lle, *next; 794 struct lle_match_data lmd; 795 796 bzero(&lmd, sizeof(lmd)); 797 CK_LIST_INIT(&lmd.dchain); 798 lmd.func = func; 799 lmd.farg = farg; 800 801 IF_AFDATA_WLOCK(llt->llt_ifp); 802 lltable_foreach_lle(llt, lltable_delete_conditional_cb, &lmd); 803 llentries_unlink(llt, &lmd.dchain); 804 IF_AFDATA_WUNLOCK(llt->llt_ifp); 805 806 CK_LIST_FOREACH_SAFE(lle, &lmd.dchain, lle_chain, next) 807 llt->llt_delete_entry(llt, lle); 808 } 809 810 struct lltable * 811 lltable_allocate_htbl(uint32_t hsize) 812 { 813 struct lltable *llt; 814 int i; 815 816 llt = malloc(sizeof(struct lltable), M_LLTABLE, M_WAITOK | M_ZERO); 817 llt->llt_hsize = hsize; 818 llt->lle_head = malloc(sizeof(struct llentries) * hsize, 819 M_LLTABLE, M_WAITOK | M_ZERO); 820 821 for (i = 0; i < llt->llt_hsize; i++) 822 CK_LIST_INIT(&llt->lle_head[i]); 823 824 /* Set some default callbacks */ 825 llt->llt_link_entry = htable_link_entry; 826 llt->llt_unlink_entry = htable_unlink_entry; 827 llt->llt_prefix_free = htable_prefix_free; 828 llt->llt_foreach_entry = htable_foreach_lle; 829 llt->llt_free_tbl = htable_free_tbl; 830 831 return (llt); 832 } 833 834 /* 835 * Links lltable to global llt list. 836 */ 837 void 838 lltable_link(struct lltable *llt) 839 { 840 841 LLTABLE_LIST_WLOCK(); 842 SLIST_INSERT_HEAD(&V_lltables, llt, llt_link); 843 LLTABLE_LIST_WUNLOCK(); 844 } 845 846 static void 847 lltable_unlink(struct lltable *llt) 848 { 849 850 LLTABLE_LIST_WLOCK(); 851 SLIST_REMOVE(&V_lltables, llt, lltable, llt_link); 852 LLTABLE_LIST_WUNLOCK(); 853 854 } 855 856 /* 857 * Gets interface @ifp lltable for the specified @family 858 */ 859 struct lltable * 860 lltable_get(struct ifnet *ifp, int family) 861 { 862 switch (family) { 863 #ifdef INET 864 case AF_INET: 865 return (in_lltable_get(ifp)); 866 #endif 867 #ifdef INET6 868 case AF_INET6: 869 return (in6_lltable_get(ifp)); 870 #endif 871 } 872 873 return (NULL); 874 } 875 876 /* 877 * External methods used by lltable consumers 878 */ 879 880 int 881 lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg) 882 { 883 884 return (llt->llt_foreach_entry(llt, f, farg)); 885 } 886 887 struct llentry * 888 lltable_alloc_entry(struct lltable *llt, u_int flags, 889 const struct sockaddr *l3addr) 890 { 891 892 return (llt->llt_alloc_entry(llt, flags, l3addr)); 893 } 894 895 void 896 lltable_free_entry(struct lltable *llt, struct llentry *lle) 897 { 898 899 llt->llt_free_entry(llt, lle); 900 } 901 902 int 903 lltable_link_entry(struct lltable *llt, struct llentry *lle) 904 { 905 int error = llt->llt_link_entry(llt, lle); 906 907 if (error == 0 && (lle->la_flags & LLE_PUB) != 0) 908 llt->llt_flags |= LLT_ADDEDPROXY; 909 910 return (error); 911 } 912 913 void 914 lltable_link_child_entry(struct llentry *lle, struct llentry *child_lle) 915 { 916 child_lle->lle_parent = lle; 917 child_lle->lle_tbl = lle->lle_tbl; 918 child_lle->la_flags |= LLE_LINKED; 919 CK_SLIST_INSERT_HEAD(&lle->lle_children, child_lle, lle_child_next); 920 } 921 922 void 923 lltable_unlink_child_entry(struct llentry *child_lle) 924 { 925 struct llentry *lle = child_lle->lle_parent; 926 927 child_lle->la_flags &= ~LLE_LINKED; 928 child_lle->lle_parent = NULL; 929 CK_SLIST_REMOVE(&lle->lle_children, child_lle, llentry, lle_child_next); 930 } 931 932 int 933 lltable_unlink_entry(struct lltable *llt, struct llentry *lle) 934 { 935 936 return (llt->llt_unlink_entry(lle)); 937 } 938 939 void 940 lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa) 941 { 942 struct lltable *llt; 943 944 llt = lle->lle_tbl; 945 llt->llt_fill_sa_entry(lle, sa); 946 } 947 948 struct ifnet * 949 lltable_get_ifp(const struct lltable *llt) 950 { 951 952 return (llt->llt_ifp); 953 } 954 955 int 956 lltable_get_af(const struct lltable *llt) 957 { 958 959 return (llt->llt_af); 960 } 961 962 /* 963 * Called in route_output when rtm_flags contains RTF_LLDATA. 964 */ 965 int 966 lla_rt_output(struct rt_msghdr *rtm, struct rt_addrinfo *info) 967 { 968 struct sockaddr_dl *dl = 969 (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY]; 970 struct sockaddr *dst = (struct sockaddr *)info->rti_info[RTAX_DST]; 971 struct ifnet *ifp; 972 struct lltable *llt; 973 struct llentry *lle, *lle_tmp; 974 uint8_t linkhdr[LLE_MAX_LINKHDR]; 975 size_t linkhdrsize; 976 int lladdr_off; 977 u_int laflags = 0; 978 int error; 979 980 if (dl == NULL || dl->sdl_family != AF_LINK) 981 return (EINVAL); 982 983 /* XXX: should be ntohs() */ 984 ifp = ifnet_byindex(dl->sdl_index); 985 if (ifp == NULL) { 986 log(LOG_INFO, "%s: invalid ifp (sdl_index %d)\n", 987 __func__, dl->sdl_index); 988 return EINVAL; 989 } 990 991 llt = lltable_get(ifp, dst->sa_family); 992 993 if (llt == NULL) 994 return (ESRCH); 995 996 error = 0; 997 998 switch (rtm->rtm_type) { 999 case RTM_ADD: 1000 /* Add static LLE */ 1001 laflags = 0; 1002 if (rtm->rtm_rmx.rmx_expire == 0) 1003 laflags = LLE_STATIC; 1004 lle = lltable_alloc_entry(llt, laflags, dst); 1005 if (lle == NULL) 1006 return (ENOMEM); 1007 1008 linkhdrsize = sizeof(linkhdr); 1009 if (lltable_calc_llheader(ifp, dst->sa_family, LLADDR(dl), 1010 linkhdr, &linkhdrsize, &lladdr_off) != 0) { 1011 lltable_free_entry(llt, lle); 1012 return (EINVAL); 1013 } 1014 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize, 1015 lladdr_off); 1016 if ((rtm->rtm_flags & RTF_ANNOUNCE)) 1017 lle->la_flags |= LLE_PUB; 1018 lle->la_expire = rtm->rtm_rmx.rmx_expire; 1019 1020 laflags = lle->la_flags; 1021 1022 /* Try to link new entry */ 1023 lle_tmp = NULL; 1024 IF_AFDATA_WLOCK(ifp); 1025 LLE_WLOCK(lle); 1026 lle_tmp = lla_lookup(llt, LLE_EXCLUSIVE, dst); 1027 if (lle_tmp != NULL) { 1028 /* Check if we are trying to replace immutable entry */ 1029 if ((lle_tmp->la_flags & LLE_IFADDR) != 0) { 1030 IF_AFDATA_WUNLOCK(ifp); 1031 LLE_WUNLOCK(lle_tmp); 1032 lltable_free_entry(llt, lle); 1033 return (EPERM); 1034 } 1035 /* Unlink existing entry from table */ 1036 lltable_unlink_entry(llt, lle_tmp); 1037 } 1038 lltable_link_entry(llt, lle); 1039 IF_AFDATA_WUNLOCK(ifp); 1040 1041 if (lle_tmp != NULL) { 1042 EVENTHANDLER_INVOKE(lle_event, lle_tmp,LLENTRY_EXPIRED); 1043 lltable_free_entry(llt, lle_tmp); 1044 } 1045 1046 /* 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. 1051 */ 1052 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED); 1053 LLE_WUNLOCK(lle); 1054 llt->llt_post_resolved(llt, lle); 1055 break; 1056 1057 case RTM_DELETE: 1058 return (lltable_delete_addr(llt, 0, dst)); 1059 1060 default: 1061 error = EINVAL; 1062 } 1063 1064 return (error); 1065 } 1066 1067 #ifdef DDB 1068 static void 1069 llatbl_lle_show(struct llentry *lle) 1070 { 1071 uint8_t octet[6]; 1072 sa_family_t af = AF_UNSPEC; 1073 char l3_addr_fmt[] = " l3_addr=%s (af=%d)\n"; 1074 1075 db_printf("lle=%p\n", lle); 1076 db_printf(" lle_next=%p\n", lle->lle_next.cle_next); 1077 db_printf(" lle_lock=%p\n", &lle->lle_lock); 1078 db_printf(" lle_tbl=%p\n", lle->lle_tbl); 1079 db_printf(" lle_head=%p\n", lle->lle_head); 1080 db_printf(" la_hold=%p\n", lle->la_hold); 1081 db_printf(" la_numheld=%d\n", lle->la_numheld); 1082 db_printf(" la_expire=%ju\n", (uintmax_t)lle->la_expire); 1083 db_printf(" la_flags=0x%04x\n", lle->la_flags); 1084 db_printf(" la_asked=%u\n", lle->la_asked); 1085 db_printf(" la_preempt=%u\n", lle->la_preempt); 1086 db_printf(" ln_state=%d\n", lle->ln_state); 1087 db_printf(" ln_router=%u\n", lle->ln_router); 1088 db_printf(" ln_ntick=%ju\n", (uintmax_t)lle->ln_ntick); 1089 db_printf(" lle_refcnt=%d\n", lle->lle_refcnt); 1090 bcopy(lle->ll_addr, octet, sizeof(octet)); 1091 db_printf(" ll_addr=%02x:%02x:%02x:%02x:%02x:%02x\n", 1092 octet[0], octet[1], octet[2], octet[3], octet[4], octet[5]); 1093 db_printf(" lle_timer=%p\n", &lle->lle_timer); 1094 1095 if (lle->lle_tbl) { 1096 af = lle->lle_tbl->llt_af; 1097 } 1098 1099 switch (af) { 1100 #ifdef INET 1101 case AF_INET: 1102 { 1103 struct sockaddr_in sin; 1104 char l3s[INET_ADDRSTRLEN]; 1105 1106 lltable_fill_sa_entry(lle, (struct sockaddr *)&sin); 1107 (void) inet_ntop(af, &sin.sin_addr, l3s, sizeof(l3s)); 1108 db_printf(l3_addr_fmt, l3s, af); 1109 break; 1110 } 1111 #endif 1112 #ifdef INET6 1113 case AF_INET6: 1114 { 1115 struct sockaddr_in6 sin6; 1116 char l3s[INET6_ADDRSTRLEN]; 1117 1118 lltable_fill_sa_entry(lle, (struct sockaddr *)&sin6); 1119 (void) inet_ntop(af, &sin6.sin6_addr, l3s, sizeof(l3s)); 1120 db_printf(l3_addr_fmt, l3s, af); 1121 break; 1122 } 1123 #endif 1124 default: 1125 db_printf(l3_addr_fmt, "N/A", af); 1126 break; 1127 } 1128 } 1129 1130 DB_SHOW_COMMAND(llentry, db_show_llentry) 1131 { 1132 1133 if (!have_addr) { 1134 db_printf("usage: show llentry <struct llentry *>\n"); 1135 return; 1136 } 1137 1138 llatbl_lle_show((struct llentry *)addr); 1139 } 1140 1141 static void 1142 llatbl_llt_show(struct lltable *llt) 1143 { 1144 int i; 1145 struct llentry *lle; 1146 1147 db_printf("llt=%p llt_af=%d llt_ifp=%p\n", 1148 llt, llt->llt_af, llt->llt_ifp); 1149 1150 for (i = 0; i < llt->llt_hsize; i++) { 1151 CK_LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 1152 llatbl_lle_show(lle); 1153 if (db_pager_quit) 1154 return; 1155 } 1156 } 1157 } 1158 1159 DB_SHOW_COMMAND(lltable, db_show_lltable) 1160 { 1161 1162 if (!have_addr) { 1163 db_printf("usage: show lltable <struct lltable *>\n"); 1164 return; 1165 } 1166 1167 llatbl_llt_show((struct lltable *)addr); 1168 } 1169 1170 DB_SHOW_ALL_COMMAND(lltables, db_show_all_lltables) 1171 { 1172 VNET_ITERATOR_DECL(vnet_iter); 1173 struct lltable *llt; 1174 1175 VNET_FOREACH(vnet_iter) { 1176 CURVNET_SET_QUIET(vnet_iter); 1177 #ifdef VIMAGE 1178 db_printf("vnet=%p\n", curvnet); 1179 #endif 1180 SLIST_FOREACH(llt, &V_lltables, llt_link) { 1181 db_printf("llt=%p llt_af=%d llt_ifp=%p(%s)\n", 1182 llt, llt->llt_af, llt->llt_ifp, 1183 (llt->llt_ifp != NULL) ? 1184 llt->llt_ifp->if_xname : "?"); 1185 if (have_addr && addr != 0) /* verbose */ 1186 llatbl_llt_show(llt); 1187 if (db_pager_quit) { 1188 CURVNET_RESTORE(); 1189 return; 1190 } 1191 } 1192 CURVNET_RESTORE(); 1193 } 1194 } 1195 #endif 1196