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