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