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