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