mbuf: Save linker layer, IP and TCP/UDP header length
[dragonfly.git] / sys / net / vlan / if_vlan_ether.c
CommitLineData
83790f85
SZ
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 $
83790f85
SZ
30 */
31
32#include <sys/param.h>
33#include <sys/mbuf.h>
34#include <sys/serialize.h>
35
4f7ea046 36#include <net/bpf.h>
83790f85
SZ
37#include <net/ethernet.h>
38#include <net/if.h>
39#include <net/if_arp.h>
40#include <net/ifq_var.h>
83790f85
SZ
41#include <net/netmsg.h>
42
4f7ea046
SZ
43#include <net/vlan/if_vlan_var.h>
44#include <net/vlan/if_vlan_ether.h>
83790f85
SZ
45
46void
002c1265 47vlan_start_dispatch(netmsg_t msg)
83790f85 48{
002c1265 49 struct netmsg_packet *nmp = &msg->packet;
83790f85
SZ
50 struct mbuf *m;
51 struct ifnet *ifp;
52 struct altq_pktattr pktattr;
53
54 m = nmp->nm_packet;
002c1265 55 ifp = msg->lmsg.u.ms_resultp;
83790f85
SZ
56
57 M_ASSERTPKTHDR(m);
ed20d0e3 58 KASSERT(m->m_flags & M_VLANTAG, ("mbuf has not been vlan tagged!"));
83790f85 59
a3dd34d2 60 ifnet_serialize_tx(ifp);
83790f85
SZ
61
62 /*
63 * Make sure that the interface is still UP and RUNNING,
64 * since interface state may have been changed when the
65 * mbuf is pending on the msgport.
66 */
67 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
68 (IFF_UP | IFF_RUNNING)) {
69 m_freem(m);
70 goto back;
71 }
72
73 /*
74 * If ALTQ is enabled on the parent interface, do
75 * classification; the queueing discipline might
76 * not require classification, but might require
77 * the address family/header pointer in the pktattr.
78 */
79 if (ifq_is_enabled(&ifp->if_snd))
80 altq_etherclassify(&ifp->if_snd, m, &pktattr);
81
82 /*
83 * If underlying interface can do VLAN tag insertion itself,
84 * just pass the packet along.
85 */
86 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) {
87 uint16_t vlantag = m->m_pkthdr.ether_vlantag;
88 struct ether_vlan_header *evl;
89
90 M_PREPEND(m, EVL_ENCAPLEN, MB_DONTWAIT);
91 if (m == NULL) {
92 if_printf(ifp, "vlan%u M_PREPEND failed", vlantag);
93 goto back;
94 }
95 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
96
97 m = m_pullup(m, ETHER_HDR_LEN + EVL_ENCAPLEN);
98 if (m == NULL) {
99 if_printf(ifp, "vlan%u m_pullup failed", vlantag);
100 goto back;
101 }
7df36335 102 m->m_pkthdr.csum_lhlen = sizeof(struct ether_vlan_header);
83790f85
SZ
103
104 /*
105 * Transform the Ethernet header into an Ethernet header
106 * with 802.1Q encapsulation.
107 */
108 bcopy(mtod(m, char *) + EVL_ENCAPLEN, mtod(m, char *),
109 sizeof(struct ether_header));
110 evl = mtod(m, struct ether_vlan_header *);
111 evl->evl_proto = evl->evl_encap_proto;
112 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
113 evl->evl_tag = htons(vlantag);
114#ifdef DEBUG
664f5072 115 kprintf("vlan_start: %*D\n", (int)sizeof *evl,
83790f85
SZ
116 (unsigned char *)evl, ":");
117#endif
118 /* Hardware does not need to setup vlan tagging */
119 m->m_flags &= ~M_VLANTAG;
120 }
121 ifq_handoff(ifp, m, &pktattr);
122back:
a3dd34d2 123 ifnet_deserialize_tx(ifp);
83790f85 124}
4f7ea046
SZ
125
126void
127vlan_ether_ptap(struct bpf_if *bp, struct mbuf *m, uint16_t vlantag)
128{
129 const struct ether_header *eh;
130 struct ether_vlan_header evh;
131
132 KASSERT(m->m_len >= ETHER_HDR_LEN,
ed20d0e3 133 ("ether header is not contiguous!"));
4f7ea046
SZ
134
135 eh = mtod(m, const struct ether_header *);
136 m_adj(m, ETHER_HDR_LEN);
137
138 bcopy(eh, &evh, 2 * ETHER_ADDR_LEN);
139 evh.evl_encap_proto = htons(ETHERTYPE_VLAN);
140 evh.evl_tag = htons(vlantag);
141 evh.evl_proto = eh->ether_type;
142 bpf_ptap(bp, m, &evh, ETHER_HDR_LEN + EVL_ENCAPLEN);
143
144 /* XXX assumes data was left intact */
145 M_PREPEND(m, ETHER_HDR_LEN, MB_WAIT);
146}
e6b5847c
SZ
147
148void
149vlan_ether_decap(struct mbuf **m0)
150{
151 struct mbuf *m = *m0;
152 struct ether_vlan_header *evh;
153
154 KKASSERT((m->m_flags & M_VLANTAG) == 0);
155
156 if (m->m_len < sizeof(*evh)) {
157 /* Error in the caller */
158 m_freem(m);
159 *m0 = NULL;
160 return;
161 }
162 evh = mtod(m, struct ether_vlan_header *);
163
164 m->m_pkthdr.ether_vlantag = ntohs(evh->evl_tag);
165 m->m_flags |= M_VLANTAG;
166
167 bcopy((uint8_t *)evh, (uint8_t *)evh + EVL_ENCAPLEN,
168 2 * ETHER_ADDR_LEN);
169 m_adj(m, EVL_ENCAPLEN);
170 *m0 = m;
171}