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