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