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