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