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