1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include "opt_inet.h" 33 #include "opt_inet6.h" 34 #include "opt_netgraph.h" 35 #include "opt_mbuf_profiling.h" 36 #include "opt_rss.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/devctl.h> 41 #include <sys/eventhandler.h> 42 #include <sys/jail.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mbuf.h> 47 #include <sys/module.h> 48 #include <sys/msan.h> 49 #include <sys/proc.h> 50 #include <sys/priv.h> 51 #include <sys/random.h> 52 #include <sys/socket.h> 53 #include <sys/sockio.h> 54 #include <sys/sysctl.h> 55 #include <sys/uuid.h> 56 #ifdef KDB 57 #include <sys/kdb.h> 58 #endif 59 60 #include <net/ieee_oui.h> 61 #include <net/if.h> 62 #include <net/if_var.h> 63 #include <net/if_private.h> 64 #include <net/if_arp.h> 65 #include <net/netisr.h> 66 #include <net/route.h> 67 #include <net/if_llc.h> 68 #include <net/if_dl.h> 69 #include <net/if_types.h> 70 #include <net/bpf.h> 71 #include <net/ethernet.h> 72 #include <net/if_bridgevar.h> 73 #include <net/if_vlan_var.h> 74 #include <net/if_llatbl.h> 75 #include <net/pfil.h> 76 #include <net/rss_config.h> 77 #include <net/vnet.h> 78 79 #include <netpfil/pf/pf_mtag.h> 80 81 #if defined(INET) || defined(INET6) 82 #include <netinet/in.h> 83 #include <netinet/in_var.h> 84 #include <netinet/if_ether.h> 85 #include <netinet/ip_carp.h> 86 #include <netinet/ip_var.h> 87 #endif 88 #ifdef INET6 89 #include <netinet6/nd6.h> 90 #endif 91 #include <security/mac/mac_framework.h> 92 93 #include <crypto/sha1.h> 94 95 VNET_DEFINE(pfil_head_t, link_pfil_head); /* Packet filter hooks */ 96 97 /* netgraph node hooks for ng_ether(4) */ 98 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 99 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 100 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 101 102 /* if_bridge(4) support */ 103 void (*bridge_dn_p)(struct mbuf *, struct ifnet *); 104 bool (*bridge_same_p)(const void *, const void *); 105 void *(*bridge_get_softc_p)(struct ifnet *); 106 bool (*bridge_member_ifaddrs_p)(void); 107 108 /* if_lagg(4) support */ 109 struct mbuf *(*lagg_input_ethernet_p)(struct ifnet *, struct mbuf *); 110 111 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 112 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 113 114 static int ether_resolvemulti(struct ifnet *, struct sockaddr **, 115 struct sockaddr *); 116 static int ether_requestencap(struct ifnet *, struct if_encap_req *); 117 118 static inline bool ether_do_pcp(struct ifnet *, struct mbuf *); 119 120 #define senderr(e) do { error = (e); goto bad;} while (0) 121 122 static void 123 update_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 * Handle link-layer encapsulation requests. 140 */ 141 static int 142 ether_requestencap(struct ifnet *ifp, struct if_encap_req *req) 143 { 144 struct ether_header *eh; 145 struct arphdr *ah; 146 uint16_t etype; 147 const u_char *lladdr; 148 149 if (req->rtype != IFENCAP_LL) 150 return (EOPNOTSUPP); 151 152 if (req->bufsize < ETHER_HDR_LEN) 153 return (ENOMEM); 154 155 eh = (struct ether_header *)req->buf; 156 lladdr = req->lladdr; 157 req->lladdr_off = 0; 158 159 switch (req->family) { 160 case AF_INET: 161 etype = htons(ETHERTYPE_IP); 162 break; 163 case AF_INET6: 164 etype = htons(ETHERTYPE_IPV6); 165 break; 166 case AF_ARP: 167 ah = (struct arphdr *)req->hdata; 168 ah->ar_hrd = htons(ARPHRD_ETHER); 169 170 switch(ntohs(ah->ar_op)) { 171 case ARPOP_REVREQUEST: 172 case ARPOP_REVREPLY: 173 etype = htons(ETHERTYPE_REVARP); 174 break; 175 case ARPOP_REQUEST: 176 case ARPOP_REPLY: 177 default: 178 etype = htons(ETHERTYPE_ARP); 179 break; 180 } 181 182 if (req->flags & IFENCAP_FLAG_BROADCAST) 183 lladdr = ifp->if_broadcastaddr; 184 break; 185 default: 186 return (EAFNOSUPPORT); 187 } 188 189 memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type)); 190 memcpy(eh->ether_dhost, lladdr, ETHER_ADDR_LEN); 191 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 192 req->bufsize = sizeof(struct ether_header); 193 194 return (0); 195 } 196 197 static int 198 ether_resolve_addr(struct ifnet *ifp, struct mbuf *m, 199 const struct sockaddr *dst, struct route *ro, u_char *phdr, 200 uint32_t *pflags, struct llentry **plle) 201 { 202 uint32_t lleflags = 0; 203 int error = 0; 204 #if defined(INET) || defined(INET6) 205 struct ether_header *eh = (struct ether_header *)phdr; 206 uint16_t etype; 207 #endif 208 209 if (plle) 210 *plle = NULL; 211 212 switch (dst->sa_family) { 213 #ifdef INET 214 case AF_INET: 215 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) 216 error = arpresolve(ifp, 0, m, dst, phdr, &lleflags, 217 plle); 218 else { 219 if (m->m_flags & M_BCAST) 220 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, 221 ETHER_ADDR_LEN); 222 else { 223 const struct in_addr *a; 224 a = &(((const struct sockaddr_in *)dst)->sin_addr); 225 ETHER_MAP_IP_MULTICAST(a, eh->ether_dhost); 226 } 227 etype = htons(ETHERTYPE_IP); 228 memcpy(&eh->ether_type, &etype, sizeof(etype)); 229 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 230 } 231 break; 232 #endif 233 #ifdef INET6 234 case AF_INET6: 235 if ((m->m_flags & M_MCAST) == 0) { 236 int af = RO_GET_FAMILY(ro, dst); 237 error = nd6_resolve(ifp, LLE_SF(af, 0), m, dst, phdr, 238 &lleflags, plle); 239 } else { 240 const struct in6_addr *a6; 241 a6 = &(((const struct sockaddr_in6 *)dst)->sin6_addr); 242 ETHER_MAP_IPV6_MULTICAST(a6, eh->ether_dhost); 243 etype = htons(ETHERTYPE_IPV6); 244 memcpy(&eh->ether_type, &etype, sizeof(etype)); 245 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); 246 } 247 break; 248 #endif 249 default: 250 if_printf(ifp, "can't handle af%d\n", dst->sa_family); 251 if (m != NULL) 252 m_freem(m); 253 return (EAFNOSUPPORT); 254 } 255 256 if (error == EHOSTDOWN) { 257 if (ro != NULL && (ro->ro_flags & RT_HAS_GW) != 0) 258 error = EHOSTUNREACH; 259 } 260 261 if (error != 0) 262 return (error); 263 264 *pflags = RT_MAY_LOOP; 265 if (lleflags & LLE_IFADDR) 266 *pflags |= RT_L2_ME; 267 268 return (0); 269 } 270 271 /* 272 * Ethernet output routine. 273 * Encapsulate a packet of type family for the local net. 274 * Use trailer local net encapsulation if enough data in first 275 * packet leaves a multiple of 512 bytes of data in remainder. 276 */ 277 int 278 ether_output(struct ifnet *ifp, struct mbuf *m, 279 const struct sockaddr *dst, struct route *ro) 280 { 281 int error = 0; 282 char linkhdr[ETHER_HDR_LEN], *phdr; 283 struct ether_header *eh; 284 struct pf_mtag *t; 285 bool loop_copy; 286 int hlen; /* link layer header length */ 287 uint32_t pflags; 288 struct llentry *lle = NULL; 289 int addref = 0; 290 291 phdr = NULL; 292 pflags = 0; 293 if (ro != NULL) { 294 /* XXX BPF uses ro_prepend */ 295 if (ro->ro_prepend != NULL) { 296 phdr = ro->ro_prepend; 297 hlen = ro->ro_plen; 298 } else if (!(m->m_flags & (M_BCAST | M_MCAST))) { 299 if ((ro->ro_flags & RT_LLE_CACHE) != 0) { 300 lle = ro->ro_lle; 301 if (lle != NULL && 302 (lle->la_flags & LLE_VALID) == 0) { 303 LLE_FREE(lle); 304 lle = NULL; /* redundant */ 305 ro->ro_lle = NULL; 306 } 307 if (lle == NULL) { 308 /* if we lookup, keep cache */ 309 addref = 1; 310 } else 311 /* 312 * Notify LLE code that 313 * the entry was used 314 * by datapath. 315 */ 316 llentry_provide_feedback(lle); 317 } 318 if (lle != NULL) { 319 phdr = lle->r_linkdata; 320 hlen = lle->r_hdrlen; 321 pflags = lle->r_flags; 322 } 323 } 324 } 325 326 #ifdef MAC 327 error = mac_ifnet_check_transmit(ifp, m); 328 if (error) 329 senderr(error); 330 #endif 331 332 M_PROFILE(m); 333 if (ifp->if_flags & IFF_MONITOR) 334 senderr(ENETDOWN); 335 if (!((ifp->if_flags & IFF_UP) && 336 (ifp->if_drv_flags & IFF_DRV_RUNNING))) 337 senderr(ENETDOWN); 338 339 if (phdr == NULL) { 340 /* No prepend data supplied. Try to calculate ourselves. */ 341 phdr = linkhdr; 342 hlen = ETHER_HDR_LEN; 343 error = ether_resolve_addr(ifp, m, dst, ro, phdr, &pflags, 344 addref ? &lle : NULL); 345 if (addref && lle != NULL) 346 ro->ro_lle = lle; 347 if (error != 0) 348 return (error == EWOULDBLOCK ? 0 : error); 349 } 350 351 if ((pflags & RT_L2_ME) != 0) { 352 update_mbuf_csumflags(m, m); 353 return (if_simloop(ifp, m, RO_GET_FAMILY(ro, dst), 0)); 354 } 355 loop_copy = (pflags & RT_MAY_LOOP) != 0; 356 357 /* 358 * Add local net header. If no space in first mbuf, 359 * allocate another. 360 * 361 * Note that we do prepend regardless of RT_HAS_HEADER flag. 362 * This is done because BPF code shifts m_data pointer 363 * to the end of ethernet header prior to calling if_output(). 364 */ 365 M_PREPEND(m, hlen, M_NOWAIT); 366 if (m == NULL) 367 senderr(ENOBUFS); 368 if ((pflags & RT_HAS_HEADER) == 0) { 369 eh = mtod(m, struct ether_header *); 370 memcpy(eh, phdr, hlen); 371 } 372 373 /* 374 * If a simplex interface, and the packet is being sent to our 375 * Ethernet address or a broadcast address, loopback a copy. 376 * XXX To make a simplex device behave exactly like a duplex 377 * device, we should copy in the case of sending to our own 378 * ethernet address (thus letting the original actually appear 379 * on the wire). However, we don't do that here for security 380 * reasons and compatibility with the original behavior. 381 */ 382 if ((m->m_flags & M_BCAST) && loop_copy && (ifp->if_flags & IFF_SIMPLEX) && 383 ((t = pf_find_mtag(m)) == NULL || !t->routed)) { 384 struct mbuf *n; 385 386 /* 387 * Because if_simloop() modifies the packet, we need a 388 * writable copy through m_dup() instead of a readonly 389 * one as m_copy[m] would give us. The alternative would 390 * be to modify if_simloop() to handle the readonly mbuf, 391 * but performancewise it is mostly equivalent (trading 392 * extra data copying vs. extra locking). 393 * 394 * XXX This is a local workaround. A number of less 395 * often used kernel parts suffer from the same bug. 396 * See PR kern/105943 for a proposed general solution. 397 */ 398 if ((n = m_dup(m, M_NOWAIT)) != NULL) { 399 update_mbuf_csumflags(m, n); 400 (void)if_simloop(ifp, n, RO_GET_FAMILY(ro, dst), hlen); 401 } else 402 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 403 } 404 405 /* 406 * Bridges require special output handling. 407 */ 408 if (ifp->if_bridge) { 409 BRIDGE_OUTPUT(ifp, m, error); 410 return (error); 411 } 412 413 #if defined(INET) || defined(INET6) 414 if (ifp->if_carp && 415 (error = (*carp_output_p)(ifp, m, dst))) 416 goto bad; 417 #endif 418 419 /* Handle ng_ether(4) processing, if any */ 420 if (ifp->if_l2com != NULL) { 421 KASSERT(ng_ether_output_p != NULL, 422 ("ng_ether_output_p is NULL")); 423 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) { 424 bad: if (m != NULL) 425 m_freem(m); 426 return (error); 427 } 428 if (m == NULL) 429 return (0); 430 } 431 432 /* Continue with link-layer output */ 433 return ether_output_frame(ifp, m); 434 } 435 436 static bool 437 ether_set_pcp(struct mbuf **mp, struct ifnet *ifp, uint8_t pcp) 438 { 439 struct ether_8021q_tag qtag; 440 struct ether_header *eh; 441 442 eh = mtod(*mp, struct ether_header *); 443 if (eh->ether_type == htons(ETHERTYPE_VLAN) || 444 eh->ether_type == htons(ETHERTYPE_QINQ)) { 445 (*mp)->m_flags &= ~M_VLANTAG; 446 return (true); 447 } 448 449 qtag.vid = 0; 450 qtag.pcp = pcp; 451 qtag.proto = ETHERTYPE_VLAN; 452 if (ether_8021q_frame(mp, ifp, ifp, &qtag)) 453 return (true); 454 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 455 return (false); 456 } 457 458 /* 459 * Ethernet link layer output routine to send a raw frame to the device. 460 * 461 * This assumes that the 14 byte Ethernet header is present and contiguous 462 * in the first mbuf (if BRIDGE'ing). 463 */ 464 int 465 ether_output_frame(struct ifnet *ifp, struct mbuf *m) 466 { 467 if (ether_do_pcp(ifp, m) && !ether_set_pcp(&m, ifp, ifp->if_pcp)) 468 return (0); 469 470 if (PFIL_HOOKED_OUT(V_link_pfil_head)) 471 switch (pfil_mbuf_out(V_link_pfil_head, &m, ifp, NULL)) { 472 case PFIL_DROPPED: 473 return (EACCES); 474 case PFIL_CONSUMED: 475 return (0); 476 } 477 478 /* 479 * Queue message on interface, update output statistics if successful, 480 * and start output if interface not yet active. 481 * 482 * If KMSAN is enabled, use it to verify that the data does not contain 483 * any uninitialized bytes. 484 */ 485 kmsan_check_mbuf(m, "ether_output"); 486 return ((ifp->if_transmit)(ifp, m)); 487 } 488 489 /* 490 * Process a received Ethernet packet; the packet is in the 491 * mbuf chain m with the ethernet header at the front. 492 */ 493 static void 494 ether_input_internal(struct ifnet *ifp, struct mbuf *m) 495 { 496 struct ether_header *eh; 497 u_short etype; 498 499 if ((ifp->if_flags & IFF_UP) == 0) { 500 m_freem(m); 501 return; 502 } 503 #ifdef DIAGNOSTIC 504 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 505 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n"); 506 m_freem(m); 507 return; 508 } 509 #endif 510 if (__predict_false(m->m_len < ETHER_HDR_LEN)) { 511 /* Drivers should pullup and ensure the mbuf is valid */ 512 if_printf(ifp, "discard frame w/o leading ethernet " 513 "header (len %d pkt len %d)\n", 514 m->m_len, m->m_pkthdr.len); 515 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 516 m_freem(m); 517 return; 518 } 519 eh = mtod(m, struct ether_header *); 520 etype = ntohs(eh->ether_type); 521 random_harvest_queue_ether(m, sizeof(*m)); 522 523 CURVNET_SET_QUIET(ifp->if_vnet); 524 525 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 526 if (ETHER_IS_BROADCAST(eh->ether_dhost)) 527 m->m_flags |= M_BCAST; 528 else 529 m->m_flags |= M_MCAST; 530 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 531 } 532 533 #ifdef MAC 534 /* 535 * Tag the mbuf with an appropriate MAC label before any other 536 * consumers can get to it. 537 */ 538 mac_ifnet_create_mbuf(ifp, m); 539 #endif 540 541 /* 542 * Give bpf a chance at the packet. 543 */ 544 ETHER_BPF_MTAP(ifp, m); 545 546 if (!(ifp->if_capenable & IFCAP_HWSTATS)) 547 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 548 549 /* Allow monitor mode to claim this frame, after stats are updated. */ 550 if (ifp->if_flags & IFF_MONITOR) { 551 m_freem(m); 552 CURVNET_RESTORE(); 553 return; 554 } 555 556 /* Handle input from a lagg(4) port */ 557 if (ifp->if_type == IFT_IEEE8023ADLAG) { 558 KASSERT(lagg_input_ethernet_p != NULL, 559 ("%s: if_lagg not loaded!", __func__)); 560 m = (*lagg_input_ethernet_p)(ifp, m); 561 if (m != NULL) 562 ifp = m->m_pkthdr.rcvif; 563 else { 564 CURVNET_RESTORE(); 565 return; 566 } 567 } 568 569 /* 570 * If the hardware did not process an 802.1Q tag, do this now, 571 * to allow 802.1P priority frames to be passed to the main input 572 * path correctly. 573 */ 574 if ((m->m_flags & M_VLANTAG) == 0 && 575 ((etype == ETHERTYPE_VLAN) || (etype == ETHERTYPE_QINQ))) { 576 struct ether_vlan_header *evl; 577 578 if (m->m_len < sizeof(*evl) && 579 (m = m_pullup(m, sizeof(*evl))) == NULL) { 580 #ifdef DIAGNOSTIC 581 if_printf(ifp, "cannot pullup VLAN header\n"); 582 #endif 583 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 584 CURVNET_RESTORE(); 585 return; 586 } 587 588 evl = mtod(m, struct ether_vlan_header *); 589 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag); 590 m->m_flags |= M_VLANTAG; 591 592 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN, 593 ETHER_HDR_LEN - ETHER_TYPE_LEN); 594 m_adj(m, ETHER_VLAN_ENCAP_LEN); 595 eh = mtod(m, struct ether_header *); 596 } 597 598 M_SETFIB(m, ifp->if_fib); 599 600 /* Allow ng_ether(4) to claim this frame. */ 601 if (ifp->if_l2com != NULL) { 602 KASSERT(ng_ether_input_p != NULL, 603 ("%s: ng_ether_input_p is NULL", __func__)); 604 m->m_flags &= ~M_PROMISC; 605 (*ng_ether_input_p)(ifp, &m); 606 if (m == NULL) { 607 CURVNET_RESTORE(); 608 return; 609 } 610 eh = mtod(m, struct ether_header *); 611 } 612 613 /* 614 * Allow if_bridge(4) to claim this frame. 615 * 616 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it 617 * and the frame should be delivered locally. 618 * 619 * If M_BRIDGE_INJECT is set, the packet was received directly by the 620 * bridge via netmap, so "ifp" is the bridge itself and the packet 621 * should be re-examined. 622 */ 623 if (ifp->if_bridge != NULL || (m->m_flags & M_BRIDGE_INJECT) != 0) { 624 m->m_flags &= ~M_PROMISC; 625 BRIDGE_INPUT(ifp, m); 626 if (m == NULL) { 627 CURVNET_RESTORE(); 628 return; 629 } 630 eh = mtod(m, struct ether_header *); 631 } 632 633 #if defined(INET) || defined(INET6) 634 /* 635 * Clear M_PROMISC on frame so that carp(4) will see it when the 636 * mbuf flows up to Layer 3. 637 * FreeBSD's implementation of carp(4) uses the inprotosw 638 * to dispatch IPPROTO_CARP. carp(4) also allocates its own 639 * Ethernet addresses of the form 00:00:5e:00:01:xx, which 640 * is outside the scope of the M_PROMISC test below. 641 * TODO: Maintain a hash table of ethernet addresses other than 642 * ether_dhost which may be active on this ifp. 643 */ 644 if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) { 645 m->m_flags &= ~M_PROMISC; 646 } else 647 #endif 648 { 649 /* 650 * If the frame received was not for our MAC address, set the 651 * M_PROMISC flag on the mbuf chain. The frame may need to 652 * be seen by the rest of the Ethernet input path in case of 653 * re-entry (e.g. bridge, vlan, netgraph) but should not be 654 * seen by upper protocol layers. 655 */ 656 if (!ETHER_IS_MULTICAST(eh->ether_dhost) && 657 memcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0) 658 m->m_flags |= M_PROMISC; 659 } 660 661 ether_demux(ifp, m); 662 CURVNET_RESTORE(); 663 } 664 665 /* 666 * Ethernet input dispatch; by default, direct dispatch here regardless of 667 * global configuration. However, if RSS is enabled, hook up RSS affinity 668 * so that when deferred or hybrid dispatch is enabled, we can redistribute 669 * load based on RSS. 670 * 671 * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or 672 * not it had already done work distribution via multi-queue. Then we could 673 * direct dispatch in the event load balancing was already complete and 674 * handle the case of interfaces with different capabilities better. 675 * 676 * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions 677 * at multiple layers? 678 * 679 * XXXRW: For now, enable all this only if RSS is compiled in, although it 680 * works fine without RSS. Need to characterise the performance overhead 681 * of the detour through the netisr code in the event the result is always 682 * direct dispatch. 683 */ 684 static void 685 ether_nh_input(struct mbuf *m) 686 { 687 688 M_ASSERTPKTHDR(m); 689 KASSERT(m->m_pkthdr.rcvif != NULL, 690 ("%s: NULL interface pointer", __func__)); 691 ether_input_internal(m->m_pkthdr.rcvif, m); 692 } 693 694 static struct netisr_handler ether_nh = { 695 .nh_name = "ether", 696 .nh_handler = ether_nh_input, 697 .nh_proto = NETISR_ETHER, 698 #ifdef RSS 699 .nh_policy = NETISR_POLICY_CPU, 700 .nh_dispatch = NETISR_DISPATCH_DIRECT, 701 .nh_m2cpuid = rss_m2cpuid, 702 #else 703 .nh_policy = NETISR_POLICY_SOURCE, 704 .nh_dispatch = NETISR_DISPATCH_DIRECT, 705 #endif 706 }; 707 708 static void 709 ether_init(__unused void *arg) 710 { 711 712 netisr_register(ðer_nh); 713 } 714 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL); 715 716 static void 717 vnet_ether_init(const __unused void *arg) 718 { 719 struct pfil_head_args args; 720 721 args.pa_version = PFIL_VERSION; 722 args.pa_flags = PFIL_IN | PFIL_OUT; 723 args.pa_type = PFIL_TYPE_ETHERNET; 724 args.pa_headname = PFIL_ETHER_NAME; 725 V_link_pfil_head = pfil_head_register(&args); 726 727 #ifdef VIMAGE 728 netisr_register_vnet(ðer_nh); 729 #endif 730 } 731 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY, 732 vnet_ether_init, NULL); 733 734 #ifdef VIMAGE 735 static void 736 vnet_ether_pfil_destroy(const __unused void *arg) 737 { 738 739 pfil_head_unregister(V_link_pfil_head); 740 } 741 VNET_SYSUNINIT(vnet_ether_pfil_uninit, SI_SUB_PROTO_PFIL, SI_ORDER_ANY, 742 vnet_ether_pfil_destroy, NULL); 743 744 static void 745 vnet_ether_destroy(__unused void *arg) 746 { 747 748 netisr_unregister_vnet(ðer_nh); 749 } 750 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, 751 vnet_ether_destroy, NULL); 752 #endif 753 754 static void 755 ether_input(struct ifnet *ifp, struct mbuf *m) 756 { 757 struct epoch_tracker et; 758 struct mbuf *mn; 759 bool needs_epoch; 760 761 needs_epoch = (ifp->if_flags & IFF_NEEDSEPOCH); 762 #ifdef INVARIANTS 763 /* 764 * This temporary code is here to prevent epoch unaware and unmarked 765 * drivers to panic the system. Once all drivers are taken care of, 766 * the whole INVARIANTS block should go away. 767 */ 768 if (!needs_epoch && !in_epoch(net_epoch_preempt)) { 769 static bool printedonce; 770 771 needs_epoch = true; 772 if (!printedonce) { 773 printedonce = true; 774 if_printf(ifp, "called %s w/o net epoch! " 775 "PLEASE file a bug report.", __func__); 776 #ifdef KDB 777 kdb_backtrace(); 778 #endif 779 } 780 } 781 #endif 782 783 /* 784 * The drivers are allowed to pass in a chain of packets linked with 785 * m_nextpkt. We split them up into separate packets here and pass 786 * them up. This allows the drivers to amortize the receive lock. 787 */ 788 CURVNET_SET_QUIET(ifp->if_vnet); 789 if (__predict_false(needs_epoch)) 790 NET_EPOCH_ENTER(et); 791 while (m) { 792 mn = m->m_nextpkt; 793 m->m_nextpkt = NULL; 794 795 /* 796 * We will rely on rcvif being set properly in the deferred 797 * context, so assert it is correct here. 798 */ 799 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 800 KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch m %p " 801 "rcvif %p ifp %p", __func__, m, m->m_pkthdr.rcvif, ifp)); 802 netisr_dispatch(NETISR_ETHER, m); 803 m = mn; 804 } 805 if (__predict_false(needs_epoch)) 806 NET_EPOCH_EXIT(et); 807 CURVNET_RESTORE(); 808 } 809 810 /* 811 * Upper layer processing for a received Ethernet packet. 812 */ 813 void 814 ether_demux(struct ifnet *ifp, struct mbuf *m) 815 { 816 struct ether_header *eh; 817 int i, isr; 818 u_short ether_type; 819 820 NET_EPOCH_ASSERT(); 821 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__)); 822 823 /* Do not grab PROMISC frames in case we are re-entered. */ 824 if (PFIL_HOOKED_IN(V_link_pfil_head) && !(m->m_flags & M_PROMISC)) { 825 i = pfil_mbuf_in(V_link_pfil_head, &m, ifp, NULL); 826 if (i != PFIL_PASS) 827 return; 828 } 829 830 eh = mtod(m, struct ether_header *); 831 ether_type = ntohs(eh->ether_type); 832 833 /* 834 * If this frame has a VLAN tag other than 0, call vlan_input() 835 * if its module is loaded. Otherwise, drop. 836 */ 837 if ((m->m_flags & M_VLANTAG) && 838 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) { 839 if (ifp->if_vlantrunk == NULL) { 840 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1); 841 m_freem(m); 842 return; 843 } 844 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!", 845 __func__)); 846 /* Clear before possibly re-entering ether_input(). */ 847 m->m_flags &= ~M_PROMISC; 848 (*vlan_input_p)(ifp, m); 849 return; 850 } 851 852 /* 853 * Pass promiscuously received frames to the upper layer if the user 854 * requested this by setting IFF_PPROMISC. Otherwise, drop them. 855 */ 856 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) { 857 m_freem(m); 858 return; 859 } 860 861 /* 862 * Reset layer specific mbuf flags to avoid confusing upper layers. 863 */ 864 m->m_flags &= ~M_VLANTAG; 865 m_clrprotoflags(m); 866 867 /* 868 * Dispatch frame to upper layer. 869 */ 870 switch (ether_type) { 871 #ifdef INET 872 case ETHERTYPE_IP: 873 isr = NETISR_IP; 874 break; 875 876 case ETHERTYPE_ARP: 877 if (ifp->if_flags & IFF_NOARP) { 878 /* Discard packet if ARP is disabled on interface */ 879 m_freem(m); 880 return; 881 } 882 isr = NETISR_ARP; 883 break; 884 #endif 885 #ifdef INET6 886 case ETHERTYPE_IPV6: 887 isr = NETISR_IPV6; 888 break; 889 #endif 890 default: 891 goto discard; 892 } 893 894 /* Strip off Ethernet header. */ 895 m_adj(m, ETHER_HDR_LEN); 896 897 netisr_dispatch(isr, m); 898 return; 899 900 discard: 901 /* 902 * Packet is to be discarded. If netgraph is present, 903 * hand the packet to it for last chance processing; 904 * otherwise dispose of it. 905 */ 906 if (ifp->if_l2com != NULL) { 907 KASSERT(ng_ether_input_orphan_p != NULL, 908 ("ng_ether_input_orphan_p is NULL")); 909 (*ng_ether_input_orphan_p)(ifp, m); 910 return; 911 } 912 m_freem(m); 913 } 914 915 /* 916 * Convert Ethernet address to printable (loggable) representation. 917 * This routine is for compatibility; it's better to just use 918 * 919 * printf("%6D", <pointer to address>, ":"); 920 * 921 * since there's no static buffer involved. 922 */ 923 char * 924 ether_sprintf(const u_char *ap) 925 { 926 static char etherbuf[18]; 927 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":"); 928 return (etherbuf); 929 } 930 931 /* 932 * Perform common duties while attaching to interface list 933 */ 934 void 935 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla) 936 { 937 int i; 938 struct ifaddr *ifa; 939 struct sockaddr_dl *sdl; 940 941 ifp->if_addrlen = ETHER_ADDR_LEN; 942 ifp->if_hdrlen = (ifp->if_capabilities & IFCAP_VLAN_MTU) != 0 ? 943 ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN : ETHER_HDR_LEN; 944 ifp->if_mtu = ETHERMTU; 945 if_attach(ifp); 946 ifp->if_output = ether_output; 947 ifp->if_input = ether_input; 948 ifp->if_resolvemulti = ether_resolvemulti; 949 ifp->if_requestencap = ether_requestencap; 950 if (ifp->if_baudrate == 0) 951 ifp->if_baudrate = IF_Mbps(10); /* just a default */ 952 ifp->if_broadcastaddr = etherbroadcastaddr; 953 954 ifa = ifp->if_addr; 955 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 956 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 957 sdl->sdl_type = IFT_ETHER; 958 sdl->sdl_alen = ifp->if_addrlen; 959 bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 960 961 if (ifp->if_hw_addr != NULL) 962 bcopy(lla, ifp->if_hw_addr, ifp->if_addrlen); 963 964 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 965 966 /* Announce Ethernet MAC address if non-zero. */ 967 for (i = 0; i < ifp->if_addrlen; i++) 968 if (lla[i] != 0) 969 break; 970 if (i != ifp->if_addrlen) 971 if_printf(ifp, "Ethernet address: %6D\n", lla, ":"); 972 973 uuid_ether_add(LLADDR(sdl)); 974 975 /* Add necessary bits are setup; announce it now. */ 976 EVENTHANDLER_INVOKE(ether_ifattach_event, ifp); 977 if (IS_DEFAULT_VNET(curvnet)) 978 devctl_notify("ETHERNET", ifp->if_xname, "IFATTACH", NULL); 979 } 980 981 /* 982 * Perform common duties while detaching an Ethernet interface 983 */ 984 void 985 ether_ifdetach(struct ifnet *ifp) 986 { 987 struct sockaddr_dl *sdl; 988 989 sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr); 990 uuid_ether_del(LLADDR(sdl)); 991 992 bpfdetach(ifp); 993 if_detach(ifp); 994 } 995 996 SYSCTL_DECL(_net_link); 997 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 998 "Ethernet"); 999 1000 #if 0 1001 /* 1002 * This is for reference. We have a table-driven version 1003 * of the little-endian crc32 generator, which is faster 1004 * than the double-loop. 1005 */ 1006 uint32_t 1007 ether_crc32_le(const uint8_t *buf, size_t len) 1008 { 1009 size_t i; 1010 uint32_t crc; 1011 int bit; 1012 uint8_t data; 1013 1014 crc = 0xffffffff; /* initial value */ 1015 1016 for (i = 0; i < len; i++) { 1017 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 1018 carry = (crc ^ data) & 1; 1019 crc >>= 1; 1020 if (carry) 1021 crc = (crc ^ ETHER_CRC_POLY_LE); 1022 } 1023 } 1024 1025 return (crc); 1026 } 1027 #else 1028 uint32_t 1029 ether_crc32_le(const uint8_t *buf, size_t len) 1030 { 1031 static const uint32_t crctab[] = { 1032 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 1033 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 1034 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 1035 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 1036 }; 1037 size_t i; 1038 uint32_t crc; 1039 1040 crc = 0xffffffff; /* initial value */ 1041 1042 for (i = 0; i < len; i++) { 1043 crc ^= buf[i]; 1044 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1045 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1046 } 1047 1048 return (crc); 1049 } 1050 #endif 1051 1052 uint32_t 1053 ether_crc32_be(const uint8_t *buf, size_t len) 1054 { 1055 size_t i; 1056 uint32_t crc, carry; 1057 int bit; 1058 uint8_t data; 1059 1060 crc = 0xffffffff; /* initial value */ 1061 1062 for (i = 0; i < len; i++) { 1063 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 1064 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); 1065 crc <<= 1; 1066 if (carry) 1067 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 1068 } 1069 } 1070 1071 return (crc); 1072 } 1073 1074 int 1075 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 1076 { 1077 struct ifaddr *ifa = (struct ifaddr *) data; 1078 struct ifreq *ifr = (struct ifreq *) data; 1079 int error = 0; 1080 1081 switch (command) { 1082 case SIOCSIFADDR: 1083 ifp->if_flags |= IFF_UP; 1084 1085 switch (ifa->ifa_addr->sa_family) { 1086 #ifdef INET 1087 case AF_INET: 1088 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 1089 arp_ifinit(ifp, ifa); 1090 break; 1091 #endif 1092 default: 1093 ifp->if_init(ifp->if_softc); 1094 break; 1095 } 1096 break; 1097 1098 case SIOCGIFADDR: 1099 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 1100 ETHER_ADDR_LEN); 1101 break; 1102 1103 case SIOCSIFMTU: 1104 /* 1105 * Set the interface MTU. 1106 */ 1107 if (ifr->ifr_mtu > ETHERMTU) { 1108 error = EINVAL; 1109 } else { 1110 ifp->if_mtu = ifr->ifr_mtu; 1111 } 1112 break; 1113 1114 case SIOCSLANPCP: 1115 error = priv_check(curthread, PRIV_NET_SETLANPCP); 1116 if (error != 0) 1117 break; 1118 if (ifr->ifr_lan_pcp > 7 && 1119 ifr->ifr_lan_pcp != IFNET_PCP_NONE) { 1120 error = EINVAL; 1121 } else { 1122 ifp->if_pcp = ifr->ifr_lan_pcp; 1123 /* broadcast event about PCP change */ 1124 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP); 1125 } 1126 break; 1127 1128 case SIOCGLANPCP: 1129 ifr->ifr_lan_pcp = ifp->if_pcp; 1130 break; 1131 1132 default: 1133 error = EINVAL; /* XXX netbsd has ENOTTY??? */ 1134 break; 1135 } 1136 return (error); 1137 } 1138 1139 static int 1140 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 1141 struct sockaddr *sa) 1142 { 1143 struct sockaddr_dl *sdl; 1144 #ifdef INET 1145 struct sockaddr_in *sin; 1146 #endif 1147 #ifdef INET6 1148 struct sockaddr_in6 *sin6; 1149 #endif 1150 u_char *e_addr; 1151 1152 switch(sa->sa_family) { 1153 case AF_LINK: 1154 /* 1155 * No mapping needed. Just check that it's a valid MC address. 1156 */ 1157 sdl = (struct sockaddr_dl *)sa; 1158 e_addr = LLADDR(sdl); 1159 if (!ETHER_IS_MULTICAST(e_addr)) 1160 return EADDRNOTAVAIL; 1161 *llsa = NULL; 1162 return 0; 1163 1164 #ifdef INET 1165 case AF_INET: 1166 sin = (struct sockaddr_in *)sa; 1167 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 1168 return EADDRNOTAVAIL; 1169 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 1170 sdl->sdl_alen = ETHER_ADDR_LEN; 1171 e_addr = LLADDR(sdl); 1172 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 1173 *llsa = (struct sockaddr *)sdl; 1174 return 0; 1175 #endif 1176 #ifdef INET6 1177 case AF_INET6: 1178 sin6 = (struct sockaddr_in6 *)sa; 1179 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1180 /* 1181 * An IP6 address of 0 means listen to all 1182 * of the Ethernet multicast address used for IP6. 1183 * (This is used for multicast routers.) 1184 */ 1185 ifp->if_flags |= IFF_ALLMULTI; 1186 *llsa = NULL; 1187 return 0; 1188 } 1189 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1190 return EADDRNOTAVAIL; 1191 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 1192 sdl->sdl_alen = ETHER_ADDR_LEN; 1193 e_addr = LLADDR(sdl); 1194 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 1195 *llsa = (struct sockaddr *)sdl; 1196 return 0; 1197 #endif 1198 1199 default: 1200 /* 1201 * Well, the text isn't quite right, but it's the name 1202 * that counts... 1203 */ 1204 return EAFNOSUPPORT; 1205 } 1206 } 1207 1208 static moduledata_t ether_mod = { 1209 .name = "ether", 1210 }; 1211 1212 void 1213 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen) 1214 { 1215 struct ether_vlan_header vlan; 1216 struct mbuf mv, mb; 1217 1218 KASSERT((m->m_flags & M_VLANTAG) != 0, 1219 ("%s: vlan information not present", __func__)); 1220 KASSERT(m->m_len >= sizeof(struct ether_header), 1221 ("%s: mbuf not large enough for header", __func__)); 1222 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header)); 1223 vlan.evl_proto = vlan.evl_encap_proto; 1224 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN); 1225 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag); 1226 m->m_len -= sizeof(struct ether_header); 1227 m->m_data += sizeof(struct ether_header); 1228 /* 1229 * If a data link has been supplied by the caller, then we will need to 1230 * re-create a stack allocated mbuf chain with the following structure: 1231 * 1232 * (1) mbuf #1 will contain the supplied data link 1233 * (2) mbuf #2 will contain the vlan header 1234 * (3) mbuf #3 will contain the original mbuf's packet data 1235 * 1236 * Otherwise, submit the packet and vlan header via bpf_mtap2(). 1237 */ 1238 if (data != NULL) { 1239 mv.m_next = m; 1240 mv.m_data = (caddr_t)&vlan; 1241 mv.m_len = sizeof(vlan); 1242 mb.m_next = &mv; 1243 mb.m_data = data; 1244 mb.m_len = dlen; 1245 bpf_mtap(bp, &mb); 1246 } else 1247 bpf_mtap2(bp, &vlan, sizeof(vlan), m); 1248 m->m_len += sizeof(struct ether_header); 1249 m->m_data -= sizeof(struct ether_header); 1250 } 1251 1252 struct mbuf * 1253 ether_vlanencap_proto(struct mbuf *m, uint16_t tag, uint16_t proto) 1254 { 1255 struct ether_vlan_header *evl; 1256 1257 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT); 1258 if (m == NULL) 1259 return (NULL); 1260 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 1261 1262 if (m->m_len < sizeof(*evl)) { 1263 m = m_pullup(m, sizeof(*evl)); 1264 if (m == NULL) 1265 return (NULL); 1266 } 1267 1268 /* 1269 * Transform the Ethernet header into an Ethernet header 1270 * with 802.1Q encapsulation. 1271 */ 1272 evl = mtod(m, struct ether_vlan_header *); 1273 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN, 1274 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN); 1275 evl->evl_encap_proto = htons(proto); 1276 evl->evl_tag = htons(tag); 1277 return (m); 1278 } 1279 1280 void 1281 ether_bpf_mtap_if(struct ifnet *ifp, struct mbuf *m) 1282 { 1283 if (bpf_peers_present(ifp->if_bpf)) { 1284 M_ASSERTVALID(m); 1285 if ((m->m_flags & M_VLANTAG) != 0) 1286 ether_vlan_mtap(ifp->if_bpf, m, NULL, 0); 1287 else 1288 bpf_mtap(ifp->if_bpf, m); 1289 } 1290 } 1291 1292 static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1293 "IEEE 802.1Q VLAN"); 1294 static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, 1295 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1296 "for consistency"); 1297 1298 VNET_DEFINE_STATIC(int, soft_pad); 1299 #define V_soft_pad VNET(soft_pad) 1300 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW | CTLFLAG_VNET, 1301 &VNET_NAME(soft_pad), 0, 1302 "pad short frames before tagging"); 1303 1304 /* 1305 * For now, make preserving PCP via an mbuf tag optional, as it increases 1306 * per-packet memory allocations and frees. In the future, it would be 1307 * preferable to reuse ether_vtag for this, or similar. 1308 */ 1309 VNET_DEFINE(int, vlan_mtag_pcp) = 0; 1310 #define V_vlan_mtag_pcp VNET(vlan_mtag_pcp) 1311 SYSCTL_INT(_net_link_vlan, OID_AUTO, mtag_pcp, CTLFLAG_RW | CTLFLAG_VNET, 1312 &VNET_NAME(vlan_mtag_pcp), 0, 1313 "Retain VLAN PCP information as packets are passed up the stack"); 1314 1315 static inline bool 1316 ether_do_pcp(struct ifnet *ifp, struct mbuf *m) 1317 { 1318 if (ifp->if_type == IFT_L2VLAN) 1319 return (false); 1320 if (ifp->if_pcp != IFNET_PCP_NONE || (m->m_flags & M_VLANTAG) != 0) 1321 return (true); 1322 if (V_vlan_mtag_pcp && 1323 m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_OUT, NULL) != NULL) 1324 return (true); 1325 return (false); 1326 } 1327 1328 bool 1329 ether_8021q_frame(struct mbuf **mp, struct ifnet *ife, struct ifnet *p, 1330 const struct ether_8021q_tag *qtag) 1331 { 1332 struct m_tag *mtag; 1333 int n; 1334 uint16_t tag; 1335 uint8_t pcp = qtag->pcp; 1336 static const char pad[8]; /* just zeros */ 1337 1338 /* 1339 * Pad the frame to the minimum size allowed if told to. 1340 * This option is in accord with IEEE Std 802.1Q, 2003 Ed., 1341 * paragraph C.4.4.3.b. It can help to work around buggy 1342 * bridges that violate paragraph C.4.4.3.a from the same 1343 * document, i.e., fail to pad short frames after untagging. 1344 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but 1345 * untagging it will produce a 62-byte frame, which is a runt 1346 * and requires padding. There are VLAN-enabled network 1347 * devices that just discard such runts instead or mishandle 1348 * them somehow. 1349 */ 1350 if (V_soft_pad && p->if_type == IFT_ETHER) { 1351 for (n = ETHERMIN + ETHER_HDR_LEN - (*mp)->m_pkthdr.len; 1352 n > 0; n -= sizeof(pad)) { 1353 if (!m_append(*mp, min(n, sizeof(pad)), pad)) 1354 break; 1355 } 1356 if (n > 0) { 1357 m_freem(*mp); 1358 *mp = NULL; 1359 if_printf(ife, "cannot pad short frame"); 1360 return (false); 1361 } 1362 } 1363 1364 /* 1365 * If PCP is set in mbuf, use it 1366 */ 1367 if ((*mp)->m_flags & M_VLANTAG) { 1368 pcp = EVL_PRIOFTAG((*mp)->m_pkthdr.ether_vtag); 1369 } 1370 1371 /* 1372 * If underlying interface can do VLAN tag insertion itself, 1373 * just pass the packet along. However, we need some way to 1374 * tell the interface where the packet came from so that it 1375 * knows how to find the VLAN tag to use, so we attach a 1376 * packet tag that holds it. 1377 */ 1378 if (V_vlan_mtag_pcp && (mtag = m_tag_locate(*mp, MTAG_8021Q, 1379 MTAG_8021Q_PCP_OUT, NULL)) != NULL) 1380 tag = EVL_MAKETAG(qtag->vid, *(uint8_t *)(mtag + 1), 0); 1381 else 1382 tag = EVL_MAKETAG(qtag->vid, pcp, 0); 1383 if ((p->if_capenable & IFCAP_VLAN_HWTAGGING) && 1384 (qtag->proto == ETHERTYPE_VLAN)) { 1385 (*mp)->m_pkthdr.ether_vtag = tag; 1386 (*mp)->m_flags |= M_VLANTAG; 1387 } else { 1388 *mp = ether_vlanencap_proto(*mp, tag, qtag->proto); 1389 if (*mp == NULL) { 1390 if_printf(ife, "unable to prepend 802.1Q header"); 1391 return (false); 1392 } 1393 (*mp)->m_flags &= ~M_VLANTAG; 1394 } 1395 return (true); 1396 } 1397 1398 /* 1399 * Allocate an address from the FreeBSD Foundation OUI. This uses a 1400 * cryptographic hash function on the containing jail's name, UUID and the 1401 * interface name to attempt to provide a unique but stable address. 1402 * Pseudo-interfaces which require a MAC address should use this function to 1403 * allocate non-locally-administered addresses. 1404 */ 1405 void 1406 ether_gen_addr_byname(const char *nameunit, struct ether_addr *hwaddr) 1407 { 1408 SHA1_CTX ctx; 1409 char *buf; 1410 char uuid[HOSTUUIDLEN + 1]; 1411 uint64_t addr; 1412 int i, sz; 1413 unsigned char digest[SHA1_RESULTLEN]; 1414 char jailname[MAXHOSTNAMELEN]; 1415 1416 getcredhostuuid(curthread->td_ucred, uuid, sizeof(uuid)); 1417 if (strncmp(uuid, DEFAULT_HOSTUUID, sizeof(uuid)) == 0) { 1418 /* Fall back to a random mac address. */ 1419 goto rando; 1420 } 1421 1422 /* If each (vnet) jail would also have a unique hostuuid this would not 1423 * be necessary. */ 1424 getjailname(curthread->td_ucred, jailname, sizeof(jailname)); 1425 sz = asprintf(&buf, M_TEMP, "%s-%s-%s", uuid, nameunit, 1426 jailname); 1427 if (sz < 0) { 1428 /* Fall back to a random mac address. */ 1429 goto rando; 1430 } 1431 1432 SHA1Init(&ctx); 1433 SHA1Update(&ctx, buf, sz); 1434 SHA1Final(digest, &ctx); 1435 free(buf, M_TEMP); 1436 1437 addr = (digest[0] << 8) | digest[1] | OUI_FREEBSD_GENERATED_LOW; 1438 for (i = 0; i < ETHER_ADDR_LEN; ++i) { 1439 hwaddr->octet[i] = addr >> ((ETHER_ADDR_LEN - i - 1) * 8) & 1440 0xFF; 1441 } 1442 1443 return; 1444 rando: 1445 arc4rand(hwaddr, sizeof(*hwaddr), 0); 1446 /* Unicast */ 1447 hwaddr->octet[0] &= 0xFE; 1448 /* Locally administered. */ 1449 hwaddr->octet[0] |= 0x02; 1450 } 1451 1452 void 1453 ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr) 1454 { 1455 ether_gen_addr_byname(if_name(ifp), hwaddr); 1456 } 1457 1458 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); 1459 MODULE_VERSION(ether, 1); 1460