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