1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 #include <sys/cdefs.h>
22 #include "opt_wlan.h"
23
24 #include <sys/param.h>
25 #include <sys/lock.h>
26 #include <sys/mutex.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/taskqueue.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36
37 #include <net/if.h>
38 #include <net/if_var.h>
39 #include <net/ethernet.h>
40 #include <net/if_media.h>
41
42 #include <net80211/ieee80211_var.h>
43 #include <net80211/ieee80211_radiotap.h>
44 #include <net80211/ieee80211_ratectl.h>
45 #ifdef IEEE80211_SUPPORT_SUPERG
46 #include <net80211/ieee80211_superg.h>
47 #endif
48
49 #include <dev/rtwn/if_rtwnreg.h>
50 #include <dev/rtwn/if_rtwnvar.h>
51
52 #include <dev/rtwn/if_rtwn_beacon.h>
53 #include <dev/rtwn/if_rtwn_debug.h>
54 #include <dev/rtwn/if_rtwn_ridx.h>
55 #include <dev/rtwn/if_rtwn_tx.h>
56
57 void
rtwn_drain_mbufq(struct rtwn_softc * sc)58 rtwn_drain_mbufq(struct rtwn_softc *sc)
59 {
60 struct mbuf *m;
61 struct ieee80211_node *ni;
62 RTWN_ASSERT_LOCKED(sc);
63 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
64 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
65 m->m_pkthdr.rcvif = NULL;
66 ieee80211_free_node(ni);
67 m_freem(m);
68 }
69 }
70
71 #ifdef IEEE80211_SUPPORT_SUPERG
72 void
rtwn_ff_flush_all(struct rtwn_softc * sc,union sec_param * data)73 rtwn_ff_flush_all(struct rtwn_softc *sc, union sec_param *data)
74 {
75 struct ieee80211com *ic = &sc->sc_ic;
76
77 RTWN_UNLOCK(sc);
78 ieee80211_ff_flush_all(ic);
79 RTWN_LOCK(sc);
80 }
81 #endif
82
83 static uint8_t
rtwn_get_cipher(u_int ic_cipher)84 rtwn_get_cipher(u_int ic_cipher)
85 {
86 uint8_t cipher;
87
88 switch (ic_cipher) {
89 case IEEE80211_CIPHER_NONE:
90 cipher = RTWN_TXDW1_CIPHER_NONE;
91 break;
92 case IEEE80211_CIPHER_WEP:
93 case IEEE80211_CIPHER_TKIP:
94 cipher = RTWN_TXDW1_CIPHER_RC4;
95 break;
96 case IEEE80211_CIPHER_AES_CCM:
97 cipher = RTWN_TXDW1_CIPHER_AES;
98 break;
99 default:
100 KASSERT(0, ("%s: unknown cipher %d\n", __func__,
101 ic_cipher));
102 return (RTWN_TXDW1_CIPHER_SM4);
103 }
104
105 return (cipher);
106 }
107
108 static int
rtwn_tx_data(struct rtwn_softc * sc,struct ieee80211_node * ni,struct mbuf * m)109 rtwn_tx_data(struct rtwn_softc *sc, struct ieee80211_node *ni,
110 struct mbuf *m)
111 {
112 const struct ieee80211_txparam *tp = ni->ni_txparms;
113 struct ieee80211com *ic = &sc->sc_ic;
114 struct ieee80211vap *vap = ni->ni_vap;
115 struct ieee80211_key *k = NULL;
116 struct ieee80211_frame *wh;
117 struct rtwn_tx_desc_common *txd;
118 struct rtwn_tx_buf buf;
119 uint8_t rate, ridx, type;
120 bool force_rate = false;
121 u_int cipher;
122 int ismcast;
123
124 RTWN_ASSERT_LOCKED(sc);
125
126 wh = mtod(m, struct ieee80211_frame *);
127 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
128 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
129
130 /* Choose a TX rate index. */
131 if (type == IEEE80211_FC0_TYPE_MGT ||
132 type == IEEE80211_FC0_TYPE_CTL ||
133 (m->m_flags & M_EAPOL) != 0) {
134 rate = tp->mgmtrate;
135 force_rate = true;
136 } else if (ismcast) {
137 force_rate = true;
138 rate = tp->mcastrate;
139 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
140 force_rate = true;
141 rate = tp->ucastrate;
142 } else {
143 if (sc->sc_ratectl == RTWN_RATECTL_NET80211) {
144 /* XXX pass pktlen */
145 (void) ieee80211_ratectl_rate(ni, NULL, 0);
146 rate = ni->ni_txrate;
147 } else {
148 if (ni->ni_flags & IEEE80211_NODE_HT)
149 rate = IEEE80211_RATE_MCS | 0x4; /* MCS4 */
150 else if (ic->ic_curmode != IEEE80211_MODE_11B)
151 rate = ridx2rate[RTWN_RIDX_OFDM36];
152 else
153 rate = ridx2rate[RTWN_RIDX_CCK55];
154 }
155 }
156
157 ridx = rate2ridx(rate);
158
159 cipher = IEEE80211_CIPHER_NONE;
160 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
161 k = ieee80211_crypto_encap(ni, m);
162 if (k == NULL) {
163 device_printf(sc->sc_dev,
164 "ieee80211_crypto_encap returns NULL.\n");
165 return (ENOBUFS);
166 }
167 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT))
168 cipher = k->wk_cipher->ic_cipher;
169
170 /* in case packet header moved, reset pointer */
171 wh = mtod(m, struct ieee80211_frame *);
172 }
173
174 /* Fill Tx descriptor. */
175 txd = (struct rtwn_tx_desc_common *)&buf;
176 memset(txd, 0, sc->txdesc_len);
177 txd->txdw1 = htole32(SM(RTWN_TXDW1_CIPHER, rtwn_get_cipher(cipher)));
178
179 rtwn_fill_tx_desc(sc, ni, m, txd, ridx, force_rate, tp->maxretry);
180
181 if (ieee80211_radiotap_active_vap(vap)) {
182 struct rtwn_tx_radiotap_header *tap = &sc->sc_txtap;
183
184 tap->wt_flags = rtwn_tx_radiotap_flags(sc, txd);
185 if (k != NULL)
186 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
187 ieee80211_radiotap_tx(vap, m);
188 }
189
190 return (rtwn_tx_start(sc, ni, m, (uint8_t *)txd, type, 0));
191 }
192
193 static int
rtwn_tx_raw(struct rtwn_softc * sc,struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)194 rtwn_tx_raw(struct rtwn_softc *sc, struct ieee80211_node *ni,
195 struct mbuf *m, const struct ieee80211_bpf_params *params)
196 {
197 struct ieee80211vap *vap = ni->ni_vap;
198 struct ieee80211_key *k = NULL;
199 struct ieee80211_frame *wh;
200 struct rtwn_tx_desc_common *txd;
201 struct rtwn_tx_buf buf;
202 uint8_t type;
203 u_int cipher;
204
205 /* Encrypt the frame if need be. */
206 cipher = IEEE80211_CIPHER_NONE;
207 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
208 /* Retrieve key for TX. */
209 k = ieee80211_crypto_encap(ni, m);
210 if (k == NULL) {
211 device_printf(sc->sc_dev,
212 "ieee80211_crypto_encap returns NULL.\n");
213 return (ENOBUFS);
214 }
215 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT))
216 cipher = k->wk_cipher->ic_cipher;
217 }
218
219 wh = mtod(m, struct ieee80211_frame *);
220 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
221
222 /* Fill Tx descriptor. */
223 txd = (struct rtwn_tx_desc_common *)&buf;
224 memset(txd, 0, sc->txdesc_len);
225 txd->txdw1 = htole32(SM(RTWN_TXDW1_CIPHER, rtwn_get_cipher(cipher)));
226
227 rtwn_fill_tx_desc_raw(sc, ni, m, txd, params);
228
229 if (ieee80211_radiotap_active_vap(vap)) {
230 struct rtwn_tx_radiotap_header *tap = &sc->sc_txtap;
231
232 tap->wt_flags = rtwn_tx_radiotap_flags(sc, txd);
233 if (k != NULL)
234 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
235 ieee80211_radiotap_tx(vap, m);
236 }
237
238 return (rtwn_tx_start(sc, ni, m, (uint8_t *)txd, type, 0));
239 }
240
241 int
rtwn_transmit(struct ieee80211com * ic,struct mbuf * m)242 rtwn_transmit(struct ieee80211com *ic, struct mbuf *m)
243 {
244 struct rtwn_softc *sc = ic->ic_softc;
245 int error;
246
247 RTWN_LOCK(sc);
248 if ((sc->sc_flags & RTWN_RUNNING) == 0) {
249 RTWN_UNLOCK(sc);
250 return (ENXIO);
251 }
252 error = mbufq_enqueue(&sc->sc_snd, m);
253 if (error) {
254 RTWN_UNLOCK(sc);
255 return (error);
256 }
257 rtwn_start(sc);
258 RTWN_UNLOCK(sc);
259
260 return (0);
261 }
262
263 void
rtwn_start(struct rtwn_softc * sc)264 rtwn_start(struct rtwn_softc *sc)
265 {
266 struct ieee80211_node *ni;
267 struct mbuf *m;
268
269 RTWN_ASSERT_LOCKED(sc);
270
271 /* Ensure no work is scheduled during reset/teardown */
272 if ((sc->sc_flags & RTWN_RUNNING) == 0)
273 return;
274
275 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
276 if (sc->qfullmsk != 0) {
277 mbufq_prepend(&sc->sc_snd, m);
278 break;
279 }
280 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
281 m->m_pkthdr.rcvif = NULL;
282
283 RTWN_DPRINTF(sc, RTWN_DEBUG_XMIT,
284 "%s: called; m %p, ni %p\n", __func__, m, ni);
285
286 if (rtwn_tx_data(sc, ni, m) != 0) {
287 if_inc_counter(ni->ni_vap->iv_ifp,
288 IFCOUNTER_OERRORS, 1);
289 m_freem(m);
290 #ifdef D4054
291 ieee80211_tx_watchdog_refresh(ni->ni_ic, -1, 0);
292 #endif
293 ieee80211_free_node(ni);
294 break;
295 }
296 }
297 }
298
299 int
rtwn_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)300 rtwn_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
301 const struct ieee80211_bpf_params *params)
302 {
303 struct ieee80211com *ic = ni->ni_ic;
304 struct rtwn_softc *sc = ic->ic_softc;
305 int error;
306
307 RTWN_DPRINTF(sc, RTWN_DEBUG_XMIT, "%s: called; m %p, ni %p\n",
308 __func__, m, ni);
309
310 /* prevent management frames from being sent if we're not ready */
311 RTWN_LOCK(sc);
312 if (!(sc->sc_flags & RTWN_RUNNING)) {
313 error = ENETDOWN;
314 goto end;
315 }
316
317 if (sc->qfullmsk != 0) {
318 error = ENOBUFS;
319 goto end;
320 }
321
322 if (params == NULL) {
323 /*
324 * Legacy path; interpret frame contents to decide
325 * precisely how to send the frame.
326 */
327 error = rtwn_tx_data(sc, ni, m);
328 } else {
329 /*
330 * Caller supplied explicit parameters to use in
331 * sending the frame.
332 */
333 error = rtwn_tx_raw(sc, ni, m, params);
334 }
335
336 end:
337 if (error != 0) {
338 if (m->m_flags & M_TXCB)
339 ieee80211_process_callback(ni, m, 1);
340 m_freem(m);
341 }
342
343 RTWN_UNLOCK(sc);
344
345 return (error);
346 }
347