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