1 /* $OpenBSD: if_ethersubr.c,v 1.132 2009/03/05 19:47:05 michele Exp $ */ 2 /* $NetBSD: if_ethersubr.c,v 1.19 1996/05/07 02:40:30 thorpej Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1989, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 62 */ 63 64 /* 65 %%% portions-copyright-nrl-95 66 Portions of this software are Copyright 1995-1998 by Randall Atkinson, 67 Ronald Lee, Daniel McDonald, Bao Phan, and Chris Winters. All Rights 68 Reserved. All rights under this copyright have been assigned to the US 69 Naval Research Laboratory (NRL). The NRL Copyright Notice and License 70 Agreement Version 1.1 (January 17, 1995) applies to these portions of the 71 software. 72 You should have received a copy of the license with this software. If you 73 didn't get a copy, you may request one from <license@ipv6.nrl.navy.mil>. 74 */ 75 76 #include "bpfilter.h" 77 78 #include <sys/param.h> 79 #include <sys/systm.h> 80 #include <sys/kernel.h> 81 #include <sys/malloc.h> 82 #include <sys/mbuf.h> 83 #include <sys/protosw.h> 84 #include <sys/socket.h> 85 #include <sys/ioctl.h> 86 #include <sys/errno.h> 87 #include <sys/syslog.h> 88 #include <sys/timeout.h> 89 90 #include <machine/cpu.h> 91 92 #include <net/if.h> 93 #include <net/netisr.h> 94 #include <net/route.h> 95 #include <net/if_llc.h> 96 #include <net/if_dl.h> 97 #include <net/if_media.h> 98 #include <net/if_types.h> 99 100 #include <netinet/in.h> 101 #ifdef INET 102 #include <netinet/in_var.h> 103 #endif 104 #include <netinet/if_ether.h> 105 #include <netinet/ip_ipsp.h> 106 107 #include <dev/rndvar.h> 108 109 #if NBPFILTER > 0 110 #include <net/bpf.h> 111 #endif 112 113 #include "bridge.h" 114 #if NBRIDGE > 0 115 #include <net/if_bridge.h> 116 #endif 117 118 #include "vlan.h" 119 #if NVLAN > 0 120 #include <net/if_vlan_var.h> 121 #endif /* NVLAN > 0 */ 122 123 #include "carp.h" 124 #if NCARP > 0 125 #include <netinet/ip_carp.h> 126 #endif 127 128 #include "pppoe.h" 129 #if NPPPOE > 0 130 #include <net/if_pppoe.h> 131 #endif 132 133 #include "trunk.h" 134 #if NTRUNK > 0 135 #include <net/if_trunk.h> 136 #endif 137 138 #ifdef AOE 139 #include <net/if_aoe.h> 140 #endif /* AOE */ 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 #ifdef NETATALK 151 #include <netatalk/at.h> 152 #include <netatalk/at_var.h> 153 #include <netatalk/at_extern.h> 154 155 extern u_char at_org_code[ 3 ]; 156 extern u_char aarp_org_code[ 3 ]; 157 #endif /* NETATALK */ 158 159 #ifdef MPLS 160 #include <netmpls/mpls.h> 161 #endif /* MPLS */ 162 163 u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 164 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 165 #define senderr(e) { error = (e); goto bad;} 166 167 168 int 169 ether_ioctl(struct ifnet *ifp, struct arpcom *arp, u_long cmd, caddr_t data) 170 { 171 struct ifaddr *ifa = (struct ifaddr *)data; 172 struct ifreq *ifr = (struct ifreq *)data; 173 int error = 0; 174 175 switch (cmd) { 176 case SIOCSIFADDR: 177 switch (ifa->ifa_addr->sa_family) { 178 #ifdef NETATALK 179 case AF_APPLETALK: 180 /* Nothing to do. */ 181 break; 182 #endif /* NETATALK */ 183 } 184 break; 185 186 case SIOCSIFMTU: 187 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ifp->if_hardmtu) 188 error = EINVAL; 189 else 190 ifp->if_mtu = ifr->ifr_mtu; 191 break; 192 193 case SIOCADDMULTI: 194 case SIOCDELMULTI: 195 if (ifp->if_flags & IFF_MULTICAST) { 196 error = (cmd == SIOCADDMULTI) ? 197 ether_addmulti(ifr, arp) : 198 ether_delmulti(ifr, arp); 199 } else 200 error = ENOTTY; 201 break; 202 203 default: 204 error = ENOTTY; 205 } 206 207 return (error); 208 } 209 210 /* 211 * Ethernet output routine. 212 * Encapsulate a packet of type family for the local net. 213 * Assumes that ifp is actually pointer to arpcom structure. 214 */ 215 int 216 ether_output(ifp0, m0, dst, rt0) 217 struct ifnet *ifp0; 218 struct mbuf *m0; 219 struct sockaddr *dst; 220 struct rtentry *rt0; 221 { 222 u_int16_t etype; 223 int s, len, error = 0, hdrcmplt = 0; 224 u_char edst[ETHER_ADDR_LEN], esrc[ETHER_ADDR_LEN]; 225 struct mbuf *m = m0; 226 struct rtentry *rt; 227 struct mbuf *mcopy = (struct mbuf *)0; 228 struct ether_header *eh; 229 struct arpcom *ac = (struct arpcom *)ifp0; 230 short mflags; 231 struct ifnet *ifp = ifp0; 232 233 #if NTRUNK > 0 234 if (ifp->if_type == IFT_IEEE8023ADLAG) 235 senderr(EBUSY); 236 #endif 237 238 #if NCARP > 0 239 if (ifp->if_type == IFT_CARP) { 240 struct ifaddr *ifa; 241 242 /* loop back if this is going to the carp interface */ 243 if (dst != NULL && LINK_STATE_IS_UP(ifp0->if_link_state) && 244 (ifa = ifa_ifwithaddr(dst)) != NULL && 245 ifa->ifa_ifp == ifp0) 246 return (looutput(ifp0, m, dst, rt0)); 247 248 ifp = ifp->if_carpdev; 249 ac = (struct arpcom *)ifp; 250 251 if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) != 252 (IFF_UP|IFF_RUNNING)) 253 senderr(ENETDOWN); 254 } 255 #endif /* NCARP > 0 */ 256 257 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 258 senderr(ENETDOWN); 259 if ((rt = rt0) != NULL) { 260 if ((rt->rt_flags & RTF_UP) == 0) { 261 if ((rt0 = rt = rtalloc1(dst, 1, 0)) != NULL) 262 rt->rt_refcnt--; 263 else 264 senderr(EHOSTUNREACH); 265 } 266 #ifdef MPLS 267 if (rt->rt_flags & RTF_MPLS) { 268 if ((m = mpls_output(m, rt)) == NULL) 269 senderr(EHOSTUNREACH); 270 } 271 #endif 272 if (rt->rt_flags & RTF_GATEWAY) { 273 if (rt->rt_gwroute == 0) 274 goto lookup; 275 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 276 rtfree(rt); rt = rt0; 277 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0); 278 if ((rt = rt->rt_gwroute) == 0) 279 senderr(EHOSTUNREACH); 280 } 281 } 282 if (rt->rt_flags & RTF_REJECT) 283 if (rt->rt_rmx.rmx_expire == 0 || 284 time_second < rt->rt_rmx.rmx_expire) 285 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 286 } 287 288 switch (dst->sa_family) { 289 290 #ifdef INET 291 case AF_INET: 292 if (!arpresolve(ac, rt, m, dst, edst)) 293 return (0); /* if not yet resolved */ 294 /* If broadcasting on a simplex interface, loopback a copy */ 295 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) && 296 !m->m_pkthdr.pf.routed) 297 mcopy = m_copy(m, 0, (int)M_COPYALL); 298 #ifdef MPLS 299 if (rt0 != NULL && rt0->rt_flags & RTF_MPLS) 300 etype = htons(ETHERTYPE_MPLS); 301 else 302 #endif 303 etype = htons(ETHERTYPE_IP); 304 break; 305 #endif 306 #ifdef INET6 307 case AF_INET6: 308 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)) 309 return (0); /* it must be impossible, but... */ 310 etype = htons(ETHERTYPE_IPV6); 311 break; 312 #endif 313 #ifdef NETATALK 314 case AF_APPLETALK: { 315 struct at_ifaddr *aa; 316 317 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) { 318 #ifdef NETATALKDEBUG 319 extern char *prsockaddr(struct sockaddr *); 320 printf("aarpresolv: failed for %s\n", prsockaddr(dst)); 321 #endif /* NETATALKDEBUG */ 322 return (0); 323 } 324 325 /* 326 * ifaddr is the first thing in at_ifaddr 327 */ 328 aa = (struct at_ifaddr *)at_ifawithnet( 329 (struct sockaddr_at *)dst, 330 TAILQ_FIRST(&ifp->if_addrlist)); 331 if (aa == 0) 332 goto bad; 333 334 /* 335 * In the phase 2 case, we need to prepend an mbuf for the llc 336 * header. Since we must preserve the value of m, which is 337 * passed to us by value, we m_copy() the first mbuf, 338 * and use it for our llc header. 339 */ 340 if (aa->aa_flags & AFA_PHASE2) { 341 struct llc llc; 342 343 M_PREPEND(m, AT_LLC_SIZE, M_DONTWAIT); 344 if (m == NULL) 345 return (0); 346 /* 347 * FreeBSD doesn't count the LLC len in 348 * ifp->obytes, so they increment a length 349 * field here. We don't do this. 350 */ 351 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 352 llc.llc_control = LLC_UI; 353 bcopy(at_org_code, llc.llc_snap.org_code, 354 sizeof(at_org_code)); 355 llc.llc_snap.ether_type = htons( ETHERTYPE_AT ); 356 bcopy(&llc, mtod(m, caddr_t), AT_LLC_SIZE); 357 etype = htons(m->m_pkthdr.len); 358 } else { 359 etype = htons(ETHERTYPE_AT); 360 } 361 } break; 362 #endif /* NETATALK */ 363 #ifdef MPLS 364 case AF_MPLS: 365 if (rt) 366 dst = rt_key(rt); 367 else 368 senderr(EHOSTUNREACH); 369 370 switch (dst->sa_family) { 371 case AF_LINK: 372 if (((struct sockaddr_dl *)dst)->sdl_alen < 373 sizeof(edst)) 374 senderr(EHOSTUNREACH); 375 bcopy(LLADDR(((struct sockaddr_dl *)dst)), edst, 376 sizeof(edst)); 377 break; 378 case AF_INET: 379 if (!arpresolve(ac, rt, m, dst, edst)) 380 return (0); /* if not yet resolved */ 381 break; 382 default: 383 senderr(EHOSTUNREACH); 384 } 385 /* XXX handling for simplex devices in case of M/BCAST ?? */ 386 if (m->m_flags & (M_BCAST | M_MCAST)) 387 etype = htons(ETHERTYPE_MPLS_MCAST); 388 else 389 etype = htons(ETHERTYPE_MPLS); 390 break; 391 #endif /* MPLS */ 392 case pseudo_AF_HDRCMPLT: 393 hdrcmplt = 1; 394 eh = (struct ether_header *)dst->sa_data; 395 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof(esrc)); 396 /* FALLTHROUGH */ 397 398 case AF_UNSPEC: 399 eh = (struct ether_header *)dst->sa_data; 400 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof(edst)); 401 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */ 402 etype = eh->ether_type; 403 break; 404 405 default: 406 printf("%s: can't handle af%d\n", ifp->if_xname, 407 dst->sa_family); 408 senderr(EAFNOSUPPORT); 409 } 410 411 /* XXX Should we feed-back an unencrypted IPsec packet ? */ 412 if (mcopy) 413 (void) looutput(ifp, mcopy, dst, rt); 414 415 /* 416 * Add local net header. If no space in first mbuf, 417 * allocate another. 418 */ 419 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 420 if (m == 0) 421 senderr(ENOBUFS); 422 eh = mtod(m, struct ether_header *); 423 bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type, 424 sizeof(eh->ether_type)); 425 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof(edst)); 426 if (hdrcmplt) 427 bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost, 428 sizeof(eh->ether_shost)); 429 else 430 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 431 sizeof(eh->ether_shost)); 432 433 #if NCARP > 0 434 if (ifp0 != ifp && ifp0->if_type == IFT_CARP) 435 carp_rewrite_lladdr(ifp0, eh->ether_shost); 436 #endif 437 438 #if NBRIDGE > 0 439 /* 440 * Interfaces that are bridge members need special handling 441 * for output. 442 */ 443 if (ifp->if_bridge) { 444 struct m_tag *mtag; 445 446 /* 447 * Check if this packet has already been sent out through 448 * this bridge, in which case we simply send it out 449 * without further bridge processing. 450 */ 451 for (mtag = m_tag_find(m, PACKET_TAG_BRIDGE, NULL); mtag; 452 mtag = m_tag_find(m, PACKET_TAG_BRIDGE, mtag)) { 453 #ifdef DEBUG 454 /* Check that the information is there */ 455 if (mtag->m_tag_len != sizeof(caddr_t)) { 456 error = EINVAL; 457 goto bad; 458 } 459 #endif 460 if (!bcmp(&ifp->if_bridge, mtag + 1, sizeof(caddr_t))) 461 break; 462 } 463 if (mtag == NULL) { 464 /* Attach a tag so we can detect loops */ 465 mtag = m_tag_get(PACKET_TAG_BRIDGE, sizeof(caddr_t), 466 M_NOWAIT); 467 if (mtag == NULL) { 468 error = ENOBUFS; 469 goto bad; 470 } 471 bcopy(&ifp->if_bridge, mtag + 1, sizeof(caddr_t)); 472 m_tag_prepend(m, mtag); 473 error = bridge_output(ifp, m, NULL, NULL); 474 return (error); 475 } 476 } 477 #endif 478 479 mflags = m->m_flags; 480 len = m->m_pkthdr.len; 481 s = splnet(); 482 /* 483 * Queue message on interface, and start output if interface 484 * not yet active. 485 */ 486 IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error); 487 if (error) { 488 /* mbuf is already freed */ 489 splx(s); 490 return (error); 491 } 492 ifp->if_obytes += len; 493 #if NCARP > 0 494 if (ifp != ifp0) 495 ifp0->if_obytes += len; 496 #endif /* NCARP > 0 */ 497 if (mflags & M_MCAST) 498 ifp->if_omcasts++; 499 if_start(ifp); 500 splx(s); 501 return (error); 502 503 bad: 504 if (m) 505 m_freem(m); 506 return (error); 507 } 508 509 /* 510 * Process a received Ethernet packet; 511 * the packet is in the mbuf chain m without 512 * the ether header, which is provided separately. 513 */ 514 void 515 ether_input(ifp0, eh, m) 516 struct ifnet *ifp0; 517 struct ether_header *eh; 518 struct mbuf *m; 519 { 520 struct ifqueue *inq; 521 u_int16_t etype; 522 int s, llcfound = 0; 523 struct llc *l; 524 struct arpcom *ac; 525 struct ifnet *ifp = ifp0; 526 #if NTRUNK > 0 527 int i = 0; 528 #endif 529 #if NPPPOE > 0 530 struct ether_header *eh_tmp; 531 #endif 532 533 m_cluncount(m, 1); 534 535 if (eh == NULL) { 536 eh = mtod(m, struct ether_header *); 537 m_adj(m, ETHER_HDR_LEN); 538 } 539 540 #if NTRUNK > 0 541 /* Handle input from a trunk port */ 542 while (ifp->if_type == IFT_IEEE8023ADLAG) { 543 if (++i > TRUNK_MAX_STACKING) { 544 m_freem(m); 545 return; 546 } 547 if (trunk_input(ifp, eh, m) != 0) 548 return; 549 550 /* Has been set to the trunk interface */ 551 ifp = m->m_pkthdr.rcvif; 552 } 553 #endif 554 555 if ((ifp->if_flags & IFF_UP) == 0) { 556 m_freem(m); 557 return; 558 } 559 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 560 if ((ifp->if_flags & IFF_SIMPLEX) == 0) { 561 struct ifaddr *ifa; 562 struct sockaddr_dl *sdl = NULL; 563 564 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 565 if ((sdl = 566 (struct sockaddr_dl *)ifa->ifa_addr) && 567 sdl->sdl_family == AF_LINK) 568 break; 569 } 570 /* 571 * If this is not a simplex interface, drop the packet 572 * if it came from us. 573 */ 574 if (sdl && bcmp(LLADDR(sdl), eh->ether_shost, 575 ETHER_ADDR_LEN) == 0) { 576 m_freem(m); 577 return; 578 } 579 } 580 581 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 582 sizeof(etherbroadcastaddr)) == 0) 583 m->m_flags |= M_BCAST; 584 else 585 m->m_flags |= M_MCAST; 586 ifp->if_imcasts++; 587 #if NTRUNK > 0 588 if (ifp != ifp0) 589 ifp0->if_imcasts++; 590 #endif 591 } 592 593 ifp->if_ibytes += m->m_pkthdr.len + sizeof(*eh); 594 #if NTRUNK > 0 595 if (ifp != ifp0) 596 ifp0->if_ibytes += m->m_pkthdr.len + sizeof(*eh); 597 #endif 598 599 etype = ntohs(eh->ether_type); 600 601 if (!(netisr & (1 << NETISR_RND_DONE))) { 602 add_net_randomness(etype); 603 atomic_setbits_int(&netisr, (1 << NETISR_RND_DONE)); 604 } 605 606 #if NVLAN > 0 607 if (((m->m_flags & M_VLANTAG) || etype == ETHERTYPE_VLAN) 608 && (vlan_input(eh, m) == 0)) 609 return; 610 #endif 611 612 #if NBRIDGE > 0 613 /* 614 * Tap the packet off here for a bridge, if configured and 615 * active for this interface. bridge_input returns 616 * NULL if it has consumed the packet, otherwise, it 617 * gets processed as normal. 618 */ 619 if (ifp->if_bridge) { 620 if (m->m_flags & M_PROTO1) 621 m->m_flags &= ~M_PROTO1; 622 else { 623 m = bridge_input(ifp, eh, m); 624 if (m == NULL) 625 return; 626 /* The bridge has determined it's for us. */ 627 ifp = m->m_pkthdr.rcvif; 628 } 629 } 630 #endif 631 632 #if NVLAN > 0 633 if ((m->m_flags & M_VLANTAG) || etype == ETHERTYPE_VLAN) { 634 /* The bridge did not want the vlan frame either, drop it. */ 635 ifp->if_noproto++; 636 m_freem(m); 637 return; 638 } 639 #endif /* NVLAN > 0 */ 640 641 #if NCARP > 0 642 if (ifp->if_carp) { 643 if (ifp->if_type != IFT_CARP && 644 (carp_input(m, (u_int8_t *)&eh->ether_shost, 645 (u_int8_t *)&eh->ether_dhost, eh->ether_type) == 0)) 646 return; 647 /* clear mcast if received on a carp IP balanced address */ 648 else if (ifp->if_type == IFT_CARP && 649 m->m_flags & (M_BCAST|M_MCAST) && 650 carp_our_mcastaddr(ifp, (u_int8_t *)&eh->ether_dhost)) 651 m->m_flags &= ~(M_BCAST|M_MCAST); 652 } 653 #endif /* NCARP > 0 */ 654 655 ac = (struct arpcom *)ifp; 656 657 /* 658 * If packet has been filtered by the bpf listener, drop it now 659 */ 660 if (m->m_flags & M_FILDROP) { 661 m_free(m); 662 return; 663 } 664 665 /* 666 * If packet is unicast and we're in promiscuous mode, make sure it 667 * is for us. Drop otherwise. 668 */ 669 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0 && 670 (ifp->if_flags & IFF_PROMISC)) { 671 if (bcmp(ac->ac_enaddr, (caddr_t)eh->ether_dhost, 672 ETHER_ADDR_LEN)) { 673 m_freem(m); 674 return; 675 } 676 } 677 678 /* 679 * Schedule softnet interrupt and enqueue packet within the same spl. 680 */ 681 s = splnet(); 682 decapsulate: 683 684 switch (etype) { 685 #ifdef INET 686 case ETHERTYPE_IP: 687 schednetisr(NETISR_IP); 688 inq = &ipintrq; 689 break; 690 691 case ETHERTYPE_ARP: 692 if (ifp->if_flags & IFF_NOARP) 693 goto dropanyway; 694 schednetisr(NETISR_ARP); 695 inq = &arpintrq; 696 break; 697 698 case ETHERTYPE_REVARP: 699 if (ifp->if_flags & IFF_NOARP) 700 goto dropanyway; 701 revarpinput(m); /* XXX queue? */ 702 goto done; 703 704 #endif 705 #ifdef INET6 706 /* 707 * Schedule IPv6 software interrupt for incoming IPv6 packet. 708 */ 709 case ETHERTYPE_IPV6: 710 schednetisr(NETISR_IPV6); 711 inq = &ip6intrq; 712 break; 713 #endif /* INET6 */ 714 #ifdef NETATALK 715 case ETHERTYPE_AT: 716 schednetisr(NETISR_ATALK); 717 inq = &atintrq1; 718 break; 719 case ETHERTYPE_AARP: 720 /* probably this should be done with a NETISR as well */ 721 /* XXX queue this */ 722 aarpinput((struct arpcom *)ifp, m); 723 goto done; 724 #endif 725 #if NPPPOE > 0 726 case ETHERTYPE_PPPOEDISC: 727 case ETHERTYPE_PPPOE: 728 /* XXX we dont have this flag */ 729 /* 730 if (m->m_flags & M_PROMISC) { 731 m_freem(m); 732 goto done; 733 } 734 */ 735 #ifndef PPPOE_SERVER 736 if (m->m_flags & (M_MCAST | M_BCAST)) { 737 m_freem(m); 738 goto done; 739 } 740 #endif 741 M_PREPEND(m, sizeof(*eh), M_DONTWAIT); 742 if (m == NULL) 743 goto done; 744 745 eh_tmp = mtod(m, struct ether_header *); 746 bcopy(eh, eh_tmp, sizeof(struct ether_header)); 747 748 if (etype == ETHERTYPE_PPPOEDISC) 749 inq = &pppoediscinq; 750 else 751 inq = &pppoeinq; 752 753 schednetisr(NETISR_PPPOE); 754 break; 755 #endif /* NPPPOE > 0 */ 756 #ifdef AOE 757 case ETHERTYPE_AOE: 758 aoe_input(ifp, m); 759 goto done; 760 #endif /* AOE */ 761 #ifdef MPLS 762 case ETHERTYPE_MPLS: 763 case ETHERTYPE_MPLS_MCAST: 764 inq = &mplsintrq; 765 schednetisr(NETISR_MPLS); 766 break; 767 #endif 768 default: 769 if (llcfound || etype > ETHERMTU) 770 goto dropanyway; 771 llcfound = 1; 772 l = mtod(m, struct llc *); 773 switch (l->llc_dsap) { 774 case LLC_SNAP_LSAP: 775 #ifdef NETATALK 776 /* 777 * Some protocols (like Appletalk) need special 778 * handling depending on if they are type II 779 * or SNAP encapsulated. Everything else 780 * gets handled by stripping off the SNAP header 781 * and going back up to decapsulate. 782 */ 783 if (l->llc_control == LLC_UI && 784 l->llc_ssap == LLC_SNAP_LSAP && 785 Bcmp(&(l->llc_snap.org_code)[0], 786 at_org_code, sizeof(at_org_code)) == 0 && 787 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) { 788 inq = &atintrq2; 789 m_adj(m, AT_LLC_SIZE); 790 schednetisr(NETISR_ATALK); 791 break; 792 } 793 794 if (l->llc_control == LLC_UI && 795 l->llc_ssap == LLC_SNAP_LSAP && 796 Bcmp(&(l->llc_snap.org_code)[0], 797 aarp_org_code, sizeof(aarp_org_code)) == 0 && 798 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) { 799 m_adj(m, AT_LLC_SIZE); 800 /* XXX Really this should use netisr too */ 801 aarpinput((struct arpcom *)ifp, m); 802 goto done; 803 } 804 #endif /* NETATALK */ 805 if (l->llc_control == LLC_UI && 806 l->llc_dsap == LLC_SNAP_LSAP && 807 l->llc_ssap == LLC_SNAP_LSAP) { 808 /* SNAP */ 809 if (m->m_pkthdr.len > etype) 810 m_adj(m, etype - m->m_pkthdr.len); 811 m->m_data += 6; /* XXX */ 812 m->m_len -= 6; /* XXX */ 813 m->m_pkthdr.len -= 6; /* XXX */ 814 M_PREPEND(m, sizeof(*eh), M_DONTWAIT); 815 if (m == 0) 816 goto done; 817 *mtod(m, struct ether_header *) = *eh; 818 goto decapsulate; 819 } 820 goto dropanyway; 821 dropanyway: 822 default: 823 m_freem(m); 824 goto done; 825 } 826 } 827 828 IF_INPUT_ENQUEUE(inq, m); 829 done: 830 splx(s); 831 } 832 833 /* 834 * Convert Ethernet address to printable (loggable) representation. 835 */ 836 static char digits[] = "0123456789abcdef"; 837 char * 838 ether_sprintf(ap) 839 u_char *ap; 840 { 841 int i; 842 static char etherbuf[ETHER_ADDR_LEN * 3]; 843 char *cp = etherbuf; 844 845 for (i = 0; i < ETHER_ADDR_LEN; i++) { 846 *cp++ = digits[*ap >> 4]; 847 *cp++ = digits[*ap++ & 0xf]; 848 *cp++ = ':'; 849 } 850 *--cp = 0; 851 return (etherbuf); 852 } 853 854 /* 855 * Perform common duties while attaching to interface list 856 */ 857 void 858 ether_ifattach(ifp) 859 struct ifnet *ifp; 860 { 861 862 /* 863 * Any interface which provides a MAC address which is obviously 864 * invalid gets whacked, so that users will notice. 865 */ 866 if (ETHER_IS_MULTICAST(((struct arpcom *)ifp)->ac_enaddr)) { 867 ((struct arpcom *)ifp)->ac_enaddr[0] = 0x00; 868 ((struct arpcom *)ifp)->ac_enaddr[1] = 0xfe; 869 ((struct arpcom *)ifp)->ac_enaddr[2] = 0xe1; 870 ((struct arpcom *)ifp)->ac_enaddr[3] = 0xba; 871 ((struct arpcom *)ifp)->ac_enaddr[4] = 0xd0; 872 /* 873 * XXX use of random() by anything except the scheduler is 874 * normally invalid, but this is boot time, so pre-scheduler, 875 * and the random subsystem is not alive yet 876 */ 877 ((struct arpcom *)ifp)->ac_enaddr[5] = (u_char)random() & 0xff; 878 } 879 880 ifp->if_type = IFT_ETHER; 881 ifp->if_addrlen = ETHER_ADDR_LEN; 882 ifp->if_hdrlen = ETHER_HDR_LEN; 883 ifp->if_mtu = ETHERMTU; 884 ifp->if_output = ether_output; 885 886 if (ifp->if_hardmtu == 0) 887 ifp->if_hardmtu = ETHERMTU; 888 889 if_alloc_sadl(ifp); 890 bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr, 891 LLADDR(ifp->if_sadl), ifp->if_addrlen); 892 LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs); 893 #if NBPFILTER > 0 894 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, ETHER_HDR_LEN); 895 #endif 896 } 897 898 void 899 ether_ifdetach(ifp) 900 struct ifnet *ifp; 901 { 902 struct arpcom *ac = (struct arpcom *)ifp; 903 struct ether_multi *enm; 904 905 for (enm = LIST_FIRST(&ac->ac_multiaddrs); 906 enm != LIST_END(&ac->ac_multiaddrs); 907 enm = LIST_FIRST(&ac->ac_multiaddrs)) { 908 LIST_REMOVE(enm, enm_list); 909 free(enm, M_IFMADDR); 910 } 911 912 #if 0 913 /* moved to if_detach() */ 914 if_free_sadl(ifp); 915 #endif 916 } 917 918 #if 0 919 /* 920 * This is for reference. We have table-driven versions of the 921 * crc32 generators, which are faster than the double-loop. 922 */ 923 u_int32_t __pure 924 ether_crc32_le_update(u_int_32_t crc, const u_int8_t *buf, size_t len) 925 { 926 u_int32_t c, carry; 927 size_t i, j; 928 929 for (i = 0; i < len; i++) { 930 c = buf[i]; 931 for (j = 0; j < 8; j++) { 932 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 933 crc >>= 1; 934 c >>= 1; 935 if (carry) 936 crc = (crc ^ ETHER_CRC_POLY_LE); 937 } 938 } 939 940 return (crc); 941 } 942 943 u_int32_t __pure 944 ether_crc32_be_update(u_int_32_t crc, const u_int8_t *buf, size_t len) 945 { 946 u_int32_t c, carry; 947 size_t i, j; 948 949 for (i = 0; i < len; i++) { 950 c = buf[i]; 951 for (j = 0; j < 8; j++) { 952 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 953 crc <<= 1; 954 c >>= 1; 955 if (carry) 956 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 957 } 958 } 959 960 return (crc); 961 } 962 #else 963 u_int32_t __pure 964 ether_crc32_le_update(u_int32_t crc, const u_int8_t *buf, size_t len) 965 { 966 static const u_int32_t crctab[] = { 967 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 968 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 969 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 970 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 971 }; 972 size_t i; 973 974 for (i = 0; i < len; i++) { 975 crc ^= buf[i]; 976 crc = (crc >> 4) ^ crctab[crc & 0xf]; 977 crc = (crc >> 4) ^ crctab[crc & 0xf]; 978 } 979 980 return (crc); 981 } 982 983 u_int32_t __pure 984 ether_crc32_be_update(u_int32_t crc, const u_int8_t *buf, size_t len) 985 { 986 static const u_int8_t rev[] = { 987 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, 988 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf 989 }; 990 static const u_int32_t crctab[] = { 991 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 992 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 993 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 994 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd 995 }; 996 size_t i; 997 u_int8_t data; 998 999 for (i = 0; i < len; i++) { 1000 data = buf[i]; 1001 crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data & 0xf]]; 1002 crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data >> 4]]; 1003 } 1004 1005 return (crc); 1006 } 1007 #endif 1008 1009 u_int32_t 1010 ether_crc32_le(const u_int8_t *buf, size_t len) 1011 { 1012 return ether_crc32_le_update(0xffffffff, buf, len); 1013 } 1014 1015 u_int32_t 1016 ether_crc32_be(const u_int8_t *buf, size_t len) 1017 { 1018 return ether_crc32_be_update(0xffffffff, buf, len); 1019 } 1020 1021 #ifdef INET 1022 u_char ether_ipmulticast_min[ETHER_ADDR_LEN] = 1023 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 1024 u_char ether_ipmulticast_max[ETHER_ADDR_LEN] = 1025 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 1026 #endif 1027 1028 #ifdef INET6 1029 u_char ether_ip6multicast_min[ETHER_ADDR_LEN] = 1030 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 1031 u_char ether_ip6multicast_max[ETHER_ADDR_LEN] = 1032 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff }; 1033 #endif 1034 1035 /* 1036 * Convert a sockaddr into an Ethernet address or range of Ethernet 1037 * addresses. 1038 */ 1039 int 1040 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN], 1041 u_int8_t addrhi[ETHER_ADDR_LEN]) 1042 { 1043 #ifdef INET 1044 struct sockaddr_in *sin; 1045 #endif /* INET */ 1046 #ifdef INET6 1047 struct sockaddr_in6 *sin6; 1048 #endif /* INET6 */ 1049 1050 switch (sa->sa_family) { 1051 1052 case AF_UNSPEC: 1053 bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN); 1054 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 1055 break; 1056 1057 #ifdef INET 1058 case AF_INET: 1059 sin = satosin(sa); 1060 if (sin->sin_addr.s_addr == INADDR_ANY) { 1061 /* 1062 * An IP address of INADDR_ANY means listen to 1063 * or stop listening to all of the Ethernet 1064 * multicast addresses used for IP. 1065 * (This is for the sake of IP multicast routers.) 1066 */ 1067 bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN); 1068 bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN); 1069 } else { 1070 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 1071 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 1072 } 1073 break; 1074 #endif 1075 #ifdef INET6 1076 case AF_INET6: 1077 sin6 = satosin6(sa); 1078 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1079 /* 1080 * An IP6 address of 0 means listen to or stop 1081 * listening to all of the Ethernet multicast 1082 * address used for IP6. 1083 * 1084 * (This might not be healthy, given IPv6's reliance on 1085 * multicast for things like neighbor discovery. 1086 * Perhaps initializing all-nodes, solicited nodes, and 1087 * possibly all-routers for this interface afterwards 1088 * is not a bad idea.) 1089 */ 1090 1091 bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN); 1092 bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN); 1093 } else { 1094 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 1095 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 1096 } 1097 break; 1098 #endif 1099 1100 default: 1101 return (EAFNOSUPPORT); 1102 } 1103 return (0); 1104 } 1105 1106 /* 1107 * Add an Ethernet multicast address or range of addresses to the list for a 1108 * given interface. 1109 */ 1110 int 1111 ether_addmulti(ifr, ac) 1112 struct ifreq *ifr; 1113 struct arpcom *ac; 1114 { 1115 struct ether_multi *enm; 1116 u_char addrlo[ETHER_ADDR_LEN]; 1117 u_char addrhi[ETHER_ADDR_LEN]; 1118 int s = splnet(), error; 1119 1120 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 1121 if (error != 0) { 1122 splx(s); 1123 return (error); 1124 } 1125 1126 /* 1127 * Verify that we have valid Ethernet multicast addresses. 1128 */ 1129 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) { 1130 splx(s); 1131 return (EINVAL); 1132 } 1133 /* 1134 * See if the address range is already in the list. 1135 */ 1136 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 1137 if (enm != NULL) { 1138 /* 1139 * Found it; just increment the reference count. 1140 */ 1141 ++enm->enm_refcount; 1142 splx(s); 1143 return (0); 1144 } 1145 /* 1146 * New address or range; malloc a new multicast record 1147 * and link it into the interface's multicast list. 1148 */ 1149 enm = malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); 1150 if (enm == NULL) { 1151 splx(s); 1152 return (ENOBUFS); 1153 } 1154 bcopy(addrlo, enm->enm_addrlo, ETHER_ADDR_LEN); 1155 bcopy(addrhi, enm->enm_addrhi, ETHER_ADDR_LEN); 1156 enm->enm_ac = ac; 1157 enm->enm_refcount = 1; 1158 LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list); 1159 ac->ac_multicnt++; 1160 if (bcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) 1161 ac->ac_multirangecnt++; 1162 splx(s); 1163 /* 1164 * Return ENETRESET to inform the driver that the list has changed 1165 * and its reception filter should be adjusted accordingly. 1166 */ 1167 return (ENETRESET); 1168 } 1169 1170 /* 1171 * Delete a multicast address record. 1172 */ 1173 int 1174 ether_delmulti(ifr, ac) 1175 struct ifreq *ifr; 1176 struct arpcom *ac; 1177 { 1178 struct ether_multi *enm; 1179 u_char addrlo[ETHER_ADDR_LEN]; 1180 u_char addrhi[ETHER_ADDR_LEN]; 1181 int s = splnet(), error; 1182 1183 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 1184 if (error != 0) { 1185 splx(s); 1186 return (error); 1187 } 1188 1189 /* 1190 * Look up the address in our list. 1191 */ 1192 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 1193 if (enm == NULL) { 1194 splx(s); 1195 return (ENXIO); 1196 } 1197 if (--enm->enm_refcount != 0) { 1198 /* 1199 * Still some claims to this record. 1200 */ 1201 splx(s); 1202 return (0); 1203 } 1204 /* 1205 * No remaining claims to this record; unlink and free it. 1206 */ 1207 LIST_REMOVE(enm, enm_list); 1208 free(enm, M_IFMADDR); 1209 ac->ac_multicnt--; 1210 if (bcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) 1211 ac->ac_multirangecnt--; 1212 splx(s); 1213 /* 1214 * Return ENETRESET to inform the driver that the list has changed 1215 * and its reception filter should be adjusted accordingly. 1216 */ 1217 return (ENETRESET); 1218 } 1219