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