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