if_ethersubr.c revision 275195
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: head/sys/net/if_ethersubr.c 275195 2014-11-27 21:29:19Z melifaro $ 31 */ 32 33#include "opt_inet.h" 34#include "opt_inet6.h" 35#include "opt_netgraph.h" 36#include "opt_mbuf_profiling.h" 37#include "opt_rss.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/lock.h> 43#include <sys/malloc.h> 44#include <sys/module.h> 45#include <sys/mbuf.h> 46#include <sys/random.h> 47#include <sys/socket.h> 48#include <sys/sockio.h> 49#include <sys/sysctl.h> 50#include <sys/uuid.h> 51 52#include <net/if.h> 53#include <net/if_var.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/if_llatbl.h> 65#include <net/pfil.h> 66#include <net/vnet.h> 67 68#include <netpfil/pf/pf_mtag.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/in_rss.h> 75#include <netinet/ip_carp.h> 76#include <netinet/ip_var.h> 77#endif 78#ifdef INET6 79#include <netinet6/nd6.h> 80#endif 81#include <security/mac/mac_framework.h> 82 83#ifdef CTASSERT 84CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2); 85CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN); 86#endif 87 88VNET_DEFINE(struct pfil_head, link_pfil_hook); /* Packet filter hooks */ 89 90/* netgraph node hooks for ng_ether(4) */ 91void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 92void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 93int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 94void (*ng_ether_attach_p)(struct ifnet *ifp); 95void (*ng_ether_detach_p)(struct ifnet *ifp); 96 97void (*vlan_input_p)(struct ifnet *, struct mbuf *); 98 99/* if_bridge(4) support */ 100struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *); 101int (*bridge_output_p)(struct ifnet *, struct mbuf *, 102 struct sockaddr *, struct rtentry *); 103void (*bridge_dn_p)(struct mbuf *, struct ifnet *); 104 105/* if_lagg(4) support */ 106struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *); 107 108static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 109 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 110 111static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 112 struct sockaddr *); 113#ifdef VIMAGE 114static void ether_reassign(struct ifnet *, struct vnet *, char *); 115#endif 116 117#define ETHER_IS_BROADCAST(addr) \ 118 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 119 120#define senderr(e) do { error = (e); goto bad;} while (0) 121 122static void 123update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst) 124{ 125 int csum_flags = 0; 126 127 if (src->m_pkthdr.csum_flags & CSUM_IP) 128 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 129 if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 130 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 131 if (src->m_pkthdr.csum_flags & CSUM_SCTP) 132 csum_flags |= CSUM_SCTP_VALID; 133 dst->m_pkthdr.csum_flags |= csum_flags; 134 if (csum_flags & CSUM_DATA_VALID) 135 dst->m_pkthdr.csum_data = 0xffff; 136} 137 138/* 139 * Ethernet output routine. 140 * Encapsulate a packet of type family for the local net. 141 * Use trailer local net encapsulation if enough data in first 142 * packet leaves a multiple of 512 bytes of data in remainder. 143 */ 144int 145ether_output(struct ifnet *ifp, struct mbuf *m, 146 const struct sockaddr *dst, struct route *ro) 147{ 148 short type; 149 int error = 0, hdrcmplt = 0; 150 u_char edst[ETHER_ADDR_LEN]; 151 struct llentry *lle = NULL; 152 struct rtentry *rt0 = NULL; 153 struct ether_header *eh; 154 struct pf_mtag *t; 155 int loop_copy = 1; 156 int hlen; /* link layer header length */ 157 158 if (ro != NULL) { 159 if (!(m->m_flags & (M_BCAST | M_MCAST))) 160 lle = ro->ro_lle; 161 rt0 = ro->ro_rt; 162 } 163#ifdef MAC 164 error = mac_ifnet_check_transmit(ifp, m); 165 if (error) 166 senderr(error); 167#endif 168 169 M_PROFILE(m); 170 if (ifp->if_flags & IFF_MONITOR) 171 senderr(ENETDOWN); 172 if (!((ifp->if_flags & IFF_UP) && 173 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 174 senderr(ENETDOWN); 175 176 hlen = ETHER_HDR_LEN; 177 switch (dst->sa_family) { 178#ifdef INET 179 case AF_INET: 180 if (lle != NULL && (lle->la_flags & LLE_VALID)) 181 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst)); 182 else 183 error = arpresolve(ifp, rt0, m, dst, edst, &lle); 184 if (error) 185 return (error == EWOULDBLOCK ? 0 : error); 186 type = htons(ETHERTYPE_IP); 187 break; 188 case AF_ARP: 189 { 190 struct arphdr *ah; 191 ah = mtod(m, struct arphdr *); 192 ah->ar_hrd = htons(ARPHRD_ETHER); 193 194 loop_copy = 0; /* if this is for us, don't do it */ 195 196 switch(ntohs(ah->ar_op)) { 197 case ARPOP_REVREQUEST: 198 case ARPOP_REVREPLY: 199 type = htons(ETHERTYPE_REVARP); 200 break; 201 case ARPOP_REQUEST: 202 case ARPOP_REPLY: 203 default: 204 type = htons(ETHERTYPE_ARP); 205 break; 206 } 207 208 if (m->m_flags & M_BCAST) 209 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN); 210 else 211 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN); 212 213 } 214 break; 215#endif 216#ifdef INET6 217 case AF_INET6: 218 if (lle != NULL && (lle->la_flags & LLE_VALID)) 219 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst)); 220 else 221 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle); 222 if (error) 223 return error; 224 type = htons(ETHERTYPE_IPV6); 225 break; 226#endif 227 case pseudo_AF_HDRCMPLT: 228 { 229 hdrcmplt = 1; 230 /* FALLTHROUGH */ 231 232 case AF_UNSPEC: 233 loop_copy = 0; /* if this is for us, don't do it */ 234 eh = (struct ether_header *)dst->sa_data; 235 (void)memcpy(edst, eh->ether_dhost, sizeof (edst)); 236 type = eh->ether_type; 237 break; 238 } 239 default: 240 if_printf(ifp, "can't handle af%d\n", dst->sa_family); 241 senderr(EAFNOSUPPORT); 242 } 243 244 if (lle != NULL && (lle->la_flags & LLE_IFADDR)) { 245 update_mbuf_csumflags(m, m); 246 return (if_simloop(ifp, m, dst->sa_family, 0)); 247 } 248 249 /* 250 * Add local net header. If no space in first mbuf, 251 * allocate another. 252 */ 253 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT); 254 if (m == NULL) 255 senderr(ENOBUFS); 256 eh = mtod(m, struct ether_header *); 257 if (hdrcmplt == 0) { 258 memcpy(&eh->ether_type, &type, sizeof(eh->ether_type)); 259 memcpy(eh->ether_dhost, edst, sizeof (edst)); 260 memcpy(eh->ether_shost, IF_LLADDR(ifp),sizeof(eh->ether_shost)); 261 } 262 263 /* 264 * If a simplex interface, and the packet is being sent to our 265 * Ethernet address or a broadcast address, loopback a copy. 266 * XXX To make a simplex device behave exactly like a duplex 267 * device, we should copy in the case of sending to our own 268 * ethernet address (thus letting the original actually appear 269 * on the wire). However, we don't do that here for security 270 * reasons and compatibility with the original behavior. 271 */ 272 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy && 273 ((t = pf_find_mtag(m)) == NULL || !t->routed)) { 274 if (m->m_flags & M_BCAST) { 275 struct mbuf *n; 276 277 /* 278 * Because if_simloop() modifies the packet, we need a 279 * writable copy through m_dup() instead of a readonly 280 * one as m_copy[m] would give us. The alternative would 281 * be to modify if_simloop() to handle the readonly mbuf, 282 * but performancewise it is mostly equivalent (trading 283 * extra data copying vs. extra locking). 284 * 285 * XXX This is a local workaround. A number of less 286 * often used kernel parts suffer from the same bug. 287 * See PR kern/105943 for a proposed general solution. 288 */ 289 if ((n = m_dup(m, M_NOWAIT)) != NULL) { 290 update_mbuf_csumflags(m, n); 291 (void)if_simloop(ifp, n, dst->sa_family, hlen); 292 } else 293 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 294 } else if (bcmp(eh->ether_dhost, eh->ether_shost, 295 ETHER_ADDR_LEN) == 0) { 296 update_mbuf_csumflags(m, m); 297 (void) if_simloop(ifp, m, dst->sa_family, hlen); 298 return (0); /* XXX */ 299 } 300 } 301 302 /* 303 * Bridges require special output handling. 304 */ 305 if (ifp->if_bridge) { 306 BRIDGE_OUTPUT(ifp, m, error); 307 return (error); 308 } 309 310#if defined(INET) || defined(INET6) 311 if (ifp->if_carp && 312 (error = (*carp_output_p)(ifp, m, dst))) 313 goto bad; 314#endif 315 316 /* Handle ng_ether(4) processing, if any */ 317 if (ifp->if_l2com != NULL) { 318 KASSERT(ng_ether_output_p != NULL, 319 ("ng_ether_output_p is NULL")); 320 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 321bad: if (m != NULL) 322 m_freem(m); 323 return (error); 324 } 325 if (m == NULL) 326 return (0); 327 } 328 329 /* Continue with link-layer output */ 330 return ether_output_frame(ifp, m); 331} 332 333/* 334 * Ethernet link layer output routine to send a raw frame to the device. 335 * 336 * This assumes that the 14 byte Ethernet header is present and contiguous 337 * in the first mbuf (if BRIDGE'ing). 338 */ 339int 340ether_output_frame(struct ifnet *ifp, struct mbuf *m) 341{ 342 int i; 343 344 if (PFIL_HOOKED(&V_link_pfil_hook)) { 345 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL); 346 347 if (i != 0) 348 return (EACCES); 349 350 if (m == NULL) 351 return (0); 352 } 353 354 /* 355 * Queue message on interface, update output statistics if 356 * successful, and start output if interface not yet active. 357 */ 358 return ((ifp->if_transmit)(ifp, m)); 359} 360 361#if defined(INET) || defined(INET6) 362#endif 363 364/* 365 * Process a received Ethernet packet; the packet is in the 366 * mbuf chain m with the ethernet header at the front. 367 */ 368static void 369ether_input_internal(struct ifnet *ifp, struct mbuf *m) 370{ 371 struct ether_header *eh; 372 u_short etype; 373 374 if ((ifp->if_flags & IFF_UP) == 0) { 375 m_freem(m); 376 return; 377 } 378#ifdef DIAGNOSTIC 379 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 380 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n"); 381 m_freem(m); 382 return; 383 } 384#endif 385 /* 386 * Do consistency checks to verify assumptions 387 * made by code past this point. 388 */ 389 if ((m->m_flags & M_PKTHDR) == 0) { 390 if_printf(ifp, "discard frame w/o packet header\n"); 391 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 392 m_freem(m); 393 return; 394 } 395 if (m->m_len < ETHER_HDR_LEN) { 396 /* XXX maybe should pullup? */ 397 if_printf(ifp, "discard frame w/o leading ethernet " 398 "header (len %u pkt len %u)\n", 399 m->m_len, m->m_pkthdr.len); 400 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 401 m_freem(m); 402 return; 403 } 404 eh = mtod(m, struct ether_header *); 405 etype = ntohs(eh->ether_type); 406 if (m->m_pkthdr.rcvif == NULL) { 407 if_printf(ifp, "discard frame w/o interface pointer\n"); 408 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 409 m_freem(m); 410 return; 411 } 412#ifdef DIAGNOSTIC 413 if (m->m_pkthdr.rcvif != ifp) { 414 if_printf(ifp, "Warning, frame marked as received on %s\n", 415 m->m_pkthdr.rcvif->if_xname); 416 } 417#endif 418 419 CURVNET_SET_QUIET(ifp->if_vnet); 420 421 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 422 if (ETHER_IS_BROADCAST(eh->ether_dhost)) 423 m->m_flags |= M_BCAST; 424 else 425 m->m_flags |= M_MCAST; 426 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 427 } 428 429#ifdef MAC 430 /* 431 * Tag the mbuf with an appropriate MAC label before any other 432 * consumers can get to it. 433 */ 434 mac_ifnet_create_mbuf(ifp, m); 435#endif 436 437 /* 438 * Give bpf a chance at the packet. 439 */ 440 ETHER_BPF_MTAP(ifp, m); 441 442 /* 443 * If the CRC is still on the packet, trim it off. We do this once 444 * and once only in case we are re-entered. Nothing else on the 445 * Ethernet receive path expects to see the FCS. 446 */ 447 if (m->m_flags & M_HASFCS) { 448 m_adj(m, -ETHER_CRC_LEN); 449 m->m_flags &= ~M_HASFCS; 450 } 451 452 if (!(ifp->if_capenable & IFCAP_HWSTATS)) 453 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 454 455 /* Allow monitor mode to claim this frame, after stats are updated. */ 456 if (ifp->if_flags & IFF_MONITOR) { 457 m_freem(m); 458 CURVNET_RESTORE(); 459 return; 460 } 461 462 /* Handle input from a lagg(4) port */ 463 if (ifp->if_type == IFT_IEEE8023ADLAG) { 464 KASSERT(lagg_input_p != NULL, 465 ("%s: if_lagg not loaded!", __func__)); 466 m = (*lagg_input_p)(ifp, m); 467 if (m != NULL) 468 ifp = m->m_pkthdr.rcvif; 469 else { 470 CURVNET_RESTORE(); 471 return; 472 } 473 } 474 475 /* 476 * If the hardware did not process an 802.1Q tag, do this now, 477 * to allow 802.1P priority frames to be passed to the main input 478 * path correctly. 479 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels. 480 */ 481 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) { 482 struct ether_vlan_header *evl; 483 484 if (m->m_len < sizeof(*evl) && 485 (m = m_pullup(m, sizeof(*evl))) == NULL) { 486#ifdef DIAGNOSTIC 487 if_printf(ifp, "cannot pullup VLAN header\n"); 488#endif 489 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 490 m_freem(m); 491 CURVNET_RESTORE(); 492 return; 493 } 494 495 evl = mtod(m, struct ether_vlan_header *); 496 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag); 497 m->m_flags |= M_VLANTAG; 498 499 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 500 ETHER_HDR_LEN - ETHER_TYPE_LEN); 501 m_adj(m, ETHER_VLAN_ENCAP_LEN); 502 eh = mtod(m, struct ether_header *); 503 } 504 505 M_SETFIB(m, ifp->if_fib); 506 507 /* Allow ng_ether(4) to claim this frame. */ 508 if (ifp->if_l2com != NULL) { 509 KASSERT(ng_ether_input_p != NULL, 510 ("%s: ng_ether_input_p is NULL", __func__)); 511 m->m_flags &= ~M_PROMISC; 512 (*ng_ether_input_p)(ifp, &m); 513 if (m == NULL) { 514 CURVNET_RESTORE(); 515 return; 516 } 517 eh = mtod(m, struct ether_header *); 518 } 519 520 /* 521 * Allow if_bridge(4) to claim this frame. 522 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it 523 * and the frame should be delivered locally. 524 */ 525 if (ifp->if_bridge != NULL) { 526 m->m_flags &= ~M_PROMISC; 527 BRIDGE_INPUT(ifp, m); 528 if (m == NULL) { 529 CURVNET_RESTORE(); 530 return; 531 } 532 eh = mtod(m, struct ether_header *); 533 } 534 535#if defined(INET) || defined(INET6) 536 /* 537 * Clear M_PROMISC on frame so that carp(4) will see it when the 538 * mbuf flows up to Layer 3. 539 * FreeBSD's implementation of carp(4) uses the inprotosw 540 * to dispatch IPPROTO_CARP. carp(4) also allocates its own 541 * Ethernet addresses of the form 00:00:5e:00:01:xx, which 542 * is outside the scope of the M_PROMISC test below. 543 * TODO: Maintain a hash table of ethernet addresses other than 544 * ether_dhost which may be active on this ifp. 545 */ 546 if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) { 547 m->m_flags &= ~M_PROMISC; 548 } else 549#endif 550 { 551 /* 552 * If the frame received was not for our MAC address, set the 553 * M_PROMISC flag on the mbuf chain. The frame may need to 554 * be seen by the rest of the Ethernet input path in case of 555 * re-entry (e.g. bridge, vlan, netgraph) but should not be 556 * seen by upper protocol layers. 557 */ 558 if (!ETHER_IS_MULTICAST(eh->ether_dhost) && 559 bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0) 560 m->m_flags |= M_PROMISC; 561 } 562 563 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER); 564 565 ether_demux(ifp, m); 566 CURVNET_RESTORE(); 567} 568 569/* 570 * Ethernet input dispatch; by default, direct dispatch here regardless of 571 * global configuration. However, if RSS is enabled, hook up RSS affinity 572 * so that when deferred or hybrid dispatch is enabled, we can redistribute 573 * load based on RSS. 574 * 575 * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or 576 * not it had already done work distribution via multi-queue. Then we could 577 * direct dispatch in the event load balancing was already complete and 578 * handle the case of interfaces with different capabilities better. 579 * 580 * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions 581 * at multiple layers? 582 * 583 * XXXRW: For now, enable all this only if RSS is compiled in, although it 584 * works fine without RSS. Need to characterise the performance overhead 585 * of the detour through the netisr code in the event the result is always 586 * direct dispatch. 587 */ 588static void 589ether_nh_input(struct mbuf *m) 590{ 591 592 ether_input_internal(m->m_pkthdr.rcvif, m); 593} 594 595static struct netisr_handler ether_nh = { 596 .nh_name = "ether", 597 .nh_handler = ether_nh_input, 598 .nh_proto = NETISR_ETHER, 599#ifdef RSS 600 .nh_policy = NETISR_POLICY_CPU, 601 .nh_dispatch = NETISR_DISPATCH_DIRECT, 602 .nh_m2cpuid = rss_m2cpuid, 603#else 604 .nh_policy = NETISR_POLICY_SOURCE, 605 .nh_dispatch = NETISR_DISPATCH_DIRECT, 606#endif 607}; 608 609static void 610ether_init(__unused void *arg) 611{ 612 613 netisr_register(ðer_nh); 614} 615SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL); 616 617static void 618vnet_ether_init(__unused void *arg) 619{ 620 int i; 621 622 /* Initialize packet filter hooks. */ 623 V_link_pfil_hook.ph_type = PFIL_TYPE_AF; 624 V_link_pfil_hook.ph_af = AF_LINK; 625 if ((i = pfil_head_register(&V_link_pfil_hook)) != 0) 626 printf("%s: WARNING: unable to register pfil link hook, " 627 "error %d\n", __func__, i); 628} 629VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY, 630 vnet_ether_init, NULL); 631 632static void 633vnet_ether_destroy(__unused void *arg) 634{ 635 int i; 636 637 if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0) 638 printf("%s: WARNING: unable to unregister pfil link hook, " 639 "error %d\n", __func__, i); 640} 641VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, 642 vnet_ether_destroy, NULL); 643 644 645 646static void 647ether_input(struct ifnet *ifp, struct mbuf *m) 648{ 649 650 struct mbuf *mn; 651 652 /* 653 * The drivers are allowed to pass in a chain of packets linked with 654 * m_nextpkt. We split them up into separate packets here and pass 655 * them up. This allows the drivers to amortize the receive lock. 656 */ 657 while (m) { 658 mn = m->m_nextpkt; 659 m->m_nextpkt = NULL; 660 661 /* 662 * We will rely on rcvif being set properly in the deferred context, 663 * so assert it is correct here. 664 */ 665 KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__)); 666 netisr_dispatch(NETISR_ETHER, m); 667 m = mn; 668 } 669} 670 671/* 672 * Upper layer processing for a received Ethernet packet. 673 */ 674void 675ether_demux(struct ifnet *ifp, struct mbuf *m) 676{ 677 struct ether_header *eh; 678 int i, isr; 679 u_short ether_type; 680 681 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__)); 682 683 /* Do not grab PROMISC frames in case we are re-entered. */ 684 if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) { 685 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL); 686 687 if (i != 0 || m == NULL) 688 return; 689 } 690 691 eh = mtod(m, struct ether_header *); 692 ether_type = ntohs(eh->ether_type); 693 694 /* 695 * If this frame has a VLAN tag other than 0, call vlan_input() 696 * if its module is loaded. Otherwise, drop. 697 */ 698 if ((m->m_flags & M_VLANTAG) && 699 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) { 700 if (ifp->if_vlantrunk == NULL) { 701 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 702 m_freem(m); 703 return; 704 } 705 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!", 706 __func__)); 707 /* Clear before possibly re-entering ether_input(). */ 708 m->m_flags &= ~M_PROMISC; 709 (*vlan_input_p)(ifp, m); 710 return; 711 } 712 713 /* 714 * Pass promiscuously received frames to the upper layer if the user 715 * requested this by setting IFF_PPROMISC. Otherwise, drop them. 716 */ 717 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) { 718 m_freem(m); 719 return; 720 } 721 722 /* 723 * Reset layer specific mbuf flags to avoid confusing upper layers. 724 * Strip off Ethernet header. 725 */ 726 m->m_flags &= ~M_VLANTAG; 727 m_clrprotoflags(m); 728 m_adj(m, ETHER_HDR_LEN); 729 730 /* 731 * Dispatch frame to upper layer. 732 */ 733 switch (ether_type) { 734#ifdef INET 735 case ETHERTYPE_IP: 736 if ((m = ip_fastforward(m)) == NULL) 737 return; 738 isr = NETISR_IP; 739 break; 740 741 case ETHERTYPE_ARP: 742 if (ifp->if_flags & IFF_NOARP) { 743 /* Discard packet if ARP is disabled on interface */ 744 m_freem(m); 745 return; 746 } 747 isr = NETISR_ARP; 748 break; 749#endif 750#ifdef INET6 751 case ETHERTYPE_IPV6: 752 isr = NETISR_IPV6; 753 break; 754#endif 755 default: 756 goto discard; 757 } 758 netisr_dispatch(isr, m); 759 return; 760 761discard: 762 /* 763 * Packet is to be discarded. If netgraph is present, 764 * hand the packet to it for last chance processing; 765 * otherwise dispose of it. 766 */ 767 if (ifp->if_l2com != NULL) { 768 KASSERT(ng_ether_input_orphan_p != NULL, 769 ("ng_ether_input_orphan_p is NULL")); 770 /* 771 * Put back the ethernet header so netgraph has a 772 * consistent view of inbound packets. 773 */ 774 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT); 775 (*ng_ether_input_orphan_p)(ifp, m); 776 return; 777 } 778 m_freem(m); 779} 780 781/* 782 * Convert Ethernet address to printable (loggable) representation. 783 * This routine is for compatibility; it's better to just use 784 * 785 * printf("%6D", <pointer to address>, ":"); 786 * 787 * since there's no static buffer involved. 788 */ 789char * 790ether_sprintf(const u_char *ap) 791{ 792 static char etherbuf[18]; 793 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":"); 794 return (etherbuf); 795} 796 797/* 798 * Perform common duties while attaching to interface list 799 */ 800void 801ether_ifattach(struct ifnet *ifp, const u_int8_t *lla) 802{ 803 int i; 804 struct ifaddr *ifa; 805 struct sockaddr_dl *sdl; 806 807 ifp->if_addrlen = ETHER_ADDR_LEN; 808 ifp->if_hdrlen = ETHER_HDR_LEN; 809 if_attach(ifp); 810 ifp->if_mtu = ETHERMTU; 811 ifp->if_output = ether_output; 812 ifp->if_input = ether_input; 813 ifp->if_resolvemulti = ether_resolvemulti; 814#ifdef VIMAGE 815 ifp->if_reassign = ether_reassign; 816#endif 817 if (ifp->if_baudrate == 0) 818 ifp->if_baudrate = IF_Mbps(10); /* just a default */ 819 ifp->if_broadcastaddr = etherbroadcastaddr; 820 821 ifa = ifp->if_addr; 822 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 823 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 824 sdl->sdl_type = IFT_ETHER; 825 sdl->sdl_alen = ifp->if_addrlen; 826 bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 827 828 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 829 if (ng_ether_attach_p != NULL) 830 (*ng_ether_attach_p)(ifp); 831 832 /* Announce Ethernet MAC address if non-zero. */ 833 for (i = 0; i < ifp->if_addrlen; i++) 834 if (lla[i] != 0) 835 break; 836 if (i != ifp->if_addrlen) 837 if_printf(ifp, "Ethernet address: %6D\n", lla, ":"); 838 839 uuid_ether_add(LLADDR(sdl)); 840} 841 842/* 843 * Perform common duties while detaching an Ethernet interface 844 */ 845void 846ether_ifdetach(struct ifnet *ifp) 847{ 848 struct sockaddr_dl *sdl; 849 850 sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr); 851 uuid_ether_del(LLADDR(sdl)); 852 853 if (ifp->if_l2com != NULL) { 854 KASSERT(ng_ether_detach_p != NULL, 855 ("ng_ether_detach_p is NULL")); 856 (*ng_ether_detach_p)(ifp); 857 } 858 859 bpfdetach(ifp); 860 if_detach(ifp); 861} 862 863#ifdef VIMAGE 864void 865ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused) 866{ 867 868 if (ifp->if_l2com != NULL) { 869 KASSERT(ng_ether_detach_p != NULL, 870 ("ng_ether_detach_p is NULL")); 871 (*ng_ether_detach_p)(ifp); 872 } 873 874 if (ng_ether_attach_p != NULL) { 875 CURVNET_SET_QUIET(new_vnet); 876 (*ng_ether_attach_p)(ifp); 877 CURVNET_RESTORE(); 878 } 879} 880#endif 881 882SYSCTL_DECL(_net_link); 883SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 884 885#if 0 886/* 887 * This is for reference. We have a table-driven version 888 * of the little-endian crc32 generator, which is faster 889 * than the double-loop. 890 */ 891uint32_t 892ether_crc32_le(const uint8_t *buf, size_t len) 893{ 894 size_t i; 895 uint32_t crc; 896 int bit; 897 uint8_t data; 898 899 crc = 0xffffffff; /* initial value */ 900 901 for (i = 0; i < len; i++) { 902 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 903 carry = (crc ^ data) & 1; 904 crc >>= 1; 905 if (carry) 906 crc = (crc ^ ETHER_CRC_POLY_LE); 907 } 908 } 909 910 return (crc); 911} 912#else 913uint32_t 914ether_crc32_le(const uint8_t *buf, size_t len) 915{ 916 static const uint32_t crctab[] = { 917 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 918 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 919 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 920 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 921 }; 922 size_t i; 923 uint32_t crc; 924 925 crc = 0xffffffff; /* initial value */ 926 927 for (i = 0; i < len; i++) { 928 crc ^= buf[i]; 929 crc = (crc >> 4) ^ crctab[crc & 0xf]; 930 crc = (crc >> 4) ^ crctab[crc & 0xf]; 931 } 932 933 return (crc); 934} 935#endif 936 937uint32_t 938ether_crc32_be(const uint8_t *buf, size_t len) 939{ 940 size_t i; 941 uint32_t crc, carry; 942 int bit; 943 uint8_t data; 944 945 crc = 0xffffffff; /* initial value */ 946 947 for (i = 0; i < len; i++) { 948 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 949 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); 950 crc <<= 1; 951 if (carry) 952 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 953 } 954 } 955 956 return (crc); 957} 958 959int 960ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 961{ 962 struct ifaddr *ifa = (struct ifaddr *) data; 963 struct ifreq *ifr = (struct ifreq *) data; 964 int error = 0; 965 966 switch (command) { 967 case SIOCSIFADDR: 968 ifp->if_flags |= IFF_UP; 969 970 switch (ifa->ifa_addr->sa_family) { 971#ifdef INET 972 case AF_INET: 973 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 974 arp_ifinit(ifp, ifa); 975 break; 976#endif 977 default: 978 ifp->if_init(ifp->if_softc); 979 break; 980 } 981 break; 982 983 case SIOCGIFADDR: 984 { 985 struct sockaddr *sa; 986 987 sa = (struct sockaddr *) & ifr->ifr_data; 988 bcopy(IF_LLADDR(ifp), 989 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 990 } 991 break; 992 993 case SIOCSIFMTU: 994 /* 995 * Set the interface MTU. 996 */ 997 if (ifr->ifr_mtu > ETHERMTU) { 998 error = EINVAL; 999 } else { 1000 ifp->if_mtu = ifr->ifr_mtu; 1001 } 1002 break; 1003 default: 1004 error = EINVAL; /* XXX netbsd has ENOTTY??? */ 1005 break; 1006 } 1007 return (error); 1008} 1009 1010static int 1011ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 1012 struct sockaddr *sa) 1013{ 1014 struct sockaddr_dl *sdl; 1015#ifdef INET 1016 struct sockaddr_in *sin; 1017#endif 1018#ifdef INET6 1019 struct sockaddr_in6 *sin6; 1020#endif 1021 u_char *e_addr; 1022 1023 switch(sa->sa_family) { 1024 case AF_LINK: 1025 /* 1026 * No mapping needed. Just check that it's a valid MC address. 1027 */ 1028 sdl = (struct sockaddr_dl *)sa; 1029 e_addr = LLADDR(sdl); 1030 if (!ETHER_IS_MULTICAST(e_addr)) 1031 return EADDRNOTAVAIL; 1032 *llsa = 0; 1033 return 0; 1034 1035#ifdef INET 1036 case AF_INET: 1037 sin = (struct sockaddr_in *)sa; 1038 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 1039 return EADDRNOTAVAIL; 1040 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 1041 sdl->sdl_alen = ETHER_ADDR_LEN; 1042 e_addr = LLADDR(sdl); 1043 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 1044 *llsa = (struct sockaddr *)sdl; 1045 return 0; 1046#endif 1047#ifdef INET6 1048 case AF_INET6: 1049 sin6 = (struct sockaddr_in6 *)sa; 1050 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1051 /* 1052 * An IP6 address of 0 means listen to all 1053 * of the Ethernet multicast address used for IP6. 1054 * (This is used for multicast routers.) 1055 */ 1056 ifp->if_flags |= IFF_ALLMULTI; 1057 *llsa = 0; 1058 return 0; 1059 } 1060 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1061 return EADDRNOTAVAIL; 1062 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 1063 sdl->sdl_alen = ETHER_ADDR_LEN; 1064 e_addr = LLADDR(sdl); 1065 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 1066 *llsa = (struct sockaddr *)sdl; 1067 return 0; 1068#endif 1069 1070 default: 1071 /* 1072 * Well, the text isn't quite right, but it's the name 1073 * that counts... 1074 */ 1075 return EAFNOSUPPORT; 1076 } 1077} 1078 1079static moduledata_t ether_mod = { 1080 .name = "ether", 1081}; 1082 1083void 1084ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen) 1085{ 1086 struct ether_vlan_header vlan; 1087 struct mbuf mv, mb; 1088 1089 KASSERT((m->m_flags & M_VLANTAG) != 0, 1090 ("%s: vlan information not present", __func__)); 1091 KASSERT(m->m_len >= sizeof(struct ether_header), 1092 ("%s: mbuf not large enough for header", __func__)); 1093 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header)); 1094 vlan.evl_proto = vlan.evl_encap_proto; 1095 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN); 1096 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag); 1097 m->m_len -= sizeof(struct ether_header); 1098 m->m_data += sizeof(struct ether_header); 1099 /* 1100 * If a data link has been supplied by the caller, then we will need to 1101 * re-create a stack allocated mbuf chain with the following structure: 1102 * 1103 * (1) mbuf #1 will contain the supplied data link 1104 * (2) mbuf #2 will contain the vlan header 1105 * (3) mbuf #3 will contain the original mbuf's packet data 1106 * 1107 * Otherwise, submit the packet and vlan header via bpf_mtap2(). 1108 */ 1109 if (data != NULL) { 1110 mv.m_next = m; 1111 mv.m_data = (caddr_t)&vlan; 1112 mv.m_len = sizeof(vlan); 1113 mb.m_next = &mv; 1114 mb.m_data = data; 1115 mb.m_len = dlen; 1116 bpf_mtap(bp, &mb); 1117 } else 1118 bpf_mtap2(bp, &vlan, sizeof(vlan), m); 1119 m->m_len += sizeof(struct ether_header); 1120 m->m_data -= sizeof(struct ether_header); 1121} 1122 1123struct mbuf * 1124ether_vlanencap(struct mbuf *m, uint16_t tag) 1125{ 1126 struct ether_vlan_header *evl; 1127 1128 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT); 1129 if (m == NULL) 1130 return (NULL); 1131 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 1132 1133 if (m->m_len < sizeof(*evl)) { 1134 m = m_pullup(m, sizeof(*evl)); 1135 if (m == NULL) 1136 return (NULL); 1137 } 1138 1139 /* 1140 * Transform the Ethernet header into an Ethernet header 1141 * with 802.1Q encapsulation. 1142 */ 1143 evl = mtod(m, struct ether_vlan_header *); 1144 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN, 1145 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN); 1146 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 1147 evl->evl_tag = htons(tag); 1148 return (m); 1149} 1150 1151DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); 1152MODULE_VERSION(ether, 1); 1153