1 /* $OpenBSD: if_ethersubr.c,v 1.278 2022/02/22 01:15:02 guenther Exp $ */ 2 /* $NetBSD: if_ethersubr.c,v 1.19 1996/05/07 02:40:30 thorpej Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * 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 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1989, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 62 */ 63 64 /* 65 %%% portions-copyright-nrl-95 66 Portions of this software are Copyright 1995-1998 by Randall Atkinson, 67 Ronald Lee, Daniel McDonald, Bao Phan, and Chris Winters. All Rights 68 Reserved. All rights under this copyright have been assigned to the US 69 Naval Research Laboratory (NRL). The NRL Copyright Notice and License 70 Agreement Version 1.1 (January 17, 1995) applies to these portions of the 71 software. 72 You should have received a copy of the license with this software. If you 73 didn't get a copy, you may request one from <license@ipv6.nrl.navy.mil>. 74 */ 75 76 #include "bpfilter.h" 77 78 #include <sys/param.h> 79 #include <sys/systm.h> 80 #include <sys/kernel.h> 81 #include <sys/malloc.h> 82 #include <sys/mbuf.h> 83 #include <sys/socket.h> 84 #include <sys/ioctl.h> 85 #include <sys/errno.h> 86 #include <sys/syslog.h> 87 #include <sys/timeout.h> 88 #include <sys/smr.h> 89 90 #include <net/if.h> 91 #include <net/netisr.h> 92 #include <net/route.h> 93 #include <net/if_llc.h> 94 #include <net/if_dl.h> 95 #include <net/if_media.h> 96 #include <net/if_types.h> 97 98 #include <netinet/in.h> 99 #include <netinet/if_ether.h> 100 #include <netinet/ip_ipsp.h> 101 102 #if NBPFILTER > 0 103 #include <net/bpf.h> 104 #endif 105 106 #include "vlan.h" 107 #if NVLAN > 0 108 #include <net/if_vlan_var.h> 109 #endif 110 111 #include "carp.h" 112 #if NCARP > 0 113 #include <netinet/ip_carp.h> 114 #endif 115 116 #include "pppoe.h" 117 #if NPPPOE > 0 118 #include <net/if_pppoe.h> 119 #endif 120 121 #include "bpe.h" 122 #if NBPE > 0 123 #include <net/if_bpe.h> 124 #endif 125 126 #ifdef INET6 127 #include <netinet6/in6_var.h> 128 #include <netinet6/nd6.h> 129 #endif 130 131 #ifdef PIPEX 132 #include <net/pipex.h> 133 #endif 134 135 #ifdef MPLS 136 #include <netmpls/mpls.h> 137 #endif /* MPLS */ 138 139 u_int8_t etherbroadcastaddr[ETHER_ADDR_LEN] = 140 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 141 u_int8_t etheranyaddr[ETHER_ADDR_LEN] = 142 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 143 #define senderr(e) { error = (e); goto bad;} 144 145 int 146 ether_ioctl(struct ifnet *ifp, struct arpcom *arp, u_long cmd, caddr_t data) 147 { 148 struct ifreq *ifr = (struct ifreq *)data; 149 int error = 0; 150 151 switch (cmd) { 152 case SIOCSIFADDR: 153 break; 154 155 case SIOCSIFMTU: 156 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ifp->if_hardmtu) 157 error = EINVAL; 158 else 159 ifp->if_mtu = ifr->ifr_mtu; 160 break; 161 162 case SIOCADDMULTI: 163 case SIOCDELMULTI: 164 if (ifp->if_flags & IFF_MULTICAST) { 165 error = (cmd == SIOCADDMULTI) ? 166 ether_addmulti(ifr, arp) : 167 ether_delmulti(ifr, arp); 168 } else 169 error = ENOTTY; 170 break; 171 172 default: 173 error = ENOTTY; 174 } 175 176 return (error); 177 } 178 179 180 void 181 ether_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt) 182 { 183 if (rt == NULL) 184 return; 185 186 switch (rt_key(rt)->sa_family) { 187 case AF_INET: 188 arp_rtrequest(ifp, req, rt); 189 break; 190 #ifdef INET6 191 case AF_INET6: 192 nd6_rtrequest(ifp, req, rt); 193 break; 194 #endif 195 default: 196 break; 197 } 198 } 199 200 int 201 ether_resolve(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 202 struct rtentry *rt, struct ether_header *eh) 203 { 204 struct arpcom *ac = (struct arpcom *)ifp; 205 sa_family_t af = dst->sa_family; 206 int error = 0; 207 208 if (!ISSET(ifp->if_flags, IFF_RUNNING)) 209 senderr(ENETDOWN); 210 211 KASSERT(rt != NULL || ISSET(m->m_flags, M_MCAST|M_BCAST) || 212 af == AF_UNSPEC || af == pseudo_AF_HDRCMPLT); 213 214 #ifdef DIAGNOSTIC 215 if (ifp->if_rdomain != rtable_l2(m->m_pkthdr.ph_rtableid)) { 216 printf("%s: trying to send packet on wrong domain. " 217 "if %d vs. mbuf %d\n", ifp->if_xname, 218 ifp->if_rdomain, rtable_l2(m->m_pkthdr.ph_rtableid)); 219 } 220 #endif 221 222 switch (af) { 223 case AF_INET: 224 error = arpresolve(ifp, rt, m, dst, eh->ether_dhost); 225 if (error) 226 return (error); 227 eh->ether_type = htons(ETHERTYPE_IP); 228 229 /* 230 * If broadcasting on a simplex interface, loopback a copy. 231 * The checksum must be calculated in software. Keep the 232 * condition in sync with in_ifcap_cksum(). 233 */ 234 if (ISSET(m->m_flags, M_BCAST) && 235 ISSET(ifp->if_flags, IFF_SIMPLEX) && 236 !m->m_pkthdr.pf.routed) { 237 struct mbuf *mcopy; 238 239 /* XXX Should we input an unencrypted IPsec packet? */ 240 mcopy = m_copym(m, 0, M_COPYALL, M_NOWAIT); 241 if (mcopy != NULL) 242 if_input_local(ifp, mcopy, af); 243 } 244 break; 245 #ifdef INET6 246 case AF_INET6: 247 error = nd6_resolve(ifp, rt, m, dst, eh->ether_dhost); 248 if (error) 249 return (error); 250 eh->ether_type = htons(ETHERTYPE_IPV6); 251 break; 252 #endif 253 #ifdef MPLS 254 case AF_MPLS: 255 if (rt == NULL) 256 senderr(EHOSTUNREACH); 257 258 if (!ISSET(ifp->if_xflags, IFXF_MPLS)) 259 senderr(ENETUNREACH); 260 261 dst = ISSET(rt->rt_flags, RTF_GATEWAY) ? 262 rt->rt_gateway : rt_key(rt); 263 264 switch (dst->sa_family) { 265 case AF_LINK: 266 if (satosdl(dst)->sdl_alen < sizeof(eh->ether_dhost)) 267 senderr(EHOSTUNREACH); 268 memcpy(eh->ether_dhost, LLADDR(satosdl(dst)), 269 sizeof(eh->ether_dhost)); 270 break; 271 #ifdef INET6 272 case AF_INET6: 273 error = nd6_resolve(ifp, rt, m, dst, eh->ether_dhost); 274 if (error) 275 return (error); 276 break; 277 #endif 278 case AF_INET: 279 error = arpresolve(ifp, rt, m, dst, eh->ether_dhost); 280 if (error) 281 return (error); 282 break; 283 default: 284 senderr(EHOSTUNREACH); 285 } 286 /* XXX handling for simplex devices in case of M/BCAST ?? */ 287 if (m->m_flags & (M_BCAST | M_MCAST)) 288 eh->ether_type = htons(ETHERTYPE_MPLS_MCAST); 289 else 290 eh->ether_type = htons(ETHERTYPE_MPLS); 291 break; 292 #endif /* MPLS */ 293 case pseudo_AF_HDRCMPLT: 294 /* take the whole header from the sa */ 295 memcpy(eh, dst->sa_data, sizeof(*eh)); 296 return (0); 297 298 case AF_UNSPEC: 299 /* take the dst and type from the sa, but get src below */ 300 memcpy(eh, dst->sa_data, sizeof(*eh)); 301 break; 302 303 default: 304 printf("%s: can't handle af%d\n", ifp->if_xname, af); 305 senderr(EAFNOSUPPORT); 306 } 307 308 memcpy(eh->ether_shost, ac->ac_enaddr, sizeof(eh->ether_shost)); 309 310 return (0); 311 312 bad: 313 m_freem(m); 314 return (error); 315 } 316 317 struct mbuf* 318 ether_encap(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 319 struct rtentry *rt, int *errorp) 320 { 321 struct ether_header eh; 322 int error; 323 324 error = ether_resolve(ifp, m, dst, rt, &eh); 325 switch (error) { 326 case 0: 327 break; 328 case EAGAIN: 329 error = 0; 330 default: 331 *errorp = error; 332 return (NULL); 333 } 334 335 m = m_prepend(m, ETHER_ALIGN + sizeof(eh), M_DONTWAIT); 336 if (m == NULL) { 337 *errorp = ENOBUFS; 338 return (NULL); 339 } 340 341 m_adj(m, ETHER_ALIGN); 342 memcpy(mtod(m, struct ether_header *), &eh, sizeof(eh)); 343 344 return (m); 345 } 346 347 int 348 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 349 struct rtentry *rt) 350 { 351 int error; 352 353 m = ether_encap(ifp, m, dst, rt, &error); 354 if (m == NULL) 355 return (error); 356 357 return (if_enqueue(ifp, m)); 358 } 359 360 /* 361 * Process a received Ethernet packet. 362 * 363 * Ethernet input has several "phases" of filtering packets to 364 * support virtual/pseudo interfaces before actual layer 3 protocol 365 * handling. 366 * 367 * First phase: 368 * 369 * The first phase supports drivers that aggregate multiple Ethernet 370 * ports into a single logical interface, ie, aggr(4) and trunk(4). 371 * These drivers intercept packets by swapping out the if_input handler 372 * on the "port" interfaces to steal the packets before they get here 373 * to ether_input(). 374 */ 375 void 376 ether_input(struct ifnet *ifp, struct mbuf *m) 377 { 378 struct ether_header *eh; 379 void (*input)(struct ifnet *, struct mbuf *); 380 u_int16_t etype; 381 struct arpcom *ac; 382 const struct ether_brport *eb; 383 unsigned int sdelim = 0; 384 uint64_t dst, self; 385 386 /* Drop short frames */ 387 if (m->m_len < ETHER_HDR_LEN) 388 goto dropanyway; 389 390 /* 391 * Second phase: service delimited packet filtering. 392 * 393 * Let vlan(4) and svlan(4) look at "service delimited" 394 * packets. If a virtual interface does not exist to take 395 * those packets, they're returned to ether_input() so a 396 * bridge can have a go at forwarding them. 397 */ 398 399 eh = mtod(m, struct ether_header *); 400 dst = ether_addr_to_e64((struct ether_addr *)eh->ether_dhost); 401 etype = ntohs(eh->ether_type); 402 403 if (ISSET(m->m_flags, M_VLANTAG) || 404 etype == ETHERTYPE_VLAN || etype == ETHERTYPE_QINQ) { 405 #if NVLAN > 0 406 m = vlan_input(ifp, m, &sdelim); 407 if (m == NULL) 408 return; 409 #else 410 sdelim = 1; 411 #endif 412 } 413 414 /* 415 * Third phase: bridge processing. 416 * 417 * Give the packet to a bridge interface, ie, bridge(4), 418 * veb(4), or tpmr(4), if it is configured. A bridge 419 * may take the packet and forward it to another port, or it 420 * may return it here to ether_input() to support local 421 * delivery to this port. 422 */ 423 424 ac = (struct arpcom *)ifp; 425 426 smr_read_enter(); 427 eb = SMR_PTR_GET(&ac->ac_brport); 428 if (eb != NULL) 429 eb->eb_port_take(eb->eb_port); 430 smr_read_leave(); 431 if (eb != NULL) { 432 m = (*eb->eb_input)(ifp, m, dst, eb->eb_port); 433 eb->eb_port_rele(eb->eb_port); 434 if (m == NULL) { 435 return; 436 } 437 } 438 439 /* 440 * Fourth phase: drop service delimited packets. 441 * 442 * If the packet has a tag, and a bridge didn't want it, 443 * it's not for this port. 444 */ 445 446 if (sdelim) 447 goto dropanyway; 448 449 /* 450 * Fifth phase: destination address check. 451 * 452 * Is the packet specifically addressed to this port? 453 */ 454 455 eh = mtod(m, struct ether_header *); 456 self = ether_addr_to_e64((struct ether_addr *)ac->ac_enaddr); 457 if (dst != self) { 458 #if NCARP > 0 459 /* 460 * If it's not for this port, it could be for carp(4). 461 */ 462 if (ifp->if_type != IFT_CARP && 463 !SRPL_EMPTY_LOCKED(&ifp->if_carp)) { 464 m = carp_input(ifp, m, dst); 465 if (m == NULL) 466 return; 467 468 eh = mtod(m, struct ether_header *); 469 } 470 #endif 471 472 /* 473 * If not, it must be multicast or broadcast to go further. 474 */ 475 if (!ETH64_IS_MULTICAST(dst)) 476 goto dropanyway; 477 478 /* 479 * If this is not a simplex interface, drop the packet 480 * if it came from us. 481 */ 482 if ((ifp->if_flags & IFF_SIMPLEX) == 0) { 483 uint64_t src = ether_addr_to_e64( 484 (struct ether_addr *)eh->ether_shost); 485 if (self == src) 486 goto dropanyway; 487 } 488 489 SET(m->m_flags, ETH64_IS_BROADCAST(dst) ? M_BCAST : M_MCAST); 490 ifp->if_imcasts++; 491 } 492 493 /* 494 * Sixth phase: protocol demux. 495 * 496 * At this point it is known that the packet is destined 497 * for layer 3 protocol handling on the local port. 498 */ 499 etype = ntohs(eh->ether_type); 500 501 switch (etype) { 502 case ETHERTYPE_IP: 503 input = ipv4_input; 504 break; 505 506 case ETHERTYPE_ARP: 507 if (ifp->if_flags & IFF_NOARP) 508 goto dropanyway; 509 input = arpinput; 510 break; 511 512 case ETHERTYPE_REVARP: 513 if (ifp->if_flags & IFF_NOARP) 514 goto dropanyway; 515 input = revarpinput; 516 break; 517 518 #ifdef INET6 519 /* 520 * Schedule IPv6 software interrupt for incoming IPv6 packet. 521 */ 522 case ETHERTYPE_IPV6: 523 input = ipv6_input; 524 break; 525 #endif /* INET6 */ 526 #if NPPPOE > 0 || defined(PIPEX) 527 case ETHERTYPE_PPPOEDISC: 528 case ETHERTYPE_PPPOE: 529 if (m->m_flags & (M_MCAST | M_BCAST)) 530 goto dropanyway; 531 #ifdef PIPEX 532 if (pipex_enable) { 533 struct pipex_session *session; 534 535 if ((session = pipex_pppoe_lookup_session(m)) != NULL) { 536 pipex_pppoe_input(m, session); 537 return; 538 } 539 } 540 #endif 541 if (etype == ETHERTYPE_PPPOEDISC) 542 pppoe_disc_input(m); 543 else 544 pppoe_data_input(m); 545 return; 546 #endif 547 #ifdef MPLS 548 case ETHERTYPE_MPLS: 549 case ETHERTYPE_MPLS_MCAST: 550 input = mpls_input; 551 break; 552 #endif 553 #if NBPE > 0 554 case ETHERTYPE_PBB: 555 bpe_input(ifp, m); 556 return; 557 #endif 558 default: 559 goto dropanyway; 560 } 561 562 m_adj(m, sizeof(*eh)); 563 (*input)(ifp, m); 564 return; 565 dropanyway: 566 m_freem(m); 567 return; 568 } 569 570 int 571 ether_brport_isset(struct ifnet *ifp) 572 { 573 struct arpcom *ac = (struct arpcom *)ifp; 574 575 KERNEL_ASSERT_LOCKED(); 576 if (SMR_PTR_GET_LOCKED(&ac->ac_brport) != NULL) 577 return (EBUSY); 578 579 return (0); 580 } 581 582 void 583 ether_brport_set(struct ifnet *ifp, const struct ether_brport *eb) 584 { 585 struct arpcom *ac = (struct arpcom *)ifp; 586 587 KERNEL_ASSERT_LOCKED(); 588 KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_brport) == NULL, 589 "%s setting an already set brport", ifp->if_xname); 590 591 SMR_PTR_SET_LOCKED(&ac->ac_brport, eb); 592 } 593 594 void 595 ether_brport_clr(struct ifnet *ifp) 596 { 597 struct arpcom *ac = (struct arpcom *)ifp; 598 599 KERNEL_ASSERT_LOCKED(); 600 KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_brport) != NULL, 601 "%s clearing an already clear brport", ifp->if_xname); 602 603 SMR_PTR_SET_LOCKED(&ac->ac_brport, NULL); 604 } 605 606 const struct ether_brport * 607 ether_brport_get(struct ifnet *ifp) 608 { 609 struct arpcom *ac = (struct arpcom *)ifp; 610 SMR_ASSERT_CRITICAL(); 611 return (SMR_PTR_GET(&ac->ac_brport)); 612 } 613 614 const struct ether_brport * 615 ether_brport_get_locked(struct ifnet *ifp) 616 { 617 struct arpcom *ac = (struct arpcom *)ifp; 618 KERNEL_ASSERT_LOCKED(); 619 return (SMR_PTR_GET_LOCKED(&ac->ac_brport)); 620 } 621 622 /* 623 * Convert Ethernet address to printable (loggable) representation. 624 */ 625 static char digits[] = "0123456789abcdef"; 626 char * 627 ether_sprintf(u_char *ap) 628 { 629 int i; 630 static char etherbuf[ETHER_ADDR_LEN * 3]; 631 char *cp = etherbuf; 632 633 for (i = 0; i < ETHER_ADDR_LEN; i++) { 634 *cp++ = digits[*ap >> 4]; 635 *cp++ = digits[*ap++ & 0xf]; 636 *cp++ = ':'; 637 } 638 *--cp = 0; 639 return (etherbuf); 640 } 641 642 /* 643 * Generate a (hopefully) acceptable MAC address, if asked. 644 */ 645 void 646 ether_fakeaddr(struct ifnet *ifp) 647 { 648 static int unit; 649 int rng = arc4random(); 650 651 /* Non-multicast; locally administered address */ 652 ((struct arpcom *)ifp)->ac_enaddr[0] = 0xfe; 653 ((struct arpcom *)ifp)->ac_enaddr[1] = 0xe1; 654 ((struct arpcom *)ifp)->ac_enaddr[2] = 0xba; 655 ((struct arpcom *)ifp)->ac_enaddr[3] = 0xd0 | (unit++ & 0xf); 656 ((struct arpcom *)ifp)->ac_enaddr[4] = rng; 657 ((struct arpcom *)ifp)->ac_enaddr[5] = rng >> 8; 658 } 659 660 /* 661 * Perform common duties while attaching to interface list 662 */ 663 void 664 ether_ifattach(struct ifnet *ifp) 665 { 666 struct arpcom *ac = (struct arpcom *)ifp; 667 668 /* 669 * Any interface which provides a MAC address which is obviously 670 * invalid gets whacked, so that users will notice. 671 */ 672 if (ETHER_IS_MULTICAST(((struct arpcom *)ifp)->ac_enaddr)) 673 ether_fakeaddr(ifp); 674 675 ifp->if_type = IFT_ETHER; 676 ifp->if_addrlen = ETHER_ADDR_LEN; 677 ifp->if_hdrlen = ETHER_HDR_LEN; 678 ifp->if_mtu = ETHERMTU; 679 ifp->if_input = ether_input; 680 if (ifp->if_output == NULL) 681 ifp->if_output = ether_output; 682 ifp->if_rtrequest = ether_rtrequest; 683 684 if (ifp->if_hardmtu == 0) 685 ifp->if_hardmtu = ETHERMTU; 686 687 if_alloc_sadl(ifp); 688 memcpy(LLADDR(ifp->if_sadl), ac->ac_enaddr, ifp->if_addrlen); 689 LIST_INIT(&ac->ac_multiaddrs); 690 #if NBPFILTER > 0 691 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, ETHER_HDR_LEN); 692 #endif 693 } 694 695 void 696 ether_ifdetach(struct ifnet *ifp) 697 { 698 struct arpcom *ac = (struct arpcom *)ifp; 699 struct ether_multi *enm; 700 701 /* Undo pseudo-driver changes. */ 702 if_deactivate(ifp); 703 704 for (enm = LIST_FIRST(&ac->ac_multiaddrs); 705 enm != NULL; 706 enm = LIST_FIRST(&ac->ac_multiaddrs)) { 707 LIST_REMOVE(enm, enm_list); 708 free(enm, M_IFMADDR, sizeof *enm); 709 } 710 } 711 712 #if 0 713 /* 714 * This is for reference. We have table-driven versions of the 715 * crc32 generators, which are faster than the double-loop. 716 */ 717 u_int32_t __pure 718 ether_crc32_le_update(u_int_32_t crc, const u_int8_t *buf, size_t len) 719 { 720 u_int32_t c, carry; 721 size_t i, j; 722 723 for (i = 0; i < len; i++) { 724 c = buf[i]; 725 for (j = 0; j < 8; j++) { 726 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 727 crc >>= 1; 728 c >>= 1; 729 if (carry) 730 crc = (crc ^ ETHER_CRC_POLY_LE); 731 } 732 } 733 734 return (crc); 735 } 736 737 u_int32_t __pure 738 ether_crc32_be_update(u_int_32_t crc, const u_int8_t *buf, size_t len) 739 { 740 u_int32_t c, carry; 741 size_t i, j; 742 743 for (i = 0; i < len; i++) { 744 c = buf[i]; 745 for (j = 0; j < 8; j++) { 746 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 747 crc <<= 1; 748 c >>= 1; 749 if (carry) 750 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 751 } 752 } 753 754 return (crc); 755 } 756 #else 757 u_int32_t __pure 758 ether_crc32_le_update(u_int32_t crc, const u_int8_t *buf, size_t len) 759 { 760 static const u_int32_t crctab[] = { 761 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 762 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 763 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 764 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 765 }; 766 size_t i; 767 768 for (i = 0; i < len; i++) { 769 crc ^= buf[i]; 770 crc = (crc >> 4) ^ crctab[crc & 0xf]; 771 crc = (crc >> 4) ^ crctab[crc & 0xf]; 772 } 773 774 return (crc); 775 } 776 777 u_int32_t __pure 778 ether_crc32_be_update(u_int32_t crc, const u_int8_t *buf, size_t len) 779 { 780 static const u_int8_t rev[] = { 781 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, 782 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf 783 }; 784 static const u_int32_t crctab[] = { 785 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 786 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 787 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 788 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd 789 }; 790 size_t i; 791 u_int8_t data; 792 793 for (i = 0; i < len; i++) { 794 data = buf[i]; 795 crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data & 0xf]]; 796 crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data >> 4]]; 797 } 798 799 return (crc); 800 } 801 #endif 802 803 u_int32_t 804 ether_crc32_le(const u_int8_t *buf, size_t len) 805 { 806 return ether_crc32_le_update(0xffffffff, buf, len); 807 } 808 809 u_int32_t 810 ether_crc32_be(const u_int8_t *buf, size_t len) 811 { 812 return ether_crc32_be_update(0xffffffff, buf, len); 813 } 814 815 u_char ether_ipmulticast_min[ETHER_ADDR_LEN] = 816 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 817 u_char ether_ipmulticast_max[ETHER_ADDR_LEN] = 818 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 819 820 #ifdef INET6 821 u_char ether_ip6multicast_min[ETHER_ADDR_LEN] = 822 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 823 u_char ether_ip6multicast_max[ETHER_ADDR_LEN] = 824 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff }; 825 #endif 826 827 /* 828 * Convert a sockaddr into an Ethernet address or range of Ethernet 829 * addresses. 830 */ 831 int 832 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN], 833 u_int8_t addrhi[ETHER_ADDR_LEN]) 834 { 835 struct sockaddr_in *sin; 836 #ifdef INET6 837 struct sockaddr_in6 *sin6; 838 #endif /* INET6 */ 839 840 switch (sa->sa_family) { 841 842 case AF_UNSPEC: 843 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN); 844 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 845 break; 846 847 case AF_INET: 848 sin = satosin(sa); 849 if (sin->sin_addr.s_addr == INADDR_ANY) { 850 /* 851 * An IP address of INADDR_ANY means listen to 852 * or stop listening to all of the Ethernet 853 * multicast addresses used for IP. 854 * (This is for the sake of IP multicast routers.) 855 */ 856 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN); 857 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN); 858 } else { 859 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 860 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 861 } 862 break; 863 #ifdef INET6 864 case AF_INET6: 865 sin6 = satosin6(sa); 866 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 867 /* 868 * An IP6 address of 0 means listen to or stop 869 * listening to all of the Ethernet multicast 870 * address used for IP6. 871 * 872 * (This might not be healthy, given IPv6's reliance on 873 * multicast for things like neighbor discovery. 874 * Perhaps initializing all-nodes, solicited nodes, and 875 * possibly all-routers for this interface afterwards 876 * is not a bad idea.) 877 */ 878 879 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN); 880 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN); 881 } else { 882 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 883 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 884 } 885 break; 886 #endif 887 888 default: 889 return (EAFNOSUPPORT); 890 } 891 return (0); 892 } 893 894 /* 895 * Add an Ethernet multicast address or range of addresses to the list for a 896 * given interface. 897 */ 898 int 899 ether_addmulti(struct ifreq *ifr, struct arpcom *ac) 900 { 901 struct ether_multi *enm; 902 u_char addrlo[ETHER_ADDR_LEN]; 903 u_char addrhi[ETHER_ADDR_LEN]; 904 int s = splnet(), error; 905 906 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 907 if (error != 0) { 908 splx(s); 909 return (error); 910 } 911 912 /* 913 * Verify that we have valid Ethernet multicast addresses. 914 */ 915 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) { 916 splx(s); 917 return (EINVAL); 918 } 919 /* 920 * See if the address range is already in the list. 921 */ 922 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 923 if (enm != NULL) { 924 /* 925 * Found it; just increment the reference count. 926 */ 927 ++enm->enm_refcount; 928 splx(s); 929 return (0); 930 } 931 /* 932 * New address or range; malloc a new multicast record 933 * and link it into the interface's multicast list. 934 */ 935 enm = malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); 936 if (enm == NULL) { 937 splx(s); 938 return (ENOBUFS); 939 } 940 memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN); 941 memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN); 942 enm->enm_refcount = 1; 943 LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list); 944 ac->ac_multicnt++; 945 if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) 946 ac->ac_multirangecnt++; 947 splx(s); 948 /* 949 * Return ENETRESET to inform the driver that the list has changed 950 * and its reception filter should be adjusted accordingly. 951 */ 952 return (ENETRESET); 953 } 954 955 /* 956 * Delete a multicast address record. 957 */ 958 int 959 ether_delmulti(struct ifreq *ifr, struct arpcom *ac) 960 { 961 struct ether_multi *enm; 962 u_char addrlo[ETHER_ADDR_LEN]; 963 u_char addrhi[ETHER_ADDR_LEN]; 964 int s = splnet(), error; 965 966 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 967 if (error != 0) { 968 splx(s); 969 return (error); 970 } 971 972 /* 973 * Look up the address in our list. 974 */ 975 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 976 if (enm == NULL) { 977 splx(s); 978 return (ENXIO); 979 } 980 if (--enm->enm_refcount != 0) { 981 /* 982 * Still some claims to this record. 983 */ 984 splx(s); 985 return (0); 986 } 987 /* 988 * No remaining claims to this record; unlink and free it. 989 */ 990 LIST_REMOVE(enm, enm_list); 991 free(enm, M_IFMADDR, sizeof *enm); 992 ac->ac_multicnt--; 993 if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) 994 ac->ac_multirangecnt--; 995 splx(s); 996 /* 997 * Return ENETRESET to inform the driver that the list has changed 998 * and its reception filter should be adjusted accordingly. 999 */ 1000 return (ENETRESET); 1001 } 1002 1003 uint64_t 1004 ether_addr_to_e64(const struct ether_addr *ea) 1005 { 1006 uint64_t e64 = 0; 1007 size_t i; 1008 1009 for (i = 0; i < nitems(ea->ether_addr_octet); i++) { 1010 e64 <<= 8; 1011 e64 |= ea->ether_addr_octet[i]; 1012 } 1013 1014 return (e64); 1015 } 1016 1017 void 1018 ether_e64_to_addr(struct ether_addr *ea, uint64_t e64) 1019 { 1020 size_t i = nitems(ea->ether_addr_octet); 1021 1022 do { 1023 ea->ether_addr_octet[--i] = e64; 1024 e64 >>= 8; 1025 } while (i > 0); 1026 } 1027