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