1 /* $OpenBSD: if_ethersubr.c,v 1.136 2009/11/03 10:59:04 claudio 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 #ifdef DIAGNOSTIC 234 if (ifp->if_rdomain != rtable_l2(m->m_pkthdr.rdomain)) { 235 printf("%s: trying to send packet on wrong domain. " 236 "if %d vs. mbuf %d, AF %d\n", ifp->if_xname, 237 ifp->if_rdomain, rtable_l2(m->m_pkthdr.rdomain), 238 dst->sa_family); 239 } 240 #endif 241 242 #if NTRUNK > 0 243 if (ifp->if_type == IFT_IEEE8023ADLAG) 244 senderr(EBUSY); 245 #endif 246 247 #if NCARP > 0 248 if (ifp->if_type == IFT_CARP) { 249 struct ifaddr *ifa; 250 251 /* loop back if this is going to the carp interface */ 252 if (dst != NULL && LINK_STATE_IS_UP(ifp0->if_link_state) && 253 (ifa = ifa_ifwithaddr(dst, ifp->if_rdomain)) != NULL && 254 ifa->ifa_ifp == ifp0) 255 return (looutput(ifp0, m, dst, rt0)); 256 257 ifp = ifp->if_carpdev; 258 ac = (struct arpcom *)ifp; 259 260 if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) != 261 (IFF_UP|IFF_RUNNING)) 262 senderr(ENETDOWN); 263 } 264 #endif /* NCARP > 0 */ 265 266 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 267 senderr(ENETDOWN); 268 if ((rt = rt0) != NULL) { 269 if ((rt->rt_flags & RTF_UP) == 0) { 270 if ((rt0 = rt = rtalloc1(dst, 1, 271 m->m_pkthdr.rdomain)) != NULL) 272 rt->rt_refcnt--; 273 else 274 senderr(EHOSTUNREACH); 275 } 276 #ifdef MPLS 277 if (rt->rt_flags & RTF_MPLS) { 278 if ((m = mpls_output(m, rt)) == NULL) 279 senderr(EHOSTUNREACH); 280 } 281 #endif 282 if (rt->rt_flags & RTF_GATEWAY) { 283 if (rt->rt_gwroute == 0) 284 goto lookup; 285 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 286 rtfree(rt); rt = rt0; 287 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 288 ifp->if_rdomain); 289 if ((rt = rt->rt_gwroute) == 0) 290 senderr(EHOSTUNREACH); 291 } 292 } 293 if (rt->rt_flags & RTF_REJECT) 294 if (rt->rt_rmx.rmx_expire == 0 || 295 time_second < rt->rt_rmx.rmx_expire) 296 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 297 } 298 299 switch (dst->sa_family) { 300 301 #ifdef INET 302 case AF_INET: 303 if (!arpresolve(ac, rt, m, dst, edst)) 304 return (0); /* if not yet resolved */ 305 /* If broadcasting on a simplex interface, loopback a copy */ 306 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) && 307 !m->m_pkthdr.pf.routed) 308 mcopy = m_copy(m, 0, (int)M_COPYALL); 309 #ifdef MPLS 310 if (rt0 != NULL && rt0->rt_flags & RTF_MPLS) 311 etype = htons(ETHERTYPE_MPLS); 312 else 313 #endif 314 etype = htons(ETHERTYPE_IP); 315 break; 316 #endif 317 #ifdef INET6 318 case AF_INET6: 319 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)) 320 return (0); /* it must be impossible, but... */ 321 etype = htons(ETHERTYPE_IPV6); 322 break; 323 #endif 324 #ifdef NETATALK 325 case AF_APPLETALK: { 326 struct at_ifaddr *aa; 327 328 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) { 329 #ifdef NETATALKDEBUG 330 extern char *prsockaddr(struct sockaddr *); 331 printf("aarpresolv: failed for %s\n", prsockaddr(dst)); 332 #endif /* NETATALKDEBUG */ 333 return (0); 334 } 335 336 /* 337 * ifaddr is the first thing in at_ifaddr 338 */ 339 aa = (struct at_ifaddr *)at_ifawithnet( 340 (struct sockaddr_at *)dst, 341 TAILQ_FIRST(&ifp->if_addrlist)); 342 if (aa == 0) 343 goto bad; 344 345 /* 346 * In the phase 2 case, we need to prepend an mbuf for the llc 347 * header. Since we must preserve the value of m, which is 348 * passed to us by value, we m_copy() the first mbuf, 349 * and use it for our llc header. 350 */ 351 if (aa->aa_flags & AFA_PHASE2) { 352 struct llc llc; 353 354 M_PREPEND(m, AT_LLC_SIZE, M_DONTWAIT); 355 if (m == NULL) 356 return (0); 357 /* 358 * FreeBSD doesn't count the LLC len in 359 * ifp->obytes, so they increment a length 360 * field here. We don't do this. 361 */ 362 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 363 llc.llc_control = LLC_UI; 364 bcopy(at_org_code, llc.llc_snap.org_code, 365 sizeof(at_org_code)); 366 llc.llc_snap.ether_type = htons( ETHERTYPE_AT ); 367 bcopy(&llc, mtod(m, caddr_t), AT_LLC_SIZE); 368 etype = htons(m->m_pkthdr.len); 369 } else { 370 etype = htons(ETHERTYPE_AT); 371 } 372 } break; 373 #endif /* NETATALK */ 374 #ifdef MPLS 375 case AF_MPLS: 376 if (rt) 377 dst = rt_key(rt); 378 else 379 senderr(EHOSTUNREACH); 380 381 switch (dst->sa_family) { 382 case AF_LINK: 383 if (((struct sockaddr_dl *)dst)->sdl_alen < 384 sizeof(edst)) 385 senderr(EHOSTUNREACH); 386 bcopy(LLADDR(((struct sockaddr_dl *)dst)), edst, 387 sizeof(edst)); 388 break; 389 case AF_INET: 390 if (!arpresolve(ac, rt, m, dst, edst)) 391 return (0); /* if not yet resolved */ 392 break; 393 default: 394 senderr(EHOSTUNREACH); 395 } 396 /* XXX handling for simplex devices in case of M/BCAST ?? */ 397 if (m->m_flags & (M_BCAST | M_MCAST)) 398 etype = htons(ETHERTYPE_MPLS_MCAST); 399 else 400 etype = htons(ETHERTYPE_MPLS); 401 break; 402 #endif /* MPLS */ 403 case pseudo_AF_HDRCMPLT: 404 hdrcmplt = 1; 405 eh = (struct ether_header *)dst->sa_data; 406 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof(esrc)); 407 /* FALLTHROUGH */ 408 409 case AF_UNSPEC: 410 eh = (struct ether_header *)dst->sa_data; 411 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof(edst)); 412 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */ 413 etype = eh->ether_type; 414 break; 415 416 default: 417 printf("%s: can't handle af%d\n", ifp->if_xname, 418 dst->sa_family); 419 senderr(EAFNOSUPPORT); 420 } 421 422 /* XXX Should we feed-back an unencrypted IPsec packet ? */ 423 if (mcopy) 424 (void) looutput(ifp, mcopy, dst, rt); 425 426 /* 427 * Add local net header. If no space in first mbuf, 428 * allocate another. 429 */ 430 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 431 if (m == 0) 432 senderr(ENOBUFS); 433 eh = mtod(m, struct ether_header *); 434 bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type, 435 sizeof(eh->ether_type)); 436 bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof(edst)); 437 if (hdrcmplt) 438 bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost, 439 sizeof(eh->ether_shost)); 440 else 441 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 442 sizeof(eh->ether_shost)); 443 444 #if NCARP > 0 445 if (ifp0 != ifp && ifp0->if_type == IFT_CARP) 446 carp_rewrite_lladdr(ifp0, eh->ether_shost); 447 #endif 448 449 #if NBRIDGE > 0 450 /* 451 * Interfaces that are bridge members need special handling 452 * for output. 453 */ 454 if (ifp->if_bridge) { 455 struct m_tag *mtag; 456 457 /* 458 * Check if this packet has already been sent out through 459 * this bridge, in which case we simply send it out 460 * without further bridge processing. 461 */ 462 for (mtag = m_tag_find(m, PACKET_TAG_BRIDGE, NULL); mtag; 463 mtag = m_tag_find(m, PACKET_TAG_BRIDGE, mtag)) { 464 #ifdef DEBUG 465 /* Check that the information is there */ 466 if (mtag->m_tag_len != sizeof(caddr_t)) { 467 error = EINVAL; 468 goto bad; 469 } 470 #endif 471 if (!bcmp(&ifp->if_bridge, mtag + 1, sizeof(caddr_t))) 472 break; 473 } 474 if (mtag == NULL) { 475 /* Attach a tag so we can detect loops */ 476 mtag = m_tag_get(PACKET_TAG_BRIDGE, sizeof(caddr_t), 477 M_NOWAIT); 478 if (mtag == NULL) { 479 error = ENOBUFS; 480 goto bad; 481 } 482 bcopy(&ifp->if_bridge, mtag + 1, sizeof(caddr_t)); 483 m_tag_prepend(m, mtag); 484 error = bridge_output(ifp, m, NULL, NULL); 485 return (error); 486 } 487 } 488 #endif 489 490 mflags = m->m_flags; 491 len = m->m_pkthdr.len; 492 s = splnet(); 493 /* 494 * Queue message on interface, and start output if interface 495 * not yet active. 496 */ 497 IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error); 498 if (error) { 499 /* mbuf is already freed */ 500 splx(s); 501 return (error); 502 } 503 ifp->if_obytes += len; 504 #if NCARP > 0 505 if (ifp != ifp0) 506 ifp0->if_obytes += len; 507 #endif /* NCARP > 0 */ 508 if (mflags & M_MCAST) 509 ifp->if_omcasts++; 510 if_start(ifp); 511 splx(s); 512 return (error); 513 514 bad: 515 if (m) 516 m_freem(m); 517 return (error); 518 } 519 520 /* 521 * Process a received Ethernet packet; 522 * the packet is in the mbuf chain m without 523 * the ether header, which is provided separately. 524 */ 525 void 526 ether_input(ifp0, eh, m) 527 struct ifnet *ifp0; 528 struct ether_header *eh; 529 struct mbuf *m; 530 { 531 struct ifqueue *inq; 532 u_int16_t etype; 533 int s, llcfound = 0; 534 struct llc *l; 535 struct arpcom *ac; 536 struct ifnet *ifp = ifp0; 537 #if NTRUNK > 0 538 int i = 0; 539 #endif 540 #if NPPPOE > 0 541 struct ether_header *eh_tmp; 542 #endif 543 544 m_cluncount(m, 1); 545 546 /* mark incomming routing domain */ 547 m->m_pkthdr.rdomain = ifp->if_rdomain; 548 549 if (eh == NULL) { 550 eh = mtod(m, struct ether_header *); 551 m_adj(m, ETHER_HDR_LEN); 552 } 553 554 #if NTRUNK > 0 555 /* Handle input from a trunk port */ 556 while (ifp->if_type == IFT_IEEE8023ADLAG) { 557 if (++i > TRUNK_MAX_STACKING) { 558 m_freem(m); 559 return; 560 } 561 if (trunk_input(ifp, eh, m) != 0) 562 return; 563 564 /* Has been set to the trunk interface */ 565 ifp = m->m_pkthdr.rcvif; 566 } 567 #endif 568 569 if ((ifp->if_flags & IFF_UP) == 0) { 570 m_freem(m); 571 return; 572 } 573 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 574 if ((ifp->if_flags & IFF_SIMPLEX) == 0) { 575 struct ifaddr *ifa; 576 struct sockaddr_dl *sdl = NULL; 577 578 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 579 if ((sdl = 580 (struct sockaddr_dl *)ifa->ifa_addr) && 581 sdl->sdl_family == AF_LINK) 582 break; 583 } 584 /* 585 * If this is not a simplex interface, drop the packet 586 * if it came from us. 587 */ 588 if (sdl && bcmp(LLADDR(sdl), eh->ether_shost, 589 ETHER_ADDR_LEN) == 0) { 590 m_freem(m); 591 return; 592 } 593 } 594 595 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 596 sizeof(etherbroadcastaddr)) == 0) 597 m->m_flags |= M_BCAST; 598 else 599 m->m_flags |= M_MCAST; 600 ifp->if_imcasts++; 601 #if NTRUNK > 0 602 if (ifp != ifp0) 603 ifp0->if_imcasts++; 604 #endif 605 } 606 607 ifp->if_ibytes += m->m_pkthdr.len + sizeof(*eh); 608 #if NTRUNK > 0 609 if (ifp != ifp0) 610 ifp0->if_ibytes += m->m_pkthdr.len + sizeof(*eh); 611 #endif 612 613 etype = ntohs(eh->ether_type); 614 615 if (!(netisr & (1 << NETISR_RND_DONE))) { 616 add_net_randomness(etype); 617 atomic_setbits_int(&netisr, (1 << NETISR_RND_DONE)); 618 } 619 620 #if NVLAN > 0 621 if (((m->m_flags & M_VLANTAG) || etype == ETHERTYPE_VLAN) 622 && (vlan_input(eh, m) == 0)) 623 return; 624 #endif 625 626 #if NBRIDGE > 0 627 /* 628 * Tap the packet off here for a bridge, if configured and 629 * active for this interface. bridge_input returns 630 * NULL if it has consumed the packet, otherwise, it 631 * gets processed as normal. 632 */ 633 if (ifp->if_bridge) { 634 if (m->m_flags & M_PROTO1) 635 m->m_flags &= ~M_PROTO1; 636 else { 637 m = bridge_input(ifp, eh, m); 638 if (m == NULL) 639 return; 640 /* The bridge has determined it's for us. */ 641 ifp = m->m_pkthdr.rcvif; 642 } 643 } 644 #endif 645 646 #if NVLAN > 0 647 if ((m->m_flags & M_VLANTAG) || etype == ETHERTYPE_VLAN) { 648 /* The bridge did not want the vlan frame either, drop it. */ 649 ifp->if_noproto++; 650 m_freem(m); 651 return; 652 } 653 #endif /* NVLAN > 0 */ 654 655 #if NCARP > 0 656 if (ifp->if_carp) { 657 if (ifp->if_type != IFT_CARP && 658 (carp_input(m, (u_int8_t *)&eh->ether_shost, 659 (u_int8_t *)&eh->ether_dhost, eh->ether_type) == 0)) 660 return; 661 /* clear mcast if received on a carp IP balanced address */ 662 else if (ifp->if_type == IFT_CARP && 663 m->m_flags & (M_BCAST|M_MCAST) && 664 carp_our_mcastaddr(ifp, (u_int8_t *)&eh->ether_dhost)) 665 m->m_flags &= ~(M_BCAST|M_MCAST); 666 } 667 #endif /* NCARP > 0 */ 668 669 ac = (struct arpcom *)ifp; 670 671 /* 672 * If packet has been filtered by the bpf listener, drop it now 673 */ 674 if (m->m_flags & M_FILDROP) { 675 m_free(m); 676 return; 677 } 678 679 /* 680 * If packet is unicast and we're in promiscuous mode, make sure it 681 * is for us. Drop otherwise. 682 */ 683 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0 && 684 (ifp->if_flags & IFF_PROMISC)) { 685 if (bcmp(ac->ac_enaddr, (caddr_t)eh->ether_dhost, 686 ETHER_ADDR_LEN)) { 687 m_freem(m); 688 return; 689 } 690 } 691 692 /* 693 * Schedule softnet interrupt and enqueue packet within the same spl. 694 */ 695 s = splnet(); 696 decapsulate: 697 698 switch (etype) { 699 #ifdef INET 700 case ETHERTYPE_IP: 701 schednetisr(NETISR_IP); 702 inq = &ipintrq; 703 break; 704 705 case ETHERTYPE_ARP: 706 if (ifp->if_flags & IFF_NOARP) 707 goto dropanyway; 708 schednetisr(NETISR_ARP); 709 inq = &arpintrq; 710 break; 711 712 case ETHERTYPE_REVARP: 713 if (ifp->if_flags & IFF_NOARP) 714 goto dropanyway; 715 revarpinput(m); /* XXX queue? */ 716 goto done; 717 718 #endif 719 #ifdef INET6 720 /* 721 * Schedule IPv6 software interrupt for incoming IPv6 packet. 722 */ 723 case ETHERTYPE_IPV6: 724 schednetisr(NETISR_IPV6); 725 inq = &ip6intrq; 726 break; 727 #endif /* INET6 */ 728 #ifdef NETATALK 729 case ETHERTYPE_AT: 730 schednetisr(NETISR_ATALK); 731 inq = &atintrq1; 732 break; 733 case ETHERTYPE_AARP: 734 /* probably this should be done with a NETISR as well */ 735 /* XXX queue this */ 736 aarpinput((struct arpcom *)ifp, m); 737 goto done; 738 #endif 739 #if NPPPOE > 0 740 case ETHERTYPE_PPPOEDISC: 741 case ETHERTYPE_PPPOE: 742 /* XXX we dont have this flag */ 743 /* 744 if (m->m_flags & M_PROMISC) { 745 m_freem(m); 746 goto done; 747 } 748 */ 749 #ifndef PPPOE_SERVER 750 if (m->m_flags & (M_MCAST | M_BCAST)) { 751 m_freem(m); 752 goto done; 753 } 754 #endif 755 M_PREPEND(m, sizeof(*eh), M_DONTWAIT); 756 if (m == NULL) 757 goto done; 758 759 eh_tmp = mtod(m, struct ether_header *); 760 bcopy(eh, eh_tmp, sizeof(struct ether_header)); 761 762 if (etype == ETHERTYPE_PPPOEDISC) 763 inq = &pppoediscinq; 764 else 765 inq = &pppoeinq; 766 767 schednetisr(NETISR_PPPOE); 768 break; 769 #endif /* NPPPOE > 0 */ 770 #ifdef AOE 771 case ETHERTYPE_AOE: 772 aoe_input(ifp, m); 773 goto done; 774 #endif /* AOE */ 775 #ifdef MPLS 776 case ETHERTYPE_MPLS: 777 case ETHERTYPE_MPLS_MCAST: 778 inq = &mplsintrq; 779 schednetisr(NETISR_MPLS); 780 break; 781 #endif 782 default: 783 if (llcfound || etype > ETHERMTU) 784 goto dropanyway; 785 llcfound = 1; 786 l = mtod(m, struct llc *); 787 switch (l->llc_dsap) { 788 case LLC_SNAP_LSAP: 789 #ifdef NETATALK 790 /* 791 * Some protocols (like Appletalk) need special 792 * handling depending on if they are type II 793 * or SNAP encapsulated. Everything else 794 * gets handled by stripping off the SNAP header 795 * and going back up to decapsulate. 796 */ 797 if (l->llc_control == LLC_UI && 798 l->llc_ssap == LLC_SNAP_LSAP && 799 Bcmp(&(l->llc_snap.org_code)[0], 800 at_org_code, sizeof(at_org_code)) == 0 && 801 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) { 802 inq = &atintrq2; 803 m_adj(m, AT_LLC_SIZE); 804 schednetisr(NETISR_ATALK); 805 break; 806 } 807 808 if (l->llc_control == LLC_UI && 809 l->llc_ssap == LLC_SNAP_LSAP && 810 Bcmp(&(l->llc_snap.org_code)[0], 811 aarp_org_code, sizeof(aarp_org_code)) == 0 && 812 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) { 813 m_adj(m, AT_LLC_SIZE); 814 /* XXX Really this should use netisr too */ 815 aarpinput((struct arpcom *)ifp, m); 816 goto done; 817 } 818 #endif /* NETATALK */ 819 if (l->llc_control == LLC_UI && 820 l->llc_dsap == LLC_SNAP_LSAP && 821 l->llc_ssap == LLC_SNAP_LSAP) { 822 /* SNAP */ 823 if (m->m_pkthdr.len > etype) 824 m_adj(m, etype - m->m_pkthdr.len); 825 m->m_data += 6; /* XXX */ 826 m->m_len -= 6; /* XXX */ 827 m->m_pkthdr.len -= 6; /* XXX */ 828 M_PREPEND(m, sizeof(*eh), M_DONTWAIT); 829 if (m == 0) 830 goto done; 831 *mtod(m, struct ether_header *) = *eh; 832 goto decapsulate; 833 } 834 goto dropanyway; 835 dropanyway: 836 default: 837 m_freem(m); 838 goto done; 839 } 840 } 841 842 IF_INPUT_ENQUEUE(inq, m); 843 done: 844 splx(s); 845 } 846 847 /* 848 * Convert Ethernet address to printable (loggable) representation. 849 */ 850 static char digits[] = "0123456789abcdef"; 851 char * 852 ether_sprintf(ap) 853 u_char *ap; 854 { 855 int i; 856 static char etherbuf[ETHER_ADDR_LEN * 3]; 857 char *cp = etherbuf; 858 859 for (i = 0; i < ETHER_ADDR_LEN; i++) { 860 *cp++ = digits[*ap >> 4]; 861 *cp++ = digits[*ap++ & 0xf]; 862 *cp++ = ':'; 863 } 864 *--cp = 0; 865 return (etherbuf); 866 } 867 868 /* 869 * Perform common duties while attaching to interface list 870 */ 871 void 872 ether_ifattach(ifp) 873 struct ifnet *ifp; 874 { 875 876 /* 877 * Any interface which provides a MAC address which is obviously 878 * invalid gets whacked, so that users will notice. 879 */ 880 if (ETHER_IS_MULTICAST(((struct arpcom *)ifp)->ac_enaddr)) { 881 ((struct arpcom *)ifp)->ac_enaddr[0] = 0x00; 882 ((struct arpcom *)ifp)->ac_enaddr[1] = 0xfe; 883 ((struct arpcom *)ifp)->ac_enaddr[2] = 0xe1; 884 ((struct arpcom *)ifp)->ac_enaddr[3] = 0xba; 885 ((struct arpcom *)ifp)->ac_enaddr[4] = 0xd0; 886 /* 887 * XXX use of random() by anything except the scheduler is 888 * normally invalid, but this is boot time, so pre-scheduler, 889 * and the random subsystem is not alive yet 890 */ 891 ((struct arpcom *)ifp)->ac_enaddr[5] = (u_char)random() & 0xff; 892 } 893 894 ifp->if_type = IFT_ETHER; 895 ifp->if_addrlen = ETHER_ADDR_LEN; 896 ifp->if_hdrlen = ETHER_HDR_LEN; 897 ifp->if_mtu = ETHERMTU; 898 ifp->if_output = ether_output; 899 900 if (ifp->if_hardmtu == 0) 901 ifp->if_hardmtu = ETHERMTU; 902 903 if_alloc_sadl(ifp); 904 bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr, 905 LLADDR(ifp->if_sadl), ifp->if_addrlen); 906 LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs); 907 #if NBPFILTER > 0 908 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, ETHER_HDR_LEN); 909 #endif 910 } 911 912 void 913 ether_ifdetach(ifp) 914 struct ifnet *ifp; 915 { 916 struct arpcom *ac = (struct arpcom *)ifp; 917 struct ether_multi *enm; 918 919 for (enm = LIST_FIRST(&ac->ac_multiaddrs); 920 enm != LIST_END(&ac->ac_multiaddrs); 921 enm = LIST_FIRST(&ac->ac_multiaddrs)) { 922 LIST_REMOVE(enm, enm_list); 923 free(enm, M_IFMADDR); 924 } 925 926 #if 0 927 /* moved to if_detach() */ 928 if_free_sadl(ifp); 929 #endif 930 } 931 932 #if 0 933 /* 934 * This is for reference. We have table-driven versions of the 935 * crc32 generators, which are faster than the double-loop. 936 */ 937 u_int32_t __pure 938 ether_crc32_le_update(u_int_32_t crc, const u_int8_t *buf, size_t len) 939 { 940 u_int32_t c, carry; 941 size_t i, j; 942 943 for (i = 0; i < len; i++) { 944 c = buf[i]; 945 for (j = 0; j < 8; j++) { 946 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); 947 crc >>= 1; 948 c >>= 1; 949 if (carry) 950 crc = (crc ^ ETHER_CRC_POLY_LE); 951 } 952 } 953 954 return (crc); 955 } 956 957 u_int32_t __pure 958 ether_crc32_be_update(u_int_32_t crc, const u_int8_t *buf, size_t len) 959 { 960 u_int32_t c, carry; 961 size_t i, j; 962 963 for (i = 0; i < len; i++) { 964 c = buf[i]; 965 for (j = 0; j < 8; j++) { 966 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); 967 crc <<= 1; 968 c >>= 1; 969 if (carry) 970 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 971 } 972 } 973 974 return (crc); 975 } 976 #else 977 u_int32_t __pure 978 ether_crc32_le_update(u_int32_t crc, const u_int8_t *buf, size_t len) 979 { 980 static const u_int32_t crctab[] = { 981 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 982 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 983 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 984 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 985 }; 986 size_t i; 987 988 for (i = 0; i < len; i++) { 989 crc ^= buf[i]; 990 crc = (crc >> 4) ^ crctab[crc & 0xf]; 991 crc = (crc >> 4) ^ crctab[crc & 0xf]; 992 } 993 994 return (crc); 995 } 996 997 u_int32_t __pure 998 ether_crc32_be_update(u_int32_t crc, const u_int8_t *buf, size_t len) 999 { 1000 static const u_int8_t rev[] = { 1001 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, 1002 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf 1003 }; 1004 static const u_int32_t crctab[] = { 1005 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 1006 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 1007 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 1008 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd 1009 }; 1010 size_t i; 1011 u_int8_t data; 1012 1013 for (i = 0; i < len; i++) { 1014 data = buf[i]; 1015 crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data & 0xf]]; 1016 crc = (crc << 4) ^ crctab[(crc >> 28) ^ rev[data >> 4]]; 1017 } 1018 1019 return (crc); 1020 } 1021 #endif 1022 1023 u_int32_t 1024 ether_crc32_le(const u_int8_t *buf, size_t len) 1025 { 1026 return ether_crc32_le_update(0xffffffff, buf, len); 1027 } 1028 1029 u_int32_t 1030 ether_crc32_be(const u_int8_t *buf, size_t len) 1031 { 1032 return ether_crc32_be_update(0xffffffff, buf, len); 1033 } 1034 1035 #ifdef INET 1036 u_char ether_ipmulticast_min[ETHER_ADDR_LEN] = 1037 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 1038 u_char ether_ipmulticast_max[ETHER_ADDR_LEN] = 1039 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 1040 #endif 1041 1042 #ifdef INET6 1043 u_char ether_ip6multicast_min[ETHER_ADDR_LEN] = 1044 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 1045 u_char ether_ip6multicast_max[ETHER_ADDR_LEN] = 1046 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff }; 1047 #endif 1048 1049 /* 1050 * Convert a sockaddr into an Ethernet address or range of Ethernet 1051 * addresses. 1052 */ 1053 int 1054 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN], 1055 u_int8_t addrhi[ETHER_ADDR_LEN]) 1056 { 1057 #ifdef INET 1058 struct sockaddr_in *sin; 1059 #endif /* INET */ 1060 #ifdef INET6 1061 struct sockaddr_in6 *sin6; 1062 #endif /* INET6 */ 1063 1064 switch (sa->sa_family) { 1065 1066 case AF_UNSPEC: 1067 bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN); 1068 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 1069 break; 1070 1071 #ifdef INET 1072 case AF_INET: 1073 sin = satosin(sa); 1074 if (sin->sin_addr.s_addr == INADDR_ANY) { 1075 /* 1076 * An IP address of INADDR_ANY means listen to 1077 * or stop listening to all of the Ethernet 1078 * multicast addresses used for IP. 1079 * (This is for the sake of IP multicast routers.) 1080 */ 1081 bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN); 1082 bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN); 1083 } else { 1084 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 1085 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 1086 } 1087 break; 1088 #endif 1089 #ifdef INET6 1090 case AF_INET6: 1091 sin6 = satosin6(sa); 1092 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1093 /* 1094 * An IP6 address of 0 means listen to or stop 1095 * listening to all of the Ethernet multicast 1096 * address used for IP6. 1097 * 1098 * (This might not be healthy, given IPv6's reliance on 1099 * multicast for things like neighbor discovery. 1100 * Perhaps initializing all-nodes, solicited nodes, and 1101 * possibly all-routers for this interface afterwards 1102 * is not a bad idea.) 1103 */ 1104 1105 bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN); 1106 bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN); 1107 } else { 1108 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo); 1109 bcopy(addrlo, addrhi, ETHER_ADDR_LEN); 1110 } 1111 break; 1112 #endif 1113 1114 default: 1115 return (EAFNOSUPPORT); 1116 } 1117 return (0); 1118 } 1119 1120 /* 1121 * Add an Ethernet multicast address or range of addresses to the list for a 1122 * given interface. 1123 */ 1124 int 1125 ether_addmulti(ifr, ac) 1126 struct ifreq *ifr; 1127 struct arpcom *ac; 1128 { 1129 struct ether_multi *enm; 1130 u_char addrlo[ETHER_ADDR_LEN]; 1131 u_char addrhi[ETHER_ADDR_LEN]; 1132 int s = splnet(), error; 1133 1134 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 1135 if (error != 0) { 1136 splx(s); 1137 return (error); 1138 } 1139 1140 /* 1141 * Verify that we have valid Ethernet multicast addresses. 1142 */ 1143 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) { 1144 splx(s); 1145 return (EINVAL); 1146 } 1147 /* 1148 * See if the address range is already in the list. 1149 */ 1150 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 1151 if (enm != NULL) { 1152 /* 1153 * Found it; just increment the reference count. 1154 */ 1155 ++enm->enm_refcount; 1156 splx(s); 1157 return (0); 1158 } 1159 /* 1160 * New address or range; malloc a new multicast record 1161 * and link it into the interface's multicast list. 1162 */ 1163 enm = malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); 1164 if (enm == NULL) { 1165 splx(s); 1166 return (ENOBUFS); 1167 } 1168 bcopy(addrlo, enm->enm_addrlo, ETHER_ADDR_LEN); 1169 bcopy(addrhi, enm->enm_addrhi, ETHER_ADDR_LEN); 1170 enm->enm_ac = ac; 1171 enm->enm_refcount = 1; 1172 LIST_INSERT_HEAD(&ac->ac_multiaddrs, enm, enm_list); 1173 ac->ac_multicnt++; 1174 if (bcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) 1175 ac->ac_multirangecnt++; 1176 splx(s); 1177 /* 1178 * Return ENETRESET to inform the driver that the list has changed 1179 * and its reception filter should be adjusted accordingly. 1180 */ 1181 return (ENETRESET); 1182 } 1183 1184 /* 1185 * Delete a multicast address record. 1186 */ 1187 int 1188 ether_delmulti(ifr, ac) 1189 struct ifreq *ifr; 1190 struct arpcom *ac; 1191 { 1192 struct ether_multi *enm; 1193 u_char addrlo[ETHER_ADDR_LEN]; 1194 u_char addrhi[ETHER_ADDR_LEN]; 1195 int s = splnet(), error; 1196 1197 error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi); 1198 if (error != 0) { 1199 splx(s); 1200 return (error); 1201 } 1202 1203 /* 1204 * Look up the address in our list. 1205 */ 1206 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 1207 if (enm == NULL) { 1208 splx(s); 1209 return (ENXIO); 1210 } 1211 if (--enm->enm_refcount != 0) { 1212 /* 1213 * Still some claims to this record. 1214 */ 1215 splx(s); 1216 return (0); 1217 } 1218 /* 1219 * No remaining claims to this record; unlink and free it. 1220 */ 1221 LIST_REMOVE(enm, enm_list); 1222 free(enm, M_IFMADDR); 1223 ac->ac_multicnt--; 1224 if (bcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) 1225 ac->ac_multirangecnt--; 1226 splx(s); 1227 /* 1228 * Return ENETRESET to inform the driver that the list has changed 1229 * and its reception filter should be adjusted accordingly. 1230 */ 1231 return (ENETRESET); 1232 } 1233