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