1 /*- 2 * Copyright (c) 1982, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 30 * $FreeBSD$ 31 */ 32 33 #include "opt_atalk.h" 34 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 #include "opt_ipx.h" 37 #include "opt_bdg.h" 38 #include "opt_mac.h" 39 #include "opt_netgraph.h" 40 #include "opt_carp.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/mac.h> 46 #include <sys/malloc.h> 47 #include <sys/module.h> 48 #include <sys/mbuf.h> 49 #include <sys/random.h> 50 #include <sys/socket.h> 51 #include <sys/sockio.h> 52 #include <sys/sysctl.h> 53 54 #include <net/if.h> 55 #include <net/if_arp.h> 56 #include <net/netisr.h> 57 #include <net/route.h> 58 #include <net/if_llc.h> 59 #include <net/if_dl.h> 60 #include <net/if_types.h> 61 #include <net/bpf.h> 62 #include <net/ethernet.h> 63 #include <net/bridge.h> 64 #include <net/if_vlan_var.h> 65 66 #if defined(INET) || defined(INET6) 67 #include <netinet/in.h> 68 #include <netinet/in_var.h> 69 #include <netinet/if_ether.h> 70 #include <netinet/ip_fw.h> 71 #include <netinet/ip_dummynet.h> 72 #endif 73 #ifdef INET6 74 #include <netinet6/nd6.h> 75 #endif 76 77 #ifdef DEV_CARP 78 #include <netinet/ip_carp.h> 79 #endif 80 81 #ifdef IPX 82 #include <netipx/ipx.h> 83 #include <netipx/ipx_if.h> 84 #endif 85 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m); 86 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, 87 struct sockaddr *dst, short *tp, int *hlen); 88 89 #ifdef NETATALK 90 #include <netatalk/at.h> 91 #include <netatalk/at_var.h> 92 #include <netatalk/at_extern.h> 93 94 #define llc_snap_org_code llc_un.type_snap.org_code 95 #define llc_snap_ether_type llc_un.type_snap.ether_type 96 97 extern u_char at_org_code[3]; 98 extern u_char aarp_org_code[3]; 99 #endif /* NETATALK */ 100 101 /* netgraph node hooks for ng_ether(4) */ 102 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 103 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 104 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 105 void (*ng_ether_attach_p)(struct ifnet *ifp); 106 void (*ng_ether_detach_p)(struct ifnet *ifp); 107 108 void (*vlan_input_p)(struct ifnet *, struct mbuf *); 109 110 /* bridge support */ 111 int do_bridge; 112 bridge_in_t *bridge_in_ptr; 113 bdg_forward_t *bdg_forward_ptr; 114 bdgtakeifaces_t *bdgtakeifaces_ptr; 115 struct bdg_softc *ifp2sc; 116 117 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *); 118 int (*bridge_output_p)(struct ifnet *, struct mbuf *, 119 struct sockaddr *, struct rtentry *); 120 121 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 122 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 123 124 static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 125 struct sockaddr *); 126 127 #define senderr(e) do { error = (e); goto bad;} while (0) 128 129 #if defined(INET) || defined(INET6) 130 int 131 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 132 struct ip_fw **rule, int shared); 133 static int ether_ipfw; 134 #endif 135 136 /* 137 * Ethernet output routine. 138 * Encapsulate a packet of type family for the local net. 139 * Use trailer local net encapsulation if enough data in first 140 * packet leaves a multiple of 512 bytes of data in remainder. 141 * Assumes that ifp is actually pointer to arpcom structure. 142 */ 143 int 144 ether_output(struct ifnet *ifp, struct mbuf *m, 145 struct sockaddr *dst, struct rtentry *rt0) 146 { 147 short type; 148 int error, hdrcmplt = 0; 149 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN]; 150 struct ether_header *eh; 151 int loop_copy = 0; 152 int hlen; /* link layer header length */ 153 154 #ifdef MAC 155 error = mac_check_ifnet_transmit(ifp, m); 156 if (error) 157 senderr(error); 158 #endif 159 160 if (ifp->if_flags & IFF_MONITOR) 161 senderr(ENETDOWN); 162 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 163 senderr(ENETDOWN); 164 165 hlen = ETHER_HDR_LEN; 166 switch (dst->sa_family) { 167 #ifdef INET 168 case AF_INET: 169 error = arpresolve(ifp, rt0, m, dst, edst); 170 if (error) 171 return (error == EWOULDBLOCK ? 0 : error); 172 type = htons(ETHERTYPE_IP); 173 break; 174 case AF_ARP: 175 { 176 struct arphdr *ah; 177 ah = mtod(m, struct arphdr *); 178 ah->ar_hrd = htons(ARPHRD_ETHER); 179 180 loop_copy = -1; /* if this is for us, don't do it */ 181 182 switch(ntohs(ah->ar_op)) { 183 case ARPOP_REVREQUEST: 184 case ARPOP_REVREPLY: 185 type = htons(ETHERTYPE_REVARP); 186 break; 187 case ARPOP_REQUEST: 188 case ARPOP_REPLY: 189 default: 190 type = htons(ETHERTYPE_ARP); 191 break; 192 } 193 194 if (m->m_flags & M_BCAST) 195 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN); 196 else 197 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN); 198 199 } 200 break; 201 #endif 202 #ifdef INET6 203 case AF_INET6: 204 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst); 205 if (error) 206 return error; 207 type = htons(ETHERTYPE_IPV6); 208 break; 209 #endif 210 #ifdef IPX 211 case AF_IPX: 212 if (ef_outputp) { 213 error = ef_outputp(ifp, &m, dst, &type, &hlen); 214 if (error) 215 goto bad; 216 } else 217 type = htons(ETHERTYPE_IPX); 218 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 219 (caddr_t)edst, sizeof (edst)); 220 break; 221 #endif 222 #ifdef NETATALK 223 case AF_APPLETALK: 224 { 225 struct at_ifaddr *aa; 226 227 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) 228 senderr(EHOSTUNREACH); /* XXX */ 229 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) 230 return (0); 231 /* 232 * In the phase 2 case, need to prepend an mbuf for the llc header. 233 */ 234 if ( aa->aa_flags & AFA_PHASE2 ) { 235 struct llc llc; 236 237 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT); 238 if (m == NULL) 239 senderr(ENOBUFS); 240 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 241 llc.llc_control = LLC_UI; 242 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 243 llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 244 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN); 245 type = htons(m->m_pkthdr.len); 246 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN; 247 } else { 248 type = htons(ETHERTYPE_AT); 249 } 250 break; 251 } 252 #endif /* NETATALK */ 253 254 case pseudo_AF_HDRCMPLT: 255 hdrcmplt = 1; 256 eh = (struct ether_header *)dst->sa_data; 257 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc)); 258 /* FALLTHROUGH */ 259 260 case AF_UNSPEC: 261 loop_copy = -1; /* if this is for us, don't do it */ 262 eh = (struct ether_header *)dst->sa_data; 263 (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 264 type = eh->ether_type; 265 break; 266 267 default: 268 if_printf(ifp, "can't handle af%d\n", dst->sa_family); 269 senderr(EAFNOSUPPORT); 270 } 271 272 /* 273 * Add local net header. If no space in first mbuf, 274 * allocate another. 275 */ 276 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 277 if (m == NULL) 278 senderr(ENOBUFS); 279 eh = mtod(m, struct ether_header *); 280 (void)memcpy(&eh->ether_type, &type, 281 sizeof(eh->ether_type)); 282 (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 283 if (hdrcmplt) 284 (void)memcpy(eh->ether_shost, esrc, 285 sizeof(eh->ether_shost)); 286 else 287 (void)memcpy(eh->ether_shost, IFP2AC(ifp)->ac_enaddr, 288 sizeof(eh->ether_shost)); 289 290 /* 291 * Bridges require special output handling. 292 */ 293 if (ifp->if_bridge) { 294 KASSERT(bridge_output_p != NULL, 295 ("ether_input: if_bridge not loaded!")); 296 return ((*bridge_output_p)(ifp, m, NULL, NULL)); 297 } 298 299 /* 300 * If a simplex interface, and the packet is being sent to our 301 * Ethernet address or a broadcast address, loopback a copy. 302 * XXX To make a simplex device behave exactly like a duplex 303 * device, we should copy in the case of sending to our own 304 * ethernet address (thus letting the original actually appear 305 * on the wire). However, we don't do that here for security 306 * reasons and compatibility with the original behavior. 307 */ 308 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 309 int csum_flags = 0; 310 311 if (m->m_pkthdr.csum_flags & CSUM_IP) 312 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 313 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 314 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 315 316 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 317 struct mbuf *n; 318 319 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 320 n->m_pkthdr.csum_flags |= csum_flags; 321 if (csum_flags & CSUM_DATA_VALID) 322 n->m_pkthdr.csum_data = 0xffff; 323 (void)if_simloop(ifp, n, dst->sa_family, hlen); 324 } else 325 ifp->if_iqdrops++; 326 } else if (bcmp(eh->ether_dhost, eh->ether_shost, 327 ETHER_ADDR_LEN) == 0) { 328 m->m_pkthdr.csum_flags |= csum_flags; 329 if (csum_flags & CSUM_DATA_VALID) 330 m->m_pkthdr.csum_data = 0xffff; 331 (void) if_simloop(ifp, m, dst->sa_family, hlen); 332 return (0); /* XXX */ 333 } 334 } 335 336 #ifdef DEV_CARP 337 if (ifp->if_carp && 338 (error = carp_output(ifp, m, dst, NULL))) 339 goto bad; 340 #endif 341 342 /* Handle ng_ether(4) processing, if any */ 343 if (IFP2AC(ifp)->ac_netgraph != NULL) { 344 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 345 bad: if (m != NULL) 346 m_freem(m); 347 return (error); 348 } 349 if (m == NULL) 350 return (0); 351 } 352 353 /* Continue with link-layer output */ 354 return ether_output_frame(ifp, m); 355 } 356 357 /* 358 * Ethernet link layer output routine to send a raw frame to the device. 359 * 360 * This assumes that the 14 byte Ethernet header is present and contiguous 361 * in the first mbuf (if BRIDGE'ing). 362 */ 363 int 364 ether_output_frame(struct ifnet *ifp, struct mbuf *m) 365 { 366 #if defined(INET) || defined(INET6) 367 struct ip_fw *rule = ip_dn_claim_rule(m); 368 #else 369 void *rule = NULL; 370 #endif 371 int error; 372 373 if (rule == NULL && BDG_ACTIVE(ifp)) { 374 /* 375 * Beware, the bridge code notices the null rcvif and 376 * uses that identify that it's being called from 377 * ether_output as opposd to ether_input. Yech. 378 */ 379 m->m_pkthdr.rcvif = NULL; 380 m = bdg_forward_ptr(m, ifp); 381 if (m != NULL) 382 m_freem(m); 383 return (0); 384 } 385 #if defined(INET) || defined(INET6) 386 if (IPFW_LOADED && ether_ipfw != 0) { 387 if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) { 388 if (m) { 389 m_freem(m); 390 return EACCES; /* pkt dropped */ 391 } else 392 return 0; /* consumed e.g. in a pipe */ 393 } 394 } 395 #endif 396 397 /* 398 * Queue message on interface, update output statistics if 399 * successful, and start output if interface not yet active. 400 */ 401 IFQ_HANDOFF(ifp, m, error); 402 return (error); 403 } 404 405 #if defined(INET) || defined(INET6) 406 /* 407 * ipfw processing for ethernet packets (in and out). 408 * The second parameter is NULL from ether_demux, and ifp from 409 * ether_output_frame. This section of code could be used from 410 * bridge.c as well as long as we use some extra info 411 * to distinguish that case from ether_output_frame(); 412 */ 413 int 414 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 415 struct ip_fw **rule, int shared) 416 { 417 struct ether_header *eh; 418 struct ether_header save_eh; 419 struct mbuf *m; 420 int i; 421 struct ip_fw_args args; 422 423 if (*rule != NULL && fw_one_pass) 424 return 1; /* dummynet packet, already partially processed */ 425 426 /* 427 * I need some amt of data to be contiguous, and in case others need 428 * the packet (shared==1) also better be in the first mbuf. 429 */ 430 m = *m0; 431 i = min( m->m_pkthdr.len, max_protohdr); 432 if ( shared || m->m_len < i) { 433 m = m_pullup(m, i); 434 if (m == NULL) { 435 *m0 = m; 436 return 0; 437 } 438 } 439 eh = mtod(m, struct ether_header *); 440 save_eh = *eh; /* save copy for restore below */ 441 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */ 442 443 args.m = m; /* the packet we are looking at */ 444 args.oif = dst; /* destination, if any */ 445 args.rule = *rule; /* matching rule to restart */ 446 args.next_hop = NULL; /* we do not support forward yet */ 447 args.eh = &save_eh; /* MAC header for bridged/MAC packets */ 448 i = ip_fw_chk_ptr(&args); 449 m = args.m; 450 if (m != NULL) { 451 /* 452 * Restore Ethernet header, as needed, in case the 453 * mbuf chain was replaced by ipfw. 454 */ 455 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 456 if (m == NULL) { 457 *m0 = m; 458 return 0; 459 } 460 if (eh != mtod(m, struct ether_header *)) 461 bcopy(&save_eh, mtod(m, struct ether_header *), 462 ETHER_HDR_LEN); 463 } 464 *m0 = m; 465 *rule = args.rule; 466 467 if (i == IP_FW_DENY) /* drop */ 468 return 0; 469 470 KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL")); 471 472 if (i == IP_FW_PASS) /* a PASS rule. */ 473 return 1; 474 475 if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) { 476 /* 477 * Pass the pkt to dummynet, which consumes it. 478 * If shared, make a copy and keep the original. 479 */ 480 if (shared) { 481 m = m_copypacket(m, M_DONTWAIT); 482 if (m == NULL) 483 return 0; 484 } else { 485 /* 486 * Pass the original to dummynet and 487 * nothing back to the caller 488 */ 489 *m0 = NULL ; 490 } 491 ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args); 492 return 0; 493 } 494 /* 495 * XXX at some point add support for divert/forward actions. 496 * If none of the above matches, we have to drop the pkt. 497 */ 498 return 0; 499 } 500 #endif 501 502 /* 503 * Process a received Ethernet packet; the packet is in the 504 * mbuf chain m with the ethernet header at the front. 505 */ 506 static void 507 ether_input(struct ifnet *ifp, struct mbuf *m) 508 { 509 struct ether_header *eh; 510 u_short etype; 511 512 /* 513 * Do consistency checks to verify assumptions 514 * made by code past this point. 515 */ 516 if ((m->m_flags & M_PKTHDR) == 0) { 517 if_printf(ifp, "discard frame w/o packet header\n"); 518 ifp->if_ierrors++; 519 m_freem(m); 520 return; 521 } 522 if (m->m_len < ETHER_HDR_LEN) { 523 /* XXX maybe should pullup? */ 524 if_printf(ifp, "discard frame w/o leading ethernet " 525 "header (len %u pkt len %u)\n", 526 m->m_len, m->m_pkthdr.len); 527 ifp->if_ierrors++; 528 m_freem(m); 529 return; 530 } 531 eh = mtod(m, struct ether_header *); 532 etype = ntohs(eh->ether_type); 533 if (m->m_pkthdr.len > 534 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) { 535 if_printf(ifp, "discard oversize frame " 536 "(ether type %x flags %x len %u > max %lu)\n", 537 etype, m->m_flags, m->m_pkthdr.len, 538 ETHER_MAX_FRAME(ifp, etype, 539 m->m_flags & M_HASFCS)); 540 ifp->if_ierrors++; 541 m_freem(m); 542 return; 543 } 544 if (m->m_pkthdr.rcvif == NULL) { 545 if_printf(ifp, "discard frame w/o interface pointer\n"); 546 ifp->if_ierrors++; 547 m_freem(m); 548 return; 549 } 550 #ifdef DIAGNOSTIC 551 if (m->m_pkthdr.rcvif != ifp) { 552 if_printf(ifp, "Warning, frame marked as received on %s\n", 553 m->m_pkthdr.rcvif->if_xname); 554 } 555 #endif 556 557 #ifdef MAC 558 /* 559 * Tag the mbuf with an appropriate MAC label before any other 560 * consumers can get to it. 561 */ 562 mac_create_mbuf_from_ifnet(ifp, m); 563 #endif 564 565 /* 566 * Give bpf a chance at the packet. 567 */ 568 BPF_MTAP(ifp, m); 569 570 if (ifp->if_flags & IFF_MONITOR) { 571 /* 572 * Interface marked for monitoring; discard packet. 573 */ 574 m_freem(m); 575 return; 576 } 577 578 /* If the CRC is still on the packet, trim it off. */ 579 if (m->m_flags & M_HASFCS) { 580 m_adj(m, -ETHER_CRC_LEN); 581 m->m_flags &= ~M_HASFCS; 582 } 583 584 ifp->if_ibytes += m->m_pkthdr.len; 585 586 /* Handle ng_ether(4) processing, if any */ 587 if (IFP2AC(ifp)->ac_netgraph != NULL) { 588 (*ng_ether_input_p)(ifp, &m); 589 if (m == NULL) 590 return; 591 } 592 593 /* 594 * Tap the packet off here for a bridge. bridge_input() 595 * will return NULL if it has consumed the packet, otherwise 596 * it gets processed as normal. Note that bridge_input() 597 * will always return the original packet if we need to 598 * process it locally. 599 */ 600 if (ifp->if_bridge) { 601 KASSERT(bridge_input_p != NULL, 602 ("ether_input: if_bridge not loaded!")); 603 604 /* Mark the packet as broadcast or multicast. This is also set 605 * further down the code in ether_demux() but since the bridge 606 * input routine rarely returns a mbuf for further processing, 607 * it is an acceptable duplication. 608 */ 609 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 610 if (bcmp(etherbroadcastaddr, eh->ether_dhost, 611 sizeof(etherbroadcastaddr)) == 0) 612 m->m_flags |= M_BCAST; 613 else 614 m->m_flags |= M_MCAST; 615 } 616 617 m = (*bridge_input_p)(ifp, m); 618 if (m == NULL) 619 return; 620 /* 621 * Bridge has determined that the packet is for us. 622 * Update our interface pointer -- we may have had 623 * to "bridge" the packet locally. 624 */ 625 ifp = m->m_pkthdr.rcvif; 626 } 627 628 /* Check for bridging mode */ 629 if (BDG_ACTIVE(ifp) ) 630 if ((m = bridge_in_ptr(ifp, m)) == NULL) 631 return; 632 633 /* First chunk of an mbuf contains good entropy */ 634 if (harvest.ethernet) 635 random_harvest(m, 16, 3, 0, RANDOM_NET); 636 ether_demux(ifp, m); 637 } 638 639 /* 640 * Upper layer processing for a received Ethernet packet. 641 */ 642 void 643 ether_demux(struct ifnet *ifp, struct mbuf *m) 644 { 645 struct ether_header *eh; 646 int isr; 647 u_short ether_type; 648 #if defined(NETATALK) 649 struct llc *l; 650 #endif 651 #if defined(INET) || defined(INET6) 652 struct ip_fw *rule = ip_dn_claim_rule(m); 653 #endif 654 655 KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer")); 656 657 eh = mtod(m, struct ether_header *); 658 ether_type = ntohs(eh->ether_type); 659 660 #if defined(INET) || defined(INET6) 661 if (rule) /* packet was already bridged */ 662 goto post_stats; 663 #endif 664 665 if (!(BDG_ACTIVE(ifp)) && 666 !((ether_type == ETHERTYPE_VLAN || m->m_flags & M_VLANTAG) && 667 ifp->if_nvlans > 0)) { 668 #ifdef DEV_CARP 669 /* 670 * XXX: Okay, we need to call carp_forus() and - if it is for 671 * us jump over code that does the normal check 672 * "ac_enaddr == ether_dhost". The check sequence is a bit 673 * different from OpenBSD, so we jump over as few code as 674 * possible, to catch _all_ sanity checks. This needs 675 * evaluation, to see if the carp ether_dhost values break any 676 * of these checks! 677 */ 678 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) 679 goto pre_stats; 680 #endif 681 /* 682 * Discard packet if upper layers shouldn't see it because it 683 * was unicast to a different Ethernet address. If the driver 684 * is working properly, then this situation can only happen 685 * when the interface is in promiscuous mode. 686 * 687 * If VLANs are active, and this packet has a VLAN tag, do 688 * not drop it here but pass it on to the VLAN layer, to 689 * give them a chance to consider it as well (e. g. in case 690 * bridging is only active on a VLAN). They will drop it if 691 * it's undesired. 692 */ 693 if ((ifp->if_flags & IFF_PROMISC) != 0 694 && !ETHER_IS_MULTICAST(eh->ether_dhost) 695 && bcmp(eh->ether_dhost, 696 IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0 697 && (ifp->if_flags & IFF_PPROMISC) == 0) { 698 m_freem(m); 699 return; 700 } 701 } 702 703 #ifdef DEV_CARP 704 pre_stats: 705 #endif 706 /* Discard packet if interface is not up */ 707 if ((ifp->if_flags & IFF_UP) == 0) { 708 m_freem(m); 709 return; 710 } 711 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 712 if (bcmp(etherbroadcastaddr, eh->ether_dhost, 713 sizeof(etherbroadcastaddr)) == 0) 714 m->m_flags |= M_BCAST; 715 else 716 m->m_flags |= M_MCAST; 717 } 718 if (m->m_flags & (M_BCAST|M_MCAST)) 719 ifp->if_imcasts++; 720 721 #if defined(INET) || defined(INET6) 722 post_stats: 723 if (IPFW_LOADED && ether_ipfw != 0) { 724 if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) { 725 if (m) 726 m_freem(m); 727 return; 728 } 729 } 730 #endif 731 732 /* 733 * Check to see if the device performed the VLAN decapsulation and 734 * provided us with the tag. 735 */ 736 if (m->m_flags & M_VLANTAG) { 737 /* 738 * If no VLANs are configured, drop. 739 */ 740 if (ifp->if_nvlans == 0) { 741 ifp->if_noproto++; 742 m_freem(m); 743 return; 744 } 745 /* 746 * vlan_input() will either recursively call ether_input() 747 * or drop the packet. 748 */ 749 KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!")); 750 (*vlan_input_p)(ifp, m); 751 return; 752 } 753 754 /* 755 * Handle protocols that expect to have the Ethernet header 756 * (and possibly FCS) intact. 757 */ 758 switch (ether_type) { 759 case ETHERTYPE_VLAN: 760 if (ifp->if_nvlans != 0) { 761 KASSERT(vlan_input_p,("ether_input: VLAN not loaded!")); 762 (*vlan_input_p)(ifp, m); 763 } else { 764 ifp->if_noproto++; 765 m_freem(m); 766 } 767 return; 768 } 769 770 /* Strip off Ethernet header. */ 771 m_adj(m, ETHER_HDR_LEN); 772 773 /* If the CRC is still on the packet, trim it off. */ 774 if (m->m_flags & M_HASFCS) { 775 m_adj(m, -ETHER_CRC_LEN); 776 m->m_flags &= ~M_HASFCS; 777 } 778 779 switch (ether_type) { 780 #ifdef INET 781 case ETHERTYPE_IP: 782 if (ip_fastforward(m)) 783 return; 784 isr = NETISR_IP; 785 break; 786 787 case ETHERTYPE_ARP: 788 if (ifp->if_flags & IFF_NOARP) { 789 /* Discard packet if ARP is disabled on interface */ 790 m_freem(m); 791 return; 792 } 793 isr = NETISR_ARP; 794 break; 795 #endif 796 #ifdef IPX 797 case ETHERTYPE_IPX: 798 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 799 return; 800 isr = NETISR_IPX; 801 break; 802 #endif 803 #ifdef INET6 804 case ETHERTYPE_IPV6: 805 isr = NETISR_IPV6; 806 break; 807 #endif 808 #ifdef NETATALK 809 case ETHERTYPE_AT: 810 isr = NETISR_ATALK1; 811 break; 812 case ETHERTYPE_AARP: 813 isr = NETISR_AARP; 814 break; 815 #endif /* NETATALK */ 816 default: 817 #ifdef IPX 818 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 819 return; 820 #endif /* IPX */ 821 #if defined(NETATALK) 822 if (ether_type > ETHERMTU) 823 goto discard; 824 l = mtod(m, struct llc *); 825 if (l->llc_dsap == LLC_SNAP_LSAP && 826 l->llc_ssap == LLC_SNAP_LSAP && 827 l->llc_control == LLC_UI) { 828 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code, 829 sizeof(at_org_code)) == 0 && 830 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 831 m_adj(m, LLC_SNAPFRAMELEN); 832 isr = NETISR_ATALK2; 833 break; 834 } 835 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 836 sizeof(aarp_org_code)) == 0 && 837 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 838 m_adj(m, LLC_SNAPFRAMELEN); 839 isr = NETISR_AARP; 840 break; 841 } 842 } 843 #endif /* NETATALK */ 844 goto discard; 845 } 846 netisr_dispatch(isr, m); 847 return; 848 849 discard: 850 /* 851 * Packet is to be discarded. If netgraph is present, 852 * hand the packet to it for last chance processing; 853 * otherwise dispose of it. 854 */ 855 if (IFP2AC(ifp)->ac_netgraph != NULL) { 856 /* 857 * Put back the ethernet header so netgraph has a 858 * consistent view of inbound packets. 859 */ 860 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 861 (*ng_ether_input_orphan_p)(ifp, m); 862 return; 863 } 864 m_freem(m); 865 } 866 867 /* 868 * Convert Ethernet address to printable (loggable) representation. 869 * This routine is for compatibility; it's better to just use 870 * 871 * printf("%6D", <pointer to address>, ":"); 872 * 873 * since there's no static buffer involved. 874 */ 875 char * 876 ether_sprintf(const u_char *ap) 877 { 878 static char etherbuf[18]; 879 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":"); 880 return (etherbuf); 881 } 882 883 /* 884 * Perform common duties while attaching to interface list 885 */ 886 void 887 ether_ifattach(struct ifnet *ifp, const u_int8_t *llc) 888 { 889 int i; 890 struct ifaddr *ifa; 891 struct sockaddr_dl *sdl; 892 893 ifp->if_type = IFT_ETHER; 894 ifp->if_addrlen = ETHER_ADDR_LEN; 895 ifp->if_hdrlen = ETHER_HDR_LEN; 896 if_attach(ifp); 897 ifp->if_mtu = ETHERMTU; 898 ifp->if_output = ether_output; 899 ifp->if_input = ether_input; 900 ifp->if_resolvemulti = ether_resolvemulti; 901 if (ifp->if_baudrate == 0) 902 ifp->if_baudrate = IF_Mbps(10); /* just a default */ 903 ifp->if_broadcastaddr = etherbroadcastaddr; 904 905 ifa = ifaddr_byindex(ifp->if_index); 906 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 907 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 908 sdl->sdl_type = IFT_ETHER; 909 sdl->sdl_alen = ifp->if_addrlen; 910 bcopy(llc, LLADDR(sdl), ifp->if_addrlen); 911 /* 912 * XXX: This doesn't belong here; we do it until 913 * XXX: all drivers are cleaned up 914 */ 915 if (llc != IFP2AC(ifp)->ac_enaddr) 916 bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen); 917 918 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 919 if (ng_ether_attach_p != NULL) 920 (*ng_ether_attach_p)(ifp); 921 if (BDG_LOADED) 922 bdgtakeifaces_ptr(); 923 924 /* Announce Ethernet MAC address if non-zero. */ 925 for (i = 0; i < ifp->if_addrlen; i++) 926 if (llc[i] != 0) 927 break; 928 if (i != ifp->if_addrlen) 929 if_printf(ifp, "Ethernet address: %6D\n", llc, ":"); 930 if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0) 931 if_printf(ifp, "if_start running deferred for Giant\n"); 932 } 933 934 /* 935 * Perform common duties while detaching an Ethernet interface 936 */ 937 void 938 ether_ifdetach(struct ifnet *ifp) 939 { 940 if (IFP2AC(ifp)->ac_netgraph != NULL) 941 (*ng_ether_detach_p)(ifp); 942 bpfdetach(ifp); 943 if_detach(ifp); 944 if (BDG_LOADED) 945 bdgtakeifaces_ptr(); 946 } 947 948 SYSCTL_DECL(_net_link); 949 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 950 #if defined(INET) || defined(INET6) 951 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, 952 ðer_ipfw,0,"Pass ether pkts through firewall"); 953 #endif 954 955 #if 0 956 /* 957 * This is for reference. We have a table-driven version 958 * of the little-endian crc32 generator, which is faster 959 * than the double-loop. 960 */ 961 uint32_t 962 ether_crc32_le(const uint8_t *buf, size_t len) 963 { 964 size_t i; 965 uint32_t crc; 966 int bit; 967 uint8_t data; 968 969 crc = 0xffffffff; /* initial value */ 970 971 for (i = 0; i < len; i++) { 972 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) 973 carry = (crc ^ data) & 1; 974 crc >>= 1; 975 if (carry) 976 crc = (crc ^ ETHER_CRC_POLY_LE); 977 } 978 979 return (crc); 980 } 981 #else 982 uint32_t 983 ether_crc32_le(const uint8_t *buf, size_t len) 984 { 985 static const uint32_t crctab[] = { 986 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 987 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 988 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 989 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 990 }; 991 size_t i; 992 uint32_t crc; 993 994 crc = 0xffffffff; /* initial value */ 995 996 for (i = 0; i < len; i++) { 997 crc ^= buf[i]; 998 crc = (crc >> 4) ^ crctab[crc & 0xf]; 999 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1000 } 1001 1002 return (crc); 1003 } 1004 #endif 1005 1006 uint32_t 1007 ether_crc32_be(const uint8_t *buf, size_t len) 1008 { 1009 size_t i; 1010 uint32_t crc, carry; 1011 int bit; 1012 uint8_t data; 1013 1014 crc = 0xffffffff; /* initial value */ 1015 1016 for (i = 0; i < len; i++) { 1017 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 1018 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); 1019 crc <<= 1; 1020 if (carry) 1021 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 1022 } 1023 } 1024 1025 return (crc); 1026 } 1027 1028 int 1029 ether_ioctl(struct ifnet *ifp, int command, caddr_t data) 1030 { 1031 struct ifaddr *ifa = (struct ifaddr *) data; 1032 struct ifreq *ifr = (struct ifreq *) data; 1033 int error = 0; 1034 1035 switch (command) { 1036 case SIOCSIFADDR: 1037 ifp->if_flags |= IFF_UP; 1038 1039 switch (ifa->ifa_addr->sa_family) { 1040 #ifdef INET 1041 case AF_INET: 1042 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 1043 arp_ifinit(ifp, ifa); 1044 break; 1045 #endif 1046 #ifdef IPX 1047 /* 1048 * XXX - This code is probably wrong 1049 */ 1050 case AF_IPX: 1051 { 1052 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 1053 struct arpcom *ac = IFP2AC(ifp); 1054 1055 if (ipx_nullhost(*ina)) 1056 ina->x_host = 1057 *(union ipx_host *) 1058 ac->ac_enaddr; 1059 else { 1060 bcopy((caddr_t) ina->x_host.c_host, 1061 (caddr_t) ac->ac_enaddr, 1062 sizeof(ac->ac_enaddr)); 1063 } 1064 1065 /* 1066 * Set new address 1067 */ 1068 ifp->if_init(ifp->if_softc); 1069 break; 1070 } 1071 #endif 1072 default: 1073 ifp->if_init(ifp->if_softc); 1074 break; 1075 } 1076 break; 1077 1078 case SIOCGIFADDR: 1079 { 1080 struct sockaddr *sa; 1081 1082 sa = (struct sockaddr *) & ifr->ifr_data; 1083 bcopy(IFP2AC(ifp)->ac_enaddr, 1084 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 1085 } 1086 break; 1087 1088 case SIOCSIFMTU: 1089 /* 1090 * Set the interface MTU. 1091 */ 1092 if (ifr->ifr_mtu > ETHERMTU) { 1093 error = EINVAL; 1094 } else { 1095 ifp->if_mtu = ifr->ifr_mtu; 1096 } 1097 break; 1098 default: 1099 error = EINVAL; /* XXX netbsd has ENOTTY??? */ 1100 break; 1101 } 1102 return (error); 1103 } 1104 1105 static int 1106 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 1107 struct sockaddr *sa) 1108 { 1109 struct sockaddr_dl *sdl; 1110 #ifdef INET 1111 struct sockaddr_in *sin; 1112 #endif 1113 #ifdef INET6 1114 struct sockaddr_in6 *sin6; 1115 #endif 1116 u_char *e_addr; 1117 1118 switch(sa->sa_family) { 1119 case AF_LINK: 1120 /* 1121 * No mapping needed. Just check that it's a valid MC address. 1122 */ 1123 sdl = (struct sockaddr_dl *)sa; 1124 e_addr = LLADDR(sdl); 1125 if (!ETHER_IS_MULTICAST(e_addr)) 1126 return EADDRNOTAVAIL; 1127 *llsa = 0; 1128 return 0; 1129 1130 #ifdef INET 1131 case AF_INET: 1132 sin = (struct sockaddr_in *)sa; 1133 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 1134 return EADDRNOTAVAIL; 1135 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1136 M_WAITOK|M_ZERO); 1137 sdl->sdl_len = sizeof *sdl; 1138 sdl->sdl_family = AF_LINK; 1139 sdl->sdl_index = ifp->if_index; 1140 sdl->sdl_type = IFT_ETHER; 1141 sdl->sdl_alen = ETHER_ADDR_LEN; 1142 e_addr = LLADDR(sdl); 1143 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 1144 *llsa = (struct sockaddr *)sdl; 1145 return 0; 1146 #endif 1147 #ifdef INET6 1148 case AF_INET6: 1149 sin6 = (struct sockaddr_in6 *)sa; 1150 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1151 /* 1152 * An IP6 address of 0 means listen to all 1153 * of the Ethernet multicast address used for IP6. 1154 * (This is used for multicast routers.) 1155 */ 1156 ifp->if_flags |= IFF_ALLMULTI; 1157 *llsa = 0; 1158 return 0; 1159 } 1160 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1161 return EADDRNOTAVAIL; 1162 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1163 M_WAITOK|M_ZERO); 1164 sdl->sdl_len = sizeof *sdl; 1165 sdl->sdl_family = AF_LINK; 1166 sdl->sdl_index = ifp->if_index; 1167 sdl->sdl_type = IFT_ETHER; 1168 sdl->sdl_alen = ETHER_ADDR_LEN; 1169 e_addr = LLADDR(sdl); 1170 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 1171 *llsa = (struct sockaddr *)sdl; 1172 return 0; 1173 #endif 1174 1175 default: 1176 /* 1177 * Well, the text isn't quite right, but it's the name 1178 * that counts... 1179 */ 1180 return EAFNOSUPPORT; 1181 } 1182 } 1183 1184 static moduledata_t ether_mod = { 1185 "ether", 1186 NULL, 1187 0 1188 }; 1189 1190 DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 1191 MODULE_VERSION(ether, 1); 1192