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