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