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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 34 * $FreeBSD$ 35 */ 36 37 #include "opt_atalk.h" 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 #include "opt_ipx.h" 41 #include "opt_bdg.h" 42 #include "opt_mac.h" 43 #include "opt_netgraph.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/mac.h> 49 #include <sys/malloc.h> 50 #include <sys/mbuf.h> 51 #include <sys/random.h> 52 #include <sys/socket.h> 53 #include <sys/sockio.h> 54 #include <sys/sysctl.h> 55 56 #include <net/if.h> 57 #include <net/netisr.h> 58 #include <net/route.h> 59 #include <net/if_llc.h> 60 #include <net/if_dl.h> 61 #include <net/if_types.h> 62 #include <net/bpf.h> 63 #include <net/ethernet.h> 64 #include <net/bridge.h> 65 #include <net/if_vlan_var.h> 66 67 #if defined(INET) || defined(INET6) 68 #include <netinet/in.h> 69 #include <netinet/in_var.h> 70 #include <netinet/if_ether.h> 71 #include <netinet/ip_fw.h> 72 #include <netinet/ip_dummynet.h> 73 #endif 74 #ifdef INET6 75 #include <netinet6/nd6.h> 76 #endif 77 78 #ifdef IPX 79 #include <netipx/ipx.h> 80 #include <netipx/ipx_if.h> 81 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m); 82 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, 83 struct sockaddr *dst, short *tp, int *hlen); 84 #endif 85 86 #ifdef NETATALK 87 #include <netatalk/at.h> 88 #include <netatalk/at_var.h> 89 #include <netatalk/at_extern.h> 90 91 #define llc_snap_org_code llc_un.type_snap.org_code 92 #define llc_snap_ether_type llc_un.type_snap.ether_type 93 94 extern u_char at_org_code[3]; 95 extern u_char aarp_org_code[3]; 96 #endif /* NETATALK */ 97 98 /* netgraph node hooks for ng_ether(4) */ 99 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 100 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 101 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 102 void (*ng_ether_attach_p)(struct ifnet *ifp); 103 void (*ng_ether_detach_p)(struct ifnet *ifp); 104 105 void (*vlan_input_p)(struct ifnet *, struct mbuf *); 106 107 /* bridge support */ 108 int do_bridge; 109 bridge_in_t *bridge_in_ptr; 110 bdg_forward_t *bdg_forward_ptr; 111 bdgtakeifaces_t *bdgtakeifaces_ptr; 112 struct bdg_softc *ifp2sc; 113 114 static u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 115 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 116 117 static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 118 struct sockaddr *); 119 120 #define senderr(e) do { error = (e); goto bad;} while (0) 121 #define IFP2AC(IFP) ((struct arpcom *)IFP) 122 123 int 124 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 125 struct ip_fw **rule, int shared); 126 static int ether_ipfw; 127 128 /* 129 * Ethernet output routine. 130 * Encapsulate a packet of type family for the local net. 131 * Use trailer local net encapsulation if enough data in first 132 * packet leaves a multiple of 512 bytes of data in remainder. 133 * Assumes that ifp is actually pointer to arpcom structure. 134 */ 135 int 136 ether_output(struct ifnet *ifp, struct mbuf *m, 137 struct sockaddr *dst, struct rtentry *rt0) 138 { 139 short type; 140 int error = 0, hdrcmplt = 0; 141 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN]; 142 struct rtentry *rt; 143 struct ether_header *eh; 144 int loop_copy = 0; 145 int hlen; /* link layer header length */ 146 struct arpcom *ac = IFP2AC(ifp); 147 148 #ifdef MAC 149 error = mac_check_ifnet_transmit(ifp, m); 150 if (error) 151 senderr(error); 152 #endif 153 154 if (ifp->if_flags & IFF_MONITOR) 155 senderr(ENETDOWN); 156 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 157 senderr(ENETDOWN); 158 159 error = rt_check(&rt, &rt0, dst); 160 if (error) 161 goto bad; 162 163 hlen = ETHER_HDR_LEN; 164 switch (dst->sa_family) { 165 #ifdef INET 166 case AF_INET: 167 if (!arpresolve(ifp, rt, m, dst, edst, rt0)) 168 return (0); /* if not yet resolved */ 169 type = htons(ETHERTYPE_IP); 170 break; 171 #endif 172 #ifdef INET6 173 case AF_INET6: 174 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) { 175 /* Something bad happened */ 176 return(0); 177 } 178 type = htons(ETHERTYPE_IPV6); 179 break; 180 #endif 181 #ifdef IPX 182 case AF_IPX: 183 if (ef_outputp) { 184 error = ef_outputp(ifp, &m, dst, &type, &hlen); 185 if (error) 186 goto bad; 187 } else 188 type = htons(ETHERTYPE_IPX); 189 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), 190 (caddr_t)edst, sizeof (edst)); 191 break; 192 #endif 193 #ifdef NETATALK 194 case AF_APPLETALK: 195 { 196 struct at_ifaddr *aa; 197 198 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { 199 goto bad; 200 } 201 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) 202 return (0); 203 /* 204 * In the phase 2 case, need to prepend an mbuf for the llc header. 205 * Since we must preserve the value of m, which is passed to us by 206 * value, we m_copy() the first mbuf, and use it for our llc header. 207 */ 208 if ( aa->aa_flags & AFA_PHASE2 ) { 209 struct llc llc; 210 211 M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT); 212 if (m == NULL) 213 senderr(ENOBUFS); 214 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; 215 llc.llc_control = LLC_UI; 216 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code)); 217 llc.llc_snap_ether_type = htons( ETHERTYPE_AT ); 218 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN); 219 type = htons(m->m_pkthdr.len); 220 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN; 221 } else { 222 type = htons(ETHERTYPE_AT); 223 } 224 break; 225 } 226 #endif /* NETATALK */ 227 228 case pseudo_AF_HDRCMPLT: 229 hdrcmplt = 1; 230 eh = (struct ether_header *)dst->sa_data; 231 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc)); 232 /* FALLTHROUGH */ 233 234 case AF_UNSPEC: 235 loop_copy = -1; /* if this is for us, don't do it */ 236 eh = (struct ether_header *)dst->sa_data; 237 (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 238 type = eh->ether_type; 239 break; 240 241 default: 242 if_printf(ifp, "can't handle af%d\n", dst->sa_family); 243 senderr(EAFNOSUPPORT); 244 } 245 246 /* 247 * Add local net header. If no space in first mbuf, 248 * allocate another. 249 */ 250 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 251 if (m == NULL) 252 senderr(ENOBUFS); 253 eh = mtod(m, struct ether_header *); 254 (void)memcpy(&eh->ether_type, &type, 255 sizeof(eh->ether_type)); 256 (void)memcpy(eh->ether_dhost, edst, sizeof (edst)); 257 if (hdrcmplt) 258 (void)memcpy(eh->ether_shost, esrc, 259 sizeof(eh->ether_shost)); 260 else 261 (void)memcpy(eh->ether_shost, ac->ac_enaddr, 262 sizeof(eh->ether_shost)); 263 264 /* 265 * If a simplex interface, and the packet is being sent to our 266 * Ethernet address or a broadcast address, loopback a copy. 267 * XXX To make a simplex device behave exactly like a duplex 268 * device, we should copy in the case of sending to our own 269 * ethernet address (thus letting the original actually appear 270 * on the wire). However, we don't do that here for security 271 * reasons and compatibility with the original behavior. 272 */ 273 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 274 int csum_flags = 0; 275 276 if (m->m_pkthdr.csum_flags & CSUM_IP) 277 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 278 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 279 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 280 281 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 282 struct mbuf *n; 283 284 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 285 n->m_pkthdr.csum_flags |= csum_flags; 286 if (csum_flags & CSUM_DATA_VALID) 287 n->m_pkthdr.csum_data = 0xffff; 288 (void)if_simloop(ifp, n, dst->sa_family, hlen); 289 } else 290 ifp->if_iqdrops++; 291 } else if (bcmp(eh->ether_dhost, eh->ether_shost, 292 ETHER_ADDR_LEN) == 0) { 293 m->m_pkthdr.csum_flags |= csum_flags; 294 if (csum_flags & CSUM_DATA_VALID) 295 m->m_pkthdr.csum_data = 0xffff; 296 (void) if_simloop(ifp, m, dst->sa_family, hlen); 297 return (0); /* XXX */ 298 } 299 } 300 301 /* Handle ng_ether(4) processing, if any */ 302 if (ng_ether_output_p != NULL) { 303 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 304 bad: if (m != NULL) 305 m_freem(m); 306 return (error); 307 } 308 if (m == NULL) 309 return (0); 310 } 311 312 /* Continue with link-layer output */ 313 return ether_output_frame(ifp, m); 314 } 315 316 /* 317 * Ethernet link layer output routine to send a raw frame to the device. 318 * 319 * This assumes that the 14 byte Ethernet header is present and contiguous 320 * in the first mbuf (if BRIDGE'ing). 321 */ 322 int 323 ether_output_frame(struct ifnet *ifp, struct mbuf *m) 324 { 325 struct ip_fw *rule = NULL; 326 327 /* Extract info from dummynet tag, ignore others */ 328 for (; m->m_type == MT_TAG; m = m->m_next) 329 if (m->m_flags == PACKET_TAG_DUMMYNET) 330 rule = ((struct dn_pkt *)m)->rule; 331 332 if (rule == NULL && BDG_ACTIVE(ifp)) { 333 /* 334 * Beware, the bridge code notices the null rcvif and 335 * uses that identify that it's being called from 336 * ether_output as opposd to ether_input. Yech. 337 */ 338 m->m_pkthdr.rcvif = NULL; 339 m = bdg_forward_ptr(m, ifp); 340 if (m != NULL) 341 m_freem(m); 342 return (0); 343 } 344 if (IPFW_LOADED && ether_ipfw != 0) { 345 if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) { 346 if (m) { 347 m_freem(m); 348 return ENOBUFS; /* pkt dropped */ 349 } else 350 return 0; /* consumed e.g. in a pipe */ 351 } 352 } 353 354 /* 355 * Queue message on interface, update output statistics if 356 * successful, and start output if interface not yet active. 357 */ 358 return (IF_HANDOFF(&ifp->if_snd, m, ifp) ? 0 : ENOBUFS); 359 } 360 361 /* 362 * ipfw processing for ethernet packets (in and out). 363 * The second parameter is NULL from ether_demux, and ifp from 364 * ether_output_frame. This section of code could be used from 365 * bridge.c as well as long as we use some extra info 366 * to distinguish that case from ether_output_frame(); 367 */ 368 int 369 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, 370 struct ip_fw **rule, int shared) 371 { 372 struct ether_header *eh; 373 struct ether_header save_eh; 374 struct mbuf *m; 375 int i; 376 struct ip_fw_args args; 377 378 if (*rule != NULL && fw_one_pass) 379 return 1; /* dummynet packet, already partially processed */ 380 381 /* 382 * I need some amt of data to be contiguous, and in case others need 383 * the packet (shared==1) also better be in the first mbuf. 384 */ 385 m = *m0; 386 i = min( m->m_pkthdr.len, max_protohdr); 387 if ( shared || m->m_len < i) { 388 m = m_pullup(m, i); 389 if (m == NULL) { 390 *m0 = m; 391 return 0; 392 } 393 } 394 eh = mtod(m, struct ether_header *); 395 save_eh = *eh; /* save copy for restore below */ 396 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */ 397 398 args.m = m; /* the packet we are looking at */ 399 args.oif = dst; /* destination, if any */ 400 args.divert_rule = 0; /* we do not support divert yet */ 401 args.rule = *rule; /* matching rule to restart */ 402 args.next_hop = NULL; /* we do not support forward yet */ 403 args.eh = &save_eh; /* MAC header for bridged/MAC packets */ 404 i = ip_fw_chk_ptr(&args); 405 m = args.m; 406 if (m != NULL) { 407 /* 408 * Restore Ethernet header, as needed, in case the 409 * mbuf chain was replaced by ipfw. 410 */ 411 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 412 if (m == NULL) { 413 *m0 = m; 414 return 0; 415 } 416 if (eh != mtod(m, struct ether_header *)) 417 bcopy(&save_eh, mtod(m, struct ether_header *), 418 ETHER_HDR_LEN); 419 } 420 *m0 = m; 421 *rule = args.rule; 422 423 if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) /* drop */ 424 return 0; 425 426 if (i == 0) /* a PASS rule. */ 427 return 1; 428 429 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) { 430 /* 431 * Pass the pkt to dummynet, which consumes it. 432 * If shared, make a copy and keep the original. 433 */ 434 if (shared) { 435 m = m_copypacket(m, M_DONTWAIT); 436 if (m == NULL) 437 return 0; 438 } else { 439 /* 440 * Pass the original to dummynet and 441 * nothing back to the caller 442 */ 443 *m0 = NULL ; 444 } 445 ip_dn_io_ptr(m, (i & 0xffff), 446 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args); 447 return 0; 448 } 449 /* 450 * XXX at some point add support for divert/forward actions. 451 * If none of the above matches, we have to drop the pkt. 452 */ 453 return 0; 454 } 455 456 /* 457 * Process a received Ethernet packet; the packet is in the 458 * mbuf chain m with the ethernet header at the front. 459 */ 460 static void 461 ether_input(struct ifnet *ifp, struct mbuf *m) 462 { 463 struct ether_header *eh; 464 u_short etype; 465 466 /* 467 * Do consistency checks to verify assumptions 468 * made by code past this point. 469 */ 470 if ((m->m_flags & M_PKTHDR) == 0) { 471 if_printf(ifp, "discard frame w/o packet header\n"); 472 ifp->if_ierrors++; 473 m_freem(m); 474 return; 475 } 476 if (m->m_len < ETHER_HDR_LEN) { 477 /* XXX maybe should pullup? */ 478 if_printf(ifp, "discard frame w/o leading ethernet " 479 "header (len %u pkt len %u)\n", 480 m->m_len, m->m_pkthdr.len); 481 ifp->if_ierrors++; 482 m_freem(m); 483 return; 484 } 485 eh = mtod(m, struct ether_header *); 486 etype = ntohs(eh->ether_type); 487 if (m->m_pkthdr.len > 488 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) { 489 if_printf(ifp, "discard oversize frame " 490 "(ether type %x flags %x len %u > max %lu)\n", 491 etype, m->m_flags, m->m_pkthdr.len, 492 ETHER_MAX_FRAME(ifp, etype, 493 m->m_flags & M_HASFCS)); 494 ifp->if_ierrors++; 495 m_freem(m); 496 return; 497 } 498 if (m->m_pkthdr.rcvif == NULL) { 499 if_printf(ifp, "discard frame w/o interface pointer\n"); 500 ifp->if_ierrors++; 501 m_freem(m); 502 return; 503 } 504 #ifdef DIAGNOSTIC 505 if (m->m_pkthdr.rcvif != ifp) { 506 if_printf(ifp, "Warning, frame marked as received on %s\n", 507 m->m_pkthdr.rcvif->if_xname); 508 } 509 #endif 510 511 #ifdef MAC 512 /* 513 * Tag the mbuf with an appropriate MAC label before any other 514 * consumers can get to it. 515 */ 516 mac_create_mbuf_from_ifnet(ifp, m); 517 #endif 518 519 /* 520 * Give bpf a chance at the packet. 521 */ 522 BPF_MTAP(ifp, m); 523 524 if (ifp->if_flags & IFF_MONITOR) { 525 /* 526 * Interface marked for monitoring; discard packet. 527 */ 528 m_freem(m); 529 return; 530 } 531 532 /* If the CRC is still on the packet, trim it off. */ 533 if (m->m_flags & M_HASFCS) { 534 m_adj(m, -ETHER_CRC_LEN); 535 m->m_flags &= ~M_HASFCS; 536 } 537 538 ifp->if_ibytes += m->m_pkthdr.len; 539 540 /* Handle ng_ether(4) processing, if any */ 541 if (ng_ether_input_p != NULL) { 542 (*ng_ether_input_p)(ifp, &m); 543 if (m == NULL) 544 return; 545 } 546 547 /* Check for bridging mode */ 548 if (BDG_ACTIVE(ifp) ) { 549 struct ifnet *bif; 550 551 /* 552 * Check with bridging code to see how the packet 553 * should be handled. Possibilities are: 554 * 555 * BDG_BCAST broadcast 556 * BDG_MCAST multicast 557 * BDG_LOCAL for local address, don't forward 558 * BDG_DROP discard 559 * ifp forward only to specified interface(s) 560 * 561 * Non-local destinations are handled by passing the 562 * packet back to the bridge code. 563 */ 564 bif = bridge_in_ptr(ifp, eh); 565 if (bif == BDG_DROP) { /* discard packet */ 566 m_freem(m); 567 return; 568 } 569 if (bif != BDG_LOCAL) { /* non-local, forward */ 570 m = bdg_forward_ptr(m, bif); 571 /* 572 * The bridge may consume the packet if it's not 573 * supposed to be passed up or if a problem occurred 574 * while doing its job. This is reflected by it 575 * returning a NULL mbuf pointer. 576 */ 577 if (m == NULL) { 578 if (bif == BDG_BCAST || bif == BDG_MCAST) 579 if_printf(ifp, 580 "bridge dropped %s packet\n", 581 bif == BDG_BCAST ? "broadcast" : 582 "multicast"); 583 return; 584 } 585 /* 586 * But in some cases the bridge may return the 587 * packet for us to free; sigh. 588 */ 589 if (bif != BDG_BCAST && bif != BDG_MCAST) { 590 m_freem(m); 591 return; 592 } 593 } 594 } 595 596 ether_demux(ifp, m); 597 /* First chunk of an mbuf contains good entropy */ 598 if (harvest.ethernet) 599 random_harvest(m, 16, 3, 0, RANDOM_NET); 600 } 601 602 /* 603 * Upper layer processing for a received Ethernet packet. 604 */ 605 void 606 ether_demux(struct ifnet *ifp, struct mbuf *m) 607 { 608 struct ether_header *eh; 609 int isr; 610 u_short ether_type; 611 #if defined(NETATALK) 612 struct llc *l; 613 #endif 614 struct ip_fw *rule = NULL; 615 616 /* Extract info from dummynet tag, ignore others */ 617 for (;m->m_type == MT_TAG; m = m->m_next) 618 if (m->m_flags == PACKET_TAG_DUMMYNET) { 619 rule = ((struct dn_pkt *)m)->rule; 620 ifp = m->m_next->m_pkthdr.rcvif; 621 } 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 859 /* 860 * Perform common duties while detaching an Ethernet interface 861 */ 862 void 863 ether_ifdetach(struct ifnet *ifp) 864 { 865 if (ng_ether_detach_p != NULL) 866 (*ng_ether_detach_p)(ifp); 867 bpfdetach(ifp); 868 if_detach(ifp); 869 if (BDG_LOADED) 870 bdgtakeifaces_ptr(); 871 } 872 873 SYSCTL_DECL(_net_link); 874 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 875 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, 876 ðer_ipfw,0,"Pass ether pkts through firewall"); 877 878 int 879 ether_ioctl(struct ifnet *ifp, int command, caddr_t data) 880 { 881 struct ifaddr *ifa = (struct ifaddr *) data; 882 struct ifreq *ifr = (struct ifreq *) data; 883 int error = 0; 884 885 switch (command) { 886 case SIOCSIFADDR: 887 ifp->if_flags |= IFF_UP; 888 889 switch (ifa->ifa_addr->sa_family) { 890 #ifdef INET 891 case AF_INET: 892 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 893 arp_ifinit(ifp, ifa); 894 break; 895 #endif 896 #ifdef IPX 897 /* 898 * XXX - This code is probably wrong 899 */ 900 case AF_IPX: 901 { 902 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 903 struct arpcom *ac = IFP2AC(ifp); 904 905 if (ipx_nullhost(*ina)) 906 ina->x_host = 907 *(union ipx_host *) 908 ac->ac_enaddr; 909 else { 910 bcopy((caddr_t) ina->x_host.c_host, 911 (caddr_t) ac->ac_enaddr, 912 sizeof(ac->ac_enaddr)); 913 } 914 915 /* 916 * Set new address 917 */ 918 ifp->if_init(ifp->if_softc); 919 break; 920 } 921 #endif 922 default: 923 ifp->if_init(ifp->if_softc); 924 break; 925 } 926 break; 927 928 case SIOCGIFADDR: 929 { 930 struct sockaddr *sa; 931 932 sa = (struct sockaddr *) & ifr->ifr_data; 933 bcopy(IFP2AC(ifp)->ac_enaddr, 934 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 935 } 936 break; 937 938 case SIOCSIFMTU: 939 /* 940 * Set the interface MTU. 941 */ 942 if (ifr->ifr_mtu > ETHERMTU) { 943 error = EINVAL; 944 } else { 945 ifp->if_mtu = ifr->ifr_mtu; 946 } 947 break; 948 default: 949 error = EINVAL; /* XXX netbsd has ENOTTY??? */ 950 break; 951 } 952 return (error); 953 } 954 955 static int 956 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 957 struct sockaddr *sa) 958 { 959 struct sockaddr_dl *sdl; 960 struct sockaddr_in *sin; 961 #ifdef INET6 962 struct sockaddr_in6 *sin6; 963 #endif 964 u_char *e_addr; 965 966 switch(sa->sa_family) { 967 case AF_LINK: 968 /* 969 * No mapping needed. Just check that it's a valid MC address. 970 */ 971 sdl = (struct sockaddr_dl *)sa; 972 e_addr = LLADDR(sdl); 973 if ((e_addr[0] & 1) != 1) 974 return EADDRNOTAVAIL; 975 *llsa = 0; 976 return 0; 977 978 #ifdef INET 979 case AF_INET: 980 sin = (struct sockaddr_in *)sa; 981 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 982 return EADDRNOTAVAIL; 983 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 984 M_WAITOK|M_ZERO); 985 sdl->sdl_len = sizeof *sdl; 986 sdl->sdl_family = AF_LINK; 987 sdl->sdl_index = ifp->if_index; 988 sdl->sdl_type = IFT_ETHER; 989 sdl->sdl_alen = ETHER_ADDR_LEN; 990 e_addr = LLADDR(sdl); 991 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 992 *llsa = (struct sockaddr *)sdl; 993 return 0; 994 #endif 995 #ifdef INET6 996 case AF_INET6: 997 sin6 = (struct sockaddr_in6 *)sa; 998 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 999 /* 1000 * An IP6 address of 0 means listen to all 1001 * of the Ethernet multicast address used for IP6. 1002 * (This is used for multicast routers.) 1003 */ 1004 ifp->if_flags |= IFF_ALLMULTI; 1005 *llsa = 0; 1006 return 0; 1007 } 1008 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1009 return EADDRNOTAVAIL; 1010 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, 1011 M_WAITOK|M_ZERO); 1012 sdl->sdl_len = sizeof *sdl; 1013 sdl->sdl_family = AF_LINK; 1014 sdl->sdl_index = ifp->if_index; 1015 sdl->sdl_type = IFT_ETHER; 1016 sdl->sdl_alen = ETHER_ADDR_LEN; 1017 e_addr = LLADDR(sdl); 1018 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 1019 *llsa = (struct sockaddr *)sdl; 1020 return 0; 1021 #endif 1022 1023 default: 1024 /* 1025 * Well, the text isn't quite right, but it's the name 1026 * that counts... 1027 */ 1028 return EAFNOSUPPORT; 1029 } 1030 } 1031 1032 static moduledata_t ether_mod = { 1033 "ether", 1034 NULL, 1035 0 1036 }; 1037 1038 DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 1039 MODULE_VERSION(ether, 1); 1040