1 /* $NetBSD: if_ethersubr.c,v 1.194 2013/03/01 18:25:56 joerg 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.194 2013/03/01 18:25:56 joerg 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_pfil_hooks.h" 73 #include "opt_pppoe.h" 74 #include "vlan.h" 75 #include "pppoe.h" 76 #include "bridge.h" 77 #include "arp.h" 78 #include "agr.h" 79 80 #include <sys/param.h> 81 #include <sys/systm.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 #ifdef MBUFTRACE 216 m_claimm(m, ifp->if_mowner); 217 #endif 218 219 #if NCARP > 0 220 if (ifp->if_type == IFT_CARP) { 221 struct ifaddr *ifa; 222 223 /* loop back if this is going to the carp interface */ 224 if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP && 225 (ifa = ifa_ifwithaddr(dst)) != NULL && 226 ifa->ifa_ifp == ifp0) 227 return looutput(ifp0, m, dst, rt0); 228 229 ifp = ifp->if_carpdev; 230 /* ac = (struct arpcom *)ifp; */ 231 232 if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) != 233 (IFF_UP|IFF_RUNNING)) 234 senderr(ENETDOWN); 235 } 236 #endif /* NCARP > 0 */ 237 238 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 239 senderr(ENETDOWN); 240 if ((rt = rt0) != NULL) { 241 if ((rt->rt_flags & RTF_UP) == 0) { 242 if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) { 243 rt->rt_refcnt--; 244 if (rt->rt_ifp != ifp) 245 return (*rt->rt_ifp->if_output) 246 (ifp, m0, dst, rt); 247 } else 248 senderr(EHOSTUNREACH); 249 } 250 if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) { 251 if (rt->rt_gwroute == NULL) 252 goto lookup; 253 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 254 rtfree(rt); rt = rt0; 255 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1); 256 if ((rt = rt->rt_gwroute) == NULL) 257 senderr(EHOSTUNREACH); 258 /* the "G" test below also prevents rt == rt0 */ 259 if ((rt->rt_flags & RTF_GATEWAY) || 260 (rt->rt_ifp != ifp)) { 261 rt->rt_refcnt--; 262 rt0->rt_gwroute = NULL; 263 senderr(EHOSTUNREACH); 264 } 265 } 266 } 267 if (rt->rt_flags & RTF_REJECT) 268 if (rt->rt_rmx.rmx_expire == 0 || 269 (u_long) time_second < rt->rt_rmx.rmx_expire) 270 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 271 } 272 273 switch (dst->sa_family) { 274 275 #ifdef INET 276 case AF_INET: 277 if (m->m_flags & M_BCAST) 278 (void)memcpy(edst, etherbroadcastaddr, sizeof(edst)); 279 else if (m->m_flags & M_MCAST) 280 ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst); 281 else if (!arpresolve(ifp, rt, m, dst, edst)) 282 return (0); /* if not yet resolved */ 283 /* If broadcasting on a simplex interface, loopback a copy */ 284 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 285 mcopy = m_copy(m, 0, (int)M_COPYALL); 286 etype = htons(ETHERTYPE_IP); 287 break; 288 289 case AF_ARP: 290 ah = mtod(m, struct arphdr *); 291 if (m->m_flags & M_BCAST) 292 (void)memcpy(edst, etherbroadcastaddr, sizeof(edst)); 293 else { 294 void *tha = ar_tha(ah); 295 296 if (tha == NULL) { 297 /* fake with ARPHDR_IEEE1394 */ 298 return 0; 299 } 300 memcpy(edst, tha, sizeof(edst)); 301 } 302 303 ah->ar_hrd = htons(ARPHRD_ETHER); 304 305 switch (ntohs(ah->ar_op)) { 306 case ARPOP_REVREQUEST: 307 case ARPOP_REVREPLY: 308 etype = htons(ETHERTYPE_REVARP); 309 break; 310 311 case ARPOP_REQUEST: 312 case ARPOP_REPLY: 313 default: 314 etype = htons(ETHERTYPE_ARP); 315 } 316 317 break; 318 #endif 319 #ifdef INET6 320 case AF_INET6: 321 if (!nd6_storelladdr(ifp, rt, m, dst, edst, sizeof(edst))){ 322 /* something bad happened */ 323 return (0); 324 } 325 etype = htons(ETHERTYPE_IPV6); 326 break; 327 #endif 328 #ifdef NETATALK 329 case AF_APPLETALK: 330 if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) { 331 #ifdef NETATALKDEBUG 332 printf("aarpresolv failed\n"); 333 #endif /* NETATALKDEBUG */ 334 return (0); 335 } 336 /* 337 * ifaddr is the first thing in at_ifaddr 338 */ 339 aa = (struct at_ifaddr *) at_ifawithnet( 340 (const struct sockaddr_at *)dst, ifp); 341 if (aa == NULL) 342 goto bad; 343 344 /* 345 * In the phase 2 case, we need to prepend an mbuf for the 346 * llc header. Since we must preserve the value of m, 347 * which is passed to us by value, we m_copy() the first 348 * mbuf, and use it for our llc header. 349 */ 350 if (aa->aa_flags & AFA_PHASE2) { 351 struct llc llc; 352 353 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT); 354 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 355 llc.llc_control = LLC_UI; 356 memcpy(llc.llc_snap_org_code, at_org_code, 357 sizeof(llc.llc_snap_org_code)); 358 llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK); 359 memcpy(mtod(m, void *), &llc, sizeof(struct llc)); 360 } else { 361 etype = htons(ETHERTYPE_ATALK); 362 } 363 break; 364 #endif /* NETATALK */ 365 #ifdef IPX 366 case AF_IPX: 367 etype = htons(ETHERTYPE_IPX); 368 memcpy(edst, 369 &(((const struct sockaddr_ipx *)dst)->sipx_addr.x_host), 370 sizeof(edst)); 371 /* If broadcasting on a simplex interface, loopback a copy */ 372 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 373 mcopy = m_copy(m, 0, (int)M_COPYALL); 374 break; 375 #endif 376 case pseudo_AF_HDRCMPLT: 377 hdrcmplt = 1; 378 memcpy(esrc, 379 ((const struct ether_header *)dst->sa_data)->ether_shost, 380 sizeof(esrc)); 381 /* FALLTHROUGH */ 382 383 case AF_UNSPEC: 384 memcpy(edst, 385 ((const struct ether_header *)dst->sa_data)->ether_dhost, 386 sizeof(edst)); 387 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */ 388 etype = ((const struct ether_header *)dst->sa_data)->ether_type; 389 break; 390 391 default: 392 printf("%s: can't handle af%d\n", ifp->if_xname, 393 dst->sa_family); 394 senderr(EAFNOSUPPORT); 395 } 396 397 #ifdef MPLS 398 if (rt0 != NULL && rt_gettag(rt0) != NULL && 399 rt_gettag(rt0)->sa_family == AF_MPLS && 400 (m->m_flags & (M_MCAST | M_BCAST)) == 0) { 401 union mpls_shim msh; 402 msh.s_addr = MPLS_GETSADDR(rt0); 403 if (msh.shim.label != MPLS_LABEL_IMPLNULL) 404 etype = htons(ETHERTYPE_MPLS); 405 } 406 #endif 407 408 if (mcopy) 409 (void)looutput(ifp, mcopy, dst, rt); 410 411 /* If no ether type is set, this must be a 802.2 formatted packet. 412 */ 413 if (etype == 0) 414 etype = htons(m->m_pkthdr.len); 415 /* 416 * Add local net header. If no space in first mbuf, 417 * allocate another. 418 */ 419 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 420 if (m == 0) 421 senderr(ENOBUFS); 422 eh = mtod(m, struct ether_header *); 423 /* Note: etype is already in network byte order. */ 424 (void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type)); 425 memcpy(eh->ether_dhost, edst, sizeof(edst)); 426 if (hdrcmplt) 427 memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost)); 428 else 429 memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl), 430 sizeof(eh->ether_shost)); 431 432 #if NCARP > 0 433 if (ifp0 != ifp && ifp0->if_type == IFT_CARP) { 434 memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl), 435 sizeof(eh->ether_shost)); 436 } 437 #endif /* NCARP > 0 */ 438 439 #ifdef PFIL_HOOKS 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 #endif 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 struct ifqueue *inq; 580 uint16_t etype; 581 struct ether_header *eh; 582 size_t ehlen; 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 #ifdef PFIL_HOOKS 687 if ((m->m_flags & M_PROMISC) == 0) { 688 if (pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_IN) != 0) 689 return; 690 if (m == NULL) 691 return; 692 693 eh = mtod(m, struct ether_header *); 694 etype = ntohs(eh->ether_type); 695 ehlen = sizeof(*eh); 696 } 697 #endif 698 699 #if NAGR > 0 700 if (ifp->if_agrprivate && 701 __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) { 702 m->m_flags &= ~M_PROMISC; 703 agr_input(ifp, m); 704 return; 705 } 706 #endif /* NAGR > 0 */ 707 708 /* 709 * If VLANs are configured on the interface, check to 710 * see if the device performed the decapsulation and 711 * provided us with the tag. 712 */ 713 if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) { 714 #if NVLAN > 0 715 /* 716 * vlan_input() will either recursively call ether_input() 717 * or drop the packet. 718 */ 719 vlan_input(ifp, m); 720 #else 721 m_freem(m); 722 #endif 723 return; 724 } 725 726 /* 727 * Handle protocols that expect to have the Ethernet header 728 * (and possibly FCS) intact. 729 */ 730 switch (etype) { 731 case ETHERTYPE_VLAN: { 732 struct ether_vlan_header *evl = (void *)eh; 733 /* 734 * If there is a tag of 0, then the VLAN header was probably 735 * just being used to store the priority. Extract the ether 736 * type, and if IP or IPV6, let them deal with it. 737 */ 738 if (m->m_len <= sizeof(*evl) 739 && EVL_VLANOFTAG(evl->evl_tag) == 0) { 740 etype = ntohs(evl->evl_proto); 741 ehlen = sizeof(*evl); 742 if ((m->m_flags & M_PROMISC) == 0 743 && (etype == ETHERTYPE_IP 744 || etype == ETHERTYPE_IPV6)) 745 break; 746 } 747 #if NVLAN > 0 748 /* 749 * vlan_input() will either recursively call ether_input() 750 * or drop the packet. 751 */ 752 if (((struct ethercom *)ifp)->ec_nvlans != 0) 753 vlan_input(ifp, m); 754 else 755 #endif /* NVLAN > 0 */ 756 m_freem(m); 757 return; 758 } 759 #if NPPPOE > 0 760 case ETHERTYPE_PPPOEDISC: 761 case ETHERTYPE_PPPOE: 762 if (m->m_flags & M_PROMISC) { 763 m_freem(m); 764 return; 765 } 766 #ifndef PPPOE_SERVER 767 if (m->m_flags & (M_MCAST | M_BCAST)) { 768 m_freem(m); 769 return; 770 } 771 #endif 772 773 if (etype == ETHERTYPE_PPPOEDISC) 774 inq = &ppoediscinq; 775 else 776 inq = &ppoeinq; 777 if (IF_QFULL(inq)) { 778 IF_DROP(inq); 779 m_freem(m); 780 } else 781 IF_ENQUEUE(inq, m); 782 softint_schedule(pppoe_softintr); 783 return; 784 #endif /* NPPPOE > 0 */ 785 case ETHERTYPE_SLOWPROTOCOLS: { 786 uint8_t subtype; 787 788 #if defined(DIAGNOSTIC) 789 if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) { 790 panic("ether_input: too short slow protocol packet"); 791 } 792 #endif 793 m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype); 794 switch (subtype) { 795 #if NAGR > 0 796 case SLOWPROTOCOLS_SUBTYPE_LACP: 797 if (ifp->if_agrprivate) { 798 ieee8023ad_lacp_input(ifp, m); 799 return; 800 } 801 break; 802 803 case SLOWPROTOCOLS_SUBTYPE_MARKER: 804 if (ifp->if_agrprivate) { 805 ieee8023ad_marker_input(ifp, m); 806 return; 807 } 808 break; 809 #endif /* NAGR > 0 */ 810 default: 811 if (subtype == 0 || subtype > 10) { 812 /* illegal value */ 813 m_freem(m); 814 return; 815 } 816 /* unknown subtype */ 817 break; 818 } 819 /* FALLTHROUGH */ 820 } 821 default: 822 if (m->m_flags & M_PROMISC) { 823 m_freem(m); 824 return; 825 } 826 } 827 828 /* If the CRC is still on the packet, trim it off. */ 829 if (m->m_flags & M_HASFCS) { 830 m_adj(m, -ETHER_CRC_LEN); 831 m->m_flags &= ~M_HASFCS; 832 } 833 834 if (etype > ETHERMTU + sizeof (struct ether_header)) { 835 /* Strip off the Ethernet header. */ 836 m_adj(m, ehlen); 837 838 switch (etype) { 839 #ifdef INET 840 case ETHERTYPE_IP: 841 #ifdef GATEWAY 842 if (ipflow_fastforward(m)) 843 return; 844 #endif 845 schednetisr(NETISR_IP); 846 inq = &ipintrq; 847 break; 848 849 case ETHERTYPE_ARP: 850 schednetisr(NETISR_ARP); 851 inq = &arpintrq; 852 break; 853 854 case ETHERTYPE_REVARP: 855 revarpinput(m); /* XXX queue? */ 856 return; 857 #endif 858 #ifdef INET6 859 case ETHERTYPE_IPV6: 860 #ifdef GATEWAY 861 if (ip6flow_fastforward(&m)) 862 return; 863 #endif 864 schednetisr(NETISR_IPV6); 865 inq = &ip6intrq; 866 break; 867 #endif 868 #ifdef IPX 869 case ETHERTYPE_IPX: 870 schednetisr(NETISR_IPX); 871 inq = &ipxintrq; 872 break; 873 #endif 874 #ifdef NETATALK 875 case ETHERTYPE_ATALK: 876 schednetisr(NETISR_ATALK); 877 inq = &atintrq1; 878 break; 879 case ETHERTYPE_AARP: 880 /* probably this should be done with a NETISR as well */ 881 aarpinput(ifp, m); /* XXX */ 882 return; 883 #endif /* NETATALK */ 884 #ifdef MPLS 885 case ETHERTYPE_MPLS: 886 schednetisr(NETISR_MPLS); 887 inq = &mplsintrq; 888 break; 889 #endif 890 default: 891 m_freem(m); 892 return; 893 } 894 } else { 895 #if defined (LLC) || defined (NETATALK) 896 l = (struct llc *)(eh+1); 897 switch (l->llc_dsap) { 898 #ifdef NETATALK 899 case LLC_SNAP_LSAP: 900 switch (l->llc_control) { 901 case LLC_UI: 902 if (l->llc_ssap != LLC_SNAP_LSAP) { 903 goto dropanyway; 904 } 905 906 if (memcmp(&(l->llc_snap_org_code)[0], 907 at_org_code, sizeof(at_org_code)) == 0 && 908 ntohs(l->llc_snap_ether_type) == 909 ETHERTYPE_ATALK) { 910 inq = &atintrq2; 911 m_adj(m, sizeof(struct ether_header) 912 + sizeof(struct llc)); 913 schednetisr(NETISR_ATALK); 914 break; 915 } 916 917 if (memcmp(&(l->llc_snap_org_code)[0], 918 aarp_org_code, 919 sizeof(aarp_org_code)) == 0 && 920 ntohs(l->llc_snap_ether_type) == 921 ETHERTYPE_AARP) { 922 m_adj( m, sizeof(struct ether_header) 923 + sizeof(struct llc)); 924 aarpinput(ifp, m); /* XXX */ 925 return; 926 } 927 928 default: 929 goto dropanyway; 930 } 931 break; 932 dropanyway: 933 #endif 934 default: 935 m_freem(m); 936 return; 937 } 938 #else /* ISO || LLC || NETATALK*/ 939 m_freem(m); 940 return; 941 #endif /* ISO || LLC || NETATALK*/ 942 } 943 944 if (IF_QFULL(inq)) { 945 IF_DROP(inq); 946 m_freem(m); 947 } else 948 IF_ENQUEUE(inq, m); 949 } 950 951 /* 952 * Convert Ethernet address to printable (loggable) representation. 953 */ 954 char * 955 ether_sprintf(const u_char *ap) 956 { 957 static char etherbuf[3 * ETHER_ADDR_LEN]; 958 return ether_snprintf(etherbuf, sizeof(etherbuf), ap); 959 } 960 961 char * 962 ether_snprintf(char *buf, size_t len, const u_char *ap) 963 { 964 char *cp = buf; 965 size_t i; 966 967 for (i = 0; i < len / 3; i++) { 968 *cp++ = hexdigits[*ap >> 4]; 969 *cp++ = hexdigits[*ap++ & 0xf]; 970 *cp++ = ':'; 971 } 972 *--cp = '\0'; 973 return buf; 974 } 975 976 /* 977 * Perform common duties while attaching to interface list 978 */ 979 void 980 ether_ifattach(struct ifnet *ifp, const uint8_t *lla) 981 { 982 struct ethercom *ec = (struct ethercom *)ifp; 983 984 ifp->if_type = IFT_ETHER; 985 ifp->if_hdrlen = ETHER_HDR_LEN; 986 ifp->if_dlt = DLT_EN10MB; 987 ifp->if_mtu = ETHERMTU; 988 ifp->if_output = ether_output; 989 ifp->if_input = ether_input; 990 if (ifp->if_baudrate == 0) 991 ifp->if_baudrate = IF_Mbps(10); /* just a default */ 992 993 if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla)); 994 995 LIST_INIT(&ec->ec_multiaddrs); 996 ifp->if_broadcastaddr = etherbroadcastaddr; 997 bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header)); 998 #ifdef MBUFTRACE 999 strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname, 1000 sizeof(ec->ec_tx_mowner.mo_name)); 1001 strlcpy(ec->ec_tx_mowner.mo_descr, "tx", 1002 sizeof(ec->ec_tx_mowner.mo_descr)); 1003 strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname, 1004 sizeof(ec->ec_rx_mowner.mo_name)); 1005 strlcpy(ec->ec_rx_mowner.mo_descr, "rx", 1006 sizeof(ec->ec_rx_mowner.mo_descr)); 1007 MOWNER_ATTACH(&ec->ec_tx_mowner); 1008 MOWNER_ATTACH(&ec->ec_rx_mowner); 1009 ifp->if_mowner = &ec->ec_tx_mowner; 1010 #endif 1011 } 1012 1013 void 1014 ether_ifdetach(struct ifnet *ifp) 1015 { 1016 struct ethercom *ec = (void *) ifp; 1017 struct ether_multi *enm; 1018 int s; 1019 1020 /* 1021 * Prevent further calls to ioctl (for example turning off 1022 * promiscuous mode from the bridge code), which eventually can 1023 * call if_init() which can cause panics because the interface 1024 * is in the process of being detached. Return device not configured 1025 * instead. 1026 */ 1027 ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio; 1028 1029 #if NBRIDGE > 0 1030 if (ifp->if_bridge) 1031 bridge_ifdetach(ifp); 1032 #endif 1033 1034 bpf_detach(ifp); 1035 1036 #if NVLAN > 0 1037 if (ec->ec_nvlans) 1038 vlan_ifdetach(ifp); 1039 #endif 1040 1041 s = splnet(); 1042 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) { 1043 LIST_REMOVE(enm, enm_list); 1044 free(enm, M_IFMADDR); 1045 ec->ec_multicnt--; 1046 } 1047 splx(s); 1048 1049 #if 0 /* done in if_detach() */ 1050 if_free_sadl(ifp); 1051 #endif 1052 1053 ifp->if_mowner = NULL; 1054 MOWNER_DETACH(&ec->ec_rx_mowner); 1055 MOWNER_DETACH(&ec->ec_tx_mowner); 1056 } 1057 1058 #if 0 1059 /* 1060 * This is for reference. We have a table-driven version 1061 * of the little-endian crc32 generator, which is faster 1062 * than the double-loop. 1063 */ 1064 uint32_t 1065 ether_crc32_le(const uint8_t *buf, size_t len) 1066 { 1067 uint32_t c, crc, carry; 1068 size_t i, j; 1069 1070 crc = 0xffffffffU; /* initial value */ 1071 1072 for (i = 0; i < len; i++) { 1073 c = buf[i]; 1074 for (j = 0; j < 8; j++) { 1075 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 1076 crc >>= 1; 1077 c >>= 1; 1078 if (carry) 1079 crc = (crc ^ ETHER_CRC_POLY_LE); 1080 } 1081 } 1082 1083 return (crc); 1084 } 1085 #else 1086 uint32_t 1087 ether_crc32_le(const uint8_t *buf, size_t len) 1088 { 1089 static const uint32_t crctab[] = { 1090 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 1091 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 1092 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 1093 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 1094 }; 1095 uint32_t crc; 1096 size_t i; 1097 1098 crc = 0xffffffffU; /* initial value */ 1099 1100 for (i = 0; i < len; i++) { 1101 crc ^= buf[i]; 1102 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1103 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1104 } 1105 1106 return (crc); 1107 } 1108 #endif 1109 1110 uint32_t 1111 ether_crc32_be(const uint8_t *buf, size_t len) 1112 { 1113 uint32_t c, crc, carry; 1114 size_t i, j; 1115 1116 crc = 0xffffffffU; /* initial value */ 1117 1118 for (i = 0; i < len; i++) { 1119 c = buf[i]; 1120 for (j = 0; j < 8; j++) { 1121 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 1122 crc <<= 1; 1123 c >>= 1; 1124 if (carry) 1125 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 1126 } 1127 } 1128 1129 return (crc); 1130 } 1131 1132 #ifdef INET 1133 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] = 1134 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 1135 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] = 1136 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 1137 #endif 1138 #ifdef INET6 1139 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] = 1140 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 1141 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] = 1142 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff }; 1143 #endif 1144 1145 /* 1146 * ether_aton implementation, not using a static buffer. 1147 */ 1148 int 1149 ether_aton_r(u_char *dest, size_t len, const char *str) 1150 { 1151 const u_char *cp = (const void *)str; 1152 u_char *ep; 1153 1154 #define atox(c) (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10)) 1155 1156 if (len < ETHER_ADDR_LEN) 1157 return ENOSPC; 1158 1159 ep = dest + ETHER_ADDR_LEN; 1160 1161 while (*cp) { 1162 if (!isxdigit(*cp)) 1163 return EINVAL; 1164 *dest = atox(*cp); 1165 cp++; 1166 if (isxdigit(*cp)) { 1167 *dest = (*dest << 4) | atox(*cp); 1168 dest++; 1169 cp++; 1170 } else 1171 dest++; 1172 if (dest == ep) 1173 return *cp == '\0' ? 0 : ENAMETOOLONG; 1174 switch (*cp) { 1175 case ':': 1176 case '-': 1177 case '.': 1178 cp++; 1179 break; 1180 } 1181 } 1182 return ENOBUFS; 1183 } 1184 1185 /* 1186 * Convert a sockaddr into an Ethernet address or range of Ethernet 1187 * addresses. 1188 */ 1189 int 1190 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN], 1191 uint8_t addrhi[ETHER_ADDR_LEN]) 1192 { 1193 #ifdef INET 1194 const struct sockaddr_in *sin; 1195 #endif /* INET */ 1196 #ifdef INET6 1197 const struct sockaddr_in6 *sin6; 1198 #endif /* INET6 */ 1199 1200 switch (sa->sa_family) { 1201 1202 case AF_UNSPEC: 1203 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN); 1204 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 1205 break; 1206 1207 #ifdef INET 1208 case AF_INET: 1209 sin = satocsin(sa); 1210 if (sin->sin_addr.s_addr == INADDR_ANY) { 1211 /* 1212 * An IP address of INADDR_ANY means listen to 1213 * or stop listening to all of the Ethernet 1214 * multicast addresses used for IP. 1215 * (This is for the sake of IP multicast routers.) 1216 */ 1217 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN); 1218 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN); 1219 } 1220 else { 1221 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 1222 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 1223 } 1224 break; 1225 #endif 1226 #ifdef INET6 1227 case AF_INET6: 1228 sin6 = satocsin6(sa); 1229 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1230 /* 1231 * An IP6 address of 0 means listen to or stop 1232 * listening to all of the Ethernet multicast 1233 * address used for IP6. 1234 * (This is used for multicast routers.) 1235 */ 1236 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN); 1237 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN); 1238 } else { 1239 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 1240 memcpy(addrhi, addrlo, ETHER_ADDR_LEN); 1241 } 1242 break; 1243 #endif 1244 1245 default: 1246 return EAFNOSUPPORT; 1247 } 1248 return 0; 1249 } 1250 1251 /* 1252 * Add an Ethernet multicast address or range of addresses to the list for a 1253 * given interface. 1254 */ 1255 int 1256 ether_addmulti(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 * Verify that we have valid Ethernet multicast addresses. 1271 */ 1272 if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) { 1273 splx(s); 1274 return EINVAL; 1275 } 1276 /* 1277 * See if the address range is already in the list. 1278 */ 1279 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm); 1280 if (enm != NULL) { 1281 /* 1282 * Found it; just increment the reference count. 1283 */ 1284 ++enm->enm_refcount; 1285 splx(s); 1286 return 0; 1287 } 1288 /* 1289 * New address or range; malloc a new multicast record 1290 * and link it into the interface's multicast list. 1291 */ 1292 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); 1293 if (enm == NULL) { 1294 splx(s); 1295 return ENOBUFS; 1296 } 1297 memcpy(enm->enm_addrlo, addrlo, 6); 1298 memcpy(enm->enm_addrhi, addrhi, 6); 1299 enm->enm_refcount = 1; 1300 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list); 1301 ec->ec_multicnt++; 1302 splx(s); 1303 /* 1304 * Return ENETRESET to inform the driver that the list has changed 1305 * and its reception filter should be adjusted accordingly. 1306 */ 1307 return ENETRESET; 1308 } 1309 1310 /* 1311 * Delete a multicast address record. 1312 */ 1313 int 1314 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec) 1315 { 1316 struct ether_multi *enm; 1317 u_char addrlo[ETHER_ADDR_LEN]; 1318 u_char addrhi[ETHER_ADDR_LEN]; 1319 int s = splnet(), error; 1320 1321 error = ether_multiaddr(sa, addrlo, addrhi); 1322 if (error != 0) { 1323 splx(s); 1324 return (error); 1325 } 1326 1327 /* 1328 * Look ur the address in our list. 1329 */ 1330 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm); 1331 if (enm == NULL) { 1332 splx(s); 1333 return (ENXIO); 1334 } 1335 if (--enm->enm_refcount != 0) { 1336 /* 1337 * Still some claims to this record. 1338 */ 1339 splx(s); 1340 return (0); 1341 } 1342 /* 1343 * No remaining claims to this record; unlink and free it. 1344 */ 1345 LIST_REMOVE(enm, enm_list); 1346 free(enm, M_IFMADDR); 1347 ec->ec_multicnt--; 1348 splx(s); 1349 /* 1350 * Return ENETRESET to inform the driver that the list has changed 1351 * and its reception filter should be adjusted accordingly. 1352 */ 1353 return (ENETRESET); 1354 } 1355 1356 void 1357 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb) 1358 { 1359 ec->ec_ifflags_cb = cb; 1360 } 1361 1362 /* 1363 * Common ioctls for Ethernet interfaces. Note, we must be 1364 * called at splnet(). 1365 */ 1366 int 1367 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1368 { 1369 struct ethercom *ec = (void *) ifp; 1370 struct eccapreq *eccr; 1371 struct ifreq *ifr = (struct ifreq *)data; 1372 struct if_laddrreq *iflr = data; 1373 const struct sockaddr_dl *sdl; 1374 static const uint8_t zero[ETHER_ADDR_LEN]; 1375 int error; 1376 1377 switch (cmd) { 1378 case SIOCINITIFADDR: 1379 { 1380 struct ifaddr *ifa = (struct ifaddr *)data; 1381 if (ifa->ifa_addr->sa_family != AF_LINK 1382 && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) != 1383 (IFF_UP|IFF_RUNNING)) { 1384 ifp->if_flags |= IFF_UP; 1385 if ((error = (*ifp->if_init)(ifp)) != 0) 1386 return error; 1387 } 1388 #ifdef INET 1389 if (ifa->ifa_addr->sa_family == AF_INET) 1390 arp_ifinit(ifp, ifa); 1391 #endif /* INET */ 1392 return 0; 1393 } 1394 1395 case SIOCSIFMTU: 1396 { 1397 int maxmtu; 1398 1399 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU) 1400 maxmtu = ETHERMTU_JUMBO; 1401 else 1402 maxmtu = ETHERMTU; 1403 1404 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu) 1405 return EINVAL; 1406 else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET) 1407 return error; 1408 else if (ifp->if_flags & IFF_UP) { 1409 /* Make sure the device notices the MTU change. */ 1410 return (*ifp->if_init)(ifp); 1411 } else 1412 return 0; 1413 } 1414 1415 case SIOCSIFFLAGS: 1416 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 1417 return error; 1418 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 1419 case IFF_RUNNING: 1420 /* 1421 * If interface is marked down and it is running, 1422 * then stop and disable it. 1423 */ 1424 (*ifp->if_stop)(ifp, 1); 1425 break; 1426 case IFF_UP: 1427 /* 1428 * If interface is marked up and it is stopped, then 1429 * start it. 1430 */ 1431 return (*ifp->if_init)(ifp); 1432 case IFF_UP|IFF_RUNNING: 1433 error = 0; 1434 if (ec->ec_ifflags_cb == NULL || 1435 (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) { 1436 /* 1437 * Reset the interface to pick up 1438 * changes in any other flags that 1439 * affect the hardware state. 1440 */ 1441 return (*ifp->if_init)(ifp); 1442 } else 1443 return error; 1444 case 0: 1445 break; 1446 } 1447 return 0; 1448 case SIOCGETHERCAP: 1449 eccr = (struct eccapreq *)data; 1450 eccr->eccr_capabilities = ec->ec_capabilities; 1451 eccr->eccr_capenable = ec->ec_capenable; 1452 return 0; 1453 case SIOCADDMULTI: 1454 return ether_addmulti(ifreq_getaddr(cmd, ifr), ec); 1455 case SIOCDELMULTI: 1456 return ether_delmulti(ifreq_getaddr(cmd, ifr), ec); 1457 case SIOCSIFMEDIA: 1458 case SIOCGIFMEDIA: 1459 if (ec->ec_mii == NULL) 1460 return ENOTTY; 1461 return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd); 1462 case SIOCALIFADDR: 1463 sdl = satocsdl(sstocsa(&iflr->addr)); 1464 if (sdl->sdl_family != AF_LINK) 1465 ; 1466 else if (ETHER_IS_MULTICAST(CLLADDR(sdl))) 1467 return EINVAL; 1468 else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0) 1469 return EINVAL; 1470 /*FALLTHROUGH*/ 1471 default: 1472 return ifioctl_common(ifp, cmd, data); 1473 } 1474 return 0; 1475 } 1476