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