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