1 /*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 #include "opt_wlan.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/mbuf.h>
37 #include <sys/kernel.h>
38 #include <sys/endian.h>
39
40 #include <sys/socket.h>
41
42 #include <net/bpf.h>
43 #include <net/ethernet.h>
44 #include <net/if.h>
45 #include <net/if_llc.h>
46 #include <net/if_media.h>
47 #include <net/if_vlan_var.h>
48
49 #include <net80211/ieee80211_var.h>
50 #include <net80211/ieee80211_regdomain.h>
51 #ifdef IEEE80211_SUPPORT_SUPERG
52 #include <net80211/ieee80211_superg.h>
53 #endif
54 #ifdef IEEE80211_SUPPORT_TDMA
55 #include <net80211/ieee80211_tdma.h>
56 #endif
57 #include <net80211/ieee80211_wds.h>
58 #include <net80211/ieee80211_mesh.h>
59
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/if_ether.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #endif
66 #ifdef INET6
67 #include <netinet/ip6.h>
68 #endif
69
70 #include <security/mac/mac_framework.h>
71
72 #define ETHER_HEADER_COPY(dst, src) \
73 memcpy(dst, src, sizeof(struct ether_header))
74
75 /* unalligned little endian access */
76 #define LE_WRITE_2(p, v) do { \
77 ((uint8_t *)(p))[0] = (v) & 0xff; \
78 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
79 } while (0)
80 #define LE_WRITE_4(p, v) do { \
81 ((uint8_t *)(p))[0] = (v) & 0xff; \
82 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
83 ((uint8_t *)(p))[2] = ((v) >> 16) & 0xff; \
84 ((uint8_t *)(p))[3] = ((v) >> 24) & 0xff; \
85 } while (0)
86
87 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
88 u_int hdrsize, u_int ciphdrsize, u_int mtu);
89 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
90
91 #ifdef IEEE80211_DEBUG
92 /*
93 * Decide if an outbound management frame should be
94 * printed when debugging is enabled. This filters some
95 * of the less interesting frames that come frequently
96 * (e.g. beacons).
97 */
98 static __inline int
99 doprint(struct ieee80211vap *vap, int subtype)
100 {
101 switch (subtype) {
102 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
103 return (vap->iv_opmode == IEEE80211_M_IBSS);
104 }
105 return 1;
106 }
107 #endif
108
109 /*
110 * Start method for vap's. All packets from the stack come
111 * through here. We handle common processing of the packets
112 * before dispatching them to the underlying device.
113 */
114 void
115 ieee80211_start(struct ifnet *ifp)
116 {
117 #define IS_DWDS(vap) \
118 (vap->iv_opmode == IEEE80211_M_WDS && \
119 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
120 struct ieee80211vap *vap = ifp->if_softc;
121 struct ieee80211com *ic = vap->iv_ic;
122 struct ifnet *parent = ic->ic_ifp;
123 struct ieee80211_node *ni;
124 struct mbuf *m;
125 struct ether_header *eh;
126 int error;
127
128 /* NB: parent must be up and running */
129 if (!IFNET_IS_UP_RUNNING(parent)) {
130 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
131 "%s: ignore queue, parent %s not up+running\n",
132 __func__, parent->if_xname);
133 /* XXX stat */
134 return;
135 }
136 if (vap->iv_state == IEEE80211_S_SLEEP) {
137 /*
138 * In power save, wakeup device for transmit.
139 */
140 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
141 return;
142 }
143 /*
144 * No data frames go out unless we're running.
145 * Note in particular this covers CAC and CSA
146 * states (though maybe we should check muting
147 * for CSA).
148 */
149 if (vap->iv_state != IEEE80211_S_RUN) {
150 IEEE80211_LOCK(ic);
151 /* re-check under the com lock to avoid races */
152 if (vap->iv_state != IEEE80211_S_RUN) {
153 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
154 "%s: ignore queue, in %s state\n",
155 __func__, ieee80211_state_name[vap->iv_state]);
156 vap->iv_stats.is_tx_badstate++;
157 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
158 IEEE80211_UNLOCK(ic);
159 return;
160 }
161 IEEE80211_UNLOCK(ic);
162 }
163 for (;;) {
164 IFQ_DEQUEUE(&ifp->if_snd, m);
165 if (m == NULL)
166 break;
167 /*
168 * Sanitize mbuf flags for net80211 use. We cannot
169 * clear M_PWR_SAV or M_MORE_DATA because these may
170 * be set for frames that are re-submitted from the
171 * power save queue.
172 *
173 * NB: This must be done before ieee80211_classify as
174 * it marks EAPOL in frames with M_EAPOL.
175 */
176 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
177 /*
178 * Cancel any background scan.
179 */
180 if (ic->ic_flags & IEEE80211_F_SCAN)
181 ieee80211_cancel_anyscan(vap);
182 /*
183 * Find the node for the destination so we can do
184 * things like power save and fast frames aggregation.
185 *
186 * NB: past this point various code assumes the first
187 * mbuf has the 802.3 header present (and contiguous).
188 */
189 ni = NULL;
190 if (m->m_len < sizeof(struct ether_header) &&
191 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
192 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
193 "discard frame, %s\n", "m_pullup failed");
194 vap->iv_stats.is_tx_nobuf++; /* XXX */
195 ifp->if_oerrors++;
196 continue;
197 }
198 eh = mtod(m, struct ether_header *);
199 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
200 if (IS_DWDS(vap)) {
201 /*
202 * Only unicast frames from the above go out
203 * DWDS vaps; multicast frames are handled by
204 * dispatching the frame as it comes through
205 * the AP vap (see below).
206 */
207 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
208 eh->ether_dhost, "mcast", "%s", "on DWDS");
209 vap->iv_stats.is_dwds_mcast++;
210 m_freem(m);
211 continue;
212 }
213 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
214 /*
215 * Spam DWDS vap's w/ multicast traffic.
216 */
217 /* XXX only if dwds in use? */
218 ieee80211_dwds_mcast(vap, m);
219 }
220 }
221 #ifdef IEEE80211_SUPPORT_MESH
222 if (vap->iv_opmode != IEEE80211_M_MBSS) {
223 #endif
224 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
225 if (ni == NULL) {
226 /* NB: ieee80211_find_txnode does stat+msg */
227 ifp->if_oerrors++;
228 m_freem(m);
229 continue;
230 }
231 if (ni->ni_associd == 0 &&
232 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
233 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
234 eh->ether_dhost, NULL,
235 "sta not associated (type 0x%04x)",
236 htons(eh->ether_type));
237 vap->iv_stats.is_tx_notassoc++;
238 ifp->if_oerrors++;
239 m_freem(m);
240 ieee80211_free_node(ni);
241 continue;
242 }
243 #ifdef IEEE80211_SUPPORT_MESH
244 } else {
245 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
246 /*
247 * Proxy station only if configured.
248 */
249 if (!ieee80211_mesh_isproxyena(vap)) {
250 IEEE80211_DISCARD_MAC(vap,
251 IEEE80211_MSG_OUTPUT |
252 IEEE80211_MSG_MESH,
253 eh->ether_dhost, NULL,
254 "%s", "proxy not enabled");
255 vap->iv_stats.is_mesh_notproxy++;
256 ifp->if_oerrors++;
257 m_freem(m);
258 continue;
259 }
260 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
261 }
262 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
263 if (ni == NULL) {
264 /*
265 * NB: ieee80211_mesh_discover holds/disposes
266 * frame (e.g. queueing on path discovery).
267 */
268 ifp->if_oerrors++;
269 continue;
270 }
271 }
272 #endif
273 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
274 (m->m_flags & M_PWR_SAV) == 0) {
275 /*
276 * Station in power save mode; pass the frame
277 * to the 802.11 layer and continue. We'll get
278 * the frame back when the time is right.
279 * XXX lose WDS vap linkage?
280 */
281 (void) ieee80211_pwrsave(ni, m);
282 ieee80211_free_node(ni);
283 continue;
284 }
285 /* calculate priority so drivers can find the tx queue */
286 if (ieee80211_classify(ni, m)) {
287 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
288 eh->ether_dhost, NULL,
289 "%s", "classification failure");
290 vap->iv_stats.is_tx_classify++;
291 ifp->if_oerrors++;
292 m_freem(m);
293 ieee80211_free_node(ni);
294 continue;
295 }
296 /*
297 * Stash the node pointer. Note that we do this after
298 * any call to ieee80211_dwds_mcast because that code
299 * uses any existing value for rcvif to identify the
300 * interface it (might have been) received on.
301 */
302 m->m_pkthdr.rcvif = (void *)ni;
303
304 BPF_MTAP(ifp, m); /* 802.3 tx */
305
306 /*
307 * Check if A-MPDU tx aggregation is setup or if we
308 * should try to enable it. The sta must be associated
309 * with HT and A-MPDU enabled for use. When the policy
310 * routine decides we should enable A-MPDU we issue an
311 * ADDBA request and wait for a reply. The frame being
312 * encapsulated will go out w/o using A-MPDU, or possibly
313 * it might be collected by the driver and held/retransmit.
314 * The default ic_ampdu_enable routine handles staggering
315 * ADDBA requests in case the receiver NAK's us or we are
316 * otherwise unable to establish a BA stream.
317 */
318 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
319 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
320 (m->m_flags & M_EAPOL) == 0) {
321 const int ac = M_WME_GETAC(m);
322 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac];
323
324 ieee80211_txampdu_count_packet(tap);
325 if (IEEE80211_AMPDU_RUNNING(tap)) {
326 /*
327 * Operational, mark frame for aggregation.
328 *
329 * XXX do tx aggregation here
330 */
331 m->m_flags |= M_AMPDU_MPDU;
332 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
333 ic->ic_ampdu_enable(ni, tap)) {
334 /*
335 * Not negotiated yet, request service.
336 */
337 ieee80211_ampdu_request(ni, tap);
338 /* XXX hold frame for reply? */
339 }
340 }
341 #ifdef IEEE80211_SUPPORT_SUPERG
342 else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
343 m = ieee80211_ff_check(ni, m);
344 if (m == NULL) {
345 /* NB: any ni ref held on stageq */
346 continue;
347 }
348 }
349 #endif /* IEEE80211_SUPPORT_SUPERG */
350 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
351 /*
352 * Encapsulate the packet in prep for transmission.
353 */
354 m = ieee80211_encap(vap, ni, m);
355 if (m == NULL) {
356 /* NB: stat+msg handled in ieee80211_encap */
357 ieee80211_free_node(ni);
358 continue;
359 }
360 }
361
362 error = parent->if_transmit(parent, m);
363 if (error != 0) {
364 /* NB: IFQ_HANDOFF reclaims mbuf */
365 ieee80211_free_node(ni);
366 } else {
367 ifp->if_opackets++;
368 }
369 ic->ic_lastdata = ticks;
370 }
371 #undef IS_DWDS
372 }
373
374 /*
375 * 802.11 output routine. This is (currently) used only to
376 * connect bpf write calls to the 802.11 layer for injecting
377 * raw 802.11 frames.
378 */
379 int
380 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
381 struct sockaddr *dst, struct route *ro)
382 {
383 #define senderr(e) do { error = (e); goto bad;} while (0)
384 struct ieee80211_node *ni = NULL;
385 struct ieee80211vap *vap;
386 struct ieee80211_frame *wh;
387 int error;
388
389 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
390 /*
391 * Short-circuit requests if the vap is marked OACTIVE
392 * as this can happen because a packet came down through
393 * ieee80211_start before the vap entered RUN state in
394 * which case it's ok to just drop the frame. This
395 * should not be necessary but callers of if_output don't
396 * check OACTIVE.
397 */
398 senderr(ENETDOWN);
399 }
400 vap = ifp->if_softc;
401 /*
402 * Hand to the 802.3 code if not tagged as
403 * a raw 802.11 frame.
404 */
405 if (dst->sa_family != AF_IEEE80211)
406 return vap->iv_output(ifp, m, dst, ro);
407 #ifdef MAC
408 error = mac_ifnet_check_transmit(ifp, m);
409 if (error)
410 senderr(error);
411 #endif
412 if (ifp->if_flags & IFF_MONITOR)
413 senderr(ENETDOWN);
414 if (!IFNET_IS_UP_RUNNING(ifp))
415 senderr(ENETDOWN);
416 if (vap->iv_state == IEEE80211_S_CAC) {
417 IEEE80211_DPRINTF(vap,
418 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
419 "block %s frame in CAC state\n", "raw data");
420 vap->iv_stats.is_tx_badstate++;
421 senderr(EIO); /* XXX */
422 }
423 /* XXX bypass bridge, pfil, carp, etc. */
424
425 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
426 senderr(EIO); /* XXX */
427 wh = mtod(m, struct ieee80211_frame *);
428 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
429 IEEE80211_FC0_VERSION_0)
430 senderr(EIO); /* XXX */
431
432 /* locate destination node */
433 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
434 case IEEE80211_FC1_DIR_NODS:
435 case IEEE80211_FC1_DIR_FROMDS:
436 ni = ieee80211_find_txnode(vap, wh->i_addr1);
437 break;
438 case IEEE80211_FC1_DIR_TODS:
439 case IEEE80211_FC1_DIR_DSTODS:
440 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
441 senderr(EIO); /* XXX */
442 ni = ieee80211_find_txnode(vap, wh->i_addr3);
443 break;
444 default:
445 senderr(EIO); /* XXX */
446 }
447 if (ni == NULL) {
448 /*
449 * Permit packets w/ bpf params through regardless
450 * (see below about sa_len).
451 */
452 if (dst->sa_len == 0)
453 senderr(EHOSTUNREACH);
454 ni = ieee80211_ref_node(vap->iv_bss);
455 }
456
457 /*
458 * Sanitize mbuf for net80211 flags leaked from above.
459 *
460 * NB: This must be done before ieee80211_classify as
461 * it marks EAPOL in frames with M_EAPOL.
462 */
463 m->m_flags &= ~M_80211_TX;
464
465 /* calculate priority so drivers can find the tx queue */
466 /* XXX assumes an 802.3 frame */
467 if (ieee80211_classify(ni, m))
468 senderr(EIO); /* XXX */
469
470 ifp->if_opackets++;
471 IEEE80211_NODE_STAT(ni, tx_data);
472 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
473 IEEE80211_NODE_STAT(ni, tx_mcast);
474 m->m_flags |= M_MCAST;
475 } else
476 IEEE80211_NODE_STAT(ni, tx_ucast);
477 /* NB: ieee80211_encap does not include 802.11 header */
478 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
479
480 /*
481 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
482 * present by setting the sa_len field of the sockaddr (yes,
483 * this is a hack).
484 * NB: we assume sa_data is suitably aligned to cast.
485 */
486 return vap->iv_ic->ic_raw_xmit(ni, m,
487 (const struct ieee80211_bpf_params *)(dst->sa_len ?
488 dst->sa_data : NULL));
489 bad:
490 if (m != NULL)
491 m_freem(m);
492 if (ni != NULL)
493 ieee80211_free_node(ni);
494 ifp->if_oerrors++;
495 return error;
496 #undef senderr
497 }
498
499 /*
500 * Set the direction field and address fields of an outgoing
501 * frame. Note this should be called early on in constructing
502 * a frame as it sets i_fc[1]; other bits can then be or'd in.
503 */
504 void
505 ieee80211_send_setup(
506 struct ieee80211_node *ni,
507 struct mbuf *m,
508 int type, int tid,
509 const uint8_t sa[IEEE80211_ADDR_LEN],
510 const uint8_t da[IEEE80211_ADDR_LEN],
511 const uint8_t bssid[IEEE80211_ADDR_LEN])
512 {
513 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
514 struct ieee80211vap *vap = ni->ni_vap;
515 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
516 ieee80211_seq seqno;
517
518 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
519 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
520 switch (vap->iv_opmode) {
521 case IEEE80211_M_STA:
522 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
523 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
524 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
525 IEEE80211_ADDR_COPY(wh->i_addr3, da);
526 break;
527 case IEEE80211_M_IBSS:
528 case IEEE80211_M_AHDEMO:
529 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
530 IEEE80211_ADDR_COPY(wh->i_addr1, da);
531 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
532 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
533 break;
534 case IEEE80211_M_HOSTAP:
535 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
536 IEEE80211_ADDR_COPY(wh->i_addr1, da);
537 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
538 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
539 break;
540 case IEEE80211_M_WDS:
541 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
542 IEEE80211_ADDR_COPY(wh->i_addr1, da);
543 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
544 IEEE80211_ADDR_COPY(wh->i_addr3, da);
545 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
546 break;
547 case IEEE80211_M_MBSS:
548 #ifdef IEEE80211_SUPPORT_MESH
549 /* XXX add support for proxied addresses */
550 if (IEEE80211_IS_MULTICAST(da)) {
551 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
552 /* XXX next hop */
553 IEEE80211_ADDR_COPY(wh->i_addr1, da);
554 IEEE80211_ADDR_COPY(wh->i_addr2,
555 vap->iv_myaddr);
556 } else {
557 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
558 IEEE80211_ADDR_COPY(wh->i_addr1, da);
559 IEEE80211_ADDR_COPY(wh->i_addr2,
560 vap->iv_myaddr);
561 IEEE80211_ADDR_COPY(wh->i_addr3, da);
562 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
563 }
564 #endif
565 break;
566 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
567 break;
568 }
569 } else {
570 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
571 IEEE80211_ADDR_COPY(wh->i_addr1, da);
572 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
573 #ifdef IEEE80211_SUPPORT_MESH
574 if (vap->iv_opmode == IEEE80211_M_MBSS)
575 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
576 else
577 #endif
578 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
579 }
580 *(uint16_t *)&wh->i_dur[0] = 0;
581
582 seqno = ni->ni_txseqs[tid]++;
583 *(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
584 M_SEQNO_SET(m, seqno);
585
586 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
587 m->m_flags |= M_MCAST;
588 #undef WH4
589 }
590
591 /*
592 * Send a management frame to the specified node. The node pointer
593 * must have a reference as the pointer will be passed to the driver
594 * and potentially held for a long time. If the frame is successfully
595 * dispatched to the driver, then it is responsible for freeing the
596 * reference (and potentially free'ing up any associated storage);
597 * otherwise deal with reclaiming any reference (on error).
598 */
599 int
600 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
601 struct ieee80211_bpf_params *params)
602 {
603 struct ieee80211vap *vap = ni->ni_vap;
604 struct ieee80211com *ic = ni->ni_ic;
605 struct ieee80211_frame *wh;
606
607 KASSERT(ni != NULL, ("null node"));
608
609 if (vap->iv_state == IEEE80211_S_CAC) {
610 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
611 ni, "block %s frame in CAC state",
612 ieee80211_mgt_subtype_name[
613 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
614 IEEE80211_FC0_SUBTYPE_SHIFT]);
615 vap->iv_stats.is_tx_badstate++;
616 ieee80211_free_node(ni);
617 m_freem(m);
618 return EIO; /* XXX */
619 }
620
621 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
622 if (m == NULL) {
623 ieee80211_free_node(ni);
624 return ENOMEM;
625 }
626
627 wh = mtod(m, struct ieee80211_frame *);
628 ieee80211_send_setup(ni, m,
629 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
630 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
631 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
632 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
633 "encrypting frame (%s)", __func__);
634 wh->i_fc[1] |= IEEE80211_FC1_WEP;
635 }
636 m->m_flags |= M_ENCAP; /* mark encapsulated */
637
638 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
639 M_WME_SETAC(m, params->ibp_pri);
640
641 #ifdef IEEE80211_DEBUG
642 /* avoid printing too many frames */
643 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
644 ieee80211_msg_dumppkts(vap)) {
645 printf("[%s] send %s on channel %u\n",
646 ether_sprintf(wh->i_addr1),
647 ieee80211_mgt_subtype_name[
648 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
649 IEEE80211_FC0_SUBTYPE_SHIFT],
650 ieee80211_chan2ieee(ic, ic->ic_curchan));
651 }
652 #endif
653 IEEE80211_NODE_STAT(ni, tx_mgmt);
654
655 return ic->ic_raw_xmit(ni, m, params);
656 }
657
658 /*
659 * Send a null data frame to the specified node. If the station
660 * is setup for QoS then a QoS Null Data frame is constructed.
661 * If this is a WDS station then a 4-address frame is constructed.
662 *
663 * NB: the caller is assumed to have setup a node reference
664 * for use; this is necessary to deal with a race condition
665 * when probing for inactive stations. Like ieee80211_mgmt_output
666 * we must cleanup any node reference on error; however we
667 * can safely just unref it as we know it will never be the
668 * last reference to the node.
669 */
670 int
671 ieee80211_send_nulldata(struct ieee80211_node *ni)
672 {
673 struct ieee80211vap *vap = ni->ni_vap;
674 struct ieee80211com *ic = ni->ni_ic;
675 struct mbuf *m;
676 struct ieee80211_frame *wh;
677 int hdrlen;
678 uint8_t *frm;
679
680 if (vap->iv_state == IEEE80211_S_CAC) {
681 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
682 ni, "block %s frame in CAC state", "null data");
683 ieee80211_unref_node(&ni);
684 vap->iv_stats.is_tx_badstate++;
685 return EIO; /* XXX */
686 }
687
688 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
689 hdrlen = sizeof(struct ieee80211_qosframe);
690 else
691 hdrlen = sizeof(struct ieee80211_frame);
692 /* NB: only WDS vap's get 4-address frames */
693 if (vap->iv_opmode == IEEE80211_M_WDS)
694 hdrlen += IEEE80211_ADDR_LEN;
695 if (ic->ic_flags & IEEE80211_F_DATAPAD)
696 hdrlen = roundup(hdrlen, sizeof(uint32_t));
697
698 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
699 if (m == NULL) {
700 /* XXX debug msg */
701 ieee80211_unref_node(&ni);
702 vap->iv_stats.is_tx_nobuf++;
703 return ENOMEM;
704 }
705 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
706 ("leading space %zd", M_LEADINGSPACE(m)));
707 M_PREPEND(m, hdrlen, M_DONTWAIT);
708 if (m == NULL) {
709 /* NB: cannot happen */
710 ieee80211_free_node(ni);
711 return ENOMEM;
712 }
713
714 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
715 if (ni->ni_flags & IEEE80211_NODE_QOS) {
716 const int tid = WME_AC_TO_TID(WME_AC_BE);
717 uint8_t *qos;
718
719 ieee80211_send_setup(ni, m,
720 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
721 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
722
723 if (vap->iv_opmode == IEEE80211_M_WDS)
724 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
725 else
726 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
727 qos[0] = tid & IEEE80211_QOS_TID;
728 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
729 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
730 qos[1] = 0;
731 } else {
732 ieee80211_send_setup(ni, m,
733 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
734 IEEE80211_NONQOS_TID,
735 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
736 }
737 if (vap->iv_opmode != IEEE80211_M_WDS) {
738 /* NB: power management bit is never sent by an AP */
739 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
740 vap->iv_opmode != IEEE80211_M_HOSTAP)
741 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
742 }
743 m->m_len = m->m_pkthdr.len = hdrlen;
744 m->m_flags |= M_ENCAP; /* mark encapsulated */
745
746 M_WME_SETAC(m, WME_AC_BE);
747
748 IEEE80211_NODE_STAT(ni, tx_data);
749
750 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
751 "send %snull data frame on channel %u, pwr mgt %s",
752 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
753 ieee80211_chan2ieee(ic, ic->ic_curchan),
754 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
755
756 return ic->ic_raw_xmit(ni, m, NULL);
757 }
758
759 /*
760 * Assign priority to a frame based on any vlan tag assigned
761 * to the station and/or any Diffserv setting in an IP header.
762 * Finally, if an ACM policy is setup (in station mode) it's
763 * applied.
764 */
765 int
766 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
767 {
768 const struct ether_header *eh = mtod(m, struct ether_header *);
769 int v_wme_ac, d_wme_ac, ac;
770
771 /*
772 * Always promote PAE/EAPOL frames to high priority.
773 */
774 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
775 /* NB: mark so others don't need to check header */
776 m->m_flags |= M_EAPOL;
777 ac = WME_AC_VO;
778 goto done;
779 }
780 /*
781 * Non-qos traffic goes to BE.
782 */
783 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
784 ac = WME_AC_BE;
785 goto done;
786 }
787
788 /*
789 * If node has a vlan tag then all traffic
790 * to it must have a matching tag.
791 */
792 v_wme_ac = 0;
793 if (ni->ni_vlan != 0) {
794 if ((m->m_flags & M_VLANTAG) == 0) {
795 IEEE80211_NODE_STAT(ni, tx_novlantag);
796 return 1;
797 }
798 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
799 EVL_VLANOFTAG(ni->ni_vlan)) {
800 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
801 return 1;
802 }
803 /* map vlan priority to AC */
804 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
805 }
806
807 /* XXX m_copydata may be too slow for fast path */
808 #ifdef INET
809 if (eh->ether_type == htons(ETHERTYPE_IP)) {
810 uint8_t tos;
811 /*
812 * IP frame, map the DSCP bits from the TOS field.
813 */
814 /* NB: ip header may not be in first mbuf */
815 m_copydata(m, sizeof(struct ether_header) +
816 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
817 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
818 d_wme_ac = TID_TO_WME_AC(tos);
819 } else {
820 #endif /* INET */
821 #ifdef INET6
822 if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
823 uint32_t flow;
824 uint8_t tos;
825 /*
826 * IPv6 frame, map the DSCP bits from the traffic class field.
827 */
828 m_copydata(m, sizeof(struct ether_header) +
829 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
830 (caddr_t) &flow);
831 tos = (uint8_t)(ntohl(flow) >> 20);
832 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
833 d_wme_ac = TID_TO_WME_AC(tos);
834 } else {
835 #endif /* INET6 */
836 d_wme_ac = WME_AC_BE;
837 #ifdef INET6
838 }
839 #endif
840 #ifdef INET
841 }
842 #endif
843 /*
844 * Use highest priority AC.
845 */
846 if (v_wme_ac > d_wme_ac)
847 ac = v_wme_ac;
848 else
849 ac = d_wme_ac;
850
851 /*
852 * Apply ACM policy.
853 */
854 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
855 static const int acmap[4] = {
856 WME_AC_BK, /* WME_AC_BE */
857 WME_AC_BK, /* WME_AC_BK */
858 WME_AC_BE, /* WME_AC_VI */
859 WME_AC_VI, /* WME_AC_VO */
860 };
861 struct ieee80211com *ic = ni->ni_ic;
862
863 while (ac != WME_AC_BK &&
864 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
865 ac = acmap[ac];
866 }
867 done:
868 M_WME_SETAC(m, ac);
869 return 0;
870 }
871
872 /*
873 * Insure there is sufficient contiguous space to encapsulate the
874 * 802.11 data frame. If room isn't already there, arrange for it.
875 * Drivers and cipher modules assume we have done the necessary work
876 * and fail rudely if they don't find the space they need.
877 */
878 struct mbuf *
879 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
880 struct ieee80211_key *key, struct mbuf *m)
881 {
882 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
883 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
884
885 if (key != NULL) {
886 /* XXX belongs in crypto code? */
887 needed_space += key->wk_cipher->ic_header;
888 /* XXX frags */
889 /*
890 * When crypto is being done in the host we must insure
891 * the data are writable for the cipher routines; clone
892 * a writable mbuf chain.
893 * XXX handle SWMIC specially
894 */
895 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
896 m = m_unshare(m, M_NOWAIT);
897 if (m == NULL) {
898 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
899 "%s: cannot get writable mbuf\n", __func__);
900 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
901 return NULL;
902 }
903 }
904 }
905 /*
906 * We know we are called just before stripping an Ethernet
907 * header and prepending an LLC header. This means we know
908 * there will be
909 * sizeof(struct ether_header) - sizeof(struct llc)
910 * bytes recovered to which we need additional space for the
911 * 802.11 header and any crypto header.
912 */
913 /* XXX check trailing space and copy instead? */
914 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
915 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
916 if (n == NULL) {
917 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
918 "%s: cannot expand storage\n", __func__);
919 vap->iv_stats.is_tx_nobuf++;
920 m_freem(m);
921 return NULL;
922 }
923 KASSERT(needed_space <= MHLEN,
924 ("not enough room, need %u got %zu\n", needed_space, MHLEN));
925 /*
926 * Setup new mbuf to have leading space to prepend the
927 * 802.11 header and any crypto header bits that are
928 * required (the latter are added when the driver calls
929 * back to ieee80211_crypto_encap to do crypto encapsulation).
930 */
931 /* NB: must be first 'cuz it clobbers m_data */
932 m_move_pkthdr(n, m);
933 n->m_len = 0; /* NB: m_gethdr does not set */
934 n->m_data += needed_space;
935 /*
936 * Pull up Ethernet header to create the expected layout.
937 * We could use m_pullup but that's overkill (i.e. we don't
938 * need the actual data) and it cannot fail so do it inline
939 * for speed.
940 */
941 /* NB: struct ether_header is known to be contiguous */
942 n->m_len += sizeof(struct ether_header);
943 m->m_len -= sizeof(struct ether_header);
944 m->m_data += sizeof(struct ether_header);
945 /*
946 * Replace the head of the chain.
947 */
948 n->m_next = m;
949 m = n;
950 }
951 return m;
952 #undef TO_BE_RECLAIMED
953 }
954
955 /*
956 * Return the transmit key to use in sending a unicast frame.
957 * If a unicast key is set we use that. When no unicast key is set
958 * we fall back to the default transmit key.
959 */
960 static __inline struct ieee80211_key *
961 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
962 struct ieee80211_node *ni)
963 {
964 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
965 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
966 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
967 return NULL;
968 return &vap->iv_nw_keys[vap->iv_def_txkey];
969 } else {
970 return &ni->ni_ucastkey;
971 }
972 }
973
974 /*
975 * Return the transmit key to use in sending a multicast frame.
976 * Multicast traffic always uses the group key which is installed as
977 * the default tx key.
978 */
979 static __inline struct ieee80211_key *
980 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
981 struct ieee80211_node *ni)
982 {
983 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
984 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
985 return NULL;
986 return &vap->iv_nw_keys[vap->iv_def_txkey];
987 }
988
989 /*
990 * Encapsulate an outbound data frame. The mbuf chain is updated.
991 * If an error is encountered NULL is returned. The caller is required
992 * to provide a node reference and pullup the ethernet header in the
993 * first mbuf.
994 *
995 * NB: Packet is assumed to be processed by ieee80211_classify which
996 * marked EAPOL frames w/ M_EAPOL.
997 */
998 struct mbuf *
999 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1000 struct mbuf *m)
1001 {
1002 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1003 struct ieee80211com *ic = ni->ni_ic;
1004 #ifdef IEEE80211_SUPPORT_MESH
1005 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1006 struct ieee80211_meshcntl_ae10 *mc;
1007 #endif
1008 struct ether_header eh;
1009 struct ieee80211_frame *wh;
1010 struct ieee80211_key *key;
1011 struct llc *llc;
1012 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1013 ieee80211_seq seqno;
1014 int meshhdrsize, meshae;
1015 uint8_t *qos;
1016
1017 /*
1018 * Copy existing Ethernet header to a safe place. The
1019 * rest of the code assumes it's ok to strip it when
1020 * reorganizing state for the final encapsulation.
1021 */
1022 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1023 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1024
1025 /*
1026 * Insure space for additional headers. First identify
1027 * transmit key to use in calculating any buffer adjustments
1028 * required. This is also used below to do privacy
1029 * encapsulation work. Then calculate the 802.11 header
1030 * size and any padding required by the driver.
1031 *
1032 * Note key may be NULL if we fall back to the default
1033 * transmit key and that is not set. In that case the
1034 * buffer may not be expanded as needed by the cipher
1035 * routines, but they will/should discard it.
1036 */
1037 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1038 if (vap->iv_opmode == IEEE80211_M_STA ||
1039 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1040 (vap->iv_opmode == IEEE80211_M_WDS &&
1041 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1042 key = ieee80211_crypto_getucastkey(vap, ni);
1043 else
1044 key = ieee80211_crypto_getmcastkey(vap, ni);
1045 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1046 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1047 eh.ether_dhost,
1048 "no default transmit key (%s) deftxkey %u",
1049 __func__, vap->iv_def_txkey);
1050 vap->iv_stats.is_tx_nodefkey++;
1051 goto bad;
1052 }
1053 } else
1054 key = NULL;
1055 /*
1056 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1057 * frames so suppress use. This may be an issue if other
1058 * ap's require all data frames to be QoS-encapsulated
1059 * once negotiated in which case we'll need to make this
1060 * configurable.
1061 */
1062 addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) &&
1063 (m->m_flags & M_EAPOL) == 0;
1064 if (addqos)
1065 hdrsize = sizeof(struct ieee80211_qosframe);
1066 else
1067 hdrsize = sizeof(struct ieee80211_frame);
1068 #ifdef IEEE80211_SUPPORT_MESH
1069 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1070 /*
1071 * Mesh data frames are encapsulated according to the
1072 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1073 * o Group Addressed data (aka multicast) originating
1074 * at the local sta are sent w/ 3-address format and
1075 * address extension mode 00
1076 * o Individually Addressed data (aka unicast) originating
1077 * at the local sta are sent w/ 4-address format and
1078 * address extension mode 00
1079 * o Group Addressed data forwarded from a non-mesh sta are
1080 * sent w/ 3-address format and address extension mode 01
1081 * o Individually Address data from another sta are sent
1082 * w/ 4-address format and address extension mode 10
1083 */
1084 is4addr = 0; /* NB: don't use, disable */
1085 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1086 hdrsize += IEEE80211_ADDR_LEN; /* unicast are 4-addr */
1087 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1088 /* XXX defines for AE modes */
1089 if (IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1090 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1091 meshae = 0;
1092 else
1093 meshae = 4; /* NB: pseudo */
1094 } else if (IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1095 meshae = 1;
1096 meshhdrsize += 1*IEEE80211_ADDR_LEN;
1097 } else {
1098 meshae = 2;
1099 meshhdrsize += 2*IEEE80211_ADDR_LEN;
1100 }
1101 } else {
1102 #endif
1103 /*
1104 * 4-address frames need to be generated for:
1105 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1106 * o packets sent through a vap marked for relaying
1107 * (e.g. a station operating with dynamic WDS)
1108 */
1109 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1110 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1111 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1112 if (is4addr)
1113 hdrsize += IEEE80211_ADDR_LEN;
1114 meshhdrsize = meshae = 0;
1115 #ifdef IEEE80211_SUPPORT_MESH
1116 }
1117 #endif
1118 /*
1119 * Honor driver DATAPAD requirement.
1120 */
1121 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1122 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1123 else
1124 hdrspace = hdrsize;
1125
1126 if (__predict_true((m->m_flags & M_FF) == 0)) {
1127 /*
1128 * Normal frame.
1129 */
1130 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1131 if (m == NULL) {
1132 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1133 goto bad;
1134 }
1135 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1136 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1137 llc = mtod(m, struct llc *);
1138 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1139 llc->llc_control = LLC_UI;
1140 llc->llc_snap.org_code[0] = 0;
1141 llc->llc_snap.org_code[1] = 0;
1142 llc->llc_snap.org_code[2] = 0;
1143 llc->llc_snap.ether_type = eh.ether_type;
1144 } else {
1145 #ifdef IEEE80211_SUPPORT_SUPERG
1146 /*
1147 * Aggregated frame.
1148 */
1149 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1150 if (m == NULL)
1151 #endif
1152 goto bad;
1153 }
1154 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1155
1156 M_PREPEND(m, hdrspace + meshhdrsize, M_DONTWAIT);
1157 if (m == NULL) {
1158 vap->iv_stats.is_tx_nobuf++;
1159 goto bad;
1160 }
1161 wh = mtod(m, struct ieee80211_frame *);
1162 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1163 *(uint16_t *)wh->i_dur = 0;
1164 qos = NULL; /* NB: quiet compiler */
1165 if (is4addr) {
1166 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1167 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1168 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1169 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1170 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1171 } else switch (vap->iv_opmode) {
1172 case IEEE80211_M_STA:
1173 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1174 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1175 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1176 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1177 break;
1178 case IEEE80211_M_IBSS:
1179 case IEEE80211_M_AHDEMO:
1180 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1181 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1182 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1183 /*
1184 * NB: always use the bssid from iv_bss as the
1185 * neighbor's may be stale after an ibss merge
1186 */
1187 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1188 break;
1189 case IEEE80211_M_HOSTAP:
1190 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1191 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1192 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1193 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1194 break;
1195 #ifdef IEEE80211_SUPPORT_MESH
1196 case IEEE80211_M_MBSS:
1197 /* NB: offset by hdrspace to deal with DATAPAD */
1198 mc = (struct ieee80211_meshcntl_ae10 *)
1199 (mtod(m, uint8_t *) + hdrspace);
1200 switch (meshae) {
1201 case 0: /* ucast, no proxy */
1202 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1203 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1204 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1205 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1206 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1207 mc->mc_flags = 0;
1208 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1209 break;
1210 case 4: /* mcast, no proxy */
1211 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1212 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1213 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1214 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1215 mc->mc_flags = 0; /* NB: AE is really 0 */
1216 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1217 break;
1218 case 1: /* mcast, proxy */
1219 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1220 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1221 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1222 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1223 mc->mc_flags = 1;
1224 IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_shost);
1225 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1226 break;
1227 case 2: /* ucast, proxy */
1228 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1229 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1230 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1231 /* XXX not right, need MeshDA */
1232 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1233 /* XXX assume are MeshSA */
1234 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1235 mc->mc_flags = 2;
1236 IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_dhost);
1237 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_shost);
1238 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1239 break;
1240 default:
1241 KASSERT(0, ("meshae %d", meshae));
1242 break;
1243 }
1244 mc->mc_ttl = ms->ms_ttl;
1245 ms->ms_seq++;
1246 LE_WRITE_4(mc->mc_seq, ms->ms_seq);
1247 break;
1248 #endif
1249 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1250 default:
1251 goto bad;
1252 }
1253 if (m->m_flags & M_MORE_DATA)
1254 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1255 if (addqos) {
1256 int ac, tid;
1257
1258 if (is4addr) {
1259 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1260 /* NB: mesh case handled earlier */
1261 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1262 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1263 ac = M_WME_GETAC(m);
1264 /* map from access class/queue to 11e header priorty value */
1265 tid = WME_AC_TO_TID(ac);
1266 qos[0] = tid & IEEE80211_QOS_TID;
1267 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1268 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1269 qos[1] = 0;
1270 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1271
1272 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1273 /*
1274 * NB: don't assign a sequence # to potential
1275 * aggregates; we expect this happens at the
1276 * point the frame comes off any aggregation q
1277 * as otherwise we may introduce holes in the
1278 * BA sequence space and/or make window accouting
1279 * more difficult.
1280 *
1281 * XXX may want to control this with a driver
1282 * capability; this may also change when we pull
1283 * aggregation up into net80211
1284 */
1285 seqno = ni->ni_txseqs[tid]++;
1286 *(uint16_t *)wh->i_seq =
1287 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1288 M_SEQNO_SET(m, seqno);
1289 }
1290 } else {
1291 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1292 *(uint16_t *)wh->i_seq =
1293 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1294 M_SEQNO_SET(m, seqno);
1295 }
1296
1297
1298 /* check if xmit fragmentation is required */
1299 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1300 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1301 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1302 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1303 if (key != NULL) {
1304 /*
1305 * IEEE 802.1X: send EAPOL frames always in the clear.
1306 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1307 */
1308 if ((m->m_flags & M_EAPOL) == 0 ||
1309 ((vap->iv_flags & IEEE80211_F_WPA) &&
1310 (vap->iv_opmode == IEEE80211_M_STA ?
1311 !IEEE80211_KEY_UNDEFINED(key) :
1312 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1313 wh->i_fc[1] |= IEEE80211_FC1_WEP;
1314 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1315 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1316 eh.ether_dhost,
1317 "%s", "enmic failed, discard frame");
1318 vap->iv_stats.is_crypto_enmicfail++;
1319 goto bad;
1320 }
1321 }
1322 }
1323 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1324 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1325 goto bad;
1326
1327 m->m_flags |= M_ENCAP; /* mark encapsulated */
1328
1329 IEEE80211_NODE_STAT(ni, tx_data);
1330 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1331 IEEE80211_NODE_STAT(ni, tx_mcast);
1332 m->m_flags |= M_MCAST;
1333 } else
1334 IEEE80211_NODE_STAT(ni, tx_ucast);
1335 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1336
1337 return m;
1338 bad:
1339 if (m != NULL)
1340 m_freem(m);
1341 return NULL;
1342 #undef WH4
1343 }
1344
1345 /*
1346 * Fragment the frame according to the specified mtu.
1347 * The size of the 802.11 header (w/o padding) is provided
1348 * so we don't need to recalculate it. We create a new
1349 * mbuf for each fragment and chain it through m_nextpkt;
1350 * we might be able to optimize this by reusing the original
1351 * packet's mbufs but that is significantly more complicated.
1352 */
1353 static int
1354 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1355 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1356 {
1357 struct ieee80211_frame *wh, *whf;
1358 struct mbuf *m, *prev, *next;
1359 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1360
1361 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1362 KASSERT(m0->m_pkthdr.len > mtu,
1363 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1364
1365 wh = mtod(m0, struct ieee80211_frame *);
1366 /* NB: mark the first frag; it will be propagated below */
1367 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1368 totalhdrsize = hdrsize + ciphdrsize;
1369 fragno = 1;
1370 off = mtu - ciphdrsize;
1371 remainder = m0->m_pkthdr.len - off;
1372 prev = m0;
1373 do {
1374 fragsize = totalhdrsize + remainder;
1375 if (fragsize > mtu)
1376 fragsize = mtu;
1377 /* XXX fragsize can be >2048! */
1378 KASSERT(fragsize < MCLBYTES,
1379 ("fragment size %u too big!", fragsize));
1380 if (fragsize > MHLEN)
1381 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1382 else
1383 m = m_gethdr(M_DONTWAIT, MT_DATA);
1384 if (m == NULL)
1385 goto bad;
1386 /* leave room to prepend any cipher header */
1387 m_align(m, fragsize - ciphdrsize);
1388
1389 /*
1390 * Form the header in the fragment. Note that since
1391 * we mark the first fragment with the MORE_FRAG bit
1392 * it automatically is propagated to each fragment; we
1393 * need only clear it on the last fragment (done below).
1394 */
1395 whf = mtod(m, struct ieee80211_frame *);
1396 memcpy(whf, wh, hdrsize);
1397 *(uint16_t *)&whf->i_seq[0] |= htole16(
1398 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1399 IEEE80211_SEQ_FRAG_SHIFT);
1400 fragno++;
1401
1402 payload = fragsize - totalhdrsize;
1403 /* NB: destination is known to be contiguous */
1404 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
1405 m->m_len = hdrsize + payload;
1406 m->m_pkthdr.len = hdrsize + payload;
1407 m->m_flags |= M_FRAG;
1408
1409 /* chain up the fragment */
1410 prev->m_nextpkt = m;
1411 prev = m;
1412
1413 /* deduct fragment just formed */
1414 remainder -= payload;
1415 off += payload;
1416 } while (remainder != 0);
1417
1418 /* set the last fragment */
1419 m->m_flags |= M_LASTFRAG;
1420 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1421
1422 /* strip first mbuf now that everything has been copied */
1423 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1424 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1425
1426 vap->iv_stats.is_tx_fragframes++;
1427 vap->iv_stats.is_tx_frags += fragno-1;
1428
1429 return 1;
1430 bad:
1431 /* reclaim fragments but leave original frame for caller to free */
1432 for (m = m0->m_nextpkt; m != NULL; m = next) {
1433 next = m->m_nextpkt;
1434 m->m_nextpkt = NULL; /* XXX paranoid */
1435 m_freem(m);
1436 }
1437 m0->m_nextpkt = NULL;
1438 return 0;
1439 }
1440
1441 /*
1442 * Add a supported rates element id to a frame.
1443 */
1444 uint8_t *
1445 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1446 {
1447 int nrates;
1448
1449 *frm++ = IEEE80211_ELEMID_RATES;
1450 nrates = rs->rs_nrates;
1451 if (nrates > IEEE80211_RATE_SIZE)
1452 nrates = IEEE80211_RATE_SIZE;
1453 *frm++ = nrates;
1454 memcpy(frm, rs->rs_rates, nrates);
1455 return frm + nrates;
1456 }
1457
1458 /*
1459 * Add an extended supported rates element id to a frame.
1460 */
1461 uint8_t *
1462 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1463 {
1464 /*
1465 * Add an extended supported rates element if operating in 11g mode.
1466 */
1467 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1468 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1469 *frm++ = IEEE80211_ELEMID_XRATES;
1470 *frm++ = nrates;
1471 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1472 frm += nrates;
1473 }
1474 return frm;
1475 }
1476
1477 /*
1478 * Add an ssid element to a frame.
1479 */
1480 static uint8_t *
1481 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1482 {
1483 *frm++ = IEEE80211_ELEMID_SSID;
1484 *frm++ = len;
1485 memcpy(frm, ssid, len);
1486 return frm + len;
1487 }
1488
1489 /*
1490 * Add an erp element to a frame.
1491 */
1492 static uint8_t *
1493 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1494 {
1495 uint8_t erp;
1496
1497 *frm++ = IEEE80211_ELEMID_ERP;
1498 *frm++ = 1;
1499 erp = 0;
1500 if (ic->ic_nonerpsta != 0)
1501 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1502 if (ic->ic_flags & IEEE80211_F_USEPROT)
1503 erp |= IEEE80211_ERP_USE_PROTECTION;
1504 if (ic->ic_flags & IEEE80211_F_USEBARKER)
1505 erp |= IEEE80211_ERP_LONG_PREAMBLE;
1506 *frm++ = erp;
1507 return frm;
1508 }
1509
1510 /*
1511 * Add a CFParams element to a frame.
1512 */
1513 static uint8_t *
1514 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1515 {
1516 #define ADDSHORT(frm, v) do { \
1517 LE_WRITE_2(frm, v); \
1518 frm += 2; \
1519 } while (0)
1520 *frm++ = IEEE80211_ELEMID_CFPARMS;
1521 *frm++ = 6;
1522 *frm++ = 0; /* CFP count */
1523 *frm++ = 2; /* CFP period */
1524 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
1525 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
1526 return frm;
1527 #undef ADDSHORT
1528 }
1529
1530 static __inline uint8_t *
1531 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1532 {
1533 memcpy(frm, ie->ie_data, ie->ie_len);
1534 return frm + ie->ie_len;
1535 }
1536
1537 static __inline uint8_t *
1538 add_ie(uint8_t *frm, const uint8_t *ie)
1539 {
1540 memcpy(frm, ie, 2 + ie[1]);
1541 return frm + 2 + ie[1];
1542 }
1543
1544 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1545 /*
1546 * Add a WME information element to a frame.
1547 */
1548 static uint8_t *
1549 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1550 {
1551 static const struct ieee80211_wme_info info = {
1552 .wme_id = IEEE80211_ELEMID_VENDOR,
1553 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1554 .wme_oui = { WME_OUI_BYTES },
1555 .wme_type = WME_OUI_TYPE,
1556 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1557 .wme_version = WME_VERSION,
1558 .wme_info = 0,
1559 };
1560 memcpy(frm, &info, sizeof(info));
1561 return frm + sizeof(info);
1562 }
1563
1564 /*
1565 * Add a WME parameters element to a frame.
1566 */
1567 static uint8_t *
1568 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1569 {
1570 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1571 #define ADDSHORT(frm, v) do { \
1572 LE_WRITE_2(frm, v); \
1573 frm += 2; \
1574 } while (0)
1575 /* NB: this works 'cuz a param has an info at the front */
1576 static const struct ieee80211_wme_info param = {
1577 .wme_id = IEEE80211_ELEMID_VENDOR,
1578 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1579 .wme_oui = { WME_OUI_BYTES },
1580 .wme_type = WME_OUI_TYPE,
1581 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1582 .wme_version = WME_VERSION,
1583 };
1584 int i;
1585
1586 memcpy(frm, ¶m, sizeof(param));
1587 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1588 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1589 *frm++ = 0; /* reserved field */
1590 for (i = 0; i < WME_NUM_AC; i++) {
1591 const struct wmeParams *ac =
1592 &wme->wme_bssChanParams.cap_wmeParams[i];
1593 *frm++ = SM(i, WME_PARAM_ACI)
1594 | SM(ac->wmep_acm, WME_PARAM_ACM)
1595 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1596 ;
1597 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1598 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1599 ;
1600 ADDSHORT(frm, ac->wmep_txopLimit);
1601 }
1602 return frm;
1603 #undef SM
1604 #undef ADDSHORT
1605 }
1606 #undef WME_OUI_BYTES
1607
1608 /*
1609 * Add an 11h Power Constraint element to a frame.
1610 */
1611 static uint8_t *
1612 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1613 {
1614 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1615 /* XXX per-vap tx power limit? */
1616 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1617
1618 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1619 frm[1] = 1;
1620 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
1621 return frm + 3;
1622 }
1623
1624 /*
1625 * Add an 11h Power Capability element to a frame.
1626 */
1627 static uint8_t *
1628 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1629 {
1630 frm[0] = IEEE80211_ELEMID_PWRCAP;
1631 frm[1] = 2;
1632 frm[2] = c->ic_minpower;
1633 frm[3] = c->ic_maxpower;
1634 return frm + 4;
1635 }
1636
1637 /*
1638 * Add an 11h Supported Channels element to a frame.
1639 */
1640 static uint8_t *
1641 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1642 {
1643 static const int ielen = 26;
1644
1645 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1646 frm[1] = ielen;
1647 /* XXX not correct */
1648 memcpy(frm+2, ic->ic_chan_avail, ielen);
1649 return frm + 2 + ielen;
1650 }
1651
1652 /*
1653 * Add an 11h Channel Switch Announcement element to a frame.
1654 * Note that we use the per-vap CSA count to adjust the global
1655 * counter so we can use this routine to form probe response
1656 * frames and get the current count.
1657 */
1658 static uint8_t *
1659 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1660 {
1661 struct ieee80211com *ic = vap->iv_ic;
1662 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1663
1664 csa->csa_ie = IEEE80211_ELEMID_CSA;
1665 csa->csa_len = 3;
1666 csa->csa_mode = 1; /* XXX force quiet on channel */
1667 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1668 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1669 return frm + sizeof(*csa);
1670 }
1671
1672 /*
1673 * Add an 11h country information element to a frame.
1674 */
1675 static uint8_t *
1676 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1677 {
1678
1679 if (ic->ic_countryie == NULL ||
1680 ic->ic_countryie_chan != ic->ic_bsschan) {
1681 /*
1682 * Handle lazy construction of ie. This is done on
1683 * first use and after a channel change that requires
1684 * re-calculation.
1685 */
1686 if (ic->ic_countryie != NULL)
1687 free(ic->ic_countryie, M_80211_NODE_IE);
1688 ic->ic_countryie = ieee80211_alloc_countryie(ic);
1689 if (ic->ic_countryie == NULL)
1690 return frm;
1691 ic->ic_countryie_chan = ic->ic_bsschan;
1692 }
1693 return add_appie(frm, ic->ic_countryie);
1694 }
1695
1696 /*
1697 * Send a probe request frame with the specified ssid
1698 * and any optional information element data.
1699 */
1700 int
1701 ieee80211_send_probereq(struct ieee80211_node *ni,
1702 const uint8_t sa[IEEE80211_ADDR_LEN],
1703 const uint8_t da[IEEE80211_ADDR_LEN],
1704 const uint8_t bssid[IEEE80211_ADDR_LEN],
1705 const uint8_t *ssid, size_t ssidlen)
1706 {
1707 struct ieee80211vap *vap = ni->ni_vap;
1708 struct ieee80211com *ic = ni->ni_ic;
1709 const struct ieee80211_txparam *tp;
1710 struct ieee80211_bpf_params params;
1711 struct ieee80211_frame *wh;
1712 const struct ieee80211_rateset *rs;
1713 struct mbuf *m;
1714 uint8_t *frm;
1715
1716 if (vap->iv_state == IEEE80211_S_CAC) {
1717 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1718 "block %s frame in CAC state", "probe request");
1719 vap->iv_stats.is_tx_badstate++;
1720 return EIO; /* XXX */
1721 }
1722
1723 /*
1724 * Hold a reference on the node so it doesn't go away until after
1725 * the xmit is complete all the way in the driver. On error we
1726 * will remove our reference.
1727 */
1728 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1729 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1730 __func__, __LINE__,
1731 ni, ether_sprintf(ni->ni_macaddr),
1732 ieee80211_node_refcnt(ni)+1);
1733 ieee80211_ref_node(ni);
1734
1735 /*
1736 * prreq frame format
1737 * [tlv] ssid
1738 * [tlv] supported rates
1739 * [tlv] RSN (optional)
1740 * [tlv] extended supported rates
1741 * [tlv] WPA (optional)
1742 * [tlv] user-specified ie's
1743 */
1744 m = ieee80211_getmgtframe(&frm,
1745 ic->ic_headroom + sizeof(struct ieee80211_frame),
1746 2 + IEEE80211_NWID_LEN
1747 + 2 + IEEE80211_RATE_SIZE
1748 + sizeof(struct ieee80211_ie_wpa)
1749 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1750 + sizeof(struct ieee80211_ie_wpa)
1751 + (vap->iv_appie_probereq != NULL ?
1752 vap->iv_appie_probereq->ie_len : 0)
1753 );
1754 if (m == NULL) {
1755 vap->iv_stats.is_tx_nobuf++;
1756 ieee80211_free_node(ni);
1757 return ENOMEM;
1758 }
1759
1760 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1761 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1762 frm = ieee80211_add_rates(frm, rs);
1763 if (vap->iv_flags & IEEE80211_F_WPA2) {
1764 if (vap->iv_rsn_ie != NULL)
1765 frm = add_ie(frm, vap->iv_rsn_ie);
1766 /* XXX else complain? */
1767 }
1768 frm = ieee80211_add_xrates(frm, rs);
1769 if (vap->iv_flags & IEEE80211_F_WPA1) {
1770 if (vap->iv_wpa_ie != NULL)
1771 frm = add_ie(frm, vap->iv_wpa_ie);
1772 /* XXX else complain? */
1773 }
1774 if (vap->iv_appie_probereq != NULL)
1775 frm = add_appie(frm, vap->iv_appie_probereq);
1776 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1777
1778 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
1779 ("leading space %zd", M_LEADINGSPACE(m)));
1780 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1781 if (m == NULL) {
1782 /* NB: cannot happen */
1783 ieee80211_free_node(ni);
1784 return ENOMEM;
1785 }
1786
1787 wh = mtod(m, struct ieee80211_frame *);
1788 ieee80211_send_setup(ni, m,
1789 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1790 IEEE80211_NONQOS_TID, sa, da, bssid);
1791 /* XXX power management? */
1792 m->m_flags |= M_ENCAP; /* mark encapsulated */
1793
1794 M_WME_SETAC(m, WME_AC_BE);
1795
1796 IEEE80211_NODE_STAT(ni, tx_probereq);
1797 IEEE80211_NODE_STAT(ni, tx_mgmt);
1798
1799 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1800 "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
1801 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
1802 ssidlen, ssid);
1803
1804 memset(¶ms, 0, sizeof(params));
1805 params.ibp_pri = M_WME_GETAC(m);
1806 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1807 params.ibp_rate0 = tp->mgmtrate;
1808 if (IEEE80211_IS_MULTICAST(da)) {
1809 params.ibp_flags |= IEEE80211_BPF_NOACK;
1810 params.ibp_try0 = 1;
1811 } else
1812 params.ibp_try0 = tp->maxretry;
1813 params.ibp_power = ni->ni_txpower;
1814 return ic->ic_raw_xmit(ni, m, ¶ms);
1815 }
1816
1817 /*
1818 * Calculate capability information for mgt frames.
1819 */
1820 uint16_t
1821 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
1822 {
1823 struct ieee80211com *ic = vap->iv_ic;
1824 uint16_t capinfo;
1825
1826 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
1827
1828 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1829 capinfo = IEEE80211_CAPINFO_ESS;
1830 else if (vap->iv_opmode == IEEE80211_M_IBSS)
1831 capinfo = IEEE80211_CAPINFO_IBSS;
1832 else
1833 capinfo = 0;
1834 if (vap->iv_flags & IEEE80211_F_PRIVACY)
1835 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1836 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1837 IEEE80211_IS_CHAN_2GHZ(chan))
1838 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1839 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1840 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1841 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
1842 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
1843 return capinfo;
1844 }
1845
1846 /*
1847 * Send a management frame. The node is for the destination (or ic_bss
1848 * when in station mode). Nodes other than ic_bss have their reference
1849 * count bumped to reflect our use for an indeterminant time.
1850 */
1851 int
1852 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
1853 {
1854 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
1855 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1856 struct ieee80211vap *vap = ni->ni_vap;
1857 struct ieee80211com *ic = ni->ni_ic;
1858 struct ieee80211_node *bss = vap->iv_bss;
1859 struct ieee80211_bpf_params params;
1860 struct mbuf *m;
1861 uint8_t *frm;
1862 uint16_t capinfo;
1863 int has_challenge, is_shared_key, ret, status;
1864
1865 KASSERT(ni != NULL, ("null node"));
1866
1867 /*
1868 * Hold a reference on the node so it doesn't go away until after
1869 * the xmit is complete all the way in the driver. On error we
1870 * will remove our reference.
1871 */
1872 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1873 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1874 __func__, __LINE__,
1875 ni, ether_sprintf(ni->ni_macaddr),
1876 ieee80211_node_refcnt(ni)+1);
1877 ieee80211_ref_node(ni);
1878
1879 memset(¶ms, 0, sizeof(params));
1880 switch (type) {
1881
1882 case IEEE80211_FC0_SUBTYPE_AUTH:
1883 status = arg >> 16;
1884 arg &= 0xffff;
1885 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1886 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1887 ni->ni_challenge != NULL);
1888
1889 /*
1890 * Deduce whether we're doing open authentication or
1891 * shared key authentication. We do the latter if
1892 * we're in the middle of a shared key authentication
1893 * handshake or if we're initiating an authentication
1894 * request and configured to use shared key.
1895 */
1896 is_shared_key = has_challenge ||
1897 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1898 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1899 bss->ni_authmode == IEEE80211_AUTH_SHARED);
1900
1901 m = ieee80211_getmgtframe(&frm,
1902 ic->ic_headroom + sizeof(struct ieee80211_frame),
1903 3 * sizeof(uint16_t)
1904 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1905 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
1906 );
1907 if (m == NULL)
1908 senderr(ENOMEM, is_tx_nobuf);
1909
1910 ((uint16_t *)frm)[0] =
1911 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1912 : htole16(IEEE80211_AUTH_ALG_OPEN);
1913 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
1914 ((uint16_t *)frm)[2] = htole16(status);/* status */
1915
1916 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1917 ((uint16_t *)frm)[3] =
1918 htole16((IEEE80211_CHALLENGE_LEN << 8) |
1919 IEEE80211_ELEMID_CHALLENGE);
1920 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
1921 IEEE80211_CHALLENGE_LEN);
1922 m->m_pkthdr.len = m->m_len =
1923 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
1924 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1925 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1926 "request encrypt frame (%s)", __func__);
1927 /* mark frame for encryption */
1928 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
1929 }
1930 } else
1931 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
1932
1933 /* XXX not right for shared key */
1934 if (status == IEEE80211_STATUS_SUCCESS)
1935 IEEE80211_NODE_STAT(ni, tx_auth);
1936 else
1937 IEEE80211_NODE_STAT(ni, tx_auth_fail);
1938
1939 if (vap->iv_opmode == IEEE80211_M_STA)
1940 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1941 (void *) vap->iv_state);
1942 break;
1943
1944 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1945 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1946 "send station deauthenticate (reason %d)", arg);
1947 m = ieee80211_getmgtframe(&frm,
1948 ic->ic_headroom + sizeof(struct ieee80211_frame),
1949 sizeof(uint16_t));
1950 if (m == NULL)
1951 senderr(ENOMEM, is_tx_nobuf);
1952 *(uint16_t *)frm = htole16(arg); /* reason */
1953 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
1954
1955 IEEE80211_NODE_STAT(ni, tx_deauth);
1956 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1957
1958 ieee80211_node_unauthorize(ni); /* port closed */
1959 break;
1960
1961 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1962 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1963 /*
1964 * asreq frame format
1965 * [2] capability information
1966 * [2] listen interval
1967 * [6*] current AP address (reassoc only)
1968 * [tlv] ssid
1969 * [tlv] supported rates
1970 * [tlv] extended supported rates
1971 * [4] power capability (optional)
1972 * [28] supported channels (optional)
1973 * [tlv] HT capabilities
1974 * [tlv] WME (optional)
1975 * [tlv] Vendor OUI HT capabilities (optional)
1976 * [tlv] Atheros capabilities (if negotiated)
1977 * [tlv] AppIE's (optional)
1978 */
1979 m = ieee80211_getmgtframe(&frm,
1980 ic->ic_headroom + sizeof(struct ieee80211_frame),
1981 sizeof(uint16_t)
1982 + sizeof(uint16_t)
1983 + IEEE80211_ADDR_LEN
1984 + 2 + IEEE80211_NWID_LEN
1985 + 2 + IEEE80211_RATE_SIZE
1986 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1987 + 4
1988 + 2 + 26
1989 + sizeof(struct ieee80211_wme_info)
1990 + sizeof(struct ieee80211_ie_htcap)
1991 + 4 + sizeof(struct ieee80211_ie_htcap)
1992 #ifdef IEEE80211_SUPPORT_SUPERG
1993 + sizeof(struct ieee80211_ath_ie)
1994 #endif
1995 + (vap->iv_appie_wpa != NULL ?
1996 vap->iv_appie_wpa->ie_len : 0)
1997 + (vap->iv_appie_assocreq != NULL ?
1998 vap->iv_appie_assocreq->ie_len : 0)
1999 );
2000 if (m == NULL)
2001 senderr(ENOMEM, is_tx_nobuf);
2002
2003 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2004 ("wrong mode %u", vap->iv_opmode));
2005 capinfo = IEEE80211_CAPINFO_ESS;
2006 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2007 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2008 /*
2009 * NB: Some 11a AP's reject the request when
2010 * short premable is set.
2011 */
2012 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2013 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2014 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2015 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2016 (ic->ic_caps & IEEE80211_C_SHSLOT))
2017 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2018 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2019 (vap->iv_flags & IEEE80211_F_DOTH))
2020 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2021 *(uint16_t *)frm = htole16(capinfo);
2022 frm += 2;
2023
2024 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2025 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2026 bss->ni_intval));
2027 frm += 2;
2028
2029 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2030 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2031 frm += IEEE80211_ADDR_LEN;
2032 }
2033
2034 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2035 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2036 if (vap->iv_flags & IEEE80211_F_WPA2) {
2037 if (vap->iv_rsn_ie != NULL)
2038 frm = add_ie(frm, vap->iv_rsn_ie);
2039 /* XXX else complain? */
2040 }
2041 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2042 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2043 frm = ieee80211_add_powercapability(frm,
2044 ic->ic_curchan);
2045 frm = ieee80211_add_supportedchannels(frm, ic);
2046 }
2047 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2048 ni->ni_ies.htcap_ie != NULL &&
2049 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
2050 frm = ieee80211_add_htcap(frm, ni);
2051 if (vap->iv_flags & IEEE80211_F_WPA1) {
2052 if (vap->iv_wpa_ie != NULL)
2053 frm = add_ie(frm, vap->iv_wpa_ie);
2054 /* XXX else complain */
2055 }
2056 if ((ic->ic_flags & IEEE80211_F_WME) &&
2057 ni->ni_ies.wme_ie != NULL)
2058 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2059 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2060 ni->ni_ies.htcap_ie != NULL &&
2061 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
2062 frm = ieee80211_add_htcap_vendor(frm, ni);
2063 #ifdef IEEE80211_SUPPORT_SUPERG
2064 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2065 frm = ieee80211_add_ath(frm,
2066 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2067 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2068 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2069 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2070 }
2071 #endif /* IEEE80211_SUPPORT_SUPERG */
2072 if (vap->iv_appie_assocreq != NULL)
2073 frm = add_appie(frm, vap->iv_appie_assocreq);
2074 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2075
2076 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2077 (void *) vap->iv_state);
2078 break;
2079
2080 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2081 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2082 /*
2083 * asresp frame format
2084 * [2] capability information
2085 * [2] status
2086 * [2] association ID
2087 * [tlv] supported rates
2088 * [tlv] extended supported rates
2089 * [tlv] HT capabilities (standard, if STA enabled)
2090 * [tlv] HT information (standard, if STA enabled)
2091 * [tlv] WME (if configured and STA enabled)
2092 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2093 * [tlv] HT information (vendor OUI, if STA enabled)
2094 * [tlv] Atheros capabilities (if STA enabled)
2095 * [tlv] AppIE's (optional)
2096 */
2097 m = ieee80211_getmgtframe(&frm,
2098 ic->ic_headroom + sizeof(struct ieee80211_frame),
2099 sizeof(uint16_t)
2100 + sizeof(uint16_t)
2101 + sizeof(uint16_t)
2102 + 2 + IEEE80211_RATE_SIZE
2103 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2104 + sizeof(struct ieee80211_ie_htcap) + 4
2105 + sizeof(struct ieee80211_ie_htinfo) + 4
2106 + sizeof(struct ieee80211_wme_param)
2107 #ifdef IEEE80211_SUPPORT_SUPERG
2108 + sizeof(struct ieee80211_ath_ie)
2109 #endif
2110 + (vap->iv_appie_assocresp != NULL ?
2111 vap->iv_appie_assocresp->ie_len : 0)
2112 );
2113 if (m == NULL)
2114 senderr(ENOMEM, is_tx_nobuf);
2115
2116 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2117 *(uint16_t *)frm = htole16(capinfo);
2118 frm += 2;
2119
2120 *(uint16_t *)frm = htole16(arg); /* status */
2121 frm += 2;
2122
2123 if (arg == IEEE80211_STATUS_SUCCESS) {
2124 *(uint16_t *)frm = htole16(ni->ni_associd);
2125 IEEE80211_NODE_STAT(ni, tx_assoc);
2126 } else
2127 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2128 frm += 2;
2129
2130 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2131 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2132 /* NB: respond according to what we received */
2133 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2134 frm = ieee80211_add_htcap(frm, ni);
2135 frm = ieee80211_add_htinfo(frm, ni);
2136 }
2137 if ((vap->iv_flags & IEEE80211_F_WME) &&
2138 ni->ni_ies.wme_ie != NULL)
2139 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2140 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2141 frm = ieee80211_add_htcap_vendor(frm, ni);
2142 frm = ieee80211_add_htinfo_vendor(frm, ni);
2143 }
2144 #ifdef IEEE80211_SUPPORT_SUPERG
2145 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2146 frm = ieee80211_add_ath(frm,
2147 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2148 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2149 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2150 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2151 #endif /* IEEE80211_SUPPORT_SUPERG */
2152 if (vap->iv_appie_assocresp != NULL)
2153 frm = add_appie(frm, vap->iv_appie_assocresp);
2154 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2155 break;
2156
2157 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2158 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2159 "send station disassociate (reason %d)", arg);
2160 m = ieee80211_getmgtframe(&frm,
2161 ic->ic_headroom + sizeof(struct ieee80211_frame),
2162 sizeof(uint16_t));
2163 if (m == NULL)
2164 senderr(ENOMEM, is_tx_nobuf);
2165 *(uint16_t *)frm = htole16(arg); /* reason */
2166 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2167
2168 IEEE80211_NODE_STAT(ni, tx_disassoc);
2169 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2170 break;
2171
2172 default:
2173 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2174 "invalid mgmt frame type %u", type);
2175 senderr(EINVAL, is_tx_unknownmgt);
2176 /* NOTREACHED */
2177 }
2178
2179 /* NB: force non-ProbeResp frames to the highest queue */
2180 params.ibp_pri = WME_AC_VO;
2181 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2182 /* NB: we know all frames are unicast */
2183 params.ibp_try0 = bss->ni_txparms->maxretry;
2184 params.ibp_power = bss->ni_txpower;
2185 return ieee80211_mgmt_output(ni, m, type, ¶ms);
2186 bad:
2187 ieee80211_free_node(ni);
2188 return ret;
2189 #undef senderr
2190 #undef HTFLAGS
2191 }
2192
2193 /*
2194 * Return an mbuf with a probe response frame in it.
2195 * Space is left to prepend and 802.11 header at the
2196 * front but it's left to the caller to fill in.
2197 */
2198 struct mbuf *
2199 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2200 {
2201 struct ieee80211vap *vap = bss->ni_vap;
2202 struct ieee80211com *ic = bss->ni_ic;
2203 const struct ieee80211_rateset *rs;
2204 struct mbuf *m;
2205 uint16_t capinfo;
2206 uint8_t *frm;
2207
2208 /*
2209 * probe response frame format
2210 * [8] time stamp
2211 * [2] beacon interval
2212 * [2] cabability information
2213 * [tlv] ssid
2214 * [tlv] supported rates
2215 * [tlv] parameter set (FH/DS)
2216 * [tlv] parameter set (IBSS)
2217 * [tlv] country (optional)
2218 * [3] power control (optional)
2219 * [5] channel switch announcement (CSA) (optional)
2220 * [tlv] extended rate phy (ERP)
2221 * [tlv] extended supported rates
2222 * [tlv] RSN (optional)
2223 * [tlv] HT capabilities
2224 * [tlv] HT information
2225 * [tlv] WPA (optional)
2226 * [tlv] WME (optional)
2227 * [tlv] Vendor OUI HT capabilities (optional)
2228 * [tlv] Vendor OUI HT information (optional)
2229 * [tlv] Atheros capabilities
2230 * [tlv] AppIE's (optional)
2231 * [tlv] Mesh ID (MBSS)
2232 * [tlv] Mesh Conf (MBSS)
2233 */
2234 m = ieee80211_getmgtframe(&frm,
2235 ic->ic_headroom + sizeof(struct ieee80211_frame),
2236 8
2237 + sizeof(uint16_t)
2238 + sizeof(uint16_t)
2239 + 2 + IEEE80211_NWID_LEN
2240 + 2 + IEEE80211_RATE_SIZE
2241 + 7 /* max(7,3) */
2242 + IEEE80211_COUNTRY_MAX_SIZE
2243 + 3
2244 + sizeof(struct ieee80211_csa_ie)
2245 + 3
2246 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2247 + sizeof(struct ieee80211_ie_wpa)
2248 + sizeof(struct ieee80211_ie_htcap)
2249 + sizeof(struct ieee80211_ie_htinfo)
2250 + sizeof(struct ieee80211_ie_wpa)
2251 + sizeof(struct ieee80211_wme_param)
2252 + 4 + sizeof(struct ieee80211_ie_htcap)
2253 + 4 + sizeof(struct ieee80211_ie_htinfo)
2254 #ifdef IEEE80211_SUPPORT_SUPERG
2255 + sizeof(struct ieee80211_ath_ie)
2256 #endif
2257 #ifdef IEEE80211_SUPPORT_MESH
2258 + 2 + IEEE80211_MESHID_LEN
2259 + sizeof(struct ieee80211_meshconf_ie)
2260 #endif
2261 + (vap->iv_appie_proberesp != NULL ?
2262 vap->iv_appie_proberesp->ie_len : 0)
2263 );
2264 if (m == NULL) {
2265 vap->iv_stats.is_tx_nobuf++;
2266 return NULL;
2267 }
2268
2269 memset(frm, 0, 8); /* timestamp should be filled later */
2270 frm += 8;
2271 *(uint16_t *)frm = htole16(bss->ni_intval);
2272 frm += 2;
2273 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2274 *(uint16_t *)frm = htole16(capinfo);
2275 frm += 2;
2276
2277 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2278 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2279 frm = ieee80211_add_rates(frm, rs);
2280
2281 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2282 *frm++ = IEEE80211_ELEMID_FHPARMS;
2283 *frm++ = 5;
2284 *frm++ = bss->ni_fhdwell & 0x00ff;
2285 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2286 *frm++ = IEEE80211_FH_CHANSET(
2287 ieee80211_chan2ieee(ic, bss->ni_chan));
2288 *frm++ = IEEE80211_FH_CHANPAT(
2289 ieee80211_chan2ieee(ic, bss->ni_chan));
2290 *frm++ = bss->ni_fhindex;
2291 } else {
2292 *frm++ = IEEE80211_ELEMID_DSPARMS;
2293 *frm++ = 1;
2294 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2295 }
2296
2297 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2298 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2299 *frm++ = 2;
2300 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2301 }
2302 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2303 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2304 frm = ieee80211_add_countryie(frm, ic);
2305 if (vap->iv_flags & IEEE80211_F_DOTH) {
2306 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2307 frm = ieee80211_add_powerconstraint(frm, vap);
2308 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2309 frm = ieee80211_add_csa(frm, vap);
2310 }
2311 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2312 frm = ieee80211_add_erp(frm, ic);
2313 frm = ieee80211_add_xrates(frm, rs);
2314 if (vap->iv_flags & IEEE80211_F_WPA2) {
2315 if (vap->iv_rsn_ie != NULL)
2316 frm = add_ie(frm, vap->iv_rsn_ie);
2317 /* XXX else complain? */
2318 }
2319 /*
2320 * NB: legacy 11b clients do not get certain ie's.
2321 * The caller identifies such clients by passing
2322 * a token in legacy to us. Could expand this to be
2323 * any legacy client for stuff like HT ie's.
2324 */
2325 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2326 legacy != IEEE80211_SEND_LEGACY_11B) {
2327 frm = ieee80211_add_htcap(frm, bss);
2328 frm = ieee80211_add_htinfo(frm, bss);
2329 }
2330 if (vap->iv_flags & IEEE80211_F_WPA1) {
2331 if (vap->iv_wpa_ie != NULL)
2332 frm = add_ie(frm, vap->iv_wpa_ie);
2333 /* XXX else complain? */
2334 }
2335 if (vap->iv_flags & IEEE80211_F_WME)
2336 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2337 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2338 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2339 legacy != IEEE80211_SEND_LEGACY_11B) {
2340 frm = ieee80211_add_htcap_vendor(frm, bss);
2341 frm = ieee80211_add_htinfo_vendor(frm, bss);
2342 }
2343 #ifdef IEEE80211_SUPPORT_SUPERG
2344 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2345 legacy != IEEE80211_SEND_LEGACY_11B)
2346 frm = ieee80211_add_athcaps(frm, bss);
2347 #endif
2348 if (vap->iv_appie_proberesp != NULL)
2349 frm = add_appie(frm, vap->iv_appie_proberesp);
2350 #ifdef IEEE80211_SUPPORT_MESH
2351 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2352 frm = ieee80211_add_meshid(frm, vap);
2353 frm = ieee80211_add_meshconf(frm, vap);
2354 }
2355 #endif
2356 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2357
2358 return m;
2359 }
2360
2361 /*
2362 * Send a probe response frame to the specified mac address.
2363 * This does not go through the normal mgt frame api so we
2364 * can specify the destination address and re-use the bss node
2365 * for the sta reference.
2366 */
2367 int
2368 ieee80211_send_proberesp(struct ieee80211vap *vap,
2369 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2370 {
2371 struct ieee80211_node *bss = vap->iv_bss;
2372 struct ieee80211com *ic = vap->iv_ic;
2373 struct ieee80211_frame *wh;
2374 struct mbuf *m;
2375
2376 if (vap->iv_state == IEEE80211_S_CAC) {
2377 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2378 "block %s frame in CAC state", "probe response");
2379 vap->iv_stats.is_tx_badstate++;
2380 return EIO; /* XXX */
2381 }
2382
2383 /*
2384 * Hold a reference on the node so it doesn't go away until after
2385 * the xmit is complete all the way in the driver. On error we
2386 * will remove our reference.
2387 */
2388 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2389 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2390 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2391 ieee80211_node_refcnt(bss)+1);
2392 ieee80211_ref_node(bss);
2393
2394 m = ieee80211_alloc_proberesp(bss, legacy);
2395 if (m == NULL) {
2396 ieee80211_free_node(bss);
2397 return ENOMEM;
2398 }
2399
2400 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2401 KASSERT(m != NULL, ("no room for header"));
2402
2403 wh = mtod(m, struct ieee80211_frame *);
2404 ieee80211_send_setup(bss, m,
2405 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2406 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2407 /* XXX power management? */
2408 m->m_flags |= M_ENCAP; /* mark encapsulated */
2409
2410 M_WME_SETAC(m, WME_AC_BE);
2411
2412 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2413 "send probe resp on channel %u to %s%s\n",
2414 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2415 legacy ? " <legacy>" : "");
2416 IEEE80211_NODE_STAT(bss, tx_mgmt);
2417
2418 return ic->ic_raw_xmit(bss, m, NULL);
2419 }
2420
2421 /*
2422 * Allocate and build a RTS (Request To Send) control frame.
2423 */
2424 struct mbuf *
2425 ieee80211_alloc_rts(struct ieee80211com *ic,
2426 const uint8_t ra[IEEE80211_ADDR_LEN],
2427 const uint8_t ta[IEEE80211_ADDR_LEN],
2428 uint16_t dur)
2429 {
2430 struct ieee80211_frame_rts *rts;
2431 struct mbuf *m;
2432
2433 /* XXX honor ic_headroom */
2434 m = m_gethdr(M_DONTWAIT, MT_DATA);
2435 if (m != NULL) {
2436 rts = mtod(m, struct ieee80211_frame_rts *);
2437 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2438 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2439 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2440 *(u_int16_t *)rts->i_dur = htole16(dur);
2441 IEEE80211_ADDR_COPY(rts->i_ra, ra);
2442 IEEE80211_ADDR_COPY(rts->i_ta, ta);
2443
2444 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2445 }
2446 return m;
2447 }
2448
2449 /*
2450 * Allocate and build a CTS (Clear To Send) control frame.
2451 */
2452 struct mbuf *
2453 ieee80211_alloc_cts(struct ieee80211com *ic,
2454 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2455 {
2456 struct ieee80211_frame_cts *cts;
2457 struct mbuf *m;
2458
2459 /* XXX honor ic_headroom */
2460 m = m_gethdr(M_DONTWAIT, MT_DATA);
2461 if (m != NULL) {
2462 cts = mtod(m, struct ieee80211_frame_cts *);
2463 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2464 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2465 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2466 *(u_int16_t *)cts->i_dur = htole16(dur);
2467 IEEE80211_ADDR_COPY(cts->i_ra, ra);
2468
2469 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2470 }
2471 return m;
2472 }
2473
2474 static void
2475 ieee80211_tx_mgt_timeout(void *arg)
2476 {
2477 struct ieee80211_node *ni = arg;
2478 struct ieee80211vap *vap = ni->ni_vap;
2479
2480 if (vap->iv_state != IEEE80211_S_INIT &&
2481 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2482 /*
2483 * NB: it's safe to specify a timeout as the reason here;
2484 * it'll only be used in the right state.
2485 */
2486 ieee80211_new_state(vap, IEEE80211_S_SCAN,
2487 IEEE80211_SCAN_FAIL_TIMEOUT);
2488 }
2489 }
2490
2491 static void
2492 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2493 {
2494 struct ieee80211vap *vap = ni->ni_vap;
2495 enum ieee80211_state ostate = (enum ieee80211_state) arg;
2496
2497 /*
2498 * Frame transmit completed; arrange timer callback. If
2499 * transmit was successfuly we wait for response. Otherwise
2500 * we arrange an immediate callback instead of doing the
2501 * callback directly since we don't know what state the driver
2502 * is in (e.g. what locks it is holding). This work should
2503 * not be too time-critical and not happen too often so the
2504 * added overhead is acceptable.
2505 *
2506 * XXX what happens if !acked but response shows up before callback?
2507 */
2508 if (vap->iv_state == ostate)
2509 callout_reset(&vap->iv_mgtsend,
2510 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2511 ieee80211_tx_mgt_timeout, ni);
2512 }
2513
2514 static void
2515 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2516 struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
2517 {
2518 struct ieee80211vap *vap = ni->ni_vap;
2519 struct ieee80211com *ic = ni->ni_ic;
2520 struct ieee80211_rateset *rs = &ni->ni_rates;
2521 uint16_t capinfo;
2522
2523 /*
2524 * beacon frame format
2525 * [8] time stamp
2526 * [2] beacon interval
2527 * [2] cabability information
2528 * [tlv] ssid
2529 * [tlv] supported rates
2530 * [3] parameter set (DS)
2531 * [8] CF parameter set (optional)
2532 * [tlv] parameter set (IBSS/TIM)
2533 * [tlv] country (optional)
2534 * [3] power control (optional)
2535 * [5] channel switch announcement (CSA) (optional)
2536 * [tlv] extended rate phy (ERP)
2537 * [tlv] extended supported rates
2538 * [tlv] RSN parameters
2539 * [tlv] HT capabilities
2540 * [tlv] HT information
2541 * XXX Vendor-specific OIDs (e.g. Atheros)
2542 * [tlv] WPA parameters
2543 * [tlv] WME parameters
2544 * [tlv] Vendor OUI HT capabilities (optional)
2545 * [tlv] Vendor OUI HT information (optional)
2546 * [tlv] Atheros capabilities (optional)
2547 * [tlv] TDMA parameters (optional)
2548 * [tlv] Mesh ID (MBSS)
2549 * [tlv] Mesh Conf (MBSS)
2550 * [tlv] application data (optional)
2551 */
2552
2553 memset(bo, 0, sizeof(*bo));
2554
2555 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
2556 frm += 8;
2557 *(uint16_t *)frm = htole16(ni->ni_intval);
2558 frm += 2;
2559 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2560 bo->bo_caps = (uint16_t *)frm;
2561 *(uint16_t *)frm = htole16(capinfo);
2562 frm += 2;
2563 *frm++ = IEEE80211_ELEMID_SSID;
2564 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2565 *frm++ = ni->ni_esslen;
2566 memcpy(frm, ni->ni_essid, ni->ni_esslen);
2567 frm += ni->ni_esslen;
2568 } else
2569 *frm++ = 0;
2570 frm = ieee80211_add_rates(frm, rs);
2571 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2572 *frm++ = IEEE80211_ELEMID_DSPARMS;
2573 *frm++ = 1;
2574 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2575 }
2576 if (ic->ic_flags & IEEE80211_F_PCF) {
2577 bo->bo_cfp = frm;
2578 frm = ieee80211_add_cfparms(frm, ic);
2579 }
2580 bo->bo_tim = frm;
2581 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2582 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2583 *frm++ = 2;
2584 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2585 bo->bo_tim_len = 0;
2586 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2587 vap->iv_opmode == IEEE80211_M_MBSS) {
2588 /* TIM IE is the same for Mesh and Hostap */
2589 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2590
2591 tie->tim_ie = IEEE80211_ELEMID_TIM;
2592 tie->tim_len = 4; /* length */
2593 tie->tim_count = 0; /* DTIM count */
2594 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
2595 tie->tim_bitctl = 0; /* bitmap control */
2596 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
2597 frm += sizeof(struct ieee80211_tim_ie);
2598 bo->bo_tim_len = 1;
2599 }
2600 bo->bo_tim_trailer = frm;
2601 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2602 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2603 frm = ieee80211_add_countryie(frm, ic);
2604 if (vap->iv_flags & IEEE80211_F_DOTH) {
2605 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2606 frm = ieee80211_add_powerconstraint(frm, vap);
2607 bo->bo_csa = frm;
2608 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2609 frm = ieee80211_add_csa(frm, vap);
2610 } else
2611 bo->bo_csa = frm;
2612 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
2613 bo->bo_erp = frm;
2614 frm = ieee80211_add_erp(frm, ic);
2615 }
2616 frm = ieee80211_add_xrates(frm, rs);
2617 if (vap->iv_flags & IEEE80211_F_WPA2) {
2618 if (vap->iv_rsn_ie != NULL)
2619 frm = add_ie(frm, vap->iv_rsn_ie);
2620 /* XXX else complain */
2621 }
2622 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2623 frm = ieee80211_add_htcap(frm, ni);
2624 bo->bo_htinfo = frm;
2625 frm = ieee80211_add_htinfo(frm, ni);
2626 }
2627 if (vap->iv_flags & IEEE80211_F_WPA1) {
2628 if (vap->iv_wpa_ie != NULL)
2629 frm = add_ie(frm, vap->iv_wpa_ie);
2630 /* XXX else complain */
2631 }
2632 if (vap->iv_flags & IEEE80211_F_WME) {
2633 bo->bo_wme = frm;
2634 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2635 }
2636 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2637 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
2638 frm = ieee80211_add_htcap_vendor(frm, ni);
2639 frm = ieee80211_add_htinfo_vendor(frm, ni);
2640 }
2641 #ifdef IEEE80211_SUPPORT_SUPERG
2642 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
2643 bo->bo_ath = frm;
2644 frm = ieee80211_add_athcaps(frm, ni);
2645 }
2646 #endif
2647 #ifdef IEEE80211_SUPPORT_TDMA
2648 if (vap->iv_caps & IEEE80211_C_TDMA) {
2649 bo->bo_tdma = frm;
2650 frm = ieee80211_add_tdma(frm, vap);
2651 }
2652 #endif
2653 if (vap->iv_appie_beacon != NULL) {
2654 bo->bo_appie = frm;
2655 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2656 frm = add_appie(frm, vap->iv_appie_beacon);
2657 }
2658 #ifdef IEEE80211_SUPPORT_MESH
2659 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2660 frm = ieee80211_add_meshid(frm, vap);
2661 bo->bo_meshconf = frm;
2662 frm = ieee80211_add_meshconf(frm, vap);
2663 }
2664 #endif
2665 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2666 bo->bo_csa_trailer_len = frm - bo->bo_csa;
2667 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2668 }
2669
2670 /*
2671 * Allocate a beacon frame and fillin the appropriate bits.
2672 */
2673 struct mbuf *
2674 ieee80211_beacon_alloc(struct ieee80211_node *ni,
2675 struct ieee80211_beacon_offsets *bo)
2676 {
2677 struct ieee80211vap *vap = ni->ni_vap;
2678 struct ieee80211com *ic = ni->ni_ic;
2679 struct ifnet *ifp = vap->iv_ifp;
2680 struct ieee80211_frame *wh;
2681 struct mbuf *m;
2682 int pktlen;
2683 uint8_t *frm;
2684
2685 /*
2686 * beacon frame format
2687 * [8] time stamp
2688 * [2] beacon interval
2689 * [2] cabability information
2690 * [tlv] ssid
2691 * [tlv] supported rates
2692 * [3] parameter set (DS)
2693 * [8] CF parameter set (optional)
2694 * [tlv] parameter set (IBSS/TIM)
2695 * [tlv] country (optional)
2696 * [3] power control (optional)
2697 * [5] channel switch announcement (CSA) (optional)
2698 * [tlv] extended rate phy (ERP)
2699 * [tlv] extended supported rates
2700 * [tlv] RSN parameters
2701 * [tlv] HT capabilities
2702 * [tlv] HT information
2703 * [tlv] Vendor OUI HT capabilities (optional)
2704 * [tlv] Vendor OUI HT information (optional)
2705 * XXX Vendor-specific OIDs (e.g. Atheros)
2706 * [tlv] WPA parameters
2707 * [tlv] WME parameters
2708 * [tlv] TDMA parameters (optional)
2709 * [tlv] Mesh ID (MBSS)
2710 * [tlv] Mesh Conf (MBSS)
2711 * [tlv] application data (optional)
2712 * NB: we allocate the max space required for the TIM bitmap.
2713 * XXX how big is this?
2714 */
2715 pktlen = 8 /* time stamp */
2716 + sizeof(uint16_t) /* beacon interval */
2717 + sizeof(uint16_t) /* capabilities */
2718 + 2 + ni->ni_esslen /* ssid */
2719 + 2 + IEEE80211_RATE_SIZE /* supported rates */
2720 + 2 + 1 /* DS parameters */
2721 + 2 + 6 /* CF parameters */
2722 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
2723 + IEEE80211_COUNTRY_MAX_SIZE /* country */
2724 + 2 + 1 /* power control */
2725 + sizeof(struct ieee80211_csa_ie) /* CSA */
2726 + 2 + 1 /* ERP */
2727 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2728 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
2729 2*sizeof(struct ieee80211_ie_wpa) : 0)
2730 /* XXX conditional? */
2731 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2732 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2733 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
2734 sizeof(struct ieee80211_wme_param) : 0)
2735 #ifdef IEEE80211_SUPPORT_SUPERG
2736 + sizeof(struct ieee80211_ath_ie) /* ATH */
2737 #endif
2738 #ifdef IEEE80211_SUPPORT_TDMA
2739 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
2740 sizeof(struct ieee80211_tdma_param) : 0)
2741 #endif
2742 #ifdef IEEE80211_SUPPORT_MESH
2743 + 2 + ni->ni_meshidlen
2744 + sizeof(struct ieee80211_meshconf_ie)
2745 #endif
2746 + IEEE80211_MAX_APPIE
2747 ;
2748 m = ieee80211_getmgtframe(&frm,
2749 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2750 if (m == NULL) {
2751 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2752 "%s: cannot get buf; size %u\n", __func__, pktlen);
2753 vap->iv_stats.is_tx_nobuf++;
2754 return NULL;
2755 }
2756 ieee80211_beacon_construct(m, frm, bo, ni);
2757
2758 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2759 KASSERT(m != NULL, ("no space for 802.11 header?"));
2760 wh = mtod(m, struct ieee80211_frame *);
2761 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2762 IEEE80211_FC0_SUBTYPE_BEACON;
2763 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2764 *(uint16_t *)wh->i_dur = 0;
2765 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2766 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
2767 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2768 *(uint16_t *)wh->i_seq = 0;
2769
2770 return m;
2771 }
2772
2773 /*
2774 * Update the dynamic parts of a beacon frame based on the current state.
2775 */
2776 int
2777 ieee80211_beacon_update(struct ieee80211_node *ni,
2778 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
2779 {
2780 struct ieee80211vap *vap = ni->ni_vap;
2781 struct ieee80211com *ic = ni->ni_ic;
2782 int len_changed = 0;
2783 uint16_t capinfo;
2784
2785 IEEE80211_LOCK(ic);
2786 /*
2787 * Handle 11h channel change when we've reached the count.
2788 * We must recalculate the beacon frame contents to account
2789 * for the new channel. Note we do this only for the first
2790 * vap that reaches this point; subsequent vaps just update
2791 * their beacon state to reflect the recalculated channel.
2792 */
2793 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
2794 vap->iv_csa_count == ic->ic_csa_count) {
2795 vap->iv_csa_count = 0;
2796 /*
2797 * Effect channel change before reconstructing the beacon
2798 * frame contents as many places reference ni_chan.
2799 */
2800 if (ic->ic_csa_newchan != NULL)
2801 ieee80211_csa_completeswitch(ic);
2802 /*
2803 * NB: ieee80211_beacon_construct clears all pending
2804 * updates in bo_flags so we don't need to explicitly
2805 * clear IEEE80211_BEACON_CSA.
2806 */
2807 ieee80211_beacon_construct(m,
2808 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
2809
2810 /* XXX do WME aggressive mode processing? */
2811 IEEE80211_UNLOCK(ic);
2812 return 1; /* just assume length changed */
2813 }
2814
2815 /* XXX faster to recalculate entirely or just changes? */
2816 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2817 *bo->bo_caps = htole16(capinfo);
2818
2819 if (vap->iv_flags & IEEE80211_F_WME) {
2820 struct ieee80211_wme_state *wme = &ic->ic_wme;
2821
2822 /*
2823 * Check for agressive mode change. When there is
2824 * significant high priority traffic in the BSS
2825 * throttle back BE traffic by using conservative
2826 * parameters. Otherwise BE uses agressive params
2827 * to optimize performance of legacy/non-QoS traffic.
2828 */
2829 if (wme->wme_flags & WME_F_AGGRMODE) {
2830 if (wme->wme_hipri_traffic >
2831 wme->wme_hipri_switch_thresh) {
2832 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2833 "%s: traffic %u, disable aggressive mode\n",
2834 __func__, wme->wme_hipri_traffic);
2835 wme->wme_flags &= ~WME_F_AGGRMODE;
2836 ieee80211_wme_updateparams_locked(vap);
2837 wme->wme_hipri_traffic =
2838 wme->wme_hipri_switch_hysteresis;
2839 } else
2840 wme->wme_hipri_traffic = 0;
2841 } else {
2842 if (wme->wme_hipri_traffic <=
2843 wme->wme_hipri_switch_thresh) {
2844 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2845 "%s: traffic %u, enable aggressive mode\n",
2846 __func__, wme->wme_hipri_traffic);
2847 wme->wme_flags |= WME_F_AGGRMODE;
2848 ieee80211_wme_updateparams_locked(vap);
2849 wme->wme_hipri_traffic = 0;
2850 } else
2851 wme->wme_hipri_traffic =
2852 wme->wme_hipri_switch_hysteresis;
2853 }
2854 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
2855 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
2856 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
2857 }
2858 }
2859
2860 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
2861 ieee80211_ht_update_beacon(vap, bo);
2862 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
2863 }
2864 #ifdef IEEE80211_SUPPORT_TDMA
2865 if (vap->iv_caps & IEEE80211_C_TDMA) {
2866 /*
2867 * NB: the beacon is potentially updated every TBTT.
2868 */
2869 ieee80211_tdma_update_beacon(vap, bo);
2870 }
2871 #endif
2872 #ifdef IEEE80211_SUPPORT_MESH
2873 if (vap->iv_opmode == IEEE80211_M_MBSS)
2874 ieee80211_mesh_update_beacon(vap, bo);
2875 #endif
2876
2877 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2878 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
2879 struct ieee80211_tim_ie *tie =
2880 (struct ieee80211_tim_ie *) bo->bo_tim;
2881 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
2882 u_int timlen, timoff, i;
2883 /*
2884 * ATIM/DTIM needs updating. If it fits in the
2885 * current space allocated then just copy in the
2886 * new bits. Otherwise we need to move any trailing
2887 * data to make room. Note that we know there is
2888 * contiguous space because ieee80211_beacon_allocate
2889 * insures there is space in the mbuf to write a
2890 * maximal-size virtual bitmap (based on iv_max_aid).
2891 */
2892 /*
2893 * Calculate the bitmap size and offset, copy any
2894 * trailer out of the way, and then copy in the
2895 * new bitmap and update the information element.
2896 * Note that the tim bitmap must contain at least
2897 * one byte and any offset must be even.
2898 */
2899 if (vap->iv_ps_pending != 0) {
2900 timoff = 128; /* impossibly large */
2901 for (i = 0; i < vap->iv_tim_len; i++)
2902 if (vap->iv_tim_bitmap[i]) {
2903 timoff = i &~ 1;
2904 break;
2905 }
2906 KASSERT(timoff != 128, ("tim bitmap empty!"));
2907 for (i = vap->iv_tim_len-1; i >= timoff; i--)
2908 if (vap->iv_tim_bitmap[i])
2909 break;
2910 timlen = 1 + (i - timoff);
2911 } else {
2912 timoff = 0;
2913 timlen = 1;
2914 }
2915 if (timlen != bo->bo_tim_len) {
2916 /* copy up/down trailer */
2917 int adjust = tie->tim_bitmap+timlen
2918 - bo->bo_tim_trailer;
2919 ovbcopy(bo->bo_tim_trailer,
2920 bo->bo_tim_trailer+adjust,
2921 bo->bo_tim_trailer_len);
2922 bo->bo_tim_trailer += adjust;
2923 bo->bo_erp += adjust;
2924 bo->bo_htinfo += adjust;
2925 #ifdef IEEE80211_SUPERG_SUPPORT
2926 bo->bo_ath += adjust;
2927 #endif
2928 #ifdef IEEE80211_TDMA_SUPPORT
2929 bo->bo_tdma += adjust;
2930 #endif
2931 #ifdef IEEE80211_MESH_SUPPORT
2932 bo->bo_meshconf += adjust;
2933 #endif
2934 bo->bo_appie += adjust;
2935 bo->bo_wme += adjust;
2936 bo->bo_csa += adjust;
2937 bo->bo_tim_len = timlen;
2938
2939 /* update information element */
2940 tie->tim_len = 3 + timlen;
2941 tie->tim_bitctl = timoff;
2942 len_changed = 1;
2943 }
2944 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
2945 bo->bo_tim_len);
2946
2947 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
2948
2949 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
2950 "%s: TIM updated, pending %u, off %u, len %u\n",
2951 __func__, vap->iv_ps_pending, timoff, timlen);
2952 }
2953 /* count down DTIM period */
2954 if (tie->tim_count == 0)
2955 tie->tim_count = tie->tim_period - 1;
2956 else
2957 tie->tim_count--;
2958 /* update state for buffered multicast frames on DTIM */
2959 if (mcast && tie->tim_count == 0)
2960 tie->tim_bitctl |= 1;
2961 else
2962 tie->tim_bitctl &= ~1;
2963 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
2964 struct ieee80211_csa_ie *csa =
2965 (struct ieee80211_csa_ie *) bo->bo_csa;
2966
2967 /*
2968 * Insert or update CSA ie. If we're just starting
2969 * to count down to the channel switch then we need
2970 * to insert the CSA ie. Otherwise we just need to
2971 * drop the count. The actual change happens above
2972 * when the vap's count reaches the target count.
2973 */
2974 if (vap->iv_csa_count == 0) {
2975 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
2976 bo->bo_erp += sizeof(*csa);
2977 bo->bo_htinfo += sizeof(*csa);
2978 bo->bo_wme += sizeof(*csa);
2979 #ifdef IEEE80211_SUPERG_SUPPORT
2980 bo->bo_ath += sizeof(*csa);
2981 #endif
2982 #ifdef IEEE80211_TDMA_SUPPORT
2983 bo->bo_tdma += sizeof(*csa);
2984 #endif
2985 #ifdef IEEE80211_MESH_SUPPORT
2986 bo->bo_meshconf += sizeof(*csa);
2987 #endif
2988 bo->bo_appie += sizeof(*csa);
2989 bo->bo_csa_trailer_len += sizeof(*csa);
2990 bo->bo_tim_trailer_len += sizeof(*csa);
2991 m->m_len += sizeof(*csa);
2992 m->m_pkthdr.len += sizeof(*csa);
2993
2994 ieee80211_add_csa(bo->bo_csa, vap);
2995 } else
2996 csa->csa_count--;
2997 vap->iv_csa_count++;
2998 /* NB: don't clear IEEE80211_BEACON_CSA */
2999 }
3000 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3001 /*
3002 * ERP element needs updating.
3003 */
3004 (void) ieee80211_add_erp(bo->bo_erp, ic);
3005 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3006 }
3007 #ifdef IEEE80211_SUPPORT_SUPERG
3008 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3009 ieee80211_add_athcaps(bo->bo_ath, ni);
3010 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3011 }
3012 #endif
3013 }
3014 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3015 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3016 int aielen;
3017 uint8_t *frm;
3018
3019 aielen = 0;
3020 if (aie != NULL)
3021 aielen += aie->ie_len;
3022 if (aielen != bo->bo_appie_len) {
3023 /* copy up/down trailer */
3024 int adjust = aielen - bo->bo_appie_len;
3025 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3026 bo->bo_tim_trailer_len);
3027 bo->bo_tim_trailer += adjust;
3028 bo->bo_appie += adjust;
3029 bo->bo_appie_len = aielen;
3030
3031 len_changed = 1;
3032 }
3033 frm = bo->bo_appie;
3034 if (aie != NULL)
3035 frm = add_appie(frm, aie);
3036 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3037 }
3038 IEEE80211_UNLOCK(ic);
3039
3040 return len_changed;
3041 }
Cache object: 3e5d065cb16f982524bcff190c507208
|