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