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