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 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 INIT_VNET_NET(ifp->if_vnet); 438 439 if (ip_fw_chk_ptr && V_ether_ipfw != 0) { 440 if (ether_ipfw_chk(&m, ifp, 0) == 0) { 441 if (m) { 442 m_freem(m); 443 return EACCES; /* pkt dropped */ 444 } else 445 return 0; /* consumed e.g. in a pipe */ 446 } 447 } 448 #endif 449 450 /* 451 * Queue message on interface, update output statistics if 452 * successful, and start output if interface not yet active. 453 */ 454 return ((ifp->if_transmit)(ifp, m)); 455 } 456 457 #if defined(INET) || defined(INET6) 458 /* 459 * ipfw processing for ethernet packets (in and out). 460 * The second parameter is NULL from ether_demux, and ifp from 461 * ether_output_frame. 462 */ 463 int 464 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, int shared) 465 { 466 INIT_VNET_INET(dst->if_vnet); 467 struct ether_header *eh; 468 struct ether_header save_eh; 469 struct mbuf *m; 470 int i; 471 struct ip_fw_args args; 472 struct dn_pkt_tag *dn_tag; 473 474 dn_tag = ip_dn_claim_tag(*m0); 475 476 if (dn_tag != NULL) { 477 if (dn_tag->rule != NULL && V_fw_one_pass) 478 /* dummynet packet, already partially processed */ 479 return (1); 480 args.rule = dn_tag->rule; /* matching rule to restart */ 481 args.rule_id = dn_tag->rule_id; 482 args.chain_id = dn_tag->chain_id; 483 } else 484 args.rule = NULL; 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 = 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 INIT_VNET_NET(ifp->if_vnet); 778 /* 779 * Allow dummynet and/or ipfw to claim the frame. 780 * Do not do this for PROMISC frames in case we are re-entered. 781 */ 782 if (ip_fw_chk_ptr && V_ether_ipfw != 0 && !(m->m_flags & M_PROMISC)) { 783 if (ether_ipfw_chk(&m, NULL, 0) == 0) { 784 if (m) 785 m_freem(m); /* dropped; free mbuf chain */ 786 return; /* consumed */ 787 } 788 } 789 #endif 790 eh = mtod(m, struct ether_header *); 791 ether_type = ntohs(eh->ether_type); 792 793 /* 794 * If this frame has a VLAN tag other than 0, call vlan_input() 795 * if its module is loaded. Otherwise, drop. 796 */ 797 if ((m->m_flags & M_VLANTAG) && 798 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) { 799 if (ifp->if_vlantrunk == NULL) { 800 ifp->if_noproto++; 801 m_freem(m); 802 return; 803 } 804 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!", 805 __func__)); 806 /* Clear before possibly re-entering ether_input(). */ 807 m->m_flags &= ~M_PROMISC; 808 (*vlan_input_p)(ifp, m); 809 return; 810 } 811 812 /* 813 * Pass promiscuously received frames to the upper layer if the user 814 * requested this by setting IFF_PPROMISC. Otherwise, drop them. 815 */ 816 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) { 817 m_freem(m); 818 return; 819 } 820 821 /* 822 * Reset layer specific mbuf flags to avoid confusing upper layers. 823 * Strip off Ethernet header. 824 */ 825 m->m_flags &= ~M_VLANTAG; 826 m->m_flags &= ~(M_PROTOFLAGS); 827 m_adj(m, ETHER_HDR_LEN); 828 829 /* 830 * Dispatch frame to upper layer. 831 */ 832 switch (ether_type) { 833 #ifdef INET 834 case ETHERTYPE_IP: 835 if ((m = ip_fastforward(m)) == NULL) 836 return; 837 isr = NETISR_IP; 838 break; 839 840 case ETHERTYPE_ARP: 841 if (ifp->if_flags & IFF_NOARP) { 842 /* Discard packet if ARP is disabled on interface */ 843 m_freem(m); 844 return; 845 } 846 isr = NETISR_ARP; 847 break; 848 #endif 849 #ifdef IPX 850 case ETHERTYPE_IPX: 851 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 852 return; 853 isr = NETISR_IPX; 854 break; 855 #endif 856 #ifdef INET6 857 case ETHERTYPE_IPV6: 858 isr = NETISR_IPV6; 859 break; 860 #endif 861 #ifdef NETATALK 862 case ETHERTYPE_AT: 863 isr = NETISR_ATALK1; 864 break; 865 case ETHERTYPE_AARP: 866 isr = NETISR_AARP; 867 break; 868 #endif /* NETATALK */ 869 default: 870 #ifdef IPX 871 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) 872 return; 873 #endif /* IPX */ 874 #if defined(NETATALK) 875 if (ether_type > ETHERMTU) 876 goto discard; 877 l = mtod(m, struct llc *); 878 if (l->llc_dsap == LLC_SNAP_LSAP && 879 l->llc_ssap == LLC_SNAP_LSAP && 880 l->llc_control == LLC_UI) { 881 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code, 882 sizeof(at_org_code)) == 0 && 883 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { 884 m_adj(m, LLC_SNAPFRAMELEN); 885 isr = NETISR_ATALK2; 886 break; 887 } 888 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, 889 sizeof(aarp_org_code)) == 0 && 890 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { 891 m_adj(m, LLC_SNAPFRAMELEN); 892 isr = NETISR_AARP; 893 break; 894 } 895 } 896 #endif /* NETATALK */ 897 goto discard; 898 } 899 netisr_dispatch(isr, m); 900 return; 901 902 discard: 903 /* 904 * Packet is to be discarded. If netgraph is present, 905 * hand the packet to it for last chance processing; 906 * otherwise dispose of it. 907 */ 908 if (IFP2AC(ifp)->ac_netgraph != NULL) { 909 KASSERT(ng_ether_input_orphan_p != NULL, 910 ("ng_ether_input_orphan_p is NULL")); 911 /* 912 * Put back the ethernet header so netgraph has a 913 * consistent view of inbound packets. 914 */ 915 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 916 (*ng_ether_input_orphan_p)(ifp, m); 917 return; 918 } 919 m_freem(m); 920 } 921 922 /* 923 * Convert Ethernet address to printable (loggable) representation. 924 * This routine is for compatibility; it's better to just use 925 * 926 * printf("%6D", <pointer to address>, ":"); 927 * 928 * since there's no static buffer involved. 929 */ 930 char * 931 ether_sprintf(const u_char *ap) 932 { 933 static char etherbuf[18]; 934 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":"); 935 return (etherbuf); 936 } 937 938 /* 939 * Perform common duties while attaching to interface list 940 */ 941 void 942 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla) 943 { 944 int i; 945 struct ifaddr *ifa; 946 struct sockaddr_dl *sdl; 947 948 ifp->if_addrlen = ETHER_ADDR_LEN; 949 ifp->if_hdrlen = ETHER_HDR_LEN; 950 if_attach(ifp); 951 ifp->if_mtu = ETHERMTU; 952 ifp->if_output = ether_output; 953 ifp->if_input = ether_input; 954 ifp->if_resolvemulti = ether_resolvemulti; 955 if (ifp->if_baudrate == 0) 956 ifp->if_baudrate = IF_Mbps(10); /* just a default */ 957 ifp->if_broadcastaddr = etherbroadcastaddr; 958 959 ifa = ifp->if_addr; 960 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 961 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 962 sdl->sdl_type = IFT_ETHER; 963 sdl->sdl_alen = ifp->if_addrlen; 964 bcopy(lla, LLADDR(sdl), ifp->if_addrlen); 965 966 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 967 if (ng_ether_attach_p != NULL) 968 (*ng_ether_attach_p)(ifp); 969 970 /* Announce Ethernet MAC address if non-zero. */ 971 for (i = 0; i < ifp->if_addrlen; i++) 972 if (lla[i] != 0) 973 break; 974 if (i != ifp->if_addrlen) 975 if_printf(ifp, "Ethernet address: %6D\n", lla, ":"); 976 } 977 978 /* 979 * Perform common duties while detaching an Ethernet interface 980 */ 981 void 982 ether_ifdetach(struct ifnet *ifp) 983 { 984 if (IFP2AC(ifp)->ac_netgraph != NULL) { 985 KASSERT(ng_ether_detach_p != NULL, 986 ("ng_ether_detach_p is NULL")); 987 (*ng_ether_detach_p)(ifp); 988 } 989 990 bpfdetach(ifp); 991 if_detach(ifp); 992 } 993 994 SYSCTL_DECL(_net_link); 995 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); 996 #if defined(INET) || defined(INET6) 997 SYSCTL_V_INT(V_NET, vnet_net, _net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, 998 ether_ipfw, 0, "Pass ether pkts through firewall"); 999 #endif 1000 1001 #if 0 1002 /* 1003 * This is for reference. We have a table-driven version 1004 * of the little-endian crc32 generator, which is faster 1005 * than the double-loop. 1006 */ 1007 uint32_t 1008 ether_crc32_le(const uint8_t *buf, size_t len) 1009 { 1010 size_t i; 1011 uint32_t crc; 1012 int bit; 1013 uint8_t data; 1014 1015 crc = 0xffffffff; /* initial value */ 1016 1017 for (i = 0; i < len; i++) { 1018 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 1019 carry = (crc ^ data) & 1; 1020 crc >>= 1; 1021 if (carry) 1022 crc = (crc ^ ETHER_CRC_POLY_LE); 1023 } 1024 } 1025 1026 return (crc); 1027 } 1028 #else 1029 uint32_t 1030 ether_crc32_le(const uint8_t *buf, size_t len) 1031 { 1032 static const uint32_t crctab[] = { 1033 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 1034 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 1035 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 1036 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 1037 }; 1038 size_t i; 1039 uint32_t crc; 1040 1041 crc = 0xffffffff; /* initial value */ 1042 1043 for (i = 0; i < len; i++) { 1044 crc ^= buf[i]; 1045 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1046 crc = (crc >> 4) ^ crctab[crc & 0xf]; 1047 } 1048 1049 return (crc); 1050 } 1051 #endif 1052 1053 uint32_t 1054 ether_crc32_be(const uint8_t *buf, size_t len) 1055 { 1056 size_t i; 1057 uint32_t crc, carry; 1058 int bit; 1059 uint8_t data; 1060 1061 crc = 0xffffffff; /* initial value */ 1062 1063 for (i = 0; i < len; i++) { 1064 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) { 1065 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); 1066 crc <<= 1; 1067 if (carry) 1068 crc = (crc ^ ETHER_CRC_POLY_BE) | carry; 1069 } 1070 } 1071 1072 return (crc); 1073 } 1074 1075 int 1076 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 1077 { 1078 struct ifaddr *ifa = (struct ifaddr *) data; 1079 struct ifreq *ifr = (struct ifreq *) data; 1080 int error = 0; 1081 1082 switch (command) { 1083 case SIOCSIFADDR: 1084 ifp->if_flags |= IFF_UP; 1085 1086 switch (ifa->ifa_addr->sa_family) { 1087 #ifdef INET 1088 case AF_INET: 1089 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 1090 arp_ifinit(ifp, ifa); 1091 break; 1092 #endif 1093 #ifdef IPX 1094 /* 1095 * XXX - This code is probably wrong 1096 */ 1097 case AF_IPX: 1098 { 1099 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr); 1100 1101 if (ipx_nullhost(*ina)) 1102 ina->x_host = 1103 *(union ipx_host *) 1104 IF_LLADDR(ifp); 1105 else { 1106 bcopy((caddr_t) ina->x_host.c_host, 1107 (caddr_t) IF_LLADDR(ifp), 1108 ETHER_ADDR_LEN); 1109 } 1110 1111 /* 1112 * Set new address 1113 */ 1114 ifp->if_init(ifp->if_softc); 1115 break; 1116 } 1117 #endif 1118 default: 1119 ifp->if_init(ifp->if_softc); 1120 break; 1121 } 1122 break; 1123 1124 case SIOCGIFADDR: 1125 { 1126 struct sockaddr *sa; 1127 1128 sa = (struct sockaddr *) & ifr->ifr_data; 1129 bcopy(IF_LLADDR(ifp), 1130 (caddr_t) sa->sa_data, ETHER_ADDR_LEN); 1131 } 1132 break; 1133 1134 case SIOCSIFMTU: 1135 /* 1136 * Set the interface MTU. 1137 */ 1138 if (ifr->ifr_mtu > ETHERMTU) { 1139 error = EINVAL; 1140 } else { 1141 ifp->if_mtu = ifr->ifr_mtu; 1142 } 1143 break; 1144 default: 1145 error = EINVAL; /* XXX netbsd has ENOTTY??? */ 1146 break; 1147 } 1148 return (error); 1149 } 1150 1151 static int 1152 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 1153 struct sockaddr *sa) 1154 { 1155 struct sockaddr_dl *sdl; 1156 #ifdef INET 1157 struct sockaddr_in *sin; 1158 #endif 1159 #ifdef INET6 1160 struct sockaddr_in6 *sin6; 1161 #endif 1162 u_char *e_addr; 1163 1164 switch(sa->sa_family) { 1165 case AF_LINK: 1166 /* 1167 * No mapping needed. Just check that it's a valid MC address. 1168 */ 1169 sdl = (struct sockaddr_dl *)sa; 1170 e_addr = LLADDR(sdl); 1171 if (!ETHER_IS_MULTICAST(e_addr)) 1172 return EADDRNOTAVAIL; 1173 *llsa = 0; 1174 return 0; 1175 1176 #ifdef INET 1177 case AF_INET: 1178 sin = (struct sockaddr_in *)sa; 1179 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 1180 return EADDRNOTAVAIL; 1181 sdl = malloc(sizeof *sdl, M_IFMADDR, 1182 M_NOWAIT|M_ZERO); 1183 if (sdl == NULL) 1184 return ENOMEM; 1185 sdl->sdl_len = sizeof *sdl; 1186 sdl->sdl_family = AF_LINK; 1187 sdl->sdl_index = ifp->if_index; 1188 sdl->sdl_type = IFT_ETHER; 1189 sdl->sdl_alen = ETHER_ADDR_LEN; 1190 e_addr = LLADDR(sdl); 1191 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); 1192 *llsa = (struct sockaddr *)sdl; 1193 return 0; 1194 #endif 1195 #ifdef INET6 1196 case AF_INET6: 1197 sin6 = (struct sockaddr_in6 *)sa; 1198 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1199 /* 1200 * An IP6 address of 0 means listen to all 1201 * of the Ethernet multicast address used for IP6. 1202 * (This is used for multicast routers.) 1203 */ 1204 ifp->if_flags |= IFF_ALLMULTI; 1205 *llsa = 0; 1206 return 0; 1207 } 1208 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1209 return EADDRNOTAVAIL; 1210 sdl = malloc(sizeof *sdl, M_IFMADDR, 1211 M_NOWAIT|M_ZERO); 1212 if (sdl == NULL) 1213 return (ENOMEM); 1214 sdl->sdl_len = sizeof *sdl; 1215 sdl->sdl_family = AF_LINK; 1216 sdl->sdl_index = ifp->if_index; 1217 sdl->sdl_type = IFT_ETHER; 1218 sdl->sdl_alen = ETHER_ADDR_LEN; 1219 e_addr = LLADDR(sdl); 1220 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); 1221 *llsa = (struct sockaddr *)sdl; 1222 return 0; 1223 #endif 1224 1225 default: 1226 /* 1227 * Well, the text isn't quite right, but it's the name 1228 * that counts... 1229 */ 1230 return EAFNOSUPPORT; 1231 } 1232 } 1233 1234 static void* 1235 ether_alloc(u_char type, struct ifnet *ifp) 1236 { 1237 struct arpcom *ac; 1238 1239 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO); 1240 ac->ac_ifp = ifp; 1241 1242 return (ac); 1243 } 1244 1245 static void 1246 ether_free(void *com, u_char type) 1247 { 1248 1249 free(com, M_ARPCOM); 1250 } 1251 1252 static int 1253 ether_modevent(module_t mod, int type, void *data) 1254 { 1255 1256 switch (type) { 1257 case MOD_LOAD: 1258 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free); 1259 break; 1260 case MOD_UNLOAD: 1261 if_deregister_com_alloc(IFT_ETHER); 1262 break; 1263 default: 1264 return EOPNOTSUPP; 1265 } 1266 1267 return (0); 1268 } 1269 1270 static moduledata_t ether_mod = { 1271 "ether", 1272 ether_modevent, 1273 0 1274 }; 1275 1276 void 1277 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen) 1278 { 1279 struct ether_vlan_header vlan; 1280 struct mbuf mv, mb; 1281 1282 KASSERT((m->m_flags & M_VLANTAG) != 0, 1283 ("%s: vlan information not present", __func__)); 1284 KASSERT(m->m_len >= sizeof(struct ether_header), 1285 ("%s: mbuf not large enough for header", __func__)); 1286 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header)); 1287 vlan.evl_proto = vlan.evl_encap_proto; 1288 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN); 1289 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag); 1290 m->m_len -= sizeof(struct ether_header); 1291 m->m_data += sizeof(struct ether_header); 1292 /* 1293 * If a data link has been supplied by the caller, then we will need to 1294 * re-create a stack allocated mbuf chain with the following structure: 1295 * 1296 * (1) mbuf #1 will contain the supplied data link 1297 * (2) mbuf #2 will contain the vlan header 1298 * (3) mbuf #3 will contain the original mbuf's packet data 1299 * 1300 * Otherwise, submit the packet and vlan header via bpf_mtap2(). 1301 */ 1302 if (data != NULL) { 1303 mv.m_next = m; 1304 mv.m_data = (caddr_t)&vlan; 1305 mv.m_len = sizeof(vlan); 1306 mb.m_next = &mv; 1307 mb.m_data = data; 1308 mb.m_len = dlen; 1309 bpf_mtap(bp, &mb); 1310 } else 1311 bpf_mtap2(bp, &vlan, sizeof(vlan), m); 1312 m->m_len += sizeof(struct ether_header); 1313 m->m_data -= sizeof(struct ether_header); 1314 } 1315 1316 struct mbuf * 1317 ether_vlanencap(struct mbuf *m, uint16_t tag) 1318 { 1319 struct ether_vlan_header *evl; 1320 1321 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT); 1322 if (m == NULL) 1323 return (NULL); 1324 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 1325 1326 if (m->m_len < sizeof(*evl)) { 1327 m = m_pullup(m, sizeof(*evl)); 1328 if (m == NULL) 1329 return (NULL); 1330 } 1331 1332 /* 1333 * Transform the Ethernet header into an Ethernet header 1334 * with 802.1Q encapsulation. 1335 */ 1336 evl = mtod(m, struct ether_vlan_header *); 1337 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN, 1338 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN); 1339 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 1340 evl->evl_tag = htons(tag); 1341 return (m); 1342 } 1343 1344 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); 1345 MODULE_VERSION(ether, 1); 1346