1 /* $NetBSD: if_ethersubr.c,v 1.236 2017/01/24 18:37:20 maxv 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.236 2017/01/24 18:37:20 maxv 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/malloc.h> 84 #include <sys/mbuf.h> 85 #include <sys/mutex.h> 86 #include <sys/ioctl.h> 87 #include <sys/errno.h> 88 #include <sys/device.h> 89 #include <sys/rnd.h> 90 #include <sys/rndsource.h> 91 #include <sys/cpu.h> 92 #include <sys/kmem.h> 93 94 #include <net/if.h> 95 #include <net/netisr.h> 96 #include <net/route.h> 97 #include <net/if_llc.h> 98 #include <net/if_dl.h> 99 #include <net/if_types.h> 100 #include <net/pktqueue.h> 101 102 #include <net/if_media.h> 103 #include <dev/mii/mii.h> 104 #include <dev/mii/miivar.h> 105 106 #if NARP == 0 107 /* 108 * XXX there should really be a way to issue this warning from within config(8) 109 */ 110 #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. 111 #endif 112 113 #include <net/bpf.h> 114 115 #include <net/if_ether.h> 116 #include <net/if_vlanvar.h> 117 118 #if NPPPOE > 0 119 #include <net/if_pppoe.h> 120 #endif 121 122 #if NAGR > 0 123 #include <net/agr/ieee8023_slowprotocols.h> /* XXX */ 124 #include <net/agr/ieee8023ad.h> 125 #include <net/agr/if_agrvar.h> 126 #endif 127 128 #if NBRIDGE > 0 129 #include <net/if_bridgevar.h> 130 #endif 131 132 #include <netinet/in.h> 133 #ifdef INET 134 #include <netinet/in_var.h> 135 #endif 136 #include <netinet/if_inarp.h> 137 138 #ifdef INET6 139 #ifndef INET 140 #include <netinet/in.h> 141 #endif 142 #include <netinet6/in6_var.h> 143 #include <netinet6/nd6.h> 144 #endif 145 146 147 #include "carp.h" 148 #if NCARP > 0 149 #include <netinet/ip_carp.h> 150 #endif 151 152 #ifdef NETATALK 153 #include <netatalk/at.h> 154 #include <netatalk/at_var.h> 155 #include <netatalk/at_extern.h> 156 157 #define llc_snap_org_code llc_un.type_snap.org_code 158 #define llc_snap_ether_type llc_un.type_snap.ether_type 159 160 extern u_char at_org_code[3]; 161 extern u_char aarp_org_code[3]; 162 #endif /* NETATALK */ 163 164 #ifdef MPLS 165 #include <netmpls/mpls.h> 166 #include <netmpls/mpls_var.h> 167 #endif 168 169 static struct timeval bigpktppslim_last; 170 static int bigpktppslim = 2; /* XXX */ 171 static int bigpktpps_count; 172 static kmutex_t bigpktpps_lock __cacheline_aligned; 173 174 175 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = 176 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 177 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] = 178 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 }; 179 #define senderr(e) { error = (e); goto bad;} 180 181 static int ether_output(struct ifnet *, struct mbuf *, 182 const struct sockaddr *, const struct rtentry *); 183 184 /* 185 * Ethernet output routine. 186 * Encapsulate a packet of type family for the local net. 187 * Assumes that ifp is actually pointer to ethercom structure. 188 */ 189 static int 190 ether_output(struct ifnet * const ifp0, struct mbuf * const m0, 191 const struct sockaddr * const dst, 192 const struct rtentry *rt) 193 { 194 uint16_t etype = 0; 195 int error = 0, hdrcmplt = 0; 196 uint8_t esrc[6], edst[6]; 197 struct mbuf *m = m0; 198 struct mbuf *mcopy = NULL; 199 struct ether_header *eh; 200 struct ifnet *ifp = ifp0; 201 #ifdef INET 202 struct arphdr *ah; 203 #endif /* INET */ 204 #ifdef NETATALK 205 struct at_ifaddr *aa; 206 #endif /* NETATALK */ 207 208 /* 209 * some paths such as carp_output() call ethr_output() with "ifp" 210 * argument as other than ether ifnet. 211 */ 212 KASSERT(ifp->if_output != ether_output 213 || ifp->if_extflags & IFEF_OUTPUT_MPSAFE); 214 215 #ifdef MBUFTRACE 216 m_claimm(m, ifp->if_mowner); 217 #endif 218 219 #if NCARP > 0 220 if (ifp->if_type == IFT_CARP) { 221 struct ifaddr *ifa; 222 int s = pserialize_read_enter(); 223 224 /* loop back if this is going to the carp interface */ 225 if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP && 226 (ifa = ifa_ifwithaddr(dst)) != NULL) { 227 if (ifa->ifa_ifp == ifp0) { 228 pserialize_read_exit(s); 229 return looutput(ifp0, m, dst, rt); 230 } 231 } 232 pserialize_read_exit(s); 233 234 ifp = ifp->if_carpdev; 235 /* ac = (struct arpcom *)ifp; */ 236 237 if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) != 238 (IFF_UP|IFF_RUNNING)) 239 senderr(ENETDOWN); 240 } 241 #endif /* NCARP > 0 */ 242 243 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 244 senderr(ENETDOWN); 245 246 switch (dst->sa_family) { 247 248 #ifdef INET 249 case AF_INET: 250 #ifndef NET_MPSAFE 251 KERNEL_LOCK(1, NULL); 252 #endif 253 if (m->m_flags & M_BCAST) 254 (void)memcpy(edst, etherbroadcastaddr, sizeof(edst)); 255 else if (m->m_flags & M_MCAST) 256 ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst); 257 else if ((error = arpresolve(ifp, rt, m, dst, edst, 258 sizeof(edst))) != 0) { 259 #ifndef NET_MPSAFE 260 KERNEL_UNLOCK_ONE(NULL); 261 #endif 262 return error == EWOULDBLOCK ? 0 : error; 263 } 264 #ifndef NET_MPSAFE 265 KERNEL_UNLOCK_ONE(NULL); 266 #endif 267 /* If broadcasting on a simplex interface, loopback a copy */ 268 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 269 mcopy = m_copy(m, 0, (int)M_COPYALL); 270 etype = htons(ETHERTYPE_IP); 271 break; 272 273 case AF_ARP: 274 ah = mtod(m, struct arphdr *); 275 if (m->m_flags & M_BCAST) 276 (void)memcpy(edst, etherbroadcastaddr, sizeof(edst)); 277 else { 278 void *tha = ar_tha(ah); 279 280 if (tha == NULL) { 281 /* fake with ARPHDR_IEEE1394 */ 282 m_freem(m); 283 return 0; 284 } 285 memcpy(edst, tha, sizeof(edst)); 286 } 287 288 ah->ar_hrd = htons(ARPHRD_ETHER); 289 290 switch (ntohs(ah->ar_op)) { 291 case ARPOP_REVREQUEST: 292 case ARPOP_REVREPLY: 293 etype = htons(ETHERTYPE_REVARP); 294 break; 295 296 case ARPOP_REQUEST: 297 case ARPOP_REPLY: 298 default: 299 etype = htons(ETHERTYPE_ARP); 300 } 301 302 break; 303 #endif 304 #ifdef INET6 305 case AF_INET6: 306 if (!nd6_storelladdr(ifp, rt, m, dst, edst, sizeof(edst))){ 307 /* something bad happened */ 308 return (0); 309 } 310 etype = htons(ETHERTYPE_IPV6); 311 break; 312 #endif 313 #ifdef NETATALK 314 case AF_APPLETALK: { 315 struct ifaddr *ifa; 316 int s; 317 318 KERNEL_LOCK(1, NULL); 319 if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) { 320 #ifdef NETATALKDEBUG 321 printf("aarpresolv failed\n"); 322 #endif /* NETATALKDEBUG */ 323 KERNEL_UNLOCK_ONE(NULL); 324 return (0); 325 } 326 /* 327 * ifaddr is the first thing in at_ifaddr 328 */ 329 s = pserialize_read_enter(); 330 ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp); 331 if (ifa == NULL) { 332 pserialize_read_exit(s); 333 KERNEL_UNLOCK_ONE(NULL); 334 goto bad; 335 } 336 aa = (struct at_ifaddr *)ifa; 337 338 /* 339 * In the phase 2 case, we need to prepend an mbuf for the 340 * llc header. Since we must preserve the value of m, 341 * which is passed to us by value, we m_copy() the first 342 * mbuf, and use it for our llc header. 343 */ 344 if (aa->aa_flags & AFA_PHASE2) { 345 struct llc llc; 346 347 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT); 348 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 349 llc.llc_control = LLC_UI; 350 memcpy(llc.llc_snap_org_code, at_org_code, 351 sizeof(llc.llc_snap_org_code)); 352 llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK); 353 memcpy(mtod(m, void *), &llc, sizeof(struct llc)); 354 } else { 355 etype = htons(ETHERTYPE_ATALK); 356 } 357 pserialize_read_exit(s); 358 KERNEL_UNLOCK_ONE(NULL); 359 break; 360 } 361 #endif /* NETATALK */ 362 case pseudo_AF_HDRCMPLT: 363 hdrcmplt = 1; 364 memcpy(esrc, 365 ((const struct ether_header *)dst->sa_data)->ether_shost, 366 sizeof(esrc)); 367 /* FALLTHROUGH */ 368 369 case AF_UNSPEC: 370 memcpy(edst, 371 ((const struct ether_header *)dst->sa_data)->ether_dhost, 372 sizeof(edst)); 373 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */ 374 etype = ((const struct ether_header *)dst->sa_data)->ether_type; 375 break; 376 377 default: 378 printf("%s: can't handle af%d\n", ifp->if_xname, 379 dst->sa_family); 380 senderr(EAFNOSUPPORT); 381 } 382 383 #ifdef MPLS 384 KERNEL_LOCK(1, NULL); 385 { 386 struct m_tag *mtag; 387 mtag = m_tag_find(m, PACKET_TAG_MPLS, NULL); 388 if (mtag != NULL) { 389 /* Having the tag itself indicates it's MPLS */ 390 etype = htons(ETHERTYPE_MPLS); 391 m_tag_delete(m, mtag); 392 } 393 } 394 KERNEL_UNLOCK_ONE(NULL); 395 #endif 396 397 if (mcopy) 398 (void)looutput(ifp, mcopy, dst, rt); 399 400 /* If no ether type is set, this must be a 802.2 formatted packet. 401 */ 402 if (etype == 0) 403 etype = htons(m->m_pkthdr.len); 404 /* 405 * Add local net header. If no space in first mbuf, 406 * allocate another. 407 */ 408 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 409 if (m == 0) 410 senderr(ENOBUFS); 411 eh = mtod(m, struct ether_header *); 412 /* Note: etype is already in network byte order. */ 413 (void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type)); 414 memcpy(eh->ether_dhost, edst, sizeof(edst)); 415 if (hdrcmplt) 416 memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost)); 417 else 418 memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl), 419 sizeof(eh->ether_shost)); 420 421 #if NCARP > 0 422 if (ifp0 != ifp && ifp0->if_type == IFT_CARP) { 423 memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl), 424 sizeof(eh->ether_shost)); 425 } 426 #endif /* NCARP > 0 */ 427 428 if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0) 429 return (error); 430 if (m == NULL) 431 return (0); 432 433 #if NBRIDGE > 0 434 /* 435 * Bridges require special output handling. 436 */ 437 if (ifp->if_bridge) 438 return (bridge_output(ifp, m, NULL, NULL)); 439 #endif 440 441 #if NCARP > 0 442 if (ifp != ifp0) 443 ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN; 444 #endif /* NCARP > 0 */ 445 446 #ifdef ALTQ 447 KERNEL_LOCK(1, NULL); 448 /* 449 * If ALTQ is enabled on the parent interface, do 450 * classification; the queueing discipline might not 451 * require classification, but might require the 452 * address family/header pointer in the pktattr. 453 */ 454 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 455 altq_etherclassify(&ifp->if_snd, m); 456 KERNEL_UNLOCK_ONE(NULL); 457 #endif 458 return ifq_enqueue(ifp, m); 459 460 bad: 461 if (m) 462 m_freem(m); 463 return (error); 464 } 465 466 #ifdef ALTQ 467 /* 468 * This routine is a slight hack to allow a packet to be classified 469 * if the Ethernet headers are present. It will go away when ALTQ's 470 * classification engine understands link headers. 471 */ 472 void 473 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m) 474 { 475 struct ether_header *eh; 476 uint16_t ether_type; 477 int hlen, af, hdrsize; 478 void *hdr; 479 480 hlen = ETHER_HDR_LEN; 481 eh = mtod(m, struct ether_header *); 482 483 ether_type = htons(eh->ether_type); 484 485 if (ether_type < ETHERMTU) { 486 /* LLC/SNAP */ 487 struct llc *llc = (struct llc *)(eh + 1); 488 hlen += 8; 489 490 if (m->m_len < hlen || 491 llc->llc_dsap != LLC_SNAP_LSAP || 492 llc->llc_ssap != LLC_SNAP_LSAP || 493 llc->llc_control != LLC_UI) { 494 /* Not SNAP. */ 495 goto bad; 496 } 497 498 ether_type = htons(llc->llc_un.type_snap.ether_type); 499 } 500 501 switch (ether_type) { 502 case ETHERTYPE_IP: 503 af = AF_INET; 504 hdrsize = 20; /* sizeof(struct ip) */ 505 break; 506 507 case ETHERTYPE_IPV6: 508 af = AF_INET6; 509 hdrsize = 40; /* sizeof(struct ip6_hdr) */ 510 break; 511 512 default: 513 af = AF_UNSPEC; 514 hdrsize = 0; 515 break; 516 } 517 518 while (m->m_len <= hlen) { 519 hlen -= m->m_len; 520 m = m->m_next; 521 } 522 if (m->m_len < (hlen + hdrsize)) { 523 /* 524 * protocol header not in a single mbuf. 525 * We can't cope with this situation right 526 * now (but it shouldn't ever happen, really, anyhow). 527 */ 528 #ifdef DEBUG 529 printf("altq_etherclassify: headers span multiple mbufs: " 530 "%d < %d\n", m->m_len, (hlen + hdrsize)); 531 #endif 532 goto bad; 533 } 534 535 m->m_data += hlen; 536 m->m_len -= hlen; 537 538 hdr = mtod(m, void *); 539 540 if (ALTQ_NEEDS_CLASSIFY(ifq)) 541 m->m_pkthdr.pattr_class = 542 (*ifq->altq_classify)(ifq->altq_clfier, m, af); 543 m->m_pkthdr.pattr_af = af; 544 m->m_pkthdr.pattr_hdr = hdr; 545 546 m->m_data -= hlen; 547 m->m_len += hlen; 548 549 return; 550 551 bad: 552 m->m_pkthdr.pattr_class = NULL; 553 m->m_pkthdr.pattr_hdr = NULL; 554 m->m_pkthdr.pattr_af = AF_UNSPEC; 555 } 556 #endif /* ALTQ */ 557 558 /* 559 * Process a received Ethernet packet; 560 * the packet is in the mbuf chain m with 561 * the ether header. 562 */ 563 void 564 ether_input(struct ifnet *ifp, struct mbuf *m) 565 { 566 struct ethercom *ec = (struct ethercom *) ifp; 567 pktqueue_t *pktq = NULL; 568 struct ifqueue *inq = NULL; 569 uint16_t etype; 570 struct ether_header *eh; 571 size_t ehlen; 572 static int earlypkts; 573 int isr = 0; 574 #if defined (LLC) || defined(NETATALK) 575 struct llc *l; 576 #endif 577 578 KASSERT(!cpu_intr_p()); 579 580 if ((ifp->if_flags & IFF_UP) == 0) { 581 m_freem(m); 582 return; 583 } 584 585 #ifdef MBUFTRACE 586 m_claimm(m, &ec->ec_rx_mowner); 587 #endif 588 eh = mtod(m, struct ether_header *); 589 etype = ntohs(eh->ether_type); 590 ehlen = sizeof(*eh); 591 592 if(__predict_false(earlypkts < 100 || !rnd_initial_entropy)) { 593 rnd_add_data(NULL, eh, ehlen, 0); 594 earlypkts++; 595 } 596 597 /* 598 * Determine if the packet is within its size limits. 599 */ 600 if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len > 601 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) { 602 mutex_enter(&bigpktpps_lock); 603 if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count, 604 bigpktppslim)) { 605 printf("%s: discarding oversize frame (len=%d)\n", 606 ifp->if_xname, m->m_pkthdr.len); 607 } 608 mutex_exit(&bigpktpps_lock); 609 m_freem(m); 610 return; 611 } 612 613 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 614 /* 615 * If this is not a simplex interface, drop the packet 616 * if it came from us. 617 */ 618 if ((ifp->if_flags & IFF_SIMPLEX) == 0 && 619 memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost, 620 ETHER_ADDR_LEN) == 0) { 621 m_freem(m); 622 return; 623 } 624 625 if (memcmp(etherbroadcastaddr, 626 eh->ether_dhost, ETHER_ADDR_LEN) == 0) 627 m->m_flags |= M_BCAST; 628 else 629 m->m_flags |= M_MCAST; 630 ifp->if_imcasts++; 631 } 632 633 /* If the CRC is still on the packet, trim it off. */ 634 if (m->m_flags & M_HASFCS) { 635 m_adj(m, -ETHER_CRC_LEN); 636 m->m_flags &= ~M_HASFCS; 637 } 638 639 ifp->if_ibytes += m->m_pkthdr.len; 640 641 #if NCARP > 0 642 if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) { 643 /* 644 * clear M_PROMISC, in case the packets comes from a 645 * vlan 646 */ 647 m->m_flags &= ~M_PROMISC; 648 if (carp_input(m, (uint8_t *)&eh->ether_shost, 649 (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0) 650 return; 651 } 652 #endif /* NCARP > 0 */ 653 if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 && 654 (ifp->if_flags & IFF_PROMISC) != 0 && 655 memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost, 656 ETHER_ADDR_LEN) != 0) { 657 m->m_flags |= M_PROMISC; 658 } 659 660 if ((m->m_flags & M_PROMISC) == 0) { 661 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0) 662 return; 663 if (m == NULL) 664 return; 665 666 eh = mtod(m, struct ether_header *); 667 etype = ntohs(eh->ether_type); 668 ehlen = sizeof(*eh); 669 } 670 671 #if NAGR > 0 672 if (ifp->if_agrprivate && 673 __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) { 674 m->m_flags &= ~M_PROMISC; 675 agr_input(ifp, m); 676 return; 677 } 678 #endif /* NAGR > 0 */ 679 680 /* 681 * If VLANs are configured on the interface, check to 682 * see if the device performed the decapsulation and 683 * provided us with the tag. 684 */ 685 if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) { 686 #if NVLAN > 0 687 /* 688 * vlan_input() will either recursively call ether_input() 689 * or drop the packet. 690 */ 691 vlan_input(ifp, m); 692 #else 693 m_freem(m); 694 #endif 695 return; 696 } 697 698 /* 699 * Handle protocols that expect to have the Ethernet header 700 * (and possibly FCS) intact. 701 */ 702 switch (etype) { 703 case ETHERTYPE_VLAN: { 704 struct ether_vlan_header *evl = (void *)eh; 705 /* 706 * If there is a tag of 0, then the VLAN header was probably 707 * just being used to store the priority. Extract the ether 708 * type, and if IP or IPV6, let them deal with it. 709 */ 710 if (m->m_len <= sizeof(*evl) 711 && EVL_VLANOFTAG(evl->evl_tag) == 0) { 712 etype = ntohs(evl->evl_proto); 713 ehlen = sizeof(*evl); 714 if ((m->m_flags & M_PROMISC) == 0 715 && (etype == ETHERTYPE_IP 716 || etype == ETHERTYPE_IPV6)) 717 break; 718 } 719 #if NVLAN > 0 720 /* 721 * vlan_input() will either recursively call ether_input() 722 * or drop the packet. 723 */ 724 if (((struct ethercom *)ifp)->ec_nvlans != 0) 725 vlan_input(ifp, m); 726 else 727 #endif /* NVLAN > 0 */ 728 m_freem(m); 729 return; 730 } 731 #if NPPPOE > 0 732 case ETHERTYPE_PPPOEDISC: 733 pppoedisc_input(ifp, m); 734 return; 735 case ETHERTYPE_PPPOE: 736 pppoe_input(ifp, m); 737 return; 738 #endif /* NPPPOE > 0 */ 739 case ETHERTYPE_SLOWPROTOCOLS: { 740 uint8_t subtype; 741 742 #if defined(DIAGNOSTIC) 743 if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) { 744 panic("ether_input: too short slow protocol packet"); 745 } 746 #endif 747 m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype); 748 switch (subtype) { 749 #if NAGR > 0 750 case SLOWPROTOCOLS_SUBTYPE_LACP: 751 if (ifp->if_agrprivate) { 752 ieee8023ad_lacp_input(ifp, m); 753 return; 754 } 755 break; 756 757 case SLOWPROTOCOLS_SUBTYPE_MARKER: 758 if (ifp->if_agrprivate) { 759 ieee8023ad_marker_input(ifp, m); 760 return; 761 } 762 break; 763 #endif /* NAGR > 0 */ 764 default: 765 if (subtype == 0 || subtype > 10) { 766 /* illegal value */ 767 m_freem(m); 768 return; 769 } 770 /* unknown subtype */ 771 break; 772 } 773 /* FALLTHROUGH */ 774 } 775 default: 776 if (m->m_flags & M_PROMISC) { 777 m_freem(m); 778 return; 779 } 780 } 781 782 /* If the CRC is still on the packet, trim it off. */ 783 if (m->m_flags & M_HASFCS) { 784 m_adj(m, -ETHER_CRC_LEN); 785 m->m_flags &= ~M_HASFCS; 786 } 787 788 if (etype > ETHERMTU + sizeof (struct ether_header)) { 789 /* Strip off the Ethernet header. */ 790 m_adj(m, ehlen); 791 792 switch (etype) { 793 #ifdef INET 794 case ETHERTYPE_IP: 795 #ifdef GATEWAY 796 if (ipflow_fastforward(m)) 797 return; 798 #endif 799 pktq = ip_pktq; 800 break; 801 802 case ETHERTYPE_ARP: 803 isr = NETISR_ARP; 804 inq = &arpintrq; 805 break; 806 807 case ETHERTYPE_REVARP: 808 revarpinput(m); /* XXX queue? */ 809 return; 810 #endif 811 #ifdef INET6 812 case ETHERTYPE_IPV6: 813 if (__predict_false(!in6_present)) { 814 m_freem(m); 815 return; 816 } 817 #ifdef GATEWAY 818 if (ip6flow_fastforward(&m)) 819 return; 820 #endif 821 pktq = ip6_pktq; 822 break; 823 #endif 824 #ifdef NETATALK 825 case ETHERTYPE_ATALK: 826 isr = NETISR_ATALK; 827 inq = &atintrq1; 828 break; 829 case ETHERTYPE_AARP: 830 /* probably this should be done with a NETISR as well */ 831 aarpinput(ifp, m); /* XXX */ 832 return; 833 #endif /* NETATALK */ 834 #ifdef MPLS 835 case ETHERTYPE_MPLS: 836 isr = NETISR_MPLS; 837 inq = &mplsintrq; 838 break; 839 #endif 840 default: 841 m_freem(m); 842 return; 843 } 844 } else { 845 #if defined (LLC) || defined (NETATALK) 846 l = (struct llc *)(eh+1); 847 switch (l->llc_dsap) { 848 #ifdef NETATALK 849 case LLC_SNAP_LSAP: 850 switch (l->llc_control) { 851 case LLC_UI: 852 if (l->llc_ssap != LLC_SNAP_LSAP) { 853 goto dropanyway; 854 } 855 856 if (memcmp(&(l->llc_snap_org_code)[0], 857 at_org_code, sizeof(at_org_code)) == 0 && 858 ntohs(l->llc_snap_ether_type) == 859 ETHERTYPE_ATALK) { 860 inq = &atintrq2; 861 m_adj(m, sizeof(struct ether_header) 862 + sizeof(struct llc)); 863 isr = NETISR_ATALK; 864 break; 865 } 866 867 if (memcmp(&(l->llc_snap_org_code)[0], 868 aarp_org_code, 869 sizeof(aarp_org_code)) == 0 && 870 ntohs(l->llc_snap_ether_type) == 871 ETHERTYPE_AARP) { 872 m_adj( m, sizeof(struct ether_header) 873 + sizeof(struct llc)); 874 aarpinput(ifp, m); /* XXX */ 875 return; 876 } 877 878 default: 879 goto dropanyway; 880 } 881 break; 882 dropanyway: 883 #endif 884 default: 885 m_freem(m); 886 return; 887 } 888 #else /* ISO || LLC || NETATALK*/ 889 m_freem(m); 890 return; 891 #endif /* ISO || LLC || NETATALK*/ 892 } 893 894 if (__predict_true(pktq)) { 895 #ifdef NET_MPSAFE 896 const u_int h = curcpu()->ci_index; 897 #else 898 const uint32_t h = pktq_rps_hash(m); 899 #endif 900 if (__predict_false(!pktq_enqueue(pktq, m, h))) { 901 m_freem(m); 902 } 903 return; 904 } 905 906 if (__predict_false(!inq)) { 907 /* Should not happen. */ 908 m_freem(m); 909 return; 910 } 911 912 IFQ_LOCK(inq); 913 if (IF_QFULL(inq)) { 914 IF_DROP(inq); 915 IFQ_UNLOCK(inq); 916 m_freem(m); 917 } else { 918 IF_ENQUEUE(inq, m); 919 IFQ_UNLOCK(inq); 920 schednetisr(isr); 921 } 922 } 923 924 /* 925 * Convert Ethernet address to printable (loggable) representation. 926 */ 927 char * 928 ether_sprintf(const u_char *ap) 929 { 930 static char etherbuf[3 * ETHER_ADDR_LEN]; 931 return ether_snprintf(etherbuf, sizeof(etherbuf), ap); 932 } 933 934 char * 935 ether_snprintf(char *buf, size_t len, const u_char *ap) 936 { 937 char *cp = buf; 938 size_t i; 939 940 for (i = 0; i < len / 3; i++) { 941 *cp++ = hexdigits[*ap >> 4]; 942 *cp++ = hexdigits[*ap++ & 0xf]; 943 *cp++ = ':'; 944 } 945 *--cp = '\0'; 946 return buf; 947 } 948 949 /* 950 * Perform common duties while attaching to interface list 951 */ 952 void 953 ether_ifattach(struct ifnet *ifp, const uint8_t *lla) 954 { 955 struct ethercom *ec = (struct ethercom *)ifp; 956 957 ifp->if_extflags |= IFEF_OUTPUT_MPSAFE; 958 ifp->if_type = IFT_ETHER; 959 ifp->if_hdrlen = ETHER_HDR_LEN; 960 ifp->if_dlt = DLT_EN10MB; 961 ifp->if_mtu = ETHERMTU; 962 ifp->if_output = ether_output; 963 ifp->_if_input = ether_input; 964 if (ifp->if_baudrate == 0) 965 ifp->if_baudrate = IF_Mbps(10); /* just a default */ 966 967 if (lla != NULL) 968 if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla)); 969 970 LIST_INIT(&ec->ec_multiaddrs); 971 ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET); 972 ifp->if_broadcastaddr = etherbroadcastaddr; 973 bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header)); 974 #ifdef MBUFTRACE 975 strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname, 976 sizeof(ec->ec_tx_mowner.mo_name)); 977 strlcpy(ec->ec_tx_mowner.mo_descr, "tx", 978 sizeof(ec->ec_tx_mowner.mo_descr)); 979 strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname, 980 sizeof(ec->ec_rx_mowner.mo_name)); 981 strlcpy(ec->ec_rx_mowner.mo_descr, "rx", 982 sizeof(ec->ec_rx_mowner.mo_descr)); 983 MOWNER_ATTACH(&ec->ec_tx_mowner); 984 MOWNER_ATTACH(&ec->ec_rx_mowner); 985 ifp->if_mowner = &ec->ec_tx_mowner; 986 #endif 987 } 988 989 void 990 ether_ifdetach(struct ifnet *ifp) 991 { 992 struct ethercom *ec = (void *) ifp; 993 struct ether_multi *enm; 994 int s; 995 996 /* 997 * Prevent further calls to ioctl (for example turning off 998 * promiscuous mode from the bridge code), which eventually can 999 * call if_init() which can cause panics because the interface 1000 * is in the process of being detached. Return device not configured 1001 * instead. 1002 */ 1003 ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio; 1004 1005 #if NBRIDGE > 0 1006 if (ifp->if_bridge) 1007 bridge_ifdetach(ifp); 1008 #endif 1009 1010 bpf_detach(ifp); 1011 1012 #if NVLAN > 0 1013 if (ec->ec_nvlans) 1014 vlan_ifdetach(ifp); 1015 #endif 1016 1017 s = splnet(); 1018 mutex_enter(ec->ec_lock); 1019 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) { 1020 LIST_REMOVE(enm, enm_list); 1021 kmem_free(enm, sizeof(*enm)); 1022 ec->ec_multicnt--; 1023 } 1024 mutex_exit(ec->ec_lock); 1025 splx(s); 1026 1027 mutex_destroy(ec->ec_lock); 1028 1029 ifp->if_mowner = NULL; 1030 MOWNER_DETACH(&ec->ec_rx_mowner); 1031 MOWNER_DETACH(&ec->ec_tx_mowner); 1032 } 1033 1034 #if 0 1035 /* 1036 * This is for reference. We have a table-driven version 1037 * of the little-endian crc32 generator, which is faster 1038 * than the double-loop. 1039 */ 1040 uint32_t 1041 ether_crc32_le(const uint8_t *buf, size_t len) 1042 { 1043 uint32_t c, crc, carry; 1044 size_t i, j; 1045 1046 crc = 0xffffffffU; /* initial value */ 1047 1048 for (i = 0; i < len; i++) { 1049 c = buf[i]; 1050 for (j = 0; j < 8; j++) { 1051 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 1052 crc >>= 1; 1053 c >>= 1; 1054 if (carry) 1055 crc = (crc ^ ETHER_CRC_POLY_LE); 1056 } 1057 } 1058 1059 return (crc); 1060 } 1061 #else 1062 uint32_t 1063 ether_crc32_le(const uint8_t *buf, size_t len) 1064 { 1065 static const uint32_t crctab[] = { 1066 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 1067 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 1068 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 1069 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 1070 }; 1071 uint32_t crc; 1072 size_t i; 1073 1074 crc = 0xffffffffU; /* initial value */ 1075 1076 for (i = 0; i < len; i++) { 1077 crc ^= buf[i]; 1078 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1079 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1080 } 1081 1082 return (crc); 1083 } 1084 #endif 1085 1086 uint32_t 1087 ether_crc32_be(const uint8_t *buf, size_t len) 1088 { 1089 uint32_t c, crc, carry; 1090 size_t i, j; 1091 1092 crc = 0xffffffffU; /* initial value */ 1093 1094 for (i = 0; i < len; i++) { 1095 c = buf[i]; 1096 for (j = 0; j < 8; j++) { 1097 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 1098 crc <<= 1; 1099 c >>= 1; 1100 if (carry) 1101 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 1102 } 1103 } 1104 1105 return (crc); 1106 } 1107 1108 #ifdef INET 1109 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] = 1110 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 1111 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] = 1112 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 1113 #endif 1114 #ifdef INET6 1115 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] = 1116 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 1117 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] = 1118 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff }; 1119 #endif 1120 1121 /* 1122 * ether_aton implementation, not using a static buffer. 1123 */ 1124 int 1125 ether_aton_r(u_char *dest, size_t len, const char *str) 1126 { 1127 const u_char *cp = (const void *)str; 1128 u_char *ep; 1129 1130 #define atox(c) (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10)) 1131 1132 if (len < ETHER_ADDR_LEN) 1133 return ENOSPC; 1134 1135 ep = dest + ETHER_ADDR_LEN; 1136 1137 while (*cp) { 1138 if (!isxdigit(*cp)) 1139 return EINVAL; 1140 *dest = atox(*cp); 1141 cp++; 1142 if (isxdigit(*cp)) { 1143 *dest = (*dest << 4) | atox(*cp); 1144 dest++; 1145 cp++; 1146 } else 1147 dest++; 1148 if (dest == ep) 1149 return *cp == '\0' ? 0 : ENAMETOOLONG; 1150 switch (*cp) { 1151 case ':': 1152 case '-': 1153 case '.': 1154 cp++; 1155 break; 1156 } 1157 } 1158 return ENOBUFS; 1159 } 1160 1161 /* 1162 * Convert a sockaddr into an Ethernet address or range of Ethernet 1163 * addresses. 1164 */ 1165 int 1166 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN], 1167 uint8_t addrhi[ETHER_ADDR_LEN]) 1168 { 1169 #ifdef INET 1170 const struct sockaddr_in *sin; 1171 #endif /* INET */ 1172 #ifdef INET6 1173 const struct sockaddr_in6 *sin6; 1174 #endif /* INET6 */ 1175 1176 switch (sa->sa_family) { 1177 1178 case AF_UNSPEC: 1179 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN); 1180 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 1181 break; 1182 1183 #ifdef INET 1184 case AF_INET: 1185 sin = satocsin(sa); 1186 if (sin->sin_addr.s_addr == INADDR_ANY) { 1187 /* 1188 * An IP address of INADDR_ANY means listen to 1189 * or stop listening to all of the Ethernet 1190 * multicast addresses used for IP. 1191 * (This is for the sake of IP multicast routers.) 1192 */ 1193 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN); 1194 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN); 1195 } 1196 else { 1197 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 1198 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 1199 } 1200 break; 1201 #endif 1202 #ifdef INET6 1203 case AF_INET6: 1204 sin6 = satocsin6(sa); 1205 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1206 /* 1207 * An IP6 address of 0 means listen to or stop 1208 * listening to all of the Ethernet multicast 1209 * address used for IP6. 1210 * (This is used for multicast routers.) 1211 */ 1212 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN); 1213 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN); 1214 } else { 1215 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 1216 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 1217 } 1218 break; 1219 #endif 1220 1221 default: 1222 return EAFNOSUPPORT; 1223 } 1224 return 0; 1225 } 1226 1227 /* 1228 * Add an Ethernet multicast address or range of addresses to the list for a 1229 * given interface. 1230 */ 1231 int 1232 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec) 1233 { 1234 struct ether_multi *enm, *_enm; 1235 u_char addrlo[ETHER_ADDR_LEN]; 1236 u_char addrhi[ETHER_ADDR_LEN]; 1237 int s, error = 0; 1238 1239 /* Allocate out of lock */ 1240 /* XXX still can be called in softint */ 1241 enm = kmem_intr_alloc(sizeof(*enm), KM_SLEEP); 1242 if (enm == NULL) 1243 return ENOBUFS; 1244 1245 s = splnet(); 1246 mutex_enter(ec->ec_lock); 1247 error = ether_multiaddr(sa, addrlo, addrhi); 1248 if (error != 0) 1249 goto out; 1250 1251 /* 1252 * Verify that we have valid Ethernet multicast addresses. 1253 */ 1254 if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) { 1255 error = EINVAL; 1256 goto out; 1257 } 1258 /* 1259 * See if the address range is already in the list. 1260 */ 1261 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, _enm); 1262 if (_enm != NULL) { 1263 /* 1264 * Found it; just increment the reference count. 1265 */ 1266 ++_enm->enm_refcount; 1267 error = 0; 1268 goto out; 1269 } 1270 /* 1271 * New address or range; malloc a new multicast record 1272 * and link it into the interface's multicast list. 1273 */ 1274 memcpy(enm->enm_addrlo, addrlo, 6); 1275 memcpy(enm->enm_addrhi, addrhi, 6); 1276 enm->enm_refcount = 1; 1277 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list); 1278 ec->ec_multicnt++; 1279 /* 1280 * Return ENETRESET to inform the driver that the list has changed 1281 * and its reception filter should be adjusted accordingly. 1282 */ 1283 error = ENETRESET; 1284 enm = NULL; 1285 out: 1286 mutex_exit(ec->ec_lock); 1287 splx(s); 1288 if (enm != NULL) 1289 kmem_free(enm, sizeof(*enm)); 1290 return error; 1291 } 1292 1293 /* 1294 * Delete a multicast address record. 1295 */ 1296 int 1297 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec) 1298 { 1299 struct ether_multi *enm; 1300 u_char addrlo[ETHER_ADDR_LEN]; 1301 u_char addrhi[ETHER_ADDR_LEN]; 1302 int s, error; 1303 1304 s = splnet(); 1305 mutex_enter(ec->ec_lock); 1306 error = ether_multiaddr(sa, addrlo, addrhi); 1307 if (error != 0) 1308 goto error; 1309 1310 /* 1311 * Look ur the address in our list. 1312 */ 1313 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm); 1314 if (enm == NULL) { 1315 error = ENXIO; 1316 goto error; 1317 } 1318 if (--enm->enm_refcount != 0) { 1319 /* 1320 * Still some claims to this record. 1321 */ 1322 error = 0; 1323 goto error; 1324 } 1325 /* 1326 * No remaining claims to this record; unlink and free it. 1327 */ 1328 LIST_REMOVE(enm, enm_list); 1329 ec->ec_multicnt--; 1330 mutex_exit(ec->ec_lock); 1331 splx(s); 1332 1333 kmem_free(enm, sizeof(*enm)); 1334 /* 1335 * Return ENETRESET to inform the driver that the list has changed 1336 * and its reception filter should be adjusted accordingly. 1337 */ 1338 return ENETRESET; 1339 error: 1340 mutex_exit(ec->ec_lock); 1341 splx(s); 1342 return error; 1343 } 1344 1345 void 1346 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb) 1347 { 1348 ec->ec_ifflags_cb = cb; 1349 } 1350 1351 /* 1352 * Common ioctls for Ethernet interfaces. Note, we must be 1353 * called at splnet(). 1354 */ 1355 int 1356 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1357 { 1358 struct ethercom *ec = (void *) ifp; 1359 struct eccapreq *eccr; 1360 struct ifreq *ifr = (struct ifreq *)data; 1361 struct if_laddrreq *iflr = data; 1362 const struct sockaddr_dl *sdl; 1363 static const uint8_t zero[ETHER_ADDR_LEN]; 1364 int error; 1365 1366 switch (cmd) { 1367 case SIOCINITIFADDR: 1368 { 1369 struct ifaddr *ifa = (struct ifaddr *)data; 1370 if (ifa->ifa_addr->sa_family != AF_LINK 1371 && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) != 1372 (IFF_UP|IFF_RUNNING)) { 1373 ifp->if_flags |= IFF_UP; 1374 if ((error = (*ifp->if_init)(ifp)) != 0) 1375 return error; 1376 } 1377 #ifdef INET 1378 if (ifa->ifa_addr->sa_family == AF_INET) 1379 arp_ifinit(ifp, ifa); 1380 #endif /* INET */ 1381 return 0; 1382 } 1383 1384 case SIOCSIFMTU: 1385 { 1386 int maxmtu; 1387 1388 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU) 1389 maxmtu = ETHERMTU_JUMBO; 1390 else 1391 maxmtu = ETHERMTU; 1392 1393 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu) 1394 return EINVAL; 1395 else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET) 1396 return error; 1397 else if (ifp->if_flags & IFF_UP) { 1398 /* Make sure the device notices the MTU change. */ 1399 return (*ifp->if_init)(ifp); 1400 } else 1401 return 0; 1402 } 1403 1404 case SIOCSIFFLAGS: 1405 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 1406 return error; 1407 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 1408 case IFF_RUNNING: 1409 /* 1410 * If interface is marked down and it is running, 1411 * then stop and disable it. 1412 */ 1413 (*ifp->if_stop)(ifp, 1); 1414 break; 1415 case IFF_UP: 1416 /* 1417 * If interface is marked up and it is stopped, then 1418 * start it. 1419 */ 1420 return (*ifp->if_init)(ifp); 1421 case IFF_UP|IFF_RUNNING: 1422 error = 0; 1423 if (ec->ec_ifflags_cb == NULL || 1424 (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) { 1425 /* 1426 * Reset the interface to pick up 1427 * changes in any other flags that 1428 * affect the hardware state. 1429 */ 1430 return (*ifp->if_init)(ifp); 1431 } else 1432 return error; 1433 case 0: 1434 break; 1435 } 1436 return 0; 1437 case SIOCGETHERCAP: 1438 eccr = (struct eccapreq *)data; 1439 eccr->eccr_capabilities = ec->ec_capabilities; 1440 eccr->eccr_capenable = ec->ec_capenable; 1441 return 0; 1442 case SIOCADDMULTI: 1443 return ether_addmulti(ifreq_getaddr(cmd, ifr), ec); 1444 case SIOCDELMULTI: 1445 return ether_delmulti(ifreq_getaddr(cmd, ifr), ec); 1446 case SIOCSIFMEDIA: 1447 case SIOCGIFMEDIA: 1448 if (ec->ec_mii == NULL) 1449 return ENOTTY; 1450 return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd); 1451 case SIOCALIFADDR: 1452 sdl = satocsdl(sstocsa(&iflr->addr)); 1453 if (sdl->sdl_family != AF_LINK) 1454 ; 1455 else if (ETHER_IS_MULTICAST(CLLADDR(sdl))) 1456 return EINVAL; 1457 else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0) 1458 return EINVAL; 1459 /*FALLTHROUGH*/ 1460 default: 1461 return ifioctl_common(ifp, cmd, data); 1462 } 1463 return 0; 1464 } 1465 1466 /* 1467 * Enable/disable passing VLAN packets if the parent interface supports it. 1468 * Return: 1469 * 0: Ok 1470 * -1: Parent interface does not support vlans 1471 * >0: Error 1472 */ 1473 int 1474 ether_enable_vlan_mtu(struct ifnet *ifp) 1475 { 1476 int error; 1477 struct ethercom *ec = (void *)ifp; 1478 1479 /* Parent does not support VLAN's */ 1480 if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0) 1481 return -1; 1482 1483 /* 1484 * Parent supports the VLAN_MTU capability, 1485 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames; 1486 * enable it. 1487 */ 1488 ec->ec_capenable |= ETHERCAP_VLAN_MTU; 1489 1490 /* Interface is down, defer for later */ 1491 if ((ifp->if_flags & IFF_UP) == 0) 1492 return 0; 1493 1494 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0) 1495 return 0; 1496 1497 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU; 1498 return error; 1499 } 1500 1501 int 1502 ether_disable_vlan_mtu(struct ifnet *ifp) 1503 { 1504 int error; 1505 struct ethercom *ec = (void *)ifp; 1506 1507 /* We still have VLAN's, defer for later */ 1508 if (ec->ec_nvlans != 0) 1509 return 0; 1510 1511 /* Parent does not support VLAB's, nothing to do. */ 1512 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0) 1513 return -1; 1514 1515 /* 1516 * Disable Tx/Rx of VLAN-sized frames. 1517 */ 1518 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU; 1519 1520 /* Interface is down, defer for later */ 1521 if ((ifp->if_flags & IFF_UP) == 0) 1522 return 0; 1523 1524 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0) 1525 return 0; 1526 1527 ec->ec_capenable |= ETHERCAP_VLAN_MTU; 1528 return error; 1529 } 1530 1531 static int 1532 ether_multicast_sysctl(SYSCTLFN_ARGS) 1533 { 1534 struct ether_multi *enm; 1535 struct ifnet *ifp; 1536 struct ethercom *ec; 1537 int error = 0; 1538 size_t written; 1539 struct psref psref; 1540 int bound, s; 1541 unsigned int multicnt; 1542 struct ether_multi_sysctl *addrs; 1543 int i; 1544 1545 if (namelen != 1) 1546 return EINVAL; 1547 1548 bound = curlwp_bind(); 1549 ifp = if_get_byindex(name[0], &psref); 1550 if (ifp == NULL) { 1551 error = ENODEV; 1552 goto out; 1553 } 1554 if (ifp->if_type != IFT_ETHER) { 1555 if_put(ifp, &psref); 1556 *oldlenp = 0; 1557 goto out; 1558 } 1559 ec = (struct ethercom *)ifp; 1560 1561 if (oldp == NULL) { 1562 if_put(ifp, &psref); 1563 *oldlenp = ec->ec_multicnt * sizeof(*addrs); 1564 goto out; 1565 } 1566 1567 /* 1568 * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which 1569 * is sleepable, with holding it. Copy data to a local buffer first 1570 * with holding it and then call sysctl_copyout without holding it. 1571 */ 1572 retry: 1573 multicnt = ec->ec_multicnt; 1574 addrs = kmem_alloc(sizeof(*addrs) * multicnt, KM_SLEEP); 1575 1576 s = splnet(); 1577 mutex_enter(ec->ec_lock); 1578 if (multicnt < ec->ec_multicnt) { 1579 /* The number of multicast addresses have increased */ 1580 mutex_exit(ec->ec_lock); 1581 splx(s); 1582 kmem_free(addrs, sizeof(*addrs) * multicnt); 1583 goto retry; 1584 } 1585 1586 i = 0; 1587 LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) { 1588 struct ether_multi_sysctl *addr = &addrs[i]; 1589 addr->enm_refcount = enm->enm_refcount; 1590 memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN); 1591 memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN); 1592 i++; 1593 } 1594 mutex_exit(ec->ec_lock); 1595 splx(s); 1596 1597 error = 0; 1598 written = 0; 1599 for (i = 0; i < multicnt; i++) { 1600 struct ether_multi_sysctl *addr = &addrs[i]; 1601 1602 if (written + sizeof(*addr) > *oldlenp) 1603 break; 1604 error = sysctl_copyout(l, addr, oldp, sizeof(*addr)); 1605 if (error) 1606 break; 1607 written += sizeof(*addr); 1608 oldp = (char *)oldp + sizeof(*addr); 1609 } 1610 kmem_free(addrs, sizeof(*addrs) * multicnt); 1611 1612 if_put(ifp, &psref); 1613 1614 *oldlenp = written; 1615 out: 1616 curlwp_bindx(bound); 1617 return error; 1618 } 1619 1620 static void 1621 ether_sysctl_setup(struct sysctllog **clog) 1622 { 1623 const struct sysctlnode *rnode = NULL; 1624 1625 sysctl_createv(clog, 0, NULL, &rnode, 1626 CTLFLAG_PERMANENT, 1627 CTLTYPE_NODE, "ether", 1628 SYSCTL_DESCR("Ethernet-specific information"), 1629 NULL, 0, NULL, 0, 1630 CTL_NET, CTL_CREATE, CTL_EOL); 1631 1632 sysctl_createv(clog, 0, &rnode, NULL, 1633 CTLFLAG_PERMANENT, 1634 CTLTYPE_NODE, "multicast", 1635 SYSCTL_DESCR("multicast addresses"), 1636 ether_multicast_sysctl, 0, NULL, 0, 1637 CTL_CREATE, CTL_EOL); 1638 } 1639 1640 void 1641 etherinit(void) 1642 { 1643 1644 mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET); 1645 ether_sysctl_setup(NULL); 1646 } 1647