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 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/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/netisr.h> 54 #include <net/route.h> 55 #include <net/if_llc.h> 56 #include <net/if_dl.h> 57 #include <net/if_types.h> 58 #include <net/bpf.h> 59 #include <net/ethernet.h> 60 #include <net/bridge.h> 61 #include <net/if_vlan_var.h> 62 63 #if defined(INET) || defined(INET6) 64 #include <netinet/in.h> 65 #include <netinet/in_var.h> 66 #include <netinet/if_ether.h> 67 #include <netinet/ip_fw.h> 68 #include <netinet/ip_dummynet.h> 69 #endif 70 #ifdef INET6 71 #include <netinet6/nd6.h> 72 #endif 73 74 #ifdef IPX 75 #include <netipx/ipx.h> 76 #include <netipx/ipx_if.h> 77 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m); 78 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, 79 struct sockaddr *dst, short *tp, int *hlen); 80 #endif 81 82 #ifdef NETATALK 83 #include <netatalk/at.h> 84 #include <netatalk/at_var.h> 85 #include <netatalk/at_extern.h> 86 87 #define llc_snap_org_code llc_un.type_snap.org_code 88 #define llc_snap_ether_type llc_un.type_snap.ether_type 89 90 extern u_char at_org_code[3]; 91 extern u_char aarp_org_code[3]; 92 #endif /* NETATALK */ 93 94 /* netgraph node hooks for ng_ether(4) */ 95 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 96 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 97 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 98 void (*ng_ether_attach_p)(struct ifnet *ifp); 99 void (*ng_ether_detach_p)(struct ifnet *ifp); 100 101 void (*vlan_input_p)(struct ifnet *, struct mbuf *); 102 103 /* bridge support */ 104 int do_bridge; 105 bridge_in_t *bridge_in_ptr; 106 bdg_forward_t *bdg_forward_ptr; 107 bdgtakeifaces_t *bdgtakeifaces_ptr; 108 struct bdg_softc *ifp2sc; 109 110 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 111 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 112 113 static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 114 struct sockaddr *); 115 116 #define senderr(e) do { error = (e); goto bad;} while (0) 117 118 int 119 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 120 struct ip_fw **rule, int shared); 121 static int ether_ipfw; 122 123 /* 124 * Ethernet output routine. 125 * Encapsulate a packet of type family for the local net. 126 * Use trailer local net encapsulation if enough data in first 127 * packet leaves a multiple of 512 bytes of data in remainder. 128 * Assumes that ifp is actually pointer to arpcom structure. 129 */ 130 int 131 ether_output(struct ifnet *ifp, struct mbuf *m, 132 struct sockaddr *dst, struct rtentry *rt0) 133 { 134 short type; 135 int error, hdrcmplt = 0; 136 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN]; 137 struct ether_header *eh; 138 int loop_copy = 0; 139 int hlen; /* link layer header length */ 140 141 #ifdef MAC 142 error = mac_check_ifnet_transmit(ifp, m); 143 if (error) 144 senderr(error); 145 #endif 146 147 if (ifp->if_flags & IFF_MONITOR) 148 senderr(ENETDOWN); 149 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 150 senderr(ENETDOWN); 151 152 hlen = ETHER_HDR_LEN; 153 switch (dst->sa_family) { 154 #ifdef INET 155 case AF_INET: 156 error = arpresolve(ifp, rt0, m, dst, edst); 157 if (error) 158 return (error == EWOULDBLOCK ? 0 : error); 159 type = htons(ETHERTYPE_IP); 160 break; 161 case AF_ARP: 162 { 163 struct arphdr *ah; 164 ah = mtod(m, struct arphdr *); 165 ah->ar_hrd = htons(ARPHRD_ETHER); 166 167 loop_copy = -1; /* if this is for us, don't do it */ 168 169 switch(ntohs(ah->ar_op)) { 170 case ARPOP_REVREQUEST: 171 case ARPOP_REVREPLY: 172 type = htons(ETHERTYPE_REVARP); 173 break; 174 case ARPOP_REQUEST: 175 case ARPOP_REPLY: 176 default: 177 type = htons(ETHERTYPE_ARP); 178 break; 179 } 180 181 if (m->m_flags & M_BCAST) 182 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN); 183 else 184 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN); 185 186 } 187 break; 188 #endif 189 #ifdef INET6 190 case AF_INET6: 191 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst); 192 if (error) 193 return error; 194 type = htons(ETHERTYPE_IPV6); 195 break; 196 #endif 197 #ifdef IPX 198 case AF_IPX: 199 if (ef_outputp) { 200 error = ef_outputp(ifp, &m, dst, &type, &hlen); 201 if (error) 202 goto bad; 203 } else 204 type = htons(ETHERTYPE_IPX); 205 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 206 (caddr_t)edst, sizeof (edst)); 207 break; 208 #endif 209 #ifdef NETATALK 210 case AF_APPLETALK: 211 { 212 struct at_ifaddr *aa; 213 214 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) 215 senderr(EHOSTUNREACH); /* XXX */ 216 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) 217 return (0); 218 /* 219 * In the phase 2 case, need to prepend an mbuf for the llc header. 220 */ 221 if ( aa->aa_flags & AFA_PHASE2 ) { 222 struct llc llc; 223 224 M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT); 225 if (m == NULL) 226 senderr(ENOBUFS); 227 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 228 llc.llc_control = LLC_UI; 229 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 230 llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 231 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN); 232 type = htons(m->m_pkthdr.len); 233 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN; 234 } else { 235 type = htons(ETHERTYPE_AT); 236 } 237 break; 238 } 239 #endif /* NETATALK */ 240 241 case pseudo_AF_HDRCMPLT: 242 hdrcmplt = 1; 243 eh = (struct ether_header *)dst->sa_data; 244 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc)); 245 /* FALLTHROUGH */ 246 247 case AF_UNSPEC: 248 loop_copy = -1; /* if this is for us, don't do it */ 249 eh = (struct ether_header *)dst->sa_data; 250 (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 251 type = eh->ether_type; 252 break; 253 254 default: 255 if_printf(ifp, "can't handle af%d\n", dst->sa_family); 256 senderr(EAFNOSUPPORT); 257 } 258 259 /* 260 * Add local net header. If no space in first mbuf, 261 * allocate another. 262 */ 263 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 264 if (m == NULL) 265 senderr(ENOBUFS); 266 eh = mtod(m, struct ether_header *); 267 (void)memcpy(&eh->ether_type, &type, 268 sizeof(eh->ether_type)); 269 (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 270 if (hdrcmplt) 271 (void)memcpy(eh->ether_shost, esrc, 272 sizeof(eh->ether_shost)); 273 else 274 (void)memcpy(eh->ether_shost, IFP2AC(ifp)->ac_enaddr, 275 sizeof(eh->ether_shost)); 276 277 /* 278 * If a simplex interface, and the packet is being sent to our 279 * Ethernet address or a broadcast address, loopback a copy. 280 * XXX To make a simplex device behave exactly like a duplex 281 * device, we should copy in the case of sending to our own 282 * ethernet address (thus letting the original actually appear 283 * on the wire). However, we don't do that here for security 284 * reasons and compatibility with the original behavior. 285 */ 286 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 287 int csum_flags = 0; 288 289 if (m->m_pkthdr.csum_flags & CSUM_IP) 290 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 291 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 292 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 293 294 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 295 struct mbuf *n; 296 297 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 298 n->m_pkthdr.csum_flags |= csum_flags; 299 if (csum_flags & CSUM_DATA_VALID) 300 n->m_pkthdr.csum_data = 0xffff; 301 (void)if_simloop(ifp, n, dst->sa_family, hlen); 302 } else 303 ifp->if_iqdrops++; 304 } else if (bcmp(eh->ether_dhost, eh->ether_shost, 305 ETHER_ADDR_LEN) == 0) { 306 m->m_pkthdr.csum_flags |= csum_flags; 307 if (csum_flags & CSUM_DATA_VALID) 308 m->m_pkthdr.csum_data = 0xffff; 309 (void) if_simloop(ifp, m, dst->sa_family, hlen); 310 return (0); /* XXX */ 311 } 312 } 313 314 /* Handle ng_ether(4) processing, if any */ 315 if (ng_ether_output_p != NULL) { 316 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 317 bad: if (m != NULL) 318 m_freem(m); 319 return (error); 320 } 321 if (m == NULL) 322 return (0); 323 } 324 325 /* Continue with link-layer output */ 326 return ether_output_frame(ifp, m); 327 } 328 329 /* 330 * Ethernet link layer output routine to send a raw frame to the device. 331 * 332 * This assumes that the 14 byte Ethernet header is present and contiguous 333 * in the first mbuf (if BRIDGE'ing). 334 */ 335 int 336 ether_output_frame(struct ifnet *ifp, struct mbuf *m) 337 { 338 struct ip_fw *rule = ip_dn_claim_rule(m); 339 340 if (rule == NULL && BDG_ACTIVE(ifp)) { 341 /* 342 * Beware, the bridge code notices the null rcvif and 343 * uses that identify that it's being called from 344 * ether_output as opposd to ether_input. Yech. 345 */ 346 m->m_pkthdr.rcvif = NULL; 347 m = bdg_forward_ptr(m, ifp); 348 if (m != NULL) 349 m_freem(m); 350 return (0); 351 } 352 if (IPFW_LOADED && ether_ipfw != 0) { 353 if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) { 354 if (m) { 355 m_freem(m); 356 return EACCES; /* pkt dropped */ 357 } else 358 return 0; /* consumed e.g. in a pipe */ 359 } 360 } 361 362 /* 363 * Queue message on interface, update output statistics if 364 * successful, and start output if interface not yet active. 365 */ 366 return (IF_HANDOFF(&ifp->if_snd, m, ifp) ? 0 : ENOBUFS); 367 } 368 369 /* 370 * ipfw processing for ethernet packets (in and out). 371 * The second parameter is NULL from ether_demux, and ifp from 372 * ether_output_frame. This section of code could be used from 373 * bridge.c as well as long as we use some extra info 374 * to distinguish that case from ether_output_frame(); 375 */ 376 int 377 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 378 struct ip_fw **rule, int shared) 379 { 380 struct ether_header *eh; 381 struct ether_header save_eh; 382 struct mbuf *m; 383 int i; 384 struct ip_fw_args args; 385 386 if (*rule != NULL && fw_one_pass) 387 return 1; /* dummynet packet, already partially processed */ 388 389 /* 390 * I need some amt of data to be contiguous, and in case others need 391 * the packet (shared==1) also better be in the first mbuf. 392 */ 393 m = *m0; 394 i = min( m->m_pkthdr.len, max_protohdr); 395 if ( shared || m->m_len < i) { 396 m = m_pullup(m, i); 397 if (m == NULL) { 398 *m0 = m; 399 return 0; 400 } 401 } 402 eh = mtod(m, struct ether_header *); 403 save_eh = *eh; /* save copy for restore below */ 404 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */ 405 406 args.m = m; /* the packet we are looking at */ 407 args.oif = dst; /* destination, if any */ 408 args.rule = *rule; /* matching rule to restart */ 409 args.next_hop = NULL; /* we do not support forward yet */ 410 args.eh = &save_eh; /* MAC header for bridged/MAC packets */ 411 i = ip_fw_chk_ptr(&args); 412 m = args.m; 413 if (m != NULL) { 414 /* 415 * Restore Ethernet header, as needed, in case the 416 * mbuf chain was replaced by ipfw. 417 */ 418 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 419 if (m == NULL) { 420 *m0 = m; 421 return 0; 422 } 423 if (eh != mtod(m, struct ether_header *)) 424 bcopy(&save_eh, mtod(m, struct ether_header *), 425 ETHER_HDR_LEN); 426 } 427 *m0 = m; 428 *rule = args.rule; 429 430 if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) /* drop */ 431 return 0; 432 433 if (i == 0) /* a PASS rule. */ 434 return 1; 435 436 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) { 437 /* 438 * Pass the pkt to dummynet, which consumes it. 439 * If shared, make a copy and keep the original. 440 */ 441 if (shared) { 442 m = m_copypacket(m, M_DONTWAIT); 443 if (m == NULL) 444 return 0; 445 } else { 446 /* 447 * Pass the original to dummynet and 448 * nothing back to the caller 449 */ 450 *m0 = NULL ; 451 } 452 ip_dn_io_ptr(m, (i & 0xffff), 453 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args); 454 return 0; 455 } 456 /* 457 * XXX at some point add support for divert/forward actions. 458 * If none of the above matches, we have to drop the pkt. 459 */ 460 return 0; 461 } 462 463 /* 464 * Process a received Ethernet packet; the packet is in the 465 * mbuf chain m with the ethernet header at the front. 466 */ 467 static void 468 ether_input(struct ifnet *ifp, struct mbuf *m) 469 { 470 struct ether_header *eh; 471 u_short etype; 472 473 /* 474 * Do consistency checks to verify assumptions 475 * made by code past this point. 476 */ 477 if ((m->m_flags & M_PKTHDR) == 0) { 478 if_printf(ifp, "discard frame w/o packet header\n"); 479 ifp->if_ierrors++; 480 m_freem(m); 481 return; 482 } 483 if (m->m_len < ETHER_HDR_LEN) { 484 /* XXX maybe should pullup? */ 485 if_printf(ifp, "discard frame w/o leading ethernet " 486 "header (len %u pkt len %u)\n", 487 m->m_len, m->m_pkthdr.len); 488 ifp->if_ierrors++; 489 m_freem(m); 490 return; 491 } 492 eh = mtod(m, struct ether_header *); 493 etype = ntohs(eh->ether_type); 494 if (m->m_pkthdr.len > 495 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) { 496 if_printf(ifp, "discard oversize frame " 497 "(ether type %x flags %x len %u > max %lu)\n", 498 etype, m->m_flags, m->m_pkthdr.len, 499 ETHER_MAX_FRAME(ifp, etype, 500 m->m_flags & M_HASFCS)); 501 ifp->if_ierrors++; 502 m_freem(m); 503 return; 504 } 505 if (m->m_pkthdr.rcvif == NULL) { 506 if_printf(ifp, "discard frame w/o interface pointer\n"); 507 ifp->if_ierrors++; 508 m_freem(m); 509 return; 510 } 511 #ifdef DIAGNOSTIC 512 if (m->m_pkthdr.rcvif != ifp) { 513 if_printf(ifp, "Warning, frame marked as received on %s\n", 514 m->m_pkthdr.rcvif->if_xname); 515 } 516 #endif 517 518 #ifdef MAC 519 /* 520 * Tag the mbuf with an appropriate MAC label before any other 521 * consumers can get to it. 522 */ 523 mac_create_mbuf_from_ifnet(ifp, m); 524 #endif 525 526 /* 527 * Give bpf a chance at the packet. 528 */ 529 BPF_MTAP(ifp, m); 530 531 if (ifp->if_flags & IFF_MONITOR) { 532 /* 533 * Interface marked for monitoring; discard packet. 534 */ 535 m_freem(m); 536 return; 537 } 538 539 /* If the CRC is still on the packet, trim it off. */ 540 if (m->m_flags & M_HASFCS) { 541 m_adj(m, -ETHER_CRC_LEN); 542 m->m_flags &= ~M_HASFCS; 543 } 544 545 ifp->if_ibytes += m->m_pkthdr.len; 546 547 /* Handle ng_ether(4) processing, if any */ 548 if (ng_ether_input_p != NULL) { 549 (*ng_ether_input_p)(ifp, &m); 550 if (m == NULL) 551 return; 552 } 553 554 /* Check for bridging mode */ 555 if (BDG_ACTIVE(ifp) ) { 556 struct ifnet *bif; 557 558 /* 559 * Check with bridging code to see how the packet 560 * should be handled. Possibilities are: 561 * 562 * BDG_BCAST broadcast 563 * BDG_MCAST multicast 564 * BDG_LOCAL for local address, don't forward 565 * BDG_DROP discard 566 * ifp forward only to specified interface(s) 567 * 568 * Non-local destinations are handled by passing the 569 * packet back to the bridge code. 570 */ 571 bif = bridge_in_ptr(ifp, eh); 572 if (bif == BDG_DROP) { /* discard packet */ 573 m_freem(m); 574 return; 575 } 576 if (bif != BDG_LOCAL) { /* non-local, forward */ 577 m = bdg_forward_ptr(m, bif); 578 /* 579 * The bridge may consume the packet if it's not 580 * supposed to be passed up or if a problem occurred 581 * while doing its job. This is reflected by it 582 * returning a NULL mbuf pointer. 583 */ 584 if (m == NULL) { 585 if (bif == BDG_BCAST || bif == BDG_MCAST) 586 if_printf(ifp, 587 "bridge dropped %s packet\n", 588 bif == BDG_BCAST ? "broadcast" : 589 "multicast"); 590 return; 591 } 592 /* 593 * But in some cases the bridge may return the 594 * packet for us to free; sigh. 595 */ 596 if (bif != BDG_BCAST && bif != BDG_MCAST) { 597 m_freem(m); 598 return; 599 } 600 } 601 } 602 603 ether_demux(ifp, m); 604 /* First chunk of an mbuf contains good entropy */ 605 if (harvest.ethernet) 606 random_harvest(m, 16, 3, 0, RANDOM_NET); 607 } 608 609 /* 610 * Upper layer processing for a received Ethernet packet. 611 */ 612 void 613 ether_demux(struct ifnet *ifp, struct mbuf *m) 614 { 615 struct ether_header *eh; 616 int isr; 617 u_short ether_type; 618 #if defined(NETATALK) 619 struct llc *l; 620 #endif 621 struct ip_fw *rule = ip_dn_claim_rule(m); 622 623 KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer")); 624 625 eh = mtod(m, struct ether_header *); 626 627 if (rule) /* packet was already bridged */ 628 goto post_stats; 629 630 if (!(BDG_ACTIVE(ifp))) { 631 /* 632 * Discard packet if upper layers shouldn't see it because it 633 * was unicast to a different Ethernet address. If the driver 634 * is working properly, then this situation can only happen 635 * when the interface is in promiscuous mode. 636 */ 637 if ((ifp->if_flags & IFF_PROMISC) != 0 638 && (eh->ether_dhost[0] & 1) == 0 639 && bcmp(eh->ether_dhost, 640 IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0 641 && (ifp->if_flags & IFF_PPROMISC) == 0) { 642 m_freem(m); 643 return; 644 } 645 } 646 647 /* Discard packet if interface is not up */ 648 if ((ifp->if_flags & IFF_UP) == 0) { 649 m_freem(m); 650 return; 651 } 652 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 653 if (bcmp(etherbroadcastaddr, eh->ether_dhost, 654 sizeof(etherbroadcastaddr)) == 0) 655 m->m_flags |= M_BCAST; 656 else 657 m->m_flags |= M_MCAST; 658 } 659 if (m->m_flags & (M_BCAST|M_MCAST)) 660 ifp->if_imcasts++; 661 662 post_stats: 663 if (IPFW_LOADED && ether_ipfw != 0) { 664 if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) { 665 if (m) 666 m_freem(m); 667 return; 668 } 669 } 670 671 /* 672 * If VLANs are configured on the interface, check to 673 * see if the device performed the decapsulation and 674 * provided us with the tag. 675 */ 676 if (ifp->if_nvlans && 677 m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) { 678 /* 679 * vlan_input() will either recursively call ether_input() 680 * or drop the packet. 681 */ 682 KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!")); 683 (*vlan_input_p)(ifp, m); 684 return; 685 } 686 687 ether_type = ntohs(eh->ether_type); 688 689 /* 690 * Handle protocols that expect to have the Ethernet header 691 * (and possibly FCS) intact. 692 */ 693 switch (ether_type) { 694 case ETHERTYPE_VLAN: 695 if (ifp->if_nvlans != 0) { 696 KASSERT(vlan_input_p,("ether_input: VLAN not loaded!")); 697 (*vlan_input_p)(ifp, m); 698 } else { 699 ifp->if_noproto++; 700 m_freem(m); 701 } 702 return; 703 } 704 705 /* Strip off Ethernet header. */ 706 m_adj(m, ETHER_HDR_LEN); 707 708 /* If the CRC is still on the packet, trim it off. */ 709 if (m->m_flags & M_HASFCS) { 710 m_adj(m, -ETHER_CRC_LEN); 711 m->m_flags &= ~M_HASFCS; 712 } 713 714 switch (ether_type) { 715 #ifdef INET 716 case ETHERTYPE_IP: 717 if (ip_fastforward(m)) 718 return; 719 isr = NETISR_IP; 720 break; 721 722 case ETHERTYPE_ARP: 723 if (ifp->if_flags & IFF_NOARP) { 724 /* Discard packet if ARP is disabled on interface */ 725 m_freem(m); 726 return; 727 } 728 isr = NETISR_ARP; 729 break; 730 #endif 731 #ifdef IPX 732 case ETHERTYPE_IPX: 733 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 734 return; 735 isr = NETISR_IPX; 736 break; 737 #endif 738 #ifdef INET6 739 case ETHERTYPE_IPV6: 740 isr = NETISR_IPV6; 741 break; 742 #endif 743 #ifdef NETATALK 744 case ETHERTYPE_AT: 745 isr = NETISR_ATALK1; 746 break; 747 case ETHERTYPE_AARP: 748 isr = NETISR_AARP; 749 break; 750 #endif /* NETATALK */ 751 default: 752 #ifdef IPX 753 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 754 return; 755 #endif /* IPX */ 756 #if defined(NETATALK) 757 if (ether_type > ETHERMTU) 758 goto discard; 759 l = mtod(m, struct llc *); 760 if (l->llc_dsap == LLC_SNAP_LSAP && 761 l->llc_ssap == LLC_SNAP_LSAP && 762 l->llc_control == LLC_UI) { 763 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code, 764 sizeof(at_org_code)) == 0 && 765 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 766 m_adj(m, LLC_SNAPFRAMELEN); 767 isr = NETISR_ATALK2; 768 break; 769 } 770 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 771 sizeof(aarp_org_code)) == 0 && 772 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 773 m_adj(m, LLC_SNAPFRAMELEN); 774 isr = NETISR_AARP; 775 break; 776 } 777 } 778 #endif /* NETATALK */ 779 goto discard; 780 } 781 netisr_dispatch(isr, m); 782 return; 783 784 discard: 785 /* 786 * Packet is to be discarded. If netgraph is present, 787 * hand the packet to it for last chance processing; 788 * otherwise dispose of it. 789 */ 790 if (ng_ether_input_orphan_p != NULL) { 791 /* 792 * Put back the ethernet header so netgraph has a 793 * consistent view of inbound packets. 794 */ 795 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 796 (*ng_ether_input_orphan_p)(ifp, m); 797 return; 798 } 799 m_freem(m); 800 } 801 802 /* 803 * Convert Ethernet address to printable (loggable) representation. 804 * This routine is for compatibility; it's better to just use 805 * 806 * printf("%6D", <pointer to address>, ":"); 807 * 808 * since there's no static buffer involved. 809 */ 810 char * 811 ether_sprintf(const u_char *ap) 812 { 813 static char etherbuf[18]; 814 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":"); 815 return (etherbuf); 816 } 817 818 /* 819 * Perform common duties while attaching to interface list 820 */ 821 void 822 ether_ifattach(struct ifnet *ifp, const u_int8_t *llc) 823 { 824 struct ifaddr *ifa; 825 struct sockaddr_dl *sdl; 826 827 ifp->if_type = IFT_ETHER; 828 ifp->if_addrlen = ETHER_ADDR_LEN; 829 ifp->if_hdrlen = ETHER_HDR_LEN; 830 if_attach(ifp); 831 ifp->if_mtu = ETHERMTU; 832 ifp->if_output = ether_output; 833 ifp->if_input = ether_input; 834 ifp->if_resolvemulti = ether_resolvemulti; 835 if (ifp->if_baudrate == 0) 836 ifp->if_baudrate = IF_Mbps(10); /* just a default */ 837 ifp->if_broadcastaddr = etherbroadcastaddr; 838 839 ifa = ifaddr_byindex(ifp->if_index); 840 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 841 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 842 sdl->sdl_type = IFT_ETHER; 843 sdl->sdl_alen = ifp->if_addrlen; 844 bcopy(llc, LLADDR(sdl), ifp->if_addrlen); 845 /* 846 * XXX: This doesn't belong here; we do it until 847 * XXX: all drivers are cleaned up 848 */ 849 if (llc != IFP2AC(ifp)->ac_enaddr) 850 bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen); 851 852 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 853 if (ng_ether_attach_p != NULL) 854 (*ng_ether_attach_p)(ifp); 855 if (BDG_LOADED) 856 bdgtakeifaces_ptr(); 857 858 /* Announce Ethernet MAC address. */ 859 if_printf(ifp, "Ethernet address: %6D\n", llc, ":"); 860 } 861 862 /* 863 * Perform common duties while detaching an Ethernet interface 864 */ 865 void 866 ether_ifdetach(struct ifnet *ifp) 867 { 868 if (ng_ether_detach_p != NULL) 869 (*ng_ether_detach_p)(ifp); 870 bpfdetach(ifp); 871 if_detach(ifp); 872 if (BDG_LOADED) 873 bdgtakeifaces_ptr(); 874 } 875 876 SYSCTL_DECL(_net_link); 877 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 878 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, 879 ðer_ipfw,0,"Pass ether pkts through firewall"); 880 881 int 882 ether_ioctl(struct ifnet *ifp, int command, caddr_t data) 883 { 884 struct ifaddr *ifa = (struct ifaddr *) data; 885 struct ifreq *ifr = (struct ifreq *) data; 886 int error = 0; 887 888 switch (command) { 889 case SIOCSIFADDR: 890 ifp->if_flags |= IFF_UP; 891 892 switch (ifa->ifa_addr->sa_family) { 893 #ifdef INET 894 case AF_INET: 895 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 896 arp_ifinit(ifp, ifa); 897 break; 898 #endif 899 #ifdef IPX 900 /* 901 * XXX - This code is probably wrong 902 */ 903 case AF_IPX: 904 { 905 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 906 struct arpcom *ac = IFP2AC(ifp); 907 908 if (ipx_nullhost(*ina)) 909 ina->x_host = 910 *(union ipx_host *) 911 ac->ac_enaddr; 912 else { 913 bcopy((caddr_t) ina->x_host.c_host, 914 (caddr_t) ac->ac_enaddr, 915 sizeof(ac->ac_enaddr)); 916 } 917 918 /* 919 * Set new address 920 */ 921 ifp->if_init(ifp->if_softc); 922 break; 923 } 924 #endif 925 default: 926 ifp->if_init(ifp->if_softc); 927 break; 928 } 929 break; 930 931 case SIOCGIFADDR: 932 { 933 struct sockaddr *sa; 934 935 sa = (struct sockaddr *) & ifr->ifr_data; 936 bcopy(IFP2AC(ifp)->ac_enaddr, 937 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 938 } 939 break; 940 941 case SIOCSIFMTU: 942 /* 943 * Set the interface MTU. 944 */ 945 if (ifr->ifr_mtu > ETHERMTU) { 946 error = EINVAL; 947 } else { 948 ifp->if_mtu = ifr->ifr_mtu; 949 } 950 break; 951 default: 952 error = EINVAL; /* XXX netbsd has ENOTTY??? */ 953 break; 954 } 955 return (error); 956 } 957 958 static int 959 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 960 struct sockaddr *sa) 961 { 962 struct sockaddr_dl *sdl; 963 struct sockaddr_in *sin; 964 #ifdef INET6 965 struct sockaddr_in6 *sin6; 966 #endif 967 u_char *e_addr; 968 969 switch(sa->sa_family) { 970 case AF_LINK: 971 /* 972 * No mapping needed. Just check that it's a valid MC address. 973 */ 974 sdl = (struct sockaddr_dl *)sa; 975 e_addr = LLADDR(sdl); 976 if ((e_addr[0] & 1) != 1) 977 return EADDRNOTAVAIL; 978 *llsa = 0; 979 return 0; 980 981 #ifdef INET 982 case AF_INET: 983 sin = (struct sockaddr_in *)sa; 984 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 985 return EADDRNOTAVAIL; 986 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 987 M_WAITOK|M_ZERO); 988 sdl->sdl_len = sizeof *sdl; 989 sdl->sdl_family = AF_LINK; 990 sdl->sdl_index = ifp->if_index; 991 sdl->sdl_type = IFT_ETHER; 992 sdl->sdl_alen = ETHER_ADDR_LEN; 993 e_addr = LLADDR(sdl); 994 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 995 *llsa = (struct sockaddr *)sdl; 996 return 0; 997 #endif 998 #ifdef INET6 999 case AF_INET6: 1000 sin6 = (struct sockaddr_in6 *)sa; 1001 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1002 /* 1003 * An IP6 address of 0 means listen to all 1004 * of the Ethernet multicast address used for IP6. 1005 * (This is used for multicast routers.) 1006 */ 1007 ifp->if_flags |= IFF_ALLMULTI; 1008 *llsa = 0; 1009 return 0; 1010 } 1011 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1012 return EADDRNOTAVAIL; 1013 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1014 M_WAITOK|M_ZERO); 1015 sdl->sdl_len = sizeof *sdl; 1016 sdl->sdl_family = AF_LINK; 1017 sdl->sdl_index = ifp->if_index; 1018 sdl->sdl_type = IFT_ETHER; 1019 sdl->sdl_alen = ETHER_ADDR_LEN; 1020 e_addr = LLADDR(sdl); 1021 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 1022 *llsa = (struct sockaddr *)sdl; 1023 return 0; 1024 #endif 1025 1026 default: 1027 /* 1028 * Well, the text isn't quite right, but it's the name 1029 * that counts... 1030 */ 1031 return EAFNOSUPPORT; 1032 } 1033 } 1034 1035 static moduledata_t ether_mod = { 1036 "ether", 1037 NULL, 1038 0 1039 }; 1040 1041 DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 1042 MODULE_VERSION(ether, 1); 1043