1 /* $OpenBSD: if_ethersubr.c,v 1.288 2023/04/05 23:01:03 kn 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 for (enm = LIST_FIRST(&ac->ac_multiaddrs); 713 enm != NULL; 714 enm = LIST_FIRST(&ac->ac_multiaddrs)) { 715 LIST_REMOVE(enm, enm_list); 716 free(enm, M_IFMADDR, sizeof *enm); 717 } 718 } 719 720 #if 0 721 /* 722 * This is for reference. We have table-driven versions of the 723 * crc32 generators, which are faster than the double-loop. 724 */ 725 u_int32_t __pure 726 ether_crc32_le_update(u_int_32_t crc, const u_int8_t *buf, size_t len) 727 { 728 u_int32_t c, carry; 729 size_t i, j; 730 731 for (i = 0; i < len; i++) { 732 c = buf[i]; 733 for (j = 0; j < 8; j++) { 734 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 735 crc >>= 1; 736 c >>= 1; 737 if (carry) 738 crc = (crc ^ ETHER_CRC_POLY_LE); 739 } 740 } 741 742 return (crc); 743 } 744 745 u_int32_t __pure 746 ether_crc32_be_update(u_int_32_t crc, const u_int8_t *buf, size_t len) 747 { 748 u_int32_t c, carry; 749 size_t i, j; 750 751 for (i = 0; i < len; i++) { 752 c = buf[i]; 753 for (j = 0; j < 8; j++) { 754 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 755 crc <<= 1; 756 c >>= 1; 757 if (carry) 758 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 759 } 760 } 761 762 return (crc); 763 } 764 #else 765 u_int32_t __pure 766 ether_crc32_le_update(u_int32_t crc, const u_int8_t *buf, size_t len) 767 { 768 static const u_int32_t crctab[] = { 769 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 770 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 771 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 772 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 773 }; 774 size_t i; 775 776 for (i = 0; i < len; i++) { 777 crc ^= buf[i]; 778 crc = (crc >> 4) ^ crctab[crc & 0xf]; 779 crc = (crc >> 4) ^ crctab[crc & 0xf]; 780 } 781 782 return (crc); 783 } 784 785 u_int32_t __pure 786 ether_crc32_be_update(u_int32_t crc, const u_int8_t *buf, size_t len) 787 { 788 static const u_int8_t rev[] = { 789 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, 790 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf 791 }; 792 static const u_int32_t crctab[] = { 793 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 794 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 795 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 796 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd 797 }; 798 size_t i; 799 u_int8_t data; 800 801 for (i = 0; i < len; i++) { 802 data = buf[i]; 803 crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data & 0xf]]; 804 crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data >> 4]]; 805 } 806 807 return (crc); 808 } 809 #endif 810 811 u_int32_t 812 ether_crc32_le(const u_int8_t *buf, size_t len) 813 { 814 return ether_crc32_le_update(0xffffffff, buf, len); 815 } 816 817 u_int32_t 818 ether_crc32_be(const u_int8_t *buf, size_t len) 819 { 820 return ether_crc32_be_update(0xffffffff, buf, len); 821 } 822 823 u_char ether_ipmulticast_min[ETHER_ADDR_LEN] = 824 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 825 u_char ether_ipmulticast_max[ETHER_ADDR_LEN] = 826 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 827 828 #ifdef INET6 829 u_char ether_ip6multicast_min[ETHER_ADDR_LEN] = 830 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 831 u_char ether_ip6multicast_max[ETHER_ADDR_LEN] = 832 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff }; 833 #endif 834 835 /* 836 * Convert a sockaddr into an Ethernet address or range of Ethernet 837 * addresses. 838 */ 839 int 840 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN], 841 u_int8_t addrhi[ETHER_ADDR_LEN]) 842 { 843 struct sockaddr_in *sin; 844 #ifdef INET6 845 struct sockaddr_in6 *sin6; 846 #endif /* INET6 */ 847 848 switch (sa->sa_family) { 849 850 case AF_UNSPEC: 851 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN); 852 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 853 break; 854 855 case AF_INET: 856 sin = satosin(sa); 857 if (sin->sin_addr.s_addr == INADDR_ANY) { 858 /* 859 * An IP address of INADDR_ANY means listen to 860 * or stop listening to all of the Ethernet 861 * multicast addresses used for IP. 862 * (This is for the sake of IP multicast routers.) 863 */ 864 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN); 865 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN); 866 } else { 867 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 868 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 869 } 870 break; 871 #ifdef INET6 872 case AF_INET6: 873 sin6 = satosin6(sa); 874 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 875 /* 876 * An IP6 address of 0 means listen to or stop 877 * listening to all of the Ethernet multicast 878 * address used for IP6. 879 * 880 * (This might not be healthy, given IPv6's reliance on 881 * multicast for things like neighbor discovery. 882 * Perhaps initializing all-nodes, solicited nodes, and 883 * possibly all-routers for this interface afterwards 884 * is not a bad idea.) 885 */ 886 887 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN); 888 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN); 889 } else { 890 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 891 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 892 } 893 break; 894 #endif 895 896 default: 897 return (EAFNOSUPPORT); 898 } 899 return (0); 900 } 901 902 /* 903 * Add an Ethernet multicast address or range of addresses to the list for a 904 * given interface. 905 */ 906 int 907 ether_addmulti(struct ifreq *ifr, struct arpcom *ac) 908 { 909 struct ether_multi *enm; 910 u_char addrlo[ETHER_ADDR_LEN]; 911 u_char addrhi[ETHER_ADDR_LEN]; 912 int s = splnet(), error; 913 914 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 915 if (error != 0) { 916 splx(s); 917 return (error); 918 } 919 920 /* 921 * Verify that we have valid Ethernet multicast addresses. 922 */ 923 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) { 924 splx(s); 925 return (EINVAL); 926 } 927 /* 928 * See if the address range is already in the list. 929 */ 930 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 931 if (enm != NULL) { 932 /* 933 * Found it; just increment the reference count. 934 */ 935 ++enm->enm_refcount; 936 splx(s); 937 return (0); 938 } 939 /* 940 * New address or range; malloc a new multicast record 941 * and link it into the interface's multicast list. 942 */ 943 enm = malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); 944 if (enm == NULL) { 945 splx(s); 946 return (ENOBUFS); 947 } 948 memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN); 949 memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN); 950 enm->enm_refcount = 1; 951 LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list); 952 ac->ac_multicnt++; 953 if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) 954 ac->ac_multirangecnt++; 955 splx(s); 956 /* 957 * Return ENETRESET to inform the driver that the list has changed 958 * and its reception filter should be adjusted accordingly. 959 */ 960 return (ENETRESET); 961 } 962 963 /* 964 * Delete a multicast address record. 965 */ 966 int 967 ether_delmulti(struct ifreq *ifr, struct arpcom *ac) 968 { 969 struct ether_multi *enm; 970 u_char addrlo[ETHER_ADDR_LEN]; 971 u_char addrhi[ETHER_ADDR_LEN]; 972 int s = splnet(), error; 973 974 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 975 if (error != 0) { 976 splx(s); 977 return (error); 978 } 979 980 /* 981 * Look up the address in our list. 982 */ 983 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 984 if (enm == NULL) { 985 splx(s); 986 return (ENXIO); 987 } 988 if (--enm->enm_refcount != 0) { 989 /* 990 * Still some claims to this record. 991 */ 992 splx(s); 993 return (0); 994 } 995 /* 996 * No remaining claims to this record; unlink and free it. 997 */ 998 LIST_REMOVE(enm, enm_list); 999 free(enm, M_IFMADDR, sizeof *enm); 1000 ac->ac_multicnt--; 1001 if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) 1002 ac->ac_multirangecnt--; 1003 splx(s); 1004 /* 1005 * Return ENETRESET to inform the driver that the list has changed 1006 * and its reception filter should be adjusted accordingly. 1007 */ 1008 return (ENETRESET); 1009 } 1010 1011 uint64_t 1012 ether_addr_to_e64(const struct ether_addr *ea) 1013 { 1014 uint64_t e64 = 0; 1015 size_t i; 1016 1017 for (i = 0; i < nitems(ea->ether_addr_octet); i++) { 1018 e64 <<= 8; 1019 e64 |= ea->ether_addr_octet[i]; 1020 } 1021 1022 return (e64); 1023 } 1024 1025 void 1026 ether_e64_to_addr(struct ether_addr *ea, uint64_t e64) 1027 { 1028 size_t i = nitems(ea->ether_addr_octet); 1029 1030 do { 1031 ea->ether_addr_octet[--i] = e64; 1032 e64 >>= 8; 1033 } while (i > 0); 1034 } 1035 1036 /* Parse different TCP/IP protocol headers for a quick view inside an mbuf. */ 1037 void 1038 ether_extract_headers(struct mbuf *mp, struct ether_extracted *ext) 1039 { 1040 struct mbuf *m; 1041 uint64_t hlen; 1042 int hoff; 1043 uint8_t ipproto; 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 switch (ntohs(ext->eh->ether_type)) { 1053 case ETHERTYPE_IP: 1054 m = m_getptr(mp, sizeof(*ext->eh), &hoff); 1055 if (m == NULL || m->m_len - hoff < sizeof(*ext->ip4)) 1056 return; 1057 ext->ip4 = (struct ip *)(mtod(m, caddr_t) + hoff); 1058 1059 if (ISSET(ntohs(ext->ip4->ip_off), IP_MF|IP_OFFMASK)) 1060 return; 1061 1062 hlen = ext->ip4->ip_hl << 2; 1063 ipproto = ext->ip4->ip_p; 1064 1065 break; 1066 #ifdef INET6 1067 case ETHERTYPE_IPV6: 1068 m = m_getptr(mp, sizeof(*ext->eh), &hoff); 1069 if (m == NULL || m->m_len - hoff < sizeof(*ext->ip6)) 1070 return; 1071 ext->ip6 = (struct ip6_hdr *)(mtod(m, caddr_t) + hoff); 1072 1073 hlen = sizeof(*ext->ip6); 1074 ipproto = ext->ip6->ip6_nxt; 1075 1076 break; 1077 #endif 1078 default: 1079 return; 1080 } 1081 1082 switch (ipproto) { 1083 case IPPROTO_TCP: 1084 m = m_getptr(m, hoff + hlen, &hoff); 1085 if (m == NULL || m->m_len - hoff < sizeof(*ext->tcp)) 1086 return; 1087 ext->tcp = (struct tcphdr *)(mtod(m, caddr_t) + hoff); 1088 break; 1089 1090 case IPPROTO_UDP: 1091 m = m_getptr(m, hoff + hlen, &hoff); 1092 if (m == NULL || m->m_len - hoff < sizeof(*ext->udp)) 1093 return; 1094 ext->udp = (struct udphdr *)(mtod(m, caddr_t) + hoff); 1095 break; 1096 } 1097 } 1098