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