1 /* 2 * Copyright 1998 Massachusetts Institute of Technology 3 * 4 * Permission to use, copy, modify, and distribute this software and 5 * its documentation for any purpose and without fee is hereby 6 * granted, provided that both the above copyright notice and this 7 * permission notice appear in all copies, that both the above 8 * copyright notice and this permission notice appear in all 9 * supporting documentation, and that the name of M.I.T. not be used 10 * in advertising or publicity pertaining to distribution of the 11 * software without specific, written prior permission. M.I.T. makes 12 * no representations about the suitability of this software for any 13 * purpose. It is provided "as is" without express or implied 14 * warranty. 15 * 16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/net/if_vlan.c,v 1.15.2.13 2003/02/14 22:25:58 fenner Exp $ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/mbuf.h> 34 #include <sys/serialize.h> 35 36 #include <net/bpf.h> 37 #include <net/ethernet.h> 38 #include <net/if.h> 39 #include <net/if_arp.h> 40 #include <net/ifq_var.h> 41 #include <net/netmsg.h> 42 43 #include <net/vlan/if_vlan_var.h> 44 #include <net/vlan/if_vlan_ether.h> 45 46 void 47 vlan_start_dispatch(netmsg_t msg) 48 { 49 struct netmsg_packet *nmp = &msg->packet; 50 struct mbuf *m; 51 struct ifnet *ifp; 52 struct altq_pktattr pktattr; 53 #ifdef DEBUG 54 char hexstr[HEX_NCPYLEN(sizeof(struct ether_vlan_header))]; 55 #endif 56 m = nmp->nm_packet; 57 ifp = msg->lmsg.u.ms_resultp; 58 59 M_ASSERTPKTHDR(m); 60 KASSERT(m->m_flags & M_VLANTAG, ("mbuf has not been vlan tagged!")); 61 62 /* 63 * If ALTQ is enabled on the parent interface, do 64 * classification; the queueing discipline might 65 * not require classification, but might require 66 * the address family/header pointer in the pktattr. 67 */ 68 if (ifq_is_enabled(&ifp->if_snd)) 69 altq_etherclassify(&ifp->if_snd, m, &pktattr); 70 71 /* 72 * If underlying interface can do VLAN tag insertion itself, 73 * just pass the packet along. 74 */ 75 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) { 76 uint16_t vlantag = m->m_pkthdr.ether_vlantag; 77 struct ether_vlan_header *evl; 78 79 M_PREPEND(m, EVL_ENCAPLEN, M_NOWAIT); 80 if (m == NULL) { 81 if_printf(ifp, "vlan%u M_PREPEND failed", vlantag); 82 return; 83 } 84 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */ 85 86 m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN); 87 if (m == NULL) { 88 if_printf(ifp, "vlan%u m_pullup failed", vlantag); 89 return; 90 } 91 m->m_pkthdr.csum_lhlen = sizeof(struct ether_vlan_header); 92 93 /* 94 * Transform the Ethernet header into an Ethernet header 95 * with 802.1Q encapsulation. 96 */ 97 bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *), 98 sizeof(struct ether_header)); 99 evl = mtod(m, struct ether_vlan_header *); 100 evl->evl_proto = evl->evl_encap_proto; 101 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 102 evl->evl_tag = htons(vlantag); 103 #ifdef DEBUG 104 kprintf("vlan_start: %s\n", hexncpy((u_char *)evl, sizeof(*evl), 105 hexstr, HEX_NCPYLEN(sizeof(*evl)), ":")); 106 #endif 107 /* Hardware does not need to setup vlan tagging */ 108 m->m_flags &= ~M_VLANTAG; 109 } 110 ifq_dispatch(ifp, m, &pktattr); 111 } 112 113 void 114 vlan_ether_ptap(struct bpf_if *bp, struct mbuf *m, uint16_t vlantag) 115 { 116 const struct ether_header *eh; 117 struct ether_vlan_header evh; 118 119 KASSERT(m->m_len >= ETHER_HDR_LEN, 120 ("ether header is not contiguous!")); 121 122 eh = mtod(m, const struct ether_header *); 123 m_adj(m, ETHER_HDR_LEN); 124 125 bcopy(eh, &evh, 2 * ETHER_ADDR_LEN); 126 evh.evl_encap_proto = htons(ETHERTYPE_VLAN); 127 evh.evl_tag = htons(vlantag); 128 evh.evl_proto = eh->ether_type; 129 bpf_ptap(bp, m, &evh, ETHER_HDR_LEN + EVL_ENCAPLEN); 130 131 /* XXX assumes data was left intact */ 132 M_PREPEND(m, ETHER_HDR_LEN, M_WAITOK); 133 } 134 135 void 136 vlan_ether_decap(struct mbuf **m0) 137 { 138 struct mbuf *m = *m0; 139 struct ether_vlan_header *evh; 140 141 KKASSERT((m->m_flags & M_VLANTAG) == 0); 142 143 if (m->m_len < sizeof(*evh)) { 144 /* Error in the caller */ 145 m_freem(m); 146 *m0 = NULL; 147 return; 148 } 149 evh = mtod(m, struct ether_vlan_header *); 150 151 m->m_pkthdr.ether_vlantag = ntohs(evh->evl_tag); 152 m->m_flags |= M_VLANTAG; 153 154 bcopy((uint8_t *)evh, (uint8_t *)evh + EVL_ENCAPLEN, 155 2 * ETHER_ADDR_LEN); 156 m_adj(m, EVL_ENCAPLEN); 157 *m0 = m; 158 } 159