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