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