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