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