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