1 /* $NetBSD: if_ethersubr.c,v 1.332 2025/10/04 04:44:19 thorpej Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1989, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.332 2025/10/04 04:44:19 thorpej Exp $"); 65 66 #ifdef _KERNEL_OPT 67 #include "opt_inet.h" 68 #include "opt_atalk.h" 69 #include "opt_mbuftrace.h" 70 #include "opt_mpls.h" 71 #include "opt_gateway.h" 72 #include "opt_pppoe.h" 73 #include "opt_net_mpsafe.h" 74 #endif 75 76 #include "vlan.h" 77 #include "pppoe.h" 78 #include "bridge.h" 79 #include "arp.h" 80 #include "agr.h" 81 82 #include <sys/sysctl.h> 83 #include <sys/mbuf.h> 84 #include <sys/mutex.h> 85 #include <sys/ioctl.h> 86 #include <sys/errno.h> 87 #include <sys/device.h> 88 #include <sys/entropy.h> 89 #include <sys/rndsource.h> 90 #include <sys/cpu.h> 91 #include <sys/kmem.h> 92 #include <sys/hook.h> 93 94 #include <net/if.h> 95 #include <net/route.h> 96 #include <net/if_llc.h> 97 #include <net/if_dl.h> 98 #include <net/if_types.h> 99 #include <net/pktqueue.h> 100 101 #include <net/if_media.h> 102 #include <dev/mii/mii.h> 103 #include <dev/mii/miivar.h> 104 105 #if NARP == 0 106 /* 107 * XXX there should really be a way to issue this warning from within config(8) 108 */ 109 #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK. 110 #endif 111 112 #include <net/bpf.h> 113 114 #include <net/if_ether.h> 115 #include <net/if_vlanvar.h> 116 117 #if NPPPOE > 0 118 #include <net/if_pppoe.h> 119 #endif 120 121 #if NAGR > 0 122 #include <net/ether_slowprotocols.h> 123 #include <net/agr/ieee8023ad.h> 124 #include <net/agr/if_agrvar.h> 125 #endif 126 127 #if NBRIDGE > 0 128 #include <net/if_bridgevar.h> 129 #endif 130 131 #include <netinet/in.h> 132 #ifdef INET 133 #include <netinet/in_var.h> 134 #endif 135 #include <netinet/if_inarp.h> 136 137 #ifdef INET6 138 #ifndef INET 139 #include <netinet/in.h> 140 #endif 141 #include <netinet6/in6_var.h> 142 #include <netinet6/nd6.h> 143 #endif 144 145 #include "carp.h" 146 #if NCARP > 0 147 #include <netinet/ip_carp.h> 148 #endif 149 150 #ifdef NETATALK 151 #include <netatalk/at.h> 152 #include <netatalk/at_var.h> 153 #include <netatalk/at_extern.h> 154 155 #define llc_snap_org_code llc_un.type_snap.org_code 156 #define llc_snap_ether_type llc_un.type_snap.ether_type 157 158 extern u_char at_org_code[3]; 159 extern u_char aarp_org_code[3]; 160 #endif /* NETATALK */ 161 162 #ifdef MPLS 163 #include <netmpls/mpls.h> 164 #include <netmpls/mpls_var.h> 165 #endif 166 167 CTASSERT(sizeof(struct ether_addr) == 6); 168 CTASSERT(sizeof(struct ether_header) == 14); 169 170 #ifdef DIAGNOSTIC 171 static struct timeval bigpktppslim_last; 172 static int bigpktppslim = 2; /* XXX */ 173 static int bigpktpps_count; 174 static kmutex_t bigpktpps_lock __cacheline_aligned; 175 #endif 176 177 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = 178 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 179 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] = 180 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 }; 181 #define senderr(e) { error = (e); goto bad;} 182 183 static pktq_rps_hash_func_t ether_pktq_rps_hash_p; 184 185 /* 186 * Attempt to get the Ethernet address from device properties. 187 * 188 * We generally follow the Device Tree specification with regard 189 * to the property names, but that's OK because the property names 190 * are pretty generic. 191 */ 192 bool 193 ether_getaddr(device_t dev, uint8_t enaddr[ETHER_ADDR_LEN]) 194 { 195 /* 196 * Check first for the "mac-address" property. The bindings 197 * say that this would be used only if it is different then the 198 * "local-mac-address" property. But if it's the same, then 199 * using it is exactly the same as using "local-mac-address". So, 200 * we first look for "mac-address", and if that's not there, then 201 * we look for "local-mac-address". 202 */ 203 if (device_getprop_data(dev, "mac-address", enaddr, 204 ETHER_ADDR_LEN) == ETHER_ADDR_LEN || 205 device_getprop_data(dev, "local-mac-address", enaddr, 206 ETHER_ADDR_LEN) == ETHER_ADDR_LEN) { 207 return true; 208 } 209 return false; 210 } 211 212 /* 213 * Ethernet output routine. 214 * Encapsulate a packet of type family for the local net. 215 * Assumes that ifp is actually pointer to ethercom structure. 216 */ 217 static int 218 ether_output(struct ifnet * const ifp0, struct mbuf * const m0, 219 const struct sockaddr * const dst, const struct rtentry *rt) 220 { 221 uint8_t esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN]; 222 uint16_t etype = 0; 223 int error = 0, hdrcmplt = 0; 224 struct mbuf *m = m0; 225 struct mbuf *mcopy = NULL; 226 struct ether_header *eh; 227 struct ifnet *ifp = ifp0; 228 #ifdef INET 229 struct arphdr *ah; 230 #endif 231 #ifdef NETATALK 232 struct at_ifaddr *aa; 233 #endif 234 235 #ifdef MBUFTRACE 236 m_claimm(m, ifp->if_mowner); 237 #endif 238 239 #if NCARP > 0 240 if (ifp->if_type == IFT_CARP) { 241 struct ifaddr *ifa; 242 int s = pserialize_read_enter(); 243 244 /* loop back if this is going to the carp interface */ 245 if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP && 246 (ifa = ifa_ifwithaddr(dst)) != NULL) { 247 if (ifa->ifa_ifp == ifp0) { 248 pserialize_read_exit(s); 249 return looutput(ifp0, m, dst, rt); 250 } 251 } 252 pserialize_read_exit(s); 253 254 ifp = ifp->if_carpdev; 255 /* ac = (struct arpcom *)ifp; */ 256 257 if ((ifp0->if_flags & (IFF_UP | IFF_RUNNING)) != 258 (IFF_UP | IFF_RUNNING)) 259 senderr(ENETDOWN); 260 } 261 #endif 262 263 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) 264 senderr(ENETDOWN); 265 266 switch (dst->sa_family) { 267 268 #ifdef INET 269 case AF_INET: 270 if (m->m_flags & M_BCAST) { 271 memcpy(edst, etherbroadcastaddr, sizeof(edst)); 272 } else if (m->m_flags & M_MCAST) { 273 ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst); 274 } else { 275 error = arpresolve(ifp0, rt, m, dst, edst, sizeof(edst)); 276 if (error) 277 return (error == EWOULDBLOCK) ? 0 : error; 278 } 279 /* If broadcasting on a simplex interface, loopback a copy */ 280 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 281 mcopy = m_copypacket(m, M_DONTWAIT); 282 etype = htons(ETHERTYPE_IP); 283 break; 284 285 case AF_ARP: 286 ah = mtod(m, struct arphdr *); 287 if (m->m_flags & M_BCAST) { 288 memcpy(edst, etherbroadcastaddr, sizeof(edst)); 289 } else { 290 void *tha = ar_tha(ah); 291 292 if (tha == NULL) { 293 /* fake with ARPHRD_IEEE1394 */ 294 m_freem(m); 295 return 0; 296 } 297 memcpy(edst, tha, sizeof(edst)); 298 } 299 300 ah->ar_hrd = htons(ARPHRD_ETHER); 301 302 switch (ntohs(ah->ar_op)) { 303 case ARPOP_REVREQUEST: 304 case ARPOP_REVREPLY: 305 etype = htons(ETHERTYPE_REVARP); 306 break; 307 308 case ARPOP_REQUEST: 309 case ARPOP_REPLY: 310 default: 311 etype = htons(ETHERTYPE_ARP); 312 } 313 break; 314 #endif 315 316 #ifdef INET6 317 case AF_INET6: 318 if (m->m_flags & M_BCAST) { 319 memcpy(edst, etherbroadcastaddr, sizeof(edst)); 320 } else if (m->m_flags & M_MCAST) { 321 ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr, 322 edst); 323 } else { 324 error = nd6_resolve(ifp0, rt, m, dst, edst, 325 sizeof(edst)); 326 if (error) 327 return (error == EWOULDBLOCK) ? 0 : error; 328 } 329 etype = htons(ETHERTYPE_IPV6); 330 break; 331 #endif 332 333 #ifdef NETATALK 334 case AF_APPLETALK: { 335 struct ifaddr *ifa; 336 int s; 337 338 KERNEL_LOCK(1, NULL); 339 340 if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) { 341 KERNEL_UNLOCK_ONE(NULL); 342 return 0; 343 } 344 345 /* 346 * ifaddr is the first thing in at_ifaddr 347 */ 348 s = pserialize_read_enter(); 349 ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp); 350 if (ifa == NULL) { 351 pserialize_read_exit(s); 352 KERNEL_UNLOCK_ONE(NULL); 353 senderr(EADDRNOTAVAIL); 354 } 355 aa = (struct at_ifaddr *)ifa; 356 357 /* 358 * In the phase 2 case, we need to prepend an mbuf for the 359 * llc header. 360 */ 361 if (aa->aa_flags & AFA_PHASE2) { 362 struct llc llc; 363 364 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT); 365 if (m == NULL) { 366 pserialize_read_exit(s); 367 KERNEL_UNLOCK_ONE(NULL); 368 senderr(ENOBUFS); 369 } 370 371 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 372 llc.llc_control = LLC_UI; 373 memcpy(llc.llc_snap_org_code, at_org_code, 374 sizeof(llc.llc_snap_org_code)); 375 llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK); 376 memcpy(mtod(m, void *), &llc, sizeof(struct llc)); 377 } else { 378 etype = htons(ETHERTYPE_ATALK); 379 } 380 pserialize_read_exit(s); 381 KERNEL_UNLOCK_ONE(NULL); 382 break; 383 } 384 #endif /* NETATALK */ 385 386 case pseudo_AF_HDRCMPLT: 387 hdrcmplt = 1; 388 memcpy(esrc, 389 ((const struct ether_header *)dst->sa_data)->ether_shost, 390 sizeof(esrc)); 391 /* FALLTHROUGH */ 392 393 case AF_UNSPEC: 394 memcpy(edst, 395 ((const struct ether_header *)dst->sa_data)->ether_dhost, 396 sizeof(edst)); 397 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */ 398 etype = ((const struct ether_header *)dst->sa_data)->ether_type; 399 break; 400 401 default: 402 rt_unhandled(__func__, ifp, dst); 403 senderr(EAFNOSUPPORT); 404 } 405 406 #ifdef MPLS 407 { 408 struct m_tag *mtag; 409 mtag = m_tag_find(m, PACKET_TAG_MPLS); 410 if (mtag != NULL) { 411 /* Having the tag itself indicates it's MPLS */ 412 etype = htons(ETHERTYPE_MPLS); 413 m_tag_delete(m, mtag); 414 } 415 } 416 #endif 417 418 if (mcopy) 419 (void)looutput(ifp, mcopy, dst, rt); 420 421 KASSERT((m->m_flags & M_PKTHDR) != 0); 422 423 /* 424 * If no ether type is set, this must be a 802.2 formatted packet. 425 */ 426 if (etype == 0) 427 etype = htons(m->m_pkthdr.len); 428 429 /* 430 * Add local net header. If no space in first mbuf, allocate another. 431 */ 432 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT); 433 if (m == NULL) 434 senderr(ENOBUFS); 435 436 eh = mtod(m, struct ether_header *); 437 /* Note: etype is already in network byte order. */ 438 memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type)); 439 memcpy(eh->ether_dhost, edst, sizeof(edst)); 440 if (hdrcmplt) { 441 memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost)); 442 } else { 443 memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl), 444 sizeof(eh->ether_shost)); 445 } 446 447 #if NCARP > 0 448 if (ifp0 != ifp && ifp0->if_type == IFT_CARP) { 449 /* update with virtual MAC */ 450 memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl), 451 sizeof(eh->ether_shost)); 452 } 453 #endif 454 455 if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0) 456 return error; 457 if (m == NULL) 458 return 0; 459 460 #if NBRIDGE > 0 461 /* 462 * Bridges require special output handling. 463 */ 464 if (ifp->if_bridge) 465 return bridge_output(ifp, m, NULL, NULL); 466 #endif 467 468 #if NCARP > 0 469 if (ifp != ifp0) 470 if_statadd(ifp0, if_obytes, m->m_pkthdr.len + ETHER_HDR_LEN); 471 #endif 472 473 #ifdef ALTQ 474 KERNEL_LOCK(1, NULL); 475 /* 476 * If ALTQ is enabled on the parent interface, do 477 * classification; the queueing discipline might not 478 * require classification, but might require the 479 * address family/header pointer in the pktattr. 480 */ 481 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 482 altq_etherclassify(&ifp->if_snd, m); 483 KERNEL_UNLOCK_ONE(NULL); 484 #endif 485 return ifq_enqueue(ifp, m); 486 487 bad: 488 if_statinc(ifp, if_oerrors); 489 m_freem(m); 490 return error; 491 } 492 493 #ifdef ALTQ 494 /* 495 * This routine is a slight hack to allow a packet to be classified 496 * if the Ethernet headers are present. It will go away when ALTQ's 497 * classification engine understands link headers. 498 * 499 * XXX: We may need to do m_pullups here. First to ensure struct ether_header 500 * is indeed contiguous, then to read the LLC and so on. 501 */ 502 void 503 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m) 504 { 505 struct ether_header *eh; 506 struct mbuf *mtop = m; 507 uint16_t ether_type; 508 int hlen, af, hdrsize; 509 void *hdr; 510 511 KASSERT((mtop->m_flags & M_PKTHDR) != 0); 512 513 hlen = ETHER_HDR_LEN; 514 eh = mtod(m, struct ether_header *); 515 516 ether_type = htons(eh->ether_type); 517 518 if (ether_type < ETHERMTU) { 519 /* LLC/SNAP */ 520 struct llc *llc = (struct llc *)(eh + 1); 521 hlen += 8; 522 523 if (m->m_len < hlen || 524 llc->llc_dsap != LLC_SNAP_LSAP || 525 llc->llc_ssap != LLC_SNAP_LSAP || 526 llc->llc_control != LLC_UI) { 527 /* Not SNAP. */ 528 goto bad; 529 } 530 531 ether_type = htons(llc->llc_un.type_snap.ether_type); 532 } 533 534 switch (ether_type) { 535 case ETHERTYPE_IP: 536 af = AF_INET; 537 hdrsize = 20; /* sizeof(struct ip) */ 538 break; 539 540 case ETHERTYPE_IPV6: 541 af = AF_INET6; 542 hdrsize = 40; /* sizeof(struct ip6_hdr) */ 543 break; 544 545 default: 546 af = AF_UNSPEC; 547 hdrsize = 0; 548 break; 549 } 550 551 while (m->m_len <= hlen) { 552 hlen -= m->m_len; 553 m = m->m_next; 554 if (m == NULL) 555 goto bad; 556 } 557 558 if (m->m_len < (hlen + hdrsize)) { 559 /* 560 * protocol header not in a single mbuf. 561 * We can't cope with this situation right 562 * now (but it shouldn't ever happen, really, anyhow). 563 */ 564 #ifdef DEBUG 565 printf("altq_etherclassify: headers span multiple mbufs: " 566 "%d < %d\n", m->m_len, (hlen + hdrsize)); 567 #endif 568 goto bad; 569 } 570 571 m->m_data += hlen; 572 m->m_len -= hlen; 573 574 hdr = mtod(m, void *); 575 576 if (ALTQ_NEEDS_CLASSIFY(ifq)) { 577 mtop->m_pkthdr.pattr_class = 578 (*ifq->altq_classify)(ifq->altq_clfier, m, af); 579 } 580 mtop->m_pkthdr.pattr_af = af; 581 mtop->m_pkthdr.pattr_hdr = hdr; 582 583 m->m_data -= hlen; 584 m->m_len += hlen; 585 586 return; 587 588 bad: 589 mtop->m_pkthdr.pattr_class = NULL; 590 mtop->m_pkthdr.pattr_hdr = NULL; 591 mtop->m_pkthdr.pattr_af = AF_UNSPEC; 592 } 593 #endif /* ALTQ */ 594 595 #if defined (LLC) || defined (NETATALK) 596 static void 597 ether_input_llc(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh) 598 { 599 pktqueue_t *pktq = NULL; 600 struct llc *l; 601 602 if (m->m_len < sizeof(*eh) + sizeof(struct llc)) 603 goto error; 604 605 l = (struct llc *)(eh+1); 606 switch (l->llc_dsap) { 607 #ifdef NETATALK 608 case LLC_SNAP_LSAP: 609 switch (l->llc_control) { 610 case LLC_UI: 611 if (l->llc_ssap != LLC_SNAP_LSAP) 612 goto error; 613 614 if (memcmp(&(l->llc_snap_org_code)[0], 615 at_org_code, sizeof(at_org_code)) == 0 && 616 ntohs(l->llc_snap_ether_type) == 617 ETHERTYPE_ATALK) { 618 pktq = at_pktq2; 619 m_adj(m, sizeof(struct ether_header) 620 + sizeof(struct llc)); 621 break; 622 } 623 624 if (memcmp(&(l->llc_snap_org_code)[0], 625 aarp_org_code, 626 sizeof(aarp_org_code)) == 0 && 627 ntohs(l->llc_snap_ether_type) == 628 ETHERTYPE_AARP) { 629 m_adj(m, sizeof(struct ether_header) 630 + sizeof(struct llc)); 631 aarpinput(ifp, m); /* XXX queue? */ 632 return; 633 } 634 635 default: 636 goto error; 637 } 638 break; 639 #endif 640 default: 641 goto noproto; 642 } 643 644 KASSERT(pktq != NULL); 645 if (__predict_false(!pktq_enqueue(pktq, m, 0))) { 646 m_freem(m); 647 } 648 return; 649 650 noproto: 651 m_freem(m); 652 if_statinc(ifp, if_noproto); 653 return; 654 error: 655 m_freem(m); 656 if_statinc(ifp, if_ierrors); 657 return; 658 } 659 #endif /* defined (LLC) || defined (NETATALK) */ 660 661 /* 662 * Process a received Ethernet packet; 663 * the packet is in the mbuf chain m with 664 * the ether header. 665 */ 666 void 667 ether_input(struct ifnet *ifp, struct mbuf *m) 668 { 669 #if NVLAN > 0 || defined(MBUFTRACE) 670 struct ethercom *ec = (struct ethercom *) ifp; 671 #endif 672 pktqueue_t *pktq = NULL; 673 uint16_t etype; 674 struct ether_header *eh; 675 size_t ehlen; 676 static int earlypkts; 677 678 /* No RPS for not-IP. */ 679 pktq_rps_hash_func_t rps_hash = NULL; 680 681 KASSERT(!cpu_intr_p()); 682 KASSERT((m->m_flags & M_PKTHDR) != 0); 683 684 if ((ifp->if_flags & IFF_UP) == 0) 685 goto drop; 686 687 #ifdef MBUFTRACE 688 m_claimm(m, &ec->ec_rx_mowner); 689 #endif 690 691 if (__predict_false(m->m_len < sizeof(*eh))) { 692 if ((m = m_pullup(m, sizeof(*eh))) == NULL) { 693 if_statinc(ifp, if_ierrors); 694 return; 695 } 696 } 697 698 eh = mtod(m, struct ether_header *); 699 etype = ntohs(eh->ether_type); 700 ehlen = sizeof(*eh); 701 702 if (__predict_false(earlypkts < 100 || 703 entropy_epoch() == (unsigned)-1)) { 704 rnd_add_data(NULL, eh, ehlen, 0); 705 earlypkts++; 706 } 707 708 /* 709 * Determine if the packet is within its size limits. For MPLS the 710 * header length is variable, so we skip the check. 711 */ 712 if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len > 713 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) { 714 #ifdef DIAGNOSTIC 715 mutex_enter(&bigpktpps_lock); 716 if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count, 717 bigpktppslim)) { 718 printf("%s: discarding oversize frame (len=%d)\n", 719 ifp->if_xname, m->m_pkthdr.len); 720 } 721 mutex_exit(&bigpktpps_lock); 722 #endif 723 goto error; 724 } 725 726 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 727 /* 728 * If this is not a simplex interface, drop the packet 729 * if it came from us. 730 */ 731 if ((ifp->if_flags & IFF_SIMPLEX) == 0 && 732 memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost, 733 ETHER_ADDR_LEN) == 0) { 734 goto drop; 735 } 736 737 if (memcmp(etherbroadcastaddr, 738 eh->ether_dhost, ETHER_ADDR_LEN) == 0) 739 m->m_flags |= M_BCAST; 740 else 741 m->m_flags |= M_MCAST; 742 if_statinc(ifp, if_imcasts); 743 } 744 745 /* If the CRC is still on the packet, trim it off. */ 746 if (m->m_flags & M_HASFCS) { 747 m_adj(m, -ETHER_CRC_LEN); 748 m->m_flags &= ~M_HASFCS; 749 } 750 751 if_statadd(ifp, if_ibytes, m->m_pkthdr.len); 752 753 if (!vlan_has_tag(m) && etype == ETHERTYPE_VLAN) { 754 m = ether_strip_vlantag(m); 755 if (m == NULL) { 756 if_statinc(ifp, if_ierrors); 757 return; 758 } 759 760 eh = mtod(m, struct ether_header *); 761 etype = ntohs(eh->ether_type); 762 ehlen = sizeof(*eh); 763 } 764 765 if ((m->m_flags & (M_BCAST | M_MCAST | M_PROMISC)) == 0 && 766 (ifp->if_flags & IFF_PROMISC) != 0 && 767 memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost, 768 ETHER_ADDR_LEN) != 0) { 769 m->m_flags |= M_PROMISC; 770 } 771 772 if ((m->m_flags & M_PROMISC) == 0) { 773 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0) 774 return; 775 if (m == NULL) 776 return; 777 778 eh = mtod(m, struct ether_header *); 779 etype = ntohs(eh->ether_type); 780 } 781 782 /* 783 * Processing a logical interfaces that are able 784 * to configure vlan(4). 785 */ 786 #if NAGR > 0 787 if (ifp->if_lagg != NULL && 788 __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) { 789 m->m_flags &= ~M_PROMISC; 790 agr_input(ifp, m); 791 return; 792 } 793 #endif 794 795 /* 796 * VLAN processing. 797 * 798 * VLAN provides service delimiting so the frames are 799 * processed before other handlings. If a VLAN interface 800 * does not exist to take those frames, they're returned 801 * to ether_input(). 802 */ 803 804 if (vlan_has_tag(m)) { 805 if (EVL_VLANOFTAG(vlan_get_tag(m)) == 0) { 806 if (etype == ETHERTYPE_VLAN || 807 etype == ETHERTYPE_QINQ) 808 goto drop; 809 810 /* XXX we should actually use the prio value? */ 811 m->m_flags &= ~M_VLANTAG; 812 } else { 813 #if NVLAN > 0 814 if (ec->ec_nvlans > 0) { 815 m = vlan_input(ifp, m); 816 817 /* vlan_input() called ether_input() recursively */ 818 if (m == NULL) 819 return; 820 } 821 #endif 822 /* drop VLAN frames not for this port. */ 823 goto noproto; 824 } 825 } 826 827 #if NCARP > 0 828 if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) { 829 /* 830 * Clear M_PROMISC, in case the packet comes from a 831 * vlan. 832 */ 833 m->m_flags &= ~M_PROMISC; 834 if (carp_input(m, (uint8_t *)&eh->ether_shost, 835 (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0) 836 return; 837 } 838 #endif 839 840 /* 841 * Handle protocols that expect to have the Ethernet header 842 * (and possibly FCS) intact. 843 */ 844 switch (etype) { 845 #if NPPPOE > 0 846 case ETHERTYPE_PPPOEDISC: 847 pppoedisc_input(ifp, m); 848 return; 849 850 case ETHERTYPE_PPPOE: 851 pppoe_input(ifp, m); 852 return; 853 #endif 854 855 case ETHERTYPE_SLOWPROTOCOLS: { 856 uint8_t subtype; 857 858 if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) 859 goto error; 860 861 m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype); 862 switch (subtype) { 863 #if NAGR > 0 864 case SLOWPROTOCOLS_SUBTYPE_LACP: 865 if (ifp->if_lagg != NULL) { 866 ieee8023ad_lacp_input(ifp, m); 867 return; 868 } 869 break; 870 871 case SLOWPROTOCOLS_SUBTYPE_MARKER: 872 if (ifp->if_lagg != NULL) { 873 ieee8023ad_marker_input(ifp, m); 874 return; 875 } 876 break; 877 #endif 878 879 default: 880 if (subtype == 0 || subtype > 10) { 881 /* illegal value */ 882 goto noproto; 883 } 884 /* unknown subtype */ 885 break; 886 } 887 } 888 /* FALLTHROUGH */ 889 default: 890 if (m->m_flags & M_PROMISC) 891 goto drop; 892 } 893 894 /* If the CRC is still on the packet, trim it off. */ 895 if (m->m_flags & M_HASFCS) { 896 m_adj(m, -ETHER_CRC_LEN); 897 m->m_flags &= ~M_HASFCS; 898 } 899 900 /* etype represents the size of the payload in this case */ 901 if (etype <= ETHERMTU + sizeof(struct ether_header)) { 902 KASSERT(ehlen == sizeof(*eh)); 903 #if defined (LLC) || defined (NETATALK) 904 ether_input_llc(ifp, m, eh); 905 return; 906 #else 907 /* ethertype of 0-1500 is regarded as noproto */ 908 goto noproto; 909 #endif 910 } 911 912 /* For ARP packets, store the source address so that 913 * ARP DAD probes can be validated. */ 914 if (etype == ETHERTYPE_ARP) { 915 struct m_tag *mtag; 916 917 mtag = m_tag_get(PACKET_TAG_ETHERNET_SRC, ETHER_ADDR_LEN, 918 M_NOWAIT); 919 if (mtag != NULL) { 920 memcpy(mtag + 1, &eh->ether_shost, ETHER_ADDR_LEN); 921 m_tag_prepend(m, mtag); 922 } 923 } 924 925 /* Strip off the Ethernet header. */ 926 m_adj(m, ehlen); 927 928 switch (etype) { 929 #ifdef INET 930 case ETHERTYPE_IP: 931 #ifdef GATEWAY 932 if (ipflow_fastforward(m)) 933 return; 934 #endif 935 pktq = ip_pktq; 936 rps_hash = atomic_load_relaxed(ðer_pktq_rps_hash_p); 937 break; 938 939 case ETHERTYPE_ARP: 940 pktq = arp_pktq; 941 break; 942 943 case ETHERTYPE_REVARP: 944 revarpinput(m); /* XXX queue? */ 945 return; 946 #endif 947 948 #ifdef INET6 949 case ETHERTYPE_IPV6: 950 if (__predict_false(!in6_present)) 951 goto noproto; 952 #ifdef GATEWAY 953 if (ip6flow_fastforward(&m)) 954 return; 955 #endif 956 pktq = ip6_pktq; 957 rps_hash = atomic_load_relaxed(ðer_pktq_rps_hash_p); 958 break; 959 #endif 960 961 #ifdef NETATALK 962 case ETHERTYPE_ATALK: 963 pktq = at_pktq1; 964 break; 965 966 case ETHERTYPE_AARP: 967 aarpinput(ifp, m); /* XXX queue? */ 968 return; 969 #endif 970 971 #ifdef MPLS 972 case ETHERTYPE_MPLS: 973 pktq = mpls_pktq; 974 break; 975 #endif 976 977 default: 978 goto noproto; 979 } 980 981 KASSERT(pktq != NULL); 982 const uint32_t h = rps_hash ? pktq_rps_hash(&rps_hash, m) : 0; 983 if (__predict_false(!pktq_enqueue(pktq, m, h))) { 984 m_freem(m); 985 } 986 return; 987 988 drop: 989 m_freem(m); 990 if_statinc(ifp, if_iqdrops); 991 return; 992 noproto: 993 m_freem(m); 994 if_statinc(ifp, if_noproto); 995 return; 996 error: 997 m_freem(m); 998 if_statinc(ifp, if_ierrors); 999 return; 1000 } 1001 1002 static void 1003 ether_bpf_mtap(struct bpf_if *bp, struct mbuf *m, u_int direction) 1004 { 1005 struct ether_vlan_header evl; 1006 struct m_hdr mh, md; 1007 1008 KASSERT(bp != NULL); 1009 1010 if (!vlan_has_tag(m)) { 1011 bpf_mtap3(bp, m, direction); 1012 return; 1013 } 1014 1015 memcpy(&evl, mtod(m, char *), ETHER_HDR_LEN); 1016 evl.evl_proto = evl.evl_encap_proto; 1017 evl.evl_encap_proto = htons(ETHERTYPE_VLAN); 1018 evl.evl_tag = htons(vlan_get_tag(m)); 1019 1020 md.mh_flags = 0; 1021 md.mh_data = m->m_data + ETHER_HDR_LEN; 1022 md.mh_len = m->m_len - ETHER_HDR_LEN; 1023 md.mh_next = m->m_next; 1024 1025 mh.mh_flags = 0; 1026 mh.mh_data = (char *)&evl; 1027 mh.mh_len = sizeof(evl); 1028 mh.mh_next = (struct mbuf *)&md; 1029 1030 bpf_mtap3(bp, (struct mbuf *)&mh, direction); 1031 } 1032 1033 /* 1034 * Convert Ethernet address to printable (loggable) representation. 1035 */ 1036 char * 1037 ether_sprintf(const u_char *ap) 1038 { 1039 static char etherbuf[3 * ETHER_ADDR_LEN]; 1040 return ether_snprintf(etherbuf, sizeof(etherbuf), ap); 1041 } 1042 1043 char * 1044 ether_snprintf(char *buf, size_t len, const u_char *ap) 1045 { 1046 char *cp = buf; 1047 size_t i; 1048 1049 for (i = 0; i < len / 3; i++) { 1050 *cp++ = hexdigits[*ap >> 4]; 1051 *cp++ = hexdigits[*ap++ & 0xf]; 1052 *cp++ = ':'; 1053 } 1054 *--cp = '\0'; 1055 return buf; 1056 } 1057 1058 /* 1059 * Perform common duties while attaching to interface list 1060 */ 1061 void 1062 ether_ifattach(struct ifnet *ifp, const uint8_t *lla) 1063 { 1064 struct ethercom *ec = (struct ethercom *)ifp; 1065 char xnamebuf[HOOKNAMSIZ]; 1066 1067 if (lla != NULL && ETHER_IS_MULTICAST(lla)) 1068 aprint_error("The multicast bit is set in the MAC address. " 1069 "It's wrong.\n"); 1070 1071 ifp->if_type = IFT_ETHER; 1072 ifp->if_hdrlen = ETHER_HDR_LEN; 1073 ifp->if_dlt = DLT_EN10MB; 1074 ifp->if_mtu = ETHERMTU; 1075 ifp->if_output = ether_output; 1076 ifp->_if_input = ether_input; 1077 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) 1078 ifp->if_bpf_mtap = ether_bpf_mtap; 1079 if (ifp->if_baudrate == 0) 1080 ifp->if_baudrate = IF_Mbps(10); /* just a default */ 1081 1082 if (lla != NULL) 1083 if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla)); 1084 1085 LIST_INIT(&ec->ec_multiaddrs); 1086 SIMPLEQ_INIT(&ec->ec_vids); 1087 ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET); 1088 ec->ec_flags = 0; 1089 ifp->if_broadcastaddr = etherbroadcastaddr; 1090 bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header)); 1091 snprintf(xnamebuf, sizeof(xnamebuf), 1092 "%s-ether_ifdetachhooks", ifp->if_xname); 1093 ec->ec_ifdetach_hooks = simplehook_create(IPL_NET, xnamebuf); 1094 #ifdef MBUFTRACE 1095 mowner_init_owner(&ec->ec_tx_mowner, ifp->if_xname, "tx"); 1096 mowner_init_owner(&ec->ec_rx_mowner, ifp->if_xname, "rx"); 1097 MOWNER_ATTACH(&ec->ec_tx_mowner); 1098 MOWNER_ATTACH(&ec->ec_rx_mowner); 1099 ifp->if_mowner = &ec->ec_tx_mowner; 1100 #endif 1101 } 1102 1103 void 1104 ether_ifdetach(struct ifnet *ifp) 1105 { 1106 struct ethercom *ec = (void *) ifp; 1107 struct ether_multi *enm; 1108 1109 IFNET_ASSERT_UNLOCKED(ifp); 1110 /* 1111 * Prevent further calls to ioctl (for example turning off 1112 * promiscuous mode from the bridge code), which eventually can 1113 * call if_init() which can cause panics because the interface 1114 * is in the process of being detached. Return device not configured 1115 * instead. 1116 */ 1117 ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *), 1118 enxio); 1119 1120 simplehook_dohooks(ec->ec_ifdetach_hooks); 1121 KASSERT(!simplehook_has_hooks(ec->ec_ifdetach_hooks)); 1122 simplehook_destroy(ec->ec_ifdetach_hooks); 1123 1124 bpf_detach(ifp); 1125 1126 ETHER_LOCK(ec); 1127 KASSERT(ec->ec_nvlans == 0); 1128 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) { 1129 LIST_REMOVE(enm, enm_list); 1130 kmem_free(enm, sizeof(*enm)); 1131 ec->ec_multicnt--; 1132 } 1133 ETHER_UNLOCK(ec); 1134 1135 mutex_obj_free(ec->ec_lock); 1136 ec->ec_lock = NULL; 1137 1138 ifp->if_mowner = NULL; 1139 MOWNER_DETACH(&ec->ec_rx_mowner); 1140 MOWNER_DETACH(&ec->ec_tx_mowner); 1141 } 1142 1143 void * 1144 ether_ifdetachhook_establish(struct ifnet *ifp, 1145 void (*fn)(void *), void *arg) 1146 { 1147 struct ethercom *ec; 1148 khook_t *hk; 1149 1150 if (ifp->if_type != IFT_ETHER) 1151 return NULL; 1152 1153 ec = (struct ethercom *)ifp; 1154 hk = simplehook_establish(ec->ec_ifdetach_hooks, 1155 fn, arg); 1156 1157 return (void *)hk; 1158 } 1159 1160 void 1161 ether_ifdetachhook_disestablish(struct ifnet *ifp, 1162 void *vhook, kmutex_t *lock) 1163 { 1164 struct ethercom *ec; 1165 1166 if (vhook == NULL) 1167 return; 1168 1169 ec = (struct ethercom *)ifp; 1170 simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock); 1171 } 1172 1173 #if 0 1174 /* 1175 * This is for reference. We have a table-driven version 1176 * of the little-endian crc32 generator, which is faster 1177 * than the double-loop. 1178 */ 1179 uint32_t 1180 ether_crc32_le(const uint8_t *buf, size_t len) 1181 { 1182 uint32_t c, crc, carry; 1183 size_t i, j; 1184 1185 crc = 0xffffffffU; /* initial value */ 1186 1187 for (i = 0; i < len; i++) { 1188 c = buf[i]; 1189 for (j = 0; j < 8; j++) { 1190 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 1191 crc >>= 1; 1192 c >>= 1; 1193 if (carry) 1194 crc = (crc ^ ETHER_CRC_POLY_LE); 1195 } 1196 } 1197 1198 return (crc); 1199 } 1200 #else 1201 uint32_t 1202 ether_crc32_le(const uint8_t *buf, size_t len) 1203 { 1204 static const uint32_t crctab[] = { 1205 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 1206 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 1207 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 1208 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 1209 }; 1210 uint32_t crc; 1211 size_t i; 1212 1213 crc = 0xffffffffU; /* initial value */ 1214 1215 for (i = 0; i < len; i++) { 1216 crc ^= buf[i]; 1217 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1218 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1219 } 1220 1221 return (crc); 1222 } 1223 #endif 1224 1225 uint32_t 1226 ether_crc32_be(const uint8_t *buf, size_t len) 1227 { 1228 uint32_t c, crc, carry; 1229 size_t i, j; 1230 1231 crc = 0xffffffffU; /* initial value */ 1232 1233 for (i = 0; i < len; i++) { 1234 c = buf[i]; 1235 for (j = 0; j < 8; j++) { 1236 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 1237 crc <<= 1; 1238 c >>= 1; 1239 if (carry) 1240 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 1241 } 1242 } 1243 1244 return (crc); 1245 } 1246 1247 #ifdef INET 1248 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] = 1249 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 1250 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] = 1251 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 1252 #endif 1253 #ifdef INET6 1254 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] = 1255 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 1256 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] = 1257 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff }; 1258 #endif 1259 1260 /* 1261 * ether_aton implementation, not using a static buffer. 1262 */ 1263 int 1264 ether_aton_r(u_char *dest, size_t len, const char *str) 1265 { 1266 const u_char *cp = (const void *)str; 1267 u_char *ep; 1268 1269 #define atox(c) (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10)) 1270 1271 if (len < ETHER_ADDR_LEN) 1272 return ENOSPC; 1273 1274 ep = dest + ETHER_ADDR_LEN; 1275 1276 while (*cp) { 1277 if (!isxdigit(*cp)) 1278 return EINVAL; 1279 1280 *dest = atox(*cp); 1281 cp++; 1282 if (isxdigit(*cp)) { 1283 *dest = (*dest << 4) | atox(*cp); 1284 cp++; 1285 } 1286 dest++; 1287 1288 if (dest == ep) 1289 return (*cp == '\0') ? 0 : ENAMETOOLONG; 1290 1291 switch (*cp) { 1292 case ':': 1293 case '-': 1294 case '.': 1295 cp++; 1296 break; 1297 } 1298 } 1299 return ENOBUFS; 1300 } 1301 1302 /* 1303 * Convert a sockaddr into an Ethernet address or range of Ethernet 1304 * addresses. 1305 */ 1306 int 1307 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN], 1308 uint8_t addrhi[ETHER_ADDR_LEN]) 1309 { 1310 #ifdef INET 1311 const struct sockaddr_in *sin; 1312 #endif 1313 #ifdef INET6 1314 const struct sockaddr_in6 *sin6; 1315 #endif 1316 1317 switch (sa->sa_family) { 1318 1319 case AF_UNSPEC: 1320 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN); 1321 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 1322 break; 1323 1324 #ifdef INET 1325 case AF_INET: 1326 sin = satocsin(sa); 1327 if (sin->sin_addr.s_addr == INADDR_ANY) { 1328 /* 1329 * An IP address of INADDR_ANY means listen to 1330 * or stop listening to all of the Ethernet 1331 * multicast addresses used for IP. 1332 * (This is for the sake of IP multicast routers.) 1333 */ 1334 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN); 1335 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN); 1336 } else { 1337 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 1338 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 1339 } 1340 break; 1341 #endif 1342 #ifdef INET6 1343 case AF_INET6: 1344 sin6 = satocsin6(sa); 1345 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1346 /* 1347 * An IP6 address of 0 means listen to or stop 1348 * listening to all of the Ethernet multicast 1349 * address used for IP6. 1350 * (This is used for multicast routers.) 1351 */ 1352 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN); 1353 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN); 1354 } else { 1355 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 1356 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 1357 } 1358 break; 1359 #endif 1360 1361 default: 1362 return EAFNOSUPPORT; 1363 } 1364 return 0; 1365 } 1366 1367 /* 1368 * Add an Ethernet multicast address or range of addresses to the list for a 1369 * given interface. 1370 */ 1371 int 1372 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec) 1373 { 1374 struct ether_multi *enm, *_enm; 1375 u_char addrlo[ETHER_ADDR_LEN]; 1376 u_char addrhi[ETHER_ADDR_LEN]; 1377 int error = 0; 1378 1379 /* Allocate out of lock */ 1380 enm = kmem_alloc(sizeof(*enm), KM_SLEEP); 1381 1382 ETHER_LOCK(ec); 1383 error = ether_multiaddr(sa, addrlo, addrhi); 1384 if (error != 0) 1385 goto out; 1386 1387 /* 1388 * Verify that we have valid Ethernet multicast addresses. 1389 */ 1390 if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) { 1391 error = EINVAL; 1392 goto out; 1393 } 1394 1395 /* 1396 * See if the address range is already in the list. 1397 */ 1398 _enm = ether_lookup_multi(addrlo, addrhi, ec); 1399 if (_enm != NULL) { 1400 /* 1401 * Found it; just increment the reference count. 1402 */ 1403 ++_enm->enm_refcount; 1404 error = 0; 1405 goto out; 1406 } 1407 1408 /* 1409 * Link a new multicast record into the interface's multicast list. 1410 */ 1411 memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN); 1412 memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN); 1413 enm->enm_refcount = 1; 1414 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list); 1415 ec->ec_multicnt++; 1416 1417 /* 1418 * Return ENETRESET to inform the driver that the list has changed 1419 * and its reception filter should be adjusted accordingly. 1420 */ 1421 error = ENETRESET; 1422 enm = NULL; 1423 1424 out: 1425 ETHER_UNLOCK(ec); 1426 if (enm != NULL) 1427 kmem_free(enm, sizeof(*enm)); 1428 return error; 1429 } 1430 1431 /* 1432 * Delete a multicast address record. 1433 */ 1434 int 1435 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec) 1436 { 1437 struct ether_multi *enm; 1438 u_char addrlo[ETHER_ADDR_LEN]; 1439 u_char addrhi[ETHER_ADDR_LEN]; 1440 int error; 1441 1442 ETHER_LOCK(ec); 1443 error = ether_multiaddr(sa, addrlo, addrhi); 1444 if (error != 0) 1445 goto error; 1446 1447 /* 1448 * Look up the address in our list. 1449 */ 1450 enm = ether_lookup_multi(addrlo, addrhi, ec); 1451 if (enm == NULL) { 1452 error = ENXIO; 1453 goto error; 1454 } 1455 if (--enm->enm_refcount != 0) { 1456 /* 1457 * Still some claims to this record. 1458 */ 1459 error = 0; 1460 goto error; 1461 } 1462 1463 /* 1464 * No remaining claims to this record; unlink and free it. 1465 */ 1466 LIST_REMOVE(enm, enm_list); 1467 ec->ec_multicnt--; 1468 ETHER_UNLOCK(ec); 1469 kmem_free(enm, sizeof(*enm)); 1470 1471 /* 1472 * Return ENETRESET to inform the driver that the list has changed 1473 * and its reception filter should be adjusted accordingly. 1474 */ 1475 return ENETRESET; 1476 1477 error: 1478 ETHER_UNLOCK(ec); 1479 return error; 1480 } 1481 1482 void 1483 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb) 1484 { 1485 ec->ec_ifflags_cb = cb; 1486 } 1487 1488 void 1489 ether_set_vlan_cb(struct ethercom *ec, ether_vlancb_t cb) 1490 { 1491 1492 ec->ec_vlan_cb = cb; 1493 } 1494 1495 static int 1496 ether_ioctl_reinit(struct ethercom *ec) 1497 { 1498 struct ifnet *ifp = &ec->ec_if; 1499 int error; 1500 1501 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1502 1503 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { 1504 case IFF_RUNNING: 1505 /* 1506 * If interface is marked down and it is running, 1507 * then stop and disable it. 1508 */ 1509 if_stop(ifp, 1); 1510 break; 1511 case IFF_UP: 1512 /* 1513 * If interface is marked up and it is stopped, then 1514 * start it. 1515 */ 1516 return if_init(ifp); 1517 case IFF_UP | IFF_RUNNING: 1518 error = 0; 1519 if (ec->ec_ifflags_cb != NULL) { 1520 error = (*ec->ec_ifflags_cb)(ec); 1521 if (error == ENETRESET) { 1522 /* 1523 * Reset the interface to pick up 1524 * changes in any other flags that 1525 * affect the hardware state. 1526 */ 1527 return if_init(ifp); 1528 } 1529 } else 1530 error = if_init(ifp); 1531 return error; 1532 case 0: 1533 break; 1534 } 1535 1536 return 0; 1537 } 1538 1539 /* 1540 * Common ioctls for Ethernet interfaces. Note, we must be 1541 * called at splnet(). 1542 */ 1543 int 1544 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1545 { 1546 struct ethercom *ec = (void *)ifp; 1547 struct eccapreq *eccr; 1548 struct ifreq *ifr = (struct ifreq *)data; 1549 struct if_laddrreq *iflr = data; 1550 const struct sockaddr_dl *sdl; 1551 static const uint8_t zero[ETHER_ADDR_LEN]; 1552 int error; 1553 1554 switch (cmd) { 1555 case SIOCINITIFADDR: 1556 { 1557 struct ifaddr *ifa = (struct ifaddr *)data; 1558 if (ifa->ifa_addr->sa_family != AF_LINK 1559 && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) != 1560 (IFF_UP | IFF_RUNNING)) { 1561 ifp->if_flags |= IFF_UP; 1562 if ((error = if_init(ifp)) != 0) 1563 return error; 1564 } 1565 #ifdef INET 1566 if (ifa->ifa_addr->sa_family == AF_INET) 1567 arp_ifinit(ifp, ifa); 1568 #endif 1569 return 0; 1570 } 1571 1572 case SIOCSIFMTU: 1573 { 1574 int maxmtu; 1575 1576 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU) 1577 maxmtu = ETHERMTU_JUMBO; 1578 else 1579 maxmtu = ETHERMTU; 1580 1581 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu) 1582 return EINVAL; 1583 else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET) 1584 return error; 1585 else if (ifp->if_flags & IFF_UP) { 1586 /* Make sure the device notices the MTU change. */ 1587 return if_init(ifp); 1588 } else 1589 return 0; 1590 } 1591 1592 case SIOCSIFFLAGS: 1593 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 1594 return error; 1595 return ether_ioctl_reinit(ec); 1596 case SIOCGIFFLAGS: 1597 error = ifioctl_common(ifp, cmd, data); 1598 if (error == 0) { 1599 /* Set IFF_ALLMULTI for backcompat */ 1600 ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ? 1601 IFF_ALLMULTI : 0; 1602 } 1603 return error; 1604 case SIOCGETHERCAP: 1605 eccr = (struct eccapreq *)data; 1606 eccr->eccr_capabilities = ec->ec_capabilities; 1607 eccr->eccr_capenable = ec->ec_capenable; 1608 return 0; 1609 case SIOCSETHERCAP: 1610 eccr = (struct eccapreq *)data; 1611 if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0) 1612 return EINVAL; 1613 if (eccr->eccr_capenable == ec->ec_capenable) 1614 return 0; 1615 #if 0 /* notyet */ 1616 ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE) 1617 | (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE); 1618 #else 1619 ec->ec_capenable = eccr->eccr_capenable; 1620 #endif 1621 return ether_ioctl_reinit(ec); 1622 case SIOCADDMULTI: 1623 return ether_addmulti(ifreq_getaddr(cmd, ifr), ec); 1624 case SIOCDELMULTI: 1625 return ether_delmulti(ifreq_getaddr(cmd, ifr), ec); 1626 case SIOCSIFMEDIA: 1627 case SIOCGIFMEDIA: 1628 if (ec->ec_mii != NULL) 1629 return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, 1630 cmd); 1631 else if (ec->ec_ifmedia != NULL) 1632 return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd); 1633 else 1634 return ENOTTY; 1635 break; 1636 case SIOCALIFADDR: 1637 sdl = satocsdl(sstocsa(&iflr->addr)); 1638 if (sdl->sdl_family != AF_LINK) 1639 ; 1640 else if (ETHER_IS_MULTICAST(CLLADDR(sdl))) 1641 return EINVAL; 1642 else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0) 1643 return EINVAL; 1644 /*FALLTHROUGH*/ 1645 default: 1646 return ifioctl_common(ifp, cmd, data); 1647 } 1648 return 0; 1649 } 1650 1651 /* 1652 * Enable/disable passing VLAN packets if the parent interface supports it. 1653 * Return: 1654 * 0: Ok 1655 * -1: Parent interface does not support vlans 1656 * >0: Error 1657 */ 1658 int 1659 ether_enable_vlan_mtu(struct ifnet *ifp) 1660 { 1661 int error; 1662 struct ethercom *ec = (void *)ifp; 1663 1664 /* Parent does not support VLAN's */ 1665 if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0) 1666 return -1; 1667 1668 /* 1669 * Parent supports the VLAN_MTU capability, 1670 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames; 1671 * enable it. 1672 */ 1673 ec->ec_capenable |= ETHERCAP_VLAN_MTU; 1674 1675 /* Interface is down, defer for later */ 1676 if ((ifp->if_flags & IFF_UP) == 0) 1677 return 0; 1678 1679 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0) 1680 return 0; 1681 1682 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU; 1683 return error; 1684 } 1685 1686 int 1687 ether_disable_vlan_mtu(struct ifnet *ifp) 1688 { 1689 int error; 1690 struct ethercom *ec = (void *)ifp; 1691 1692 /* We still have VLAN's, defer for later */ 1693 if (ec->ec_nvlans != 0) 1694 return 0; 1695 1696 /* Parent does not support VLAB's, nothing to do. */ 1697 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0) 1698 return -1; 1699 1700 /* 1701 * Disable Tx/Rx of VLAN-sized frames. 1702 */ 1703 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU; 1704 1705 /* Interface is down, defer for later */ 1706 if ((ifp->if_flags & IFF_UP) == 0) 1707 return 0; 1708 1709 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0) 1710 return 0; 1711 1712 ec->ec_capenable |= ETHERCAP_VLAN_MTU; 1713 return error; 1714 } 1715 1716 /* 1717 * Add and delete VLAN TAG 1718 */ 1719 int 1720 ether_add_vlantag(struct ifnet *ifp, uint16_t vtag, bool *vlanmtu_status) 1721 { 1722 struct ethercom *ec = (void *)ifp; 1723 struct vlanid_list *vidp; 1724 bool vlanmtu_enabled; 1725 uint16_t vid = EVL_VLANOFTAG(vtag); 1726 int error; 1727 1728 vlanmtu_enabled = false; 1729 1730 /* Add a vid to the list */ 1731 vidp = kmem_alloc(sizeof(*vidp), KM_SLEEP); 1732 vidp->vid = vid; 1733 1734 ETHER_LOCK(ec); 1735 ec->ec_nvlans++; 1736 SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidp, vid_list); 1737 ETHER_UNLOCK(ec); 1738 1739 if (ec->ec_nvlans == 1) { 1740 IFNET_LOCK(ifp); 1741 error = ether_enable_vlan_mtu(ifp); 1742 IFNET_UNLOCK(ifp); 1743 1744 if (error == 0) { 1745 vlanmtu_enabled = true; 1746 } else if (error != -1) { 1747 goto fail; 1748 } 1749 } 1750 1751 if (ec->ec_vlan_cb != NULL) { 1752 error = (*ec->ec_vlan_cb)(ec, vid, true); 1753 if (error != 0) 1754 goto fail; 1755 } 1756 1757 if (vlanmtu_status != NULL) 1758 *vlanmtu_status = vlanmtu_enabled; 1759 1760 return 0; 1761 fail: 1762 ETHER_LOCK(ec); 1763 ec->ec_nvlans--; 1764 SIMPLEQ_REMOVE(&ec->ec_vids, vidp, vlanid_list, vid_list); 1765 ETHER_UNLOCK(ec); 1766 1767 if (vlanmtu_enabled) { 1768 IFNET_LOCK(ifp); 1769 (void)ether_disable_vlan_mtu(ifp); 1770 IFNET_UNLOCK(ifp); 1771 } 1772 1773 kmem_free(vidp, sizeof(*vidp)); 1774 1775 return error; 1776 } 1777 1778 int 1779 ether_del_vlantag(struct ifnet *ifp, uint16_t vtag) 1780 { 1781 struct ethercom *ec = (void *)ifp; 1782 struct vlanid_list *vidp; 1783 uint16_t vid = EVL_VLANOFTAG(vtag); 1784 1785 ETHER_LOCK(ec); 1786 SIMPLEQ_FOREACH(vidp, &ec->ec_vids, vid_list) { 1787 if (vidp->vid == vid) { 1788 SIMPLEQ_REMOVE(&ec->ec_vids, vidp, 1789 vlanid_list, vid_list); 1790 ec->ec_nvlans--; 1791 break; 1792 } 1793 } 1794 ETHER_UNLOCK(ec); 1795 1796 if (vidp == NULL) 1797 return ENOENT; 1798 1799 if (ec->ec_vlan_cb != NULL) { 1800 (void)(*ec->ec_vlan_cb)(ec, vidp->vid, false); 1801 } 1802 1803 if (ec->ec_nvlans == 0) { 1804 IFNET_LOCK(ifp); 1805 (void)ether_disable_vlan_mtu(ifp); 1806 IFNET_UNLOCK(ifp); 1807 } 1808 1809 kmem_free(vidp, sizeof(*vidp)); 1810 1811 return 0; 1812 } 1813 1814 int 1815 ether_inject_vlantag(struct mbuf **mp, uint16_t etype, uint16_t tag) 1816 { 1817 static const size_t min_data_len = 1818 ETHER_MIN_LEN - ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN; 1819 /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */ 1820 static const char vlan_zero_pad_buff[ETHER_MIN_LEN] = { 0 }; 1821 1822 struct ether_vlan_header *evl; 1823 struct mbuf *m = *mp; 1824 int error; 1825 1826 error = 0; 1827 1828 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT); 1829 if (m == NULL) { 1830 error = ENOBUFS; 1831 goto out; 1832 } 1833 1834 if (m->m_len < sizeof(*evl)) { 1835 m = m_pullup(m, sizeof(*evl)); 1836 if (m == NULL) { 1837 error = ENOBUFS; 1838 goto out; 1839 } 1840 } 1841 1842 /* 1843 * Transform the Ethernet header into an 1844 * Ethernet header with 802.1Q encapsulation. 1845 */ 1846 memmove(mtod(m, void *), 1847 mtod(m, char *) + ETHER_VLAN_ENCAP_LEN, 1848 sizeof(struct ether_header)); 1849 evl = mtod(m, struct ether_vlan_header *); 1850 evl->evl_proto = evl->evl_encap_proto; 1851 evl->evl_encap_proto = htons(etype); 1852 evl->evl_tag = htons(tag); 1853 1854 /* 1855 * To cater for VLAN-aware layer 2 ethernet 1856 * switches which may need to strip the tag 1857 * before forwarding the packet, make sure 1858 * the packet+tag is at least 68 bytes long. 1859 * This is necessary because our parent will 1860 * only pad to 64 bytes (ETHER_MIN_LEN) and 1861 * some switches will not pad by themselves 1862 * after deleting a tag. 1863 */ 1864 if (m->m_pkthdr.len < min_data_len) { 1865 m_copyback(m, m->m_pkthdr.len, 1866 min_data_len - m->m_pkthdr.len, 1867 vlan_zero_pad_buff); 1868 } 1869 1870 m->m_flags &= ~M_VLANTAG; 1871 1872 out: 1873 *mp = m; 1874 return error; 1875 } 1876 1877 struct mbuf * 1878 ether_strip_vlantag(struct mbuf *m) 1879 { 1880 struct ether_vlan_header *evl; 1881 1882 if (m->m_len < sizeof(*evl) && 1883 (m = m_pullup(m, sizeof(*evl))) == NULL) { 1884 return NULL; 1885 } 1886 1887 if (m_makewritable(&m, 0, sizeof(*evl), M_DONTWAIT)) { 1888 m_freem(m); 1889 return NULL; 1890 } 1891 1892 evl = mtod(m, struct ether_vlan_header *); 1893 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN); 1894 1895 vlan_set_tag(m, ntohs(evl->evl_tag)); 1896 1897 /* 1898 * Restore the original ethertype. We'll remove 1899 * the encapsulation after we've found the vlan 1900 * interface corresponding to the tag. 1901 */ 1902 evl->evl_encap_proto = evl->evl_proto; 1903 1904 /* 1905 * Remove the encapsulation header and append tag. 1906 * The original header has already been fixed up above. 1907 */ 1908 vlan_set_tag(m, ntohs(evl->evl_tag)); 1909 memmove((char *)evl + ETHER_VLAN_ENCAP_LEN, evl, 1910 offsetof(struct ether_vlan_header, evl_encap_proto)); 1911 m_adj(m, ETHER_VLAN_ENCAP_LEN); 1912 1913 return m; 1914 } 1915 1916 static int 1917 ether_multicast_sysctl(SYSCTLFN_ARGS) 1918 { 1919 struct ether_multi *enm; 1920 struct ifnet *ifp; 1921 struct ethercom *ec; 1922 int error = 0; 1923 size_t written; 1924 struct psref psref; 1925 int bound; 1926 unsigned int multicnt; 1927 struct ether_multi_sysctl *addrs; 1928 int i; 1929 1930 if (namelen != 1) 1931 return EINVAL; 1932 1933 bound = curlwp_bind(); 1934 ifp = if_get_byindex(name[0], &psref); 1935 if (ifp == NULL) { 1936 error = ENODEV; 1937 goto out; 1938 } 1939 if (ifp->if_type != IFT_ETHER) { 1940 if_put(ifp, &psref); 1941 *oldlenp = 0; 1942 goto out; 1943 } 1944 ec = (struct ethercom *)ifp; 1945 1946 if (oldp == NULL) { 1947 if_put(ifp, &psref); 1948 *oldlenp = ec->ec_multicnt * sizeof(*addrs); 1949 goto out; 1950 } 1951 1952 /* 1953 * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which 1954 * is sleepable, while holding it. Copy data to a local buffer first 1955 * with the lock taken and then call sysctl_copyout without holding it. 1956 */ 1957 retry: 1958 multicnt = ec->ec_multicnt; 1959 1960 if (multicnt == 0) { 1961 if_put(ifp, &psref); 1962 *oldlenp = 0; 1963 goto out; 1964 } 1965 1966 addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP); 1967 1968 ETHER_LOCK(ec); 1969 if (multicnt != ec->ec_multicnt) { 1970 /* The number of multicast addresses has changed */ 1971 ETHER_UNLOCK(ec); 1972 kmem_free(addrs, sizeof(*addrs) * multicnt); 1973 goto retry; 1974 } 1975 1976 i = 0; 1977 LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) { 1978 struct ether_multi_sysctl *addr = &addrs[i]; 1979 addr->enm_refcount = enm->enm_refcount; 1980 memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN); 1981 memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN); 1982 i++; 1983 } 1984 ETHER_UNLOCK(ec); 1985 1986 error = 0; 1987 written = 0; 1988 for (i = 0; i < multicnt; i++) { 1989 struct ether_multi_sysctl *addr = &addrs[i]; 1990 1991 if (written + sizeof(*addr) > *oldlenp) 1992 break; 1993 error = sysctl_copyout(l, addr, oldp, sizeof(*addr)); 1994 if (error) 1995 break; 1996 written += sizeof(*addr); 1997 oldp = (char *)oldp + sizeof(*addr); 1998 } 1999 kmem_free(addrs, sizeof(*addrs) * multicnt); 2000 2001 if_put(ifp, &psref); 2002 2003 *oldlenp = written; 2004 out: 2005 curlwp_bindx(bound); 2006 return error; 2007 } 2008 2009 static void 2010 ether_sysctl_setup(struct sysctllog **clog) 2011 { 2012 const struct sysctlnode *rnode = NULL; 2013 2014 sysctl_createv(clog, 0, NULL, &rnode, 2015 CTLFLAG_PERMANENT, 2016 CTLTYPE_NODE, "ether", 2017 SYSCTL_DESCR("Ethernet-specific information"), 2018 NULL, 0, NULL, 0, 2019 CTL_NET, CTL_CREATE, CTL_EOL); 2020 2021 sysctl_createv(clog, 0, &rnode, NULL, 2022 CTLFLAG_PERMANENT, 2023 CTLTYPE_NODE, "multicast", 2024 SYSCTL_DESCR("multicast addresses"), 2025 ether_multicast_sysctl, 0, NULL, 0, 2026 CTL_CREATE, CTL_EOL); 2027 2028 sysctl_createv(clog, 0, &rnode, NULL, 2029 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 2030 CTLTYPE_STRING, "rps_hash", 2031 SYSCTL_DESCR("Interface rps hash function control"), 2032 sysctl_pktq_rps_hash_handler, 0, (void *)ðer_pktq_rps_hash_p, 2033 PKTQ_RPS_HASH_NAME_LEN, 2034 CTL_CREATE, CTL_EOL); 2035 } 2036 2037 void 2038 etherinit(void) 2039 { 2040 2041 #ifdef DIAGNOSTIC 2042 mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET); 2043 #endif 2044 ether_pktq_rps_hash_p = pktq_rps_hash_default; 2045 ether_sysctl_setup(NULL); 2046 } 2047