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