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