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