1 /* $OpenBSD: if_ethersubr.c,v 1.291 2023/07/27 20:21:25 jan 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 #include <netinet/ip.h> 102 #include <netinet/ip6.h> 103 #include <netinet/tcp.h> 104 #include <netinet/udp.h> 105 106 #if NBPFILTER > 0 107 #include <net/bpf.h> 108 #endif 109 110 #include "vlan.h" 111 #if NVLAN > 0 112 #include <net/if_vlan_var.h> 113 #endif 114 115 #include "carp.h" 116 #if NCARP > 0 117 #include <netinet/ip_carp.h> 118 #endif 119 120 #include "pppoe.h" 121 #if NPPPOE > 0 122 #include <net/if_pppoe.h> 123 #endif 124 125 #include "bpe.h" 126 #if NBPE > 0 127 #include <net/if_bpe.h> 128 #endif 129 130 #ifdef INET6 131 #include <netinet6/in6_var.h> 132 #include <netinet6/nd6.h> 133 #endif 134 135 #ifdef PIPEX 136 #include <net/pipex.h> 137 #endif 138 139 #ifdef MPLS 140 #include <netmpls/mpls.h> 141 #endif /* MPLS */ 142 143 u_int8_t etherbroadcastaddr[ETHER_ADDR_LEN] = 144 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 145 u_int8_t etheranyaddr[ETHER_ADDR_LEN] = 146 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 147 #define senderr(e) { error = (e); goto bad;} 148 149 int 150 ether_ioctl(struct ifnet *ifp, struct arpcom *arp, u_long cmd, caddr_t data) 151 { 152 struct ifreq *ifr = (struct ifreq *)data; 153 int error = 0; 154 155 switch (cmd) { 156 case SIOCSIFADDR: 157 break; 158 159 case SIOCSIFMTU: 160 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ifp->if_hardmtu) 161 error = EINVAL; 162 else 163 ifp->if_mtu = ifr->ifr_mtu; 164 break; 165 166 case SIOCADDMULTI: 167 case SIOCDELMULTI: 168 if (ifp->if_flags & IFF_MULTICAST) { 169 error = (cmd == SIOCADDMULTI) ? 170 ether_addmulti(ifr, arp) : 171 ether_delmulti(ifr, arp); 172 } else 173 error = ENOTTY; 174 break; 175 176 default: 177 error = ENOTTY; 178 } 179 180 return (error); 181 } 182 183 184 void 185 ether_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt) 186 { 187 if (rt == NULL) 188 return; 189 190 switch (rt_key(rt)->sa_family) { 191 case AF_INET: 192 arp_rtrequest(ifp, req, rt); 193 break; 194 #ifdef INET6 195 case AF_INET6: 196 nd6_rtrequest(ifp, req, rt); 197 break; 198 #endif 199 default: 200 break; 201 } 202 } 203 204 int 205 ether_resolve(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 206 struct rtentry *rt, struct ether_header *eh) 207 { 208 struct arpcom *ac = (struct arpcom *)ifp; 209 sa_family_t af = dst->sa_family; 210 int error = 0; 211 212 if (!ISSET(ifp->if_flags, IFF_RUNNING)) 213 senderr(ENETDOWN); 214 215 KASSERT(rt != NULL || ISSET(m->m_flags, M_MCAST|M_BCAST) || 216 af == AF_UNSPEC || af == pseudo_AF_HDRCMPLT); 217 218 #ifdef DIAGNOSTIC 219 if (ifp->if_rdomain != rtable_l2(m->m_pkthdr.ph_rtableid)) { 220 printf("%s: trying to send packet on wrong domain. " 221 "if %d vs. mbuf %d\n", ifp->if_xname, 222 ifp->if_rdomain, rtable_l2(m->m_pkthdr.ph_rtableid)); 223 } 224 #endif 225 226 switch (af) { 227 case AF_INET: 228 error = arpresolve(ifp, rt, m, dst, eh->ether_dhost); 229 if (error) 230 return (error); 231 eh->ether_type = htons(ETHERTYPE_IP); 232 233 /* 234 * If broadcasting on a simplex interface, loopback a copy. 235 * The checksum must be calculated in software. Keep the 236 * condition in sync with in_ifcap_cksum(). 237 */ 238 if (ISSET(m->m_flags, M_BCAST) && 239 ISSET(ifp->if_flags, IFF_SIMPLEX) && 240 !m->m_pkthdr.pf.routed) { 241 struct mbuf *mcopy; 242 243 /* XXX Should we input an unencrypted IPsec packet? */ 244 mcopy = m_copym(m, 0, M_COPYALL, M_NOWAIT); 245 if (mcopy != NULL) 246 if_input_local(ifp, mcopy, af); 247 } 248 break; 249 #ifdef INET6 250 case AF_INET6: 251 error = nd6_resolve(ifp, rt, m, dst, eh->ether_dhost); 252 if (error) 253 return (error); 254 eh->ether_type = htons(ETHERTYPE_IPV6); 255 break; 256 #endif 257 #ifdef MPLS 258 case AF_MPLS: 259 if (rt == NULL) 260 senderr(EHOSTUNREACH); 261 262 if (!ISSET(ifp->if_xflags, IFXF_MPLS)) 263 senderr(ENETUNREACH); 264 265 dst = ISSET(rt->rt_flags, RTF_GATEWAY) ? 266 rt->rt_gateway : rt_key(rt); 267 268 switch (dst->sa_family) { 269 case AF_LINK: 270 if (satosdl(dst)->sdl_alen < sizeof(eh->ether_dhost)) 271 senderr(EHOSTUNREACH); 272 memcpy(eh->ether_dhost, LLADDR(satosdl(dst)), 273 sizeof(eh->ether_dhost)); 274 break; 275 #ifdef INET6 276 case AF_INET6: 277 error = nd6_resolve(ifp, rt, m, dst, eh->ether_dhost); 278 if (error) 279 return (error); 280 break; 281 #endif 282 case AF_INET: 283 error = arpresolve(ifp, rt, m, dst, eh->ether_dhost); 284 if (error) 285 return (error); 286 break; 287 default: 288 senderr(EHOSTUNREACH); 289 } 290 /* XXX handling for simplex devices in case of M/BCAST ?? */ 291 if (m->m_flags & (M_BCAST | M_MCAST)) 292 eh->ether_type = htons(ETHERTYPE_MPLS_MCAST); 293 else 294 eh->ether_type = htons(ETHERTYPE_MPLS); 295 break; 296 #endif /* MPLS */ 297 case pseudo_AF_HDRCMPLT: 298 /* take the whole header from the sa */ 299 memcpy(eh, dst->sa_data, sizeof(*eh)); 300 return (0); 301 302 case AF_UNSPEC: 303 /* take the dst and type from the sa, but get src below */ 304 memcpy(eh, dst->sa_data, sizeof(*eh)); 305 break; 306 307 default: 308 printf("%s: can't handle af%d\n", ifp->if_xname, af); 309 senderr(EAFNOSUPPORT); 310 } 311 312 memcpy(eh->ether_shost, ac->ac_enaddr, sizeof(eh->ether_shost)); 313 314 return (0); 315 316 bad: 317 m_freem(m); 318 return (error); 319 } 320 321 struct mbuf* 322 ether_encap(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 323 struct rtentry *rt, int *errorp) 324 { 325 struct ether_header eh; 326 int error; 327 328 error = ether_resolve(ifp, m, dst, rt, &eh); 329 switch (error) { 330 case 0: 331 break; 332 case EAGAIN: 333 error = 0; 334 default: 335 *errorp = error; 336 return (NULL); 337 } 338 339 m = m_prepend(m, ETHER_ALIGN + sizeof(eh), M_DONTWAIT); 340 if (m == NULL) { 341 *errorp = ENOBUFS; 342 return (NULL); 343 } 344 345 m_adj(m, ETHER_ALIGN); 346 memcpy(mtod(m, struct ether_header *), &eh, sizeof(eh)); 347 348 return (m); 349 } 350 351 int 352 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 353 struct rtentry *rt) 354 { 355 int error; 356 357 m = ether_encap(ifp, m, dst, rt, &error); 358 if (m == NULL) 359 return (error); 360 361 return (if_enqueue(ifp, m)); 362 } 363 364 /* 365 * Process a received Ethernet packet. 366 * 367 * Ethernet input has several "phases" of filtering packets to 368 * support virtual/pseudo interfaces before actual layer 3 protocol 369 * handling. 370 * 371 * First phase: 372 * 373 * The first phase supports drivers that aggregate multiple Ethernet 374 * ports into a single logical interface, ie, aggr(4) and trunk(4). 375 * These drivers intercept packets by swapping out the if_input handler 376 * on the "port" interfaces to steal the packets before they get here 377 * to ether_input(). 378 */ 379 void 380 ether_input(struct ifnet *ifp, struct mbuf *m) 381 { 382 struct ether_header *eh; 383 void (*input)(struct ifnet *, struct mbuf *); 384 u_int16_t etype; 385 struct arpcom *ac; 386 const struct ether_brport *eb; 387 unsigned int sdelim = 0; 388 uint64_t dst, self; 389 390 /* Drop short frames */ 391 if (m->m_len < ETHER_HDR_LEN) 392 goto dropanyway; 393 394 /* 395 * Second phase: service delimited packet filtering. 396 * 397 * Let vlan(4) and svlan(4) look at "service delimited" 398 * packets. If a virtual interface does not exist to take 399 * those packets, they're returned to ether_input() so a 400 * bridge can have a go at forwarding them. 401 */ 402 403 eh = mtod(m, struct ether_header *); 404 dst = ether_addr_to_e64((struct ether_addr *)eh->ether_dhost); 405 etype = ntohs(eh->ether_type); 406 407 if (ISSET(m->m_flags, M_VLANTAG) || 408 etype == ETHERTYPE_VLAN || etype == ETHERTYPE_QINQ) { 409 #if NVLAN > 0 410 m = vlan_input(ifp, m, &sdelim); 411 if (m == NULL) 412 return; 413 #else 414 sdelim = 1; 415 #endif 416 } 417 418 /* 419 * Third phase: bridge processing. 420 * 421 * Give the packet to a bridge interface, ie, bridge(4), 422 * veb(4), or tpmr(4), if it is configured. A bridge 423 * may take the packet and forward it to another port, or it 424 * may return it here to ether_input() to support local 425 * delivery to this port. 426 */ 427 428 ac = (struct arpcom *)ifp; 429 430 smr_read_enter(); 431 eb = SMR_PTR_GET(&ac->ac_brport); 432 if (eb != NULL) 433 eb->eb_port_take(eb->eb_port); 434 smr_read_leave(); 435 if (eb != NULL) { 436 m = (*eb->eb_input)(ifp, m, dst, eb->eb_port); 437 eb->eb_port_rele(eb->eb_port); 438 if (m == NULL) { 439 return; 440 } 441 } 442 443 /* 444 * Fourth phase: drop service delimited packets. 445 * 446 * If the packet has a tag, and a bridge didn't want it, 447 * it's not for this port. 448 */ 449 450 if (sdelim) 451 goto dropanyway; 452 453 /* 454 * Fifth phase: destination address check. 455 * 456 * Is the packet specifically addressed to this port? 457 */ 458 459 eh = mtod(m, struct ether_header *); 460 self = ether_addr_to_e64((struct ether_addr *)ac->ac_enaddr); 461 if (dst != self) { 462 #if NCARP > 0 463 /* 464 * If it's not for this port, it could be for carp(4). 465 */ 466 if (ifp->if_type != IFT_CARP && 467 !SRPL_EMPTY_LOCKED(&ifp->if_carp)) { 468 m = carp_input(ifp, m, dst); 469 if (m == NULL) 470 return; 471 472 eh = mtod(m, struct ether_header *); 473 } 474 #endif 475 476 /* 477 * If not, it must be multicast or broadcast to go further. 478 */ 479 if (!ETH64_IS_MULTICAST(dst)) 480 goto dropanyway; 481 482 /* 483 * If this is not a simplex interface, drop the packet 484 * if it came from us. 485 */ 486 if ((ifp->if_flags & IFF_SIMPLEX) == 0) { 487 uint64_t src = ether_addr_to_e64( 488 (struct ether_addr *)eh->ether_shost); 489 if (self == src) 490 goto dropanyway; 491 } 492 493 SET(m->m_flags, ETH64_IS_BROADCAST(dst) ? M_BCAST : M_MCAST); 494 ifp->if_imcasts++; 495 } 496 497 /* 498 * Sixth phase: protocol demux. 499 * 500 * At this point it is known that the packet is destined 501 * for layer 3 protocol handling on the local port. 502 */ 503 etype = ntohs(eh->ether_type); 504 505 switch (etype) { 506 case ETHERTYPE_IP: 507 input = ipv4_input; 508 break; 509 510 case ETHERTYPE_ARP: 511 if (ifp->if_flags & IFF_NOARP) 512 goto dropanyway; 513 input = arpinput; 514 break; 515 516 case ETHERTYPE_REVARP: 517 if (ifp->if_flags & IFF_NOARP) 518 goto dropanyway; 519 input = revarpinput; 520 break; 521 522 #ifdef INET6 523 /* 524 * Schedule IPv6 software interrupt for incoming IPv6 packet. 525 */ 526 case ETHERTYPE_IPV6: 527 input = ipv6_input; 528 break; 529 #endif /* INET6 */ 530 #if NPPPOE > 0 || defined(PIPEX) 531 case ETHERTYPE_PPPOEDISC: 532 case ETHERTYPE_PPPOE: 533 if (m->m_flags & (M_MCAST | M_BCAST)) 534 goto dropanyway; 535 #ifdef PIPEX 536 if (pipex_enable) { 537 struct pipex_session *session; 538 539 if ((session = pipex_pppoe_lookup_session(m)) != NULL) { 540 pipex_pppoe_input(m, session); 541 pipex_rele_session(session); 542 return; 543 } 544 } 545 #endif 546 if (etype == ETHERTYPE_PPPOEDISC) { 547 if (mq_enqueue(&pppoediscinq, m) == 0) 548 schednetisr(NETISR_PPPOE); 549 } else { 550 if (mq_enqueue(&pppoeinq, m) == 0) 551 schednetisr(NETISR_PPPOE); 552 } 553 return; 554 #endif 555 #ifdef MPLS 556 case ETHERTYPE_MPLS: 557 case ETHERTYPE_MPLS_MCAST: 558 input = mpls_input; 559 break; 560 #endif 561 #if NBPE > 0 562 case ETHERTYPE_PBB: 563 bpe_input(ifp, m); 564 return; 565 #endif 566 default: 567 goto dropanyway; 568 } 569 570 m_adj(m, sizeof(*eh)); 571 (*input)(ifp, m); 572 return; 573 dropanyway: 574 m_freem(m); 575 return; 576 } 577 578 int 579 ether_brport_isset(struct ifnet *ifp) 580 { 581 struct arpcom *ac = (struct arpcom *)ifp; 582 583 KERNEL_ASSERT_LOCKED(); 584 if (SMR_PTR_GET_LOCKED(&ac->ac_brport) != NULL) 585 return (EBUSY); 586 587 return (0); 588 } 589 590 void 591 ether_brport_set(struct ifnet *ifp, const struct ether_brport *eb) 592 { 593 struct arpcom *ac = (struct arpcom *)ifp; 594 595 KERNEL_ASSERT_LOCKED(); 596 KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_brport) == NULL, 597 "%s setting an already set brport", ifp->if_xname); 598 599 SMR_PTR_SET_LOCKED(&ac->ac_brport, eb); 600 } 601 602 void 603 ether_brport_clr(struct ifnet *ifp) 604 { 605 struct arpcom *ac = (struct arpcom *)ifp; 606 607 KERNEL_ASSERT_LOCKED(); 608 KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_brport) != NULL, 609 "%s clearing an already clear brport", ifp->if_xname); 610 611 SMR_PTR_SET_LOCKED(&ac->ac_brport, NULL); 612 } 613 614 const struct ether_brport * 615 ether_brport_get(struct ifnet *ifp) 616 { 617 struct arpcom *ac = (struct arpcom *)ifp; 618 SMR_ASSERT_CRITICAL(); 619 return (SMR_PTR_GET(&ac->ac_brport)); 620 } 621 622 const struct ether_brport * 623 ether_brport_get_locked(struct ifnet *ifp) 624 { 625 struct arpcom *ac = (struct arpcom *)ifp; 626 KERNEL_ASSERT_LOCKED(); 627 return (SMR_PTR_GET_LOCKED(&ac->ac_brport)); 628 } 629 630 /* 631 * Convert Ethernet address to printable (loggable) representation. 632 */ 633 static char digits[] = "0123456789abcdef"; 634 char * 635 ether_sprintf(u_char *ap) 636 { 637 int i; 638 static char etherbuf[ETHER_ADDR_LEN * 3]; 639 char *cp = etherbuf; 640 641 for (i = 0; i < ETHER_ADDR_LEN; i++) { 642 *cp++ = digits[*ap >> 4]; 643 *cp++ = digits[*ap++ & 0xf]; 644 *cp++ = ':'; 645 } 646 *--cp = 0; 647 return (etherbuf); 648 } 649 650 /* 651 * Generate a (hopefully) acceptable MAC address, if asked. 652 */ 653 void 654 ether_fakeaddr(struct ifnet *ifp) 655 { 656 static int unit; 657 int rng = arc4random(); 658 659 /* Non-multicast; locally administered address */ 660 ((struct arpcom *)ifp)->ac_enaddr[0] = 0xfe; 661 ((struct arpcom *)ifp)->ac_enaddr[1] = 0xe1; 662 ((struct arpcom *)ifp)->ac_enaddr[2] = 0xba; 663 ((struct arpcom *)ifp)->ac_enaddr[3] = 0xd0 | (unit++ & 0xf); 664 ((struct arpcom *)ifp)->ac_enaddr[4] = rng; 665 ((struct arpcom *)ifp)->ac_enaddr[5] = rng >> 8; 666 } 667 668 /* 669 * Perform common duties while attaching to interface list 670 */ 671 void 672 ether_ifattach(struct ifnet *ifp) 673 { 674 struct arpcom *ac = (struct arpcom *)ifp; 675 676 /* 677 * Any interface which provides a MAC address which is obviously 678 * invalid gets whacked, so that users will notice. 679 */ 680 if (ETHER_IS_MULTICAST(((struct arpcom *)ifp)->ac_enaddr)) 681 ether_fakeaddr(ifp); 682 683 ifp->if_type = IFT_ETHER; 684 ifp->if_addrlen = ETHER_ADDR_LEN; 685 ifp->if_hdrlen = ETHER_HDR_LEN; 686 ifp->if_mtu = ETHERMTU; 687 ifp->if_input = ether_input; 688 if (ifp->if_output == NULL) 689 ifp->if_output = ether_output; 690 ifp->if_rtrequest = ether_rtrequest; 691 692 if (ifp->if_hardmtu == 0) 693 ifp->if_hardmtu = ETHERMTU; 694 695 if_alloc_sadl(ifp); 696 memcpy(LLADDR(ifp->if_sadl), ac->ac_enaddr, ifp->if_addrlen); 697 LIST_INIT(&ac->ac_multiaddrs); 698 #if NBPFILTER > 0 699 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, ETHER_HDR_LEN); 700 #endif 701 } 702 703 void 704 ether_ifdetach(struct ifnet *ifp) 705 { 706 struct arpcom *ac = (struct arpcom *)ifp; 707 struct ether_multi *enm; 708 709 /* Undo pseudo-driver changes. */ 710 if_deactivate(ifp); 711 712 while (!LIST_EMPTY(&ac->ac_multiaddrs)) { 713 enm = LIST_FIRST(&ac->ac_multiaddrs); 714 LIST_REMOVE(enm, enm_list); 715 free(enm, M_IFMADDR, sizeof *enm); 716 } 717 } 718 719 #if 0 720 /* 721 * This is for reference. We have table-driven versions of the 722 * crc32 generators, which are faster than the double-loop. 723 */ 724 u_int32_t __pure 725 ether_crc32_le_update(u_int_32_t crc, const u_int8_t *buf, size_t len) 726 { 727 u_int32_t c, carry; 728 size_t i, j; 729 730 for (i = 0; i < len; i++) { 731 c = buf[i]; 732 for (j = 0; j < 8; j++) { 733 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 734 crc >>= 1; 735 c >>= 1; 736 if (carry) 737 crc = (crc ^ ETHER_CRC_POLY_LE); 738 } 739 } 740 741 return (crc); 742 } 743 744 u_int32_t __pure 745 ether_crc32_be_update(u_int_32_t crc, const u_int8_t *buf, size_t len) 746 { 747 u_int32_t c, carry; 748 size_t i, j; 749 750 for (i = 0; i < len; i++) { 751 c = buf[i]; 752 for (j = 0; j < 8; j++) { 753 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 754 crc <<= 1; 755 c >>= 1; 756 if (carry) 757 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 758 } 759 } 760 761 return (crc); 762 } 763 #else 764 u_int32_t __pure 765 ether_crc32_le_update(u_int32_t crc, const u_int8_t *buf, size_t len) 766 { 767 static const u_int32_t crctab[] = { 768 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 769 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 770 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 771 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 772 }; 773 size_t i; 774 775 for (i = 0; i < len; i++) { 776 crc ^= buf[i]; 777 crc = (crc >> 4) ^ crctab[crc & 0xf]; 778 crc = (crc >> 4) ^ crctab[crc & 0xf]; 779 } 780 781 return (crc); 782 } 783 784 u_int32_t __pure 785 ether_crc32_be_update(u_int32_t crc, const u_int8_t *buf, size_t len) 786 { 787 static const u_int8_t rev[] = { 788 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, 789 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf 790 }; 791 static const u_int32_t crctab[] = { 792 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 793 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 794 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 795 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd 796 }; 797 size_t i; 798 u_int8_t data; 799 800 for (i = 0; i < len; i++) { 801 data = buf[i]; 802 crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data & 0xf]]; 803 crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data >> 4]]; 804 } 805 806 return (crc); 807 } 808 #endif 809 810 u_int32_t 811 ether_crc32_le(const u_int8_t *buf, size_t len) 812 { 813 return ether_crc32_le_update(0xffffffff, buf, len); 814 } 815 816 u_int32_t 817 ether_crc32_be(const u_int8_t *buf, size_t len) 818 { 819 return ether_crc32_be_update(0xffffffff, buf, len); 820 } 821 822 u_char ether_ipmulticast_min[ETHER_ADDR_LEN] = 823 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 824 u_char ether_ipmulticast_max[ETHER_ADDR_LEN] = 825 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 826 827 #ifdef INET6 828 u_char ether_ip6multicast_min[ETHER_ADDR_LEN] = 829 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 830 u_char ether_ip6multicast_max[ETHER_ADDR_LEN] = 831 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff }; 832 #endif 833 834 /* 835 * Convert a sockaddr into an Ethernet address or range of Ethernet 836 * addresses. 837 */ 838 int 839 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN], 840 u_int8_t addrhi[ETHER_ADDR_LEN]) 841 { 842 struct sockaddr_in *sin; 843 #ifdef INET6 844 struct sockaddr_in6 *sin6; 845 #endif /* INET6 */ 846 847 switch (sa->sa_family) { 848 849 case AF_UNSPEC: 850 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN); 851 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 852 break; 853 854 case AF_INET: 855 sin = satosin(sa); 856 if (sin->sin_addr.s_addr == INADDR_ANY) { 857 /* 858 * An IP address of INADDR_ANY means listen to 859 * or stop listening to all of the Ethernet 860 * multicast addresses used for IP. 861 * (This is for the sake of IP multicast routers.) 862 */ 863 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN); 864 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN); 865 } else { 866 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 867 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 868 } 869 break; 870 #ifdef INET6 871 case AF_INET6: 872 sin6 = satosin6(sa); 873 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 874 /* 875 * An IP6 address of 0 means listen to or stop 876 * listening to all of the Ethernet multicast 877 * address used for IP6. 878 * 879 * (This might not be healthy, given IPv6's reliance on 880 * multicast for things like neighbor discovery. 881 * Perhaps initializing all-nodes, solicited nodes, and 882 * possibly all-routers for this interface afterwards 883 * is not a bad idea.) 884 */ 885 886 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN); 887 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN); 888 } else { 889 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 890 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 891 } 892 break; 893 #endif 894 895 default: 896 return (EAFNOSUPPORT); 897 } 898 return (0); 899 } 900 901 /* 902 * Add an Ethernet multicast address or range of addresses to the list for a 903 * given interface. 904 */ 905 int 906 ether_addmulti(struct ifreq *ifr, struct arpcom *ac) 907 { 908 struct ether_multi *enm; 909 u_char addrlo[ETHER_ADDR_LEN]; 910 u_char addrhi[ETHER_ADDR_LEN]; 911 int s = splnet(), error; 912 913 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 914 if (error != 0) { 915 splx(s); 916 return (error); 917 } 918 919 /* 920 * Verify that we have valid Ethernet multicast addresses. 921 */ 922 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) { 923 splx(s); 924 return (EINVAL); 925 } 926 /* 927 * See if the address range is already in the list. 928 */ 929 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 930 if (enm != NULL) { 931 /* 932 * Found it; just increment the reference count. 933 */ 934 refcnt_take(&enm->enm_refcnt); 935 splx(s); 936 return (0); 937 } 938 /* 939 * New address or range; malloc a new multicast record 940 * and link it into the interface's multicast list. 941 */ 942 enm = malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); 943 if (enm == NULL) { 944 splx(s); 945 return (ENOBUFS); 946 } 947 memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN); 948 memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN); 949 refcnt_init_trace(&enm->enm_refcnt, DT_REFCNT_IDX_ETHMULTI); 950 LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list); 951 ac->ac_multicnt++; 952 if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) 953 ac->ac_multirangecnt++; 954 splx(s); 955 /* 956 * Return ENETRESET to inform the driver that the list has changed 957 * and its reception filter should be adjusted accordingly. 958 */ 959 return (ENETRESET); 960 } 961 962 /* 963 * Delete a multicast address record. 964 */ 965 int 966 ether_delmulti(struct ifreq *ifr, struct arpcom *ac) 967 { 968 struct ether_multi *enm; 969 u_char addrlo[ETHER_ADDR_LEN]; 970 u_char addrhi[ETHER_ADDR_LEN]; 971 int s = splnet(), error; 972 973 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 974 if (error != 0) { 975 splx(s); 976 return (error); 977 } 978 979 /* 980 * Look up the address in our list. 981 */ 982 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 983 if (enm == NULL) { 984 splx(s); 985 return (ENXIO); 986 } 987 if (refcnt_rele(&enm->enm_refcnt) == 0) { 988 /* 989 * Still some claims to this record. 990 */ 991 splx(s); 992 return (0); 993 } 994 /* 995 * No remaining claims to this record; unlink and free it. 996 */ 997 LIST_REMOVE(enm, enm_list); 998 free(enm, M_IFMADDR, sizeof *enm); 999 ac->ac_multicnt--; 1000 if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) 1001 ac->ac_multirangecnt--; 1002 splx(s); 1003 /* 1004 * Return ENETRESET to inform the driver that the list has changed 1005 * and its reception filter should be adjusted accordingly. 1006 */ 1007 return (ENETRESET); 1008 } 1009 1010 uint64_t 1011 ether_addr_to_e64(const struct ether_addr *ea) 1012 { 1013 uint64_t e64 = 0; 1014 size_t i; 1015 1016 for (i = 0; i < nitems(ea->ether_addr_octet); i++) { 1017 e64 <<= 8; 1018 e64 |= ea->ether_addr_octet[i]; 1019 } 1020 1021 return (e64); 1022 } 1023 1024 void 1025 ether_e64_to_addr(struct ether_addr *ea, uint64_t e64) 1026 { 1027 size_t i = nitems(ea->ether_addr_octet); 1028 1029 do { 1030 ea->ether_addr_octet[--i] = e64; 1031 e64 >>= 8; 1032 } while (i > 0); 1033 } 1034 1035 /* Parse different TCP/IP protocol headers for a quick view inside an mbuf. */ 1036 void 1037 ether_extract_headers(struct mbuf *mp, struct ether_extracted *ext) 1038 { 1039 struct mbuf *m; 1040 uint64_t hlen; 1041 int hoff; 1042 uint8_t ipproto; 1043 uint16_t ether_type; 1044 1045 /* Return NULL if header was not recognized. */ 1046 memset(ext, 0, sizeof(*ext)); 1047 1048 if (mp->m_len < sizeof(*ext->eh)) 1049 return; 1050 1051 ext->eh = mtod(mp, struct ether_header *); 1052 ether_type = ntohs(ext->eh->ether_type); 1053 hlen = sizeof(*ext->eh); 1054 1055 #if NVLAN > 0 1056 if (ether_type == ETHERTYPE_VLAN) { 1057 ext->evh = mtod(mp, struct ether_vlan_header *); 1058 ether_type = ntohs(ext->evh->evl_proto); 1059 hlen = sizeof(*ext->evh); 1060 } 1061 #endif 1062 1063 switch (ether_type) { 1064 case ETHERTYPE_IP: 1065 m = m_getptr(mp, hlen, &hoff); 1066 if (m == NULL || m->m_len - hoff < sizeof(*ext->ip4)) 1067 return; 1068 ext->ip4 = (struct ip *)(mtod(m, caddr_t) + hoff); 1069 1070 if (ISSET(ntohs(ext->ip4->ip_off), IP_MF|IP_OFFMASK)) 1071 return; 1072 1073 hlen = ext->ip4->ip_hl << 2; 1074 ipproto = ext->ip4->ip_p; 1075 1076 break; 1077 #ifdef INET6 1078 case ETHERTYPE_IPV6: 1079 m = m_getptr(mp, hlen, &hoff); 1080 if (m == NULL || m->m_len - hoff < sizeof(*ext->ip6)) 1081 return; 1082 ext->ip6 = (struct ip6_hdr *)(mtod(m, caddr_t) + hoff); 1083 1084 hlen = sizeof(*ext->ip6); 1085 ipproto = ext->ip6->ip6_nxt; 1086 1087 break; 1088 #endif 1089 default: 1090 return; 1091 } 1092 1093 switch (ipproto) { 1094 case IPPROTO_TCP: 1095 m = m_getptr(m, hoff + hlen, &hoff); 1096 if (m == NULL || m->m_len - hoff < sizeof(*ext->tcp)) 1097 return; 1098 ext->tcp = (struct tcphdr *)(mtod(m, caddr_t) + hoff); 1099 break; 1100 1101 case IPPROTO_UDP: 1102 m = m_getptr(m, hoff + hlen, &hoff); 1103 if (m == NULL || m->m_len - hoff < sizeof(*ext->udp)) 1104 return; 1105 ext->udp = (struct udphdr *)(mtod(m, caddr_t) + hoff); 1106 break; 1107 } 1108 } 1109