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